aegida-console / lib / chat / models.ts
models.ts
Raw
import type { ModelId } from "@/lib/chat/types";
import { isModelId } from "@/lib/chat/types";

export type ChatModel = {
  id: ModelId;
  name: string;
  description: string;
};

export const AUTO_MODEL: ChatModel = {
  id: "auto",
  name: "Aegida Auto",
  description: "Автоматический выбор модели через Aegida Gate.",
};

export const MODELS: ChatModel[] = [AUTO_MODEL];

export function parseChatModelsResponse(value: unknown): ChatModel[] | null {
  if (!isRecord(value) || !Array.isArray(value.models)) return null;
  const models: ChatModel[] = [];
  const seen = new Set<string>();
  for (const item of value.models) {
    if (
      !isRecord(item) ||
      !isModelId(item.id) ||
      typeof item.name !== "string" ||
      !item.name.trim() ||
      typeof item.description !== "string" ||
      seen.has(item.id)
    ) {
      return null;
    }
    seen.add(item.id);
    models.push({ id: item.id, name: item.name, description: item.description });
  }
  return seen.has("auto") ? models : null;
}

function isRecord(value: unknown): value is Record<string, unknown> {
  return typeof value === "object" && value !== null && !Array.isArray(value);
}