agents 0.0.47 → 0.0.48

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/dist/index.d.ts CHANGED
@@ -1,108 +1,96 @@
1
- import { Server, Connection, PartyServerOptions } from "partyserver";
2
- export { Connection, ConnectionContext, WSMessage } from "partyserver";
3
- import { AsyncLocalStorage } from "node:async_hooks";
4
- import { WorkflowEntrypoint as WorkflowEntrypoint$1 } from "cloudflare:workers";
1
+ import { Server, Connection, PartyServerOptions } from 'partyserver';
2
+ export { Connection, ConnectionContext, WSMessage } from 'partyserver';
3
+ import { AsyncLocalStorage } from 'node:async_hooks';
4
+ import { WorkflowEntrypoint as WorkflowEntrypoint$1 } from 'cloudflare:workers';
5
5
 
6
6
  /**
7
7
  * RPC request message from client
8
8
  */
9
9
  type RPCRequest = {
10
- type: "rpc";
11
- id: string;
12
- method: string;
13
- args: unknown[];
10
+ type: "rpc";
11
+ id: string;
12
+ method: string;
13
+ args: unknown[];
14
14
  };
15
15
  /**
16
16
  * State update message from client
17
17
  */
18
18
  type StateUpdateMessage = {
19
- type: "cf_agent_state";
20
- state: unknown;
19
+ type: "cf_agent_state";
20
+ state: unknown;
21
21
  };
22
22
  /**
23
23
  * RPC response message to client
24
24
  */
25
25
  type RPCResponse = {
26
- type: "rpc";
27
- id: string;
28
- } & (
29
- | {
30
- success: true;
31
- result: unknown;
32
- done?: false;
33
- }
34
- | {
35
- success: true;
36
- result: unknown;
37
- done: true;
38
- }
39
- | {
40
- success: false;
41
- error: string;
42
- }
43
- );
26
+ type: "rpc";
27
+ id: string;
28
+ } & ({
29
+ success: true;
30
+ result: unknown;
31
+ done?: false;
32
+ } | {
33
+ success: true;
34
+ result: unknown;
35
+ done: true;
36
+ } | {
37
+ success: false;
38
+ error: string;
39
+ });
44
40
  /**
45
41
  * Metadata for a callable method
46
42
  */
47
43
  type CallableMetadata = {
48
- /** Optional description of what the method does */
49
- description?: string;
50
- /** Whether the method supports streaming responses */
51
- streaming?: boolean;
44
+ /** Optional description of what the method does */
45
+ description?: string;
46
+ /** Whether the method supports streaming responses */
47
+ streaming?: boolean;
52
48
  };
53
49
  /**
54
50
  * Decorator that marks a method as callable by clients
55
51
  * @param metadata Optional metadata about the callable method
56
52
  */
57
- declare function unstable_callable(
58
- metadata?: CallableMetadata
59
- ): <This, Args extends unknown[], Return>(
60
- target: (this: This, ...args: Args) => Return,
61
- context: ClassMethodDecoratorContext
62
- ) => (this: This, ...args: Args) => Return;
53
+ declare function unstable_callable(metadata?: CallableMetadata): <This, Args extends unknown[], Return>(target: (this: This, ...args: Args) => Return, context: ClassMethodDecoratorContext) => (this: This, ...args: Args) => Return;
63
54
  /**
64
55
  * A class for creating workflow entry points that can be used with Cloudflare Workers
65
56
  */
66
- declare class WorkflowEntrypoint extends WorkflowEntrypoint$1 {}
57
+ declare class WorkflowEntrypoint extends WorkflowEntrypoint$1 {
58
+ }
67
59
  /**
68
60
  * Represents a scheduled task within an Agent
69
61
  * @template T Type of the payload data
70
62
  */
71
63
  type Schedule<T = string> = {
72
- /** Unique identifier for the schedule */
73
- id: string;
74
- /** Name of the method to be called */
75
- callback: string;
76
- /** Data to be passed to the callback */
77
- payload: T;
78
- } & (
79
- | {
80
- /** Type of schedule for one-time execution at a specific time */
81
- type: "scheduled";
82
- /** Timestamp when the task should execute */
83
- time: number;
84
- }
85
- | {
86
- /** Type of schedule for delayed execution */
87
- type: "delayed";
88
- /** Timestamp when the task should execute */
89
- time: number;
90
- /** Number of seconds to delay execution */
91
- delayInSeconds: number;
92
- }
93
- | {
94
- /** Type of schedule for recurring execution based on cron expression */
95
- type: "cron";
96
- /** Timestamp for the next execution */
97
- time: number;
98
- /** Cron expression defining the schedule */
99
- cron: string;
100
- }
101
- );
64
+ /** Unique identifier for the schedule */
65
+ id: string;
66
+ /** Name of the method to be called */
67
+ callback: string;
68
+ /** Data to be passed to the callback */
69
+ payload: T;
70
+ } & ({
71
+ /** Type of schedule for one-time execution at a specific time */
72
+ type: "scheduled";
73
+ /** Timestamp when the task should execute */
74
+ time: number;
75
+ } | {
76
+ /** Type of schedule for delayed execution */
77
+ type: "delayed";
78
+ /** Timestamp when the task should execute */
79
+ time: number;
80
+ /** Number of seconds to delay execution */
81
+ delayInSeconds: number;
82
+ } | {
83
+ /** Type of schedule for recurring execution based on cron expression */
84
+ type: "cron";
85
+ /** Timestamp for the next execution */
86
+ time: number;
87
+ /** Cron expression defining the schedule */
88
+ cron: string;
89
+ });
102
90
  declare const unstable_context: AsyncLocalStorage<{
103
- agent: Agent<unknown>;
104
- connection: Connection | undefined;
105
- request: Request | undefined;
91
+ agent: Agent<unknown>;
92
+ connection: Connection | undefined;
93
+ request: Request | undefined;
106
94
  }>;
107
95
  /**
108
96
  * Base class for creating Agent implementations
@@ -110,113 +98,105 @@ declare const unstable_context: AsyncLocalStorage<{
110
98
  * @template State State type to store within the Agent
111
99
  */
112
100
  declare class Agent<Env, State = unknown> extends Server<Env> {
113
- #private;
114
- /**
115
- * Initial state for the Agent
116
- * Override to provide default state values
117
- */
118
- initialState: State;
119
- /**
120
- * Current state of the Agent
121
- */
122
- get state(): State;
123
- /**
124
- * Agent configuration options
125
- */
126
- static options: {
127
- /** Whether the Agent should hibernate when inactive */
128
- hibernate: boolean;
129
- };
130
- /**
131
- * Execute SQL queries against the Agent's database
132
- * @template T Type of the returned rows
133
- * @param strings SQL query template strings
134
- * @param values Values to be inserted into the query
135
- * @returns Array of query results
136
- */
137
- sql<T = Record<string, string | number | boolean | null>>(
138
- strings: TemplateStringsArray,
139
- ...values: (string | number | boolean | null)[]
140
- ): T[];
141
- constructor(ctx: AgentContext, env: Env);
142
- /**
143
- * Update the Agent's state
144
- * @param state New state to set
145
- */
146
- setState(state: State): void;
147
- /**
148
- * Called when the Agent's state is updated
149
- * @param state Updated state
150
- * @param source Source of the state update ("server" or a client connection)
151
- */
152
- onStateUpdate(state: State | undefined, source: Connection | "server"): void;
153
- /**
154
- * Called when the Agent receives an email
155
- * @param email Email message to process
156
- */
157
- onEmail(email: ForwardableEmailMessage): Promise<void>;
158
- onError(connection: Connection, error: unknown): void | Promise<void>;
159
- onError(error: unknown): void | Promise<void>;
160
- /**
161
- * Render content (not implemented in base class)
162
- */
163
- render(): void;
164
- /**
165
- * Schedule a task to be executed in the future
166
- * @template T Type of the payload data
167
- * @param when When to execute the task (Date, seconds delay, or cron expression)
168
- * @param callback Name of the method to call
169
- * @param payload Data to pass to the callback
170
- * @returns Schedule object representing the scheduled task
171
- */
172
- schedule<T = string>(
173
- when: Date | string | number,
174
- callback: keyof this,
175
- payload?: T
176
- ): Promise<Schedule<T>>;
177
- /**
178
- * Get a scheduled task by ID
179
- * @template T Type of the payload data
180
- * @param id ID of the scheduled task
181
- * @returns The Schedule object or undefined if not found
182
- */
183
- getSchedule<T = string>(id: string): Promise<Schedule<T> | undefined>;
184
- /**
185
- * Get scheduled tasks matching the given criteria
186
- * @template T Type of the payload data
187
- * @param criteria Criteria to filter schedules
188
- * @returns Array of matching Schedule objects
189
- */
190
- getSchedules<T = string>(criteria?: {
191
- id?: string;
192
- type?: "scheduled" | "delayed" | "cron";
193
- timeRange?: {
194
- start?: Date;
195
- end?: Date;
101
+ #private;
102
+ /**
103
+ * Initial state for the Agent
104
+ * Override to provide default state values
105
+ */
106
+ initialState: State;
107
+ /**
108
+ * Current state of the Agent
109
+ */
110
+ get state(): State;
111
+ /**
112
+ * Agent configuration options
113
+ */
114
+ static options: {
115
+ /** Whether the Agent should hibernate when inactive */
116
+ hibernate: boolean;
196
117
  };
197
- }): Schedule<T>[];
198
- /**
199
- * Cancel a scheduled task
200
- * @param id ID of the task to cancel
201
- * @returns true if the task was cancelled, false otherwise
202
- */
203
- cancelSchedule(id: string): Promise<boolean>;
204
- /**
205
- * Method called when an alarm fires
206
- * Executes any scheduled tasks that are due
207
- */
208
- alarm(): Promise<void>;
209
- /**
210
- * Destroy the Agent, removing all state and scheduled tasks
211
- */
212
- destroy(): Promise<void>;
118
+ /**
119
+ * Execute SQL queries against the Agent's database
120
+ * @template T Type of the returned rows
121
+ * @param strings SQL query template strings
122
+ * @param values Values to be inserted into the query
123
+ * @returns Array of query results
124
+ */
125
+ sql<T = Record<string, string | number | boolean | null>>(strings: TemplateStringsArray, ...values: (string | number | boolean | null)[]): T[];
126
+ constructor(ctx: AgentContext, env: Env);
127
+ /**
128
+ * Update the Agent's state
129
+ * @param state New state to set
130
+ */
131
+ setState(state: State): void;
132
+ /**
133
+ * Called when the Agent's state is updated
134
+ * @param state Updated state
135
+ * @param source Source of the state update ("server" or a client connection)
136
+ */
137
+ onStateUpdate(state: State | undefined, source: Connection | "server"): void;
138
+ /**
139
+ * Called when the Agent receives an email
140
+ * @param email Email message to process
141
+ */
142
+ onEmail(email: ForwardableEmailMessage): Promise<void>;
143
+ onError(connection: Connection, error: unknown): void | Promise<void>;
144
+ onError(error: unknown): void | Promise<void>;
145
+ /**
146
+ * Render content (not implemented in base class)
147
+ */
148
+ render(): void;
149
+ /**
150
+ * Schedule a task to be executed in the future
151
+ * @template T Type of the payload data
152
+ * @param when When to execute the task (Date, seconds delay, or cron expression)
153
+ * @param callback Name of the method to call
154
+ * @param payload Data to pass to the callback
155
+ * @returns Schedule object representing the scheduled task
156
+ */
157
+ schedule<T = string>(when: Date | string | number, callback: keyof this, payload?: T): Promise<Schedule<T>>;
158
+ /**
159
+ * Get a scheduled task by ID
160
+ * @template T Type of the payload data
161
+ * @param id ID of the scheduled task
162
+ * @returns The Schedule object or undefined if not found
163
+ */
164
+ getSchedule<T = string>(id: string): Promise<Schedule<T> | undefined>;
165
+ /**
166
+ * Get scheduled tasks matching the given criteria
167
+ * @template T Type of the payload data
168
+ * @param criteria Criteria to filter schedules
169
+ * @returns Array of matching Schedule objects
170
+ */
171
+ getSchedules<T = string>(criteria?: {
172
+ id?: string;
173
+ type?: "scheduled" | "delayed" | "cron";
174
+ timeRange?: {
175
+ start?: Date;
176
+ end?: Date;
177
+ };
178
+ }): Schedule<T>[];
179
+ /**
180
+ * Cancel a scheduled task
181
+ * @param id ID of the task to cancel
182
+ * @returns true if the task was cancelled, false otherwise
183
+ */
184
+ cancelSchedule(id: string): Promise<boolean>;
185
+ /**
186
+ * Method called when an alarm fires
187
+ * Executes any scheduled tasks that are due
188
+ */
189
+ alarm(): Promise<void>;
190
+ /**
191
+ * Destroy the Agent, removing all state and scheduled tasks
192
+ */
193
+ destroy(): Promise<void>;
213
194
  }
214
195
  /**
215
196
  * Namespace for creating Agent instances
216
197
  * @template Agentic Type of the Agent class
217
198
  */
218
- type AgentNamespace<Agentic extends Agent<unknown>> =
219
- DurableObjectNamespace<Agentic>;
199
+ type AgentNamespace<Agentic extends Agent<unknown>> = DurableObjectNamespace<Agentic>;
220
200
  /**
221
201
  * Agent's durable context
222
202
  */
@@ -225,10 +205,10 @@ type AgentContext = DurableObjectState;
225
205
  * Configuration options for Agent routing
226
206
  */
227
207
  type AgentOptions<Env> = PartyServerOptions<Env> & {
228
- /**
229
- * Whether to enable CORS for the Agent
230
- */
231
- cors?: boolean | HeadersInit | undefined;
208
+ /**
209
+ * Whether to enable CORS for the Agent
210
+ */
211
+ cors?: boolean | HeadersInit | undefined;
232
212
  };
233
213
  /**
234
214
  * Route a request to the appropriate Agent
@@ -237,22 +217,14 @@ type AgentOptions<Env> = PartyServerOptions<Env> & {
237
217
  * @param options Routing options
238
218
  * @returns Response from the Agent or undefined if no route matched
239
219
  */
240
- declare function routeAgentRequest<Env>(
241
- request: Request,
242
- env: Env,
243
- options?: AgentOptions<Env>
244
- ): Promise<Response | null>;
220
+ declare function routeAgentRequest<Env>(request: Request, env: Env, options?: AgentOptions<Env>): Promise<Response | null>;
245
221
  /**
246
222
  * Route an email to the appropriate Agent
247
223
  * @param email Email message to route
248
224
  * @param env Environment containing Agent bindings
249
225
  * @param options Routing options
250
226
  */
251
- declare function routeAgentEmail<Env>(
252
- email: ForwardableEmailMessage,
253
- env: Env,
254
- options?: AgentOptions<Env>
255
- ): Promise<void>;
227
+ declare function routeAgentEmail<Env>(email: ForwardableEmailMessage, env: Env, options?: AgentOptions<Env>): Promise<void>;
256
228
  /**
257
229
  * Get or create an Agent by name
258
230
  * @template Env Environment type containing bindings
@@ -262,47 +234,26 @@ declare function routeAgentEmail<Env>(
262
234
  * @param options Options for Agent creation
263
235
  * @returns Promise resolving to an Agent instance stub
264
236
  */
265
- declare function getAgentByName<Env, T extends Agent<Env>>(
266
- namespace: AgentNamespace<T>,
267
- name: string,
268
- options?: {
237
+ declare function getAgentByName<Env, T extends Agent<Env>>(namespace: AgentNamespace<T>, name: string, options?: {
269
238
  jurisdiction?: DurableObjectJurisdiction;
270
239
  locationHint?: DurableObjectLocationHint;
271
- }
272
- ): Promise<DurableObjectStub<T>>;
240
+ }): Promise<DurableObjectStub<T>>;
273
241
  /**
274
242
  * A wrapper for streaming responses in callable methods
275
243
  */
276
244
  declare class StreamingResponse {
277
- #private;
278
- constructor(connection: Connection, id: string);
279
- /**
280
- * Send a chunk of data to the client
281
- * @param chunk The data to send
282
- */
283
- send(chunk: unknown): void;
284
- /**
285
- * End the stream and send the final chunk (if any)
286
- * @param finalChunk Optional final chunk of data to send
287
- */
288
- end(finalChunk?: unknown): void;
245
+ #private;
246
+ constructor(connection: Connection, id: string);
247
+ /**
248
+ * Send a chunk of data to the client
249
+ * @param chunk The data to send
250
+ */
251
+ send(chunk: unknown): void;
252
+ /**
253
+ * End the stream and send the final chunk (if any)
254
+ * @param finalChunk Optional final chunk of data to send
255
+ */
256
+ end(finalChunk?: unknown): void;
289
257
  }
290
258
 
291
- export {
292
- Agent,
293
- type AgentContext,
294
- type AgentNamespace,
295
- type AgentOptions,
296
- type CallableMetadata,
297
- type RPCRequest,
298
- type RPCResponse,
299
- type Schedule,
300
- type StateUpdateMessage,
301
- StreamingResponse,
302
- WorkflowEntrypoint,
303
- getAgentByName,
304
- routeAgentEmail,
305
- routeAgentRequest,
306
- unstable_callable,
307
- unstable_context,
308
- };
259
+ export { Agent, type AgentContext, type AgentNamespace, type AgentOptions, type CallableMetadata, type RPCRequest, type RPCResponse, type Schedule, type StateUpdateMessage, StreamingResponse, WorkflowEntrypoint, getAgentByName, routeAgentEmail, routeAgentRequest, unstable_callable, unstable_context };