openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 5.16 kB
JavaScript
import{l as normalizeOptionalString}from"./string-coerce-CpTFcSwY.mjs";import{E as parseRawSessionConversationRef,O as parseThreadSessionSuffix}from"./session-key-BPgKfU2m.mjs";import{r as resolveIngressWorkspaceOverrideForSessionRun}from"./spawned-context-BR4pD9zG.mjs";import{r as resolveAgentMainSessionKey}from"./main-session-C6FGqx0x.mjs";import"./sessions-DQD7zQ95.mjs";import{o as resolveTrustedGroupId}from"./agent-tools.policy-KzzDFr2X.mjs";
//!src/gateway/server-methods/agent-subagent-registration.ts
function normalizeTrustedGroupMetadata(e){return{groupId:normalizeOptionalString(e?.groupId),groupChannel:normalizeOptionalString(e?.groupChannel),groupSpace:normalizeOptionalString(e?.groupSpace??e?.space)}}function resolveSessionKeyGroupId(e){const{baseSessionKey:t}=parseThreadSessionSuffix(e);const n=parseRawSessionConversationRef(t??e);if(!n||n.kind!==`group`&&n.kind!==`channel`)return;return n.rawId}function resolveTrustedGroupMetadata(e){return{groupId:e.stored.groupId??e.inherited?.groupId??resolveSessionKeyGroupId(e.sessionKey)??(e.spawnedBy?resolveSessionKeyGroupId(e.spawnedBy):void 0),groupChannel:e.stored.groupChannel??e.inherited?.groupChannel,groupSpace:e.stored.groupSpace??e.inherited?.groupSpace}}function resolveAgentSessionGroup(e){const t=resolveTrustedGroupMetadata(e);const n=t.groupId?resolveTrustedGroupId({groupId:t.groupId,sessionKey:e.sessionKey,spawnedBy:e.spawnedBy}):void 0;const r=Boolean(t.groupId)&&requestGroupMatchesTrusted({requestGroupId:e.requested.groupId,trustedGroupId:t.groupId});return n?.dropped?{groupId:void 0,groupChannel:void 0,groupSpace:void 0}:{groupId:t.groupId,groupChannel:t.groupChannel??(r?e.requested.groupChannel:void 0),groupSpace:t.groupSpace??(r?e.requested.groupSpace:void 0)}}function requestGroupMatchesTrusted(e){const t=e.requestGroupId?.trim();if(!t)return true;return Boolean(e.trustedGroupId&&t===e.trustedGroupId)}async function capturePluginSubagentSessionEffects(e){const{sessionId:t,storePath:n}=e.admitted;const r=e.admitted.entry?.sessionId===t?{lifecycleRevision:e.admitted.entry.lifecycleRevision}:void 0;const[{captureSessionEntryCurrentRead:i},{withSessionEntryReadOnlyInWorker:a}]=await Promise.all([import(`./session-entry-current-runtime-BxarzXDW.mjs`),import(`./session-entry-read-runtime-DneR6aFS.mjs`)]);e.assertCurrent();const o={agentId:e.childAgentId,storePath:n,sessionKey:e.childSessionKey,sessionId:t};const s=await a(o,e.assertCurrent,async(n,a)=>{if(!n.ok)throw n.error;const s=n.value;if(s?.sessionId!==t||r&&s.lifecycleRevision!==r.lifecycleRevision)throw new Error(`Plugin subagent session changed before registration.`);const{sessionId:c,lifecycleRevision:l,lifecycleRunId:u,activeWriterRunId:d}=s;const f=i(o,a);const assertCurrentEntry=e=>{f.assertSourceCurrent();if(e?.sessionId!==c||e.lifecycleRevision!==l||e.lifecycleRunId!==u||e.activeWriterRunId!==d)throw new Error(`Plugin subagent child session owner changed.`)};e.assertCurrent();return{async isCurrent(){try{assertCurrentEntry(await f.readCurrent());return true}catch{return false}},assertHostCurrent(){f.assertSourceCurrent();if(!f.source)assertCurrentEntry(f.readCurrent())},assertCurrentEntry,...f.source?{nativeCheck:{source:f.source,assertCurrent:assertCurrentEntry}}:{}}});e.assertCurrent();s.assertHostCurrent();return s}async function registerPluginSubagentRunFromGateway(e){const{childSessionKey:t}=e;const n=resolveAgentMainSessionKey({cfg:e.cfg,agentId:e.childAgentId});const r=e.requester?.sessionKey??n;const{adoptPausedSubagentRunForFollowUp:i,adoptPausedSubagentRunIntoSuccessor:a,registerSubagentRun:o}=await import(`./subagent-registry-BP2GLmsn.mjs`);const s=e.assertCurrent();let c;if(e.retainAdmission){if(!e.admittedSession)throw new Error(`Plugin subagent rejection cleanup requires its admitted session.`);c=await capturePluginSubagentSessionEffects({childAgentId:e.childAgentId,childSessionKey:t,admitted:e.admittedSession,assertCurrent:e.assertCurrent});e.assertCurrent()}if(!e.requester&&await i({childSessionKey:t,childAgentId:e.childAgentId,runId:e.runId,task:e.task,gatewayContextResolver:e.gatewayContextResolver,assertCurrent:e.assertCurrent,retainAdmission:e.retainAdmission,admissionSessionEffects:c}))return;await o({runId:e.runId,childSessionKey:t,childAgentId:e.childAgentId,sessionEntry:s,controllerSessionKey:n,requesterSessionKey:r,requesterOrigin:e.requester?.origin,requesterDisplayKey:e.requester?r:`main`,task:e.task,cleanup:`keep`,agentDir:e.agentDir,workspaceDir:resolveIngressWorkspaceOverrideForSessionRun({spawnedBy:s?.spawnedBy,workspaceDir:s?.spawnedWorkspaceDir,cwd:s?.spawnedCwd})??e.workspaceDir,...e.pluginId?{label:`plugin:${e.pluginId}`}:{},expectsCompletionMessage:e.requester!==void 0,spawnMode:`run`,gatewayContextResolver:e.gatewayContextResolver},{assertCurrent:e.assertCurrent,retainAdmission:e.retainAdmission,admissionSessionEffects:c});if(!e.requester)await a({childSessionKey:t,childAgentId:e.childAgentId,assertCurrent:e.assertCurrent,retainAdmission:e.retainAdmission,admissionSessionEffects:c})}export{registerPluginSubagentRunFromGateway as n,resolveAgentSessionGroup as r,normalizeTrustedGroupMetadata as t};