ai-chatbot/lib/ai/model-runtime.ts

42 lines
1.1 KiB
TypeScript

interface ModelRuntimeLike {
modelId?: string | null;
defaultParams?: unknown;
}
interface ResolveModelRuntimeIdOptions {
overrideModelId?: string | null;
providerType?: string | null;
}
function toRecord(value: unknown): Record<string, unknown> | null {
if (typeof value !== "object" || value === null || Array.isArray(value)) {
return null;
}
return value as Record<string, unknown>;
}
function normalizeRuntimeModelId(value: unknown): string | null {
if (typeof value !== "string") return null;
const normalized = value.trim();
return normalized || null;
}
export function resolveModelRuntimeId(
model: ModelRuntimeLike | null | undefined,
options?: ResolveModelRuntimeIdOptions,
): string {
const overrideModelId = normalizeRuntimeModelId(options?.overrideModelId);
if (overrideModelId) {
return overrideModelId;
}
const defaultParams = toRecord(model?.defaultParams);
const endpointModelId = normalizeRuntimeModelId(defaultParams?.endpoint);
if (endpointModelId) {
return endpointModelId;
}
const resolvedModelId = normalizeRuntimeModelId(model?.modelId) || "";
return resolvedModelId;
}