@economic/agents 0.0.1-alpha.9 → 0.0.1-beta.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 +553 -46
- package/dist/index.d.mts +116 -492
- package/dist/index.mjs +386 -430
- package/dist/react.d.mts +25 -0
- package/dist/react.mjs +38 -0
- package/package.json +24 -26
- package/schema/schema.sql +29 -0
package/README.md
CHANGED
|
@@ -1,87 +1,594 @@
|
|
|
1
1
|
# @economic/agents
|
|
2
2
|
|
|
3
|
-
Base
|
|
3
|
+
Base class and utilities for building LLM chat agents on Cloudflare's Agents SDK with lazy skill loading, optional message compaction, and built-in audit logging.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
```bash
|
|
6
|
+
npm install @economic/agents ai @cloudflare/ai-chat agents
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
For React UIs that import `@economic/agents/react`, also install `react`. It is an **optional** peer of the package so Workers-only installs are not required to add React. The hook still needs `agents`, `ai`, and `@cloudflare/ai-chat` at runtime (same as the Cloudflare SDK imports it wraps).
|
|
6
10
|
|
|
7
|
-
|
|
8
|
-
- **`AIChatAgentBase`** — base class for when you need full control over `streamText`. Implement `getTools()`, `getSkills()`, and your own `onChatMessage` decorated with `@withSkills`. Compaction is **disabled by default**.
|
|
9
|
-
- **`withSkills`** — method decorator used with `AIChatAgentBase`.
|
|
10
|
-
- **`createSkills`** — lower-level factory for wiring lazy skill loading into any agent subclass yourself.
|
|
11
|
-
- **`filterEphemeralMessages`**, **`injectGuidance`** — utilities used internally, exported for custom wiring.
|
|
12
|
-
- **`compactIfNeeded`**, **`compactMessages`**, **`estimateMessagesTokens`**, **`COMPACT_TOKEN_THRESHOLD`** — compaction utilities, exported for use with `AIChatAgentBase` or fully custom agents.
|
|
13
|
-
- Types: `Tool`, `Skill`, `SkillsConfig`, `SkillsResult`, `SkillContext`.
|
|
11
|
+
---
|
|
14
12
|
|
|
15
|
-
|
|
13
|
+
## Overview
|
|
16
14
|
|
|
17
|
-
|
|
15
|
+
`@economic/agents` provides:
|
|
18
16
|
|
|
19
|
-
|
|
17
|
+
- **`AIChatAgent`** — an abstract Cloudflare Durable Object base class. Implement `onChatMessage`, call `this.buildLLMParams()`, and pass the result to `streamText` from the AI SDK.
|
|
18
|
+
- **`guard`** — optional TypeScript 5+ method decorator for `onChatMessage`. Runs your function with `options.body`; return a `Response` to short-circuit (e.g. auth), or nothing to continue.
|
|
19
|
+
- **`buildLLMParams`** — the standalone version of the above, for use outside of `AIChatAgent` or in custom agent implementations.
|
|
20
|
+
- **`useAIChatAgent`** (subpath `@economic/agents/react`) — React hook that wraps `useAgent` (`agents/react`) and `useAgentChat` (`@cloudflare/ai-chat/react`). Connection status is **callback-only** (`onConnectionStatusChange`). On WebSocket close codes **`>= 3000`**, the hook calls `agent.close()` to stop reconnection, then forwards `onClose` (use `event.reason` for the server message). Pass-through **`onOpen`**, **`onClose`**, **`onError`** are supported.
|
|
20
21
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
Skills and compaction are AI SDK concerns — they control what goes to the LLM. The CF layer is responsible for WebSockets, Durable Objects, and message persistence. These are kept separate.
|
|
23
|
+
|
|
24
|
+
### React client
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { useAIChatAgent, type AgentConnectionStatus } from "@economic/agents/react";
|
|
28
|
+
import { useState } from "react";
|
|
29
|
+
|
|
30
|
+
const [connectionStatus, setConnectionStatus] = useState<AgentConnectionStatus>("connecting");
|
|
31
|
+
|
|
32
|
+
const { agent, chat } = useAIChatAgent({
|
|
33
|
+
agent: "MyAgent",
|
|
34
|
+
host: "localhost:8787",
|
|
35
|
+
chatId: "session-id",
|
|
36
|
+
toolContext: {},
|
|
37
|
+
connectionParams: { userId: "…" },
|
|
38
|
+
onConnectionStatusChange: setConnectionStatus,
|
|
39
|
+
onOpen: (event) => {},
|
|
40
|
+
onClose: (event) => {},
|
|
41
|
+
onError: (event) => {},
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const { messages, sendMessage, status, stop } = chat;
|
|
25
45
|
```
|
|
26
46
|
|
|
27
|
-
|
|
47
|
+
Server-side agent code still imports only from `@economic/agents`; the `/react` entry is a separate build output and does not pull Workers runtime code into the client bundle.
|
|
28
48
|
|
|
29
|
-
|
|
49
|
+
---
|
|
30
50
|
|
|
31
|
-
|
|
51
|
+
## Quick start
|
|
32
52
|
|
|
33
53
|
```typescript
|
|
54
|
+
import { streamText } from "ai";
|
|
55
|
+
import { openai } from "@ai-sdk/openai";
|
|
56
|
+
import { tool } from "ai";
|
|
57
|
+
import { z } from "zod";
|
|
34
58
|
import { AIChatAgent } from "@economic/agents";
|
|
59
|
+
import type { Skill } from "@economic/agents";
|
|
35
60
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
61
|
+
const searchSkill: Skill = {
|
|
62
|
+
name: "search",
|
|
63
|
+
description: "Web search tools",
|
|
64
|
+
guidance: "Use search_web for any queries requiring up-to-date information.",
|
|
65
|
+
tools: {
|
|
66
|
+
search_web: tool({
|
|
67
|
+
description: "Search the web",
|
|
68
|
+
inputSchema: z.object({ query: z.string() }),
|
|
69
|
+
execute: async ({ query }) => `Results for: ${query}`,
|
|
70
|
+
}),
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export class MyAgent extends AIChatAgent<Env> {
|
|
75
|
+
// Set fastModel to enable automatic compaction and future background summarization.
|
|
76
|
+
protected fastModel = openai("gpt-4o-mini");
|
|
77
|
+
|
|
78
|
+
async onChatMessage(onFinish, options) {
|
|
79
|
+
const params = await this.buildLLMParams({
|
|
80
|
+
options,
|
|
81
|
+
onFinish,
|
|
82
|
+
model: openai("gpt-4o"),
|
|
83
|
+
system: "You are a helpful assistant.",
|
|
84
|
+
skills: [searchSkill],
|
|
85
|
+
});
|
|
86
|
+
return streamText(params).toUIMessageStreamResponse();
|
|
42
87
|
}
|
|
43
|
-
|
|
44
|
-
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
No D1 database needed — skill state is persisted to Durable Object SQLite automatically.
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
|
|
95
|
+
## Prerequisites
|
|
96
|
+
|
|
97
|
+
### Cloudflare environment
|
|
98
|
+
|
|
99
|
+
Your agent class is a Durable Object. Declare it in `wrangler.jsonc`:
|
|
100
|
+
|
|
101
|
+
```jsonc
|
|
102
|
+
{
|
|
103
|
+
"durable_objects": {
|
|
104
|
+
"bindings": [{ "name": "MyAgent", "class_name": "MyAgent" }],
|
|
105
|
+
},
|
|
106
|
+
"migrations": [{ "tag": "v1", "new_sqlite_classes": ["MyAgent"] }],
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Run `wrangler types` after to generate typed `Env` bindings.
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
114
|
+
## `AIChatAgent`
|
|
115
|
+
|
|
116
|
+
Extend this class and implement `onChatMessage`. Call `this.buildLLMParams()` to prepare the call, then pass the result to `streamText` or `generateText`.
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
import { streamText } from "ai";
|
|
120
|
+
import { AIChatAgent } from "@economic/agents";
|
|
121
|
+
|
|
122
|
+
export class ChatAgent extends AIChatAgent<Env> {
|
|
123
|
+
async onChatMessage(onFinish, options) {
|
|
124
|
+
const body = (options?.body ?? {}) as { userTier: "free" | "pro" };
|
|
125
|
+
const model = body.userTier === "pro" ? openai("gpt-4o") : openai("gpt-4o-mini");
|
|
126
|
+
|
|
127
|
+
const params = await this.buildLLMParams({
|
|
128
|
+
options,
|
|
129
|
+
onFinish,
|
|
130
|
+
model,
|
|
131
|
+
system: "You are a helpful assistant.",
|
|
132
|
+
skills: [searchSkill, calcSkill], // available for on-demand loading
|
|
133
|
+
tools: { alwaysOnTool }, // always active, regardless of loaded skills
|
|
134
|
+
});
|
|
135
|
+
return streamText(params).toUIMessageStreamResponse();
|
|
45
136
|
}
|
|
46
|
-
|
|
47
|
-
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### `this.buildLLMParams(config)`
|
|
141
|
+
|
|
142
|
+
Protected method on `AIChatAgent`. Wraps the standalone `buildLLMParams` function with:
|
|
143
|
+
|
|
144
|
+
- `messages` pre-filled from `this.messages`
|
|
145
|
+
- `activeSkills` pre-filled from `await this.getLoadedSkills()`
|
|
146
|
+
- `fastModel` injected from `this.fastModel`
|
|
147
|
+
- `log` injected into `experimental_context` alongside `options.body`
|
|
148
|
+
- Automatic error logging for non-clean finish reasons
|
|
149
|
+
- Compaction threshold defaulting: when `maxMessagesBeforeCompaction` is not in the config, defaults to `30`. Pass `maxMessagesBeforeCompaction: undefined` explicitly to disable compaction.
|
|
150
|
+
|
|
151
|
+
Config is everything accepted by the standalone `buildLLMParams` except `messages`, `activeSkills`, and `fastModel`.
|
|
152
|
+
|
|
153
|
+
### `guard`
|
|
154
|
+
|
|
155
|
+
Method decorator (TypeScript 5+ stage-3) for handlers shaped like `onChatMessage(onFinish, options?)`. Before your method runs, it calls your `GuardFn` with `options?.body` (the same custom body the client sends via `useAgentChat` / `body` on the chat request).
|
|
156
|
+
|
|
157
|
+
- Return **`undefined` / nothing** — the decorated method runs as usual.
|
|
158
|
+
- Return a **`Response`** — that response is returned immediately; `onChatMessage` is not called.
|
|
159
|
+
|
|
160
|
+
All policy (tokens, tiers, rate limits) lives in the guard function; the decorator only forwards `body` and branches on whether a `Response` was returned.
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
import { streamText } from "ai";
|
|
164
|
+
import { openai } from "@ai-sdk/openai";
|
|
165
|
+
import { AIChatAgent, guard, type GuardFn } from "@economic/agents";
|
|
166
|
+
|
|
167
|
+
const requireToken: GuardFn = async (body) => {
|
|
168
|
+
const token = body?.token;
|
|
169
|
+
if (typeof token !== "string" || !(await isValidToken(token))) {
|
|
170
|
+
return new Response("Unauthorized", { status: 401 });
|
|
48
171
|
}
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
export class ChatAgent extends AIChatAgent<Env> {
|
|
175
|
+
protected fastModel = openai("gpt-4o-mini");
|
|
49
176
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
177
|
+
@guard(requireToken)
|
|
178
|
+
async onChatMessage(onFinish, options) {
|
|
179
|
+
const params = await this.buildLLMParams({
|
|
180
|
+
options,
|
|
181
|
+
onFinish,
|
|
182
|
+
model: openai("gpt-4o"),
|
|
183
|
+
system: "You are a helpful assistant.",
|
|
184
|
+
});
|
|
185
|
+
return streamText(params).toUIMessageStreamResponse();
|
|
53
186
|
}
|
|
54
187
|
}
|
|
55
188
|
```
|
|
56
189
|
|
|
57
|
-
|
|
190
|
+
### `fastModel` property
|
|
191
|
+
|
|
192
|
+
Override `fastModel` on your subclass to enable automatic compaction and future background conversation summarization:
|
|
193
|
+
|
|
194
|
+
```typescript
|
|
195
|
+
export class MyAgent extends AIChatAgent<Env> {
|
|
196
|
+
protected fastModel = openai("gpt-4o-mini");
|
|
197
|
+
// ...
|
|
198
|
+
}
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
When `fastModel` is set, compaction runs automatically with a default threshold of 30 messages. No per-call configuration is needed in the common case. You can still customise or disable it per-call via `maxMessagesBeforeCompaction`.
|
|
58
202
|
|
|
59
|
-
|
|
203
|
+
When `fastModel` is `undefined` (the default), compaction is disabled regardless of `maxMessagesBeforeCompaction`.
|
|
60
204
|
|
|
61
|
-
`
|
|
205
|
+
### `getLoadedSkills()`
|
|
62
206
|
|
|
63
|
-
|
|
207
|
+
Protected method on `AIChatAgent`. Returns skill names persisted from previous turns (read from DO SQLite). Used internally by `this.buildLLMParams()`.
|
|
208
|
+
|
|
209
|
+
### `persistMessages` (automatic)
|
|
210
|
+
|
|
211
|
+
When `persistMessages` runs at the end of each turn, it:
|
|
212
|
+
|
|
213
|
+
1. Scans `activate_skill` tool results for newly loaded skill state.
|
|
214
|
+
2. Writes the updated skill name list to DO SQLite (no D1 needed).
|
|
215
|
+
3. Logs a turn summary via `log()`.
|
|
216
|
+
4. Strips all `activate_skill` and `list_capabilities` messages from history.
|
|
217
|
+
5. Delegates to the CF base `persistMessages` for message storage and WS broadcast.
|
|
218
|
+
|
|
219
|
+
### `onConnect` (automatic)
|
|
220
|
+
|
|
221
|
+
Replays the full message history to newly connected clients — without this, a page refresh would show an empty UI even though history is in DO SQLite.
|
|
222
|
+
|
|
223
|
+
---
|
|
224
|
+
|
|
225
|
+
## `buildLLMParams` (standalone)
|
|
226
|
+
|
|
227
|
+
The standalone `buildLLMParams` builds the full parameter object for a Vercel AI SDK `streamText` or `generateText` call. Use this directly only if you are not extending `AIChatAgent`, or need fine-grained control.
|
|
64
228
|
|
|
65
229
|
```typescript
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
230
|
+
import { buildLLMParams } from "@economic/agents";
|
|
231
|
+
|
|
232
|
+
const params = await buildLLMParams({
|
|
233
|
+
options, // OnChatMessageOptions — extracts abortSignal and body
|
|
234
|
+
onFinish, // StreamTextOnFinishCallback<ToolSet>
|
|
235
|
+
model, // LanguageModel
|
|
236
|
+
messages: this.messages, // UIMessage[] — converted to ModelMessage[] internally
|
|
237
|
+
activeSkills: await this.getLoadedSkills(),
|
|
238
|
+
system: "You are a helpful assistant.",
|
|
239
|
+
skills: [searchSkill, codeSkill],
|
|
240
|
+
tools: { myAlwaysOnTool },
|
|
241
|
+
stopWhen: stepCountIs(20), // defaults to stepCountIs(20)
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
return streamText(params).toUIMessageStreamResponse();
|
|
245
|
+
// or: generateText(params);
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
| Parameter | Type | Required | Description |
|
|
249
|
+
| ----------------------------- | ------------------------------------- | -------- | ------------------------------------------------------------------------------------------------- |
|
|
250
|
+
| `options` | `OnChatMessageOptions \| undefined` | Yes | CF options object. Extracts `abortSignal` and `experimental_context`. |
|
|
251
|
+
| `onFinish` | `StreamTextOnFinishCallback<ToolSet>` | Yes | Called when the stream completes. |
|
|
252
|
+
| `model` | `LanguageModel` | Yes | The language model to use. |
|
|
253
|
+
| `messages` | `UIMessage[]` | Yes | Conversation history. Converted to `ModelMessage[]` internally. |
|
|
254
|
+
| `activeSkills` | `string[]` | No | Names of skills loaded in previous turns. Pass `await this.getLoadedSkills()`. |
|
|
255
|
+
| `skills` | `Skill[]` | No | Skills available for on-demand loading. Wires up meta-tools automatically. |
|
|
256
|
+
| `system` | `string` | No | Base system prompt. |
|
|
257
|
+
| `tools` | `ToolSet` | No | Always-on tools, active every turn regardless of loaded skills. |
|
|
258
|
+
| `maxMessagesBeforeCompaction` | `number \| undefined` | No | Verbatim tail kept during compaction. Defaults to `30` when omitted. Pass `undefined` to disable. |
|
|
259
|
+
| `stopWhen` | `StopCondition` | No | Stop condition. Defaults to `stepCountIs(20)`. |
|
|
260
|
+
|
|
261
|
+
When `skills` are provided, `buildLLMParams`:
|
|
262
|
+
|
|
263
|
+
- Registers `activate_skill` and `list_capabilities` meta-tools.
|
|
264
|
+
- Sets initial `activeTools` (meta + always-on + loaded skill tools).
|
|
265
|
+
- Wires up `prepareStep` to update `activeTools` after each step.
|
|
266
|
+
- Composes `system` with guidance from loaded skills.
|
|
267
|
+
|
|
268
|
+
---
|
|
269
|
+
|
|
270
|
+
## Defining skills
|
|
271
|
+
|
|
272
|
+
```typescript
|
|
273
|
+
import { tool } from "ai";
|
|
274
|
+
import { z } from "zod";
|
|
275
|
+
import type { Skill } from "@economic/agents";
|
|
276
|
+
|
|
277
|
+
// Skill with guidance — injected into the system prompt when the skill is loaded
|
|
278
|
+
export const calculatorSkill: Skill = {
|
|
279
|
+
name: "calculator",
|
|
280
|
+
description: "Mathematical calculation and expression evaluation",
|
|
281
|
+
guidance:
|
|
282
|
+
"Use the calculate tool for any arithmetic or algebraic expressions. " +
|
|
283
|
+
"Always show the expression you are evaluating.",
|
|
284
|
+
tools: {
|
|
285
|
+
calculate: tool({
|
|
286
|
+
description: "Evaluate a mathematical expression and return the result.",
|
|
287
|
+
inputSchema: z.object({
|
|
288
|
+
expression: z.string().describe('e.g. "2 + 2", "Math.sqrt(144)"'),
|
|
289
|
+
}),
|
|
290
|
+
execute: async ({ expression }) => {
|
|
291
|
+
const result = new Function(`"use strict"; return (${expression})`)();
|
|
292
|
+
return `${expression} = ${result}`;
|
|
293
|
+
},
|
|
294
|
+
}),
|
|
295
|
+
},
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
// Skill without guidance — tools are self-explanatory
|
|
299
|
+
export const datetimeSkill: Skill = {
|
|
300
|
+
name: "datetime",
|
|
301
|
+
description: "Current date and time information in any timezone",
|
|
302
|
+
tools: {
|
|
303
|
+
get_current_datetime: tool({
|
|
304
|
+
description: "Get the current date and time in an optional IANA timezone.",
|
|
305
|
+
inputSchema: z.object({
|
|
306
|
+
timezone: z.string().optional().describe('e.g. "Europe/Copenhagen"'),
|
|
307
|
+
}),
|
|
308
|
+
execute: async ({ timezone = "UTC" }) =>
|
|
309
|
+
new Date().toLocaleString("en-GB", {
|
|
310
|
+
timeZone: timezone,
|
|
311
|
+
dateStyle: "full",
|
|
312
|
+
timeStyle: "long",
|
|
313
|
+
}),
|
|
314
|
+
}),
|
|
315
|
+
},
|
|
316
|
+
};
|
|
69
317
|
```
|
|
70
318
|
|
|
71
|
-
|
|
319
|
+
### `Skill` fields
|
|
320
|
+
|
|
321
|
+
| Field | Type | Required | Description |
|
|
322
|
+
| ------------- | --------- | -------- | ---------------------------------------------------------------------------- |
|
|
323
|
+
| `name` | `string` | Yes | Unique identifier used by `activate_skill` and for DO SQLite persistence. |
|
|
324
|
+
| `description` | `string` | Yes | One-line description shown in the `activate_skill` schema. |
|
|
325
|
+
| `guidance` | `string` | No | Instructions appended to the `system` prompt when this skill is loaded. |
|
|
326
|
+
| `tools` | `ToolSet` | Yes | Record of tool names to `tool()` definitions. Names must be globally unique. |
|
|
327
|
+
|
|
328
|
+
---
|
|
329
|
+
|
|
330
|
+
## Compaction
|
|
331
|
+
|
|
332
|
+
When `fastModel` is set on the agent class, compaction runs automatically before each turn:
|
|
333
|
+
|
|
334
|
+
1. The message list is split into an older window and a recent verbatim tail.
|
|
335
|
+
2. `fastModel` generates a concise summary of the older window.
|
|
336
|
+
3. That summary + the verbatim tail is what gets sent to the LLM.
|
|
337
|
+
4. Full history in DO SQLite is unaffected — compaction is in-memory only.
|
|
338
|
+
|
|
339
|
+
### Enabling compaction
|
|
340
|
+
|
|
341
|
+
Override `fastModel` on your subclass. Compaction runs automatically with a default threshold of 30 messages — no per-call config needed:
|
|
72
342
|
|
|
73
343
|
```typescript
|
|
74
|
-
|
|
75
|
-
|
|
344
|
+
export class MyAgent extends AIChatAgent<Env> {
|
|
345
|
+
protected fastModel = openai("gpt-4o-mini");
|
|
346
|
+
|
|
347
|
+
async onChatMessage(onFinish, options) {
|
|
348
|
+
const params = await this.buildLLMParams({
|
|
349
|
+
options,
|
|
350
|
+
onFinish,
|
|
351
|
+
model: openai("gpt-4o"),
|
|
352
|
+
system: "...",
|
|
353
|
+
// No compaction config needed — runs automatically with default threshold
|
|
354
|
+
});
|
|
355
|
+
return streamText(params).toUIMessageStreamResponse();
|
|
356
|
+
}
|
|
76
357
|
}
|
|
77
358
|
```
|
|
78
359
|
|
|
79
|
-
|
|
360
|
+
### Customising the threshold
|
|
361
|
+
|
|
362
|
+
Pass `maxMessagesBeforeCompaction` to override the default of 30:
|
|
363
|
+
|
|
364
|
+
```typescript
|
|
365
|
+
const params = await this.buildLLMParams({
|
|
366
|
+
options,
|
|
367
|
+
onFinish,
|
|
368
|
+
model: openai("gpt-4o"),
|
|
369
|
+
maxMessagesBeforeCompaction: 50, // keep last 50 messages verbatim
|
|
370
|
+
});
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
### Disabling compaction
|
|
374
|
+
|
|
375
|
+
Pass `maxMessagesBeforeCompaction: undefined` explicitly to disable compaction for that call, even when `fastModel` is set:
|
|
376
|
+
|
|
377
|
+
```typescript
|
|
378
|
+
const params = await this.buildLLMParams({
|
|
379
|
+
options,
|
|
380
|
+
onFinish,
|
|
381
|
+
model: openai("gpt-4o"),
|
|
382
|
+
maxMessagesBeforeCompaction: undefined, // compaction off
|
|
383
|
+
});
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
Compaction is always off when `fastModel` is `undefined` (the base class default).
|
|
387
|
+
|
|
388
|
+
---
|
|
389
|
+
|
|
390
|
+
## Built-in meta tools
|
|
391
|
+
|
|
392
|
+
Two meta tools are automatically registered when `skills` are provided. You do not need to define or wire them.
|
|
393
|
+
|
|
394
|
+
### `activate_skill`
|
|
395
|
+
|
|
396
|
+
Loads one or more skills by name, making their tools available for the rest of the conversation. The LLM calls this when it needs capabilities it does not currently have.
|
|
397
|
+
|
|
398
|
+
- Loading is idempotent — calling for an already-loaded skill is a no-op.
|
|
399
|
+
- The skills available are exactly those passed as `skills` — filter by request body to control access.
|
|
400
|
+
- When skills are successfully loaded, the new state is embedded in the tool result. `persistMessages` extracts it and writes to DO SQLite.
|
|
401
|
+
- All `activate_skill` messages are stripped from history before persistence — state is restored from DO SQLite, not from message history.
|
|
402
|
+
|
|
403
|
+
### `list_capabilities`
|
|
404
|
+
|
|
405
|
+
Returns a summary of active tools, loaded skills, and skills available to load. Always stripped from history before persistence.
|
|
406
|
+
|
|
407
|
+
---
|
|
408
|
+
|
|
409
|
+
## Passing request context to tools
|
|
410
|
+
|
|
411
|
+
Pass arbitrary data via the `body` option of `useAgentChat`. It arrives as `experimental_context` in tool `execute` functions.
|
|
412
|
+
|
|
413
|
+
When using `this.buildLLMParams()`, the context is automatically composed: your body fields plus a `log` function for writing audit events. Use `AgentContext<TBody>` to type it:
|
|
80
414
|
|
|
81
415
|
```typescript
|
|
82
|
-
|
|
83
|
-
|
|
416
|
+
// types.ts
|
|
417
|
+
import type { AgentContext } from "@economic/agents";
|
|
418
|
+
|
|
419
|
+
interface AgentBody {
|
|
420
|
+
authorization: string;
|
|
421
|
+
userId: string;
|
|
84
422
|
}
|
|
423
|
+
|
|
424
|
+
export type ToolContext = AgentContext<AgentBody>;
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
```typescript
|
|
428
|
+
// Client
|
|
429
|
+
useAgentChat({ body: { authorization: token, userId: "u_123" } });
|
|
430
|
+
|
|
431
|
+
// Tool
|
|
432
|
+
execute: async (args, { experimental_context }) => {
|
|
433
|
+
const ctx = experimental_context as ToolContext;
|
|
434
|
+
await ctx.log("tool called", { userId: ctx.userId });
|
|
435
|
+
const data = await fetchSomething(ctx.authorization);
|
|
436
|
+
return data;
|
|
437
|
+
};
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
`log` is a no-op when `AGENT_DB` is not bound — so no changes are needed in tools when running without a D1 database.
|
|
441
|
+
|
|
442
|
+
---
|
|
443
|
+
|
|
444
|
+
## Audit logging — D1 setup
|
|
445
|
+
|
|
446
|
+
`AIChatAgent` writes audit events to a Cloudflare D1 database when `AGENT_DB` is bound on the environment. Each agent worker has its own dedicated D1 database.
|
|
447
|
+
|
|
448
|
+
### 1. Create the D1 database
|
|
449
|
+
|
|
450
|
+
In the [Cloudflare dashboard](https://dash.cloudflare.com) → **Workers & Pages** → **D1** → **Create database**. Note the database name and ID.
|
|
451
|
+
|
|
452
|
+
### 2. Create the schema
|
|
453
|
+
|
|
454
|
+
Open the database in the D1 dashboard, select **Console**, and run the contents of [`schema/schema.sql`](schema/schema.sql) — this creates both the `audit_events` and `conversations` tables in one step:
|
|
455
|
+
|
|
456
|
+
```sql
|
|
457
|
+
CREATE TABLE IF NOT EXISTS audit_events (
|
|
458
|
+
id TEXT PRIMARY KEY,
|
|
459
|
+
durable_object_id TEXT NOT NULL,
|
|
460
|
+
user_id TEXT NOT NULL,
|
|
461
|
+
message TEXT NOT NULL,
|
|
462
|
+
payload TEXT,
|
|
463
|
+
created_at TEXT NOT NULL
|
|
464
|
+
);
|
|
465
|
+
CREATE INDEX IF NOT EXISTS audit_events_user ON audit_events(user_id);
|
|
466
|
+
CREATE INDEX IF NOT EXISTS audit_events_do ON audit_events(durable_object_id);
|
|
467
|
+
CREATE INDEX IF NOT EXISTS audit_events_ts ON audit_events(created_at);
|
|
468
|
+
|
|
469
|
+
CREATE TABLE IF NOT EXISTS conversations (
|
|
470
|
+
durable_object_id TEXT PRIMARY KEY,
|
|
471
|
+
user_id TEXT NOT NULL,
|
|
472
|
+
title TEXT,
|
|
473
|
+
summary TEXT,
|
|
474
|
+
created_at TEXT NOT NULL,
|
|
475
|
+
updated_at TEXT NOT NULL
|
|
476
|
+
);
|
|
477
|
+
CREATE INDEX IF NOT EXISTS conversations_user ON conversations(user_id);
|
|
478
|
+
CREATE INDEX IF NOT EXISTS conversations_ts ON conversations(updated_at);
|
|
479
|
+
```
|
|
480
|
+
|
|
481
|
+
Safe to re-run — all statements use `IF NOT EXISTS`.
|
|
482
|
+
|
|
483
|
+
### 3. Bind it in `wrangler.jsonc`
|
|
484
|
+
|
|
485
|
+
```jsonc
|
|
486
|
+
"d1_databases": [
|
|
487
|
+
{ "binding": "AGENT_DB", "database_name": "agents", "database_id": "YOUR_DB_ID" }
|
|
488
|
+
]
|
|
85
489
|
```
|
|
86
490
|
|
|
87
|
-
|
|
491
|
+
Then run `wrangler types` to regenerate the `Env` type.
|
|
492
|
+
|
|
493
|
+
### 4. Seed local development
|
|
494
|
+
|
|
495
|
+
```bash
|
|
496
|
+
npm run db:setup
|
|
497
|
+
```
|
|
498
|
+
|
|
499
|
+
This runs the schema SQL against the local D1 SQLite file (`.wrangler/state/`). Re-running is harmless.
|
|
500
|
+
|
|
501
|
+
If `AGENT_DB` is not bound, all `log()` calls are silent no-ops — the agent works without it.
|
|
502
|
+
|
|
503
|
+
### Providing `userId`
|
|
504
|
+
|
|
505
|
+
The `user_id` column is `NOT NULL`. The base class reads `userId` automatically from `options.body` — no subclass override is needed. The client must include it in the `body` passed to `useAgentChat`:
|
|
506
|
+
|
|
507
|
+
```typescript
|
|
508
|
+
useAgentChat({
|
|
509
|
+
agent,
|
|
510
|
+
body: {
|
|
511
|
+
userId: "148583_matt", // compose from agreement number + user identifier
|
|
512
|
+
// ...other fields
|
|
513
|
+
},
|
|
514
|
+
});
|
|
515
|
+
```
|
|
516
|
+
|
|
517
|
+
If the client omits `userId`, the audit insert is skipped and a `console.error` is emitted. This will be visible in Wrangler's output during local development and in Workers Logs in production.
|
|
518
|
+
|
|
519
|
+
---
|
|
520
|
+
|
|
521
|
+
## Conversations — D1 setup
|
|
522
|
+
|
|
523
|
+
`AIChatAgent` maintains a `conversations` table in `AGENT_DB` alongside `audit_events`. One row is kept per Durable Object instance (i.e. per conversation). The row is upserted automatically after every turn — no subclass code needed.
|
|
524
|
+
|
|
525
|
+
The `conversations` table is created by the same `schema/schema.sql` file used for audit events — no separate setup step needed.
|
|
526
|
+
|
|
527
|
+
### Upsert behaviour
|
|
528
|
+
|
|
529
|
+
- **First turn**: a new row is inserted with `created_at` and `updated_at` both set to now. `title` and `summary` are `NULL`.
|
|
530
|
+
- **Subsequent turns**: only `user_id` and `updated_at` are updated. `created_at`, `title`, and `summary` are never overwritten by the upsert.
|
|
531
|
+
- `title` and `summary` are populated automatically after the conversation goes idle (see below).
|
|
532
|
+
|
|
533
|
+
### Automatic title and summary generation
|
|
534
|
+
|
|
535
|
+
After every turn, `AIChatAgent` schedules a `generateSummary` callback to fire 30 minutes in the future. If another message arrives before the timer fires, the schedule is cancelled and reset — so the callback only runs once the conversation has been idle for 30 minutes.
|
|
536
|
+
|
|
537
|
+
When `generateSummary` fires it:
|
|
538
|
+
|
|
539
|
+
1. Fetches the current summary from D1 (if any).
|
|
540
|
+
2. Takes the last 30 messages (`SUMMARY_CONTEXT_MESSAGES`) to keep the prompt bounded.
|
|
541
|
+
3. Calls `fastModel` with `Output.object()` to generate a structured `{ title, summary }`.
|
|
542
|
+
4. If a previous summary exists, it is included in the prompt so the model can detect direction changes.
|
|
543
|
+
5. Writes the result back to the `conversations` row.
|
|
544
|
+
|
|
545
|
+
No subclass code is needed — this runs automatically when `AGENT_DB` is bound and `fastModel` is set on the class.
|
|
546
|
+
|
|
547
|
+
### Querying conversation lists
|
|
548
|
+
|
|
549
|
+
To fetch all conversations for a user, ordered by most recent:
|
|
550
|
+
|
|
551
|
+
```sql
|
|
552
|
+
SELECT durable_object_id, title, summary, created_at, updated_at
|
|
553
|
+
FROM conversations
|
|
554
|
+
WHERE user_id = '148583_matt'
|
|
555
|
+
ORDER BY updated_at DESC;
|
|
556
|
+
```
|
|
557
|
+
|
|
558
|
+
If `userId` is not set on the request body, the upsert is skipped and a `console.error` is emitted — the same behaviour as audit logging.
|
|
559
|
+
|
|
560
|
+
---
|
|
561
|
+
|
|
562
|
+
## API reference
|
|
563
|
+
|
|
564
|
+
### Classes
|
|
565
|
+
|
|
566
|
+
| Export | Description |
|
|
567
|
+
| ------------- | --------------------------------------------------------------------------------------------------------------------- |
|
|
568
|
+
| `AIChatAgent` | Abstract CF Durable Object base class. Implement `onChatMessage`. Manages skill state, history replay, and audit log. |
|
|
569
|
+
|
|
570
|
+
### Functions
|
|
571
|
+
|
|
572
|
+
| Export | Signature | Description |
|
|
573
|
+
| ---------------- | -------------------------------------- | ------------------------------------------------------------------------------------------------------ |
|
|
574
|
+
| `guard` | `(guardFn: GuardFn)` | Method decorator: runs `guardFn` with `options.body`; a returned `Response` short-circuits the method. |
|
|
575
|
+
| `buildLLMParams` | `async (config) => Promise<LLMParams>` | Builds the full parameter object for `streamText` or `generateText`. |
|
|
576
|
+
|
|
577
|
+
### Types
|
|
578
|
+
|
|
579
|
+
| Export | Description |
|
|
580
|
+
| ---------------------- | ---------------------------------------------------------------------------------------------------------------- |
|
|
581
|
+
| `GuardFn` | `(body) => Response \| void \| Promise<...>`. Receives chat request `body`; return `Response` to block the turn. |
|
|
582
|
+
| `Skill` | A named group of tools with optional guidance. |
|
|
583
|
+
| `AgentContext<TBody>` | Request body type merged with `log`. Use as the type of `experimental_context`. |
|
|
584
|
+
| `BuildLLMParamsConfig` | Config type for the standalone `buildLLMParams` function. |
|
|
585
|
+
|
|
586
|
+
---
|
|
587
|
+
|
|
588
|
+
## Development
|
|
589
|
+
|
|
590
|
+
```bash
|
|
591
|
+
npm install # install dependencies
|
|
592
|
+
npm test # run tests
|
|
593
|
+
npm pack # build
|
|
594
|
+
```
|