Files
openchamber/packages/ui/src/lib/modelMetadata.ts
T
Bohdan Triapitsyn 966f35da6c refactor: unify model picker behavior
Share model picker UI across chat, agents, and multi-run
Prefer live provider limits with metadata fallback
2026-05-20 16:26:40 +03:00

36 lines
1.3 KiB
TypeScript

import type { ModelMetadata } from '@/types';
type LiveProviderModel = Record<string, unknown> & { id?: string; name?: string };
const getNumericLimit = (limit: unknown, key: 'context' | 'output') => {
if (!limit || typeof limit !== 'object') return undefined;
const value = (limit as Record<string, unknown>)[key];
return typeof value === 'number' && Number.isFinite(value) ? value : undefined;
};
export const mergeModelMetadataWithLiveModel = (
providerId: string,
model: LiveProviderModel,
metadata?: ModelMetadata,
): ModelMetadata | undefined => {
const liveContextLimit = getNumericLimit(model.limit, 'context');
const liveOutputLimit = getNumericLimit(model.limit, 'output');
const contextLimit = liveContextLimit ?? metadata?.limit?.context;
const outputLimit = liveOutputLimit ?? metadata?.limit?.output;
if (contextLimit === undefined && outputLimit === undefined) return metadata;
return {
...(metadata ?? {
id: typeof model.id === 'string' ? model.id : '',
providerId,
name: typeof model.name === 'string' ? model.name : undefined,
}),
limit: {
...metadata?.limit,
...(contextLimit !== undefined ? { context: contextLimit } : {}),
...(outputLimit !== undefined ? { output: outputLimit } : {}),
},
};
};