{"record":{"id":"3d674acdef418244","repo":"linshenkx/prompt-optimizer","slug":"target-prompt-is-required","errorCode":null,"errorMessage":"Target prompt is required","messagePattern":"Target prompt is required","errorType":"validation","errorClass":"OptimizationError","httpStatus":null,"severity":"error","filePath":"packages/core/src/services/prompt/service.ts","lineNumber":849,"sourceCode":"    } catch (error) {\n      const errorMessage =\n        error instanceof Error ? error.message : String(error);\n      throw new IterationError(\n        originalPrompt,\n        iterateInput,\n        `Iteration failed: ${errorMessage}`,\n      );\n    }\n  }\n\n  // === 新增：支持提示词类型的增强方法 ===\n\n  /**\n   * 验证优化请求参数\n   */\n  private validateOptimizationRequest(request: OptimizationRequest) {\n    if (!request.targetPrompt?.trim()) {\n      throw new OptimizationError(\"\", \"Target prompt is required\");\n    }\n    if (!request.modelKey?.trim()) {\n      throw new OptimizationError(request.targetPrompt, \"Model key is required\");\n    }\n  }\n\n  private hasInputImages(request: OptimizationRequest): request is OptimizationRequest & { inputImages: NonNullable<OptimizationRequest[\"inputImages\"]> } {\n    return Array.isArray(request.inputImages) && request.inputImages.length > 0;\n  }\n\n  private hasTestInputImages(inputImages?: ImageInputRef[]): inputImages is ImageInputRef[] {\n    return Array.isArray(inputImages) && inputImages.length > 0;\n  }\n\n  private getSafeTestErrorMessage(error: unknown, inputImages?: ImageInputRef[]): string {\n    let message = error instanceof Error ? error.message : String(error);\n    if (!this.hasTestInputImages(inputImages)) {\n      return message;","sourceCodeStart":831,"sourceCodeEnd":867,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/prompt/service.ts#L831-L867","documentation":"Optimization requests must include a non-empty targetPrompt; validateOptimizationRequest throws OptimizationError when targetPrompt is missing, empty, or only whitespace. This guard runs before any model or template work in both optimizePrompt and optimizePromptStream.","triggerScenarios":"Calling optimizePrompt/optimizePromptStream with targetPrompt undefined, '', or a string of spaces/newlines; e.g. submitting the optimize form before typing a prompt.","commonSituations":"UI form submitted before user input; programmatic callers passing an object built from empty state; trailing-whitespace-only strings from copy/paste.","solutions":["Validate and trim targetPrompt before calling the API","Disable the optimize action in the UI until a non-empty prompt is entered","Log the request payload shape in tests to catch missing fields early"],"exampleFix":"// before\nawait service.optimizePrompt({ targetPrompt: '   ', modelKey });\n\n// after\nconst target = targetPrompt?.trim();\nif (!target) throw new Error('Enter a prompt to optimize');\nawait service.optimizePrompt({ targetPrompt: target, modelKey });","handlingStrategy":"validation","validationCode":"const target = request.targetPrompt?.trim() ?? '';\nif (!target) throw new Error('targetPrompt is required');","typeGuard":"const hasTargetPrompt = (\n  r: Partial<OptimizationRequest>\n): r is OptimizationRequest =>\n  typeof r.targetPrompt === 'string' && r.targetPrompt.trim().length > 0;","tryCatchPattern":"try {\n  await svc.optimizePrompt(req);\n} catch (e) {\n  if (e instanceof OptimizationError && e.message === 'Target prompt is required') {\n    // show empty-input validation message to user\n  } else throw e;\n}","preventionTips":["Trim and check required string fields before calling optimize APIs","Disable submit actions until required inputs are non-empty","Write unit tests covering empty/whitespace request fields"],"tags":["validation","required-field","optimization"],"backgroundTag":"missing-required-parameter","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}