@m6d/cortex 0.9.0 → 0.10.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 (4) hide show
  1. package/README.md +56 -10
  2. package/dist/index.d.ts +656 -342
  3. package/dist/index.js +6980 -385
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -54,16 +54,17 @@ export const cortexServer = defineServer({
54
54
  return session ? { sub: session.userId, claims: { name: session.name } } : null;
55
55
  },
56
56
  events: {
57
- approval_requested: (event) => notifyReviewers(event),
57
+ run_waiting: (event) => notifyReviewers(event),
58
+ run_finished: (event) => recordOutcome(event.run),
58
59
  },
59
60
  });
60
61
 
61
62
  app.route("/cortex", cortexServer);
62
63
  ```
63
64
 
64
- Cortex reads `GET /` (the manifest, versioned by its own content) on every sync, and calls `POST /variables`, `POST /tools/:name`, `POST /verify` and `POST /events/approval_requested` behind it. A run-scoped call carries the run's token, which `resolve` and `run` receive as `token`: the end user's bearer in a chat, the trigger's credential in a trigger run. A tool's input is an object schema (any other fails at startup, since every call's `input` is an object), parsed before `run` sees it. A refusal answers as described in [Errors](#errors).
65
+ Cortex reads `GET /` (the manifest, versioned by its own content) on every sync, and calls `POST /variables`, `POST /tools/:name`, `POST /verify` and `POST /events/:event` (`run_waiting`, `run_finished`) behind it. A run-scoped call carries the run's token, which `resolve` and `run` receive as `token`: the end user's bearer in a chat, the trigger's credential in a trigger run. A tool's input is an object schema (any other fails at startup, since every call's `input` is an object), parsed before `run` sees it. With `delegation: true`, Cortex also calls the server for a user whose token is gone (a message that waited past it): such a call carries no token, and `user.delegated` (`{ reason, runId }`) says why, so a tool can refuse what it should not do for a user who is not there. A refusal answers as described in [Errors](#errors).
65
66
 
66
- A name Cortex would refuse fails `defineServer` at startup: a tool or variable name that is no identifier, and Cortex's built-in names (`utcTime`, `locale`, `timezone`, and the built-in tools such as `executeCode` and `requestLocation`), and so does an input of a tool that needs approval that names `approvalTitle` or closes a sub-schema to it (`additionalProperties: false` inside `allOf`, `anyOf` or `oneOf`), since Cortex adds the card's title to it. The protocol is described in full in `apps/cortex/docs/cortex-integration.md`.
67
+ A name Cortex would refuse fails `defineServer` at startup: a tool or variable name that is no identifier, and Cortex's built-in names (`utcTime`, `locale`, `timezone`, `threadId`, and the built-in tools such as `executeCode` and `requestLocation`), and so does an input of a tool that needs approval that names `approvalTitle` or closes a sub-schema to it (`additionalProperties: false` inside `allOf`, `anyOf` or `oneOf`), since Cortex adds the card's title to it. The protocol is described in full in `apps/cortex/docs/cortex-integration.md`.
67
68
 
68
69
  ## Dataset hosts
69
70
 
@@ -112,22 +113,67 @@ Throw `new CortexError(kind, detail)` from a tool, a variable resolver, `verify`
112
113
  ## Triggers
113
114
 
114
115
  ```ts
115
- import { createTriggerClient, isFiringRunning } from "@m6d/cortex";
116
+ import { createTriggerClient } from "@m6d/cortex";
116
117
 
117
118
  const triggers = createTriggerClient({
118
- url: "https://engine.cortex.example.com", // the engine's public origin, ENGINE_PUBLIC_URL
119
+ url: "https://engine.cortex.example.com", // ENGINE_PUBLIC_URL
119
120
  agent: "reviewer",
120
121
  secrets: { "pr-opened": process.env.PR_TRIGGER_SECRET! },
121
122
  });
122
123
 
123
- const firing = await triggers.fire("pr-opened", { pull_request }, { wait: 10_000 });
124
- if (isFiringRunning(firing)) {
125
- // Outlasted the wait: read it back later.
126
- await triggers.read("pr-opened", firing.firingId);
124
+ const answer = await triggers.fire(
125
+ "pr-opened",
126
+ { pull_request },
127
+ {
128
+ wait: 10_000,
129
+ idempotencyKey: "pr-42",
130
+ },
131
+ );
132
+ if ("state" in answer && answer.state === "started") {
133
+ const run = await triggers.read("pr-opened", answer.runId);
134
+ }
135
+ if ("status" in answer && answer.status === "waiting") {
136
+ const call = answer.waits[0]!;
137
+ await triggers.answer(
138
+ "pr-opened",
139
+ answer.id,
140
+ {
141
+ toolCallId: call.toolCallId,
142
+ approved: true,
143
+ },
144
+ { wait: 10_000 },
145
+ );
127
146
  }
147
+
148
+ const { runs } = await triggers.list("pr-opened", { limit: 20 });
149
+ // Use `${run.createdAt},${run.id}` as `before` for the next page.
150
+ if (runs[0]) await triggers.cancel("pr-opened", runs[0].id);
151
+ ```
152
+
153
+ `fire` returns a typed union. A `state` identifies a started input, a waiting input, a refused input, an input cancelled before any run took it, or a rejected payload. A `status` identifies a run record returned after waiting. Run statuses are `running`, `waiting`, `completed`, `failed`, and `cancelled`. Completed runs carry `result`; waiting runs carry `waits`, the calls they wait on.
154
+
155
+ `wait` accepts 0 to 300000 milliseconds. `busy` (`"queue"` or `"steer"`) picks where a fire goes while a run holds its thread, within the trigger's busy rule, and never past one that refuses. Retrying the same idempotency key and payload returns the same input and run. Changing the payload under that key returns a `conflict` refusal. Refusals under 409 or 429 and payload issues under 422 are typed results. `answer` returns the same union, so a decision queued behind another run returns its waiting input. Other HTTP errors and malformed responses throw `TriggerClientError`. Answering a call that is no longer waiting throws under 409.
156
+
157
+ Every method takes a trigger name to select its secret. `read`, `answer`, and `cancel` use the agent's run routes. `list` uses the trigger's runs route and preserves newest-first order. Every method accepts an abort `signal`.
158
+
159
+ ### Telling a user's thread
160
+
161
+ A trigger whose thread policy is `thread` lands in an existing thread of the agent's, named by the payload. A tool call already carries its thread, so a tool can store it, and your backend fires the trigger with it once the event happens:
162
+
163
+ ```ts
164
+ // In defineServer's tools: remember which thread asked.
165
+ watchOrder: {
166
+ description: "Tell the user here when their order changes status",
167
+ input: z.object({ orderId: z.string() }),
168
+ readOnly: false,
169
+ run: async ({ orderId }, { thread }) => watchers.add(orderId, thread.id),
170
+ },
171
+
172
+ // Later, when the order ships.
173
+ await triggers.fire("order-updated", { orderId, status: "shipped", threadId });
128
174
  ```
129
175
 
130
- A firing parked on a tool that needs approval is resumed with `triggers.answer`. Until it is, a firing on the same thread (a `single` or `keyed` policy) is dropped with reason `approval_pending` (409). A status other than 2xx, 409 or 422, or a body that is no firing, throws `TriggerClientError`.
176
+ The manifest's trigger reads the id with `threadPolicy: { policy: "thread", key: "/threadId", wake: true }`, and its input schema declares `threadId`. The run acts as the thread's user, delegated with reason `trigger`, so a server with `delegation: true` hears them; the trigger's credential is not presented. `wake: false` leaves the trigger's instructions in the thread as a note, with its payload references filled, and runs no turn. A tool defined in Cortex gets the id from an input field filled by the `threadId` variable.
131
177
 
132
178
  ## Management API
133
179