Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 31 additions & 29 deletions packages/effect-acp/src/protocol.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,37 +108,39 @@ it.layer(NodeServices.layer)("effect-acp protocol", (it) => {
},
});

yield* Queue.offer(
input,
yield* encodeJsonl(SessionUpdateNotification, {
jsonrpc: "2.0",
method: "session/update",
params: {
sessionId: "session-1",
update: {
sessionUpdate: "plan",
entries: [
{
content: "Inspect repository",
priority: "high",
status: "in_progress",
},
],
},
const updateFrame = yield* encodeJsonl(SessionUpdateNotification, {
jsonrpc: "2.0",
method: "session/update",
params: {
sessionId: "session-1",
update: {
sessionUpdate: "plan",
entries: [
{
content: "Inspect repository",
priority: "high",
status: "in_progress",
},
],
},
}),
);
},
});
const completionFrame = yield* encodeJsonl(ElicitationCompleteNotification, {
jsonrpc: "2.0",
method: "elicitation/complete",
params: {
elicitationId: "elicitation-1",
},
});

yield* Queue.offer(
input,
yield* encodeJsonl(ElicitationCompleteNotification, {
jsonrpc: "2.0",
method: "elicitation/complete",
params: {
elicitationId: "elicitation-1",
},
}),
);
// Frames split across reads, with one read ending one frame and starting the next.
const wire = new Uint8Array([...updateFrame, ...completionFrame]);
const splits = [10, updateFrame.length + 10, wire.length];
let start = 0;
for (const end of splits) {
yield* Queue.offer(input, wire.subarray(start, end));
start = end;
}

const [update, completion] = yield* Deferred.await(notifications);
assert.equal(update?._tag, "SessionUpdate");
Expand Down
12 changes: 9 additions & 3 deletions packages/effect-acp/src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,15 @@ const makeStrictNdJsonRpcParser = () => {
let buffer = "";
return {
decode: (bytes: Uint8Array | string): ReadonlyArray<unknown> => {
buffer += typeof bytes === "string" ? bytes : decoder.decode(bytes, { stream: true });
const lines = buffer.split("\n");
buffer = lines.pop() ?? "";
const chunk = typeof bytes === "string" ? bytes : decoder.decode(bytes, { stream: true });
const lastNewline = chunk.lastIndexOf("\n");
// Splitting the whole buffer per chunk is quadratic for multi-megabyte lines.
if (lastNewline === -1) {
buffer += chunk;
return [];
}
const lines = (buffer + chunk.slice(0, lastNewline)).split("\n");
buffer = chunk.slice(lastNewline + 1);
return lines.flatMap((line) => codec.decode(line));
},
encode: (response: Parameters<typeof codec.encode>[0]) => {
Expand Down
Loading