{"record":{"id":"d4d7d8e3a9852a84","repo":"linshenkx/prompt-optimizer","slug":"user-prompt-is-required","errorCode":null,"errorMessage":"User prompt is required","messagePattern":"User prompt is required","errorType":"validation","errorClass":"TestError","httpStatus":null,"severity":"warning","filePath":"packages/core/src/services/prompt/service.ts","lineNumber":392,"sourceCode":"        iterateInput,\n        `Iteration failed: ${errorMessage}`,\n      );\n    }\n  }\n\n  /**\n   * 测试提示词 - 支持可选系统提示词\n   */\n  async testPrompt(\n    systemPrompt: string,\n    userPrompt: string,\n    modelKey: string,\n    inputImages?: ImageInputRef[],\n  ): Promise<string> {\n    try {\n      // 对于用户提示词优化，systemPrompt 可以为空\n      if (!userPrompt?.trim()) {\n        throw new TestError(systemPrompt, userPrompt, \"User prompt is required\");\n      }\n      if (!modelKey?.trim()) {\n        throw new TestError(systemPrompt, userPrompt, \"Model key is required\");\n      }\n\n      const modelConfig = await this.modelManager.getModel(modelKey);\n      if (!modelConfig) {\n        throw new TestError(systemPrompt, userPrompt, \"Model not found\");\n      }\n\n      if (this.hasTestInputImages(inputImages)) {\n        const result = await this.requireImageUnderstandingService().understand({\n          modelConfig,\n          systemPrompt: systemPrompt?.trim() ? systemPrompt : undefined,\n          userPrompt,\n          images: inputImages,\n        });\n","sourceCodeStart":374,"sourceCodeEnd":410,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/prompt/service.ts#L374-L410","documentation":"testPrompt validates its inputs before running: for user-prompt testing the systemPrompt may be empty, but userPrompt must be a non-empty, non-whitespace string. A blank userPrompt aborts immediately with TestError 'User prompt is required'.","triggerScenarios":"Calling testPrompt(systemPrompt, \"\", modelKey), testPrompt(systemPrompt, \"   \", modelKey), or passing undefined/null userPrompt (e.g. an unbound form field in the UI before the user typed anything).","commonSituations":"Testing a submit handler before validation fires; state where the prompt textarea is still empty; trimming user input upstream and passing the trimmed-empty value downstream.","solutions":["Ensure the caller validates that userPrompt contains non-whitespace text before invoking testPrompt","Trim input in the UI/form layer and disable the test action while the prompt is blank","Default or reconstruct the userPrompt if it legitimately can be empty in your flow","Add a unit assertion for non-empty userPrompt in calling code paths"],"exampleFix":"// before\nawait promptService.testPrompt(systemPrompt, userPrompt, modelKey); // userPrompt may be \"\"\n\n// after\nconst trimmed = userPrompt?.trim();\nif (!trimmed) throw new Error(\"Type a user prompt before testing.\");\nawait promptService.testPrompt(systemPrompt, trimmed, modelKey);","handlingStrategy":"validation","validationCode":"if (!userPrompt?.trim()) {\n  throw new Error(\"userPrompt must be non-empty before testing\");\n}\nawait promptService.testPrompt(systemPrompt, userPrompt.trim(), modelKey);","typeGuard":"const isNonEmptyPrompt = (p?: string | null): p is string =>\n  typeof p === \"string\" && p.trim().length > 0;","tryCatchPattern":"null","preventionTips":["Disable test actions until the prompt field has content","Trim input in form layers before service calls","Add unit tests asserting blank prompts are rejected upstream"],"tags":["validation","input-validation","test-prompt","required-field"],"backgroundTag":"required-input-missing","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}