{"record":{"id":"dc692f646fde729c","repo":"linshenkx/prompt-optimizer","slug":"no-templates-available-for-type-templatetype","errorCode":null,"errorMessage":"No templates available for type: ${templateType}","messagePattern":"No templates available for type: (.+?)","errorType":"exception","errorClass":"ServiceDependencyError","httpStatus":null,"severity":"critical","filePath":"packages/core/src/services/prompt/service.ts","lineNumber":1081,"sourceCode":"          return fallbackTemplates[0].id;\n        }\n      }\n\n      // 最后的回退：获取所有模板中第一个可用的内置模板\n      const allTemplates = await this.templateManager.listTemplates();\n      const availableTemplate = allTemplates.find((t) => t.isBuiltin);\n      if (availableTemplate) {\n        console.warn(\n          `Using fallback builtin template: ${availableTemplate.id} for type ${templateType}`,\n        );\n        return availableTemplate.id;\n      }\n    } catch (fallbackError) {\n      console.error(`Fallback template search failed:`, fallbackError);\n    }\n\n    // 如果所有方法都失败，抛出错误\n    throw new ServiceDependencyError('TemplateManager', `No templates available for type: ${templateType}`);\n  }\n\n  // saveOptimizationHistory 方法已移除\n  // 历史记录保存现在由UI层的historyManager.createNewChain方法处理\n\n  // saveTestHistory 方法已移除\n  // 测试功能不再保存历史记录，保持架构一致性\n  // 测试是临时性验证，不应与优化历史记录混合\n\n  // 注意：迭代历史记录由UI层管理，而非核心服务层\n  // 原因：\n  // 1. 迭代需要现有的chainId，这个信息由UI层的状态管理器维护\n  // 2. 迭代与用户交互紧密结合，需要实时更新UI状态\n  // 3. 版本管理逻辑在UI层更容易处理\n  //\n  // 相比之下，优化操作会创建新的链，所以可以在核心层处理\n  // 这种混合架构是经过权衡的设计决策\n","sourceCodeStart":1063,"sourceCodeEnd":1099,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/prompt/service.ts#L1063-L1099","documentation":"getDefaultTemplateId tries several strategies to find a default template for a type; if all lookups fail (including a fallback search), it throws ServiceDependencyError stating no templates exist for that type. This surfaces whenever template resolution needs a default that isn't present.","triggerScenarios":"Requesting a default template for 'optimize', 'userOptimize', or 'iterate' when the template store contains zero templates of that type — e.g. empty database/storage or templates with mismatched type tags.","commonSituations":"First run without seeding built-in templates; storage wiped or reset; templates persisted under a different type key after a version migration; filtered/paginated query bugs returning empty result sets.","solutions":["Seed/import the built-in default templates for the required types","Inspect stored templates and confirm their type field matches what the service queries ('optimize', 'userOptimize', 'iterate')","Check the fallback search error logged to console — the underlying query failure explains why nothing was found","Repair or reset template storage if data is corrupted"],"exampleFix":null,"handlingStrategy":"fallback","validationCode":"const templates = await templateManager.listTemplates();\nif (!templates.some(t => t.type === 'optimize')) {\n  await seedDefaultTemplates();\n}","typeGuard":"const hasTemplatesForType = (\n  list: { type: string }[], type: string\n): boolean => list.some(t => t.type === type);","tryCatchPattern":"try {\n  const id = await svc.getDefaultTemplateId('optimize');\n} catch (e) {\n  if (e instanceof ServiceDependencyError && /No templates available/.test(e.message)) {\n    await seedDefaultTemplates();\n    return svc.getDefaultTemplateId('optimize');\n  }\n  throw e;\n}","preventionTips":["Run template seeding as part of app initialization and assert it succeeded","Back up template storage before migrations","Add a smoke test that queries one template of each required type on boot"],"tags":["template","seed-data","storage"],"backgroundTag":"no-default-template-available","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}