@yusukeshib/pi-babysit 0.6.36 → 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 +16 -170
- 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/transcript-view.mjs
DELETED
|
@@ -1,324 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* transcript-view.mjs — render standard Pi RPC events with Pi's own TUI
|
|
3
|
-
* components, mirroring interactive mode's event handling.
|
|
4
|
-
*
|
|
5
|
-
* RPC `message_update` events are delta-only, so the streaming assistant
|
|
6
|
-
* message is rebuilt from content-block deltas (see Pi docs/json.md).
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
const RED = "\x1b[31m";
|
|
10
|
-
const DIM = "\x1b[2m";
|
|
11
|
-
const RESET = "\x1b[0m";
|
|
12
|
-
|
|
13
|
-
function userText(message) {
|
|
14
|
-
if (typeof message.content === "string") return message.content;
|
|
15
|
-
if (!Array.isArray(message.content)) return "";
|
|
16
|
-
return message.content
|
|
17
|
-
.filter((block) => block?.type === "text" && typeof block.text === "string")
|
|
18
|
-
.map((block) => block.text)
|
|
19
|
-
.join("");
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/** Apply one delta-only assistantMessageEvent to a mutable assistant message. */
|
|
23
|
-
export function applyAssistantDelta(message, update) {
|
|
24
|
-
if (!update || typeof update !== "object") return;
|
|
25
|
-
const content = Array.isArray(message.content) ? message.content : (message.content = []);
|
|
26
|
-
const index = update.contentIndex;
|
|
27
|
-
switch (update.type) {
|
|
28
|
-
case "text_start":
|
|
29
|
-
content[index] = { type: "text", text: "" };
|
|
30
|
-
break;
|
|
31
|
-
case "text_delta":
|
|
32
|
-
if (content[index]?.type !== "text") content[index] = { type: "text", text: "" };
|
|
33
|
-
content[index].text += update.delta ?? "";
|
|
34
|
-
break;
|
|
35
|
-
case "text_end":
|
|
36
|
-
content[index] = { ...content[index], type: "text", text: String(update.content ?? content[index]?.text ?? "") };
|
|
37
|
-
break;
|
|
38
|
-
case "thinking_start":
|
|
39
|
-
content[index] = { type: "thinking", thinking: "" };
|
|
40
|
-
break;
|
|
41
|
-
case "thinking_delta":
|
|
42
|
-
if (content[index]?.type !== "thinking") content[index] = { type: "thinking", thinking: "" };
|
|
43
|
-
content[index].thinking += update.delta ?? "";
|
|
44
|
-
break;
|
|
45
|
-
case "thinking_end":
|
|
46
|
-
content[index] = {
|
|
47
|
-
...content[index],
|
|
48
|
-
type: "thinking",
|
|
49
|
-
thinking: String(update.content ?? content[index]?.thinking ?? ""),
|
|
50
|
-
};
|
|
51
|
-
break;
|
|
52
|
-
case "toolcall_start":
|
|
53
|
-
content[index] = { type: "toolCall", id: update.id, name: update.toolName, arguments: {} };
|
|
54
|
-
break;
|
|
55
|
-
case "toolcall_end":
|
|
56
|
-
if (update.toolCall) content[index] = update.toolCall;
|
|
57
|
-
break;
|
|
58
|
-
default:
|
|
59
|
-
break;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Render context without a loaded Pi environment: built-in tool renderers only
|
|
65
|
-
* and default settings. pi-environment.mjs builds the full one.
|
|
66
|
-
*/
|
|
67
|
-
export function defaultRenderContext(runtime) {
|
|
68
|
-
return {
|
|
69
|
-
getToolDefinition: (name) => runtime.withBuiltInRenderers?.(name, undefined),
|
|
70
|
-
getMessageRenderer: () => undefined,
|
|
71
|
-
getMarkdownTransformers: () => [],
|
|
72
|
-
markdownTheme: () => runtime.agent.getMarkdownTheme(),
|
|
73
|
-
hideThinkingBlock: false,
|
|
74
|
-
outputPad: undefined,
|
|
75
|
-
showImages: false,
|
|
76
|
-
imageWidthCells: undefined,
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export function createTranscriptView({ runtime, tui, cwd = process.cwd(), render = defaultRenderContext(runtime) }) {
|
|
81
|
-
const {
|
|
82
|
-
AssistantMessageComponent,
|
|
83
|
-
ToolExecutionComponent,
|
|
84
|
-
UserMessageComponent,
|
|
85
|
-
CustomMessageComponent,
|
|
86
|
-
CompactionSummaryMessageComponent,
|
|
87
|
-
BranchSummaryMessageComponent,
|
|
88
|
-
SkillInvocationMessageComponent,
|
|
89
|
-
parseSkillBlock,
|
|
90
|
-
} = runtime.agent;
|
|
91
|
-
const { Container, Spacer, Text } = runtime.tui;
|
|
92
|
-
|
|
93
|
-
const container = new Container();
|
|
94
|
-
const pendingTools = new Map();
|
|
95
|
-
const expandables = [];
|
|
96
|
-
const assistants = [];
|
|
97
|
-
let expanded = false;
|
|
98
|
-
let hideThinking = render.hideThinkingBlock === true;
|
|
99
|
-
let streaming;
|
|
100
|
-
|
|
101
|
-
// Constructor arguments below follow interactive mode (InteractiveMode.handleEvent / addMessageToChat).
|
|
102
|
-
const markdownTheme = () => render.markdownTheme();
|
|
103
|
-
const transformers = () => render.getMarkdownTransformers();
|
|
104
|
-
const outputPad = render.outputPad;
|
|
105
|
-
const toolOptions = { showImages: render.showImages, imageWidthCells: render.imageWidthCells, outputPad };
|
|
106
|
-
const add = (component) => {
|
|
107
|
-
container.addChild(component);
|
|
108
|
-
return component;
|
|
109
|
-
};
|
|
110
|
-
const addExpandable = (component) => {
|
|
111
|
-
component.setExpanded?.(expanded);
|
|
112
|
-
expandables.push(component);
|
|
113
|
-
return add(component);
|
|
114
|
-
};
|
|
115
|
-
const note = (text, color = DIM) => {
|
|
116
|
-
add(new Spacer(1));
|
|
117
|
-
add(new Text(`${color}${text}${RESET}`, 1, 0));
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
function toolComponent(name, id, args) {
|
|
121
|
-
let component = pendingTools.get(id);
|
|
122
|
-
if (component) return component;
|
|
123
|
-
component = new ToolExecutionComponent(name, id, args ?? {}, toolOptions, render.getToolDefinition(name), tui, cwd);
|
|
124
|
-
addExpandable(component);
|
|
125
|
-
pendingTools.set(id, component);
|
|
126
|
-
return component;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
function syncToolCalls(message) {
|
|
130
|
-
for (const block of message.content ?? []) {
|
|
131
|
-
if (block?.type !== "toolCall" || !block.id) continue;
|
|
132
|
-
const existing = pendingTools.get(block.id);
|
|
133
|
-
if (existing) existing.updateArgs(block.arguments ?? {});
|
|
134
|
-
else toolComponent(block.name, block.id, block.arguments);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
function newAssistant(message) {
|
|
139
|
-
// undefined label: the component's default, as in interactive mode before an extension changes it.
|
|
140
|
-
const component = new AssistantMessageComponent(
|
|
141
|
-
undefined,
|
|
142
|
-
hideThinking,
|
|
143
|
-
markdownTheme(),
|
|
144
|
-
undefined,
|
|
145
|
-
outputPad,
|
|
146
|
-
transformers(),
|
|
147
|
-
);
|
|
148
|
-
assistants.push(component);
|
|
149
|
-
add(component);
|
|
150
|
-
component.updateContent(message, true);
|
|
151
|
-
return component;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
function finishAssistant(component, message) {
|
|
155
|
-
component.updateContent(message, false);
|
|
156
|
-
if (message.stopReason === "aborted" || message.stopReason === "error") {
|
|
157
|
-
const errorMessage =
|
|
158
|
-
message.errorMessage || (message.stopReason === "aborted" ? "Operation aborted" : "Error");
|
|
159
|
-
for (const tool of pendingTools.values()) {
|
|
160
|
-
tool.updateResult({ content: [{ type: "text", text: errorMessage }], isError: true });
|
|
161
|
-
}
|
|
162
|
-
pendingTools.clear();
|
|
163
|
-
} else {
|
|
164
|
-
for (const tool of pendingTools.values()) tool.setArgsComplete();
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
function addMessage(message) {
|
|
169
|
-
switch (message.role) {
|
|
170
|
-
case "user": {
|
|
171
|
-
const text = userText(message);
|
|
172
|
-
if (!text) return;
|
|
173
|
-
if (container.children.length > 0) add(new Spacer(1));
|
|
174
|
-
const skillBlock = parseSkillBlock?.(text);
|
|
175
|
-
if (skillBlock && SkillInvocationMessageComponent) {
|
|
176
|
-
addExpandable(
|
|
177
|
-
new SkillInvocationMessageComponent(skillBlock, markdownTheme(), outputPad, transformers()),
|
|
178
|
-
);
|
|
179
|
-
if (skillBlock.userMessage) {
|
|
180
|
-
add(new Spacer(1));
|
|
181
|
-
add(new UserMessageComponent(skillBlock.userMessage, markdownTheme(), outputPad, transformers()));
|
|
182
|
-
}
|
|
183
|
-
return;
|
|
184
|
-
}
|
|
185
|
-
add(new UserMessageComponent(text, markdownTheme(), outputPad, transformers()));
|
|
186
|
-
return;
|
|
187
|
-
}
|
|
188
|
-
case "custom":
|
|
189
|
-
if (message.display) {
|
|
190
|
-
const renderer = render.getMessageRenderer(message.customType);
|
|
191
|
-
addExpandable(new CustomMessageComponent(message, renderer, markdownTheme(), outputPad, transformers()));
|
|
192
|
-
}
|
|
193
|
-
return;
|
|
194
|
-
case "compactionSummary":
|
|
195
|
-
add(new Spacer(1));
|
|
196
|
-
addExpandable(
|
|
197
|
-
new CompactionSummaryMessageComponent(message, markdownTheme(), outputPad, transformers()),
|
|
198
|
-
);
|
|
199
|
-
return;
|
|
200
|
-
case "branchSummary":
|
|
201
|
-
add(new Spacer(1));
|
|
202
|
-
addExpandable(new BranchSummaryMessageComponent(message, markdownTheme(), outputPad, transformers()));
|
|
203
|
-
return;
|
|
204
|
-
default:
|
|
205
|
-
return;
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
function apply(event) {
|
|
210
|
-
switch (event?.type) {
|
|
211
|
-
case "agent_start":
|
|
212
|
-
pendingTools.clear();
|
|
213
|
-
return;
|
|
214
|
-
case "message_start": {
|
|
215
|
-
const message = event.message;
|
|
216
|
-
if (!message) return;
|
|
217
|
-
if (message.role === "assistant") {
|
|
218
|
-
const copy = { ...message, content: [...(Array.isArray(message.content) ? message.content : [])] };
|
|
219
|
-
streaming = { message: copy, component: newAssistant(copy) };
|
|
220
|
-
} else {
|
|
221
|
-
addMessage(message);
|
|
222
|
-
}
|
|
223
|
-
return;
|
|
224
|
-
}
|
|
225
|
-
case "message_update": {
|
|
226
|
-
if (!streaming) return;
|
|
227
|
-
if (event.message?.role === "assistant") {
|
|
228
|
-
// Cumulative snapshot from old Pi releases.
|
|
229
|
-
streaming.message = { ...event.message, content: [...(event.message.content ?? [])] };
|
|
230
|
-
} else {
|
|
231
|
-
applyAssistantDelta(streaming.message, event.assistantMessageEvent);
|
|
232
|
-
}
|
|
233
|
-
if (event.usage) streaming.message.usage = event.usage;
|
|
234
|
-
streaming.component.updateContent(streaming.message, true);
|
|
235
|
-
syncToolCalls(streaming.message);
|
|
236
|
-
return;
|
|
237
|
-
}
|
|
238
|
-
case "message_end": {
|
|
239
|
-
const message = event.message;
|
|
240
|
-
if (message?.role !== "assistant") return;
|
|
241
|
-
const component = streaming?.component ?? newAssistant(message);
|
|
242
|
-
syncToolCalls(message);
|
|
243
|
-
finishAssistant(component, message);
|
|
244
|
-
streaming = undefined;
|
|
245
|
-
return;
|
|
246
|
-
}
|
|
247
|
-
case "tool_execution_start": {
|
|
248
|
-
if (event.parentToolCallId) return;
|
|
249
|
-
const component = toolComponent(event.toolName, event.toolCallId, event.args);
|
|
250
|
-
component.markExecutionStarted?.();
|
|
251
|
-
return;
|
|
252
|
-
}
|
|
253
|
-
case "tool_execution_update":
|
|
254
|
-
pendingTools.get(event.toolCallId)?.updateResult({ ...event.partialResult, isError: false }, true);
|
|
255
|
-
return;
|
|
256
|
-
case "tool_execution_end": {
|
|
257
|
-
const component = pendingTools.get(event.toolCallId);
|
|
258
|
-
if (!component) return;
|
|
259
|
-
component.updateResult({ ...event.result, isError: event.isError, durationMs: event.durationMs });
|
|
260
|
-
pendingTools.delete(event.toolCallId);
|
|
261
|
-
return;
|
|
262
|
-
}
|
|
263
|
-
case "agent_end":
|
|
264
|
-
if (streaming) {
|
|
265
|
-
container.removeChild(streaming.component);
|
|
266
|
-
streaming = undefined;
|
|
267
|
-
}
|
|
268
|
-
pendingTools.clear();
|
|
269
|
-
return;
|
|
270
|
-
case "entry_appended": {
|
|
271
|
-
const entry = event.entry;
|
|
272
|
-
if (entry?.type === "custom_message" && entry.display) {
|
|
273
|
-
addMessage({
|
|
274
|
-
role: "custom",
|
|
275
|
-
customType: entry.customType,
|
|
276
|
-
content: entry.content,
|
|
277
|
-
display: entry.display,
|
|
278
|
-
details: entry.details,
|
|
279
|
-
timestamp: entry.timestamp,
|
|
280
|
-
});
|
|
281
|
-
}
|
|
282
|
-
return;
|
|
283
|
-
}
|
|
284
|
-
case "compaction_end":
|
|
285
|
-
if (event.result?.summary) {
|
|
286
|
-
addMessage({
|
|
287
|
-
role: "compactionSummary",
|
|
288
|
-
summary: event.result.summary,
|
|
289
|
-
tokensBefore: event.result.tokensBefore,
|
|
290
|
-
timestamp: Date.now(),
|
|
291
|
-
});
|
|
292
|
-
}
|
|
293
|
-
return;
|
|
294
|
-
case "extension_error":
|
|
295
|
-
note(`Extension error${event.extensionPath ? ` (${event.extensionPath})` : ""}: ${event.error ?? ""}`, RED);
|
|
296
|
-
return;
|
|
297
|
-
case "error":
|
|
298
|
-
note(`Error: ${event.message ?? event.error ?? JSON.stringify(event)}`, RED);
|
|
299
|
-
return;
|
|
300
|
-
case "response":
|
|
301
|
-
if (event.success === false) note(`RPC ${event.command ?? "command"} failed: ${event.error ?? ""}`, RED);
|
|
302
|
-
return;
|
|
303
|
-
default:
|
|
304
|
-
return;
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
return {
|
|
309
|
-
container,
|
|
310
|
-
apply,
|
|
311
|
-
get expanded() {
|
|
312
|
-
return expanded;
|
|
313
|
-
},
|
|
314
|
-
setToolsExpanded(value) {
|
|
315
|
-
expanded = value;
|
|
316
|
-
for (const component of expandables) component.setExpanded?.(value);
|
|
317
|
-
},
|
|
318
|
-
toggleThinking() {
|
|
319
|
-
hideThinking = !hideThinking;
|
|
320
|
-
for (const component of assistants) component.setHideThinkingBlock(hideThinking);
|
|
321
|
-
return hideThinking;
|
|
322
|
-
},
|
|
323
|
-
};
|
|
324
|
-
}
|