agents 0.3.6 → 0.3.8
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-BA8NJB6l.js} +17 -4
- package/dist/client-BA8NJB6l.js.map +1 -0
- package/dist/{client-Cxno-5sH.d.ts → client-DGpERepg.d.ts} +8 -14
- package/dist/client.d.ts +93 -24
- package/dist/client.js +202 -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-CqnVUbg1.js +2146 -0
- package/dist/src-CqnVUbg1.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
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import {
|
|
2
|
+
WorkflowEvent,
|
|
3
|
+
WorkflowSleepDuration,
|
|
4
|
+
WorkflowStep
|
|
5
|
+
} from "cloudflare:workers";
|
|
6
|
+
|
|
7
|
+
//#region src/workflow-types.d.ts
|
|
8
|
+
/**
|
|
9
|
+
* Type alias for WorkflowEvent in AgentWorkflow context.
|
|
10
|
+
* Identical to WorkflowEvent - provided for naming consistency with AgentWorkflowStep.
|
|
11
|
+
*/
|
|
12
|
+
type AgentWorkflowEvent<Params = unknown> = WorkflowEvent<Params>;
|
|
13
|
+
/**
|
|
14
|
+
* Extended WorkflowStep with durable Agent communication methods.
|
|
15
|
+
* All added methods on this interface are durable - they're idempotent and won't
|
|
16
|
+
* repeat on workflow retry.
|
|
17
|
+
*/
|
|
18
|
+
interface AgentWorkflowStep extends WorkflowStep {
|
|
19
|
+
/**
|
|
20
|
+
* Report successful completion to the Agent (durable).
|
|
21
|
+
* Triggers onWorkflowComplete() on the Agent.
|
|
22
|
+
* @param result - Optional result data
|
|
23
|
+
*/
|
|
24
|
+
reportComplete<T = unknown>(result?: T): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* Report an error to the Agent (durable).
|
|
27
|
+
* Triggers onWorkflowError() on the Agent.
|
|
28
|
+
* @param error - Error or error message
|
|
29
|
+
*/
|
|
30
|
+
reportError(error: Error | string): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Send a custom event to the Agent (durable).
|
|
33
|
+
* Triggers onWorkflowEvent() on the Agent.
|
|
34
|
+
* @param event - Custom event payload
|
|
35
|
+
*/
|
|
36
|
+
sendEvent<T = unknown>(event: T): Promise<void>;
|
|
37
|
+
/**
|
|
38
|
+
* Update the Agent's state entirely (durable).
|
|
39
|
+
* This will replace the Agent's state and broadcast to all connected clients.
|
|
40
|
+
* @param state - New state to set
|
|
41
|
+
*/
|
|
42
|
+
updateAgentState(state: unknown): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Merge partial state into the Agent's existing state (durable).
|
|
45
|
+
* Performs a shallow merge and broadcasts to all connected clients.
|
|
46
|
+
* @param partialState - Partial state to merge
|
|
47
|
+
*/
|
|
48
|
+
mergeAgentState(partialState: Record<string, unknown>): Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* Reset the Agent's state to its initialState (durable).
|
|
51
|
+
* Broadcasts the reset state to all connected clients.
|
|
52
|
+
*/
|
|
53
|
+
resetAgentState(): Promise<void>;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Internal parameters injected by runWorkflow() to identify the originating Agent
|
|
57
|
+
*/
|
|
58
|
+
type AgentWorkflowInternalParams = {
|
|
59
|
+
/** Name/ID of the Agent that started this workflow */ __agentName: string; /** Environment binding name for the Agent's namespace */
|
|
60
|
+
__agentBinding: string; /** Workflow binding name (for callbacks) */
|
|
61
|
+
__workflowName: string;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Combined workflow params: user params + internal agent params
|
|
65
|
+
*/
|
|
66
|
+
type AgentWorkflowParams<T = unknown> = T & AgentWorkflowInternalParams;
|
|
67
|
+
/**
|
|
68
|
+
* Workflow callback types for Agent-Workflow communication
|
|
69
|
+
*/
|
|
70
|
+
type WorkflowCallbackType = "progress" | "complete" | "error" | "event";
|
|
71
|
+
/**
|
|
72
|
+
* Base callback structure sent from Workflow to Agent
|
|
73
|
+
*/
|
|
74
|
+
type WorkflowCallbackBase = {
|
|
75
|
+
/** Workflow binding name */ workflowName: string; /** ID of the workflow instance */
|
|
76
|
+
workflowId: string; /** Type of callback */
|
|
77
|
+
type: WorkflowCallbackType; /** Timestamp when callback was sent */
|
|
78
|
+
timestamp: number;
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* Default progress type - covers common use cases.
|
|
82
|
+
* Developers can define their own progress type for domain-specific needs.
|
|
83
|
+
*/
|
|
84
|
+
type DefaultProgress = {
|
|
85
|
+
/** Current step name */ step?: string; /** Step/overall status */
|
|
86
|
+
status?:
|
|
87
|
+
| "pending"
|
|
88
|
+
| "running"
|
|
89
|
+
| "complete"
|
|
90
|
+
| "error"; /** Human-readable message */
|
|
91
|
+
message?: string; /** Progress percentage (0-1) */
|
|
92
|
+
percent?: number; /** Allow additional custom fields */
|
|
93
|
+
[key: string]: unknown;
|
|
94
|
+
};
|
|
95
|
+
/**
|
|
96
|
+
* Progress callback - reports workflow progress with typed payload
|
|
97
|
+
*/
|
|
98
|
+
type WorkflowProgressCallback<P = DefaultProgress> = WorkflowCallbackBase & {
|
|
99
|
+
type: "progress"; /** Typed progress data */
|
|
100
|
+
progress: P;
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* Complete callback - workflow finished successfully
|
|
104
|
+
*/
|
|
105
|
+
type WorkflowCompleteCallback = WorkflowCallbackBase & {
|
|
106
|
+
type: "complete"; /** Result of the workflow */
|
|
107
|
+
result?: unknown;
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* Error callback - workflow encountered an error
|
|
111
|
+
*/
|
|
112
|
+
type WorkflowErrorCallback = WorkflowCallbackBase & {
|
|
113
|
+
type: "error"; /** Error message */
|
|
114
|
+
error: string;
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
* Event callback - custom event from workflow
|
|
118
|
+
*/
|
|
119
|
+
type WorkflowEventCallback = WorkflowCallbackBase & {
|
|
120
|
+
type: "event"; /** Custom event payload */
|
|
121
|
+
event: unknown;
|
|
122
|
+
};
|
|
123
|
+
/**
|
|
124
|
+
* Union of all callback types
|
|
125
|
+
*/
|
|
126
|
+
type WorkflowCallback<P = DefaultProgress> =
|
|
127
|
+
| WorkflowProgressCallback<P>
|
|
128
|
+
| WorkflowCompleteCallback
|
|
129
|
+
| WorkflowErrorCallback
|
|
130
|
+
| WorkflowEventCallback;
|
|
131
|
+
/**
|
|
132
|
+
* Workflow status values - derived from Cloudflare's InstanceStatus
|
|
133
|
+
*/
|
|
134
|
+
type WorkflowStatus = InstanceStatus["status"];
|
|
135
|
+
/**
|
|
136
|
+
* Row structure for cf_agents_workflows tracking table
|
|
137
|
+
*/
|
|
138
|
+
type WorkflowTrackingRow = {
|
|
139
|
+
/** Internal row ID (UUID) */ id: string; /** Cloudflare Workflow instance ID */
|
|
140
|
+
workflow_id: string; /** Workflow binding name */
|
|
141
|
+
workflow_name: string; /** Current workflow status */
|
|
142
|
+
status: WorkflowStatus; /** JSON-serialized metadata for querying */
|
|
143
|
+
metadata: string | null; /** Error name if workflow failed */
|
|
144
|
+
error_name: string | null; /** Error message if workflow failed */
|
|
145
|
+
error_message: string | null; /** Unix timestamp when workflow was created */
|
|
146
|
+
created_at: number; /** Unix timestamp when workflow was last updated */
|
|
147
|
+
updated_at: number; /** Unix timestamp when workflow completed (null if not complete) */
|
|
148
|
+
completed_at: number | null;
|
|
149
|
+
};
|
|
150
|
+
/**
|
|
151
|
+
* Options for runWorkflow()
|
|
152
|
+
*/
|
|
153
|
+
type RunWorkflowOptions = {
|
|
154
|
+
/** Custom workflow instance ID (auto-generated if not provided) */ id?: string; /** Optional metadata for querying (stored as JSON) */
|
|
155
|
+
metadata?: Record<
|
|
156
|
+
string,
|
|
157
|
+
unknown
|
|
158
|
+
>; /** Agent binding name (auto-detected from class name if not provided) */
|
|
159
|
+
agentBinding?: string;
|
|
160
|
+
};
|
|
161
|
+
/**
|
|
162
|
+
* Event payload for sendWorkflowEvent()
|
|
163
|
+
*/
|
|
164
|
+
type WorkflowEventPayload = {
|
|
165
|
+
/** Event type name */ type: string; /** Event payload data */
|
|
166
|
+
payload: unknown;
|
|
167
|
+
};
|
|
168
|
+
/**
|
|
169
|
+
* Parsed workflow tracking info returned by getWorkflow()
|
|
170
|
+
*/
|
|
171
|
+
type WorkflowInfo = {
|
|
172
|
+
/** Internal row ID */ id: string; /** Cloudflare Workflow instance ID */
|
|
173
|
+
workflowId: string; /** Workflow binding name */
|
|
174
|
+
workflowName: string; /** Current workflow status */
|
|
175
|
+
status: WorkflowStatus; /** Metadata (parsed from JSON) */
|
|
176
|
+
metadata: Record<string, unknown> | null; /** Error info if workflow failed */
|
|
177
|
+
error: {
|
|
178
|
+
name: string;
|
|
179
|
+
message: string;
|
|
180
|
+
} | null; /** When workflow was created */
|
|
181
|
+
createdAt: Date; /** When workflow was last updated */
|
|
182
|
+
updatedAt: Date; /** When workflow completed (null if not complete) */
|
|
183
|
+
completedAt: Date | null;
|
|
184
|
+
};
|
|
185
|
+
/**
|
|
186
|
+
* Criteria for querying tracked workflows
|
|
187
|
+
*/
|
|
188
|
+
type WorkflowQueryCriteria = {
|
|
189
|
+
/** Filter by status */ status?:
|
|
190
|
+
| WorkflowStatus
|
|
191
|
+
| WorkflowStatus[]; /** Filter by workflow binding name */
|
|
192
|
+
workflowName?: string; /** Filter by metadata key-value pairs (exact match) */
|
|
193
|
+
metadata?: Record<
|
|
194
|
+
string,
|
|
195
|
+
string | number | boolean
|
|
196
|
+
>; /** Limit number of results (default 50, max 100) */
|
|
197
|
+
limit?: number; /** Order by created_at */
|
|
198
|
+
orderBy?:
|
|
199
|
+
| "asc"
|
|
200
|
+
| "desc"; /** Cursor for pagination (from previous WorkflowPage.nextCursor) */
|
|
201
|
+
cursor?: string;
|
|
202
|
+
};
|
|
203
|
+
/**
|
|
204
|
+
* Paginated result from getWorkflows()
|
|
205
|
+
*/
|
|
206
|
+
type WorkflowPage = {
|
|
207
|
+
/** Workflows for this page */ workflows: WorkflowInfo[]; /** Total count of workflows matching the criteria (ignoring pagination) */
|
|
208
|
+
total: number; /** Cursor for next page, or null if no more pages */
|
|
209
|
+
nextCursor: string | null;
|
|
210
|
+
};
|
|
211
|
+
/**
|
|
212
|
+
* Standard approval event payload used by approveWorkflow/rejectWorkflow
|
|
213
|
+
*/
|
|
214
|
+
type ApprovalEventPayload = {
|
|
215
|
+
/** Whether the workflow was approved */ approved: boolean; /** Optional reason for approval/rejection */
|
|
216
|
+
reason?: string; /** Optional additional metadata */
|
|
217
|
+
metadata?: Record<string, unknown>;
|
|
218
|
+
};
|
|
219
|
+
/**
|
|
220
|
+
* Options for waitForApproval()
|
|
221
|
+
*/
|
|
222
|
+
type WaitForApprovalOptions = {
|
|
223
|
+
/** Step name for waitForEvent (default: "wait-for-approval") */ stepName?: string; /** Timeout duration (e.g., "7 days") */
|
|
224
|
+
timeout?: WorkflowSleepDuration; /** Event type to wait for (default: "approval") */
|
|
225
|
+
eventType?: string;
|
|
226
|
+
};
|
|
227
|
+
/**
|
|
228
|
+
* Error thrown when a workflow is rejected via rejectWorkflow()
|
|
229
|
+
*/
|
|
230
|
+
declare class WorkflowRejectedError extends Error {
|
|
231
|
+
readonly reason?: string | undefined;
|
|
232
|
+
readonly workflowId?: string | undefined;
|
|
233
|
+
constructor(reason?: string | undefined, workflowId?: string | undefined);
|
|
234
|
+
}
|
|
235
|
+
//#endregion
|
|
236
|
+
export {
|
|
237
|
+
WorkflowTrackingRow as S,
|
|
238
|
+
WorkflowPage as _,
|
|
239
|
+
ApprovalEventPayload as a,
|
|
240
|
+
WorkflowRejectedError as b,
|
|
241
|
+
WaitForApprovalOptions as c,
|
|
242
|
+
WorkflowCallbackType as d,
|
|
243
|
+
WorkflowCompleteCallback as f,
|
|
244
|
+
WorkflowInfo as g,
|
|
245
|
+
WorkflowEventPayload as h,
|
|
246
|
+
AgentWorkflowStep as i,
|
|
247
|
+
WorkflowCallback as l,
|
|
248
|
+
WorkflowEventCallback as m,
|
|
249
|
+
AgentWorkflowInternalParams as n,
|
|
250
|
+
DefaultProgress as o,
|
|
251
|
+
WorkflowErrorCallback as p,
|
|
252
|
+
AgentWorkflowParams as r,
|
|
253
|
+
RunWorkflowOptions as s,
|
|
254
|
+
AgentWorkflowEvent as t,
|
|
255
|
+
WorkflowCallbackBase as u,
|
|
256
|
+
WorkflowProgressCallback as v,
|
|
257
|
+
WorkflowStatus as x,
|
|
258
|
+
WorkflowQueryCriteria as y
|
|
259
|
+
};
|
|
260
|
+
//# sourceMappingURL=workflow-types-Z_Oem1FJ.d.ts.map
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import {
|
|
2
|
+
S as WorkflowTrackingRow,
|
|
3
|
+
_ as WorkflowPage,
|
|
4
|
+
a as ApprovalEventPayload,
|
|
5
|
+
b as WorkflowRejectedError,
|
|
6
|
+
c as WaitForApprovalOptions,
|
|
7
|
+
d as WorkflowCallbackType,
|
|
8
|
+
f as WorkflowCompleteCallback,
|
|
9
|
+
g as WorkflowInfo,
|
|
10
|
+
h as WorkflowEventPayload,
|
|
11
|
+
i as AgentWorkflowStep,
|
|
12
|
+
l as WorkflowCallback,
|
|
13
|
+
m as WorkflowEventCallback,
|
|
14
|
+
n as AgentWorkflowInternalParams,
|
|
15
|
+
o as DefaultProgress,
|
|
16
|
+
p as WorkflowErrorCallback,
|
|
17
|
+
r as AgentWorkflowParams,
|
|
18
|
+
s as RunWorkflowOptions,
|
|
19
|
+
t as AgentWorkflowEvent,
|
|
20
|
+
u as WorkflowCallbackBase,
|
|
21
|
+
v as WorkflowProgressCallback,
|
|
22
|
+
x as WorkflowStatus,
|
|
23
|
+
y as WorkflowQueryCriteria
|
|
24
|
+
} from "./workflow-types-Z_Oem1FJ.js";
|
|
25
|
+
export {
|
|
26
|
+
AgentWorkflowEvent,
|
|
27
|
+
AgentWorkflowInternalParams,
|
|
28
|
+
AgentWorkflowParams,
|
|
29
|
+
AgentWorkflowStep,
|
|
30
|
+
ApprovalEventPayload,
|
|
31
|
+
DefaultProgress,
|
|
32
|
+
RunWorkflowOptions,
|
|
33
|
+
WaitForApprovalOptions,
|
|
34
|
+
WorkflowCallback,
|
|
35
|
+
WorkflowCallbackBase,
|
|
36
|
+
WorkflowCallbackType,
|
|
37
|
+
WorkflowCompleteCallback,
|
|
38
|
+
WorkflowErrorCallback,
|
|
39
|
+
WorkflowEventCallback,
|
|
40
|
+
WorkflowEventPayload,
|
|
41
|
+
WorkflowInfo,
|
|
42
|
+
WorkflowPage,
|
|
43
|
+
WorkflowProgressCallback,
|
|
44
|
+
WorkflowQueryCriteria,
|
|
45
|
+
WorkflowRejectedError,
|
|
46
|
+
WorkflowStatus,
|
|
47
|
+
WorkflowTrackingRow
|
|
48
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//#region src/workflow-types.ts
|
|
2
|
+
/**
|
|
3
|
+
* Error thrown when a workflow is rejected via rejectWorkflow()
|
|
4
|
+
*/
|
|
5
|
+
var WorkflowRejectedError = class extends Error {
|
|
6
|
+
constructor(reason, workflowId) {
|
|
7
|
+
super(reason ? `Workflow rejected: ${reason}` : "Workflow rejected");
|
|
8
|
+
this.reason = reason;
|
|
9
|
+
this.workflowId = workflowId;
|
|
10
|
+
this.name = "WorkflowRejectedError";
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
//#endregion
|
|
15
|
+
export { WorkflowRejectedError };
|
|
16
|
+
//# sourceMappingURL=workflow-types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"workflow-types.js","names":[],"sources":["../src/workflow-types.ts"],"sourcesContent":["/**\n * Workflow integration types for Agents\n *\n * These types provide seamless integration between Cloudflare Agents\n * and Cloudflare Workflows for durable, multi-step background processing.\n *\n * Note: This file is kept separate from workflows.ts to avoid circular dependencies.\n * Both index.ts (Agent class) and workflows.ts (AgentWorkflow class) import from here.\n */\n\nimport type {\n WorkflowEvent,\n WorkflowStep,\n WorkflowSleepDuration\n} from \"cloudflare:workers\";\n\n/**\n * Type alias for WorkflowEvent in AgentWorkflow context.\n * Identical to WorkflowEvent - provided for naming consistency with AgentWorkflowStep.\n */\nexport type AgentWorkflowEvent<Params = unknown> = WorkflowEvent<Params>;\n\n/**\n * Extended WorkflowStep with durable Agent communication methods.\n * All added methods on this interface are durable - they're idempotent and won't\n * repeat on workflow retry.\n */\nexport interface AgentWorkflowStep extends WorkflowStep {\n /**\n * Report successful completion to the Agent (durable).\n * Triggers onWorkflowComplete() on the Agent.\n * @param result - Optional result data\n */\n reportComplete<T = unknown>(result?: T): Promise<void>;\n\n /**\n * Report an error to the Agent (durable).\n * Triggers onWorkflowError() on the Agent.\n * @param error - Error or error message\n */\n reportError(error: Error | string): Promise<void>;\n\n /**\n * Send a custom event to the Agent (durable).\n * Triggers onWorkflowEvent() on the Agent.\n * @param event - Custom event payload\n */\n sendEvent<T = unknown>(event: T): Promise<void>;\n\n /**\n * Update the Agent's state entirely (durable).\n * This will replace the Agent's state and broadcast to all connected clients.\n * @param state - New state to set\n */\n updateAgentState(state: unknown): Promise<void>;\n\n /**\n * Merge partial state into the Agent's existing state (durable).\n * Performs a shallow merge and broadcasts to all connected clients.\n * @param partialState - Partial state to merge\n */\n mergeAgentState(partialState: Record<string, unknown>): Promise<void>;\n\n /**\n * Reset the Agent's state to its initialState (durable).\n * Broadcasts the reset state to all connected clients.\n */\n resetAgentState(): Promise<void>;\n}\n\n/**\n * Internal parameters injected by runWorkflow() to identify the originating Agent\n */\nexport type AgentWorkflowInternalParams = {\n /** Name/ID of the Agent that started this workflow */\n __agentName: string;\n /** Environment binding name for the Agent's namespace */\n __agentBinding: string;\n /** Workflow binding name (for callbacks) */\n __workflowName: string;\n};\n\n/**\n * Combined workflow params: user params + internal agent params\n */\nexport type AgentWorkflowParams<T = unknown> = T & AgentWorkflowInternalParams;\n\n/**\n * Workflow callback types for Agent-Workflow communication\n */\nexport type WorkflowCallbackType = \"progress\" | \"complete\" | \"error\" | \"event\";\n\n/**\n * Base callback structure sent from Workflow to Agent\n */\nexport type WorkflowCallbackBase = {\n /** Workflow binding name */\n workflowName: string;\n /** ID of the workflow instance */\n workflowId: string;\n /** Type of callback */\n type: WorkflowCallbackType;\n /** Timestamp when callback was sent */\n timestamp: number;\n};\n\n/**\n * Default progress type - covers common use cases.\n * Developers can define their own progress type for domain-specific needs.\n */\nexport type DefaultProgress = {\n /** Current step name */\n step?: string;\n /** Step/overall status */\n status?: \"pending\" | \"running\" | \"complete\" | \"error\";\n /** Human-readable message */\n message?: string;\n /** Progress percentage (0-1) */\n percent?: number;\n /** Allow additional custom fields */\n [key: string]: unknown;\n};\n\n/**\n * Progress callback - reports workflow progress with typed payload\n */\nexport type WorkflowProgressCallback<P = DefaultProgress> =\n WorkflowCallbackBase & {\n type: \"progress\";\n /** Typed progress data */\n progress: P;\n };\n\n/**\n * Complete callback - workflow finished successfully\n */\nexport type WorkflowCompleteCallback = WorkflowCallbackBase & {\n type: \"complete\";\n /** Result of the workflow */\n result?: unknown;\n};\n\n/**\n * Error callback - workflow encountered an error\n */\nexport type WorkflowErrorCallback = WorkflowCallbackBase & {\n type: \"error\";\n /** Error message */\n error: string;\n};\n\n/**\n * Event callback - custom event from workflow\n */\nexport type WorkflowEventCallback = WorkflowCallbackBase & {\n type: \"event\";\n /** Custom event payload */\n event: unknown;\n};\n\n/**\n * Union of all callback types\n */\nexport type WorkflowCallback<P = DefaultProgress> =\n | WorkflowProgressCallback<P>\n | WorkflowCompleteCallback\n | WorkflowErrorCallback\n | WorkflowEventCallback;\n\n/**\n * Workflow status values - derived from Cloudflare's InstanceStatus\n */\nexport type WorkflowStatus = InstanceStatus[\"status\"];\n\n/**\n * Row structure for cf_agents_workflows tracking table\n */\nexport type WorkflowTrackingRow = {\n /** Internal row ID (UUID) */\n id: string;\n /** Cloudflare Workflow instance ID */\n workflow_id: string;\n /** Workflow binding name */\n workflow_name: string;\n /** Current workflow status */\n status: WorkflowStatus;\n /** JSON-serialized metadata for querying */\n metadata: string | null;\n /** Error name if workflow failed */\n error_name: string | null;\n /** Error message if workflow failed */\n error_message: string | null;\n /** Unix timestamp when workflow was created */\n created_at: number;\n /** Unix timestamp when workflow was last updated */\n updated_at: number;\n /** Unix timestamp when workflow completed (null if not complete) */\n completed_at: number | null;\n};\n\n/**\n * Options for runWorkflow()\n */\nexport type RunWorkflowOptions = {\n /** Custom workflow instance ID (auto-generated if not provided) */\n id?: string;\n /** Optional metadata for querying (stored as JSON) */\n metadata?: Record<string, unknown>;\n /** Agent binding name (auto-detected from class name if not provided) */\n agentBinding?: string;\n};\n\n/**\n * Event payload for sendWorkflowEvent()\n */\nexport type WorkflowEventPayload = {\n /** Event type name */\n type: string;\n /** Event payload data */\n payload: unknown;\n};\n\n/**\n * Parsed workflow tracking info returned by getWorkflow()\n */\nexport type WorkflowInfo = {\n /** Internal row ID */\n id: string;\n /** Cloudflare Workflow instance ID */\n workflowId: string;\n /** Workflow binding name */\n workflowName: string;\n /** Current workflow status */\n status: WorkflowStatus;\n /** Metadata (parsed from JSON) */\n metadata: Record<string, unknown> | null;\n /** Error info if workflow failed */\n error: { name: string; message: string } | null;\n /** When workflow was created */\n createdAt: Date;\n /** When workflow was last updated */\n updatedAt: Date;\n /** When workflow completed (null if not complete) */\n completedAt: Date | null;\n};\n\n/**\n * Criteria for querying tracked workflows\n */\nexport type WorkflowQueryCriteria = {\n /** Filter by status */\n status?: WorkflowStatus | WorkflowStatus[];\n /** Filter by workflow binding name */\n workflowName?: string;\n /** Filter by metadata key-value pairs (exact match) */\n metadata?: Record<string, string | number | boolean>;\n /** Limit number of results (default 50, max 100) */\n limit?: number;\n /** Order by created_at */\n orderBy?: \"asc\" | \"desc\";\n /** Cursor for pagination (from previous WorkflowPage.nextCursor) */\n cursor?: string;\n};\n\n/**\n * Paginated result from getWorkflows()\n */\nexport type WorkflowPage = {\n /** Workflows for this page */\n workflows: WorkflowInfo[];\n /** Total count of workflows matching the criteria (ignoring pagination) */\n total: number;\n /** Cursor for next page, or null if no more pages */\n nextCursor: string | null;\n};\n\n/**\n * Standard approval event payload used by approveWorkflow/rejectWorkflow\n */\nexport type ApprovalEventPayload = {\n /** Whether the workflow was approved */\n approved: boolean;\n /** Optional reason for approval/rejection */\n reason?: string;\n /** Optional additional metadata */\n metadata?: Record<string, unknown>;\n};\n\n/**\n * Options for waitForApproval()\n */\nexport type WaitForApprovalOptions = {\n /** Step name for waitForEvent (default: \"wait-for-approval\") */\n stepName?: string;\n /** Timeout duration (e.g., \"7 days\") */\n timeout?: WorkflowSleepDuration;\n /** Event type to wait for (default: \"approval\") */\n eventType?: string;\n};\n\n/**\n * Error thrown when a workflow is rejected via rejectWorkflow()\n */\nexport class WorkflowRejectedError extends Error {\n constructor(\n public readonly reason?: string,\n public readonly workflowId?: string\n ) {\n super(reason ? `Workflow rejected: ${reason}` : \"Workflow rejected\");\n this.name = \"WorkflowRejectedError\";\n }\n}\n"],"mappings":";;;;AA+SA,IAAa,wBAAb,cAA2C,MAAM;CAC/C,YACE,AAAgB,QAChB,AAAgB,YAChB;AACA,QAAM,SAAS,sBAAsB,WAAW,oBAAoB;EAHpD;EACA;AAGhB,OAAK,OAAO"}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import "./internal_context-CEu5ji80.js";
|
|
2
|
+
import "./email-8ljcpvwV.js";
|
|
3
|
+
import "./client-DGpERepg.js";
|
|
4
|
+
import {
|
|
5
|
+
S as WorkflowTrackingRow,
|
|
6
|
+
_ as WorkflowPage,
|
|
7
|
+
a as ApprovalEventPayload,
|
|
8
|
+
b as WorkflowRejectedError,
|
|
9
|
+
c as WaitForApprovalOptions,
|
|
10
|
+
d as WorkflowCallbackType,
|
|
11
|
+
f as WorkflowCompleteCallback,
|
|
12
|
+
g as WorkflowInfo,
|
|
13
|
+
h as WorkflowEventPayload,
|
|
14
|
+
i as AgentWorkflowStep,
|
|
15
|
+
l as WorkflowCallback,
|
|
16
|
+
m as WorkflowEventCallback,
|
|
17
|
+
o as DefaultProgress,
|
|
18
|
+
p as WorkflowErrorCallback,
|
|
19
|
+
r as AgentWorkflowParams,
|
|
20
|
+
s as RunWorkflowOptions,
|
|
21
|
+
t as AgentWorkflowEvent,
|
|
22
|
+
v as WorkflowProgressCallback,
|
|
23
|
+
x as WorkflowStatus,
|
|
24
|
+
y as WorkflowQueryCriteria
|
|
25
|
+
} from "./workflow-types-Z_Oem1FJ.js";
|
|
26
|
+
import { Agent } from "./index.js";
|
|
27
|
+
import { WorkflowEntrypoint } from "cloudflare:workers";
|
|
28
|
+
|
|
29
|
+
//#region src/workflows.d.ts
|
|
30
|
+
/**
|
|
31
|
+
* Base class for Workflows that need access to their originating Agent.
|
|
32
|
+
*
|
|
33
|
+
* @template AgentType - The Agent class type (for typed RPC access)
|
|
34
|
+
* @template Params - User-defined params passed to the workflow (optional)
|
|
35
|
+
* @template ProgressType - Type for progress reporting (defaults to DefaultProgress)
|
|
36
|
+
* @template Env - Environment type (defaults to Cloudflare.Env)
|
|
37
|
+
*/
|
|
38
|
+
declare class AgentWorkflow<
|
|
39
|
+
AgentType extends Agent = Agent,
|
|
40
|
+
Params = unknown,
|
|
41
|
+
ProgressType = DefaultProgress,
|
|
42
|
+
Env extends Cloudflare.Env = Cloudflare.Env
|
|
43
|
+
> extends WorkflowEntrypoint<Env, AgentWorkflowParams<Params>> {
|
|
44
|
+
/**
|
|
45
|
+
* The Agent stub - initialized before run() is called.
|
|
46
|
+
* Use this.agent to access the Agent's RPC methods.
|
|
47
|
+
*/
|
|
48
|
+
private _agent;
|
|
49
|
+
/**
|
|
50
|
+
* Workflow instance ID
|
|
51
|
+
*/
|
|
52
|
+
private _workflowId;
|
|
53
|
+
/**
|
|
54
|
+
* Workflow binding name (for callbacks)
|
|
55
|
+
*/
|
|
56
|
+
private _workflowName;
|
|
57
|
+
constructor(ctx: ExecutionContext, env: Env);
|
|
58
|
+
/**
|
|
59
|
+
* Initialize the Agent stub from workflow params.
|
|
60
|
+
* Called automatically before run() executes.
|
|
61
|
+
*/
|
|
62
|
+
private _initAgent;
|
|
63
|
+
/**
|
|
64
|
+
* Wrap WorkflowStep with durable Agent communication methods.
|
|
65
|
+
* Methods added to the wrapped step are idempotent and won't repeat on retry.
|
|
66
|
+
*
|
|
67
|
+
* Note: We add methods directly to the step object to preserve instanceof checks
|
|
68
|
+
* that Cloudflare's runtime may perform on the WorkflowStep class.
|
|
69
|
+
*/
|
|
70
|
+
private _wrapStep;
|
|
71
|
+
/**
|
|
72
|
+
* Get the Agent stub for RPC calls.
|
|
73
|
+
* Provides typed access to the Agent's methods.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```typescript
|
|
77
|
+
* // Call any public method on the Agent
|
|
78
|
+
* await this.agent.updateStatus('processing');
|
|
79
|
+
* const data = await this.agent.getData();
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
get agent(): DurableObjectStub<AgentType>;
|
|
83
|
+
/**
|
|
84
|
+
* Get the workflow instance ID
|
|
85
|
+
*/
|
|
86
|
+
get workflowId(): string;
|
|
87
|
+
/**
|
|
88
|
+
* Get the workflow binding name
|
|
89
|
+
*/
|
|
90
|
+
get workflowName(): string;
|
|
91
|
+
/**
|
|
92
|
+
* Send a notification to the Agent via RPC.
|
|
93
|
+
*
|
|
94
|
+
* @param callback - Callback payload to send
|
|
95
|
+
*/
|
|
96
|
+
protected notifyAgent(callback: WorkflowCallback): Promise<void>;
|
|
97
|
+
/**
|
|
98
|
+
* Report progress to the Agent with typed progress data.
|
|
99
|
+
* Triggers onWorkflowProgress() on the Agent.
|
|
100
|
+
*
|
|
101
|
+
* @param progress - Typed progress data
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```typescript
|
|
105
|
+
* // Using default progress type
|
|
106
|
+
* await this.reportProgress({ step: 'fetch', status: 'running' });
|
|
107
|
+
* await this.reportProgress({ step: 'fetch', status: 'complete', percent: 0.5 });
|
|
108
|
+
*
|
|
109
|
+
* // With custom progress type
|
|
110
|
+
* await this.reportProgress({ stage: 'extract', recordsProcessed: 100 });
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
protected reportProgress(progress: ProgressType): Promise<void>;
|
|
114
|
+
/**
|
|
115
|
+
* Broadcast a message to all connected WebSocket clients via the Agent.
|
|
116
|
+
* This is non-durable and may repeat on workflow retry.
|
|
117
|
+
*
|
|
118
|
+
* @param message - Message to broadcast (will be JSON-stringified)
|
|
119
|
+
*/
|
|
120
|
+
protected broadcastToClients(message: unknown): void;
|
|
121
|
+
/**
|
|
122
|
+
* Wait for approval from the Agent.
|
|
123
|
+
* Handles rejection by reporting error (durably) and throwing WorkflowRejectedError.
|
|
124
|
+
*
|
|
125
|
+
* @param step - AgentWorkflowStep object
|
|
126
|
+
* @param options - Wait options (timeout, eventType, stepName)
|
|
127
|
+
* @returns Approval payload (throws WorkflowRejectedError if rejected)
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```typescript
|
|
131
|
+
* const approval = await this.waitForApproval(step, { timeout: '7 days' });
|
|
132
|
+
* // approval contains the payload from approveWorkflow()
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
protected waitForApproval<T = unknown>(
|
|
136
|
+
step: AgentWorkflowStep,
|
|
137
|
+
options?: WaitForApprovalOptions
|
|
138
|
+
): Promise<T>;
|
|
139
|
+
}
|
|
140
|
+
//#endregion
|
|
141
|
+
export {
|
|
142
|
+
AgentWorkflow,
|
|
143
|
+
type AgentWorkflowEvent,
|
|
144
|
+
type AgentWorkflowStep,
|
|
145
|
+
type ApprovalEventPayload,
|
|
146
|
+
type DefaultProgress,
|
|
147
|
+
type RunWorkflowOptions,
|
|
148
|
+
type WaitForApprovalOptions,
|
|
149
|
+
type WorkflowCallback,
|
|
150
|
+
type WorkflowCallbackType,
|
|
151
|
+
type WorkflowCompleteCallback,
|
|
152
|
+
type WorkflowErrorCallback,
|
|
153
|
+
type WorkflowEventCallback,
|
|
154
|
+
type WorkflowEventPayload,
|
|
155
|
+
type WorkflowInfo,
|
|
156
|
+
type WorkflowPage,
|
|
157
|
+
type WorkflowProgressCallback,
|
|
158
|
+
type WorkflowQueryCriteria,
|
|
159
|
+
WorkflowRejectedError,
|
|
160
|
+
type WorkflowStatus,
|
|
161
|
+
type WorkflowTrackingRow
|
|
162
|
+
};
|
|
163
|
+
//# sourceMappingURL=workflows.d.ts.map
|