@trigger.dev/core 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.
package/dist/v3/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { z } from 'zod';
2
- import { SpanStatusCode, trace, propagation, context, DiagLogLevel, diag, DiagConsoleLogger, SpanKind } from '@opentelemetry/api';
2
+ import { SpanStatusCode, propagation, context, trace, DiagLogLevel, diag, DiagConsoleLogger, SpanKind } from '@opentelemetry/api';
3
3
  import { fromZodError } from 'zod-validation-error';
4
4
  import { AsyncLocalStorage } from 'node:async_hooks';
5
5
  import { io } from 'socket.io-client';
@@ -92,7 +92,8 @@ var TaskRunErrorCodes = {
92
92
  TASK_PROCESS_EXITED_WITH_NON_ZERO_CODE: "TASK_PROCESS_EXITED_WITH_NON_ZERO_CODE",
93
93
  TASK_RUN_CANCELLED: "TASK_RUN_CANCELLED",
94
94
  TASK_OUTPUT_ERROR: "TASK_OUTPUT_ERROR",
95
- HANDLE_ERROR_ERROR: "HANDLE_ERROR_ERROR"
95
+ HANDLE_ERROR_ERROR: "HANDLE_ERROR_ERROR",
96
+ GRACEFUL_EXIT_TIMEOUT: "GRACEFUL_EXIT_TIMEOUT"
96
97
  };
97
98
  var TaskRunInternalError = z.object({
98
99
  type: z.literal("INTERNAL_ERROR"),
@@ -106,7 +107,8 @@ var TaskRunInternalError = z.object({
106
107
  "TASK_PROCESS_EXITED_WITH_NON_ZERO_CODE",
107
108
  "TASK_RUN_CANCELLED",
108
109
  "TASK_OUTPUT_ERROR",
109
- "HANDLE_ERROR_ERROR"
110
+ "HANDLE_ERROR_ERROR",
111
+ "GRACEFUL_EXIT_TIMEOUT"
110
112
  ]),
111
113
  message: z.string().optional()
112
114
  });
@@ -123,7 +125,8 @@ var TaskRun = z.object({
123
125
  context: z.any(),
124
126
  tags: z.array(z.string()),
125
127
  isTest: z.boolean().default(false),
126
- createdAt: z.coerce.date()
128
+ createdAt: z.coerce.date(),
129
+ idempotencyKey: z.string().optional()
127
130
  });
128
131
  var TaskRunExecutionTask = z.object({
129
132
  id: z.string(),
@@ -2059,7 +2062,8 @@ var SemanticInternalAttributes = {
2059
2062
  RETRY_AT: "retry.at",
2060
2063
  RETRY_DELAY: "retry.delay",
2061
2064
  RETRY_COUNT: "retry.count",
2062
- LINK_TITLE: "$link.title"
2065
+ LINK_TITLE: "$link.title",
2066
+ IDEMPOTENCY_KEY: "ctx.run.idempotencyKey"
2063
2067
  };
2064
2068
 
2065
2069
  // src/v3/tasks/taskContextManager.ts
@@ -2118,7 +2122,8 @@ var _TaskContextManager = class _TaskContextManager {
2118
2122
  [SemanticInternalAttributes.RUN_IS_TEST]: this.ctx.run.isTest,
2119
2123
  [SemanticInternalAttributes.ORGANIZATION_SLUG]: this.ctx.organization.slug,
2120
2124
  [SemanticInternalAttributes.ORGANIZATION_NAME]: this.ctx.organization.name,
2121
- [SemanticInternalAttributes.BATCH_ID]: this.ctx.batch?.id
2125
+ [SemanticInternalAttributes.BATCH_ID]: this.ctx.batch?.id,
2126
+ [SemanticInternalAttributes.IDEMPOTENCY_KEY]: this.ctx.run.idempotencyKey
2122
2127
  };
2123
2128
  }
2124
2129
  return {};
@@ -2209,6 +2214,27 @@ var _ApiClient = class _ApiClient {
2209
2214
  this.accessToken = accessToken;
2210
2215
  this.baseUrl = baseUrl.replace(/\/$/, "");
2211
2216
  }
2217
+ async getRunResult(runId) {
2218
+ try {
2219
+ return await zodfetch(TaskRunExecutionResult, `${this.baseUrl}/api/v1/runs/${runId}/result`, {
2220
+ method: "GET",
2221
+ headers: __privateMethod(this, _getHeaders, getHeaders_fn).call(this, false)
2222
+ }, zodFetchOptions);
2223
+ } catch (error) {
2224
+ if (error instanceof APIError) {
2225
+ if (error.status === 404) {
2226
+ return void 0;
2227
+ }
2228
+ }
2229
+ throw error;
2230
+ }
2231
+ }
2232
+ async getBatchResults(batchId) {
2233
+ return await zodfetch(BatchTaskRunExecutionResult, `${this.baseUrl}/api/v1/batches/${batchId}/results`, {
2234
+ method: "GET",
2235
+ headers: __privateMethod(this, _getHeaders, getHeaders_fn).call(this, false)
2236
+ }, zodFetchOptions);
2237
+ }
2212
2238
  triggerTask(taskId, body, options) {
2213
2239
  return zodfetch(TriggerTaskResponse, `${this.baseUrl}/api/v1/tasks/${taskId}/trigger`, {
2214
2240
  method: "POST",
@@ -3454,12 +3480,23 @@ __name(_NoopTaskLogger, "NoopTaskLogger");
3454
3480
  var NoopTaskLogger = _NoopTaskLogger;
3455
3481
  function safeJsonProcess(value) {
3456
3482
  try {
3457
- return JSON.parse(JSON.stringify(value));
3483
+ return JSON.parse(JSON.stringify(value, jsonErrorReplacer));
3458
3484
  } catch {
3459
3485
  return value;
3460
3486
  }
3461
3487
  }
3462
3488
  __name(safeJsonProcess, "safeJsonProcess");
3489
+ function jsonErrorReplacer(key, value) {
3490
+ if (value instanceof Error) {
3491
+ return {
3492
+ name: value.name,
3493
+ message: value.message,
3494
+ stack: value.stack
3495
+ };
3496
+ }
3497
+ return value;
3498
+ }
3499
+ __name(jsonErrorReplacer, "jsonErrorReplacer");
3463
3500
 
3464
3501
  // src/v3/logger/index.ts
3465
3502
  var API_NAME3 = "logger";
@@ -3699,6 +3736,17 @@ function formatDurationInDays(milliseconds) {
3699
3736
  return duration;
3700
3737
  }
3701
3738
  __name(formatDurationInDays, "formatDurationInDays");
3739
+ async function unboundedTimeout(delay = 0, value, options) {
3740
+ const maxDelay = 2147483647;
3741
+ const fullTimeouts = Math.floor(delay / maxDelay);
3742
+ const remainingDelay = delay % maxDelay;
3743
+ let lastTimeoutResult = await setTimeout$1(remainingDelay, value, options);
3744
+ for (let i = 0; i < fullTimeouts; i++) {
3745
+ lastTimeoutResult = await setTimeout$1(maxDelay, value, options);
3746
+ }
3747
+ return lastTimeoutResult;
3748
+ }
3749
+ __name(unboundedTimeout, "unboundedTimeout");
3702
3750
 
3703
3751
  // src/v3/runtime/devRuntimeManager.ts
3704
3752
  var _DevRuntimeManager = class _DevRuntimeManager {
@@ -3710,14 +3758,10 @@ var _DevRuntimeManager = class _DevRuntimeManager {
3710
3758
  disable() {
3711
3759
  }
3712
3760
  async waitForDuration(ms) {
3713
- return new Promise((resolve) => {
3714
- setTimeout(resolve, ms);
3715
- });
3761
+ await unboundedTimeout(ms);
3716
3762
  }
3717
3763
  async waitUntil(date) {
3718
- return new Promise((resolve) => {
3719
- setTimeout(resolve, date.getTime() - Date.now());
3720
- });
3764
+ return this.waitForDuration(date.getTime() - Date.now());
3721
3765
  }
3722
3766
  async waitForTask(params) {
3723
3767
  const pendingCompletion = this._pendingCompletionNotifications.get(params.id);
@@ -3725,10 +3769,9 @@ var _DevRuntimeManager = class _DevRuntimeManager {
3725
3769
  this._pendingCompletionNotifications.delete(params.id);
3726
3770
  return pendingCompletion;
3727
3771
  }
3728
- const promise = new Promise((resolve, reject) => {
3772
+ const promise = new Promise((resolve) => {
3729
3773
  this._taskWaits.set(params.id, {
3730
- resolve,
3731
- reject
3774
+ resolve
3732
3775
  });
3733
3776
  });
3734
3777
  return await promise;
@@ -3745,16 +3788,11 @@ var _DevRuntimeManager = class _DevRuntimeManager {
3745
3788
  const pendingCompletion = this._pendingCompletionNotifications.get(runId);
3746
3789
  if (pendingCompletion) {
3747
3790
  this._pendingCompletionNotifications.delete(runId);
3748
- if (pendingCompletion.ok) {
3749
- resolve(pendingCompletion);
3750
- } else {
3751
- reject(pendingCompletion);
3752
- }
3791
+ resolve(pendingCompletion);
3753
3792
  return;
3754
3793
  }
3755
3794
  this._taskWaits.set(runId, {
3756
- resolve,
3757
- reject
3795
+ resolve
3758
3796
  });
3759
3797
  });
3760
3798
  }));
@@ -3770,16 +3808,14 @@ var _DevRuntimeManager = class _DevRuntimeManager {
3770
3808
  this._pendingCompletionNotifications.set(execution.run.id, completion);
3771
3809
  return;
3772
3810
  }
3773
- if (completion.ok) {
3774
- wait.resolve(completion);
3775
- } else {
3776
- wait.reject(completion);
3777
- }
3811
+ wait.resolve(completion);
3778
3812
  this._taskWaits.delete(execution.run.id);
3779
3813
  }
3780
3814
  };
3781
3815
  __name(_DevRuntimeManager, "DevRuntimeManager");
3782
3816
  var DevRuntimeManager = _DevRuntimeManager;
3817
+
3818
+ // src/v3/runtime/prodRuntimeManager.ts
3783
3819
  var _ProdRuntimeManager = class _ProdRuntimeManager {
3784
3820
  constructor(ipc, options = {}) {
3785
3821
  this.ipc = ipc;
@@ -3791,7 +3827,7 @@ var _ProdRuntimeManager = class _ProdRuntimeManager {
3791
3827
  }
3792
3828
  async waitForDuration(ms) {
3793
3829
  const now = Date.now();
3794
- const resolveAfterDuration = setTimeout$1(ms, "duration");
3830
+ const resolveAfterDuration = unboundedTimeout(ms, "duration");
3795
3831
  if (ms <= this.waitThresholdInMs) {
3796
3832
  await resolveAfterDuration;
3797
3833
  return;
@@ -3829,16 +3865,17 @@ var _ProdRuntimeManager = class _ProdRuntimeManager {
3829
3865
  return this.waitForDuration(date.getTime() - Date.now());
3830
3866
  }
3831
3867
  async waitForTask(params) {
3832
- const promise = new Promise((resolve, reject) => {
3868
+ const promise = new Promise((resolve) => {
3833
3869
  this._taskWaits.set(params.id, {
3834
- resolve,
3835
- reject
3870
+ resolve
3836
3871
  });
3837
3872
  });
3838
3873
  await this.ipc.send("WAIT_FOR_TASK", {
3839
3874
  friendlyId: params.id
3840
3875
  });
3841
- return await promise;
3876
+ const result = await promise;
3877
+ clock.reset();
3878
+ return result;
3842
3879
  }
3843
3880
  async waitForBatch(params) {
3844
3881
  if (!params.runs.length) {
@@ -3850,8 +3887,7 @@ var _ProdRuntimeManager = class _ProdRuntimeManager {
3850
3887
  const promise = Promise.all(params.runs.map((runId) => {
3851
3888
  return new Promise((resolve, reject) => {
3852
3889
  this._taskWaits.set(runId, {
3853
- resolve,
3854
- reject
3890
+ resolve
3855
3891
  });
3856
3892
  });
3857
3893
  }));
@@ -3860,6 +3896,7 @@ var _ProdRuntimeManager = class _ProdRuntimeManager {
3860
3896
  runFriendlyIds: params.runs
3861
3897
  });
3862
3898
  const results = await promise;
3899
+ clock.reset();
3863
3900
  return {
3864
3901
  id: params.id,
3865
3902
  items: results
@@ -3870,11 +3907,7 @@ var _ProdRuntimeManager = class _ProdRuntimeManager {
3870
3907
  if (!wait) {
3871
3908
  return;
3872
3909
  }
3873
- if (completion.ok) {
3874
- wait.resolve(completion);
3875
- } else {
3876
- wait.reject(completion);
3877
- }
3910
+ wait.resolve(completion);
3878
3911
  this._taskWaits.delete(execution.run.id);
3879
3912
  }
3880
3913
  get waitThresholdInMs() {
@@ -4763,7 +4796,7 @@ var _TaskExecutor = class _TaskExecutor {
4763
4796
  }
4764
4797
  return {
4765
4798
  ok: true,
4766
- id: execution.attempt.id,
4799
+ id: execution.run.id,
4767
4800
  output: finalOutput.data,
4768
4801
  outputType: finalOutput.dataType
4769
4802
  };
@@ -4771,7 +4804,7 @@ var _TaskExecutor = class _TaskExecutor {
4771
4804
  recordSpanException(span, stringifyError);
4772
4805
  return {
4773
4806
  ok: false,
4774
- id: execution.attempt.id,
4807
+ id: execution.run.id,
4775
4808
  error: {
4776
4809
  type: "INTERNAL_ERROR",
4777
4810
  code: TaskRunErrorCodes.TASK_OUTPUT_ERROR,
@@ -4784,7 +4817,7 @@ var _TaskExecutor = class _TaskExecutor {
4784
4817
  const handleErrorResult = await __privateMethod(this, _handleError, handleError_fn).call(this, execution, runError, parsedPayload, ctx);
4785
4818
  recordSpanException(span, handleErrorResult.error ?? runError);
4786
4819
  return {
4787
- id: execution.attempt.id,
4820
+ id: execution.run.id,
4788
4821
  ok: false,
4789
4822
  error: handleErrorResult.error ? parseError(handleErrorResult.error) : parseError(runError),
4790
4823
  retry: handleErrorResult.status === "retry" ? handleErrorResult.retry : void 0,
@@ -4794,7 +4827,7 @@ var _TaskExecutor = class _TaskExecutor {
4794
4827
  recordSpanException(span, handleErrorError);
4795
4828
  return {
4796
4829
  ok: false,
4797
- id: execution.attempt.id,
4830
+ id: execution.run.id,
4798
4831
  error: {
4799
4832
  type: "INTERNAL_ERROR",
4800
4833
  code: TaskRunErrorCodes.HANDLE_ERROR_ERROR,