{"record":{"id":"0a18c2f29b5e97f9","repo":"linshenkx/prompt-optimizer","slug":"config-invalid-0a18c2","errorCode":"CONFIG_INVALID","errorMessage":"ElectronImageModelManagerProxy can only be used in Electron renderer process","messagePattern":"ElectronImageModelManagerProxy can only be used in Electron renderer process","errorType":"exception","errorClass":"BaseError","httpStatus":null,"severity":"error","filePath":"packages/core/src/services/image/electron-proxy.ts","lineNumber":113,"sourceCode":"  }\n\n  async testConnection(config: ImageModelConfig): Promise<ImageResult> {\n    const safeCfg = safeSerializeForIPC(config)\n    return await this.electronAPI.image.testConnection(safeCfg)\n  }\n\n  async getDynamicModels(providerId: string, connectionConfig: Record<string, any>) {\n    const safeConn = safeSerializeForIPC(connectionConfig || {})\n    return await this.electronAPI.image.getDynamicModels(providerId, safeConn)\n  }\n}\n\nexport class ElectronImageModelManagerProxy implements IImageModelManager {\n  private electronAPI: ElectronAPI\n\n  constructor() {\n    if (typeof window === 'undefined' || !window.electronAPI) {\n      throw new BaseError(IMAGE_ERROR_CODES.CONFIG_INVALID, 'ElectronImageModelManagerProxy can only be used in Electron renderer process')\n    }\n    this.electronAPI = (window as unknown as { electronAPI: ElectronAPI }).electronAPI\n  }\n\n  async ensureInitialized(): Promise<void> {\n    await this.electronAPI.imageModel.ensureInitialized()\n  }\n\n  async isInitialized(): Promise<boolean> {\n    return await this.electronAPI.imageModel.isInitialized()\n  }\n\n  // 新的配置 CRUD 操作\n  async addConfig(config: ImageModelConfigInput): Promise<void> {\n    const safeCfg = safeSerializeForIPC(config)\n    await this.electronAPI.imageModel.addConfig(safeCfg)\n  }\n","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/image/electron-proxy.ts#L95-L131","documentation":"ElectronImageModelManagerProxy is the renderer-side IPC proxy for image model management and, like the service proxy, requires `window.electronAPI`. The constructor throws CONFIG_INVALID when that bridge is absent, indicating the environment or preload setup is wrong.","triggerScenarios":"Constructing ElectronImageModelManagerProxy outside an Electron renderer (browser, Node, tests) or in a renderer whose preload script did not expose electronAPI.imageModel.","commonSituations":"Running unit tests without an electronAPI mock; preload not loaded (wrong path in BrowserWindow webPreferences); contextIsolation enabled but no contextBridge exposure; opening the app UI in a normal browser for debugging.","solutions":["Verify the preload script path in BrowserWindow({ webPreferences: { preload: ... } })","Expose the API: contextBridge.exposeInMainWorld('electronAPI', { imageModel: {...} })","Mock window.electronAPI in test setup before constructing the proxy","Guard construction with a window.electronAPI check and fall back to the direct manager"],"exampleFix":"// before\nconst mgr = new ElectronImageModelManagerProxy()\n\n// after\nif (typeof window !== 'undefined' && window.electronAPI?.imageModel) {\n  mgr = new ElectronImageModelManagerProxy()\n} else {\n  mgr = createDirectImageModelManager()\n}","handlingStrategy":"type-guard","validationCode":"const canUseManagerProxy = typeof window !== 'undefined' && !!(window as any).electronAPI?.imageModel","typeGuard":"function isElectronRenderer(w: unknown): w is Window & { electronAPI: ElectronAPI } {\n  return typeof w !== 'undefined' && !!(w as any)?.electronAPI\n}","tryCatchPattern":null,"preventionTips":["Verify webPreferences.preload path points to the built preload bundle","Expose imageModel on the electronAPI bridge in preload","Centralize proxy construction behind one factory that checks the environment"],"tags":["electron","ipc","preload","config","renderer-process"],"backgroundTag":"electron-preload-api-missing","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}