agents 0.3.6 → 0.3.7
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 +303 -314
- package/dist/ai-types.js +1 -1
- package/dist/{client-0lfEZpSQ.js → client-6CeOh-EV.js} +7 -3
- package/dist/client-6CeOh-EV.js.map +1 -0
- package/dist/{client-Cxno-5sH.d.ts → client-DGpERepg.d.ts} +8 -14
- package/dist/client.d.ts +80 -24
- package/dist/client.js +184 -2
- package/dist/client.js.map +1 -0
- package/dist/{do-oauth-client-provider-BH9zFtSy.d.ts → do-oauth-client-provider-BqnOQzjy.d.ts} +1 -1
- package/dist/{do-oauth-client-provider-BfPFgQU0.js → do-oauth-client-provider-DDg8QrEA.js} +1 -1
- package/dist/{do-oauth-client-provider-BfPFgQU0.js.map → do-oauth-client-provider-DDg8QrEA.js.map} +1 -1
- package/dist/email-8ljcpvwV.d.ts +157 -0
- package/dist/email-XHsSYsTO.js +223 -0
- package/dist/email-XHsSYsTO.js.map +1 -0
- package/dist/email.d.ts +30 -0
- package/dist/email.js +3 -0
- package/dist/{index-B7Ny-XfU.d.ts → index-N6791tVt.d.ts} +18 -3
- package/dist/index.d.ts +543 -87
- package/dist/index.js +6 -6
- package/dist/{internal_context-neg89p5n.d.ts → internal_context-CEu5ji80.d.ts} +8 -3
- package/dist/{internal_context-oN047Id3.js → internal_context-D9eKFth1.js} +1 -1
- package/dist/internal_context-D9eKFth1.js.map +1 -0
- package/dist/internal_context.d.ts +1 -1
- package/dist/internal_context.js +1 -1
- package/dist/mcp/client.d.ts +1 -1
- package/dist/mcp/client.js +2 -2
- package/dist/mcp/do-oauth-client-provider.d.ts +1 -1
- package/dist/mcp/do-oauth-client-provider.js +1 -1
- package/dist/mcp/index.d.ts +4 -2
- package/dist/mcp/index.js +35 -35
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/x402.d.ts +0 -1
- package/dist/mcp/x402.js.map +1 -1
- package/dist/{mcp-AK39tq6H.d.ts → mcp-BwPscEiF.d.ts} +1 -1
- package/dist/observability/index.d.ts +1 -1
- package/dist/observability/index.js +5 -5
- package/dist/react.d.ts +70 -26
- package/dist/react.js +83 -21
- package/dist/react.js.map +1 -1
- package/dist/schedule.d.ts +23 -2
- package/dist/schedule.js +23 -1
- package/dist/schedule.js.map +1 -1
- package/dist/serializable.d.ts +68 -3
- package/dist/src-BFP4sOQ4.js +2146 -0
- package/dist/src-BFP4sOQ4.js.map +1 -0
- package/dist/types-BITaDFf-.js +16 -0
- package/dist/{types-4b5tlB0u.js.map → types-BITaDFf-.js.map} +1 -1
- package/dist/{types-C5vR2Gzv.d.ts → types-DSSHBW6w.d.ts} +2 -1
- package/dist/types.d.ts +1 -1
- package/dist/types.js +1 -1
- package/dist/utils-B49TmLCI.js +16 -0
- package/dist/utils-B49TmLCI.js.map +1 -0
- package/dist/utils.d.ts +10 -0
- package/dist/utils.js +3 -0
- package/dist/workflow-types-Z_Oem1FJ.d.ts +260 -0
- package/dist/workflow-types.d.ts +48 -0
- package/dist/workflow-types.js +16 -0
- package/dist/workflow-types.js.map +1 -0
- package/dist/workflows.d.ts +163 -0
- package/dist/workflows.js +240 -0
- package/dist/workflows.js.map +1 -0
- package/package.json +22 -12
- package/dist/client-0lfEZpSQ.js.map +0 -1
- package/dist/client-CEO0P7vN.js +0 -117
- package/dist/client-CEO0P7vN.js.map +0 -1
- package/dist/internal_context-oN047Id3.js.map +0 -1
- package/dist/src-C_iKczoR.js +0 -1191
- package/dist/src-C_iKczoR.js.map +0 -1
- package/dist/types-4b5tlB0u.js +0 -15
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,23 @@
|
|
|
1
|
-
import { n as AgentEmail } from "./internal_context-
|
|
1
|
+
import { n as AgentEmail } from "./internal_context-CEu5ji80.js";
|
|
2
|
+
import {
|
|
3
|
+
l as createHeaderBasedEmailResolver,
|
|
4
|
+
r as EmailResolver
|
|
5
|
+
} from "./email-8ljcpvwV.js";
|
|
2
6
|
import {
|
|
3
7
|
h as TransportType,
|
|
4
8
|
t as MCPClientManager,
|
|
5
9
|
u as MCPConnectionState
|
|
6
|
-
} from "./client-
|
|
7
|
-
import {
|
|
8
|
-
|
|
10
|
+
} from "./client-DGpERepg.js";
|
|
11
|
+
import {
|
|
12
|
+
_ as WorkflowPage,
|
|
13
|
+
g as WorkflowInfo,
|
|
14
|
+
h as WorkflowEventPayload,
|
|
15
|
+
l as WorkflowCallback,
|
|
16
|
+
s as RunWorkflowOptions,
|
|
17
|
+
y as WorkflowQueryCriteria
|
|
18
|
+
} from "./workflow-types-Z_Oem1FJ.js";
|
|
19
|
+
import { t as Observability } from "./index-N6791tVt.js";
|
|
20
|
+
import { t as MessageType } from "./types-DSSHBW6w.js";
|
|
9
21
|
import {
|
|
10
22
|
Connection,
|
|
11
23
|
Connection as Connection$1,
|
|
@@ -23,7 +35,6 @@ import {
|
|
|
23
35
|
} from "@modelcontextprotocol/sdk/types.js";
|
|
24
36
|
|
|
25
37
|
//#region src/index.d.ts
|
|
26
|
-
|
|
27
38
|
/**
|
|
28
39
|
* RPC request message from client
|
|
29
40
|
*/
|
|
@@ -66,9 +77,7 @@ type RPCResponse = {
|
|
|
66
77
|
* Metadata for a callable method
|
|
67
78
|
*/
|
|
68
79
|
type CallableMetadata = {
|
|
69
|
-
/** Optional description of what the method does */
|
|
70
|
-
description?: string;
|
|
71
|
-
/** Whether the method supports streaming responses */
|
|
80
|
+
/** Optional description of what the method does */ description?: string; /** Whether the method supports streaming responses */
|
|
72
81
|
streaming?: boolean;
|
|
73
82
|
};
|
|
74
83
|
/**
|
|
@@ -94,7 +103,12 @@ declare function callable(
|
|
|
94
103
|
* @deprecated this has been renamed to callable, and unstable_callable will be removed in the next major version
|
|
95
104
|
* @param metadata Optional metadata about the callable method
|
|
96
105
|
*/
|
|
97
|
-
declare const unstable_callable: (
|
|
106
|
+
declare const unstable_callable: (
|
|
107
|
+
metadata?: CallableMetadata
|
|
108
|
+
) => <This, Args extends unknown[], Return>(
|
|
109
|
+
target: (this: This, ...args: Args) => Return,
|
|
110
|
+
context: ClassMethodDecoratorContext
|
|
111
|
+
) => (this: This, ...args: Args) => Return;
|
|
98
112
|
type QueueItem<T = string> = {
|
|
99
113
|
id: string;
|
|
100
114
|
payload: T;
|
|
@@ -106,35 +120,29 @@ type QueueItem<T = string> = {
|
|
|
106
120
|
* @template T Type of the payload data
|
|
107
121
|
*/
|
|
108
122
|
type Schedule<T = string> = {
|
|
109
|
-
/** Unique identifier for the schedule */
|
|
110
|
-
|
|
111
|
-
/** Name of the method to be called */
|
|
112
|
-
callback: string;
|
|
113
|
-
/** Data to be passed to the callback */
|
|
123
|
+
/** Unique identifier for the schedule */ id: string; /** Name of the method to be called */
|
|
124
|
+
callback: string; /** Data to be passed to the callback */
|
|
114
125
|
payload: T;
|
|
115
126
|
} & (
|
|
116
127
|
| {
|
|
117
|
-
/** Type of schedule for one-time execution at a specific time */
|
|
118
|
-
type: "scheduled";
|
|
119
|
-
/** Timestamp when the task should execute */
|
|
128
|
+
/** Type of schedule for one-time execution at a specific time */ type: "scheduled"; /** Timestamp when the task should execute */
|
|
120
129
|
time: number;
|
|
121
130
|
}
|
|
122
131
|
| {
|
|
123
|
-
/** Type of schedule for delayed execution */
|
|
124
|
-
|
|
125
|
-
/** Timestamp when the task should execute */
|
|
126
|
-
time: number;
|
|
127
|
-
/** Number of seconds to delay execution */
|
|
132
|
+
/** Type of schedule for delayed execution */ type: "delayed"; /** Timestamp when the task should execute */
|
|
133
|
+
time: number; /** Number of seconds to delay execution */
|
|
128
134
|
delayInSeconds: number;
|
|
129
135
|
}
|
|
130
136
|
| {
|
|
131
|
-
/** Type of schedule for recurring execution based on cron expression */
|
|
132
|
-
|
|
133
|
-
/** Timestamp for the next execution */
|
|
134
|
-
time: number;
|
|
135
|
-
/** Cron expression defining the schedule */
|
|
137
|
+
/** Type of schedule for recurring execution based on cron expression */ type: "cron"; /** Timestamp for the next execution */
|
|
138
|
+
time: number; /** Cron expression defining the schedule */
|
|
136
139
|
cron: string;
|
|
137
140
|
}
|
|
141
|
+
| {
|
|
142
|
+
/** Type of schedule for recurring execution at fixed intervals */ type: "interval"; /** Timestamp for the next execution */
|
|
143
|
+
time: number; /** Number of seconds between executions */
|
|
144
|
+
intervalSeconds: number;
|
|
145
|
+
}
|
|
138
146
|
);
|
|
139
147
|
/**
|
|
140
148
|
* MCP Server state update message from server -> Client
|
|
@@ -165,6 +173,40 @@ type MCPServer = {
|
|
|
165
173
|
instructions: string | null;
|
|
166
174
|
capabilities: ServerCapabilities | null;
|
|
167
175
|
};
|
|
176
|
+
/**
|
|
177
|
+
* Options for adding an MCP server
|
|
178
|
+
*/
|
|
179
|
+
type AddMcpServerOptions = {
|
|
180
|
+
/** OAuth callback host (auto-derived from request if omitted) */ callbackHost?: string; /** Agents routing prefix (default: "agents") */
|
|
181
|
+
agentsPrefix?: string; /** MCP client options */
|
|
182
|
+
client?: ConstructorParameters<typeof Client>[1]; /** Transport options */
|
|
183
|
+
transport?: {
|
|
184
|
+
/** Custom headers for authentication (e.g., bearer tokens, CF Access) */ headers?: HeadersInit; /** Transport type: "sse", "streamable-http", or "auto" (default) */
|
|
185
|
+
type?: TransportType;
|
|
186
|
+
};
|
|
187
|
+
};
|
|
188
|
+
/**
|
|
189
|
+
* Default options for Agent configuration.
|
|
190
|
+
* Child classes can override specific options without spreading.
|
|
191
|
+
*/
|
|
192
|
+
declare const DEFAULT_AGENT_STATIC_OPTIONS: {
|
|
193
|
+
/** Whether the Agent should hibernate when inactive */ hibernate: boolean; /** Whether to send identity (name, agent) to clients on connect */
|
|
194
|
+
sendIdentityOnConnect: boolean;
|
|
195
|
+
/**
|
|
196
|
+
* Timeout in seconds before a running interval schedule is considered "hung"
|
|
197
|
+
* and force-reset. Increase this if you have callbacks that legitimately
|
|
198
|
+
* take longer than 30 seconds.
|
|
199
|
+
*/
|
|
200
|
+
hungScheduleTimeoutSeconds: number;
|
|
201
|
+
};
|
|
202
|
+
type ResolvedAgentOptions = typeof DEFAULT_AGENT_STATIC_OPTIONS;
|
|
203
|
+
/**
|
|
204
|
+
* Configuration options for the Agent.
|
|
205
|
+
* Override in subclasses via `static options`.
|
|
206
|
+
* All fields are optional - defaults are applied at runtime.
|
|
207
|
+
* Note: `hibernate` defaults to `true` if not specified.
|
|
208
|
+
*/
|
|
209
|
+
type AgentStaticOptions = Partial<ResolvedAgentOptions>;
|
|
168
210
|
declare function getCurrentAgent<
|
|
169
211
|
T extends Agent<Cloudflare.Env> = Agent<Cloudflare.Env>
|
|
170
212
|
>(): {
|
|
@@ -173,6 +215,19 @@ declare function getCurrentAgent<
|
|
|
173
215
|
request: Request | undefined;
|
|
174
216
|
email: AgentEmail | undefined;
|
|
175
217
|
};
|
|
218
|
+
/**
|
|
219
|
+
* Extract string keys from Env where the value is a Workflow binding.
|
|
220
|
+
*/
|
|
221
|
+
type WorkflowBinding<E> = {
|
|
222
|
+
[K in keyof E & string]: E[K] extends Workflow ? K : never;
|
|
223
|
+
}[keyof E & string];
|
|
224
|
+
/**
|
|
225
|
+
* Type for workflow name parameter.
|
|
226
|
+
* When Env has typed Workflow bindings, provides autocomplete for those keys.
|
|
227
|
+
* Also accepts any string for dynamic use cases and compatibility.
|
|
228
|
+
* The `string & {}` trick preserves autocomplete while allowing any string.
|
|
229
|
+
*/
|
|
230
|
+
type WorkflowName<E> = WorkflowBinding<E> | (string & {});
|
|
176
231
|
/**
|
|
177
232
|
* Base class for creating Agent implementations
|
|
178
233
|
* @template Env Environment type containing bindings
|
|
@@ -198,12 +253,18 @@ declare class Agent<
|
|
|
198
253
|
*/
|
|
199
254
|
get state(): State;
|
|
200
255
|
/**
|
|
201
|
-
* Agent configuration options
|
|
256
|
+
* Agent configuration options.
|
|
257
|
+
* Override in subclasses - only specify what you want to change.
|
|
258
|
+
* @example
|
|
259
|
+
* class SecureAgent extends Agent {
|
|
260
|
+
* static options = { sendIdentityOnConnect: false };
|
|
261
|
+
* }
|
|
202
262
|
*/
|
|
203
|
-
static options:
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
263
|
+
static options: AgentStaticOptions;
|
|
264
|
+
/**
|
|
265
|
+
* Resolved options (merges defaults with subclass overrides)
|
|
266
|
+
*/
|
|
267
|
+
private get _resolvedOptions();
|
|
207
268
|
/**
|
|
208
269
|
* The observability implementation to use for the Agent
|
|
209
270
|
*/
|
|
@@ -220,12 +281,23 @@ declare class Agent<
|
|
|
220
281
|
...values: (string | number | boolean | null)[]
|
|
221
282
|
): T[];
|
|
222
283
|
constructor(ctx: AgentContext, env: Env);
|
|
284
|
+
/**
|
|
285
|
+
* Check for workflows referencing unknown bindings and warn with migration suggestion.
|
|
286
|
+
*/
|
|
287
|
+
private _checkOrphanedWorkflows;
|
|
223
288
|
private _setStateInternal;
|
|
224
289
|
/**
|
|
225
290
|
* Update the Agent's state
|
|
226
291
|
* @param state New state to set
|
|
227
292
|
*/
|
|
228
293
|
setState(state: State): void;
|
|
294
|
+
/**
|
|
295
|
+
* Called before the Agent's state is persisted and broadcast.
|
|
296
|
+
* Override to validate or reject an update by throwing an error.
|
|
297
|
+
*
|
|
298
|
+
* IMPORTANT: This hook must be synchronous.
|
|
299
|
+
*/
|
|
300
|
+
validateStateChange(nextState: State, source: Connection$1 | "server"): void;
|
|
229
301
|
/**
|
|
230
302
|
* Called when the Agent's state is updated
|
|
231
303
|
* @param state Updated state
|
|
@@ -245,6 +317,9 @@ declare class Agent<
|
|
|
245
317
|
* Reply to an email
|
|
246
318
|
* @param email The email to reply to
|
|
247
319
|
* @param options Options for the reply
|
|
320
|
+
* @param options.secret Secret for signing agent headers (enables secure reply routing).
|
|
321
|
+
* Required if the email was routed via createSecureReplyEmailResolver.
|
|
322
|
+
* Pass explicit `null` to opt-out of signing (not recommended for secure routing).
|
|
248
323
|
* @returns void
|
|
249
324
|
*/
|
|
250
325
|
replyToEmail(
|
|
@@ -255,6 +330,7 @@ declare class Agent<
|
|
|
255
330
|
body: string;
|
|
256
331
|
contentType?: string;
|
|
257
332
|
headers?: Record<string, string>;
|
|
333
|
+
secret?: string | null;
|
|
258
334
|
}
|
|
259
335
|
): Promise<void>;
|
|
260
336
|
private _tryCatch;
|
|
@@ -318,6 +394,19 @@ declare class Agent<
|
|
|
318
394
|
callback: keyof this,
|
|
319
395
|
payload?: T
|
|
320
396
|
): Promise<Schedule<T>>;
|
|
397
|
+
/**
|
|
398
|
+
* Schedule a task to run repeatedly at a fixed interval
|
|
399
|
+
* @template T Type of the payload data
|
|
400
|
+
* @param intervalSeconds Number of seconds between executions
|
|
401
|
+
* @param callback Name of the method to call
|
|
402
|
+
* @param payload Data to pass to the callback
|
|
403
|
+
* @returns Schedule object representing the scheduled task
|
|
404
|
+
*/
|
|
405
|
+
scheduleEvery<T = string>(
|
|
406
|
+
intervalSeconds: number,
|
|
407
|
+
callback: keyof this,
|
|
408
|
+
payload?: T
|
|
409
|
+
): Promise<Schedule<T>>;
|
|
321
410
|
/**
|
|
322
411
|
* Get a scheduled task by ID
|
|
323
412
|
* @template T Type of the payload data
|
|
@@ -333,7 +422,7 @@ declare class Agent<
|
|
|
333
422
|
*/
|
|
334
423
|
getSchedules<T = string>(criteria?: {
|
|
335
424
|
id?: string;
|
|
336
|
-
type?: "scheduled" | "delayed" | "cron";
|
|
425
|
+
type?: "scheduled" | "delayed" | "cron" | "interval";
|
|
337
426
|
timeRange?: {
|
|
338
427
|
start?: Date;
|
|
339
428
|
end?: Date;
|
|
@@ -359,26 +448,418 @@ declare class Agent<
|
|
|
359
448
|
* Destroy the Agent, removing all state and scheduled tasks
|
|
360
449
|
*/
|
|
361
450
|
destroy(): Promise<void>;
|
|
451
|
+
/**
|
|
452
|
+
* Check if a method is callable
|
|
453
|
+
* @param method The method name to check
|
|
454
|
+
* @returns True if the method is marked as callable
|
|
455
|
+
*/
|
|
456
|
+
private _isCallable;
|
|
362
457
|
/**
|
|
363
458
|
* Get all methods marked as callable on this Agent
|
|
364
459
|
* @returns A map of method names to their metadata
|
|
365
460
|
*/
|
|
366
|
-
|
|
461
|
+
getCallableMethods(): Map<string, CallableMetadata>;
|
|
462
|
+
/**
|
|
463
|
+
* Start a workflow and track it in this Agent's database.
|
|
464
|
+
* Automatically injects agent identity into the workflow params.
|
|
465
|
+
*
|
|
466
|
+
* @template P - Type of params to pass to the workflow
|
|
467
|
+
* @param workflowName - Name of the workflow binding in env (e.g., 'MY_WORKFLOW')
|
|
468
|
+
* @param params - Params to pass to the workflow
|
|
469
|
+
* @param options - Optional workflow options
|
|
470
|
+
* @returns The workflow instance ID
|
|
471
|
+
*
|
|
472
|
+
* @example
|
|
473
|
+
* ```typescript
|
|
474
|
+
* const workflowId = await this.runWorkflow(
|
|
475
|
+
* 'MY_WORKFLOW',
|
|
476
|
+
* { taskId: '123', data: 'process this' }
|
|
477
|
+
* );
|
|
478
|
+
* ```
|
|
479
|
+
*/
|
|
480
|
+
runWorkflow<P = unknown>(
|
|
481
|
+
workflowName: WorkflowName<Env>,
|
|
482
|
+
params: P,
|
|
483
|
+
options?: RunWorkflowOptions
|
|
484
|
+
): Promise<string>;
|
|
485
|
+
/**
|
|
486
|
+
* Send an event to a running workflow.
|
|
487
|
+
* The workflow can wait for this event using step.waitForEvent().
|
|
488
|
+
*
|
|
489
|
+
* @param workflowName - Name of the workflow binding in env (e.g., 'MY_WORKFLOW')
|
|
490
|
+
* @param workflowId - ID of the workflow instance
|
|
491
|
+
* @param event - Event to send
|
|
492
|
+
*
|
|
493
|
+
* @example
|
|
494
|
+
* ```typescript
|
|
495
|
+
* await this.sendWorkflowEvent(
|
|
496
|
+
* 'MY_WORKFLOW',
|
|
497
|
+
* workflowId,
|
|
498
|
+
* { type: 'approval', payload: { approved: true } }
|
|
499
|
+
* );
|
|
500
|
+
* ```
|
|
501
|
+
*/
|
|
502
|
+
sendWorkflowEvent(
|
|
503
|
+
workflowName: WorkflowName<Env>,
|
|
504
|
+
workflowId: string,
|
|
505
|
+
event: WorkflowEventPayload
|
|
506
|
+
): Promise<void>;
|
|
507
|
+
/**
|
|
508
|
+
* Approve a waiting workflow.
|
|
509
|
+
* Sends an approval event to the workflow that can be received by waitForApproval().
|
|
510
|
+
*
|
|
511
|
+
* @param workflowId - ID of the workflow to approve
|
|
512
|
+
* @param data - Optional approval data (reason, metadata)
|
|
513
|
+
*
|
|
514
|
+
* @example
|
|
515
|
+
* ```typescript
|
|
516
|
+
* await this.approveWorkflow(workflowId, {
|
|
517
|
+
* reason: 'Approved by admin',
|
|
518
|
+
* metadata: { approvedBy: userId }
|
|
519
|
+
* });
|
|
520
|
+
* ```
|
|
521
|
+
*/
|
|
522
|
+
approveWorkflow(
|
|
523
|
+
workflowId: string,
|
|
524
|
+
data?: {
|
|
525
|
+
reason?: string;
|
|
526
|
+
metadata?: Record<string, unknown>;
|
|
527
|
+
}
|
|
528
|
+
): Promise<void>;
|
|
529
|
+
/**
|
|
530
|
+
* Reject a waiting workflow.
|
|
531
|
+
* Sends a rejection event to the workflow that will cause waitForApproval() to throw.
|
|
532
|
+
*
|
|
533
|
+
* @param workflowId - ID of the workflow to reject
|
|
534
|
+
* @param data - Optional rejection data (reason)
|
|
535
|
+
*
|
|
536
|
+
* @example
|
|
537
|
+
* ```typescript
|
|
538
|
+
* await this.rejectWorkflow(workflowId, {
|
|
539
|
+
* reason: 'Request denied by admin'
|
|
540
|
+
* });
|
|
541
|
+
* ```
|
|
542
|
+
*/
|
|
543
|
+
rejectWorkflow(
|
|
544
|
+
workflowId: string,
|
|
545
|
+
data?: {
|
|
546
|
+
reason?: string;
|
|
547
|
+
}
|
|
548
|
+
): Promise<void>;
|
|
549
|
+
/**
|
|
550
|
+
* Terminate a running workflow.
|
|
551
|
+
* This immediately stops the workflow and sets its status to "terminated".
|
|
552
|
+
*
|
|
553
|
+
* @param workflowId - ID of the workflow to terminate (must be tracked via runWorkflow)
|
|
554
|
+
* @throws Error if workflow not found in tracking table
|
|
555
|
+
* @throws Error if workflow binding not found in environment
|
|
556
|
+
* @throws Error if workflow is already completed/errored/terminated (from Cloudflare)
|
|
557
|
+
*
|
|
558
|
+
* @note `terminate()` is not yet supported in local development (wrangler dev).
|
|
559
|
+
* It will throw an error locally but works when deployed to Cloudflare.
|
|
560
|
+
*
|
|
561
|
+
* @example
|
|
562
|
+
* ```typescript
|
|
563
|
+
* await this.terminateWorkflow(workflowId);
|
|
564
|
+
* ```
|
|
565
|
+
*/
|
|
566
|
+
terminateWorkflow(workflowId: string): Promise<void>;
|
|
567
|
+
/**
|
|
568
|
+
* Pause a running workflow.
|
|
569
|
+
* The workflow can be resumed later with resumeWorkflow().
|
|
570
|
+
*
|
|
571
|
+
* @param workflowId - ID of the workflow to pause (must be tracked via runWorkflow)
|
|
572
|
+
* @throws Error if workflow not found in tracking table
|
|
573
|
+
* @throws Error if workflow binding not found in environment
|
|
574
|
+
* @throws Error if workflow is not running (from Cloudflare)
|
|
575
|
+
*
|
|
576
|
+
* @note `pause()` is not yet supported in local development (wrangler dev).
|
|
577
|
+
* It will throw an error locally but works when deployed to Cloudflare.
|
|
578
|
+
*
|
|
579
|
+
* @example
|
|
580
|
+
* ```typescript
|
|
581
|
+
* await this.pauseWorkflow(workflowId);
|
|
582
|
+
* ```
|
|
583
|
+
*/
|
|
584
|
+
pauseWorkflow(workflowId: string): Promise<void>;
|
|
585
|
+
/**
|
|
586
|
+
* Resume a paused workflow.
|
|
587
|
+
*
|
|
588
|
+
* @param workflowId - ID of the workflow to resume (must be tracked via runWorkflow)
|
|
589
|
+
* @throws Error if workflow not found in tracking table
|
|
590
|
+
* @throws Error if workflow binding not found in environment
|
|
591
|
+
* @throws Error if workflow is not paused (from Cloudflare)
|
|
592
|
+
*
|
|
593
|
+
* @note `resume()` is not yet supported in local development (wrangler dev).
|
|
594
|
+
* It will throw an error locally but works when deployed to Cloudflare.
|
|
595
|
+
*
|
|
596
|
+
* @example
|
|
597
|
+
* ```typescript
|
|
598
|
+
* await this.resumeWorkflow(workflowId);
|
|
599
|
+
* ```
|
|
600
|
+
*/
|
|
601
|
+
resumeWorkflow(workflowId: string): Promise<void>;
|
|
602
|
+
/**
|
|
603
|
+
* Restart a workflow instance.
|
|
604
|
+
* This re-runs the workflow from the beginning with the same ID.
|
|
605
|
+
*
|
|
606
|
+
* @param workflowId - ID of the workflow to restart (must be tracked via runWorkflow)
|
|
607
|
+
* @param options - Optional settings
|
|
608
|
+
* @param options.resetTracking - If true (default), resets created_at and clears error fields.
|
|
609
|
+
* If false, preserves original timestamps.
|
|
610
|
+
* @throws Error if workflow not found in tracking table
|
|
611
|
+
* @throws Error if workflow binding not found in environment
|
|
612
|
+
*
|
|
613
|
+
* @note `restart()` is not yet supported in local development (wrangler dev).
|
|
614
|
+
* It will throw an error locally but works when deployed to Cloudflare.
|
|
615
|
+
*
|
|
616
|
+
* @example
|
|
617
|
+
* ```typescript
|
|
618
|
+
* // Reset tracking (default)
|
|
619
|
+
* await this.restartWorkflow(workflowId);
|
|
620
|
+
*
|
|
621
|
+
* // Preserve original timestamps
|
|
622
|
+
* await this.restartWorkflow(workflowId, { resetTracking: false });
|
|
623
|
+
* ```
|
|
624
|
+
*/
|
|
625
|
+
restartWorkflow(
|
|
626
|
+
workflowId: string,
|
|
627
|
+
options?: {
|
|
628
|
+
resetTracking?: boolean;
|
|
629
|
+
}
|
|
630
|
+
): Promise<void>;
|
|
631
|
+
/**
|
|
632
|
+
* Find a workflow binding by its name.
|
|
633
|
+
*/
|
|
634
|
+
private _findWorkflowBindingByName;
|
|
635
|
+
/**
|
|
636
|
+
* Get all workflow binding names from the environment.
|
|
637
|
+
*/
|
|
638
|
+
private _getWorkflowBindingNames;
|
|
639
|
+
/**
|
|
640
|
+
* Get the status of a workflow and update the tracking record.
|
|
641
|
+
*
|
|
642
|
+
* @param workflowName - Name of the workflow binding in env (e.g., 'MY_WORKFLOW')
|
|
643
|
+
* @param workflowId - ID of the workflow instance
|
|
644
|
+
* @returns The workflow status
|
|
645
|
+
*/
|
|
646
|
+
getWorkflowStatus(
|
|
647
|
+
workflowName: WorkflowName<Env>,
|
|
648
|
+
workflowId: string
|
|
649
|
+
): Promise<InstanceStatus>;
|
|
650
|
+
/**
|
|
651
|
+
* Get a tracked workflow by ID.
|
|
652
|
+
*
|
|
653
|
+
* @param workflowId - Workflow instance ID
|
|
654
|
+
* @returns Workflow info or undefined if not found
|
|
655
|
+
*/
|
|
656
|
+
getWorkflow(workflowId: string): WorkflowInfo | undefined;
|
|
657
|
+
/**
|
|
658
|
+
* Query tracked workflows with cursor-based pagination.
|
|
659
|
+
*
|
|
660
|
+
* @param criteria - Query criteria including optional cursor for pagination
|
|
661
|
+
* @returns WorkflowPage with workflows, total count, and next cursor
|
|
662
|
+
*
|
|
663
|
+
* @example
|
|
664
|
+
* ```typescript
|
|
665
|
+
* // First page
|
|
666
|
+
* const page1 = this.getWorkflows({ status: 'running', limit: 20 });
|
|
667
|
+
*
|
|
668
|
+
* // Next page
|
|
669
|
+
* if (page1.nextCursor) {
|
|
670
|
+
* const page2 = this.getWorkflows({
|
|
671
|
+
* status: 'running',
|
|
672
|
+
* limit: 20,
|
|
673
|
+
* cursor: page1.nextCursor
|
|
674
|
+
* });
|
|
675
|
+
* }
|
|
676
|
+
* ```
|
|
677
|
+
*/
|
|
678
|
+
getWorkflows(criteria?: WorkflowQueryCriteria): WorkflowPage;
|
|
679
|
+
/**
|
|
680
|
+
* Count workflows matching criteria (for pagination total).
|
|
681
|
+
*/
|
|
682
|
+
private _countWorkflows;
|
|
683
|
+
/**
|
|
684
|
+
* Encode a cursor from workflow info for pagination.
|
|
685
|
+
* Stores createdAt as Unix timestamp in seconds (matching DB storage).
|
|
686
|
+
*/
|
|
687
|
+
private _encodeCursor;
|
|
688
|
+
/**
|
|
689
|
+
* Decode a pagination cursor.
|
|
690
|
+
* Returns createdAt as Unix timestamp in seconds (matching DB storage).
|
|
691
|
+
*/
|
|
692
|
+
private _decodeCursor;
|
|
693
|
+
/**
|
|
694
|
+
* Delete a workflow tracking record.
|
|
695
|
+
*
|
|
696
|
+
* @param workflowId - ID of the workflow to delete
|
|
697
|
+
* @returns true if a record was deleted, false if not found
|
|
698
|
+
*/
|
|
699
|
+
deleteWorkflow(workflowId: string): boolean;
|
|
700
|
+
/**
|
|
701
|
+
* Delete workflow tracking records matching criteria.
|
|
702
|
+
* Useful for cleaning up old completed/errored workflows.
|
|
703
|
+
*
|
|
704
|
+
* @param criteria - Criteria for which workflows to delete
|
|
705
|
+
* @returns Number of records matching criteria (expected deleted count)
|
|
706
|
+
*
|
|
707
|
+
* @example
|
|
708
|
+
* ```typescript
|
|
709
|
+
* // Delete all completed workflows created more than 7 days ago
|
|
710
|
+
* const deleted = this.deleteWorkflows({
|
|
711
|
+
* status: 'complete',
|
|
712
|
+
* createdBefore: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000)
|
|
713
|
+
* });
|
|
714
|
+
*
|
|
715
|
+
* // Delete all errored and terminated workflows
|
|
716
|
+
* const deleted = this.deleteWorkflows({
|
|
717
|
+
* status: ['errored', 'terminated']
|
|
718
|
+
* });
|
|
719
|
+
* ```
|
|
720
|
+
*/
|
|
721
|
+
deleteWorkflows(
|
|
722
|
+
criteria?: Omit<WorkflowQueryCriteria, "limit" | "orderBy"> & {
|
|
723
|
+
createdBefore?: Date;
|
|
724
|
+
}
|
|
725
|
+
): number;
|
|
726
|
+
/**
|
|
727
|
+
* Migrate workflow tracking records from an old binding name to a new one.
|
|
728
|
+
* Use this after renaming a workflow binding in wrangler.toml.
|
|
729
|
+
*
|
|
730
|
+
* @param oldName - Previous workflow binding name
|
|
731
|
+
* @param newName - New workflow binding name
|
|
732
|
+
* @returns Number of records migrated
|
|
733
|
+
*
|
|
734
|
+
* @example
|
|
735
|
+
* ```typescript
|
|
736
|
+
* // After renaming OLD_WORKFLOW to NEW_WORKFLOW in wrangler.toml
|
|
737
|
+
* async onStart() {
|
|
738
|
+
* const migrated = this.migrateWorkflowBinding('OLD_WORKFLOW', 'NEW_WORKFLOW');
|
|
739
|
+
* }
|
|
740
|
+
* ```
|
|
741
|
+
*/
|
|
742
|
+
migrateWorkflowBinding(oldName: string, newName: string): number;
|
|
743
|
+
/**
|
|
744
|
+
* Update workflow tracking record from InstanceStatus
|
|
745
|
+
*/
|
|
746
|
+
private _updateWorkflowTracking;
|
|
747
|
+
/**
|
|
748
|
+
* Convert a database row to WorkflowInfo
|
|
749
|
+
*/
|
|
750
|
+
private _rowToWorkflowInfo;
|
|
751
|
+
/**
|
|
752
|
+
* Find the binding name for this Agent's namespace by matching class name.
|
|
753
|
+
* Returns undefined if no match found - use options.agentBinding as fallback.
|
|
754
|
+
*/
|
|
755
|
+
private _findAgentBindingName;
|
|
756
|
+
/**
|
|
757
|
+
* Handle a callback from a workflow.
|
|
758
|
+
* Called when the Agent receives a callback at /_workflow/callback.
|
|
759
|
+
* Override this to handle all callback types in one place.
|
|
760
|
+
*
|
|
761
|
+
* @param callback - The callback payload
|
|
762
|
+
*/
|
|
763
|
+
onWorkflowCallback(callback: WorkflowCallback): Promise<void>;
|
|
764
|
+
/**
|
|
765
|
+
* Called when a workflow reports progress.
|
|
766
|
+
* Override to handle progress updates.
|
|
767
|
+
*
|
|
768
|
+
* @param workflowName - Workflow binding name
|
|
769
|
+
* @param workflowId - ID of the workflow
|
|
770
|
+
* @param progress - Typed progress data (default: DefaultProgress)
|
|
771
|
+
*/
|
|
772
|
+
onWorkflowProgress(
|
|
773
|
+
_workflowName: string,
|
|
774
|
+
_workflowId: string,
|
|
775
|
+
_progress: unknown
|
|
776
|
+
): Promise<void>;
|
|
777
|
+
/**
|
|
778
|
+
* Called when a workflow completes successfully.
|
|
779
|
+
* Override to handle completion.
|
|
780
|
+
*
|
|
781
|
+
* @param workflowName - Workflow binding name
|
|
782
|
+
* @param workflowId - ID of the workflow
|
|
783
|
+
* @param result - Optional result data
|
|
784
|
+
*/
|
|
785
|
+
onWorkflowComplete(
|
|
786
|
+
_workflowName: string,
|
|
787
|
+
_workflowId: string,
|
|
788
|
+
_result?: unknown
|
|
789
|
+
): Promise<void>;
|
|
790
|
+
/**
|
|
791
|
+
* Called when a workflow encounters an error.
|
|
792
|
+
* Override to handle errors.
|
|
793
|
+
*
|
|
794
|
+
* @param workflowName - Workflow binding name
|
|
795
|
+
* @param workflowId - ID of the workflow
|
|
796
|
+
* @param error - Error message
|
|
797
|
+
*/
|
|
798
|
+
onWorkflowError(
|
|
799
|
+
_workflowName: string,
|
|
800
|
+
_workflowId: string,
|
|
801
|
+
_error: string
|
|
802
|
+
): Promise<void>;
|
|
803
|
+
/**
|
|
804
|
+
* Called when a workflow sends a custom event.
|
|
805
|
+
* Override to handle custom events.
|
|
806
|
+
*
|
|
807
|
+
* @param workflowName - Workflow binding name
|
|
808
|
+
* @param workflowId - ID of the workflow
|
|
809
|
+
* @param event - Custom event payload
|
|
810
|
+
*/
|
|
811
|
+
onWorkflowEvent(
|
|
812
|
+
_workflowName: string,
|
|
813
|
+
_workflowId: string,
|
|
814
|
+
_event: unknown
|
|
815
|
+
): Promise<void>;
|
|
816
|
+
/**
|
|
817
|
+
* Handle a workflow callback via RPC.
|
|
818
|
+
* @internal - Called by AgentWorkflow, do not call directly
|
|
819
|
+
*/
|
|
820
|
+
_workflow_handleCallback(callback: WorkflowCallback): Promise<void>;
|
|
821
|
+
/**
|
|
822
|
+
* Broadcast a message to all connected clients via RPC.
|
|
823
|
+
* @internal - Called by AgentWorkflow, do not call directly
|
|
824
|
+
*/
|
|
825
|
+
_workflow_broadcast(message: unknown): void;
|
|
826
|
+
/**
|
|
827
|
+
* Update agent state via RPC.
|
|
828
|
+
* @internal - Called by AgentWorkflow, do not call directly
|
|
829
|
+
*/
|
|
830
|
+
_workflow_updateState(
|
|
831
|
+
action: "set" | "merge" | "reset",
|
|
832
|
+
state?: unknown
|
|
833
|
+
): void;
|
|
367
834
|
/**
|
|
368
835
|
* Connect to a new MCP Server
|
|
369
836
|
*
|
|
837
|
+
* @example
|
|
838
|
+
* // Simple usage
|
|
839
|
+
* await this.addMcpServer("github", "https://mcp.github.com");
|
|
840
|
+
*
|
|
841
|
+
* @example
|
|
842
|
+
* // With options (preferred for custom headers, transport, etc.)
|
|
843
|
+
* await this.addMcpServer("github", "https://mcp.github.com", {
|
|
844
|
+
* transport: { headers: { "Authorization": "Bearer ..." } }
|
|
845
|
+
* });
|
|
846
|
+
*
|
|
847
|
+
* @example
|
|
848
|
+
* // Legacy 5-parameter signature (still supported)
|
|
849
|
+
* await this.addMcpServer("github", url, callbackHost, agentsPrefix, options);
|
|
850
|
+
*
|
|
370
851
|
* @param serverName Name of the MCP server
|
|
371
|
-
* @param url MCP Server
|
|
372
|
-
* @param
|
|
373
|
-
* @param agentsPrefix agents routing prefix if not using `agents`
|
|
374
|
-
* @param options MCP client and transport options
|
|
852
|
+
* @param url MCP Server URL
|
|
853
|
+
* @param callbackHostOrOptions Options object, or callback host string (legacy)
|
|
854
|
+
* @param agentsPrefix agents routing prefix if not using `agents` (legacy)
|
|
855
|
+
* @param options MCP client and transport options (legacy)
|
|
375
856
|
* @returns Server id and state - either "authenticating" with authUrl, or "ready"
|
|
376
857
|
* @throws If connection or discovery fails
|
|
377
858
|
*/
|
|
378
859
|
addMcpServer(
|
|
379
860
|
serverName: string,
|
|
380
861
|
url: string,
|
|
381
|
-
|
|
862
|
+
callbackHostOrOptions?: string | AddMcpServerOptions,
|
|
382
863
|
agentsPrefix?: string,
|
|
383
864
|
options?: {
|
|
384
865
|
client?: ConstructorParameters<typeof Client>[1];
|
|
@@ -456,38 +937,14 @@ declare function routeAgentRequest<Env>(
|
|
|
456
937
|
env: Env,
|
|
457
938
|
options?: AgentOptions<Env>
|
|
458
939
|
): Promise<Response | null>;
|
|
459
|
-
type EmailResolver<Env> = (
|
|
460
|
-
email: ForwardableEmailMessage,
|
|
461
|
-
env: Env
|
|
462
|
-
) => Promise<{
|
|
463
|
-
agentName: string;
|
|
464
|
-
agentId: string;
|
|
465
|
-
} | null>;
|
|
466
|
-
/**
|
|
467
|
-
* Create a resolver that uses the message-id header to determine the agent to route the email to
|
|
468
|
-
* @returns A function that resolves the agent to route the email to
|
|
469
|
-
*/
|
|
470
|
-
declare function createHeaderBasedEmailResolver<Env>(): EmailResolver<Env>;
|
|
471
|
-
/**
|
|
472
|
-
* Create a resolver that uses the email address to determine the agent to route the email to
|
|
473
|
-
* @param defaultAgentName The default agent name to use if the email address does not contain a sub-address
|
|
474
|
-
* @returns A function that resolves the agent to route the email to
|
|
475
|
-
*/
|
|
476
|
-
declare function createAddressBasedEmailResolver<Env>(
|
|
477
|
-
defaultAgentName: string
|
|
478
|
-
): EmailResolver<Env>;
|
|
479
|
-
/**
|
|
480
|
-
* Create a resolver that uses the agentName and agentId to determine the agent to route the email to
|
|
481
|
-
* @param agentName The name of the agent to route the email to
|
|
482
|
-
* @param agentId The id of the agent to route the email to
|
|
483
|
-
* @returns A function that resolves the agent to route the email to
|
|
484
|
-
*/
|
|
485
|
-
declare function createCatchAllEmailResolver<Env>(
|
|
486
|
-
agentName: string,
|
|
487
|
-
agentId: string
|
|
488
|
-
): EmailResolver<Env>;
|
|
489
940
|
type EmailRoutingOptions<Env> = AgentOptions<Env> & {
|
|
490
941
|
resolver: EmailResolver<Env>;
|
|
942
|
+
/**
|
|
943
|
+
* Callback invoked when no routing information is found for an email.
|
|
944
|
+
* Use this to reject the email or perform custom handling.
|
|
945
|
+
* If not provided, a warning is logged and the email is dropped.
|
|
946
|
+
*/
|
|
947
|
+
onNoRoute?: (email: ForwardableEmailMessage) => void | Promise<void>;
|
|
491
948
|
};
|
|
492
949
|
/**
|
|
493
950
|
* Route an email to the appropriate Agent
|
|
@@ -501,17 +958,6 @@ declare function routeAgentEmail<Env extends Cloudflare.Env = Cloudflare.Env>(
|
|
|
501
958
|
env: Env,
|
|
502
959
|
options: EmailRoutingOptions<Env>
|
|
503
960
|
): Promise<void>;
|
|
504
|
-
type EmailSendOptions = {
|
|
505
|
-
to: string;
|
|
506
|
-
subject: string;
|
|
507
|
-
body: string;
|
|
508
|
-
contentType?: string;
|
|
509
|
-
headers?: Record<string, string>;
|
|
510
|
-
includeRoutingHeaders?: boolean;
|
|
511
|
-
agentName?: string;
|
|
512
|
-
agentId?: string;
|
|
513
|
-
domain?: string;
|
|
514
|
-
};
|
|
515
961
|
/**
|
|
516
962
|
* Get or create an Agent by name
|
|
517
963
|
* @template Env Environment type containing bindings
|
|
@@ -542,30 +988,42 @@ declare class StreamingResponse {
|
|
|
542
988
|
private _id;
|
|
543
989
|
private _closed;
|
|
544
990
|
constructor(connection: Connection$1, id: string);
|
|
991
|
+
/**
|
|
992
|
+
* Whether the stream has been closed (via end() or error())
|
|
993
|
+
*/
|
|
994
|
+
get isClosed(): boolean;
|
|
545
995
|
/**
|
|
546
996
|
* Send a chunk of data to the client
|
|
547
997
|
* @param chunk The data to send
|
|
998
|
+
* @returns false if stream is already closed (no-op), true if sent
|
|
548
999
|
*/
|
|
549
|
-
send(chunk: unknown):
|
|
1000
|
+
send(chunk: unknown): boolean;
|
|
550
1001
|
/**
|
|
551
1002
|
* End the stream and send the final chunk (if any)
|
|
552
1003
|
* @param finalChunk Optional final chunk of data to send
|
|
1004
|
+
* @returns false if stream is already closed (no-op), true if sent
|
|
1005
|
+
*/
|
|
1006
|
+
end(finalChunk?: unknown): boolean;
|
|
1007
|
+
/**
|
|
1008
|
+
* Send an error to the client and close the stream
|
|
1009
|
+
* @param message Error message to send
|
|
1010
|
+
* @returns false if stream is already closed (no-op), true if sent
|
|
553
1011
|
*/
|
|
554
|
-
|
|
1012
|
+
error(message: string): boolean;
|
|
555
1013
|
}
|
|
556
1014
|
//#endregion
|
|
557
1015
|
export {
|
|
1016
|
+
AddMcpServerOptions,
|
|
558
1017
|
Agent,
|
|
559
1018
|
AgentContext,
|
|
560
|
-
type AgentEmail,
|
|
561
1019
|
AgentNamespace,
|
|
562
1020
|
AgentOptions,
|
|
1021
|
+
AgentStaticOptions,
|
|
563
1022
|
CallableMetadata,
|
|
564
1023
|
type Connection,
|
|
565
1024
|
type ConnectionContext,
|
|
566
|
-
|
|
1025
|
+
DEFAULT_AGENT_STATIC_OPTIONS,
|
|
567
1026
|
EmailRoutingOptions,
|
|
568
|
-
EmailSendOptions,
|
|
569
1027
|
MCPServer,
|
|
570
1028
|
MCPServerMessage,
|
|
571
1029
|
MCPServersState,
|
|
@@ -579,8 +1037,6 @@ export {
|
|
|
579
1037
|
type TransportType,
|
|
580
1038
|
type WSMessage,
|
|
581
1039
|
callable,
|
|
582
|
-
createAddressBasedEmailResolver,
|
|
583
|
-
createCatchAllEmailResolver,
|
|
584
1040
|
createHeaderBasedEmailResolver,
|
|
585
1041
|
getAgentByName,
|
|
586
1042
|
getCurrentAgent,
|