@restranger/repi-subagents 0.0.1-1a48fc0 → 0.0.1-3546f1c.dirty
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -18
- package/index.ts +2 -0
- package/package.json +8 -8
- package/src/chat-preview.ts +526 -0
- package/src/command.ts +3 -0
- package/src/fleet-ui.ts +678 -19
- package/src/format.ts +50 -7
- package/src/index.ts +11 -0
- package/src/manager.ts +177 -2
- package/src/prompt.ts +18 -5
- package/src/tools.ts +8 -11
- package/src/types.ts +55 -0
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@ project Markdown declarations and JSON overrides.
|
|
|
12
12
|
```bash
|
|
13
13
|
pi install npm:@restranger/repi-subagents
|
|
14
14
|
# or from this repository
|
|
15
|
-
pi --extension ./packages/subagents/
|
|
15
|
+
pi --extension ./packages/subagents/index.ts
|
|
16
16
|
```
|
|
17
17
|
|
|
18
18
|
## Built-in default agents
|
|
@@ -237,19 +237,19 @@ Three surfaces share the manager's live run table. All of them are display-width
|
|
|
237
237
|
colours survive truncation, while cursor movement, OSC titles and other control sequences are
|
|
238
238
|
stripped from child/tool text before it reaches the terminal.
|
|
239
239
|
|
|
240
|
-
- **
|
|
241
|
-
`
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
- **Status widget above the editor.** A tree of
|
|
246
|
-
`
|
|
247
|
-
the active row, tool count, tokens, turn budget
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
240
|
+
- **Collapsed tool call.** A `subagent` call renders as a two-line header — `subagent <agent>
|
|
241
|
+
[foreground|background]` and `task: …` — and nothing more while the child runs. Expanding the
|
|
242
|
+
row (`enter`/`ctrl+o`) reveals the streaming detail collected through `onUpdate`: status,
|
|
243
|
+
elapsed time, tool count, turn budget, the two most recent finished steps and the child's
|
|
244
|
+
current tool (`↳ bash bun run test`). Once the run settles the row shows the outcome compactly.
|
|
245
|
+
- **Status widget above the editor.** A tree of every active run, foreground and background:
|
|
246
|
+
`Active agents: 2 · 14.2k tok · /subagents fleet`, one branch per run, the status with a
|
|
247
|
+
shimmering elapsed time under the active row, tool count, tokens, turn budget, the two most
|
|
248
|
+
recent finished steps and the child's current tool. The widget is unmounted as soon as the last
|
|
249
|
+
child settles. Every line is truncated to the `render(width)` value and the whole widget is
|
|
250
|
+
bounded by `terminal.rows` (3–12 lines, six rows reserved for the dock), so it cannot push Pi's
|
|
251
|
+
editor off screen. Hosts without a TUI (RPC, print, JSON) receive plain lines instead of the
|
|
252
|
+
component.
|
|
253
253
|
- **`/subagents fleet` inspector.** A full-screen overlay (`anchor: "center"`, `width: "100%"`,
|
|
254
254
|
`maxHeight: "100%"`, `margin: 0`) with the same native Pi chrome as `/models` and `/llama`:
|
|
255
255
|
`─` rules in the border colour frame the header, the body and the hint line, the title is
|
|
@@ -268,16 +268,18 @@ stripped from child/tool text before it reaches the terminal.
|
|
|
268
268
|
hosts without a custom TUI component the same dashboard is emitted as a text notification.
|
|
269
269
|
|
|
270
270
|
Activity comes from the child session's `tool_execution_start`/`tool_execution_end` events and
|
|
271
|
-
from assistant turns
|
|
272
|
-
|
|
271
|
+
from assistant turns. The widget still keeps the compact activity/step view, while `/subagents fleet`
|
|
272
|
+
renders a native Pi chat transcript using the host's own components (`AssistantMessageComponent`,
|
|
273
|
+
`UserMessageComponent`, `ToolExecutionComponent` with syntax highlighting, diffs and expandable results),
|
|
274
|
+
falling back to a text renderer when running in headless/mock hosts.
|
|
273
275
|
|
|
274
276
|
## P0 limitations / next steps
|
|
275
277
|
|
|
276
278
|
- No parent-context fork, steering, intercom or batch fan-out. The `/subagents fleet` inspector is read-only
|
|
277
279
|
apart from cancelling a run; result handoff into the parent conversation is still manual via
|
|
278
280
|
`subagent_wait`.
|
|
279
|
-
- No transcript
|
|
280
|
-
|
|
281
|
+
- No parent/child transcript interop beyond the fleet view: child sessions are still in-memory
|
|
282
|
+
(`SessionManager.inMemory()`), so there is no separate transcript command, export or replay yet.
|
|
281
283
|
- No Git worktree isolation; all children share the parent `cwd`.
|
|
282
284
|
- No shared task board or DAG tasks.
|
|
283
285
|
- `ask` is denied inside children until parent prompt routing lands.
|
package/index.ts
ADDED
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@restranger/repi-subagents",
|
|
3
|
-
"version": "0.0.1-
|
|
3
|
+
"version": "0.0.1-3546f1c.dirty",
|
|
4
4
|
"description": "Declarative subagents with Markdown definitions, Fleet UI, and isolated child sessions for pi-coding-agent",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "./
|
|
7
|
-
"types": "./
|
|
6
|
+
"main": "./index.ts",
|
|
7
|
+
"types": "./index.ts",
|
|
8
8
|
"exports": {
|
|
9
|
-
".": "./
|
|
9
|
+
".": "./index.ts"
|
|
10
10
|
},
|
|
11
|
-
"files": ["src", "README.md", "!src/**/__tests__"],
|
|
11
|
+
"files": ["index.ts", "src", "README.md", "!src/**/__tests__"],
|
|
12
12
|
"keywords": ["pi-package", "pi-coding-agent", "subagents", "multi-agent", "fleet", "tui"],
|
|
13
13
|
"author": "ReStranger <restranger@disroot.org>",
|
|
14
14
|
"license": "MIT",
|
|
@@ -25,11 +25,11 @@
|
|
|
25
25
|
"access": "public"
|
|
26
26
|
},
|
|
27
27
|
"pi": {
|
|
28
|
-
"extensions": ["./
|
|
28
|
+
"extensions": ["./index.ts"]
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@restranger/repi-lib": "0.0.1-
|
|
32
|
-
"@restranger/repi-ui": "0.0.1-
|
|
31
|
+
"@restranger/repi-lib": "0.0.1-3546f1c.dirty",
|
|
32
|
+
"@restranger/repi-ui": "0.0.1-3546f1c.dirty"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
35
|
"@earendil-works/pi-coding-agent": ">=0.79.0"
|
|
@@ -0,0 +1,526 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Native Pi chat rendering for the `/subagents fleet` inspector.
|
|
3
|
+
*
|
|
4
|
+
* The fleet preview used to re-implement Markdown, thinking blocks, tool calls and tool results in
|
|
5
|
+
* `fleet-ui.ts`. That renderer could never match the real chat: built-in tool rows (`read`, `bash`,
|
|
6
|
+
* `edit`, …), diff highlighting and the collapsed/expanded tool output all live in Pi's own
|
|
7
|
+
* components. This module loads those components lazily from the host package and feeds them the
|
|
8
|
+
* child transcript, so a subagent preview is literally the same chat the user sees in the main
|
|
9
|
+
* window.
|
|
10
|
+
*
|
|
11
|
+
* The host package is optional: plugins declare it as an optional peer dependency and tests run
|
|
12
|
+
* without it. `loadChatPreview()` therefore returns `undefined` when the import fails, and the
|
|
13
|
+
* inspector keeps its plain-text renderer as a fallback.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import type {
|
|
17
|
+
SubagentRunSnapshot,
|
|
18
|
+
SubagentTranscriptAssistantMessage,
|
|
19
|
+
SubagentTranscriptToolResultMessage,
|
|
20
|
+
} from "./types.js";
|
|
21
|
+
|
|
22
|
+
/** Structural shape of a Pi TUI component; only `render` is required here. */
|
|
23
|
+
export interface HostComponent {
|
|
24
|
+
render(width: number): string[];
|
|
25
|
+
invalidate?(): void;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface HostAssistantComponent extends HostComponent {
|
|
29
|
+
updateContent(message: unknown, streaming?: boolean): void;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface HostToolComponent extends HostComponent {
|
|
33
|
+
updateResult(result: unknown, isPartial?: boolean): void;
|
|
34
|
+
setExpanded(expanded: boolean): void;
|
|
35
|
+
markExecutionStarted?(): void;
|
|
36
|
+
setArgsComplete?(): void;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface HostToolExecutionOptions {
|
|
40
|
+
readonly toolName: string;
|
|
41
|
+
readonly toolCallId: string;
|
|
42
|
+
readonly args: unknown;
|
|
43
|
+
readonly toolDefinition?: unknown;
|
|
44
|
+
readonly cwd: string;
|
|
45
|
+
/** The TUI passed to `ctx.ui.custom`; host tool components only call `requestRender()`. */
|
|
46
|
+
readonly ui?: unknown;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The subset of `@earendil-works/pi-coding-agent` this module needs.
|
|
51
|
+
*
|
|
52
|
+
* Kept structural so the package compiles (and its tests run) without the host dependency.
|
|
53
|
+
*/
|
|
54
|
+
export interface HostChatApi {
|
|
55
|
+
createAssistantMessage(message: unknown, streaming: boolean): HostAssistantComponent;
|
|
56
|
+
createUserMessage(text: string): HostComponent;
|
|
57
|
+
createToolExecution(options: HostToolExecutionOptions): HostToolComponent;
|
|
58
|
+
/** Renderers of a built-in tool (`read`, `bash`, …), used when the child did not capture them. */
|
|
59
|
+
builtinToolDefinition?(toolName: string, cwd: string): unknown;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface ChatPreviewOptions {
|
|
63
|
+
/**
|
|
64
|
+
* Tool definitions captured from the child session. Their `renderCall`/`renderResult` make a
|
|
65
|
+
* preview row identical to the main chat, including extension and MCP tools.
|
|
66
|
+
*/
|
|
67
|
+
readonly toolDefinition?: (runId: string, toolName: string) => unknown;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface ChatPreviewRenderOptions {
|
|
71
|
+
/** Whether tool output is expanded (`Enter` in the inspector). */
|
|
72
|
+
readonly expanded: boolean;
|
|
73
|
+
/** TUI handle for tool components. */
|
|
74
|
+
readonly ui?: unknown;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export interface ChatPreview {
|
|
78
|
+
/** Render the selected run as a Pi chat transcript. */
|
|
79
|
+
render(run: SubagentRunSnapshot, width: number, options: ChatPreviewRenderOptions): string[];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Host constructors, validated at load time so a version mismatch degrades to the fallback. */
|
|
83
|
+
interface HostModule {
|
|
84
|
+
readonly AssistantMessageComponent?: new (
|
|
85
|
+
message?: unknown,
|
|
86
|
+
hideThinkingBlock?: boolean,
|
|
87
|
+
markdownTheme?: unknown,
|
|
88
|
+
hiddenThinkingLabel?: string,
|
|
89
|
+
outputPad?: number,
|
|
90
|
+
) => HostAssistantComponent;
|
|
91
|
+
readonly UserMessageComponent?: new (text: string, markdownTheme?: unknown, outputPad?: number) => HostComponent;
|
|
92
|
+
readonly ToolExecutionComponent?: new (
|
|
93
|
+
toolName: string,
|
|
94
|
+
toolCallId: string,
|
|
95
|
+
args: unknown,
|
|
96
|
+
options: Record<string, unknown> | undefined,
|
|
97
|
+
toolDefinition: unknown,
|
|
98
|
+
ui: unknown,
|
|
99
|
+
cwd: string,
|
|
100
|
+
) => HostToolComponent;
|
|
101
|
+
readonly getMarkdownTheme?: () => unknown;
|
|
102
|
+
readonly initTheme?: (themeName?: string) => void;
|
|
103
|
+
readonly createReadToolDefinition?: (cwd: string, options?: unknown) => unknown;
|
|
104
|
+
readonly createBashToolDefinition?: (cwd: string, options?: unknown) => unknown;
|
|
105
|
+
readonly createEditToolDefinition?: (cwd: string, options?: unknown) => unknown;
|
|
106
|
+
readonly createWriteToolDefinition?: (cwd: string, options?: unknown) => unknown;
|
|
107
|
+
readonly createGrepToolDefinition?: (cwd: string, options?: unknown) => unknown;
|
|
108
|
+
readonly createFindToolDefinition?: (cwd: string, options?: unknown) => unknown;
|
|
109
|
+
readonly createLsToolDefinition?: (cwd: string, options?: unknown) => unknown;
|
|
110
|
+
readonly createPowerShellToolDefinition?: (cwd: string, options?: unknown) => unknown;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const HOST_PACKAGE = "@earendil-works/pi-coding-agent";
|
|
114
|
+
const MAX_CACHED_RUNS = 8;
|
|
115
|
+
const MAX_CACHED_TOOL_DEFINITIONS = 64;
|
|
116
|
+
const OUTPUT_PAD = 1;
|
|
117
|
+
const HIDDEN_THINKING_LABEL = "Thinking...";
|
|
118
|
+
|
|
119
|
+
type BlockKind = "user" | "assistant" | "tool" | "partial";
|
|
120
|
+
|
|
121
|
+
interface ChatBlock {
|
|
122
|
+
readonly kind: BlockKind;
|
|
123
|
+
readonly component: HostComponent;
|
|
124
|
+
/** Rendered lines for `width`; `undefined` after the content changed. */
|
|
125
|
+
lines?: string[];
|
|
126
|
+
width?: number;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
interface RunChatState {
|
|
130
|
+
attempt: number;
|
|
131
|
+
task: string;
|
|
132
|
+
readonly blocks: ChatBlock[];
|
|
133
|
+
/** Tool components by call id, so a result finds the row its call created. */
|
|
134
|
+
readonly tools: Map<string, ChatBlock>;
|
|
135
|
+
/** How many completed transcript messages have been materialized. */
|
|
136
|
+
applied: number;
|
|
137
|
+
partial?: ChatBlock;
|
|
138
|
+
partialVersion: string;
|
|
139
|
+
expanded: boolean;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Incremental native transcript.
|
|
144
|
+
*
|
|
145
|
+
* Completed messages never change once they are in the transcript, so components are created once
|
|
146
|
+
* and only their rendered lines are cached. The streaming partial message updates one component in
|
|
147
|
+
* place (`updateContent`), which is what the host does during a normal turn; rebuilding the whole
|
|
148
|
+
* transcript on every delta would re-render every earlier Markdown block at 12.5 Hz.
|
|
149
|
+
*/
|
|
150
|
+
class NativeChatPreview implements ChatPreview {
|
|
151
|
+
private readonly states = new Map<string, RunChatState>();
|
|
152
|
+
|
|
153
|
+
constructor(
|
|
154
|
+
private readonly api: HostChatApi,
|
|
155
|
+
private readonly options: ChatPreviewOptions = {},
|
|
156
|
+
) {}
|
|
157
|
+
|
|
158
|
+
render(run: SubagentRunSnapshot, width: number, options: ChatPreviewRenderOptions): string[] {
|
|
159
|
+
const state = this.stateFor(run);
|
|
160
|
+
this.sync(state, run, options);
|
|
161
|
+
const lines: string[] = [];
|
|
162
|
+
for (const block of state.blocks) {
|
|
163
|
+
if (block.lines === undefined || block.width !== width) {
|
|
164
|
+
block.lines = block.component.render(width);
|
|
165
|
+
block.width = width;
|
|
166
|
+
}
|
|
167
|
+
lines.push(...block.lines);
|
|
168
|
+
}
|
|
169
|
+
return trimBlankEdges(lines);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
private stateFor(run: SubagentRunSnapshot): RunChatState {
|
|
173
|
+
const key = `${run.id}:${run.attempt ?? 1}`;
|
|
174
|
+
const existing = this.states.get(key);
|
|
175
|
+
if (existing && existing.task === run.task && (run.transcript?.length ?? 0) >= existing.applied) {
|
|
176
|
+
return existing;
|
|
177
|
+
}
|
|
178
|
+
const state: RunChatState = {
|
|
179
|
+
attempt: run.attempt ?? 1,
|
|
180
|
+
task: run.task,
|
|
181
|
+
blocks: [this.userBlock(run.task)],
|
|
182
|
+
tools: new Map(),
|
|
183
|
+
applied: 0,
|
|
184
|
+
partialVersion: "",
|
|
185
|
+
expanded: false,
|
|
186
|
+
};
|
|
187
|
+
this.states.delete(key);
|
|
188
|
+
this.states.set(key, state);
|
|
189
|
+
this.evictStates(key);
|
|
190
|
+
return state;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Keep the newest runs; a long session must not retain a component tree per finished run. */
|
|
194
|
+
private evictStates(keep: string): void {
|
|
195
|
+
while (this.states.size > MAX_CACHED_RUNS) {
|
|
196
|
+
const oldest = this.states.keys().next().value;
|
|
197
|
+
if (!oldest || oldest === keep) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
this.states.delete(oldest);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
private userBlock(task: string): ChatBlock {
|
|
205
|
+
return { kind: "user", component: this.api.createUserMessage(task) };
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
private sync(state: RunChatState, run: SubagentRunSnapshot, options: ChatPreviewRenderOptions): void {
|
|
209
|
+
if (state.expanded !== options.expanded) {
|
|
210
|
+
state.expanded = options.expanded;
|
|
211
|
+
for (const block of state.tools.values()) {
|
|
212
|
+
(block.component as HostToolComponent).setExpanded(options.expanded);
|
|
213
|
+
block.lines = undefined;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const transcript = run.transcript ?? [];
|
|
218
|
+
for (let index = state.applied; index < transcript.length; index++) {
|
|
219
|
+
const message = transcript[index];
|
|
220
|
+
if (!message) {
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
if (message.role === "assistant") {
|
|
224
|
+
state.blocks.push({
|
|
225
|
+
kind: "assistant",
|
|
226
|
+
component: this.api.createAssistantMessage(toHostAssistantMessage(message), false),
|
|
227
|
+
});
|
|
228
|
+
for (const part of message.content) {
|
|
229
|
+
if (part.type !== "toolCall") {
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
const block = this.toolBlock(run, part.name, part.id, parseToolArguments(part.arguments), options);
|
|
233
|
+
state.blocks.push(block);
|
|
234
|
+
state.tools.set(part.id, block);
|
|
235
|
+
}
|
|
236
|
+
} else {
|
|
237
|
+
this.applyToolResult(state, run, message, options);
|
|
238
|
+
}
|
|
239
|
+
state.applied = index + 1;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
this.syncPartial(state, run);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
private toolBlock(
|
|
246
|
+
run: SubagentRunSnapshot,
|
|
247
|
+
toolName: string,
|
|
248
|
+
toolCallId: string,
|
|
249
|
+
args: unknown,
|
|
250
|
+
options: ChatPreviewRenderOptions,
|
|
251
|
+
): ChatBlock {
|
|
252
|
+
const definition =
|
|
253
|
+
this.options.toolDefinition?.(run.id, toolName) ?? this.api.builtinToolDefinition?.(toolName, run.cwd);
|
|
254
|
+
const component = this.api.createToolExecution({
|
|
255
|
+
toolName,
|
|
256
|
+
toolCallId,
|
|
257
|
+
args,
|
|
258
|
+
...(definition !== undefined ? { toolDefinition: definition } : {}),
|
|
259
|
+
cwd: run.cwd,
|
|
260
|
+
...(options.ui !== undefined ? { ui: options.ui } : {}),
|
|
261
|
+
});
|
|
262
|
+
component.setExpanded(options.expanded);
|
|
263
|
+
return { kind: "tool", component };
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* A result normally finds the row created by its call. A transcript that starts mid-run (or a
|
|
268
|
+
* call id the host did not echo) still gets a row, so the output is never silently dropped.
|
|
269
|
+
*/
|
|
270
|
+
private applyToolResult(
|
|
271
|
+
state: RunChatState,
|
|
272
|
+
run: SubagentRunSnapshot,
|
|
273
|
+
message: SubagentTranscriptToolResultMessage,
|
|
274
|
+
options: ChatPreviewRenderOptions,
|
|
275
|
+
): void {
|
|
276
|
+
const block = message.toolCallId ? state.tools.get(message.toolCallId) : undefined;
|
|
277
|
+
if (block) {
|
|
278
|
+
(block.component as HostToolComponent).updateResult(toHostToolResult(message));
|
|
279
|
+
block.lines = undefined;
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
const created = this.toolBlock(run, message.toolName, message.toolCallId ?? `result-${state.applied}`, {}, options);
|
|
283
|
+
(created.component as HostToolComponent).updateResult(toHostToolResult(message));
|
|
284
|
+
state.blocks.push(created);
|
|
285
|
+
if (message.toolCallId) {
|
|
286
|
+
state.tools.set(message.toolCallId, created);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
private syncPartial(state: RunChatState, run: SubagentRunSnapshot): void {
|
|
291
|
+
const partial = run.partialAssistant;
|
|
292
|
+
if (!partial) {
|
|
293
|
+
if (state.partial) {
|
|
294
|
+
const index = state.blocks.indexOf(state.partial);
|
|
295
|
+
if (index >= 0) {
|
|
296
|
+
state.blocks.splice(index, 1);
|
|
297
|
+
}
|
|
298
|
+
state.partial = undefined;
|
|
299
|
+
state.partialVersion = "";
|
|
300
|
+
}
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
const version = assistantVersion(partial);
|
|
304
|
+
if (!state.partial) {
|
|
305
|
+
state.partial = {
|
|
306
|
+
kind: "partial",
|
|
307
|
+
component: this.api.createAssistantMessage(toHostAssistantMessage(partial), true),
|
|
308
|
+
};
|
|
309
|
+
state.blocks.push(state.partial);
|
|
310
|
+
state.partialVersion = version;
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
if (state.partialVersion !== version) {
|
|
314
|
+
(state.partial.component as HostAssistantComponent).updateContent(toHostAssistantMessage(partial), true);
|
|
315
|
+
state.partial.lines = undefined;
|
|
316
|
+
state.partialVersion = version;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Load Pi's chat components and build a preview renderer.
|
|
323
|
+
*
|
|
324
|
+
* Returns `undefined` when the host package or one of the components is unavailable (tests,
|
|
325
|
+
* headless hosts, a future Pi that renamed them); the caller keeps the plain-text fallback.
|
|
326
|
+
*/
|
|
327
|
+
export async function loadChatPreview(options: ChatPreviewOptions = {}): Promise<ChatPreview | undefined> {
|
|
328
|
+
const api = await loadHostChatApi();
|
|
329
|
+
return api ? new NativeChatPreview(api, options) : undefined;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/** Build a preview on an explicit host surface; tests inject fakes here. */
|
|
333
|
+
export function createChatPreview(api: HostChatApi, options: ChatPreviewOptions = {}): ChatPreview {
|
|
334
|
+
return new NativeChatPreview(api, options);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/** Test hook to inject a mock HostChatApi without needing the actual peer dependency. */
|
|
338
|
+
export function _setHostChatApiForTest(api: HostChatApi | undefined): void {
|
|
339
|
+
hostChatApi = api ? Promise.resolve(api) : undefined;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
let hostChatApi: Promise<HostChatApi | undefined> | undefined;
|
|
343
|
+
|
|
344
|
+
function loadHostChatApi(): Promise<HostChatApi | undefined> {
|
|
345
|
+
hostChatApi ??= (async () => {
|
|
346
|
+
try {
|
|
347
|
+
const module = (await import(HOST_PACKAGE)) as HostModule;
|
|
348
|
+
return createHostChatApi(module);
|
|
349
|
+
} catch {
|
|
350
|
+
return undefined;
|
|
351
|
+
}
|
|
352
|
+
})();
|
|
353
|
+
return hostChatApi;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
function createHostChatApi(module: HostModule): HostChatApi | undefined {
|
|
357
|
+
const { AssistantMessageComponent, UserMessageComponent, ToolExecutionComponent, getMarkdownTheme } = module;
|
|
358
|
+
if (
|
|
359
|
+
typeof AssistantMessageComponent !== "function" ||
|
|
360
|
+
typeof UserMessageComponent !== "function" ||
|
|
361
|
+
typeof ToolExecutionComponent !== "function"
|
|
362
|
+
) {
|
|
363
|
+
return undefined;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
let markdownTheme: unknown;
|
|
367
|
+
try {
|
|
368
|
+
markdownTheme = getMarkdownTheme?.();
|
|
369
|
+
} catch {
|
|
370
|
+
try {
|
|
371
|
+
module.initTheme?.("dark");
|
|
372
|
+
markdownTheme = getMarkdownTheme?.();
|
|
373
|
+
} catch {
|
|
374
|
+
markdownTheme = undefined;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
const factories: Record<string, ((cwd: string, options?: unknown) => unknown) | undefined> = {
|
|
379
|
+
read: module.createReadToolDefinition,
|
|
380
|
+
bash: module.createBashToolDefinition,
|
|
381
|
+
edit: module.createEditToolDefinition,
|
|
382
|
+
write: module.createWriteToolDefinition,
|
|
383
|
+
grep: module.createGrepToolDefinition,
|
|
384
|
+
find: module.createFindToolDefinition,
|
|
385
|
+
ls: module.createLsToolDefinition,
|
|
386
|
+
powershell: module.createPowerShellToolDefinition,
|
|
387
|
+
};
|
|
388
|
+
const definitions = new Map<string, unknown>();
|
|
389
|
+
|
|
390
|
+
return {
|
|
391
|
+
createAssistantMessage(message, streaming) {
|
|
392
|
+
const component = new AssistantMessageComponent(
|
|
393
|
+
undefined,
|
|
394
|
+
false,
|
|
395
|
+
markdownTheme,
|
|
396
|
+
HIDDEN_THINKING_LABEL,
|
|
397
|
+
OUTPUT_PAD,
|
|
398
|
+
);
|
|
399
|
+
component.updateContent(message, streaming);
|
|
400
|
+
return component;
|
|
401
|
+
},
|
|
402
|
+
createUserMessage(text) {
|
|
403
|
+
return new UserMessageComponent(text, markdownTheme, OUTPUT_PAD);
|
|
404
|
+
},
|
|
405
|
+
createToolExecution(options) {
|
|
406
|
+
const ui = options.ui ?? { requestRender: () => undefined };
|
|
407
|
+
const component = new ToolExecutionComponent(
|
|
408
|
+
options.toolName,
|
|
409
|
+
options.toolCallId,
|
|
410
|
+
options.args,
|
|
411
|
+
{ showImages: false, outputPad: OUTPUT_PAD },
|
|
412
|
+
options.toolDefinition,
|
|
413
|
+
ui,
|
|
414
|
+
options.cwd,
|
|
415
|
+
);
|
|
416
|
+
component.markExecutionStarted?.();
|
|
417
|
+
component.setArgsComplete?.();
|
|
418
|
+
return component;
|
|
419
|
+
},
|
|
420
|
+
builtinToolDefinition(toolName, cwd) {
|
|
421
|
+
const factory = factories[toolName];
|
|
422
|
+
if (!factory) {
|
|
423
|
+
return undefined;
|
|
424
|
+
}
|
|
425
|
+
const key = `${cwd}\u0000${toolName}`;
|
|
426
|
+
if (definitions.has(key)) {
|
|
427
|
+
return definitions.get(key);
|
|
428
|
+
}
|
|
429
|
+
let definition: unknown;
|
|
430
|
+
try {
|
|
431
|
+
definition = factory(cwd);
|
|
432
|
+
} catch {
|
|
433
|
+
definition = undefined;
|
|
434
|
+
}
|
|
435
|
+
if (definitions.size >= MAX_CACHED_TOOL_DEFINITIONS) {
|
|
436
|
+
definitions.clear();
|
|
437
|
+
}
|
|
438
|
+
definitions.set(key, definition);
|
|
439
|
+
return definition;
|
|
440
|
+
},
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/** Pi's assistant message shape; the component only reads `content`, `stopReason` and `errorMessage`. */
|
|
445
|
+
function toHostAssistantMessage(message: SubagentTranscriptAssistantMessage): unknown {
|
|
446
|
+
return {
|
|
447
|
+
role: "assistant",
|
|
448
|
+
content: message.content.map((part) => {
|
|
449
|
+
switch (part.type) {
|
|
450
|
+
case "text":
|
|
451
|
+
return { type: "text", text: part.text };
|
|
452
|
+
case "thinking":
|
|
453
|
+
return {
|
|
454
|
+
type: "thinking",
|
|
455
|
+
thinking: part.thinking,
|
|
456
|
+
...(part.redacted ? { redacted: true } : {}),
|
|
457
|
+
};
|
|
458
|
+
case "toolCall":
|
|
459
|
+
return {
|
|
460
|
+
type: "toolCall",
|
|
461
|
+
id: part.id,
|
|
462
|
+
name: part.name,
|
|
463
|
+
arguments: parseToolArguments(part.arguments),
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
}),
|
|
467
|
+
...(message.stopReason ? { stopReason: message.stopReason } : {}),
|
|
468
|
+
...(message.errorMessage ? { errorMessage: message.errorMessage } : {}),
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
function toHostToolResult(message: SubagentTranscriptToolResultMessage): unknown {
|
|
473
|
+
const content = message.content.map((part) =>
|
|
474
|
+
part.type === "text"
|
|
475
|
+
? { type: "text", text: part.text }
|
|
476
|
+
: { type: "text", text: `[image${part.mimeType ? `: ${part.mimeType}` : ""}]` },
|
|
477
|
+
);
|
|
478
|
+
return {
|
|
479
|
+
content,
|
|
480
|
+
isError: message.isError,
|
|
481
|
+
...(message.durationMs !== undefined ? { durationMs: message.durationMs } : {}),
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/** The transcript stores pretty-printed JSON; the tool renderers need the object back. */
|
|
486
|
+
function parseToolArguments(text: string | undefined): unknown {
|
|
487
|
+
if (!text) {
|
|
488
|
+
return {};
|
|
489
|
+
}
|
|
490
|
+
try {
|
|
491
|
+
return JSON.parse(text);
|
|
492
|
+
} catch {
|
|
493
|
+
return {};
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
function assistantVersion(message: SubagentTranscriptAssistantMessage): string {
|
|
498
|
+
let size = 0;
|
|
499
|
+
for (const part of message.content) {
|
|
500
|
+
switch (part.type) {
|
|
501
|
+
case "text":
|
|
502
|
+
size += part.text.length;
|
|
503
|
+
break;
|
|
504
|
+
case "thinking":
|
|
505
|
+
size += part.thinking.length;
|
|
506
|
+
break;
|
|
507
|
+
case "toolCall":
|
|
508
|
+
size += part.name.length + (part.arguments?.length ?? 0) + (part.argumentsError?.length ?? 0);
|
|
509
|
+
break;
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
return `${message.content.length}:${size}:${message.stopReason ?? ""}:${message.errorMessage ?? ""}`;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/** Drop the blank lines the first and last component add, so the pane header/footer stay tight. */
|
|
516
|
+
function trimBlankEdges(lines: string[]): string[] {
|
|
517
|
+
let start = 0;
|
|
518
|
+
let end = lines.length;
|
|
519
|
+
while (start < end && lines[start] === "") {
|
|
520
|
+
start++;
|
|
521
|
+
}
|
|
522
|
+
while (end > start && lines[end - 1] === "") {
|
|
523
|
+
end--;
|
|
524
|
+
}
|
|
525
|
+
return lines.slice(start, end);
|
|
526
|
+
}
|
package/src/command.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type ArgumentChoice, type PiContext, type PiExtensionAPI, completeArguments } from "@restranger/repi-lib";
|
|
2
|
+
import { loadChatPreview } from "./chat-preview.js";
|
|
2
3
|
import { FleetView } from "./fleet-ui.js";
|
|
3
4
|
import { formatAgentCatalog, formatDiagnostics, formatFleetDashboard, formatRunList } from "./format.js";
|
|
4
5
|
import type { SubagentManager } from "./manager.js";
|
|
@@ -56,9 +57,11 @@ export async function openFleet(
|
|
|
56
57
|
state.fleet.refresh();
|
|
57
58
|
return;
|
|
58
59
|
}
|
|
60
|
+
const chatPreview = await loadChatPreview().catch(() => undefined);
|
|
59
61
|
const view = new FleetView({
|
|
60
62
|
runs: () => state.manager.list(sessionId),
|
|
61
63
|
now,
|
|
64
|
+
...(chatPreview ? { chatPreview } : {}),
|
|
62
65
|
...(initialId ? { initialId } : {}),
|
|
63
66
|
onCancel: (id) => {
|
|
64
67
|
state.manager.abort(id, sessionId);
|