openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
144 lines (143 loc) • 4.87 kB
JavaScript
import { Dc as validateAgentWaitParams, Ec as validateAgentParams } from "./src-BX8OAe1J.mjs";
import "./agent-run-registry-D7vkrqFT.mjs";
import { a as captureExternalSessionCommitGuard, o as composeSessionSourceAssertion } from "./session-source-authority-TlTpfRVE.mjs";
import { r as getAgentRunLifecycleGeneration } from "./agent-run-registry-state-BFfw9a4W.mjs";
import { t as ErrorCodes } from "./gateway-error-details-B69XiHdh.mjs";
import { p as errorShape } from "./error-codes-BzF7aRQ0.mjs";
import { t as SessionMutationAuthorizationChangedError } from "./session-mutation-authorization-error-0K0ZVV7q.mjs";
import { d as isGatewayAdmin, x as withSessionSharingTarget } from "./session-sharing-policy-3TAaxC-R.mjs";
import { c as prepareSessionSharingProfiles, o as prepareProjectedSessionSharing } from "./session-sharing-read-BqjuH16A.mjs";
import { t as assertValidParams } from "./validation-DwNk6A-B.mjs";
import { t as prepareAgentRequestPreflight } from "./agent-request-preflight-DGBbpBRg.mjs";
import { t as createAgentTurnService } from "./agent-turn-service-DxnYW_Qu.mjs";
import { n as createAgentRuntimeAuthorityGuard } from "./agent-runtime-authority-CtOnoFP2.mjs";
import { a as captureGatewayClientUploadCommitGuard } from "./upload-policy-Dp8z9OKP.mjs";
import { n as resolveAgentTurnRunObserver, t as captureAgentTurnPrincipal } from "./principal-sFUJS96k.mjs";
//#region src/gateway/agent-turn/io.ts
function createAgentTurnIo(e) {
const emit = (t, n) => {
if (n === void 0) {
e(...t);
return;
}
e(...t, n);
};
return {
emitAcceptance: emit,
emitFinal: emit
};
}
//#endregion
//#region src/gateway/server-methods/agent-run-handler.ts
const agentRunHandler = async ({ params: e, respond: t, context: n, client: r, isWebchatConnect: i, hasCurrentClientAuthority: a, sessionMutationCommitGuard: o }) => {
const s = captureGatewayClientUploadCommitGuard({
method: "agent",
requestParams: e,
client: r,
context: n
});
const c = composeSessionSourceAssertion([captureExternalSessionCommitGuard(o)], (e) => {
e();
if (a?.() === false) throw new Error("Gateway caller authority is no longer active.");
});
c();
const l = createAgentTurnIo(t);
if (!assertValidParams(e, validateAgentParams, "agent", (e, t, n, r) => l.emitAcceptance([
e,
t,
n
], r))) return;
const u = createAgentRuntimeAuthorityGuard(r, n, t, c);
if (!u.ensureActive()) return;
const d = e;
const f = captureAgentTurnPrincipal(r);
const p = await prepareAgentRequestPreflight({
request: d,
context: n,
client: f,
io: l
});
if (!p) return;
const m = resolveAgentTurnRunObserver({
principal: f,
registerToolEventRecipient: n.registerToolEventRecipient
});
try {
await createAgentTurnService({
context: n,
isWebchatConnect: i
}).startTurn({
assertAdmissionCurrent: u.commitGuard,
assertInputCommitAllowed: s,
hasCurrentClientAuthority: a,
preflight: p,
principal: f,
io: l,
onRunObserved: m
});
} catch (e) {
if (e instanceof SessionMutationAuthorizationChangedError) {
t(false, void 0, e.error);
return;
}
u.handleClosedError(e);
}
};
//#endregion
//#region src/gateway/server-methods/agent.ts
const agentWaitHandler = async ({ params: e, respond: t, context: n, client: r, isWebchatConnect: i }) => {
if (!assertValidParams(e, validateAgentWaitParams, "agent.wait", t)) return;
const a = r ?? null;
const o = createAgentTurnService({
context: n,
isWebchatConnect: i
}).prepareWaitForTurn(e);
let s;
const authorizeWait = async (e, r) => {
if (!a?.authenticatedUserProfile || isGatewayAdmin(a)) {
r?.();
return true;
}
const i = await (s ??= prepareSessionSharingProfiles(a));
const o = n.getRuntimeConfig();
const c = prepareProjectedSessionSharing({
client: a,
cfg: o,
profiles: i,
isMember: () => false
});
if (c.sessionCap !== "none" && c.sessionCap !== "invited") {
r?.();
return true;
}
const deny = () => {
t(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "agent run was not found"));
return false;
};
if (!e?.sessionKey) return deny();
return withSessionSharingTarget({
cfg: o,
sessionKey: e.sessionKey,
agentId: e.agentId
}, ({ target: t, members: o, assertCurrent: s }) => {
s();
const c = prepareProjectedSessionSharing({
cfg: n.getRuntimeConfig(),
client: a,
profiles: i,
isMember: (e, t) => o.some((e) => e.identityId === t)
});
if (!t || e.lifecycleGeneration !== getAgentRunLifecycleGeneration() || e.sessionId !== void 0 && t.entry.sessionId !== e.sessionId || !c.canReadTarget(t)) return deny();
r?.();
return true;
});
};
const c = await o.wait();
await authorizeWait(c.session, () => t(true, c.result));
};
const agentHandlers = {
agent: agentRunHandler,
"agent.wait": agentWaitHandler
};
//#endregion
export { agentHandlers };