agents 0.0.0-bcae0ba → 0.0.0-c3e8618

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