@introspection-ai/recipes 0.63.0 → 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 +30 -6
- package/dist/agents.d.ts.map +1 -1
- package/dist/agents.js +283 -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 +263 -52
- 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/host-api.md +4 -3
- package/docs/pi-extension.md +4 -1
- package/docs/productivity/google.md +27 -15
- package/package.json +6 -6
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Messages between the agents of one Pi session: the root agent and the runs
|
|
3
|
+
* it started, at every depth. A run sends one with `send_message`, naming its
|
|
4
|
+
* recipient as `root` or another run's name (its label) or id, as Claude
|
|
5
|
+
* Code's teammates use `SendMessage`.
|
|
6
|
+
*
|
|
7
|
+
* A message reaches an agent that is waiting on its runs at once, whichever
|
|
8
|
+
* run it waits on: Pi delivers a steer only after the current tool call ends,
|
|
9
|
+
* and an `agent` wait blocks until its run settles, so a steer to a waiting
|
|
10
|
+
* agent would never arrive. Otherwise it is steered into the agent's turn, or
|
|
11
|
+
* starts one.
|
|
12
|
+
*/
|
|
13
|
+
import { type ToolDefinition } from "@earendil-works/pi-coding-agent";
|
|
14
|
+
/** Pi custom message type of a message delivered to the root agent. */
|
|
15
|
+
export declare const AGENT_RUN_MESSAGE_TYPE = "agent_run_message";
|
|
16
|
+
/** The name the root agent is addressed by. */
|
|
17
|
+
export declare const ROOT_AGENT = "root";
|
|
18
|
+
/** A message one agent sent another. */
|
|
19
|
+
export interface AgentRunMessage {
|
|
20
|
+
/** The sender's name: its run label, or `root`. */
|
|
21
|
+
from: string;
|
|
22
|
+
/** The sender's run id, `root` for the root agent. */
|
|
23
|
+
fromId: string;
|
|
24
|
+
text: string;
|
|
25
|
+
/**
|
|
26
|
+
* How it reaches an agent that is not waiting: `steer`, before its next
|
|
27
|
+
* model call, or `followUp`, once it finishes its current work. A wait in
|
|
28
|
+
* progress takes either at once.
|
|
29
|
+
*/
|
|
30
|
+
deliver: AgentMessageDelivery;
|
|
31
|
+
}
|
|
32
|
+
/** The ways a message can reach an agent that is not waiting. */
|
|
33
|
+
export type AgentMessageDelivery = "steer" | "followUp";
|
|
34
|
+
/**
|
|
35
|
+
* A run's own progress note, from `send_message` with `deliver: "status"`: shown
|
|
36
|
+
* on its line for the person, never to an agent. `usage` is the running total
|
|
37
|
+
* of work it ran elsewhere, such as cloud trials, apart from its own model calls.
|
|
38
|
+
*/
|
|
39
|
+
export interface AgentStatusNote {
|
|
40
|
+
text: string;
|
|
41
|
+
usage?: ReportedUsage;
|
|
42
|
+
}
|
|
43
|
+
/** Usage a run reports for work it ran elsewhere; each report replaces the last. */
|
|
44
|
+
export interface ReportedUsage {
|
|
45
|
+
input_tokens: number;
|
|
46
|
+
output_tokens: number;
|
|
47
|
+
cost_usd: number;
|
|
48
|
+
}
|
|
49
|
+
/** The model-facing text of a delivered message. */
|
|
50
|
+
export declare function renderAgentMessage(message: AgentRunMessage): string;
|
|
51
|
+
/** Several messages, in the order they were sent. */
|
|
52
|
+
export declare function renderAgentMessages(messages: readonly AgentRunMessage[]): string;
|
|
53
|
+
/**
|
|
54
|
+
* One agent's incoming messages, kept in send order. A message to an agent in
|
|
55
|
+
* a wait ends it with every held message before it. With none waiting, a steer
|
|
56
|
+
* goes to the agent's session now; a follow-up to a busy agent is held until
|
|
57
|
+
* its next wait ends or its turn does, so it cannot land after a message sent
|
|
58
|
+
* later.
|
|
59
|
+
*/
|
|
60
|
+
export declare class AgentInbox {
|
|
61
|
+
private readonly held;
|
|
62
|
+
private readonly waiters;
|
|
63
|
+
private readonly send;
|
|
64
|
+
private readonly busy;
|
|
65
|
+
private starting;
|
|
66
|
+
private delivering;
|
|
67
|
+
/**
|
|
68
|
+
* `send` hands one message to the agent's session; `busy` says whether it is
|
|
69
|
+
* mid-turn, where a follow-up handed over now would land late.
|
|
70
|
+
*/
|
|
71
|
+
constructor(send: (message: AgentRunMessage) => Promise<void>, busy?: () => boolean);
|
|
72
|
+
/** Hold every message until `started`: the agent's session is not there yet to take one. */
|
|
73
|
+
holdUntilStarted(): void;
|
|
74
|
+
/** The agent's session is up: the messages held until now, in send order, for its first prompt. */
|
|
75
|
+
started(): AgentRunMessage[];
|
|
76
|
+
/** Give the oldest wait every held message and this one, else send or hold it, in send order. */
|
|
77
|
+
deliver(message: AgentRunMessage): Promise<void>;
|
|
78
|
+
private deliverNow;
|
|
79
|
+
/**
|
|
80
|
+
* Put back messages a wait took but did not report, because what it waited on
|
|
81
|
+
* settled first: they are held again, ahead of anything held since.
|
|
82
|
+
*/
|
|
83
|
+
restore(messages: readonly AgentRunMessage[]): void;
|
|
84
|
+
/** Whether messages are held for the end of the agent's turn. */
|
|
85
|
+
hasHeld(): boolean;
|
|
86
|
+
/** The held messages, in send order, removed from the inbox. */
|
|
87
|
+
take(): AgentRunMessage[];
|
|
88
|
+
/** Send the held messages to the agent's session, at the end of its turn, in turn with deliveries. */
|
|
89
|
+
flush(): Promise<void>;
|
|
90
|
+
/**
|
|
91
|
+
* The messages delivered while this is pending, held ones first, or
|
|
92
|
+
* `undefined` once `signal` aborts. Held follow-ups alone don't end it: the
|
|
93
|
+
* wait they ride on is the agent's current work. Stop listening by aborting.
|
|
94
|
+
*/
|
|
95
|
+
next(signal: AbortSignal): Promise<AgentRunMessage[] | undefined>;
|
|
96
|
+
}
|
|
97
|
+
/** An agent that can receive messages. */
|
|
98
|
+
export interface AgentMessageRecipient {
|
|
99
|
+
/** Run id, or `root`. */
|
|
100
|
+
id: string;
|
|
101
|
+
/** The name it is addressed by: its run label, or `root`. */
|
|
102
|
+
name?: string;
|
|
103
|
+
inbox: AgentInbox;
|
|
104
|
+
/** Whether it can take a message now: a settled run cannot. */
|
|
105
|
+
accepting(): boolean;
|
|
106
|
+
/** Show the note on this agent's line, in place of the tool it is in. Root has none. */
|
|
107
|
+
setStatus?(note: AgentStatusNote): void;
|
|
108
|
+
}
|
|
109
|
+
/** Every agent of a session that can be messaged, by id and by name. */
|
|
110
|
+
export declare class AgentMessageBus {
|
|
111
|
+
private readonly byId;
|
|
112
|
+
private readonly byName;
|
|
113
|
+
/** Whether a live agent already goes by `name`. */
|
|
114
|
+
hasName(name: string): boolean;
|
|
115
|
+
/** Add an agent; a name already in use is refused. Returns its removal. */
|
|
116
|
+
register(recipient: AgentMessageRecipient): () => void;
|
|
117
|
+
/** The line a run's system prompt carries so it knows how it is addressed. */
|
|
118
|
+
introduce(id: string): string;
|
|
119
|
+
/** The name a sender signs a message with: its name, else its id. */
|
|
120
|
+
nameOf(id: string): string;
|
|
121
|
+
/** Set the agent `id`'s own status note; it reaches no other agent. */
|
|
122
|
+
setStatus(id: string, note: AgentStatusNote): void;
|
|
123
|
+
/** Send `text` from the agent `fromId` to `to`, a name or a run id. */
|
|
124
|
+
send(fromId: string, to: string, text: string, deliver?: AgentMessageDelivery): Promise<string>;
|
|
125
|
+
}
|
|
126
|
+
/** The `send_message` tool of the agent `senderId`. */
|
|
127
|
+
export declare function createSendMessageTool(bus: AgentMessageBus, senderId: string): ToolDefinition;
|
|
128
|
+
//# sourceMappingURL=messages.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../../src/child/messages.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAEL,KAAK,cAAc,EACpB,MAAM,iCAAiC,CAAC;AAKzC,uEAAuE;AACvE,eAAO,MAAM,sBAAsB,sBAAsB,CAAC;AAE1D,+CAA+C;AAC/C,eAAO,MAAM,UAAU,SAAS,CAAC;AAEjC,wCAAwC;AACxC,MAAM,WAAW,eAAe;IAC9B,mDAAmD;IACnD,IAAI,EAAE,MAAM,CAAC;IACb,sDAAsD;IACtD,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb;;;;OAIG;IACH,OAAO,EAAE,oBAAoB,CAAC;CAC/B;AAED,iEAAiE;AACjE,MAAM,MAAM,oBAAoB,GAAG,OAAO,GAAG,UAAU,CAAC;AAExD;;;;GAIG;AACH,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,aAAa,CAAC;CACvB;AAED,oFAAoF;AACpF,MAAM,WAAW,aAAa;IAC5B,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,EAAE,MAAM,CAAC;IACtB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,oDAAoD;AACpD,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,eAAe,GAAG,MAAM,CAMnE;AAED,qDAAqD;AACrD,wBAAgB,mBAAmB,CACjC,QAAQ,EAAE,SAAS,eAAe,EAAE,GACnC,MAAM,CAER;AAED;;;;;;GAMG;AACH,qBAAa,UAAU;IACrB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAyB;IAC9C,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAoD;IAC5E,OAAO,CAAC,QAAQ,CAAC,IAAI,CAA8C;IACnE,OAAO,CAAC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,GAAG,OAAO,CAE7B;IAED,2EAA2E;IAC3E,QAAQ,CAAC,SAAS,EAAE,qBAAqB,GAAG,MAAM,IAAI,CAiCrD;IAED,8EAA8E;IAC9E,SAAS,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,CAG5B;IAED,qEAAqE;IACrE,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,CAEzB;IAED,uEAAuE;IACvE,SAAS,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,eAAe,GAAG,IAAI,CAEjD;IAED,uEAAuE;IACjE,IAAI,CACR,MAAM,EAAE,MAAM,EACd,EAAE,EAAE,MAAM,EACV,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,oBAA8B,GACtC,OAAO,CAAC,MAAM,CAAC,CA0BjB;CACF;AAED,uDAAuD;AACvD,wBAAgB,qBAAqB,CACnC,GAAG,EAAE,eAAe,EACpB,QAAQ,EAAE,MAAM,GACf,cAAc,CA2DhB"}
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Messages between the agents of one Pi session: the root agent and the runs
|
|
3
|
+
* it started, at every depth. A run sends one with `send_message`, naming its
|
|
4
|
+
* recipient as `root` or another run's name (its label) or id, as Claude
|
|
5
|
+
* Code's teammates use `SendMessage`.
|
|
6
|
+
*
|
|
7
|
+
* A message reaches an agent that is waiting on its runs at once, whichever
|
|
8
|
+
* run it waits on: Pi delivers a steer only after the current tool call ends,
|
|
9
|
+
* and an `agent` wait blocks until its run settles, so a steer to a waiting
|
|
10
|
+
* agent would never arrive. Otherwise it is steered into the agent's turn, or
|
|
11
|
+
* starts one.
|
|
12
|
+
*/
|
|
13
|
+
import { defineTool, } from "@earendil-works/pi-coding-agent";
|
|
14
|
+
import { SEND_MESSAGE_ARGUMENTS } from "./arguments.generated.js";
|
|
15
|
+
/** Pi custom message type of a message delivered to the root agent. */
|
|
16
|
+
export const AGENT_RUN_MESSAGE_TYPE = "agent_run_message";
|
|
17
|
+
/** The name the root agent is addressed by. */
|
|
18
|
+
export const ROOT_AGENT = "root";
|
|
19
|
+
/** The model-facing text of a delivered message. */
|
|
20
|
+
export function renderAgentMessage(message) {
|
|
21
|
+
return [
|
|
22
|
+
`<agent_message from="${message.from}" id="${message.fromId}">`,
|
|
23
|
+
message.text,
|
|
24
|
+
"</agent_message>",
|
|
25
|
+
].join("\n");
|
|
26
|
+
}
|
|
27
|
+
/** Several messages, in the order they were sent. */
|
|
28
|
+
export function renderAgentMessages(messages) {
|
|
29
|
+
return messages.map(renderAgentMessage).join("\n\n");
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* One agent's incoming messages, kept in send order. A message to an agent in
|
|
33
|
+
* a wait ends it with every held message before it. With none waiting, a steer
|
|
34
|
+
* goes to the agent's session now; a follow-up to a busy agent is held until
|
|
35
|
+
* its next wait ends or its turn does, so it cannot land after a message sent
|
|
36
|
+
* later.
|
|
37
|
+
*/
|
|
38
|
+
export class AgentInbox {
|
|
39
|
+
held = [];
|
|
40
|
+
waiters = [];
|
|
41
|
+
send;
|
|
42
|
+
busy;
|
|
43
|
+
starting = false;
|
|
44
|
+
// Deliveries run one at a time, so a later sender never overtakes an earlier one mid-send.
|
|
45
|
+
delivering = Promise.resolve();
|
|
46
|
+
/**
|
|
47
|
+
* `send` hands one message to the agent's session; `busy` says whether it is
|
|
48
|
+
* mid-turn, where a follow-up handed over now would land late.
|
|
49
|
+
*/
|
|
50
|
+
constructor(send, busy = () => true) {
|
|
51
|
+
this.send = send;
|
|
52
|
+
this.busy = busy;
|
|
53
|
+
}
|
|
54
|
+
/** Hold every message until `started`: the agent's session is not there yet to take one. */
|
|
55
|
+
holdUntilStarted() {
|
|
56
|
+
this.starting = true;
|
|
57
|
+
}
|
|
58
|
+
/** The agent's session is up: the messages held until now, in send order, for its first prompt. */
|
|
59
|
+
started() {
|
|
60
|
+
this.starting = false;
|
|
61
|
+
return this.take();
|
|
62
|
+
}
|
|
63
|
+
/** Give the oldest wait every held message and this one, else send or hold it, in send order. */
|
|
64
|
+
deliver(message) {
|
|
65
|
+
const delivered = this.delivering.then(() => this.deliverNow(message));
|
|
66
|
+
this.delivering = delivered.catch(() => { });
|
|
67
|
+
return delivered;
|
|
68
|
+
}
|
|
69
|
+
async deliverNow(message) {
|
|
70
|
+
if (this.starting) {
|
|
71
|
+
this.held.push(message);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const waiter = this.waiters.shift();
|
|
75
|
+
if (waiter) {
|
|
76
|
+
waiter([...this.take(), message]);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (message.deliver === "followUp" && this.busy()) {
|
|
80
|
+
this.held.push(message);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
// Older held follow-ups go first, as this one is delivered, so none lands after it.
|
|
84
|
+
for (const held of this.take())
|
|
85
|
+
await this.send({ ...held, deliver: message.deliver });
|
|
86
|
+
await this.send(message);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Put back messages a wait took but did not report, because what it waited on
|
|
90
|
+
* settled first: they are held again, ahead of anything held since.
|
|
91
|
+
*/
|
|
92
|
+
restore(messages) {
|
|
93
|
+
this.held.unshift(...messages);
|
|
94
|
+
}
|
|
95
|
+
/** Whether messages are held for the end of the agent's turn. */
|
|
96
|
+
hasHeld() {
|
|
97
|
+
return this.held.length > 0;
|
|
98
|
+
}
|
|
99
|
+
/** The held messages, in send order, removed from the inbox. */
|
|
100
|
+
take() {
|
|
101
|
+
return this.held.splice(0);
|
|
102
|
+
}
|
|
103
|
+
/** Send the held messages to the agent's session, at the end of its turn, in turn with deliveries. */
|
|
104
|
+
flush() {
|
|
105
|
+
const flushed = this.delivering.then(async () => {
|
|
106
|
+
for (const message of this.take())
|
|
107
|
+
await this.send(message);
|
|
108
|
+
});
|
|
109
|
+
this.delivering = flushed.catch(() => { });
|
|
110
|
+
return flushed;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* The messages delivered while this is pending, held ones first, or
|
|
114
|
+
* `undefined` once `signal` aborts. Held follow-ups alone don't end it: the
|
|
115
|
+
* wait they ride on is the agent's current work. Stop listening by aborting.
|
|
116
|
+
*/
|
|
117
|
+
next(signal) {
|
|
118
|
+
if (signal.aborted)
|
|
119
|
+
return Promise.resolve(undefined);
|
|
120
|
+
return new Promise((resolve) => {
|
|
121
|
+
const take = (messages) => {
|
|
122
|
+
signal.removeEventListener("abort", stop);
|
|
123
|
+
resolve(messages);
|
|
124
|
+
};
|
|
125
|
+
const stop = () => {
|
|
126
|
+
const index = this.waiters.indexOf(take);
|
|
127
|
+
if (index >= 0)
|
|
128
|
+
this.waiters.splice(index, 1);
|
|
129
|
+
resolve(undefined);
|
|
130
|
+
};
|
|
131
|
+
this.waiters.push(take);
|
|
132
|
+
signal.addEventListener("abort", stop, { once: true });
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
// The form of a run id, which a label may not take.
|
|
137
|
+
const RUN_ID = /^agent-run-\d+$/;
|
|
138
|
+
/** Every agent of a session that can be messaged, by id and by name. */
|
|
139
|
+
export class AgentMessageBus {
|
|
140
|
+
byId = new Map();
|
|
141
|
+
byName = new Map();
|
|
142
|
+
/** Whether a live agent already goes by `name`. */
|
|
143
|
+
hasName(name) {
|
|
144
|
+
return this.byName.has(name);
|
|
145
|
+
}
|
|
146
|
+
/** Add an agent; a name already in use is refused. Returns its removal. */
|
|
147
|
+
register(recipient) {
|
|
148
|
+
if (recipient.name !== undefined && this.byName.has(recipient.name)) {
|
|
149
|
+
throw new Error(`An agent named "${recipient.name}" is already running in this session; pick another label.`);
|
|
150
|
+
}
|
|
151
|
+
// A name resolves before an id, so neither may stand for the other's run.
|
|
152
|
+
if (recipient.name !== undefined &&
|
|
153
|
+
recipient.name !== recipient.id &&
|
|
154
|
+
(RUN_ID.test(recipient.name) || this.byId.has(recipient.name))) {
|
|
155
|
+
throw new Error(`"${recipient.name}" reads as a run id; pick a label that does not.`);
|
|
156
|
+
}
|
|
157
|
+
if (this.byName.has(recipient.id)) {
|
|
158
|
+
throw new Error(`A run is labelled "${recipient.id}", which is this run's id; pick another label for it.`);
|
|
159
|
+
}
|
|
160
|
+
this.byId.set(recipient.id, recipient);
|
|
161
|
+
if (recipient.name !== undefined)
|
|
162
|
+
this.byName.set(recipient.name, recipient.id);
|
|
163
|
+
return () => {
|
|
164
|
+
if (this.byId.get(recipient.id) !== recipient)
|
|
165
|
+
return;
|
|
166
|
+
this.byId.delete(recipient.id);
|
|
167
|
+
if (recipient.name !== undefined &&
|
|
168
|
+
this.byName.get(recipient.name) === recipient.id)
|
|
169
|
+
this.byName.delete(recipient.name);
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
/** The line a run's system prompt carries so it knows how it is addressed. */
|
|
173
|
+
introduce(id) {
|
|
174
|
+
const name = this.byId.get(id)?.name;
|
|
175
|
+
return `Your name is ${name ?? id}${name ? ` (run ${id})` : ""}. Sign messages with your name; send_message's \`to\` is ${ROOT_AGENT} or another run's name.`;
|
|
176
|
+
}
|
|
177
|
+
/** The name a sender signs a message with: its name, else its id. */
|
|
178
|
+
nameOf(id) {
|
|
179
|
+
return this.byId.get(id)?.name ?? id;
|
|
180
|
+
}
|
|
181
|
+
/** Set the agent `id`'s own status note; it reaches no other agent. */
|
|
182
|
+
setStatus(id, note) {
|
|
183
|
+
this.byId.get(id)?.setStatus?.(note);
|
|
184
|
+
}
|
|
185
|
+
/** Send `text` from the agent `fromId` to `to`, a name or a run id. */
|
|
186
|
+
async send(fromId, to, text, deliver = "steer") {
|
|
187
|
+
const recipientId = this.byName.get(to) ?? to;
|
|
188
|
+
const recipient = this.byId.get(recipientId);
|
|
189
|
+
if (!recipient || recipientId === fromId) {
|
|
190
|
+
const others = [...this.byId.values()]
|
|
191
|
+
.filter((agent) => agent.id !== fromId && agent.accepting())
|
|
192
|
+
.map((agent) => agent.name ?? agent.id);
|
|
193
|
+
const reason = recipient
|
|
194
|
+
? "You cannot message yourself"
|
|
195
|
+
: `No agent "${to}"`;
|
|
196
|
+
throw new Error(`${reason}. You can message: ${others.length ? others.join(", ") : "nobody"}.`);
|
|
197
|
+
}
|
|
198
|
+
if (!recipient.accepting()) {
|
|
199
|
+
throw new Error(`"${to}" has finished and takes no messages; it was started by its parent, which can resume it.`);
|
|
200
|
+
}
|
|
201
|
+
await recipient.inbox.deliver({
|
|
202
|
+
from: this.nameOf(fromId),
|
|
203
|
+
fromId,
|
|
204
|
+
text,
|
|
205
|
+
deliver,
|
|
206
|
+
});
|
|
207
|
+
return recipient.name ?? recipient.id;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
/** The `send_message` tool of the agent `senderId`. */
|
|
211
|
+
export function createSendMessageTool(bus, senderId) {
|
|
212
|
+
return defineTool({
|
|
213
|
+
name: "send_message",
|
|
214
|
+
label: "Send message",
|
|
215
|
+
description: [
|
|
216
|
+
`Send a message to another agent of this session while you keep working: "${ROOT_AGENT}" is the agent that started this work.`,
|
|
217
|
+
"It arrives at once, even while they wait on you, so use it for what they need to know before you finish: a result that changes their plan, a failure, a question.",
|
|
218
|
+
"Your final answer still reaches them when you finish; don't repeat it here.",
|
|
219
|
+
].join(" "),
|
|
220
|
+
parameters: SEND_MESSAGE_ARGUMENTS,
|
|
221
|
+
async execute(_callId, rawParams) {
|
|
222
|
+
const params = rawParams;
|
|
223
|
+
try {
|
|
224
|
+
if (params.deliver === "status") {
|
|
225
|
+
bus.setStatus(senderId, {
|
|
226
|
+
text: params.message,
|
|
227
|
+
...(params.usage
|
|
228
|
+
? {
|
|
229
|
+
usage: {
|
|
230
|
+
input_tokens: params.usage.input,
|
|
231
|
+
output_tokens: params.usage.output,
|
|
232
|
+
cost_usd: params.usage.cost,
|
|
233
|
+
},
|
|
234
|
+
}
|
|
235
|
+
: {}),
|
|
236
|
+
});
|
|
237
|
+
return {
|
|
238
|
+
content: [{ type: "text", text: "Status set." }],
|
|
239
|
+
details: { to: params.to },
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
if (params.usage)
|
|
243
|
+
throw new Error('usage goes with deliver "status" only: it reports work you ran elsewhere, on your own line.');
|
|
244
|
+
const to = await bus.send(senderId, params.to, params.message, params.deliver);
|
|
245
|
+
return {
|
|
246
|
+
content: [{ type: "text", text: `Sent to ${to}.` }],
|
|
247
|
+
details: { to },
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
catch (error) {
|
|
251
|
+
return {
|
|
252
|
+
content: [
|
|
253
|
+
{
|
|
254
|
+
type: "text",
|
|
255
|
+
text: error instanceof Error ? error.message : String(error),
|
|
256
|
+
},
|
|
257
|
+
],
|
|
258
|
+
details: { to: params.to },
|
|
259
|
+
isError: true,
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
},
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
//# sourceMappingURL=messages.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"messages.js","sourceRoot":"","sources":["../../src/child/messages.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EACL,UAAU,GAEX,MAAM,iCAAiC,CAAC;AAGzC,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAElE,uEAAuE;AACvE,MAAM,CAAC,MAAM,sBAAsB,GAAG,mBAAmB,CAAC;AAE1D,+CAA+C;AAC/C,MAAM,CAAC,MAAM,UAAU,GAAG,MAAM,CAAC;AAqCjC,oDAAoD;AACpD,MAAM,UAAU,kBAAkB,CAAC,OAAwB;IACzD,OAAO;QACL,wBAAwB,OAAO,CAAC,IAAI,SAAS,OAAO,CAAC,MAAM,IAAI;QAC/D,OAAO,CAAC,IAAI;QACZ,kBAAkB;KACnB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACf,CAAC;AAED,qDAAqD;AACrD,MAAM,UAAU,mBAAmB,CACjC,QAAoC;IAEpC,OAAO,QAAQ,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAA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model } from "@earendil-works/pi-ai";
|
|
2
2
|
import type { AgentSessionEvent, ModelRegistry, SettingsManager } from "@earendil-works/pi-coding-agent";
|
|
3
3
|
import { type AgentRunEventObserver } from "../agents.js";
|
|
4
|
+
import { type AgentInbox, type AgentMessageBus } from "./messages.js";
|
|
4
5
|
import type { ResolvedRecipe } from "../recipe/resolve.js";
|
|
5
6
|
import type { ApiLocalConfig } from "../api-bindings.js";
|
|
6
7
|
import type { RecipeClockOptions } from "../clock.js";
|
|
@@ -32,6 +33,15 @@ export interface CreateIsolatedChildSessionOptions {
|
|
|
32
33
|
depth?: number;
|
|
33
34
|
/** Observe events from runs this child starts in turn. */
|
|
34
35
|
onAgentRunEvent?: AgentRunEventObserver;
|
|
36
|
+
/**
|
|
37
|
+
* The session's agents, this child's run id and its inbox: the child gets
|
|
38
|
+
* `send_message`, and runs it starts join the same bus.
|
|
39
|
+
*/
|
|
40
|
+
messages?: {
|
|
41
|
+
bus: AgentMessageBus;
|
|
42
|
+
id: string;
|
|
43
|
+
inbox: AgentInbox;
|
|
44
|
+
};
|
|
35
45
|
/**
|
|
36
46
|
* Pi's model registry, when the child was bound through it: the runs this
|
|
37
47
|
* child starts are bound the same way, each for its own model and provider,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/child/session.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,GAAG,EAAE,eAAe,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AACzE,OAAO,KAAK,EACV,iBAAiB,EACjB,aAAa,EACb,eAAe,EAChB,MAAM,iCAAiC,CAAC;AAGzC,OAAO,EAGL,KAAK,qBAAqB,EAC3B,MAAM,cAAc,CAAC;AAGtB,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC3D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,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AAC,0BAA0B,CAAC,CAiIrC"}
|
package/dist/child/session.js
CHANGED
|
@@ -6,7 +6,13 @@ import { applyRecipeAgentModelConfigToModel } from "../recipe/model.js";
|
|
|
6
6
|
import { MAX_AGENT_RUN_DEPTH, createDelegatedRuns, } from "../agents.js";
|
|
7
7
|
import { autoResolveInteractions } from "../interactions.js";
|
|
8
8
|
import { promptResultError } from "./agent.js";
|
|
9
|
+
import { createSendMessageTool, } from "./messages.js";
|
|
9
10
|
import { createAgentSessionInternal, } from "../session.js";
|
|
11
|
+
/** Appends the run's own name to its system prompt, once, so it can sign and address messages. */
|
|
12
|
+
function introduceRun(messages) {
|
|
13
|
+
const line = messages.bus.introduce(messages.id);
|
|
14
|
+
return (resolved) => (resolved ? `${resolved}\n\n${line}` : line);
|
|
15
|
+
}
|
|
10
16
|
/**
|
|
11
17
|
* Shared child-session primitive used by both the embedded and interactive Pi
|
|
12
18
|
* controllers. It owns the child's private MCP state. A child below
|
|
@@ -17,7 +23,7 @@ export async function createIsolatedChildSession(opts) {
|
|
|
17
23
|
const depth = opts.depth ?? 1;
|
|
18
24
|
const delegates = depth < MAX_AGENT_RUN_DEPTH &&
|
|
19
25
|
opts.recipe.selectAgent(opts.agentName).subagents.size > 0;
|
|
20
|
-
const delegated = createDelegatedRuns();
|
|
26
|
+
const delegated = createDelegatedRuns(opts.messages?.inbox);
|
|
21
27
|
const registry = opts.modelRegistry;
|
|
22
28
|
const nestedFactory = registry
|
|
23
29
|
? async (options) => {
|
|
@@ -65,6 +71,12 @@ export async function createIsolatedChildSession(opts) {
|
|
|
65
71
|
...(opts.onEvent ? { onEvent: opts.onEvent } : {}),
|
|
66
72
|
mcpRuntimeDir,
|
|
67
73
|
sessionRole: "subagent",
|
|
74
|
+
...(opts.messages
|
|
75
|
+
? {
|
|
76
|
+
sendMessageTool: createSendMessageTool(opts.messages.bus, opts.messages.id),
|
|
77
|
+
transformSystemPrompt: introduceRun(opts.messages),
|
|
78
|
+
}
|
|
79
|
+
: {}),
|
|
68
80
|
...(delegates
|
|
69
81
|
? {
|
|
70
82
|
agentRun: {
|
|
@@ -75,8 +87,15 @@ export async function createIsolatedChildSession(opts) {
|
|
|
75
87
|
...(opts.onAgentRunEvent
|
|
76
88
|
? { onAgentRunEvent: opts.onAgentRunEvent }
|
|
77
89
|
: {}),
|
|
78
|
-
...(opts.concurrency !== undefined
|
|
79
|
-
? {
|
|
90
|
+
...(opts.concurrency !== undefined || opts.messages
|
|
91
|
+
? {
|
|
92
|
+
inProcessRunController: {
|
|
93
|
+
...(opts.concurrency !== undefined
|
|
94
|
+
? { concurrency: opts.concurrency }
|
|
95
|
+
: {}),
|
|
96
|
+
...(opts.messages ? { messages: opts.messages.bus } : {}),
|
|
97
|
+
},
|
|
98
|
+
}
|
|
80
99
|
: {}),
|
|
81
100
|
...(nestedFactory ? { sessionFactory: nestedFactory } : {}),
|
|
82
101
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.js","sourceRoot":"","sources":["../../src/child/session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAOjC,OAAO,EAAE,wBAAwB,EAAE,MAAM,qBAAqB,CAAC;AAC/D,OAAO,EAAE,kCAAkC,EAAE,MAAM,oBAAoB,CAAC;AACxE,OAAO,EACL,mBAAmB,EACnB,mBAAmB,GAEpB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAC;AAC7D,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"session.js","sourceRoot":"","sources":["../../src/child/session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE,MAAM,kBAAkB,CAAC;AAC/C,O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AM,EAAE,CAAC,aAAa,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;gBAC5D,CAAC;YACH,CAAC;SACF,CAAC;IACJ,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,EAAE,CAAC,aAAa,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;QAC1D,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Usage of a child's assistant message while it streams. A provider reports
|
|
3
|
+
* input and cache tokens when the message starts and output only when it ends,
|
|
4
|
+
* so output is estimated from the characters streamed so far, at four to a
|
|
5
|
+
* token, and priced at the run's last completed rate. `message_end` replaces
|
|
6
|
+
* the estimate with the real total.
|
|
7
|
+
*/
|
|
8
|
+
export interface RunUsage {
|
|
9
|
+
input: number;
|
|
10
|
+
output: number;
|
|
11
|
+
cacheRead: number;
|
|
12
|
+
cacheWrite: number;
|
|
13
|
+
cost: number;
|
|
14
|
+
}
|
|
15
|
+
interface StreamingUsage {
|
|
16
|
+
input: number;
|
|
17
|
+
output: number;
|
|
18
|
+
cacheRead: number;
|
|
19
|
+
cacheWrite: number;
|
|
20
|
+
/** Cost the provider has reported so far, less its output cost. */
|
|
21
|
+
cost: number;
|
|
22
|
+
/** Output cost the provider has reported so far, covering `output` tokens. */
|
|
23
|
+
outputCost: number;
|
|
24
|
+
chars: number;
|
|
25
|
+
}
|
|
26
|
+
/** Cost of one output token at the rate of the run's last completed message. */
|
|
27
|
+
export interface StreamingState {
|
|
28
|
+
streaming?: StreamingUsage;
|
|
29
|
+
outputRate?: number;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Applies one child session event to the run's streaming state. True when a
|
|
33
|
+
* message started, the one moment worth an update of its own; deltas are read
|
|
34
|
+
* by the next redraw.
|
|
35
|
+
*/
|
|
36
|
+
export declare function applyStreamingEvent(state: StreamingState, event: unknown): boolean;
|
|
37
|
+
/** The run's completed usage plus each streaming message's estimate. */
|
|
38
|
+
export declare function liveUsage(completed: RunUsage | undefined, states: Iterable<StreamingState>): RunUsage | undefined;
|
|
39
|
+
export {};
|
|
40
|
+
//# sourceMappingURL=streaming-usage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"streaming-usage.d.ts","sourceRoot":"","sources":["../../src/child/streaming-usage.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,MAAM,WAAW,QAAQ;IACvB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;CACd;AAED,UAAU,cAAc;IACtB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,mEAAmE;IACnE,IAAI,EAAE,MAAM,CAAC;IACb,8EAA8E;IAC9E,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE,MAAM,CAAC;CACf;AAED,gFAAgF;AAChF,MAAM,WAAW,cAAc;IAC7B,SAAS,CAAC,EAAE,cAAc,CAAC;IAC3B,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAwBD;;;;GAIG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,cAAc,EACrB,KAAK,EAAE,OAAO,GACb,OAAO,CA4CT;AAED,wEAAwE;AACxE,wBAAgB,SAAS,CACvB,SAAS,EAAE,QAAQ,GAAG,SAAS,EAC/B,MAAM,EAAE,QAAQ,CAAC,cAAc,CAAC,GAC/B,QAAQ,GAAG,SAAS,CA2BtB"}
|