@trigger.dev/sdk 3.0.0-beta.13 → 3.0.0-beta.15

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.
@@ -12,7 +12,7 @@ type Queue = RequireOne<QueueOptions, "name">;
12
12
  declare function queue(options: {
13
13
  name: string;
14
14
  } & QueueOptions): Queue;
15
- type TaskOptions<TPayload, TOutput = any, TInitOutput extends InitOutput = any> = {
15
+ type TaskOptions<TPayload = void, TOutput = unknown, TInitOutput extends InitOutput = any> = {
16
16
  /** An id for your task. This must be unique inside your project and not change between versions. */
17
17
  id: string;
18
18
  /** The retry settings when an uncaught error is thrown.
@@ -122,36 +122,78 @@ type TaskRunResult<TOutput = any> = {
122
122
  } | {
123
123
  ok: false;
124
124
  id: string;
125
- error: any;
125
+ error: unknown;
126
126
  };
127
127
  type BatchResult<TOutput = any> = {
128
128
  id: string;
129
129
  runs: TaskRunResult<TOutput>[];
130
130
  };
131
- interface Task<TInput, TOutput = any> {
131
+ type BatchItem<TInput> = TInput extends void ? {
132
+ payload?: TInput;
133
+ options?: TaskRunOptions;
134
+ } : {
135
+ payload: TInput;
136
+ options?: TaskRunOptions;
137
+ };
138
+ interface Task<TInput = void, TOutput = any> {
139
+ /**
140
+ * The id of the task.
141
+ */
132
142
  id: string;
133
- trigger: (params: {
134
- payload: TInput;
135
- options?: TaskRunOptions;
136
- }) => Promise<InvokeHandle>;
137
- batchTrigger: (params: {
138
- items: {
139
- payload: TInput;
140
- options?: TaskRunOptions;
141
- }[];
142
- batchOptions?: BatchRunOptions;
143
- }) => Promise<InvokeBatchHandle>;
144
- triggerAndWait: (params: {
145
- payload: TInput;
146
- options?: TaskRunOptions;
147
- }) => Promise<TOutput>;
148
- batchTriggerAndWait: (params: {
149
- items: {
150
- payload: TInput;
151
- options?: TaskRunOptions;
152
- }[];
153
- batchOptions?: BatchRunOptions;
154
- }) => Promise<BatchResult<TOutput>>;
143
+ /**
144
+ * Trigger a task with the given payload, and continue without waiting for the result. If you want to wait for the result, use `triggerAndWait`. Returns the id of the triggered task run.
145
+ * @param payload
146
+ * @param options
147
+ * @returns InvokeHandle
148
+ * - `id` - The id of the triggered task run.
149
+ */
150
+ trigger: (payload: TInput, options?: TaskRunOptions) => Promise<InvokeHandle>;
151
+ /**
152
+ * Batch trigger multiple task runs with the given payloads, and continue without waiting for the results. If you want to wait for the results, use `batchTriggerAndWait`. Returns the id of the triggered batch.
153
+ * @param items
154
+ * @returns InvokeBatchHandle
155
+ * - `batchId` - The id of the triggered batch.
156
+ * - `runs` - The ids of the triggered task runs.
157
+ */
158
+ batchTrigger: (items: Array<BatchItem<TInput>>) => Promise<InvokeBatchHandle>;
159
+ /**
160
+ * Trigger a task with the given payload, and wait for the result. Returns the result of the task run
161
+ * @param payload
162
+ * @param options - Options for the task run
163
+ * @returns TaskRunResult
164
+ * @example
165
+ * ```
166
+ * const result = await task.triggerAndWait({ foo: "bar" });
167
+ *
168
+ * if (result.ok) {
169
+ * console.log(result.output);
170
+ * } else {
171
+ * console.error(result.error);
172
+ * }
173
+ * ```
174
+ */
175
+ triggerAndWait: (payload: TInput, options?: TaskRunOptions) => Promise<TaskRunResult<TOutput>>;
176
+ /**
177
+ * Batch trigger multiple task runs with the given payloads, and wait for the results. Returns the results of the task runs.
178
+ * @param items
179
+ * @returns BatchResult
180
+ * @example
181
+ * ```
182
+ * const result = await task.batchTriggerAndWait([
183
+ * { payload: { foo: "bar" } },
184
+ * { payload: { foo: "baz" } },
185
+ * ]);
186
+ *
187
+ * for (const run of result.runs) {
188
+ * if (run.ok) {
189
+ * console.log(run.output);
190
+ * } else {
191
+ * console.error(run.error);
192
+ * }
193
+ * }
194
+ * ```
195
+ */
196
+ batchTriggerAndWait: (items: Array<BatchItem<TInput>>) => Promise<BatchResult<TOutput>>;
155
197
  }
156
198
  type TaskRunOptions = {
157
199
  idempotencyKey?: string;
@@ -162,9 +204,6 @@ type TaskRunOptions = {
162
204
  concurrencyKey?: string;
163
205
  };
164
206
  type TaskRunConcurrencyOptions = Queue;
165
- type BatchRunOptions = TaskRunOptions & {
166
- maxConcurrency?: number;
167
- };
168
207
 
169
208
  /** Creates a task that can be triggered
170
209
  * @param options - Task options
@@ -184,7 +223,7 @@ type BatchRunOptions = TaskRunOptions & {
184
223
  *
185
224
  * @returns A task that can be triggered
186
225
  */
187
- declare function task$1<TInput, TOutput = any, TInitOutput extends InitOutput = any>(options: TaskOptions<TInput, TOutput, TInitOutput>): Task<TInput, TOutput>;
226
+ declare function task$1<TInput = void, TOutput = unknown, TInitOutput extends InitOutput = any>(options: TaskOptions<TInput, TOutput, TInitOutput>): Task<TInput, TOutput>;
188
227
 
189
228
  type WaitOptions = {
190
229
  seconds: number;
@@ -12,7 +12,7 @@ type Queue = RequireOne<QueueOptions, "name">;
12
12
  declare function queue(options: {
13
13
  name: string;
14
14
  } & QueueOptions): Queue;
15
- type TaskOptions<TPayload, TOutput = any, TInitOutput extends InitOutput = any> = {
15
+ type TaskOptions<TPayload = void, TOutput = unknown, TInitOutput extends InitOutput = any> = {
16
16
  /** An id for your task. This must be unique inside your project and not change between versions. */
17
17
  id: string;
18
18
  /** The retry settings when an uncaught error is thrown.
@@ -122,36 +122,78 @@ type TaskRunResult<TOutput = any> = {
122
122
  } | {
123
123
  ok: false;
124
124
  id: string;
125
- error: any;
125
+ error: unknown;
126
126
  };
127
127
  type BatchResult<TOutput = any> = {
128
128
  id: string;
129
129
  runs: TaskRunResult<TOutput>[];
130
130
  };
131
- interface Task<TInput, TOutput = any> {
131
+ type BatchItem<TInput> = TInput extends void ? {
132
+ payload?: TInput;
133
+ options?: TaskRunOptions;
134
+ } : {
135
+ payload: TInput;
136
+ options?: TaskRunOptions;
137
+ };
138
+ interface Task<TInput = void, TOutput = any> {
139
+ /**
140
+ * The id of the task.
141
+ */
132
142
  id: string;
133
- trigger: (params: {
134
- payload: TInput;
135
- options?: TaskRunOptions;
136
- }) => Promise<InvokeHandle>;
137
- batchTrigger: (params: {
138
- items: {
139
- payload: TInput;
140
- options?: TaskRunOptions;
141
- }[];
142
- batchOptions?: BatchRunOptions;
143
- }) => Promise<InvokeBatchHandle>;
144
- triggerAndWait: (params: {
145
- payload: TInput;
146
- options?: TaskRunOptions;
147
- }) => Promise<TOutput>;
148
- batchTriggerAndWait: (params: {
149
- items: {
150
- payload: TInput;
151
- options?: TaskRunOptions;
152
- }[];
153
- batchOptions?: BatchRunOptions;
154
- }) => Promise<BatchResult<TOutput>>;
143
+ /**
144
+ * Trigger a task with the given payload, and continue without waiting for the result. If you want to wait for the result, use `triggerAndWait`. Returns the id of the triggered task run.
145
+ * @param payload
146
+ * @param options
147
+ * @returns InvokeHandle
148
+ * - `id` - The id of the triggered task run.
149
+ */
150
+ trigger: (payload: TInput, options?: TaskRunOptions) => Promise<InvokeHandle>;
151
+ /**
152
+ * Batch trigger multiple task runs with the given payloads, and continue without waiting for the results. If you want to wait for the results, use `batchTriggerAndWait`. Returns the id of the triggered batch.
153
+ * @param items
154
+ * @returns InvokeBatchHandle
155
+ * - `batchId` - The id of the triggered batch.
156
+ * - `runs` - The ids of the triggered task runs.
157
+ */
158
+ batchTrigger: (items: Array<BatchItem<TInput>>) => Promise<InvokeBatchHandle>;
159
+ /**
160
+ * Trigger a task with the given payload, and wait for the result. Returns the result of the task run
161
+ * @param payload
162
+ * @param options - Options for the task run
163
+ * @returns TaskRunResult
164
+ * @example
165
+ * ```
166
+ * const result = await task.triggerAndWait({ foo: "bar" });
167
+ *
168
+ * if (result.ok) {
169
+ * console.log(result.output);
170
+ * } else {
171
+ * console.error(result.error);
172
+ * }
173
+ * ```
174
+ */
175
+ triggerAndWait: (payload: TInput, options?: TaskRunOptions) => Promise<TaskRunResult<TOutput>>;
176
+ /**
177
+ * Batch trigger multiple task runs with the given payloads, and wait for the results. Returns the results of the task runs.
178
+ * @param items
179
+ * @returns BatchResult
180
+ * @example
181
+ * ```
182
+ * const result = await task.batchTriggerAndWait([
183
+ * { payload: { foo: "bar" } },
184
+ * { payload: { foo: "baz" } },
185
+ * ]);
186
+ *
187
+ * for (const run of result.runs) {
188
+ * if (run.ok) {
189
+ * console.log(run.output);
190
+ * } else {
191
+ * console.error(run.error);
192
+ * }
193
+ * }
194
+ * ```
195
+ */
196
+ batchTriggerAndWait: (items: Array<BatchItem<TInput>>) => Promise<BatchResult<TOutput>>;
155
197
  }
156
198
  type TaskRunOptions = {
157
199
  idempotencyKey?: string;
@@ -162,9 +204,6 @@ type TaskRunOptions = {
162
204
  concurrencyKey?: string;
163
205
  };
164
206
  type TaskRunConcurrencyOptions = Queue;
165
- type BatchRunOptions = TaskRunOptions & {
166
- maxConcurrency?: number;
167
- };
168
207
 
169
208
  /** Creates a task that can be triggered
170
209
  * @param options - Task options
@@ -184,7 +223,7 @@ type BatchRunOptions = TaskRunOptions & {
184
223
  *
185
224
  * @returns A task that can be triggered
186
225
  */
187
- declare function task$1<TInput, TOutput = any, TInitOutput extends InitOutput = any>(options: TaskOptions<TInput, TOutput, TInitOutput>): Task<TInput, TOutput>;
226
+ declare function task$1<TInput = void, TOutput = unknown, TInitOutput extends InitOutput = any>(options: TaskOptions<TInput, TOutput, TInitOutput>): Task<TInput, TOutput>;
188
227
 
189
228
  type WaitOptions = {
190
229
  seconds: number;
package/dist/v3/index.js CHANGED
@@ -18,7 +18,7 @@ var __publicField = (obj, key, value) => {
18
18
  };
19
19
 
20
20
  // package.json
21
- var version = "3.0.0-beta.13";
21
+ var version = "3.0.0-beta.15";
22
22
  var tracer = new v3.TriggerTracer({
23
23
  name: "@trigger.dev/sdk",
24
24
  version
@@ -32,7 +32,7 @@ __name(queue, "queue");
32
32
  function createTask(params) {
33
33
  const task3 = {
34
34
  id: params.id,
35
- trigger: async ({ payload, options }) => {
35
+ trigger: async (payload, options) => {
36
36
  const apiClient = v3.apiClientManager.client;
37
37
  if (!apiClient) {
38
38
  throw apiClientMissingError();
@@ -61,7 +61,6 @@ function createTask(params) {
61
61
  [v3.SemanticInternalAttributes.STYLE_ICON]: "trigger",
62
62
  ["messaging.client_id"]: v3.taskContextManager.worker?.id,
63
63
  [semanticConventions.SEMATTRS_MESSAGING_DESTINATION]: params.queue?.name ?? params.id,
64
- ["messaging.message.body.size"]: JSON.stringify(payload).length,
65
64
  [semanticConventions.SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev",
66
65
  ...taskMetadata ? v3.accessoryAttributes({
67
66
  items: [
@@ -76,7 +75,7 @@ function createTask(params) {
76
75
  });
77
76
  return handle;
78
77
  },
79
- batchTrigger: async ({ items }) => {
78
+ batchTrigger: async (items) => {
80
79
  const apiClient = v3.apiClientManager.client;
81
80
  if (!apiClient) {
82
81
  throw apiClientMissingError();
@@ -109,7 +108,6 @@ function createTask(params) {
109
108
  ["messaging.batch.message_count"]: items.length,
110
109
  ["messaging.client_id"]: v3.taskContextManager.worker?.id,
111
110
  [semanticConventions.SEMATTRS_MESSAGING_DESTINATION]: params.queue?.name ?? params.id,
112
- ["messaging.message.body.size"]: items.map((item) => JSON.stringify(item.payload)).join("").length,
113
111
  [semanticConventions.SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev",
114
112
  [v3.SemanticInternalAttributes.STYLE_ICON]: "trigger",
115
113
  ...taskMetadata ? v3.accessoryAttributes({
@@ -125,7 +123,7 @@ function createTask(params) {
125
123
  });
126
124
  return response;
127
125
  },
128
- triggerAndWait: async ({ payload, options }) => {
126
+ triggerAndWait: async (payload, options) => {
129
127
  const ctx = v3.taskContextManager.ctx;
130
128
  if (!ctx) {
131
129
  throw new Error("triggerAndWait can only be used from inside a task.run()");
@@ -150,15 +148,21 @@ function createTask(params) {
150
148
  }
151
149
  });
152
150
  span.setAttribute("messaging.message.id", response.id);
151
+ if (options?.idempotencyKey) {
152
+ const result2 = await apiClient.getRunResult(response.id);
153
+ if (result2) {
154
+ v3.logger.log(`Result reused from previous task run with idempotency key '${options.idempotencyKey}'.`, {
155
+ runId: response.id,
156
+ idempotencyKey: options.idempotencyKey
157
+ });
158
+ return await handleTaskRunExecutionResult(result2);
159
+ }
160
+ }
153
161
  const result = await v3.runtime.waitForTask({
154
162
  id: response.id,
155
163
  ctx
156
164
  });
157
- const runResult = await handleTaskRunExecutionResult(result);
158
- if (!runResult.ok) {
159
- throw runResult.error;
160
- }
161
- return runResult.output;
165
+ return await handleTaskRunExecutionResult(result);
162
166
  }, {
163
167
  kind: api.SpanKind.PRODUCER,
164
168
  attributes: {
@@ -179,7 +183,7 @@ function createTask(params) {
179
183
  }
180
184
  });
181
185
  },
182
- batchTriggerAndWait: async ({ items }) => {
186
+ batchTriggerAndWait: async (items) => {
183
187
  const ctx = v3.taskContextManager.ctx;
184
188
  if (!ctx) {
185
189
  throw new Error("batchTriggerAndWait can only be used from inside a task.run()");
@@ -208,12 +212,47 @@ function createTask(params) {
208
212
  dependentAttempt: ctx.attempt.id
209
213
  });
210
214
  span.setAttribute("messaging.message.id", response.batchId);
215
+ const getBatchResults = /* @__PURE__ */ __name(async () => {
216
+ const hasIdempotencyKey = items.some((item) => item.options?.idempotencyKey);
217
+ if (hasIdempotencyKey) {
218
+ const results = await apiClient.getBatchResults(response.batchId);
219
+ if (results) {
220
+ return results;
221
+ }
222
+ }
223
+ return {
224
+ id: response.batchId,
225
+ items: []
226
+ };
227
+ }, "getBatchResults");
228
+ const existingResults = await getBatchResults();
229
+ const incompleteRuns = response.runs.filter((runId) => !existingResults.items.some((item) => item.id === runId));
230
+ if (incompleteRuns.length === 0) {
231
+ v3.logger.log(`Results reused from previous task runs because of the provided idempotency keys.`);
232
+ const runs3 = await handleBatchTaskRunExecutionResult(existingResults.items);
233
+ return {
234
+ id: existingResults.id,
235
+ runs: runs3
236
+ };
237
+ }
211
238
  const result = await v3.runtime.waitForBatch({
212
239
  id: response.batchId,
213
- runs: response.runs,
240
+ runs: incompleteRuns,
214
241
  ctx
215
242
  });
216
- const runs2 = await handleBatchTaskRunExecutionResult(result.items);
243
+ const combinedItems = [];
244
+ for (const runId of response.runs) {
245
+ const existingItem = existingResults.items.find((item) => item.id === runId);
246
+ if (existingItem) {
247
+ combinedItems.push(existingItem);
248
+ } else {
249
+ const newItem = result.items.find((item) => item.id === runId);
250
+ if (newItem) {
251
+ combinedItems.push(newItem);
252
+ }
253
+ }
254
+ }
255
+ const runs2 = await handleBatchTaskRunExecutionResult(combinedItems);
217
256
  return {
218
257
  id: result.id,
219
258
  runs: runs2
@@ -225,7 +264,6 @@ function createTask(params) {
225
264
  ["messaging.batch.message_count"]: items.length,
226
265
  ["messaging.client_id"]: v3.taskContextManager.worker?.id,
227
266
  [semanticConventions.SEMATTRS_MESSAGING_DESTINATION]: params.queue?.name ?? params.id,
228
- ["messaging.message.body.size"]: items.map((item) => JSON.stringify(item.payload)).join("").length,
229
267
  [semanticConventions.SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev",
230
268
  [v3.SemanticInternalAttributes.STYLE_ICON]: "trigger",
231
269
  ...taskMetadata ? v3.accessoryAttributes({