@sylphx/sdk 0.26.0 → 0.28.0
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 +56 -7
- package/dist/connect.d.ts +2838 -0
- package/dist/connect.mjs +220 -0
- package/dist/connect.mjs.map +1 -0
- package/dist/index.d.ts +24491 -2055
- package/dist/index.mjs +30959 -2469
- package/dist/index.mjs.map +1 -1
- package/dist/nextjs/index.mjs +29 -26
- package/dist/nextjs/index.mjs.map +1 -1
- package/dist/react/index.d.ts +38 -1140
- package/dist/react/index.mjs +959 -2632
- package/dist/react/index.mjs.map +1 -1
- package/dist/server/index.d.ts +21 -38
- package/dist/server/index.mjs +27 -27
- package/dist/server/index.mjs.map +1 -1
- package/dist/web-analytics.mjs +1 -1
- package/dist/web-analytics.mjs.map +1 -1
- package/package.json +132 -126
- package/dist/tasks.d.ts +0 -1463
- package/dist/tasks.mjs +0 -1907
- package/dist/tasks.mjs.map +0 -1
package/dist/tasks.d.ts
DELETED
|
@@ -1,1463 +0,0 @@
|
|
|
1
|
-
import { MachineSize, DurableWorkUpsertCallbackTargetResponse, DurableWorkUpsertCallbackTargetRequestBody, DurableWorkGetTaskRunResponse, DurableWorkTaskDlqEntry, DurableWorkTaskDlqListResponse, DurableWorkTaskReplayRequestBody, DurableWorkTaskReplayResponse, DurableWorkTaskRunStep } from '@sylphx/contract';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* SDK Configuration — ADR-055 Connection URL API
|
|
5
|
-
*
|
|
6
|
-
* v0.5.0: The primary entry point is `createClient(url)` which accepts
|
|
7
|
-
* a `sylphx://` connection URL. The old `createConfig({ ref, publicKey })`
|
|
8
|
-
* API is removed.
|
|
9
|
-
*
|
|
10
|
-
* @example
|
|
11
|
-
* ```typescript
|
|
12
|
-
* import { createClient } from '@sylphx/sdk'
|
|
13
|
-
*
|
|
14
|
-
* const sylphx = createClient(process.env.SYLPHX_URL!)
|
|
15
|
-
* // Parses: sylphx://pk_prod_{ref?}_{hex}@bold-river-a1b2c3.api.sylphx.com
|
|
16
|
-
* ```
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* SDK Configuration object — immutable, frozen.
|
|
21
|
-
*
|
|
22
|
-
* Created by `createClient()` or `createServerClient()`.
|
|
23
|
-
* Passed to all pure SDK functions (`track()`, `signIn()`, etc.).
|
|
24
|
-
*/
|
|
25
|
-
interface SylphxConfig {
|
|
26
|
-
/** The credential string (pk_* or sk_*) */
|
|
27
|
-
readonly credential: string;
|
|
28
|
-
/** Credential type: 'pk' (publishable) or 'sk' (secret) */
|
|
29
|
-
readonly credentialType: 'pk' | 'sk';
|
|
30
|
-
/** Target environment: dev, stg, prod, or prev */
|
|
31
|
-
readonly env: 'dev' | 'stg' | 'prod' | 'prev';
|
|
32
|
-
/** Resource slug (first DNS label), e.g. 'bold-river-a1b2c3' */
|
|
33
|
-
readonly slug: string;
|
|
34
|
-
/** Pre-computed API base URL, e.g. 'https://bold-river-a1b2c3.api.sylphx.com/v1' */
|
|
35
|
-
readonly baseUrl: string;
|
|
36
|
-
/** Optional access token for authenticated requests */
|
|
37
|
-
readonly accessToken?: string;
|
|
38
|
-
/**
|
|
39
|
-
* Secret key — populated when credentialType is 'sk'.
|
|
40
|
-
* Backward-compatible alias for `credential` when credential is sk_*.
|
|
41
|
-
*/
|
|
42
|
-
readonly secretKey?: string;
|
|
43
|
-
/**
|
|
44
|
-
* Publishable key — populated when credentialType is 'pk'.
|
|
45
|
-
* Backward-compatible alias for `credential` when credential is pk_*.
|
|
46
|
-
*/
|
|
47
|
-
readonly publicKey?: string;
|
|
48
|
-
/**
|
|
49
|
-
* @deprecated Use `slug`. Backward-compatible alias.
|
|
50
|
-
*/
|
|
51
|
-
readonly ref: string;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* task() / taskStub()
|
|
56
|
-
*
|
|
57
|
-
* Core compute primitives — Trigger.dev v4 inspired.
|
|
58
|
-
*
|
|
59
|
-
* task() — define a TypeScript task handler that gets a typed TaskHandle
|
|
60
|
-
* taskStub() — typed stub for cross-language tasks (Python, etc.)
|
|
61
|
-
*
|
|
62
|
-
* Both return a TaskHandle<TPayload, TResult> with:
|
|
63
|
-
* .trigger(payload) → fire-and-forget, returns RunHandle
|
|
64
|
-
* .triggerAndWait(payload) → dispatch + poll until complete
|
|
65
|
-
* .batchTrigger(payloads) → parallel dispatch N runs
|
|
66
|
-
*
|
|
67
|
-
* RunHandle exposes waitForCompletion() and cancel().
|
|
68
|
-
*
|
|
69
|
-
* Config is auto-resolved from SYLPHX_SECRET_URL or passed explicitly via
|
|
70
|
-
* TriggerOptions.config.
|
|
71
|
-
*
|
|
72
|
-
* @example TypeScript task (function mode — HTTP callback in running service)
|
|
73
|
-
* ```typescript
|
|
74
|
-
* export const sendEmail = task({
|
|
75
|
-
* id: 'send-email',
|
|
76
|
-
* run: async ({ to, subject }, { step }) => {
|
|
77
|
-
* const validated = await step.run('validate', () => validate(to))
|
|
78
|
-
* await step.sleep('cooldown', '5 minutes')
|
|
79
|
-
* return step.run('send', () => mailer.send(validated, subject))
|
|
80
|
-
* }
|
|
81
|
-
* })
|
|
82
|
-
*
|
|
83
|
-
* // From anywhere — fully type-safe
|
|
84
|
-
* const handle = await sendEmail.trigger({ to: 'kyle@...', subject: 'Hi' })
|
|
85
|
-
* const result = await handle.waitForCompletion()
|
|
86
|
-
* ```
|
|
87
|
-
*
|
|
88
|
-
* @example TypeScript task (job mode — isolated runtime, managed machine)
|
|
89
|
-
* ```typescript
|
|
90
|
-
* export const processVideo = task({
|
|
91
|
-
* id: 'process-video',
|
|
92
|
-
* machine: 'large',
|
|
93
|
-
* run: async ({ videoUrl }) => { ... }
|
|
94
|
-
* })
|
|
95
|
-
* ```
|
|
96
|
-
*
|
|
97
|
-
* @example Cross-language stub (Python ML training)
|
|
98
|
-
* ```typescript
|
|
99
|
-
* // Register in sylphx.toml, then stub here for type safety:
|
|
100
|
-
* export const trainFold = taskStub<
|
|
101
|
-
* { fold: number; n_trials?: number },
|
|
102
|
-
* { best_icir: number }
|
|
103
|
-
* >('train-fold')
|
|
104
|
-
*
|
|
105
|
-
* // Dispatch 5 parallel folds
|
|
106
|
-
* const handles = await trainFold.batchTrigger(
|
|
107
|
-
* Array.from({ length: 5 }, (_, i) => ({ fold: i, n_trials: 200 }))
|
|
108
|
-
* )
|
|
109
|
-
* await Promise.all(handles.runs.map(h => h.waitForCompletion()))
|
|
110
|
-
* ```
|
|
111
|
-
*/
|
|
112
|
-
|
|
113
|
-
type TaskMachineSize = MachineSize;
|
|
114
|
-
interface TaskRunContext {
|
|
115
|
-
step: StepContext$1;
|
|
116
|
-
/** Current attempt number (starts at 1) */
|
|
117
|
-
attempt: number;
|
|
118
|
-
}
|
|
119
|
-
interface StepContext$1 {
|
|
120
|
-
/** Execute a named step. Replays from cache if already completed (durable). */
|
|
121
|
-
run<T>(id: string, fn: () => T | Promise<T>): Promise<T>;
|
|
122
|
-
/** Sleep for a duration. Replays instantly if already resolved. */
|
|
123
|
-
sleep(id: string, duration: string): Promise<void>;
|
|
124
|
-
}
|
|
125
|
-
interface TriggerOptions {
|
|
126
|
-
/** Override auto-resolved config (defaults to SYLPHX_SECRET_URL env var) */
|
|
127
|
-
config?: SylphxConfig;
|
|
128
|
-
/** Deduplication key — same key within 24h returns existing run */
|
|
129
|
-
idempotencyKey?: string;
|
|
130
|
-
/** Delay dispatch, e.g. '5m', '1h' */
|
|
131
|
-
delay?: string;
|
|
132
|
-
/** Dispatch at specific time */
|
|
133
|
-
scheduledFor?: Date;
|
|
134
|
-
/** Override max retry attempts */
|
|
135
|
-
maxAttempts?: number;
|
|
136
|
-
/** Queue name override */
|
|
137
|
-
queue?: string;
|
|
138
|
-
}
|
|
139
|
-
interface RunHandle<TResult = unknown> {
|
|
140
|
-
/** Task run UUID */
|
|
141
|
-
id: string;
|
|
142
|
-
/** Current status at time of trigger */
|
|
143
|
-
status: string;
|
|
144
|
-
/**
|
|
145
|
-
* Poll until the run reaches a terminal state (completed/failed/cancelled).
|
|
146
|
-
* Throws if failed or timed out.
|
|
147
|
-
*/
|
|
148
|
-
waitForCompletion(options?: {
|
|
149
|
-
timeoutMs?: number;
|
|
150
|
-
}): Promise<TResult>;
|
|
151
|
-
/**
|
|
152
|
-
* Record durable cancellation for this run. Pending/running/waiting/failed
|
|
153
|
-
* runs become terminal and job-mode cleanup is retried by Platform.
|
|
154
|
-
*/
|
|
155
|
-
cancel(): Promise<boolean>;
|
|
156
|
-
}
|
|
157
|
-
interface BatchHandle<TResult = unknown> {
|
|
158
|
-
runs: RunHandle<TResult>[];
|
|
159
|
-
}
|
|
160
|
-
/** Inline trigger configuration on a task definition (ADR-040) */
|
|
161
|
-
interface TaskTriggerConfig {
|
|
162
|
-
/**
|
|
163
|
-
* Cron schedule — automatically fires this task on the given schedule.
|
|
164
|
-
* Standard 5-field cron expression: '0 2 * * *' (every day at 2am)
|
|
165
|
-
* The platform registers this trigger on deploy via serve().
|
|
166
|
-
*/
|
|
167
|
-
cron?: string;
|
|
168
|
-
/**
|
|
169
|
-
* Event name — automatically fires this task when the named event is published.
|
|
170
|
-
* e.g. 'user.signup', 'order.created'
|
|
171
|
-
* Future: event triggers are not yet dispatched by the platform.
|
|
172
|
-
*/
|
|
173
|
-
event?: string;
|
|
174
|
-
}
|
|
175
|
-
interface TaskConfig<TPayload, TResult> {
|
|
176
|
-
/** Stable task identifier — must not change across deploys */
|
|
177
|
-
id: string;
|
|
178
|
-
/**
|
|
179
|
-
* Automatic trigger configuration (ADR-040).
|
|
180
|
-
* If set, the platform registers this trigger when serve() is called.
|
|
181
|
-
* trigger.cron → cron schedule fires task automatically
|
|
182
|
-
* trigger.event → event fires task automatically
|
|
183
|
-
*/
|
|
184
|
-
trigger?: TaskTriggerConfig;
|
|
185
|
-
/**
|
|
186
|
-
* Managed machine size. If set → task runs in an isolated runtime (job mode).
|
|
187
|
-
* If absent → task runs as an HTTP callback in the running service (function mode).
|
|
188
|
-
*/
|
|
189
|
-
machine?: TaskMachineSize;
|
|
190
|
-
/** Queue config */
|
|
191
|
-
queue?: {
|
|
192
|
-
name?: string;
|
|
193
|
-
concurrency?: number;
|
|
194
|
-
};
|
|
195
|
-
/** Retry policy */
|
|
196
|
-
retry?: {
|
|
197
|
-
maxAttempts?: number;
|
|
198
|
-
backoff?: 'fixed' | 'exponential';
|
|
199
|
-
};
|
|
200
|
-
/** Timeout in seconds (default: 300 for function mode, 86400 for job mode) */
|
|
201
|
-
timeout?: number;
|
|
202
|
-
/** The task handler. In function mode: runs via HTTP dispatch. In job mode: runs in an isolated runtime. */
|
|
203
|
-
run: (payload: TPayload, ctx: TaskRunContext) => Promise<TResult>;
|
|
204
|
-
}
|
|
205
|
-
interface TaskHandle<TPayload = unknown, TResult = unknown> {
|
|
206
|
-
/** The stable task ID */
|
|
207
|
-
readonly id: string;
|
|
208
|
-
/** Trigger config if task has an automatic trigger (cron/event) */
|
|
209
|
-
readonly _trigger?: TaskTriggerConfig;
|
|
210
|
-
/** Managed machine size if job-mode task (undefined for function-mode or stubs) */
|
|
211
|
-
readonly _machine?: TaskMachineSize;
|
|
212
|
-
/**
|
|
213
|
-
* The run function — used internally by serve() to register the handler.
|
|
214
|
-
* undefined for taskStub (cross-language).
|
|
215
|
-
*/
|
|
216
|
-
readonly _run?: (payload: TPayload, ctx: TaskRunContext) => Promise<TResult>;
|
|
217
|
-
/** Dispatch the task. Returns immediately with a RunHandle. */
|
|
218
|
-
trigger(payload: TPayload, options?: TriggerOptions): Promise<RunHandle<TResult>>;
|
|
219
|
-
/** Dispatch and wait for result. Convenience wrapper over trigger + waitForCompletion. */
|
|
220
|
-
triggerAndWait(payload: TPayload, options?: TriggerOptions): Promise<TResult>;
|
|
221
|
-
/** Dispatch N runs in parallel. Returns handles for all. */
|
|
222
|
-
batchTrigger(payloads: TPayload[], options?: TriggerOptions): Promise<BatchHandle<TResult>>;
|
|
223
|
-
}
|
|
224
|
-
/**
|
|
225
|
-
* Define a typed task.
|
|
226
|
-
*
|
|
227
|
-
* Returns a TaskHandle<TPayload, TResult> that can be used to trigger the task
|
|
228
|
-
* from anywhere (same service or different service) with full type safety.
|
|
229
|
-
*
|
|
230
|
-
* The returned handle's _run function is used by serve() to register the
|
|
231
|
-
* HTTP handler for function-mode tasks.
|
|
232
|
-
*/
|
|
233
|
-
declare function task$1<TPayload = unknown, TResult = unknown>(config: TaskConfig<TPayload, TResult>): TaskHandle<TPayload, TResult>;
|
|
234
|
-
/**
|
|
235
|
-
* Create a typed stub for a cross-language task (e.g. Python ML worker).
|
|
236
|
-
*
|
|
237
|
-
* The task must be registered separately via sylphx.toml manifest.
|
|
238
|
-
* The stub provides type safety for the trigger call without requiring
|
|
239
|
-
* a TypeScript handler.
|
|
240
|
-
*
|
|
241
|
-
* @example
|
|
242
|
-
* ```typescript
|
|
243
|
-
* export const trainFold = taskStub<
|
|
244
|
-
* { fold: number; n_trials?: number },
|
|
245
|
-
* { best_icir: number }
|
|
246
|
-
* >('train-fold')
|
|
247
|
-
*
|
|
248
|
-
* const result = await trainFold.triggerAndWait({ fold: 0 })
|
|
249
|
-
* console.log(result.best_icir) // typed!
|
|
250
|
-
* ```
|
|
251
|
-
*/
|
|
252
|
-
declare function taskStub<TPayload = unknown, TResult = unknown>(id: string): TaskHandle<TPayload, TResult>;
|
|
253
|
-
|
|
254
|
-
/**
|
|
255
|
-
* serve()
|
|
256
|
-
*
|
|
257
|
-
* Create a Web API-compatible route handler for a set of task definitions.
|
|
258
|
-
* Supports Next.js App Router, Hono, and any Web API Request/Response handler.
|
|
259
|
-
*
|
|
260
|
-
* serve() wires up:
|
|
261
|
-
* - GET → health check, returns registered task IDs + trigger sync status
|
|
262
|
-
* - POST → dispatch handler (receives from platform, routes to task run fn)
|
|
263
|
-
*
|
|
264
|
-
* On startup (first GET or POST), serve() automatically syncs all inline triggers
|
|
265
|
-
* (task({ trigger: { cron: '...' } })) to the platform via TriggersClient.
|
|
266
|
-
* This is idempotent — uses idempotencyKey = `task:{taskId}:{triggerType}:{value}`.
|
|
267
|
-
*
|
|
268
|
-
* Only function-mode tasks (no machine config) register HTTP handlers here.
|
|
269
|
-
* Job-mode tasks are dispatched directly by the platform, but their
|
|
270
|
-
* inline triggers are still synced.
|
|
271
|
-
*
|
|
272
|
-
* @example Next.js App Router
|
|
273
|
-
* ```typescript
|
|
274
|
-
* // app/api/tasks/route.ts
|
|
275
|
-
* import { serve } from '@sylphx/sdk/tasks'
|
|
276
|
-
* import { sendEmail, dailyCleanup } from '@/tasks'
|
|
277
|
-
*
|
|
278
|
-
* export const { GET, POST } = serve({ tasks: [sendEmail, dailyCleanup] })
|
|
279
|
-
* ```
|
|
280
|
-
*
|
|
281
|
-
* @example Inline cron trigger
|
|
282
|
-
* ```typescript
|
|
283
|
-
* export const dailyCleanup = task({
|
|
284
|
-
* id: 'daily-cleanup',
|
|
285
|
-
* trigger: { cron: '0 2 * * *' },
|
|
286
|
-
* run: async () => { ... },
|
|
287
|
-
* })
|
|
288
|
-
* // No TriggersClient.create() call needed — serve() registers it automatically.
|
|
289
|
-
* ```
|
|
290
|
-
*/
|
|
291
|
-
|
|
292
|
-
interface ServeOptions {
|
|
293
|
-
/** Task handles to serve */
|
|
294
|
-
tasks: TaskHandle<unknown, unknown>[];
|
|
295
|
-
/** Platform signing secret for verifying dispatches. Defaults to SYLPHX_SIGNING_SECRET env var. */
|
|
296
|
-
signingSecret?: string;
|
|
297
|
-
/**
|
|
298
|
-
* Server connection URL for trigger registration.
|
|
299
|
-
* Defaults to SYLPHX_SECRET_URL env var.
|
|
300
|
-
* Only required if any tasks have inline trigger config.
|
|
301
|
-
*/
|
|
302
|
-
connectionUrl?: string;
|
|
303
|
-
}
|
|
304
|
-
interface ServeResult {
|
|
305
|
-
GET: (req: Request) => Promise<Response>;
|
|
306
|
-
POST: (req: Request) => Promise<Response>;
|
|
307
|
-
}
|
|
308
|
-
/**
|
|
309
|
-
* Create a route handler that serves task definitions.
|
|
310
|
-
*
|
|
311
|
-
* Auto-syncs inline triggers (task({ trigger: { cron: '...' } })) on first request.
|
|
312
|
-
* Task sync is idempotent and non-blocking — handler responds even if sync is in progress.
|
|
313
|
-
*/
|
|
314
|
-
declare function serve(options: ServeOptions): ServeResult;
|
|
315
|
-
|
|
316
|
-
/**
|
|
317
|
-
* Tasks Types
|
|
318
|
-
*
|
|
319
|
-
* Type definitions for background job scheduling and workflows.
|
|
320
|
-
* Supports durable execution, retries, and workflow composition.
|
|
321
|
-
*/
|
|
322
|
-
/** Task status */
|
|
323
|
-
type TaskStatus$1 = 'pending' | 'scheduled' | 'running' | 'completed' | 'failed' | 'cancelled' | 'retrying' | 'dead';
|
|
324
|
-
/** Job priority */
|
|
325
|
-
type TaskPriority = 'low' | 'normal' | 'high' | 'critical';
|
|
326
|
-
/** Job payload - must be JSON serializable */
|
|
327
|
-
type TaskPayload = Record<string, unknown>;
|
|
328
|
-
/** Job result */
|
|
329
|
-
type TaskResult$1 = unknown;
|
|
330
|
-
/** Job definition */
|
|
331
|
-
interface TaskDefinition<TPayload extends TaskPayload = TaskPayload, TResult = TaskResult$1> {
|
|
332
|
-
/** Unique job type identifier */
|
|
333
|
-
type: string;
|
|
334
|
-
/** Human-readable name */
|
|
335
|
-
name?: string;
|
|
336
|
-
/** Description */
|
|
337
|
-
description?: string;
|
|
338
|
-
/** Job handler (server-side) */
|
|
339
|
-
handler?: (payload: TPayload, context: TaskContext) => Promise<TResult>;
|
|
340
|
-
/** Default options */
|
|
341
|
-
defaults?: TaskOptions;
|
|
342
|
-
/** Validation schema */
|
|
343
|
-
validate?: (payload: TPayload) => boolean | string;
|
|
344
|
-
}
|
|
345
|
-
/** Job options */
|
|
346
|
-
interface TaskOptions {
|
|
347
|
-
/** Priority */
|
|
348
|
-
priority?: TaskPriority;
|
|
349
|
-
/** Max retry attempts */
|
|
350
|
-
maxRetries?: number;
|
|
351
|
-
/** Retry delay strategy */
|
|
352
|
-
retryDelay?: RetryDelayStrategy;
|
|
353
|
-
/** Timeout in ms */
|
|
354
|
-
timeout?: number;
|
|
355
|
-
/** Delay execution */
|
|
356
|
-
delay?: number | Date;
|
|
357
|
-
/** Cron schedule */
|
|
358
|
-
cron?: string;
|
|
359
|
-
/** Unique job ID (for deduplication) */
|
|
360
|
-
uniqueId?: string;
|
|
361
|
-
/** Unique key TTL in ms (how long to prevent duplicates) */
|
|
362
|
-
uniqueTtl?: number;
|
|
363
|
-
/** Dead letter queue options */
|
|
364
|
-
dlq?: DLQOptions;
|
|
365
|
-
/** Tags for filtering */
|
|
366
|
-
tags?: string[];
|
|
367
|
-
/** Custom metadata */
|
|
368
|
-
metadata?: Record<string, unknown>;
|
|
369
|
-
}
|
|
370
|
-
/** Retry delay strategy */
|
|
371
|
-
type RetryDelayStrategy = {
|
|
372
|
-
type: 'fixed';
|
|
373
|
-
delay: number;
|
|
374
|
-
} | {
|
|
375
|
-
type: 'exponential';
|
|
376
|
-
base: number;
|
|
377
|
-
maxDelay?: number;
|
|
378
|
-
} | {
|
|
379
|
-
type: 'linear';
|
|
380
|
-
initial: number;
|
|
381
|
-
increment: number;
|
|
382
|
-
maxDelay?: number;
|
|
383
|
-
} | {
|
|
384
|
-
type: 'custom';
|
|
385
|
-
delays: number[];
|
|
386
|
-
};
|
|
387
|
-
/** DLQ options */
|
|
388
|
-
interface DLQOptions {
|
|
389
|
-
/** Enable DLQ */
|
|
390
|
-
enabled?: boolean;
|
|
391
|
-
/** Max age before auto-discard (ms) */
|
|
392
|
-
maxAge?: number;
|
|
393
|
-
/** Auto-retry from DLQ */
|
|
394
|
-
autoRetry?: boolean;
|
|
395
|
-
/** Auto-retry interval (ms) */
|
|
396
|
-
autoRetryInterval?: number;
|
|
397
|
-
/** Max auto-retry attempts from DLQ */
|
|
398
|
-
maxAutoRetries?: number;
|
|
399
|
-
}
|
|
400
|
-
/** Job instance */
|
|
401
|
-
interface TaskInstance<TPayload extends TaskPayload = TaskPayload, TResult = TaskResult$1> {
|
|
402
|
-
/** Unique job ID */
|
|
403
|
-
id: string;
|
|
404
|
-
/** Job type */
|
|
405
|
-
type: string;
|
|
406
|
-
/** Job payload */
|
|
407
|
-
payload: TPayload;
|
|
408
|
-
/** Current status */
|
|
409
|
-
status: TaskStatus$1;
|
|
410
|
-
/** Priority */
|
|
411
|
-
priority: TaskPriority;
|
|
412
|
-
/** Created timestamp */
|
|
413
|
-
createdAt: number;
|
|
414
|
-
/** Scheduled execution time */
|
|
415
|
-
scheduledAt?: number;
|
|
416
|
-
/** Started timestamp */
|
|
417
|
-
startedAt?: number;
|
|
418
|
-
/** Completed timestamp */
|
|
419
|
-
completedAt?: number;
|
|
420
|
-
/** Current attempt number */
|
|
421
|
-
attempt: number;
|
|
422
|
-
/** Max retry attempts */
|
|
423
|
-
maxRetries: number;
|
|
424
|
-
/** Job result (if completed) */
|
|
425
|
-
result?: TResult;
|
|
426
|
-
/** Error message (if failed) */
|
|
427
|
-
error?: string;
|
|
428
|
-
/** Error stack trace */
|
|
429
|
-
errorStack?: string;
|
|
430
|
-
/** Progress (0-100) */
|
|
431
|
-
progress?: number;
|
|
432
|
-
/** Progress message */
|
|
433
|
-
progressMessage?: string;
|
|
434
|
-
/** Tags */
|
|
435
|
-
tags?: string[];
|
|
436
|
-
/** Custom metadata */
|
|
437
|
-
metadata?: Record<string, unknown>;
|
|
438
|
-
/** Parent job ID (for workflows) */
|
|
439
|
-
parentId?: string;
|
|
440
|
-
/** Workflow step name (for workflows) */
|
|
441
|
-
stepName?: string;
|
|
442
|
-
}
|
|
443
|
-
/** Job context passed to handlers */
|
|
444
|
-
interface TaskContext {
|
|
445
|
-
/** Job ID */
|
|
446
|
-
jobId: string;
|
|
447
|
-
/** Current attempt number */
|
|
448
|
-
attempt: number;
|
|
449
|
-
/** Signal for cancellation */
|
|
450
|
-
signal?: AbortSignal;
|
|
451
|
-
/** Update progress */
|
|
452
|
-
setProgress: (progress: number, message?: string) => void;
|
|
453
|
-
/** Log message */
|
|
454
|
-
log: (message: string, level?: 'info' | 'warn' | 'error') => void;
|
|
455
|
-
/** Check if should continue (for long-running jobs) */
|
|
456
|
-
shouldContinue: () => boolean;
|
|
457
|
-
/** Schedule a child job */
|
|
458
|
-
scheduleChild: <T extends TaskPayload>(type: string, payload: T, options?: TaskOptions) => Promise<string>;
|
|
459
|
-
/** Sleep (durable) */
|
|
460
|
-
sleep: (ms: number) => Promise<void>;
|
|
461
|
-
}
|
|
462
|
-
/** Workflow definition */
|
|
463
|
-
interface WorkflowDefinition<__TInput extends TaskPayload = TaskPayload, TOutput = TaskResult$1> {
|
|
464
|
-
/** Unique workflow type */
|
|
465
|
-
type: string;
|
|
466
|
-
/** Human-readable name */
|
|
467
|
-
name?: string;
|
|
468
|
-
/** Description */
|
|
469
|
-
description?: string;
|
|
470
|
-
/** Workflow steps */
|
|
471
|
-
steps: WorkflowStep[];
|
|
472
|
-
/** Default options */
|
|
473
|
-
defaults?: WorkflowOptions;
|
|
474
|
-
/** On complete callback */
|
|
475
|
-
onComplete?: (result: TOutput) => void;
|
|
476
|
-
/** On error callback */
|
|
477
|
-
onError?: (error: Error, step: string) => void;
|
|
478
|
-
}
|
|
479
|
-
/** Workflow options */
|
|
480
|
-
interface WorkflowOptions extends TaskOptions {
|
|
481
|
-
/** Continue on step failure */
|
|
482
|
-
continueOnFailure?: boolean;
|
|
483
|
-
/** Max concurrent steps (for parallel) */
|
|
484
|
-
maxConcurrency?: number;
|
|
485
|
-
}
|
|
486
|
-
/** Workflow step */
|
|
487
|
-
type WorkflowStep = TaskStep | ConditionalStep | ParallelStep | LoopStep | WaitStep | SubworkflowStep;
|
|
488
|
-
/** Job step */
|
|
489
|
-
interface TaskStep {
|
|
490
|
-
type: 'job';
|
|
491
|
-
name: string;
|
|
492
|
-
/** Job type to execute */
|
|
493
|
-
jobType: string;
|
|
494
|
-
/** Payload transform (receives workflow input + previous results) */
|
|
495
|
-
payload?: (context: StepContext) => TaskPayload;
|
|
496
|
-
/** Step options */
|
|
497
|
-
options?: TaskOptions;
|
|
498
|
-
/** Condition to run this step */
|
|
499
|
-
if?: (context: StepContext) => boolean;
|
|
500
|
-
/** On complete transform */
|
|
501
|
-
onComplete?: (result: TaskResult$1, context: StepContext) => unknown;
|
|
502
|
-
}
|
|
503
|
-
/** Conditional step */
|
|
504
|
-
interface ConditionalStep {
|
|
505
|
-
type: 'conditional';
|
|
506
|
-
name: string;
|
|
507
|
-
/** Condition function */
|
|
508
|
-
condition: (context: StepContext) => boolean;
|
|
509
|
-
/** Steps if condition is true */
|
|
510
|
-
then: WorkflowStep[];
|
|
511
|
-
/** Steps if condition is false */
|
|
512
|
-
else?: WorkflowStep[];
|
|
513
|
-
}
|
|
514
|
-
/** Parallel step */
|
|
515
|
-
interface ParallelStep {
|
|
516
|
-
type: 'parallel';
|
|
517
|
-
name: string;
|
|
518
|
-
/** Steps to run in parallel */
|
|
519
|
-
steps: WorkflowStep[];
|
|
520
|
-
/** Wait for all or any */
|
|
521
|
-
waitFor?: 'all' | 'any';
|
|
522
|
-
/** Max concurrent */
|
|
523
|
-
maxConcurrency?: number;
|
|
524
|
-
}
|
|
525
|
-
/** Loop step */
|
|
526
|
-
interface LoopStep {
|
|
527
|
-
type: 'loop';
|
|
528
|
-
name: string;
|
|
529
|
-
/** Array to iterate over, or function to get items */
|
|
530
|
-
items: unknown[] | ((context: StepContext) => unknown[]);
|
|
531
|
-
/** Step to execute for each item */
|
|
532
|
-
step: WorkflowStep;
|
|
533
|
-
/** Max concurrent iterations */
|
|
534
|
-
maxConcurrency?: number;
|
|
535
|
-
/** Break condition */
|
|
536
|
-
breakIf?: (item: unknown, index: number, context: StepContext) => boolean;
|
|
537
|
-
}
|
|
538
|
-
/** Wait step */
|
|
539
|
-
interface WaitStep {
|
|
540
|
-
type: 'wait';
|
|
541
|
-
name: string;
|
|
542
|
-
/** Duration in ms, Date, or function */
|
|
543
|
-
until: number | Date | ((context: StepContext) => number | Date);
|
|
544
|
-
}
|
|
545
|
-
/** Sub-workflow step */
|
|
546
|
-
interface SubworkflowStep {
|
|
547
|
-
type: 'subworkflow';
|
|
548
|
-
name: string;
|
|
549
|
-
/** Workflow type to execute */
|
|
550
|
-
workflowType: string;
|
|
551
|
-
/** Payload transform */
|
|
552
|
-
payload?: (context: StepContext) => TaskPayload;
|
|
553
|
-
/** Options */
|
|
554
|
-
options?: WorkflowOptions;
|
|
555
|
-
}
|
|
556
|
-
/** Step context */
|
|
557
|
-
interface StepContext {
|
|
558
|
-
/** Workflow input */
|
|
559
|
-
input: TaskPayload;
|
|
560
|
-
/** Results from previous steps */
|
|
561
|
-
results: Record<string, TaskResult$1>;
|
|
562
|
-
/** Current step name */
|
|
563
|
-
currentStep: string;
|
|
564
|
-
/** Workflow metadata */
|
|
565
|
-
metadata: Record<string, unknown>;
|
|
566
|
-
}
|
|
567
|
-
/** Workflow instance */
|
|
568
|
-
interface Workflow<_TInput extends TaskPayload = TaskPayload, TOutput = TaskResult$1> {
|
|
569
|
-
/** Unique workflow ID */
|
|
570
|
-
id: string;
|
|
571
|
-
/** Workflow type */
|
|
572
|
-
type: string;
|
|
573
|
-
/** Input payload */
|
|
574
|
-
input: _TInput;
|
|
575
|
-
/** Current status */
|
|
576
|
-
status: TaskStatus$1;
|
|
577
|
-
/** Created timestamp */
|
|
578
|
-
createdAt: number;
|
|
579
|
-
/** Started timestamp */
|
|
580
|
-
startedAt?: number;
|
|
581
|
-
/** Completed timestamp */
|
|
582
|
-
completedAt?: number;
|
|
583
|
-
/** Current step name */
|
|
584
|
-
currentStep?: string;
|
|
585
|
-
/** Step results */
|
|
586
|
-
stepResults: Record<string, TaskResult$1>;
|
|
587
|
-
/** Step statuses */
|
|
588
|
-
stepStatuses: Record<string, TaskStatus$1>;
|
|
589
|
-
/** Final output */
|
|
590
|
-
output?: TOutput;
|
|
591
|
-
/** Error */
|
|
592
|
-
error?: string;
|
|
593
|
-
/** Metadata */
|
|
594
|
-
metadata?: Record<string, unknown>;
|
|
595
|
-
}
|
|
596
|
-
/** Cron schedule */
|
|
597
|
-
interface CronSchedule {
|
|
598
|
-
/** Cron expression */
|
|
599
|
-
expression: string;
|
|
600
|
-
/** Timezone */
|
|
601
|
-
timezone?: string;
|
|
602
|
-
/** Start date */
|
|
603
|
-
startAt?: Date;
|
|
604
|
-
/** End date */
|
|
605
|
-
endAt?: Date;
|
|
606
|
-
/** Max runs */
|
|
607
|
-
maxRuns?: number;
|
|
608
|
-
}
|
|
609
|
-
/** Scheduled job */
|
|
610
|
-
interface ScheduledTask {
|
|
611
|
-
/** Schedule ID */
|
|
612
|
-
id: string;
|
|
613
|
-
/** Job type */
|
|
614
|
-
type: string;
|
|
615
|
-
/** Job payload */
|
|
616
|
-
payload: TaskPayload;
|
|
617
|
-
/** Cron schedule */
|
|
618
|
-
cron: CronSchedule;
|
|
619
|
-
/** Current status */
|
|
620
|
-
status: 'active' | 'paused' | 'completed';
|
|
621
|
-
/** Run count */
|
|
622
|
-
runCount: number;
|
|
623
|
-
/** Next run timestamp */
|
|
624
|
-
nextRunAt?: number;
|
|
625
|
-
/** Last run timestamp */
|
|
626
|
-
lastRunAt?: number;
|
|
627
|
-
/** Created timestamp */
|
|
628
|
-
createdAt: number;
|
|
629
|
-
}
|
|
630
|
-
/** Job event */
|
|
631
|
-
type TaskEvent = {
|
|
632
|
-
type: 'job:created';
|
|
633
|
-
job: Job;
|
|
634
|
-
} | {
|
|
635
|
-
type: 'job:started';
|
|
636
|
-
job: Job;
|
|
637
|
-
} | {
|
|
638
|
-
type: 'job:progress';
|
|
639
|
-
job: Job;
|
|
640
|
-
progress: number;
|
|
641
|
-
message?: string;
|
|
642
|
-
} | {
|
|
643
|
-
type: 'job:completed';
|
|
644
|
-
job: Job;
|
|
645
|
-
result: TaskResult$1;
|
|
646
|
-
} | {
|
|
647
|
-
type: 'job:failed';
|
|
648
|
-
job: Job;
|
|
649
|
-
error: string;
|
|
650
|
-
} | {
|
|
651
|
-
type: 'job:retrying';
|
|
652
|
-
job: Job;
|
|
653
|
-
attempt: number;
|
|
654
|
-
} | {
|
|
655
|
-
type: 'job:dead';
|
|
656
|
-
job: Job;
|
|
657
|
-
} | {
|
|
658
|
-
type: 'job:cancelled';
|
|
659
|
-
job: Job;
|
|
660
|
-
};
|
|
661
|
-
/** Workflow event */
|
|
662
|
-
type WorkflowEvent = {
|
|
663
|
-
type: 'workflow:started';
|
|
664
|
-
workflow: Workflow;
|
|
665
|
-
} | {
|
|
666
|
-
type: 'workflow:step:started';
|
|
667
|
-
workflow: Workflow;
|
|
668
|
-
step: string;
|
|
669
|
-
} | {
|
|
670
|
-
type: 'workflow:step:completed';
|
|
671
|
-
workflow: Workflow;
|
|
672
|
-
step: string;
|
|
673
|
-
result: TaskResult$1;
|
|
674
|
-
} | {
|
|
675
|
-
type: 'workflow:step:failed';
|
|
676
|
-
workflow: Workflow;
|
|
677
|
-
step: string;
|
|
678
|
-
error: string;
|
|
679
|
-
} | {
|
|
680
|
-
type: 'workflow:completed';
|
|
681
|
-
workflow: Workflow;
|
|
682
|
-
output: TaskResult$1;
|
|
683
|
-
} | {
|
|
684
|
-
type: 'workflow:failed';
|
|
685
|
-
workflow: Workflow;
|
|
686
|
-
error: string;
|
|
687
|
-
};
|
|
688
|
-
/** Jobs client configuration */
|
|
689
|
-
interface TasksConfig {
|
|
690
|
-
/** API endpoint */
|
|
691
|
-
apiEndpoint?: string;
|
|
692
|
-
/** API key */
|
|
693
|
-
apiKey?: string;
|
|
694
|
-
/** Default job options */
|
|
695
|
-
defaultOptions?: TaskOptions;
|
|
696
|
-
/** Enable debug logging */
|
|
697
|
-
debug?: boolean;
|
|
698
|
-
/** Event callbacks */
|
|
699
|
-
onTaskEvent?: (event: TaskEvent) => void;
|
|
700
|
-
onWorkflowEvent?: (event: WorkflowEvent) => void;
|
|
701
|
-
}
|
|
702
|
-
/** Default configuration */
|
|
703
|
-
declare const DEFAULT_JOBS_CONFIG: Required<Pick<TasksConfig, 'defaultOptions' | 'debug'>>;
|
|
704
|
-
/** Default retry delays (ms) */
|
|
705
|
-
declare const DEFAULT_RETRY_DELAYS: readonly [1000, 5000, 15000, 30000, 60000];
|
|
706
|
-
type Job<TPayload extends TaskPayload = TaskPayload, TResult = TaskResult$1> = TaskInstance<TPayload, TResult>;
|
|
707
|
-
type JobContext = TaskContext;
|
|
708
|
-
type JobDefinition<TPayload extends TaskPayload = TaskPayload, TResult = TaskResult$1> = TaskDefinition<TPayload, TResult>;
|
|
709
|
-
type JobEvent = TaskEvent;
|
|
710
|
-
type JobOptions = TaskOptions;
|
|
711
|
-
type JobPayload = TaskPayload;
|
|
712
|
-
type JobPriority = TaskPriority;
|
|
713
|
-
type JobResult = TaskResult$1;
|
|
714
|
-
/** @deprecated use TaskStatus */
|
|
715
|
-
type JobStatus = TaskStatus$1;
|
|
716
|
-
type JobStep = TaskStep;
|
|
717
|
-
type ScheduledJob = ScheduledTask;
|
|
718
|
-
/**
|
|
719
|
-
* Step execution context injected into every task handler.
|
|
720
|
-
* Methods implement the stateless-replay model:
|
|
721
|
-
* - Completed steps return from cache immediately.
|
|
722
|
-
* - Uncached steps execute and signal the platform.
|
|
723
|
-
*/
|
|
724
|
-
type NativeStepContext = {
|
|
725
|
-
/**
|
|
726
|
-
* Execute a named step with automatic checkpointing.
|
|
727
|
-
*
|
|
728
|
-
* On first run: executes fn and signals the platform to save the result.
|
|
729
|
-
* On replay: returns the cached result without re-running fn.
|
|
730
|
-
*/
|
|
731
|
-
run<T>(name: string, fn: () => T | Promise<T>): Promise<T>;
|
|
732
|
-
/**
|
|
733
|
-
* Execute an external side effect with a durable idempotency key.
|
|
734
|
-
*
|
|
735
|
-
* First encounter: signals Platform to persist a side-effect receipt before
|
|
736
|
-
* the external call. Replay then invokes fn with that persisted key. If the
|
|
737
|
-
* handler crashes after the external call but before completion settles, the
|
|
738
|
-
* next replay reuses the same key so the downstream service suppresses duplicates.
|
|
739
|
-
*/
|
|
740
|
-
sideEffect<T>(stepName: string, effectKey: string, externalService: string, fn: (idempotencyKey: string) => NativeSideEffectOutcome<T> | Promise<NativeSideEffectOutcome<T>>): Promise<T>;
|
|
741
|
-
/**
|
|
742
|
-
* Pause execution for the given duration.
|
|
743
|
-
*
|
|
744
|
-
* On first encounter: signals the platform to wait; stops execution.
|
|
745
|
-
* After the wait resolves: returns immediately (no-op on replay).
|
|
746
|
-
*
|
|
747
|
-
* @param name Step identifier (must be unique within the handler).
|
|
748
|
-
* @param duration Human-readable duration: '5s', '30m', '2h', '1d'.
|
|
749
|
-
*/
|
|
750
|
-
sleep(name: string, duration: string): Promise<void>;
|
|
751
|
-
/**
|
|
752
|
-
* Pause execution until a named event is published via TriggersClient.publishEvent().
|
|
753
|
-
*
|
|
754
|
-
* On first encounter: signals the platform to wait for the event; stops execution.
|
|
755
|
-
* After the event arrives: returns the event payload.
|
|
756
|
-
* If timeout expires before event arrives: returns null.
|
|
757
|
-
*
|
|
758
|
-
* @param name Step identifier (must be unique within the handler).
|
|
759
|
-
* @param eventName The event name to listen for (e.g. 'user.approved').
|
|
760
|
-
* @param options Optional timeout ('24h', '7d') and payload filter.
|
|
761
|
-
*
|
|
762
|
-
* @example Human-in-the-loop approval
|
|
763
|
-
* ```typescript
|
|
764
|
-
* const approval = await step.waitForEvent<{ approvedBy: string }>(
|
|
765
|
-
* 'wait-approval',
|
|
766
|
-
* 'order.approved',
|
|
767
|
-
* { timeout: '48h', filter: { orderId: payload.orderId } },
|
|
768
|
-
* )
|
|
769
|
-
* if (!approval) throw new Error('Approval timed out')
|
|
770
|
-
* await notifyCustomer(approval.approvedBy)
|
|
771
|
-
* ```
|
|
772
|
-
*/
|
|
773
|
-
waitForEvent<T = unknown>(name: string, eventName: string, options?: {
|
|
774
|
-
timeout?: string;
|
|
775
|
-
filter?: Record<string, unknown>;
|
|
776
|
-
}): Promise<T | null>;
|
|
777
|
-
};
|
|
778
|
-
interface NativeSideEffectOutcome<T = unknown> {
|
|
779
|
-
readonly handle?: string;
|
|
780
|
-
readonly result: T;
|
|
781
|
-
}
|
|
782
|
-
interface NativeSideEffectReceipt {
|
|
783
|
-
readonly stepName: string;
|
|
784
|
-
readonly effectKey: string;
|
|
785
|
-
readonly externalService: string;
|
|
786
|
-
readonly idempotencyKey: string;
|
|
787
|
-
readonly handle: string | null;
|
|
788
|
-
readonly result: unknown;
|
|
789
|
-
readonly completedAt: string | null;
|
|
790
|
-
readonly createdAt: string;
|
|
791
|
-
}
|
|
792
|
-
/**
|
|
793
|
-
* A named task definition registered with sylphx.tasks.define().
|
|
794
|
-
*/
|
|
795
|
-
interface NativeTaskDefinition<TPayload = unknown, TResult = unknown> {
|
|
796
|
-
/** Unique task identifier — must match the taskName used in .trigger() */
|
|
797
|
-
name: string;
|
|
798
|
-
/** The task handler function. Runs with stateless-replay semantics. */
|
|
799
|
-
handler: (payload: TPayload, ctx: {
|
|
800
|
-
step: NativeStepContext;
|
|
801
|
-
}) => Promise<TResult>;
|
|
802
|
-
/** Optional task-level configuration */
|
|
803
|
-
options?: {
|
|
804
|
-
/** Cron expression for scheduled tasks, e.g. '0 9 * * 1-5' */
|
|
805
|
-
cron?: string;
|
|
806
|
-
/** Maximum execution attempts (default: 3) */
|
|
807
|
-
maxAttempts?: number;
|
|
808
|
-
/** Execution timeout, e.g. '5m', '1h' */
|
|
809
|
-
timeout?: string;
|
|
810
|
-
};
|
|
811
|
-
}
|
|
812
|
-
/**
|
|
813
|
-
* The shape returned by GET/POST /tasks/:runId in the platform API.
|
|
814
|
-
*/
|
|
815
|
-
type TaskRunDispatchLeaseState = 'active' | 'expired' | 'legacy';
|
|
816
|
-
interface TaskRunDispatchLease {
|
|
817
|
-
/**
|
|
818
|
-
* Customer-safe dispatch claim state. Raw claim tokens and controller
|
|
819
|
-
* identities are intentionally not exposed through the public SDK.
|
|
820
|
-
*/
|
|
821
|
-
state: TaskRunDispatchLeaseState;
|
|
822
|
-
claimedAt: string | null;
|
|
823
|
-
expiresAt: string | null;
|
|
824
|
-
}
|
|
825
|
-
interface TaskRunStatus {
|
|
826
|
-
id: string;
|
|
827
|
-
taskName: string;
|
|
828
|
-
status: 'pending' | 'running' | 'waiting' | 'completed' | 'failed' | 'cancelled';
|
|
829
|
-
payload?: unknown;
|
|
830
|
-
result?: unknown;
|
|
831
|
-
error?: string;
|
|
832
|
-
attempt: number;
|
|
833
|
-
maxAttempts: number;
|
|
834
|
-
scheduledFor?: string;
|
|
835
|
-
startedAt?: string;
|
|
836
|
-
completedAt?: string;
|
|
837
|
-
dispatchLease?: TaskRunDispatchLease | null;
|
|
838
|
-
definition?: {
|
|
839
|
-
id: string;
|
|
840
|
-
version: number | null;
|
|
841
|
-
hash: string | null;
|
|
842
|
-
source: string | null;
|
|
843
|
-
} | null;
|
|
844
|
-
createdAt: string;
|
|
845
|
-
updatedAt: string;
|
|
846
|
-
}
|
|
847
|
-
|
|
848
|
-
/**
|
|
849
|
-
* Tasks Handler
|
|
850
|
-
*
|
|
851
|
-
* Server-side endpoint factory for user task definitions.
|
|
852
|
-
* Implements the Sylphx stateless-replay model:
|
|
853
|
-
*
|
|
854
|
-
* 1. Platform dispatches POST to the app's /api/tasks endpoint.
|
|
855
|
-
* 2. The handler replays from the beginning.
|
|
856
|
-
* 3. Completed steps hit a cache and return immediately.
|
|
857
|
-
* 4. The first uncached step executes, then throws StepCompleteSignal.
|
|
858
|
-
* 5. The platform saves the result and re-dispatches.
|
|
859
|
-
* 6. After all steps complete the handler returns normally.
|
|
860
|
-
*
|
|
861
|
-
* @example
|
|
862
|
-
* ```typescript
|
|
863
|
-
* // app/api/tasks/route.ts
|
|
864
|
-
* import { sylphx } from '@/lib/sylphx'
|
|
865
|
-
*
|
|
866
|
-
* const sendEmail = sylphx.tasks.define('send-email', async (payload, { step }) => {
|
|
867
|
-
* const validated = await step.run('validate', () => validateEmail(payload.to))
|
|
868
|
-
* await step.sleep('cool-down', '5 minutes')
|
|
869
|
-
* return step.run('send', () => sendEmail(validated))
|
|
870
|
-
* })
|
|
871
|
-
*
|
|
872
|
-
* export const { GET, POST } = sylphx.tasks.handler([sendEmail])
|
|
873
|
-
* ```
|
|
874
|
-
*/
|
|
875
|
-
|
|
876
|
-
/**
|
|
877
|
-
* Thrown by step.run() when a step's result is not yet cached.
|
|
878
|
-
* The handler aborts; the platform saves the result and re-dispatches.
|
|
879
|
-
*/
|
|
880
|
-
declare class StepCompleteSignal {
|
|
881
|
-
readonly stepName: string;
|
|
882
|
-
readonly result: unknown;
|
|
883
|
-
readonly _isStepCompleteSignal = true;
|
|
884
|
-
constructor(stepName: string, result: unknown);
|
|
885
|
-
}
|
|
886
|
-
declare class StepSideEffectStartSignal {
|
|
887
|
-
readonly stepName: string;
|
|
888
|
-
readonly effectKey: string;
|
|
889
|
-
readonly externalService: string;
|
|
890
|
-
readonly _isStepSideEffectStartSignal = true;
|
|
891
|
-
constructor(stepName: string, effectKey: string, externalService: string);
|
|
892
|
-
}
|
|
893
|
-
declare class StepSideEffectCompleteSignal {
|
|
894
|
-
readonly stepName: string;
|
|
895
|
-
readonly effectKey: string;
|
|
896
|
-
readonly handle: string | undefined;
|
|
897
|
-
readonly result: unknown;
|
|
898
|
-
readonly _isStepSideEffectCompleteSignal = true;
|
|
899
|
-
constructor(stepName: string, effectKey: string, handle: string | undefined, result: unknown);
|
|
900
|
-
}
|
|
901
|
-
/**
|
|
902
|
-
* Thrown by step.sleep() when the sleep is not yet resolved.
|
|
903
|
-
* The handler aborts; the platform waits and re-dispatches.
|
|
904
|
-
*/
|
|
905
|
-
declare class StepSleepSignal {
|
|
906
|
-
readonly stepName: string;
|
|
907
|
-
readonly duration: string;
|
|
908
|
-
readonly _isStepSleepSignal = true;
|
|
909
|
-
constructor(stepName: string, duration: string);
|
|
910
|
-
}
|
|
911
|
-
/**
|
|
912
|
-
* Build the step proxy for a single handler invocation.
|
|
913
|
-
*
|
|
914
|
-
* @param completedSteps Map of stepName → cached result
|
|
915
|
-
* @param resolvedWaits Map of stepName → wait result (sleep = undefined, event = event payload)
|
|
916
|
-
*/
|
|
917
|
-
declare function createStepContext(completedSteps: Map<string, unknown>, resolvedWaits: Map<string, unknown>, sideEffectReceipts?: Map<string, NativeSideEffectReceipt>): NativeStepContext;
|
|
918
|
-
/**
|
|
919
|
-
* Constant-time HMAC-SHA256 signature verification.
|
|
920
|
-
* Accepts both raw hex and 'sha256={hex}' formats.
|
|
921
|
-
*/
|
|
922
|
-
declare function verifySignature(body: string, signature: string, secret: string): boolean;
|
|
923
|
-
interface TasksHandlerOptions {
|
|
924
|
-
/** Platform signing secret. If not set, signature verification is skipped. */
|
|
925
|
-
signingSecret?: string;
|
|
926
|
-
}
|
|
927
|
-
interface TasksHandlerResult {
|
|
928
|
-
GET: (req: Request) => Promise<Response>;
|
|
929
|
-
POST: (req: Request) => Promise<Response>;
|
|
930
|
-
}
|
|
931
|
-
/**
|
|
932
|
-
* Create a Next.js-compatible route handler for a list of task definitions.
|
|
933
|
-
*
|
|
934
|
-
* @example
|
|
935
|
-
* ```typescript
|
|
936
|
-
* export const { GET, POST } = createTasksHandler([sendEmail, processOrder])
|
|
937
|
-
* ```
|
|
938
|
-
*/
|
|
939
|
-
declare function createTasksHandler(taskDefs: NativeTaskDefinition[], options?: TasksHandlerOptions): TasksHandlerResult;
|
|
940
|
-
|
|
941
|
-
/**
|
|
942
|
-
* Task API Functions
|
|
943
|
-
*
|
|
944
|
-
* Standalone functions for scheduling tasks and managing crons via the Sylphx API.
|
|
945
|
-
* These are backward-compat exports preserved from the pre-refactor tasks.ts.
|
|
946
|
-
*/
|
|
947
|
-
|
|
948
|
-
interface TaskInput {
|
|
949
|
-
/** Callback URL to call when task executes */
|
|
950
|
-
callbackUrl: string;
|
|
951
|
-
/** Task name/type */
|
|
952
|
-
name?: string;
|
|
953
|
-
/** Task type for categorization */
|
|
954
|
-
type?: string;
|
|
955
|
-
/** Task payload sent to callback */
|
|
956
|
-
payload?: Record<string, unknown>;
|
|
957
|
-
/** HTTP method for callback (default: POST) */
|
|
958
|
-
method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
|
|
959
|
-
/** Additional headers for callback */
|
|
960
|
-
headers?: Record<string, string>;
|
|
961
|
-
/** Delay before executing (in seconds, max 604800 = 7 days) */
|
|
962
|
-
delay?: number;
|
|
963
|
-
/** Schedule for later (ISO timestamp) */
|
|
964
|
-
scheduledFor?: string;
|
|
965
|
-
/** Number of retries on failure (0-5, default: 3) */
|
|
966
|
-
retries?: number;
|
|
967
|
-
/** Request timeout in seconds (1-300, default: 30) */
|
|
968
|
-
timeout?: number;
|
|
969
|
-
/**
|
|
970
|
-
* Idempotency key for safe retries (Stripe/Inngest pattern).
|
|
971
|
-
*
|
|
972
|
-
* When provided, prevents duplicate task execution if the same
|
|
973
|
-
* key is used within a 24-hour window.
|
|
974
|
-
*/
|
|
975
|
-
idempotencyKey?: string;
|
|
976
|
-
}
|
|
977
|
-
interface TaskResult {
|
|
978
|
-
/** Task ID */
|
|
979
|
-
taskId: string;
|
|
980
|
-
/** Scheduled execution time */
|
|
981
|
-
scheduledFor?: string;
|
|
982
|
-
}
|
|
983
|
-
interface TaskStatus {
|
|
984
|
-
id: string;
|
|
985
|
-
name?: string;
|
|
986
|
-
status: 'pending' | 'queued' | 'running' | 'completed' | 'failed' | 'cancelled';
|
|
987
|
-
payload?: Record<string, unknown>;
|
|
988
|
-
result?: unknown;
|
|
989
|
-
error?: string;
|
|
990
|
-
createdAt: string;
|
|
991
|
-
queuedAt?: string;
|
|
992
|
-
startedAt?: string;
|
|
993
|
-
completedAt?: string;
|
|
994
|
-
}
|
|
995
|
-
type TaskRunDlqEntry = DurableWorkTaskDlqEntry;
|
|
996
|
-
type TaskRunDlqListResponse = DurableWorkTaskDlqListResponse;
|
|
997
|
-
type TaskRunReplayInput = DurableWorkTaskReplayRequestBody;
|
|
998
|
-
type TaskRunReplayResponse = DurableWorkTaskReplayResponse;
|
|
999
|
-
interface ListTaskRunDlqOptions {
|
|
1000
|
-
/** Filter DLQ entries to a single task name. */
|
|
1001
|
-
taskName?: string;
|
|
1002
|
-
/** Page size. Platform caps this at 100. */
|
|
1003
|
-
limit?: number;
|
|
1004
|
-
/** Page offset. */
|
|
1005
|
-
offset?: number;
|
|
1006
|
-
}
|
|
1007
|
-
interface TaskCallbackTargetInput extends DurableWorkUpsertCallbackTargetRequestBody {
|
|
1008
|
-
/** Stable low-cardinality target key, e.g. `spiron.task-callback.default`. */
|
|
1009
|
-
targetKey: string;
|
|
1010
|
-
}
|
|
1011
|
-
type TaskCallbackTarget = DurableWorkUpsertCallbackTargetResponse;
|
|
1012
|
-
interface CronInput {
|
|
1013
|
-
/** Callback URL to call on each cron trigger */
|
|
1014
|
-
callbackUrl: string;
|
|
1015
|
-
/** Cron expression (e.g., '0 0 * * *' for daily at midnight) */
|
|
1016
|
-
cron: string;
|
|
1017
|
-
/** Task name (required, max 200 chars) */
|
|
1018
|
-
name: string;
|
|
1019
|
-
/** Task type for categorization */
|
|
1020
|
-
type?: string;
|
|
1021
|
-
/** Task payload sent to callback */
|
|
1022
|
-
payload?: Record<string, unknown>;
|
|
1023
|
-
/** HTTP method for callback (default: POST) */
|
|
1024
|
-
method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
|
|
1025
|
-
/** Additional headers for callback */
|
|
1026
|
-
headers?: Record<string, string>;
|
|
1027
|
-
/** Number of retries on failure (0-5, default: 3) */
|
|
1028
|
-
retries?: number;
|
|
1029
|
-
/** Start in paused state */
|
|
1030
|
-
paused?: boolean;
|
|
1031
|
-
/**
|
|
1032
|
-
* Idempotency key for safe cron creation.
|
|
1033
|
-
*
|
|
1034
|
-
* When provided, prevents duplicate cron schedule creation if
|
|
1035
|
-
* the same key is used.
|
|
1036
|
-
*/
|
|
1037
|
-
idempotencyKey?: string;
|
|
1038
|
-
}
|
|
1039
|
-
/**
|
|
1040
|
-
* Schedule a one-time task for execution
|
|
1041
|
-
*/
|
|
1042
|
-
declare function scheduleTask(config: SylphxConfig, input: TaskInput): Promise<TaskResult>;
|
|
1043
|
-
/**
|
|
1044
|
-
* Get a task's status by ID
|
|
1045
|
-
*/
|
|
1046
|
-
declare function getTask(config: SylphxConfig, taskId: string): Promise<TaskStatus>;
|
|
1047
|
-
/** The durable step ledger entry surfaced by `getTaskRun`. */
|
|
1048
|
-
type TaskRunStep = DurableWorkTaskRunStep;
|
|
1049
|
-
/** The full durable task-run view surfaced by `getTaskRun`. */
|
|
1050
|
-
type TaskRunDetail = DurableWorkGetTaskRunResponse;
|
|
1051
|
-
/**
|
|
1052
|
-
* Get the full durable view of a task run, including the step ledger.
|
|
1053
|
-
*
|
|
1054
|
-
* Each step carries an `executorToken` — the opaque-but-stable identity of the
|
|
1055
|
-
* worker that materialized it. The token changes across a pod-kill resume, so
|
|
1056
|
-
* comparing executor tokens across steps is the evidence that a run resumed on
|
|
1057
|
-
* a different worker (durable HA). `status` reads `executed` for every recorded
|
|
1058
|
-
* step (a step is persisted once, on the dispatch that executed it; later
|
|
1059
|
-
* dispatches replay it from cache and record nothing). The platform never
|
|
1060
|
-
* returns step result payloads — only identity, executor, and timing.
|
|
1061
|
-
*/
|
|
1062
|
-
declare function getTaskRun(config: SylphxConfig, runId: string): Promise<TaskRunDetail>;
|
|
1063
|
-
/**
|
|
1064
|
-
* Cancel a pending, running, waiting, or failed task run.
|
|
1065
|
-
*/
|
|
1066
|
-
declare function cancelTask(config: SylphxConfig, taskId: string): Promise<boolean>;
|
|
1067
|
-
/**
|
|
1068
|
-
* List terminal failed task runs eligible for customer-safe replay.
|
|
1069
|
-
*/
|
|
1070
|
-
declare function listTaskRunDlq(config: SylphxConfig, options?: ListTaskRunDlqOptions): Promise<TaskRunDlqListResponse>;
|
|
1071
|
-
/**
|
|
1072
|
-
* Replay a terminal failed task run with an explicit idempotency key.
|
|
1073
|
-
*/
|
|
1074
|
-
declare function replayTaskRun(config: SylphxConfig, taskId: string, input: TaskRunReplayInput): Promise<TaskRunReplayResponse>;
|
|
1075
|
-
/**
|
|
1076
|
-
* List tasks with optional filters
|
|
1077
|
-
*/
|
|
1078
|
-
declare function listTasks(config: SylphxConfig, options?: {
|
|
1079
|
-
status?: TaskStatus['status'];
|
|
1080
|
-
limit?: number;
|
|
1081
|
-
offset?: number;
|
|
1082
|
-
}): Promise<{
|
|
1083
|
-
tasks: TaskStatus[];
|
|
1084
|
-
total: number;
|
|
1085
|
-
}>;
|
|
1086
|
-
/**
|
|
1087
|
-
* Upsert a reusable task callback target.
|
|
1088
|
-
*
|
|
1089
|
-
* This is the P5 callback-target SSOT path: endpoint rotation is one idempotent
|
|
1090
|
-
* PUT instead of re-registering every cron schedule. New schedules can bind to
|
|
1091
|
-
* `targetKey` once Platform's schedule-binding slice is enabled.
|
|
1092
|
-
*/
|
|
1093
|
-
declare function upsertTaskCallbackTarget(config: SylphxConfig, input: TaskCallbackTargetInput): Promise<TaskCallbackTarget>;
|
|
1094
|
-
/**
|
|
1095
|
-
* Create a recurring cron task
|
|
1096
|
-
*/
|
|
1097
|
-
declare function createCron(config: SylphxConfig, input: CronInput): Promise<CronSchedule>;
|
|
1098
|
-
/**
|
|
1099
|
-
* Pause a cron schedule
|
|
1100
|
-
*/
|
|
1101
|
-
declare function pauseCron(config: SylphxConfig, scheduleId: string): Promise<boolean>;
|
|
1102
|
-
/**
|
|
1103
|
-
* Resume a cron schedule
|
|
1104
|
-
*/
|
|
1105
|
-
declare function resumeCron(config: SylphxConfig, scheduleId: string): Promise<boolean>;
|
|
1106
|
-
/**
|
|
1107
|
-
* Delete a cron schedule
|
|
1108
|
-
*/
|
|
1109
|
-
declare function deleteCron(config: SylphxConfig, scheduleId: string): Promise<boolean>;
|
|
1110
|
-
|
|
1111
|
-
/**
|
|
1112
|
-
* Tasks Legacy Client
|
|
1113
|
-
*
|
|
1114
|
-
* @deprecated Use task() / taskStub() / serve() from the tasks module instead.
|
|
1115
|
-
*
|
|
1116
|
-
* This file preserves backward compatibility for consumers of the old TasksClient API.
|
|
1117
|
-
* The TasksClient class is unchanged — it is imported by ./index.ts under the 'legacy' export group.
|
|
1118
|
-
*/
|
|
1119
|
-
|
|
1120
|
-
/**
|
|
1121
|
-
* Jobs Client
|
|
1122
|
-
*
|
|
1123
|
-
* Client for scheduling jobs and workflows via the Sylph API.
|
|
1124
|
-
*/
|
|
1125
|
-
declare class TasksClient {
|
|
1126
|
-
private config;
|
|
1127
|
-
private eventHandlers;
|
|
1128
|
-
constructor(config: TasksConfig);
|
|
1129
|
-
/**
|
|
1130
|
-
* Schedule a job for execution
|
|
1131
|
-
*/
|
|
1132
|
-
schedule<TPayload extends JobPayload = JobPayload>(type: string, payload: TPayload, options?: JobOptions): Promise<Job<TPayload>>;
|
|
1133
|
-
/**
|
|
1134
|
-
* Schedule a job with delay
|
|
1135
|
-
*/
|
|
1136
|
-
scheduleDelayed<TPayload extends JobPayload = JobPayload>(type: string, payload: TPayload, delay: number | Date, options?: JobOptions): Promise<Job<TPayload>>;
|
|
1137
|
-
/**
|
|
1138
|
-
* Schedule a recurring job (cron)
|
|
1139
|
-
*/
|
|
1140
|
-
scheduleCron<TPayload extends JobPayload = JobPayload>(type: string, payload: TPayload, cron: string | CronSchedule, options?: JobOptions): Promise<ScheduledJob>;
|
|
1141
|
-
/**
|
|
1142
|
-
* Get a job by ID
|
|
1143
|
-
*/
|
|
1144
|
-
getJob<TPayload extends JobPayload = JobPayload, TResult = JobResult>(jobId: string): Promise<Job<TPayload, TResult> | null>;
|
|
1145
|
-
/**
|
|
1146
|
-
* Cancel a job
|
|
1147
|
-
*/
|
|
1148
|
-
cancelJob(jobId: string): Promise<boolean>;
|
|
1149
|
-
/**
|
|
1150
|
-
* Retry a failed job
|
|
1151
|
-
*/
|
|
1152
|
-
retryJob(jobId: string): Promise<Job>;
|
|
1153
|
-
/**
|
|
1154
|
-
* List jobs with filters
|
|
1155
|
-
*/
|
|
1156
|
-
listJobs(options?: {
|
|
1157
|
-
type?: string;
|
|
1158
|
-
status?: string;
|
|
1159
|
-
limit?: number;
|
|
1160
|
-
offset?: number;
|
|
1161
|
-
}): Promise<{
|
|
1162
|
-
jobs: Job[];
|
|
1163
|
-
total: number;
|
|
1164
|
-
}>;
|
|
1165
|
-
/**
|
|
1166
|
-
* List DLQ entries
|
|
1167
|
-
*/
|
|
1168
|
-
listDLQ(options?: {
|
|
1169
|
-
limit?: number;
|
|
1170
|
-
offset?: number;
|
|
1171
|
-
}): Promise<{
|
|
1172
|
-
entries: Job[];
|
|
1173
|
-
total: number;
|
|
1174
|
-
}>;
|
|
1175
|
-
/**
|
|
1176
|
-
* Retry a DLQ entry
|
|
1177
|
-
*/
|
|
1178
|
-
retryDLQEntry(entryId: string): Promise<Job>;
|
|
1179
|
-
/**
|
|
1180
|
-
* Discard a DLQ entry
|
|
1181
|
-
*/
|
|
1182
|
-
discardDLQEntry(entryId: string): Promise<boolean>;
|
|
1183
|
-
/**
|
|
1184
|
-
* Start a workflow
|
|
1185
|
-
*/
|
|
1186
|
-
startWorkflow<TInput extends JobPayload = JobPayload, TOutput = JobResult>(workflow: WorkflowDefinition<TInput, TOutput>, input: TInput, options?: JobOptions): Promise<Workflow<TInput, TOutput>>;
|
|
1187
|
-
/**
|
|
1188
|
-
* Get a workflow by ID
|
|
1189
|
-
*/
|
|
1190
|
-
getWorkflow<TInput extends JobPayload = JobPayload, TOutput = JobResult>(workflowId: string): Promise<Workflow<TInput, TOutput> | null>;
|
|
1191
|
-
/**
|
|
1192
|
-
* Cancel a workflow
|
|
1193
|
-
*/
|
|
1194
|
-
cancelWorkflow(workflowId: string): Promise<boolean>;
|
|
1195
|
-
/**
|
|
1196
|
-
* List workflows
|
|
1197
|
-
*/
|
|
1198
|
-
listWorkflows(options?: {
|
|
1199
|
-
type?: string;
|
|
1200
|
-
status?: string;
|
|
1201
|
-
limit?: number;
|
|
1202
|
-
offset?: number;
|
|
1203
|
-
}): Promise<{
|
|
1204
|
-
workflows: Workflow[];
|
|
1205
|
-
total: number;
|
|
1206
|
-
}>;
|
|
1207
|
-
/**
|
|
1208
|
-
* List scheduled jobs
|
|
1209
|
-
*/
|
|
1210
|
-
listScheduledJobs(options?: {
|
|
1211
|
-
status?: 'active' | 'paused' | 'completed';
|
|
1212
|
-
limit?: number;
|
|
1213
|
-
offset?: number;
|
|
1214
|
-
}): Promise<{
|
|
1215
|
-
scheduled: ScheduledJob[];
|
|
1216
|
-
total: number;
|
|
1217
|
-
}>;
|
|
1218
|
-
/**
|
|
1219
|
-
* Pause a scheduled job
|
|
1220
|
-
*/
|
|
1221
|
-
pauseScheduledJob(scheduleId: string): Promise<ScheduledJob>;
|
|
1222
|
-
/**
|
|
1223
|
-
* Resume a scheduled job
|
|
1224
|
-
*/
|
|
1225
|
-
resumeScheduledJob(scheduleId: string): Promise<ScheduledJob>;
|
|
1226
|
-
/**
|
|
1227
|
-
* Delete a scheduled job
|
|
1228
|
-
*/
|
|
1229
|
-
deleteScheduledJob(scheduleId: string): Promise<boolean>;
|
|
1230
|
-
/**
|
|
1231
|
-
* Subscribe to job events
|
|
1232
|
-
*/
|
|
1233
|
-
onJobEvent(handler: (event: JobEvent) => void): () => void;
|
|
1234
|
-
/**
|
|
1235
|
-
* Subscribe to workflow events
|
|
1236
|
-
*/
|
|
1237
|
-
onWorkflowEvent(handler: (event: WorkflowEvent) => void): () => void;
|
|
1238
|
-
private fetch;
|
|
1239
|
-
/**
|
|
1240
|
-
* Define a named task handler.
|
|
1241
|
-
*
|
|
1242
|
-
* Returns a TaskDefinition that can be passed to tasks.handler([...]).
|
|
1243
|
-
* The handler runs with stateless-replay semantics — each dispatch
|
|
1244
|
-
* replays from the beginning with completed steps injected from cache.
|
|
1245
|
-
*
|
|
1246
|
-
* @example
|
|
1247
|
-
* ```typescript
|
|
1248
|
-
* const sendEmail = sylphx.tasks.define('send-email', async (payload, { step }) => {
|
|
1249
|
-
* const validated = await step.run('validate', () => validate(payload.to))
|
|
1250
|
-
* await step.sleep('cool-down', '5 minutes')
|
|
1251
|
-
* return step.run('send', () => send(validated))
|
|
1252
|
-
* })
|
|
1253
|
-
* ```
|
|
1254
|
-
*/
|
|
1255
|
-
define<TPayload = unknown, TResult = unknown>(name: string, fn: (payload: TPayload, ctx: {
|
|
1256
|
-
step: NativeStepContext;
|
|
1257
|
-
}) => Promise<TResult>, options?: NativeTaskDefinition<TPayload, TResult>['options']): NativeTaskDefinition<TPayload, TResult>;
|
|
1258
|
-
/**
|
|
1259
|
-
* Create a Next.js / Web-API-compatible route handler for a set of task definitions.
|
|
1260
|
-
*
|
|
1261
|
-
* @example
|
|
1262
|
-
* ```typescript
|
|
1263
|
-
* // app/api/tasks/route.ts
|
|
1264
|
-
* export const { GET, POST } = sylphx.tasks.handler([sendEmail, processOrder])
|
|
1265
|
-
* ```
|
|
1266
|
-
*/
|
|
1267
|
-
handler(tasks: NativeTaskDefinition[], handlerOptions?: TasksHandlerOptions): TasksHandlerResult;
|
|
1268
|
-
/**
|
|
1269
|
-
* Trigger a task run on the Sylphx platform.
|
|
1270
|
-
*
|
|
1271
|
-
* The platform will dispatch the task to the app's handler endpoint.
|
|
1272
|
-
* Returns the taskRunId which can be used to poll status.
|
|
1273
|
-
*/
|
|
1274
|
-
trigger<TPayload = unknown>(taskName: string, payload?: TPayload, options?: {
|
|
1275
|
-
delay?: string;
|
|
1276
|
-
scheduledFor?: Date;
|
|
1277
|
-
idempotencyKey?: string;
|
|
1278
|
-
maxAttempts?: number;
|
|
1279
|
-
}): Promise<{
|
|
1280
|
-
taskRunId: string;
|
|
1281
|
-
status: string;
|
|
1282
|
-
createdAt: string;
|
|
1283
|
-
}>;
|
|
1284
|
-
/**
|
|
1285
|
-
* Fetch the current status of a task run.
|
|
1286
|
-
*/
|
|
1287
|
-
getTaskRun(taskRunId: string): Promise<TaskRunStatus>;
|
|
1288
|
-
/**
|
|
1289
|
-
* Cancel a pending, running, waiting, or failed task run.
|
|
1290
|
-
*/
|
|
1291
|
-
cancelTaskRun(taskRunId: string): Promise<boolean>;
|
|
1292
|
-
/**
|
|
1293
|
-
* List task runs for the current project/environment.
|
|
1294
|
-
*/
|
|
1295
|
-
listTaskRuns(options?: {
|
|
1296
|
-
taskName?: string;
|
|
1297
|
-
status?: TaskRunStatus['status'];
|
|
1298
|
-
limit?: number;
|
|
1299
|
-
offset?: number;
|
|
1300
|
-
}): Promise<{
|
|
1301
|
-
runs: TaskRunStatus[];
|
|
1302
|
-
total: number;
|
|
1303
|
-
}>;
|
|
1304
|
-
}
|
|
1305
|
-
/**
|
|
1306
|
-
* Create a Jobs client instance
|
|
1307
|
-
*/
|
|
1308
|
-
declare function createTasksClient(config: TasksConfig): TasksClient;
|
|
1309
|
-
/** @deprecated Use TasksClient */
|
|
1310
|
-
declare class JobsClient extends TasksClient {
|
|
1311
|
-
}
|
|
1312
|
-
/** @deprecated Use createTasksClient */
|
|
1313
|
-
declare const createJobsClient: typeof createTasksClient;
|
|
1314
|
-
|
|
1315
|
-
/**
|
|
1316
|
-
* Workflow Builder DSL
|
|
1317
|
-
*
|
|
1318
|
-
* Fluent API for defining workflows with type safety.
|
|
1319
|
-
*
|
|
1320
|
-
* @example
|
|
1321
|
-
* ```typescript
|
|
1322
|
-
* const workflow = createWorkflow('onboarding')
|
|
1323
|
-
* .name('User Onboarding')
|
|
1324
|
-
* .step('create-account', task('create-user'))
|
|
1325
|
-
* .step('send-welcome', task('send-email', (ctx) => ({
|
|
1326
|
-
* to: ctx.results['create-account'].email,
|
|
1327
|
-
* template: 'welcome',
|
|
1328
|
-
* })))
|
|
1329
|
-
* .step('wait-verification', wait(24 * 60 * 60 * 1000))
|
|
1330
|
-
* .step('check-verified', conditional(
|
|
1331
|
-
* (ctx) => ctx.results['create-account'].verified,
|
|
1332
|
-
* [task('activate-account')],
|
|
1333
|
-
* [task('send-reminder')]
|
|
1334
|
-
* ))
|
|
1335
|
-
* .build()
|
|
1336
|
-
* ```
|
|
1337
|
-
*/
|
|
1338
|
-
|
|
1339
|
-
/**
|
|
1340
|
-
* Create a new workflow builder
|
|
1341
|
-
*/
|
|
1342
|
-
declare function createWorkflow<TInput extends TaskPayload = TaskPayload, TOutput = TaskResult$1>(type: string): WorkflowBuilder<TInput, TOutput>;
|
|
1343
|
-
/**
|
|
1344
|
-
* Workflow Builder
|
|
1345
|
-
*
|
|
1346
|
-
* Fluent API for building workflow definitions.
|
|
1347
|
-
*/
|
|
1348
|
-
declare class WorkflowBuilder<TInput extends TaskPayload = TaskPayload, TOutput = TaskResult$1> {
|
|
1349
|
-
private definition;
|
|
1350
|
-
private _steps;
|
|
1351
|
-
constructor(type: string);
|
|
1352
|
-
/**
|
|
1353
|
-
* Set workflow name
|
|
1354
|
-
*/
|
|
1355
|
-
name(name: string): this;
|
|
1356
|
-
/**
|
|
1357
|
-
* Set workflow description
|
|
1358
|
-
*/
|
|
1359
|
-
description(description: string): this;
|
|
1360
|
-
/**
|
|
1361
|
-
* Set default options
|
|
1362
|
-
*/
|
|
1363
|
-
defaults(options: WorkflowOptions): this;
|
|
1364
|
-
/**
|
|
1365
|
-
* Add a step to the workflow
|
|
1366
|
-
*/
|
|
1367
|
-
step(name: string, step: WorkflowStep): this;
|
|
1368
|
-
/**
|
|
1369
|
-
* Add multiple steps
|
|
1370
|
-
*/
|
|
1371
|
-
steps(...steps: Array<[string, WorkflowStep]>): this;
|
|
1372
|
-
/**
|
|
1373
|
-
* Add on complete callback
|
|
1374
|
-
*/
|
|
1375
|
-
onComplete(callback: (result: TOutput) => void): this;
|
|
1376
|
-
/**
|
|
1377
|
-
* Add on error callback
|
|
1378
|
-
*/
|
|
1379
|
-
onError(callback: (error: Error, step: string) => void): this;
|
|
1380
|
-
/**
|
|
1381
|
-
* Build the workflow definition
|
|
1382
|
-
*/
|
|
1383
|
-
build(): WorkflowDefinition<TInput, TOutput>;
|
|
1384
|
-
}
|
|
1385
|
-
/**
|
|
1386
|
-
* Create a job step
|
|
1387
|
-
*/
|
|
1388
|
-
declare function task(taskType: string, payload?: TaskPayload | ((context: StepContext) => TaskPayload), options?: TaskOptions): TaskStep;
|
|
1389
|
-
/**
|
|
1390
|
-
* Create a conditional step
|
|
1391
|
-
*/
|
|
1392
|
-
declare function conditional(condition: (context: StepContext) => boolean, thenSteps: WorkflowStep[], elseSteps?: WorkflowStep[]): ConditionalStep;
|
|
1393
|
-
/**
|
|
1394
|
-
* Create a parallel step
|
|
1395
|
-
*/
|
|
1396
|
-
declare function parallel(steps: WorkflowStep[], options?: {
|
|
1397
|
-
waitFor?: 'all' | 'any';
|
|
1398
|
-
maxConcurrency?: number;
|
|
1399
|
-
}): ParallelStep;
|
|
1400
|
-
/**
|
|
1401
|
-
* Create a loop step
|
|
1402
|
-
*/
|
|
1403
|
-
declare function loop(items: unknown[] | ((context: StepContext) => unknown[]), step: WorkflowStep, options?: {
|
|
1404
|
-
maxConcurrency?: number;
|
|
1405
|
-
breakIf?: (item: unknown, index: number, context: StepContext) => boolean;
|
|
1406
|
-
}): LoopStep;
|
|
1407
|
-
/**
|
|
1408
|
-
* Create a wait step
|
|
1409
|
-
*/
|
|
1410
|
-
declare function wait(until: number | Date | ((context: StepContext) => number | Date)): WaitStep;
|
|
1411
|
-
/**
|
|
1412
|
-
* Create a sub-workflow step
|
|
1413
|
-
*/
|
|
1414
|
-
declare function subworkflow(workflowType: string, payload?: TaskPayload | ((context: StepContext) => TaskPayload), options?: WorkflowOptions): SubworkflowStep;
|
|
1415
|
-
/**
|
|
1416
|
-
* Create a job step with conditional execution
|
|
1417
|
-
*/
|
|
1418
|
-
declare function jobIf(condition: (context: StepContext) => boolean, taskType: string, payload?: TaskPayload | ((context: StepContext) => TaskPayload), options?: TaskOptions): TaskStep;
|
|
1419
|
-
/**
|
|
1420
|
-
* Create a delay step (alias for wait)
|
|
1421
|
-
*/
|
|
1422
|
-
declare function delay(ms: number): WaitStep;
|
|
1423
|
-
/**
|
|
1424
|
-
* Create a step that sleeps until a specific time
|
|
1425
|
-
*/
|
|
1426
|
-
declare function sleepUntil(date: Date | ((context: StepContext) => Date)): WaitStep;
|
|
1427
|
-
/**
|
|
1428
|
-
* Create a retry wrapper for a step
|
|
1429
|
-
*/
|
|
1430
|
-
declare function withRetry(step: TaskStep, options: {
|
|
1431
|
-
maxRetries: number;
|
|
1432
|
-
retryDelay?: number;
|
|
1433
|
-
}): TaskStep;
|
|
1434
|
-
/**
|
|
1435
|
-
* Create a timeout wrapper for a step
|
|
1436
|
-
*/
|
|
1437
|
-
declare function withTimeout(step: TaskStep, timeoutMs: number): TaskStep;
|
|
1438
|
-
/**
|
|
1439
|
-
* Compose multiple workflows sequentially
|
|
1440
|
-
*/
|
|
1441
|
-
declare function sequence(...workflows: WorkflowDefinition[]): WorkflowStep[];
|
|
1442
|
-
/**
|
|
1443
|
-
* Fan-out pattern: Execute same job for multiple items
|
|
1444
|
-
*/
|
|
1445
|
-
declare function fanOut(items: unknown[] | ((context: StepContext) => unknown[]), taskType: string, payloadMapper: (item: unknown, index: number) => TaskPayload, options?: {
|
|
1446
|
-
maxConcurrency?: number;
|
|
1447
|
-
}): LoopStep;
|
|
1448
|
-
/**
|
|
1449
|
-
* Saga pattern: Define compensating actions for each step
|
|
1450
|
-
*/
|
|
1451
|
-
interface SagaStep {
|
|
1452
|
-
forward: TaskStep;
|
|
1453
|
-
compensate: TaskStep;
|
|
1454
|
-
}
|
|
1455
|
-
declare function saga(steps: SagaStep[]): WorkflowStep[];
|
|
1456
|
-
/**
|
|
1457
|
-
* Validate a workflow definition
|
|
1458
|
-
*/
|
|
1459
|
-
declare function validateWorkflow(workflow: WorkflowDefinition): string[];
|
|
1460
|
-
/** @deprecated Use task() instead */
|
|
1461
|
-
declare const job: typeof task;
|
|
1462
|
-
|
|
1463
|
-
export { type BatchHandle, type ConditionalStep, type CronInput, type CronSchedule, DEFAULT_JOBS_CONFIG, DEFAULT_RETRY_DELAYS, type DLQOptions, type Job, type JobContext, type JobDefinition, type JobEvent, type JobOptions, type JobPayload, type JobPriority, type JobResult, type JobStatus, type JobStep, JobsClient, type TasksConfig as JobsConfig, type ListTaskRunDlqOptions, type LoopStep, type NativeSideEffectOutcome, type NativeSideEffectReceipt, type NativeStepContext, type NativeTaskDefinition, type ParallelStep, type RetryDelayStrategy, type RunHandle, type SagaStep, type ScheduledJob, type ServeOptions, type ServeResult, StepCompleteSignal, type StepContext, StepSideEffectCompleteSignal, StepSideEffectStartSignal, StepSleepSignal, type SubworkflowStep, type TaskCallbackTarget, type TaskCallbackTargetInput, type TaskConfig, type TaskDefinition, type TaskHandle, type TaskInput, type TaskMachineSize, type TaskResult, type TaskRunContext, type TaskRunDetail, type TaskRunDlqEntry, type TaskRunDlqListResponse, type TaskRunReplayInput, type TaskRunReplayResponse, type TaskRunStatus, type TaskRunStatus as TaskRunStatusLegacy, type TaskRunStep, type TaskStatus, type TaskTriggerConfig, TasksClient, type TasksConfig, type TasksHandlerOptions, type TasksHandlerResult, type TriggerOptions, type WaitStep, type Workflow, WorkflowBuilder, type WorkflowDefinition, type WorkflowEvent, type WorkflowOptions, type WorkflowStep, cancelTask, conditional, createCron, createJobsClient, createStepContext, createTasksClient, createTasksHandler, createWorkflow, delay, deleteCron, fanOut, getTask, getTaskRun, job, jobIf, listTaskRunDlq, listTasks, loop, parallel, pauseCron, replayTaskRun, resumeCron, saga, scheduleTask, sequence, serve, sleepUntil, subworkflow, task$1 as task, taskStub, upsertTaskCallbackTarget, validateWorkflow, verifySignature, wait, withRetry, withTimeout };
|