linshenkx/prompt-optimizer · error · EvaluationValidationError

Function mode must not be empty.

Error message

Function mode must not be empty.

What it means

OptimizationError thrown by optimizeMessage when the model registry returns nothing for request.modelKey — the message-optimization counterpart of the 'Model not found' check in optimizePrompt. It fires after request validation but before template resolution.

Solutions

  1. Validate the key against modelManager.listModels() before calling optimizeMessage.
  2. Refresh the model list and re-select a valid model when the error occurs.
  3. Await model-manager initialization before enabling message optimization in the UI.
  4. Fall back to the first available model when the saved default is missing.

Example fix

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

// after
const models = await modelManager.listModels();
const modelKey = models.some(m => m.key === staleKey) ? staleKey : models[0]?.key;
if (!modelKey) throw new Error('No models configured');
await promptService.optimizeMessage({ selectedMessageId: id, messages, modelKey });
Defensive patterns

Strategy: validation

Validate before calling

if (!(await modelManager.getModel(request.modelKey))) throw new Error('Model not configured');

Type guard

async function modelExists(modelManager: any, key: string): Promise<boolean> {
  return !!(await modelManager.getModel(key));
}

Try / catch

try { await svc.optimizeMessage(req); } catch (e) { if (e instanceof OptimizationError && e.message === 'Model not found') await refreshModels(); }

Prevention

When it happens

Trigger: Calling optimizeMessage with a modelKey absent from the model manager: model deleted after selection, stale default, models not yet loaded, or key from another profile.

Common situations: Same as prompt-flow: deleted default model, imported settings referencing missing models, async model list races, key format mismatches.

Related errors


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

Appendix: source

Thrown at packages/core/src/services/evaluation/service.ts:461

          error instanceof Error ? error : undefined
        )
      );
    }
  }

  /**
   * 验证评估请求
   */
  private validateRequest(request: EvaluationRequest): void {
    if (!request.evaluationModelKey?.trim()) {
      throw new EvaluationValidationError('Evaluation model key must not be empty.');
    }

    if (!request.mode) {
      throw new EvaluationValidationError('Evaluation mode configuration must not be empty.');
    }
    if (!request.mode.functionMode) {
      throw new EvaluationValidationError('Function mode must not be empty.');
    }
    if (!request.mode.subMode) {
      throw new EvaluationValidationError('Sub mode must not be empty.');
    }

    switch (request.type) {
      case 'result':
        if (!request.target?.workspacePrompt?.trim()) {
          throw new EvaluationValidationError('Workspace prompt must not be empty.');
        }
        this.validateTestCase(request.testCase, 'Result evaluation test case');
        this.validateSnapshot(request.snapshot, 'Result evaluation snapshot');
        if (request.snapshot.testCaseId !== request.testCase.id) {
          throw new EvaluationValidationError(
            'Result evaluation snapshot testCaseId must match testCase.id.'
          );
        }
        if (this.isImageText2ImageMode(request) && !this.hasSnapshotOutputMedia(request.snapshot)) {

View on GitHub (pinned to 3e677b1d9f)