linshenkx/prompt-optimizer · warning · FavoriteValidationError

Image mode must specify imageSubMode

Error message

Image mode must specify imageSubMode

What it means

addFavorite requires image-mode favorites to declare imageSubMode; without it the image function mode is ambiguous and FavoriteValidationError is thrown.

Source

Thrown at packages/core/src/services/favorite/manager.ts:283

    if (!favorite.content?.trim()) {
      throw new FavoriteValidationError('Prompt content cannot be empty');
    }

    // 验证 functionMode 必填
    if (!favorite.functionMode) {
      throw new FavoriteValidationError('Function mode (functionMode) cannot be empty');
    }

    // 验证功能模式分类的完整性
    if (favorite.functionMode === 'basic' || favorite.functionMode === 'context') {
      if (!favorite.optimizationMode) {
        throw new FavoriteValidationError(`${favorite.functionMode} mode must specify optimizationMode`);
      }
    }

    if (favorite.functionMode === 'image') {
      if (!favorite.imageSubMode) {
        throw new FavoriteValidationError('Image mode must specify imageSubMode');
      }
    }

    assertFavoriteMetadataHasNoInlineImages(favorite.metadata);

    const favoriteData = {
      title: favorite.title?.trim() || favorite.content.slice(0, 50) + (favorite.content.length > 50 ? '...' : ''),
      content: favorite.content,
      description: favorite.description,
      category: favorite.category,
      tags: favorite.tags || [],
      functionMode: favorite.functionMode,
      optimizationMode: favorite.optimizationMode,
      imageSubMode: favorite.imageSubMode,
      metadata: favorite.metadata
    };

    const now = Date.now();

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Set imageSubMode whenever functionMode is 'image'
  2. Validate in the form: image mode reveals a mandatory sub-mode picker
  3. Map old imported favorites to a default imageSubMode before insert

Example fix

// before
await manager.addFavorite({ content, functionMode: 'image' });
// after
await manager.addFavorite({ content, functionMode: 'image', imageSubMode: 'generation' });
Defensive patterns

Strategy: validation

Validate before calling

if (fav.functionMode === 'image' && !fav.imageSubMode) fav.imageSubMode = 'generation';

Type guard

const isImageModeComplete = (f: Partial<FavoritePrompt>): f is FavoritePrompt & { imageSubMode: string } =>
  f.functionMode !== 'image' || !!f.imageSubMode;

Try / catch

try { await manager.addFavorite(fav); } catch (e) { if (e instanceof FavoriteValidationError && /imageSubMode/.test(e.message)) { pickSubMode(); return; } throw e; }

Prevention

When it happens

Trigger: addFavorite({ functionMode: 'image' }) with no imageSubMode.

Common situations: Image-mode favorites added by code paths that predate imageSubMode; forms missing the sub-mode selector; imports of old favorites without the field.

Related errors


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