@deathbeam/pi-delegate 0.0.22 → 0.0.23
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 +80 -0
- package/index.ts +15 -7
- package/package.json +1 -1
package/child.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import type { ChildProcessWithoutNullStreams } from "node:child_process";
|
|
2
|
+
import { readdirSync, readFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
2
4
|
import { limitOutput, resultPreview, toolCallDetail } from "./format.ts";
|
|
3
5
|
|
|
4
6
|
type Child = ChildProcessWithoutNullStreams;
|
|
@@ -299,3 +301,81 @@ export function runChild(
|
|
|
299
301
|
steer: (message) => send({ type: "steer", message }),
|
|
300
302
|
};
|
|
301
303
|
}
|
|
304
|
+
|
|
305
|
+
/** Text content of a persisted transcript message, in part order. */
|
|
306
|
+
function transcriptText(content: unknown): string[] {
|
|
307
|
+
if (typeof content === "string") return [content];
|
|
308
|
+
if (!Array.isArray(content)) return [];
|
|
309
|
+
return content
|
|
310
|
+
.map((part) => (part && typeof part === "object" ? (part as { type?: string; text?: string }) : {}))
|
|
311
|
+
.filter((part) => part.type === "text" && typeof part.text === "string")
|
|
312
|
+
.map((part) => part.text!);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Replays the child's persisted transcript as the activity a resumed job continues from, so the
|
|
317
|
+
* inspector keeps the old history. A missing or unreadable transcript seeds nothing: the resume
|
|
318
|
+
* itself still works.
|
|
319
|
+
*/
|
|
320
|
+
export function priorActivity(sessionDir: string, id: string): ChildActivity[] {
|
|
321
|
+
let sessionFile: string | undefined;
|
|
322
|
+
try {
|
|
323
|
+
// Sessions are <timestamp>_<sessionId>.jsonl; a recreated pinned session is the newest one.
|
|
324
|
+
sessionFile = readdirSync(sessionDir)
|
|
325
|
+
.filter((name) => name.endsWith(`_delegate-${id}.jsonl`))
|
|
326
|
+
.sort()
|
|
327
|
+
.at(-1);
|
|
328
|
+
} catch {
|
|
329
|
+
return [];
|
|
330
|
+
}
|
|
331
|
+
if (!sessionFile) return [];
|
|
332
|
+
let lines: string[] = [];
|
|
333
|
+
try {
|
|
334
|
+
lines = readFileSync(join(sessionDir, sessionFile), "utf8").split("\n");
|
|
335
|
+
} catch {
|
|
336
|
+
return [];
|
|
337
|
+
}
|
|
338
|
+
const normalize = (text: string) => text.replace(/\s+/g, " ").trim();
|
|
339
|
+
const activity: ChildActivity[] = [];
|
|
340
|
+
for (const line of lines) {
|
|
341
|
+
if (!line.trim()) continue;
|
|
342
|
+
let entry: { type?: string; message?: unknown };
|
|
343
|
+
try {
|
|
344
|
+
entry = JSON.parse(line);
|
|
345
|
+
} catch {
|
|
346
|
+
continue; // Same tolerance pi's session reader has for malformed lines.
|
|
347
|
+
}
|
|
348
|
+
const message =
|
|
349
|
+
entry?.type === "message"
|
|
350
|
+
? (entry.message as {
|
|
351
|
+
role?: "assistant" | "user" | "toolResult";
|
|
352
|
+
content?: unknown;
|
|
353
|
+
toolName?: string;
|
|
354
|
+
})
|
|
355
|
+
: undefined;
|
|
356
|
+
if (!message?.role) continue;
|
|
357
|
+
if (message.role === "toolResult") {
|
|
358
|
+
const preview = resultPreview({ content: message.content });
|
|
359
|
+
activity.push({
|
|
360
|
+
kind: "result",
|
|
361
|
+
text: normalize(`↳ ${message.toolName ?? "tool"}: ${preview ?? "(no text output)"}`),
|
|
362
|
+
});
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
// Assistant text lands before its tool calls, matching the live event order.
|
|
366
|
+
const text = transcriptText(message.content).join("");
|
|
367
|
+
if (text) activity.push({ kind: message.role, text });
|
|
368
|
+
if (message.role !== "assistant") continue;
|
|
369
|
+
for (const part of Array.isArray(message.content) ? message.content : []) {
|
|
370
|
+
const call =
|
|
371
|
+
part && typeof part === "object" ? (part as { type?: string; name?: string; arguments?: unknown }) : {};
|
|
372
|
+
if (call.type !== "toolCall" || !call.name) continue;
|
|
373
|
+
activity.push({
|
|
374
|
+
kind: "tool",
|
|
375
|
+
text: normalize(call.name),
|
|
376
|
+
detail: normalize(toolCallDetail(call.name, call.arguments)) || undefined,
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
return activity;
|
|
381
|
+
}
|
package/index.ts
CHANGED
|
@@ -29,13 +29,14 @@ import {
|
|
|
29
29
|
usageStats,
|
|
30
30
|
widgetJobs,
|
|
31
31
|
} from "./format.ts";
|
|
32
|
-
import { runChild } from "./child.ts";
|
|
32
|
+
import { priorActivity, runChild } from "./child.ts";
|
|
33
33
|
import { createJobStore, resumeDecision, type DelegateJob } from "./store.ts";
|
|
34
34
|
import { registerInspector } from "./inspector.ts";
|
|
35
35
|
import { configFor, contextWindowFor, discoverAgents, resolveModel } from "./agents.ts";
|
|
36
36
|
|
|
37
37
|
type DelegateDetails = Pick<DelegateJob, "id" | "agent" | "description" | "task" | "model" | "tools"> & {
|
|
38
38
|
background: boolean;
|
|
39
|
+
resumed: boolean;
|
|
39
40
|
};
|
|
40
41
|
|
|
41
42
|
type SteerDetails = Pick<
|
|
@@ -354,7 +355,8 @@ export default function (pi: ExtensionAPI) {
|
|
|
354
355
|
task: prior?.task ?? params.task ?? "",
|
|
355
356
|
model: displayModel,
|
|
356
357
|
status: "running",
|
|
357
|
-
|
|
358
|
+
// A resume continues the pinned transcript, so the inspector replays the child's own history.
|
|
359
|
+
activity: prior ? priorActivity(DELEGATE_SESSION_DIR, prior.id) : [],
|
|
358
360
|
tools,
|
|
359
361
|
toolCalls: 0,
|
|
360
362
|
startedAt: Date.now(),
|
|
@@ -369,6 +371,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
369
371
|
model: displayModel,
|
|
370
372
|
tools,
|
|
371
373
|
background: ctx.hasUI,
|
|
374
|
+
resumed: Boolean(prior),
|
|
372
375
|
};
|
|
373
376
|
const args = [
|
|
374
377
|
"--session-id",
|
|
@@ -426,7 +429,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
426
429
|
content: [
|
|
427
430
|
{
|
|
428
431
|
type: "text",
|
|
429
|
-
text:
|
|
432
|
+
text: `${prior ? "Resumed" : "Started"} delegate "${job.agent}" in the background (job ${job.id}). Steer it with delegate_steer or stop it with delegate_cancel. Do not call tools solely to wait, even no-op bash commands; when its report is your next dependency, end your turn and it will arrive automatically unless cancelled.`,
|
|
430
433
|
},
|
|
431
434
|
],
|
|
432
435
|
details,
|
|
@@ -434,18 +437,21 @@ export default function (pi: ExtensionAPI) {
|
|
|
434
437
|
},
|
|
435
438
|
|
|
436
439
|
renderCall(args, theme, context) {
|
|
437
|
-
|
|
438
|
-
const
|
|
440
|
+
// A resume passes only the job id; recover the identity and task from the stored job.
|
|
441
|
+
const resumed = args?.resume ? store.find(jobId(args.resume)) : undefined;
|
|
442
|
+
const agent = argText(args?.agent).trim() || resumed?.agent || "";
|
|
443
|
+
const description = argText(args?.description).trim() || resumed?.description || "";
|
|
439
444
|
const identity = [
|
|
440
445
|
agent ? theme.fg("toolTitle", theme.bold(agent)) : "",
|
|
441
446
|
description ? theme.fg("accent", description) : "",
|
|
447
|
+
resumed ? theme.fg("muted", "(resumed)") : "",
|
|
442
448
|
]
|
|
443
449
|
.filter(Boolean)
|
|
444
450
|
.join(" ");
|
|
445
451
|
return toolLayout(
|
|
446
452
|
"delegate",
|
|
447
453
|
identity || theme.fg("muted", "…"),
|
|
448
|
-
argText(args?.task),
|
|
454
|
+
argText(args?.task) || resumed?.task || "",
|
|
449
455
|
theme,
|
|
450
456
|
context.expanded,
|
|
451
457
|
);
|
|
@@ -455,7 +461,9 @@ export default function (pi: ExtensionAPI) {
|
|
|
455
461
|
const details = result.details as DelegateDetails | undefined;
|
|
456
462
|
const body = resultText(result);
|
|
457
463
|
if (context.isError || !details?.agent) return resultFallback(body, theme, context.isError);
|
|
458
|
-
const status = details.background
|
|
464
|
+
const status = details.background
|
|
465
|
+
? `${details.resumed ? "resumed" : "started"} in background`
|
|
466
|
+
: "completed";
|
|
459
467
|
// The done icon means actual completion; a background launch reports its status without it.
|
|
460
468
|
const header = details.background
|
|
461
469
|
? `${jobIdentity(theme, details)} ${theme.fg("muted", status)}`
|