openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
142 lines (141 loc) • 5.41 kB
JavaScript
import { c as resolveUserPath } from "./home-dir-EKmgmdPG.mjs";
import "./utils-C8YECn2Q.mjs";
import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs";
import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs";
import "./session-key-CqAyZeVk.mjs";
import { n as listAgentEntries } from "./agent-roster-t1ooUdXp.mjs";
import "./agent-scope-config-DmXgFGKP.mjs";
import { t as resolveDefaultAgentWorkspaceDir } from "./workspace-default-path-CWX4WiCH.mjs";
import "./workspace-default-DvctYF3A.mjs";
import { p as resolveConfigSnapshotHash } from "./io.read-helpers-qAcqsRC0.mjs";
import { t as ConfigMutationConflictError } from "./mutation-conflict-Bs0nsNWn.mjs";
import { r as hasResolvedRosterBeforeMigrations } from "./agent-roster-provenance-DiT1dDed.mjs";
import { s as readConfigFileSnapshot } from "./io.runtime-DsJ2dLCQ.mjs";
import { a as readAgentDeletionJournalForCreation } from "./agent-deletion-journal.read-DjOO2RZM.mjs";
import { o as transformConfigFileWithRetry, s as withConfigMutationExclusive } from "./mutate-DhAOi0-2.mjs";
import "./config-CMA-J084.mjs";
import { i as validateAgentTeamMemberIds, n as loadAgentRole, r as loadAgentTeamPreset } from "./agent-roles-BJUCg-FF.mjs";
import { t as isUnconfiguredConfigSource } from "./fresh-install-config-6qQ4_7BH.mjs";
import { n as createAgent, r as validateAgentIdInput } from "./agent-create-l2Gq2JQH.mjs";
import path from "node:path";
//#region src/agents/agent-team.ts
/** Create a directed fleet through the same lifecycle owner as individual agents. */
async function createAgentTeam(e = {}) {
const t = await loadAgentTeamPreset(e.preset);
const n = [t.coordinator, ...t.specialists].map((t, n) => {
const r = n === 0 ? e.coordinator ?? t.id : t.id;
const i = validateAgentIdInput(e.prefix ? `${e.prefix}-${r}` : r);
if (!i.ok) throw new Error(i.message);
return {
role: t.role,
id: i.agentId
};
});
const r = n.map(({ id: e }) => e);
const i = validateAgentTeamMemberIds(r);
if (i) return {
status: "error",
message: i
};
await Promise.all(n.map(({ role: e }) => loadAgentRole(e)));
const a = r[0];
return await withConfigMutationExclusive(async (t) => {
const i = await readConfigFileSnapshot();
if (!i.valid) return {
status: "error",
message: "Cannot create a team from an invalid OpenClaw config."
};
if (Object.hasOwn(e, "expectedConfigHash") && (resolveConfigSnapshotHash(i) ?? null) !== e.expectedConfigHash) throw new ConfigMutationConflictError("config changed before team creation", { retryable: false });
const o = hasResolvedRosterBeforeMigrations(i);
if (e.bootstrapFirstAgent && o) return {
status: "error",
message: "Cannot create the first team: an agent roster already exists."
};
const s = !o && (e.bootstrapFirstAgent === true || !i.exists || isUnconfiguredConfigSource(i.sourceConfigBeforeMigrations ?? t));
const c = new Set(s ? [] : listAgentEntries(t).map(({ id: e }) => normalizeAgentId(e)));
const l = r.filter((e) => c.has(e));
if (l.length) return {
status: "error",
message: `Agents already exist: ${l.join(", ")}. Choose another coordinator or --prefix.`
};
const u = await Promise.all(r.map((e) => readAgentDeletionJournalForCreation(e)));
const d = r.filter((e, t) => {
const n = u[t];
return n && !n.cleanupCompleted;
});
if (d.length) return {
status: "error",
message: `Agent deletion cleanup is pending: ${d.join(", ")}.`
};
const f = resolveUserPath(e.workspaceRoot?.trim() || t.agents?.defaults?.workspace || resolveDefaultAgentWorkspaceDir());
const p = t.agents?.defaults?.systemAgent?.agentId?.trim();
const m = p || a;
const h = [];
const fail = (e) => ({
status: "error",
message: `${e}${h.length ? ` Created agents retained: ${h.map(({ agentId: e }) => e).join(", ")}.` : ""}`,
...h.length ? { retainedAgents: h } : {}
});
let g = t;
let _;
try {
for (const [t, i] of n.entries()) {
const n = await createAgent({
role: i.role,
entry: {
id: i.id,
workspace: path.join(f, i.id),
subagents: t === 0 ? {
allowAgents: r.slice(1),
delegationMode: "prefer"
} : { allowAgents: [] }
},
bootstrapFirstAgent: t === 0 && s,
...t === 0 && Object.hasOwn(e, "expectedConfigHash") ? { expectedConfigHash: e.expectedConfigHash } : {},
beforePersistentApply: e.beforePersistentApply,
provenance: e.provenance,
onCommitted: ({ config: e, ...t }) => {
g = e;
_ = t.configHash;
h.push(t);
}
});
if (n.status === "error") return fail(n.message);
}
if (!p) {
const t = await transformConfigFileWithRetry({
maxAttempts: 1,
writeOptions: e.beforePersistentApply ? { assertConfigPathForWrite: e.beforePersistentApply } : void 0,
transform: (e) => ({ nextConfig: {
...e,
agents: {
...e.agents,
defaults: {
...e.agents?.defaults,
systemAgent: {
...e.agents?.defaults?.systemAgent,
agentId: a
}
}
}
} })
});
g = t.nextConfig;
_ = t.persistedHash ?? void 0;
}
} catch (e) {
if (!h.length) throw e;
return fail(formatErrorMessage(e));
}
return {
status: "created",
coordinatorId: a,
ambientOwnerId: m,
agents: h,
config: g,
..._ ? { configHash: _ } : {}
};
});
}
//#endregion
export { createAgentTeam as t };