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.5 KB
Raw Blame
import type { ChatModelCard } from '@lobechat/types';
import { omit } from 'es-toolkit/compat';
import type { AiModelType } from 'model-bank';
import {
  CHAT_MODEL_IMAGE_GENERATION_PARAMS,
  DECISION_MODEL_SUFFIX,
  DECISION_MODEL_WHITELIST,
  IMAGE_GENERATION_MODEL_WHITELIST,
  LOBE_DEFAULT_MODEL_LIST,
} from 'model-bank';

export {
  DECISION_MODEL_WHITELIST,
  IMAGE_GENERATION_MODEL_WHITELIST,
  isAutoGeneratedDecisionModelId,
  isAutoGeneratedImageModelId,
} from 'model-bank';

const toDecisionModel = (model: ChatModelCard): ChatModelCard => {
  const id = `${model.id}${DECISION_MODEL_SUFFIX}`;
  // The chat card's abilities and output pricing do not apply to the decision endpoint.
  const builtin = LOBE_DEFAULT_MODEL_LIST.find(
    (card) => card.id === id && card.type === 'decision',
  );

  return {
    contextWindowTokens: builtin?.contextWindowTokens ?? model.contextWindowTokens,
    description: builtin?.description,
    displayName: builtin?.displayName ?? model.displayName,
    enabled: model.enabled,
    id,
    pricing: builtin?.pricing,
    releasedAt: builtin?.releasedAt ?? model.releasedAt,
    type: 'decision',
  };
};

/**
 * Appends a `<id>:decision` card for each whitelisted chat model in the list.
 *
 * Only providers that serve those models through `decide()` call this from their `models()`,
 * so other providers listing the same id (aggregators, proxies) do not get a decision card that
 * would fail with `CapabilityNotSupported`.
 */
export const appendDecisionModels = (models: ChatModelCard[]): ChatModelCard[] => [
  ...models,
  ...models
    .filter(
      (model) =>
        (model.type ?? 'chat') === 'chat' &&
        (DECISION_MODEL_WHITELIST as readonly string[]).includes(model.id),
    )
    .map(toDecisionModel),
];

/**
 * Process model list: ensure type field exists and generate image generation models for whitelisted models
 * @param models Original model list
 * @param getModelTypeProperty Optional callback function to get model type property
 * @returns Processed model list (including image generation models)
 */
export async function postProcessModelList(
  models: ChatModelCard[],
  getModelTypeProperty?: (modelId: string) => Promise<AiModelType>,
): Promise<ChatModelCard[]> {
  // 1. Ensure all models have type field
  const finalModels = await Promise.all(
    models.map(async (model) => {
      let modelType: AiModelType | undefined = model.type;

      if (!modelType && getModelTypeProperty) {
        modelType = await getModelTypeProperty(model.id);
      }

      return {
        ...model,
        type: modelType || 'chat',
      };
    }),
  );

  // 2. Check whitelist models and generate corresponding image versions
  const imageModels: ChatModelCard[] = [];

  for (const whitelistPattern of IMAGE_GENERATION_MODEL_WHITELIST) {
    const matchingModels = finalModels.filter((model) => model.id.endsWith(whitelistPattern));

    for (const model of matchingModels) {
      // Remove unnecessary properties, keep the rest
      const rest = omit(model, [
        'files',
        'functionCall',
        'reasoning',
        'search',
        'imageOutput',
        'video',
        'vision',
        'type',
        'parameters',
      ]);

      imageModels.push({
        ...rest, // Keep other fields (such as displayName, pricing, enabled, contextWindowTokens, etc.)
        id: `${model.id}:image`,
        parameters: CHAT_MODEL_IMAGE_GENERATION_PARAMS, // Set image parameters
        type: 'image', // Override to image type
      });
    }
  }

  return [...finalModels, ...imageModels];
}