linshenkx/prompt-optimizer · warning · DataError

DATA_ERROR_CODES.ELECTRON_API_UNAVAILABLE

DATA_ERROR_CODES.ELECTRON_API_UNAVAILABLE

Error message

ElectronDataManagerProxy can only be used in Electron renderer process

What it means

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.

Source

Thrown at packages/core/src/services/data/electron-proxy.ts:16

import { IDataManager } from './types';
import { DataError } from './errors';
import { DATA_ERROR_CODES } from '../../constants/error-codes';
import { safeSerializeForIPC } from '../../utils/ipc-serialization';

/**
 * Electron环境下的DataManager代理
 * 通过IPC调用主进程中的真实DataManager实例
 */
export class ElectronDataManagerProxy implements IDataManager {
  private electronAPI: any;

  constructor() {
    // 验证Electron环境
    if (typeof window === 'undefined' || !(window as any).electronAPI) {
      throw new DataError(
        DATA_ERROR_CODES.ELECTRON_API_UNAVAILABLE,
        'ElectronDataManagerProxy can only be used in Electron renderer process',
      );
    }
    this.electronAPI = (window as any).electronAPI;
  }

  async exportAllData(): Promise<string> {
    return this.electronAPI.data.exportAllData();
  }

  async importAllData(dataString: string): Promise<void> {
    await this.electronAPI.data.importAllData(safeSerializeForIPC(dataString));
  }
} 

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Guard the call: only invoke optimizeMessage when messages.length > 0.
  2. Await conversation/message loading before enabling the optimize action.
  3. Debounce or disable actions during message-list load states.
  4. Catch OptimizationError and inform the user that context messages are required.

Example fix

// before
await promptService.optimizeMessage({ selectedMessageId: id, messages: [], modelKey });

// after
if (!messages.length) throw new Error('Conversation context required');
await promptService.optimizeMessage({ selectedMessageId: id, messages, modelKey });
Defensive patterns

Strategy: validation

Validate before calling

if (!Array.isArray(request.messages) || request.messages.length === 0) throw new Error('Context messages required');

Type guard

function hasMessages(r: unknown): r is { messages: unknown[] } {
  return Array.isArray((r as any)?.messages) && (r as any).messages.length > 0;
}

Try / catch

try { await svc.optimizeMessage(req); } catch (e) { if (e instanceof OptimizationError && /Messages array/.test(e.message)) await reloadConversation(); }

Prevention

When it happens

Trigger: Calling optimizeMessage with messages: [] or omitting the field — e.g. optimizing before conversation history loads, or passing a filtered array that matched nothing.

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

Related errors


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