@economic/agents 0.0.1-alpha.22 → 0.0.1-alpha.24

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
@@ -3,7 +3,7 @@
3
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
6
+ npm install @economic/agents ai @cloudflare/ai-chat agents
7
7
  ```
8
8
 
9
9
  ---
package/dist/index.d.mts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { AIChatAgent as AIChatAgent$1, OnChatMessageOptions } from "@cloudflare/ai-chat";
2
2
  import { LanguageModel, ToolSet, UIMessage, generateText, streamText } from "ai";
3
+ import { Connection, ConnectionContext } from "agents";
3
4
 
4
5
  //#region src/features/skills/index.d.ts
5
6
  /**
@@ -79,9 +80,9 @@ declare function buildLLMParams(config: BuildLLMParamsConfig): Promise<LLMParams
79
80
  */
80
81
  declare abstract class AIChatAgent<Env extends Cloudflare.Env = Cloudflare.Env> extends AIChatAgent$1<Env> {
81
82
  /**
82
- * Composed user identifier extracted from `options.body.userId` during
83
- * `buildLLMParams`. Expected format: `{agreementNumber}_{userId}`, e.g. `148583_matt`.
84
- * Undefined if the client did not include `userId` in the request body.
83
+ * Composed user identifier extracted from the query params on the initial connection.
84
+ * Expected format: `{agreementNumber}_{userId}`, e.g. `148583_matt`.
85
+ * Undefined if the client did not include `userId`.
85
86
  */
86
87
  protected _userId: string | undefined;
87
88
  /**
@@ -100,9 +101,14 @@ declare abstract class AIChatAgent<Env extends Cloudflare.Env = Cloudflare.Env>
100
101
  /**
101
102
  * Resolves the D1 database binding and userId required for all D1 writes.
102
103
  * Returns null and silently no-ops if AGENT_DB is not bound.
103
- * Returns null and logs an error if userId is missing from the request body.
104
+ * Returns null and logs an error if userId is missing.
104
105
  */
105
106
  private resolveD1Context;
107
+ onConnect(connection: Connection, ctx: ConnectionContext): Promise<void>;
108
+ /**
109
+ * Returns all conversations for the current user.
110
+ */
111
+ getConversations(): Promise<Record<string, unknown>[] | undefined>;
106
112
  /**
107
113
  * Writes an audit event to D1 if `AGENT_DB` is bound on the environment,
108
114
  * otherwise silently does nothing.
@@ -147,7 +153,7 @@ declare abstract class AIChatAgent<Env extends Cloudflare.Env = Cloudflare.Env>
147
153
  * return streamText(params).toUIMessageStreamResponse();
148
154
  * ```
149
155
  */
150
- protected buildLLMParams(config: Omit<BuildLLMParamsConfig, "messages" | "activeSkills" | "fastModel">): ReturnType<typeof buildLLMParams>;
156
+ protected buildLLMParams<TBody = Record<string, unknown>>(config: Omit<BuildLLMParamsConfig, "messages" | "activeSkills" | "fastModel">): ReturnType<typeof buildLLMParams>;
151
157
  /**
152
158
  * Skill names persisted from previous turns, read from DO SQLite.
153
159
  * Returns an empty array if no skills have been loaded yet.
@@ -172,49 +178,6 @@ declare abstract class AIChatAgent<Env extends Cloudflare.Env = Cloudflare.Env>
172
178
  }): Promise<void>;
173
179
  }
174
180
  //#endregion
175
- //#region src/decorators/guard.d.ts
176
- /**
177
- * Function run before a guarded handler (e.g. `onChatMessage`). Receives the
178
- * custom request body from chat options (`options.body`, same shape as
179
- * `OnChatMessageOptions` from `@cloudflare/ai-chat`).
180
- *
181
- * Return a {@link Response} to short-circuit and skip the decorated method.
182
- * Return nothing (or `undefined`) to allow the method to run.
183
- *
184
- * Typical uses include auth, rate limits, or feature flags — all logic lives here;
185
- * the `guard` decorator only forwards `body` and handles the return shape.
186
- */
187
- type GuardFn<TBody = Record<string, unknown>> = (body: TBody) => Response | void | Promise<Response | void>;
188
- /**
189
- * Method decorator (TypeScript 5+ stage-3) that runs `guardFn` with the second
190
- * argument's `body` (the chat request body). If `guardFn` returns a
191
- * {@link Response}, that value is returned and the original method is not called.
192
- *
193
- * Intended for `onChatMessage(onFinish, options?)` on subclasses of
194
- * `AIChatAgent`; `options` is read as `{ body?: TBody }`.
195
- *
196
- * @param guardFn - Called with `options?.body` (cast to `TBody`) before the method body.
197
- *
198
- * @example
199
- * ```ts
200
- * interface RequestBody { token: string }
201
- *
202
- * const requireToken: GuardFn<RequestBody> = async (body) => {
203
- * if (!await isValidToken(body?.token)) {
204
- * return new Response("Unauthorized", { status: 401 });
205
- * }
206
- * };
207
- *
208
- * class MyAgent extends AIChatAgent<Env> {
209
- * @guard(requireToken)
210
- * async onChatMessage(onFinish, options) {
211
- * // ...
212
- * }
213
- * }
214
- * ```
215
- */
216
- declare function guard<TBody = Record<string, unknown>>(guardFn: GuardFn<TBody>): (target: (...args: any[]) => Promise<Response>, _context: ClassMethodDecoratorContext) => (this: unknown, ...args: unknown[]) => Promise<Response>;
217
- //#endregion
218
181
  //#region src/types.d.ts
219
182
  /**
220
183
  * The context object available throughout an agent's lifetime — passed via
@@ -223,12 +186,12 @@ declare function guard<TBody = Record<string, unknown>>(guardFn: GuardFn<TBody>)
223
186
  *
224
187
  * Define your own body shape and compose:
225
188
  * ```typescript
226
- * interface MyBody { userId: string; userTier: "free" | "pro" }
227
- * type MyContext = AgentContext<MyBody>;
189
+ * interface MyBody { userTier: "free" | "pro" }
190
+ * type MyContext = AgentToolContext<MyBody>;
228
191
  * ```
229
192
  */
230
- type AgentContext<TBody = Record<string, unknown>> = TBody & {
193
+ type AgentToolContext<TBody = Record<string, unknown>> = TBody & {
231
194
  log: (message: string, payload?: Record<string, unknown>) => void | Promise<void>;
232
195
  };
233
196
  //#endregion
234
- export { AIChatAgent, type AgentContext, type BuildLLMParamsConfig, type GuardFn, type Skill, buildLLMParams, guard };
197
+ export { AIChatAgent, type AgentToolContext, type BuildLLMParamsConfig, type Skill, buildLLMParams };