mirror of https://github.com/openclaw/openclaw.git
66 lines
1.9 KiB
TypeScript
66 lines
1.9 KiB
TypeScript
import { normalizeModelCompat } from "../../src/agents/model-compat.js";
|
|
import type {
|
|
ProviderResolveDynamicModelContext,
|
|
ProviderRuntimeModel,
|
|
} from "../../src/plugins/types.js";
|
|
|
|
export const OPENAI_API_BASE_URL = "https://api.openai.com/v1";
|
|
|
|
export function matchesExactOrPrefix(id: string, values: readonly string[]): boolean {
|
|
const normalizedId = id.trim().toLowerCase();
|
|
return values.some((value) => {
|
|
const normalizedValue = value.trim().toLowerCase();
|
|
return normalizedId === normalizedValue || normalizedId.startsWith(normalizedValue);
|
|
});
|
|
}
|
|
|
|
export function isOpenAIApiBaseUrl(baseUrl?: string): boolean {
|
|
const trimmed = baseUrl?.trim();
|
|
if (!trimmed) {
|
|
return false;
|
|
}
|
|
return /^https?:\/\/api\.openai\.com(?:\/v1)?\/?$/i.test(trimmed);
|
|
}
|
|
|
|
export function cloneFirstTemplateModel(params: {
|
|
providerId: string;
|
|
modelId: string;
|
|
templateIds: readonly string[];
|
|
ctx: ProviderResolveDynamicModelContext;
|
|
patch?: Partial<ProviderRuntimeModel>;
|
|
}): ProviderRuntimeModel | undefined {
|
|
const trimmedModelId = params.modelId.trim();
|
|
for (const templateId of [...new Set(params.templateIds)].filter(Boolean)) {
|
|
const template = params.ctx.modelRegistry.find(
|
|
params.providerId,
|
|
templateId,
|
|
) as ProviderRuntimeModel | null;
|
|
if (!template) {
|
|
continue;
|
|
}
|
|
return normalizeModelCompat({
|
|
...template,
|
|
id: trimmedModelId,
|
|
name: trimmedModelId,
|
|
...params.patch,
|
|
} as ProviderRuntimeModel);
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
export function findCatalogTemplate(params: {
|
|
entries: ReadonlyArray<{ provider: string; id: string }>;
|
|
providerId: string;
|
|
templateIds: readonly string[];
|
|
}) {
|
|
return params.templateIds
|
|
.map((templateId) =>
|
|
params.entries.find(
|
|
(entry) =>
|
|
entry.provider.toLowerCase() === params.providerId.toLowerCase() &&
|
|
entry.id.toLowerCase() === templateId.toLowerCase(),
|
|
),
|
|
)
|
|
.find((entry) => entry !== undefined);
|
|
}
|