Log outgoing ACP notifications before sending

- Emit decoded and raw protocol log events for notifications
- Cover logOutgoing behavior with a protocol test
This commit is contained in:
Julius Marminge committed 2026-04-17 16:44:48 -07:00
1 parent b1b7907853
commit 95baf47f17
2 files changed
+84 -36

No files matched your search

+38
View File
@@ -112,6 +112,44 @@ it.layer(NodeServices.layer)("effect-acp protocol", (it) => {
}),
);
it.effect("logs outgoing notifications when logOutgoing is enabled", () =>
Effect.gen(function* () {
const { stdio } = yield* makeInMemoryStdio();
const events: Array<AcpProtocol.AcpProtocolLogEvent> = [];
const transport = yield* AcpProtocol.makeAcpPatchedProtocol({
stdio,
serverRequestMethods: new Set(),
logOutgoing: true,
logger: (event) =>
Effect.sync(() => {
events.push(event);
}),
});
yield* transport.notifications.sendSessionCancel({ sessionId: "session-1" });
assert.deepEqual(events, [
{
direction: "outgoing",
stage: "decoded",
payload: {
jsonrpc: "2.0",
method: "session/cancel",
params: {
sessionId: "session-1",
},
},
},
{
direction: "outgoing",
stage: "raw",
payload:
'{"jsonrpc":"2.0","method":"session/cancel","params":{"sessionId":"session-1"}}\n',
},
]);
}),
);
it.effect("supports generic extension requests over the patched transport", () =>
Effect.gen(function* () {
const { stdio, input, output } = yield* makeInMemoryStdio();
+46 -36
View File
@@ -445,43 +445,53 @@ export const makeAcpPatchedProtocol = (
supportsSpanPropagation: true,
});
const sendNotification = (method: string, payload: unknown) =>
Queue.offer(
outgoing,
`${JSON.stringify({
jsonrpc: "2.0",
method,
...(payload !== undefined ? { params: payload } : {}),
})}\n`,
).pipe(Effect.asVoid);
const sendRequest = (method: string, payload: unknown) =>
Effect.gen(function* () {
const requestId = yield* Ref.modify(
nextRequestId,
(current) => [current, current + 1n] as const,
);
const deferred = yield* Deferred.make<unknown, AcpError.AcpError>();
yield* Ref.update(extPending, (pending) =>
new Map(pending).set(String(requestId), deferred),
);
yield* offerOutgoing({
_tag: "Request",
id: String(requestId),
tag: method,
payload,
headers: [],
}).pipe(
Effect.catch((error) =>
Ref.update(extPending, (pending) => {
const next = new Map(pending);
next.delete(String(requestId));
return next;
}).pipe(Effect.andThen(Effect.fail(error))),
),
);
return yield* Deferred.await(deferred);
const sendNotification = Effect.fn("sendNotification")(function* (
method: string,
payload: unknown,
) {
const message = {
jsonrpc: "2.0" as const,
method,
...(payload !== undefined ? { params: payload } : {}),
};
yield* logProtocol({
direction: "outgoing",
stage: "decoded",
payload: message,
});
const encoded = `${JSON.stringify(message)}\n`;
yield* logProtocol({
direction: "outgoing",
stage: "raw",
payload: encoded,
});
yield* Queue.offer(outgoing, encoded).pipe(Effect.asVoid);
});
const sendRequest = Effect.fn("sendRequest")(function* (method: string, payload: unknown) {
const requestId = yield* Ref.modify(
nextRequestId,
(current) => [current, current + 1n] as const,
);
const deferred = yield* Deferred.make<unknown, AcpError.AcpError>();
yield* Ref.update(extPending, (pending) => new Map(pending).set(String(requestId), deferred));
yield* offerOutgoing({
_tag: "Request",
id: String(requestId),
tag: method,
payload,
headers: [],
}).pipe(
Effect.catch((error) =>
Ref.update(extPending, (pending) => {
const next = new Map(pending);
next.delete(String(requestId));
return next;
}).pipe(Effect.andThen(Effect.fail(error))),
),
);
return yield* Deferred.await(deferred);
});
return {
clientProtocol,