@introspection-ai/recipes 0.13.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/LICENSE +191 -0
- package/README.md +184 -0
- package/dist/agent-tool.d.ts +42 -0
- package/dist/agent-tool.d.ts.map +1 -0
- package/dist/agent-tool.js +159 -0
- package/dist/agent-tool.js.map +1 -0
- package/dist/child-agent-completions.d.ts +61 -0
- package/dist/child-agent-completions.d.ts.map +1 -0
- package/dist/child-agent-completions.js +135 -0
- package/dist/child-agent-completions.js.map +1 -0
- package/dist/child-agent-store.d.ts +48 -0
- package/dist/child-agent-store.d.ts.map +1 -0
- package/dist/child-agent-store.js +98 -0
- package/dist/child-agent-store.js.map +1 -0
- package/dist/child-agent.d.ts +43 -0
- package/dist/child-agent.d.ts.map +1 -0
- package/dist/child-agent.js +268 -0
- package/dist/child-agent.js.map +1 -0
- package/dist/direct-cli.d.ts +9 -0
- package/dist/direct-cli.d.ts.map +1 -0
- package/dist/direct-cli.js +22 -0
- package/dist/direct-cli.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/inspect.d.ts +26 -0
- package/dist/inspect.d.ts.map +1 -0
- package/dist/inspect.js +70 -0
- package/dist/inspect.js.map +1 -0
- package/dist/interactions.d.ts +204 -0
- package/dist/interactions.d.ts.map +1 -0
- package/dist/interactions.js +358 -0
- package/dist/interactions.js.map +1 -0
- package/dist/mcp-catalog.d.ts +8 -0
- package/dist/mcp-catalog.d.ts.map +1 -0
- package/dist/mcp-catalog.js +60 -0
- package/dist/mcp-catalog.js.map +1 -0
- package/dist/mcp-cli-core.d.ts +79 -0
- package/dist/mcp-cli-core.d.ts.map +1 -0
- package/dist/mcp-cli-core.js +1717 -0
- package/dist/mcp-cli-core.js.map +1 -0
- package/dist/mcp-cli-help.d.ts +2 -0
- package/dist/mcp-cli-help.d.ts.map +1 -0
- package/dist/mcp-cli-help.js +36 -0
- package/dist/mcp-cli-help.js.map +1 -0
- package/dist/mcp-cli-policy.d.ts +17 -0
- package/dist/mcp-cli-policy.d.ts.map +1 -0
- package/dist/mcp-cli-policy.js +230 -0
- package/dist/mcp-cli-policy.js.map +1 -0
- package/dist/mcp-cli.d.ts +3 -0
- package/dist/mcp-cli.d.ts.map +1 -0
- package/dist/mcp-cli.js +33 -0
- package/dist/mcp-cli.js.map +1 -0
- package/dist/mcp-client.d.ts +3 -0
- package/dist/mcp-client.d.ts.map +1 -0
- package/dist/mcp-client.js +86 -0
- package/dist/mcp-client.js.map +1 -0
- package/dist/mcp-command-context.d.ts +13 -0
- package/dist/mcp-command-context.d.ts.map +1 -0
- package/dist/mcp-command-context.js +29 -0
- package/dist/mcp-command-context.js.map +1 -0
- package/dist/mcp-contract.d.ts +16 -0
- package/dist/mcp-contract.d.ts.map +1 -0
- package/dist/mcp-contract.js +306 -0
- package/dist/mcp-contract.js.map +1 -0
- package/dist/mcp-daemon-client.d.ts +10 -0
- package/dist/mcp-daemon-client.d.ts.map +1 -0
- package/dist/mcp-daemon-client.js +119 -0
- package/dist/mcp-daemon-client.js.map +1 -0
- package/dist/mcp-daemon-protocol.d.ts +64 -0
- package/dist/mcp-daemon-protocol.d.ts.map +1 -0
- package/dist/mcp-daemon-protocol.js +6 -0
- package/dist/mcp-daemon-protocol.js.map +1 -0
- package/dist/mcp-daemon.d.ts +3 -0
- package/dist/mcp-daemon.d.ts.map +1 -0
- package/dist/mcp-daemon.js +76880 -0
- package/dist/mcp-daemon.js.map +1 -0
- package/dist/mcp-run-worker.d.ts +2 -0
- package/dist/mcp-run-worker.d.ts.map +1 -0
- package/dist/mcp-run-worker.js +76579 -0
- package/dist/mcp-run-worker.js.map +1 -0
- package/dist/mcp.d.ts +168 -0
- package/dist/mcp.d.ts.map +1 -0
- package/dist/mcp.js +558 -0
- package/dist/mcp.js.map +1 -0
- package/dist/pi-extension.d.ts +30 -0
- package/dist/pi-extension.d.ts.map +1 -0
- package/dist/pi-extension.js +880 -0
- package/dist/pi-extension.js.map +1 -0
- package/dist/provider-env.d.ts +2 -0
- package/dist/provider-env.d.ts.map +1 -0
- package/dist/provider-env.js +21 -0
- package/dist/provider-env.js.map +1 -0
- package/dist/recipe/resolve.d.ts +30 -0
- package/dist/recipe/resolve.d.ts.map +1 -0
- package/dist/recipe/resolve.js +131 -0
- package/dist/recipe/resolve.js.map +1 -0
- package/dist/recipe-agent.d.ts +71 -0
- package/dist/recipe-agent.d.ts.map +1 -0
- package/dist/recipe-agent.js +555 -0
- package/dist/recipe-agent.js.map +1 -0
- package/dist/recipe-extensions.d.ts +9 -0
- package/dist/recipe-extensions.d.ts.map +1 -0
- package/dist/recipe-extensions.js +81 -0
- package/dist/recipe-extensions.js.map +1 -0
- package/dist/recipe-mcp-config.d.ts +15 -0
- package/dist/recipe-mcp-config.d.ts.map +1 -0
- package/dist/recipe-mcp-config.js +82 -0
- package/dist/recipe-mcp-config.js.map +1 -0
- package/dist/recipe-model.d.ts +49 -0
- package/dist/recipe-model.d.ts.map +1 -0
- package/dist/recipe-model.js +299 -0
- package/dist/recipe-model.js.map +1 -0
- package/dist/recipe-package.d.ts +79 -0
- package/dist/recipe-package.d.ts.map +1 -0
- package/dist/recipe-package.js +479 -0
- package/dist/recipe-package.js.map +1 -0
- package/dist/recipe-skills.d.ts +7 -0
- package/dist/recipe-skills.d.ts.map +1 -0
- package/dist/recipe-skills.js +46 -0
- package/dist/recipe-skills.js.map +1 -0
- package/dist/run-controller.d.ts +32 -0
- package/dist/run-controller.d.ts.map +1 -0
- package/dist/run-controller.js +259 -0
- package/dist/run-controller.js.map +1 -0
- package/dist/run.d.ts +28 -0
- package/dist/run.d.ts.map +1 -0
- package/dist/run.js +84 -0
- package/dist/run.js.map +1 -0
- package/dist/session.d.ts +140 -0
- package/dist/session.d.ts.map +1 -0
- package/dist/session.js +363 -0
- package/dist/session.js.map +1 -0
- package/dist/test-utils.d.ts +32 -0
- package/dist/test-utils.d.ts.map +1 -0
- package/dist/test-utils.js +259 -0
- package/dist/test-utils.js.map +1 -0
- package/dist/testing.d.ts +25 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +96 -0
- package/dist/testing.js.map +1 -0
- package/docs/agent-composition.md +196 -0
- package/docs/deployment-configuration.md +65 -0
- package/docs/index.md +49 -0
- package/docs/interactions.md +210 -0
- package/docs/mcp-auth.md +80 -0
- package/docs/mcp-configuration.md +126 -0
- package/docs/migration.md +70 -0
- package/docs/pi-extension.md +97 -0
- package/docs/python-bindings-release.md +47 -0
- package/docs/recipe-evals.md +70 -0
- package/docs/recipe-flow.md +41 -0
- package/docs/recipe-format.md +186 -0
- package/docs/recipe-judges.md +152 -0
- package/docs/runtime-library.md +171 -0
- package/package.json +106 -0
- package/vendor/mcp-client/darwin-arm64/mcp-client +0 -0
- package/vendor/mcp-client/darwin-x64/mcp-client +0 -0
- package/vendor/mcp-client/linux-arm64/mcp-client +0 -0
- package/vendor/mcp-client/linux-x64/mcp-client +0 -0
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { autoResolveInteractions } from "./interactions.js";
|
|
3
|
+
import { promptResultText } from "./child-agent.js";
|
|
4
|
+
/** Default in-process child concurrency. */
|
|
5
|
+
export const DEFAULT_SUBAGENT_CONCURRENCY = 4;
|
|
6
|
+
class RunNotFoundError extends Error {
|
|
7
|
+
constructor(id) {
|
|
8
|
+
super(`Agent run not found: ${id}`);
|
|
9
|
+
this.name = "RunNotFoundError";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The default rung-3 subagent controller: children are recipe sessions
|
|
14
|
+
* created through `createRecipeSession` in this process, with bounded
|
|
15
|
+
* concurrency and delegation depth.
|
|
16
|
+
*
|
|
17
|
+
* Recovery rule every controller must honor: a child whose agent profile no
|
|
18
|
+
* longer exists errors the parent's `agent` tool call — it never wedges it.
|
|
19
|
+
* Here that falls out of resolution: `createRecipeSession` throws for an
|
|
20
|
+
* unknown agent name, the run settles as `failed`, and `start`/`wait`
|
|
21
|
+
* resolve with that failure.
|
|
22
|
+
*/
|
|
23
|
+
export function createInProcessRunController(opts) {
|
|
24
|
+
const env = opts.env ?? process.env;
|
|
25
|
+
const concurrency = Math.max(1, opts.concurrency ?? DEFAULT_SUBAGENT_CONCURRENCY);
|
|
26
|
+
const runs = new Map();
|
|
27
|
+
let active = 0;
|
|
28
|
+
const queue = [];
|
|
29
|
+
async function acquireSlot() {
|
|
30
|
+
if (active < concurrency) {
|
|
31
|
+
active += 1;
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
await new Promise((resolve) => queue.push(resolve));
|
|
35
|
+
active += 1;
|
|
36
|
+
}
|
|
37
|
+
function releaseSlot() {
|
|
38
|
+
active -= 1;
|
|
39
|
+
queue.shift()?.();
|
|
40
|
+
}
|
|
41
|
+
function touch(run, patch) {
|
|
42
|
+
run.summary = {
|
|
43
|
+
...run.summary,
|
|
44
|
+
...patch,
|
|
45
|
+
last_activity_at: Date.now(),
|
|
46
|
+
};
|
|
47
|
+
if (run.summary.output && !patch.output_preview) {
|
|
48
|
+
run.summary.output_preview = run.summary.output.slice(0, 400);
|
|
49
|
+
}
|
|
50
|
+
void Promise.resolve(run.onUpdate?.(run.summary)).catch(() => { });
|
|
51
|
+
if (run.summary.status !== "running") {
|
|
52
|
+
for (const wake of run.waiters.splice(0))
|
|
53
|
+
wake();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function requireRun(id) {
|
|
57
|
+
const run = runs.get(id);
|
|
58
|
+
if (!run)
|
|
59
|
+
throw new RunNotFoundError(id);
|
|
60
|
+
return run;
|
|
61
|
+
}
|
|
62
|
+
async function executePrompt(run, prompt) {
|
|
63
|
+
await acquireSlot();
|
|
64
|
+
try {
|
|
65
|
+
if (run.summary.status !== "running")
|
|
66
|
+
return;
|
|
67
|
+
if (!run.handle) {
|
|
68
|
+
const sessionFactory = opts.sessionFactory ??
|
|
69
|
+
(await import("./session.js")).createRecipeSession;
|
|
70
|
+
run.handle = await sessionFactory({
|
|
71
|
+
recipeDir: opts.recipeDir,
|
|
72
|
+
agentName: run.summary.agent_name,
|
|
73
|
+
cwd: opts.cwd,
|
|
74
|
+
env,
|
|
75
|
+
...(opts.credentials ? { credentials: opts.credentials } : {}),
|
|
76
|
+
...(opts.otel
|
|
77
|
+
? {
|
|
78
|
+
otel: {
|
|
79
|
+
...opts.otel,
|
|
80
|
+
// Keep one conversation across the agent tree; let each
|
|
81
|
+
// child derive its own Recipe agent id and name.
|
|
82
|
+
meta: {
|
|
83
|
+
conversationId: opts.otel.meta?.conversationId,
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
}
|
|
87
|
+
: {}),
|
|
88
|
+
// The parent materialized the MCP session for itself and its
|
|
89
|
+
// visible subagents; children reuse that runtime.
|
|
90
|
+
mcpMode: "inherit",
|
|
91
|
+
// Delegation is one level deep by the Recipe format contract.
|
|
92
|
+
runController: null,
|
|
93
|
+
onEvent: (event) => {
|
|
94
|
+
const record = event;
|
|
95
|
+
if (record.type === "tool_execution_start") {
|
|
96
|
+
touch(run, { current_tool: String(record.toolName ?? "") });
|
|
97
|
+
}
|
|
98
|
+
if (record.type === "tool_execution_end") {
|
|
99
|
+
touch(run, { current_tool: undefined });
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
if (run.summary.status !== "running") {
|
|
104
|
+
await run.handle.dispose().catch(() => { });
|
|
105
|
+
run.handle = null;
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
// Children never own the root interaction lifecycle: their asks resolve
|
|
110
|
+
// internally so a child cannot strand the parent waiting on a user.
|
|
111
|
+
await autoResolveInteractions(() => run.handle.session.prompt(prompt));
|
|
112
|
+
const output = promptResultText({
|
|
113
|
+
messages: [...run.handle.session.messages],
|
|
114
|
+
});
|
|
115
|
+
// The prompt await can race an external interrupt even though the run
|
|
116
|
+
// was "running" before it began.
|
|
117
|
+
const interrupted = run.summary.status === "interrupted";
|
|
118
|
+
touch(run, {
|
|
119
|
+
status: interrupted ? "interrupted" : "completed",
|
|
120
|
+
output,
|
|
121
|
+
completed_at: Date.now(),
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
catch (err) {
|
|
125
|
+
touch(run, {
|
|
126
|
+
status: "failed",
|
|
127
|
+
error: err instanceof Error ? err.message : String(err),
|
|
128
|
+
completed_at: Date.now(),
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
finally {
|
|
132
|
+
releaseSlot();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return {
|
|
136
|
+
list() {
|
|
137
|
+
return [...runs.values()].map((run) => run.summary);
|
|
138
|
+
},
|
|
139
|
+
get(id) {
|
|
140
|
+
return runs.get(id)?.summary ?? null;
|
|
141
|
+
},
|
|
142
|
+
async start(input) {
|
|
143
|
+
const id = `agent-run-${randomUUID().slice(0, 8)}`;
|
|
144
|
+
const run = {
|
|
145
|
+
summary: {
|
|
146
|
+
agent_run_id: id,
|
|
147
|
+
invocation_name: input.name,
|
|
148
|
+
agent_name: input.name,
|
|
149
|
+
label: input.label ?? input.prompt.slice(0, 80),
|
|
150
|
+
prompt: input.prompt,
|
|
151
|
+
status: "running",
|
|
152
|
+
started_at: Date.now(),
|
|
153
|
+
last_activity_at: Date.now(),
|
|
154
|
+
},
|
|
155
|
+
handle: null,
|
|
156
|
+
settled: Promise.resolve(),
|
|
157
|
+
onUpdate: input.onUpdate,
|
|
158
|
+
waiters: [],
|
|
159
|
+
};
|
|
160
|
+
runs.set(id, run);
|
|
161
|
+
run.settled = executePrompt(run, input.prompt);
|
|
162
|
+
return run.summary;
|
|
163
|
+
},
|
|
164
|
+
async wait(id, signal) {
|
|
165
|
+
const run = requireRun(id);
|
|
166
|
+
if (run.summary.status !== "running")
|
|
167
|
+
return run.summary;
|
|
168
|
+
if (signal?.aborted) {
|
|
169
|
+
throw signal.reason ?? new Error("wait aborted");
|
|
170
|
+
}
|
|
171
|
+
await new Promise((resolve, reject) => {
|
|
172
|
+
const wake = () => {
|
|
173
|
+
signal?.removeEventListener("abort", onAbort);
|
|
174
|
+
resolve();
|
|
175
|
+
};
|
|
176
|
+
const onAbort = () => {
|
|
177
|
+
const index = run.waiters.indexOf(wake);
|
|
178
|
+
if (index >= 0)
|
|
179
|
+
run.waiters.splice(index, 1);
|
|
180
|
+
reject(signal?.reason ?? new Error("wait aborted"));
|
|
181
|
+
};
|
|
182
|
+
run.waiters.push(wake);
|
|
183
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
184
|
+
if (signal?.aborted)
|
|
185
|
+
onAbort();
|
|
186
|
+
});
|
|
187
|
+
return run.summary;
|
|
188
|
+
},
|
|
189
|
+
async message(id, message) {
|
|
190
|
+
const run = requireRun(id);
|
|
191
|
+
if (run.summary.status === "closed") {
|
|
192
|
+
throw new Error(`Agent run is closed: ${id}`);
|
|
193
|
+
}
|
|
194
|
+
if (run.summary.status === "running" && !run.handle) {
|
|
195
|
+
await run.settled;
|
|
196
|
+
}
|
|
197
|
+
if (run.summary.status === "running" && run.handle) {
|
|
198
|
+
await run.handle.session.steer(message);
|
|
199
|
+
touch(run, {});
|
|
200
|
+
return run.summary;
|
|
201
|
+
}
|
|
202
|
+
// A terminal (completed/failed/interrupted) child accepts a follow-up
|
|
203
|
+
// turn on its existing session.
|
|
204
|
+
touch(run, {
|
|
205
|
+
status: "running",
|
|
206
|
+
completed_at: undefined,
|
|
207
|
+
error: undefined,
|
|
208
|
+
output: undefined,
|
|
209
|
+
output_preview: undefined,
|
|
210
|
+
current_tool: undefined,
|
|
211
|
+
});
|
|
212
|
+
run.settled = executePrompt(run, message);
|
|
213
|
+
return run.summary;
|
|
214
|
+
},
|
|
215
|
+
async interrupt(id) {
|
|
216
|
+
const run = requireRun(id);
|
|
217
|
+
if (run.summary.status === "running") {
|
|
218
|
+
touch(run, { status: "interrupted", completed_at: Date.now() });
|
|
219
|
+
await run.handle?.session.abort().catch(() => { });
|
|
220
|
+
}
|
|
221
|
+
return run.summary;
|
|
222
|
+
},
|
|
223
|
+
async close(id) {
|
|
224
|
+
const run = requireRun(id);
|
|
225
|
+
if (run.summary.status === "running") {
|
|
226
|
+
await run.handle?.session.abort().catch(() => { });
|
|
227
|
+
}
|
|
228
|
+
await run.handle?.dispose().catch(() => { });
|
|
229
|
+
run.handle = null;
|
|
230
|
+
if (run.summary.status === "running" || run.summary.status !== "closed") {
|
|
231
|
+
touch(run, { status: "closed", completed_at: run.summary.completed_at ?? Date.now() });
|
|
232
|
+
}
|
|
233
|
+
return run.summary;
|
|
234
|
+
},
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
/** A controller for sessions whose recipe declares no subagents. */
|
|
238
|
+
export function inertRunController() {
|
|
239
|
+
return {
|
|
240
|
+
list: () => [],
|
|
241
|
+
get: () => null,
|
|
242
|
+
async start() {
|
|
243
|
+
throw new Error("This recipe declares no subagents");
|
|
244
|
+
},
|
|
245
|
+
async wait(id) {
|
|
246
|
+
throw new RunNotFoundError(id);
|
|
247
|
+
},
|
|
248
|
+
async message(id) {
|
|
249
|
+
throw new RunNotFoundError(id);
|
|
250
|
+
},
|
|
251
|
+
async interrupt(id) {
|
|
252
|
+
throw new RunNotFoundError(id);
|
|
253
|
+
},
|
|
254
|
+
async close(id) {
|
|
255
|
+
throw new RunNotFoundError(id);
|
|
256
|
+
},
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
//# sourceMappingURL=run-controller.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run-controller.js","sourceRoot":"","sources":["../src/run-controller.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAOzC,OAAO,EAAE,uBAAuB,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAOpD,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,GAAE,CAAC,CAAC,CAAC;YAC5C,GAAG,CAAC,MAAM,GAAG,IAAI,CAAC;YAClB,IAAI,GAAG,CAAC,OAAO,CAAC,MAAM,KAAK,SAAS,IAAI,GAAG,CAAC,OAAO,CAAC,MAAM,KAAK,QAAQ,EAAE,CAAC;gBACxE,KAAK,CAAC,GAAG,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,YAAY,EAAE,GAAG,CAAC,OAAO,CAAC,YAAY,IAAI,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;YACzF,CAAC;YACD,OAAO,GAAG,CAAC,OAAO,CAAC;QACrB,CAAC;KACF,CAAC;AACJ,CAAC;AAED,oEAAoE;AACpE,MAAM,UAAU,kBAAkB;IAChC,OAAO;QACL,IAAI,EAAE,GAAG,EAAE,CAAC,EAAE;QACd,GAAG,EAAE,GAAG,EAAE,CAAC,IAAI;QACf,KAAK,CAAC,KAAK;YACT,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAC;QACvD,CAAC;QACD,KAAK,CAAC,IAAI,CAAC,EAAU;YACnB,MAAM,IAAI,gBAAgB,CAAC,EAAE,CAAC,CAAC;QACjC,CAAC;QACD,KAAK,CAAC,OAAO,CAAC,EAAU;YACtB,MAAM,IAAI,gBAAgB,CAAC,EAAE,CAAC,CAAC;QACjC,CAAC;QACD,KAAK,CAAC,SAAS,CAAC,EAAU;YACxB,MAAM,IAAI,gBAAgB,CAAC,EAAE,CAAC,CAAC;QACjC,CAAC;QACD,KAAK,CAAC,KAAK,CAAC,EAAU;YACpB,MAAM,IAAI,gBAAgB,CAAC,EAAE,CAAC,CAAC;QACjC,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/run.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { AgentMessage } from "@earendil-works/pi-agent-core";
|
|
2
|
+
import { type CreateRecipeSessionOptions } from "./session.js";
|
|
3
|
+
/**
|
|
4
|
+
* One turn, no server: create session → single prompt → await settle →
|
|
5
|
+
* dispose, always. The programmatic analog of `pi --mode json -p`.
|
|
6
|
+
*
|
|
7
|
+
* Agent-level failure never throws — it lands in `status: "failed"`. Only
|
|
8
|
+
* caller mistakes (bad options, unreadable recipe, missing credentials)
|
|
9
|
+
* throw, from session construction.
|
|
10
|
+
*/
|
|
11
|
+
export interface RunRecipeOptions extends CreateRecipeSessionOptions {
|
|
12
|
+
prompt: string;
|
|
13
|
+
timeoutMs?: number;
|
|
14
|
+
signal?: AbortSignal;
|
|
15
|
+
/** Session factory; defaults to `createRecipeSession`. Test/DI seam. */
|
|
16
|
+
sessionFactory?: (options: CreateRecipeSessionOptions) => Promise<import("./session.js").RecipeSessionHandle>;
|
|
17
|
+
}
|
|
18
|
+
export interface RecipeRunResult {
|
|
19
|
+
status: "finished" | "failed" | "cancelled";
|
|
20
|
+
/** Final assistant message text ("" if none). */
|
|
21
|
+
text: string;
|
|
22
|
+
/** Full transcript, Pi's message shape. */
|
|
23
|
+
messages: AgentMessage[];
|
|
24
|
+
/** Present iff status === "failed". */
|
|
25
|
+
error?: string;
|
|
26
|
+
}
|
|
27
|
+
export declare function runRecipe(options: RunRecipeOptions): Promise<RecipeRunResult>;
|
|
28
|
+
//# sourceMappingURL=run.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run.d.ts","sourceRoot":"","sources":["../src/run.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAElE,OAAO,EAEL,KAAK,0BAA0B,EAChC,MAAM,cAAc,CAAC;AAEtB;;;;;;;GAOG;AACH,MAAM,WAAW,gBAAiB,SAAQ,0BAA0B;IAClE,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,wEAAwE;IACxE,cAAc,CAAC,EAAE,CACf,OAAO,EAAE,0BAA0B,KAChC,OAAO,CAAC,OAAO,cAAc,EAAE,mBAAmB,CAAC,CAAC;CAC1D;AAED,MAAM,WAAW,eAAe;IAC9B,MAAM,EAAE,UAAU,GAAG,QAAQ,GAAG,WAAW,CAAC;IAC5C,iDAAiD;IACjD,IAAI,EAAE,MAAM,CAAC;IACb,2CAA2C;IAC3C,QAAQ,EAAE,YAAY,EAAE,CAAC;IACzB,uCAAuC;IACvC,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAqBD,wBAAsB,SAAS,CAC7B,OAAO,EAAE,gBAAgB,GACxB,OAAO,CAAC,eAAe,CAAC,CAmE1B"}
|
package/dist/run.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { promptResultText } from "./child-agent.js";
|
|
2
|
+
import { createRecipeSession, } from "./session.js";
|
|
3
|
+
function lastAssistantStop(messages) {
|
|
4
|
+
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
5
|
+
const message = messages[index];
|
|
6
|
+
if (message?.role !== "assistant")
|
|
7
|
+
continue;
|
|
8
|
+
return {
|
|
9
|
+
...(typeof message.stopReason === "string"
|
|
10
|
+
? { stopReason: message.stopReason }
|
|
11
|
+
: {}),
|
|
12
|
+
...(typeof message.errorMessage === "string"
|
|
13
|
+
? { errorMessage: message.errorMessage }
|
|
14
|
+
: {}),
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
return {};
|
|
18
|
+
}
|
|
19
|
+
export async function runRecipe(options) {
|
|
20
|
+
const { prompt, timeoutMs, signal, sessionFactory, ...sessionOptions } = options;
|
|
21
|
+
if (typeof prompt !== "string" || !prompt.trim()) {
|
|
22
|
+
throw new TypeError("runRecipe requires a non-empty prompt");
|
|
23
|
+
}
|
|
24
|
+
if (signal?.aborted) {
|
|
25
|
+
return { status: "cancelled", text: "", messages: [] };
|
|
26
|
+
}
|
|
27
|
+
const handle = await (sessionFactory ?? createRecipeSession)(sessionOptions);
|
|
28
|
+
let timedOut = false;
|
|
29
|
+
let aborted = false;
|
|
30
|
+
let timer;
|
|
31
|
+
const onAbort = () => {
|
|
32
|
+
aborted = true;
|
|
33
|
+
void handle.session.abort().catch(() => { });
|
|
34
|
+
};
|
|
35
|
+
try {
|
|
36
|
+
if (timeoutMs !== undefined) {
|
|
37
|
+
timer = setTimeout(() => {
|
|
38
|
+
timedOut = true;
|
|
39
|
+
void handle.session.abort().catch(() => { });
|
|
40
|
+
}, timeoutMs);
|
|
41
|
+
}
|
|
42
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
43
|
+
// The signal may have fired while the asynchronous session factory was
|
|
44
|
+
// resolving, before the listener existed.
|
|
45
|
+
if (signal?.aborted) {
|
|
46
|
+
aborted = true;
|
|
47
|
+
await handle.session.abort().catch(() => { });
|
|
48
|
+
return {
|
|
49
|
+
status: "cancelled",
|
|
50
|
+
text: "",
|
|
51
|
+
messages: [...handle.session.messages],
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
let promptError;
|
|
55
|
+
try {
|
|
56
|
+
await handle.session.prompt(prompt);
|
|
57
|
+
}
|
|
58
|
+
catch (err) {
|
|
59
|
+
promptError = err instanceof Error ? err.message : String(err);
|
|
60
|
+
}
|
|
61
|
+
const messages = [...handle.session.messages];
|
|
62
|
+
const text = promptResultText({ messages });
|
|
63
|
+
const { stopReason, errorMessage } = lastAssistantStop(messages);
|
|
64
|
+
if (timedOut || aborted || stopReason === "aborted") {
|
|
65
|
+
return { status: "cancelled", text, messages };
|
|
66
|
+
}
|
|
67
|
+
if (promptError !== undefined || stopReason === "error") {
|
|
68
|
+
return {
|
|
69
|
+
status: "failed",
|
|
70
|
+
text,
|
|
71
|
+
messages,
|
|
72
|
+
error: promptError ?? errorMessage ?? "agent error",
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
return { status: "finished", text, messages };
|
|
76
|
+
}
|
|
77
|
+
finally {
|
|
78
|
+
if (timer !== undefined)
|
|
79
|
+
clearTimeout(timer);
|
|
80
|
+
signal?.removeEventListener("abort", onAbort);
|
|
81
|
+
await handle.dispose();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=run.js.map
|
package/dist/run.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run.js","sourceRoot":"","sources":["../src/run.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,EACL,mBAAmB,GAEpB,MAAM,cAAc,CAAC;AA8BtB,SAAS,iBAAiB,CAAC,QAAiC;IAI1D,KAAK,IAAI,KAAK,GAAG,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,KAAK,IAAI,CAAC,EAAE,KAAK,IAAI,CAAC,EAAE,CAAC;QAC7D,MAAM,OAAO,GAAG,QAAQ,CAAC,KAAK,CAAuC,CAAC;QACtE,IAAI,OAAO,EAAE,IAAI,KAAK,WAAW;YAAE,SAAS;QAC5C,OAAO;YACL,GAAG,CAAC,OAAO,OAAO,CAAC,UAAU,KAAK,QAAQ;gBACxC,CAAC,CAAC,EAAE,UAAU,EAAE,OAAO,CAAC,UAAU,EAAE;gBACpC,CAAC,CAAC,EAAE,CAAC;YACP,GAAG,CAAC,OAAO,OAAO,CAAC,YAAY,KAAK,QAAQ;gBAC1C,CAAC,CAAC,EAAE,YAAY,EAAE,OAAO,CAAC,YAAY,EAAE;gBACxC,CAAC,CAAC,EAAE,CAAC;SACR,CAAC;IACJ,CAAC;IACD,OAAO,EAAE,CAAC;AACZ,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,SAAS,CAC7B,OAAyB;IAEzB,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,cAAc,EAAE,GAAG,cAAc,EAAE,GACpE,OAAO,CAAC;IACV,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,EAAE,CAAC;QACjD,MAAM,IAAI,SAAS,CAAC,uCAAuC,CAAC,CAAC;IAC/D,CAAC;IACD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
import { type CredentialStore } from "@earendil-works/pi-ai";
|
|
2
|
+
import { type Model } from "@earendil-works/pi-ai/compat";
|
|
3
|
+
import type { ThinkingLevel } from "@earendil-works/pi-agent-core";
|
|
4
|
+
import { SessionManager, SettingsManager, type AgentSession, type AgentSessionEvent, type AgentSessionRuntimeDiagnostic, type EventBus, type ExtensionFactory, type ToolDefinition } from "@earendil-works/pi-coding-agent";
|
|
5
|
+
import { type AgentMeta, type InstrumentSessionOptions } from "@introspection-sdk/introspection-pi";
|
|
6
|
+
import { type AgentRunController } from "./agent-tool.js";
|
|
7
|
+
import { type McpLocalConfig } from "./mcp.js";
|
|
8
|
+
import { type ResolvedRecipe } from "./recipe/resolve.js";
|
|
9
|
+
export { expectedProviderEnvVars } from "./provider-env.js";
|
|
10
|
+
/**
|
|
11
|
+
* Everything between "resolved recipe" and "live Pi session", done once:
|
|
12
|
+
* model construction and credentials, MCP materialization, skills /
|
|
13
|
+
* extensions / system-prompt wiring, subagent tool registration.
|
|
14
|
+
*
|
|
15
|
+
* Fails closed at construction: recipe resolution errors propagate, a
|
|
16
|
+
* `required: true` MCP server with no binding throws `McpBindingError`, and a
|
|
17
|
+
* model whose provider has no credential throws `RecipeCredentialError`.
|
|
18
|
+
*/
|
|
19
|
+
export interface CreateRecipeSessionOptions {
|
|
20
|
+
recipeDir: string;
|
|
21
|
+
/** Default: `agents/agent.yaml`, else the recipe's single-agent rule. */
|
|
22
|
+
agentName?: string;
|
|
23
|
+
/** Agent workspace. Default: `process.cwd()`. */
|
|
24
|
+
cwd?: string;
|
|
25
|
+
/** Model credentials. Default: derived from provider env keys. */
|
|
26
|
+
credentials?: CredentialStore;
|
|
27
|
+
/** Override the recipe's `<provider>/<model_id>` spec. */
|
|
28
|
+
model?: string;
|
|
29
|
+
/**
|
|
30
|
+
* Host-constructed model transport. The recipe still owns its model
|
|
31
|
+
* configuration; this replaces only catalog lookup and transport wiring.
|
|
32
|
+
*/
|
|
33
|
+
modelOverride?: Model<any>;
|
|
34
|
+
thinkingLevel?: ThinkingLevel;
|
|
35
|
+
/** Explicit local MCP binding file. Default: `<cwd|recipeDir>/.pi/mcp.local.json`. */
|
|
36
|
+
mcpBindingsPath?: string;
|
|
37
|
+
/** Inline MCP bindings, for hosts that synthesize them instead of reading a file. */
|
|
38
|
+
mcpBindings?: McpLocalConfig;
|
|
39
|
+
/**
|
|
40
|
+
* MCP materialization mode. "materialize" (default) resolves bindings and
|
|
41
|
+
* prepares the session MCP runtime in `env`; "inherit" trusts that the host
|
|
42
|
+
* process already materialized a session covering this agent's servers
|
|
43
|
+
* (subagent sessions run this way).
|
|
44
|
+
*/
|
|
45
|
+
mcpMode?: "materialize" | "inherit";
|
|
46
|
+
/** `${VAR}` resolution source and MCP runtime environment. Default: `process.env`. */
|
|
47
|
+
env?: NodeJS.ProcessEnv;
|
|
48
|
+
/** Default: `SessionManager.inMemory(cwd)`. */
|
|
49
|
+
sessionManager?: SessionManager;
|
|
50
|
+
/** Host-owned settings (compaction, retry). Default: `SettingsManager.create(cwd, recipeDir)`. */
|
|
51
|
+
settingsManager?: SettingsManager;
|
|
52
|
+
/** Host extensions appended after the recipe's own extensions. */
|
|
53
|
+
extensionFactories?: ExtensionFactory[];
|
|
54
|
+
/** Host event bus shared with the surrounding runtime. */
|
|
55
|
+
eventBus?: EventBus;
|
|
56
|
+
/** Host-owned tools. Recipe tool selection still decides which names are live. */
|
|
57
|
+
customTools?: ToolDefinition[];
|
|
58
|
+
/**
|
|
59
|
+
* Subagent run controller. Default: an in-process controller spawning
|
|
60
|
+
* children through this same rung. Pass `null` to disable the `agent` tool
|
|
61
|
+
* even when the recipe declares subagents.
|
|
62
|
+
*/
|
|
63
|
+
runController?: AgentRunController | null;
|
|
64
|
+
/** Host hooks for the shared `agent` tool UI contract. */
|
|
65
|
+
agentToolOptions?: {
|
|
66
|
+
acknowledgeCompletions?(ids: readonly string[]): void;
|
|
67
|
+
};
|
|
68
|
+
/** Concurrency bound for the default in-process subagent controller. */
|
|
69
|
+
subagentLimits?: {
|
|
70
|
+
concurrency?: number;
|
|
71
|
+
};
|
|
72
|
+
/** Extra skill roots beyond the recipe's. */
|
|
73
|
+
additionalSkillPaths?: string[];
|
|
74
|
+
/** Replace the recipe's resolved skill roots with host-materialized roots. */
|
|
75
|
+
skillPaths?: string[];
|
|
76
|
+
/** Post-resolution system prompt hook. */
|
|
77
|
+
systemPrompt?: (resolved: string) => string;
|
|
78
|
+
/** Observe Pi resource diagnostics during construction. */
|
|
79
|
+
onDiagnostics?: (diagnostics: AgentSessionRuntimeDiagnostic[]) => void;
|
|
80
|
+
/** Tap on `session.subscribe`, detached at dispose. */
|
|
81
|
+
onEvent?: (event: AgentSessionEvent) => void;
|
|
82
|
+
/**
|
|
83
|
+
* Attach GenAI semantic-convention instrumentation with a host-owned OTel
|
|
84
|
+
* tracer. Recipes creates no provider, processor, exporter, or global
|
|
85
|
+
* context; those remain the host's responsibility.
|
|
86
|
+
*/
|
|
87
|
+
otel?: RecipeSessionOtelOptions;
|
|
88
|
+
}
|
|
89
|
+
export interface RecipeSessionOtelOptions extends Omit<InstrumentSessionOptions, "meta"> {
|
|
90
|
+
/**
|
|
91
|
+
* Span identity. Missing fields derive from the resolved Recipe; a fresh
|
|
92
|
+
* conversation id is generated per session by default.
|
|
93
|
+
*/
|
|
94
|
+
meta?: Partial<AgentMeta>;
|
|
95
|
+
}
|
|
96
|
+
export interface RecipeSessionHandle {
|
|
97
|
+
/** The live Pi session: prompt / steer / followUp / abort / subscribe. */
|
|
98
|
+
session: AgentSession;
|
|
99
|
+
recipe: ResolvedRecipe;
|
|
100
|
+
/** The subagent run controller serving this session's `agent` tool. */
|
|
101
|
+
runs: AgentRunController;
|
|
102
|
+
/** Abort any in-flight turn, dispose the session, stop session MCP. */
|
|
103
|
+
dispose(): Promise<void>;
|
|
104
|
+
}
|
|
105
|
+
/** The session model's provider has no resolvable credential. */
|
|
106
|
+
export declare class RecipeCredentialError extends Error {
|
|
107
|
+
readonly provider: string;
|
|
108
|
+
/** Env vars that would satisfy the provider, most conventional first. */
|
|
109
|
+
readonly expectedEnvVars: readonly string[];
|
|
110
|
+
readonly name = "RecipeCredentialError";
|
|
111
|
+
constructor(provider: string,
|
|
112
|
+
/** Env vars that would satisfy the provider, most conventional first. */
|
|
113
|
+
expectedEnvVars: readonly string[]);
|
|
114
|
+
}
|
|
115
|
+
/** The recipe's model spec does not resolve to a known model. */
|
|
116
|
+
export declare class RecipeModelError extends Error {
|
|
117
|
+
readonly modelSpec: string;
|
|
118
|
+
readonly name = "RecipeModelError";
|
|
119
|
+
constructor(modelSpec: string);
|
|
120
|
+
}
|
|
121
|
+
export declare class RecipeMcpEnvironmentInUseError extends Error {
|
|
122
|
+
readonly name = "RecipeMcpEnvironmentInUseError";
|
|
123
|
+
constructor();
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Everything `createRecipeSession` fail-closes on, without creating a
|
|
127
|
+
* session or starting MCP: recipe resolution, model lookup, credentials.
|
|
128
|
+
* Hosts with a boot phase (the serve layer) run this once to fail fast.
|
|
129
|
+
*/
|
|
130
|
+
export declare function preflightRecipeSession(opts: CreateRecipeSessionOptions): Promise<ResolvedRecipe>;
|
|
131
|
+
/**
|
|
132
|
+
* Materialize the session MCP runtime for a recipe scope into `env` at
|
|
133
|
+
* `cwd`. Exposed for hosts that materialize once per process (the serve
|
|
134
|
+
* layer) and create their sessions with `mcpMode: "inherit"`.
|
|
135
|
+
*/
|
|
136
|
+
export declare function materializeRecipeSessionMcp(recipe: ResolvedRecipe, cwd: string, env: NodeJS.ProcessEnv, opts?: Pick<CreateRecipeSessionOptions, "mcpBindings" | "mcpBindingsPath" | "mcpMode">): Promise<{
|
|
137
|
+
materialized: boolean;
|
|
138
|
+
}>;
|
|
139
|
+
export declare function createRecipeSession(opts: CreateRecipeSessionOptions): Promise<RecipeSessionHandle>;
|
|
140
|
+
//# sourceMappingURL=session.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"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