@diousk/pi-subagents-fast 0.20.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 +808 -0
- package/CONTRIBUTING.md +72 -0
- package/LICENSE +21 -0
- package/README.md +1034 -0
- package/SECURITY.md +95 -0
- package/dist/abortable.d.ts +12 -0
- package/dist/abortable.js +42 -0
- package/dist/agent-color.d.ts +35 -0
- package/dist/agent-color.js +123 -0
- package/dist/agent-file-toggle.d.ts +125 -0
- package/dist/agent-file-toggle.js +260 -0
- package/dist/agent-manager.d.ts +472 -0
- package/dist/agent-manager.js +1338 -0
- package/dist/agent-runner.d.ts +312 -0
- package/dist/agent-runner.js +1034 -0
- package/dist/agent-types.d.ts +119 -0
- package/dist/agent-types.js +286 -0
- package/dist/child-context.d.ts +2 -0
- package/dist/child-context.js +12 -0
- package/dist/context.d.ts +12 -0
- package/dist/context.js +56 -0
- package/dist/cross-extension-rpc.d.ts +66 -0
- package/dist/cross-extension-rpc.js +138 -0
- package/dist/custom-agents.d.ts +54 -0
- package/dist/custom-agents.js +316 -0
- package/dist/default-agents.d.ts +7 -0
- package/dist/default-agents.js +122 -0
- package/dist/enabled-models.d.ts +49 -0
- package/dist/enabled-models.js +145 -0
- package/dist/env.d.ts +6 -0
- package/dist/env.js +28 -0
- package/dist/group-join.d.ts +32 -0
- package/dist/group-join.js +116 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.js +3682 -0
- package/dist/invocation-config.d.ts +107 -0
- package/dist/invocation-config.js +83 -0
- package/dist/memory.d.ts +53 -0
- package/dist/memory.js +165 -0
- package/dist/mention-clone.d.ts +87 -0
- package/dist/mention-clone.js +153 -0
- package/dist/mention.d.ts +81 -0
- package/dist/mention.js +131 -0
- package/dist/model-resolver.d.ts +36 -0
- package/dist/model-resolver.js +95 -0
- package/dist/model-scope.d.ts +49 -0
- package/dist/model-scope.js +48 -0
- package/dist/nested-tools.d.ts +55 -0
- package/dist/nested-tools.js +299 -0
- package/dist/output-file.d.ts +43 -0
- package/dist/output-file.js +142 -0
- package/dist/prompts.d.ts +55 -0
- package/dist/prompts.js +91 -0
- package/dist/schedule-store.d.ts +38 -0
- package/dist/schedule-store.js +155 -0
- package/dist/schedule.d.ts +109 -0
- package/dist/schedule.js +359 -0
- package/dist/settings.d.ts +360 -0
- package/dist/settings.js +251 -0
- package/dist/skill-loader.d.ts +24 -0
- package/dist/skill-loader.js +93 -0
- package/dist/status-note.d.ts +61 -0
- package/dist/status-note.js +85 -0
- package/dist/structured-output.d.ts +61 -0
- package/dist/structured-output.js +112 -0
- package/dist/types.d.ts +371 -0
- package/dist/types.js +5 -0
- package/dist/ui/agent-mention.d.ts +82 -0
- package/dist/ui/agent-mention.js +187 -0
- package/dist/ui/agent-widget.d.ts +219 -0
- package/dist/ui/agent-widget.js +592 -0
- package/dist/ui/conversation-viewer.d.ts +120 -0
- package/dist/ui/conversation-viewer.js +578 -0
- package/dist/ui/fleet-list.d.ts +195 -0
- package/dist/ui/fleet-list.js +471 -0
- package/dist/ui/schedule-menu.d.ts +16 -0
- package/dist/ui/schedule-menu.js +94 -0
- package/dist/ui/select-item.d.ts +27 -0
- package/dist/ui/select-item.js +34 -0
- package/dist/ui/viewer-keys.d.ts +20 -0
- package/dist/ui/viewer-keys.js +17 -0
- package/dist/ui/workflow-card.d.ts +175 -0
- package/dist/ui/workflow-card.js +332 -0
- package/dist/ui/workflow-dialog.d.ts +305 -0
- package/dist/ui/workflow-dialog.js +843 -0
- package/dist/ui/workflow-menu.d.ts +60 -0
- package/dist/ui/workflow-menu.js +147 -0
- package/dist/usage.d.ts +135 -0
- package/dist/usage.js +120 -0
- package/dist/workflow/collisions.d.ts +95 -0
- package/dist/workflow/collisions.js +88 -0
- package/dist/workflow/entry.d.ts +32 -0
- package/dist/workflow/entry.js +29 -0
- package/dist/workflow/host.d.ts +62 -0
- package/dist/workflow/host.js +362 -0
- package/dist/workflow/journal.d.ts +97 -0
- package/dist/workflow/journal.js +120 -0
- package/dist/workflow/json-schema.d.ts +51 -0
- package/dist/workflow/json-schema.js +111 -0
- package/dist/workflow/meta.d.ts +67 -0
- package/dist/workflow/meta.js +317 -0
- package/dist/workflow/progress.d.ts +224 -0
- package/dist/workflow/progress.js +361 -0
- package/dist/workflow/runtime.d.ts +334 -0
- package/dist/workflow/runtime.js +830 -0
- package/dist/workflow/saved.d.ts +90 -0
- package/dist/workflow/saved.js +203 -0
- package/dist/workflow/task.d.ts +136 -0
- package/dist/workflow/task.js +207 -0
- package/dist/workflow/tool-description.d.ts +38 -0
- package/dist/workflow/tool-description.js +199 -0
- package/dist/workflow/worker-source.d.ts +47 -0
- package/dist/workflow/worker-source.js +778 -0
- package/dist/worktree.d.ts +52 -0
- package/dist/worktree.js +164 -0
- package/dist/xml.d.ts +10 -0
- package/dist/xml.js +12 -0
- package/docs/rpc.md +183 -0
- package/docs/workflows.md +437 -0
- package/examples/agent-tool-description.md +42 -0
- package/examples/workflows/compose.js +51 -0
- package/examples/workflows/fan-out-audit.js +47 -0
- package/examples/workflows/gated-fix.js +60 -0
- package/examples/workflows/lib/count-child.js +27 -0
- package/examples/workflows/review-panel.js +63 -0
- package/examples/workflows/structured-findings.js +78 -0
- package/package.json +68 -0
- package/src/abortable.ts +43 -0
- package/src/agent-color.ts +161 -0
- package/src/agent-file-toggle.ts +270 -0
- package/src/agent-manager.ts +1581 -0
- package/src/agent-runner.ts +1286 -0
- package/src/agent-types.ts +346 -0
- package/src/child-context.ts +15 -0
- package/src/context.ts +58 -0
- package/src/cross-extension-rpc.ts +198 -0
- package/src/custom-agents.ts +333 -0
- package/src/default-agents.ts +126 -0
- package/src/enabled-models.ts +180 -0
- package/src/env.ts +33 -0
- package/src/group-join.ts +141 -0
- package/src/index.ts +3991 -0
- package/src/invocation-config.ts +155 -0
- package/src/memory.ts +179 -0
- package/src/mention-clone.ts +196 -0
- package/src/mention.ts +141 -0
- package/src/model-resolver.ts +118 -0
- package/src/model-scope.ts +70 -0
- package/src/nested-tools.ts +422 -0
- package/src/output-file.ts +155 -0
- package/src/prompts.ts +142 -0
- package/src/schedule-store.ts +153 -0
- package/src/schedule.ts +386 -0
- package/src/settings.ts +587 -0
- package/src/skill-loader.ts +102 -0
- package/src/status-note.ts +90 -0
- package/src/structured-output.ts +130 -0
- package/src/types.ts +384 -0
- package/src/ui/agent-mention.ts +216 -0
- package/src/ui/agent-widget.ts +664 -0
- package/src/ui/conversation-viewer.ts +589 -0
- package/src/ui/fleet-list.ts +543 -0
- package/src/ui/schedule-menu.ts +105 -0
- package/src/ui/select-item.ts +45 -0
- package/src/ui/viewer-keys.ts +39 -0
- package/src/ui/workflow-card.ts +470 -0
- package/src/ui/workflow-dialog.ts +1115 -0
- package/src/ui/workflow-menu.ts +193 -0
- package/src/usage.ts +167 -0
- package/src/workflow/collisions.ts +123 -0
- package/src/workflow/entry.ts +47 -0
- package/src/workflow/host.ts +403 -0
- package/src/workflow/journal.ts +164 -0
- package/src/workflow/json-schema.ts +128 -0
- package/src/workflow/meta.ts +325 -0
- package/src/workflow/progress.ts +550 -0
- package/src/workflow/runtime.ts +1219 -0
- package/src/workflow/saved.ts +217 -0
- package/src/workflow/task.ts +302 -0
- package/src/workflow/tool-description.ts +200 -0
- package/src/workflow/worker-source.ts +781 -0
- package/src/worktree.ts +205 -0
- package/src/xml.ts +13 -0
|
@@ -0,0 +1,578 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* conversation-viewer.ts — Live conversation overlay for viewing agent sessions.
|
|
3
|
+
*
|
|
4
|
+
* Displays a scrollable, live-updating view of an agent's conversation.
|
|
5
|
+
* Subscribes to session events for real-time streaming updates.
|
|
6
|
+
*/
|
|
7
|
+
import { getMarkdownTheme } from "@earendil-works/pi-coding-agent";
|
|
8
|
+
import { Input, Markdown, matchesKey, truncateToWidth, visibleWidth, wrapTextWithAnsi } from "@earendil-works/pi-tui";
|
|
9
|
+
import { renderAgentName } from "../agent-color.js";
|
|
10
|
+
import { extractText } from "../context.js";
|
|
11
|
+
import { getLifetimeCost, getLifetimeTotal, getSessionContextPercent } from "../usage.js";
|
|
12
|
+
import { buildInvocationTags, describeActivity, fgPreservingNestedStyles, formatCost, formatDuration, formatSessionTokens, getPromptModeLabel } from "./agent-widget.js";
|
|
13
|
+
import { createViewerKeys } from "./viewer-keys.js";
|
|
14
|
+
/** Base lines consumed by chrome: top border + header + header sep + footer sep + footer + bottom border. */
|
|
15
|
+
const CHROME_LINES_BASE = 6;
|
|
16
|
+
const MIN_VIEWPORT = 3;
|
|
17
|
+
/** Height ceiling shared by the overlay's `maxHeight` and the viewer's internal viewport cap. */
|
|
18
|
+
export const VIEWPORT_HEIGHT_PCT = 70;
|
|
19
|
+
/**
|
|
20
|
+
* Cap on a single tool result or bash output before the viewer elides the rest.
|
|
21
|
+
*
|
|
22
|
+
* The cap is not cosmetic — it bounds render cost. `buildContentLines()` runs on
|
|
23
|
+
* every render *and* on every scroll key (`handleInput` calls it to compute
|
|
24
|
+
* `maxScroll`), so an uncapped 200 KB result costs ~6 ms per keystroke to parse
|
|
25
|
+
* as Markdown, against ~0.5 ms once capped and effectively nothing on a cache
|
|
26
|
+
* hit (best of 5, width 76). 16 KB is roughly a screenful at every terminal size
|
|
27
|
+
* and still ~30x the 500 characters this replaces, which was small enough to cut
|
|
28
|
+
* most real results mid-sentence.
|
|
29
|
+
*/
|
|
30
|
+
export const RESULT_MAX_CHARS = 16_000;
|
|
31
|
+
/** Cycle order for the viewer's `m` key. */
|
|
32
|
+
const MARKDOWN_MODES = ["off", "assistant", "all"];
|
|
33
|
+
/** Footer labels — short, because the idle footer is already full at 80 columns. */
|
|
34
|
+
const MARKDOWN_MODE_LABELS = {
|
|
35
|
+
off: "raw",
|
|
36
|
+
assistant: "md",
|
|
37
|
+
all: "md+",
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Both options keep the renderer from *rewriting* source that only looks like
|
|
41
|
+
* Markdown: without them `3) a / 7) b / 9) c` comes back renumbered `3. 4. 5.`
|
|
42
|
+
* and backslash escapes are normalized away. Neither is a safe edit to make to
|
|
43
|
+
* a tool's output, and both are cheap to switch off.
|
|
44
|
+
*/
|
|
45
|
+
const MARKDOWN_OPTIONS = {
|
|
46
|
+
preserveOrderedListMarkers: true,
|
|
47
|
+
preserveBackslashEscapes: true,
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Pi's own Markdown theme when this process has one, else a theme built from the
|
|
51
|
+
* viewer's `Theme`.
|
|
52
|
+
*
|
|
53
|
+
* Preferring pi's is what buys syntax-highlighted code fences (it carries a
|
|
54
|
+
* `highlightCode`), and it keeps this surface consistent with the notification
|
|
55
|
+
* renderer, which uses the same source. It has to be *probed* rather than
|
|
56
|
+
* try/caught around the call: `getMarkdownTheme()` returns arrow functions that
|
|
57
|
+
* read pi's global theme lazily, so an uninitialized theme throws inside
|
|
58
|
+
* `render()` — long after this returns — and takes the overlay with it. That is
|
|
59
|
+
* the case in tests and any embedded session that never called `initTheme()`.
|
|
60
|
+
*/
|
|
61
|
+
function resolveMarkdownTheme(th) {
|
|
62
|
+
try {
|
|
63
|
+
const piTheme = getMarkdownTheme();
|
|
64
|
+
piTheme.heading("probe");
|
|
65
|
+
return piTheme;
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
return fallbackMarkdownTheme(th);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* `Theme` carries only `fg` and `bold`, so the three remaining styles are
|
|
73
|
+
* written as raw SGR. Rendering them as plain text instead would silently drop
|
|
74
|
+
* `*emphasis*`'s markers with nothing in their place, turning a formatting
|
|
75
|
+
* change into a content change.
|
|
76
|
+
*/
|
|
77
|
+
function fallbackMarkdownTheme(th) {
|
|
78
|
+
const sgr = (on, off) => (text) => `\x1b[${on}m${text}\x1b[${off}m`;
|
|
79
|
+
return {
|
|
80
|
+
heading: text => th.bold(th.fg("accent", text)),
|
|
81
|
+
link: text => th.fg("accent", text),
|
|
82
|
+
linkUrl: text => th.fg("muted", text),
|
|
83
|
+
code: text => th.fg("muted", text),
|
|
84
|
+
codeBlock: text => th.fg("muted", text),
|
|
85
|
+
codeBlockBorder: text => th.fg("dim", text),
|
|
86
|
+
quote: text => th.fg("muted", text),
|
|
87
|
+
quoteBorder: text => th.fg("dim", text),
|
|
88
|
+
hr: text => th.fg("dim", text),
|
|
89
|
+
listBullet: text => th.fg("accent", text),
|
|
90
|
+
bold: text => th.bold(text),
|
|
91
|
+
italic: sgr(3, 23),
|
|
92
|
+
underline: sgr(4, 24),
|
|
93
|
+
strikethrough: sgr(9, 29),
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Cap `text` at `RESULT_MAX_CHARS`, reporting the elision separately rather than
|
|
98
|
+
* appending it.
|
|
99
|
+
*
|
|
100
|
+
* Separately because the notice is the viewer's chrome, not the tool's output.
|
|
101
|
+
* Appended into the string it becomes content: a cut landing inside a fenced
|
|
102
|
+
* code block — likely, on exactly the large `ctx_execute` results this is for —
|
|
103
|
+
* renders the notice as a line of source inside the fence.
|
|
104
|
+
*/
|
|
105
|
+
function capResult(text) {
|
|
106
|
+
if (text.length <= RESULT_MAX_CHARS)
|
|
107
|
+
return { text, elided: 0 };
|
|
108
|
+
return {
|
|
109
|
+
text: text.slice(0, RESULT_MAX_CHARS),
|
|
110
|
+
elided: text.length - RESULT_MAX_CHARS,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* `999` · `1.5k` · `8.4M` — a magnitude cue, not an exact count, past 1000.
|
|
115
|
+
*
|
|
116
|
+
* The bracket is chosen against the *rounded* value, so 999,999 reads `1M`
|
|
117
|
+
* rather than the `1000.0k` a naive `< 1e6` test produces.
|
|
118
|
+
*/
|
|
119
|
+
function humanCount(n) {
|
|
120
|
+
if (n < 1_000)
|
|
121
|
+
return `${n}`;
|
|
122
|
+
const thousands = n < 999_950;
|
|
123
|
+
const value = thousands ? n / 1_000 : n / 1_000_000;
|
|
124
|
+
return `${value.toFixed(1).replace(/\.0$/, "")}${thousands ? "k" : "M"}`;
|
|
125
|
+
}
|
|
126
|
+
function truncationNote(elided) {
|
|
127
|
+
return `... (truncated, ${humanCount(elided)} more character${elided === 1 ? "" : "s"})`;
|
|
128
|
+
}
|
|
129
|
+
export class ConversationViewer {
|
|
130
|
+
tui;
|
|
131
|
+
session;
|
|
132
|
+
record;
|
|
133
|
+
activity;
|
|
134
|
+
theme;
|
|
135
|
+
done;
|
|
136
|
+
onStop;
|
|
137
|
+
onSteer;
|
|
138
|
+
showCost;
|
|
139
|
+
viewerMarkdown;
|
|
140
|
+
onMarkdownMode;
|
|
141
|
+
scrollOffset = 0;
|
|
142
|
+
autoScroll = true;
|
|
143
|
+
unsubscribe;
|
|
144
|
+
lastInnerW = 0;
|
|
145
|
+
closed = false;
|
|
146
|
+
/** Two-press confirm guard for the stop key, so a stray key can't kill the agent. */
|
|
147
|
+
stopArmed = false;
|
|
148
|
+
keys;
|
|
149
|
+
/** Steering composer — present while the user is typing a message to the agent. */
|
|
150
|
+
composer;
|
|
151
|
+
/** Resolved once: pi's Markdown theme is fixed for the life of the process. */
|
|
152
|
+
markdownTheme;
|
|
153
|
+
/** Set by the `m` key. Wins over the setting so `m` works without a persist hook. */
|
|
154
|
+
markdownModeOverride;
|
|
155
|
+
/**
|
|
156
|
+
* One `Markdown` per message, so its own text/width cache does the work. A
|
|
157
|
+
* fresh instance per render would re-parse the whole transcript on every
|
|
158
|
+
* keystroke — the component caches, but only across calls to the same object.
|
|
159
|
+
* Weak so a compacted-away message doesn't pin its render.
|
|
160
|
+
*/
|
|
161
|
+
markdownCache = new WeakMap();
|
|
162
|
+
constructor(tui, session, record, activity, theme, done,
|
|
163
|
+
/** Abort the agent shown here. Omitted → no stop affordance (e.g. read-only history). */
|
|
164
|
+
onStop,
|
|
165
|
+
/** User keybindings from `ctx.ui.custom()`. Omitted → hardcoded defaults. */
|
|
166
|
+
keybindings,
|
|
167
|
+
/** Send a steering message to the agent. Omitted → no compose affordance. */
|
|
168
|
+
onSteer,
|
|
169
|
+
/**
|
|
170
|
+
* Whether the header shows an estimated cost after the token count. Read
|
|
171
|
+
* once, at construction: the overlay is opened from a menu, so the setting
|
|
172
|
+
* cannot change while it is on screen.
|
|
173
|
+
*/
|
|
174
|
+
showCost = false,
|
|
175
|
+
/**
|
|
176
|
+
* The current `viewerMarkdown` setting. Read live rather than captured,
|
|
177
|
+
* unlike `showCost`: `m` changes it while the overlay is on screen.
|
|
178
|
+
* Omitted → `assistant`.
|
|
179
|
+
*/
|
|
180
|
+
viewerMarkdown,
|
|
181
|
+
/**
|
|
182
|
+
* Persist a mode chosen with `m`, so the key and `/agents → Settings` mean
|
|
183
|
+
* the same thing. Omitted → `m` still cycles, viewer-locally.
|
|
184
|
+
*/
|
|
185
|
+
onMarkdownMode) {
|
|
186
|
+
this.tui = tui;
|
|
187
|
+
this.session = session;
|
|
188
|
+
this.record = record;
|
|
189
|
+
this.activity = activity;
|
|
190
|
+
this.theme = theme;
|
|
191
|
+
this.done = done;
|
|
192
|
+
this.onStop = onStop;
|
|
193
|
+
this.onSteer = onSteer;
|
|
194
|
+
this.showCost = showCost;
|
|
195
|
+
this.viewerMarkdown = viewerMarkdown;
|
|
196
|
+
this.onMarkdownMode = onMarkdownMode;
|
|
197
|
+
this.markdownTheme = resolveMarkdownTheme(theme);
|
|
198
|
+
this.keys = createViewerKeys(keybindings);
|
|
199
|
+
this.unsubscribe = session.subscribe(() => {
|
|
200
|
+
if (this.closed)
|
|
201
|
+
return;
|
|
202
|
+
this.tui.requestRender();
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
handleInput(data) {
|
|
206
|
+
// While composing a steer message, the input owns all keys (Enter sends,
|
|
207
|
+
// Esc cancels — both wired in openComposer()). Editing keys flow through.
|
|
208
|
+
if (this.composer) {
|
|
209
|
+
this.composer.handleInput(data);
|
|
210
|
+
this.tui.requestRender();
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
if (matchesKey(data, "escape") || matchesKey(data, "ctrl+c") || matchesKey(data, "q")) {
|
|
214
|
+
this.closed = true;
|
|
215
|
+
this.done(undefined);
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
// Enter opens the steering composer (only while the agent can still be
|
|
219
|
+
// steered) — then type + Enter sends, Esc or an empty submit returns. When
|
|
220
|
+
// not steerable, fall through so the key still disarms a pending stop.
|
|
221
|
+
if (matchesKey(data, "enter") && this.canSteer()) {
|
|
222
|
+
this.stopArmed = false;
|
|
223
|
+
this.openComposer();
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
// Stop/abort the agent (only while it can still be stopped). Two-press:
|
|
227
|
+
// first "x" arms, second confirms — any other key disarms.
|
|
228
|
+
if (matchesKey(data, "x")) {
|
|
229
|
+
if (this.isStoppable()) {
|
|
230
|
+
if (this.stopArmed) {
|
|
231
|
+
this.stopArmed = false;
|
|
232
|
+
this.onStop?.();
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
this.stopArmed = true;
|
|
236
|
+
}
|
|
237
|
+
this.tui.requestRender();
|
|
238
|
+
}
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
// Cycle raw → assistant-only → everything. The escape hatch that makes
|
|
242
|
+
// Markdown rendering safe to default on: a result the renderer reshapes
|
|
243
|
+
// (a diff, an indented log, a `#`-commented script) is one key from verbatim.
|
|
244
|
+
if (matchesKey(data, "m")) {
|
|
245
|
+
this.stopArmed = false;
|
|
246
|
+
const next = MARKDOWN_MODES[(MARKDOWN_MODES.indexOf(this.markdownMode()) + 1) % MARKDOWN_MODES.length];
|
|
247
|
+
this.markdownModeOverride = next;
|
|
248
|
+
this.onMarkdownMode?.(next);
|
|
249
|
+
this.tui.requestRender();
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
if (this.stopArmed)
|
|
253
|
+
this.stopArmed = false;
|
|
254
|
+
const totalLines = this.buildContentLines(this.lastInnerW).length;
|
|
255
|
+
const viewportHeight = this.viewportHeight();
|
|
256
|
+
const maxScroll = Math.max(0, totalLines - viewportHeight);
|
|
257
|
+
if (this.keys.scrollUp(data)) {
|
|
258
|
+
this.scrollOffset = Math.max(0, this.scrollOffset - 1);
|
|
259
|
+
this.autoScroll = this.scrollOffset >= maxScroll;
|
|
260
|
+
}
|
|
261
|
+
else if (this.keys.scrollDown(data)) {
|
|
262
|
+
this.scrollOffset = Math.min(maxScroll, this.scrollOffset + 1);
|
|
263
|
+
this.autoScroll = this.scrollOffset >= maxScroll;
|
|
264
|
+
}
|
|
265
|
+
else if (this.keys.pageUp(data)) {
|
|
266
|
+
this.scrollOffset = Math.max(0, this.scrollOffset - viewportHeight);
|
|
267
|
+
this.autoScroll = false;
|
|
268
|
+
}
|
|
269
|
+
else if (this.keys.pageDown(data)) {
|
|
270
|
+
this.scrollOffset = Math.min(maxScroll, this.scrollOffset + viewportHeight);
|
|
271
|
+
this.autoScroll = this.scrollOffset >= maxScroll;
|
|
272
|
+
}
|
|
273
|
+
else if (matchesKey(data, "home")) {
|
|
274
|
+
this.scrollOffset = 0;
|
|
275
|
+
this.autoScroll = false;
|
|
276
|
+
}
|
|
277
|
+
else if (matchesKey(data, "end")) {
|
|
278
|
+
this.scrollOffset = maxScroll;
|
|
279
|
+
this.autoScroll = true;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
render(width) {
|
|
283
|
+
if (width < 6)
|
|
284
|
+
return []; // too narrow for any meaningful rendering
|
|
285
|
+
const th = this.theme;
|
|
286
|
+
const innerW = width - 4; // border + padding
|
|
287
|
+
this.lastInnerW = innerW;
|
|
288
|
+
const lines = [];
|
|
289
|
+
const pad = (s, len) => {
|
|
290
|
+
const vis = visibleWidth(s);
|
|
291
|
+
return s + " ".repeat(Math.max(0, len - vis));
|
|
292
|
+
};
|
|
293
|
+
const row = (content) => th.fg("border", "│") + " " + truncateToWidth(pad(content, innerW), innerW, "...", true) + " " + th.fg("border", "│");
|
|
294
|
+
const hrTop = th.fg("border", `╭${"─".repeat(width - 2)}╮`);
|
|
295
|
+
const hrBot = th.fg("border", `╰${"─".repeat(width - 2)}╯`);
|
|
296
|
+
const hrMid = row(th.fg("dim", "─".repeat(innerW)));
|
|
297
|
+
// Header
|
|
298
|
+
lines.push(hrTop);
|
|
299
|
+
const modeLabel = getPromptModeLabel(this.record.type);
|
|
300
|
+
const modeTag = modeLabel ? ` ${th.fg("dim", `(${modeLabel})`)}` : "";
|
|
301
|
+
const statusIcon = this.record.status === "running"
|
|
302
|
+
? th.fg("accent", "●")
|
|
303
|
+
: this.record.status === "completed"
|
|
304
|
+
? th.fg("success", "✓")
|
|
305
|
+
: this.record.status === "error"
|
|
306
|
+
? th.fg("error", "✗")
|
|
307
|
+
: th.fg("dim", "○");
|
|
308
|
+
const duration = formatDuration(this.record.startedAt, this.record.completedAt);
|
|
309
|
+
const headerParts = [duration];
|
|
310
|
+
const toolUses = this.activity?.toolUses ?? this.record.toolUses;
|
|
311
|
+
if (toolUses > 0)
|
|
312
|
+
headerParts.unshift(`${toolUses} tool${toolUses === 1 ? "" : "s"}`);
|
|
313
|
+
// Spend from the record, context from the live session: the record is the
|
|
314
|
+
// only total that survives the agent finishing and the only one carrying a
|
|
315
|
+
// nested child's spend.
|
|
316
|
+
const tokens = getLifetimeTotal(this.record.lifetimeUsage);
|
|
317
|
+
if (tokens > 0) {
|
|
318
|
+
const percent = getSessionContextPercent(this.activity?.session);
|
|
319
|
+
headerParts.push(formatSessionTokens(tokens, percent, th, this.record.compactionCount));
|
|
320
|
+
}
|
|
321
|
+
const cost = this.showCost ? formatCost(getLifetimeCost(this.record.lifetimeUsage)) : "";
|
|
322
|
+
if (cost)
|
|
323
|
+
headerParts.push(cost);
|
|
324
|
+
lines.push(row(`${statusIcon} ${renderAgentName(this.record.type, th, { bold: true })}${modeTag} ${th.fg("muted", this.record.description)} ${th.fg("dim", "·")} ${fgPreservingNestedStyles(th, "dim", headerParts.join(" · "))}`));
|
|
325
|
+
const invocationLine = this.invocationLine();
|
|
326
|
+
if (invocationLine)
|
|
327
|
+
lines.push(row(invocationLine));
|
|
328
|
+
lines.push(hrMid);
|
|
329
|
+
// Content area — rebuild every render (live data, no cache needed)
|
|
330
|
+
const contentLines = this.buildContentLines(innerW);
|
|
331
|
+
const viewportHeight = this.viewportHeight();
|
|
332
|
+
const maxScroll = Math.max(0, contentLines.length - viewportHeight);
|
|
333
|
+
if (this.autoScroll) {
|
|
334
|
+
this.scrollOffset = maxScroll;
|
|
335
|
+
}
|
|
336
|
+
const visibleStart = Math.min(this.scrollOffset, maxScroll);
|
|
337
|
+
const visible = contentLines.slice(visibleStart, visibleStart + viewportHeight);
|
|
338
|
+
for (let i = 0; i < viewportHeight; i++) {
|
|
339
|
+
lines.push(row(visible[i] ?? ""));
|
|
340
|
+
}
|
|
341
|
+
// Footer
|
|
342
|
+
lines.push(hrMid);
|
|
343
|
+
if (this.composer) {
|
|
344
|
+
// Composer row: the Input renders its own `> ` prompt and cursor.
|
|
345
|
+
lines.push(row(this.composer.render(innerW)[0] ?? ""));
|
|
346
|
+
const composeHint = th.fg("dim", "Enter send · Esc cancel");
|
|
347
|
+
const composeLeft = th.fg("accent", "✎ steer");
|
|
348
|
+
const composeGap = Math.max(1, innerW - visibleWidth(composeLeft) - visibleWidth(composeHint));
|
|
349
|
+
lines.push(row(composeLeft + " ".repeat(composeGap) + composeHint));
|
|
350
|
+
}
|
|
351
|
+
else {
|
|
352
|
+
// Actions on the left, navigation on the right. The scroll hint keeps its
|
|
353
|
+
// full key list so the less-obvious bindings stay discoverable; it leads
|
|
354
|
+
// the right group so "Esc close" is the only part that truncates first.
|
|
355
|
+
const sep = th.fg("dim", " · ");
|
|
356
|
+
const actions = [];
|
|
357
|
+
if (this.canSteer())
|
|
358
|
+
actions.push(th.fg("dim", "Enter steer"));
|
|
359
|
+
if (this.isStoppable()) {
|
|
360
|
+
actions.push(this.stopArmed ? th.fg("error", "x again to STOP") : th.fg("dim", "x stop"));
|
|
361
|
+
}
|
|
362
|
+
// Abbreviated (`raw`/`md`/`md+`) because the idle footer is already full
|
|
363
|
+
// at 80 columns with steer + stop present, and this group has no
|
|
364
|
+
// degradation step below "drop the line-count readout".
|
|
365
|
+
actions.push(th.fg("dim", `m ${MARKDOWN_MODE_LABELS[this.markdownMode()]}`));
|
|
366
|
+
const footerRight = th.fg("dim", "↑↓ scroll · PgUp/PgDn or Shift+↑↓ · Esc close");
|
|
367
|
+
// Prepend the line-count/scroll-% readout only when there's spare width —
|
|
368
|
+
// it's the first thing dropped so it never crowds out the hints.
|
|
369
|
+
const scrollPct = contentLines.length <= viewportHeight
|
|
370
|
+
? "100%"
|
|
371
|
+
: `${Math.round(((visibleStart + viewportHeight) / contentLines.length) * 100)}%`;
|
|
372
|
+
const count = th.fg("dim", `${contentLines.length} lines · ${scrollPct}`);
|
|
373
|
+
const withCount = [count, ...actions].join(sep);
|
|
374
|
+
const footerLeft = visibleWidth(withCount) + visibleWidth(footerRight) + 1 <= innerW
|
|
375
|
+
? withCount
|
|
376
|
+
: actions.join(sep);
|
|
377
|
+
const footerGap = Math.max(1, innerW - visibleWidth(footerLeft) - visibleWidth(footerRight));
|
|
378
|
+
lines.push(row(footerLeft + " ".repeat(footerGap) + footerRight));
|
|
379
|
+
}
|
|
380
|
+
lines.push(hrBot);
|
|
381
|
+
return lines;
|
|
382
|
+
}
|
|
383
|
+
/** Stoppable only when a stop handler exists and the agent is still active. */
|
|
384
|
+
isStoppable() {
|
|
385
|
+
return !!this.onStop && (this.record.status === "running" || this.record.status === "queued");
|
|
386
|
+
}
|
|
387
|
+
/** The mode in force: an `m` press, else the setting, else the default. */
|
|
388
|
+
markdownMode() {
|
|
389
|
+
return this.markdownModeOverride ?? this.viewerMarkdown?.() ?? "assistant";
|
|
390
|
+
}
|
|
391
|
+
/** Wrap `text` literally — the pre-Markdown path, and the fallback from it. */
|
|
392
|
+
rawLines(text, width, dim) {
|
|
393
|
+
const lines = wrapTextWithAnsi(text, width);
|
|
394
|
+
return dim ? lines.map(l => this.theme.fg("dim", l)) : lines;
|
|
395
|
+
}
|
|
396
|
+
/** Render `text` as Markdown, reusing this message's component instance. */
|
|
397
|
+
markdownLines(msg, text, width, dim) {
|
|
398
|
+
let entry = this.markdownCache.get(msg);
|
|
399
|
+
if (!entry) {
|
|
400
|
+
entry = {
|
|
401
|
+
md: new Markdown(text, 0, 0, this.markdownTheme,
|
|
402
|
+
// Keeps result prose visually receded, the way the raw path's
|
|
403
|
+
// per-line `fg("dim", …)` did. Fenced code is the exception and is
|
|
404
|
+
// left alone deliberately: pi's theme highlights it with its own
|
|
405
|
+
// colors, which this would otherwise flatten.
|
|
406
|
+
dim ? { color: (t) => this.theme.fg("dim", t) } : undefined, MARKDOWN_OPTIONS),
|
|
407
|
+
text,
|
|
408
|
+
};
|
|
409
|
+
this.markdownCache.set(msg, entry);
|
|
410
|
+
}
|
|
411
|
+
else if (entry.text !== text) {
|
|
412
|
+
// Streaming: the message object is stable, its text grows. A failed
|
|
413
|
+
// prefix remains unsafe after append-only deltas, so retry only when the
|
|
414
|
+
// content was replaced or truncated.
|
|
415
|
+
const shouldRetry = !text.startsWith(entry.text);
|
|
416
|
+
entry.md.setText(text);
|
|
417
|
+
entry.text = text;
|
|
418
|
+
if (shouldRetry)
|
|
419
|
+
entry.failed = false;
|
|
420
|
+
}
|
|
421
|
+
if (entry.failed)
|
|
422
|
+
return this.rawLines(text, width, dim);
|
|
423
|
+
try {
|
|
424
|
+
return entry.md.render(width);
|
|
425
|
+
}
|
|
426
|
+
catch {
|
|
427
|
+
// The parser is recursive and this is arbitrary tool output: ~54 nested
|
|
428
|
+
// blockquotes overflow the stack, and no amount of fuzzing proves that is
|
|
429
|
+
// the only such input. `render()` is on the TUI's critical path, so a
|
|
430
|
+
// throw here takes the overlay down for content the literal path shows
|
|
431
|
+
// fine — degrade to that instead, and remember, since the throw would
|
|
432
|
+
// otherwise repeat on every render and every scroll key.
|
|
433
|
+
entry.failed = true;
|
|
434
|
+
return this.rawLines(text, width, dim);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
/** Steerable only when a steer handler exists and the agent is still active. */
|
|
438
|
+
canSteer() {
|
|
439
|
+
return !!this.onSteer && (this.record.status === "running" || this.record.status === "queued");
|
|
440
|
+
}
|
|
441
|
+
/** Open the inline steering composer and route subsequent input to it. */
|
|
442
|
+
openComposer() {
|
|
443
|
+
const input = new Input();
|
|
444
|
+
input.focused = true;
|
|
445
|
+
input.onSubmit = (value) => {
|
|
446
|
+
const message = value.trim();
|
|
447
|
+
this.composer = undefined;
|
|
448
|
+
if (message)
|
|
449
|
+
this.onSteer?.(message);
|
|
450
|
+
this.tui.requestRender();
|
|
451
|
+
};
|
|
452
|
+
input.onEscape = () => {
|
|
453
|
+
this.composer = undefined;
|
|
454
|
+
this.tui.requestRender();
|
|
455
|
+
};
|
|
456
|
+
this.composer = input;
|
|
457
|
+
this.tui.requestRender();
|
|
458
|
+
}
|
|
459
|
+
invalidate() { }
|
|
460
|
+
dispose() {
|
|
461
|
+
this.closed = true;
|
|
462
|
+
if (this.unsubscribe) {
|
|
463
|
+
this.unsubscribe();
|
|
464
|
+
this.unsubscribe = undefined;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
// ---- Private ----
|
|
468
|
+
viewportHeight() {
|
|
469
|
+
// Cap mirrors the overlay's maxHeight — otherwise the viewer would render
|
|
470
|
+
// more lines than the overlay shows and clip the footer.
|
|
471
|
+
const maxRows = Math.floor((this.tui.terminal.rows * VIEWPORT_HEIGHT_PCT) / 100);
|
|
472
|
+
return Math.max(MIN_VIEWPORT, maxRows - this.chromeLines());
|
|
473
|
+
}
|
|
474
|
+
chromeLines() {
|
|
475
|
+
// The composer adds one row above the footer hint while it's open.
|
|
476
|
+
return CHROME_LINES_BASE + (this.invocationLine() ? 1 : 0) + (this.composer ? 1 : 0);
|
|
477
|
+
}
|
|
478
|
+
invocationLine() {
|
|
479
|
+
// Canonical id here, short label everywhere else: this overlay is opened to
|
|
480
|
+
// inspect one agent and has the width for it, and two providers can serve
|
|
481
|
+
// models whose short names read alike.
|
|
482
|
+
const { modelName, modelId, tags } = buildInvocationTags(this.record.invocation);
|
|
483
|
+
const model = modelId ?? modelName;
|
|
484
|
+
const parts = model ? [model, ...tags] : tags;
|
|
485
|
+
if (parts.length === 0)
|
|
486
|
+
return undefined;
|
|
487
|
+
return this.theme.fg("dim", ` ↳ ${parts.join(" · ")}`);
|
|
488
|
+
}
|
|
489
|
+
buildContentLines(width) {
|
|
490
|
+
if (width <= 0)
|
|
491
|
+
return [];
|
|
492
|
+
const th = this.theme;
|
|
493
|
+
const messages = this.session.messages;
|
|
494
|
+
const lines = [];
|
|
495
|
+
if (messages.length === 0) {
|
|
496
|
+
lines.push(th.fg("dim", "(waiting for first message...)"));
|
|
497
|
+
return lines;
|
|
498
|
+
}
|
|
499
|
+
const mode = this.markdownMode();
|
|
500
|
+
let needsSeparator = false;
|
|
501
|
+
for (const msg of messages) {
|
|
502
|
+
if (msg.role === "user") {
|
|
503
|
+
const text = typeof msg.content === "string"
|
|
504
|
+
? msg.content
|
|
505
|
+
: extractText(msg.content);
|
|
506
|
+
if (!text.trim())
|
|
507
|
+
continue;
|
|
508
|
+
if (needsSeparator)
|
|
509
|
+
lines.push(th.fg("dim", "───"));
|
|
510
|
+
lines.push(th.fg("accent", "[User]"));
|
|
511
|
+
for (const line of wrapTextWithAnsi(text.trim(), width)) {
|
|
512
|
+
lines.push(line);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
else if (msg.role === "assistant") {
|
|
516
|
+
const textParts = [];
|
|
517
|
+
const toolCalls = [];
|
|
518
|
+
for (const c of msg.content) {
|
|
519
|
+
if (c.type === "text" && c.text)
|
|
520
|
+
textParts.push(c.text);
|
|
521
|
+
else if (c.type === "toolCall") {
|
|
522
|
+
toolCalls.push(c.name ?? c.toolName ?? "unknown");
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
if (needsSeparator)
|
|
526
|
+
lines.push(th.fg("dim", "───"));
|
|
527
|
+
lines.push(th.bold("[Assistant]"));
|
|
528
|
+
if (textParts.length > 0) {
|
|
529
|
+
const text = textParts.join("\n").trim();
|
|
530
|
+
lines.push(...(mode === "off"
|
|
531
|
+
? this.rawLines(text, width, false)
|
|
532
|
+
: this.markdownLines(msg, text, width, false)));
|
|
533
|
+
}
|
|
534
|
+
for (const name of toolCalls) {
|
|
535
|
+
lines.push(truncateToWidth(th.fg("muted", ` [Tool: ${name}]`), width));
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
else if (msg.role === "toolResult") {
|
|
539
|
+
const { text, elided } = capResult(extractText(msg.content).trim());
|
|
540
|
+
if (!text)
|
|
541
|
+
continue;
|
|
542
|
+
if (needsSeparator)
|
|
543
|
+
lines.push(th.fg("dim", "───"));
|
|
544
|
+
lines.push(th.fg("dim", "[Result]"));
|
|
545
|
+
lines.push(...(mode === "all"
|
|
546
|
+
? this.markdownLines(msg, text, width, true)
|
|
547
|
+
: this.rawLines(text, width, true)));
|
|
548
|
+
if (elided)
|
|
549
|
+
lines.push(truncateToWidth(th.fg("dim", truncationNote(elided)), width));
|
|
550
|
+
}
|
|
551
|
+
else if (msg.role === "bashExecution") {
|
|
552
|
+
const bash = msg;
|
|
553
|
+
if (needsSeparator)
|
|
554
|
+
lines.push(th.fg("dim", "───"));
|
|
555
|
+
lines.push(truncateToWidth(th.fg("muted", ` $ ${bash.command}`), width));
|
|
556
|
+
if (bash.output?.trim()) {
|
|
557
|
+
// Same cap as a tool result, never Markdown: command output is the one
|
|
558
|
+
// thing here that is definitionally not authored as Markdown.
|
|
559
|
+
const { text, elided } = capResult(bash.output.trim());
|
|
560
|
+
lines.push(...this.rawLines(text, width, true));
|
|
561
|
+
if (elided)
|
|
562
|
+
lines.push(truncateToWidth(th.fg("dim", truncationNote(elided)), width));
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
else {
|
|
566
|
+
continue;
|
|
567
|
+
}
|
|
568
|
+
needsSeparator = true;
|
|
569
|
+
}
|
|
570
|
+
// Streaming indicator for running agents
|
|
571
|
+
if (this.record.status === "running" && this.activity) {
|
|
572
|
+
const act = describeActivity(this.activity.activeTools, this.activity.responseText);
|
|
573
|
+
lines.push("");
|
|
574
|
+
lines.push(truncateToWidth(th.fg("accent", "▍ ") + th.fg("dim", act), width));
|
|
575
|
+
}
|
|
576
|
+
return lines.map(l => truncateToWidth(l, width));
|
|
577
|
+
}
|
|
578
|
+
}
|