UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

210 lines (209 loc) • 8.69 kB
import { _ as resolvePrimaryStringValue } from "./string-coerce-CCIiB4H7.mjs"; import { s as raceWithTimeout } from "./src-BPmQBLJp.mjs"; import { r as resolveAgentConfig, s as resolveAgentModelConfigForRuntime } from "./agent-scope-config-DmXgFGKP.mjs"; import { a as getRuntimeConfigSnapshot, c as getRuntimeConfigSourceSnapshot, w as selectApplicableRuntimeConfig } from "./runtime-snapshot-WbwbY5b7.mjs"; import { s as resolveAgentModelPrimaryValue, u as toAgentModelListLike } from "./model-input-DrM4DWHG.mjs"; import { i as DEFAULT_PROVIDER, n as DEFAULT_MODEL } from "./defaults-BWyoTPqn.mjs"; import { t as splitTrailingAuthProfile } from "./model-ref-profile-C7DWqVP0.mjs"; import { f as resolveSubagentModelConfigSelectionResult } from "./agent-scope-BwB8YH6y.mjs"; import { l as normalizeThinkLevel } from "./thinking.shared-jmDGV33r.mjs"; import { C as findModelInCatalog, h as resolveConfiguredModelRef, m as resolveConfiguredModelPolicyAllow, s as getModelRefStatus, v as resolveHooksGmailModel } from "./model-selection-shared-9Kz63Hhm.mjs"; import "./config-CMA-J084.mjs"; import { t as resolveAllowedModelRefCore } from "./model-selection-resolve-CyMfPqoi.mjs"; import "./thinking-Da4gHblr.mjs"; import { n as resolveConfiguredThinkingDefaultCore } from "./model-thinking-default-DTaWSs8g.mjs"; import { n as publishedModelCatalogOwnerMatchesAgent } from "./prepared-model-catalog-owner-Cf9g0CZ4.mjs"; import { r as normalizeThinkingCatalogProviders } from "./thinking-runtime-BKWl4CUs.mjs"; import { l as loadResolvedPublishedModelCatalogOwner, o as loadProviderScopedThinkingCatalog } from "./prepared-model-catalog-DSktrHGK.mjs"; //#region src/cron/isolated-agent/run-config.ts /** Selects the active reloadable config when it descends from the cron caller's snapshot. */ function resolveCronActiveRuntimeConfig(e) { const t = getRuntimeConfigSnapshot(); const n = getRuntimeConfigSourceSnapshot(); if (!t || !n) return e; return selectApplicableRuntimeConfig({ inputConfig: e, runtimeConfig: t, runtimeSourceConfig: n }) ?? e; } /** Selects the active runtime snapshot, then derives isolated cron agent defaults from it. */ function resolveCronAgentConfig(e) { const t = resolveCronActiveRuntimeConfig(e.config); const { model: n, sandbox: r, memory: i, models: a, params: o, ...s } = e.agentConfigOverride ?? {}; const c = resolveAgentModelConfigForRuntime(e.agentConfigOverride); const l = Object.fromEntries(Object.entries(s).filter(([, e]) => e !== void 0)); const u = { ...t.agents?.defaults, ...l }; const d = toAgentModelListLike(u.model) ?? {}; if (typeof c === "string") u.model = { ...d, primary: c }; else if (c) u.model = { ...d, ...c }; return { runtimeConfig: t, agentDefaults: u, cfgWithAgentDefaults: { ...t, agents: Object.assign({}, t.agents, { defaults: u }) } }; } //#endregion //#region src/cron/isolated-agent/model-selection.ts const CRON_THINKING_HYDRATION_WAIT_MS = 5e3; function formatCronPayloadModelRejection(e) { const { modelOverride: t, error: n } = e; if (n.startsWith("model not allowed:")) { const r = n.slice(18).trim(); const i = resolveConfiguredModelPolicyAllow(e); return `automation model override '${t}' rejected by ${i.configPath ?? "agents.defaults.modelPolicy.allow"}: ${r} is not in [${i.refs.length ? i.refs.toSorted().join(", ") : "(none configured)"}]`; } return `automation model override '${t}' rejected: ${n}`; } async function resolveCronModelSelectionOwner(e) { const t = e.publishedRuntime ? Object.freeze({ ...e.publishedRuntime, metadataSnapshot: e.publishedRuntime.pluginGeneration.pluginMetadataSnapshot, modelCatalog: e.publishedRuntime.readFullModelCatalog?.() ?? e.publishedRuntime.modelCatalog }) : await loadResolvedPublishedModelCatalogOwner({ config: e.cfg, ...e.agentId ? { agentId: e.agentId } : {}, ...e.agentDir ? { agentDir: e.agentDir } : {}, ...e.workspaceDir ? { workspaceDir: e.workspaceDir } : {}, readOnly: true, allowGatewaySubagentBinding: true }); if (e.requiredAgentId && !publishedModelCatalogOwnerMatchesAgent(t, e.requiredAgentId)) throw new Error(`cron model catalog owner changed from ${e.requiredAgentId} to ${t.agentId}`); return t; } async function resolveCronThinkingCatalog(e) { const t = normalizeThinkingCatalogProviders(e.owner.modelCatalog.entries); const n = findModelInCatalog(t, e.provider, e.model); if (e.agentRuntime === "openclaw" && n && (n.nativeRuntime === void 0 || n.nativeRuntime === "openclaw")) return t; const r = loadProviderScopedThinkingCatalog({ config: e.owner.config, provider: e.provider, model: e.model, agentRuntime: e.agentRuntime, agentId: e.owner.agentId, agentDir: e.owner.agentDir, workspaceDir: e.owner.workspaceDir }); const i = await raceWithTimeout(r.then(normalizeThinkingCatalogProviders), CRON_THINKING_HYDRATION_WAIT_MS, () => void 0, { ref: false }); return i && findModelInCatalog(i, e.provider, e.model) ? i : t; } async function resolveCronThinkingSelection(e) { const t = normalizeThinkLevel(e.jobThinking) ?? normalizeThinkLevel(e.hookThinking) ?? normalizeThinkLevel(e.sessionThinking); const n = t ?? resolveConfiguredThinkingDefaultCore({ cfg: e.cfg, agentId: e.owner.agentId, provider: e.provider, model: e.model }); return { catalog: n === "off" && e.agentRuntime === "openclaw" ? e.owner.modelCatalog.entries : await resolveCronThinkingCatalog(e), immutableThinkLevel: t, loadThinkingCatalog: async (t, n, r) => await resolveCronThinkingCatalog({ owner: e.owner, provider: t, model: n, agentRuntime: r }), requestedThinkLevel: n }; } /** Resolves the effective scheduled model across defaults, agents, hooks, payload, and session state. */ async function resolveCronModelSelection(e) { const t = e.owner ?? await resolveCronModelSelectionOwner({ cfg: e.cfg, ...e.agentId ? { agentId: e.agentId, requiredAgentId: e.agentId, agentDir: e.agentDir, workspaceDir: e.workspaceDir } : {} }); const n = t.agentId; const r = e.agentConfigOverride ? t.config === e.cfg && (!e.agentId || n === e.agentId) ? e.agentConfigOverride : resolveAgentConfig(t.config, n) : void 0; const { cfgWithAgentDefaults: i } = resolveCronAgentConfig({ config: t.config, agentConfigOverride: r }); const a = resolveConfiguredModelRef({ cfg: i, agentId: n, defaultProvider: DEFAULT_PROVIDER, defaultModel: DEFAULT_MODEL, manifestPlugins: t.metadataSnapshot }); const o = { cfg: t.config, catalog: t.modelCatalog.entries, defaultProvider: a.provider, defaultModel: a, agentId: n, manifestPlugins: t.metadataSnapshot }; const s = resolveAgentConfig(t.config, n); const c = typeof s?.model === "string" && resolveAgentModelPrimaryValue(s.model) === resolveAgentModelPrimaryValue(t.config.agents?.defaults?.model); const selection = (e, n, r) => { const a = splitTrailingAuthProfile(r ?? "").profile; return { ok: true, provider: e.provider, model: e.model, modelSource: n, useSubagentFallbacks: n === "subagent", inheritDefaultFallbacksForAgentStringModel: c && (n === "default" || n === "agent"), ...a ? { configuredProfileId: a } : {}, cfgWithAgentDefaults: i, owner: t }; }; const override = (e, r) => { if (!e) return; const i = resolveAllowedModelRefCore({ ...o, raw: e }); if (!("error" in i)) return selection(i.ref, r, r === "session" ? void 0 : e); return r === "payload" ? { ok: false, error: formatCronPayloadModelRejection({ cfg: t.config, agentId: n, modelOverride: e, error: i.error }) } : void 0; }; const l = override(typeof e.payloadModel === "string" ? e.payloadModel.trim() : void 0, "payload"); if (l) return l; const u = e.isGmailHook ? resolveHooksGmailModel({ cfg: t.config, defaultProvider: DEFAULT_PROVIDER, manifestPlugins: t.metadataSnapshot }) : null; if (u && getModelRefStatus({ ...o, ref: u }).allowed) return selection(u, "hook", t.config.hooks?.gmail?.model); const d = e.sessionEntry.modelOverride?.trim(); const f = override(d ? `${e.sessionEntry.providerOverride?.trim() || a.provider}/${d}` : void 0, "session"); if (f) return f; const p = resolveSubagentModelConfigSelectionResult({ cfg: t.config, agentId: n, agentConfigOverride: r }); return override(resolvePrimaryStringValue(p?.raw), p?.source === "agent" ? "agent" : "subagent") ?? selection(a, "default", resolveAgentModelPrimaryValue(i.agents?.defaults?.model)); } //#endregion export { resolveCronAgentConfig as a, resolveCronActiveRuntimeConfig as i, resolveCronModelSelectionOwner as n, resolveCronThinkingSelection as r, resolveCronModelSelection as t };