openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
129 lines (128 loc) • 5.19 kB
JavaScript
import { n as listAgentEntries, p as tryResolveSoleAgentId, t as hasAgentRosterProperty } from "./agent-roster-t1ooUdXp.mjs";
import { u as resolveSessionTranscriptsDirForAgent } from "./paths-DvQ5AJvD.mjs";
import { a as resolveAgentDir, f as resolveAgentWorkspaceDir } from "./agent-scope-config-DmXgFGKP.mjs";
import { t as splitTrailingAuthProfile } from "./model-ref-profile-C7DWqVP0.mjs";
import "./agent-scope-BwB8YH6y.mjs";
import { C as copyRuntimeConfigWriteApplication } from "./io.runtime-DsJ2dLCQ.mjs";
import { n as mutateConfigFileWithRetry, o as transformConfigFileWithRetry } from "./mutate-DhAOi0-2.mjs";
import "./config-CMA-J084.mjs";
import "./sessions-D5Vvu3AE.mjs";
import { i as pruneAgentConfig, r as findAgentEntryIndex, t as applyAgentConfig } from "./agents.config-T4w0h7rr.mjs";
import { n as createAgent } from "./agent-create-l2Gq2JQH.mjs";
//#region src/gateway/server-methods/agents-config-mutations.ts
var AgentConfigPreconditionError = class extends Error {};
var AgentModelSelectionError = class extends Error {};
function isModelOnlyUpdate(e) {
return Boolean(e.model) && e.name === void 0 && e.workspace === void 0 && e.identity === void 0;
}
function validateAgentModelSelectionUpdate(e) {
if (!e.agentRuntime) return;
if (!isModelOnlyUpdate(e) || e.emoji !== void 0 || e.avatar !== void 0 || !e.model) return "Runtime selection requires a model-only update.";
if (splitTrailingAuthProfile(e.model).profile) return "Choose a model without an OpenClaw sign-in override for this runtime.";
}
function isConfiguredAgent(e, t) {
return findAgentEntryIndex(listAgentEntries(e), t) >= 0;
}
function isImplicitAgentModelUpdate(e, t) {
return !hasAgentRosterProperty(e) && tryResolveSoleAgentId(e) === t.agentId && isModelOnlyUpdate(t) && t.agentRuntime !== void 0;
}
function createAgentConfigEntry(e, t) {
return createAgent({
...e,
transformConfig: (e) => transformConfigFileWithRetry({
...e,
writeOptions: copyRuntimeConfigWriteApplication(t, e.writeOptions ?? {})
})
});
}
async function updateAgentConfigEntry(e, t) {
const n = e.agentRuntime ? await Promise.all([
import("./model-runtime-choice-DDfgqzCb.mjs"),
import("./shared-BLkGfkPx.mjs"),
import("./setup-model-selection-Cwize44i.mjs")
]) : void 0;
let r;
await mutateConfigFileWithRetry({
afterWrite: { mode: "auto" },
writeOptions: copyRuntimeConfigWriteApplication(t, {
...e.identity ? { allowConfigSizeDrop: true } : {},
assertConfigPathForWrite: () => {
e.assertCurrent?.();
const t = r?.();
if (t) throw new AgentModelSelectionError(t);
}
}),
mutate: async (t) => {
r = void 0;
const i = isConfiguredAgent(t, e.agentId);
if (!i && !isImplicitAgentModelUpdate(t, e)) throw new AgentConfigPreconditionError(`agent "${e.agentId}" not found`);
let a = t;
if (e.model && n) {
const [o, s, c] = n;
const l = s.resolveModelTarget({
raw: e.model,
cfg: t
});
const u = await o.preparePublishedModelRuntimeChoice({
cfg: t,
agentId: e.agentId,
provider: l.provider,
model: l.model,
runtimeId: e.agentRuntime
});
if (u.kind === "unavailable") throw new AgentModelSelectionError(u.message);
r = u.validate;
a = await c.applySystemAgentModelSelection({
config: t,
model: e.model,
agentRuntimeId: u.runtimeId,
...i ? { targetAgentId: e.agentId } : { runtimeInDefaults: true }
});
}
const o = i ? applyAgentConfig(a, {
agentId: e.agentId,
...e.name ? { name: e.name } : {},
...e.workspace ? { workspace: e.workspace } : {},
...!e.agentRuntime && e.model !== void 0 ? { model: e.model } : {},
...e.identity ? { identity: e.identity } : {}
}) : a;
Object.assign(t, o);
}
});
}
/** Removes an agent entry and returns filesystem roots the caller should clean up. */
async function deleteAgentConfigEntry(e) {
const t = await mutateConfigFileWithRetry({
afterWrite: { mode: "auto" },
writeOptions: copyRuntimeConfigWriteApplication(e.writeOptions, {
allowedAgentRosterRemovals: [e.agentId],
assertConfigPathForWrite: e.assertCurrent,
beforeCommit: e.assertCurrentAsync,
allowConfigSizeDrop: true
}),
mutate: (t) => {
e.assertCurrent?.();
if (!isConfiguredAgent(t, e.agentId) && !e.allowMissing) throw new AgentConfigPreconditionError(`agent "${e.agentId}" not found`);
const n = listAgentEntries(t).find((t) => t.id === e.agentId);
if (n) e.validate?.(n);
const r = n ? resolveAgentWorkspaceDir(t, e.agentId) : "";
const i = resolveAgentDir(t, e.agentId);
const a = resolveSessionTranscriptsDirForAgent(e.agentId);
const o = pruneAgentConfig(t, e.agentId);
Object.assign(t, o.config);
if (!n) return;
return {
workspaceDir: r,
agentDir: i,
sessionsDir: a,
removedBindings: o.removedBindings
};
}
});
return {
nextConfig: t.nextConfig,
result: t.result
};
}
//#endregion
export { isConfiguredAgent as a, validateAgentModelSelectionUpdate as c, deleteAgentConfigEntry as i, AgentModelSelectionError as n, isImplicitAgentModelUpdate as o, createAgentConfigEntry as r, updateAgentConfigEntry as s, AgentConfigPreconditionError as t };