@op0ai/sdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/LICENSE +38 -0
  2. package/README.md +28 -0
  3. package/dist/agent.d.ts +23 -0
  4. package/dist/agent.d.ts.map +1 -0
  5. package/dist/agent.js +72 -0
  6. package/dist/agent.js.map +1 -0
  7. package/dist/agui.d.ts +87 -0
  8. package/dist/agui.d.ts.map +1 -0
  9. package/dist/agui.js +22 -0
  10. package/dist/agui.js.map +1 -0
  11. package/dist/config.d.ts +76 -0
  12. package/dist/config.d.ts.map +1 -0
  13. package/dist/config.js +38 -0
  14. package/dist/config.js.map +1 -0
  15. package/dist/data.d.ts +168 -0
  16. package/dist/data.d.ts.map +1 -0
  17. package/dist/data.js +211 -0
  18. package/dist/data.js.map +1 -0
  19. package/dist/define.d.ts +30 -0
  20. package/dist/define.d.ts.map +1 -0
  21. package/dist/define.js +87 -0
  22. package/dist/define.js.map +1 -0
  23. package/dist/distill.d.ts +68 -0
  24. package/dist/distill.d.ts.map +1 -0
  25. package/dist/distill.js +113 -0
  26. package/dist/distill.js.map +1 -0
  27. package/dist/events.d.ts +39 -0
  28. package/dist/events.d.ts.map +1 -0
  29. package/dist/events.js +25 -0
  30. package/dist/events.js.map +1 -0
  31. package/dist/evlog.d.ts +71 -0
  32. package/dist/evlog.d.ts.map +1 -0
  33. package/dist/evlog.js +124 -0
  34. package/dist/evlog.js.map +1 -0
  35. package/dist/guards.d.ts +61 -0
  36. package/dist/guards.d.ts.map +1 -0
  37. package/dist/guards.js +88 -0
  38. package/dist/guards.js.map +1 -0
  39. package/dist/hash.d.ts +6 -0
  40. package/dist/hash.d.ts.map +1 -0
  41. package/dist/hash.js +18 -0
  42. package/dist/hash.js.map +1 -0
  43. package/dist/index.d.ts +120 -0
  44. package/dist/index.d.ts.map +1 -0
  45. package/dist/index.js +175 -0
  46. package/dist/index.js.map +1 -0
  47. package/dist/platform.d.ts +97 -0
  48. package/dist/platform.d.ts.map +1 -0
  49. package/dist/platform.js +10 -0
  50. package/dist/platform.js.map +1 -0
  51. package/dist/session.d.ts +47 -0
  52. package/dist/session.d.ts.map +1 -0
  53. package/dist/session.js +88 -0
  54. package/dist/session.js.map +1 -0
  55. package/dist/source.d.ts +65 -0
  56. package/dist/source.d.ts.map +1 -0
  57. package/dist/source.js +96 -0
  58. package/dist/source.js.map +1 -0
  59. package/dist/tool.d.ts +43 -0
  60. package/dist/tool.d.ts.map +1 -0
  61. package/dist/tool.js +16 -0
  62. package/dist/tool.js.map +1 -0
  63. package/dist/usectx.d.ts +93 -0
  64. package/dist/usectx.d.ts.map +1 -0
  65. package/dist/usectx.js +220 -0
  66. package/dist/usectx.js.map +1 -0
  67. package/dist/vault.d.ts +21 -0
  68. package/dist/vault.d.ts.map +1 -0
  69. package/dist/vault.js +25 -0
  70. package/dist/vault.js.map +1 -0
  71. package/package.json +75 -0
package/LICENSE ADDED
@@ -0,0 +1,38 @@
1
+ op0 SDK & CLI — Proprietary License
2
+ Copyright (c) 2026 op0 (op0.ai). All rights reserved.
3
+
4
+ This software (the "op0 SDK", the `op0` package, including its SDK library and
5
+ CLI) is licensed, not sold. It is NOT open source.
6
+
7
+ 1. GRANT (use-with-op0). You are granted a non-exclusive, non-transferable,
8
+ revocable license to install and use this software SOLELY to build, deploy,
9
+ and operate agents on the op0 runtime — whether the managed op0 cloud
10
+ (op0.ai) or an op0 runtime you self-host under a separate commercial license
11
+ from op0. All normal development use in service of that purpose (local dev,
12
+ CI, running the CLI, importing the SDK into your agent code) is permitted.
13
+
14
+ 2. RESTRICTIONS. You may not, except as this License expressly permits:
15
+ (a) redistribute, resell, sublicense, or publish this software or substantial
16
+ portions of it as a standalone package or SDK;
17
+ (b) use it to build, train, or operate a runtime or platform that competes
18
+ with the op0 runtime, or to reimplement the op0 runtime API for a
19
+ competing service;
20
+ (c) remove or obscure this notice, or represent the software as open source.
21
+
22
+ 3. SELF-HOSTING. Running the op0 runtime outside the managed op0 cloud requires
23
+ a separate commercial license. Contact arth@op0.ai.
24
+
25
+ 4. WHAT IS OPEN. This License covers the SDK and CLI only. op0's examples,
26
+ patterns, and templates are published separately under their own permissive
27
+ (MIT) licenses — see the op0 repositories and LICENSING.md. Nothing here
28
+ restricts those.
29
+
30
+ 5. NO WARRANTY. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31
+ EXPRESS OR IMPLIED. IN NO EVENT SHALL op0 BE LIABLE FOR ANY CLAIM, DAMAGES,
32
+ OR OTHER LIABILITY ARISING FROM THE SOFTWARE OR ITS USE.
33
+
34
+ 6. TERMINATION. This License terminates automatically if you breach it. On
35
+ termination you must cease use and destroy your copies.
36
+
37
+ op0 may release future versions under different terms (including a delayed-open
38
+ license) based on usage patterns; that is a possibility, not a commitment.
package/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # @op0ai/sdk
2
+
3
+ The **op0 SDK** — an agent with a computer: a governed function under a **RunSpec**
4
+ (goal + stop rules + verifier), typed ports, a vendor-free core. Agents built with it
5
+ run on [op0](https://op0.ai), the governed runtime for agent work.
6
+
7
+ ```sh
8
+ npm i @op0ai/sdk # pnpm add / yarn add / bun add @op0ai/sdk
9
+ ```
10
+
11
+ ```ts
12
+ import { defineCordConfig } from "@op0ai/sdk";
13
+
14
+ export default defineCordConfig({
15
+ id: "my-agent",
16
+ charter: "Answer tide-time questions, briefly, with units.",
17
+ runSpec: {
18
+ goal: "answer tersely",
19
+ stop: { budget: 500, steps: 4 }, // halts BEFORE any model spend
20
+ },
21
+ });
22
+ ```
23
+
24
+ Scaffold a full project: `npm create @op0ai my-agent` · CLI: [`@op0ai/cli`](https://www.npmjs.com/package/@op0ai/cli) · Docs: [op0.ai/docs](https://op0.ai/docs)
25
+
26
+ **License**: proprietary — free to use *with* the op0 runtime (managed, or self-hosted under a
27
+ commercial license). Not open source; see LICENSE. What teaches op0 (examples, patterns,
28
+ templates) is published separately under MIT.
@@ -0,0 +1,23 @@
1
+ import type { Inference, Harness, ToolSpec, ToolCall, Resources } from "./platform.ts";
2
+ /**
3
+ * minimalHarness — model → (tool calls)* → final text, bounded by maxSteps. The agent reasons freely but can
4
+ * only ACT through the `tools` it's given (which are the cord's governed ports — see portsAsTools), so an
5
+ * agent-ful cord stays inside its declared scope by construction, not by prompt.
6
+ */
7
+ export declare const minimalHarness: (inference: Inference) => Harness;
8
+ /**
9
+ * fakeInference — a scripted model for TDD (no network, deterministic). Pass the exact turn sequence the loop
10
+ * should see: each entry is either tool calls or final text. This is how the agent loop is unit-tested; the
11
+ * real adapter (AI Gateway / Anthropic) plugs into the same Inference port at deploy.
12
+ */
13
+ export declare const fakeInference: (script: Array<{
14
+ text?: string;
15
+ toolCalls?: ToolCall[];
16
+ }>) => Inference;
17
+ /**
18
+ * portsAsTools — expose selected resource ports to the agent as callable tools. THIS is governed-tools = ports:
19
+ * the agent can only act through capabilities the cord declared + the host bound. `onTool` lets the cord audit
20
+ * every call (one wide-event per action). ponytail: only the ports you pass become tools — nothing else.
21
+ */
22
+ export declare const portsAsTools: (res: Resources, onTool?: (name: string, args: Record<string, unknown>) => void) => ToolSpec[];
23
+ //# sourceMappingURL=agent.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAW,SAAS,EAAE,MAAM,eAAe,CAAC;AAEhG;;;;GAIG;AACH,eAAO,MAAM,cAAc,GAAI,WAAW,SAAS,KAAG,OA8BpD,CAAC;AAEH;;;;GAIG;AACH,eAAO,MAAM,aAAa,GAAI,QAAQ,KAAK,CAAC;IAAE,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,QAAQ,EAAE,CAAA;CAAE,CAAC,KAAG,SAGxF,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,YAAY,GAAI,KAAK,SAAS,EAAE,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,IAAI,KAAG,QAAQ,EAoBrH,CAAC"}
package/dist/agent.js ADDED
@@ -0,0 +1,72 @@
1
+ /**
2
+ * minimalHarness — model → (tool calls)* → final text, bounded by maxSteps. The agent reasons freely but can
3
+ * only ACT through the `tools` it's given (which are the cord's governed ports — see portsAsTools), so an
4
+ * agent-ful cord stays inside its declared scope by construction, not by prompt.
5
+ */
6
+ export const minimalHarness = (inference) => ({
7
+ async run({ instructions, input, tools, maxSteps = 8 }) {
8
+ const messages = [
9
+ { role: "system", content: instructions },
10
+ { role: "user", content: input },
11
+ ];
12
+ const byName = new Map(tools.map((t) => [t.name, t]));
13
+ // Outer loop: genuinely sequential — each step's model call reads `messages`, which only gains this
14
+ // step's tool results after they're appended below; there is nothing to await concurrently across steps.
15
+ for (let step = 0; step < maxSteps; step++) {
16
+ // oxlint-disable-next-line no-await-in-loop
17
+ const r = await inference.generate({ messages, tools });
18
+ if (r.toolCalls && r.toolCalls.length > 0) {
19
+ // one assistant turn carrying the tool_calls, then one tool turn per result (OpenAI convention; the
20
+ // adapter maps it to the provider wire format). Unknown tools are surfaced, never thrown — self-correct.
21
+ messages.push({ role: "assistant", content: r.text ?? "", toolCalls: r.toolCalls });
22
+ // Inner loop: kept sequential too — tools are governed ports (kv_incr etc.) whose side effects can
23
+ // be order-sensitive; parallelizing would reorder effects the model issued in a specific sequence.
24
+ for (const tc of r.toolCalls) {
25
+ const tool = byName.get(tc.name);
26
+ // oxlint-disable-next-line no-await-in-loop
27
+ const result = tool ? await tool.run(tc.args) : { error: `unknown tool: ${tc.name}` };
28
+ messages.push({ role: "tool", content: JSON.stringify(result), toolCallId: tc.id });
29
+ }
30
+ continue; // let the model see the tool results and decide next
31
+ }
32
+ return { output: r.text ?? "", steps: step + 1 };
33
+ }
34
+ throw new Error(`harness: maxSteps ${maxSteps} exhausted without a final answer`);
35
+ },
36
+ });
37
+ /**
38
+ * fakeInference — a scripted model for TDD (no network, deterministic). Pass the exact turn sequence the loop
39
+ * should see: each entry is either tool calls or final text. This is how the agent loop is unit-tested; the
40
+ * real adapter (AI Gateway / Anthropic) plugs into the same Inference port at deploy.
41
+ */
42
+ export const fakeInference = (script) => {
43
+ let i = 0;
44
+ return { async generate() { return script[i++] ?? { text: "(script exhausted)" }; } };
45
+ };
46
+ /**
47
+ * portsAsTools — expose selected resource ports to the agent as callable tools. THIS is governed-tools = ports:
48
+ * the agent can only act through capabilities the cord declared + the host bound. `onTool` lets the cord audit
49
+ * every call (one wide-event per action). ponytail: only the ports you pass become tools — nothing else.
50
+ */
51
+ export const portsAsTools = (res, onTool) => {
52
+ const tools = [];
53
+ if (res.kv) {
54
+ tools.push({
55
+ name: "kv_incr",
56
+ description: "Increment a durable counter by key. args: { key: string, by?: number }",
57
+ parameters: { type: "object", properties: { key: { type: "string" }, by: { type: "number" } }, required: ["key"] },
58
+ run: async (a) => { onTool?.("kv_incr", a); return { count: await res.kv.incr(String(a['key']), typeof a['by'] === "number" ? a['by'] : 1) }; },
59
+ });
60
+ }
61
+ if (res.vault) {
62
+ tools.push({
63
+ name: "vault_put",
64
+ description: "Persist a deliverable. args: { key: string, body: string }",
65
+ parameters: { type: "object", properties: { key: { type: "string" }, body: { type: "string" } }, required: ["key", "body"] },
66
+ run: async (a) => { onTool?.("vault_put", a); return await res.vault.put(String(a['key']), String(a['body'])); },
67
+ });
68
+ }
69
+ // add source/sql/vector tools here as a cord needs them (ponytail: not before)
70
+ return tools;
71
+ };
72
+ //# sourceMappingURL=agent.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent.js","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAOA;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,SAAoB,EAAW,EAAE,CAAC,CAAC;IAChE,KAAK,CAAC,GAAG,CAAC,E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AAC,EAAE,EAAE,GAAG,MAAM,EAAE,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO,EAAE,KAAK,EAAE,MAAM,GAAG,CAAC,EAAG,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;SACjJ,CAAC,CAAC;IACL,CAAC;IACD,IAAI,GAAG,CAAC,KAAK,EAAE,CAAC;QACd,KAAK,CAAC,IAAI,CAAC;YACT,IAAI,EAAE,WAAW;YACjB,WAAW,EAAE,4DAA4D;YACzE,UAAU,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,UAAU,EAAE,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC,EAAE;YAC5H,GAAG,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,GAAG,MAAM,EAAE,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO,MAAM,GAAG,CAAC,KAAM,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;SAClH,CAAC,CAAC;IACL,CAAC;IACD,+EAA+E;IAC/E,OAAO,KAAK,CAAC;AACf,CAAC,CAAC"}
package/dist/agui.d.ts ADDED
@@ -0,0 +1,87 @@
1
+ /** AG-UI-aligned event kinds we emit. (Names mirror the AG-UI protocol so the wire stays standard.) */
2
+ export type CordEvent = {
3
+ type: "RUN_STARTED";
4
+ runId: string;
5
+ threadId: string;
6
+ ts: number;
7
+ } | {
8
+ type: "TEXT_MESSAGE_CONTENT";
9
+ runId: string;
10
+ delta: string;
11
+ ts: number;
12
+ } | {
13
+ type: "TOOL_CALL_START";
14
+ runId: string;
15
+ toolCallId: string;
16
+ name: string;
17
+ args: unknown;
18
+ ts: number;
19
+ } | {
20
+ type: "TOOL_CALL_RESULT";
21
+ runId: string;
22
+ toolCallId: string;
23
+ name: string;
24
+ ok: boolean;
25
+ result?: unknown;
26
+ error?: string;
27
+ ts: number;
28
+ } | {
29
+ type: "SOURCE_STAGED";
30
+ runId: string;
31
+ path: string;
32
+ ts: number;
33
+ } | {
34
+ type: "SOURCE_COMMITTED";
35
+ runId: string;
36
+ paths: readonly string[];
37
+ external: string;
38
+ ts: number;
39
+ } | {
40
+ type: "STATE_DELTA";
41
+ runId: string;
42
+ patch: ReadonlyArray<{
43
+ op: "add" | "replace" | "remove";
44
+ path: string;
45
+ value?: unknown;
46
+ }>;
47
+ ts: number;
48
+ } | {
49
+ type: "RUN_FINISHED";
50
+ runId: string;
51
+ ts: number;
52
+ } | {
53
+ type: "RUN_ERROR";
54
+ runId: string;
55
+ error: string;
56
+ ts: number;
57
+ };
58
+ export type CordEventType = CordEvent["type"];
59
+ /** A sink receives every event — the host fans it to the durable log (persist) and the live client stream. */
60
+ export type CordEventSink = (event: CordEvent) => void;
61
+ /**
62
+ * A per-run emitter. Stamps runId + ts so call sites stay terse, and each helper is typed to its event's payload
63
+ * (no stringly-typed `emit("tool", {...})`). The host supplies the sink (DO append + WebSocket broadcast).
64
+ */
65
+ export interface Emitter {
66
+ readonly runId: string;
67
+ started(threadId: string): void;
68
+ text(delta: string): void;
69
+ toolStart(toolCallId: string, name: string, args: unknown): void;
70
+ toolResult(r: {
71
+ toolCallId: string;
72
+ name: string;
73
+ ok: boolean;
74
+ result?: unknown;
75
+ error?: string;
76
+ }): void;
77
+ sourceStaged(path: string): void;
78
+ sourceCommitted(paths: readonly string[], external: string): void;
79
+ stateDelta(patch: CordEvent extends {
80
+ type: "STATE_DELTA";
81
+ patch: infer P;
82
+ } ? P : never): void;
83
+ finished(): void;
84
+ errored(error: string): void;
85
+ }
86
+ export declare function createEmitter(runId: string, sink: CordEventSink): Emitter;
87
+ //# sourceMappingURL=agui.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agui.d.ts","sourceRoot":"","sources":["../src/agui.ts"],"names":[],"mappings":"AAKA,uGAAuG;AACvG,MAAM,MAAM,SAAS,GACjB;IAAE,IAAI,EAAE,aAAa,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,GACpE;IAAE,IAAI,EAAE,sBAAsB,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,GAC1E;IAAE,IAAI,EAAE,iBAAiB,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,OAAO,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,GACvG;IAAE,IAAI,EAAE,kBAAkB,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,OAAO,CAAC;IAAC,MAAM,CAAC,EAAE,OAAO,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,GACxI;IAAE,IAAI,EAAE,eAAe,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,GAClE;IAAE,IAAI,EAAE,kBAAkB,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,S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AM,CAAA;KAAE,GAAG,IAAI,CAAC;IACzG,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACjC,eAAe,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,EAAE,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IAClE,UAAU,CAAC,KAAK,EAAE,SAAS,SAAS;QAAE,IAAI,EAAE,aAAa,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC,CAAA;KAAE,GAAG,CAAC,GAAG,KAAK,GAAG,IAAI,CAAC;IAC/F,QAAQ,IAAI,IAAI,CAAC;IACjB,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CAC9B;AAKD,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,aAAa,GAAG,OAAO,CAczE"}
package/dist/agui.js ADDED
@@ -0,0 +1,22 @@
1
+ // agui.ts — the cord's event layer: a typed, AG-UI-aligned stream of what the cord is doing. It is THREE things
2
+ // at once: (1) the cord↔UI wire (AG-UI), (2) the observability story (one event per step — the "stack trace" the
3
+ // playground renders in motion), and (3) what the thin typed client consumes. Events are plain JSON so the host
4
+ // can persist them on the DO (durable → the timeline survives reload) and replay them to a reconnecting client.
5
+ // Hoisted from inside createEmitter (captures nothing — pure).
6
+ const now = () => Date.now();
7
+ export function createEmitter(runId, sink) {
8
+ const send = (event) => sink(event);
9
+ return {
10
+ runId,
11
+ started: (threadId) => send({ type: "RUN_STARTED", runId, threadId, ts: now() }),
12
+ text: (delta) => send({ type: "TEXT_MESSAGE_CONTENT", runId, delta, ts: now() }),
13
+ toolStart: (toolCallId, name, args) => send({ type: "TOOL_CALL_START", runId, toolCallId, name, args, ts: now() }),
14
+ toolResult: (r) => send({ type: "TOOL_CALL_RESULT", runId, ...r, ts: now() }),
15
+ sourceStaged: (path) => send({ type: "SOURCE_STAGED", runId, path, ts: now() }),
16
+ sourceCommitted: (paths, external) => send({ type: "SOURCE_COMMITTED", runId, paths, external, ts: now() }),
17
+ stateDelta: (patch) => send({ type: "STATE_DELTA", runId, patch, ts: now() }),
18
+ finished: () => send({ type: "RUN_FINISHED", runId, ts: now() }),
19
+ errored: (error) => send({ type: "RUN_ERROR", runId, error, ts: now() }),
20
+ };
21
+ }
22
+ //# sourceMappingURL=agui.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agui.js","sourceRoot":"","sources":["../src/agui.ts"],"names":[],"mappings":"AAAA,gHAAgH;AAChH,iHAAiH;AACjH,gHAAgH;AAChH,gHAAgH;AAoChH,+DAA+D;AAC/D,MAAM,GAAG,GAAG,GAAW,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC;AAErC,MAAM,UAAU,aAAa,CAAC,KAAa,EAAE,IAAmB;IAC9D,MAAM,IAAI,GAAG,CAAC,KAAgB,EAAQ,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACrD,OAAO;QACL,KAAK;QACL,OAAO,EAAE,CAAC,QAAQ,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,aAAa,EAAE,KAAK,EAAE,QAAQ,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,CAAC;QAChF,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,sBAAsB,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,CAAC;QAChF,SAAS,EAAE,CAAC,UAAU,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,iBAAiB,EAAE,KAAK,EAAE,UAAU,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,CAAC;QAClH,UAAU,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,kBAAkB,EAAE,KAAK,EAAE,GAAG,CAAC,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,CAAC;QAC7E,YAAY,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,eAAe,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,CAAC;QAC/E,eAAe,EAAE,CAAC,KAAK,EAAE,QAAQ,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,kBAAkB,EAAE,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,CAAC;QAC3G,UAAU,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,aAAa,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,CAAC;QAC7E,QAAQ,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,cAAc,EAAE,KAAK,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,CAAC;QAChE,OAAO,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,WAAW,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,CAAC;KACzE,CAAC;AACJ,CAAC"}
@@ -0,0 +1,76 @@
1
+ import type { Tool, ToolSet } from "./tool.ts";
2
+ /** The run contract: goal + stop rules. `stop.budget` halts run N+1 BEFORE any model spend (HTTP 429, honestly). */
3
+ export interface RunSpecConfig {
4
+ /** The goal condition — what a good answer IS. Becomes part of reviewer/verifier context in chains. */
5
+ goal?: string;
6
+ stop?: {
7
+ /** Total turns this cord serves before halting (checked pre-spend). */
8
+ budget?: number;
9
+ /** Max tool-loop steps within ONE run (default 4). */
10
+ steps?: number;
11
+ };
12
+ }
13
+ /** A fleet chain membership: pipeline (ordered transform), review-pair (maker ⇄ reviewer), dispatcher (route). */
14
+ export type FleetConfig = {
15
+ pattern: "pipeline";
16
+ members: string[];
17
+ position: number;
18
+ } | {
19
+ pattern: "review-pair";
20
+ reviewer: string;
21
+ } | {
22
+ pattern: "dispatcher";
23
+ routes: Record<string, string>;
24
+ };
25
+ /** The whole cord, one typed object. Every field maps 1:1 onto what the governed host executes. */
26
+ export interface CordConfig<Env = unknown> {
27
+ /** The cord's id — becomes `<id>.cord.run` when deployed managed. Lowercase slug. */
28
+ id: string;
29
+ /** The charter: what this cord IS. Becomes the system prompt of every run. */
30
+ charter: string;
31
+ /** The RunSpec — the run contract the host enforces (budget gate BEFORE spend). */
32
+ runSpec?: RunSpecConfig;
33
+ /** Provider-prefixed catalog slug on YOUR gateway (e.g. "workers-ai/@cf/meta/llama-3.3-70b-instruct-fp8-fast"). Omit to use the MODEL var. */
34
+ model?: string;
35
+ /** Governed tools (author with `defineTool` — zod-typed input, `execute(args, ctx)`). The host runs the tool loop, bounded by `runSpec.stop.steps`, every call ledgered. */
36
+ tools?: Tool<Env>[];
37
+ /** Manifest peers this cord may delegate to (identity, never authority — each peer runs under its own grants). */
38
+ peers?: string[];
39
+ /** Chain pattern membership (see FleetConfig). */
40
+ fleet?: FleetConfig | null;
41
+ }
42
+ /**
43
+ * Define a cord. Identity at runtime; inference + docs at authoring time.
44
+ *
45
+ * ```ts
46
+ * import { defineCordConfig, defineTool } from "@op0-ai/cord";
47
+ * import { z } from "zod";
48
+ *
49
+ * export default defineCordConfig({
50
+ * id: "tidecheck",
51
+ * charter: "Answer tide-time questions for surfers, briefly, with units.",
52
+ * runSpec: { goal: "answer tersely", stop: { budget: 500, steps: 4 } },
53
+ * tools: [
54
+ * defineTool({
55
+ * name: "tide_lookup",
56
+ * description: "Look up the next tide for a beach",
57
+ * input: z.object({ beach: z.string() }),
58
+ * execute: async ({ beach }) => ({ beach, low: "14:02", height: "+0.3m" }),
59
+ * }),
60
+ * ],
61
+ * });
62
+ * ```
63
+ */
64
+ export declare function defineCordConfig<Env = unknown>(config: CordConfig<Env>): CordConfig<Env>;
65
+ /** Project governed tools → the OpenAI-compat `tools` array (function calling through the AI Gateway).
66
+ * Zero zod import here — zod 4 schema INSTANCES self-convert (`schema.toJSONSchema()`), so the zod
67
+ * runtime rides in with the author's schemas and the factory-lean bundle stays small (40KB, not 570KB). */
68
+ export declare function toolsToOpenAI(tools: readonly Tool[] | ToolSet): Array<{
69
+ type: "function";
70
+ function: {
71
+ name: string;
72
+ description: string;
73
+ parameters: unknown;
74
+ };
75
+ }>;
76
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAE/C,oHAAoH;AACpH,MAAM,WAAW,aAAa;IAC5B,uGAAuG;IACvG,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE;QACL,uEAAuE;QACvE,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,sDAAsD;QACtD,KAAK,CAAC,EAAE,MAAM,CAAC;KAChB,CAAC;CACH;AAED,kHAAkH;AAClH,MAAM,MAAM,WAAW,GACnB;IAAE,OAAO,EAAE,UAAU,CAAC;IAAC,OAAO,EAAE,MAAM,EAAE,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,GAC5D;IAAE,OAAO,EAAE,aAAa,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,GAC5C;IAAE,OAAO,EAAE,YAAY,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAAE,CAAC;AAE9D,mGAAmG;AACnG,MAAM,WAAW,UAAU,CAAC,GAAG,GAAG,OAAO;IACvC,qFAAqF;IACrF,EAAE,EAAE,MAAM,CAAC;IACX,8EAA8E;IAC9E,OAAO,EAAE,MAAM,CAAC;IAChB,mFAAmF;IACnF,OAAO,CAAC,EAAE,aAAa,CAAC;IACxB,8IAA8I;IAC9I,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,4KAA4K;IAC5K,KAAK,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;IACpB,kHAAkH;IAClH,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;IACjB,kDAAkD;IAClD,KAAK,CAAC,EAAE,WAAW,GAAG,IAAI,CAAC;CAC5B;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,GAAG,OAAO,EAAE,MAAM,EAAE,UAAU,CAAC,GAAG,CAAC,GAAG,UAAU,CAAC,GAAG,CAAC,CAExF;AAED;;4GAE4G;AAC5G,wBAAgB,aAAa,CAAC,KAAK,EAAE,SAAS,IAAI,EAAE,GAAG,OAAO,GAAG,KAAK,CAAC;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,QAAQ,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,OAAO,CAAA;KAAE,CAAA;CAAE,CAAC,CAQjK"}
package/dist/config.js ADDED
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Define a cord. Identity at runtime; inference + docs at authoring time.
3
+ *
4
+ * ```ts
5
+ * import { defineCordConfig, defineTool } from "@op0-ai/cord";
6
+ * import { z } from "zod";
7
+ *
8
+ * export default defineCordConfig({
9
+ * id: "tidecheck",
10
+ * charter: "Answer tide-time questions for surfers, briefly, with units.",
11
+ * runSpec: { goal: "answer tersely", stop: { budget: 500, steps: 4 } },
12
+ * tools: [
13
+ * defineTool({
14
+ * name: "tide_lookup",
15
+ * description: "Look up the next tide for a beach",
16
+ * input: z.object({ beach: z.string() }),
17
+ * execute: async ({ beach }) => ({ beach, low: "14:02", height: "+0.3m" }),
18
+ * }),
19
+ * ],
20
+ * });
21
+ * ```
22
+ */
23
+ export function defineCordConfig(config) {
24
+ return config;
25
+ }
26
+ /** Project governed tools → the OpenAI-compat `tools` array (function calling through the AI Gateway).
27
+ * Zero zod import here — zod 4 schema INSTANCES self-convert (`schema.toJSONSchema()`), so the zod
28
+ * runtime rides in with the author's schemas and the factory-lean bundle stays small (40KB, not 570KB). */
29
+ export function toolsToOpenAI(tools) {
30
+ const list = Array.isArray(tools) ? tools : Object.values(tools);
31
+ return list.map((t) => {
32
+ const schema = t.inputSchema;
33
+ if (typeof schema.toJSONSchema !== "function")
34
+ throw new Error(`tool "${t.name}": inputSchema has no toJSONSchema() — zod 4.4+ schemas required`);
35
+ return { type: "function", function: { name: t.name, description: t.description, parameters: schema.toJSONSchema() } };
36
+ });
37
+ }
38
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AA4CA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,gBAAgB,CAAgB,MAAuB;IACrE,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;4GAE4G;AAC5G,MAAM,UAAU,aAAa,CAAC,KAAgC;IAC5D,MAAM,IAAI,GAAG,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACjE,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE;QACpB,MAAM,MAAM,GAAG,CAAC,CAAC,WAA0D,CAAC;QAC5E,IAAI,OAAO,MAAM,CAAC,YAAY,KAAK,UAAU;YAC3C,MAAM,IAAI,KAAK,CAAC,SAAS,CAAC,CAAC,IAAI,kEAAkE,CAAC,CAAC;QACrG,OAAO,EAAE,IAAI,EAAE,UAAmB,EAAE,QAAQ,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,WAAW,EAAE,CAAC,CAAC,WAAW,EAAE,UAAU,EAAE,MAAM,CAAC,YAAY,EAAE,EAAE,EAAE,CAAC;IAClI,CAAC,CAAC,CAAC;AACL,CAAC"}
package/dist/data.d.ts ADDED
@@ -0,0 +1,168 @@
1
+ import type { z } from "zod";
2
+ export interface TableDef<T extends z.ZodObject<z.ZodRawShape> = z.ZodObject<z.ZodRawShape>> {
3
+ readonly schema: T;
4
+ readonly indexes: Record<string, string[]>;
5
+ /** Declare a named index over columns (used by query().withIndex). Returns this for chaining. */
6
+ index(name: string, columns: string[]): TableDef<T>;
7
+ }
8
+ /** Author a table from a zod object schema; chain .index(name, cols). */
9
+ export declare function defineTable<T extends z.ZodObject<z.ZodRawShape>>(schema: T): TableDef<T>;
10
+ export type SchemaDef = Record<string, TableDef>;
11
+ /** Identity helper — keeps the schema object's literal types while documenting intent. */
12
+ export declare function defineSchema<S extends SchemaDef>(schema: S): S;
13
+ export type Row = {
14
+ id: string;
15
+ _v: number;
16
+ } & Record<string, unknown>;
17
+ /** Accumulates eq-filters for a withIndex scan. */
18
+ export interface IndexQuery {
19
+ eq(column: string, value: unknown): IndexQuery;
20
+ }
21
+ export interface QueryBuilder {
22
+ withIndex(name: string, fn: (q: IndexQuery) => IndexQuery): QueryBuilder;
23
+ collect(): Row[];
24
+ first(): Row | null;
25
+ }
26
+ /** What a query handler sees: reads only (the engine tracks the read-set). */
27
+ export interface ReadDb {
28
+ query(table: string): QueryBuilder;
29
+ get(table: string, id: string): Row | null;
30
+ }
31
+ /** What a mutation handler sees: reads + writes (writes are recorded to drive reactive recompute). */
32
+ export interface WriteDb extends ReadDb {
33
+ insert(table: string, doc: Record<string, unknown>): string;
34
+ patch(table: string, id: string, partial: Record<string, unknown>): void;
35
+ delete(table: string, id: string): void;
36
+ }
37
+ export interface QueryFn<A = unknown, R = unknown> {
38
+ readonly kind: "query";
39
+ readonly args: z.ZodType<A>;
40
+ readonly handler: (db: ReadDb, args: A) => R;
41
+ }
42
+ export interface MutationFn<A = unknown, R = unknown> {
43
+ readonly kind: "mutation";
44
+ readonly args: z.ZodType<A>;
45
+ readonly handler: (db: WriteDb, args: A) => R;
46
+ }
47
+ export interface ActionCtx {
48
+ runQuery(name: string, args: unknown): unknown;
49
+ runMutation(name: string, args: unknown): unknown;
50
+ /** bridge to the agentic cord (governed tool) — wired by the host (onloop). */
51
+ runCordTool?: (name: string, args: unknown) => Promise<unknown>;
52
+ }
53
+ export interface ActionFn<A = unknown, R = unknown> {
54
+ readonly kind: "action";
55
+ readonly args: z.ZodType<A>;
56
+ readonly handler: (ctx: ActionCtx, args: A) => Promise<R>;
57
+ }
58
+ export type AnyFn = QueryFn<any, any> | MutationFn<any, any> | ActionFn<any, any>;
59
+ export declare function query<A, R>(def: {
60
+ args: z.ZodType<A>;
61
+ handler: (db: ReadDb, args: A) => R;
62
+ }): QueryFn<A, R>;
63
+ export declare function mutation<A, R>(def: {
64
+ args: z.ZodType<A>;
65
+ handler: (db: WriteDb, args: A) => R;
66
+ }): MutationFn<A, R>;
67
+ export declare function action<A, R>(def: {
68
+ args: z.ZodType<A>;
69
+ handler: (ctx: ActionCtx, args: A) => Promise<R>;
70
+ }): ActionFn<A, R>;
71
+ type ReadSet = {
72
+ rows: Set<string>;
73
+ tables: Set<string>;
74
+ };
75
+ /** A recomputed subscription result to push to a client (the FULL re-executed query result). */
76
+ export type Transition = {
77
+ subId: string;
78
+ connId: string;
79
+ name: string;
80
+ args: unknown;
81
+ result: unknown;
82
+ version: number;
83
+ };
84
+ export declare class DataEngine {
85
+ private readonly schema;
86
+ private readonly functions;
87
+ private readonly tables;
88
+ private readonly subs;
89
+ private version;
90
+ constructor(schema: SchemaDef, functions: Record<string, AnyFn>);
91
+ private table;
92
+ /** A read-db that records into `rs` (null = untracked, for mutation-internal reads). */
93
+ private readDb;
94
+ private writeDb;
95
+ private fn;
96
+ /** Run a query (tracked). Returns the result + the read-set (for subscription registration). */
97
+ runQuery(name: string, args: unknown): {
98
+ result: unknown;
99
+ readSet: ReadSet;
100
+ version: number;
101
+ };
102
+ /** Run a mutation (single-writer), bump the version, then recompute ONLY the subscriptions whose read-set intersects the write. */
103
+ runMutation(name: string, args: unknown): {
104
+ result: unknown;
105
+ version: number;
106
+ transitions: Transition[];
107
+ };
108
+ /** Run an action (I/O escape hatch): it calls query/mutation through the ctx; not reactive. */
109
+ runAction(name: string, args: unknown, bridge?: {
110
+ runCordTool?: ActionCtx["runCordTool"];
111
+ }): Promise<unknown>;
112
+ /** Register a live subscription: returns the initial result; future intersecting mutations push Transitions. */
113
+ subscribe(subId: string, connId: string, name: string, args: unknown): {
114
+ result: unknown;
115
+ version: number;
116
+ };
117
+ unsubscribe(subId: string): void;
118
+ /** Drop all subscriptions for a connection (on WS close). */
119
+ unsubscribeConn(connId: string): void;
120
+ /** Durability: snapshot the tables (persist to the DO's this.sql) and restore on wake. */
121
+ snapshot(): {
122
+ version: number;
123
+ tables: Record<string, Row[]>;
124
+ };
125
+ restore(snap: {
126
+ version: number;
127
+ tables: Record<string, Row[]>;
128
+ }): void;
129
+ }
130
+ /** One data-plane RPC request. `query` is one-shot; `subscribe` registers a live sub; `mutation`/`action` write. */
131
+ export type DataRequest = {
132
+ op: "query";
133
+ name: string;
134
+ args: unknown;
135
+ } | {
136
+ op: "subscribe";
137
+ name: string;
138
+ args: unknown;
139
+ subId: string;
140
+ } | {
141
+ op: "unsubscribe";
142
+ subId: string;
143
+ } | {
144
+ op: "mutation";
145
+ name: string;
146
+ args: unknown;
147
+ } | {
148
+ op: "action";
149
+ name: string;
150
+ args: unknown;
151
+ };
152
+ export type DataResponse = {
153
+ ok: true;
154
+ result?: unknown;
155
+ version?: number;
156
+ transitions?: Transition[];
157
+ } | {
158
+ ok: false;
159
+ error: string;
160
+ };
161
+ /**
162
+ * Dispatch a DataRequest against the engine. The DO host wires this into its fetch (one-shot query/mutation/
163
+ * action) and its WebSocket handler (subscribe/unsubscribe), then BROADCASTS the returned `transitions` to the
164
+ * subscribed connections (group by transition.connId). Persist engine.snapshot() to this.sql after a mutation.
165
+ */
166
+ export declare function dataPlaneRpc(engine: DataEngine, req: DataRequest, connId?: string): Promise<DataResponse>;
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