gateway: add agent.subscribe/unsubscribe server method handlers

This commit is contained in:
kumarabhirup 2026-02-21 11:02:58 -08:00
parent 0f5ddf7e8f
commit 581e73d1ce
No known key found for this signature in database
GPG Key ID: DB7CA2289CAB0167
2 changed files with 47 additions and 0 deletions

View File

@ -85,6 +85,8 @@ const BASE_METHODS = [
"agent",
"agent.identity.get",
"agent.wait",
"agent.subscribe",
"agent.unsubscribe",
"browser.request",
// WebChat WebSocket-native chat methods
"chat.history",

View File

@ -38,6 +38,8 @@ import {
validateAgentIdentityParams,
validateAgentParams,
validateAgentWaitParams,
validateAgentSubscribeParams,
validateAgentUnsubscribeParams,
} from "../protocol/index.js";
import {
canonicalizeSpawnedByForAgent,
@ -690,4 +692,47 @@ export const agentHandlers: GatewayRequestHandlers = {
error: snapshot.error,
});
},
"agent.subscribe": ({ params, client, respond, context }) => {
const validated = validateAgentSubscribeParams(params);
if (!validated.ok) {
respond(
false,
undefined,
errorShape(ErrorCodes.INVALID_PARAMS, formatValidationErrors(validated.errors)),
);
return;
}
const p = validated.value;
const connId = client?.connId;
if (!connId) {
respond(false, undefined, errorShape(ErrorCodes.INVALID_PARAMS, "no connection id"));
return;
}
context.registerSessionSubscription(p.sessionKey, connId);
// Replay buffered events past the caller's cursor.
const afterSeq = typeof p.afterSeq === "number" ? p.afterSeq : 0;
const replayed = context.replaySessionEvents(p.sessionKey, afterSeq, connId);
respond(true, { cursor: context.currentGlobalSeq(), replayed });
},
"agent.unsubscribe": ({ params, client, respond, context }) => {
const validated = validateAgentUnsubscribeParams(params);
if (!validated.ok) {
respond(
false,
undefined,
errorShape(ErrorCodes.INVALID_PARAMS, formatValidationErrors(validated.errors)),
);
return;
}
const connId = client?.connId;
if (!connId) {
respond(false, undefined, errorShape(ErrorCodes.INVALID_PARAMS, "no connection id"));
return;
}
context.unregisterSessionSubscription(validated.value.sessionKey, connId);
respond(true, {});
},
};