@minpeter/pss-runtime 0.0.11 → 0.1.0-next.1
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/README.md +67 -5
- package/dist/agent-namespace.js +17 -0
- package/dist/agent-namespace.js.map +1 -0
- package/dist/agent-validation.js +35 -0
- package/dist/agent-validation.js.map +1 -0
- package/dist/agent.d.ts +11 -2
- package/dist/agent.js +79 -14
- package/dist/agent.js.map +1 -1
- package/dist/child-session-cleanups.js +61 -0
- package/dist/child-session-cleanups.js.map +1 -0
- package/dist/session/events.d.ts +21 -1
- package/dist/session/history.d.ts +1 -0
- package/dist/session/input-normalization.js +66 -0
- package/dist/session/input-normalization.js.map +1 -0
- package/dist/session/runtime-input.d.ts +1 -0
- package/dist/session/runtime-input.js +69 -0
- package/dist/session/runtime-input.js.map +1 -0
- package/dist/session/session-errors.js +23 -0
- package/dist/session/session-errors.js.map +1 -0
- package/dist/session/session-kill.js +23 -0
- package/dist/session/session-kill.js.map +1 -0
- package/dist/session/session-runtime-drain.js +22 -0
- package/dist/session/session-runtime-drain.js.map +1 -0
- package/dist/session/session-state.d.ts +1 -0
- package/dist/session/session-state.js +102 -0
- package/dist/session/session-state.js.map +1 -0
- package/dist/session/session-turn-error.js +35 -0
- package/dist/session/session-turn-error.js.map +1 -0
- package/dist/session/session.js +95 -240
- package/dist/session/session.js.map +1 -1
- package/dist/session/store/file.d.ts +1 -0
- package/dist/session/store/file.js +14 -0
- package/dist/session/store/file.js.map +1 -1
- package/dist/session/store/memory.d.ts +1 -0
- package/dist/session/store/memory.js +5 -0
- package/dist/session/store/memory.js.map +1 -1
- package/dist/session/store/types.d.ts +1 -0
- package/dist/subagent-job-cancel.js +28 -0
- package/dist/subagent-job-cancel.js.map +1 -0
- package/dist/subagent-job-output.js +63 -0
- package/dist/subagent-job-output.js.map +1 -0
- package/dist/subagent-jobs.js +151 -0
- package/dist/subagent-jobs.js.map +1 -0
- package/dist/subagent-prompt-schema.js +114 -0
- package/dist/subagent-prompt-schema.js.map +1 -0
- package/dist/subagent-run.js +111 -0
- package/dist/subagent-run.js.map +1 -0
- package/dist/subagents.js +92 -0
- package/dist/subagents.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subagent-job-cancel.js","names":[],"sources":["../src/subagent-job-cancel.ts"],"sourcesContent":["import { jsonSchema, tool } from \"ai\";\nimport {\n assertBackgroundTaskId,\n cancelJob,\n isActiveJob,\n} from \"./subagent-jobs\";\nimport type { BackgroundCancelInput, SubagentJob } from \"./subagent-types\";\n\nexport function createBackgroundCancelTool(jobs: Map<string, SubagentJob>) {\n return tool<BackgroundCancelInput, unknown, Record<string, unknown>>({\n description: \"Cancel an active background subagent job.\",\n execute: (input: BackgroundCancelInput) => {\n assertBackgroundTaskId(input.task_id, \"background_cancel\");\n const job = jobs.get(input.task_id);\n if (!job) {\n throw new Error(`Unknown background subagent task ${input.task_id}.`);\n }\n\n if (isActiveJob(job.status)) {\n cancelJob(job);\n }\n\n return {\n status: job.status,\n task_id: job.id,\n };\n },\n inputSchema: jsonSchema<BackgroundCancelInput>({\n additionalProperties: false,\n properties: {\n task_id: { type: \"string\" },\n },\n required: [\"task_id\"],\n type: \"object\",\n }),\n });\n}\n"],"mappings":";;;AAQA,SAAgB,2BAA2B,MAAgC;CACzE,OAAO,KAA8D;EACnE,aAAa;EACb,UAAU,UAAiC;GACzC,uBAAuB,MAAM,SAAS,mBAAmB;GACzD,MAAM,MAAM,KAAK,IAAI,MAAM,OAAO;GAClC,IAAI,CAAC,KACH,MAAM,IAAI,MAAM,oCAAoC,MAAM,QAAQ,EAAE;GAGtE,IAAI,YAAY,IAAI,MAAM,GACxB,UAAU,GAAG;GAGf,OAAO;IACL,QAAQ,IAAI;IACZ,SAAS,IAAI;GACf;EACF;EACA,aAAa,WAAkC;GAC7C,sBAAsB;GACtB,YAAY,EACV,SAAS,EAAE,MAAM,SAAS,EAC5B;GACA,UAAU,CAAC,SAAS;GACpB,MAAM;EACR,CAAC;CACH,CAAC;AACH"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { assertBackgroundTaskId, cleanupJob, isActiveJob } from "./subagent-jobs.js";
|
|
2
|
+
import { jsonSchema, tool } from "ai";
|
|
3
|
+
//#region src/subagent-job-output.ts
|
|
4
|
+
function createBackgroundOutputTool(jobs) {
|
|
5
|
+
return tool({
|
|
6
|
+
description: "Retrieve compact output for a background subagent job.",
|
|
7
|
+
execute: async (input, { abortSignal }) => {
|
|
8
|
+
assertBackgroundTaskId(input.task_id, "background_output");
|
|
9
|
+
const job = jobs.get(input.task_id);
|
|
10
|
+
if (!job) throw new Error(`Unknown background subagent task ${input.task_id}.`);
|
|
11
|
+
if (input.block === true && isActiveJob(job.status)) await waitForJob(job, input.timeout, abortSignal);
|
|
12
|
+
const output = {
|
|
13
|
+
result: job.result,
|
|
14
|
+
status: job.status,
|
|
15
|
+
subagent: job.subagent,
|
|
16
|
+
task_id: job.id
|
|
17
|
+
};
|
|
18
|
+
if (!(isActiveJob(job.status) || !job.settled)) {
|
|
19
|
+
if (await cleanupJob(job).then(() => true, () => false)) jobs.delete(job.id);
|
|
20
|
+
}
|
|
21
|
+
return output;
|
|
22
|
+
},
|
|
23
|
+
inputSchema: jsonSchema({
|
|
24
|
+
additionalProperties: false,
|
|
25
|
+
properties: {
|
|
26
|
+
block: { type: "boolean" },
|
|
27
|
+
task_id: { type: "string" },
|
|
28
|
+
timeout: {
|
|
29
|
+
minimum: 0,
|
|
30
|
+
type: "number"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
required: ["task_id"],
|
|
34
|
+
type: "object"
|
|
35
|
+
})
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
async function waitForJob(job, timeout, abortSignal) {
|
|
39
|
+
if (abortSignal?.aborted) return;
|
|
40
|
+
const timeoutMs = Math.min(timeout ?? 6e4, 6e5);
|
|
41
|
+
let timeoutId;
|
|
42
|
+
let abortListener;
|
|
43
|
+
const abortPromise = abortSignal ? new Promise((resolve) => {
|
|
44
|
+
abortListener = resolve;
|
|
45
|
+
abortSignal.addEventListener("abort", abortListener, { once: true });
|
|
46
|
+
}) : void 0;
|
|
47
|
+
try {
|
|
48
|
+
await Promise.race([
|
|
49
|
+
job.promise,
|
|
50
|
+
...abortPromise ? [abortPromise] : [],
|
|
51
|
+
new Promise((resolve) => {
|
|
52
|
+
timeoutId = setTimeout(resolve, timeoutMs);
|
|
53
|
+
})
|
|
54
|
+
]);
|
|
55
|
+
} finally {
|
|
56
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
57
|
+
if (abortListener) abortSignal?.removeEventListener("abort", abortListener);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
//#endregion
|
|
61
|
+
export { createBackgroundOutputTool };
|
|
62
|
+
|
|
63
|
+
//# sourceMappingURL=subagent-job-output.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subagent-job-output.js","names":[],"sources":["../src/subagent-job-output.ts"],"sourcesContent":["import { jsonSchema, tool } from \"ai\";\nimport {\n assertBackgroundTaskId,\n cleanupJob,\n isActiveJob,\n} from \"./subagent-jobs\";\nimport type { BackgroundOutputInput, SubagentJob } from \"./subagent-types\";\n\nexport function createBackgroundOutputTool(jobs: Map<string, SubagentJob>) {\n return tool<BackgroundOutputInput, unknown, Record<string, unknown>>({\n description: \"Retrieve compact output for a background subagent job.\",\n execute: async (input: BackgroundOutputInput, { abortSignal }) => {\n assertBackgroundTaskId(input.task_id, \"background_output\");\n const job = jobs.get(input.task_id);\n if (!job) {\n throw new Error(`Unknown background subagent task ${input.task_id}.`);\n }\n\n if (input.block === true && isActiveJob(job.status)) {\n await waitForJob(job, input.timeout, abortSignal);\n }\n\n const output = {\n result: job.result,\n status: job.status,\n subagent: job.subagent,\n task_id: job.id,\n };\n if (!(isActiveJob(job.status) || !job.settled)) {\n const cleaned = await cleanupJob(job).then(\n () => true,\n () => false\n );\n if (cleaned) {\n jobs.delete(job.id);\n }\n }\n\n return output;\n },\n inputSchema: jsonSchema<BackgroundOutputInput>({\n additionalProperties: false,\n properties: {\n block: { type: \"boolean\" },\n task_id: { type: \"string\" },\n timeout: { minimum: 0, type: \"number\" },\n },\n required: [\"task_id\"],\n type: \"object\",\n }),\n });\n}\n\nasync function waitForJob(\n job: SubagentJob,\n timeout: number | undefined,\n abortSignal: AbortSignal | undefined\n) {\n if (abortSignal?.aborted) {\n return;\n }\n\n const timeoutMs = Math.min(timeout ?? 60_000, 600_000);\n let timeoutId: ReturnType<typeof setTimeout> | undefined;\n let abortListener: (() => void) | undefined;\n const abortPromise = abortSignal\n ? new Promise<void>((resolve) => {\n abortListener = resolve;\n abortSignal.addEventListener(\"abort\", abortListener, { once: true });\n })\n : undefined;\n try {\n await Promise.race([\n job.promise,\n ...(abortPromise ? [abortPromise] : []),\n new Promise<void>((resolve) => {\n timeoutId = setTimeout(resolve, timeoutMs);\n }),\n ]);\n } finally {\n if (timeoutId) {\n clearTimeout(timeoutId);\n }\n if (abortListener) {\n abortSignal?.removeEventListener(\"abort\", abortListener);\n }\n }\n}\n"],"mappings":";;;AAQA,SAAgB,2BAA2B,MAAgC;CACzE,OAAO,KAA8D;EACnE,aAAa;EACb,SAAS,OAAO,OAA8B,EAAE,kBAAkB;GAChE,uBAAuB,MAAM,SAAS,mBAAmB;GACzD,MAAM,MAAM,KAAK,IAAI,MAAM,OAAO;GAClC,IAAI,CAAC,KACH,MAAM,IAAI,MAAM,oCAAoC,MAAM,QAAQ,EAAE;GAGtE,IAAI,MAAM,UAAU,QAAQ,YAAY,IAAI,MAAM,GAChD,MAAM,WAAW,KAAK,MAAM,SAAS,WAAW;GAGlD,MAAM,SAAS;IACb,QAAQ,IAAI;IACZ,QAAQ,IAAI;IACZ,UAAU,IAAI;IACd,SAAS,IAAI;GACf;GACA,IAAI,EAAE,YAAY,IAAI,MAAM,KAAK,CAAC,IAAI;QAKhC,MAJkB,WAAW,GAAG,EAAE,WAC9B,YACA,KACR,GAEE,KAAK,OAAO,IAAI,EAAE;GAAA;GAItB,OAAO;EACT;EACA,aAAa,WAAkC;GAC7C,sBAAsB;GACtB,YAAY;IACV,OAAO,EAAE,MAAM,UAAU;IACzB,SAAS,EAAE,MAAM,SAAS;IAC1B,SAAS;KAAE,SAAS;KAAG,MAAM;IAAS;GACxC;GACA,UAAU,CAAC,SAAS;GACpB,MAAM;EACR,CAAC;CACH,CAAC;AACH;AAEA,eAAe,WACb,KACA,SACA,aACA;CACA,IAAI,aAAa,SACf;CAGF,MAAM,YAAY,KAAK,IAAI,WAAW,KAAQ,GAAO;CACrD,IAAI;CACJ,IAAI;CACJ,MAAM,eAAe,cACjB,IAAI,SAAe,YAAY;EAC7B,gBAAgB;EAChB,YAAY,iBAAiB,SAAS,eAAe,EAAE,MAAM,KAAK,CAAC;CACrE,CAAC,IACD,KAAA;CACJ,IAAI;EACF,MAAM,QAAQ,KAAK;GACjB,IAAI;GACJ,GAAI,eAAe,CAAC,YAAY,IAAI,CAAC;GACrC,IAAI,SAAe,YAAY;IAC7B,YAAY,WAAW,SAAS,SAAS;GAC3C,CAAC;EACH,CAAC;CACH,UAAU;EACR,IAAI,WACF,aAAa,SAAS;EAExB,IAAI,eACF,aAAa,oBAAoB,SAAS,aAAa;CAE3D;AACF"}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { collectSubagentRunWithEvents } from "./subagent-run.js";
|
|
2
|
+
//#region src/subagent-jobs.ts
|
|
3
|
+
const maxBackgroundJobs = 64;
|
|
4
|
+
const maxRetainedBackgroundJobs = maxBackgroundJobs * 4;
|
|
5
|
+
function startBackgroundJob({ abortSignal, description, jobs, parentSession, prompt, registerCleanup, sessionKey, subagent }) {
|
|
6
|
+
const id = `bg_${crypto.randomUUID().replaceAll("-", "")}`;
|
|
7
|
+
const childSessionKey = `${sessionKey}:task:${id}`;
|
|
8
|
+
if (!hasJobCapacity(jobs)) return {
|
|
9
|
+
message: "Background subagent job was not started because the background job limit is full.",
|
|
10
|
+
run_in_background: true,
|
|
11
|
+
status: "cancelled",
|
|
12
|
+
subagent: subagent.name,
|
|
13
|
+
task_id: id
|
|
14
|
+
};
|
|
15
|
+
if (abortSignal.aborted) return {
|
|
16
|
+
message: `Background subagent job ${id} was cancelled before it started.`,
|
|
17
|
+
run_in_background: true,
|
|
18
|
+
status: "cancelled",
|
|
19
|
+
subagent: subagent.name,
|
|
20
|
+
task_id: id
|
|
21
|
+
};
|
|
22
|
+
const childSession = subagent.session(childSessionKey);
|
|
23
|
+
const abort = () => childSession.interrupt();
|
|
24
|
+
abortSignal.addEventListener("abort", abort, { once: true });
|
|
25
|
+
const cleanup = () => childSession.delete();
|
|
26
|
+
const unregisterCleanup = registerCleanup(cleanup);
|
|
27
|
+
const job = {
|
|
28
|
+
abort,
|
|
29
|
+
cleanup,
|
|
30
|
+
description,
|
|
31
|
+
id,
|
|
32
|
+
promise: Promise.resolve(),
|
|
33
|
+
sessionKey: childSessionKey,
|
|
34
|
+
settled: false,
|
|
35
|
+
status: "pending",
|
|
36
|
+
subagent: subagent.name ?? "subagent",
|
|
37
|
+
unregisterCleanup
|
|
38
|
+
};
|
|
39
|
+
job.promise = runBackgroundJob({
|
|
40
|
+
childSession,
|
|
41
|
+
job,
|
|
42
|
+
parentSession,
|
|
43
|
+
prompt
|
|
44
|
+
}).finally(() => {
|
|
45
|
+
abortSignal.removeEventListener("abort", abort);
|
|
46
|
+
job.settled = true;
|
|
47
|
+
});
|
|
48
|
+
jobs.set(id, job);
|
|
49
|
+
parentSession.emitObserverEvent({
|
|
50
|
+
description,
|
|
51
|
+
run_in_background: true,
|
|
52
|
+
subagent: subagent.name ?? "subagent",
|
|
53
|
+
task_id: id,
|
|
54
|
+
type: "subagent-job-start"
|
|
55
|
+
});
|
|
56
|
+
return {
|
|
57
|
+
message: `Background subagent job ${id} started. Use background_output({ task_id: "${id}" }) to retrieve the result.`,
|
|
58
|
+
run_in_background: true,
|
|
59
|
+
status: job.status,
|
|
60
|
+
subagent: subagent.name,
|
|
61
|
+
task_id: id
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
async function runBackgroundJob({ childSession, job, parentSession, prompt }) {
|
|
65
|
+
if (job.status === "cancelled") return;
|
|
66
|
+
job.status = "running";
|
|
67
|
+
try {
|
|
68
|
+
const { result } = await collectSubagentRunWithEvents(await childSession.send(prompt), job.subagent, (event) => emitJobUpdate(parentSession, job, event));
|
|
69
|
+
if (isCancelledJob(job)) return;
|
|
70
|
+
job.result = result;
|
|
71
|
+
job.status = result.result;
|
|
72
|
+
} catch (error) {
|
|
73
|
+
if (isCancelledJob(job)) return;
|
|
74
|
+
const jobError = error instanceof Error ? error : new Error(String(error));
|
|
75
|
+
job.status = "error";
|
|
76
|
+
job.result = {
|
|
77
|
+
error: errorMessage(jobError),
|
|
78
|
+
eventCount: 0,
|
|
79
|
+
result: "error",
|
|
80
|
+
run_in_background: false,
|
|
81
|
+
subagent: job.subagent,
|
|
82
|
+
text: ""
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
if (isCancelledJob(job)) return;
|
|
86
|
+
parentSession.enqueueRuntimeInput({
|
|
87
|
+
text: [
|
|
88
|
+
"<system-reminder>",
|
|
89
|
+
"[SUBAGENT JOB RESULT READY]",
|
|
90
|
+
`Task ID: ${job.id}`,
|
|
91
|
+
`Subagent: ${job.subagent}`,
|
|
92
|
+
`Description: ${sanitizeReminderField(job.description ?? "")}`,
|
|
93
|
+
`Use background_output({ task_id: "${job.id}" }) to retrieve the result.`,
|
|
94
|
+
"</system-reminder>"
|
|
95
|
+
].join("\n"),
|
|
96
|
+
type: "user-text"
|
|
97
|
+
}, "turn-start");
|
|
98
|
+
parentSession.emitObserverEvent({
|
|
99
|
+
error: job.result?.error,
|
|
100
|
+
eventCount: job.result?.eventCount ?? 0,
|
|
101
|
+
status: job.result?.result ?? "error",
|
|
102
|
+
subagent: job.subagent,
|
|
103
|
+
task_id: job.id,
|
|
104
|
+
type: "subagent-job-end"
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
function emitJobUpdate(parentSession, job, event) {
|
|
108
|
+
parentSession.emitObserverEvent({
|
|
109
|
+
eventType: event.type,
|
|
110
|
+
status: job.status,
|
|
111
|
+
subagent: job.subagent,
|
|
112
|
+
task_id: job.id,
|
|
113
|
+
type: "subagent-job-update"
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
function assertBackgroundTaskId(value, toolName) {
|
|
117
|
+
if (value.startsWith("bg_")) return;
|
|
118
|
+
throw new Error(`${toolName} expects a background task_id starting with bg_, not a session key: ${value}`);
|
|
119
|
+
}
|
|
120
|
+
function errorMessage(error) {
|
|
121
|
+
if (error instanceof Error) return error.message;
|
|
122
|
+
return String(error);
|
|
123
|
+
}
|
|
124
|
+
function isActiveJob(status) {
|
|
125
|
+
return status === "pending" || status === "running";
|
|
126
|
+
}
|
|
127
|
+
function isCancelledJob(job) {
|
|
128
|
+
return job.status === "cancelled";
|
|
129
|
+
}
|
|
130
|
+
function hasJobCapacity(jobs) {
|
|
131
|
+
if (jobs.size >= maxRetainedBackgroundJobs) return false;
|
|
132
|
+
let activeJobs = 0;
|
|
133
|
+
for (const job of jobs.values()) if (isActiveJob(job.status) || !job.settled) activeJobs += 1;
|
|
134
|
+
return activeJobs < maxBackgroundJobs;
|
|
135
|
+
}
|
|
136
|
+
function cancelJob(job) {
|
|
137
|
+
job.status = "cancelled";
|
|
138
|
+
job.abort();
|
|
139
|
+
}
|
|
140
|
+
function cleanupJob(job) {
|
|
141
|
+
return job.cleanup().then(() => {
|
|
142
|
+
job.unregisterCleanup?.();
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
function sanitizeReminderField(value) {
|
|
146
|
+
return value.replaceAll("\r", " ").replaceAll("\n", " ").replaceAll("<", "<").replaceAll(">", ">");
|
|
147
|
+
}
|
|
148
|
+
//#endregion
|
|
149
|
+
export { assertBackgroundTaskId, cancelJob, cleanupJob, isActiveJob, startBackgroundJob };
|
|
150
|
+
|
|
151
|
+
//# sourceMappingURL=subagent-jobs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subagent-jobs.js","names":[],"sources":["../src/subagent-jobs.ts"],"sourcesContent":["import type { AgentEvent } from \"./session/events\";\nimport type { AgentInput } from \"./session/input\";\nimport { collectSubagentRunWithEvents } from \"./subagent-run\";\nimport type { RuntimeInputSink, Subagent, SubagentJob } from \"./subagent-types\";\n\nconst maxBackgroundJobs = 64;\nconst maxRetainedBackgroundJobs = maxBackgroundJobs * 4;\n\nexport function startBackgroundJob({\n abortSignal,\n description,\n jobs,\n parentSession,\n prompt,\n registerCleanup,\n sessionKey,\n subagent,\n}: {\n readonly abortSignal: AbortSignal;\n readonly description?: string;\n readonly jobs: Map<string, SubagentJob>;\n readonly parentSession: RuntimeInputSink;\n readonly prompt: AgentInput;\n readonly registerCleanup: (cleanup: () => Promise<void>) => () => void;\n readonly sessionKey: string;\n readonly subagent: Subagent;\n}) {\n const id = `bg_${crypto.randomUUID().replaceAll(\"-\", \"\")}`;\n const childSessionKey = `${sessionKey}:task:${id}`;\n if (!hasJobCapacity(jobs)) {\n return {\n message:\n \"Background subagent job was not started because the background job limit is full.\",\n run_in_background: true,\n status: \"cancelled\",\n subagent: subagent.name,\n task_id: id,\n };\n }\n\n if (abortSignal.aborted) {\n return {\n message: `Background subagent job ${id} was cancelled before it started.`,\n run_in_background: true,\n status: \"cancelled\",\n subagent: subagent.name,\n task_id: id,\n };\n }\n\n const childSession = subagent.session(childSessionKey);\n const abort = () => childSession.interrupt();\n abortSignal.addEventListener(\"abort\", abort, { once: true });\n const cleanup = () => childSession.delete();\n const unregisterCleanup = registerCleanup(cleanup);\n\n const job: SubagentJob = {\n abort,\n cleanup,\n description,\n id,\n promise: Promise.resolve(),\n sessionKey: childSessionKey,\n settled: false,\n status: \"pending\",\n subagent: subagent.name ?? \"subagent\",\n unregisterCleanup,\n };\n job.promise = runBackgroundJob({\n childSession,\n job,\n parentSession,\n prompt,\n }).finally(() => {\n abortSignal.removeEventListener(\"abort\", abort);\n job.settled = true;\n });\n jobs.set(id, job);\n parentSession.emitObserverEvent({\n description,\n run_in_background: true,\n subagent: subagent.name ?? \"subagent\",\n task_id: id,\n type: \"subagent-job-start\",\n });\n\n return {\n message: `Background subagent job ${id} started. Use background_output({ task_id: \"${id}\" }) to retrieve the result.`,\n run_in_background: true,\n status: job.status,\n subagent: subagent.name,\n task_id: id,\n };\n}\n\nasync function runBackgroundJob({\n childSession,\n job,\n parentSession,\n prompt,\n}: {\n readonly childSession: ReturnType<Subagent[\"session\"]>;\n readonly job: SubagentJob;\n readonly parentSession: RuntimeInputSink;\n readonly prompt: AgentInput;\n}): Promise<void> {\n if (job.status === \"cancelled\") {\n return;\n }\n\n job.status = \"running\";\n try {\n const { result } = await collectSubagentRunWithEvents(\n await childSession.send(prompt),\n job.subagent,\n (event) => emitJobUpdate(parentSession, job, event)\n );\n if (isCancelledJob(job)) {\n return;\n }\n job.result = result;\n job.status = result.result;\n } catch (error) {\n if (isCancelledJob(job)) {\n return;\n }\n const jobError = error instanceof Error ? error : new Error(String(error));\n job.status = \"error\";\n job.result = {\n error: errorMessage(jobError),\n eventCount: 0,\n result: \"error\",\n run_in_background: false,\n subagent: job.subagent,\n text: \"\",\n };\n }\n\n if (isCancelledJob(job)) {\n return;\n }\n\n parentSession.enqueueRuntimeInput(\n {\n text: [\n \"<system-reminder>\",\n \"[SUBAGENT JOB RESULT READY]\",\n `Task ID: ${job.id}`,\n `Subagent: ${job.subagent}`,\n `Description: ${sanitizeReminderField(job.description ?? \"\")}`,\n `Use background_output({ task_id: \"${job.id}\" }) to retrieve the result.`,\n \"</system-reminder>\",\n ].join(\"\\n\"),\n type: \"user-text\",\n },\n \"turn-start\"\n );\n parentSession.emitObserverEvent({\n error: job.result?.error,\n eventCount: job.result?.eventCount ?? 0,\n status: job.result?.result ?? \"error\",\n subagent: job.subagent,\n task_id: job.id,\n type: \"subagent-job-end\",\n });\n}\n\nfunction emitJobUpdate(\n parentSession: RuntimeInputSink,\n job: SubagentJob,\n event: AgentEvent\n): void {\n parentSession.emitObserverEvent({\n eventType: event.type,\n status: job.status,\n subagent: job.subagent,\n task_id: job.id,\n type: \"subagent-job-update\" as const,\n });\n}\n\nexport function assertBackgroundTaskId(value: string, toolName: string): void {\n if (value.startsWith(\"bg_\")) {\n return;\n }\n\n throw new Error(\n `${toolName} expects a background task_id starting with bg_, not a session key: ${value}`\n );\n}\n\nfunction errorMessage(error: unknown): string {\n if (error instanceof Error) {\n return error.message;\n }\n\n return String(error);\n}\n\nexport function isActiveJob(status: SubagentJob[\"status\"]): boolean {\n return status === \"pending\" || status === \"running\";\n}\n\nfunction isCancelledJob(job: SubagentJob): boolean {\n return job.status === \"cancelled\";\n}\n\nfunction hasJobCapacity(jobs: Map<string, SubagentJob>): boolean {\n if (jobs.size >= maxRetainedBackgroundJobs) {\n return false;\n }\n\n let activeJobs = 0;\n for (const job of jobs.values()) {\n if (isActiveJob(job.status) || !job.settled) {\n activeJobs += 1;\n }\n }\n\n return activeJobs < maxBackgroundJobs;\n}\n\nexport function cancelJob(job: SubagentJob): void {\n job.status = \"cancelled\";\n job.abort();\n}\n\nexport function cleanupJob(job: SubagentJob): Promise<void> {\n return job.cleanup().then(() => {\n job.unregisterCleanup?.();\n });\n}\n\nfunction sanitizeReminderField(value: string): string {\n return value\n .replaceAll(\"\\r\", \" \")\n .replaceAll(\"\\n\", \" \")\n .replaceAll(\"<\", \"<\")\n .replaceAll(\">\", \">\");\n}\n"],"mappings":";;AAKA,MAAM,oBAAoB;AAC1B,MAAM,4BAA4B,oBAAoB;AAEtD,SAAgB,mBAAmB,EACjC,aACA,aACA,MACA,eACA,QACA,iBACA,YACA,YAUC;CACD,MAAM,KAAK,MAAM,OAAO,WAAW,EAAE,WAAW,KAAK,EAAE;CACvD,MAAM,kBAAkB,GAAG,WAAW,QAAQ;CAC9C,IAAI,CAAC,eAAe,IAAI,GACtB,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AAO,GAC5B,IAAI,YAAY,IAAI,MAAM,KAAK,CAAC,IAAI,SAClC,cAAc;CAIlB,OAAO,aAAa;AACtB;AAEA,SAAgB,UAAU,KAAwB;CAChD,IAAI,SAAS;CACb,IAAI,MAAM;AACZ;AAEA,SAAgB,WAAW,KAAiC;CAC1D,OAAO,IAAI,QAAQ,EAAE,WAAW;EAC9B,IAAI,oBAAoB;CAC1B,CAAC;AACH;AAEA,SAAS,sBAAsB,OAAuB;CACpD,OAAO,MACJ,WAAW,MAAM,GAAG,EACpB,WAAW,MAAM,GAAG,EACpB,WAAW,KAAK,MAAM,EACtB,WAAW,KAAK,MAAM;AAC3B"}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
//#region src/subagent-prompt-schema.ts
|
|
2
|
+
const contentArraySchema = {
|
|
3
|
+
items: { anyOf: [
|
|
4
|
+
{
|
|
5
|
+
additionalProperties: false,
|
|
6
|
+
properties: {
|
|
7
|
+
text: { type: "string" },
|
|
8
|
+
type: { const: "text" }
|
|
9
|
+
},
|
|
10
|
+
required: ["type", "text"],
|
|
11
|
+
type: "object"
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
additionalProperties: false,
|
|
15
|
+
properties: {
|
|
16
|
+
image: { type: "string" },
|
|
17
|
+
mediaType: { type: "string" },
|
|
18
|
+
type: { const: "image" }
|
|
19
|
+
},
|
|
20
|
+
required: ["type", "image"],
|
|
21
|
+
type: "object"
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
additionalProperties: false,
|
|
25
|
+
properties: {
|
|
26
|
+
data: { anyOf: [
|
|
27
|
+
{ type: "string" },
|
|
28
|
+
{
|
|
29
|
+
additionalProperties: false,
|
|
30
|
+
properties: {
|
|
31
|
+
data: { type: "string" },
|
|
32
|
+
type: { const: "data" }
|
|
33
|
+
},
|
|
34
|
+
required: ["type", "data"],
|
|
35
|
+
type: "object"
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
additionalProperties: false,
|
|
39
|
+
properties: {
|
|
40
|
+
reference: {
|
|
41
|
+
additionalProperties: { type: "string" },
|
|
42
|
+
type: "object"
|
|
43
|
+
},
|
|
44
|
+
type: { const: "reference" }
|
|
45
|
+
},
|
|
46
|
+
required: ["type", "reference"],
|
|
47
|
+
type: "object"
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
additionalProperties: false,
|
|
51
|
+
properties: {
|
|
52
|
+
text: { type: "string" },
|
|
53
|
+
type: { const: "text" }
|
|
54
|
+
},
|
|
55
|
+
required: ["type", "text"],
|
|
56
|
+
type: "object"
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
additionalProperties: false,
|
|
60
|
+
properties: {
|
|
61
|
+
type: { const: "url" },
|
|
62
|
+
url: { type: "string" }
|
|
63
|
+
},
|
|
64
|
+
required: ["type", "url"],
|
|
65
|
+
type: "object"
|
|
66
|
+
}
|
|
67
|
+
] },
|
|
68
|
+
filename: { type: "string" },
|
|
69
|
+
mediaType: { type: "string" },
|
|
70
|
+
type: { const: "file" }
|
|
71
|
+
},
|
|
72
|
+
required: [
|
|
73
|
+
"type",
|
|
74
|
+
"data",
|
|
75
|
+
"mediaType"
|
|
76
|
+
],
|
|
77
|
+
type: "object"
|
|
78
|
+
}
|
|
79
|
+
] },
|
|
80
|
+
type: "array"
|
|
81
|
+
};
|
|
82
|
+
const delegatePromptSchema = { anyOf: [
|
|
83
|
+
{ type: "string" },
|
|
84
|
+
{
|
|
85
|
+
items: { type: "string" },
|
|
86
|
+
type: "array"
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
additionalProperties: false,
|
|
90
|
+
properties: {
|
|
91
|
+
text: { anyOf: [{ type: "string" }, {
|
|
92
|
+
items: { type: "string" },
|
|
93
|
+
type: "array"
|
|
94
|
+
}] },
|
|
95
|
+
type: { const: "user-text" }
|
|
96
|
+
},
|
|
97
|
+
required: ["type", "text"],
|
|
98
|
+
type: "object"
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
additionalProperties: false,
|
|
102
|
+
properties: {
|
|
103
|
+
content: contentArraySchema,
|
|
104
|
+
type: { const: "user-message" }
|
|
105
|
+
},
|
|
106
|
+
required: ["type", "content"],
|
|
107
|
+
type: "object"
|
|
108
|
+
},
|
|
109
|
+
contentArraySchema
|
|
110
|
+
] };
|
|
111
|
+
//#endregion
|
|
112
|
+
export { delegatePromptSchema };
|
|
113
|
+
|
|
114
|
+
//# sourceMappingURL=subagent-prompt-schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subagent-prompt-schema.js","names":[],"sources":["../src/subagent-prompt-schema.ts"],"sourcesContent":["const fileDataSchema = {\n anyOf: [\n { type: \"string\" },\n {\n additionalProperties: false,\n properties: {\n data: { type: \"string\" },\n type: { const: \"data\" },\n },\n required: [\"type\", \"data\"],\n type: \"object\",\n },\n {\n additionalProperties: false,\n properties: {\n reference: {\n additionalProperties: { type: \"string\" },\n type: \"object\",\n },\n type: { const: \"reference\" },\n },\n required: [\"type\", \"reference\"],\n type: \"object\",\n },\n {\n additionalProperties: false,\n properties: {\n text: { type: \"string\" },\n type: { const: \"text\" },\n },\n required: [\"type\", \"text\"],\n type: \"object\",\n },\n {\n additionalProperties: false,\n properties: {\n type: { const: \"url\" },\n url: { type: \"string\" },\n },\n required: [\"type\", \"url\"],\n type: \"object\",\n },\n ],\n};\n\nconst contentPartSchema = {\n anyOf: [\n {\n additionalProperties: false,\n properties: {\n text: { type: \"string\" },\n type: { const: \"text\" },\n },\n required: [\"type\", \"text\"],\n type: \"object\",\n },\n {\n additionalProperties: false,\n properties: {\n image: { type: \"string\" },\n mediaType: { type: \"string\" },\n type: { const: \"image\" },\n },\n required: [\"type\", \"image\"],\n type: \"object\",\n },\n {\n additionalProperties: false,\n properties: {\n data: fileDataSchema,\n filename: { type: \"string\" },\n mediaType: { type: \"string\" },\n type: { const: \"file\" },\n },\n required: [\"type\", \"data\", \"mediaType\"],\n type: \"object\",\n },\n ],\n};\n\nconst contentArraySchema = {\n items: contentPartSchema,\n type: \"array\",\n};\n\nexport const delegatePromptSchema = {\n anyOf: [\n { type: \"string\" },\n { items: { type: \"string\" }, type: \"array\" },\n {\n additionalProperties: false,\n properties: {\n text: {\n anyOf: [\n { type: \"string\" },\n { items: { type: \"string\" }, type: \"array\" },\n ],\n },\n type: { const: \"user-text\" },\n },\n required: [\"type\", \"text\"],\n type: \"object\",\n },\n {\n additionalProperties: false,\n properties: {\n content: contentArraySchema,\n type: { const: \"user-message\" },\n },\n required: [\"type\", \"content\"],\n type: \"object\",\n },\n contentArraySchema,\n ],\n};\n"],"mappings":";AAgFA,MAAM,qBAAqB;CACzB,OAAO,EAnCP,OAAO;EACL;GACE,sBAAsB;GACtB,YAAY;IACV,MAAM,EAAE,MAAM,SAAS;IACvB,MAAM,EAAE,OAAO,OAAO;GACxB;GACA,UAAU,CAAC,QAAQ,MAAM;GACzB,MAAM;EACR;EACA;GACE,sBAAsB;GACtB,YAAY;IACV,OAAO,EAAE,MAAM,SAAS;IACxB,WAAW,EAAE,MAAM,SAAS;IAC5B,MAAM,EAAE,OAAO,QAAQ;GACzB;GACA,UAAU,CAAC,QAAQ,OAAO;GAC1B,MAAM;EACR;EACA;GACE,sBAAsB;GACtB,YAAY;IACV,MAAM,EApEZ,OAAO;KACL,EAAE,MAAM,SAAS;KACjB;MACE,sBAAsB;MACtB,YAAY;OACV,MAAM,EAAE,MAAM,SAAS;OACvB,MAAM,EAAE,OAAO,OAAO;MACxB;MACA,UAAU,CAAC,QAAQ,MAAM;MACzB,MAAM;KACR;KACA;MACE,sBAAsB;MACtB,YAAY;OACV,WAAW;QACT,sBAAsB,EAAE,MAAM,SAAS;QACvC,MAAM;OACR;OACA,MAAM,EAAE,OAAO,YAAY;MAC7B;MACA,UAAU,CAAC,QAAQ,WAAW;MAC9B,MAAM;KACR;KACA;MACE,sBAAsB;MACtB,YAAY;OACV,MAAM,EAAE,MAAM,SAAS;OACvB,MAAM,EAAE,OAAO,OAAO;MACxB;MACA,UAAU,CAAC,QAAQ,MAAM;MACzB,MAAM;KACR;KACA;MACE,sBAAsB;MACtB,YAAY;OACV,MAAM,EAAE,OAAO,MAAM;OACrB,KAAK,EAAE,MAAM,SAAS;MACxB;MACA,UAAU,CAAC,QAAQ,KAAK;MACxB,MAAM;KACR;IACF,EA2ByB;IACnB,UAAU,EAAE,MAAM,SAAS;IAC3B,WAAW,EAAE,MAAM,SAAS;IAC5B,MAAM,EAAE,OAAO,OAAO;GACxB;GACA,UAAU;IAAC;IAAQ;IAAQ;GAAW;GACtC,MAAM;EACR;CACF,EAIuB;CACvB,MAAM;AACR;AAEA,MAAa,uBAAuB,EAClC,OAAO;CACL,EAAE,MAAM,SAAS;CACjB;EAAE,OAAO,EAAE,MAAM,SAAS;EAAG,MAAM;CAAQ;CAC3C;EACE,sBAAsB;EACtB,YAAY;GACV,MAAM,EACJ,OAAO,CACL,EAAE,MAAM,SAAS,GACjB;IAAE,OAAO,EAAE,MAAM,SAAS;IAAG,MAAM;GAAQ,CAC7C,EACF;GACA,MAAM,EAAE,OAAO,YAAY;EAC7B;EACA,UAAU,CAAC,QAAQ,MAAM;EACzB,MAAM;CACR;CACA;EACE,sBAAsB;EACtB,YAAY;GACV,SAAS;GACT,MAAM,EAAE,OAAO,eAAe;EAChC;EACA,UAAU,CAAC,QAAQ,SAAS;EAC5B,MAAM;CACR;CACA;AACF,EACF"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
//#region src/subagent-run.ts
|
|
2
|
+
const maxCompactTextLength = 2e4;
|
|
3
|
+
const maxStoredEvents = 200;
|
|
4
|
+
const childSessionKeySuffixPattern = /^[A-Za-z0-9_-]{1,80}$/;
|
|
5
|
+
async function runBlockingDelegation({ abortSignal, prompt, sessionKey, subagent }) {
|
|
6
|
+
const childSession = subagent.session(sessionKey);
|
|
7
|
+
if (abortSignal?.aborted) return {
|
|
8
|
+
eventCount: 0,
|
|
9
|
+
result: "aborted",
|
|
10
|
+
run_in_background: false,
|
|
11
|
+
subagent: subagent.name ?? "subagent",
|
|
12
|
+
text: ""
|
|
13
|
+
};
|
|
14
|
+
const abort = () => childSession.interrupt();
|
|
15
|
+
abortSignal?.addEventListener("abort", abort, { once: true });
|
|
16
|
+
try {
|
|
17
|
+
return await collectSubagentRun(await childSession.send(prompt), subagent.name ?? "subagent");
|
|
18
|
+
} finally {
|
|
19
|
+
abortSignal?.removeEventListener("abort", abort);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
async function collectSubagentRun(run, subagent) {
|
|
23
|
+
return (await collectSubagentRunWithEvents(run, subagent)).result;
|
|
24
|
+
}
|
|
25
|
+
async function collectSubagentRunWithEvents(run, subagent, onEvent) {
|
|
26
|
+
let eventCount = 0;
|
|
27
|
+
let result = "completed";
|
|
28
|
+
const events = [];
|
|
29
|
+
const textParts = [];
|
|
30
|
+
let textLength = 0;
|
|
31
|
+
let textTruncated = false;
|
|
32
|
+
try {
|
|
33
|
+
for await (const event of run.events()) {
|
|
34
|
+
eventCount += 1;
|
|
35
|
+
if (events.length < maxStoredEvents) events.push(event);
|
|
36
|
+
onEvent?.(event);
|
|
37
|
+
if (event.type === "assistant-text") {
|
|
38
|
+
const appended = appendCompactText(textParts, textLength, event.text);
|
|
39
|
+
textLength = appended.length;
|
|
40
|
+
textTruncated ||= appended.truncated;
|
|
41
|
+
} else if (event.type === "turn-abort") result = "aborted";
|
|
42
|
+
else if (event.type === "turn-error") return {
|
|
43
|
+
events,
|
|
44
|
+
result: {
|
|
45
|
+
error: event.message,
|
|
46
|
+
eventCount,
|
|
47
|
+
result: "error",
|
|
48
|
+
run_in_background: false,
|
|
49
|
+
subagent,
|
|
50
|
+
text: compactText(textParts, textTruncated)
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
} catch (error) {
|
|
55
|
+
return {
|
|
56
|
+
events,
|
|
57
|
+
result: {
|
|
58
|
+
error: errorMessage(error),
|
|
59
|
+
eventCount,
|
|
60
|
+
result: "error",
|
|
61
|
+
run_in_background: false,
|
|
62
|
+
subagent,
|
|
63
|
+
text: compactText(textParts, textTruncated)
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
events,
|
|
69
|
+
result: {
|
|
70
|
+
eventCount,
|
|
71
|
+
result,
|
|
72
|
+
run_in_background: false,
|
|
73
|
+
subagent,
|
|
74
|
+
text: compactText(textParts, textTruncated)
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
function defaultChildSessionKey(parentAgentNamespace, parentSessionKey, subagent) {
|
|
79
|
+
return `parent:${parentAgentNamespace}:${parentSessionKey}:subagent:${subagent}`;
|
|
80
|
+
}
|
|
81
|
+
function scopedChildSessionKey({ parentAgentNamespace, parentSessionKey, sessionKey, subagent }) {
|
|
82
|
+
const base = defaultChildSessionKey(parentAgentNamespace, parentSessionKey, subagent);
|
|
83
|
+
if (!sessionKey) return base;
|
|
84
|
+
if (!childSessionKeySuffixPattern.test(sessionKey)) throw new Error("delegate sessionKey must be a short alphanumeric child-session suffix");
|
|
85
|
+
return `${base}:${sessionKey}`;
|
|
86
|
+
}
|
|
87
|
+
function compactText(parts, truncated) {
|
|
88
|
+
const text = parts.join("");
|
|
89
|
+
return truncated ? `${text}…[truncated]` : text;
|
|
90
|
+
}
|
|
91
|
+
function appendCompactText(parts, currentLength, next) {
|
|
92
|
+
if (currentLength >= maxCompactTextLength) return {
|
|
93
|
+
length: currentLength,
|
|
94
|
+
truncated: next.length > 0
|
|
95
|
+
};
|
|
96
|
+
const remaining = maxCompactTextLength - currentLength;
|
|
97
|
+
const chunk = next.length > remaining ? next.slice(0, remaining) : next;
|
|
98
|
+
parts.push(chunk);
|
|
99
|
+
return {
|
|
100
|
+
length: currentLength + chunk.length,
|
|
101
|
+
truncated: next.length > remaining
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function errorMessage(error) {
|
|
105
|
+
if (error instanceof Error) return error.message;
|
|
106
|
+
return String(error);
|
|
107
|
+
}
|
|
108
|
+
//#endregion
|
|
109
|
+
export { collectSubagentRunWithEvents, runBlockingDelegation, scopedChildSessionKey };
|
|
110
|
+
|
|
111
|
+
//# sourceMappingURL=subagent-run.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subagent-run.js","names":[],"sources":["../src/subagent-run.ts"],"sourcesContent":["import type { AgentEvent } from \"./session/events\";\nimport type { AgentInput } from \"./session/input\";\nimport type { AgentRun } from \"./session/run\";\nimport type {\n CompactSubagentResult,\n Subagent,\n SubagentRunResult,\n} from \"./subagent-types\";\n\nconst maxCompactTextLength = 20_000;\nconst maxStoredEvents = 200;\nconst childSessionKeySuffixPattern = /^[A-Za-z0-9_-]{1,80}$/;\n\nexport async function runBlockingDelegation({\n abortSignal,\n prompt,\n sessionKey,\n subagent,\n}: {\n readonly abortSignal?: AbortSignal;\n readonly prompt: AgentInput;\n readonly sessionKey: string;\n readonly subagent: Subagent;\n}): Promise<CompactSubagentResult> {\n const childSession = subagent.session(sessionKey);\n if (abortSignal?.aborted) {\n return {\n eventCount: 0,\n result: \"aborted\",\n run_in_background: false,\n subagent: subagent.name ?? \"subagent\",\n text: \"\",\n };\n }\n\n const abort = () => childSession.interrupt();\n abortSignal?.addEventListener(\"abort\", abort, { once: true });\n try {\n return await collectSubagentRun(\n await childSession.send(prompt),\n subagent.name ?? \"subagent\"\n );\n } finally {\n abortSignal?.removeEventListener(\"abort\", abort);\n }\n}\n\nexport async function collectSubagentRun(\n run: AgentRun,\n subagent: string\n): Promise<CompactSubagentResult> {\n return (await collectSubagentRunWithEvents(run, subagent)).result;\n}\n\nexport async function collectSubagentRunWithEvents(\n run: AgentRun,\n subagent: string,\n onEvent?: (event: AgentEvent) => void\n): Promise<SubagentRunResult> {\n let eventCount = 0;\n let result: CompactSubagentResult[\"result\"] = \"completed\";\n const events: AgentEvent[] = [];\n const textParts: string[] = [];\n let textLength = 0;\n let textTruncated = false;\n\n try {\n for await (const event of run.events()) {\n eventCount += 1;\n if (events.length < maxStoredEvents) {\n events.push(event);\n }\n onEvent?.(event);\n if (event.type === \"assistant-text\") {\n const appended = appendCompactText(textParts, textLength, event.text);\n textLength = appended.length;\n textTruncated ||= appended.truncated;\n } else if (event.type === \"turn-abort\") {\n result = \"aborted\";\n } else if (event.type === \"turn-error\") {\n return {\n events,\n result: {\n error: event.message,\n eventCount,\n result: \"error\",\n run_in_background: false,\n subagent,\n text: compactText(textParts, textTruncated),\n },\n };\n }\n }\n } catch (error) {\n return {\n events,\n result: {\n error: errorMessage(error),\n eventCount,\n result: \"error\",\n run_in_background: false,\n subagent,\n text: compactText(textParts, textTruncated),\n },\n };\n }\n\n return {\n events,\n result: {\n eventCount,\n result,\n run_in_background: false,\n subagent,\n text: compactText(textParts, textTruncated),\n },\n };\n}\n\nexport function defaultChildSessionKey(\n parentAgentNamespace: string,\n parentSessionKey: string,\n subagent: string\n): string {\n return `parent:${parentAgentNamespace}:${parentSessionKey}:subagent:${subagent}`;\n}\n\nexport function scopedChildSessionKey({\n parentAgentNamespace,\n parentSessionKey,\n sessionKey,\n subagent,\n}: {\n readonly parentAgentNamespace: string;\n readonly parentSessionKey: string;\n readonly sessionKey?: string;\n readonly subagent: string;\n}): string {\n const base = defaultChildSessionKey(\n parentAgentNamespace,\n parentSessionKey,\n subagent\n );\n if (!sessionKey) {\n return base;\n }\n\n if (!childSessionKeySuffixPattern.test(sessionKey)) {\n throw new Error(\n \"delegate sessionKey must be a short alphanumeric child-session suffix\"\n );\n }\n\n return `${base}:${sessionKey}`;\n}\n\nfunction compactText(parts: readonly string[], truncated: boolean): string {\n const text = parts.join(\"\");\n return truncated ? `${text}…[truncated]` : text;\n}\n\nfunction appendCompactText(\n parts: string[],\n currentLength: number,\n next: string\n): { readonly length: number; readonly truncated: boolean } {\n if (currentLength >= maxCompactTextLength) {\n return { length: currentLength, truncated: next.length > 0 };\n }\n\n const remaining = maxCompactTextLength - currentLength;\n const chunk = next.length > remaining ? next.slice(0, remaining) : next;\n parts.push(chunk);\n return {\n length: currentLength + chunk.length,\n truncated: next.length > remaining,\n };\n}\n\nfunction errorMessage(error: unknown): string {\n if (error instanceof Error) {\n return error.message;\n }\n\n return String(error);\n}\n"],"mappings":";AASA,MAAM,uBAAuB;AAC7B,MAAM,kBAAkB;AACxB,MAAM,+BAA+B;AAErC,eAAsB,sBAAsB,EAC1C,aACA,QACA,YACA,YAMiC;CACjC,MAAM,eAAe,SAAS,QAAQ,UAAU;CAChD,IAAI,aAAa,SACf,OAAO;EACL,YAAY;EACZ,QAAQ;EACR,mBAAmB;EACnB,UAAU,SAAS,QAAQ;EAC3B,MAAM;CACR;CAGF,MAAM,cAAc,aAAa,UAAU;CAC3C,aAAa,iBAAiB,SAAS,OAAO,EAAE,MAAM,KAAK,CAAC;CAC5D,IAAI;EACF,OAAO,MAAM,mBACX,MAAM,aAAa,KAAK,MAAM,GAC9B,SAAS,QAAQ,UACnB;CACF,UAAU;EACR,aAAa,oBAAoB,SAAS,KAAK;CACjD;AACF;AAEA,eAAsB,mBACpB,KACA,UACgC;CAChC,QAAQ,MAAM,6BAA6B,KAAK,QAAQ,GAAG;AAC7D;AAEA,eAAsB,6BACpB,KACA,UACA,SAC4B;CAC5B,IAAI,aAAa;CACjB,IAAI,SAA0C;CAC9C,MAAM,SAAuB,CAAC;CAC9B,MAAM,YAAsB,CAAC;CAC7B,IAAI,aAAa;CACjB,IAAI,gBAAgB;CAEpB,IAAI;EACF,WAAW,MAAM,SAAS,IAAI,OAAO,GAAG;GACtC,cAAc;GACd,IAAI,OAAO,SAAS,iBAClB,OAAO,KAAK,KAAK;GAEnB,UAAU,KAAK;GACf,IAAI,MAAM,SAAS,kBAAkB;IACnC,MAAM,WAAW,kBAAkB,WAAW,YAAY,MAAM,IAAI;IACpE,aAAa,SAAS;IACtB,kBAAkB,SAAS;GAC7B,OAAO,IAAI,MAAM,SAAS,cACxB,SAAS;QACJ,IAAI,MAAM,SAAS,cACxB,OA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|
|
|
1
|
+
import { normalizeAgentInput } from "./session/input-normalization.js";
|
|
2
|
+
import { runBlockingDelegation, scopedChildSessionKey } from "./subagent-run.js";
|
|
3
|
+
import { startBackgroundJob } from "./subagent-jobs.js";
|
|
4
|
+
import { createBackgroundCancelTool } from "./subagent-job-cancel.js";
|
|
5
|
+
import { createBackgroundOutputTool } from "./subagent-job-output.js";
|
|
6
|
+
import { delegatePromptSchema } from "./subagent-prompt-schema.js";
|
|
7
|
+
import { jsonSchema, tool } from "ai";
|
|
8
|
+
//#region src/subagents.ts
|
|
9
|
+
function createSubagentTools({ parentAgentNamespace, parentSession, parentSessionKey, registerChildSession, subagents }) {
|
|
10
|
+
if (subagents.length === 0) return {};
|
|
11
|
+
const jobs = /* @__PURE__ */ new Map();
|
|
12
|
+
const generatedTools = {
|
|
13
|
+
background_cancel: createBackgroundCancelTool(jobs),
|
|
14
|
+
background_output: createBackgroundOutputTool(jobs)
|
|
15
|
+
};
|
|
16
|
+
for (const subagent of subagents) {
|
|
17
|
+
const name = subagent.name;
|
|
18
|
+
if (!name) continue;
|
|
19
|
+
generatedTools[`delegate_to_${name.replaceAll("-", "_")}`] = createDelegateTool({
|
|
20
|
+
jobs,
|
|
21
|
+
parentAgentNamespace,
|
|
22
|
+
parentSession,
|
|
23
|
+
parentSessionKey,
|
|
24
|
+
registerChildSession,
|
|
25
|
+
subagent
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
return generatedTools;
|
|
29
|
+
}
|
|
30
|
+
function createDelegateTool({ jobs, parentAgentNamespace, parentSession, parentSessionKey, registerChildSession, subagent }) {
|
|
31
|
+
return tool({
|
|
32
|
+
description: `Delegate work to ${subagent.name}: ${subagent.description}`,
|
|
33
|
+
execute: async (input, { abortSignal }) => {
|
|
34
|
+
const prompt = normalizeAgentInput(input.prompt);
|
|
35
|
+
const sessionKey = scopedChildSessionKey({
|
|
36
|
+
parentAgentNamespace,
|
|
37
|
+
parentSessionKey,
|
|
38
|
+
sessionKey: input.sessionKey,
|
|
39
|
+
subagent: subagent.name ?? "subagent"
|
|
40
|
+
});
|
|
41
|
+
if (input.run_in_background === true) return startBackgroundJob({
|
|
42
|
+
abortSignal: abortSignal ?? new AbortController().signal,
|
|
43
|
+
description: input.description,
|
|
44
|
+
jobs,
|
|
45
|
+
parentSession,
|
|
46
|
+
prompt,
|
|
47
|
+
registerCleanup: (cleanup) => registerChildSession(parentSessionKey, cleanup),
|
|
48
|
+
sessionKey,
|
|
49
|
+
subagent
|
|
50
|
+
});
|
|
51
|
+
registerChildSession(parentSessionKey, () => subagent.session(sessionKey).delete());
|
|
52
|
+
parentSession.emitObserverEvent({
|
|
53
|
+
description: input.description,
|
|
54
|
+
run_in_background: false,
|
|
55
|
+
subagent: subagent.name ?? "subagent",
|
|
56
|
+
type: "subagent-job-start"
|
|
57
|
+
});
|
|
58
|
+
const result = await runBlockingDelegation({
|
|
59
|
+
abortSignal,
|
|
60
|
+
prompt,
|
|
61
|
+
sessionKey,
|
|
62
|
+
subagent
|
|
63
|
+
});
|
|
64
|
+
parentSession.emitObserverEvent({
|
|
65
|
+
error: result.error,
|
|
66
|
+
eventCount: result.eventCount,
|
|
67
|
+
status: result.result,
|
|
68
|
+
subagent: subagent.name ?? "subagent",
|
|
69
|
+
type: "subagent-job-end"
|
|
70
|
+
});
|
|
71
|
+
return result;
|
|
72
|
+
},
|
|
73
|
+
inputSchema: jsonSchema({
|
|
74
|
+
additionalProperties: false,
|
|
75
|
+
properties: {
|
|
76
|
+
description: { type: "string" },
|
|
77
|
+
prompt: delegatePromptSchema,
|
|
78
|
+
run_in_background: {
|
|
79
|
+
default: false,
|
|
80
|
+
type: "boolean"
|
|
81
|
+
},
|
|
82
|
+
sessionKey: { type: "string" }
|
|
83
|
+
},
|
|
84
|
+
required: ["prompt"],
|
|
85
|
+
type: "object"
|
|
86
|
+
})
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
//#endregion
|
|
90
|
+
export { createSubagentTools };
|
|
91
|
+
|
|
92
|
+
//# sourceMappingURL=subagents.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subagents.js","names":[],"sources":["../src/subagents.ts"],"sourcesContent":["import { jsonSchema, type ToolSet, tool } from \"ai\";\nimport { normalizeAgentInput } from \"./session/input-normalization\";\nimport { createBackgroundCancelTool } from \"./subagent-job-cancel\";\nimport { createBackgroundOutputTool } from \"./subagent-job-output\";\nimport { startBackgroundJob } from \"./subagent-jobs\";\nimport { delegatePromptSchema } from \"./subagent-prompt-schema\";\nimport { runBlockingDelegation, scopedChildSessionKey } from \"./subagent-run\";\nimport type {\n CreateSubagentToolsOptions,\n DelegateInput,\n RuntimeInputSink,\n Subagent,\n SubagentJob,\n} from \"./subagent-types\";\n\nexport function createSubagentTools({\n parentAgentNamespace,\n parentSession,\n parentSessionKey,\n registerChildSession,\n subagents,\n}: CreateSubagentToolsOptions): ToolSet {\n if (subagents.length === 0) {\n return {};\n }\n\n const jobs = new Map<string, SubagentJob>();\n const generatedTools: Record<string, unknown> = {\n background_cancel: createBackgroundCancelTool(jobs),\n background_output: createBackgroundOutputTool(jobs),\n };\n\n for (const subagent of subagents) {\n const name = subagent.name;\n if (!name) {\n continue;\n }\n\n generatedTools[`delegate_to_${name.replaceAll(\"-\", \"_\")}`] =\n createDelegateTool({\n jobs,\n parentAgentNamespace,\n parentSession,\n parentSessionKey,\n registerChildSession,\n subagent,\n });\n }\n\n return generatedTools as ToolSet;\n}\n\nfunction createDelegateTool({\n jobs,\n parentAgentNamespace,\n parentSession,\n parentSessionKey,\n registerChildSession,\n subagent,\n}: {\n readonly jobs: Map<string, SubagentJob>;\n readonly parentAgentNamespace: string;\n readonly parentSession: RuntimeInputSink;\n readonly parentSessionKey: string;\n readonly registerChildSession: CreateSubagentToolsOptions[\"registerChildSession\"];\n readonly subagent: Subagent;\n}) {\n return tool<DelegateInput, unknown, Record<string, unknown>>({\n description: `Delegate work to ${subagent.name}: ${subagent.description}`,\n execute: async (input: DelegateInput, { abortSignal }) => {\n const prompt = normalizeAgentInput(input.prompt);\n const sessionKey = scopedChildSessionKey({\n parentAgentNamespace,\n parentSessionKey,\n sessionKey: input.sessionKey,\n subagent: subagent.name ?? \"subagent\",\n });\n if (input.run_in_background === true) {\n return startBackgroundJob({\n abortSignal: abortSignal ?? new AbortController().signal,\n description: input.description,\n jobs,\n parentSession,\n prompt,\n registerCleanup: (cleanup) =>\n registerChildSession(parentSessionKey, cleanup),\n sessionKey,\n subagent,\n });\n }\n\n registerChildSession(parentSessionKey, () =>\n subagent.session(sessionKey).delete()\n );\n parentSession.emitObserverEvent({\n description: input.description,\n run_in_background: false,\n subagent: subagent.name ?? \"subagent\",\n type: \"subagent-job-start\",\n });\n const result = await runBlockingDelegation({\n abortSignal,\n prompt,\n sessionKey,\n subagent,\n });\n parentSession.emitObserverEvent({\n error: result.error,\n eventCount: result.eventCount,\n status: result.result,\n subagent: subagent.name ?? \"subagent\",\n type: \"subagent-job-end\",\n });\n return result;\n },\n inputSchema: jsonSchema<DelegateInput>({\n additionalProperties: false,\n properties: {\n description: { type: \"string\" },\n prompt: delegatePromptSchema,\n run_in_background: { default: false, type: \"boolean\" },\n sessionKey: { type: \"string\" },\n },\n required: [\"prompt\"],\n type: \"object\",\n }),\n });\n}\n"],"mappings":";;;;;;;;AAeA,SAAgB,oBAAoB,EAClC,sBACA,eACA,kBACA,sBACA,aACsC;CACtC,IAAI,UAAU,WAAW,GACvB,OAAO,CAAC;CAGV,MAAM,uBAAO,IAAI,IAAyB;CAC1C,MAAM,iBAA0C;EAC9C,mBAAmB,2BAA2B,IAAI;EAClD,mBAAmB,2BAA2B,IAAI;CACpD;CAEA,KAAK,MAAM,YAAY,WAAW;EAChC,MAAM,OAAO,SAAS;EACtB,IAAI,CAAC,MACH;EAGF,eAAe,eAAe,KAAK,WAAW,KAAK,GAAG,OACpD,mBAAmB;GACjB;GACA;GACA;GACA;GACA;GACA;EACF,CAAC;CACL;CAEA,OAAO;AACT;AAEA,SAA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