diff --git a/packages/effect-acp/src/protocol.test.ts b/packages/effect-acp/src/protocol.test.ts index 7c8e567275..10c1a8bd83 100644 --- a/packages/effect-acp/src/protocol.test.ts +++ b/packages/effect-acp/src/protocol.test.ts @@ -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 = []; + 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(); diff --git a/packages/effect-acp/src/protocol.ts b/packages/effect-acp/src/protocol.ts index 995e9ac28f..72b45c4def 100644 --- a/packages/effect-acp/src/protocol.ts +++ b/packages/effect-acp/src/protocol.ts @@ -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(); - 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(); + 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,