openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
128 lines (127 loc) • 4.91 kB
JavaScript
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { t as parseConfigPathArrayIndex } from "./path-array-index-qP-D1DoI.mjs";
import "./utils-C8YECn2Q.mjs";
import { r as normalizeConfiguredProviderCatalogModelId } from "./provider-model-id-normalization-Yii1-7Le.mjs";
import { t as AGENT_MODEL_CONFIG_KEYS } from "./configured-model-refs-DtaDJ1Vw.mjs";
import { i as normalizeAgentModelSelectionForConfig, n as normalizeAgentModelMapForConfig, r as normalizeAgentModelRefForConfig, u as toAgentModelListLike } from "./model-input-DrM4DWHG.mjs";
//#region src/config/model-input-normalization.ts
const MEDIA_MODEL_KEYS = [
"image",
"video",
"music"
];
/** Preserve a string model when a path write enters its supported object form. */
function normalizeConfigModelSelectionParent(e, t, n) {
if (typeof e !== "string") return;
const r = t[n + 1];
if (r !== "primary" && r !== "fallbacks" && r !== "timeoutMs") return;
const i = t[0] === "agents" && t[1] === "defaults";
const a = t[0] === "agents" && (t[1] === "entries" && Boolean(t[2]) || t[1] === "list" && parseConfigPathArrayIndex(t[2] ?? "") !== void 0);
const o = i ? 2 : a ? 3 : void 0;
const s = o !== void 0 && (n === o && (t[o] === "model" || t[o] === "utilityModel") || n === o + 1 && t[o] === "subagents" && t[n] === "model");
const c = i && (n === 2 && AGENT_MODEL_CONFIG_KEYS.some((e) => e !== "model" && e !== "utilityModel" && e === t[2]) || n === 3 && t[2] === "mediaModels" && MEDIA_MODEL_KEYS.some((e) => e === t[3]));
const l = a ? 3 : 0;
const u = n === l + 3 && t[l] === "tools" && t[l + 1] === "exec" && t[l + 2] === "reviewer" && t[n] === "model";
if (!s && !c && !u || r === "timeoutMs" && !c) return;
if (t[n] === "utilityModel") return { primary: e.trim() };
return toAgentModelListLike(e);
}
function normalizeStringModelRef(e) {
return typeof e === "string" ? normalizeAgentModelRefForConfig(e) : e;
}
function normalizeNestedModelField(e, t, n) {
if (!isRecord(e) || !Object.hasOwn(e, t)) return e;
const r = n(e[t]);
return r === e[t] ? e : {
...e,
[t]: r
};
}
function normalizeAgentModelScope(e) {
if (!isRecord(e)) return e;
let t = e;
for (const e of AGENT_MODEL_CONFIG_KEYS) t = normalizeNestedModelField(t, e, normalizeAgentModelSelectionForConfig);
t = normalizeNestedModelField(t, "mediaModels", (e) => {
let t = e;
for (const e of MEDIA_MODEL_KEYS) t = normalizeNestedModelField(t, e, normalizeAgentModelSelectionForConfig);
return t;
});
t = normalizeNestedModelField(t, "heartbeat", (e) => normalizeNestedModelField(e, "model", normalizeStringModelRef));
t = normalizeNestedModelField(t, "subagents", (e) => normalizeNestedModelField(e, "model", normalizeAgentModelSelectionForConfig));
t = normalizeNestedModelField(t, "compaction", (e) => normalizeNestedModelField(e, "model", normalizeStringModelRef));
return normalizeNestedModelField(t, "models", (e) => isRecord(e) ? normalizeAgentModelMapForConfig(e) : e);
}
function normalizeAgentScopes(e) {
if (!isRecord(e)) return e;
let t = e;
const assign = (e, n) => {
if (n === t[e]) return;
t = {
...t,
[e]: n
};
};
if (Object.hasOwn(e, "defaults")) assign("defaults", normalizeAgentModelScope(e.defaults));
if (isRecord(e.entries)) {
let t = false;
const n = Object.fromEntries(Object.entries(e.entries).map(([e, n]) => {
const r = normalizeAgentModelScope(n);
t ||= r !== n;
return [e, r];
}));
if (t) assign("entries", n);
}
if (Array.isArray(e.list)) {
const t = e.list;
const n = t.map(normalizeAgentModelScope);
if (n.some((e, n) => e !== t[n])) assign("list", n);
}
return t;
}
function normalizeProviderCatalogs(e, t) {
if (!isRecord(e) || !isRecord(e.providers)) return e;
let n;
let r = false;
const i = Object.fromEntries(Object.entries(e.providers).map(([e, i]) => {
if (!isRecord(i) || !Array.isArray(i.models)) return [e, i];
const a = i.models;
const o = a.map((r) => {
if (!isRecord(r) || typeof r.id !== "string") return r;
const i = r.id.trim();
if (!i) return r;
n ??= typeof t === "function" ? t() : t;
const a = normalizeConfiguredProviderCatalogModelId(e, i, n);
return a === r.id ? r : {
...r,
id: a
};
});
if (o.every((e, t) => e === a[t])) return [e, i];
r = true;
return [e, {
...i,
models: o
}];
}));
return r ? {
...e,
providers: i
} : e;
}
/** Canonicalize model refs submitted through a config mutation API before persistence. */
function normalizeSubmittedConfigModelRefs(e, t, n = {}) {
let r = e;
const i = normalizeAgentScopes(e.agents);
if (i !== e.agents) r = {
...r,
agents: i
};
const a = n.normalizeProviderCatalogs === false ? e.models : normalizeProviderCatalogs(e.models, t);
if (a !== e.models) r = {
...r,
models: a
};
return r;
}
//#endregion
export { normalizeSubmittedConfigModelRefs as n, normalizeConfigModelSelectionParent as t };