pi-better-subagents 0.1.18 → 0.1.19
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/index.ts +62 -7
- package/package.json +1 -1
- package/parse.ts +116 -1
- package/shared-navigator.ts +159 -36
package/index.ts
CHANGED
|
@@ -14,6 +14,8 @@
|
|
|
14
14
|
import { execSync } from "node:child_process";
|
|
15
15
|
import { writeFileSync, mkdirSync, statSync } from "node:fs";
|
|
16
16
|
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
17
|
+
import * as PiCodingAgent from "@earendil-works/pi-coding-agent";
|
|
18
|
+
import * as PiTui from "@earendil-works/pi-tui";
|
|
17
19
|
import { CustomEditor } from "@earendil-works/pi-coding-agent";
|
|
18
20
|
import { matchesKey, Key, truncateToWidth } from "@earendil-works/pi-tui";
|
|
19
21
|
import { Type } from "@earendil-works/pi-ai";
|
|
@@ -30,7 +32,7 @@ import {
|
|
|
30
32
|
type BackgroundWorkRow,
|
|
31
33
|
} from "./shared-navigator.ts";
|
|
32
34
|
import { spawnDetached, type SpawnResult } from "./spawn.ts";
|
|
33
|
-
import { parseRun,
|
|
35
|
+
import { parseRun, readRunTranscript, resetParseRunCursor, type Usage } from "./parse.ts";
|
|
34
36
|
import { finalizeRun as finalizeRunCore } from "./finalization.ts";
|
|
35
37
|
import { loadConfig, normalizeTools, resolveExtensionPath, SAFE_DEFAULT_TOOLS, SAFE_CLEAN_TOOLS, DEFAULT_MAX_CONCURRENT } from "./config.ts";
|
|
36
38
|
import { resolveExtensions, extensionArgs } from "./extensions.ts";
|
|
@@ -836,7 +838,8 @@ function mainAgentWorkRow(now: number): BackgroundWorkRow {
|
|
|
836
838
|
function subagentWorkDetail(id: string, now: number, options?: { logTailLines?: number }): BackgroundWorkDetail | null {
|
|
837
839
|
const detail = navigatorDetail(id, now);
|
|
838
840
|
if (!detail) return null;
|
|
839
|
-
|
|
841
|
+
void options;
|
|
842
|
+
const transcript = readRunTranscript(id);
|
|
840
843
|
const metadata = [
|
|
841
844
|
{ label: "provider", value: "Subagents" },
|
|
842
845
|
{ label: "model", value: detail.effort ? `${detail.model} · effort ${detail.effort}` : detail.model },
|
|
@@ -854,15 +857,65 @@ function subagentWorkDetail(id: string, now: number, options?: { logTailLines?:
|
|
|
854
857
|
statusTone: statusTone(detail.status),
|
|
855
858
|
subtitle: detail.currentTool ? `current tool ${detail.currentTool}` : undefined,
|
|
856
859
|
metadata,
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
// activity/result snapshot.
|
|
861
|
-
evidence: { label: "log tail", text: tailLog(id, logTailLines) },
|
|
860
|
+
evidence: { label: "transcript", text: detail.output || "(no transcript yet)" },
|
|
861
|
+
transcript: transcript.entries,
|
|
862
|
+
transcriptDiagnostic: transcript.diagnostic,
|
|
862
863
|
footerActions: [detail.status === "running" || detail.status === "orphaned" ? "x stop" : "x dismiss"],
|
|
863
864
|
};
|
|
864
865
|
}
|
|
865
866
|
|
|
867
|
+
function createSubagentTranscriptComponent(detail: BackgroundWorkDetail, theme: unknown) {
|
|
868
|
+
const ContainerComponent = (PiTui as any).Container;
|
|
869
|
+
const AssistantComponent = (PiCodingAgent as any).AssistantMessageComponent;
|
|
870
|
+
const ToolComponent = (PiCodingAgent as any).ToolExecutionComponent;
|
|
871
|
+
const markdownTheme = typeof (PiCodingAgent as any).getMarkdownTheme === "function"
|
|
872
|
+
? (PiCodingAgent as any).getMarkdownTheme()
|
|
873
|
+
: {};
|
|
874
|
+
if (!ContainerComponent || !AssistantComponent || !ToolComponent) {
|
|
875
|
+
return {
|
|
876
|
+
render: () => (detail.transcript ?? []).flatMap((entry) => entry.type === "assistant"
|
|
877
|
+
? entry.content.filter((block) => block.type === "text").flatMap((block) => String(block.text ?? "").split("\n"))
|
|
878
|
+
: [`[${entry.state}] ${entry.name}`]),
|
|
879
|
+
invalidate() {},
|
|
880
|
+
};
|
|
881
|
+
}
|
|
882
|
+
const container = new ContainerComponent();
|
|
883
|
+
const ui = { requestRender: () => { try { (uiCtx?.ui as any)?.requestRender?.(); } catch { /* ignore */ } } };
|
|
884
|
+
for (const entry of detail.transcript ?? []) {
|
|
885
|
+
if (entry.type === "assistant") {
|
|
886
|
+
const message = {
|
|
887
|
+
role: "assistant" as const,
|
|
888
|
+
content: entry.content,
|
|
889
|
+
stopReason: entry.streaming ? undefined : "stop",
|
|
890
|
+
timestamp: Date.now(),
|
|
891
|
+
};
|
|
892
|
+
const component = new AssistantComponent(message as any, true, markdownTheme, "Thinking...", 1);
|
|
893
|
+
component.updateContent(message as any, entry.streaming);
|
|
894
|
+
container.addChild(component);
|
|
895
|
+
continue;
|
|
896
|
+
}
|
|
897
|
+
const component = new ToolComponent(
|
|
898
|
+
entry.name,
|
|
899
|
+
entry.id ?? `transcript-${entry.name}`,
|
|
900
|
+
entry.args ?? {},
|
|
901
|
+
{ showImages: false },
|
|
902
|
+
undefined,
|
|
903
|
+
ui as any,
|
|
904
|
+
uiCtx?.cwd ?? process.cwd(),
|
|
905
|
+
);
|
|
906
|
+
component.markExecutionStarted();
|
|
907
|
+
component.setArgsComplete();
|
|
908
|
+
if (entry.state === "completed") {
|
|
909
|
+
const result = entry.result && typeof entry.result === "object"
|
|
910
|
+
? entry.result as any
|
|
911
|
+
: { content: entry.result == null ? [] : [{ type: "text", text: String(entry.result) }] };
|
|
912
|
+
component.updateResult({ ...result, isError: entry.isError });
|
|
913
|
+
}
|
|
914
|
+
container.addChild(component);
|
|
915
|
+
}
|
|
916
|
+
return container;
|
|
917
|
+
}
|
|
918
|
+
|
|
866
919
|
function ensureSubagentProvider(): void {
|
|
867
920
|
if (unregisterSubagentProvider) return;
|
|
868
921
|
const provider: BackgroundWorkProvider = {
|
|
@@ -870,6 +923,7 @@ function ensureSubagentProvider(): void {
|
|
|
870
923
|
label: "Subagents",
|
|
871
924
|
priority: 10,
|
|
872
925
|
visibleCount: () => navigatorRunningCount(),
|
|
926
|
+
showSection: (rows) => rows.some((row) => row.status === "running"),
|
|
873
927
|
parentRow: (now) => mainAgentWorkRow(now),
|
|
874
928
|
listRows: (now) => subagentWorkRows(now),
|
|
875
929
|
detail: (id, now, options) => subagentWorkDetail(id, now, options),
|
|
@@ -961,6 +1015,7 @@ function ensureNavigator(ctx: ExtensionContext): void {
|
|
|
961
1015
|
isOpenTrigger: (data: string) => matchesKey(data, Key.left),
|
|
962
1016
|
matchKey: (data: string, keyId: string) => matchesKey(data, keyId),
|
|
963
1017
|
truncate: truncateToWidth,
|
|
1018
|
+
createTranscriptComponent: createSubagentTranscriptComponent,
|
|
964
1019
|
});
|
|
965
1020
|
} catch { /* ignore */ }
|
|
966
1021
|
}
|
package/package.json
CHANGED
package/parse.ts
CHANGED
|
@@ -30,7 +30,7 @@ import { readBoundedTail, tailTerminalDisplay } from "./shared-log-utils.ts";
|
|
|
30
30
|
import { readAppendedLines, type LogCursor } from "./log-cursor.ts";
|
|
31
31
|
import { logPathFor } from "./registry.ts";
|
|
32
32
|
|
|
33
|
-
interface ContentBlock { type: string; text?: string; name?: string }
|
|
33
|
+
interface ContentBlock { type: string; text?: string; thinking?: string; name?: string }
|
|
34
34
|
interface Cost { total?: number }
|
|
35
35
|
interface MsgUsage { input?: number; output?: number; cacheRead?: number; cost?: Cost }
|
|
36
36
|
interface Msg { role?: string; content?: string | ContentBlock[]; usage?: MsgUsage }
|
|
@@ -130,6 +130,121 @@ export interface ParsedRun {
|
|
|
130
130
|
diagnostics: string[];
|
|
131
131
|
}
|
|
132
132
|
|
|
133
|
+
export type TranscriptEntry =
|
|
134
|
+
| { type: "assistant"; content: ContentBlock[]; streaming: boolean }
|
|
135
|
+
| {
|
|
136
|
+
type: "tool";
|
|
137
|
+
id?: string;
|
|
138
|
+
name: string;
|
|
139
|
+
args?: unknown;
|
|
140
|
+
result?: unknown;
|
|
141
|
+
isError: boolean;
|
|
142
|
+
state: "running" | "completed";
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
export interface RunTranscript {
|
|
146
|
+
entries: TranscriptEntry[];
|
|
147
|
+
truncated: boolean;
|
|
148
|
+
diagnostic?: string;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const DEFAULT_TRANSCRIPT_TAIL_BYTES = 2 * 1024 * 1024;
|
|
152
|
+
const DEFAULT_TRANSCRIPT_ENTRIES = 40;
|
|
153
|
+
|
|
154
|
+
function transcriptContent(msg: Msg | undefined): ContentBlock[] {
|
|
155
|
+
if (!msg) return [];
|
|
156
|
+
if (typeof msg.content === "string") {
|
|
157
|
+
return msg.content.trim() ? [{ type: "text", text: msg.content }] : [];
|
|
158
|
+
}
|
|
159
|
+
if (!Array.isArray(msg.content)) return [];
|
|
160
|
+
return msg.content.filter((block) =>
|
|
161
|
+
block && (
|
|
162
|
+
(block.type === "text" && typeof block.text === "string" && block.text.trim()) ||
|
|
163
|
+
(block.type === "thinking" && typeof (block as { thinking?: unknown }).thinking === "string")
|
|
164
|
+
),
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Bounded, display-oriented transcript fold for the subagent detail view. */
|
|
169
|
+
export function readRunTranscript(
|
|
170
|
+
id: string,
|
|
171
|
+
options: { maxBytes?: number; maxEntries?: number } = {},
|
|
172
|
+
): RunTranscript {
|
|
173
|
+
const maxBytes = Math.max(1024, options.maxBytes ?? DEFAULT_TRANSCRIPT_TAIL_BYTES);
|
|
174
|
+
const maxEntries = Math.max(1, options.maxEntries ?? DEFAULT_TRANSCRIPT_ENTRIES);
|
|
175
|
+
const tail = readTail(logPathFor(id), maxBytes);
|
|
176
|
+
if (tail.error) return { entries: [], truncated: false, diagnostic: `Log unreadable: ${tail.error}` };
|
|
177
|
+
|
|
178
|
+
const lines = tail.text.split(/\r?\n/);
|
|
179
|
+
if (tail.truncated) lines.shift();
|
|
180
|
+
const entries: TranscriptEntry[] = [];
|
|
181
|
+
const tools = new Map<string, Extract<TranscriptEntry, { type: "tool" }>>();
|
|
182
|
+
let anonymousTool = 0;
|
|
183
|
+
let liveAssistant: Extract<TranscriptEntry, { type: "assistant" }> | undefined;
|
|
184
|
+
|
|
185
|
+
for (const line of lines) {
|
|
186
|
+
const event = tryParse(line.trim());
|
|
187
|
+
if (!event) continue;
|
|
188
|
+
const type = event.type;
|
|
189
|
+
if (type === "message_end") {
|
|
190
|
+
const message = event.message as Msg | undefined;
|
|
191
|
+
if (message?.role !== "assistant") continue;
|
|
192
|
+
const content = transcriptContent(message);
|
|
193
|
+
if (content.length) entries.push({ type: "assistant", content, streaming: false });
|
|
194
|
+
liveAssistant = undefined;
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
if (type === "message_update") {
|
|
198
|
+
const message = event.message as Msg | undefined;
|
|
199
|
+
if (message?.role !== "assistant") continue;
|
|
200
|
+
const content = transcriptContent(message);
|
|
201
|
+
if (content.length) liveAssistant = { type: "assistant", content, streaming: true };
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
if (type === "tool_execution_start") {
|
|
205
|
+
const idValue = typeof event.toolCallId === "string" ? event.toolCallId : undefined;
|
|
206
|
+
const key = idValue ?? `anonymous:${anonymousTool++}`;
|
|
207
|
+
const tool: Extract<TranscriptEntry, { type: "tool" }> = {
|
|
208
|
+
type: "tool",
|
|
209
|
+
id: idValue,
|
|
210
|
+
name: typeof event.toolName === "string" ? event.toolName : "unknown",
|
|
211
|
+
args: event.args,
|
|
212
|
+
isError: false,
|
|
213
|
+
state: "running",
|
|
214
|
+
};
|
|
215
|
+
entries.push(tool);
|
|
216
|
+
tools.set(key, tool);
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
if (type === "tool_execution_end") {
|
|
220
|
+
const idValue = typeof event.toolCallId === "string" ? event.toolCallId : undefined;
|
|
221
|
+
let tool = idValue ? tools.get(idValue) : undefined;
|
|
222
|
+
if (!tool && typeof event.toolName === "string") {
|
|
223
|
+
tool = [...tools.values()].reverse().find((candidate) => candidate.name === event.toolName && candidate.state === "running");
|
|
224
|
+
}
|
|
225
|
+
if (!tool) {
|
|
226
|
+
tool = {
|
|
227
|
+
type: "tool",
|
|
228
|
+
id: idValue,
|
|
229
|
+
name: typeof event.toolName === "string" ? event.toolName : "unknown",
|
|
230
|
+
isError: event.isError === true,
|
|
231
|
+
state: "completed",
|
|
232
|
+
};
|
|
233
|
+
entries.push(tool);
|
|
234
|
+
}
|
|
235
|
+
tool.result = event.result;
|
|
236
|
+
tool.isError = event.isError === true;
|
|
237
|
+
tool.state = "completed";
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
if (liveAssistant) entries.push(liveAssistant);
|
|
241
|
+
return {
|
|
242
|
+
entries: entries.slice(-maxEntries),
|
|
243
|
+
truncated: tail.truncated || entries.length > maxEntries,
|
|
244
|
+
diagnostic: tail.truncated ? `Showing the latest ${fmtBytes(maxBytes)} of the transcript.` : undefined,
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
133
248
|
/**
|
|
134
249
|
* Authoritative lifecycle evidence scanned from the complete NDJSON stream.
|
|
135
250
|
* Kept separate from parseRun()'s bounded tail so large-log result parsing stays
|
package/shared-navigator.ts
CHANGED
|
@@ -39,6 +39,11 @@ export type BackgroundWorkDetail = {
|
|
|
39
39
|
metadata: Array<{ label: string; value: string }>;
|
|
40
40
|
foldedSections?: Array<{ id: string; label: string; text: string; collapsedText?: string; expandedByDefault?: boolean }>;
|
|
41
41
|
evidence: { label: string; text: string };
|
|
42
|
+
transcript?: Array<
|
|
43
|
+
| { type: "assistant"; content: Array<{ type: string; text?: string; thinking?: string }>; streaming: boolean }
|
|
44
|
+
| { type: "tool"; id?: string; name: string; args?: unknown; result?: unknown; isError: boolean; state: "running" | "completed" }
|
|
45
|
+
>;
|
|
46
|
+
transcriptDiagnostic?: string;
|
|
42
47
|
footerActions?: string[];
|
|
43
48
|
};
|
|
44
49
|
|
|
@@ -54,6 +59,7 @@ export type BackgroundWorkProvider = {
|
|
|
54
59
|
label: string;
|
|
55
60
|
priority: number;
|
|
56
61
|
visibleCount(): number;
|
|
62
|
+
showSection?(rows: BackgroundWorkRow[], now: number): boolean;
|
|
57
63
|
parentRow?(now: number): BackgroundWorkRow | null;
|
|
58
64
|
listRows(now: number): BackgroundWorkRow[];
|
|
59
65
|
detail(id: string, now: number, options?: { logTailLines?: number }): BackgroundWorkDetail | null;
|
|
@@ -67,6 +73,7 @@ type HostDeps = {
|
|
|
67
73
|
isOpenTrigger: (data: string) => boolean;
|
|
68
74
|
matchKey: (data: string, keyId: string) => boolean;
|
|
69
75
|
truncate: (s: string, width: number) => string;
|
|
76
|
+
createTranscriptComponent?: (detail: BackgroundWorkDetail, theme: unknown) => Component;
|
|
70
77
|
};
|
|
71
78
|
|
|
72
79
|
type NavigatorState = {
|
|
@@ -83,6 +90,7 @@ type NavigatorState = {
|
|
|
83
90
|
mainListCloseArm?: { id: string; armedAt: number };
|
|
84
91
|
mainListCloseArmTimer?: ReturnType<typeof setTimeout>;
|
|
85
92
|
mainListDeadlineScheduler?: RenderScheduler;
|
|
93
|
+
editorComponent?: Component;
|
|
86
94
|
detailOverlayRows?: number;
|
|
87
95
|
dispose?: () => void;
|
|
88
96
|
};
|
|
@@ -99,8 +107,7 @@ export const CLOSE_ARM_MS = 3000;
|
|
|
99
107
|
export const DEFAULT_LOG_TAIL_ROWS = 10;
|
|
100
108
|
export const LOG_TAIL_ROW_CHOICES = [10, 25] as const;
|
|
101
109
|
const MAIN_LIST_FALLBACK_WIDTH = 100;
|
|
102
|
-
const
|
|
103
|
-
const DETAIL_OVERLAY_FOOTER_MARGIN_ROWS = 3;
|
|
110
|
+
const DETAIL_OVERLAY_FOOTER_ROWS = 3;
|
|
104
111
|
const EVIDENCE_SECTION_ID = "__evidence__";
|
|
105
112
|
const RUNNING_DOT_GLYPH = "●";
|
|
106
113
|
const RUNNING_DOT_FRAMES = ["dim", "accent", "accent", "dim"] as const;
|
|
@@ -315,7 +322,7 @@ function renderWidth(width: number): number {
|
|
|
315
322
|
return Number.isFinite(width) && width > 0 ? Math.floor(width) : MAIN_LIST_FALLBACK_WIDTH;
|
|
316
323
|
}
|
|
317
324
|
|
|
318
|
-
type InternalRow = BackgroundWorkRow & { navigatorId: string; providerLabel: string };
|
|
325
|
+
type InternalRow = BackgroundWorkRow & { navigatorId: string; providerLabel: string; parentRow?: boolean };
|
|
319
326
|
|
|
320
327
|
function rowKey(providerId: string, id: string): string {
|
|
321
328
|
return `${providerId}:${id}`;
|
|
@@ -333,6 +340,19 @@ function listRows(now = Date.now()): InternalRow[] {
|
|
|
333
340
|
let providerRows: BackgroundWorkRow[] = [];
|
|
334
341
|
try { providerRows = provider.listRows(now) ?? []; } catch { providerRows = []; }
|
|
335
342
|
const orderedProviderRows = [...providerRows].sort((a, b) => b.sortStartedAt - a.sortStartedAt || rowDisplayName(a).localeCompare(rowDisplayName(b)));
|
|
343
|
+
let showSection = orderedProviderRows.length > 0 || provider.parentRow !== undefined;
|
|
344
|
+
try { showSection = provider.showSection?.(orderedProviderRows, now) ?? showSection; } catch { showSection = false; }
|
|
345
|
+
if (!showSection) continue;
|
|
346
|
+
let parentRow: BackgroundWorkRow | null = null;
|
|
347
|
+
try { parentRow = provider.parentRow?.(now) ?? null; } catch { parentRow = null; }
|
|
348
|
+
if (parentRow) {
|
|
349
|
+
rows.push({
|
|
350
|
+
...parentRow,
|
|
351
|
+
navigatorId: rowKey(parentRow.providerId, parentRow.id),
|
|
352
|
+
providerLabel: provider.label,
|
|
353
|
+
parentRow: true,
|
|
354
|
+
});
|
|
355
|
+
}
|
|
336
356
|
for (const row of orderedProviderRows) {
|
|
337
357
|
rows.push({ ...row, navigatorId: rowKey(provider.id, row.id), providerLabel: provider.label });
|
|
338
358
|
}
|
|
@@ -378,7 +398,7 @@ function focusMainList(): void {
|
|
|
378
398
|
const rows = listRows();
|
|
379
399
|
if (rows.length === 0) return;
|
|
380
400
|
const s = state();
|
|
381
|
-
|
|
401
|
+
s.mainListSelectedId = rows.find((row) => row.parentRow && row.id === "main")?.navigatorId ?? rows[0]!.navigatorId;
|
|
382
402
|
s.mainListFocused = true;
|
|
383
403
|
refreshMainListWidget();
|
|
384
404
|
}
|
|
@@ -412,16 +432,6 @@ function buildMainListLines(
|
|
|
412
432
|
const group = grouped.get(label)!;
|
|
413
433
|
if (i > 0) lines.push("");
|
|
414
434
|
lines.push(providerGroupLabel(label, fg));
|
|
415
|
-
const provider = state().providers.get(group[0]!.providerId);
|
|
416
|
-
let parentRow: BackgroundWorkRow | null = null;
|
|
417
|
-
try { parentRow = provider?.parentRow?.(Date.now()) ?? null; } catch { parentRow = null; }
|
|
418
|
-
if (parentRow) {
|
|
419
|
-
lines.push(formatMainListRow({
|
|
420
|
-
...parentRow,
|
|
421
|
-
navigatorId: rowKey(parentRow.providerId, parentRow.id),
|
|
422
|
-
providerLabel: label,
|
|
423
|
-
}, false, fg, width));
|
|
424
|
-
}
|
|
425
435
|
for (const row of group) {
|
|
426
436
|
const selected = options.focused && row.navigatorId === options.selectedId;
|
|
427
437
|
lines.push(formatMainListRow(row, selected === true, fg, width));
|
|
@@ -433,13 +443,13 @@ function buildMainListLines(
|
|
|
433
443
|
}
|
|
434
444
|
|
|
435
445
|
function shortcutsLine(focused: boolean, fg: (color: string, value: string) => string): string {
|
|
436
|
-
const keys = focused ? "↑↓
|
|
446
|
+
const keys = focused ? "↑↓ switch · Enter detail · x stop · Esc unfocus" : "← to navigate";
|
|
437
447
|
return dim(keys, fg);
|
|
438
448
|
}
|
|
439
449
|
|
|
440
450
|
function providerGroupLabel(label: string, fg: (color: string, value: string) => string): string {
|
|
441
451
|
const normalized = singleLine(label).toLowerCase();
|
|
442
|
-
return
|
|
452
|
+
return fg("warning", normalized);
|
|
443
453
|
}
|
|
444
454
|
|
|
445
455
|
function formatMainListRow(row: InternalRow, selected: boolean, fg: (color: string, value: string) => string, width: number): string {
|
|
@@ -527,13 +537,14 @@ function detailFor(navigatorId: string, now = Date.now(), options?: { logTailLin
|
|
|
527
537
|
}
|
|
528
538
|
|
|
529
539
|
function closeFor(row: InternalRow): BackgroundWorkCloseOutcome {
|
|
540
|
+
if (row.parentRow) return { action: "not-closable", providerId: row.providerId, id: row.id };
|
|
530
541
|
const provider = state().providers.get(row.providerId);
|
|
531
542
|
if (!provider) return { action: "missing", providerId: row.providerId, id: row.id };
|
|
532
543
|
try { return provider.close(row.id); } catch { return { action: "missing", providerId: row.providerId, id: row.id }; }
|
|
533
544
|
}
|
|
534
545
|
|
|
535
546
|
function closeHintFor(row: InternalRow | undefined): string | null {
|
|
536
|
-
if (!row) return null;
|
|
547
|
+
if (!row || row.parentRow) return null;
|
|
537
548
|
const provider = state().providers.get(row.providerId);
|
|
538
549
|
if (!provider) return null;
|
|
539
550
|
try { return `${provider.armCloseLabel(row)} ${row.name || row.id}`; } catch { return null; }
|
|
@@ -558,6 +569,7 @@ function installNavigatorEditor(ui: any, deps: HostDeps): unknown {
|
|
|
558
569
|
}
|
|
559
570
|
|
|
560
571
|
function wrapEditor(inner: any, deps: HostDeps): unknown {
|
|
572
|
+
if (inner && typeof inner.render === "function") state().editorComponent = inner as Component;
|
|
561
573
|
return new Proxy(inner, {
|
|
562
574
|
get(target, prop) {
|
|
563
575
|
if (prop === "handleInput") {
|
|
@@ -588,16 +600,24 @@ function handleMainListInput(data: string, deps: HostDeps): boolean {
|
|
|
588
600
|
}
|
|
589
601
|
if (deps.matchKey(data, "up")) {
|
|
590
602
|
clearMainListCloseArm();
|
|
591
|
-
if (moveMainListSelection(-1))
|
|
603
|
+
if (moveMainListSelection(-1)) {
|
|
604
|
+
refreshMainListWidget();
|
|
605
|
+
if (!selectedMainListRow()?.parentRow) openNavigator();
|
|
606
|
+
}
|
|
592
607
|
return true;
|
|
593
608
|
}
|
|
594
609
|
if (deps.matchKey(data, "down")) {
|
|
595
610
|
clearMainListCloseArm();
|
|
596
|
-
if (moveMainListSelection(1))
|
|
611
|
+
if (moveMainListSelection(1)) {
|
|
612
|
+
refreshMainListWidget();
|
|
613
|
+
if (!selectedMainListRow()?.parentRow) openNavigator();
|
|
614
|
+
}
|
|
597
615
|
return true;
|
|
598
616
|
}
|
|
599
617
|
if (deps.matchKey(data, "enter")) {
|
|
600
|
-
|
|
618
|
+
const selected = selectedMainListRow();
|
|
619
|
+
if (selected?.parentRow) unfocusMainList();
|
|
620
|
+
else openNavigator();
|
|
601
621
|
return true;
|
|
602
622
|
}
|
|
603
623
|
if (data === "x" || data === "X" || deps.matchKey(data, "x") || deps.matchKey(data, "X")) {
|
|
@@ -613,7 +633,7 @@ function handleMainListInput(data: string, deps: HostDeps): boolean {
|
|
|
613
633
|
|
|
614
634
|
function handleMainListCloseKey(): void {
|
|
615
635
|
const row = selectedMainListRow();
|
|
616
|
-
if (!row) return;
|
|
636
|
+
if (!row || row.parentRow) return;
|
|
617
637
|
const s = state();
|
|
618
638
|
const now = Date.now();
|
|
619
639
|
const arm = s.mainListCloseArm;
|
|
@@ -689,6 +709,8 @@ function createOverlayComponent(
|
|
|
689
709
|
let closeArm: { id: string; armedAt: number } | undefined;
|
|
690
710
|
let closeArmTimer: ReturnType<typeof setTimeout> | undefined;
|
|
691
711
|
let closed = false;
|
|
712
|
+
let transcriptDetail: BackgroundWorkDetail | null = null;
|
|
713
|
+
let transcriptComponent: Component | null = null;
|
|
692
714
|
|
|
693
715
|
const fg = (color: string, value: string) => theme?.fg ? theme.fg(color, value) : value;
|
|
694
716
|
|
|
@@ -704,12 +726,24 @@ function createOverlayComponent(
|
|
|
704
726
|
});
|
|
705
727
|
|
|
706
728
|
function refreshRows(): void {
|
|
707
|
-
const selectedId = overlayState.rows[overlayState.selected]?.navigatorId;
|
|
729
|
+
const selectedId = state().mainListSelectedId ?? overlayState.rows[overlayState.selected]?.navigatorId;
|
|
708
730
|
overlayState.rows = listRows();
|
|
709
731
|
const nextIdx = selectedId ? overlayState.rows.findIndex((row) => row.navigatorId === selectedId) : -1;
|
|
710
732
|
overlayState.selected = nextIdx >= 0 ? nextIdx : Math.min(overlayState.selected, Math.max(0, overlayState.rows.length - 1));
|
|
711
733
|
}
|
|
712
734
|
|
|
735
|
+
function selectOverlayRow(next: number): void {
|
|
736
|
+
overlayState.selected = Math.min(Math.max(0, overlayState.rows.length - 1), Math.max(0, next));
|
|
737
|
+
state().mainListSelectedId = overlayState.rows[overlayState.selected]?.navigatorId;
|
|
738
|
+
clearCloseArm();
|
|
739
|
+
refreshMainListWidget();
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
function activateSelectedRow(): void {
|
|
743
|
+
if (selectedRow()?.parentRow) close();
|
|
744
|
+
else openDetail();
|
|
745
|
+
}
|
|
746
|
+
|
|
713
747
|
function clearCloseArm(): void {
|
|
714
748
|
if (closeArmTimer) clearTimeout(closeArmTimer);
|
|
715
749
|
closeArmTimer = undefined;
|
|
@@ -731,13 +765,16 @@ function createOverlayComponent(
|
|
|
731
765
|
}
|
|
732
766
|
|
|
733
767
|
function selectedRow(): InternalRow | undefined {
|
|
734
|
-
if (mode === "detail" && detailId) return overlayState.rows.find((row) => row.navigatorId === detailId);
|
|
735
768
|
return overlayState.rows[overlayState.selected];
|
|
736
769
|
}
|
|
737
770
|
|
|
738
771
|
function openDetail(): void {
|
|
739
772
|
const row = selectedRow();
|
|
740
773
|
if (!row) return;
|
|
774
|
+
if (row.parentRow) {
|
|
775
|
+
close();
|
|
776
|
+
return;
|
|
777
|
+
}
|
|
741
778
|
clearCloseArm();
|
|
742
779
|
detailId = row.navigatorId;
|
|
743
780
|
expandedSections.clear();
|
|
@@ -808,10 +845,43 @@ function createOverlayComponent(
|
|
|
808
845
|
|
|
809
846
|
return {
|
|
810
847
|
render(width: number) {
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
848
|
+
refreshRows();
|
|
849
|
+
const railLines = mode === "detail"
|
|
850
|
+
? buildMainListLines(overlayState.rows, width, deps.truncate, fg, {
|
|
851
|
+
selectedId: selectedRow()?.navigatorId,
|
|
852
|
+
focused: true,
|
|
853
|
+
})
|
|
854
|
+
: [];
|
|
855
|
+
const editorLines = mode === "detail" ? renderEditorLines(width) : [];
|
|
856
|
+
const bottomLines = [...railLines, ...editorLines];
|
|
857
|
+
const overlayRows = state().detailOverlayRows;
|
|
858
|
+
const detailRows = overlayRows === undefined ? undefined : Math.max(1, overlayRows - bottomLines.length);
|
|
859
|
+
let contentLines: string[];
|
|
860
|
+
if (mode === "detail" && detail?.transcript && deps.createTranscriptComponent) {
|
|
861
|
+
if (transcriptDetail !== detail || !transcriptComponent) {
|
|
862
|
+
transcriptDetail = detail;
|
|
863
|
+
transcriptComponent = deps.createTranscriptComponent(detail, theme);
|
|
864
|
+
}
|
|
865
|
+
contentLines = buildTranscriptDetailLines(
|
|
866
|
+
detail,
|
|
867
|
+
transcriptComponent.render(width),
|
|
868
|
+
width,
|
|
869
|
+
deps.truncate,
|
|
870
|
+
fg,
|
|
871
|
+
{ minRows: detailRows },
|
|
872
|
+
);
|
|
873
|
+
} else {
|
|
874
|
+
transcriptDetail = null;
|
|
875
|
+
transcriptComponent = null;
|
|
876
|
+
contentLines = mode === "detail"
|
|
877
|
+
? buildDetailLines(detail, width, deps.truncate, fg, { expandedSections, logTailRows, minRows: detailRows })
|
|
878
|
+
: buildListLines(overlayState, width, deps.truncate, fg);
|
|
879
|
+
}
|
|
880
|
+
if (mode !== "detail") return contentLines;
|
|
881
|
+
if (detailRows === undefined) return [...contentLines, ...bottomLines];
|
|
882
|
+
const fittedContent = contentLines.slice(0, detailRows);
|
|
883
|
+
while (fittedContent.length < detailRows) fittedContent.push("");
|
|
884
|
+
return [...fittedContent, ...bottomLines];
|
|
815
885
|
},
|
|
816
886
|
handleInput(data: string) {
|
|
817
887
|
if (closed) return;
|
|
@@ -820,10 +890,25 @@ function createOverlayComponent(
|
|
|
820
890
|
return;
|
|
821
891
|
}
|
|
822
892
|
if (mode === "detail") {
|
|
823
|
-
if (deps.matchKey(data, "
|
|
893
|
+
if (deps.matchKey(data, "up")) {
|
|
894
|
+
selectOverlayRow(overlayState.selected - 1);
|
|
895
|
+
activateSelectedRow();
|
|
896
|
+
}
|
|
897
|
+
else if (deps.matchKey(data, "down")) {
|
|
898
|
+
selectOverlayRow(overlayState.selected + 1);
|
|
899
|
+
activateSelectedRow();
|
|
900
|
+
}
|
|
901
|
+
else if (deps.matchKey(data, "left")) {
|
|
902
|
+
const mainIdx = overlayState.rows.findIndex((row) => row.parentRow && row.id === "main");
|
|
903
|
+
if (mainIdx >= 0) selectOverlayRow(mainIdx);
|
|
904
|
+
close();
|
|
905
|
+
}
|
|
824
906
|
else if (deps.matchKey(data, "enter")) {
|
|
825
|
-
const
|
|
826
|
-
if (
|
|
907
|
+
const row = selectedRow();
|
|
908
|
+
if (row?.parentRow || row?.navigatorId !== detailId) openDetail();
|
|
909
|
+
else {
|
|
910
|
+
const sectionId = firstToggleableSectionId(detail);
|
|
911
|
+
if (!sectionId) return;
|
|
827
912
|
if (expandedSections.has(sectionId)) expandedSections.delete(sectionId);
|
|
828
913
|
else expandedSections.add(sectionId);
|
|
829
914
|
requestRender();
|
|
@@ -834,7 +919,10 @@ function createOverlayComponent(
|
|
|
834
919
|
if (detailId) detail = detailFor(detailId, Date.now(), { logTailLines: logTailRows }) ?? detail;
|
|
835
920
|
requestRender();
|
|
836
921
|
}
|
|
837
|
-
else if (deps.matchKey(data, "escape"))
|
|
922
|
+
else if (deps.matchKey(data, "escape")) {
|
|
923
|
+
unfocusMainList();
|
|
924
|
+
close();
|
|
925
|
+
}
|
|
838
926
|
return;
|
|
839
927
|
}
|
|
840
928
|
if (deps.matchKey(data, "up")) {
|
|
@@ -851,7 +939,7 @@ function createOverlayComponent(
|
|
|
851
939
|
close();
|
|
852
940
|
}
|
|
853
941
|
},
|
|
854
|
-
invalidate() {},
|
|
942
|
+
invalidate() { transcriptComponent?.invalidate(); },
|
|
855
943
|
dispose() {
|
|
856
944
|
clearCloseArm();
|
|
857
945
|
detailScheduler.dispose();
|
|
@@ -859,26 +947,61 @@ function createOverlayComponent(
|
|
|
859
947
|
};
|
|
860
948
|
}
|
|
861
949
|
|
|
950
|
+
function buildTranscriptDetailLines(
|
|
951
|
+
detail: BackgroundWorkDetail,
|
|
952
|
+
transcriptLines: string[],
|
|
953
|
+
width: number,
|
|
954
|
+
truncate: (s: string, width: number) => string,
|
|
955
|
+
fg: (color: string, value: string) => string,
|
|
956
|
+
options: { minRows?: number } = {},
|
|
957
|
+
): string[] {
|
|
958
|
+
const actions = [...(detail.footerActions ?? ["x close"]), "Esc close"].join(" · ");
|
|
959
|
+
const lines: string[] = [
|
|
960
|
+
fg("accent", rule(detail.title, width)),
|
|
961
|
+
dim(` ← main · ${actions}`, fg),
|
|
962
|
+
"",
|
|
963
|
+
` status ${fg(toneColor(detail.statusTone, detail.status), detail.status)}`,
|
|
964
|
+
];
|
|
965
|
+
if (detail.subtitle) lines.push(` summary ${detail.subtitle}`);
|
|
966
|
+
for (const item of detail.metadata) lines.push(` ${item.label.padEnd(8, " ").slice(0, 8)} ${item.value}`);
|
|
967
|
+
lines.push("", dim(section("transcript", width), fg));
|
|
968
|
+
if (detail.transcriptDiagnostic) lines.push(` ${dim(detail.transcriptDiagnostic, fg)}`);
|
|
969
|
+
lines.push(...(transcriptLines.length ? transcriptLines : [" (no transcript yet)"]));
|
|
970
|
+
lines.push("");
|
|
971
|
+
const footerLines = [dim(` ← main · ${actions}`, fg), dim(rule("", width), fg)];
|
|
972
|
+
padBeforeFooter(lines, footerLines.length, options.minRows);
|
|
973
|
+
lines.push(...footerLines);
|
|
974
|
+
return lines.map((line) => safeTruncate(line, width, truncate));
|
|
975
|
+
}
|
|
976
|
+
|
|
862
977
|
function detailOverlayOptions() {
|
|
863
|
-
const
|
|
864
|
-
const marginBottom = DETAIL_OVERLAY_FOOTER_MARGIN_ROWS + navigatorRows;
|
|
978
|
+
const marginBottom = DETAIL_OVERLAY_FOOTER_ROWS;
|
|
865
979
|
return {
|
|
866
980
|
anchor: "top-left" as const,
|
|
867
981
|
width: "100%" as const,
|
|
868
982
|
maxHeight: "100%" as const,
|
|
869
983
|
margin: {
|
|
870
|
-
top:
|
|
984
|
+
top: 0,
|
|
871
985
|
right: 0,
|
|
872
986
|
bottom: marginBottom,
|
|
873
987
|
left: 0,
|
|
874
988
|
},
|
|
875
989
|
visible: (_termWidth: number, termHeight: number) => {
|
|
876
|
-
state().detailOverlayRows = Math.max(1, termHeight -
|
|
990
|
+
state().detailOverlayRows = Math.max(1, termHeight - marginBottom);
|
|
877
991
|
return true;
|
|
878
992
|
},
|
|
879
993
|
};
|
|
880
994
|
}
|
|
881
995
|
|
|
996
|
+
function renderEditorLines(width: number): string[] {
|
|
997
|
+
try {
|
|
998
|
+
const lines = state().editorComponent?.render(width);
|
|
999
|
+
if (lines?.length) return lines;
|
|
1000
|
+
} catch { /* use an empty editor-shaped fallback */ }
|
|
1001
|
+
const border = "─".repeat(Math.max(1, width));
|
|
1002
|
+
return [border, "", border];
|
|
1003
|
+
}
|
|
1004
|
+
|
|
882
1005
|
function fallbackDetail(row: InternalRow): BackgroundWorkDetail {
|
|
883
1006
|
return {
|
|
884
1007
|
providerId: row.providerId,
|