@yusukeshib/pi-babysit 0.6.37 → 0.7.0
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.
- package/README.md +15 -37
- package/format-stream.mjs +4 -4
- package/index.ts +9 -24
- package/package.json +4 -6
- package/view.mjs +17 -186
- package/pi-environment.mjs +0 -176
- package/pi-runtime.mjs +0 -120
- package/rpc-stream-format.mjs +0 -446
- package/rpc-stream-proxy.mjs +0 -123
- package/transcript-view.mjs +0 -324
package/pi-runtime.mjs
DELETED
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Resolve the Pi packages of the INSTALLED pi, so a standalone viewer renders
|
|
3
|
-
* with the same components as the pi that produced the log (not a copy that
|
|
4
|
-
* happens to sit in pi-babysit's own node_modules).
|
|
5
|
-
*/
|
|
6
|
-
import fs from "node:fs";
|
|
7
|
-
import os from "node:os";
|
|
8
|
-
import path from "node:path";
|
|
9
|
-
import { execFileSync } from "node:child_process";
|
|
10
|
-
import { pathToFileURL } from "node:url";
|
|
11
|
-
|
|
12
|
-
const AGENT_PACKAGE = "@earendil-works/pi-coding-agent";
|
|
13
|
-
|
|
14
|
-
function packageName(dir) {
|
|
15
|
-
try {
|
|
16
|
-
return JSON.parse(fs.readFileSync(path.join(dir, "package.json"), "utf8")).name;
|
|
17
|
-
} catch {
|
|
18
|
-
return undefined;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function managedInstallDir() {
|
|
23
|
-
const agentDir = process.env.PI_CODING_AGENT_DIR ?? path.join(os.homedir(), ".pi", "agent");
|
|
24
|
-
try {
|
|
25
|
-
const version = fs.readFileSync(path.join(agentDir, "install", "current-version"), "utf8").trim();
|
|
26
|
-
if (!version) return undefined;
|
|
27
|
-
return path.join(agentDir, "install", "releases", version, "node_modules", AGENT_PACKAGE);
|
|
28
|
-
} catch {
|
|
29
|
-
return undefined;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function packageDirFromBinary() {
|
|
34
|
-
try {
|
|
35
|
-
const bin = execFileSync("sh", ["-c", "command -v pi"], { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }).trim();
|
|
36
|
-
if (!bin) return undefined;
|
|
37
|
-
let dir = path.dirname(fs.realpathSync(bin));
|
|
38
|
-
for (;;) {
|
|
39
|
-
if (packageName(dir) === AGENT_PACKAGE) return dir;
|
|
40
|
-
const parent = path.dirname(dir);
|
|
41
|
-
if (parent === dir) return undefined;
|
|
42
|
-
dir = parent;
|
|
43
|
-
}
|
|
44
|
-
} catch {
|
|
45
|
-
return undefined;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** Directory of the installed pi-coding-agent package, or undefined. */
|
|
50
|
-
export function findPiPackageDir() {
|
|
51
|
-
const candidates = [process.env.PI_BABYSIT_PI_PACKAGE_DIR, managedInstallDir(), packageDirFromBinary()];
|
|
52
|
-
return candidates.find((dir) => dir && packageName(dir) === AGENT_PACKAGE);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function siblingPackageEntry(agentDir, name) {
|
|
56
|
-
for (const dir of [
|
|
57
|
-
path.join(path.dirname(agentDir), name),
|
|
58
|
-
path.join(agentDir, "node_modules", "@earendil-works", name),
|
|
59
|
-
]) {
|
|
60
|
-
if (packageName(dir) === `@earendil-works/${name}`) {
|
|
61
|
-
const pkg = JSON.parse(fs.readFileSync(path.join(dir, "package.json"), "utf8"));
|
|
62
|
-
const entry = pkg.exports?.["."]?.import ?? pkg.exports?.["."]?.default ?? pkg.module ?? pkg.main ?? "index.js";
|
|
63
|
-
return pathToFileURL(path.join(dir, entry)).href;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
return undefined;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Internal (non-exported) modules of pi-coding-agent that interactive mode
|
|
71
|
-
* uses to set up its chat. Each is optional: a pi release that moved or lacks
|
|
72
|
-
* one degrades to the closest public behavior instead of failing.
|
|
73
|
-
*/
|
|
74
|
-
const INTERNAL_MODULES = {
|
|
75
|
-
renderers: "dist/core/tools/renderers/index.js",
|
|
76
|
-
builtInExtensions: "dist/extensions/index.js",
|
|
77
|
-
trust: "dist/core/trust-manager.js",
|
|
78
|
-
keybindings: "dist/core/keybindings.js",
|
|
79
|
-
theme: "dist/modes/interactive/theme/theme.js",
|
|
80
|
-
themeController: "dist/modes/interactive/theme/theme-controller.js",
|
|
81
|
-
mermaid: "dist/modes/interactive/components/mermaid.js",
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
async function importInternal(packageDir, relativePath) {
|
|
85
|
-
const file = path.join(packageDir, relativePath);
|
|
86
|
-
if (!fs.existsSync(file)) return undefined;
|
|
87
|
-
try {
|
|
88
|
-
return await import(pathToFileURL(file).href);
|
|
89
|
-
} catch {
|
|
90
|
-
return undefined;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Load { agent, tui, withBuiltInRenderers, internal, packageDir, version }.
|
|
96
|
-
* `internal` holds the optional internal modules (see INTERNAL_MODULES); they
|
|
97
|
-
* resolve to the same module instances the public entry point uses, so module
|
|
98
|
-
* state such as registered themes is shared.
|
|
99
|
-
*/
|
|
100
|
-
export async function loadPiRuntime() {
|
|
101
|
-
const packageDir = findPiPackageDir();
|
|
102
|
-
let agent;
|
|
103
|
-
let tui;
|
|
104
|
-
let version;
|
|
105
|
-
const internal = {};
|
|
106
|
-
if (packageDir) {
|
|
107
|
-
const pkg = JSON.parse(fs.readFileSync(path.join(packageDir, "package.json"), "utf8"));
|
|
108
|
-
version = pkg.version;
|
|
109
|
-
agent = await import(pathToFileURL(path.join(packageDir, pkg.exports?.["."]?.import ?? "dist/index.js")).href);
|
|
110
|
-
const tuiEntry = siblingPackageEntry(packageDir, "pi-tui");
|
|
111
|
-
tui = await import(tuiEntry ?? "@earendil-works/pi-tui");
|
|
112
|
-
for (const [key, relativePath] of Object.entries(INTERNAL_MODULES)) {
|
|
113
|
-
internal[key] = await importInternal(packageDir, relativePath);
|
|
114
|
-
}
|
|
115
|
-
} else {
|
|
116
|
-
agent = await import(AGENT_PACKAGE);
|
|
117
|
-
tui = await import("@earendil-works/pi-tui");
|
|
118
|
-
}
|
|
119
|
-
return { agent, tui, withBuiltInRenderers: internal.renderers?.withBuiltInRenderers, internal, packageDir, version };
|
|
120
|
-
}
|
package/rpc-stream-format.mjs
DELETED
|
@@ -1,446 +0,0 @@
|
|
|
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
|
-
}
|
package/rpc-stream-proxy.mjs
DELETED
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* Transparent stdin/stdout proxy for `pi --mode rpc`.
|
|
4
|
-
*
|
|
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.
|
|
10
|
-
*/
|
|
11
|
-
import { spawn } from "node:child_process";
|
|
12
|
-
import { StringDecoder } from "node:string_decoder";
|
|
13
|
-
import { pathToFileURL } from "node:url";
|
|
14
|
-
import { createRpcLogCompactor } from "./rpc-stream-format.mjs";
|
|
15
|
-
|
|
16
|
-
export function rpcLogMode(value = process.env.PI_BABYSIT_RPC_LOG_MODE) {
|
|
17
|
-
return value === "standard" ? "standard" : "compact";
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function transformRpcOutput(input, output) {
|
|
21
|
-
return new Promise((resolve, reject) => {
|
|
22
|
-
const decoder = new StringDecoder("utf8");
|
|
23
|
-
const compactor = createRpcLogCompactor({ mode: rpcLogMode() });
|
|
24
|
-
let buffered = "";
|
|
25
|
-
|
|
26
|
-
const write = (lines, terminated = true) => {
|
|
27
|
-
if (lines.length === 0) return;
|
|
28
|
-
if (!output.write(lines.join("\n") + (terminated ? "\n" : ""))) input.pause();
|
|
29
|
-
};
|
|
30
|
-
output.on("drain", () => input.resume());
|
|
31
|
-
input.on("error", reject);
|
|
32
|
-
input.on("data", (chunk) => {
|
|
33
|
-
buffered += decoder.write(chunk);
|
|
34
|
-
const lines = [];
|
|
35
|
-
for (;;) {
|
|
36
|
-
const newline = buffered.indexOf("\n");
|
|
37
|
-
if (newline < 0) break;
|
|
38
|
-
lines.push(...compactor.push(buffered.slice(0, newline)));
|
|
39
|
-
buffered = buffered.slice(newline + 1);
|
|
40
|
-
}
|
|
41
|
-
write(lines);
|
|
42
|
-
});
|
|
43
|
-
input.on("end", () => {
|
|
44
|
-
buffered += decoder.end();
|
|
45
|
-
const lines = buffered ? compactor.push(buffered) : [];
|
|
46
|
-
lines.push(...compactor.end());
|
|
47
|
-
// Keep an unterminated final record unterminated.
|
|
48
|
-
write(lines, !buffered);
|
|
49
|
-
if (output.writableNeedDrain) output.once("drain", resolve);
|
|
50
|
-
else resolve();
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function waitForDrain(stream) {
|
|
56
|
-
return stream.writableNeedDrain
|
|
57
|
-
? new Promise((resolve) => stream.once("drain", resolve))
|
|
58
|
-
: Promise.resolve();
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export function runRpcProxy(argv = process.argv.slice(2)) {
|
|
62
|
-
const separator = argv.indexOf("--");
|
|
63
|
-
const command = separator >= 0 ? argv[separator + 1] : argv[0];
|
|
64
|
-
const args = separator >= 0 ? argv.slice(separator + 2) : argv.slice(1);
|
|
65
|
-
if (!command) {
|
|
66
|
-
console.error("Usage: rpc-stream-proxy.mjs -- <command> [args...]");
|
|
67
|
-
process.exitCode = 2;
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const child = spawn(command, args, {
|
|
72
|
-
cwd: process.cwd(),
|
|
73
|
-
env: process.env,
|
|
74
|
-
stdio: ["pipe", "pipe", "pipe"],
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
let transportFailed = false;
|
|
78
|
-
process.stdin.pipe(child.stdin);
|
|
79
|
-
// The RPC worker can exit or self-reap while a command is still arriving.
|
|
80
|
-
// Ignore the resulting pipe-close race so the proxy preserves the worker's
|
|
81
|
-
// actual exit code/signal instead of crashing with an unhandled EPIPE.
|
|
82
|
-
child.stdin.on("error", (error) => {
|
|
83
|
-
if (error?.code === "EPIPE" || error?.code === "ERR_STREAM_DESTROYED") return;
|
|
84
|
-
transportFailed = true;
|
|
85
|
-
console.error(`rpc stream proxy stdin failed: ${error.message}`);
|
|
86
|
-
});
|
|
87
|
-
const outputFlushed = transformRpcOutput(child.stdout, process.stdout);
|
|
88
|
-
child.stderr.pipe(process.stderr);
|
|
89
|
-
|
|
90
|
-
const forwardedSignals = ["SIGINT", "SIGTERM", "SIGHUP"];
|
|
91
|
-
const signalHandlers = new Map(
|
|
92
|
-
forwardedSignals.map((signal) => [signal, () => child.kill(signal)]),
|
|
93
|
-
);
|
|
94
|
-
for (const [signal, handler] of signalHandlers) process.on(signal, handler);
|
|
95
|
-
let spawnFailed = false;
|
|
96
|
-
child.on("error", (error) => {
|
|
97
|
-
spawnFailed = true;
|
|
98
|
-
console.error(`rpc stream proxy failed to start ${command}: ${error.message}`);
|
|
99
|
-
});
|
|
100
|
-
child.on("close", async (code, signal) => {
|
|
101
|
-
try {
|
|
102
|
-
await outputFlushed;
|
|
103
|
-
await Promise.all([waitForDrain(process.stdout), waitForDrain(process.stderr)]);
|
|
104
|
-
} catch (error) {
|
|
105
|
-
console.error(`rpc stream proxy output failed: ${error.message}`);
|
|
106
|
-
process.exitCode = 1;
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
109
|
-
if (transportFailed) {
|
|
110
|
-
process.exitCode = 1;
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
if (signal) {
|
|
114
|
-
for (const [forwarded, handler] of signalHandlers) process.off(forwarded, handler);
|
|
115
|
-
process.kill(process.pid, signal);
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
process.exitCode = spawnFailed ? 1 : (code ?? 1);
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const entry = process.argv[1] ? pathToFileURL(process.argv[1]).href : undefined;
|
|
123
|
-
if (entry === import.meta.url) runRpcProxy();
|