{"record":{"id":"135c6e65ff104e06","repo":"linshenkx/prompt-optimizer","slug":"generation-model-key-must-not-be-empty","errorCode":null,"errorMessage":"Generation model key must not be empty.","messagePattern":"Generation model key must not be empty\\.","errorType":"validation","errorClass":"VariableValueGenerationValidationError","httpStatus":null,"severity":"error","filePath":"packages/core/src/services/variable-value-generation/service.ts","lineNumber":81,"sourceCode":"    } catch (error) {\n      // 🔧 修复：保留原始错误类型，不要过度包装\n      if (error instanceof VariableValueGenerationError) {\n        throw error;\n      }\n      throw new VariableValueGenerationExecutionError(error instanceof Error ? error.message : String(error))\n    }\n  }\n\n  /**\n   * 验证请求参数\n   */\n  private validateRequest(request: VariableValueGenerationRequest): void {\n    if (!request.promptContent?.trim()) {\n      throw new VariableValueGenerationValidationError('Prompt content must not be empty.');\n    }\n\n    if (!request.generationModelKey?.trim()) {\n      throw new VariableValueGenerationValidationError('Generation model key must not be empty.');\n    }\n\n    if (!request.variables || request.variables.length === 0) {\n      throw new VariableValueGenerationValidationError('Variables list must not be empty.');\n    }\n\n    // 验证每个变量\n    for (let i = 0; i < request.variables.length; i++) {\n      const variable = request.variables[i];\n      if (!variable.name?.trim()) {\n        throw new VariableValueGenerationValidationError(`Variable at index ${i} has empty name.`);\n      }\n    }\n  }\n\n  /**\n   * 验证模型是否存在\n   */","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/variable-value-generation/service.ts#L63-L99","documentation":"generate() requires a non-empty generationModelKey identifying which configured model to use for value generation. validateRequest throws VariableValueGenerationValidationError when the key is missing or whitespace, because there is no model to invoke.","triggerScenarios":"generate({ promptContent: '...', generationModelKey: '', variables: [...] }) or the key is undefined because the request object was built without it.","commonSituations":"Model selection dropdown defaulting to empty in the UI; key read from settings/config that was never persisted; property renamed (model vs generationModelKey) after a library upgrade; passing the model object instead of its key.","solutions":["Pass a valid model key that is registered in the model manager (verify with modelManager.getModel(key))","Default the key from your app's settings before calling generate","Check for property-name mismatches after upgrading the library's request type"],"exampleFix":"// before\nawait gen.generate({ promptContent, variables, generationModelKey: selectedModel?.key }); // may be undefined\n\n// after\nconst key = selectedModel?.key ?? DEFAULT_GENERATION_MODEL_KEY;\nif (!key) throw new Error('Select a generation model first');\nawait gen.generate({ promptContent, variables, generationModelKey: key });","handlingStrategy":"validation","validationCode":"if (!req.generationModelKey?.trim()) throw new Error('generationModelKey required');\nconst model = await modelManager.getModel(req.generationModelKey);\nif (!model) throw new Error('unknown model key');","typeGuard":"const hasModelKey = (r: VariableValueGenerationRequest) => typeof r.generationModelKey === 'string' && r.generationModelKey.trim().length > 0;","tryCatchPattern":"catch (e) { if (e instanceof VariableValueGenerationValidationError && /model key/i.test(e.message)) return promptModelSelection(); throw e; }","preventionTips":["Default the model key from app settings; never pass optional-chained values straight into generate()","Verify the key exists in the model manager during app startup"],"tags":["request-validation","model-config","variable-value-generation"],"backgroundTag":"missing-model-key","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}