UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 4.39 kB
import{c as resolveUserPath}from"./home-dir-DEN5DisQ.mjs";import"./utils-Dlk0IYAa.mjs";import{t as formatErrorMessage}from"./errors-D37BJTfI.mjs";import{n as normalizeAgentId}from"./agent-id-cOfqmfln.mjs";import"./session-key-BPgKfU2m.mjs";import{n as listAgentEntries}from"./agent-roster-4XrgUgPK.mjs";import"./agent-scope-config-CG9CFpGX.mjs";import{t as resolveDefaultAgentWorkspaceDir}from"./workspace-default-path-ULYbn-EL.mjs";import"./workspace-default-Cb7cQoAN.mjs";import{p as resolveConfigSnapshotHash}from"./io.read-helpers-B0XuPBY2.mjs";import{t as ConfigMutationConflictError}from"./mutation-conflict-qTMp0e4U.mjs";import{r as hasResolvedRosterBeforeMigrations}from"./agent-roster-provenance-5AqnirlB.mjs";import{s as readConfigFileSnapshot}from"./io.runtime-BvSeVFIY.mjs";import{a as readAgentDeletionJournalForCreation}from"./agent-deletion-journal.read-DL01GJPw.mjs";import{o as transformConfigFileWithRetry,s as withConfigMutationExclusive}from"./mutate-DcH8aT_4.mjs";import"./config-DkZ8y8Gv.mjs";import{i as validateAgentTeamMemberIds,n as loadAgentRole,r as loadAgentTeamPreset}from"./agent-roles-Bf9EiaQR.mjs";import{t as isUnconfiguredConfigSource}from"./fresh-install-config-DfZXNc2-.mjs";import{n as createAgent,r as validateAgentIdInput}from"./agent-create-VMDSixP5.mjs";import path from"node:path"; //!src/agents/agent-team.ts 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:_}:{}}})}export{createAgentTeam as t};