@deathbeam/pi-delegate 0.0.10 → 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.
Files changed (3) hide show
  1. package/format.ts +3 -16
  2. package/index.ts +142 -36
  3. package/package.json +1 -1
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
- /** Align multi-line task details under the Task label. */
114
- export function launchDetails(info: { task?: string; model?: string; tools: string[] }): string[] {
115
- const lines = [
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[] = [];
@@ -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?.();
@@ -644,31 +715,45 @@ export default function (pi: ExtensionAPI) {
644
715
  },
645
716
 
646
717
  renderCall(args, theme, context) {
647
- const summary = args.description?.trim() || args.task?.split("\n")[0]?.trim() || "";
648
- const hint = `${theme.fg("muted", "(")}${keyHint("app.tools.expand", context.expanded ? "to collapse" : "to expand")}${theme.fg("muted", ")")}`;
649
- return new Text(
650
- `${theme.fg("toolTitle", theme.bold("delegate "))}${theme.fg("accent", summary)} ${hint}`,
651
- 0,
652
- 0,
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.content.flatMap((part) => (part.type === "text" ? [part.text] : [])).join("\n");
659
- if (!details?.agent) return new Text(body || "(no output)", 0, 0);
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
- `${icon} ${jobIdentity(theme, details)} ${theme.fg("muted", details.description)} ${theme.fg("dim", status)}`,
743
+ `\n${statusText(theme, "done")} ${jobIdentity(theme, details)} ${theme.fg("dim", status)}`,
667
744
  0,
668
745
  0,
669
746
  ),
670
747
  );
671
- if (expanded) container.addChild(new Text(theme.fg("dim", launchDetails(details).join("\n")), 0, 0));
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.trim().replace(/^job\s+/i, "");
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
- const message = args.message?.split("\n")[0]?.trim() ?? "";
726
- const hint = `${theme.fg("muted", "(")}${keyHint("app.tools.expand", context.expanded ? "to collapse" : "to expand")}${theme.fg("muted", ")")}`;
727
- return new Text(
728
- `${theme.fg("toolTitle", theme.bold("delegate_steer "))}${theme.fg("accent", message)} ${hint}`,
729
- 0,
730
- 0,
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, { expanded }, theme, context) {
819
+ renderResult(result, _options, theme, context) {
735
820
  const details = result.details as SteerDetails | undefined;
736
- const body = result.content.flatMap((part) => (part.type === "text" ? [part.text] : [])).join("\n");
737
- if (!details?.agent) return new Text(body || "(no output)", 0, 0);
738
- const icon = statusText(theme, context.isError ? "failed" : "done");
739
- const container = new Container();
740
- container.addChild(
741
- new Text(
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.trim().replace(/^job\s+/i, "");
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: undefined,
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deathbeam/pi-delegate",
3
- "version": "0.0.10",
3
+ "version": "0.0.11",
4
4
  "description": "Run focused Pi subagents as background tasks",
5
5
  "type": "module",
6
6
  "license": "MIT",