UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

254 lines (253 loc) • 9.29 kB
import { r as normalizeProviderId } from "./provider-id-b5XGSf9C.mjs"; import { i as parseModelCatalogRef } from "./model-catalog-refs-Bk2MNoCv.mjs"; import { r as isDeeplyFrozenPlainData } from "./immutable-data-Y7xVjZoy.mjs"; import { A as tryResolveLegacyCompatibilityAgentId, C as resolveSessionAgentIds, b as resolveNativeModelPrimary, o as resolveAgentEntry } from "./agent-scope-config-DmXgFGKP.mjs"; import { a as getRuntimeConfigSnapshot, o as getRuntimeConfigSnapshotMetadata } from "./runtime-snapshot-WbwbY5b7.mjs"; import { s as resolveAgentModelPrimaryValue } from "./model-input-DrM4DWHG.mjs"; import { t as splitTrailingAuthProfile } from "./model-ref-profile-C7DWqVP0.mjs"; import { a as resolveMergedModelProviderConfig } from "./model-provider-config-Bbah_2ON.mjs"; import "./legacy.default-agent-owner-DpmHgK0A.mjs"; import { a as normalizeOptionalAgentRuntimeId, r as isDefaultAgentRuntimeId } from "./agent-runtime-id-D8s1CaYB.mjs"; import { i as resolveProviderModelPolicySurface } from "./provider-model-routes-CbP035Gi.mjs"; import { r as resolveProviderModelRouteAuthRequirement } from "./provider-model-route-auth-dD2KD9io.mjs"; //#region src/agents/model-auth-policy.ts /** Credential renewal and inference authorization are independent provider contracts. */ function resolveProviderModelAuthPolicy(e) { return resolveProviderModelPolicySurface(e.provider)?.resolveModelAuthPolicy?.(e) ?? { authRequirement: resolveProviderModelRouteAuthRequirement(e.mode) ?? null, compatible: true }; } function policyForModel(e) { return resolveProviderModelAuthPolicy({ provider: e.provider, mode: e.mode, authFlow: e.authFlow, capability: e.capability, api: e.modelApi, baseUrl: e.modelBaseUrl }); } function isAuthModeAllowedForModel(e) { return policyForModel(e).compatible; } function assertAuthModeAllowedForModel(e) { const t = policyForModel(e); if (t.compatible) return; throw new Error(`Auth profile "${e.profileId}" uses ${e.mode} auth, but ${e.provider}/${e.capability ?? e.modelApi ?? "inference"} ${t.incompatibilityReason ?? "does not accept this credential"}.`); } //#endregion //#region src/agents/model-runtime-policy.ts /** * Model runtime policy resolution. * * Agent execution uses this to choose a model/provider-specific runtime policy * from agent entries, model catalog config, provider config, or QA overrides. */ /** Resolve request hints; prepared owner facts never re-admit a canonical sentinel. */ function resolveAgentRuntimePolicyAgentId(e) { if (e.agentScope?.kind === "prepared") return e.agentScope.agentId; return e.config && (e.agentId?.trim() || e.sessionKey?.trim()) ? resolveSessionAgentIds({ config: e.config, agentId: e.agentId, sessionKey: e.sessionKey }).sessionAgentId : e.agentId; } const agentModelPolicyIndexes = /* @__PURE__ */ new WeakMap(); function readAgentModelPolicyIndex(e, t) { const n = t === getRuntimeConfigSnapshot() ? getRuntimeConfigSnapshotMetadata() : null; const r = n !== null || isDeeplyFrozenPlainData(e); const i = agentModelPolicyIndexes.get(e); if (r && i && i.publication === n) return i.index; const a = /* @__PURE__ */ new Map(); for (const [t, n] of Object.entries(e)) { const e = n?.agentRuntime; if (!hasRuntimePolicy(e)) continue; const r = parseModelCatalogRef(t.trim()); const i = { entryId: t, provider: r?.provider ?? "", policy: e }; for (const e of r ? [t.trim(), r.modelId] : [t.trim()]) { const t = a.get(e); if (t) t.push(i); else a.set(e, [i]); } } if (r) agentModelPolicyIndexes.set(e, { publication: n, index: a }); return a; } function hasRuntimePolicy(e) { return Boolean(e?.id?.trim()); } function normalizeModelIdForProvider(e, t) { const n = t?.trim(); if (!n) return; const r = n.indexOf("/"); if (r <= 0) return n; const i = normalizeProviderId(n.slice(0, r)); const a = normalizeProviderId(e ?? ""); if (a && i !== a) return n; return n.slice(r + 1).trim() || void 0; } function resolveEffectiveProvider(e, t) { const n = normalizeProviderId(e ?? ""); if (n) return n; return parseModelCatalogRef(t?.trim() ?? "")?.provider; } function resolvePolicyMatch(e, t) { const n = t ? e.filter((e) => e.provider === t) : []; const r = n.length > 0 ? n : e; const [i] = r; if (!i) return {}; if (!t && r.some((e) => e.provider !== i.provider)) return { ambiguous: true }; return { policy: i.policy, source: "model", matchedProvider: i.provider || t }; } function modelEntryMatchKind(e) { const t = e.entryId.trim(); if (t === e.modelId) return "exact"; const n = parseModelCatalogRef(t); if (!n) return "none"; const r = normalizeProviderId(e.provider ?? ""); if (r && n.provider !== r) return "none"; if (n.modelId === e.modelId) return "exact"; if (n.modelId === "*") return "provider-wildcard"; return "none"; } function resolveAgentModelEntryRuntimePolicy(e) { const t = normalizeModelIdForProvider(e.provider, e.modelId); if (!e.config || !t && e.matchKind !== "provider-wildcard") return {}; const n = [(e.agentId ? resolveAgentEntry(e.config, e.agentId) : void 0)?.models, e.config.agents?.defaults?.models]; const r = normalizeProviderId(e.provider ?? ""); for (const i of n) { const n = []; if (!i) continue; const a = e.matchKind === "provider-wildcard" ? "*" : t ?? ""; for (const r of readAgentModelPolicyIndex(i, e.config).get(a) ?? []) { if (!(modelEntryMatchKind({ entryId: r.entryId, provider: e.provider, modelId: t ?? "" }) === e.matchKind)) continue; n.push(r); } const o = resolvePolicyMatch(n, r); if (o.policy || o.ambiguous) return o; } return {}; } function resolveModelConfig(e) { const t = normalizeModelIdForProvider(e.provider, e.modelId); const n = e.providerConfig?.models; if (!t || !n) return; return n.find((n) => { const r = n.id.trim(); return r === t || r.endsWith(t) && modelEntryMatchKind({ entryId: r, provider: e.provider, modelId: t }) === "exact"; }); } /** Resolves the effective runtime policy for an agent/model/provider selection. */ function resolveModelRuntimePolicy(e) { const t = normalizeProviderId(e.provider ?? ""); const n = resolveEffectiveProvider(e.provider, e.modelId); const r = t ? void 0 : n; if (process.env.OPENCLAW_BUILD_PRIVATE_QA === "1") { const e = process.env.OPENCLAW_QA_FORCE_RUNTIME?.trim().toLowerCase(); if (e === "openclaw" || e === "codex") return { policy: { id: e }, source: "model", forcedByEnvironment: true }; } const i = Boolean(e.agentScope || e.agentId?.trim() || e.sessionKey?.trim()) ? resolveAgentRuntimePolicyAgentId(e) : e.config && tryResolveLegacyCompatibilityAgentId(e.config); const a = resolveAgentModelEntryRuntimePolicy({ ...e, agentId: i, provider: n, matchKind: "exact" }); if (a.ambiguous) return {}; if (a.policy) return a; const o = n ? resolveMergedModelProviderConfig(e.config, n) : void 0; const s = resolveModelConfig({ providerConfig: o, provider: n, modelId: e.modelId }); if (hasRuntimePolicy(s?.agentRuntime)) return { policy: s?.agentRuntime, source: "model", ...r ? { matchedProvider: r } : {} }; const c = resolveAgentModelEntryRuntimePolicy({ ...e, agentId: i, provider: n, matchKind: "provider-wildcard" }); if (c.policy) return c; if (hasRuntimePolicy(o?.agentRuntime)) return { policy: o?.agentRuntime, source: "provider", ...r ? { matchedProvider: r } : {} }; return {}; } /** Projects authored routing intent without changing harness compatibility or selection. */ function resolveModelRouteIntent(e) { const resolveIntent = (t, n, r, i) => { const a = n && t ? resolveProviderModelAuthPolicy({ provider: t, mode: e.config?.auth?.profiles?.[n]?.mode ?? e.resolveProfileAuthMode?.(n), authFlow: e.resolveProfileAuthFlow?.(n) }).authRequirement : void 0; const o = r && !isDefaultAgentRuntimeId(r) ? r : void 0; if (a) return { ...o ? { runtimeId: o } : {}, authRequirement: a, source: i }; return o ? { runtimeId: o, source: i } : void 0; }; const t = splitTrailingAuthProfile(e.modelId ?? ""); const n = resolveEffectiveProvider(e.provider, t.model); const r = e.runtimePolicy ?? resolveModelRuntimePolicy({ ...e, modelId: t.model }); const i = normalizeOptionalAgentRuntimeId(r.policy?.id); const a = resolveIntent(n, t.profile, i, "explicit"); if (a) return a; if (!e.config) return; const o = resolveAgentRuntimePolicyAgentId(e); const s = o ? resolveNativeModelPrimary(e.config, o) : resolveAgentModelPrimaryValue(e.config.agents?.defaults?.model); const c = s ? splitTrailingAuthProfile(s) : void 0; const l = e.primaryModel ? { provider: e.primaryModel.provider, modelId: splitTrailingAuthProfile(e.primaryModel.model).model } : c ? parseModelCatalogRef(c.model) : null; if (!l || l.provider !== n) return; const u = resolveModelRuntimePolicy({ ...e, provider: l.provider, modelId: l.modelId }); const d = normalizeOptionalAgentRuntimeId(u.policy?.id); return resolveIntent(l.provider, c?.profile, d, "inherited"); } //#endregion export { isAuthModeAllowedForModel as a, assertAuthModeAllowedForModel as i, resolveModelRouteIntent as n, resolveProviderModelAuthPolicy as o, resolveModelRuntimePolicy as r, resolveAgentRuntimePolicyAgentId as t };