@trigger.dev/core 3.0.0-beta.14 → 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';
@@ -125,7 +125,8 @@ var TaskRun = z.object({
125
125
  context: z.any(),
126
126
  tags: z.array(z.string()),
127
127
  isTest: z.boolean().default(false),
128
- createdAt: z.coerce.date()
128
+ createdAt: z.coerce.date(),
129
+ idempotencyKey: z.string().optional()
129
130
  });
130
131
  var TaskRunExecutionTask = z.object({
131
132
  id: z.string(),
@@ -2061,7 +2062,8 @@ var SemanticInternalAttributes = {
2061
2062
  RETRY_AT: "retry.at",
2062
2063
  RETRY_DELAY: "retry.delay",
2063
2064
  RETRY_COUNT: "retry.count",
2064
- LINK_TITLE: "$link.title"
2065
+ LINK_TITLE: "$link.title",
2066
+ IDEMPOTENCY_KEY: "ctx.run.idempotencyKey"
2065
2067
  };
2066
2068
 
2067
2069
  // src/v3/tasks/taskContextManager.ts
@@ -2120,7 +2122,8 @@ var _TaskContextManager = class _TaskContextManager {
2120
2122
  [SemanticInternalAttributes.RUN_IS_TEST]: this.ctx.run.isTest,
2121
2123
  [SemanticInternalAttributes.ORGANIZATION_SLUG]: this.ctx.organization.slug,
2122
2124
  [SemanticInternalAttributes.ORGANIZATION_NAME]: this.ctx.organization.name,
2123
- [SemanticInternalAttributes.BATCH_ID]: this.ctx.batch?.id
2125
+ [SemanticInternalAttributes.BATCH_ID]: this.ctx.batch?.id,
2126
+ [SemanticInternalAttributes.IDEMPOTENCY_KEY]: this.ctx.run.idempotencyKey
2124
2127
  };
2125
2128
  }
2126
2129
  return {};
@@ -2211,6 +2214,27 @@ var _ApiClient = class _ApiClient {
2211
2214
  this.accessToken = accessToken;
2212
2215
  this.baseUrl = baseUrl.replace(/\/$/, "");
2213
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
+ }
2214
2238
  triggerTask(taskId, body, options) {
2215
2239
  return zodfetch(TriggerTaskResponse, `${this.baseUrl}/api/v1/tasks/${taskId}/trigger`, {
2216
2240
  method: "POST",
@@ -3456,12 +3480,23 @@ __name(_NoopTaskLogger, "NoopTaskLogger");
3456
3480
  var NoopTaskLogger = _NoopTaskLogger;
3457
3481
  function safeJsonProcess(value) {
3458
3482
  try {
3459
- return JSON.parse(JSON.stringify(value));
3483
+ return JSON.parse(JSON.stringify(value, jsonErrorReplacer));
3460
3484
  } catch {
3461
3485
  return value;
3462
3486
  }
3463
3487
  }
3464
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");
3465
3500
 
3466
3501
  // src/v3/logger/index.ts
3467
3502
  var API_NAME3 = "logger";
@@ -3734,10 +3769,9 @@ var _DevRuntimeManager = class _DevRuntimeManager {
3734
3769
  this._pendingCompletionNotifications.delete(params.id);
3735
3770
  return pendingCompletion;
3736
3771
  }
3737
- const promise = new Promise((resolve, reject) => {
3772
+ const promise = new Promise((resolve) => {
3738
3773
  this._taskWaits.set(params.id, {
3739
- resolve,
3740
- reject
3774
+ resolve
3741
3775
  });
3742
3776
  });
3743
3777
  return await promise;
@@ -3754,16 +3788,11 @@ var _DevRuntimeManager = class _DevRuntimeManager {
3754
3788
  const pendingCompletion = this._pendingCompletionNotifications.get(runId);
3755
3789
  if (pendingCompletion) {
3756
3790
  this._pendingCompletionNotifications.delete(runId);
3757
- if (pendingCompletion.ok) {
3758
- resolve(pendingCompletion);
3759
- } else {
3760
- reject(pendingCompletion);
3761
- }
3791
+ resolve(pendingCompletion);
3762
3792
  return;
3763
3793
  }
3764
3794
  this._taskWaits.set(runId, {
3765
- resolve,
3766
- reject
3795
+ resolve
3767
3796
  });
3768
3797
  });
3769
3798
  }));
@@ -3779,11 +3808,7 @@ var _DevRuntimeManager = class _DevRuntimeManager {
3779
3808
  this._pendingCompletionNotifications.set(execution.run.id, completion);
3780
3809
  return;
3781
3810
  }
3782
- if (completion.ok) {
3783
- wait.resolve(completion);
3784
- } else {
3785
- wait.reject(completion);
3786
- }
3811
+ wait.resolve(completion);
3787
3812
  this._taskWaits.delete(execution.run.id);
3788
3813
  }
3789
3814
  };
@@ -3840,16 +3865,17 @@ var _ProdRuntimeManager = class _ProdRuntimeManager {
3840
3865
  return this.waitForDuration(date.getTime() - Date.now());
3841
3866
  }
3842
3867
  async waitForTask(params) {
3843
- const promise = new Promise((resolve, reject) => {
3868
+ const promise = new Promise((resolve) => {
3844
3869
  this._taskWaits.set(params.id, {
3845
- resolve,
3846
- reject
3870
+ resolve
3847
3871
  });
3848
3872
  });
3849
3873
  await this.ipc.send("WAIT_FOR_TASK", {
3850
3874
  friendlyId: params.id
3851
3875
  });
3852
- return await promise;
3876
+ const result = await promise;
3877
+ clock.reset();
3878
+ return result;
3853
3879
  }
3854
3880
  async waitForBatch(params) {
3855
3881
  if (!params.runs.length) {
@@ -3861,8 +3887,7 @@ var _ProdRuntimeManager = class _ProdRuntimeManager {
3861
3887
  const promise = Promise.all(params.runs.map((runId) => {
3862
3888
  return new Promise((resolve, reject) => {
3863
3889
  this._taskWaits.set(runId, {
3864
- resolve,
3865
- reject
3890
+ resolve
3866
3891
  });
3867
3892
  });
3868
3893
  }));
@@ -3871,6 +3896,7 @@ var _ProdRuntimeManager = class _ProdRuntimeManager {
3871
3896
  runFriendlyIds: params.runs
3872
3897
  });
3873
3898
  const results = await promise;
3899
+ clock.reset();
3874
3900
  return {
3875
3901
  id: params.id,
3876
3902
  items: results
@@ -3881,11 +3907,7 @@ var _ProdRuntimeManager = class _ProdRuntimeManager {
3881
3907
  if (!wait) {
3882
3908
  return;
3883
3909
  }
3884
- if (completion.ok) {
3885
- wait.resolve(completion);
3886
- } else {
3887
- wait.reject(completion);
3888
- }
3910
+ wait.resolve(completion);
3889
3911
  this._taskWaits.delete(execution.run.id);
3890
3912
  }
3891
3913
  get waitThresholdInMs() {
@@ -4774,7 +4796,7 @@ var _TaskExecutor = class _TaskExecutor {
4774
4796
  }
4775
4797
  return {
4776
4798
  ok: true,
4777
- id: execution.attempt.id,
4799
+ id: execution.run.id,
4778
4800
  output: finalOutput.data,
4779
4801
  outputType: finalOutput.dataType
4780
4802
  };
@@ -4782,7 +4804,7 @@ var _TaskExecutor = class _TaskExecutor {
4782
4804
  recordSpanException(span, stringifyError);
4783
4805
  return {
4784
4806
  ok: false,
4785
- id: execution.attempt.id,
4807
+ id: execution.run.id,
4786
4808
  error: {
4787
4809
  type: "INTERNAL_ERROR",
4788
4810
  code: TaskRunErrorCodes.TASK_OUTPUT_ERROR,
@@ -4795,7 +4817,7 @@ var _TaskExecutor = class _TaskExecutor {
4795
4817
  const handleErrorResult = await __privateMethod(this, _handleError, handleError_fn).call(this, execution, runError, parsedPayload, ctx);
4796
4818
  recordSpanException(span, handleErrorResult.error ?? runError);
4797
4819
  return {
4798
- id: execution.attempt.id,
4820
+ id: execution.run.id,
4799
4821
  ok: false,
4800
4822
  error: handleErrorResult.error ? parseError(handleErrorResult.error) : parseError(runError),
4801
4823
  retry: handleErrorResult.status === "retry" ? handleErrorResult.retry : void 0,
@@ -4805,7 +4827,7 @@ var _TaskExecutor = class _TaskExecutor {
4805
4827
  recordSpanException(span, handleErrorError);
4806
4828
  return {
4807
4829
  ok: false,
4808
- id: execution.attempt.id,
4830
+ id: execution.run.id,
4809
4831
  error: {
4810
4832
  type: "INTERNAL_ERROR",
4811
4833
  code: TaskRunErrorCodes.HANDLE_ERROR_ERROR,