{"record":{"id":"4e14601dfca90868","repo":"linshenkx/prompt-optimizer","slug":"at-least-one-message-is-required","errorCode":null,"errorMessage":"At least one message is required","messagePattern":"At least one message is required","errorType":"validation","errorClass":"TestError","httpStatus":null,"severity":"error","filePath":"packages/core/src/services/prompt/service.ts","lineNumber":1113,"sourceCode":"  // 3. 版本管理逻辑在UI层更容易处理\n  //\n  // 相比之下，优化操作会创建新的链，所以可以在核心层处理\n  // 这种混合架构是经过权衡的设计决策\n\n  /**\n   * 自定义会话测试（流式）- 高级模式功能\n   */\n  async testCustomConversationStream(\n    request: CustomConversationRequest,\n    callbacks: StreamHandlers,\n  ): Promise<void> {\n    try {\n      // 验证请求\n      if (!request.modelKey?.trim()) {\n        throw new TestError(\"\", \"\", \"Model key is required\");\n      }\n      if (!request.messages || request.messages.length === 0) {\n        throw new TestError(\"\", \"\", \"At least one message is required\");\n      }\n\n      // 验证模型存在\n      const modelConfig = await this.modelManager.getModel(request.modelKey);\n      if (!modelConfig) {\n        throw new TestError(\"\", \"\", \"Model not found\");\n      }\n\n      // 处理会话消息：替换变量\n      const processedMessages = TemplateProcessor.processConversationMessages(\n        request.messages,\n        request.variables,\n      );\n\n      if (processedMessages.length === 0) {\n        throw new TestError(\"\", \"\", \"No valid messages after processing\");\n      }\n","sourceCodeStart":1095,"sourceCodeEnd":1131,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/prompt/service.ts#L1095-L1131","documentation":"testCustomConversationStream requires at least one entry in request.messages; an empty or missing array throws TestError 'At least one message is required'. The custom conversation test replays a message list, so an empty list has nothing to send.","triggerScenarios":"Calling testCustomConversationStream with messages: [], messages: undefined, or a messages array that was filtered down to zero entries.","commonSituations":"Chat editor state cleared before Test clicked; message draft not yet appended to the conversation array; serialization dropping empty-ish message collections.","solutions":["Ensure at least one message (typically the user's draft) is appended before invoking the test","Guard in the UI: disable Test when the conversation list is empty","Construct the request only after pushing the current draft message"],"exampleFix":"// before\nawait service.testCustomConversationStream({ modelKey, messages: [] }, handlers);\n\n// after\nconst messages = [...conversation, { role: 'user', content: draft }];\nif (messages.length === 0) return;\nawait service.testCustomConversationStream({ modelKey, messages }, handlers);","handlingStrategy":"validation","validationCode":"const messages = [...conversation];\nif (messages.length === 0) throw new Error('Add at least one message before testing');","typeGuard":"const hasMessages = (\n  r: { messages?: unknown[] }\n): r is { messages: unknown[] } =>\n  Array.isArray(r.messages) && r.messages.length > 0;","tryCatchPattern":"try {\n  await svc.testCustomConversationStream(req, handlers);\n} catch (e) {\n  if (e instanceof TestError && e.message === 'At least one message is required') {\n    // notify user to write a message first\n  } else throw e;\n}","preventionTips":["Append the current draft to the conversation before invoking the test","Disable Test when the message list is empty","Build the request object only after required arrays are populated"],"tags":["validation","required-field","testing","messages"],"backgroundTag":"missing-required-parameter","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}