@introspection-ai/recipes 0.63.1 → 0.64.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/dist/agent-tool/arguments.d.ts +39 -0
- package/dist/agent-tool/arguments.d.ts.map +1 -0
- package/dist/agent-tool/arguments.generated.d.ts +4 -0
- package/dist/agent-tool/arguments.generated.d.ts.map +1 -0
- package/dist/agent-tool/arguments.generated.js +60 -0
- package/dist/agent-tool/arguments.generated.js.map +1 -0
- package/dist/agent-tool/arguments.js +8 -0
- package/dist/agent-tool/arguments.js.map +1 -0
- package/dist/agents.d.ts +28 -6
- package/dist/agents.d.ts.map +1 -1
- package/dist/agents.js +269 -46
- package/dist/agents.js.map +1 -1
- package/dist/batch/agent-batches.d.ts +53 -0
- package/dist/batch/agent-batches.d.ts.map +1 -0
- package/dist/batch/agent-batches.js +168 -0
- package/dist/batch/agent-batches.js.map +1 -0
- package/dist/batch/index.d.ts +2 -0
- package/dist/batch/index.d.ts.map +1 -1
- package/dist/batch/index.js +2 -1
- package/dist/batch/index.js.map +1 -1
- package/dist/child/agent-completions.d.ts +16 -1
- package/dist/child/agent-completions.d.ts.map +1 -1
- package/dist/child/agent-completions.js +31 -5
- package/dist/child/agent-completions.js.map +1 -1
- package/dist/child/agent-store.d.ts +2 -0
- package/dist/child/agent-store.d.ts.map +1 -1
- package/dist/child/agent-store.js.map +1 -1
- package/dist/child/agent.d.ts +9 -0
- package/dist/child/agent.d.ts.map +1 -1
- package/dist/child/agent.js +7 -0
- package/dist/child/agent.js.map +1 -1
- package/dist/child/arguments.d.ts +32 -0
- package/dist/child/arguments.d.ts.map +1 -0
- package/dist/child/arguments.generated.d.ts +4 -0
- package/dist/child/arguments.generated.d.ts.map +1 -0
- package/dist/child/arguments.generated.js +29 -0
- package/dist/child/arguments.generated.js.map +1 -0
- package/dist/child/arguments.js +8 -0
- package/dist/child/arguments.js.map +1 -0
- package/dist/child/messages.d.ts +128 -0
- package/dist/child/messages.d.ts.map +1 -0
- package/dist/child/messages.js +265 -0
- package/dist/child/messages.js.map +1 -0
- package/dist/child/session.d.ts +10 -0
- package/dist/child/session.d.ts.map +1 -1
- package/dist/child/session.js +22 -3
- package/dist/child/session.js.map +1 -1
- package/dist/child/streaming-usage.d.ts +40 -0
- package/dist/child/streaming-usage.d.ts.map +1 -0
- package/dist/child/streaming-usage.js +101 -0
- package/dist/child/streaming-usage.js.map +1 -0
- package/dist/pi-extension.d.ts +2 -0
- package/dist/pi-extension.d.ts.map +1 -1
- package/dist/pi-extension.js +236 -42
- package/dist/pi-extension.js.map +1 -1
- package/dist/recipe/manifest.generated.d.ts +1 -1
- package/dist/recipe/manifest.generated.js +1 -1
- package/dist/run-controller.d.ts +5 -0
- package/dist/run-controller.d.ts.map +1 -1
- package/dist/run-controller.js +70 -8
- package/dist/run-controller.js.map +1 -1
- package/dist/session.d.ts +11 -0
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +15 -1
- package/dist/session.js.map +1 -1
- package/dist/skill-progress.d.ts +12 -1
- package/dist/skill-progress.d.ts.map +1 -1
- package/dist/skill-progress.js +11 -4
- package/dist/skill-progress.js.map +1 -1
- package/docs/agent-batch.md +87 -0
- package/docs/agent-composition.md +49 -19
- package/docs/batch.md +7 -0
- package/docs/productivity/google.md +27 -15
- package/package.json +6 -6
package/dist/pi-extension.js
CHANGED
|
@@ -2,18 +2,23 @@ import { registerSkillProgress, skillFolders, } from "./skill-progress.js";
|
|
|
2
2
|
import { RECIPE_SESSION_ENTRY_TYPE, recipeSessionChanged, } from "./recipe-session-entry.js";
|
|
3
3
|
import { loadMemoryIndex } from "./memory.js";
|
|
4
4
|
import { registerMemoryContext } from "./memory-context.js";
|
|
5
|
+
import { DEFAULT_SUBAGENT_CONCURRENCY } from "./run-controller.js";
|
|
6
|
+
import { createAgentBatches, } from "./batch/agent-batches.js";
|
|
7
|
+
import { runBatch } from "./batch/index.js";
|
|
5
8
|
import { existsSync } from "node:fs";
|
|
6
9
|
import { rm } from "node:fs/promises";
|
|
7
10
|
import { basename, resolve } from "node:path";
|
|
8
11
|
import { createCodemodeExtension, } from "@earendil-works/pi-coding-agent";
|
|
9
|
-
import { Text } from "@earendil-works/pi-tui";
|
|
12
|
+
import { Text, truncateToWidth } from "@earendil-works/pi-tui";
|
|
10
13
|
import { createRecipeChildAgentRunner, promptResultText, } from "./child/agent.js";
|
|
11
14
|
import { registerGenAiSpansFile } from "./genai-spans-file.js";
|
|
12
15
|
import { loadRecipeExtensionFactory } from "./recipe/extensions.js";
|
|
13
16
|
import { bindRecipeExtensionFactory, createRecipeExtensionSessionContext, createRecipeExtensionRegistrationRegistry, } from "./extensions.js";
|
|
14
17
|
import { ChildAgentRunStore, } from "./child/agent-store.js";
|
|
15
|
-
import { ChildCompletionQueue, envelopeFromRun, renderCompletionNotice, usageText, } from "./child/agent-completions.js";
|
|
16
|
-
import {
|
|
18
|
+
import { ChildCompletionQueue, costText, envelopeFromRun, renderCompletionNotice, tokenText, usageText, } from "./child/agent-completions.js";
|
|
19
|
+
import { AGENT_RUN_MESSAGE_TYPE, AgentInbox, AgentMessageBus, renderAgentMessage, renderAgentMessages, ROOT_AGENT, } from "./child/messages.js";
|
|
20
|
+
import { AGENT_RUN_EVENT_ENTRY_TYPE, notifyAgentRunEvent, persistsAgentRunEvent, agentLine, runProgress, toolActivity, } from "./agents.js";
|
|
21
|
+
import { applyStreamingEvent, liveUsage, } from "./child/streaming-usage.js";
|
|
17
22
|
import { clearMcpSession, clearSessionMcpCli, configureMcpLocalConfigPath, createIsolatedMcpEnvironment, endpointBindings, formatMcpConfigurationDiagnostics, materializeMcpSession, materializeSessionMcpCli, resolveAgentMcpSelections, resolveMcpSessionServers, stopMcpDaemon, } from "./mcp/index.js";
|
|
18
23
|
import { mcpServersBehindTools } from "./mcp/policy.js";
|
|
19
24
|
import { clearMcpCatalogPreload, preloadMcpCatalogs } from "./mcp/catalog.js";
|
|
@@ -249,6 +254,35 @@ function snapshotOf(run) {
|
|
|
249
254
|
error: run.error,
|
|
250
255
|
toolCalls: run.toolCalls.map((call) => ({ ...call })),
|
|
251
256
|
...(run.usage ? { usage: { ...run.usage } } : {}),
|
|
257
|
+
...(run.pricing ? { pricing: { ...run.pricing } } : {}),
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
/** Folds how one assistant message's model is paid for into its run's pricing. */
|
|
261
|
+
function notePricing(run, event, registry) {
|
|
262
|
+
const record = asRecord(event);
|
|
263
|
+
if (record?.type !== "message_start" && record?.type !== "message_end")
|
|
264
|
+
return;
|
|
265
|
+
const message = asRecord(record.message);
|
|
266
|
+
if (message?.role !== "assistant" ||
|
|
267
|
+
typeof message.provider !== "string" ||
|
|
268
|
+
typeof message.model !== "string")
|
|
269
|
+
return;
|
|
270
|
+
const model = registry.find(message.provider, message.model);
|
|
271
|
+
const price = model?.cost;
|
|
272
|
+
const pricing = {
|
|
273
|
+
subscription: model !== undefined && registry.isUsingOAuth(model),
|
|
274
|
+
priced: price !== undefined &&
|
|
275
|
+
[price.input, price.output, price.cacheRead, price.cacheWrite].some((rate) => typeof rate === "number" && rate > 0),
|
|
276
|
+
};
|
|
277
|
+
run.pricing = mergePricing(run.pricing, pricing);
|
|
278
|
+
}
|
|
279
|
+
/** Several runs' pricing: a subscription only if every one is, priced if any one is. */
|
|
280
|
+
function mergePricing(a, b) {
|
|
281
|
+
if (!a || !b)
|
|
282
|
+
return a ?? b;
|
|
283
|
+
return {
|
|
284
|
+
subscription: a.subscription && b.subscription,
|
|
285
|
+
priced: a.priced || b.priced,
|
|
252
286
|
};
|
|
253
287
|
}
|
|
254
288
|
/** Adds an assistant message's usage, from a child's `message_end` event, to its run's. */
|
|
@@ -277,16 +311,29 @@ function addMessageUsage(run, event) {
|
|
|
277
311
|
};
|
|
278
312
|
return true;
|
|
279
313
|
}
|
|
314
|
+
/** Applies a streaming event to the state of the run that streams it. */
|
|
315
|
+
function streamFor(run, streamId, event) {
|
|
316
|
+
let state = run.streaming.get(streamId);
|
|
317
|
+
if (!state) {
|
|
318
|
+
state = {};
|
|
319
|
+
run.streaming.set(streamId, state);
|
|
320
|
+
}
|
|
321
|
+
return applyStreamingEvent(state, event);
|
|
322
|
+
}
|
|
280
323
|
function controllerSummary(run, status = run.status) {
|
|
324
|
+
const streaming = run.streaming;
|
|
281
325
|
const startedAt = Date.parse(run.startedAt);
|
|
282
326
|
const completedAt = run.completedAt ? Date.parse(run.completedAt) : undefined;
|
|
283
|
-
const
|
|
327
|
+
const running = [...run.toolCalls]
|
|
284
328
|
.reverse()
|
|
285
|
-
.find((call) => call.status === "running")
|
|
329
|
+
.find((call) => call.status === "running");
|
|
330
|
+
const currentActivity = running && toolActivity(running.name, running.args);
|
|
331
|
+
const usage = liveUsage(run.usage, streaming?.values() ?? []);
|
|
286
332
|
return {
|
|
287
333
|
agent_run_id: run.id,
|
|
288
334
|
invocation_name: run.agent,
|
|
289
335
|
agent_name: run.agent,
|
|
336
|
+
...(run.queued ? { queued: true } : {}),
|
|
290
337
|
label: run.label ?? run.agent,
|
|
291
338
|
prompt: run.prompt,
|
|
292
339
|
status,
|
|
@@ -299,7 +346,11 @@ function controllerSummary(run, status = run.status) {
|
|
|
299
346
|
: Number.isFinite(startedAt)
|
|
300
347
|
? startedAt
|
|
301
348
|
: Date.now(),
|
|
302
|
-
...(
|
|
349
|
+
...(running
|
|
350
|
+
? { current_tool: running.name, current_activity: currentActivity }
|
|
351
|
+
: {}),
|
|
352
|
+
...(run.statusNote ? { status_text: run.statusNote.text } : {}),
|
|
353
|
+
...(run.statusNote?.usage ? { reported_usage: run.statusNote.usage } : {}),
|
|
303
354
|
nested_tools: run.toolCalls.map((call) => ({
|
|
304
355
|
toolName: call.name,
|
|
305
356
|
verb: call.name,
|
|
@@ -309,14 +360,15 @@ function controllerSummary(run, status = run.status) {
|
|
|
309
360
|
output_preview: run.output,
|
|
310
361
|
output: run.output,
|
|
311
362
|
error: run.error,
|
|
312
|
-
...(
|
|
363
|
+
...(usage
|
|
313
364
|
? {
|
|
314
365
|
usage: {
|
|
315
|
-
input_tokens:
|
|
316
|
-
output_tokens:
|
|
317
|
-
cache_read_tokens:
|
|
318
|
-
cache_write_tokens:
|
|
319
|
-
cost_usd:
|
|
366
|
+
input_tokens: usage.input,
|
|
367
|
+
output_tokens: usage.output,
|
|
368
|
+
cache_read_tokens: usage.cacheRead,
|
|
369
|
+
cache_write_tokens: usage.cacheWrite,
|
|
370
|
+
cost_usd: usage.cost,
|
|
371
|
+
...(run.pricing ? { pricing: run.pricing } : {}),
|
|
320
372
|
},
|
|
321
373
|
}
|
|
322
374
|
: {}),
|
|
@@ -419,8 +471,59 @@ export function createRecipesExtension(opts = {}) {
|
|
|
419
471
|
let lastLaunchErrorKey = null;
|
|
420
472
|
let childRunIndex = 0;
|
|
421
473
|
const childRuns = new Map();
|
|
474
|
+
// Labels a batch is about to start a row under: the run started under one is the batch's.
|
|
475
|
+
const quietLabels = new Set();
|
|
476
|
+
let agentBatches;
|
|
477
|
+
const subagentConcurrency = Math.max(1, opts.subagentConcurrency ?? DEFAULT_SUBAGENT_CONCURRENCY);
|
|
478
|
+
let activeChildren = 0;
|
|
479
|
+
const waitingChildren = [];
|
|
480
|
+
/** A slot for `run`, at once or once a running run ends; false when it left the queue without one. */
|
|
481
|
+
const takeChildSlot = (run) => {
|
|
482
|
+
if (activeChildren < subagentConcurrency) {
|
|
483
|
+
activeChildren += 1;
|
|
484
|
+
return Promise.resolve(true);
|
|
485
|
+
}
|
|
486
|
+
run.queued = true;
|
|
487
|
+
return new Promise((resolve) => waitingChildren.push({ run, resolve }));
|
|
488
|
+
};
|
|
489
|
+
const freeChildSlot = () => {
|
|
490
|
+
const next = waitingChildren.shift();
|
|
491
|
+
if (!next) {
|
|
492
|
+
activeChildren -= 1;
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
next.run.queued = false;
|
|
496
|
+
next.resolve(true);
|
|
497
|
+
};
|
|
498
|
+
const leaveChildQueue = (run) => {
|
|
499
|
+
const index = waitingChildren.findIndex((waiting) => waiting.run === run);
|
|
500
|
+
if (index < 0)
|
|
501
|
+
return;
|
|
502
|
+
const [waiting] = waitingChildren.splice(index, 1);
|
|
503
|
+
run.queued = false;
|
|
504
|
+
waiting.resolve(false);
|
|
505
|
+
};
|
|
506
|
+
// The session's agents by name, for `send_message`. Root takes a message in
|
|
507
|
+
// its current wait, or as a steer or follow-up, starting a turn when idle.
|
|
508
|
+
const messages = new AgentMessageBus();
|
|
509
|
+
const rootInbox = new AgentInbox(async (message) => {
|
|
510
|
+
extensionApi?.sendMessage({
|
|
511
|
+
customType: AGENT_RUN_MESSAGE_TYPE,
|
|
512
|
+
content: renderAgentMessage(message),
|
|
513
|
+
display: true,
|
|
514
|
+
details: message,
|
|
515
|
+
},
|
|
516
|
+
// An idle root only stores a message without triggerTurn; streaming, it
|
|
517
|
+
// takes the message as `deliver` says.
|
|
518
|
+
{ triggerTurn: true, deliverAs: message.deliver });
|
|
519
|
+
}, () => !(sessionCtx?.isIdle?.() ?? true));
|
|
520
|
+
messages.register({
|
|
521
|
+
id: ROOT_AGENT,
|
|
522
|
+
name: ROOT_AGENT,
|
|
523
|
+
inbox: rootInbox,
|
|
524
|
+
accepting: () => true,
|
|
525
|
+
});
|
|
422
526
|
// Usage of runs already closed: closing a run removes it from childRuns, not its cost.
|
|
423
|
-
const closedUsage = { output: 0, cost: 0 };
|
|
424
527
|
let releaseApiBindings;
|
|
425
528
|
// Runs restored from a previous Pi process via rehydrateChildRuns(). They
|
|
426
529
|
// have no live runner: readable (status/wait) but not controllable.
|
|
@@ -436,7 +539,7 @@ export function createRecipesExtension(opts = {}) {
|
|
|
436
539
|
let agentsWidgetTimer;
|
|
437
540
|
// Re-reports the step when the running count moves; set once the footer is registered.
|
|
438
541
|
let refreshAgentStep;
|
|
439
|
-
const runningAgents = () => [...childRuns.values()].filter((run) => run.status === "running").length;
|
|
542
|
+
const runningAgents = () => [...childRuns.values()].filter((run) => run.status === "running" && !run.queued).length;
|
|
440
543
|
const renderAgentsWidget = () => {
|
|
441
544
|
refreshAgentStep?.();
|
|
442
545
|
const ctx = localAgentContext;
|
|
@@ -449,27 +552,28 @@ export function createRecipesExtension(opts = {}) {
|
|
|
449
552
|
return;
|
|
450
553
|
ctx.ui.setWidget(AGENTS_WIDGET_KEY, running.length === 0
|
|
451
554
|
? undefined
|
|
452
|
-
:
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
? ` (${summary.label})`
|
|
456
|
-
: "";
|
|
457
|
-
return `▸ ${summary.agent_name}${label} · ${runProgress(summary)}`;
|
|
555
|
+
: () => ({
|
|
556
|
+
render: (width) => running.map((run) => truncateToWidth(agentLine(controllerSummary(run)), width)),
|
|
557
|
+
invalidate: () => { },
|
|
458
558
|
}), { placement: "belowEditor" });
|
|
459
559
|
};
|
|
460
|
-
/** `
|
|
560
|
+
/** `2 agents · ↓ 4.1k · $0.450 (sub)`: the runs in progress and their usage so far; nothing once none runs. */
|
|
461
561
|
const agentsSummary = () => {
|
|
462
|
-
const runs = [...childRuns.values()];
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
const
|
|
466
|
-
const
|
|
467
|
-
|
|
562
|
+
const runs = [...childRuns.values()].filter((run) => run.status === "running");
|
|
563
|
+
if (runs.length === 0)
|
|
564
|
+
return undefined;
|
|
565
|
+
const usages = runs.map((run) => liveUsage(run.usage, run.streaming.values()));
|
|
566
|
+
const input = usages.reduce((sum, usage) => sum + (usage?.input ?? 0), 0);
|
|
567
|
+
const output = usages.reduce((sum, usage) => sum + (usage?.output ?? 0), 0);
|
|
568
|
+
const cost = usages.reduce((sum, usage) => sum + (usage?.cost ?? 0), 0);
|
|
569
|
+
const pricing = runs.reduce((merged, run) => mergePricing(merged, run.pricing), undefined);
|
|
570
|
+
const costPart = costText(cost, pricing);
|
|
571
|
+
return [
|
|
572
|
+
`${runs.length} agent${runs.length === 1 ? "" : "s"}`,
|
|
468
573
|
...(output > 0
|
|
469
|
-
? [
|
|
574
|
+
? [tokenText(input, output), ...(costPart ? [costPart] : [])]
|
|
470
575
|
: []),
|
|
471
|
-
];
|
|
472
|
-
return parts.length > 0 ? parts.join(" · ") : undefined;
|
|
576
|
+
].join(" · ");
|
|
473
577
|
};
|
|
474
578
|
const watchAgents = () => {
|
|
475
579
|
if (agentsWidgetTimer)
|
|
@@ -566,7 +670,13 @@ export function createRecipesExtension(opts = {}) {
|
|
|
566
670
|
if (!run)
|
|
567
671
|
throw new Error(`Unknown agent run: ${id}`);
|
|
568
672
|
if (run.status === "running") {
|
|
569
|
-
|
|
673
|
+
// Its inbox keeps this behind older messages; a queued run gets it with its first prompt.
|
|
674
|
+
await run.inbox.deliver({
|
|
675
|
+
from: ROOT_AGENT,
|
|
676
|
+
fromId: ROOT_AGENT,
|
|
677
|
+
text: message,
|
|
678
|
+
deliver: "steer",
|
|
679
|
+
});
|
|
570
680
|
return controllerSummary(run);
|
|
571
681
|
}
|
|
572
682
|
run.status = "running";
|
|
@@ -585,9 +695,13 @@ export function createRecipesExtension(opts = {}) {
|
|
|
585
695
|
if (!run)
|
|
586
696
|
throw new Error(`Unknown agent run: ${id}`);
|
|
587
697
|
if (run.status === "running") {
|
|
698
|
+
const queued = run.queued;
|
|
588
699
|
run.status = "interrupted";
|
|
589
700
|
run.completedAt = run.completedAt ?? new Date().toISOString();
|
|
590
|
-
|
|
701
|
+
if (queued)
|
|
702
|
+
leaveChildQueue(run);
|
|
703
|
+
else
|
|
704
|
+
await run.runner.cancel();
|
|
591
705
|
if (state)
|
|
592
706
|
void persistRun(state.cwd, run);
|
|
593
707
|
}
|
|
@@ -601,13 +715,16 @@ export function createRecipesExtension(opts = {}) {
|
|
|
601
715
|
if (!run)
|
|
602
716
|
throw new Error(`Unknown agent run: ${id}`);
|
|
603
717
|
if (run.status === "running") {
|
|
718
|
+
const queued = run.queued;
|
|
604
719
|
run.status = "interrupted";
|
|
605
|
-
|
|
720
|
+
if (queued)
|
|
721
|
+
leaveChildQueue(run);
|
|
722
|
+
else
|
|
723
|
+
await run.runner.cancel();
|
|
606
724
|
await run.promise;
|
|
607
725
|
}
|
|
608
726
|
await run.runner.shutdown();
|
|
609
|
-
|
|
610
|
-
closedUsage.cost += run.usage?.cost ?? 0;
|
|
727
|
+
run.unregisterMessages();
|
|
611
728
|
childRuns.delete(id);
|
|
612
729
|
return controllerSummary(run, "closed");
|
|
613
730
|
},
|
|
@@ -616,7 +733,10 @@ export function createRecipesExtension(opts = {}) {
|
|
|
616
733
|
},
|
|
617
734
|
};
|
|
618
735
|
async function closeAllChildRuns() {
|
|
736
|
+
// Batches stop starting rows first, or a row closed here would make room for the next.
|
|
737
|
+
const stopping = agentBatches?.stopAll();
|
|
619
738
|
await Promise.all([...childRuns.keys()].map((id) => localRunController.close(id)));
|
|
739
|
+
await stopping;
|
|
620
740
|
}
|
|
621
741
|
async function closeRootMcpRuntime() {
|
|
622
742
|
const privateEnv = state?.mcpPrivateEnv;
|
|
@@ -1203,7 +1323,18 @@ export function createRecipesExtension(opts = {}) {
|
|
|
1203
1323
|
}
|
|
1204
1324
|
}
|
|
1205
1325
|
async function runChildAgent(launchState, agentName, prompt, label, ctx, onUpdate) {
|
|
1326
|
+
if (label !== undefined && messages.hasName(label)) {
|
|
1327
|
+
throw new Error(`An agent named "${label}" is already running in this session; pick another label.`);
|
|
1328
|
+
}
|
|
1206
1329
|
const id = nextChildRunId();
|
|
1330
|
+
const inbox = new AgentInbox(async (message) => {
|
|
1331
|
+
const text = renderAgentMessage(message);
|
|
1332
|
+
if (message.deliver === "followUp")
|
|
1333
|
+
await run.runner.followUp(text);
|
|
1334
|
+
else
|
|
1335
|
+
await run.runner.steer(text);
|
|
1336
|
+
});
|
|
1337
|
+
inbox.holdUntilStarted();
|
|
1207
1338
|
// `notifyUpdate` reads it and can run before the run record exists.
|
|
1208
1339
|
// eslint-disable-next-line prefer-const
|
|
1209
1340
|
let run;
|
|
@@ -1237,6 +1368,7 @@ export function createRecipesExtension(opts = {}) {
|
|
|
1237
1368
|
}
|
|
1238
1369
|
};
|
|
1239
1370
|
const runner = createChildAgentRunner({
|
|
1371
|
+
messages: { bus: messages, id, inbox },
|
|
1240
1372
|
recipe: launchState.resolvedRecipe,
|
|
1241
1373
|
workspaceDir: launchState.cwd,
|
|
1242
1374
|
env,
|
|
@@ -1245,14 +1377,20 @@ export function createRecipesExtension(opts = {}) {
|
|
|
1245
1377
|
agentRunId: id,
|
|
1246
1378
|
// Runs the child starts in turn arrive already attributed to it, and their usage counts as its own.
|
|
1247
1379
|
onAgentRunEvent(envelope) {
|
|
1248
|
-
if (run
|
|
1249
|
-
|
|
1380
|
+
if (run) {
|
|
1381
|
+
notePricing(run, envelope.event, ctx.modelRegistry);
|
|
1382
|
+
const started = streamFor(run, envelope.agent_run_id, envelope.event);
|
|
1383
|
+
if (addMessageUsage(run, envelope.event) || started)
|
|
1384
|
+
notifyUpdate();
|
|
1385
|
+
}
|
|
1250
1386
|
recordRunEvent(envelope);
|
|
1251
1387
|
},
|
|
1252
1388
|
onEvent(event) {
|
|
1253
1389
|
if (!run)
|
|
1254
1390
|
return;
|
|
1255
|
-
|
|
1391
|
+
notePricing(run, event, ctx.modelRegistry);
|
|
1392
|
+
const started = streamFor(run, run.id, event);
|
|
1393
|
+
if (addMessageUsage(run, event) || started)
|
|
1256
1394
|
notifyUpdate();
|
|
1257
1395
|
recordRunEvent({
|
|
1258
1396
|
type: "agent_run_event",
|
|
@@ -1290,9 +1428,28 @@ export function createRecipesExtension(opts = {}) {
|
|
|
1290
1428
|
status: "running",
|
|
1291
1429
|
startedAt: new Date().toISOString(),
|
|
1292
1430
|
toolCalls: [],
|
|
1431
|
+
streaming: new Map(),
|
|
1293
1432
|
runner,
|
|
1294
1433
|
promise: Promise.resolve(undefined),
|
|
1295
1434
|
notifyUpdate,
|
|
1435
|
+
inbox,
|
|
1436
|
+
// The batch marked this label for the run it is about to start; no later run inherits it.
|
|
1437
|
+
...(label !== undefined && quietLabels.delete(label)
|
|
1438
|
+
? { batchRow: true }
|
|
1439
|
+
: {}),
|
|
1440
|
+
unregisterMessages: messages.register({
|
|
1441
|
+
id,
|
|
1442
|
+
...(label !== undefined ? { name: label } : {}),
|
|
1443
|
+
inbox,
|
|
1444
|
+
accepting: () => run.status === "running",
|
|
1445
|
+
setStatus(note) {
|
|
1446
|
+
const current = run;
|
|
1447
|
+
// A note without usage keeps the last reported total.
|
|
1448
|
+
const usage = note.usage ?? current.statusNote?.usage;
|
|
1449
|
+
current.statusNote = { text: note.text, ...(usage ? { usage } : {}) };
|
|
1450
|
+
current.notifyUpdate();
|
|
1451
|
+
},
|
|
1452
|
+
}),
|
|
1296
1453
|
};
|
|
1297
1454
|
// Registered before the first update, so the widget sees it running and keeps its timer.
|
|
1298
1455
|
childRuns.set(id, run);
|
|
@@ -1303,9 +1460,19 @@ export function createRecipesExtension(opts = {}) {
|
|
|
1303
1460
|
}
|
|
1304
1461
|
function executeChildPrompt(run, prompt, cwd) {
|
|
1305
1462
|
return (async () => {
|
|
1463
|
+
let held = false;
|
|
1306
1464
|
try {
|
|
1465
|
+
held = await takeChildSlot(run);
|
|
1466
|
+
// Closed or interrupted while it waited for a slot.
|
|
1467
|
+
if (!held || run.status !== "running")
|
|
1468
|
+
return run;
|
|
1469
|
+
run.notifyUpdate();
|
|
1307
1470
|
await run.runner.start();
|
|
1308
|
-
|
|
1471
|
+
// Messages sent before the session was up come with its first prompt.
|
|
1472
|
+
const early = run.inbox.started();
|
|
1473
|
+
const result = await run.runner.prompt(early.length > 0
|
|
1474
|
+
? `${prompt}\n\n${renderAgentMessages(early)}`
|
|
1475
|
+
: prompt);
|
|
1309
1476
|
const finalOutput = promptResultText(result);
|
|
1310
1477
|
// The streamed buffer spans every turn the child took (a nested
|
|
1311
1478
|
// child also takes a turn per batch of its own results), so the
|
|
@@ -1321,7 +1488,11 @@ export function createRecipesExtension(opts = {}) {
|
|
|
1321
1488
|
}
|
|
1322
1489
|
}
|
|
1323
1490
|
finally {
|
|
1491
|
+
if (held)
|
|
1492
|
+
freeChildSlot();
|
|
1324
1493
|
run.completedAt = new Date().toISOString();
|
|
1494
|
+
// A message cut off before its end has no real total to replace its estimate.
|
|
1495
|
+
run.streaming.clear();
|
|
1325
1496
|
// Queue the parent wake-up BEFORE persisting. The deliverer reads the
|
|
1326
1497
|
// in-memory queue (not disk), so enqueuing first makes a persisted
|
|
1327
1498
|
// status.json imply the completion is already queued — closing a race
|
|
@@ -1330,7 +1501,8 @@ export function createRecipesExtension(opts = {}) {
|
|
|
1330
1501
|
// still notifies if the disk write fails. Wait and terminal status
|
|
1331
1502
|
// reads acknowledge it back out.
|
|
1332
1503
|
const envelope = envelopeFromRun(run);
|
|
1333
|
-
|
|
1504
|
+
// A batch row is reported in its batch's results, not on its own.
|
|
1505
|
+
if (envelope && !run.batchRow)
|
|
1334
1506
|
completions.enqueue(envelope);
|
|
1335
1507
|
run.notifyUpdate();
|
|
1336
1508
|
await persistRun(cwd, run);
|
|
@@ -1370,7 +1542,7 @@ export function createRecipesExtension(opts = {}) {
|
|
|
1370
1542
|
clearTimeout(deliveryRetryTimer);
|
|
1371
1543
|
deliveryRetryTimer = null;
|
|
1372
1544
|
}
|
|
1373
|
-
if (!completions.hasPending())
|
|
1545
|
+
if (!completions.hasPending() && !rootInbox.hasHeld())
|
|
1374
1546
|
return;
|
|
1375
1547
|
// The retry timer can outlive this extension instance across a reload,
|
|
1376
1548
|
// where pi API calls throw as stale. Never crash the timer callback;
|
|
@@ -1381,6 +1553,8 @@ export function createRecipesExtension(opts = {}) {
|
|
|
1381
1553
|
deliveryRetryTimer.unref?.();
|
|
1382
1554
|
return;
|
|
1383
1555
|
}
|
|
1556
|
+
// Follow-ups held while root was busy, in send order, go the same idle-gated way.
|
|
1557
|
+
rootInbox.flush().catch(() => { });
|
|
1384
1558
|
const batch = completions.consumeBatch();
|
|
1385
1559
|
if (batch.length === 0)
|
|
1386
1560
|
return;
|
|
@@ -1401,7 +1575,10 @@ export function createRecipesExtension(opts = {}) {
|
|
|
1401
1575
|
completions.setDeliverer(deliverCompletions);
|
|
1402
1576
|
pi.on("agent_end", (_event, ctx) => {
|
|
1403
1577
|
sessionCtx = ctx;
|
|
1404
|
-
|
|
1578
|
+
if (rootInbox.hasHeld())
|
|
1579
|
+
deliverCompletions();
|
|
1580
|
+
else
|
|
1581
|
+
completions.poke();
|
|
1405
1582
|
});
|
|
1406
1583
|
// Rendered from the session's entries for now; these should become
|
|
1407
1584
|
// `instrumentSession` spans: https://github.com/introspection-org/recipes/issues/340
|
|
@@ -1413,6 +1590,12 @@ export function createRecipesExtension(opts = {}) {
|
|
|
1413
1590
|
return undefined;
|
|
1414
1591
|
return applyRecipeAgentPayloadPolicy(event.payload, ctx.model, state.resolved.modelConfig);
|
|
1415
1592
|
});
|
|
1593
|
+
pi.registerMessageRenderer(AGENT_RUN_MESSAGE_TYPE, (message, _options, theme) => {
|
|
1594
|
+
const sent = message.details;
|
|
1595
|
+
if (!sent)
|
|
1596
|
+
return undefined;
|
|
1597
|
+
return new Text(`${theme.fg("accent", `message from ${sent.from}`)} ${theme.fg("muted", sent.fromId)}\n${sent.text}`, 0, 0);
|
|
1598
|
+
});
|
|
1416
1599
|
pi.registerMessageRenderer(AGENT_COMPLETIONS_TYPE, (message, options, theme) => {
|
|
1417
1600
|
const batch = message.details
|
|
1418
1601
|
?.completions;
|
|
@@ -1491,8 +1674,19 @@ export function createRecipesExtension(opts = {}) {
|
|
|
1491
1674
|
ctx.ui.notify(recipeSummary(launchState, pi.getActiveTools()), "info");
|
|
1492
1675
|
},
|
|
1493
1676
|
});
|
|
1677
|
+
agentBatches = createAgentBatches({
|
|
1678
|
+
controller: localRunController,
|
|
1679
|
+
runBatch,
|
|
1680
|
+
cwd: () => state?.cwd ?? process.cwd(),
|
|
1681
|
+
quiet: (label) => {
|
|
1682
|
+
quietLabels.add(label);
|
|
1683
|
+
return () => quietLabels.delete(label);
|
|
1684
|
+
},
|
|
1685
|
+
});
|
|
1494
1686
|
const agentTool = createAgentTool(localRunController, visibleAgentDefinitions, {
|
|
1495
1687
|
acknowledgeCompletions: (ids) => completions.acknowledge(ids),
|
|
1688
|
+
inbox: rootInbox,
|
|
1689
|
+
batches: agentBatches,
|
|
1496
1690
|
});
|
|
1497
1691
|
agentTool.label = "Agent";
|
|
1498
1692
|
agentTool.renderCall = (params, theme, context) => {
|