agents 0.0.0-dd6a9e3 → 0.0.0-df716f2

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,217 +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
- onError(connection: Connection, error: unknown): void | Promise<void>;
153
- onError(error: unknown): void | Promise<void>;
154
- /**
155
- * Render content (not implemented in base class)
156
- */
157
- render(): void;
158
- /**
159
- * Schedule a task to be executed in the future
160
- * @template T Type of the payload data
161
- * @param when When to execute the task (Date, seconds delay, or cron expression)
162
- * @param callback Name of the method to call
163
- * @param payload Data to pass to the callback
164
- * @returns Schedule object representing the scheduled task
165
- */
166
- schedule<T = string>(
167
- when: Date | string | number,
168
- callback: keyof this,
169
- payload?: T
170
- ): Promise<Schedule<T>>;
171
- /**
172
- * Get a scheduled task by ID
173
- * @template T Type of the payload data
174
- * @param id ID of the scheduled task
175
- * @returns The Schedule object or undefined if not found
176
- */
177
- getSchedule<T = string>(id: string): Promise<Schedule<T> | undefined>;
178
- /**
179
- * Get scheduled tasks matching the given criteria
180
- * @template T Type of the payload data
181
- * @param criteria Criteria to filter schedules
182
- * @returns Array of matching Schedule objects
183
- */
184
- getSchedules<T = string>(criteria?: {
185
- description?: string;
186
- id?: string;
187
- type?: "scheduled" | "delayed" | "cron";
188
- timeRange?: {
189
- start?: Date;
190
- 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;
191
117
  };
192
- }): Schedule<T>[];
193
- /**
194
- * Cancel a scheduled task
195
- * @param id ID of the task to cancel
196
- * @returns true if the task was cancelled, false otherwise
197
- */
198
- cancelSchedule(id: string): Promise<boolean>;
199
- /**
200
- * Method called when an alarm fires
201
- * Executes any scheduled tasks that are due
202
- */
203
- alarm(): Promise<void>;
204
- /**
205
- * Destroy the Agent, removing all state and scheduled tasks
206
- */
207
- 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>;
208
194
  }
209
195
  /**
210
196
  * Namespace for creating Agent instances
211
197
  * @template Agentic Type of the Agent class
212
198
  */
213
- type AgentNamespace<Agentic extends Agent<unknown>> =
214
- DurableObjectNamespace<Agentic>;
199
+ type AgentNamespace<Agentic extends Agent<unknown>> = DurableObjectNamespace<Agentic>;
215
200
  /**
216
201
  * Agent's durable context
217
202
  */
@@ -220,10 +205,10 @@ type AgentContext = DurableObjectState;
220
205
  * Configuration options for Agent routing
221
206
  */
222
207
  type AgentOptions<Env> = PartyServerOptions<Env> & {
223
- /**
224
- * Whether to enable CORS for the Agent
225
- */
226
- cors?: boolean | HeadersInit | undefined;
208
+ /**
209
+ * Whether to enable CORS for the Agent
210
+ */
211
+ cors?: boolean | HeadersInit | undefined;
227
212
  };
228
213
  /**
229
214
  * Route a request to the appropriate Agent
@@ -232,22 +217,14 @@ type AgentOptions<Env> = PartyServerOptions<Env> & {
232
217
  * @param options Routing options
233
218
  * @returns Response from the Agent or undefined if no route matched
234
219
  */
235
- declare function routeAgentRequest<Env>(
236
- request: Request,
237
- env: Env,
238
- options?: AgentOptions<Env>
239
- ): Promise<Response | null>;
220
+ declare function routeAgentRequest<Env>(request: Request, env: Env, options?: AgentOptions<Env>): Promise<Response | null>;
240
221
  /**
241
222
  * Route an email to the appropriate Agent
242
223
  * @param email Email message to route
243
224
  * @param env Environment containing Agent bindings
244
225
  * @param options Routing options
245
226
  */
246
- declare function routeAgentEmail<Env>(
247
- email: ForwardableEmailMessage,
248
- env: Env,
249
- options?: AgentOptions<Env>
250
- ): Promise<void>;
227
+ declare function routeAgentEmail<Env>(email: ForwardableEmailMessage, env: Env, options?: AgentOptions<Env>): Promise<void>;
251
228
  /**
252
229
  * Get or create an Agent by name
253
230
  * @template Env Environment type containing bindings
@@ -257,46 +234,26 @@ declare function routeAgentEmail<Env>(
257
234
  * @param options Options for Agent creation
258
235
  * @returns Promise resolving to an Agent instance stub
259
236
  */
260
- declare function getAgentByName<Env, T extends Agent<Env>>(
261
- namespace: AgentNamespace<T>,
262
- name: string,
263
- options?: {
237
+ declare function getAgentByName<Env, T extends Agent<Env>>(namespace: AgentNamespace<T>, name: string, options?: {
264
238
  jurisdiction?: DurableObjectJurisdiction;
265
239
  locationHint?: DurableObjectLocationHint;
266
- }
267
- ): Promise<DurableObjectStub<T>>;
240
+ }): Promise<DurableObjectStub<T>>;
268
241
  /**
269
242
  * A wrapper for streaming responses in callable methods
270
243
  */
271
244
  declare class StreamingResponse {
272
- #private;
273
- constructor(connection: Connection, id: string);
274
- /**
275
- * Send a chunk of data to the client
276
- * @param chunk The data to send
277
- */
278
- send(chunk: unknown): void;
279
- /**
280
- * End the stream and send the final chunk (if any)
281
- * @param finalChunk Optional final chunk of data to send
282
- */
283
- 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;
284
257
  }
285
258
 
286
- export {
287
- Agent,
288
- type AgentContext,
289
- type AgentNamespace,
290
- type AgentOptions,
291
- type CallableMetadata,
292
- type RPCRequest,
293
- type RPCResponse,
294
- type Schedule,
295
- type StateUpdateMessage,
296
- StreamingResponse,
297
- WorkflowEntrypoint,
298
- getAgentByName,
299
- routeAgentEmail,
300
- routeAgentRequest,
301
- unstable_callable,
302
- };
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 };