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

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.
@@ -0,0 +1,26 @@
1
+ import { createRequire } from "node:module";
2
+ //#region \0rolldown/runtime.js
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __commonJSMin = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
12
+ key = keys[i];
13
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
14
+ get: ((k) => from[k]).bind(null, key),
15
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
16
+ });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
21
+ value: mod,
22
+ enumerable: true
23
+ }) : target, mod));
24
+ var __require = /* @__PURE__ */ createRequire(import.meta.url);
25
+ //#endregion
26
+ export { __require as n, __toESM as r, __commonJSMin as t };
package/dist/index.d.mts CHANGED
@@ -1,5 +1,7 @@
1
1
  import { AIChatAgent as AIChatAgent$1, OnChatMessageOptions } from "@cloudflare/ai-chat";
2
2
  import { LanguageModel, ToolSet, UIMessage, generateText, streamText } from "ai";
3
+ import { AsyncLocalStorage } from "node:async_hooks";
4
+ import { DurableObject } from "cloudflare:workers";
3
5
 
4
6
  //#region src/features/skills/index.d.ts
5
7
  /**
@@ -65,6 +67,73 @@ type BuildLLMParamsConfig = Omit<LLMParams, "messages" | "experimental_context"
65
67
  */
66
68
  declare function buildLLMParams(config: BuildLLMParamsConfig): Promise<LLMParams>;
67
69
  //#endregion
70
+ //#region ../../node_modules/partyserver/dist/index.d.ts
71
+ //#region src/types.d.ts
72
+ type ImmutablePrimitive = undefined | null | boolean | string | number;
73
+ 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>;
74
+ type ImmutableArray<T> = ReadonlyArray<Immutable<T>>;
75
+ type ImmutableMap<K, V> = ReadonlyMap<Immutable<K>, Immutable<V>>;
76
+ type ImmutableSet<T> = ReadonlySet<Immutable<T>>;
77
+ type ImmutableObject<T> = { readonly [K in keyof T]: Immutable<T[K]> };
78
+ type ConnectionState<T> = ImmutableObject<T> | null;
79
+ type ConnectionSetStateFn<T> = (prevState: ConnectionState<T>) => T;
80
+ type ConnectionContext = {
81
+ request: Request;
82
+ };
83
+ /** A WebSocket connected to the Server */
84
+ type Connection<TState = unknown> = WebSocket & {
85
+ /** Connection identifier */id: string;
86
+ /**
87
+ * Arbitrary state associated with this connection.
88
+ * Read-only — use {@link Connection.setState} to update.
89
+ *
90
+ * This property is configurable, meaning it can be redefined via
91
+ * `Object.defineProperty` by downstream consumers (e.g. the Cloudflare
92
+ * Agents SDK) to namespace or wrap internal state storage.
93
+ */
94
+ state: ConnectionState<TState>;
95
+ /**
96
+ * Update the state associated with this connection.
97
+ *
98
+ * Accepts either a new state value or an updater function that receives
99
+ * the previous state and returns the next state.
100
+ *
101
+ * This property is configurable, meaning it can be redefined via
102
+ * `Object.defineProperty` by downstream consumers. If you redefine
103
+ * `state` and `setState`, you are responsible for calling
104
+ * `serializeAttachment` / `deserializeAttachment` yourself if you need
105
+ * the state to survive hibernation.
106
+ */
107
+ setState(state: TState | ConnectionSetStateFn<TState> | null): ConnectionState<TState>;
108
+ /**
109
+ * @deprecated use {@link Connection.setState} instead.
110
+ *
111
+ * Low-level method to persist data in the connection's attachment storage.
112
+ * This property is configurable and can be redefined by downstream
113
+ * consumers that need to wrap or namespace the underlying storage.
114
+ */
115
+ serializeAttachment<T = unknown>(attachment: T): void;
116
+ /**
117
+ * @deprecated use {@link Connection.state} instead.
118
+ *
119
+ * Low-level method to read data from the connection's attachment storage.
120
+ * This property is configurable and can be redefined by downstream
121
+ * consumers that need to wrap or namespace the underlying storage.
122
+ */
123
+ deserializeAttachment<T = unknown>(): T | null;
124
+ /**
125
+ * Tags assigned to this connection via {@link Server.getConnectionTags}.
126
+ * Always includes the connection id as the first tag.
127
+ */
128
+ tags: readonly string[];
129
+ /**
130
+ * @deprecated Use `this.name` on the Server instead.
131
+ * The server name. Populated from `Server.name` after initialization.
132
+ */
133
+ server: string;
134
+ }; //#endregion
135
+ //#region src/index.d.ts
136
+ //#endregion
68
137
  //#region src/agents/AIChatAgent.d.ts
69
138
  /**
70
139
  * Base class for Cloudflare Agents SDK chat agents with lazy skill loading
@@ -79,9 +148,9 @@ declare function buildLLMParams(config: BuildLLMParamsConfig): Promise<LLMParams
79
148
  */
80
149
  declare abstract class AIChatAgent<Env extends Cloudflare.Env = Cloudflare.Env> extends AIChatAgent$1<Env> {
81
150
  /**
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.
151
+ * Composed user identifier extracted from the query params on the initial connection.
152
+ * Expected format: `{agreementNumber}_{userId}`, e.g. `148583_matt`.
153
+ * Undefined if the client did not include `userId`.
85
154
  */
86
155
  protected _userId: string | undefined;
87
156
  /**
@@ -100,9 +169,14 @@ declare abstract class AIChatAgent<Env extends Cloudflare.Env = Cloudflare.Env>
100
169
  /**
101
170
  * Resolves the D1 database binding and userId required for all D1 writes.
102
171
  * 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.
172
+ * Returns null and logs an error if userId is missing.
104
173
  */
105
174
  private resolveD1Context;
175
+ onConnect(connection: Connection, ctx: ConnectionContext): Promise<void>;
176
+ /**
177
+ * Returns all conversations for the current user.
178
+ */
179
+ getConversations(): Promise<Record<string, unknown>[] | undefined>;
106
180
  /**
107
181
  * Writes an audit event to D1 if `AGENT_DB` is bound on the environment,
108
182
  * otherwise silently does nothing.
@@ -147,7 +221,7 @@ declare abstract class AIChatAgent<Env extends Cloudflare.Env = Cloudflare.Env>
147
221
  * return streamText(params).toUIMessageStreamResponse();
148
222
  * ```
149
223
  */
150
- protected buildLLMParams(config: Omit<BuildLLMParamsConfig, "messages" | "activeSkills" | "fastModel">): ReturnType<typeof buildLLMParams>;
224
+ protected buildLLMParams<TBody = Record<string, unknown>>(config: Omit<BuildLLMParamsConfig, "messages" | "activeSkills" | "fastModel">): ReturnType<typeof buildLLMParams>;
151
225
  /**
152
226
  * Skill names persisted from previous turns, read from DO SQLite.
153
227
  * Returns an empty array if no skills have been loaded yet.
@@ -172,49 +246,6 @@ declare abstract class AIChatAgent<Env extends Cloudflare.Env = Cloudflare.Env>
172
246
  }): Promise<void>;
173
247
  }
174
248
  //#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 | undefined) => 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: unknown[]) => Promise<Response>, _context: ClassMethodDecoratorContext) => (this: unknown, ...args: unknown[]) => Promise<Response>;
217
- //#endregion
218
249
  //#region src/types.d.ts
219
250
  /**
220
251
  * The context object available throughout an agent's lifetime — passed via
@@ -223,12 +254,12 @@ declare function guard<TBody = Record<string, unknown>>(guardFn: GuardFn<TBody>)
223
254
  *
224
255
  * Define your own body shape and compose:
225
256
  * ```typescript
226
- * interface MyBody { userId: string; userTier: "free" | "pro" }
227
- * type MyContext = AgentContext<MyBody>;
257
+ * interface MyBody { userTier: "free" | "pro" }
258
+ * type MyContext = AgentToolContext<MyBody>;
228
259
  * ```
229
260
  */
230
- type AgentContext<TBody = Record<string, unknown>> = TBody & {
261
+ type AgentToolContext<TBody = Record<string, unknown>> = TBody & {
231
262
  log: (message: string, payload?: Record<string, unknown>) => void | Promise<void>;
232
263
  };
233
264
  //#endregion
234
- export { AIChatAgent, type AgentContext, type BuildLLMParamsConfig, type GuardFn, type Skill, buildLLMParams, guard };
265
+ export { AIChatAgent, type AgentToolContext, type BuildLLMParamsConfig, type Skill, buildLLMParams };