UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 4.29 kB
import{n as normalizeAgentId}from"./agent-id-cOfqmfln.mjs";import"./session-key-BPgKfU2m.mjs";import{n as listAgentEntries}from"./agent-roster-4XrgUgPK.mjs";import{E as toAgentEntriesRecord,m as resolveAmbientOwnerAgentId}from"./agent-scope-config-CG9CFpGX.mjs";import{n as createMergePatch,t as applyMergePatch}from"./merge-patch-CKGPHlQC.mjs";import{p as resolveConfigSnapshotHash}from"./io.read-helpers-B0XuPBY2.mjs";import{r as hasResolvedRosterBeforeMigrations}from"./agent-roster-provenance-5AqnirlB.mjs";import{s as readConfigFileSnapshot}from"./io.runtime-BvSeVFIY.mjs";import"./config-DkZ8y8Gv.mjs";import{t as migrateLegacyMainSessionKeys}from"./legacy-main-session-migration-CkkPPEtJ.mjs";import{n as createAgent,r as validateAgentIdInput}from"./agent-create-VMDSixP5.mjs"; //!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-Poirer_t.mjs`);const d={bootstrapFirstAgent:true,...t?{expectedConfigHash:e.expectedConfigHash}:{},beforePersistentApply:e.beforePersistentApply};const f=e.firstAgent?.team?await(await import(`./agent-team-DQz_2Ngo.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.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}:{}}}export{validateFirstOnboardingAgentName as n,ensureOnboardingAgent as t};