@earendil-works/pi-agent-core 0.85.1 → 0.86.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 +16 -6
- package/dist/agent-loop.d.ts.map +1 -1
- package/dist/agent-loop.js +64 -22
- package/dist/agent-loop.js.map +1 -1
- package/dist/agent.d.ts +5 -3
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +15 -6
- package/dist/agent.js.map +1 -1
- package/dist/harness/config.d.ts +1 -1
- package/dist/harness/config.d.ts.map +1 -1
- package/dist/harness/config.js +10 -2
- package/dist/harness/config.js.map +1 -1
- package/dist/harness/env/nodejs.d.ts +2 -1
- package/dist/harness/env/nodejs.d.ts.map +1 -1
- package/dist/harness/env/nodejs.js +98 -27
- package/dist/harness/env/nodejs.js.map +1 -1
- package/dist/harness/execution/tools.d.ts +1 -1
- package/dist/harness/execution/tools.d.ts.map +1 -1
- package/dist/harness/execution/tools.js +0 -2
- package/dist/harness/execution/tools.js.map +1 -1
- package/dist/harness/messages.d.ts.map +1 -1
- package/dist/harness/messages.js +1 -0
- package/dist/harness/messages.js.map +1 -1
- package/dist/harness/pico3/bash.d.ts +10 -0
- package/dist/harness/pico3/bash.d.ts.map +1 -0
- package/dist/harness/pico3/bash.js +25 -0
- package/dist/harness/pico3/bash.js.map +1 -0
- package/dist/harness/pico3/bounded.d.ts +21 -0
- package/dist/harness/pico3/bounded.d.ts.map +1 -0
- package/dist/harness/pico3/bounded.js +98 -0
- package/dist/harness/pico3/bounded.js.map +1 -0
- package/dist/harness/pico3/chord.d.ts +41 -0
- package/dist/harness/pico3/chord.d.ts.map +1 -0
- package/dist/harness/pico3/chord.js +130 -0
- package/dist/harness/pico3/chord.js.map +1 -0
- package/dist/harness/pico3/context.d.ts +16 -0
- package/dist/harness/pico3/context.d.ts.map +1 -0
- package/dist/harness/pico3/context.js +88 -0
- package/dist/harness/pico3/context.js.map +1 -0
- package/dist/harness/pico3/harness.d.ts +197 -0
- package/dist/harness/pico3/harness.d.ts.map +1 -0
- package/dist/harness/pico3/harness.js +654 -0
- package/dist/harness/pico3/harness.js.map +1 -0
- package/dist/harness/pico3/hooks.d.ts +10 -0
- package/dist/harness/pico3/hooks.d.ts.map +1 -0
- package/dist/harness/pico3/hooks.js +33 -0
- package/dist/harness/pico3/hooks.js.map +1 -0
- package/dist/harness/pico3/index.d.ts +19 -0
- package/dist/harness/pico3/index.d.ts.map +1 -0
- package/dist/harness/pico3/index.js +15 -0
- package/dist/harness/pico3/index.js.map +1 -0
- package/dist/harness/pico3/jsonl.d.ts +58 -0
- package/dist/harness/pico3/jsonl.d.ts.map +1 -0
- package/dist/harness/pico3/jsonl.js +325 -0
- package/dist/harness/pico3/jsonl.js.map +1 -0
- package/dist/harness/pico3/kinds/collapse.d.ts +49 -0
- package/dist/harness/pico3/kinds/collapse.d.ts.map +1 -0
- package/dist/harness/pico3/kinds/collapse.js +191 -0
- package/dist/harness/pico3/kinds/collapse.js.map +1 -0
- package/dist/harness/pico3/kinds/entries.d.ts +78 -0
- package/dist/harness/pico3/kinds/entries.d.ts.map +1 -0
- package/dist/harness/pico3/kinds/entries.js +13 -0
- package/dist/harness/pico3/kinds/entries.js.map +1 -0
- package/dist/harness/pico3/kinds/frames.d.ts +9 -0
- package/dist/harness/pico3/kinds/frames.d.ts.map +1 -0
- package/dist/harness/pico3/kinds/frames.js +75 -0
- package/dist/harness/pico3/kinds/frames.js.map +1 -0
- package/dist/harness/pico3/kinds/generation.d.ts +94 -0
- package/dist/harness/pico3/kinds/generation.d.ts.map +1 -0
- package/dist/harness/pico3/kinds/generation.js +504 -0
- package/dist/harness/pico3/kinds/generation.js.map +1 -0
- package/dist/harness/pico3/kinds/job.d.ts +44 -0
- package/dist/harness/pico3/kinds/job.d.ts.map +1 -0
- package/dist/harness/pico3/kinds/job.js +143 -0
- package/dist/harness/pico3/kinds/job.js.map +1 -0
- package/dist/harness/pico3/kinds/plugin.d.ts +15 -0
- package/dist/harness/pico3/kinds/plugin.d.ts.map +1 -0
- package/dist/harness/pico3/kinds/plugin.js +35 -0
- package/dist/harness/pico3/kinds/plugin.js.map +1 -0
- package/dist/harness/pico3/kinds/post-tools.d.ts +22 -0
- package/dist/harness/pico3/kinds/post-tools.d.ts.map +1 -0
- package/dist/harness/pico3/kinds/post-tools.js +145 -0
- package/dist/harness/pico3/kinds/post-tools.js.map +1 -0
- package/dist/harness/pico3/kinds/task-api.d.ts +3 -0
- package/dist/harness/pico3/kinds/task-api.d.ts.map +1 -0
- package/dist/harness/pico3/kinds/task-api.js +45 -0
- package/dist/harness/pico3/kinds/task-api.js.map +1 -0
- package/dist/harness/pico3/kinds/tool.d.ts +43 -0
- package/dist/harness/pico3/kinds/tool.d.ts.map +1 -0
- package/dist/harness/pico3/kinds/tool.js +373 -0
- package/dist/harness/pico3/kinds/tool.js.map +1 -0
- package/dist/harness/pico3/membrane.d.ts +23 -0
- package/dist/harness/pico3/membrane.d.ts.map +1 -0
- package/dist/harness/pico3/membrane.js +141 -0
- package/dist/harness/pico3/membrane.js.map +1 -0
- package/dist/harness/pico3/memory.d.ts +53 -0
- package/dist/harness/pico3/memory.d.ts.map +1 -0
- package/dist/harness/pico3/memory.js +264 -0
- package/dist/harness/pico3/memory.js.map +1 -0
- package/dist/harness/pico3/scheduler.d.ts +46 -0
- package/dist/harness/pico3/scheduler.d.ts.map +1 -0
- package/dist/harness/pico3/scheduler.js +438 -0
- package/dist/harness/pico3/scheduler.js.map +1 -0
- package/dist/harness/pico3/session.d.ts +263 -0
- package/dist/harness/pico3/session.d.ts.map +1 -0
- package/dist/harness/pico3/session.js +1347 -0
- package/dist/harness/pico3/session.js.map +1 -0
- package/dist/harness/pico3/system.d.ts +126 -0
- package/dist/harness/pico3/system.d.ts.map +1 -0
- package/dist/harness/pico3/system.js +244 -0
- package/dist/harness/pico3/system.js.map +1 -0
- package/dist/harness/pico3/types.d.ts +973 -0
- package/dist/harness/pico3/types.d.ts.map +1 -0
- package/dist/harness/pico3/types.js +96 -0
- package/dist/harness/pico3/types.js.map +1 -0
- package/dist/harness/pico3/view.d.ts +34 -0
- package/dist/harness/pico3/view.d.ts.map +1 -0
- package/dist/harness/pico3/view.js +417 -0
- package/dist/harness/pico3/view.js.map +1 -0
- package/dist/harness/runtime/drive/boundary.d.ts.map +1 -1
- package/dist/harness/runtime/drive/boundary.js +6 -3
- package/dist/harness/runtime/drive/boundary.js.map +1 -1
- package/dist/harness/runtime/drive/response.d.ts.map +1 -1
- package/dist/harness/runtime/drive/response.js +4 -4
- package/dist/harness/runtime/drive/response.js.map +1 -1
- package/dist/harness/runtime/drive/retry.d.ts +2 -2
- package/dist/harness/runtime/drive/retry.d.ts.map +1 -1
- package/dist/harness/runtime/drive/retry.js +3 -6
- package/dist/harness/runtime/drive/retry.js.map +1 -1
- package/dist/harness/runtime/drive/structural.d.ts.map +1 -1
- package/dist/harness/runtime/drive/structural.js +5 -5
- package/dist/harness/runtime/drive/structural.js.map +1 -1
- package/dist/harness/runtime/drive/tool-placement.d.ts.map +1 -1
- package/dist/harness/runtime/drive/tool-placement.js +2 -27
- package/dist/harness/runtime/drive/tool-placement.js.map +1 -1
- package/dist/harness/runtime/drive/tools.d.ts.map +1 -1
- package/dist/harness/runtime/drive/tools.js.map +1 -1
- package/dist/harness/session/fork-policy.d.ts +20 -12
- package/dist/harness/session/fork-policy.d.ts.map +1 -1
- package/dist/harness/session/fork-policy.js +45 -12
- package/dist/harness/session/fork-policy.js.map +1 -1
- package/dist/harness/session/fork.d.ts +0 -2
- package/dist/harness/session/fork.d.ts.map +1 -1
- package/dist/harness/session/fork.js +20 -68
- package/dist/harness/session/fork.js.map +1 -1
- package/dist/harness/session/in-memory-storage-state.d.ts +5 -5
- package/dist/harness/session/in-memory-storage-state.d.ts.map +1 -1
- package/dist/harness/session/in-memory-storage-state.js +99 -39
- package/dist/harness/session/in-memory-storage-state.js.map +1 -1
- package/dist/harness/session/index.d.ts +1 -1
- package/dist/harness/session/index.d.ts.map +1 -1
- package/dist/harness/session/index.js +1 -1
- package/dist/harness/session/index.js.map +1 -1
- package/dist/harness/session/jsonl/fork.d.ts +38 -0
- package/dist/harness/session/jsonl/fork.d.ts.map +1 -0
- package/dist/harness/session/jsonl/fork.js +264 -0
- package/dist/harness/session/jsonl/fork.js.map +1 -0
- package/dist/harness/session/jsonl/io.d.ts +14 -0
- package/dist/harness/session/jsonl/io.d.ts.map +1 -0
- package/dist/harness/session/jsonl/io.js +86 -0
- package/dist/harness/session/jsonl/io.js.map +1 -0
- package/dist/harness/session/jsonl/legacy-v3.d.ts +33 -8
- package/dist/harness/session/jsonl/legacy-v3.d.ts.map +1 -1
- package/dist/harness/session/jsonl/legacy-v3.js +285 -206
- package/dist/harness/session/jsonl/legacy-v3.js.map +1 -1
- package/dist/harness/session/jsonl/repo.d.ts +2 -1
- package/dist/harness/session/jsonl/repo.d.ts.map +1 -1
- package/dist/harness/session/jsonl/repo.js +36 -20
- package/dist/harness/session/jsonl/repo.js.map +1 -1
- package/dist/harness/session/jsonl/storage.d.ts +4 -5
- package/dist/harness/session/jsonl/storage.d.ts.map +1 -1
- package/dist/harness/session/jsonl/storage.js +39 -113
- package/dist/harness/session/jsonl/storage.js.map +1 -1
- package/dist/harness/session/memory.d.ts +3 -5
- package/dist/harness/session/memory.d.ts.map +1 -1
- package/dist/harness/session/memory.js +9 -14
- package/dist/harness/session/memory.js.map +1 -1
- package/dist/harness/session/testing/conformance/session-repo.d.ts +2 -0
- package/dist/harness/session/testing/conformance/session-repo.d.ts.map +1 -1
- package/dist/harness/session/testing/conformance/session-repo.js +220 -1
- package/dist/harness/session/testing/conformance/session-repo.js.map +1 -1
- package/dist/harness/session/testing/index.d.ts +1 -1
- package/dist/harness/session/testing/index.d.ts.map +1 -1
- package/dist/harness/session/testing/index.js +1 -1
- package/dist/harness/session/testing/index.js.map +1 -1
- package/dist/harness/session/types.d.ts +1 -0
- package/dist/harness/session/types.d.ts.map +1 -1
- package/dist/harness/session/types.js.map +1 -1
- package/dist/harness/types.d.ts +14 -0
- package/dist/harness/types.d.ts.map +1 -1
- package/dist/harness/types.js.map +1 -1
- package/dist/proxy.d.ts +2 -2
- package/dist/proxy.d.ts.map +1 -1
- package/dist/proxy.js.map +1 -1
- package/dist/types.d.ts +30 -14
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +10 -6
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { Kind, KindConfig, ProcessSpec } from "../types.ts";
|
|
2
|
+
export type JobInput = {
|
|
3
|
+
[K in keyof ProcessSpec]: ProcessSpec[K];
|
|
4
|
+
} & {
|
|
5
|
+
notify: boolean;
|
|
6
|
+
rerun: boolean;
|
|
7
|
+
every?: number;
|
|
8
|
+
notBefore?: number;
|
|
9
|
+
};
|
|
10
|
+
export type JobCheckpoint = {
|
|
11
|
+
phase: "waiting";
|
|
12
|
+
untilMs: number;
|
|
13
|
+
occurrence: number;
|
|
14
|
+
} | {
|
|
15
|
+
phase: "spawning";
|
|
16
|
+
key: string;
|
|
17
|
+
occurrence: number;
|
|
18
|
+
} | {
|
|
19
|
+
phase: "running";
|
|
20
|
+
key: string;
|
|
21
|
+
occurrence: number;
|
|
22
|
+
};
|
|
23
|
+
export type JobOutput = {
|
|
24
|
+
stdout?: string;
|
|
25
|
+
stderr?: string;
|
|
26
|
+
droppedStdout?: number;
|
|
27
|
+
droppedStderr?: number;
|
|
28
|
+
exitCode?: number;
|
|
29
|
+
occurrence?: number;
|
|
30
|
+
};
|
|
31
|
+
export type JobResult = {
|
|
32
|
+
exitCode: number;
|
|
33
|
+
occurrences: number;
|
|
34
|
+
stdout: string;
|
|
35
|
+
stderr: string;
|
|
36
|
+
};
|
|
37
|
+
export type JobFailure = {
|
|
38
|
+
reason: "spawn" | "interrupted";
|
|
39
|
+
detail: string;
|
|
40
|
+
};
|
|
41
|
+
export declare const job: Readonly<Kind<JobInput, JobCheckpoint, JobResult, JobFailure, {
|
|
42
|
+
killed: boolean;
|
|
43
|
+
}, object, KindConfig<import("../types.ts").ConfigShape, import("../types.ts").ConfigShape>, JobOutput, import("../types.ts").TaskTx>>;
|
|
44
|
+
//# sourceMappingURL=job.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"job.d.ts","sourceRoot":"","sources":["../../../../src/harness/pico3/kinds/job.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAc,IAAI,EAAE,UAAU,EAAE,WAAW,EAAsC,MAAM,aAAa,CAAC;AAEjH,MAAM,MAAM,QAAQ,GAAG;KAAG,CAAC,IAAI,MAAM,WAAW,GAAG,WAAW,CAAC,CAAC,CAAC;CAAE,GAAG;IACrE,MAAM,EAAE,OAAO,CAAC;IAChB,KAAK,EAAE,OAAO,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AACF,MAAM,MAAM,aAAa,GACtB;IAAE,KAAK,EAAE,SAAS,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,GACzD;IAAE,KAAK,EAAE,UAAU,CAAC;IAAC,GAAG,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,GACtD;IAAE,KAAK,EAAE,SAAS,CAAC;IAAC,GAAG,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,CAAC;AACzD,MAAM,MAAM,SAAS,GAAG;IACvB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AACF,MAAM,MAAM,SAAS,GAAG;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAClG,MAAM,MAAM,UAAU,GAAG;IAAE,MAAM,EAAE,OAAO,GAAG,aAAa,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AA0D7E,eAAO,MAAM,GAAG;;sIAAyB,CAAC","sourcesContent":["import type { Context } from \"@earendil-works/chord\";\nimport type { Completion, Kind, KindConfig, ProcessSpec, ProcessStatus, Runtime, Step, Task } from \"../types.ts\";\n\nexport type JobInput = { [K in keyof ProcessSpec]: ProcessSpec[K] } & {\n\tnotify: boolean;\n\trerun: boolean;\n\tevery?: number;\n\tnotBefore?: number;\n};\nexport type JobCheckpoint =\n\t| { phase: \"waiting\"; untilMs: number; occurrence: number }\n\t| { phase: \"spawning\"; key: string; occurrence: number }\n\t| { phase: \"running\"; key: string; occurrence: number };\nexport type JobOutput = {\n\tstdout?: string;\n\tstderr?: string;\n\tdroppedStdout?: number;\n\tdroppedStderr?: number;\n\texitCode?: number;\n\toccurrence?: number;\n};\nexport type JobResult = { exitCode: number; occurrences: number; stdout: string; stderr: string };\nexport type JobFailure = { reason: \"spawn\" | \"interrupted\"; detail: string };\ntype JobTask = Task<JobInput, JobCheckpoint>;\ntype JobStep = Step<JobCheckpoint, JobResult, JobFailure>;\nconst failed = (reason: JobFailure[\"reason\"], detail: string): Completion<JobResult, JobFailure> => ({\n\tstatus: \"failed\",\n\tfailure: { reason, detail },\n});\nconst notice = (text: string, now: number) => ({\n\tkind: \"pi.notice\",\n\tmodel: [{ role: \"user\" as const, content: text, timestamp: now }],\n});\n\nconst jobKind: Kind<\n\tJobInput,\n\tJobCheckpoint,\n\tJobResult,\n\tJobFailure,\n\t{ killed: boolean },\n\tobject,\n\tKindConfig,\n\tJobOutput\n> = {\n\tname: \"pi.job\",\n\tslot: () => ({}),\n\tdescribe: (task) => ({\n\t\tstage: task.checkpoint?.phase ?? \"starting\",\n\t\t...(task.slot ?? {}),\n\t}),\n\tinflight: [\"spawning\", \"running\"],\n\tphases: {\n\t\tasync waiting(task, runtime, ctx) {\n\t\t\tawait runtime.sleep(task.checkpoint.untilMs, ctx);\n\t\t\treturn spawn(task, task.checkpoint.occurrence, runtime, ctx);\n\t\t},\n\t\tasync spawning(task, runtime, ctx) {\n\t\t\treturn reconcile(task, task.checkpoint, runtime, ctx);\n\t\t},\n\t\tasync running(task, runtime, ctx) {\n\t\t\treturn reconcile(task, task.checkpoint, runtime, ctx);\n\t\t},\n\t},\n\tasync initial(task, runtime, ctx) {\n\t\tif (runtime.processHost === undefined) return { done: () => failed(\"spawn\", \"no process host\") };\n\t\tconst now = runtime.now();\n\t\tconst untilMs = task.input.notBefore ?? now;\n\t\tif (untilMs > now) return { next: { phase: \"waiting\", untilMs, occurrence: 1 } };\n\t\treturn spawn(task, 1, runtime, ctx);\n\t},\n\tasync abort(task, runtime, ctx) {\n\t\tconst checkpoint = task.checkpoint;\n\t\tif (checkpoint === undefined || checkpoint.phase === \"waiting\" || runtime.processHost === undefined)\n\t\t\treturn async () => ({ killed: false });\n\t\tawait runtime.processHost.kill(checkpoint.key, \"SIGTERM\", ctx);\n\t\tawait runtime.sleep(runtime.now() + 5000, ctx);\n\t\tawait runtime.processHost.kill(checkpoint.key, \"SIGKILL\", ctx);\n\t\treturn async () => ({ killed: true });\n\t},\n};\nexport const job = Object.freeze(jobKind);\n\nasync function spawn(task: JobTask, occurrence: number, runtime: Runtime, ctx: Context): Promise<JobStep> {\n\tconst key = `${task.id}:${occurrence}`;\n\tawait runtime.commit((tx) => tx.checkpoint({ phase: \"spawning\", key, occurrence }), ctx);\n\ttry {\n\t\tawait runtime.processHost!.start(key, task.input, ctx);\n\t} catch (error) {\n\t\tif (ctx.abortSignal?.aborted) throw error;\n\t\treturn {\n\t\t\tdone: async (tx, current) => {\n\t\t\t\tif (task.input.notify)\n\t\t\t\t\tawait tx.write(\n\t\t\t\t\t\tcurrent.conversationId,\n\t\t\t\t\t\tnotice(`job ${task.id} failed to start: ${String(error)}`, runtime.now()),\n\t\t\t\t\t);\n\t\t\t\treturn failed(\"spawn\", String(error));\n\t\t\t},\n\t\t};\n\t}\n\tawait runtime.commit((tx) => tx.checkpoint({ phase: \"running\", key, occurrence }), ctx);\n\treturn poll(task, key, occurrence, runtime, ctx);\n}\n\nasync function reconcile(\n\ttask: JobTask,\n\tcheckpoint: Extract<JobCheckpoint, { phase: \"spawning\" | \"running\" }>,\n\truntime: Runtime,\n\tctx: Context,\n): Promise<JobStep> {\n\tif (runtime.processHost === undefined) return { done: () => failed(\"interrupted\", \"no process host after restart\") };\n\tlet status: ProcessStatus;\n\ttry {\n\t\tstatus = await runtime.processHost.status(checkpoint.key, ctx);\n\t} catch (error) {\n\t\treturn { done: () => failed(\"interrupted\", `host status failed: ${String(error)}`) };\n\t}\n\tif (status.status === \"unknown\") {\n\t\tif (!task.input.rerun) return { done: () => failed(\"interrupted\", \"process outcome unknown\") };\n\t\treturn spawn(task, checkpoint.occurrence, runtime, ctx);\n\t}\n\tif (checkpoint.phase === \"spawning\") {\n\t\tawait runtime.commit(\n\t\t\t(tx) => tx.checkpoint({ phase: \"running\", key: checkpoint.key, occurrence: checkpoint.occurrence }),\n\t\t\tctx,\n\t\t);\n\t}\n\treturn poll(task, checkpoint.key, checkpoint.occurrence, runtime, ctx);\n}\n\nasync function poll(task: JobTask, key: string, occurrence: number, runtime: Runtime, ctx: Context): Promise<JobStep> {\n\tfor (let attempt = 0; ; attempt++) {\n\t\tlet status: ProcessStatus;\n\t\ttry {\n\t\t\tstatus = await runtime.processHost!.status(key, ctx);\n\t\t} catch (error) {\n\t\t\treturn { done: () => failed(\"interrupted\", `host status failed: ${String(error)}`) };\n\t\t}\n\t\tif (status.status === \"unknown\") {\n\t\t\tif (!task.input.rerun) return { done: () => failed(\"interrupted\", \"process outcome unknown\") };\n\t\t\treturn spawn(task, occurrence, runtime, ctx);\n\t\t}\n\t\tconst snapshot = status;\n\t\tawait runtime.commit((tx, current) => {\n\t\t\tconst output = tx.slot({ id: current.id, kind: jobKind });\n\t\t\toutput.stdout = snapshot.stdout;\n\t\t\toutput.stderr = snapshot.stderr;\n\t\t\toutput.droppedStdout = snapshot.droppedStdout;\n\t\t\toutput.droppedStderr = snapshot.droppedStderr;\n\t\t\tif (snapshot.status === \"exited\") output.exitCode = snapshot.exitCode;\n\t\t}, ctx);\n\t\tif (snapshot.status === \"exited\") {\n\t\t\tconst { exitCode, stdout, stderr } = snapshot;\n\t\t\tif (task.input.every === undefined) {\n\t\t\t\treturn {\n\t\t\t\t\tdone: async (tx, current) => {\n\t\t\t\t\t\tif (task.input.notify)\n\t\t\t\t\t\t\tawait tx.write(\n\t\t\t\t\t\t\t\tcurrent.conversationId,\n\t\t\t\t\t\t\t\tnotice(`job ${task.id} exited with code ${exitCode}`, runtime.now()),\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\treturn { status: \"completed\", result: { exitCode, occurrences: occurrence, stdout, stderr } };\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t}\n\t\t\tconst untilMs = runtime.now() + task.input.every;\n\t\t\treturn {\n\t\t\t\tnext: async (tx, current) => {\n\t\t\t\t\tif (task.input.notify)\n\t\t\t\t\t\tawait tx.write(\n\t\t\t\t\t\t\tcurrent.conversationId,\n\t\t\t\t\t\t\tnotice(`job ${task.id} occurrence ${occurrence} exited with code ${exitCode}`, runtime.now()),\n\t\t\t\t\t\t);\n\t\t\t\t\tconst output = tx.slot({ id: current.id, kind: jobKind });\n\t\t\t\t\toutput.stdout = \"\";\n\t\t\t\t\toutput.stderr = \"\";\n\t\t\t\t\toutput.droppedStdout = 0;\n\t\t\t\t\toutput.droppedStderr = 0;\n\t\t\t\t\tdelete output.exitCode;\n\t\t\t\t\toutput.occurrence = occurrence + 1;\n\t\t\t\t\treturn { phase: \"waiting\", untilMs, occurrence: occurrence + 1 };\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\t\tawait runtime.sleep(runtime.now() + Math.min(1000, 100 * 2 ** Math.min(attempt, 4)), ctx);\n\t}\n}\n"]}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
const failed = (reason, detail) => ({
|
|
2
|
+
status: "failed",
|
|
3
|
+
failure: { reason, detail },
|
|
4
|
+
});
|
|
5
|
+
const notice = (text, now) => ({
|
|
6
|
+
kind: "pi.notice",
|
|
7
|
+
model: [{ role: "user", content: text, timestamp: now }],
|
|
8
|
+
});
|
|
9
|
+
const jobKind = {
|
|
10
|
+
name: "pi.job",
|
|
11
|
+
slot: () => ({}),
|
|
12
|
+
describe: (task) => ({
|
|
13
|
+
stage: task.checkpoint?.phase ?? "starting",
|
|
14
|
+
...(task.slot ?? {}),
|
|
15
|
+
}),
|
|
16
|
+
inflight: ["spawning", "running"],
|
|
17
|
+
phases: {
|
|
18
|
+
async waiting(task, runtime, ctx) {
|
|
19
|
+
await runtime.sleep(task.checkpoint.untilMs, ctx);
|
|
20
|
+
return spawn(task, task.checkpoint.occurrence, runtime, ctx);
|
|
21
|
+
},
|
|
22
|
+
async spawning(task, runtime, ctx) {
|
|
23
|
+
return reconcile(task, task.checkpoint, runtime, ctx);
|
|
24
|
+
},
|
|
25
|
+
async running(task, runtime, ctx) {
|
|
26
|
+
return reconcile(task, task.checkpoint, runtime, ctx);
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
async initial(task, runtime, ctx) {
|
|
30
|
+
if (runtime.processHost === undefined)
|
|
31
|
+
return { done: () => failed("spawn", "no process host") };
|
|
32
|
+
const now = runtime.now();
|
|
33
|
+
const untilMs = task.input.notBefore ?? now;
|
|
34
|
+
if (untilMs > now)
|
|
35
|
+
return { next: { phase: "waiting", untilMs, occurrence: 1 } };
|
|
36
|
+
return spawn(task, 1, runtime, ctx);
|
|
37
|
+
},
|
|
38
|
+
async abort(task, runtime, ctx) {
|
|
39
|
+
const checkpoint = task.checkpoint;
|
|
40
|
+
if (checkpoint === undefined || checkpoint.phase === "waiting" || runtime.processHost === undefined)
|
|
41
|
+
return async () => ({ killed: false });
|
|
42
|
+
await runtime.processHost.kill(checkpoint.key, "SIGTERM", ctx);
|
|
43
|
+
await runtime.sleep(runtime.now() + 5000, ctx);
|
|
44
|
+
await runtime.processHost.kill(checkpoint.key, "SIGKILL", ctx);
|
|
45
|
+
return async () => ({ killed: true });
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
export const job = Object.freeze(jobKind);
|
|
49
|
+
async function spawn(task, occurrence, runtime, ctx) {
|
|
50
|
+
const key = `${task.id}:${occurrence}`;
|
|
51
|
+
await runtime.commit((tx) => tx.checkpoint({ phase: "spawning", key, occurrence }), ctx);
|
|
52
|
+
try {
|
|
53
|
+
await runtime.processHost.start(key, task.input, ctx);
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
if (ctx.abortSignal?.aborted)
|
|
57
|
+
throw error;
|
|
58
|
+
return {
|
|
59
|
+
done: async (tx, current) => {
|
|
60
|
+
if (task.input.notify)
|
|
61
|
+
await tx.write(current.conversationId, notice(`job ${task.id} failed to start: ${String(error)}`, runtime.now()));
|
|
62
|
+
return failed("spawn", String(error));
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
await runtime.commit((tx) => tx.checkpoint({ phase: "running", key, occurrence }), ctx);
|
|
67
|
+
return poll(task, key, occurrence, runtime, ctx);
|
|
68
|
+
}
|
|
69
|
+
async function reconcile(task, checkpoint, runtime, ctx) {
|
|
70
|
+
if (runtime.processHost === undefined)
|
|
71
|
+
return { done: () => failed("interrupted", "no process host after restart") };
|
|
72
|
+
let status;
|
|
73
|
+
try {
|
|
74
|
+
status = await runtime.processHost.status(checkpoint.key, ctx);
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
return { done: () => failed("interrupted", `host status failed: ${String(error)}`) };
|
|
78
|
+
}
|
|
79
|
+
if (status.status === "unknown") {
|
|
80
|
+
if (!task.input.rerun)
|
|
81
|
+
return { done: () => failed("interrupted", "process outcome unknown") };
|
|
82
|
+
return spawn(task, checkpoint.occurrence, runtime, ctx);
|
|
83
|
+
}
|
|
84
|
+
if (checkpoint.phase === "spawning") {
|
|
85
|
+
await runtime.commit((tx) => tx.checkpoint({ phase: "running", key: checkpoint.key, occurrence: checkpoint.occurrence }), ctx);
|
|
86
|
+
}
|
|
87
|
+
return poll(task, checkpoint.key, checkpoint.occurrence, runtime, ctx);
|
|
88
|
+
}
|
|
89
|
+
async function poll(task, key, occurrence, runtime, ctx) {
|
|
90
|
+
for (let attempt = 0;; attempt++) {
|
|
91
|
+
let status;
|
|
92
|
+
try {
|
|
93
|
+
status = await runtime.processHost.status(key, ctx);
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
return { done: () => failed("interrupted", `host status failed: ${String(error)}`) };
|
|
97
|
+
}
|
|
98
|
+
if (status.status === "unknown") {
|
|
99
|
+
if (!task.input.rerun)
|
|
100
|
+
return { done: () => failed("interrupted", "process outcome unknown") };
|
|
101
|
+
return spawn(task, occurrence, runtime, ctx);
|
|
102
|
+
}
|
|
103
|
+
const snapshot = status;
|
|
104
|
+
await runtime.commit((tx, current) => {
|
|
105
|
+
const output = tx.slot({ id: current.id, kind: jobKind });
|
|
106
|
+
output.stdout = snapshot.stdout;
|
|
107
|
+
output.stderr = snapshot.stderr;
|
|
108
|
+
output.droppedStdout = snapshot.droppedStdout;
|
|
109
|
+
output.droppedStderr = snapshot.droppedStderr;
|
|
110
|
+
if (snapshot.status === "exited")
|
|
111
|
+
output.exitCode = snapshot.exitCode;
|
|
112
|
+
}, ctx);
|
|
113
|
+
if (snapshot.status === "exited") {
|
|
114
|
+
const { exitCode, stdout, stderr } = snapshot;
|
|
115
|
+
if (task.input.every === undefined) {
|
|
116
|
+
return {
|
|
117
|
+
done: async (tx, current) => {
|
|
118
|
+
if (task.input.notify)
|
|
119
|
+
await tx.write(current.conversationId, notice(`job ${task.id} exited with code ${exitCode}`, runtime.now()));
|
|
120
|
+
return { status: "completed", result: { exitCode, occurrences: occurrence, stdout, stderr } };
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
const untilMs = runtime.now() + task.input.every;
|
|
125
|
+
return {
|
|
126
|
+
next: async (tx, current) => {
|
|
127
|
+
if (task.input.notify)
|
|
128
|
+
await tx.write(current.conversationId, notice(`job ${task.id} occurrence ${occurrence} exited with code ${exitCode}`, runtime.now()));
|
|
129
|
+
const output = tx.slot({ id: current.id, kind: jobKind });
|
|
130
|
+
output.stdout = "";
|
|
131
|
+
output.stderr = "";
|
|
132
|
+
output.droppedStdout = 0;
|
|
133
|
+
output.droppedStderr = 0;
|
|
134
|
+
delete output.exitCode;
|
|
135
|
+
output.occurrence = occurrence + 1;
|
|
136
|
+
return { phase: "waiting", untilMs, occurrence: occurrence + 1 };
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
await runtime.sleep(runtime.now() + Math.min(1000, 100 * 2 ** Math.min(attempt, 4)), ctx);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
//# sourceMappingURL=job.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"job.js","sourceRoot":"","sources":["../../../../src/harness/pico3/kinds/job.ts"],"names":[],"mappings":"AAyBA,MAAM,MAAM,GAAG,CAAC,MAA4B,EAAE,MAAc,EAAqC,EAAE,CAAC,CAAC;IACpG,MAAM,EAAE,QAAQ;IAChB,OAAO,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE;CAC3B,CAAC,CAAC;AACH,MAAM,MAAM,GAAG,CAAC,IAAY,EAAE,GAAW,EAAE,EAAE,CAAC,CAAC;IAC9C,IAAI,EAAE,WAAW;IACjB,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,MAAe,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,GAAG,EAAE,CAAC;CACjE,CAAC,CAAC;AAEH,MAAM,OAAO,GAST;IACH,IAAI,EAAE,QAAQ;IACd,IA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AAE,CAAC;oBACnB,MAAM,CAAC,aAAa,GAAG,CAAC,CAAC;oBACzB,MAAM,CAAC,aAAa,GAAG,CAAC,CAAC;oBACzB,OAAO,MAAM,CAAC,QAAQ,CAAC;oBACvB,MAAM,CAAC,UAAU,GAAG,UAAU,GAAG,CAAC,CAAC;oBACnC,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,UAAU,EAAE,UAAU,GAAG,CAAC,EAAE,CAAC;gBAAA,CACjE;aACD,CAAC;QACH,CAAC;QACD,MAAM,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,GAAG,GAAG,CAAC,IAAI,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;IAC3F,CAAC;AAAA,CACD","sourcesContent":["import type { Context } from \"@earendil-works/chord\";\nimport type { Completion, Kind, KindConfig, ProcessSpec, ProcessStatus, Runtime, Step, Task } from \"../types.ts\";\n\nexport type JobInput = { [K in keyof ProcessSpec]: ProcessSpec[K] } & {\n\tnotify: boolean;\n\trerun: boolean;\n\tevery?: number;\n\tnotBefore?: number;\n};\nexport type JobCheckpoint =\n\t| { phase: \"waiting\"; untilMs: number; occurrence: number }\n\t| { phase: \"spawning\"; key: string; occurrence: number }\n\t| { phase: \"running\"; key: string; occurrence: number };\nexport type JobOutput = {\n\tstdout?: string;\n\tstderr?: string;\n\tdroppedStdout?: number;\n\tdroppedStderr?: number;\n\texitCode?: number;\n\toccurrence?: number;\n};\nexport type JobResult = { exitCode: number; occurrences: number; stdout: string; stderr: string };\nexport type JobFailure = { reason: \"spawn\" | \"interrupted\"; detail: string };\ntype JobTask = Task<JobInput, JobCheckpoint>;\ntype JobStep = Step<JobCheckpoint, JobResult, JobFailure>;\nconst failed = (reason: JobFailure[\"reason\"], detail: string): Completion<JobResult, JobFailure> => ({\n\tstatus: \"failed\",\n\tfailure: { reason, detail },\n});\nconst notice = (text: string, now: number) => ({\n\tkind: \"pi.notice\",\n\tmodel: [{ role: \"user\" as const, content: text, timestamp: now }],\n});\n\nconst jobKind: Kind<\n\tJobInput,\n\tJobCheckpoint,\n\tJobResult,\n\tJobFailure,\n\t{ killed: boolean },\n\tobject,\n\tKindConfig,\n\tJobOutput\n> = {\n\tname: \"pi.job\",\n\tslot: () => ({}),\n\tdescribe: (task) => ({\n\t\tstage: task.checkpoint?.phase ?? \"starting\",\n\t\t...(task.slot ?? {}),\n\t}),\n\tinflight: [\"spawning\", \"running\"],\n\tphases: {\n\t\tasync waiting(task, runtime, ctx) {\n\t\t\tawait runtime.sleep(task.checkpoint.untilMs, ctx);\n\t\t\treturn spawn(task, task.checkpoint.occurrence, runtime, ctx);\n\t\t},\n\t\tasync spawning(task, runtime, ctx) {\n\t\t\treturn reconcile(task, task.checkpoint, runtime, ctx);\n\t\t},\n\t\tasync running(task, runtime, ctx) {\n\t\t\treturn reconcile(task, task.checkpoint, runtime, ctx);\n\t\t},\n\t},\n\tasync initial(task, runtime, ctx) {\n\t\tif (runtime.processHost === undefined) return { done: () => failed(\"spawn\", \"no process host\") };\n\t\tconst now = runtime.now();\n\t\tconst untilMs = task.input.notBefore ?? now;\n\t\tif (untilMs > now) return { next: { phase: \"waiting\", untilMs, occurrence: 1 } };\n\t\treturn spawn(task, 1, runtime, ctx);\n\t},\n\tasync abort(task, runtime, ctx) {\n\t\tconst checkpoint = task.checkpoint;\n\t\tif (checkpoint === undefined || checkpoint.phase === \"waiting\" || runtime.processHost === undefined)\n\t\t\treturn async () => ({ killed: false });\n\t\tawait runtime.processHost.kill(checkpoint.key, \"SIGTERM\", ctx);\n\t\tawait runtime.sleep(runtime.now() + 5000, ctx);\n\t\tawait runtime.processHost.kill(checkpoint.key, \"SIGKILL\", ctx);\n\t\treturn async () => ({ killed: true });\n\t},\n};\nexport const job = Object.freeze(jobKind);\n\nasync function spawn(task: JobTask, occurrence: number, runtime: Runtime, ctx: Context): Promise<JobStep> {\n\tconst key = `${task.id}:${occurrence}`;\n\tawait runtime.commit((tx) => tx.checkpoint({ phase: \"spawning\", key, occurrence }), ctx);\n\ttry {\n\t\tawait runtime.processHost!.start(key, task.input, ctx);\n\t} catch (error) {\n\t\tif (ctx.abortSignal?.aborted) throw error;\n\t\treturn {\n\t\t\tdone: async (tx, current) => {\n\t\t\t\tif (task.input.notify)\n\t\t\t\t\tawait tx.write(\n\t\t\t\t\t\tcurrent.conversationId,\n\t\t\t\t\t\tnotice(`job ${task.id} failed to start: ${String(error)}`, runtime.now()),\n\t\t\t\t\t);\n\t\t\t\treturn failed(\"spawn\", String(error));\n\t\t\t},\n\t\t};\n\t}\n\tawait runtime.commit((tx) => tx.checkpoint({ phase: \"running\", key, occurrence }), ctx);\n\treturn poll(task, key, occurrence, runtime, ctx);\n}\n\nasync function reconcile(\n\ttask: JobTask,\n\tcheckpoint: Extract<JobCheckpoint, { phase: \"spawning\" | \"running\" }>,\n\truntime: Runtime,\n\tctx: Context,\n): Promise<JobStep> {\n\tif (runtime.processHost === undefined) return { done: () => failed(\"interrupted\", \"no process host after restart\") };\n\tlet status: ProcessStatus;\n\ttry {\n\t\tstatus = await runtime.processHost.status(checkpoint.key, ctx);\n\t} catch (error) {\n\t\treturn { done: () => failed(\"interrupted\", `host status failed: ${String(error)}`) };\n\t}\n\tif (status.status === \"unknown\") {\n\t\tif (!task.input.rerun) return { done: () => failed(\"interrupted\", \"process outcome unknown\") };\n\t\treturn spawn(task, checkpoint.occurrence, runtime, ctx);\n\t}\n\tif (checkpoint.phase === \"spawning\") {\n\t\tawait runtime.commit(\n\t\t\t(tx) => tx.checkpoint({ phase: \"running\", key: checkpoint.key, occurrence: checkpoint.occurrence }),\n\t\t\tctx,\n\t\t);\n\t}\n\treturn poll(task, checkpoint.key, checkpoint.occurrence, runtime, ctx);\n}\n\nasync function poll(task: JobTask, key: string, occurrence: number, runtime: Runtime, ctx: Context): Promise<JobStep> {\n\tfor (let attempt = 0; ; attempt++) {\n\t\tlet status: ProcessStatus;\n\t\ttry {\n\t\t\tstatus = await runtime.processHost!.status(key, ctx);\n\t\t} catch (error) {\n\t\t\treturn { done: () => failed(\"interrupted\", `host status failed: ${String(error)}`) };\n\t\t}\n\t\tif (status.status === \"unknown\") {\n\t\t\tif (!task.input.rerun) return { done: () => failed(\"interrupted\", \"process outcome unknown\") };\n\t\t\treturn spawn(task, occurrence, runtime, ctx);\n\t\t}\n\t\tconst snapshot = status;\n\t\tawait runtime.commit((tx, current) => {\n\t\t\tconst output = tx.slot({ id: current.id, kind: jobKind });\n\t\t\toutput.stdout = snapshot.stdout;\n\t\t\toutput.stderr = snapshot.stderr;\n\t\t\toutput.droppedStdout = snapshot.droppedStdout;\n\t\t\toutput.droppedStderr = snapshot.droppedStderr;\n\t\t\tif (snapshot.status === \"exited\") output.exitCode = snapshot.exitCode;\n\t\t}, ctx);\n\t\tif (snapshot.status === \"exited\") {\n\t\t\tconst { exitCode, stdout, stderr } = snapshot;\n\t\t\tif (task.input.every === undefined) {\n\t\t\t\treturn {\n\t\t\t\t\tdone: async (tx, current) => {\n\t\t\t\t\t\tif (task.input.notify)\n\t\t\t\t\t\t\tawait tx.write(\n\t\t\t\t\t\t\t\tcurrent.conversationId,\n\t\t\t\t\t\t\t\tnotice(`job ${task.id} exited with code ${exitCode}`, runtime.now()),\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\treturn { status: \"completed\", result: { exitCode, occurrences: occurrence, stdout, stderr } };\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t}\n\t\t\tconst untilMs = runtime.now() + task.input.every;\n\t\t\treturn {\n\t\t\t\tnext: async (tx, current) => {\n\t\t\t\t\tif (task.input.notify)\n\t\t\t\t\t\tawait tx.write(\n\t\t\t\t\t\t\tcurrent.conversationId,\n\t\t\t\t\t\t\tnotice(`job ${task.id} occurrence ${occurrence} exited with code ${exitCode}`, runtime.now()),\n\t\t\t\t\t\t);\n\t\t\t\t\tconst output = tx.slot({ id: current.id, kind: jobKind });\n\t\t\t\t\toutput.stdout = \"\";\n\t\t\t\t\toutput.stderr = \"\";\n\t\t\t\t\toutput.droppedStdout = 0;\n\t\t\t\t\toutput.droppedStderr = 0;\n\t\t\t\t\tdelete output.exitCode;\n\t\t\t\t\toutput.occurrence = occurrence + 1;\n\t\t\t\t\treturn { phase: \"waiting\", untilMs, occurrence: occurrence + 1 };\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\t\tawait runtime.sleep(runtime.now() + Math.min(1000, 100 * 2 ** Math.min(attempt, 4)), ctx);\n\t}\n}\n"]}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type JsonValue, type Kind } from "../types.ts";
|
|
2
|
+
export type PluginInput = {
|
|
3
|
+
handler: string;
|
|
4
|
+
input: JsonValue;
|
|
5
|
+
};
|
|
6
|
+
type PluginCheckpoint = {
|
|
7
|
+
phase: "started";
|
|
8
|
+
};
|
|
9
|
+
type PluginFailure = {
|
|
10
|
+
reason: "missing_handler" | "threw";
|
|
11
|
+
detail: string;
|
|
12
|
+
};
|
|
13
|
+
export declare const plugin: Readonly<Kind<PluginInput, PluginCheckpoint, import("@earendil-works/chord").JsonValue, PluginFailure, null, object, import("../types.ts").KindConfig<import("../types.ts").ConfigShape, import("../types.ts").ConfigShape>, import("../types.ts").JsonObject, import("../types.ts").TaskTx>>;
|
|
14
|
+
export {};
|
|
15
|
+
//# sourceMappingURL=plugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../../../src/harness/pico3/kinds/plugin.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,SAAS,EAAE,KAAK,IAAI,EAAgD,MAAM,aAAa,CAAC;AAGtG,MAAM,MAAM,WAAW,GAAG;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,SAAS,CAAA;CAAE,CAAC;AAChE,KAAK,gBAAgB,GAAG;IAAE,KAAK,EAAE,SAAS,CAAA;CAAE,CAAC;AAC7C,KAAK,aAAa,GAAG;IAAE,MAAM,EAAE,iBAAiB,GAAG,OAAO,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAkB7E,eAAO,MAAM,MAAM,+RAA4B,CAAC","sourcesContent":["import type { Context } from \"@earendil-works/chord\";\nimport { type JsonValue, type Kind, type Runtime, type Step, type Task, toStored } from \"../types.ts\";\nimport { taskApi } from \"./task-api.ts\";\n\nexport type PluginInput = { handler: string; input: JsonValue };\ntype PluginCheckpoint = { phase: \"started\" };\ntype PluginFailure = { reason: \"missing_handler\" | \"threw\"; detail: string };\n\nconst pluginKind: Kind<PluginInput, PluginCheckpoint, JsonValue, PluginFailure, null> = {\n\tname: \"pi.plugin\",\n\tinflight: [\"started\"],\n\tphases: {\n\t\tasync started(task, runtime, ctx) {\n\t\t\treturn run(task, runtime, ctx);\n\t\t},\n\t},\n\tasync initial(task, runtime, ctx) {\n\t\tawait runtime.commit((tx) => tx.checkpoint({ phase: \"started\" }), ctx);\n\t\treturn run(task, runtime, ctx);\n\t},\n\tasync abort() {\n\t\treturn async () => null;\n\t},\n};\nexport const plugin = Object.freeze(pluginKind);\n\nasync function run(\n\ttask: Task<PluginInput, PluginCheckpoint>,\n\truntime: Runtime,\n\tctx: Context,\n): Promise<Step<PluginCheckpoint, JsonValue, PluginFailure>> {\n\tconst handler = runtime.plugins.get(task.input.handler);\n\tif (handler === undefined) {\n\t\treturn { done: () => ({ status: \"failed\", failure: { reason: \"missing_handler\", detail: task.input.handler } }) };\n\t}\n\ttry {\n\t\tconst result = toStored(await handler(task.input.input, taskApi(task, runtime), ctx));\n\t\treturn { done: () => ({ status: \"completed\", result }) };\n\t} catch (error) {\n\t\tif (ctx.abortSignal?.aborted) throw error;\n\t\treturn { done: () => ({ status: \"failed\", failure: { reason: \"threw\", detail: String(error) } }) };\n\t}\n}\n"]}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { toStored } from "../types.js";
|
|
2
|
+
import { taskApi } from "./task-api.js";
|
|
3
|
+
const pluginKind = {
|
|
4
|
+
name: "pi.plugin",
|
|
5
|
+
inflight: ["started"],
|
|
6
|
+
phases: {
|
|
7
|
+
async started(task, runtime, ctx) {
|
|
8
|
+
return run(task, runtime, ctx);
|
|
9
|
+
},
|
|
10
|
+
},
|
|
11
|
+
async initial(task, runtime, ctx) {
|
|
12
|
+
await runtime.commit((tx) => tx.checkpoint({ phase: "started" }), ctx);
|
|
13
|
+
return run(task, runtime, ctx);
|
|
14
|
+
},
|
|
15
|
+
async abort() {
|
|
16
|
+
return async () => null;
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
export const plugin = Object.freeze(pluginKind);
|
|
20
|
+
async function run(task, runtime, ctx) {
|
|
21
|
+
const handler = runtime.plugins.get(task.input.handler);
|
|
22
|
+
if (handler === undefined) {
|
|
23
|
+
return { done: () => ({ status: "failed", failure: { reason: "missing_handler", detail: task.input.handler } }) };
|
|
24
|
+
}
|
|
25
|
+
try {
|
|
26
|
+
const result = toStored(await handler(task.input.input, taskApi(task, runtime), ctx));
|
|
27
|
+
return { done: () => ({ status: "completed", result }) };
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
if (ctx.abortSignal?.aborted)
|
|
31
|
+
throw error;
|
|
32
|
+
return { done: () => ({ status: "failed", failure: { reason: "threw", detail: String(error) } }) };
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../../../src/harness/pico3/kinds/plugin.ts"],"names":[],"mappings":"AACA,OAAO,EAAiE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACtG,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAMxC,MAAM,UAAU,GAAwE;IACvF,IAAI,EAAE,WAAW;IACjB,QAAQ,EAAE,CAAC,SAAS,CAAC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CAAC,CAAC;QACtF,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,CAAC,EAAE,CAAC;IAC1D,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,IAAI,GAAG,CAAC,WAAW,EAAE,OAAO;YAAE,MAAM,KAAK,CAAC;QAC1C,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,OAAO,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,KAAK,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC;IACpG,CAAC;AAAA,CACD","sourcesContent":["import type { Context } from \"@earendil-works/chord\";\nimport { type JsonValue, type Kind, type Runtime, type Step, type Task, toStored } from \"../types.ts\";\nimport { taskApi } from \"./task-api.ts\";\n\nexport type PluginInput = { handler: string; input: JsonValue };\ntype PluginCheckpoint = { phase: \"started\" };\ntype PluginFailure = { reason: \"missing_handler\" | \"threw\"; detail: string };\n\nconst pluginKind: Kind<PluginInput, PluginCheckpoint, JsonValue, PluginFailure, null> = {\n\tname: \"pi.plugin\",\n\tinflight: [\"started\"],\n\tphases: {\n\t\tasync started(task, runtime, ctx) {\n\t\t\treturn run(task, runtime, ctx);\n\t\t},\n\t},\n\tasync initial(task, runtime, ctx) {\n\t\tawait runtime.commit((tx) => tx.checkpoint({ phase: \"started\" }), ctx);\n\t\treturn run(task, runtime, ctx);\n\t},\n\tasync abort() {\n\t\treturn async () => null;\n\t},\n};\nexport const plugin = Object.freeze(pluginKind);\n\nasync function run(\n\ttask: Task<PluginInput, PluginCheckpoint>,\n\truntime: Runtime,\n\tctx: Context,\n): Promise<Step<PluginCheckpoint, JsonValue, PluginFailure>> {\n\tconst handler = runtime.plugins.get(task.input.handler);\n\tif (handler === undefined) {\n\t\treturn { done: () => ({ status: \"failed\", failure: { reason: \"missing_handler\", detail: task.input.handler } }) };\n\t}\n\ttry {\n\t\tconst result = toStored(await handler(task.input.input, taskApi(task, runtime), ctx));\n\t\treturn { done: () => ({ status: \"completed\", result }) };\n\t} catch (error) {\n\t\tif (ctx.abortSignal?.aborted) throw error;\n\t\treturn { done: () => ({ status: \"failed\", failure: { reason: \"threw\", detail: String(error) } }) };\n\t}\n}\n"]}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Context } from "@earendil-works/chord";
|
|
2
|
+
import { type CoreKind, type HookInfo, type Id } from "../types.ts";
|
|
3
|
+
export type PostToolsInput = {
|
|
4
|
+
inputs: Id[];
|
|
5
|
+
assistant: Id;
|
|
6
|
+
tools: Id[];
|
|
7
|
+
};
|
|
8
|
+
export type PostToolsResult = {
|
|
9
|
+
successor?: Id;
|
|
10
|
+
ended?: "terminate" | "handoff";
|
|
11
|
+
};
|
|
12
|
+
export interface PostToolsHooks {
|
|
13
|
+
afterTools(assistant: Id, results: Id[], info: HookInfo, ctx: Context): void | Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
export declare const postToolsConfig: {
|
|
16
|
+
readonly sticky: {
|
|
17
|
+
readonly steeringMode: "all" | "one-at-a-time";
|
|
18
|
+
readonly followUpMode: "all" | "one-at-a-time";
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
export declare const postTools: CoreKind<PostToolsInput, never, PostToolsResult, never, null, PostToolsHooks, typeof postToolsConfig>;
|
|
22
|
+
//# sourceMappingURL=post-tools.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"post-tools.d.ts","sourceRoot":"","sources":["../../../../src/harness/pico3/kinds/post-tools.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAErD,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,QAAQ,EAAE,KAAK,EAAE,EAAyB,MAAM,aAAa,CAAC;AAG3F,MAAM,MAAM,cAAc,GAAG;IAAE,MAAM,EAAE,EAAE,EAAE,CAAC;IAAC,SAAS,EAAE,EAAE,CAAC;IAAC,KAAK,EAAE,EAAE,EAAE,CAAA;CAAE,CAAC;AAC1E,MAAM,MAAM,eAAe,GAAG;IAAE,SAAS,CAAC,EAAE,EAAE,CAAC;IAAC,KAAK,CAAC,EAAE,WAAW,GAAG,SAAS,CAAA;CAAE,CAAC;AAClF,MAAM,WAAW,cAAc;IAC9B,UAAU,CAAC,SAAS,EAAE,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,EAAE,OAAO,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC7F;AACD,eAAO,MAAM,eAAe;;;;;CAKlB,CAAC;AAEX,eAAO,MAAM,SAAS,EAAE,QAAQ,CAC/B,cAAc,EACd,KAAK,EACL,eAAe,EACf,KAAK,EACL,IAAI,EACJ,cAAc,EACd,OAAO,eAAe,CA4ItB,CAAC","sourcesContent":["import type { Context } from \"@earendil-works/chord\";\nimport type { AssistantMessage, ToolCall } from \"@earendil-works/pi-ai\";\nimport { type CoreKind, type HookInfo, type Id, type Stored, toStored } from \"../types.ts\";\nimport type { ToolTaskResult } from \"./tool.ts\";\n\nexport type PostToolsInput = { inputs: Id[]; assistant: Id; tools: Id[] };\nexport type PostToolsResult = { successor?: Id; ended?: \"terminate\" | \"handoff\" };\nexport interface PostToolsHooks {\n\tafterTools(assistant: Id, results: Id[], info: HookInfo, ctx: Context): void | Promise<void>;\n}\nexport const postToolsConfig = {\n\tsticky: {\n\t\tsteeringMode: \"one-at-a-time\" as \"all\" | \"one-at-a-time\",\n\t\tfollowUpMode: \"one-at-a-time\" as \"all\" | \"one-at-a-time\",\n\t},\n} as const;\n\nexport const postTools: CoreKind<\n\tPostToolsInput,\n\tnever,\n\tPostToolsResult,\n\tnever,\n\tnull,\n\tPostToolsHooks,\n\ttypeof postToolsConfig\n> = {\n\tname: \"pi.post_tools\",\n\tturn: true,\n\tconfig: postToolsConfig,\n\tphases: {},\n\n\tasync initial(task, rt, ctx) {\n\t\tconst rows = await rt.commit(async (tx) => {\n\t\t\tconst assistant = await tx.entry(task.input.assistant);\n\t\t\tconst calls = ((assistant?.model?.[0] as Stored<AssistantMessage> | undefined)?.content ?? []).filter(\n\t\t\t\t(content): content is Stored<ToolCall> => content.type === \"toolCall\",\n\t\t\t);\n\t\t\treturn Promise.all(\n\t\t\t\ttask.input.tools.map(async (id, index) => {\n\t\t\t\t\tconst toolTask = (await tx.task(id))!;\n\t\t\t\t\tconst call = calls[index]!;\n\t\t\t\t\tif (toolTask.outcome?.status === \"completed\") {\n\t\t\t\t\t\tconst result = toolTask.outcome.result as ToolTaskResult;\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tcall,\n\t\t\t\t\t\t\tentry: result.entry as Id | undefined,\n\t\t\t\t\t\t\tcontrol: result.control,\n\t\t\t\t\t\t\tmissing: undefined as \"orphaned\" | \"aborted\" | undefined,\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t\tif (toolTask.outcome?.status === \"aborted\") {\n\t\t\t\t\t\tconst aborted = toolTask.outcome.result as { entry?: Id };\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tcall,\n\t\t\t\t\t\t\tentry: aborted.entry,\n\t\t\t\t\t\t\tcontrol: undefined,\n\t\t\t\t\t\t\tmissing: aborted.entry === undefined ? (\"aborted\" as const) : undefined,\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t\treturn { call, entry: undefined, control: undefined, missing: \"orphaned\" as const };\n\t\t\t\t}),\n\t\t\t);\n\t\t}, ctx);\n\t\tconst results = rows.map((row) => row.entry).filter((entry): entry is Id => entry !== undefined);\n\t\tawait rt.hooks.each(ctx, (hook, api) => hook.afterTools?.(task.input.assistant, results, api, ctx));\n\n\t\treturn {\n\t\t\tdone: async (tx, current) => {\n\t\t\t\tconst conversationId = current.conversationId;\n\t\t\t\tconst head = await tx.newestEntry(conversationId, { withHead: true });\n\t\t\t\tconst state = tx.rewindable(conversationId);\n\t\t\t\tconst selectedTools = [...state.selectedTools];\n\t\t\t\tlet terminate = false;\n\t\t\t\tlet handoff: string | undefined;\n\t\t\t\tfor (const row of rows) {\n\t\t\t\t\tfor (const name of row.control?.addTools ?? [])\n\t\t\t\t\t\tif (!selectedTools.includes(name)) selectedTools.push(name);\n\t\t\t\t\tif (row.control?.terminate) terminate = true;\n\t\t\t\t\tif (row.control?.handoff !== undefined) handoff = row.control.handoff;\n\t\t\t\t}\n\t\t\t\tif (selectedTools.length !== state.selectedTools.length) {\n\t\t\t\t\tstate.selectedTools = selectedTools;\n\t\t\t\t\ttx.emit({ type: \"config.changed\", keys: [\"selectedTools\"] });\n\t\t\t\t}\n\t\t\t\tfor (const row of rows) {\n\t\t\t\t\tif (row.missing === undefined) continue;\n\t\t\t\t\ttx.appendEntry(conversationId, {\n\t\t\t\t\t\tkind: \"pi.tool_result\",\n\t\t\t\t\t\tmodel: [\n\t\t\t\t\t\t\ttoStored({\n\t\t\t\t\t\t\t\trole: \"toolResult\" as const,\n\t\t\t\t\t\t\t\ttoolCallId: row.call.id,\n\t\t\t\t\t\t\t\ttoolName: row.call.name,\n\t\t\t\t\t\t\t\tcontent: [{ type: \"text\" as const, text: `Tool result unavailable: task ${row.missing}.` }],\n\t\t\t\t\t\t\t\tisError: true,\n\t\t\t\t\t\t\t\ttimestamp: rt.now(),\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t],\n\t\t\t\t\t\tdata: { diagnostics: [{ severity: \"error\", message: row.missing, code: row.missing }] },\n\t\t\t\t\t});\n\t\t\t\t\ttx.emit({\n\t\t\t\t\t\ttype: \"warning\",\n\t\t\t\t\t\tsource: \"post_tools\",\n\t\t\t\t\t\tmessage: `synthesized ${row.missing} tool result for ${row.call.id}`,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tlet headBoundary = head?.id;\n\t\t\t\tif (handoff !== undefined) {\n\t\t\t\t\theadBoundary = tx.appendEntry(conversationId, {\n\t\t\t\t\t\tkind: \"pi.handoff\",\n\t\t\t\t\t\thead: \"self\",\n\t\t\t\t\t\tmodel: [{ role: \"user\", content: handoff, timestamp: rt.now() }],\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tif (handoff !== undefined || terminate) {\n\t\t\t\t\ttx.sticky(conversationId).turn = { tools: [] };\n\t\t\t\t\tawait tx.resolveInputs(task.input.inputs, { status: \"done\", answer: task.input.assistant });\n\t\t\t\t\ttx.emit({ type: \"turn.ended\", inputs: task.input.inputs, status: \"done\", answer: task.input.assistant });\n\t\t\t\t\tconst { triggers } = await tx.boundary(conversationId, \"final\", headBoundary);\n\t\t\t\t\tif (triggers.length > 0) tx.emit({ type: \"turn.started\", inputs: triggers });\n\t\t\t\t\tconst ended = handoff !== undefined ? \"handoff\" : \"terminate\";\n\t\t\t\t\treturn {\n\t\t\t\t\t\tstatus: \"completed\",\n\t\t\t\t\t\tresult:\n\t\t\t\t\t\t\ttriggers.length > 0\n\t\t\t\t\t\t\t\t? { ended, successor: tx.createTask({ kind: \"pi.generation\", input: { inputs: triggers } }) }\n\t\t\t\t\t\t\t\t: { ended },\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\tconst { triggers, terminated } = await tx.boundary(conversationId, \"postTools\", headBoundary);\n\t\t\t\tif (terminated) {\n\t\t\t\t\ttx.sticky(conversationId).turn = { tools: [] };\n\t\t\t\t\tawait tx.resolveInputs(task.input.inputs, { status: \"unanswered\", reason: \"terminated\" });\n\t\t\t\t\ttx.emit({ type: \"turn.ended\", inputs: task.input.inputs, status: \"unanswered\", reason: \"terminated\" });\n\t\t\t\t\tif (triggers.length > 0) tx.emit({ type: \"turn.started\", inputs: triggers });\n\t\t\t\t\treturn {\n\t\t\t\t\t\tstatus: \"completed\",\n\t\t\t\t\t\tresult:\n\t\t\t\t\t\t\ttriggers.length > 0\n\t\t\t\t\t\t\t\t? { successor: tx.createTask({ kind: \"pi.generation\", input: { inputs: triggers } }) }\n\t\t\t\t\t\t\t\t: {},\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\treturn {\n\t\t\t\t\tstatus: \"completed\",\n\t\t\t\t\tresult: {\n\t\t\t\t\t\tsuccessor: tx.createTask({\n\t\t\t\t\t\t\tkind: \"pi.generation\",\n\t\t\t\t\t\t\tinput: { inputs: [...task.input.inputs, ...triggers] },\n\t\t\t\t\t\t}),\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t},\n\t\t};\n\t},\n\n\tasync abort(task) {\n\t\treturn async (tx, current) => {\n\t\t\ttx.sticky(current.conversationId).turn = { tools: [] };\n\t\t\tawait tx.resolveInputs(task.input.inputs, { status: \"unanswered\", reason: \"aborted\" });\n\t\t\ttx.emit({ type: \"turn.ended\", inputs: task.input.inputs, status: \"unanswered\", reason: \"aborted\" });\n\t\t\treturn null;\n\t\t};\n\t},\n};\n"]}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { toStored } from "../types.js";
|
|
2
|
+
export const postToolsConfig = {
|
|
3
|
+
sticky: {
|
|
4
|
+
steeringMode: "one-at-a-time",
|
|
5
|
+
followUpMode: "one-at-a-time",
|
|
6
|
+
},
|
|
7
|
+
};
|
|
8
|
+
export const postTools = {
|
|
9
|
+
name: "pi.post_tools",
|
|
10
|
+
turn: true,
|
|
11
|
+
config: postToolsConfig,
|
|
12
|
+
phases: {},
|
|
13
|
+
async initial(task, rt, ctx) {
|
|
14
|
+
const rows = await rt.commit(async (tx) => {
|
|
15
|
+
const assistant = await tx.entry(task.input.assistant);
|
|
16
|
+
const calls = (assistant?.model?.[0]?.content ?? []).filter((content) => content.type === "toolCall");
|
|
17
|
+
return Promise.all(task.input.tools.map(async (id, index) => {
|
|
18
|
+
const toolTask = (await tx.task(id));
|
|
19
|
+
const call = calls[index];
|
|
20
|
+
if (toolTask.outcome?.status === "completed") {
|
|
21
|
+
const result = toolTask.outcome.result;
|
|
22
|
+
return {
|
|
23
|
+
call,
|
|
24
|
+
entry: result.entry,
|
|
25
|
+
control: result.control,
|
|
26
|
+
missing: undefined,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
if (toolTask.outcome?.status === "aborted") {
|
|
30
|
+
const aborted = toolTask.outcome.result;
|
|
31
|
+
return {
|
|
32
|
+
call,
|
|
33
|
+
entry: aborted.entry,
|
|
34
|
+
control: undefined,
|
|
35
|
+
missing: aborted.entry === undefined ? "aborted" : undefined,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
return { call, entry: undefined, control: undefined, missing: "orphaned" };
|
|
39
|
+
}));
|
|
40
|
+
}, ctx);
|
|
41
|
+
const results = rows.map((row) => row.entry).filter((entry) => entry !== undefined);
|
|
42
|
+
await rt.hooks.each(ctx, (hook, api) => hook.afterTools?.(task.input.assistant, results, api, ctx));
|
|
43
|
+
return {
|
|
44
|
+
done: async (tx, current) => {
|
|
45
|
+
const conversationId = current.conversationId;
|
|
46
|
+
const head = await tx.newestEntry(conversationId, { withHead: true });
|
|
47
|
+
const state = tx.rewindable(conversationId);
|
|
48
|
+
const selectedTools = [...state.selectedTools];
|
|
49
|
+
let terminate = false;
|
|
50
|
+
let handoff;
|
|
51
|
+
for (const row of rows) {
|
|
52
|
+
for (const name of row.control?.addTools ?? [])
|
|
53
|
+
if (!selectedTools.includes(name))
|
|
54
|
+
selectedTools.push(name);
|
|
55
|
+
if (row.control?.terminate)
|
|
56
|
+
terminate = true;
|
|
57
|
+
if (row.control?.handoff !== undefined)
|
|
58
|
+
handoff = row.control.handoff;
|
|
59
|
+
}
|
|
60
|
+
if (selectedTools.length !== state.selectedTools.length) {
|
|
61
|
+
state.selectedTools = selectedTools;
|
|
62
|
+
tx.emit({ type: "config.changed", keys: ["selectedTools"] });
|
|
63
|
+
}
|
|
64
|
+
for (const row of rows) {
|
|
65
|
+
if (row.missing === undefined)
|
|
66
|
+
continue;
|
|
67
|
+
tx.appendEntry(conversationId, {
|
|
68
|
+
kind: "pi.tool_result",
|
|
69
|
+
model: [
|
|
70
|
+
toStored({
|
|
71
|
+
role: "toolResult",
|
|
72
|
+
toolCallId: row.call.id,
|
|
73
|
+
toolName: row.call.name,
|
|
74
|
+
content: [{ type: "text", text: `Tool result unavailable: task ${row.missing}.` }],
|
|
75
|
+
isError: true,
|
|
76
|
+
timestamp: rt.now(),
|
|
77
|
+
}),
|
|
78
|
+
],
|
|
79
|
+
data: { diagnostics: [{ severity: "error", message: row.missing, code: row.missing }] },
|
|
80
|
+
});
|
|
81
|
+
tx.emit({
|
|
82
|
+
type: "warning",
|
|
83
|
+
source: "post_tools",
|
|
84
|
+
message: `synthesized ${row.missing} tool result for ${row.call.id}`,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
let headBoundary = head?.id;
|
|
88
|
+
if (handoff !== undefined) {
|
|
89
|
+
headBoundary = tx.appendEntry(conversationId, {
|
|
90
|
+
kind: "pi.handoff",
|
|
91
|
+
head: "self",
|
|
92
|
+
model: [{ role: "user", content: handoff, timestamp: rt.now() }],
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
if (handoff !== undefined || terminate) {
|
|
96
|
+
tx.sticky(conversationId).turn = { tools: [] };
|
|
97
|
+
await tx.resolveInputs(task.input.inputs, { status: "done", answer: task.input.assistant });
|
|
98
|
+
tx.emit({ type: "turn.ended", inputs: task.input.inputs, status: "done", answer: task.input.assistant });
|
|
99
|
+
const { triggers } = await tx.boundary(conversationId, "final", headBoundary);
|
|
100
|
+
if (triggers.length > 0)
|
|
101
|
+
tx.emit({ type: "turn.started", inputs: triggers });
|
|
102
|
+
const ended = handoff !== undefined ? "handoff" : "terminate";
|
|
103
|
+
return {
|
|
104
|
+
status: "completed",
|
|
105
|
+
result: triggers.length > 0
|
|
106
|
+
? { ended, successor: tx.createTask({ kind: "pi.generation", input: { inputs: triggers } }) }
|
|
107
|
+
: { ended },
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
const { triggers, terminated } = await tx.boundary(conversationId, "postTools", headBoundary);
|
|
111
|
+
if (terminated) {
|
|
112
|
+
tx.sticky(conversationId).turn = { tools: [] };
|
|
113
|
+
await tx.resolveInputs(task.input.inputs, { status: "unanswered", reason: "terminated" });
|
|
114
|
+
tx.emit({ type: "turn.ended", inputs: task.input.inputs, status: "unanswered", reason: "terminated" });
|
|
115
|
+
if (triggers.length > 0)
|
|
116
|
+
tx.emit({ type: "turn.started", inputs: triggers });
|
|
117
|
+
return {
|
|
118
|
+
status: "completed",
|
|
119
|
+
result: triggers.length > 0
|
|
120
|
+
? { successor: tx.createTask({ kind: "pi.generation", input: { inputs: triggers } }) }
|
|
121
|
+
: {},
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
return {
|
|
125
|
+
status: "completed",
|
|
126
|
+
result: {
|
|
127
|
+
successor: tx.createTask({
|
|
128
|
+
kind: "pi.generation",
|
|
129
|
+
input: { inputs: [...task.input.inputs, ...triggers] },
|
|
130
|
+
}),
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
},
|
|
136
|
+
async abort(task) {
|
|
137
|
+
return async (tx, current) => {
|
|
138
|
+
tx.sticky(current.conversationId).turn = { tools: [] };
|
|
139
|
+
await tx.resolveInputs(task.input.inputs, { status: "unanswered", reason: "aborted" });
|
|
140
|
+
tx.emit({ type: "turn.ended", inputs: task.input.inputs, status: "unanswered", reason: "aborted" });
|
|
141
|
+
return null;
|
|
142
|
+
};
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
//# sourceMappingURL=post-tools.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"post-tools.js","sourceRoot":"","sources":["../../../../src/harness/pico3/kinds/post-tools.ts"],"names":[],"mappings":"AAEA,OAAO,EAAsD,QAAQ,EAAE,MAAM,aAAa,CAAC;AAQ3F,MAAM,CAAC,MAAM,eAAe,GAAG;IAC9B,MAAM,EAAE;QACP,YAAY,EAAE,eAA0C;QACxD,YAAY,EAAE,eAA0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AAC,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,CAAC;YACpG,OAAO,IAAI,CAAC;QAAA,CACZ,CAAC;IAAA,CACF;CACD,CAAC","sourcesContent":["import type { Context } from \"@earendil-works/chord\";\nimport type { AssistantMessage, ToolCall } from \"@earendil-works/pi-ai\";\nimport { type CoreKind, type HookInfo, type Id, type Stored, toStored } from \"../types.ts\";\nimport type { ToolTaskResult } from \"./tool.ts\";\n\nexport type PostToolsInput = { inputs: Id[]; assistant: Id; tools: Id[] };\nexport type PostToolsResult = { successor?: Id; ended?: \"terminate\" | \"handoff\" };\nexport interface PostToolsHooks {\n\tafterTools(assistant: Id, results: Id[], info: HookInfo, ctx: Context): void | Promise<void>;\n}\nexport const postToolsConfig = {\n\tsticky: {\n\t\tsteeringMode: \"one-at-a-time\" as \"all\" | \"one-at-a-time\",\n\t\tfollowUpMode: \"one-at-a-time\" as \"all\" | \"one-at-a-time\",\n\t},\n} as const;\n\nexport const postTools: CoreKind<\n\tPostToolsInput,\n\tnever,\n\tPostToolsResult,\n\tnever,\n\tnull,\n\tPostToolsHooks,\n\ttypeof postToolsConfig\n> = {\n\tname: \"pi.post_tools\",\n\tturn: true,\n\tconfig: postToolsConfig,\n\tphases: {},\n\n\tasync initial(task, rt, ctx) {\n\t\tconst rows = await rt.commit(async (tx) => {\n\t\t\tconst assistant = await tx.entry(task.input.assistant);\n\t\t\tconst calls = ((assistant?.model?.[0] as Stored<AssistantMessage> | undefined)?.content ?? []).filter(\n\t\t\t\t(content): content is Stored<ToolCall> => content.type === \"toolCall\",\n\t\t\t);\n\t\t\treturn Promise.all(\n\t\t\t\ttask.input.tools.map(async (id, index) => {\n\t\t\t\t\tconst toolTask = (await tx.task(id))!;\n\t\t\t\t\tconst call = calls[index]!;\n\t\t\t\t\tif (toolTask.outcome?.status === \"completed\") {\n\t\t\t\t\t\tconst result = toolTask.outcome.result as ToolTaskResult;\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tcall,\n\t\t\t\t\t\t\tentry: result.entry as Id | undefined,\n\t\t\t\t\t\t\tcontrol: result.control,\n\t\t\t\t\t\t\tmissing: undefined as \"orphaned\" | \"aborted\" | undefined,\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t\tif (toolTask.outcome?.status === \"aborted\") {\n\t\t\t\t\t\tconst aborted = toolTask.outcome.result as { entry?: Id };\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tcall,\n\t\t\t\t\t\t\tentry: aborted.entry,\n\t\t\t\t\t\t\tcontrol: undefined,\n\t\t\t\t\t\t\tmissing: aborted.entry === undefined ? (\"aborted\" as const) : undefined,\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t\treturn { call, entry: undefined, control: undefined, missing: \"orphaned\" as const };\n\t\t\t\t}),\n\t\t\t);\n\t\t}, ctx);\n\t\tconst results = rows.map((row) => row.entry).filter((entry): entry is Id => entry !== undefined);\n\t\tawait rt.hooks.each(ctx, (hook, api) => hook.afterTools?.(task.input.assistant, results, api, ctx));\n\n\t\treturn {\n\t\t\tdone: async (tx, current) => {\n\t\t\t\tconst conversationId = current.conversationId;\n\t\t\t\tconst head = await tx.newestEntry(conversationId, { withHead: true });\n\t\t\t\tconst state = tx.rewindable(conversationId);\n\t\t\t\tconst selectedTools = [...state.selectedTools];\n\t\t\t\tlet terminate = false;\n\t\t\t\tlet handoff: string | undefined;\n\t\t\t\tfor (const row of rows) {\n\t\t\t\t\tfor (const name of row.control?.addTools ?? [])\n\t\t\t\t\t\tif (!selectedTools.includes(name)) selectedTools.push(name);\n\t\t\t\t\tif (row.control?.terminate) terminate = true;\n\t\t\t\t\tif (row.control?.handoff !== undefined) handoff = row.control.handoff;\n\t\t\t\t}\n\t\t\t\tif (selectedTools.length !== state.selectedTools.length) {\n\t\t\t\t\tstate.selectedTools = selectedTools;\n\t\t\t\t\ttx.emit({ type: \"config.changed\", keys: [\"selectedTools\"] });\n\t\t\t\t}\n\t\t\t\tfor (const row of rows) {\n\t\t\t\t\tif (row.missing === undefined) continue;\n\t\t\t\t\ttx.appendEntry(conversationId, {\n\t\t\t\t\t\tkind: \"pi.tool_result\",\n\t\t\t\t\t\tmodel: [\n\t\t\t\t\t\t\ttoStored({\n\t\t\t\t\t\t\t\trole: \"toolResult\" as const,\n\t\t\t\t\t\t\t\ttoolCallId: row.call.id,\n\t\t\t\t\t\t\t\ttoolName: row.call.name,\n\t\t\t\t\t\t\t\tcontent: [{ type: \"text\" as const, text: `Tool result unavailable: task ${row.missing}.` }],\n\t\t\t\t\t\t\t\tisError: true,\n\t\t\t\t\t\t\t\ttimestamp: rt.now(),\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t],\n\t\t\t\t\t\tdata: { diagnostics: [{ severity: \"error\", message: row.missing, code: row.missing }] },\n\t\t\t\t\t});\n\t\t\t\t\ttx.emit({\n\t\t\t\t\t\ttype: \"warning\",\n\t\t\t\t\t\tsource: \"post_tools\",\n\t\t\t\t\t\tmessage: `synthesized ${row.missing} tool result for ${row.call.id}`,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tlet headBoundary = head?.id;\n\t\t\t\tif (handoff !== undefined) {\n\t\t\t\t\theadBoundary = tx.appendEntry(conversationId, {\n\t\t\t\t\t\tkind: \"pi.handoff\",\n\t\t\t\t\t\thead: \"self\",\n\t\t\t\t\t\tmodel: [{ role: \"user\", content: handoff, timestamp: rt.now() }],\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tif (handoff !== undefined || terminate) {\n\t\t\t\t\ttx.sticky(conversationId).turn = { tools: [] };\n\t\t\t\t\tawait tx.resolveInputs(task.input.inputs, { status: \"done\", answer: task.input.assistant });\n\t\t\t\t\ttx.emit({ type: \"turn.ended\", inputs: task.input.inputs, status: \"done\", answer: task.input.assistant });\n\t\t\t\t\tconst { triggers } = await tx.boundary(conversationId, \"final\", headBoundary);\n\t\t\t\t\tif (triggers.length > 0) tx.emit({ type: \"turn.started\", inputs: triggers });\n\t\t\t\t\tconst ended = handoff !== undefined ? \"handoff\" : \"terminate\";\n\t\t\t\t\treturn {\n\t\t\t\t\t\tstatus: \"completed\",\n\t\t\t\t\t\tresult:\n\t\t\t\t\t\t\ttriggers.length > 0\n\t\t\t\t\t\t\t\t? { ended, successor: tx.createTask({ kind: \"pi.generation\", input: { inputs: triggers } }) }\n\t\t\t\t\t\t\t\t: { ended },\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\tconst { triggers, terminated } = await tx.boundary(conversationId, \"postTools\", headBoundary);\n\t\t\t\tif (terminated) {\n\t\t\t\t\ttx.sticky(conversationId).turn = { tools: [] };\n\t\t\t\t\tawait tx.resolveInputs(task.input.inputs, { status: \"unanswered\", reason: \"terminated\" });\n\t\t\t\t\ttx.emit({ type: \"turn.ended\", inputs: task.input.inputs, status: \"unanswered\", reason: \"terminated\" });\n\t\t\t\t\tif (triggers.length > 0) tx.emit({ type: \"turn.started\", inputs: triggers });\n\t\t\t\t\treturn {\n\t\t\t\t\t\tstatus: \"completed\",\n\t\t\t\t\t\tresult:\n\t\t\t\t\t\t\ttriggers.length > 0\n\t\t\t\t\t\t\t\t? { successor: tx.createTask({ kind: \"pi.generation\", input: { inputs: triggers } }) }\n\t\t\t\t\t\t\t\t: {},\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\treturn {\n\t\t\t\t\tstatus: \"completed\",\n\t\t\t\t\tresult: {\n\t\t\t\t\t\tsuccessor: tx.createTask({\n\t\t\t\t\t\t\tkind: \"pi.generation\",\n\t\t\t\t\t\t\tinput: { inputs: [...task.input.inputs, ...triggers] },\n\t\t\t\t\t\t}),\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t},\n\t\t};\n\t},\n\n\tasync abort(task) {\n\t\treturn async (tx, current) => {\n\t\t\ttx.sticky(current.conversationId).turn = { tools: [] };\n\t\t\tawait tx.resolveInputs(task.input.inputs, { status: \"unanswered\", reason: \"aborted\" });\n\t\t\ttx.emit({ type: \"turn.ended\", inputs: task.input.inputs, status: \"unanswered\", reason: \"aborted\" });\n\t\t\treturn null;\n\t\t};\n\t},\n};\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"task-api.d.ts","sourceRoot":"","sources":["../../../../src/harness/pico3/kinds/task-api.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAA0B,OAAO,EAAE,IAAI,EAAW,OAAO,EAAE,MAAM,aAAa,CAAC;AAE3F,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,GAAG,OAAO,CAgD7D","sourcesContent":["import type { Context } from \"@earendil-works/chord\";\nimport type { AnyKind, Id, JsonValue, Runtime, Task, TaskRef, ToolApi } from \"../types.ts\";\n\nexport function taskApi(task: Task, runtime: Runtime): ToolApi {\n\tconst isKind = (value: unknown): value is AnyKind =>\n\t\ttypeof value === \"object\" && value !== null && \"name\" in value && \"initial\" in value;\n\treturn {\n\t\ttaskId: task.id,\n\t\tconversationId: task.conversationId,\n\t\tcallId: \"\",\n\t\tstream: () => {\n\t\t\tthrow new Error(\"stream is only available to tools\");\n\t\t},\n\t\tprogress: () => {\n\t\t\tthrow new Error(\"progress is only available to tools\");\n\t\t},\n\t\tmemo: (() => Promise.reject(new Error(\"memo is only available to tools\"))) as ToolApi[\"memo\"],\n\t\tasync conversation(spec, ctx) {\n\t\t\tconst id = await runtime.createOwnedConversation(spec, ctx);\n\t\t\treturn {\n\t\t\t\tid,\n\t\t\t\tsend: async (input, childContext) => {\n\t\t\t\t\tconst inputId = await runtime.sendOwned(id, input, childContext);\n\t\t\t\t\treturn {\n\t\t\t\t\t\tid: inputId,\n\t\t\t\t\t\twait: (waitContext) => runtime.waitForInput(inputId, waitContext),\n\t\t\t\t\t\tresult: (resultContext) => runtime.commit((tx) => tx.input(inputId), resultContext),\n\t\t\t\t\t};\n\t\t\t\t},\n\t\t\t\tabort: (childContext) => runtime.abortConversation(id, childContext),\n\t\t\t};\n\t\t},\n\t\ttask: ((\n\t\t\tkind: AnyKind,\n\t\t\tinput: JsonValue,\n\t\t\topts: { background?: true; after?: Id[] },\n\t\t\tctx: Context,\n\t\t): Promise<TaskRef> => {\n\t\t\tif (!isKind(kind)) throw new Error(\"task(): pass a kind token\");\n\t\t\treturn runtime.commit((tx) => tx.createTask(kind, input as never, opts), ctx);\n\t\t}) as ToolApi[\"task\"],\n\t\tgetTask: async (ref, ctx) => (await runtime.commit((tx) => tx.task(ref.id), ctx)) as never,\n\t\twaitForTask: (ref, ctx) => runtime.waitForTask(ref.id, ctx) as never,\n\t\tslot: (ref, ctx) =>\n\t\t\truntime.commit(async (tx) => {\n\t\t\t\tconst storedTask = await tx.task(ref.id);\n\t\t\t\tif (storedTask === undefined) return undefined;\n\t\t\t\treturn (tx.snapshot({ doc: \"sticky\", conversationId: storedTask.conversationId }).tasks[ref.id] ??\n\t\t\t\t\tundefined) as never;\n\t\t\t}, ctx),\n\t};\n}\n"]}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export function taskApi(task, runtime) {
|
|
2
|
+
const isKind = (value) => typeof value === "object" && value !== null && "name" in value && "initial" in value;
|
|
3
|
+
return {
|
|
4
|
+
taskId: task.id,
|
|
5
|
+
conversationId: task.conversationId,
|
|
6
|
+
callId: "",
|
|
7
|
+
stream: () => {
|
|
8
|
+
throw new Error("stream is only available to tools");
|
|
9
|
+
},
|
|
10
|
+
progress: () => {
|
|
11
|
+
throw new Error("progress is only available to tools");
|
|
12
|
+
},
|
|
13
|
+
memo: (() => Promise.reject(new Error("memo is only available to tools"))),
|
|
14
|
+
async conversation(spec, ctx) {
|
|
15
|
+
const id = await runtime.createOwnedConversation(spec, ctx);
|
|
16
|
+
return {
|
|
17
|
+
id,
|
|
18
|
+
send: async (input, childContext) => {
|
|
19
|
+
const inputId = await runtime.sendOwned(id, input, childContext);
|
|
20
|
+
return {
|
|
21
|
+
id: inputId,
|
|
22
|
+
wait: (waitContext) => runtime.waitForInput(inputId, waitContext),
|
|
23
|
+
result: (resultContext) => runtime.commit((tx) => tx.input(inputId), resultContext),
|
|
24
|
+
};
|
|
25
|
+
},
|
|
26
|
+
abort: (childContext) => runtime.abortConversation(id, childContext),
|
|
27
|
+
};
|
|
28
|
+
},
|
|
29
|
+
task: ((kind, input, opts, ctx) => {
|
|
30
|
+
if (!isKind(kind))
|
|
31
|
+
throw new Error("task(): pass a kind token");
|
|
32
|
+
return runtime.commit((tx) => tx.createTask(kind, input, opts), ctx);
|
|
33
|
+
}),
|
|
34
|
+
getTask: async (ref, ctx) => (await runtime.commit((tx) => tx.task(ref.id), ctx)),
|
|
35
|
+
waitForTask: (ref, ctx) => runtime.waitForTask(ref.id, ctx),
|
|
36
|
+
slot: (ref, ctx) => runtime.commit(async (tx) => {
|
|
37
|
+
const storedTask = await tx.task(ref.id);
|
|
38
|
+
if (storedTask === undefined)
|
|
39
|
+
return undefined;
|
|
40
|
+
return (tx.snapshot({ doc: "sticky", conversationId: storedTask.conversationId }).tasks[ref.id] ??
|
|
41
|
+
undefined);
|
|
42
|
+
}, ctx),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=task-api.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"task-api.js","sourceRoot":"","sources":["../../../../src/harness/pico3/kinds/task-api.ts"],"names":[],"mappings":"AAGA,MAAM,UAAU,OAAO,CAAC,IAAU,EAAE,OAAgB,EAAW;IAC9D,MAAM,MAAM,GAAG,CAAC,KAAc,EAAoB,EAAE,CACnD,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,MAAM,IAAI,KAAK,IAAI,SAAS,IAAI,KAAK,CAAC;IACtF,OAAO;QACN,MAAM,EAAE,IAAI,CAAC,EAAE;QACf,cAAc,EAAE,IAAI,CAAC,cAAc;QACnC,MAAM,EAAE,EAAE;QACV,MAAM,EAAE,GAAG,EAAE,CAAC;YACb,MAAM,IAAI,KAAK,CAAC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AC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,UAAU,CAAC,IAAI,EAAE,KAAc,EAAE,IAAI,CAAC,EAAE,GAAG,CAAC,CAAC;QAAA,CAC9E,CAAoB;QACrB,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC,MAAM,OAAO,CAAC,MAAM,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,GAAG,CAAC,CAAU;QAC1F,WAAW,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC,OAAO,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,EAAE,GAAG,CAAU;QACpE,IAAI,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,CAClB,OAAO,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,EAAE,EAAE,CAAC;YAC5B,MAAM,UAAU,GAAG,MAAM,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;YACzC,IAAI,UAAU,KAAK,SAAS;gBAAE,OAAO,SAAS,CAAC;YAC/C,OAAO,CAAC,EAAE,CAAC,QAAQ,CAAC,EAAE,GAAG,EAAE,QAAQ,EAAE,cAAc,EAAE,UAAU,CAAC,cAAc,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;gBAC9F,SAAS,CAAU,CAAC;QAAA,CACrB,EAAE,GAAG,CAAC;KACR,CAAC;AAAA,CACF","sourcesContent":["import type { Context } from \"@earendil-works/chord\";\nimport type { AnyKind, Id, JsonValue, Runtime, Task, TaskRef, ToolApi } from \"../types.ts\";\n\nexport function taskApi(task: Task, runtime: Runtime): ToolApi {\n\tconst isKind = (value: unknown): value is AnyKind =>\n\t\ttypeof value === \"object\" && value !== null && \"name\" in value && \"initial\" in value;\n\treturn {\n\t\ttaskId: task.id,\n\t\tconversationId: task.conversationId,\n\t\tcallId: \"\",\n\t\tstream: () => {\n\t\t\tthrow new Error(\"stream is only available to tools\");\n\t\t},\n\t\tprogress: () => {\n\t\t\tthrow new Error(\"progress is only available to tools\");\n\t\t},\n\t\tmemo: (() => Promise.reject(new Error(\"memo is only available to tools\"))) as ToolApi[\"memo\"],\n\t\tasync conversation(spec, ctx) {\n\t\t\tconst id = await runtime.createOwnedConversation(spec, ctx);\n\t\t\treturn {\n\t\t\t\tid,\n\t\t\t\tsend: async (input, childContext) => {\n\t\t\t\t\tconst inputId = await runtime.sendOwned(id, input, childContext);\n\t\t\t\t\treturn {\n\t\t\t\t\t\tid: inputId,\n\t\t\t\t\t\twait: (waitContext) => runtime.waitForInput(inputId, waitContext),\n\t\t\t\t\t\tresult: (resultContext) => runtime.commit((tx) => tx.input(inputId), resultContext),\n\t\t\t\t\t};\n\t\t\t\t},\n\t\t\t\tabort: (childContext) => runtime.abortConversation(id, childContext),\n\t\t\t};\n\t\t},\n\t\ttask: ((\n\t\t\tkind: AnyKind,\n\t\t\tinput: JsonValue,\n\t\t\topts: { background?: true; after?: Id[] },\n\t\t\tctx: Context,\n\t\t): Promise<TaskRef> => {\n\t\t\tif (!isKind(kind)) throw new Error(\"task(): pass a kind token\");\n\t\t\treturn runtime.commit((tx) => tx.createTask(kind, input as never, opts), ctx);\n\t\t}) as ToolApi[\"task\"],\n\t\tgetTask: async (ref, ctx) => (await runtime.commit((tx) => tx.task(ref.id), ctx)) as never,\n\t\twaitForTask: (ref, ctx) => runtime.waitForTask(ref.id, ctx) as never,\n\t\tslot: (ref, ctx) =>\n\t\t\truntime.commit(async (tx) => {\n\t\t\t\tconst storedTask = await tx.task(ref.id);\n\t\t\t\tif (storedTask === undefined) return undefined;\n\t\t\t\treturn (tx.snapshot({ doc: \"sticky\", conversationId: storedTask.conversationId }).tasks[ref.id] ??\n\t\t\t\t\tundefined) as never;\n\t\t\t}, ctx),\n\t};\n}\n"]}
|