@m6d/cortex 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +156 -2
- package/dist/drizzle.d.ts +665 -0
- package/dist/drizzle.js +561 -0
- package/dist/index-z0594rs3.js +1149 -0
- package/dist/index.d.ts +4274 -0
- package/dist/index.js +8040 -0
- package/package.json +64 -5
package/README.md
CHANGED
|
@@ -1,3 +1,157 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @m6d/cortex
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Cortex for TypeScript backends. Agents are defined in Cortex; your backend reaches them through this package:
|
|
4
|
+
|
|
5
|
+
- `defineServer` serves the **server protocol**: the variables your agents' threads run with, the native tools your backend runs for them, end-user token verification, and the events Cortex tells you about.
|
|
6
|
+
- `defineDatasetHost` serves the **dataset protocol**: tables an agent asks grouped and joined questions of, under your own access rules.
|
|
7
|
+
- `createTriggerClient` fires an agent's triggers from your code.
|
|
8
|
+
- `defineManifest` types a `cortex.manifest.ts`: agents and components kept in your repository, applied with `cortex apply`.
|
|
9
|
+
|
|
10
|
+
Compiled ESM for Bun and Node. `hono` and `zod` are peers; `drizzle-orm` is an optional peer for `@m6d/cortex/drizzle`.
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
bun add @m6d/cortex hono zod
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## The server protocol
|
|
17
|
+
|
|
18
|
+
Register the server on the console's **Servers** page with the URL you mount it at and a secret holding its key, then mount:
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { defineServer, CortexError } from "@m6d/cortex";
|
|
22
|
+
import { z } from "zod";
|
|
23
|
+
|
|
24
|
+
export const cortexServer = defineServer({
|
|
25
|
+
// What Cortex presents as the bearer; checked before any of your code runs.
|
|
26
|
+
key: process.env.CORTEX_SERVER_KEY!,
|
|
27
|
+
variables: {
|
|
28
|
+
// Asked once per thread; `{{userName}}` in prompts, skills and tool inputs.
|
|
29
|
+
userName: {
|
|
30
|
+
description: "Display name of the signed-in user",
|
|
31
|
+
example: "Sara",
|
|
32
|
+
type: "string",
|
|
33
|
+
resolve: ({ user, token }) => lookUpName(user?.id, token),
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
tools: {
|
|
37
|
+
cancelOrder: {
|
|
38
|
+
description: "Cancel one of the user's open orders.",
|
|
39
|
+
input: z.object({ orderId: z.string() }),
|
|
40
|
+
readOnly: false,
|
|
41
|
+
// Every call parks until the end user approves it, on a card titled with
|
|
42
|
+
// what the model says that call will do.
|
|
43
|
+
requiresApproval: true,
|
|
44
|
+
run: async ({ orderId }, { user }) => {
|
|
45
|
+
if (!user)
|
|
46
|
+
throw new CortexError("forbidden", "Only a signed-in user can cancel an order");
|
|
47
|
+
return cancel(orderId, user.id);
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
// For agents whose auth is `server`: whose bearer this is; null refuses it.
|
|
52
|
+
verify: async (token) => {
|
|
53
|
+
const session = await sessions.find(token);
|
|
54
|
+
return session ? { sub: session.userId, claims: { name: session.name } } : null;
|
|
55
|
+
},
|
|
56
|
+
events: {
|
|
57
|
+
approval_requested: (event) => notifyReviewers(event),
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
app.route("/cortex", cortexServer);
|
|
62
|
+
```
|
|
63
|
+
|
|
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
|
+
|
|
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
|
+
|
|
68
|
+
## Dataset hosts
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
import { defineDatasetHost } from "@m6d/cortex";
|
|
72
|
+
import { drizzleDataset } from "@m6d/cortex/drizzle";
|
|
73
|
+
|
|
74
|
+
const orders = drizzleDataset<User>({
|
|
75
|
+
name: "orders",
|
|
76
|
+
description: "One row per order: who bought what, for how much, and when.",
|
|
77
|
+
table: ordersTable,
|
|
78
|
+
dialect: "postgres",
|
|
79
|
+
db,
|
|
80
|
+
// The rule the application already has; undefined means every row.
|
|
81
|
+
scoped: (user) => eq(ordersTable.customerId, user.customerId),
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
export const datasets = defineDatasetHost({
|
|
85
|
+
expose: [orders],
|
|
86
|
+
resolveUser: ({ token }) => verifyUser(token),
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
app.route("/datasets", datasets);
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Register the host on the console's **Datasets** page with that URL. Every query arrives validated against the manifest; the host runs it under the user `resolveUser` returns.
|
|
93
|
+
|
|
94
|
+
## Errors
|
|
95
|
+
|
|
96
|
+
Both protocols refuse in one envelope, `{ error: { kind, detail } }`, under the kind's HTTP status:
|
|
97
|
+
|
|
98
|
+
| Kind | Status | When |
|
|
99
|
+
| ----------------- | ------ | ------------------------------------------------------ |
|
|
100
|
+
| `invalid_request` | 400 | The body, the tool input or the query does not fit |
|
|
101
|
+
| `unsupported` | 400 | The query is valid, but this host cannot translate it |
|
|
102
|
+
| `forbidden` | 403 | The caller may not make this call or read these rows |
|
|
103
|
+
| `timeout` | 504 | The call did not finish in time |
|
|
104
|
+
| `internal` | 500 | Anything else, answered without the underlying message |
|
|
105
|
+
|
|
106
|
+
Throw `new CortexError(kind, detail)` from a tool, a variable resolver, `verify`, an event handler, a dataset source or `resolveUser` to answer that kind with your detail, which the model reads. A timeout or an abort answers `timeout`, and any other error answers `internal`. The package answers `invalid_request` itself for a malformed body, an input that fails its schema, a query the manifest refuses, and an unknown dataset or tool. An unknown tool answers under 404, a body over the size limit under 413, and a wrong server key `forbidden` under 401.
|
|
107
|
+
|
|
108
|
+
## Triggers
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
import { createTriggerClient, isFiringRunning } from "@m6d/cortex";
|
|
112
|
+
|
|
113
|
+
const triggers = createTriggerClient({
|
|
114
|
+
url: "https://engine.cortex.example.com", // the engine's public origin, ENGINE_PUBLIC_URL
|
|
115
|
+
agent: "reviewer",
|
|
116
|
+
secrets: { "pr-opened": process.env.PR_TRIGGER_SECRET! },
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
const firing = await triggers.fire("pr-opened", { pull_request }, { wait: 10_000 });
|
|
120
|
+
if (isFiringRunning(firing)) {
|
|
121
|
+
// Outlasted the wait: read it back later.
|
|
122
|
+
await triggers.read("pr-opened", firing.firingId);
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
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`.
|
|
127
|
+
|
|
128
|
+
## Manifests
|
|
129
|
+
|
|
130
|
+
```ts
|
|
131
|
+
import { defineManifest } from "@m6d/cortex";
|
|
132
|
+
|
|
133
|
+
export default defineManifest({
|
|
134
|
+
id: "orders", // every row an apply creates belongs to this manifest
|
|
135
|
+
agents: [
|
|
136
|
+
{
|
|
137
|
+
slug: "support",
|
|
138
|
+
name: "Support",
|
|
139
|
+
server: "Orders backend", // registered in the console
|
|
140
|
+
prompt: "You help customers with their orders.",
|
|
141
|
+
auth: { kind: "server" },
|
|
142
|
+
},
|
|
143
|
+
],
|
|
144
|
+
skills: [
|
|
145
|
+
{
|
|
146
|
+
name: "refunds",
|
|
147
|
+
description: "How refunds work",
|
|
148
|
+
body: "Refunds go to the original payment method within five days.",
|
|
149
|
+
audience: { agents: ["support"] },
|
|
150
|
+
},
|
|
151
|
+
],
|
|
152
|
+
});
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
`cortex apply cortex.manifest.ts` makes the deployment match: it creates what is new, updates what changed, and deletes what this manifest created and does not list. A manifest names servers, model providers, credentials and secrets, and never holds a secret value.
|
|
156
|
+
|
|
157
|
+
The server protocol, dataset hosts and the trigger client for ASP.NET Core ship in the `M6d.Cortex` NuGet package.
|