Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

3.8 KB
Raw Blame
import { AgentRuntimeErrorType } from '@lobechat/model-runtime';
import { AsyncTaskErrorType } from '@lobechat/types';
import { describe, expect, it } from 'vitest';

import { CONTENT_POLICY_ERROR_MESSAGE } from './contentPolicyError';
import { categorizeImageGenerationError } from './imageError';

describe('categorizeImageGenerationError', () => {
  it('should map runtime content policy violations to async content moderation errors', () => {
    const result = categorizeImageGenerationError({
      error: {
        error: {
          providerReason: 'IMAGE_PROHIBITED_CONTENT',
          reasonCode: 'google_image_content_policy_violation',
        },
        errorType: AgentRuntimeErrorType.ProviderContentPolicyViolation,
      },
      isAborted: false,
      isEditingImage: false,
    });

    expect(result).toEqual({
      errorMessage: CONTENT_POLICY_ERROR_MESSAGE,
      errorType: AsyncTaskErrorType.ProviderContentModeration,
    });
  });

  it('should surface provider text-only image responses as server errors', () => {
    const result = categorizeImageGenerationError({
      error: {
        error: {
          message: "I'm just a language model and can't help with that.",
          reasonCode: 'google_image_text_only_response',
        },
        errorType: AgentRuntimeErrorType.ProviderNoImageGenerated,
      },
      isAborted: false,
      isEditingImage: false,
    });

    expect(result).toEqual({
      errorMessage: "I'm just a language model and can't help with that.",
      errorType: AsyncTaskErrorType.ServerError,
    });
  });

  it('should surface provider text refusal reasons when the runtime classified an explicit refusal', () => {
    const result = categorizeImageGenerationError({
      error: {
        error: {
          message: 'No image generated: The requested output format is not supported.',
          reasonCode: 'google_image_generation_refused',
        },
        errorType: AgentRuntimeErrorType.ProviderNoImageGenerated,
      },
      isAborted: false,
      isEditingImage: false,
    });

    expect(result).toEqual({
      errorMessage: 'No image generated: The requested output format is not supported.',
      errorType: AsyncTaskErrorType.ServerError,
    });
  });

  it('should keep generic no-image provider responses as server errors', () => {
    const result = categorizeImageGenerationError({
      error: {
        errorType: AgentRuntimeErrorType.ProviderNoImageGenerated,
      },
      isAborted: false,
      isEditingImage: false,
    });

    expect(result).toEqual({
      errorMessage:
        'The provider did not return an image. This may be due to content review. Try a milder prompt or another model.',
      errorType: AsyncTaskErrorType.ServerError,
    });
  });

  it('should keep the nested upstream message when the router rethrows a normalized error', () => {
    // Shape recorded for the last (azure) channel attempt when gpt-image rejected `height`.
    const result = categorizeImageGenerationError({
      error: {
        error: {
          error: {
            code: 'unknown_parameter',
            message: "Unknown parameter: 'height'.",
            type: 'invalid_request_error',
          },
          status: 400,
        },
        errorType: 'UpstreamHttpError',
      },
      isAborted: false,
      isEditingImage: false,
    });

    expect(result).toEqual({
      errorMessage: "400 Unknown parameter: 'height'.",
      errorType: AsyncTaskErrorType.ServerError,
    });
  });

  it('should not repeat the status when the upstream message already starts with it', () => {
    const result = categorizeImageGenerationError({
      error: { message: '400 prompt is required', status: 400 },
      isAborted: false,
      isEditingImage: false,
    });

    expect(result.errorMessage).toBe('400 prompt is required');
  });
});