openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
158 lines (157 loc) • 6.4 kB
JavaScript
import { i as listAgentIds, n as listAgentEntries, p as tryResolveSoleAgentId } from "./agent-roster-t1ooUdXp.mjs";
import { a as resolveAgentDir, f as resolveAgentWorkspaceDir } from "./agent-scope-config-DmXgFGKP.mjs";
import { a as visitModelSelectorRefs, r as collectConfiguredModelRefs } from "./configured-model-refs-DtaDJ1Vw.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 { a as resolveAgentEffectiveModelPrimary, f as resolveSubagentModelConfigSelectionResult, l as resolveAgentModelFallbacksOverride, m as resolveSubagentSpawnModelFallbacksOverride } from "./agent-scope-BwB8YH6y.mjs";
import { r as resolveProviderModelRouteAuthRequirement } from "./provider-model-route-auth-dD2KD9io.mjs";
import { h as resolveConfiguredModelRef, i as buildModelAliasIndex, y as resolveModelRefFromString } from "./model-selection-shared-9Kz63Hhm.mjs";
import { p as loadAuthProfileStoreForSecretsRuntime } from "./store-runtime-dW0pXLJI.mjs";
import { r as sanitizeDoctorNote } from "./emit-notes-llcc2fq-.mjs";
import { t as createModelAuthAvailabilityResolver } from "./model-auth-availability-DC3d9ngc.mjs";
import { i as resolveLegacyHeartbeatAgents } from "./doctor-heartbeat-legacy-DmzlheVU.mjs";
//#region src/commands/doctor/shared/model-billing-route-migration.ts
function collectModelConsumers(e) {
const t = listAgentEntries(e);
const n = collectConfiguredModelRefs(e).filter(({ path: e, value: t }) => !e.startsWith("agents.") && !e.endsWith(`.models.${t}`));
const r = new Map(resolveLegacyHeartbeatAgents(e).filter((e) => e.heartbeat !== void 0).map((e) => [e.agentId, e.heartbeat]));
for (const i of listAgentIds(e)) {
const a = t.find((e) => e.id === i);
const o = resolveAgentEffectiveModelPrimary(e, i);
const s = `agents.entries.${i}`;
const add = (e, t, r = e) => n.push({
path: e,
value: t,
agentId: i,
consumer: `${r} (agent ${i})`
});
visitModelSelectorRefs({
primary: o,
fallbacks: resolveAgentModelFallbacksOverride(e, i) ?? (typeof e.agents?.defaults?.model === "object" ? e.agents.defaults.model.fallbacks : void 0)
}, `${s}.model`, (e, t) => add(e, t));
if (r.has(i)) {
const e = r.get(i)?.model ?? o;
if (e) add(`${s}.heartbeat.model`, e, a?.heartbeat?.model ? `${s}.heartbeat.model` : "agents.defaults.heartbeat.model");
}
const c = resolveSubagentModelConfigSelectionResult({
cfg: e,
agentId: i
});
visitModelSelectorRefs(c?.raw ?? e.agents?.defaults?.model, `${s}.subagents.model`, (e, t, n) => {
if (n === "primary") add(e, t, c?.source === "default-subagent" ? e.replace(s, "agents.defaults") : e);
});
for (const [t, n] of (resolveSubagentSpawnModelFallbacksOverride(e, i) ?? []).entries()) add(`${s}.subagents.model.fallbacks.${t}`, n);
if (a) {
for (const e of collectConfiguredModelRefs({ agents: { entries: { [i]: a } } })) if (e.capability !== "decision" && !e.path.endsWith(`.models.${e.value}`) && !e.path.startsWith(`${s}.model`) && e.path !== `${s}.heartbeat.model` && !e.path.startsWith(`${s}.subagents.model`)) add(e.path, e.value);
}
}
return n;
}
/** Only providers with catalog routes report `selectedRoute`; others bill by credential kind. */
function resolveBillingRequirement(e) {
if (e.runtimeAuth?.source === "native" && !e.selectedProfileId) return "native";
if (e.availability !== true) return;
return resolveProviderModelRouteAuthRequirement(e.selectedAuthMode, e.selectedRoute?.authRequirement);
}
function collectBillingRoutes(e) {
const { cfg: t, env: n, metadataSnapshot: r } = e;
const i = /* @__PURE__ */ new Map();
const prepareOwner = (i) => {
const a = i ? resolveAgentDir(t, i, n) : void 0;
const o = i ? resolveAgentWorkspaceDir(t, i, n) : void 0;
let s = e.authStores.get(a);
if (!s) {
s = loadAuthProfileStoreForSecretsRuntime(a, {
config: t,
externalCli: { mode: "none" }
});
e.authStores.set(a, s);
}
const c = {
cfg: t,
agentId: i,
manifestPlugins: r?.plugins,
allowManifestNormalization: false,
allowPluginNormalization: false
};
const l = resolveConfiguredModelRef({
...c,
defaultProvider: DEFAULT_PROVIDER,
defaultModel: DEFAULT_MODEL
}).provider;
const u = buildModelAliasIndex({
...c,
defaultProvider: l
});
return {
resolve: (e) => resolveModelRefFromString({
...c,
raw: e,
defaultProvider: l,
aliasIndex: u
})?.ref,
auth: createModelAuthAvailabilityResolver({
cfg: t,
agentId: i,
agentDir: a,
workspaceDir: o,
env: n,
metadataSnapshot: r,
externalCliProviderIds: [],
authStore: s
})
};
};
const a = /* @__PURE__ */ new Map();
for (const { path: e, value: n, consumer: r, agentId: o } of collectModelConsumers(t)) {
const s = o ?? tryResolveSoleAgentId(t);
let c = i.get(s);
if (!c) {
c = prepareOwner(s);
i.set(s, c);
}
const l = splitTrailingAuthProfile(n);
const u = c.resolve(l.model);
if (!u) continue;
const d = c.auth.evaluateRuntimeModelAuth(u.provider, {
modelId: u.model,
pinnedProfileId: l.profile
});
const f = resolveBillingRequirement(d);
if (!f) continue;
a.set(e, {
consumer: r ?? e,
modelRef: `${u.provider}/${u.model}`,
requirement: f,
profileId: d.selectedProfileId,
runtime: d.runtimeAuth?.id
});
}
return a;
}
/** Compare resolved billing facts; missing credentials are not evidence of a route change. */
function collectModelBillingRouteMigrationWarnings(e) {
const t = /* @__PURE__ */ new Map();
const n = {
env: process.env,
metadataSnapshot: e.metadataSnapshot,
authStores: t
};
const r = collectBillingRoutes({
...n,
cfg: e.before
});
const i = collectBillingRoutes({
...n,
cfg: e.after
});
const describe = (e) => `${e.modelRef} via ${e.requirement === "native" ? `${e.runtime ?? "runtime"} own credentials` : e.requirement === "api-key" ? "metered API-key" : "subscription/OAuth"}${e.profileId ? ` profile ${e.profileId}` : ""}`;
const a = [];
for (const [e, t] of r) {
const n = i.get(e);
if (n && n.requirement !== t.requirement) a.push(sanitizeDoctorNote(`Billing route changed for ${t.consumer}: ${describe(t)} -> ${describe(n)}. Review its model and authentication settings.`));
}
return a;
}
//#endregion
export { collectModelBillingRouteMigrationWarnings };