ai-chatbot/app/(chat)/api/chat/_lib/model-resolver.ts
2026-05-26 22:34:12 +08:00

44 lines
1.3 KiB
TypeScript

export function resolveModelDisplayNameByModelId(
models: Array<{ model: any; provider?: any }> | undefined,
modelId: string | undefined,
): string {
if (!modelId || !models?.length) return "";
const match = models.find(
(item) => item.model.modelId === modelId || item.model.id === modelId,
);
return String(match?.model?.displayName || "");
}
export function resolveModelEntryByRuntimeModelId(
models: Array<{ model: any; provider?: any }> | undefined,
modelId: string | undefined,
fallbackRef?: string,
) {
const candidates = [modelId, fallbackRef]
.map((item) => String(item || "").trim())
.filter(Boolean);
if (!candidates.length || !models?.length) return null;
return (
models.find((item) =>
candidates.some(
(candidate) => item.model.modelId === candidate || item.model.id === candidate,
),
) || null
);
}
export function resolveModelRefByRuntimeModelId(
models: Array<{ model: any; provider?: any }> | undefined,
modelId: string | undefined,
fallbackRef?: string,
): string {
const normalizedModelId = String(modelId || "").trim();
if (!normalizedModelId) return fallbackRef || "";
const match = models?.find(
(item) => item.model.modelId === normalizedModelId || item.model.id === normalizedModelId,
);
return String(match?.model?.id || fallbackRef || normalizedModelId || "");
}