@trigger.dev/sdk 3.2.2 → 3.3.1

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.
@@ -1,8 +1,8 @@
1
1
  import { SerializableJson } from "@trigger.dev/core";
2
- import { ApiRequestOptions, InitOutput, Queue, QueueOptions, SubtaskUnwrapError, TaskRunContext, TaskRunPromise } from "@trigger.dev/core/v3";
2
+ import { ApiRequestOptions, InitOutput, Queue, QueueOptions, SubtaskUnwrapError, TaskRunContext, TaskRunPromise, TaskFromIdentifier } from "@trigger.dev/core/v3";
3
3
  import { PollOptions } from "./runs.js";
4
- import type { AnyRunHandle, AnyTask, BatchItem, BatchResult, BatchRunHandle, BatchRunHandleFromTypes, InferRunTypes, RetrieveRunResult, RunHandle, RunHandleFromTypes, RunHandleOutput, RunHandlePayload, Task, TaskBatchOutputHandle, TaskIdentifier, TaskOptions, TaskOutput, TaskOutputHandle, TaskPayload, TaskRunOptions, TaskRunResult, TaskSchema, TaskWithSchema, TaskWithSchemaOptions, TaskWithToolOptions, ToolTask, ToolTaskParameters, TriggerApiRequestOptions } from "@trigger.dev/core/v3";
5
- export type { AnyRunHandle, AnyTask, BatchItem, BatchResult, BatchRunHandle, Queue, RunHandle, RunHandleOutput, RunHandlePayload, SerializableJson, Task, TaskBatchOutputHandle, TaskIdentifier, TaskOptions, TaskOutput, TaskOutputHandle, TaskPayload, TaskRunOptions, TaskRunResult, };
4
+ import type { AnyRunHandle, AnyTask, BatchByIdAndWaitItem, BatchByTaskAndWaitItem, BatchByIdItem, BatchByTaskItem, BatchByTaskResult, BatchByIdResult, BatchItem, BatchResult, BatchRunHandle, BatchRunHandleFromTypes, BatchTasksRunHandleFromTypes, BatchTriggerOptions, InferRunTypes, RetrieveRunResult, RunHandle, RunHandleFromTypes, RunHandleOutput, RunHandlePayload, Task, TaskBatchOutputHandle, TaskIdentifier, TaskOptions, TaskOutput, TaskOutputHandle, TaskPayload, TaskRunResult, TaskSchema, TaskWithSchema, TaskWithSchemaOptions, TaskWithToolOptions, ToolTask, ToolTaskParameters, TriggerAndWaitOptions, TriggerApiRequestOptions, TriggerOptions } from "@trigger.dev/core/v3";
5
+ export type { AnyRunHandle, AnyTask, BatchItem, BatchResult, BatchRunHandle, BatchTriggerOptions, Queue, RunHandle, RunHandleOutput, RunHandlePayload, SerializableJson, Task, TaskBatchOutputHandle, TaskIdentifier, TaskOptions, TaskOutput, TaskOutputHandle, TaskPayload, TaskRunResult, TriggerOptions, TaskFromIdentifier, };
6
6
  export { SubtaskUnwrapError, TaskRunPromise };
7
7
  export type Context = TaskRunContext;
8
8
  export declare function queue(options: {
@@ -27,7 +27,7 @@ export declare function createSchemaTask<TIdentifier extends string, TSchema ext
27
27
  *
28
28
  * @returns {RunHandle} An object with the `id` of the run. Can be used to retrieve the completed run output in a typesafe manner.
29
29
  */
30
- export declare function trigger<TTask extends AnyTask>(id: TaskIdentifier<TTask>, payload: TaskPayload<TTask>, options?: TaskRunOptions, requestOptions?: TriggerApiRequestOptions): Promise<RunHandleFromTypes<InferRunTypes<TTask>>>;
30
+ export declare function trigger<TTask extends AnyTask>(id: TaskIdentifier<TTask>, payload: TaskPayload<TTask>, options?: TriggerOptions, requestOptions?: TriggerApiRequestOptions): Promise<RunHandleFromTypes<InferRunTypes<TTask>>>;
31
31
  /**
32
32
  * Trigger a task with the given payload, and wait for the result. Returns the result of the task run
33
33
  * @param id - The id of the task to trigger
@@ -46,7 +46,7 @@ export declare function trigger<TTask extends AnyTask>(id: TaskIdentifier<TTask>
46
46
  * }
47
47
  * ```
48
48
  */
49
- export declare function triggerAndWait<TTask extends AnyTask>(id: TaskIdentifier<TTask>, payload: TaskPayload<TTask>, options?: TaskRunOptions, requestOptions?: ApiRequestOptions): TaskRunPromise<TaskOutput<TTask>>;
49
+ export declare function triggerAndWait<TTask extends AnyTask>(id: TaskIdentifier<TTask>, payload: TaskPayload<TTask>, options?: TriggerAndWaitOptions, requestOptions?: ApiRequestOptions): TaskRunPromise<TaskIdentifier<TTask>, TaskOutput<TTask>>;
50
50
  /**
51
51
  * Batch trigger multiple task runs with the given payloads, and wait for the results. Returns the results of the task runs.
52
52
  * @param id - The id of the task to trigger
@@ -71,7 +71,7 @@ export declare function triggerAndWait<TTask extends AnyTask>(id: TaskIdentifier
71
71
  * }
72
72
  * ```
73
73
  */
74
- export declare function batchTriggerAndWait<TTask extends AnyTask>(id: TaskIdentifier<TTask>, items: Array<BatchItem<TaskPayload<TTask>>>, requestOptions?: ApiRequestOptions): Promise<BatchResult<TaskOutput<TTask>>>;
74
+ export declare function batchTriggerAndWait<TTask extends AnyTask>(id: TaskIdentifier<TTask>, items: Array<BatchItem<TaskPayload<TTask>>>, requestOptions?: ApiRequestOptions): Promise<BatchResult<TaskIdentifier<TTask>, TaskOutput<TTask>>>;
75
75
  /**
76
76
  * Trigger a task by its identifier with the given payload and poll until the run is completed.
77
77
  *
@@ -87,5 +87,297 @@ export declare function batchTriggerAndWait<TTask extends AnyTask>(id: TaskIdent
87
87
  *
88
88
  * @returns {Run} The completed run, either successful or failed.
89
89
  */
90
- export declare function triggerAndPoll<TTask extends AnyTask>(id: TaskIdentifier<TTask>, payload: TaskPayload<TTask>, options?: TaskRunOptions & PollOptions, requestOptions?: TriggerApiRequestOptions): Promise<RetrieveRunResult<TTask>>;
91
- export declare function batchTrigger<TTask extends AnyTask>(id: TaskIdentifier<TTask>, items: Array<BatchItem<TaskPayload<TTask>>>, requestOptions?: TriggerApiRequestOptions): Promise<BatchRunHandleFromTypes<InferRunTypes<TTask>>>;
90
+ export declare function triggerAndPoll<TTask extends AnyTask>(id: TaskIdentifier<TTask>, payload: TaskPayload<TTask>, options?: TriggerOptions & PollOptions, requestOptions?: TriggerApiRequestOptions): Promise<RetrieveRunResult<TTask>>;
91
+ export declare function batchTrigger<TTask extends AnyTask>(id: TaskIdentifier<TTask>, items: Array<BatchItem<TaskPayload<TTask>>>, options?: BatchTriggerOptions, requestOptions?: TriggerApiRequestOptions): Promise<BatchRunHandleFromTypes<InferRunTypes<TTask>>>;
92
+ /**
93
+ * Triggers multiple runs of different tasks with specified payloads and options.
94
+ *
95
+ * @template TTask - The type of task(s) to be triggered, extends AnyTask
96
+ *
97
+ * @param {Array<BatchByIdItem<InferRunTypes<TTask>>>} items - Array of task items to trigger
98
+ * @param {BatchTriggerOptions} [options] - Optional batch-level trigger options
99
+ * @param {TriggerApiRequestOptions} [requestOptions] - Optional API request configuration
100
+ *
101
+ * @returns {Promise<BatchRunHandleFromTypes<InferRunTypes<TTask>>>} A promise that resolves with the batch run handle
102
+ * containing batch ID, cached status, idempotency info, runs, and public access token
103
+ *
104
+ * @example
105
+ * ```ts
106
+ * import { batch } from "@trigger.dev/sdk/v3";
107
+ * import type { myTask1, myTask2 } from "~/trigger/myTasks";
108
+ *
109
+ * // Trigger multiple tasks with different payloads
110
+ * const result = await batch.trigger<typeof myTask1 | typeof myTask2>([
111
+ * {
112
+ * id: "my-task-1",
113
+ * payload: { some: "data" },
114
+ * options: {
115
+ * queue: "default",
116
+ * concurrencyKey: "key",
117
+ * idempotencyKey: "unique-key",
118
+ * delay: "5m",
119
+ * tags: ["tag1", "tag2"]
120
+ * }
121
+ * },
122
+ * {
123
+ * id: "my-task-2",
124
+ * payload: { other: "data" }
125
+ * }
126
+ * ]);
127
+ * ```
128
+ *
129
+ * @description
130
+ * Each task item in the array can include:
131
+ * - `id`: The unique identifier of the task
132
+ * - `payload`: The data to pass to the task
133
+ * - `options`: Optional task-specific settings including:
134
+ * - `queue`: Specify a queue for the task
135
+ * - `concurrencyKey`: Control concurrent execution
136
+ * - `idempotencyKey`: Prevent duplicate runs
137
+ * - `idempotencyKeyTTL`: Time-to-live for idempotency key
138
+ * - `delay`: Delay before task execution
139
+ * - `ttl`: Time-to-live for the task
140
+ * - `tags`: Array of tags for the task
141
+ * - `maxAttempts`: Maximum retry attempts
142
+ * - `metadata`: Additional metadata
143
+ * - `maxDuration`: Maximum execution duration
144
+ */
145
+ export declare function batchTriggerById<TTask extends AnyTask>(items: Array<BatchByIdItem<InferRunTypes<TTask>>>, options?: BatchTriggerOptions, requestOptions?: TriggerApiRequestOptions): Promise<BatchRunHandleFromTypes<InferRunTypes<TTask>>>;
146
+ /**
147
+ * Triggers multiple tasks and waits for all of them to complete before returning their results.
148
+ * This function must be called from within a task.run() context.
149
+ *
150
+ * @template TTask - Union type of tasks to be triggered, extends AnyTask
151
+ *
152
+ * @param {Array<BatchByIdAndWaitItem<InferRunTypes<TTask>>>} items - Array of task items to trigger
153
+ * @param {TriggerApiRequestOptions} [requestOptions] - Optional API request configuration
154
+ *
155
+ * @returns {Promise<BatchByIdResult<TTask>>} A promise that resolves with the batch results, including
156
+ * success/failure status and strongly-typed outputs for each task
157
+ *
158
+ * @throws {Error} If called outside of a task.run() context
159
+ * @throws {Error} If no API client is configured
160
+ *
161
+ * @example
162
+ * ```ts
163
+ * import { batch, task } from "@trigger.dev/sdk/v3";
164
+ *
165
+ * export const parentTask = task({
166
+ * id: "parent-task",
167
+ * run: async (payload: string) => {
168
+ * const results = await batch.triggerAndWait<typeof childTask1 | typeof childTask2>([
169
+ * {
170
+ * id: "child-task-1",
171
+ * payload: { foo: "World" },
172
+ * options: {
173
+ * queue: "default",
174
+ * delay: "5m",
175
+ * tags: ["batch", "child1"]
176
+ * }
177
+ * },
178
+ * {
179
+ * id: "child-task-2",
180
+ * payload: { bar: 42 }
181
+ * }
182
+ * ]);
183
+ *
184
+ * // Type-safe result handling
185
+ * for (const result of results) {
186
+ * if (result.ok) {
187
+ * switch (result.taskIdentifier) {
188
+ * case "child-task-1":
189
+ * console.log("Child task 1 output:", result.output); // string type
190
+ * break;
191
+ * case "child-task-2":
192
+ * console.log("Child task 2 output:", result.output); // number type
193
+ * break;
194
+ * }
195
+ * } else {
196
+ * console.error("Task failed:", result.error);
197
+ * }
198
+ * }
199
+ * }
200
+ * });
201
+ * ```
202
+ *
203
+ * @description
204
+ * Each task item in the array can include:
205
+ * - `id`: The task identifier (must match one of the tasks in the union type)
206
+ * - `payload`: Strongly-typed payload matching the task's input type
207
+ * - `options`: Optional task-specific settings including:
208
+ * - `queue`: Specify a queue for the task
209
+ * - `concurrencyKey`: Control concurrent execution
210
+ * - `delay`: Delay before task execution
211
+ * - `ttl`: Time-to-live for the task
212
+ * - `tags`: Array of tags for the task
213
+ * - `maxAttempts`: Maximum retry attempts
214
+ * - `metadata`: Additional metadata
215
+ * - `maxDuration`: Maximum execution duration
216
+ *
217
+ * The function provides full type safety for:
218
+ * - Task IDs
219
+ * - Payload types
220
+ * - Return value types
221
+ * - Error handling
222
+ */
223
+ export declare function batchTriggerByIdAndWait<TTask extends AnyTask>(items: Array<BatchByIdAndWaitItem<InferRunTypes<TTask>>>, requestOptions?: TriggerApiRequestOptions): Promise<BatchByIdResult<TTask>>;
224
+ /**
225
+ * Triggers multiple tasks and waits for all of them to complete before returning their results.
226
+ * This function must be called from within a task.run() context.
227
+ *
228
+ * @template TTask - Union type of tasks to be triggered, extends AnyTask
229
+ *
230
+ * @param {Array<BatchByIdAndWaitItem<InferRunTypes<TTask>>>} items - Array of task items to trigger
231
+ * @param {TriggerApiRequestOptions} [requestOptions] - Optional API request configuration
232
+ *
233
+ * @returns {Promise<BatchByIdResult<TTask>>} A promise that resolves with the batch results, including
234
+ * success/failure status and strongly-typed outputs for each task
235
+ *
236
+ * @throws {Error} If called outside of a task.run() context
237
+ * @throws {Error} If no API client is configured
238
+ *
239
+ * @example
240
+ * ```ts
241
+ * import { batch, task } from "@trigger.dev/sdk/v3";
242
+ *
243
+ * export const parentTask = task({
244
+ * id: "parent-task",
245
+ * run: async (payload: string) => {
246
+ * const results = await batch.triggerAndWait<typeof childTask1 | typeof childTask2>([
247
+ * {
248
+ * id: "child-task-1",
249
+ * payload: { foo: "World" },
250
+ * options: {
251
+ * queue: "default",
252
+ * delay: "5m",
253
+ * tags: ["batch", "child1"]
254
+ * }
255
+ * },
256
+ * {
257
+ * id: "child-task-2",
258
+ * payload: { bar: 42 }
259
+ * }
260
+ * ]);
261
+ *
262
+ * // Type-safe result handling
263
+ * for (const result of results) {
264
+ * if (result.ok) {
265
+ * switch (result.taskIdentifier) {
266
+ * case "child-task-1":
267
+ * console.log("Child task 1 output:", result.output); // string type
268
+ * break;
269
+ * case "child-task-2":
270
+ * console.log("Child task 2 output:", result.output); // number type
271
+ * break;
272
+ * }
273
+ * } else {
274
+ * console.error("Task failed:", result.error);
275
+ * }
276
+ * }
277
+ * }
278
+ * });
279
+ * ```
280
+ *
281
+ * @description
282
+ * Each task item in the array can include:
283
+ * - `id`: The task identifier (must match one of the tasks in the union type)
284
+ * - `payload`: Strongly-typed payload matching the task's input type
285
+ * - `options`: Optional task-specific settings including:
286
+ * - `queue`: Specify a queue for the task
287
+ * - `concurrencyKey`: Control concurrent execution
288
+ * - `delay`: Delay before task execution
289
+ * - `ttl`: Time-to-live for the task
290
+ * - `tags`: Array of tags for the task
291
+ * - `maxAttempts`: Maximum retry attempts
292
+ * - `metadata`: Additional metadata
293
+ * - `maxDuration`: Maximum execution duration
294
+ *
295
+ * The function provides full type safety for:
296
+ * - Task IDs
297
+ * - Payload types
298
+ * - Return value types
299
+ * - Error handling
300
+ */
301
+ export declare function batchTriggerTasks<TTasks extends readonly AnyTask[]>(items: {
302
+ [K in keyof TTasks]: BatchByTaskItem<TTasks[K]>;
303
+ }, options?: BatchTriggerOptions, requestOptions?: TriggerApiRequestOptions): Promise<BatchTasksRunHandleFromTypes<TTasks>>;
304
+ /**
305
+ * Triggers multiple tasks and waits for all of them to complete before returning their results.
306
+ * This function must be called from within a task.run() context.
307
+ *
308
+ * @template TTask - Union type of tasks to be triggered, extends AnyTask
309
+ *
310
+ * @param {Array<BatchByIdAndWaitItem<InferRunTypes<TTask>>>} items - Array of task items to trigger
311
+ * @param {TriggerApiRequestOptions} [requestOptions] - Optional API request configuration
312
+ *
313
+ * @returns {Promise<BatchByIdResult<TTask>>} A promise that resolves with the batch results, including
314
+ * success/failure status and strongly-typed outputs for each task
315
+ *
316
+ * @throws {Error} If called outside of a task.run() context
317
+ * @throws {Error} If no API client is configured
318
+ *
319
+ * @example
320
+ * ```ts
321
+ * import { batch, task } from "@trigger.dev/sdk/v3";
322
+ *
323
+ * export const parentTask = task({
324
+ * id: "parent-task",
325
+ * run: async (payload: string) => {
326
+ * const results = await batch.triggerAndWait<typeof childTask1 | typeof childTask2>([
327
+ * {
328
+ * id: "child-task-1",
329
+ * payload: { foo: "World" },
330
+ * options: {
331
+ * queue: "default",
332
+ * delay: "5m",
333
+ * tags: ["batch", "child1"]
334
+ * }
335
+ * },
336
+ * {
337
+ * id: "child-task-2",
338
+ * payload: { bar: 42 }
339
+ * }
340
+ * ]);
341
+ *
342
+ * // Type-safe result handling
343
+ * for (const result of results) {
344
+ * if (result.ok) {
345
+ * switch (result.taskIdentifier) {
346
+ * case "child-task-1":
347
+ * console.log("Child task 1 output:", result.output); // string type
348
+ * break;
349
+ * case "child-task-2":
350
+ * console.log("Child task 2 output:", result.output); // number type
351
+ * break;
352
+ * }
353
+ * } else {
354
+ * console.error("Task failed:", result.error);
355
+ * }
356
+ * }
357
+ * }
358
+ * });
359
+ * ```
360
+ *
361
+ * @description
362
+ * Each task item in the array can include:
363
+ * - `id`: The task identifier (must match one of the tasks in the union type)
364
+ * - `payload`: Strongly-typed payload matching the task's input type
365
+ * - `options`: Optional task-specific settings including:
366
+ * - `queue`: Specify a queue for the task
367
+ * - `concurrencyKey`: Control concurrent execution
368
+ * - `delay`: Delay before task execution
369
+ * - `ttl`: Time-to-live for the task
370
+ * - `tags`: Array of tags for the task
371
+ * - `maxAttempts`: Maximum retry attempts
372
+ * - `metadata`: Additional metadata
373
+ * - `maxDuration`: Maximum execution duration
374
+ *
375
+ * The function provides full type safety for:
376
+ * - Task IDs
377
+ * - Payload types
378
+ * - Return value types
379
+ * - Error handling
380
+ */
381
+ export declare function batchTriggerAndWaitTasks<TTasks extends readonly AnyTask[]>(items: {
382
+ [K in keyof TTasks]: BatchByTaskAndWaitItem<TTasks[K]>;
383
+ }, requestOptions?: TriggerApiRequestOptions): Promise<BatchByTaskResult<TTasks>>;