UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

240 lines (239 loc) • 8.25 kB
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { r as normalizeProviderId } from "./provider-id-b5XGSf9C.mjs"; import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs"; import "./session-key-CqAyZeVk.mjs"; import { h as SYSTEM_AGENT_ID, n as listAgentEntries } from "./agent-roster-t1ooUdXp.mjs"; import { E as toAgentEntriesRecord, m as resolveAmbientOwnerAgentId } from "./agent-scope-config-DmXgFGKP.mjs"; import { i as copyConfigResolutionFacts } from "./resolution-facts-xaIKuREs.mjs"; import { n as createRuntimeConfigReader } from "./runtime-snapshot-WbwbY5b7.mjs"; import { s as resolveAgentModelPrimaryValue } from "./model-input-DrM4DWHG.mjs"; import { i as resolveConfiguredSetupModelForAgent } from "./utility-model-DHdotLLx.mjs"; import { n as cliBackendAcceptsAuthProfileForwarding, r as resolveCliExecutionAuthProfileId } from "./cli-execution-auth-iYQVEZdU.mjs"; import { isDeepStrictEqual } from "node:util"; //#region src/system-agent/inference-route.ts function projectSystemAgentExecutionConfig(e, t, n) { const r = listAgentEntries(e); const i = r.find((e) => normalizeAgentId(e.id) === t); const a = [ ...r.filter((e) => normalizeAgentId(e.id) !== SYSTEM_AGENT_ID), ...n && !i && t !== "openclaw" ? [{ id: t }] : [], { id: SYSTEM_AGENT_ID, ...i?.params !== void 0 ? { params: structuredClone(i.params) } : {}, ...i?.tools !== void 0 ? { tools: structuredClone(i.tools) } : {} } ]; const o = { ...e, agents: { ...e.agents, entries: toAgentEntriesRecord(a) } }; copyConfigResolutionFacts(e, o); return o; } async function resolveSystemAgentConfiguredRouteFromConfig(e, t, n = {}, r) { const i = r && e === (r.runtimeConfig ?? r.config) ? r.sourceConfig : e; const a = createRuntimeConfigReader(i)(); const o = a === i ? e : a; const [s, c, l, u] = await Promise.all([ import("./model-selection-MVvzg347.mjs"), import("./model-runtime-aliases-CXspN1ym.mjs"), import("./simple-completion-selection-BlCvjsk9.mjs"), import("./policy-PHHtbF71.mjs") ]); const d = resolveAmbientOwnerAgentId(e, t); const f = resolveConfiguredSetupModelForAgent({ cfg: e, agentId: d, modelTarget: n.modelTarget }); if (!f) return null; const p = l.resolveSimpleCompletionSelectionForAgent({ cfg: e, agentId: d, modelRef: f.implicitPrimary ? void 0 : f.modelRef, manifestPlugins: n.pluginMetadataPlugins }); if (!p) return null; const m = n.pluginMetadataPlugins ? { plugins: n.pluginMetadataPlugins } : void 0; const h = c.resolveCliRuntimeExecutionProvider({ provider: p.provider, cfg: e, agentId: d, modelId: p.modelId, ...p.profileId ? { authProfileId: p.profileId } : {}, ...m ? { metadataSnapshot: m } : {} }) ?? p.runtimeProvider ?? p.provider; const g = s.isCliProvider(h, e); const _ = g && cliBackendAcceptsAuthProfileForwarding({ provider: h, config: e, agentId: d }); const v = _ ? await resolveCliExecutionAuthProfileId({ cliExecutionProvider: h, authProfileProvider: p.provider, config: e, agentDir: p.agentDir, ...p.profileId ? { selected: { authProfileId: p.profileId, authProfileIdSource: "user" } } : {} }) : void 0; const y = _ ? v : p.profileId; const b = projectSystemAgentExecutionConfig(o, d, n.modelTarget === "primary"); const x = s.resolveConfiguredRouteModelLabel({ configuredRef: f.modelRef, resolved: { provider: p.provider, model: p.modelId }, ...p.profileId ? { profileId: p.profileId } : {} }); const S = { ...f.modelTarget ? { modelTarget: f.modelTarget } : {}, sourceConfig: e, runConfig: b, modelLabel: x, provider: h, model: p.modelId, agentDir: p.agentDir, agentId: d, ...y ? { authProfileId: y } : {} }; if (g) return { runner: "cli", ...S }; const C = u.resolveAgentHarnessPolicy({ config: e, agentId: d, provider: p.provider, modelId: p.modelId }); return { runner: "embedded", ...C.runtimeSource === "implicit" ? {} : { agentHarnessRuntimeOverride: C.runtime }, ...S }; } function projectRelevantModelMap(e) { if (!e.models) return; const t = Object.fromEntries(Object.entries(e.models).filter(([t, n]) => { const r = t.indexOf("/"); const i = r > 0 ? normalizeProviderId(t.slice(0, r)) : ""; const a = r > 0 ? t.slice(r + 1) : t; return e.providerIds.has(i) && (a === e.modelId || a === "*" || t === e.rawModel) || n.alias?.trim() === e.rawModel; })); return Object.keys(t).length > 0 ? t : void 0; } /** Project every config input that can change one configured agent route. */ async function projectInferenceRoute(e, t, n = {}, r = e) { const { resolveProviderIdForAuth: i } = await import("./provider-auth-aliases-BW69I6bH.mjs"); const a = resolveAmbientOwnerAgentId(e, t); const o = await resolveSystemAgentConfiguredRouteFromConfig(e, a, n); const s = listAgentEntries(e).find((e) => normalizeAgentId(e.id) === a); const c = listAgentEntries(o?.runConfig ?? {}).find((e) => normalizeAgentId(e.id) === SYSTEM_AGENT_ID); const l = e.agents?.defaults; const u = normalizeProviderId(o?.modelLabel.split("/", 1)[0] ?? ""); const d = new Set([u, normalizeProviderId(o?.provider ?? "")].filter(Boolean)); const f = n.pluginMetadataPlugins ? { plugins: n.pluginMetadataPlugins } : void 0; const p = { config: e, ...f ? { metadataSnapshot: f } : {} }; const m = new Set([...d].map((e) => i(e, p))); const h = Object.fromEntries(Object.entries(e.auth?.profiles ?? {}).filter(([, e]) => m.has(i(e.provider, { ...p, storedCredential: true })))); const g = Object.fromEntries(Object.entries(e.auth?.order ?? {}).filter(([e]) => m.has(i(e, p)))); const _ = Object.fromEntries(Object.entries(e.models?.providers ?? {}).filter(([e]) => d.has(normalizeProviderId(e))).map(([e, t]) => [e, structuredClone(t)])); const v = o?.modelTarget === "utility" ? resolveAgentModelPrimaryValue(s?.utilityModel) ?? resolveAgentModelPrimaryValue(l?.utilityModel) : typeof s?.model === "string" ? s.model : s?.model?.primary || (typeof l?.model === "string" ? l.model : l?.model?.primary); const y = s ? { model: structuredClone(s.model), ...o?.modelTarget === "utility" ? { utilityModel: structuredClone(s.utilityModel) } : {}, params: structuredClone(s.params), tools: structuredClone(s.tools), models: projectRelevantModelMap({ models: s.models, providerIds: d, modelId: o?.model, rawModel: v }) } : void 0; const b = y !== void 0 && Object.values(y).some((e) => e !== void 0); let x = null; if (o) { const { runConfig: e, sourceConfig: t, ...n } = o; x = n; } return { route: x, defaultSelection: { explicitIds: [a] }, auth: { profiles: h, order: g }, models: { mode: e.models?.mode, providers: _ }, defaults: { model: structuredClone(l?.model), ...o?.modelTarget === "utility" ? { utilityModel: structuredClone(l?.utilityModel) } : {}, params: structuredClone(l?.params), models: projectRelevantModelMap({ models: l?.models, providerIds: d, modelId: o?.model, rawModel: v }) }, ...s && b ? { agent: { id: normalizeAgentId(s.id), ...y } } : {}, ...c ? { executionAgent: { id: SYSTEM_AGENT_ID, params: structuredClone(c.params), tools: structuredClone(c.tools) } } : {}, env: structuredClone(e.env), secrets: structuredClone(e.secrets), plugins: structuredClone(r.plugins), tools: structuredClone(e.tools) }; } function withoutAgentIdentity(e) { const t = isRecord(e.agent) ? { ...e.agent, id: "<agent>" } : e.agent; return { ...e, route: e.route ? { ...e.route, agentId: "<agent>", agentDir: "<agent-dir>" } : null, defaultSelection: { explicitIds: [] }, ...t ? { agent: t } : {} }; } function sameSetupInferenceRoute(e, t, n) { return n ? isDeepStrictEqual(withoutAgentIdentity(e), withoutAgentIdentity(t)) : isDeepStrictEqual(e, t); } function sameSetupConfiguredRoute(e, t, n) { if (!n) return isDeepStrictEqual(e, t); const normalize = (e) => e ? { ...e, agentId: "<agent>", agentDir: "<agent-dir>" } : null; return isDeepStrictEqual(normalize(e), normalize(t)); } //#endregion export { sameSetupInferenceRoute as i, resolveSystemAgentConfiguredRouteFromConfig as n, sameSetupConfiguredRoute as r, projectInferenceRoute as t };