88 lines
2.5 KiB
TypeScript
88 lines
2.5 KiB
TypeScript
import { getAnthropicDefaultTeamModel } from './anthropicModelDefaults';
|
|
import { isDefaultProviderModelSelection } from './providerModelSelection';
|
|
|
|
function stripOneMillionSuffix(model: string): string {
|
|
return model.replace(/(?:\[1m\])+$/i, '');
|
|
}
|
|
|
|
function isAnthropicHaikuModel(model: string): boolean {
|
|
const baseModel = stripOneMillionSuffix(model);
|
|
return baseModel === 'haiku' || baseModel.startsWith('claude-haiku-');
|
|
}
|
|
|
|
function normalizeAvailableLaunchModels(
|
|
availableLaunchModels: Iterable<string> | undefined
|
|
): Set<string> {
|
|
const normalized = new Set<string>();
|
|
for (const model of availableLaunchModels ?? []) {
|
|
const trimmed = model.trim();
|
|
if (trimmed) {
|
|
normalized.add(trimmed);
|
|
}
|
|
}
|
|
return normalized;
|
|
}
|
|
|
|
function chooseAvailableModel(
|
|
availableModels: Set<string>,
|
|
candidates: readonly string[]
|
|
): string | null {
|
|
if (availableModels.size === 0) {
|
|
return null;
|
|
}
|
|
|
|
for (const candidate of candidates) {
|
|
if (availableModels.has(candidate)) {
|
|
return candidate;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
export function resolveAnthropicLaunchModel(params: {
|
|
selectedModel?: string | null;
|
|
limitContext: boolean;
|
|
availableLaunchModels?: Iterable<string>;
|
|
defaultLaunchModel?: string | null;
|
|
}): string | null {
|
|
const selectedModel = params.selectedModel?.trim() ?? '';
|
|
const availableModels = normalizeAvailableLaunchModels(params.availableLaunchModels);
|
|
|
|
if (!selectedModel || isDefaultProviderModelSelection(selectedModel)) {
|
|
const staticDefault = getAnthropicDefaultTeamModel(params.limitContext);
|
|
const runtimeDefault = params.defaultLaunchModel?.trim() || null;
|
|
const preferredDefault = params.limitContext
|
|
? stripOneMillionSuffix(runtimeDefault || staticDefault) || staticDefault
|
|
: runtimeDefault || staticDefault;
|
|
if (availableModels.size === 0) {
|
|
return preferredDefault;
|
|
}
|
|
|
|
return (
|
|
chooseAvailableModel(availableModels, [
|
|
preferredDefault,
|
|
stripOneMillionSuffix(runtimeDefault || preferredDefault),
|
|
staticDefault,
|
|
stripOneMillionSuffix(staticDefault),
|
|
]) ?? preferredDefault
|
|
);
|
|
}
|
|
|
|
const baseModel = stripOneMillionSuffix(selectedModel);
|
|
if (!baseModel) {
|
|
return null;
|
|
}
|
|
|
|
if (params.limitContext || isAnthropicHaikuModel(baseModel)) {
|
|
return baseModel;
|
|
}
|
|
|
|
const preferredLongContextModel = `${baseModel}[1m]`;
|
|
|
|
if (availableModels.size === 0) {
|
|
return preferredLongContextModel;
|
|
}
|
|
|
|
return chooseAvailableModel(availableModels, [preferredLongContextModel, baseModel]) ?? baseModel;
|
|
}
|