@deathbeam/pi-delegate 0.0.9 → 0.0.11
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/child.ts +21 -13
- package/format.ts +3 -16
- package/index.ts +145 -39
- package/package.json +1 -1
package/child.ts
CHANGED
|
@@ -9,7 +9,11 @@ function usageTokens(usage: any): number | undefined {
|
|
|
9
9
|
return tokens > 0 ? tokens : undefined;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
export type ChildActivity = {
|
|
12
|
+
export type ChildActivity = {
|
|
13
|
+
kind: "tool" | "result" | "assistant" | "user" | "status";
|
|
14
|
+
text: string;
|
|
15
|
+
detail?: string;
|
|
16
|
+
};
|
|
13
17
|
export type ChildUpdate = {
|
|
14
18
|
toolCalls?: number;
|
|
15
19
|
lastTool?: string;
|
|
@@ -138,30 +142,34 @@ export function runChild(
|
|
|
138
142
|
return;
|
|
139
143
|
}
|
|
140
144
|
case "message_update": {
|
|
141
|
-
const contextTokens = usageTokens(event.usage);
|
|
142
145
|
const delta =
|
|
143
146
|
event.assistantMessageEvent?.type === "text_delta"
|
|
144
147
|
? (event.assistantMessageEvent.delta ?? "")
|
|
145
148
|
: "";
|
|
146
149
|
if (delta) liveText += delta;
|
|
147
|
-
|
|
150
|
+
onUpdate?.({ contextTokens: usageTokens(event.usage) });
|
|
148
151
|
return;
|
|
149
152
|
}
|
|
150
153
|
case "message_end": {
|
|
151
|
-
|
|
152
|
-
|
|
154
|
+
const role = event.message?.role;
|
|
155
|
+
if (role !== "assistant" && role !== "user") return;
|
|
156
|
+
const content = event.message.content;
|
|
153
157
|
const text =
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
158
|
+
typeof content === "string"
|
|
159
|
+
? content
|
|
160
|
+
: (content
|
|
161
|
+
?.filter((part: any) => part.type === "text")
|
|
162
|
+
.map((part: any) => part.text ?? "")
|
|
163
|
+
.join("") ?? "");
|
|
164
|
+
if (role === "assistant") {
|
|
165
|
+
childError = event.message.errorMessage;
|
|
166
|
+
if (text) liveText = text;
|
|
167
|
+
}
|
|
168
|
+
if (text)
|
|
160
169
|
onUpdate?.({
|
|
161
170
|
contextTokens: usageTokens(event.message.usage),
|
|
162
|
-
activity: { kind:
|
|
171
|
+
activity: { kind: role, text },
|
|
163
172
|
});
|
|
164
|
-
}
|
|
165
173
|
return;
|
|
166
174
|
}
|
|
167
175
|
case "agent_settled":
|
package/format.ts
CHANGED
|
@@ -41,7 +41,6 @@ const WIDGET_MAX_JOBS = 8;
|
|
|
41
41
|
export const COLLAPSED_OUTPUT_LINES = 10;
|
|
42
42
|
export const MAX_OUTPUT_BYTES = 50 * 1024;
|
|
43
43
|
const EXPANDED_PAD = " ";
|
|
44
|
-
const TASK_LABEL = "Task: ";
|
|
45
44
|
|
|
46
45
|
/** Terminal statuses have static icons; running jobs use statusIcon's clock-driven frame. */
|
|
47
46
|
export function statusIcon(status: JobStatus, now = Date.now()): string {
|
|
@@ -110,21 +109,9 @@ export function toolCallDetail(toolName: string, args: unknown): string {
|
|
|
110
109
|
}
|
|
111
110
|
}
|
|
112
111
|
|
|
113
|
-
/**
|
|
114
|
-
export function launchDetails(info: {
|
|
115
|
-
|
|
116
|
-
`${EXPANDED_PAD}Model: ${info.model ?? "default"}`,
|
|
117
|
-
`${EXPANDED_PAD}Tools: ${formatTools(info.tools)}`,
|
|
118
|
-
];
|
|
119
|
-
const task = (info.task ?? "").trim();
|
|
120
|
-
if (task) {
|
|
121
|
-
lines.push(
|
|
122
|
-
...task
|
|
123
|
-
.split("\n")
|
|
124
|
-
.map((line, index) => `${EXPANDED_PAD}${index ? " ".repeat(TASK_LABEL.length) : TASK_LABEL}${line}`),
|
|
125
|
-
);
|
|
126
|
-
}
|
|
127
|
-
return lines;
|
|
112
|
+
/** Expanded launch metadata; the call renderer shows the task. */
|
|
113
|
+
export function launchDetails(info: { model?: string; tools: string[] }): string[] {
|
|
114
|
+
return [`${EXPANDED_PAD}Model: ${info.model ?? "default"}`, `${EXPANDED_PAD}Tools: ${formatTools(info.tools)}`];
|
|
128
115
|
}
|
|
129
116
|
|
|
130
117
|
/** Close dangling Markdown fences so collapsed previews render correctly. */
|
package/index.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
|
4
4
|
import { homedir } from "node:os";
|
|
5
5
|
import { join, resolve } from "node:path";
|
|
6
6
|
import { fileURLToPath } from "node:url";
|
|
7
|
-
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
7
|
+
import type { ExtensionAPI, ExtensionContext, Theme } from "@earendil-works/pi-coding-agent";
|
|
8
8
|
import {
|
|
9
9
|
CONFIG_DIR_NAME,
|
|
10
10
|
getAgentDir,
|
|
@@ -13,10 +13,13 @@ import {
|
|
|
13
13
|
parseFrontmatter,
|
|
14
14
|
} from "@earendil-works/pi-coding-agent";
|
|
15
15
|
import {
|
|
16
|
+
type Component,
|
|
16
17
|
Container,
|
|
17
18
|
HStack,
|
|
18
19
|
Markdown,
|
|
20
|
+
Spacer,
|
|
19
21
|
Text,
|
|
22
|
+
TruncatedText,
|
|
20
23
|
matchesKey,
|
|
21
24
|
truncateToWidth,
|
|
22
25
|
wrapTextWithAnsi,
|
|
@@ -182,6 +185,60 @@ function contextWindowFor(ctx: ExtensionContext, model: string | undefined): num
|
|
|
182
185
|
return ctx.modelRegistry.find(model.slice(0, separator), model.slice(separator + 1))?.contextWindow;
|
|
183
186
|
}
|
|
184
187
|
|
|
188
|
+
function argText(value: unknown): string {
|
|
189
|
+
return typeof value === "string" ? value : "";
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function jobId(value: unknown): string {
|
|
193
|
+
return argText(value)
|
|
194
|
+
.trim()
|
|
195
|
+
.replace(/^job\s+/i, "");
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function resultText(result: { content: readonly { type: string; text?: string }[] }): string {
|
|
199
|
+
return result.content.flatMap((part) => (part.type === "text" ? [part.text ?? ""] : [])).join("\n");
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function resultFallback(body: string, theme: Theme, isError: boolean): Text {
|
|
203
|
+
return new Text(`\n${theme.fg(isError ? "error" : "toolOutput", body || "(no output)")}`, 0, 0);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** Shared tool layout: an optional identity header, then a width-aware payload preview. */
|
|
207
|
+
function toolLayout(toolName: string, identity: string, payload: string, theme: Theme, expanded: boolean): Component {
|
|
208
|
+
const container = new Container();
|
|
209
|
+
if (toolName)
|
|
210
|
+
container.addChild(
|
|
211
|
+
new TruncatedText(`${theme.fg("toolTitle", theme.bold(toolName))}${identity ? ` ${identity}` : ""}`),
|
|
212
|
+
);
|
|
213
|
+
if (!payload.trim()) return container;
|
|
214
|
+
const body = new Text(
|
|
215
|
+
payload
|
|
216
|
+
.split("\n")
|
|
217
|
+
.map((line) => theme.fg("toolOutput", line))
|
|
218
|
+
.join("\n"),
|
|
219
|
+
0,
|
|
220
|
+
0,
|
|
221
|
+
);
|
|
222
|
+
if (toolName) container.addChild(new Spacer(1));
|
|
223
|
+
container.addChild({
|
|
224
|
+
render: (width) => {
|
|
225
|
+
// Wrapping turns one long line into many, so the cap counts rendered rows, not source lines.
|
|
226
|
+
const lines = body.render(width);
|
|
227
|
+
if (expanded || lines.length <= COLLAPSED_OUTPUT_LINES) return lines;
|
|
228
|
+
return [
|
|
229
|
+
...lines.slice(0, COLLAPSED_OUTPUT_LINES),
|
|
230
|
+
truncateToWidth(
|
|
231
|
+
theme.fg("muted", `… ${lines.length - COLLAPSED_OUTPUT_LINES} more lines, `) +
|
|
232
|
+
keyHint("app.tools.expand", "to expand"),
|
|
233
|
+
width,
|
|
234
|
+
),
|
|
235
|
+
];
|
|
236
|
+
},
|
|
237
|
+
invalidate: () => body.invalidate(),
|
|
238
|
+
});
|
|
239
|
+
return container;
|
|
240
|
+
}
|
|
241
|
+
|
|
185
242
|
export default function (pi: ExtensionAPI) {
|
|
186
243
|
const running = new Map<string, DelegateJob>();
|
|
187
244
|
const recent: InspectJob[] = [];
|
|
@@ -191,7 +248,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
191
248
|
const addActivity = (job: DelegateJob, activity: ChildActivity) => {
|
|
192
249
|
const normalize = (text: string) => text.replace(/\s+/g, " ").trim();
|
|
193
250
|
job.activity.push(
|
|
194
|
-
activity.kind === "assistant"
|
|
251
|
+
activity.kind === "assistant" || activity.kind === "user"
|
|
195
252
|
? activity
|
|
196
253
|
: {
|
|
197
254
|
...activity,
|
|
@@ -202,7 +259,6 @@ export default function (pi: ExtensionAPI) {
|
|
|
202
259
|
viewing?.();
|
|
203
260
|
};
|
|
204
261
|
|
|
205
|
-
// Status words are gone; only a failure detail is worth a log line.
|
|
206
262
|
const remember = (job: DelegateJob, status: JobStatus, detail?: string) => {
|
|
207
263
|
job.status = status;
|
|
208
264
|
if (detail) addActivity(job, { kind: "status", text: detail });
|
|
@@ -210,6 +266,21 @@ export default function (pi: ExtensionAPI) {
|
|
|
210
266
|
recent.unshift({ ...snapshot, endedAt: Date.now() });
|
|
211
267
|
};
|
|
212
268
|
|
|
269
|
+
const jobSnapshot = (job: DelegateJob) => ({
|
|
270
|
+
id: job.id,
|
|
271
|
+
agent: job.agent,
|
|
272
|
+
description: job.description,
|
|
273
|
+
contextTokens: job.contextTokens,
|
|
274
|
+
contextWindow: job.contextWindow,
|
|
275
|
+
startedAt: job.startedAt,
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
const jobTarget = (rawId: unknown, theme: Theme): string => {
|
|
279
|
+
const id = jobId(rawId);
|
|
280
|
+
const job = running.get(id) ?? recent.find((item) => item.id === id);
|
|
281
|
+
return job ? jobIdentity(theme, job) : theme.fg("muted", id || "…");
|
|
282
|
+
};
|
|
283
|
+
|
|
213
284
|
const refreshWidget = (ctx: ExtensionContext) => {
|
|
214
285
|
if (!ctx.hasUI) return;
|
|
215
286
|
viewing?.();
|
|
@@ -376,13 +447,13 @@ export default function (pi: ExtensionAPI) {
|
|
|
376
447
|
].slice(0, bodyHeight());
|
|
377
448
|
};
|
|
378
449
|
const renderEntry = (entry: ChildActivity, width: number): string[] => {
|
|
379
|
-
if (entry.kind === "assistant") {
|
|
450
|
+
if (entry.kind === "assistant" || entry.kind === "user") {
|
|
380
451
|
let md = markdown.get(entry);
|
|
381
452
|
if (!md) {
|
|
382
453
|
md = new Markdown(entry.text, 0, 0, getMarkdownTheme());
|
|
383
454
|
markdown.set(entry, md);
|
|
384
455
|
}
|
|
385
|
-
return [theme.fg("accent",
|
|
456
|
+
return [theme.fg("accent", entry.kind), ...md.render(width), ""];
|
|
386
457
|
}
|
|
387
458
|
if (entry.kind === "tool")
|
|
388
459
|
return [
|
|
@@ -644,31 +715,45 @@ export default function (pi: ExtensionAPI) {
|
|
|
644
715
|
},
|
|
645
716
|
|
|
646
717
|
renderCall(args, theme, context) {
|
|
647
|
-
const
|
|
648
|
-
const
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
718
|
+
const agent = argText(args?.agent).trim();
|
|
719
|
+
const description = argText(args?.description).trim();
|
|
720
|
+
const identity = [
|
|
721
|
+
agent ? theme.fg("toolTitle", theme.bold(agent)) : "",
|
|
722
|
+
description ? theme.fg("accent", description) : "",
|
|
723
|
+
]
|
|
724
|
+
.filter(Boolean)
|
|
725
|
+
.join(" ");
|
|
726
|
+
return toolLayout(
|
|
727
|
+
"delegate",
|
|
728
|
+
identity || theme.fg("muted", "…"),
|
|
729
|
+
argText(args?.task),
|
|
730
|
+
theme,
|
|
731
|
+
context.expanded,
|
|
653
732
|
);
|
|
654
733
|
},
|
|
655
734
|
|
|
656
735
|
renderResult(result, { expanded }, theme, context) {
|
|
657
736
|
const details = result.details as DelegateDetails | undefined;
|
|
658
|
-
const body = result
|
|
659
|
-
if (!details?.agent) return
|
|
660
|
-
|
|
661
|
-
const icon = statusText(theme, context.isError ? "failed" : "done");
|
|
737
|
+
const body = resultText(result);
|
|
738
|
+
if (context.isError || !details?.agent) return resultFallback(body, theme, context.isError);
|
|
662
739
|
const status = details.background ? "started in background" : "completed";
|
|
663
740
|
const container = new Container();
|
|
664
741
|
container.addChild(
|
|
665
742
|
new Text(
|
|
666
|
-
|
|
743
|
+
`\n${statusText(theme, "done")} ${jobIdentity(theme, details)} ${theme.fg("dim", status)}`,
|
|
667
744
|
0,
|
|
668
745
|
0,
|
|
669
746
|
),
|
|
670
747
|
);
|
|
671
|
-
|
|
748
|
+
// The call body already carries the task; only the launch metadata is new here.
|
|
749
|
+
if (expanded)
|
|
750
|
+
container.addChild(
|
|
751
|
+
new Text(
|
|
752
|
+
theme.fg("dim", launchDetails({ model: details.model, tools: details.tools }).join("\n")),
|
|
753
|
+
0,
|
|
754
|
+
0,
|
|
755
|
+
),
|
|
756
|
+
);
|
|
672
757
|
// Background results arrive as their own message; headless runs still show the output here.
|
|
673
758
|
if (!details.background && body) {
|
|
674
759
|
const { shown, hidden } = outputPreview(body, expanded ? Infinity : COLLAPSED_OUTPUT_LINES);
|
|
@@ -699,7 +784,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
699
784
|
message: Type.String({ description: "The guidance to deliver to the running child." }),
|
|
700
785
|
}),
|
|
701
786
|
async execute(_toolCallId, params) {
|
|
702
|
-
const id = params.id
|
|
787
|
+
const id = jobId(params.id);
|
|
703
788
|
const job = running.get(id);
|
|
704
789
|
if (!job?.steer) throw new Error(`No running delegate job "${id}".`);
|
|
705
790
|
await job.steer(params.message);
|
|
@@ -722,30 +807,24 @@ export default function (pi: ExtensionAPI) {
|
|
|
722
807
|
},
|
|
723
808
|
|
|
724
809
|
renderCall(args, theme, context) {
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
810
|
+
return toolLayout(
|
|
811
|
+
"delegate_steer",
|
|
812
|
+
jobTarget(args?.id, theme),
|
|
813
|
+
argText(args?.message),
|
|
814
|
+
theme,
|
|
815
|
+
context.expanded,
|
|
731
816
|
);
|
|
732
817
|
},
|
|
733
818
|
|
|
734
|
-
renderResult(result,
|
|
819
|
+
renderResult(result, _options, theme, context) {
|
|
735
820
|
const details = result.details as SteerDetails | undefined;
|
|
736
|
-
const body = result
|
|
737
|
-
if (!details?.agent) return
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
`${icon} ${jobIdentity(theme, details)} ${theme.fg("muted", details.description)} ${theme.fg("muted", usageStats(details, details.elapsedMs))}`,
|
|
743
|
-
0,
|
|
744
|
-
0,
|
|
745
|
-
),
|
|
821
|
+
const body = resultText(result);
|
|
822
|
+
if (context.isError || !details?.agent) return resultFallback(body, theme, context.isError);
|
|
823
|
+
return new Text(
|
|
824
|
+
`\n${statusText(theme, "done")} ${jobIdentity(theme, details)} ${theme.fg("muted", details.description)} ${theme.fg("muted", usageStats(details, details.elapsedMs))}`,
|
|
825
|
+
0,
|
|
826
|
+
0,
|
|
746
827
|
);
|
|
747
|
-
if (expanded) container.addChild(new Text(theme.fg("dim", details.message), 0, 0));
|
|
748
|
-
return container;
|
|
749
828
|
},
|
|
750
829
|
});
|
|
751
830
|
|
|
@@ -759,7 +838,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
759
838
|
id: Type.String({ description: "Job id from delegate or delegate_list." }),
|
|
760
839
|
}),
|
|
761
840
|
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
|
|
762
|
-
const id = params.id
|
|
841
|
+
const id = jobId(params.id);
|
|
763
842
|
const job = running.get(id);
|
|
764
843
|
if (!job) throw new Error(`No running delegate job "${id}".`);
|
|
765
844
|
running.delete(id);
|
|
@@ -774,6 +853,21 @@ export default function (pi: ExtensionAPI) {
|
|
|
774
853
|
details: { id: job.id, agent: job.agent },
|
|
775
854
|
};
|
|
776
855
|
},
|
|
856
|
+
|
|
857
|
+
renderCall(args, theme, context) {
|
|
858
|
+
return toolLayout("delegate_cancel", jobTarget(args?.id, theme), "", theme, context.expanded);
|
|
859
|
+
},
|
|
860
|
+
|
|
861
|
+
renderResult(result, _options, theme, context) {
|
|
862
|
+
const details = result.details as { id: string; agent: string } | undefined;
|
|
863
|
+
const body = resultText(result);
|
|
864
|
+
if (context.isError || !details?.agent) return resultFallback(body, theme, context.isError);
|
|
865
|
+
return new Text(
|
|
866
|
+
`\n${statusText(theme, "cancelled")} ${jobIdentity(theme, details)} ${theme.fg("muted", "cancellation requested")}`,
|
|
867
|
+
0,
|
|
868
|
+
0,
|
|
869
|
+
);
|
|
870
|
+
},
|
|
777
871
|
});
|
|
778
872
|
|
|
779
873
|
pi.registerTool({
|
|
@@ -795,8 +889,20 @@ export default function (pi: ExtensionAPI) {
|
|
|
795
889
|
);
|
|
796
890
|
return {
|
|
797
891
|
content: [{ type: "text", text: `${jobs.length} running:\n${lines.join("\n")}` }],
|
|
798
|
-
details:
|
|
892
|
+
details: { jobs: jobs.map(jobSnapshot) },
|
|
799
893
|
};
|
|
800
894
|
},
|
|
895
|
+
renderResult(result, _options, theme, context) {
|
|
896
|
+
const jobs = (result.details as { jobs?: ReturnType<typeof jobSnapshot>[] } | undefined)?.jobs;
|
|
897
|
+
if (!jobs?.length || context.isError) return resultFallback(resultText(result), theme, context.isError);
|
|
898
|
+
const now = Date.now();
|
|
899
|
+
return new Text(
|
|
900
|
+
`\n${jobs
|
|
901
|
+
.map((job) => `${jobIdentity(theme, job)} ${theme.fg("muted", jobLine(job, now - job.startedAt))}`)
|
|
902
|
+
.join("\n")}`,
|
|
903
|
+
0,
|
|
904
|
+
0,
|
|
905
|
+
);
|
|
906
|
+
},
|
|
801
907
|
});
|
|
802
908
|
}
|