@economic/agents 0.0.1-alpha.2 → 0.0.1-alpha.21

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