{"record":{"id":"377a50f129710b1d","repo":"linshenkx/prompt-optimizer","slug":"variablevaluegenerationmodelerror-modelkey","errorCode":null,"errorMessage":"VariableValueGenerationModelError(modelKey)","messagePattern":"VariableValueGenerationModelError\\(modelKey\\)","errorType":"exception","errorClass":"VariableValueGenerationModelError","httpStatus":null,"severity":"error","filePath":"packages/core/src/services/variable-value-generation/service.ts","lineNumber":103,"sourceCode":"      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   */\n  private async validateModel(modelKey: string): Promise<void> {\n    const model = await this.modelManager.getModel(modelKey);\n    if (!model) {\n      throw new VariableValueGenerationModelError(modelKey);\n    }\n  }\n\n  /**\n   * 获取变量值生成模板\n   */\n  private async getGenerationTemplate(): Promise<Template> {\n    const templateId = 'variable-value-generation';\n\n    try {\n      const template = await this.templateManager.getTemplate(templateId);\n      if (!template?.content) {\n        throw new VariableValueGenerationExecutionError(`Template \"${templateId}\" not found or empty.`);\n      }\n      return template;\n    } catch (error) {\n      if (error instanceof VariableValueGenerationError) {\n        throw error","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/variable-value-generation/service.ts#L85-L121","documentation":"generate() calls validateModel which looks the key up via modelManager.getModel(modelKey); if the manager returns nothing, VariableValueGenerationModelError is thrown. The key passed validation (non-empty string) but no model with that key is registered.","triggerScenarios":"generate({ ..., generationModelKey: 'my-model' }) when 'my-model' was never added to (or was removed from) the model manager's registry.","commonSituations":"Model was deleted or renamed by the user after the request was built; models stored in a config/database that differs between environments (dev vs prod); key casing or whitespace mismatch; the model manager was initialized with a different storage path; fresh install with no models configured yet.","solutions":["Call modelManager.getModel(key) (or list all models) before generate to verify the key exists","Register the missing model via the model manager, or fix the key to match an existing model's exact id","Persist and reload the model registry consistently across app restarts/environments","Catch VariableValueGenerationModelError and prompt the user to pick an available model"],"exampleFix":"// before\nawait gen.generate({ ...req, generationModelKey: 'gpt-4o' });\n\n// after\nconst model = await modelManager.getModel('gpt-4o');\nif (!model) {\n  const available = await modelManager.listModels();\n  throw new Error(`Model not found. Available: ${available.map(m => m.key).join(', ')}`);\n}\nawait gen.generate({ ...req, generationModelKey: 'gpt-4o' });","handlingStrategy":"validation","validationCode":"const model = await modelManager.getModel(key);\nif (!model) { const available = await modelManager.listModels(); throw new Error(`Unknown model ${key}. Available: ${available.map(m => m.key).join(', ')}`); }","typeGuard":null,"tryCatchPattern":"catch (e) { if (e instanceof VariableValueGenerationModelError) { /* re-prompt user to select a valid model */ } throw e; }","preventionTips":["Validate model keys against the manager's registry before any generate() call","Keep model configuration persisted and re-validated on startup"],"tags":["model-config","model-not-found","variable-value-generation"],"backgroundTag":"model-not-found","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}