openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
139 lines (138 loc) • 5.32 kB
JavaScript
import "./model-ref-shared-DqE2RqZv.mjs";
import { r as normalizeProviderId } from "./provider-id-b5XGSf9C.mjs";
import { i as materializeModelPolicyAllowlist, s as parseModelPolicyWildcardRef } from "./model-policy-allowlist-migration-Dd1EwQdo.mjs";
import { y as resolveMutableAgentEntry } from "./agent-scope-config-DmXgFGKP.mjs";
import { r as normalizeAgentModelRefForConfig } from "./model-input-DrM4DWHG.mjs";
import { m as resolveConfiguredModelPolicyAllow, t as LEGACY_MODEL_POLICY_ALLOW_CONFIG_PATH } from "./model-selection-shared-9Kz63Hhm.mjs";
import { S as attachRuntimeConfigWriteApplication, w as createRuntimeConfigWriteApplication } from "./io.runtime-DsJ2dLCQ.mjs";
import { a as captureGatewayRootWorkAdmissionContinuationScope } from "./gateway-work-admission-79pid1hQ.mjs";
import { t as applyPrimaryModel } from "./provider-model-primary-C2E-I43s.mjs";
import { r as logConfigUpdated } from "./logging-D0RirHBi.mjs";
import { l as resolveModelsTargetAgent, u as updateConfig } from "./shared-jZuoMzdG.mjs";
import { isDeepStrictEqual } from "node:util";
//#region src/commands/models/auth-model-policy.ts
var ProviderModelPolicyChangedError = class extends Error {
constructor(e) {
super("Model restrictions changed during sign-in. Choose model access again.");
this.config = e;
}
};
function applyProviderLoginDefaultModel(e, t) {
const n = applyPrimaryModel(e, t);
if (resolveConfiguredModelPolicyAllow({ cfg: e }).configPath === "agents.defaults.models") {
n.agents ??= {};
n.agents.defaults ??= {};
n.agents.defaults.models = e.agents?.defaults?.models;
}
return n;
}
/** Provider setup defaults cannot supply consent to change model restrictions. */
function withoutProviderModelPolicy(e, t) {
const n = structuredClone(e);
if (n.agents?.defaults) {
delete n.agents.defaults.modelPolicy;
if (resolveConfiguredModelPolicyAllow({ cfg: t }).configPath === "agents.defaults.models") delete n.agents.defaults.models;
}
for (const e of Object.values(n.agents?.entries ?? {})) delete e.modelPolicy;
return n;
}
function snapshotPolicy(e, t) {
const n = resolveConfiguredModelPolicyAllow({
cfg: e,
agentId: t
});
return {
path: n.configPath ? n.repairConfigPath : null,
refs: [...new Set(n.refs.map((e) => parseModelPolicyWildcardRef(e)?.key ?? normalizeAgentModelRefForConfig(e)))].toSorted()
};
}
function prepareProviderModelAccess(e) {
const t = normalizeProviderId(e.provider);
const n = snapshotPolicy(e.config, e.agentId);
if (n.refs.length === 0 || n.refs.includes(`${t}/*`)) return;
const r = {
message: `Your current model restrictions may hide ${e.providerLabel} models. Choose which models to show.`,
initialValue: "keep",
options: [{
value: "all",
label: `Show all ${e.providerLabel} models`
}, {
value: "keep",
label: "Keep current restrictions"
}]
};
return {
provider: t,
providerLabel: e.providerLabel,
agentId: e.agentId,
policy: n,
prompt: r
};
}
async function completeProviderModelAccess(e) {
const t = e.prepared;
if (!t) return {
kind: "unchanged",
message: ""
};
e.assertCurrent?.();
if (e.onRequested) {
e.onRequested(t);
return {
kind: "deferred",
message: ""
};
}
const n = await e.prompter.select(t.prompt);
e.assertCurrent?.();
if (n !== "all") {
e.runtime.log("Current model restrictions kept.");
return {
kind: "unchanged",
message: "Current model restrictions kept."
};
}
const r = createRuntimeConfigWriteApplication(captureGatewayRootWorkAdmissionContinuationScope()?.run);
await updateConfig((n) => {
e.assertCurrent?.(n);
resolveModelsTargetAgent(n, t.agentId, { kind: "mutation" });
if (!isDeepStrictEqual(snapshotPolicy(n, t.agentId), t.policy)) throw new ProviderModelPolicyChangedError(n);
const r = resolveConfiguredModelPolicyAllow({
cfg: n,
agentId: t.agentId
});
const i = [...r.refs, `${t.provider}/*`];
if (r.repairConfigPath === "agents.entries.*.modelPolicy.allow") {
const e = resolveMutableAgentEntry(n, t.agentId);
if (!e) throw new Error(`Agent "${t.agentId}" no longer exists.`);
e.modelPolicy = {
...e.modelPolicy,
allow: i
};
} else {
n.agents ??= {};
n.agents.defaults ??= {};
const e = n.agents.defaults;
if (r.configPath === "agents.defaults.models" && materializeModelPolicyAllowlist(n).kind === "deferred") e.models = {
...e.models,
[`${t.provider}/*`]: {}
};
else e.modelPolicy = {
...e.modelPolicy,
allow: i
};
}
return n;
}, void 0, e.beforeCommit, attachRuntimeConfigWriteApplication({ assertCurrent: e.assertCurrent }, r));
const i = r.claimed ? await r.result : "unclaimed";
logConfigUpdated(e.runtime);
const a = i === "applied" ? `All ${t.providerLabel} models are now visible.` : r.claimed ? "Model access was saved, but OpenClaw has not confirmed it is active. Open Settings and select Apply changes, then send /models." : "Model access saved. Application by the running Gateway is not confirmed. Run `openclaw gateway restart` to apply it.";
e.runtime.log(a);
return {
kind: "saved",
application: i,
message: a
};
}
//#endregion
export { withoutProviderModelPolicy as a, prepareProviderModelAccess as i, applyProviderLoginDefaultModel as n, completeProviderModelAccess as r, ProviderModelPolicyChangedError as t };