mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-05 11:42:55 +00:00
118 lines
3.8 KiB
TypeScript
118 lines
3.8 KiB
TypeScript
import { normalizeProviderId } from "@openclaw/model-catalog-core/provider-id";
|
|
import {
|
|
normalizeGooglePreviewModelId,
|
|
normalizeTogetherModelId,
|
|
} from "@openclaw/model-catalog-core/provider-model-id-normalize";
|
|
import { isRecord as isPlainRecord } from "@openclaw/normalization-core/record-coerce";
|
|
import {
|
|
normalizeLowercaseStringOrEmpty,
|
|
normalizeOptionalString,
|
|
resolvePrimaryStringValue,
|
|
} from "@openclaw/normalization-core/string-coerce";
|
|
import type { AgentModelConfig, AgentToolModelConfig } from "./types.agents-shared.js";
|
|
|
|
type AgentModelListLike = {
|
|
primary?: string;
|
|
fallbacks?: string[];
|
|
};
|
|
|
|
function modelKeyForConfig(provider: string, model: string): string {
|
|
const providerId = provider.trim();
|
|
const modelId = model.trim();
|
|
if (!providerId) {
|
|
return modelId;
|
|
}
|
|
if (!modelId) {
|
|
return providerId;
|
|
}
|
|
return normalizeLowercaseStringOrEmpty(modelId).startsWith(
|
|
`${normalizeLowercaseStringOrEmpty(providerId)}/`,
|
|
)
|
|
? modelId
|
|
: `${providerId}/${modelId}`;
|
|
}
|
|
|
|
type AgentModelInput = AgentModelConfig | AgentToolModelConfig;
|
|
|
|
export function resolveAgentModelPrimaryValue(model?: AgentModelInput): string | undefined {
|
|
return resolvePrimaryStringValue(model);
|
|
}
|
|
|
|
export function resolveAgentModelFallbackValues(model?: AgentModelInput): string[] {
|
|
if (!model || typeof model !== "object") {
|
|
return [];
|
|
}
|
|
return Array.isArray(model.fallbacks) ? model.fallbacks : [];
|
|
}
|
|
|
|
export function resolveAgentModelTimeoutMsValue(model?: AgentToolModelConfig): number | undefined {
|
|
if (!model || typeof model !== "object") {
|
|
return undefined;
|
|
}
|
|
return typeof model.timeoutMs === "number" &&
|
|
Number.isFinite(model.timeoutMs) &&
|
|
model.timeoutMs > 0
|
|
? Math.floor(model.timeoutMs)
|
|
: undefined;
|
|
}
|
|
|
|
export function toAgentModelListLike(model?: AgentModelConfig): AgentModelListLike | undefined {
|
|
if (typeof model === "string") {
|
|
const primary = normalizeOptionalString(model);
|
|
return primary ? { primary } : undefined;
|
|
}
|
|
if (!model || typeof model !== "object") {
|
|
return undefined;
|
|
}
|
|
return model;
|
|
}
|
|
|
|
const GOOGLE_PROVIDER_IDS = new Set(["google", "google-gemini-cli", "google-vertex"]);
|
|
|
|
export function normalizeAgentModelRefForConfig(model: string): string {
|
|
const trimmed = model.trim();
|
|
const slash = trimmed.indexOf("/");
|
|
if (slash <= 0 || slash >= trimmed.length - 1) {
|
|
return trimmed;
|
|
}
|
|
|
|
const provider = normalizeProviderId(trimmed.slice(0, slash));
|
|
const modelSuffix = trimmed.slice(slash + 1);
|
|
const normalizedModel =
|
|
GOOGLE_PROVIDER_IDS.has(provider) || modelSuffix.startsWith("google/")
|
|
? normalizeGooglePreviewModelId(modelSuffix)
|
|
: provider === "together"
|
|
? normalizeTogetherModelId(modelSuffix)
|
|
: modelSuffix;
|
|
return modelKeyForConfig(provider, normalizedModel);
|
|
}
|
|
|
|
function mergeAgentModelEntryForConfig(existing: unknown, incoming: unknown): unknown {
|
|
if (!isPlainRecord(existing) || !isPlainRecord(incoming)) {
|
|
return incoming;
|
|
}
|
|
|
|
const existingParams = isPlainRecord(existing.params) ? existing.params : undefined;
|
|
const incomingParams = isPlainRecord(incoming.params) ? incoming.params : undefined;
|
|
return {
|
|
...existing,
|
|
...incoming,
|
|
...(existingParams || incomingParams
|
|
? { params: { ...existingParams, ...incomingParams } }
|
|
: undefined),
|
|
};
|
|
}
|
|
|
|
export function normalizeAgentModelMapForConfig<T extends Record<string, unknown>>(models: T): T {
|
|
let mutated = false;
|
|
const next: Record<string, unknown> = {};
|
|
for (const [key, entry] of Object.entries(models)) {
|
|
const normalizedKey = normalizeAgentModelRefForConfig(key);
|
|
if (normalizedKey !== key || Object.hasOwn(next, normalizedKey)) {
|
|
mutated = true;
|
|
}
|
|
next[normalizedKey] = mergeAgentModelEntryForConfig(next[normalizedKey], entry);
|
|
}
|
|
return (mutated ? next : models) as T;
|
|
}
|