openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
159 lines (158 loc) • 5.66 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { D as parseThreadSessionSuffix, T as parseRawSessionConversationRef } from "./session-key-CqAyZeVk.mjs";
import { r as resolveAgentMainSessionKey } from "./main-session-BNL3-mRV.mjs";
import "./sessions-D5Vvu3AE.mjs";
import { o as resolveTrustedGroupId } from "./agent-tools.policy-CM-3Smt9.mjs";
//#region 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-BK5fxBar.mjs"), import("./session-entry-read-runtime-24no1ay9.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-DxTolEfW.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",
...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
});
}
//#endregion
export { registerPluginSubagentRunFromGateway as n, resolveAgentSessionGroup as r, normalizeTrustedGroupMetadata as t };