@yusukeshib/pi-babysit 0.6.32 → 0.6.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,446 @@
1
+ /**
2
+ * rpc-stream-format.mjs — pi-babysit's subagent log format for `pi --mode rpc`.
3
+ *
4
+ * The log is a subset of Pi's standard RPC event stream:
5
+ *
6
+ * - The first line is a header:
7
+ * {"type":"pi_babysit_stream","version":1,"profile":"compact"|"standard"}
8
+ * - Standard events keep their type and field meaning. The compact profile only
9
+ * OMITS fields whose value is exactly reproducible from other lines; it never
10
+ * reshapes a field.
11
+ * - pi-babysit's own data is carried by separate `pi_babysit_*` events, never by
12
+ * extra fields on standard events. Standard consumers can ignore them.
13
+ *
14
+ * Compact profile v1 omissions (see README "Subagent log format"):
15
+ * - message_start of a non-assistant message: `message.content` and
16
+ * `message.details`, only when the next line is a message_end carrying an
17
+ * identical message.
18
+ * - tool_execution_end with `isError !== true`: `result.content` and
19
+ * `result.details`. They equal the toolResult message_end for the same
20
+ * toolCallId; otherwise a `pi_babysit_tool_execution_result` event carries the
21
+ * original values.
22
+ * - turn_end `message` / `toolResults` and agent_end `messages`: only when equal
23
+ * to the value derived from message_end events by createRunTracker.
24
+ *
25
+ * Every agent_end is preceded by {"type":"pi_babysit_agent_end","parked":bool}.
26
+ *
27
+ * createRpcEventRestorer() turns a compact log back into standard events.
28
+ */
29
+ import { createHash } from "node:crypto";
30
+
31
+ export const STREAM_HEADER_TYPE = "pi_babysit_stream";
32
+ export const STREAM_VERSION = 1;
33
+ export const AGENT_END_MARKER_TYPE = "pi_babysit_agent_end";
34
+ export const TOOL_RESULT_EVENT_TYPE = "pi_babysit_tool_execution_result";
35
+ const BABYSIT_EVENT_PREFIX = "pi_babysit_";
36
+
37
+ export function streamHeader(profile) {
38
+ return { type: STREAM_HEADER_TYPE, version: STREAM_VERSION, profile };
39
+ }
40
+
41
+ /** True when the agent run ended with a background process whose exit will resume it. */
42
+ export function isParkedMessages(messages) {
43
+ if (!Array.isArray(messages)) return false;
44
+ const terminalCounts = new Map();
45
+ const addTerminal = (id) => terminalCounts.set(id, (terminalCounts.get(id) ?? 0) + 1);
46
+
47
+ for (let index = messages.length - 1; index >= 0; index--) {
48
+ const message = messages[index];
49
+ if (message?.role !== "toolResult") continue;
50
+ if (message.toolName === "babysit_wait") {
51
+ const status = message.details?.status;
52
+ if (
53
+ status &&
54
+ typeof status === "object" &&
55
+ typeof status.id === "string" &&
56
+ typeof status.state === "string" &&
57
+ status.state !== "running"
58
+ ) {
59
+ addTerminal(status.id);
60
+ }
61
+ for (const result of message.details?.results ?? []) {
62
+ if (result.kind === "exited" && typeof result.id === "string") addTerminal(result.id);
63
+ }
64
+ const first = message.details?.first;
65
+ if (first?.kind === "exited" && typeof first.id === "string") addTerminal(first.id);
66
+ continue;
67
+ }
68
+ if (
69
+ message.toolName === "babysit_kill" &&
70
+ typeof message.details?.id === "string" &&
71
+ ["exited", "killed", "dead"].includes(String(message.details.status))
72
+ ) {
73
+ addTerminal(message.details.id);
74
+ continue;
75
+ }
76
+ if (message.toolName === "process") return true;
77
+ if (message.toolName !== "babysit_run") continue;
78
+ const text = Array.isArray(message.content)
79
+ ? message.content
80
+ .filter((part) => part?.type === "text" && typeof part.text === "string")
81
+ .map((part) => part.text)
82
+ .join("")
83
+ : typeof message.content === "string"
84
+ ? message.content
85
+ : "";
86
+ const textMatch = /^Process started \(id: ([^)]+)\)\. \[notify-on-exit\]\nLog: /.exec(text);
87
+ const structuredStart =
88
+ message.details?.kind === "process" && message.details.status === "started";
89
+ if (!structuredStart && !textMatch) continue;
90
+ const id = typeof message.details?.id === "string" ? message.details.id : textMatch?.[1];
91
+ if (!id) return true;
92
+ const count = terminalCounts.get(id) ?? 0;
93
+ if (count === 0) return true;
94
+ if (count === 1) terminalCounts.delete(id);
95
+ else terminalCounts.set(id, count - 1);
96
+ }
97
+ return false;
98
+ }
99
+
100
+ const isObject = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
101
+
102
+ /**
103
+ * Derives turn_end and agent_end payloads from message_end events. The
104
+ * compactor and the restorer must feed it the same event sequence; `project`
105
+ * maps each message to the stored value (a hash in the compactor, the message
106
+ * itself in the restorer).
107
+ */
108
+ export function createRunTracker(project) {
109
+ let run = null;
110
+ let turn = [];
111
+ return {
112
+ observe(event) {
113
+ switch (event?.type) {
114
+ case "agent_start":
115
+ run = [];
116
+ turn = [];
117
+ break;
118
+ case "turn_start":
119
+ turn = [];
120
+ break;
121
+ case "message_end":
122
+ if (isObject(event.message)) {
123
+ const value = project(event.message);
124
+ run?.push(value);
125
+ turn.push({ role: event.message.role, value });
126
+ }
127
+ break;
128
+ case "agent_end":
129
+ run = null;
130
+ turn = [];
131
+ break;
132
+ }
133
+ },
134
+ /** `{ message, toolResults }` for the current turn, or null when not derivable. */
135
+ turnEnd() {
136
+ let index = turn.length - 1;
137
+ while (index >= 0 && turn[index].role !== "assistant") index--;
138
+ if (index < 0) return null;
139
+ return {
140
+ message: turn[index].value,
141
+ toolResults: turn
142
+ .slice(index + 1)
143
+ .filter((entry) => entry.role === "toolResult")
144
+ .map((entry) => entry.value),
145
+ };
146
+ },
147
+ /** Messages of the current agent run, or null when no agent_start was observed. */
148
+ agentEndMessages() {
149
+ return run ? [...run] : null;
150
+ },
151
+ };
152
+ }
153
+
154
+ const hashOf = (value) => createHash("sha1").update(JSON.stringify(value) ?? "").digest("base64");
155
+ const sameList = (a, b) => a.length === b.length && a.every((value, index) => value === b[index]);
156
+
157
+ /** Pick only the keys present on `source`, preserving JSON absent-vs-present. */
158
+ function pickPresent(source, keys) {
159
+ const out = {};
160
+ for (const key of keys) if (isObject(source) && key in source && source[key] !== undefined) out[key] = source[key];
161
+ return out;
162
+ }
163
+
164
+ function withoutKeys(source, keys) {
165
+ const out = { ...source };
166
+ for (const key of keys) delete out[key];
167
+ return out;
168
+ }
169
+
170
+ const PAYLOAD_KEYS = ["content", "details"];
171
+
172
+ /**
173
+ * Old Pi releases repeat the cumulative assistant message on every
174
+ * message_update. Pi >= 1.4 writes delta-only updates, which is the standard
175
+ * wire form; normalize older streams to it.
176
+ */
177
+ function normalizeMessageUpdate(event) {
178
+ const { message: _cumulativeMessage, assistantMessageEvent, ...rest } = event;
179
+ if (!isObject(assistantMessageEvent)) return rest;
180
+ const { partial: _cumulativePartial, ...deltaEvent } = assistantMessageEvent;
181
+ return { ...rest, assistantMessageEvent: deltaEvent };
182
+ }
183
+
184
+ function eventTypeOf(line) {
185
+ return /^\{"type":"([^"\\]+)"/.exec(line)?.[1];
186
+ }
187
+
188
+ /**
189
+ * Stateful line transformer for the proxy. `push(line)` returns the lines to
190
+ * write (possibly none while a message_start is held); `end()` flushes.
191
+ */
192
+ export function createRpcLogCompactor({ mode = "compact" } = {}) {
193
+ const compact = mode === "compact";
194
+ let headerSent = false;
195
+ let heldStart = null; // { line, json, event }
196
+ const pendingResults = new Map(); // toolCallId -> JSON of original { content, details }
197
+ const tracker = createRunTracker(hashOf);
198
+
199
+ const correctionLine = (toolCallId, originalJson) =>
200
+ `{"type":${JSON.stringify(TOOL_RESULT_EVENT_TYPE)},"toolCallId":${JSON.stringify(toolCallId)},"result":${originalJson}}`;
201
+ const flushPendingResults = (out) => {
202
+ for (const [toolCallId, originalJson] of pendingResults) out.push(correctionLine(toolCallId, originalJson));
203
+ pendingResults.clear();
204
+ };
205
+ const releaseHeld = (out) => {
206
+ if (!heldStart) return;
207
+ out.push(heldStart.line);
208
+ heldStart = null;
209
+ };
210
+
211
+ function push(line) {
212
+ const out = [];
213
+ if (!headerSent) {
214
+ out.push(JSON.stringify(streamHeader(compact ? "compact" : "standard")));
215
+ headerSent = true;
216
+ }
217
+ if (!line.trimStart().startsWith("{")) {
218
+ releaseHeld(out);
219
+ out.push(line);
220
+ return out;
221
+ }
222
+ const type = eventTypeOf(line);
223
+ if (type !== "message_end") releaseHeld(out);
224
+ // Delta-only message_update lines are the bulk of the stream; skip the
225
+ // parse when there is nothing to strip. Inside JSON strings a quote is
226
+ // escaped, so these key patterns cannot match text.
227
+ if (type === "message_update" && !line.includes('"message":') && !line.includes('"partial":')) {
228
+ out.push(line);
229
+ return out;
230
+ }
231
+ if (type === undefined || (compact ? false : type !== "message_update")) {
232
+ out.push(line);
233
+ return out;
234
+ }
235
+
236
+ let event;
237
+ try {
238
+ event = JSON.parse(line);
239
+ } catch {
240
+ releaseHeld(out);
241
+ out.push(line);
242
+ return out;
243
+ }
244
+ if (event?.type === "message_update") {
245
+ out.push(JSON.stringify(normalizeMessageUpdate(event)));
246
+ return out;
247
+ }
248
+
249
+ switch (event?.type) {
250
+ case "message_start": {
251
+ const message = event.message;
252
+ if (isObject(message) && message.role !== "assistant") {
253
+ heldStart = { line, json: JSON.stringify(message), event };
254
+ return out;
255
+ }
256
+ out.push(line);
257
+ return out;
258
+ }
259
+ case "message_end": {
260
+ const message = event.message;
261
+ tracker.observe(event);
262
+ if (message?.role === "toolResult" && pendingResults.has(message.toolCallId)) {
263
+ const originalJson = pendingResults.get(message.toolCallId);
264
+ pendingResults.delete(message.toolCallId);
265
+ if (originalJson !== JSON.stringify(pickPresent(message, PAYLOAD_KEYS))) {
266
+ out.push(correctionLine(message.toolCallId, originalJson));
267
+ }
268
+ }
269
+ if (heldStart) {
270
+ if (heldStart.json === JSON.stringify(message)) {
271
+ const startMessage = withoutKeys(heldStart.event.message, PAYLOAD_KEYS);
272
+ out.push(JSON.stringify({ ...heldStart.event, message: startMessage }));
273
+ } else {
274
+ out.push(heldStart.line);
275
+ }
276
+ heldStart = null;
277
+ }
278
+ out.push(line);
279
+ return out;
280
+ }
281
+ case "tool_execution_end": {
282
+ if (event.isError === true || !isObject(event.result) || typeof event.toolCallId !== "string") {
283
+ out.push(line);
284
+ return out;
285
+ }
286
+ pendingResults.set(event.toolCallId, JSON.stringify(pickPresent(event.result, PAYLOAD_KEYS)));
287
+ out.push(JSON.stringify({ ...event, result: withoutKeys(event.result, PAYLOAD_KEYS) }));
288
+ return out;
289
+ }
290
+ case "turn_end": {
291
+ const derived = tracker.turnEnd();
292
+ let compacted = event;
293
+ if (derived && "message" in event && hashOf(event.message) === derived.message) {
294
+ compacted = withoutKeys(compacted, ["message"]);
295
+ }
296
+ if (
297
+ derived &&
298
+ Array.isArray(event.toolResults) &&
299
+ sameList(event.toolResults.map(hashOf), derived.toolResults)
300
+ ) {
301
+ compacted = withoutKeys(compacted, ["toolResults"]);
302
+ }
303
+ out.push(compacted === event ? line : JSON.stringify(compacted));
304
+ return out;
305
+ }
306
+ case "agent_end": {
307
+ flushPendingResults(out);
308
+ out.push(JSON.stringify({ type: AGENT_END_MARKER_TYPE, parked: isParkedMessages(event.messages) }));
309
+ const derived = tracker.agentEndMessages();
310
+ tracker.observe(event);
311
+ if (derived && Array.isArray(event.messages) && sameList(event.messages.map(hashOf), derived)) {
312
+ out.push(JSON.stringify(withoutKeys(event, ["messages"])));
313
+ } else {
314
+ out.push(line);
315
+ }
316
+ return out;
317
+ }
318
+ default:
319
+ tracker.observe(event);
320
+ out.push(line);
321
+ return out;
322
+ }
323
+ }
324
+
325
+ function end() {
326
+ const out = [];
327
+ releaseHeld(out);
328
+ flushPendingResults(out);
329
+ return out;
330
+ }
331
+
332
+ return { push, end };
333
+ }
334
+
335
+ /**
336
+ * Turns a pi-babysit log back into standard Pi RPC events. Feed parsed JSON
337
+ * events from the start of the log; `push` returns the restored events to
338
+ * consume now and `end` flushes anything still held.
339
+ *
340
+ * Restoration is exact for content. Ordering is preserved except that a
341
+ * compacted tool_execution_end is released together with the toolResult
342
+ * message for the same toolCallId.
343
+ */
344
+ export function createRpcEventRestorer() {
345
+ let compact = false;
346
+ let heldStart = null;
347
+ const pendingEnds = new Map(); // toolCallId -> compacted tool_execution_end
348
+ const tracker = createRunTracker((message) => message);
349
+
350
+ const releasePendingEnd = (out, toolCallId, payload) => {
351
+ const event = pendingEnds.get(toolCallId);
352
+ if (!event) return;
353
+ pendingEnds.delete(toolCallId);
354
+ out.push({ ...event, result: { ...event.result, ...payload } });
355
+ };
356
+ const releaseHeld = (out) => {
357
+ if (!heldStart) return;
358
+ out.push(heldStart);
359
+ heldStart = null;
360
+ };
361
+ const flushPendingEnds = (out) => {
362
+ for (const event of pendingEnds.values()) out.push(event);
363
+ pendingEnds.clear();
364
+ };
365
+
366
+ function push(event) {
367
+ const out = [];
368
+ const type = event?.type;
369
+ if (type === STREAM_HEADER_TYPE) {
370
+ compact = event.profile === "compact" && event.version === STREAM_VERSION;
371
+ return out;
372
+ }
373
+ if (typeof type === "string" && type.startsWith(BABYSIT_EVENT_PREFIX)) {
374
+ if (type === TOOL_RESULT_EVENT_TYPE && typeof event.toolCallId === "string") {
375
+ releasePendingEnd(out, event.toolCallId, pickPresent(event.result, PAYLOAD_KEYS));
376
+ }
377
+ return out;
378
+ }
379
+ if (!compact) return [event];
380
+ if (type !== "message_end") releaseHeld(out);
381
+
382
+ switch (type) {
383
+ case "message_start":
384
+ if (isObject(event.message) && event.message.role !== "assistant" && !("content" in event.message)) {
385
+ heldStart = event;
386
+ return out;
387
+ }
388
+ out.push(event);
389
+ return out;
390
+ case "message_end": {
391
+ tracker.observe(event);
392
+ const message = event.message;
393
+ if (message?.role === "toolResult") {
394
+ releasePendingEnd(out, message.toolCallId, pickPresent(message, PAYLOAD_KEYS));
395
+ }
396
+ if (heldStart) {
397
+ out.push({ ...heldStart, message: { ...heldStart.message, ...pickPresent(message, PAYLOAD_KEYS) } });
398
+ heldStart = null;
399
+ }
400
+ out.push(event);
401
+ return out;
402
+ }
403
+ case "tool_execution_end":
404
+ if (
405
+ event.isError !== true &&
406
+ typeof event.toolCallId === "string" &&
407
+ isObject(event.result) &&
408
+ !("content" in event.result) &&
409
+ !("details" in event.result)
410
+ ) {
411
+ pendingEnds.set(event.toolCallId, event);
412
+ return out;
413
+ }
414
+ out.push(event);
415
+ return out;
416
+ case "turn_end": {
417
+ const derived = tracker.turnEnd();
418
+ const restored = { ...event };
419
+ if (!("message" in event) && derived) restored.message = derived.message;
420
+ if (!("toolResults" in event) && derived) restored.toolResults = derived.toolResults;
421
+ out.push(restored);
422
+ return out;
423
+ }
424
+ case "agent_end": {
425
+ flushPendingEnds(out);
426
+ const derived = tracker.agentEndMessages();
427
+ tracker.observe(event);
428
+ out.push(!("messages" in event) && derived ? { ...event, messages: derived } : event);
429
+ return out;
430
+ }
431
+ default:
432
+ tracker.observe(event);
433
+ out.push(event);
434
+ return out;
435
+ }
436
+ }
437
+
438
+ function end() {
439
+ const out = [];
440
+ releaseHeld(out);
441
+ flushPendingEnds(out);
442
+ return out;
443
+ }
444
+
445
+ return { push, end };
446
+ }
@@ -2,165 +2,50 @@
2
2
  /**
3
3
  * Transparent stdin/stdout proxy for `pi --mode rpc`.
4
4
  *
5
- * Pi's `message_update` events repeat the entire growing assistant message and
6
- * `assistantMessageEvent.partial` on every token. Those cumulative snapshots
7
- * are useful only while streaming; `message_end` is the authoritative final
8
- * message. Persisting every snapshot makes babysit logs grow quadratically.
9
- *
10
- * This proxy forwards stdin byte-for-byte. `message_update` snapshots are
11
- * always compacted. Compact log mode is the default: redundant payloads on
12
- * lifecycle/tool events are removed while authoritative `message_end`,
13
- * response, and error events remain intact. Set PI_BABYSIT_RPC_LOG_MODE=standard
14
- * to opt back into the legacy lifecycle payloads.
5
+ * stdin is forwarded byte-for-byte. stdout is rewritten line by line into
6
+ * pi-babysit's subagent log format (see rpc-stream-format.mjs): a subset of
7
+ * Pi's standard RPC event stream that only omits exactly reproducible
8
+ * duplicate payloads, plus separate `pi_babysit_*` events. Compact is the
9
+ * default profile; PI_BABYSIT_RPC_LOG_MODE=standard keeps Pi's events intact.
15
10
  */
16
11
  import { spawn } from "node:child_process";
17
12
  import { StringDecoder } from "node:string_decoder";
18
13
  import { pathToFileURL } from "node:url";
14
+ import { createRpcLogCompactor } from "./rpc-stream-format.mjs";
19
15
 
20
- function isParkedMessages(messages) {
21
- if (!Array.isArray(messages)) return false;
22
- const terminalCounts = new Map();
23
- const addTerminal = (id) => terminalCounts.set(id, (terminalCounts.get(id) ?? 0) + 1);
24
-
25
- for (let index = messages.length - 1; index >= 0; index--) {
26
- const message = messages[index];
27
- if (message?.role !== "toolResult") continue;
28
- if (message.toolName === "babysit_wait") {
29
- const status = message.details?.status;
30
- if (
31
- status &&
32
- typeof status === "object" &&
33
- typeof status.id === "string" &&
34
- typeof status.state === "string" &&
35
- status.state !== "running"
36
- ) {
37
- addTerminal(status.id);
38
- }
39
- for (const result of message.details?.results ?? []) {
40
- if (result.kind === "exited" && typeof result.id === "string") addTerminal(result.id);
41
- }
42
- const first = message.details?.first;
43
- if (first?.kind === "exited" && typeof first.id === "string") addTerminal(first.id);
44
- continue;
45
- }
46
- if (
47
- message.toolName === "babysit_kill" &&
48
- typeof message.details?.id === "string" &&
49
- ["exited", "killed", "dead"].includes(String(message.details.status))
50
- ) {
51
- addTerminal(message.details.id);
52
- continue;
53
- }
54
- if (message.toolName === "process") return true;
55
- if (message.toolName !== "babysit_run") continue;
56
- const text = Array.isArray(message.content)
57
- ? message.content
58
- .filter((part) => part?.type === "text" && typeof part.text === "string")
59
- .map((part) => part.text)
60
- .join("")
61
- : typeof message.content === "string"
62
- ? message.content
63
- : "";
64
- const textMatch = /^Process started \(id: ([^)]+)\)\. \[notify-on-exit\]\nLog: /.exec(text);
65
- const structuredStart =
66
- message.details?.kind === "process" && message.details.status === "started";
67
- if (!structuredStart && !textMatch) continue;
68
- const id = typeof message.details?.id === "string" ? message.details.id : textMatch?.[1];
69
- if (!id) return true;
70
- const count = terminalCounts.get(id) ?? 0;
71
- if (count === 0) return true;
72
- if (count === 1) terminalCounts.delete(id);
73
- else terminalCounts.set(id, count - 1);
74
- }
75
- return false;
76
- }
77
-
78
- export function compactRpcLine(
79
- line,
80
- mode = process.env.PI_BABYSIT_RPC_LOG_MODE ?? "compact",
81
- ) {
82
- if (!line.trimStart().startsWith("{")) return line;
83
- // Pi >= 1.4 already writes delta-only message_update lines (no cumulative
84
- // `message` or `partial`). They are the bulk of a worker's stream, so skip
85
- // the parse/re-serialize round trip when there is nothing to strip. Inside
86
- // JSON strings a quote is escaped, so these key patterns cannot match text.
87
- if (
88
- line.startsWith('{"type":"message_update"') &&
89
- !line.includes('"message":') &&
90
- !line.includes('"partial":')
91
- ) {
92
- return line;
93
- }
94
-
95
- let event;
96
- try {
97
- event = JSON.parse(line);
98
- } catch {
99
- return line;
100
- }
101
-
102
- if (event?.type === "message_update") {
103
- const { message: _cumulativeMessage, assistantMessageEvent, ...rest } = event;
104
- if (!assistantMessageEvent || typeof assistantMessageEvent !== "object") {
105
- return JSON.stringify(rest);
106
- }
107
- const { partial: _cumulativePartial, ...deltaEvent } = assistantMessageEvent;
108
- return JSON.stringify({ ...rest, assistantMessageEvent: deltaEvent });
109
- }
110
- if (mode !== "compact") return line;
111
-
112
- switch (event?.type) {
113
- case "message_start": {
114
- const { message, ...rest } = event;
115
- return JSON.stringify({
116
- ...rest,
117
- message:
118
- message && typeof message === "object"
119
- ? { role: message.role, toolName: message.toolName, toolCallId: message.toolCallId }
120
- : message,
121
- });
122
- }
123
- case "turn_end": {
124
- const { message: _message, toolResults: _toolResults, ...rest } = event;
125
- return JSON.stringify(rest);
126
- }
127
- case "agent_end": {
128
- const { messages, ...rest } = event;
129
- return JSON.stringify({ ...rest, piBabysitParked: isParkedMessages(messages) });
130
- }
131
- case "tool_execution_end": {
132
- if (event.isError) return line;
133
- const { result: _result, ...rest } = event;
134
- return JSON.stringify(rest);
135
- }
136
- default:
137
- return line;
138
- }
16
+ export function rpcLogMode(value = process.env.PI_BABYSIT_RPC_LOG_MODE) {
17
+ return value === "standard" ? "standard" : "compact";
139
18
  }
140
19
 
141
20
  function transformRpcOutput(input, output) {
142
21
  return new Promise((resolve, reject) => {
143
22
  const decoder = new StringDecoder("utf8");
23
+ const compactor = createRpcLogCompactor({ mode: rpcLogMode() });
144
24
  let buffered = "";
145
25
 
146
- const write = (value) => {
147
- if (!output.write(value)) input.pause();
26
+ const write = (lines, terminated = true) => {
27
+ if (lines.length === 0) return;
28
+ if (!output.write(lines.join("\n") + (terminated ? "\n" : ""))) input.pause();
148
29
  };
149
30
  output.on("drain", () => input.resume());
150
31
  input.on("error", reject);
151
32
  input.on("data", (chunk) => {
152
33
  buffered += decoder.write(chunk);
34
+ const lines = [];
153
35
  for (;;) {
154
36
  const newline = buffered.indexOf("\n");
155
37
  if (newline < 0) break;
156
- const line = buffered.slice(0, newline);
38
+ lines.push(...compactor.push(buffered.slice(0, newline)));
157
39
  buffered = buffered.slice(newline + 1);
158
- write(`${compactRpcLine(line)}\n`);
159
40
  }
41
+ write(lines);
160
42
  });
161
43
  input.on("end", () => {
162
44
  buffered += decoder.end();
163
- if (buffered) write(compactRpcLine(buffered));
45
+ const lines = buffered ? compactor.push(buffered) : [];
46
+ lines.push(...compactor.end());
47
+ // Keep an unterminated final record unterminated.
48
+ write(lines, !buffered);
164
49
  if (output.writableNeedDrain) output.once("drain", resolve);
165
50
  else resolve();
166
51
  });