mirror of
https://github.com/moltbot/moltbot.git
synced 2026-05-13 23:56:07 +00:00
115 lines
3.6 KiB
TypeScript
115 lines
3.6 KiB
TypeScript
import { normalizeProviderId } from "../agents/provider-id.js";
|
|
import { normalizeGooglePreviewModelId } from "../plugin-sdk/provider-model-id-normalize.js";
|
|
import {
|
|
normalizeLowercaseStringOrEmpty,
|
|
normalizeOptionalString,
|
|
resolvePrimaryStringValue,
|
|
} from "../shared/string-coerce.js";
|
|
import type { AgentModelConfig } from "./types.agents-shared.js";
|
|
|
|
type AgentModelListLike = {
|
|
primary?: string;
|
|
fallbacks?: string[];
|
|
timeoutMs?: number;
|
|
};
|
|
|
|
const GOOGLE_CONFIG_MODEL_PROVIDERS = new Set(["google", "google-gemini-cli", "google-vertex"]);
|
|
|
|
function isPlainRecord(value: unknown): value is Record<string, unknown> {
|
|
return Boolean(value && typeof value === "object" && !Array.isArray(value));
|
|
}
|
|
|
|
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}`;
|
|
}
|
|
|
|
export function resolveAgentModelPrimaryValue(model?: AgentModelConfig): string | undefined {
|
|
return resolvePrimaryStringValue(model);
|
|
}
|
|
|
|
export function resolveAgentModelFallbackValues(model?: AgentModelConfig): string[] {
|
|
if (!model || typeof model !== "object") {
|
|
return [];
|
|
}
|
|
return Array.isArray(model.fallbacks) ? model.fallbacks : [];
|
|
}
|
|
|
|
export function resolveAgentModelTimeoutMsValue(model?: AgentModelConfig): 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;
|
|
}
|
|
|
|
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));
|
|
if (!GOOGLE_CONFIG_MODEL_PROVIDERS.has(provider)) {
|
|
return trimmed;
|
|
}
|
|
|
|
const normalizedModel = normalizeGooglePreviewModelId(trimmed.slice(slash + 1));
|
|
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.prototype.hasOwnProperty.call(next, normalizedKey)) {
|
|
mutated = true;
|
|
}
|
|
next[normalizedKey] = mergeAgentModelEntryForConfig(next[normalizedKey], entry);
|
|
}
|
|
return (mutated ? next : models) as T;
|
|
}
|