openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
119 lines (118 loc) • 4.86 kB
JavaScript
import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs";
import "./session-key-CqAyZeVk.mjs";
import { n as listAgentEntries } from "./agent-roster-t1ooUdXp.mjs";
import { E as toAgentEntriesRecord, m as resolveAmbientOwnerAgentId } from "./agent-scope-config-DmXgFGKP.mjs";
import { n as createMergePatch, t as applyMergePatch } from "./merge-patch-D-7aajuy.mjs";
import { p as resolveConfigSnapshotHash } from "./io.read-helpers-qAcqsRC0.mjs";
import { r as hasResolvedRosterBeforeMigrations } from "./agent-roster-provenance-DiT1dDed.mjs";
import { s as readConfigFileSnapshot } from "./io.runtime-DsJ2dLCQ.mjs";
import "./config-CMA-J084.mjs";
import { t as migrateLegacyMainSessionKeys } from "./legacy-main-session-migration-DtdSvuXY.mjs";
import { n as createAgent, r as validateAgentIdInput } from "./agent-create-l2Gq2JQH.mjs";
//#region src/commands/onboard-agent.ts
function validateFirstOnboardingAgentName(e) {
const t = e?.trim();
if (!t) return "Agent name is required.";
const n = validateAgentIdInput(t);
return n.ok ? void 0 : `${n.message}. Choose another name.`;
}
function mergeOnboardingCandidate(e) {
const t = createMergePatch(e.base, e.candidate);
const n = applyMergePatch(e.currentRuntime, t);
return {
...n,
agents: {
...n.agents,
entries: toAgentEntriesRecord(listAgentEntries(e.currentRuntime))
}
};
}
async function ensureOnboardingAgent(e) {
if (e.firstAgent) {
const t = validateFirstOnboardingAgentName(e.firstAgent.name);
if (t) throw new Error(t);
}
const t = Object.hasOwn(e, "expectedConfigHash");
let n = t ? await readConfigFileSnapshot() : void 0;
if (n?.exists && !n.valid) throw new Error("Cannot create the first agent from an invalid OpenClaw config.");
if (n && (resolveConfigSnapshotHash(n) ?? null) !== e.expectedConfigHash) throw new Error("OpenClaw config changed before first-agent creation. Retry setup.");
const r = listAgentEntries(e.config);
const i = r[0];
const a = r.length > 0 && (e.preserveCandidateRoster || r.length !== 1 || i?.id !== "main" || Object.keys(i).some((e) => e !== "id"));
if (e.firstAgent?.team) {
n ??= await readConfigFileSnapshot();
if (a || hasResolvedRosterBeforeMigrations(n)) throw new Error("The requested team was not created because an agent roster already exists. Use `openclaw agents team create` to add a team.");
}
if (a) return {
config: e.config,
configBase: e.baseConfig ?? e.config,
agentId: resolveAmbientOwnerAgentId(e.config),
bootstrapPending: false,
createdAgent: false
};
n ??= await readConfigFileSnapshot();
if (n.exists && !n.valid) throw new Error("Cannot create the first agent from an invalid OpenClaw config.");
const o = n.config;
const s = e.baseConfig ?? o;
if (n.exists && hasResolvedRosterBeforeMigrations(n)) return {
config: mergeOnboardingCandidate({
base: s,
candidate: e.config,
currentRuntime: o
}),
configBase: o,
agentId: resolveAmbientOwnerAgentId(o),
bootstrapPending: false,
createdAgent: false
};
const c = e.firstAgent ? e.firstAgent.name.trim() : "main";
const l = normalizeAgentId(c);
const { resolveDefaultProactiveCadenceMs: u } = await import("./default-proactive-job-HLb7iPB_.mjs");
const d = {
bootstrapFirstAgent: true,
...t ? { expectedConfigHash: e.expectedConfigHash } : {},
beforePersistentApply: e.beforePersistentApply
};
const f = e.firstAgent?.team ? await (await import("./agent-team-uQ1HQtL-.mjs")).createAgentTeam({
...d,
coordinator: c,
workspaceRoot: e.workspace
}) : await createAgent({
...d,
proactiveCadenceMs: u(e.config, l),
entry: {
id: l,
name: c,
workspace: e.workspace
},
bootstrapMain: normalizeAgentId(c) === "main",
skipBootstrap: e.config.agents?.defaults?.skipBootstrap,
skipOptionalBootstrapFiles: e.config.agents?.defaults?.skipOptionalBootstrapFiles
});
if (f.status === "error") throw new Error(f.message);
const p = "coordinatorId" in f;
const m = await readConfigFileSnapshot();
if (!m.valid) throw new Error("Agent creation wrote an invalid OpenClaw config.");
if (f.configHash && m.hash !== f.configHash) throw new Error("OpenClaw config changed after first-agent creation. Retry setup.");
const h = mergeOnboardingCandidate({
base: s,
candidate: e.config,
currentRuntime: m.config
});
const g = (await migrateLegacyMainSessionKeys({
cfg: m.config,
mode: "detect"
})).warnings;
return {
config: h,
configBase: m.config,
agentId: p ? f.coordinatorId : f.agentId,
bootstrapPending: p ? false : f.bootstrapPending,
createdAgentIds: p ? f.agents.map((e) => e.agentId) : [f.agentId],
createdAgent: f.status === "created",
...f.configHash ? { configHash: f.configHash } : {},
...g.length > 0 ? { sessionMigrationWarnings: g } : {}
};
}
//#endregion
export { validateFirstOnboardingAgentName as n, ensureOnboardingAgent as t };