@xzzpig/pi-subagents 0.5.0 → 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/CHANGELOG.md +36 -1
- package/README.md +4 -0
- package/docs/agents.md +39 -0
- package/docs/configuration.md +1 -1
- package/node_modules/@xzzpig/pi-components/CHANGELOG.md +102 -0
- package/node_modules/@xzzpig/pi-components/LICENSE +21 -0
- package/node_modules/@xzzpig/pi-components/README.md +59 -0
- package/node_modules/@xzzpig/pi-components/dist/index.d.ts +1 -0
- package/node_modules/@xzzpig/pi-components/dist/index.js +1 -0
- package/node_modules/@xzzpig/pi-components/dist/src/transcript.d.ts +345 -0
- package/node_modules/@xzzpig/pi-components/dist/src/transcript.js +1169 -0
- package/node_modules/@xzzpig/pi-components/package.json +49 -0
- package/node_modules/@xzzpig/pi-components/src/transcript.ts +1785 -0
- package/package.json +5 -1
- package/src/agents/agent-serializer.ts +2 -0
- package/src/agents/agents.ts +43 -11
- package/src/agents/runtime-agent-registry.ts +5 -1
- package/src/extension/context-injection.ts +118 -0
- package/src/extension/doctor.ts +11 -0
- package/src/extension/index.ts +41 -0
- package/src/shared/types.ts +5 -0
- package/src/tui/fleet-native-transcript.ts +374 -0
- package/src/tui/fleet-transcript.ts +47 -1
- package/src/tui/fleet.ts +96 -24
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
TranscriptEntry,
|
|
3
|
+
TranscriptState,
|
|
4
|
+
} from "@xzzpig/pi-components/transcript";
|
|
5
|
+
import { readTranscriptRecords, type FleetTranscriptReadOptions } from "./fleet-transcript.ts";
|
|
6
|
+
|
|
7
|
+
/** Runtime shape of `@xzzpig/pi-components/transcript` as consumed here.
|
|
8
|
+
* Expressed as a static typeof-import so the dependency stays lazy at
|
|
9
|
+
* runtime: the module is only loaded through the guarded dynamic import. */
|
|
10
|
+
type NativeTranscriptModule = typeof import("@xzzpig/pi-components/transcript");
|
|
11
|
+
|
|
12
|
+
export type { NativeTranscriptModule };
|
|
13
|
+
|
|
14
|
+
const REQUIRED_NATIVE_EXPORTS = [
|
|
15
|
+
"createTranscriptState",
|
|
16
|
+
"appendEntry",
|
|
17
|
+
"ensureTurn",
|
|
18
|
+
"finishTurn",
|
|
19
|
+
"findLatestEntry",
|
|
20
|
+
"ensureToolCall",
|
|
21
|
+
"upsertText",
|
|
22
|
+
"upsertToolResult",
|
|
23
|
+
"appendNotice",
|
|
24
|
+
"renderTranscriptLines",
|
|
25
|
+
"TranscriptToolComponents",
|
|
26
|
+
"ensureTranscriptTheme",
|
|
27
|
+
] as const;
|
|
28
|
+
|
|
29
|
+
let nativeSupportPromise: Promise<NativeTranscriptModule | null> | undefined;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Structural probe for a candidate transcript module: every export the native
|
|
33
|
+
* renderer calls must be present. Exported pure so hosts and tests can check
|
|
34
|
+
* arbitrary objects (e.g. simulating an older shared-library build).
|
|
35
|
+
*/
|
|
36
|
+
export function hasRequiredNativeExports(candidate: unknown): candidate is NativeTranscriptModule {
|
|
37
|
+
if (!candidate || typeof candidate !== "object") return false;
|
|
38
|
+
return REQUIRED_NATIVE_EXPORTS.every((name) => name in (candidate as Record<string, unknown>));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Loads the shared transcript module once and validates it exposes every
|
|
43
|
+
* export the native renderer needs. Resolves `null` when the package is
|
|
44
|
+
* absent (or an older host ships an incompatible build) so callers can fall
|
|
45
|
+
* back to the legacy rail renderer without try/catch at call sites.
|
|
46
|
+
*/
|
|
47
|
+
export function loadNativeTranscriptSupport(): Promise<NativeTranscriptModule | null> {
|
|
48
|
+
nativeSupportPromise ??= (async () => {
|
|
49
|
+
try {
|
|
50
|
+
const mod: NativeTranscriptModule = await import("@xzzpig/pi-components/transcript");
|
|
51
|
+
return hasRequiredNativeExports(mod) ? mod : null;
|
|
52
|
+
} catch {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
})();
|
|
56
|
+
return nativeSupportPromise;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface NativeFleetBuildInput {
|
|
60
|
+
filePath: string;
|
|
61
|
+
trustedRoots: string[];
|
|
62
|
+
width: number;
|
|
63
|
+
expandedTools: boolean;
|
|
64
|
+
cwd?: string;
|
|
65
|
+
maxRecords?: number;
|
|
66
|
+
theme: {
|
|
67
|
+
fg(color: string, text: string): string;
|
|
68
|
+
bg(color: string, text: string): string;
|
|
69
|
+
bold(text: string): string;
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface NativeFleetBuildResult {
|
|
74
|
+
lines: string[];
|
|
75
|
+
conversationState: string;
|
|
76
|
+
truncated: boolean;
|
|
77
|
+
warning?: string;
|
|
78
|
+
entryCount: number;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function contentParts(content: unknown): Array<{ type: string; text?: string; thinking?: string }> {
|
|
82
|
+
if (!Array.isArray(content)) return [];
|
|
83
|
+
return content.filter((part): part is { type: string; text?: string; thinking?: string } =>
|
|
84
|
+
Boolean(part) && typeof part === "object" && typeof (part as { type?: unknown }).type === "string");
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function partsToText(parts: Array<{ type: string; text?: string }>): string {
|
|
88
|
+
return parts
|
|
89
|
+
.filter((part) => part.type === "text" && typeof part.text === "string")
|
|
90
|
+
.map((part) => part.text)
|
|
91
|
+
.join("\n")
|
|
92
|
+
.trim();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function partsToThinking(parts: Array<{ type: string; thinking?: string }>): string {
|
|
96
|
+
return parts
|
|
97
|
+
.filter((part) => part.type === "thinking" && typeof part.thinking === "string")
|
|
98
|
+
.map((part) => part.thinking)
|
|
99
|
+
.join("\n")
|
|
100
|
+
.trim();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function parseArgsPayload(value: unknown): Record<string, unknown> | undefined {
|
|
104
|
+
if (typeof value !== "string" || !value.trim()) return undefined;
|
|
105
|
+
try {
|
|
106
|
+
const parsed: unknown = JSON.parse(value);
|
|
107
|
+
return parsed && typeof parsed === "object" && !Array.isArray(parsed)
|
|
108
|
+
? parsed as Record<string, unknown>
|
|
109
|
+
: undefined;
|
|
110
|
+
} catch {
|
|
111
|
+
// Truncated payloads (>32 KiB at write time) are not valid JSON; D4
|
|
112
|
+
// degrades them to empty arguments instead of failing the entry.
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function stringValue(record: Record<string, unknown>, key: string): string | undefined {
|
|
118
|
+
const value = record[key];
|
|
119
|
+
return typeof value === "string" && value.trim() ? value : undefined;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function nestedField(record: Record<string, unknown>, key: string): Record<string, unknown> | undefined {
|
|
123
|
+
const value = record[key];
|
|
124
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
125
|
+
? value as Record<string, unknown>
|
|
126
|
+
: undefined;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Builds entries from child-transcript v1 JSONL records. Mirrors the legacy
|
|
130
|
+
* parser's semantics: user messages open exchanges (a finished assistant turn
|
|
131
|
+
* is closed before the next supervisor message), tool calls pair with results
|
|
132
|
+
* by toolCallId regardless of record order, stderr becomes an error notice,
|
|
133
|
+
* and stdout stays ignored. */
|
|
134
|
+
function ingestRecords(
|
|
135
|
+
mod: NativeTranscriptModule,
|
|
136
|
+
state: TranscriptState,
|
|
137
|
+
records: Array<Record<string, unknown>>,
|
|
138
|
+
): { truncatedMarkerSeen: boolean } {
|
|
139
|
+
let truncatedMarkerSeen = false;
|
|
140
|
+
let currentTurnHasFinishedAssistant = false;
|
|
141
|
+
const pendingErrors = new Map<string, boolean>();
|
|
142
|
+
const degradedArgs = new Map<string, string>();
|
|
143
|
+
|
|
144
|
+
for (const record of records) {
|
|
145
|
+
const recordType = stringValue(record, "recordType");
|
|
146
|
+
if (recordType === "truncated") {
|
|
147
|
+
truncatedMarkerSeen = true;
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (recordType === "stdout") continue;
|
|
151
|
+
|
|
152
|
+
if (recordType === "stderr") {
|
|
153
|
+
const text = stringValue(record, "text");
|
|
154
|
+
if (text) mod.appendNotice(state, text, "error");
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (recordType === "tool_start") {
|
|
159
|
+
const turnId = mod.ensureTurn(state);
|
|
160
|
+
const toolName = stringValue(record, "toolName") ?? "tool";
|
|
161
|
+
const toolCallId = stringValue(record, "toolCallId") ?? `tool-${state.nextEntryId}`;
|
|
162
|
+
const parsedArgs = parseArgsPayload(record.argsPayload);
|
|
163
|
+
if (parsedArgs === undefined && record.argsPayload !== undefined) {
|
|
164
|
+
// Truncated/corrupt payload: builtin renderers need real args and
|
|
165
|
+
// may swallow text-only results, so remember to re-surface the
|
|
166
|
+
// preview and outcome as a warning notice after pairing completes.
|
|
167
|
+
degradedArgs.set(toolCallId, stringValue(record, "argsPreview") ?? "");
|
|
168
|
+
}
|
|
169
|
+
mod.ensureToolCall(state, turnId, toolCallId, toolName, parsedArgs ?? {});
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (recordType === "tool_end") {
|
|
174
|
+
const toolCallId = stringValue(record, "toolCallId");
|
|
175
|
+
if (toolCallId) pendingErrors.set(toolCallId, record.isError === true);
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (recordType !== "message") continue;
|
|
180
|
+
|
|
181
|
+
const message = nestedField(record, "message");
|
|
182
|
+
const role = stringValue(record, "role") ?? stringValue(message ?? {}, "role");
|
|
183
|
+
|
|
184
|
+
if (role === "user") {
|
|
185
|
+
if (state.currentTurnId !== null && currentTurnHasFinishedAssistant) {
|
|
186
|
+
mod.finishTurn(state);
|
|
187
|
+
}
|
|
188
|
+
const turnId = mod.ensureTurn(state);
|
|
189
|
+
const textParts = contentParts(message?.content);
|
|
190
|
+
const text = stringValue(record, "text") ?? partsToText(textParts);
|
|
191
|
+
if (!text) continue;
|
|
192
|
+
const existing = mod.findLatestEntry(state, turnId, "user-message");
|
|
193
|
+
if (existing) existing.text = text;
|
|
194
|
+
else mod.appendEntry(state, { type: "user-message", turnId, text });
|
|
195
|
+
currentTurnHasFinishedAssistant = false;
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (role === "assistant") {
|
|
200
|
+
const turnId = mod.ensureTurn(state);
|
|
201
|
+
const parts = contentParts(message?.content);
|
|
202
|
+
const thinking = partsToThinking(parts);
|
|
203
|
+
const text = partsToText(parts);
|
|
204
|
+
if (thinking) mod.upsertText(state, turnId, "thinking", thinking, false);
|
|
205
|
+
if (text) {
|
|
206
|
+
mod.upsertText(state, turnId, "assistant-text", text, false);
|
|
207
|
+
currentTurnHasFinishedAssistant = true;
|
|
208
|
+
}
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (role === "toolResult" || role === "tool_result") {
|
|
213
|
+
const toolCallId = stringValue(record, "toolCallId") ?? stringValue(message ?? {}, "toolCallId") ?? `tool-${state.nextEntryId}`;
|
|
214
|
+
const toolName = stringValue(record, "toolName") ?? stringValue(message ?? {}, "toolName") ?? "tool";
|
|
215
|
+
const failed = record.isError === true || message?.isError === true;
|
|
216
|
+
const isError = failed || pendingErrors.get(toolCallId) === true;
|
|
217
|
+
pendingErrors.delete(toolCallId);
|
|
218
|
+
const resultContent = Array.isArray(message?.content)
|
|
219
|
+
? message.content
|
|
220
|
+
: typeof record.text === "string"
|
|
221
|
+
? [{ type: "text", text: record.text }]
|
|
222
|
+
: [];
|
|
223
|
+
const turnId = state.toolCalls.get(toolCallId)?.turnId ?? mod.ensureTurn(state);
|
|
224
|
+
mod.upsertToolResult(
|
|
225
|
+
state,
|
|
226
|
+
turnId,
|
|
227
|
+
toolCallId,
|
|
228
|
+
toolName,
|
|
229
|
+
{ content: resultContent },
|
|
230
|
+
isError,
|
|
231
|
+
false,
|
|
232
|
+
);
|
|
233
|
+
currentTurnHasFinishedAssistant = false;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Terminal-state guarantee: every recorded call gets a result entry so the
|
|
238
|
+
// native component never renders a stale running spinner for history.
|
|
239
|
+
for (const [toolCallId, toolCall] of state.toolCalls.entries()) {
|
|
240
|
+
if (toolCall.resultEntryId !== undefined) continue;
|
|
241
|
+
const callEntry = state.entries.find((entry) => entry.id === toolCall.callEntryId);
|
|
242
|
+
if (callEntry?.type !== "tool-call") continue;
|
|
243
|
+
const isError = pendingErrors.get(toolCallId) === true;
|
|
244
|
+
mod.upsertToolResult(
|
|
245
|
+
state,
|
|
246
|
+
toolCall.turnId,
|
|
247
|
+
toolCallId,
|
|
248
|
+
callEntry.toolName,
|
|
249
|
+
{ content: [{ type: "text", text: isError ? "(tool ended with error)" : "(no result recorded)" }] },
|
|
250
|
+
isError,
|
|
251
|
+
false,
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// Degraded-argument visibility net: builtin renderers can hide text-only
|
|
256
|
+
// results when args are empty, so re-surface preview + outcome as a notice.
|
|
257
|
+
for (const [toolCallId, preview] of degradedArgs) {
|
|
258
|
+
const toolRecord = state.toolCalls.get(toolCallId);
|
|
259
|
+
const callEntry = state.entries.find((entry) => entry.id === toolRecord?.callEntryId);
|
|
260
|
+
if (callEntry?.type !== "tool-call") continue;
|
|
261
|
+
const resultEntry = toolRecord?.resultEntryId === undefined
|
|
262
|
+
? undefined
|
|
263
|
+
: state.entries.find((candidate) => candidate.id === toolRecord.resultEntryId);
|
|
264
|
+
const resultText = resultEntry?.type === "tool-result"
|
|
265
|
+
? (resultEntry.result?.content ?? []).map((part) => part.text ?? "").join(" ").trim()
|
|
266
|
+
: "";
|
|
267
|
+
const summary = [
|
|
268
|
+
`${callEntry.toolName}: arguments truncated`,
|
|
269
|
+
preview ? `preview ${preview}` : undefined,
|
|
270
|
+
resultText ? `result: ${resultText.split(/\r?\n/)[0]}` : undefined,
|
|
271
|
+
].filter(Boolean).join(" · ");
|
|
272
|
+
mod.appendNotice(state, summary, "warning");
|
|
273
|
+
}
|
|
274
|
+
return { truncatedMarkerSeen };
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function conversationStateOf(entries: readonly TranscriptEntry[]): string {
|
|
278
|
+
for (let index = entries.length - 1; index >= 0; index--) {
|
|
279
|
+
const entry = entries[index];
|
|
280
|
+
if (!entry || entry.type === "turn-boundary") continue;
|
|
281
|
+
if (entry.type === "assistant-text") return "assistant response";
|
|
282
|
+
if (entry.type === "user-message") return "supervisor message";
|
|
283
|
+
if (entry.type === "tool-result") return `${entry.toolName} · ${entry.isError ? "error" : "complete"}`;
|
|
284
|
+
if (entry.type === "tool-call") {
|
|
285
|
+
const toolRecord = state_toolCallStatus(entries, entry.toolCallId);
|
|
286
|
+
return `${entry.toolName} · ${toolRecord}`;
|
|
287
|
+
}
|
|
288
|
+
return "activity";
|
|
289
|
+
}
|
|
290
|
+
return "activity";
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function state_toolCallStatus(entries: readonly TranscriptEntry[], toolCallId: string): "running" | "complete" | "error" {
|
|
294
|
+
for (let index = entries.length - 1; index >= 0; index--) {
|
|
295
|
+
const entry = entries[index];
|
|
296
|
+
if (entry?.type === "tool-result" && entry.toolCallId === toolCallId) {
|
|
297
|
+
return entry.isError ? "error" : "complete";
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return "complete";
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Renders a persisted child transcript through Pi-native components. Reads
|
|
305
|
+
* via the shared trusted-root guard, replays records into shared-library
|
|
306
|
+
* entries, then renders with the same message/tool components the main
|
|
307
|
+
* transcript uses. Returns `null` only when the caller passed a module that
|
|
308
|
+
* turned out to be unusable; read/validation problems surface as warnings.
|
|
309
|
+
*/
|
|
310
|
+
export function buildNativeFleetTranscript(
|
|
311
|
+
mod: NativeTranscriptModule,
|
|
312
|
+
input: NativeFleetBuildInput,
|
|
313
|
+
): NativeFleetBuildResult {
|
|
314
|
+
mod.ensureTranscriptTheme();
|
|
315
|
+
const readOptions: FleetTranscriptReadOptions = {
|
|
316
|
+
trustedRoots: input.trustedRoots,
|
|
317
|
+
...(input.maxRecords !== undefined ? { maxRecords: input.maxRecords } : {}),
|
|
318
|
+
};
|
|
319
|
+
const read = readTranscriptRecords(input.filePath, readOptions);
|
|
320
|
+
if (read.records.length === 0) {
|
|
321
|
+
return {
|
|
322
|
+
lines: [],
|
|
323
|
+
conversationState: "activity",
|
|
324
|
+
truncated: read.truncated,
|
|
325
|
+
...(read.warning ? { warning: read.warning } : {}),
|
|
326
|
+
entryCount: 0,
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
const state = mod.createTranscriptState({});
|
|
331
|
+
const { truncatedMarkerSeen } = ingestRecords(mod, state, read.records);
|
|
332
|
+
|
|
333
|
+
const registry = new mod.TranscriptToolComponents({
|
|
334
|
+
expanded: input.expandedTools,
|
|
335
|
+
...(input.cwd ? { cwd: input.cwd } : {}),
|
|
336
|
+
});
|
|
337
|
+
for (const entry of state.entries) {
|
|
338
|
+
if (entry.type !== "tool-call") continue;
|
|
339
|
+
registry.handleStart(entry.toolCallId, entry.toolName, entry.args);
|
|
340
|
+
const toolRecord = state.toolCalls.get(entry.toolCallId);
|
|
341
|
+
const resultEntry = toolRecord?.resultEntryId === undefined
|
|
342
|
+
? undefined
|
|
343
|
+
: state.entries.find((candidate) => candidate.id === toolRecord.resultEntryId);
|
|
344
|
+
if (resultEntry?.type === "tool-result") {
|
|
345
|
+
registry.handleEnd(entry.toolCallId, entry.toolName, resultEntry.result, resultEntry.isError);
|
|
346
|
+
} else {
|
|
347
|
+
registry.handleEnd(entry.toolCallId, entry.toolName, null, false);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const lines = mod.renderTranscriptLines(state.entries, {
|
|
352
|
+
width: input.width,
|
|
353
|
+
theme: input.theme,
|
|
354
|
+
toolComponents: registry,
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
const indicatorLines: string[] = [];
|
|
358
|
+
if (read.truncated || truncatedMarkerSeen) {
|
|
359
|
+
indicatorLines.push(input.theme.fg("dim", "↑ Earlier activity omitted"));
|
|
360
|
+
}
|
|
361
|
+
if (read.warning) {
|
|
362
|
+
for (const line of read.warning.split(/\r?\n/)) {
|
|
363
|
+
indicatorLines.push(input.theme.fg("warning", `! ${line}`));
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
return {
|
|
368
|
+
lines: [...indicatorLines, ...lines],
|
|
369
|
+
conversationState: conversationStateOf(state.entries),
|
|
370
|
+
truncated: read.truncated || truncatedMarkerSeen,
|
|
371
|
+
...(read.warning ? { warning: read.warning } : {}),
|
|
372
|
+
entryCount: state.entries.length,
|
|
373
|
+
};
|
|
374
|
+
}
|
|
@@ -59,7 +59,7 @@ export interface FleetTranscript {
|
|
|
59
59
|
warning?: string;
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
interface FleetTranscriptReadOptions {
|
|
62
|
+
export interface FleetTranscriptReadOptions {
|
|
63
63
|
trustedRoots: string[];
|
|
64
64
|
maxRecords?: number;
|
|
65
65
|
maxBytes?: number;
|
|
@@ -356,6 +356,52 @@ export function readFleetTranscript(filePath: string, options: FleetTranscriptRe
|
|
|
356
356
|
};
|
|
357
357
|
}
|
|
358
358
|
|
|
359
|
+
export interface TranscriptRecordRead {
|
|
360
|
+
records: Array<Record<string, unknown>>;
|
|
361
|
+
truncated: boolean;
|
|
362
|
+
warning?: string;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Shared trusted-root-guarded tail reader that returns raw parsed JSONL
|
|
367
|
+
* records instead of fleet events. Both the legacy rail renderer and the
|
|
368
|
+
* native-component renderer ingest through this single validation path, so
|
|
369
|
+
* symlink refusal and path-escape rejection cannot diverge between them.
|
|
370
|
+
*/
|
|
371
|
+
export function readTranscriptRecords(filePath: string, options: FleetTranscriptReadOptions): TranscriptRecordRead {
|
|
372
|
+
const validated = validateTranscriptPath(filePath, options.trustedRoots);
|
|
373
|
+
if (!validated.resolvedPath) {
|
|
374
|
+
return { records: [], truncated: false, ...(validated.warning ? { warning: safeDisplayText(validated.warning) } : {}) };
|
|
375
|
+
}
|
|
376
|
+
const maxRecords = Math.max(1, options.maxRecords ?? DEFAULT_MAX_RECORDS);
|
|
377
|
+
const tail = readTailLines(validated.resolvedPath, Math.max(1024, options.maxBytes ?? DEFAULT_MAX_BYTES));
|
|
378
|
+
const selected = tail.lines.slice(-maxRecords);
|
|
379
|
+
const records: Array<Record<string, unknown>> = [];
|
|
380
|
+
let malformed = 0;
|
|
381
|
+
for (const line of selected) {
|
|
382
|
+
if (!line.trim()) continue;
|
|
383
|
+
try {
|
|
384
|
+
const parsed: unknown = JSON.parse(line);
|
|
385
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
386
|
+
records.push(parsed as Record<string, unknown>);
|
|
387
|
+
} else {
|
|
388
|
+
malformed++;
|
|
389
|
+
}
|
|
390
|
+
} catch {
|
|
391
|
+
malformed++;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
const warnings = [
|
|
395
|
+
tail.warning,
|
|
396
|
+
malformed > 0 ? `Skipped ${malformed} malformed transcript record${malformed === 1 ? "" : "s"}.` : undefined,
|
|
397
|
+
].filter((value): value is string => Boolean(value));
|
|
398
|
+
return {
|
|
399
|
+
records,
|
|
400
|
+
truncated: tail.truncated || tail.lines.length > maxRecords,
|
|
401
|
+
...(warnings.length ? { warning: safeDisplayText(warnings.join(" ")) } : {}),
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
|
|
359
405
|
function statusGlyph(event: Extract<FleetTranscriptEvent, { kind: "tool" }>, theme: Theme): string {
|
|
360
406
|
if (event.status === "running") return theme.fg("warning", "●");
|
|
361
407
|
if (event.status === "error") return theme.fg("error", "✗");
|
package/src/tui/fleet.ts
CHANGED
|
@@ -18,7 +18,8 @@ import { stopAsyncRun } from "../runs/foreground/async-stop-action.ts";
|
|
|
18
18
|
import { resolveWorkflowForegroundSteeringTarget, steerWorkflowForegroundTarget } from "../runs/foreground/workflow-foreground-steering.ts";
|
|
19
19
|
import { contextModeBadge, contextModeLabel } from "../runs/shared/context-mode.ts";
|
|
20
20
|
import { FLEET_STATUS_WIDGET_KEY } from "./fleet-status.ts";
|
|
21
|
-
import { readFleetTranscript, renderFleetTranscript
|
|
21
|
+
import { readFleetTranscript, renderFleetTranscript } from "./fleet-transcript.ts";
|
|
22
|
+
import { buildNativeFleetTranscript, loadNativeTranscriptSupport, type NativeTranscriptModule } from "./fleet-native-transcript.ts";
|
|
22
23
|
import { handleHerdrInspectorAction } from "../inspectors/herdr/actions.ts";
|
|
23
24
|
import type { HerdrClient } from "../inspectors/herdr/client.ts";
|
|
24
25
|
import { getLivePromptAudit, type LivePromptAudit, type PromptAuditView } from "../runs/foreground/prompt-audit.ts";
|
|
@@ -47,6 +48,7 @@ export const DEFAULT_FLEET_KEYBINDINGS: Record<FleetKeybindingAction, string[]>
|
|
|
47
48
|
inspect: ["H"],
|
|
48
49
|
stop: ["D"],
|
|
49
50
|
toggleTools: ["x", "X", "ctrl+o"],
|
|
51
|
+
toggleRenderer: ["v", "V"],
|
|
50
52
|
};
|
|
51
53
|
|
|
52
54
|
type ResolvedFleetKeybindings = Record<FleetKeybindingAction, string[]>;
|
|
@@ -750,11 +752,14 @@ interface FleetDetailSections {
|
|
|
750
752
|
}
|
|
751
753
|
|
|
752
754
|
interface FleetTranscriptCache {
|
|
755
|
+
mode: "native" | "legacy";
|
|
753
756
|
path: string;
|
|
754
757
|
fingerprint: string;
|
|
755
758
|
width: number;
|
|
756
759
|
expandedTools: boolean;
|
|
757
|
-
|
|
760
|
+
conversationState: string;
|
|
761
|
+
hasContent: boolean;
|
|
762
|
+
warning?: string;
|
|
758
763
|
body: string[];
|
|
759
764
|
}
|
|
760
765
|
|
|
@@ -778,6 +783,10 @@ export class SubagentFleetComponent implements Component {
|
|
|
778
783
|
private bodyHeight = 8;
|
|
779
784
|
private lastRosterWidth: number | undefined;
|
|
780
785
|
private expandedTools = false;
|
|
786
|
+
/** Structured-view renderer selection; legacy is the always-available fallback. */
|
|
787
|
+
private rendererMode: "native" | "legacy" = "native";
|
|
788
|
+
/** undefined = still loading, null = unavailable (permanent for this inspector session). */
|
|
789
|
+
private nativeModule: NativeTranscriptModule | null | undefined;
|
|
781
790
|
private promptAuditOpen = false;
|
|
782
791
|
private promptAuditView: PromptAuditView = "authored";
|
|
783
792
|
private actionNotice: FleetActionResult | undefined;
|
|
@@ -816,6 +825,52 @@ export class SubagentFleetComponent implements Component {
|
|
|
816
825
|
this.selectedKey = options.initialKey;
|
|
817
826
|
this.refresh();
|
|
818
827
|
this.scheduleRefresh();
|
|
828
|
+
// Resolve native-renderer availability once; until it settles the
|
|
829
|
+
// inspector renders through the legacy path (see renderedTranscript).
|
|
830
|
+
void loadNativeTranscriptSupport().then((mod) => {
|
|
831
|
+
this.nativeModule = mod;
|
|
832
|
+
if (!this.disposed && this.rendererMode === "native") this.tui.requestRender();
|
|
833
|
+
});
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
private nativeRenderedOutcome(mod: NativeTranscriptModule, target: { path: string; trustedRoots: string[] }, width: number): { body: string[]; conversationState: string; hasContent: boolean; warning?: string } {
|
|
837
|
+
try {
|
|
838
|
+
const built = buildNativeFleetTranscript(mod, {
|
|
839
|
+
filePath: target.path,
|
|
840
|
+
trustedRoots: target.trustedRoots,
|
|
841
|
+
width,
|
|
842
|
+
expandedTools: this.expandedTools,
|
|
843
|
+
cwd: this.state.baseCwd,
|
|
844
|
+
theme: this.theme,
|
|
845
|
+
});
|
|
846
|
+
return { body: built.lines, conversationState: built.conversationState, hasContent: built.entryCount > 0, ...(built.warning ? { warning: built.warning } : {}) };
|
|
847
|
+
} catch {
|
|
848
|
+
// A native-render failure must never blank the inspector: degrade to
|
|
849
|
+
// the legacy renderer for the rest of this inspector session.
|
|
850
|
+
this.nativeModule = null;
|
|
851
|
+
return this.legacyRenderedOutcome(target, width);
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
private legacyRenderedOutcome(target: { path: string; trustedRoots: string[] }, width: number): { body: string[]; conversationState: string; hasContent: boolean; warning?: string } {
|
|
856
|
+
const transcript = readFleetTranscript(target.path, { trustedRoots: target.trustedRoots });
|
|
857
|
+
const body = transcript.events.length > 0
|
|
858
|
+
? renderFleetTranscript(transcript, width, this.theme, this.markdownTheme, { expandedTools: this.expandedTools })
|
|
859
|
+
: [];
|
|
860
|
+
const latest = transcript.events.at(-1);
|
|
861
|
+
const conversationState = latest?.kind === "assistant"
|
|
862
|
+
? "assistant response"
|
|
863
|
+
: latest?.kind === "user"
|
|
864
|
+
? "supervisor message"
|
|
865
|
+
: latest?.kind === "tool"
|
|
866
|
+
? `${latest.name} · ${latest.status}`
|
|
867
|
+
: "activity";
|
|
868
|
+
return {
|
|
869
|
+
body,
|
|
870
|
+
conversationState,
|
|
871
|
+
hasContent: transcript.events.length > 0,
|
|
872
|
+
...(transcript.warning ? { warning: transcript.warning } : {}),
|
|
873
|
+
};
|
|
819
874
|
}
|
|
820
875
|
|
|
821
876
|
private scheduleRefresh(): void {
|
|
@@ -1202,6 +1257,22 @@ export class SubagentFleetComponent implements Component {
|
|
|
1202
1257
|
this.expandedTools = !this.expandedTools;
|
|
1203
1258
|
this.transcriptCache = undefined;
|
|
1204
1259
|
this.tui.requestRender();
|
|
1260
|
+
return;
|
|
1261
|
+
}
|
|
1262
|
+
if (matchesFleetAction(data, this.keybindings, "toggleRenderer")) {
|
|
1263
|
+
if (this.rendererMode === "native" && this.nativeModule === null) {
|
|
1264
|
+
this.setActionNotice({ text: "Native transcript renderer unavailable in this host; staying on the text view.", isError: true });
|
|
1265
|
+
return;
|
|
1266
|
+
}
|
|
1267
|
+
if (this.rendererMode === "legacy" && this.nativeModule === undefined) {
|
|
1268
|
+
this.setActionNotice({ text: "Native transcript renderer is still loading." });
|
|
1269
|
+
return;
|
|
1270
|
+
}
|
|
1271
|
+
this.rendererMode = this.rendererMode === "native" ? "legacy" : "native";
|
|
1272
|
+
this.detailAutoFollow = true;
|
|
1273
|
+
this.transcriptCache = undefined;
|
|
1274
|
+
this.tui.requestRender();
|
|
1275
|
+
return;
|
|
1205
1276
|
}
|
|
1206
1277
|
}
|
|
1207
1278
|
|
|
@@ -1219,21 +1290,29 @@ export class SubagentFleetComponent implements Component {
|
|
|
1219
1290
|
});
|
|
1220
1291
|
}
|
|
1221
1292
|
|
|
1222
|
-
private renderedTranscript(target: { path: string; trustedRoots: string[] }, width: number): {
|
|
1223
|
-
const fingerprint = `${target.trustedRoots.join("\0")}|${transcriptFingerprint(target.path)}`;
|
|
1293
|
+
private renderedTranscript(target: { path: string; trustedRoots: string[] }, width: number): { body: string[]; conversationState: string; hasContent: boolean; warning?: string } {
|
|
1294
|
+
const fingerprint = `${this.rendererMode}|${target.trustedRoots.join("\0")}|${transcriptFingerprint(target.path)}`;
|
|
1224
1295
|
if (this.transcriptCache
|
|
1296
|
+
&& this.transcriptCache.mode === this.rendererMode
|
|
1225
1297
|
&& this.transcriptCache.path === target.path
|
|
1226
1298
|
&& this.transcriptCache.fingerprint === fingerprint
|
|
1227
1299
|
&& this.transcriptCache.width === width
|
|
1228
1300
|
&& this.transcriptCache.expandedTools === this.expandedTools) {
|
|
1229
|
-
return {
|
|
1301
|
+
return {
|
|
1302
|
+
body: [...this.transcriptCache.body],
|
|
1303
|
+
conversationState: this.transcriptCache.conversationState,
|
|
1304
|
+
hasContent: this.transcriptCache.hasContent,
|
|
1305
|
+
...(this.transcriptCache.warning ? { warning: this.transcriptCache.warning } : {}),
|
|
1306
|
+
};
|
|
1230
1307
|
}
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1308
|
+
// While the native module is still resolving (undefined) or unavailable
|
|
1309
|
+
// (null) the legacy rail renderer serves the view; both paths share the
|
|
1310
|
+
// same trusted-root read and cache entry.
|
|
1311
|
+
const outcome = this.rendererMode === "native" && this.nativeModule
|
|
1312
|
+
? this.nativeRenderedOutcome(this.nativeModule, target, width)
|
|
1313
|
+
: this.legacyRenderedOutcome(target, width);
|
|
1314
|
+
this.transcriptCache = { mode: this.rendererMode, path: target.path, fingerprint, width, expandedTools: this.expandedTools, ...outcome, body: [...outcome.body] };
|
|
1315
|
+
return outcome;
|
|
1237
1316
|
}
|
|
1238
1317
|
|
|
1239
1318
|
private promptAuditDetail(width: number): FleetDetailSections {
|
|
@@ -1270,20 +1349,13 @@ export class SubagentFleetComponent implements Component {
|
|
|
1270
1349
|
if (selected) {
|
|
1271
1350
|
const target = transcriptTarget(selected, this.state);
|
|
1272
1351
|
if (target) {
|
|
1273
|
-
const
|
|
1274
|
-
transcriptWarning =
|
|
1275
|
-
if (
|
|
1352
|
+
const rendered = this.renderedTranscript(target, width);
|
|
1353
|
+
transcriptWarning = rendered.warning;
|
|
1354
|
+
if (rendered.hasContent) {
|
|
1355
|
+
const body = rendered.body;
|
|
1276
1356
|
if (this.snapshot.error) body.unshift(this.theme.fg("warning", `Fleet scan warning: ${this.snapshot.error}`), "");
|
|
1277
|
-
const latest = transcript.events.at(-1);
|
|
1278
|
-
const conversationState = latest?.kind === "assistant"
|
|
1279
|
-
? "assistant response"
|
|
1280
|
-
: latest?.kind === "user"
|
|
1281
|
-
? "supervisor message"
|
|
1282
|
-
: latest?.kind === "tool"
|
|
1283
|
-
? `${latest.name} · ${latest.status}`
|
|
1284
|
-
: "activity";
|
|
1285
1357
|
const promptSummary = selected.kind === "foreground-active" ? foregroundAuthoredPromptSummary(selected, this.state) : undefined;
|
|
1286
|
-
return { header: structuredHeader(selected, width, this.theme, conversationState, promptSummary), body: this.withActionLines(body) };
|
|
1358
|
+
return { header: structuredHeader(selected, width, this.theme, rendered.conversationState, promptSummary), body: this.withActionLines(body) };
|
|
1287
1359
|
}
|
|
1288
1360
|
}
|
|
1289
1361
|
}
|
|
@@ -1352,7 +1424,7 @@ export class SubagentFleetComponent implements Component {
|
|
|
1352
1424
|
? ` j/k child · 1/2/3 view · g redo with guidance · c copy · wheel ↑↓ · Esc close Prompt Audit · ${position}`
|
|
1353
1425
|
: selected?.kind === "external"
|
|
1354
1426
|
? ` ${bindingLabel(this.keybindings, "selectUp")}/${bindingLabel(this.keybindings, "selectDown")} job · display-only · ${bindingLabel(this.keybindings, "refresh")} refresh · wheel ↑↓ · ${bindingLabel(this.keybindings, "close")} close · ${position}`
|
|
1355
|
-
: ` ${bindingLabel(this.keybindings, "selectUp")}/${bindingLabel(this.keybindings, "selectDown")} agent · p Prompt Audit · ${bindingLabel(this.keybindings, "inspect")} Herdr · ${bindingLabel(this.keybindings, "steer")} steer · ${bindingLabel(this.keybindings, "stop")} stop · ${bindingLabel(this.keybindings, "toggleTools")} tools · ${bindingLabel(this.keybindings, "refresh")} refresh · wheel ↑↓ · ${bindingLabel(this.keybindings, "close")} close · ${position}`;
|
|
1427
|
+
: ` ${bindingLabel(this.keybindings, "selectUp")}/${bindingLabel(this.keybindings, "selectDown")} agent · p Prompt Audit · ${bindingLabel(this.keybindings, "inspect")} Herdr · ${bindingLabel(this.keybindings, "steer")} steer · ${bindingLabel(this.keybindings, "stop")} stop · ${bindingLabel(this.keybindings, "toggleTools")} tools · ${bindingLabel(this.keybindings, "toggleRenderer")} view · ${bindingLabel(this.keybindings, "refresh")} refresh · wheel ↑↓ · ${bindingLabel(this.keybindings, "close")} close · ${position}`;
|
|
1356
1428
|
lines.push(this.theme.fg("border", "│") + fit(this.theme.fg("dim", footer), innerWidth) + this.theme.fg("border", "│"));
|
|
1357
1429
|
lines.push(this.theme.fg("border", `╰${"─".repeat(innerWidth)}╯`));
|
|
1358
1430
|
return lines.map((line) => truncateToWidth(line, width));
|