{"record":{"id":"a42d51488f7354fe","repo":"linshenkx/prompt-optimizer","slug":"data-error-codes-electron-api-unavailable","errorCode":"DATA_ERROR_CODES.ELECTRON_API_UNAVAILABLE","errorMessage":"ElectronDataManagerProxy can only be used in Electron renderer process","messagePattern":"ElectronDataManagerProxy can only be used in Electron renderer process","errorType":"error_code","errorClass":"DataError","httpStatus":null,"severity":"warning","filePath":"packages/core/src/services/data/electron-proxy.ts","lineNumber":16,"sourceCode":"import { IDataManager } from './types';\nimport { DataError } from './errors';\nimport { DATA_ERROR_CODES } from '../../constants/error-codes';\nimport { safeSerializeForIPC } from '../../utils/ipc-serialization';\n\n/**\n * Electron环境下的DataManager代理\n * 通过IPC调用主进程中的真实DataManager实例\n */\nexport class ElectronDataManagerProxy implements IDataManager {\n  private electronAPI: any;\n\n  constructor() {\n    // 验证Electron环境\n    if (typeof window === 'undefined' || !(window as any).electronAPI) {\n      throw new DataError(\n        DATA_ERROR_CODES.ELECTRON_API_UNAVAILABLE,\n        'ElectronDataManagerProxy can only be used in Electron renderer process',\n      );\n    }\n    this.electronAPI = (window as any).electronAPI;\n  }\n\n  async exportAllData(): Promise<string> {\n    return this.electronAPI.data.exportAllData();\n  }\n\n  async importAllData(dataString: string): Promise<void> {\n    await this.electronAPI.data.importAllData(safeSerializeForIPC(dataString));\n  }\n} \n","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/data/electron-proxy.ts#L1-L32","documentation":"OptimizationError thrown by validateMessageOptimizationRequest when request.messages is null, undefined, or a zero-length array. The optimizer needs the surrounding conversation context, so an empty message list is rejected before any LLM call.","triggerScenarios":"Calling optimizeMessage with messages: [] or omitting the field — e.g. optimizing before conversation history loads, or passing a filtered array that matched nothing.","commonSituations":"Race where the messages prop hasn't populated, filtering messages by a predicate that returns empty, or state initialized to [] and the action firing on mount.","solutions":["Guard the call: only invoke optimizeMessage when messages.length > 0.","Await conversation/message loading before enabling the optimize action.","Debounce or disable actions during message-list load states.","Catch OptimizationError and inform the user that context messages are required."],"exampleFix":"// before\nawait promptService.optimizeMessage({ selectedMessageId: id, messages: [], modelKey });\n\n// after\nif (!messages.length) throw new Error('Conversation context required');\nawait promptService.optimizeMessage({ selectedMessageId: id, messages, modelKey });","handlingStrategy":"validation","validationCode":"if (!Array.isArray(request.messages) || request.messages.length === 0) throw new Error('Context messages required');","typeGuard":"function hasMessages(r: unknown): r is { messages: unknown[] } {\n  return Array.isArray((r as any)?.messages) && (r as any).messages.length > 0;\n}","tryCatchPattern":"try { await svc.optimizeMessage(req); } catch (e) { if (e instanceof OptimizationError && /Messages array/.test(e.message)) await reloadConversation(); }","preventionTips":["Await message-list loading before enabling optimize","Disable action on empty conversations","Check messages.length > 0 at the call site"],"tags":["validation","message-optimization","empty-array"],"backgroundTag":"required-field-missing","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}