linshenkx/prompt-optimizer · error · BaseError

CONFIG_INVALID

CONFIG_INVALID

Error message

ElectronImageModelManagerProxy can only be used in Electron renderer process

What it means

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.

Source

Thrown at packages/core/src/services/image/electron-proxy.ts:113

  }

  async testConnection(config: ImageModelConfig): Promise<ImageResult> {
    const safeCfg = safeSerializeForIPC(config)
    return await this.electronAPI.image.testConnection(safeCfg)
  }

  async getDynamicModels(providerId: string, connectionConfig: Record<string, any>) {
    const safeConn = safeSerializeForIPC(connectionConfig || {})
    return await this.electronAPI.image.getDynamicModels(providerId, safeConn)
  }
}

export class ElectronImageModelManagerProxy implements IImageModelManager {
  private electronAPI: ElectronAPI

  constructor() {
    if (typeof window === 'undefined' || !window.electronAPI) {
      throw new BaseError(IMAGE_ERROR_CODES.CONFIG_INVALID, 'ElectronImageModelManagerProxy can only be used in Electron renderer process')
    }
    this.electronAPI = (window as unknown as { electronAPI: ElectronAPI }).electronAPI
  }

  async ensureInitialized(): Promise<void> {
    await this.electronAPI.imageModel.ensureInitialized()
  }

  async isInitialized(): Promise<boolean> {
    return await this.electronAPI.imageModel.isInitialized()
  }

  // 新的配置 CRUD 操作
  async addConfig(config: ImageModelConfigInput): Promise<void> {
    const safeCfg = safeSerializeForIPC(config)
    await this.electronAPI.imageModel.addConfig(safeCfg)
  }

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Verify the preload script path in BrowserWindow({ webPreferences: { preload: ... } })
  2. Expose the API: contextBridge.exposeInMainWorld('electronAPI', { imageModel: {...} })
  3. Mock window.electronAPI in test setup before constructing the proxy
  4. Guard construction with a window.electronAPI check and fall back to the direct manager

Example fix

// before
const mgr = new ElectronImageModelManagerProxy()

// after
if (typeof window !== 'undefined' && window.electronAPI?.imageModel) {
  mgr = new ElectronImageModelManagerProxy()
} else {
  mgr = createDirectImageModelManager()
}
Defensive patterns

Strategy: type-guard

Validate before calling

const canUseManagerProxy = typeof window !== 'undefined' && !!(window as any).electronAPI?.imageModel

Type guard

function isElectronRenderer(w: unknown): w is Window & { electronAPI: ElectronAPI } {
  return typeof w !== 'undefined' && !!(w as any)?.electronAPI
}

Prevention

When it happens

Trigger: Constructing ElectronImageModelManagerProxy outside an Electron renderer (browser, Node, tests) or in a renderer whose preload script did not expose electronAPI.imageModel.

Common situations: 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.

Related errors


AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27). Data as JSON: /api/errors/0a18c2f29b5e97f9. Report an issue: GitHub.