@economic/agents 0.0.1-alpha.10

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 ADDED
@@ -0,0 +1,87 @@
1
+ # @economic/agents
2
+
3
+ Base classes and utilities for building LLM agents on Cloudflare's Agents SDK with lazy tool loading.
4
+
5
+ ## Exports
6
+
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`.
14
+
15
+ See [COMPARISON.md](./COMPARISON.md) for a side-by-side code example of both base classes.
16
+
17
+ See [src/features/skills/README.md](./src/features/skills/README.md) for full `createSkills` documentation.
18
+
19
+ ## Development
20
+
21
+ ```bash
22
+ vp install # install dependencies
23
+ vp test # run tests
24
+ vp pack # build
25
+ ```
26
+
27
+ ---
28
+
29
+ ## Implementing your own agent
30
+
31
+ Extend `AIChatAgent` and implement the four required methods:
32
+
33
+ ```typescript
34
+ import { AIChatAgent } from "@economic/agents";
35
+
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];
45
+ }
46
+ getSystemPrompt() {
47
+ return "You are a helpful assistant.";
48
+ }
49
+
50
+ // Return the D1 binding — typed in Cloudflare.Env after `wrangler types`
51
+ protected getDB() {
52
+ return this.env.AGENT_DB;
53
+ }
54
+ }
55
+ ```
56
+
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.
58
+
59
+ ### Message compaction
60
+
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).
62
+
63
+ The compaction model defaults to `getModel()`. To use a cheaper model for summarisation, override `getCompactionModel()`:
64
+
65
+ ```typescript
66
+ protected override getCompactionModel(): LanguageModel {
67
+ return openai("gpt-4o-mini"); // cheaper model for summarisation
68
+ }
69
+ ```
70
+
71
+ To disable compaction entirely, override `getCompactionModel()` to return `undefined`:
72
+
73
+ ```typescript
74
+ protected override getCompactionModel(): LanguageModel | undefined {
75
+ return undefined; // no compaction — older messages are dropped at maxPersistedMessages
76
+ }
77
+ ```
78
+
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:
80
+
81
+ ```typescript
82
+ protected override getCompactionModel(): LanguageModel {
83
+ return openai("gpt-4o-mini");
84
+ }
85
+ ```
86
+
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.
@@ -0,0 +1,567 @@
1
+ import * as ai from "ai";
2
+ import { LanguageModel, ModelMessage, PrepareStepFunction, StreamTextOnFinishCallback, ToolSet, UIMessage } from "ai";
3
+ import { AIChatAgent as AIChatAgent$1, OnChatMessageOptions } from "@cloudflare/ai-chat";
4
+
5
+ //#region ../../node_modules/partyserver/dist/index.d.ts
6
+ //#region src/types.d.ts
7
+ type ImmutablePrimitive = undefined | null | boolean | string | number;
8
+ type Immutable<T> = T extends ImmutablePrimitive ? T : T extends Array<infer U> ? ImmutableArray<U> : T extends Map<infer K, infer V> ? ImmutableMap<K, V> : T extends Set<infer M> ? ImmutableSet<M> : ImmutableObject<T>;
9
+ type ImmutableArray<T> = ReadonlyArray<Immutable<T>>;
10
+ type ImmutableMap<K, V> = ReadonlyMap<Immutable<K>, Immutable<V>>;
11
+ type ImmutableSet<T> = ReadonlySet<Immutable<T>>;
12
+ type ImmutableObject<T> = { readonly [K in keyof T]: Immutable<T[K]> };
13
+ type ConnectionState<T> = ImmutableObject<T> | null;
14
+ type ConnectionSetStateFn<T> = (prevState: ConnectionState<T>) => T;
15
+ type ConnectionContext = {
16
+ request: Request;
17
+ };
18
+ /** A WebSocket connected to the Server */
19
+ type Connection<TState = unknown> = WebSocket & {
20
+ /** Connection identifier */id: string;
21
+ /**
22
+ * Arbitrary state associated with this connection.
23
+ * Read-only — use {@link Connection.setState} to update.
24
+ *
25
+ * This property is configurable, meaning it can be redefined via
26
+ * `Object.defineProperty` by downstream consumers (e.g. the Cloudflare
27
+ * Agents SDK) to namespace or wrap internal state storage.
28
+ */
29
+ state: ConnectionState<TState>;
30
+ /**
31
+ * Update the state associated with this connection.
32
+ *
33
+ * Accepts either a new state value or an updater function that receives
34
+ * the previous state and returns the next state.
35
+ *
36
+ * This property is configurable, meaning it can be redefined via
37
+ * `Object.defineProperty` by downstream consumers. If you redefine
38
+ * `state` and `setState`, you are responsible for calling
39
+ * `serializeAttachment` / `deserializeAttachment` yourself if you need
40
+ * the state to survive hibernation.
41
+ */
42
+ setState(state: TState | ConnectionSetStateFn<TState> | null): ConnectionState<TState>;
43
+ /**
44
+ * @deprecated use {@link Connection.setState} instead.
45
+ *
46
+ * Low-level method to persist data in the connection's attachment storage.
47
+ * This property is configurable and can be redefined by downstream
48
+ * consumers that need to wrap or namespace the underlying storage.
49
+ */
50
+ serializeAttachment<T = unknown>(attachment: T): void;
51
+ /**
52
+ * @deprecated use {@link Connection.state} instead.
53
+ *
54
+ * Low-level method to read data from the connection's attachment storage.
55
+ * This property is configurable and can be redefined by downstream
56
+ * consumers that need to wrap or namespace the underlying storage.
57
+ */
58
+ deserializeAttachment<T = unknown>(): T | null;
59
+ /**
60
+ * Tags assigned to this connection via {@link Server.getConnectionTags}.
61
+ * Always includes the connection id as the first tag.
62
+ */
63
+ tags: readonly string[];
64
+ /**
65
+ * @deprecated Use `this.name` on the Server instead.
66
+ * The server name. Populated from `Server.name` after initialization.
67
+ */
68
+ server: string;
69
+ }; //#endregion
70
+ //#region src/index.d.ts
71
+ //#endregion
72
+ //#region ../../node_modules/zod/v4/core/schemas.d.cts
73
+ declare global {
74
+ interface File {}
75
+ }
76
+ //#endregion
77
+ //#region src/features/skills/types.d.ts
78
+ /**
79
+ * A named group of related tools that can be loaded together on demand.
80
+ *
81
+ * The agent starts with only its always-on tools active. When the LLM calls
82
+ * activate_skill with a skill name, that skill's tools become available for
83
+ * the rest of the conversation.
84
+ *
85
+ * Define tools using the AI SDK's `tool()` helper — Zod schemas are supported
86
+ * natively via the `parameters` field.
87
+ */
88
+ interface Skill {
89
+ name: string;
90
+ /** One-line description shown in the activate_skill tool schema */
91
+ description: string;
92
+ /**
93
+ * Guidance text for this skill — e.g. rate limits, preferred patterns,
94
+ * when to use each tool. Injected as a system message each turn for any
95
+ * skill that is loaded, keeping the `system` prompt static and cacheable.
96
+ */
97
+ guidance?: string;
98
+ tools: ToolSet;
99
+ }
100
+ /**
101
+ * Configuration passed to createSkills().
102
+ */
103
+ interface SkillsConfig {
104
+ /** Tools that are always active regardless of loaded skills */
105
+ tools: ToolSet;
106
+ /** All available skills that can be loaded on demand */
107
+ skills: Skill[];
108
+ /**
109
+ * The base system prompt for the agent. When provided, createSkills uses
110
+ * this to compose the full system string (base + guidance) returned by
111
+ * getSystem() and from prepareStep. Guidance is kept in the system
112
+ * parameter rather than the messages array — Anthropic/Gemini only allow
113
+ * system messages at the start of the conversation.
114
+ */
115
+ systemPrompt?: string;
116
+ /**
117
+ * Skill names that were loaded in previous turns, read from D1 at turn
118
+ * start. Seeds the in-memory loadedSkills set so prior state is restored
119
+ * before the first LLM step.
120
+ */
121
+ initialLoadedSkills?: string[];
122
+ /**
123
+ * Called after activate_skill successfully loads new skills.
124
+ * Receives the complete current set of loaded skill names. The agent
125
+ * buffers this value and writes it to D1 at turn end (in persistMessages),
126
+ * keeping the write co-located with message persistence — aligned with
127
+ * how slack-bot saves loaded_categories alongside messages.
128
+ */
129
+ onSkillsChanged?: (skills: string[]) => Promise<void>;
130
+ /**
131
+ * Optional permission hook. Return false to deny access to a skill.
132
+ * Defaults to allow-all if not provided.
133
+ */
134
+ filterSkill?: (skillName: string) => Promise<boolean> | boolean;
135
+ }
136
+ /**
137
+ * Skill context injected by the @withSkills decorator.
138
+ *
139
+ * Spread the skill fields into streamText:
140
+ *
141
+ * ```typescript
142
+ * const { messages, guidance, ...skillArgs } = ctx;
143
+ * return streamText({
144
+ * model: this.getModel(),
145
+ * system: "Your base prompt — static, never includes guidance",
146
+ * messages,
147
+ * ...skillArgs,
148
+ * onFinish,
149
+ * stopWhen: stepCountIs(20),
150
+ * }).toUIMessageStreamResponse();
151
+ * ```
152
+ */
153
+ interface SkillContext {
154
+ /** All registered tools — spread into streamText */
155
+ tools: ai.ToolSet;
156
+ /** Currently active tool names — spread into streamText */
157
+ activeTools: string[];
158
+ /**
159
+ * Updates activeTools before each LLM step. Spread into streamText.
160
+ */
161
+ prepareStep: ai.PrepareStepFunction;
162
+ /**
163
+ * Guidance text from all currently-loaded skills. Compose your system
164
+ * prompt as: `${myBase}${guidance ? '\n\n' + guidance : ''}`
165
+ */
166
+ guidance: string;
167
+ /**
168
+ * Plain conversation history from DO SQLite — no guidance injected.
169
+ * Pass directly as the `messages` param of streamText.
170
+ */
171
+ messages: ai.ModelMessage[];
172
+ }
173
+ /**
174
+ * The object returned by createSkills().
175
+ * Spread the SDK-specific fields into your streamText call.
176
+ */
177
+ interface SkillsResult {
178
+ /** Guidance text for all currently-loaded skills */
179
+ getLoadedGuidance(): string;
180
+ /** Current loaded skill names */
181
+ getLoadedSkills(): string[];
182
+ /**
183
+ * Full system string: base system prompt concatenated with guidance from
184
+ * all currently-loaded skills. Only meaningful when `systemPrompt` was
185
+ * passed to createSkills. Use as the `system` param of streamText.
186
+ */
187
+ getSystem(): string;
188
+ }
189
+ //#endregion
190
+ //#region src/agents/chat/AIChatAgentBase.d.ts
191
+ /**
192
+ * Base class for chat agents with lazy skill loading.
193
+ *
194
+ * Owns:
195
+ * - D1 persistence for loaded skill state (skill names survive DO eviction)
196
+ * - Ephemeral message filtering (list_capabilities, no-op activate_skill calls)
197
+ * - Message compaction (LLM summarisation when history exceeds token threshold)
198
+ * - History replay to newly connected clients (onConnect override)
199
+ * - Skill context preparation for use with the @withSkills decorator
200
+ *
201
+ * Conversation messages are stored in Durable Object SQLite, managed
202
+ * automatically by the Cloudflare AIChatAgent — no D1 write needed for messages.
203
+ *
204
+ * D1 is written only when skills change (activate_skill was called this turn),
205
+ * not on every turn.
206
+ *
207
+ * ## Usage
208
+ *
209
+ * Extend this class when you want full control over `streamText`. Implement
210
+ * `getTools()`, `getSkills()`, and your own `onChatMessage` decorated with
211
+ * `@withSkills`:
212
+ *
213
+ * ```typescript
214
+ * export class MyAgent extends AIChatAgentBase {
215
+ * getTools() { return []; }
216
+ * getSkills() { return [searchSkill, codeSkill]; }
217
+ * getDB() { return this.env.AGENT_DB; }
218
+ *
219
+ * @withSkills
220
+ * async onChatMessage(onFinish, ctx: SkillContext, options?) {
221
+ * const { messages, ...skillArgs } = ctx;
222
+ * return streamText({
223
+ * model: openai("gpt-4o"),
224
+ * system: "You are a helpful assistant.",
225
+ * messages,
226
+ * ...skillArgs,
227
+ * onFinish,
228
+ * stopWhen: stepCountIs(20),
229
+ * }).toUIMessageStreamResponse();
230
+ * }
231
+ * }
232
+ * ```
233
+ *
234
+ * For a batteries-included experience where the base class owns `onChatMessage`,
235
+ * extend `AIChatAgent` instead.
236
+ */
237
+ declare abstract class AIChatAgentBase<Env extends Cloudflare.Env = Cloudflare.Env> extends AIChatAgent$1<Env> {
238
+ /**
239
+ * Maximum number of messages stored in DO SQLite.
240
+ *
241
+ * Lowered from the Cloudflare AIChatAgent default of 200. When compaction
242
+ * is enabled, one slot is reserved for the summary message so the verbatim
243
+ * tail is maxPersistedMessages - 1 recent messages. Raise or lower per agent.
244
+ */
245
+ maxPersistedMessages: number;
246
+ /** Tools that are always active regardless of loaded skills */
247
+ abstract getTools(): ToolSet;
248
+ /** All skills available for on-demand loading */
249
+ abstract getSkills(): Skill[];
250
+ /**
251
+ * Return a LanguageModel to use for compaction summarisation.
252
+ *
253
+ * Return undefined (default) to disable compaction — messages are kept up
254
+ * to maxPersistedMessages and older ones are dropped by the Cloudflare
255
+ * AIChatAgent's built-in hard cap.
256
+ *
257
+ * Override to use a cheaper or faster model for summarisation, or to enable
258
+ * compaction in subclasses that do not override it automatically.
259
+ */
260
+ protected getCompactionModel(): LanguageModel | undefined;
261
+ /**
262
+ * Return the D1 database binding for persisting loaded skill state.
263
+ *
264
+ * Override in your subclass to return the binding from env:
265
+ * ```typescript
266
+ * protected getDB() { return this.env.AGENT_DB; }
267
+ * ```
268
+ *
269
+ * Defaults to undefined — when undefined, loaded skills reset on every new
270
+ * conversation (skills still work within a turn, just not across turns).
271
+ */
272
+ protected getDB(): D1Database | undefined;
273
+ /**
274
+ * Optional permission hook. Return false to deny the agent access to a
275
+ * skill when activate_skill is called. Defaults to allow-all.
276
+ */
277
+ protected filterSkill(_skillName: string): Promise<boolean>;
278
+ /**
279
+ * Buffered skill state from the current turn.
280
+ *
281
+ * Set by the onSkillsChanged callback when activate_skill loads new skills
282
+ * mid-turn. Flushed to D1 in persistMessages at turn end — only written
283
+ * when this value is set, so D1 is not touched on turns where no new skills
284
+ * are loaded.
285
+ */
286
+ protected _pendingSkills: string[] | undefined;
287
+ /**
288
+ * Reads loaded skill names from D1 for this agent.
289
+ *
290
+ * Returns an empty array if no record exists (first turn, or no skills
291
+ * loaded yet). Conversation messages are not read here — the Cloudflare
292
+ * AIChatAgent provides those via this.messages from DO SQLite.
293
+ */
294
+ protected _readSkillState(): Promise<string[]>;
295
+ /**
296
+ * Writes loaded skill names to D1 for this agent.
297
+ *
298
+ * Uses INSERT OR REPLACE so the first skill load creates the row and
299
+ * subsequent loads update it. Only called when skills actually changed
300
+ * this turn (_pendingSkills is set).
301
+ */
302
+ protected _writeSkillState(skills: string[]): Promise<void>;
303
+ /**
304
+ * Flush persisted message history to a newly connected client.
305
+ *
306
+ * The Cloudflare AIChatAgent broadcasts message updates to existing
307
+ * connections via persistMessages, but does nothing for connections that
308
+ * arrive after a conversation has ended. Without this override, a page
309
+ * refresh produces an empty UI even though the history is intact in DO SQLite.
310
+ *
311
+ * Skips replay when a stream is active — CF_AGENT_STREAM_RESUMING handles
312
+ * that case and replays in-progress chunks via its own protocol.
313
+ */
314
+ onConnect(connection: Connection, ctx: ConnectionContext): Promise<void>;
315
+ /**
316
+ * Strips ephemeral content, conditionally saves skill state to D1, then
317
+ * delegates to super for DO SQLite persistence and WebSocket broadcast.
318
+ *
319
+ * The Cloudflare AIChatAgent calls persistMessages once per turn after all
320
+ * steps complete, so overriding here is the correct place to act — it runs
321
+ * after the full assistant message (including all tool results) is assembled.
322
+ *
323
+ * Two things happen here:
324
+ *
325
+ * 1. Ephemeral tool calls are stripped — list_capabilities (always) and
326
+ * activate_skill when nothing was newly loaded (no state change).
327
+ *
328
+ * 2. If skills changed this turn (_pendingSkills is set), the updated list
329
+ * is written to D1. Turns where no skills were loaded do not touch D1.
330
+ *
331
+ * Message persistence itself is handled by super.persistMessages, which
332
+ * writes to DO SQLite — no D1 write needed for messages.
333
+ */
334
+ persistMessages(messages: UIMessage[], excludeBroadcastIds?: string[], options?: {
335
+ _deleteStaleRows?: boolean;
336
+ }): Promise<void>;
337
+ /**
338
+ * Widened onChatMessage signature that accommodates the @withSkills decorator.
339
+ *
340
+ * The decorator transforms the consumer's 3-arg form (onFinish, ctx, options) into
341
+ * a 2-arg wrapper at runtime. This declaration widens the base class signature so
342
+ * that TypeScript accepts the consumer's 3-arg override without errors.
343
+ *
344
+ * @ts-ignore — intentional: widens the Cloudflare AIChatAgent's (onFinish, options?) signature.
345
+ */
346
+ onChatMessage(onFinish: StreamTextOnFinishCallback<ToolSet>, ctxOrOptions?: SkillContext | OnChatMessageOptions): Promise<Response | undefined>;
347
+ /**
348
+ * Called by the @withSkills decorator at the start of each turn.
349
+ *
350
+ * Reads loaded skill state from D1, seeds createSkills, and returns a
351
+ * SkillContext ready to use in a streamText call.
352
+ *
353
+ * Guidance is exposed as `ctx.guidance` — compose your system prompt as:
354
+ * `${myBase}${ctx.guidance ? '\n\n' + ctx.guidance : ''}`
355
+ *
356
+ * Messages are plain (no guidance injected). Guidance stays out of the
357
+ * messages array — Anthropic/Gemini only allow system messages at position 0.
358
+ */
359
+ protected _prepareSkillContext(): Promise<SkillContext>;
360
+ }
361
+ /**
362
+ * Method decorator for use with AIChatAgentBase.
363
+ *
364
+ * Apply to `onChatMessage` to receive a pre-built SkillContext as the second
365
+ * argument. The decorator reads loaded skill state from D1, seeds createSkills,
366
+ * and injects guidance into the conversation history from DO SQLite. Skill state
367
+ * changes are buffered for D1 write at turn end (only when skills actually change).
368
+ * Ephemeral cleanup is handled automatically via the persistMessages override —
369
+ * no wiring needed.
370
+ *
371
+ * ```typescript
372
+ * @withSkills
373
+ * async onChatMessage(
374
+ * onFinish: StreamTextOnFinishCallback<ToolSet>,
375
+ * ctx: SkillContext,
376
+ * options?: OnChatMessageOptions,
377
+ * ) {
378
+ * const { messages, guidance, ...skillArgs } = ctx;
379
+ * const base = "Your base prompt";
380
+ * return streamText({
381
+ * model: this.getModel(),
382
+ * system: guidance ? `${base}\n\n${guidance}` : base,
383
+ * messages,
384
+ * ...skillArgs,
385
+ * onFinish,
386
+ * stopWhen: stepCountIs(20),
387
+ * }).toUIMessageStreamResponse();
388
+ * }
389
+ * ```
390
+ */
391
+ type WithSkillsFn = (this: AIChatAgentBase, onFinish: StreamTextOnFinishCallback<ToolSet>, ctx: SkillContext, options?: OnChatMessageOptions) => Promise<Response | undefined>;
392
+ declare function withSkills(fn: WithSkillsFn, _context: ClassMethodDecoratorContext): WithSkillsFn;
393
+ //#endregion
394
+ //#region src/agents/chat/AIChatAgent.d.ts
395
+ /**
396
+ * Batteries-included base class for chat agents with lazy skill loading.
397
+ *
398
+ * Owns the full `onChatMessage` lifecycle. Implement four abstract methods and
399
+ * get lazy skill loading, cross-turn skill persistence, guidance injection,
400
+ * ephemeral message cleanup, and message compaction for free.
401
+ *
402
+ * Conversation messages are stored in Durable Object SQLite by the Cloudflare
403
+ * AIChatAgent automatically — available as this.messages at the start of each
404
+ * turn. Loaded skill state is stored in D1 (via getDB()) and read at turn start.
405
+ * Guidance is injected as a system message just before the current user turn,
406
+ * keeping the `system` param static and cacheable across all turns.
407
+ *
408
+ * ```typescript
409
+ * export class MyAgent extends AIChatAgent {
410
+ * getModel() { return openai("gpt-4o"); }
411
+ * getTools() { return tools; }
412
+ * getSkills() { return [searchSkill, codeSkill]; }
413
+ * getSystemPrompt() { return "You are a helpful assistant."; }
414
+ * getDB() { return this.env.AGENT_DB; }
415
+ * }
416
+ * ```
417
+ *
418
+ * If you need full control over the `streamText` call (custom model options,
419
+ * streaming transforms, varying the model per request, etc.) use
420
+ * `AIChatAgentBase` with the `@withSkills` decorator instead.
421
+ */
422
+ declare abstract class AIChatAgent<Env extends Cloudflare.Env = Cloudflare.Env> extends AIChatAgentBase<Env> {
423
+ /** Return the Vercel AI SDK LanguageModel to use for this agent */
424
+ abstract getModel(): LanguageModel;
425
+ /** Tools that are always active regardless of loaded skills */
426
+ abstract getTools(): ToolSet;
427
+ /** All skills available for on-demand loading */
428
+ abstract getSkills(): Skill[];
429
+ /**
430
+ * Build the base system prompt. This string is passed to streamText as-is
431
+ * and never modified — skill guidance is injected as a separate system
432
+ * message so this value stays static and cacheable.
433
+ */
434
+ abstract getSystemPrompt(): string;
435
+ /**
436
+ * Return the model used for compaction summarisation.
437
+ *
438
+ * Defaults to getModel() — the agent's primary model — so compaction is
439
+ * enabled automatically. Override to substitute a cheaper or faster model
440
+ * for summarisation (e.g. a smaller model when the primary is expensive).
441
+ *
442
+ * To opt out of message compaction: override and return undefined.
443
+ */
444
+ protected getCompactionModel(): LanguageModel;
445
+ onChatMessage(onFinish: StreamTextOnFinishCallback<ToolSet>, options?: OnChatMessageOptions): Promise<Response | undefined>;
446
+ }
447
+ //#endregion
448
+ //#region src/features/skills/index.d.ts
449
+ /**
450
+ * Creates a skill loading system for use with the Vercel AI SDK.
451
+ *
452
+ * The agent starts with only its always-on tools active. The LLM can call
453
+ * activate_skill to load skill tools on demand. Which skills are loaded is
454
+ * persisted to D1 across turns — no message-history parsing required.
455
+ *
456
+ * Guidance from loaded skills is injected as a system message just before
457
+ * the current user turn, keeping the `system` prompt static and cacheable.
458
+ * prepareStep keeps the guidance message updated if new skills load mid-turn.
459
+ *
460
+ * Usage with streamText (ai v6):
461
+ * ```typescript
462
+ * import { streamText, convertToModelMessages, stepCountIs } from "ai";
463
+ *
464
+ * // initialLoadedSkills comes from D1 (read at turn start by the agent).
465
+ * // onSkillsChanged is called when new skills are loaded; the agent
466
+ * // buffers the value and writes it to D1 at turn end in persistMessages.
467
+ * const lt = createSkills({ tools, skills, initialLoadedSkills, onSkillsChanged });
468
+ * const messages = injectGuidance(modelMessages, lt.getLoadedGuidance());
469
+ *
470
+ * const result = streamText({
471
+ * model,
472
+ * system: baseSystemPrompt, // static — never contains guidance, stays cacheable
473
+ * messages,
474
+ * tools: lt.tools,
475
+ * activeTools: lt.activeTools,
476
+ * prepareStep: lt.prepareStep, // keeps guidance message updated mid-turn
477
+ * stopWhen: stepCountIs(20),
478
+ * });
479
+ * ```
480
+ */
481
+ declare function createSkills(config: SkillsConfig): SkillsResult & {
482
+ tools: ToolSet;
483
+ activeTools: string[];
484
+ prepareStep: PrepareStepFunction;
485
+ };
486
+ /**
487
+ * Removes ephemeral messages from the conversation before it is saved to D1.
488
+ *
489
+ * Three kinds of messages are stripped:
490
+ *
491
+ * 1. list_capabilities tool calls — always stripped. Capability discovery is
492
+ * only relevant within the current turn; it adds no useful context for
493
+ * future turns.
494
+ *
495
+ * 2. activate_skill calls when nothing was newly loaded — stripped when all
496
+ * requested skills were already active, or when all were denied. In both
497
+ * cases nothing changed, so persisting the call would only add noise.
498
+ *
499
+ * 3. Guidance system messages — stripped by exact content match against the
500
+ * provided guidance string. Guidance is always recomputed from loaded skill
501
+ * definitions at turn start, so persisting it would create a redundant
502
+ * second source of truth alongside the loaded_skills D1 column.
503
+ *
504
+ * When skills ARE successfully loaded, the short "Loaded: X" result is kept
505
+ * in history for model context — so the model can see what was loaded in
506
+ * prior turns. Skill state is restored from D1 loaded_skills, not from these
507
+ * strings.
508
+ *
509
+ * If stripping leaves an assistant message with no parts, the entire message
510
+ * is dropped (e.g. a step that did nothing but call list_capabilities).
511
+ */
512
+ declare function filterEphemeralMessages(messages: UIMessage[], guidanceToStrip?: string): UIMessage[];
513
+ /**
514
+ * Injects loaded skill guidance as a system message just before the last
515
+ * message in the array (typically the current user turn).
516
+ *
517
+ * Guidance is kept separate from the static `system` prompt so that the
518
+ * system prompt stays identical on every turn and can be prompt-cached.
519
+ * Positioning just before the last message means guidance survives any
520
+ * compaction strategy that preserves recent context.
521
+ *
522
+ * Pass `previousGuidance` (the string injected on the prior call) to remove
523
+ * the stale guidance message before inserting the updated one. Removal is by
524
+ * exact content match — not by role — so other system messages (memories,
525
+ * user preferences, etc.) are left untouched.
526
+ *
527
+ * At turn start, omit `previousGuidance` — guidance is never persisted to D1
528
+ * (it is stripped by filterEphemeralMessages before saving), so there is
529
+ * nothing to remove. prepareStep uses previousGuidance within a turn to
530
+ * handle guidance updates when new skills are loaded mid-turn.
531
+ *
532
+ * ```typescript
533
+ * // Turn start — just inject
534
+ * const messages = injectGuidance(modelMessages, skills.getLoadedGuidance());
535
+ *
536
+ * // prepareStep — remove stale guidance then re-inject updated guidance
537
+ * const messages = injectGuidance(stepMessages, newGuidance, previousGuidance);
538
+ * ```
539
+ */
540
+ declare function injectGuidance(messages: ModelMessage[], guidance: string, previousGuidance?: string): ModelMessage[];
541
+ //#endregion
542
+ //#region src/agents/chat/compaction/index.d.ts
543
+ declare const COMPACT_TOKEN_THRESHOLD = 140000;
544
+ /**
545
+ * Estimates token count for a message array using a 3.5 chars/token
546
+ * approximation — the same heuristic used by slack-bot. Counts text from
547
+ * text parts, tool inputs/outputs, and reasoning parts.
548
+ */
549
+ declare function estimateMessagesTokens(messages: UIMessage[]): number;
550
+ /**
551
+ * Summarizes older messages into a single system message and appends the
552
+ * recent verbatim tail. Returns messages unchanged if the history is already
553
+ * short enough to fit within tailSize.
554
+ */
555
+ declare function compactMessages(messages: UIMessage[], model: LanguageModel, tailSize: number): Promise<UIMessage[]>;
556
+ /**
557
+ * Entry point called from persistMessages once per turn.
558
+ *
559
+ * Returns messages unchanged when:
560
+ * - model is undefined (compaction disabled on this agent)
561
+ * - estimated token count is under COMPACT_TOKEN_THRESHOLD
562
+ *
563
+ * Otherwise delegates to compactMessages.
564
+ */
565
+ declare function compactIfNeeded(messages: UIMessage[], model: LanguageModel | undefined, tailSize: number): Promise<UIMessage[]>;
566
+ //#endregion
567
+ export { AIChatAgent, AIChatAgentBase, COMPACT_TOKEN_THRESHOLD, type Skill, type SkillContext, type SkillsConfig, type SkillsResult, compactIfNeeded, compactMessages, createSkills, estimateMessagesTokens, filterEphemeralMessages, injectGuidance, withSkills };
package/dist/index.mjs ADDED
@@ -0,0 +1,676 @@
1
+ import { convertToModelMessages, generateText, jsonSchema, stepCountIs, streamText, tool } from "ai";
2
+ import { AIChatAgent as AIChatAgent$1 } from "@cloudflare/ai-chat";
3
+ //#region src/features/skills/meta-tools.ts
4
+ /**
5
+ * Names and descriptions for the built-in meta tools.
6
+ *
7
+ * The execute logic for these lives in createSkills() where it has
8
+ * access to the closure state (loadedSkills).
9
+ */
10
+ const ACTIVATE_SKILL = "activate_skill";
11
+ const LIST_CAPABILITIES = "list_capabilities";
12
+ /**
13
+ * Builds the tool description for activate_skill, including the
14
+ * current list of available skills with their descriptions.
15
+ */
16
+ function buildActivateSkillDescription(skills) {
17
+ return [
18
+ "Load additional skills to help with the user's request.",
19
+ "Call this BEFORE attempting actions that need tools from unloaded skills.",
20
+ "",
21
+ "Available skills:",
22
+ skills.map((s) => `• ${s.name} — ${s.description}`).join("\n")
23
+ ].join("\n");
24
+ }
25
+ const LIST_CAPABILITIES_DESCRIPTION = "List all tools currently available to you, which skills are loaded, and which can still be loaded. Call this when the user asks about your capabilities or what you can do.";
26
+ //#endregion
27
+ //#region src/features/skills/index.ts
28
+ /**
29
+ * Creates a skill loading system for use with the Vercel AI SDK.
30
+ *
31
+ * The agent starts with only its always-on tools active. The LLM can call
32
+ * activate_skill to load skill tools on demand. Which skills are loaded is
33
+ * persisted to D1 across turns — no message-history parsing required.
34
+ *
35
+ * Guidance from loaded skills is injected as a system message just before
36
+ * the current user turn, keeping the `system` prompt static and cacheable.
37
+ * prepareStep keeps the guidance message updated if new skills load mid-turn.
38
+ *
39
+ * Usage with streamText (ai v6):
40
+ * ```typescript
41
+ * import { streamText, convertToModelMessages, stepCountIs } from "ai";
42
+ *
43
+ * // initialLoadedSkills comes from D1 (read at turn start by the agent).
44
+ * // onSkillsChanged is called when new skills are loaded; the agent
45
+ * // buffers the value and writes it to D1 at turn end in persistMessages.
46
+ * const lt = createSkills({ tools, skills, initialLoadedSkills, onSkillsChanged });
47
+ * const messages = injectGuidance(modelMessages, lt.getLoadedGuidance());
48
+ *
49
+ * const result = streamText({
50
+ * model,
51
+ * system: baseSystemPrompt, // static — never contains guidance, stays cacheable
52
+ * messages,
53
+ * tools: lt.tools,
54
+ * activeTools: lt.activeTools,
55
+ * prepareStep: lt.prepareStep, // keeps guidance message updated mid-turn
56
+ * stopWhen: stepCountIs(20),
57
+ * });
58
+ * ```
59
+ */
60
+ function createSkills(config) {
61
+ const { tools: alwaysOnTools, skills, filterSkill, onSkillsChanged } = config;
62
+ const loadedSkills = new Set(config.initialLoadedSkills ?? []);
63
+ const skillMap = new Map(skills.map((s) => [s.name, s]));
64
+ const allTools = {};
65
+ Object.assign(allTools, alwaysOnTools);
66
+ for (const skill of skills) Object.assign(allTools, skill.tools);
67
+ function getActiveToolNames() {
68
+ const names = [
69
+ ACTIVATE_SKILL,
70
+ LIST_CAPABILITIES,
71
+ ...Object.keys(alwaysOnTools)
72
+ ];
73
+ for (const skillName of loadedSkills) {
74
+ const skill = skillMap.get(skillName);
75
+ if (!skill) continue;
76
+ for (const toolName of Object.keys(skill.tools)) if (!names.includes(toolName)) names.push(toolName);
77
+ }
78
+ return names;
79
+ }
80
+ function getLoadedGuidance() {
81
+ return [...loadedSkills].map((name) => skillMap.get(name)?.guidance).filter((g) => Boolean(g)).join("\n\n");
82
+ }
83
+ function getSystem() {
84
+ const guidance = getLoadedGuidance();
85
+ if (!config.systemPrompt) return guidance;
86
+ return guidance ? `${config.systemPrompt}\n\n${guidance}` : config.systemPrompt;
87
+ }
88
+ allTools[ACTIVATE_SKILL] = tool({
89
+ description: buildActivateSkillDescription(skills),
90
+ inputSchema: jsonSchema({
91
+ type: "object",
92
+ properties: { skills: {
93
+ type: "array",
94
+ items: {
95
+ type: "string",
96
+ enum: skills.map((s) => s.name)
97
+ },
98
+ description: "Skills to load"
99
+ } },
100
+ required: ["skills"]
101
+ }),
102
+ execute: async ({ skills: requested }) => {
103
+ const newlyLoaded = [];
104
+ const denied = [];
105
+ for (const skillName of requested) {
106
+ if (!skillMap.get(skillName)) continue;
107
+ if (loadedSkills.has(skillName)) continue;
108
+ if (!(filterSkill ? await filterSkill(skillName) : true)) {
109
+ denied.push(skillName);
110
+ continue;
111
+ }
112
+ loadedSkills.add(skillName);
113
+ newlyLoaded.push(skillName);
114
+ }
115
+ if (newlyLoaded.length > 0 && onSkillsChanged) await onSkillsChanged([...loadedSkills]);
116
+ if (newlyLoaded.length > 0) {
117
+ let result = `Loaded: ${newlyLoaded.join(", ")}.`;
118
+ if (denied.length > 0) result += ` Access denied for: ${denied.join(", ")}.`;
119
+ return result;
120
+ }
121
+ if (denied.length > 0) return `Access denied for: ${denied.join(", ")}.`;
122
+ return ALREADY_LOADED_OUTPUT;
123
+ }
124
+ });
125
+ allTools[LIST_CAPABILITIES] = tool({
126
+ description: LIST_CAPABILITIES_DESCRIPTION,
127
+ inputSchema: jsonSchema({
128
+ type: "object",
129
+ properties: {},
130
+ required: []
131
+ }),
132
+ execute: async () => {
133
+ const activeNames = getActiveToolNames();
134
+ const loadedNames = [...loadedSkills];
135
+ const unloaded = skills.filter((s) => !loadedSkills.has(s.name)).map((s) => s.name);
136
+ return [
137
+ `Active tools (${activeNames.length}): ${activeNames.join(", ")}`,
138
+ `Loaded skills: ${loadedNames.length > 0 ? loadedNames.join(", ") : "none"}`,
139
+ `Available to load: ${unloaded.length > 0 ? unloaded.join(", ") : "none"}`
140
+ ].join("\n");
141
+ }
142
+ });
143
+ const prepareStep = async () => {
144
+ return {
145
+ activeTools: getActiveToolNames(),
146
+ ...config.systemPrompt !== void 0 && { system: getSystem() }
147
+ };
148
+ };
149
+ return {
150
+ tools: allTools,
151
+ activeTools: getActiveToolNames(),
152
+ prepareStep,
153
+ getLoadedGuidance,
154
+ getSystem,
155
+ getLoadedSkills() {
156
+ return [...loadedSkills];
157
+ }
158
+ };
159
+ }
160
+ const ALREADY_LOADED_OUTPUT = "All requested skills were already loaded.";
161
+ const DENIED_OUTPUT_PREFIX = "Access denied for:";
162
+ /**
163
+ * Removes ephemeral messages from the conversation before it is saved to D1.
164
+ *
165
+ * Three kinds of messages are stripped:
166
+ *
167
+ * 1. list_capabilities tool calls — always stripped. Capability discovery is
168
+ * only relevant within the current turn; it adds no useful context for
169
+ * future turns.
170
+ *
171
+ * 2. activate_skill calls when nothing was newly loaded — stripped when all
172
+ * requested skills were already active, or when all were denied. In both
173
+ * cases nothing changed, so persisting the call would only add noise.
174
+ *
175
+ * 3. Guidance system messages — stripped by exact content match against the
176
+ * provided guidance string. Guidance is always recomputed from loaded skill
177
+ * definitions at turn start, so persisting it would create a redundant
178
+ * second source of truth alongside the loaded_skills D1 column.
179
+ *
180
+ * When skills ARE successfully loaded, the short "Loaded: X" result is kept
181
+ * in history for model context — so the model can see what was loaded in
182
+ * prior turns. Skill state is restored from D1 loaded_skills, not from these
183
+ * strings.
184
+ *
185
+ * If stripping leaves an assistant message with no parts, the entire message
186
+ * is dropped (e.g. a step that did nothing but call list_capabilities).
187
+ */
188
+ function filterEphemeralMessages(messages, guidanceToStrip) {
189
+ return messages.flatMap((msg) => {
190
+ if (msg.role === "system" && guidanceToStrip) {
191
+ if (msg.parts?.some((p) => "text" in p && p.text === guidanceToStrip)) return [];
192
+ }
193
+ if (msg.role !== "assistant" || !msg.parts?.length) return [msg];
194
+ const filtered = msg.parts.filter((part) => {
195
+ if (!("toolCallId" in part)) return true;
196
+ const { type, output } = part;
197
+ if (type === `tool-list_capabilities`) return false;
198
+ if (type === `tool-activate_skill`) {
199
+ if (typeof output !== "string") return true;
200
+ return output !== ALREADY_LOADED_OUTPUT && !output.startsWith(DENIED_OUTPUT_PREFIX);
201
+ }
202
+ return true;
203
+ });
204
+ if (filtered.length === 0) return [];
205
+ if (filtered.length === msg.parts.length) return [msg];
206
+ return [{
207
+ ...msg,
208
+ parts: filtered
209
+ }];
210
+ });
211
+ }
212
+ /**
213
+ * Injects loaded skill guidance as a system message just before the last
214
+ * message in the array (typically the current user turn).
215
+ *
216
+ * Guidance is kept separate from the static `system` prompt so that the
217
+ * system prompt stays identical on every turn and can be prompt-cached.
218
+ * Positioning just before the last message means guidance survives any
219
+ * compaction strategy that preserves recent context.
220
+ *
221
+ * Pass `previousGuidance` (the string injected on the prior call) to remove
222
+ * the stale guidance message before inserting the updated one. Removal is by
223
+ * exact content match — not by role — so other system messages (memories,
224
+ * user preferences, etc.) are left untouched.
225
+ *
226
+ * At turn start, omit `previousGuidance` — guidance is never persisted to D1
227
+ * (it is stripped by filterEphemeralMessages before saving), so there is
228
+ * nothing to remove. prepareStep uses previousGuidance within a turn to
229
+ * handle guidance updates when new skills are loaded mid-turn.
230
+ *
231
+ * ```typescript
232
+ * // Turn start — just inject
233
+ * const messages = injectGuidance(modelMessages, skills.getLoadedGuidance());
234
+ *
235
+ * // prepareStep — remove stale guidance then re-inject updated guidance
236
+ * const messages = injectGuidance(stepMessages, newGuidance, previousGuidance);
237
+ * ```
238
+ */
239
+ function injectGuidance(messages, guidance, previousGuidance) {
240
+ if (!guidance) return messages;
241
+ const base = previousGuidance ? messages.filter((m) => !(m.role === "system" && m.content === previousGuidance)) : messages;
242
+ const insertAt = base.findLastIndex((m) => m.role === "user");
243
+ return [
244
+ ...base.slice(0, insertAt),
245
+ {
246
+ role: "system",
247
+ content: guidance
248
+ },
249
+ ...base.slice(insertAt)
250
+ ];
251
+ }
252
+ //#endregion
253
+ //#region src/agents/chat/compaction/index.ts
254
+ /**
255
+ * Message compaction for long-running conversations.
256
+ *
257
+ * When the stored conversation history exceeds COMPACT_TOKEN_THRESHOLD, older
258
+ * messages are summarised via an LLM call and replaced with a single system
259
+ * message containing the summary, followed by the recent verbatim tail.
260
+ *
261
+ * Entry point: compactIfNeeded() — called once per turn from persistMessages.
262
+ *
263
+ * To remove compaction entirely: delete this directory, remove the import in
264
+ * AIChatAgentBase, and change `toSave` back to `filtered`.
265
+ */
266
+ const COMPACT_TOKEN_THRESHOLD = 14e4;
267
+ const TOOL_RESULT_PREVIEW_CHARS = 200;
268
+ const SUMMARY_MAX_TOKENS = 4e3;
269
+ /**
270
+ * Estimates token count for a message array using a 3.5 chars/token
271
+ * approximation — the same heuristic used by slack-bot. Counts text from
272
+ * text parts, tool inputs/outputs, and reasoning parts.
273
+ */
274
+ function estimateMessagesTokens(messages) {
275
+ let totalChars = 0;
276
+ for (const msg of messages) {
277
+ if (!msg.parts) continue;
278
+ for (const part of msg.parts) {
279
+ if ((part.type === "text" || part.type === "reasoning") && "text" in part) {
280
+ totalChars += part.text.length;
281
+ continue;
282
+ }
283
+ if ("toolCallId" in part) {
284
+ const toolPart = part;
285
+ if (toolPart.input) totalChars += JSON.stringify(toolPart.input).length;
286
+ if (toolPart.output !== void 0) {
287
+ const outputStr = typeof toolPart.output === "string" ? toolPart.output : JSON.stringify(toolPart.output);
288
+ totalChars += outputStr.length;
289
+ }
290
+ }
291
+ }
292
+ }
293
+ return Math.ceil(totalChars / 3.5);
294
+ }
295
+ /**
296
+ * Renders messages as human-readable text for the compaction summary prompt.
297
+ * Text parts are included verbatim; tool calls show name and a truncated result.
298
+ * step-start and empty messages are omitted.
299
+ */
300
+ function formatMessagesForSummary(messages) {
301
+ const lines = [];
302
+ for (const msg of messages) {
303
+ const roleLabel = msg.role.charAt(0).toUpperCase() + msg.role.slice(1);
304
+ const parts = [];
305
+ for (const part of msg.parts ?? []) {
306
+ if (part.type === "step-start") continue;
307
+ if ((part.type === "text" || part.type === "reasoning") && "text" in part) {
308
+ const text = part.text.trim();
309
+ if (text) parts.push(text);
310
+ continue;
311
+ }
312
+ if ("toolCallId" in part) {
313
+ const toolPart = part;
314
+ const toolName = toolPart.type.startsWith("tool-") ? toolPart.type.slice(5) : toolPart.type;
315
+ const rawOutput = toolPart.output === void 0 ? "no result" : typeof toolPart.output === "string" ? toolPart.output : JSON.stringify(toolPart.output);
316
+ const preview = rawOutput.slice(0, TOOL_RESULT_PREVIEW_CHARS);
317
+ const ellipsis = rawOutput.length > TOOL_RESULT_PREVIEW_CHARS ? "..." : "";
318
+ parts.push(`[Tool: ${toolName}, result: ${preview}${ellipsis}]`);
319
+ }
320
+ }
321
+ if (parts.length > 0) lines.push(`${roleLabel}: ${parts.join(" ")}`);
322
+ }
323
+ return lines.join("\n\n");
324
+ }
325
+ /**
326
+ * Calls the LLM to produce a concise summary of old + recent message windows.
327
+ * Weights the prompt toward recent exchanges, matching slack-bot's approach.
328
+ */
329
+ async function generateCompactionSummary(oldMessages, recentMessages, model) {
330
+ const prompt = `Summarize this conversation history concisely for an AI assistant to continue the conversation.
331
+ Focus MORE on recent exchanges (what the user was working on, what tools were used, what was found).
332
+ Include key facts, decisions, and context needed to continue the conversation.
333
+ Keep entity names, numbers, file paths, and specific details that might be referenced later.
334
+ Do NOT include pleasantries or meta-commentary - just the essential context.
335
+
336
+ OLDER MESSAGES (summarize briefly):
337
+ ${formatMessagesForSummary(oldMessages)}
338
+
339
+ RECENT MESSAGES (summarize with more detail - this is where the user currently is):
340
+ ${formatMessagesForSummary(recentMessages)}
341
+
342
+ Write a concise summary:`;
343
+ try {
344
+ const { text } = await generateText({
345
+ model,
346
+ messages: [{
347
+ role: "user",
348
+ content: prompt
349
+ }],
350
+ maxOutputTokens: SUMMARY_MAX_TOKENS
351
+ });
352
+ return text || "Unable to summarize conversation history.";
353
+ } catch (error) {
354
+ console.error("Compaction summarization error:", error);
355
+ return "Unable to summarize conversation history.";
356
+ }
357
+ }
358
+ /**
359
+ * Summarizes older messages into a single system message and appends the
360
+ * recent verbatim tail. Returns messages unchanged if the history is already
361
+ * short enough to fit within tailSize.
362
+ */
363
+ async function compactMessages(messages, model, tailSize) {
364
+ if (messages.length <= tailSize) return messages;
365
+ const splitIndex = messages.length - tailSize;
366
+ const oldMessages = messages.slice(0, splitIndex);
367
+ const recentTail = messages.slice(splitIndex);
368
+ const summary = await generateCompactionSummary(oldMessages, recentTail, model);
369
+ return [{
370
+ id: `compact_${Date.now()}_${Math.random().toString(36).slice(2, 9)}`,
371
+ role: "system",
372
+ parts: [{
373
+ type: "text",
374
+ text: `[Conversation summary - older context was compacted]\n${summary}`,
375
+ state: "done"
376
+ }]
377
+ }, ...recentTail];
378
+ }
379
+ /**
380
+ * Entry point called from persistMessages once per turn.
381
+ *
382
+ * Returns messages unchanged when:
383
+ * - model is undefined (compaction disabled on this agent)
384
+ * - estimated token count is under COMPACT_TOKEN_THRESHOLD
385
+ *
386
+ * Otherwise delegates to compactMessages.
387
+ */
388
+ async function compactIfNeeded(messages, model, tailSize) {
389
+ if (!model || estimateMessagesTokens(messages) <= 14e4) return messages;
390
+ return compactMessages(messages, model, tailSize);
391
+ }
392
+ //#endregion
393
+ //#region src/agents/chat/AIChatAgentBase.ts
394
+ /**
395
+ * Base class for chat agents with lazy skill loading.
396
+ *
397
+ * Owns:
398
+ * - D1 persistence for loaded skill state (skill names survive DO eviction)
399
+ * - Ephemeral message filtering (list_capabilities, no-op activate_skill calls)
400
+ * - Message compaction (LLM summarisation when history exceeds token threshold)
401
+ * - History replay to newly connected clients (onConnect override)
402
+ * - Skill context preparation for use with the @withSkills decorator
403
+ *
404
+ * Conversation messages are stored in Durable Object SQLite, managed
405
+ * automatically by the Cloudflare AIChatAgent — no D1 write needed for messages.
406
+ *
407
+ * D1 is written only when skills change (activate_skill was called this turn),
408
+ * not on every turn.
409
+ *
410
+ * ## Usage
411
+ *
412
+ * Extend this class when you want full control over `streamText`. Implement
413
+ * `getTools()`, `getSkills()`, and your own `onChatMessage` decorated with
414
+ * `@withSkills`:
415
+ *
416
+ * ```typescript
417
+ * export class MyAgent extends AIChatAgentBase {
418
+ * getTools() { return []; }
419
+ * getSkills() { return [searchSkill, codeSkill]; }
420
+ * getDB() { return this.env.AGENT_DB; }
421
+ *
422
+ * @withSkills
423
+ * async onChatMessage(onFinish, ctx: SkillContext, options?) {
424
+ * const { messages, ...skillArgs } = ctx;
425
+ * return streamText({
426
+ * model: openai("gpt-4o"),
427
+ * system: "You are a helpful assistant.",
428
+ * messages,
429
+ * ...skillArgs,
430
+ * onFinish,
431
+ * stopWhen: stepCountIs(20),
432
+ * }).toUIMessageStreamResponse();
433
+ * }
434
+ * }
435
+ * ```
436
+ *
437
+ * For a batteries-included experience where the base class owns `onChatMessage`,
438
+ * extend `AIChatAgent` instead.
439
+ */
440
+ var AIChatAgentBase = class extends AIChatAgent$1 {
441
+ /**
442
+ * Maximum number of messages stored in DO SQLite.
443
+ *
444
+ * Lowered from the Cloudflare AIChatAgent default of 200. When compaction
445
+ * is enabled, one slot is reserved for the summary message so the verbatim
446
+ * tail is maxPersistedMessages - 1 recent messages. Raise or lower per agent.
447
+ */
448
+ maxPersistedMessages = 50;
449
+ /**
450
+ * Return a LanguageModel to use for compaction summarisation.
451
+ *
452
+ * Return undefined (default) to disable compaction — messages are kept up
453
+ * to maxPersistedMessages and older ones are dropped by the Cloudflare
454
+ * AIChatAgent's built-in hard cap.
455
+ *
456
+ * Override to use a cheaper or faster model for summarisation, or to enable
457
+ * compaction in subclasses that do not override it automatically.
458
+ */
459
+ getCompactionModel() {}
460
+ /**
461
+ * Return the D1 database binding for persisting loaded skill state.
462
+ *
463
+ * Override in your subclass to return the binding from env:
464
+ * ```typescript
465
+ * protected getDB() { return this.env.AGENT_DB; }
466
+ * ```
467
+ *
468
+ * Defaults to undefined — when undefined, loaded skills reset on every new
469
+ * conversation (skills still work within a turn, just not across turns).
470
+ */
471
+ getDB() {}
472
+ /**
473
+ * Optional permission hook. Return false to deny the agent access to a
474
+ * skill when activate_skill is called. Defaults to allow-all.
475
+ */
476
+ async filterSkill(_skillName) {
477
+ return true;
478
+ }
479
+ /**
480
+ * Buffered skill state from the current turn.
481
+ *
482
+ * Set by the onSkillsChanged callback when activate_skill loads new skills
483
+ * mid-turn. Flushed to D1 in persistMessages at turn end — only written
484
+ * when this value is set, so D1 is not touched on turns where no new skills
485
+ * are loaded.
486
+ */
487
+ _pendingSkills;
488
+ /**
489
+ * Reads loaded skill names from D1 for this agent.
490
+ *
491
+ * Returns an empty array if no record exists (first turn, or no skills
492
+ * loaded yet). Conversation messages are not read here — the Cloudflare
493
+ * AIChatAgent provides those via this.messages from DO SQLite.
494
+ */
495
+ async _readSkillState() {
496
+ const row = await this.getDB()?.prepare("SELECT loaded_skills FROM agent_state WHERE agent_id = ?").bind(this.name).first();
497
+ if (!row?.loaded_skills) return [];
498
+ return JSON.parse(row.loaded_skills);
499
+ }
500
+ /**
501
+ * Writes loaded skill names to D1 for this agent.
502
+ *
503
+ * Uses INSERT OR REPLACE so the first skill load creates the row and
504
+ * subsequent loads update it. Only called when skills actually changed
505
+ * this turn (_pendingSkills is set).
506
+ */
507
+ async _writeSkillState(skills) {
508
+ await this.getDB()?.prepare("INSERT OR REPLACE INTO agent_state (agent_id, loaded_skills, last_updated) VALUES (?, ?, ?)").bind(this.name, JSON.stringify(skills), Date.now()).run();
509
+ }
510
+ /**
511
+ * Flush persisted message history to a newly connected client.
512
+ *
513
+ * The Cloudflare AIChatAgent broadcasts message updates to existing
514
+ * connections via persistMessages, but does nothing for connections that
515
+ * arrive after a conversation has ended. Without this override, a page
516
+ * refresh produces an empty UI even though the history is intact in DO SQLite.
517
+ *
518
+ * Skips replay when a stream is active — CF_AGENT_STREAM_RESUMING handles
519
+ * that case and replays in-progress chunks via its own protocol.
520
+ */
521
+ async onConnect(connection, ctx) {
522
+ await super.onConnect(connection, ctx);
523
+ if (!this._activeStreamId && this.messages.length > 0) connection.send(JSON.stringify({
524
+ type: "cf_agent_chat_messages",
525
+ messages: this.messages
526
+ }));
527
+ }
528
+ /**
529
+ * Strips ephemeral content, conditionally saves skill state to D1, then
530
+ * delegates to super for DO SQLite persistence and WebSocket broadcast.
531
+ *
532
+ * The Cloudflare AIChatAgent calls persistMessages once per turn after all
533
+ * steps complete, so overriding here is the correct place to act — it runs
534
+ * after the full assistant message (including all tool results) is assembled.
535
+ *
536
+ * Two things happen here:
537
+ *
538
+ * 1. Ephemeral tool calls are stripped — list_capabilities (always) and
539
+ * activate_skill when nothing was newly loaded (no state change).
540
+ *
541
+ * 2. If skills changed this turn (_pendingSkills is set), the updated list
542
+ * is written to D1. Turns where no skills were loaded do not touch D1.
543
+ *
544
+ * Message persistence itself is handled by super.persistMessages, which
545
+ * writes to DO SQLite — no D1 write needed for messages.
546
+ */
547
+ async persistMessages(messages, excludeBroadcastIds = [], options) {
548
+ const filtered = filterEphemeralMessages(messages);
549
+ if (this._pendingSkills !== void 0) {
550
+ await this._writeSkillState(this._pendingSkills);
551
+ this._pendingSkills = void 0;
552
+ }
553
+ const toSave = await compactIfNeeded(filtered, this.getCompactionModel(), this.maxPersistedMessages - 1);
554
+ return super.persistMessages(toSave, excludeBroadcastIds, options);
555
+ }
556
+ /**
557
+ * Widened onChatMessage signature that accommodates the @withSkills decorator.
558
+ *
559
+ * The decorator transforms the consumer's 3-arg form (onFinish, ctx, options) into
560
+ * a 2-arg wrapper at runtime. This declaration widens the base class signature so
561
+ * that TypeScript accepts the consumer's 3-arg override without errors.
562
+ *
563
+ * @ts-ignore — intentional: widens the Cloudflare AIChatAgent's (onFinish, options?) signature.
564
+ */
565
+ onChatMessage(onFinish, ctxOrOptions) {
566
+ return super.onChatMessage(onFinish, ctxOrOptions);
567
+ }
568
+ /**
569
+ * Called by the @withSkills decorator at the start of each turn.
570
+ *
571
+ * Reads loaded skill state from D1, seeds createSkills, and returns a
572
+ * SkillContext ready to use in a streamText call.
573
+ *
574
+ * Guidance is exposed as `ctx.guidance` — compose your system prompt as:
575
+ * `${myBase}${ctx.guidance ? '\n\n' + ctx.guidance : ''}`
576
+ *
577
+ * Messages are plain (no guidance injected). Guidance stays out of the
578
+ * messages array — Anthropic/Gemini only allow system messages at position 0.
579
+ */
580
+ async _prepareSkillContext() {
581
+ const loadedSkills = await this._readSkillState();
582
+ const skills = createSkills({
583
+ tools: this.getTools(),
584
+ skills: this.getSkills(),
585
+ initialLoadedSkills: loadedSkills,
586
+ onSkillsChanged: async (updated) => {
587
+ this._pendingSkills = updated;
588
+ },
589
+ filterSkill: (name) => this.filterSkill(name)
590
+ });
591
+ return {
592
+ tools: skills.tools,
593
+ activeTools: skills.activeTools,
594
+ prepareStep: skills.prepareStep,
595
+ guidance: skills.getLoadedGuidance(),
596
+ messages: await convertToModelMessages(this.messages)
597
+ };
598
+ }
599
+ };
600
+ function withSkills(fn, _context) {
601
+ const wrapper = async function(onFinish, maybeOptions) {
602
+ const ctx = await this._prepareSkillContext();
603
+ return fn.call(this, onFinish, ctx, maybeOptions);
604
+ };
605
+ return wrapper;
606
+ }
607
+ //#endregion
608
+ //#region src/agents/chat/AIChatAgent.ts
609
+ /**
610
+ * Batteries-included base class for chat agents with lazy skill loading.
611
+ *
612
+ * Owns the full `onChatMessage` lifecycle. Implement four abstract methods and
613
+ * get lazy skill loading, cross-turn skill persistence, guidance injection,
614
+ * ephemeral message cleanup, and message compaction for free.
615
+ *
616
+ * Conversation messages are stored in Durable Object SQLite by the Cloudflare
617
+ * AIChatAgent automatically — available as this.messages at the start of each
618
+ * turn. Loaded skill state is stored in D1 (via getDB()) and read at turn start.
619
+ * Guidance is injected as a system message just before the current user turn,
620
+ * keeping the `system` param static and cacheable across all turns.
621
+ *
622
+ * ```typescript
623
+ * export class MyAgent extends AIChatAgent {
624
+ * getModel() { return openai("gpt-4o"); }
625
+ * getTools() { return tools; }
626
+ * getSkills() { return [searchSkill, codeSkill]; }
627
+ * getSystemPrompt() { return "You are a helpful assistant."; }
628
+ * getDB() { return this.env.AGENT_DB; }
629
+ * }
630
+ * ```
631
+ *
632
+ * If you need full control over the `streamText` call (custom model options,
633
+ * streaming transforms, varying the model per request, etc.) use
634
+ * `AIChatAgentBase` with the `@withSkills` decorator instead.
635
+ */
636
+ var AIChatAgent = class extends AIChatAgentBase {
637
+ /**
638
+ * Return the model used for compaction summarisation.
639
+ *
640
+ * Defaults to getModel() — the agent's primary model — so compaction is
641
+ * enabled automatically. Override to substitute a cheaper or faster model
642
+ * for summarisation (e.g. a smaller model when the primary is expensive).
643
+ *
644
+ * To opt out of message compaction: override and return undefined.
645
+ */
646
+ getCompactionModel() {
647
+ return this.getModel();
648
+ }
649
+ async onChatMessage(onFinish, options) {
650
+ const loadedSkills = await this._readSkillState();
651
+ const skills = createSkills({
652
+ tools: this.getTools(),
653
+ skills: this.getSkills(),
654
+ systemPrompt: this.getSystemPrompt(),
655
+ initialLoadedSkills: loadedSkills,
656
+ onSkillsChanged: async (updated) => {
657
+ this._pendingSkills = updated;
658
+ },
659
+ filterSkill: (name) => this.filterSkill(name)
660
+ });
661
+ return streamText({
662
+ model: this.getModel(),
663
+ system: skills.getSystem(),
664
+ messages: await convertToModelMessages(this.messages),
665
+ tools: skills.tools,
666
+ activeTools: skills.activeTools,
667
+ prepareStep: skills.prepareStep,
668
+ experimental_context: options?.body,
669
+ stopWhen: stepCountIs(20),
670
+ abortSignal: options?.abortSignal,
671
+ onFinish
672
+ }).toUIMessageStreamResponse();
673
+ }
674
+ };
675
+ //#endregion
676
+ export { AIChatAgent, AIChatAgentBase, COMPACT_TOKEN_THRESHOLD, compactIfNeeded, compactMessages, createSkills, estimateMessagesTokens, filterEphemeralMessages, injectGuidance, withSkills };
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@economic/agents",
3
+ "version": "0.0.1-alpha.10",
4
+ "description": "A starter for creating a TypeScript package.",
5
+ "homepage": "https://github.com/author/library#readme",
6
+ "bugs": {
7
+ "url": "https://github.com/author/library/issues"
8
+ },
9
+ "license": "MIT",
10
+ "author": "Author Name <author.name@mail.com>",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/author/library.git"
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "type": "module",
19
+ "exports": {
20
+ ".": "./dist/index.mjs",
21
+ "./package.json": "./package.json"
22
+ },
23
+ "scripts": {
24
+ "build": "vp pack",
25
+ "dev": "vp pack --watch",
26
+ "test": "vp test",
27
+ "typecheck": "tsc --noEmit",
28
+ "release": "bumpp",
29
+ "prepublishOnly": "npm run build"
30
+ },
31
+ "dependencies": {
32
+ "@cloudflare/ai-chat": "^0.1.9",
33
+ "ai": "^6.0.116"
34
+ },
35
+ "devDependencies": {
36
+ "@cloudflare/workers-types": "^4.20260317.1",
37
+ "@types/node": "^25.5.0",
38
+ "@typescript/native-preview": "7.0.0-dev.20260316.1",
39
+ "bumpp": "^11.0.1",
40
+ "typescript": "^5.9.3",
41
+ "vite-plus": "latest",
42
+ "vitest": "npm:@voidzero-dev/vite-plus-test@latest"
43
+ },
44
+ "inlinedDependencies": {
45
+ "partyserver": "0.3.3",
46
+ "zod": "3.25.76",
47
+ "@modelcontextprotocol/sdk": "1.26.0",
48
+ "agents": "0.7.6"
49
+ }
50
+ }