@trigger.dev/sdk 3.0.0-beta.46 → 3.0.0-beta.47

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.js CHANGED
@@ -10,8 +10,8 @@ var __defProp = Object.defineProperty;
10
10
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
11
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
12
12
  var __export = (target, all) => {
13
- for (var name in all)
14
- __defProp(target, name, { get: all[name], enumerable: true });
13
+ for (var name2 in all)
14
+ __defProp(target, name2, { get: all[name2], enumerable: true });
15
15
  };
16
16
  var __publicField = (obj, key, value) => {
17
17
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
@@ -19,7 +19,7 @@ var __publicField = (obj, key, value) => {
19
19
  };
20
20
 
21
21
  // package.json
22
- var version = "3.0.0-beta.46";
22
+ var version = "3.0.0-beta.47";
23
23
 
24
24
  // src/v3/tracer.ts
25
25
  var tracer = new v3.TriggerTracer({
@@ -124,6 +124,10 @@ function onThrow(fn, options) {
124
124
  innerSpan.setStatus({
125
125
  code: api.SpanStatusCode.ERROR
126
126
  });
127
+ if (e instanceof Error && e.name === "AbortTaskRunError") {
128
+ innerSpan.end();
129
+ throw e;
130
+ }
127
131
  const nextRetryDelay = v3.calculateNextRetryDelay(opts, attempt);
128
132
  if (!nextRetryDelay) {
129
133
  innerSpan.end();
@@ -510,63 +514,231 @@ var runs = {
510
514
  reschedule: rescheduleRun,
511
515
  poll
512
516
  };
513
- function listRuns(paramsOrProjectRef, params) {
517
+ function listRuns(paramsOrProjectRef, paramsOrOptions, requestOptions) {
514
518
  const apiClient = v3.apiClientManager.client;
515
519
  if (!apiClient) {
516
520
  throw apiClientMissingError();
517
521
  }
522
+ const $requestOptions = listRunsRequestOptions(paramsOrProjectRef, paramsOrOptions, requestOptions);
518
523
  if (typeof paramsOrProjectRef === "string") {
519
- return apiClient.listProjectRuns(paramsOrProjectRef, params);
524
+ if (v3.isRequestOptions(paramsOrOptions)) {
525
+ return apiClient.listProjectRuns(paramsOrProjectRef, {}, $requestOptions);
526
+ } else {
527
+ return apiClient.listProjectRuns(paramsOrProjectRef, paramsOrOptions, $requestOptions);
528
+ }
520
529
  }
521
- return apiClient.listRuns(params);
530
+ return apiClient.listRuns(paramsOrProjectRef, $requestOptions);
522
531
  }
523
532
  __name(listRuns, "listRuns");
524
- function retrieveRun(runId) {
533
+ function listRunsRequestOptions(paramsOrProjectRef, paramsOrOptions, requestOptions) {
534
+ if (typeof paramsOrProjectRef === "string") {
535
+ if (v3.isRequestOptions(paramsOrOptions)) {
536
+ return v3.mergeRequestOptions({
537
+ tracer,
538
+ name: "runs.list()",
539
+ icon: "runs",
540
+ attributes: {
541
+ projectRef: paramsOrProjectRef,
542
+ ...v3.accessoryAttributes({
543
+ items: [
544
+ {
545
+ text: paramsOrProjectRef,
546
+ variant: "normal"
547
+ }
548
+ ],
549
+ style: "codepath"
550
+ })
551
+ }
552
+ }, paramsOrOptions);
553
+ } else {
554
+ return v3.mergeRequestOptions({
555
+ tracer,
556
+ name: "runs.list()",
557
+ icon: "runs",
558
+ attributes: {
559
+ projectRef: paramsOrProjectRef,
560
+ ...v3.flattenAttributes(paramsOrOptions, "queryParams"),
561
+ ...v3.accessoryAttributes({
562
+ items: [
563
+ {
564
+ text: paramsOrProjectRef,
565
+ variant: "normal"
566
+ }
567
+ ],
568
+ style: "codepath"
569
+ })
570
+ }
571
+ }, requestOptions);
572
+ }
573
+ }
574
+ return v3.mergeRequestOptions({
575
+ tracer,
576
+ name: "runs.list()",
577
+ icon: "runs",
578
+ attributes: {
579
+ ...v3.flattenAttributes(paramsOrProjectRef, "queryParams")
580
+ }
581
+ }, v3.isRequestOptions(paramsOrOptions) ? paramsOrOptions : requestOptions);
582
+ }
583
+ __name(listRunsRequestOptions, "listRunsRequestOptions");
584
+ function retrieveRun(runId, requestOptions) {
525
585
  const apiClient = v3.apiClientManager.client;
526
586
  if (!apiClient) {
527
587
  throw apiClientMissingError();
528
588
  }
589
+ const $requestOptions = v3.mergeRequestOptions({
590
+ tracer,
591
+ name: "runs.retrieve()",
592
+ icon: "runs",
593
+ attributes: {
594
+ runId: typeof runId === "string" ? runId : runId.id,
595
+ ...v3.accessoryAttributes({
596
+ items: [
597
+ {
598
+ text: typeof runId === "string" ? runId : runId.id,
599
+ variant: "normal"
600
+ }
601
+ ],
602
+ style: "codepath"
603
+ })
604
+ }
605
+ }, requestOptions);
529
606
  if (typeof runId === "string") {
530
- return apiClient.retrieveRun(runId);
607
+ return apiClient.retrieveRun(runId, $requestOptions);
531
608
  } else {
532
- return apiClient.retrieveRun(runId.id);
609
+ return apiClient.retrieveRun(runId.id, $requestOptions);
533
610
  }
534
611
  }
535
612
  __name(retrieveRun, "retrieveRun");
536
- function replayRun(runId) {
613
+ function replayRun(runId, requestOptions) {
537
614
  const apiClient = v3.apiClientManager.client;
538
615
  if (!apiClient) {
539
616
  throw apiClientMissingError();
540
617
  }
541
- return apiClient.replayRun(runId);
618
+ const $requestOptions = v3.mergeRequestOptions({
619
+ tracer,
620
+ name: "runs.replay()",
621
+ icon: "runs",
622
+ attributes: {
623
+ runId,
624
+ ...v3.accessoryAttributes({
625
+ items: [
626
+ {
627
+ text: runId,
628
+ variant: "normal"
629
+ }
630
+ ],
631
+ style: "codepath"
632
+ })
633
+ }
634
+ }, requestOptions);
635
+ return apiClient.replayRun(runId, $requestOptions);
542
636
  }
543
637
  __name(replayRun, "replayRun");
544
- function cancelRun(runId) {
638
+ function cancelRun(runId, requestOptions) {
545
639
  const apiClient = v3.apiClientManager.client;
546
640
  if (!apiClient) {
547
641
  throw apiClientMissingError();
548
642
  }
549
- return apiClient.cancelRun(runId);
643
+ const $requestOptions = v3.mergeRequestOptions({
644
+ tracer,
645
+ name: "runs.cancel()",
646
+ icon: "runs",
647
+ attributes: {
648
+ runId,
649
+ ...v3.accessoryAttributes({
650
+ items: [
651
+ {
652
+ text: runId,
653
+ variant: "normal"
654
+ }
655
+ ],
656
+ style: "codepath"
657
+ })
658
+ }
659
+ }, requestOptions);
660
+ return apiClient.cancelRun(runId, $requestOptions);
550
661
  }
551
662
  __name(cancelRun, "cancelRun");
552
- function rescheduleRun(runId, body) {
663
+ function rescheduleRun(runId, body, requestOptions) {
553
664
  const apiClient = v3.apiClientManager.client;
554
665
  if (!apiClient) {
555
666
  throw apiClientMissingError();
556
667
  }
557
- return apiClient.rescheduleRun(runId, body);
668
+ const $requestOptions = v3.mergeRequestOptions({
669
+ tracer,
670
+ name: "runs.reschedule()",
671
+ icon: "runs",
672
+ attributes: {
673
+ runId,
674
+ ...v3.accessoryAttributes({
675
+ items: [
676
+ {
677
+ text: runId,
678
+ variant: "normal"
679
+ }
680
+ ],
681
+ style: "codepath"
682
+ })
683
+ }
684
+ }, requestOptions);
685
+ return apiClient.rescheduleRun(runId, body, $requestOptions);
558
686
  }
559
687
  __name(rescheduleRun, "rescheduleRun");
560
- async function poll(handle, options) {
561
- while (true) {
562
- const run = await runs.retrieve(handle);
688
+ var MAX_POLL_ATTEMPTS = 500;
689
+ async function poll(handle, options, requestOptions) {
690
+ let attempts = 0;
691
+ while (attempts++ < MAX_POLL_ATTEMPTS) {
692
+ const run = await runs.retrieve(handle, requestOptions);
563
693
  if (run.isCompleted) {
564
694
  return run;
565
695
  }
566
696
  await new Promise((resolve) => setTimeout(resolve, options?.pollIntervalMs ?? 1e3));
567
697
  }
698
+ throw new Error(`Run ${handle} did not complete after ${MAX_POLL_ATTEMPTS} attempts`);
568
699
  }
569
700
  __name(poll, "poll");
701
+ var idempotencyKeys = {
702
+ create: createIdempotencyKey
703
+ };
704
+ function isIdempotencyKey(value) {
705
+ return typeof value === "string" && value.length === 64;
706
+ }
707
+ __name(isIdempotencyKey, "isIdempotencyKey");
708
+ async function createIdempotencyKey(key, options) {
709
+ const idempotencyKey = await generateIdempotencyKey([
710
+ ...Array.isArray(key) ? key : [
711
+ key
712
+ ]
713
+ ].concat(injectScope(options?.scope ?? "run")));
714
+ return idempotencyKey;
715
+ }
716
+ __name(createIdempotencyKey, "createIdempotencyKey");
717
+ function injectScope(scope) {
718
+ switch (scope) {
719
+ case "run": {
720
+ if (v3.taskContext?.ctx) {
721
+ return [
722
+ v3.taskContext.ctx.run.id
723
+ ];
724
+ }
725
+ }
726
+ case "attempt": {
727
+ if (v3.taskContext?.ctx) {
728
+ return [
729
+ v3.taskContext.ctx.attempt.id
730
+ ];
731
+ }
732
+ }
733
+ }
734
+ return [];
735
+ }
736
+ __name(injectScope, "injectScope");
737
+ async function generateIdempotencyKey(keyMaterial) {
738
+ const hash = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(keyMaterial.join("-")));
739
+ return Array.from(new Uint8Array(hash)).map((byte) => byte.toString(16).padStart(2, "0")).join("");
740
+ }
741
+ __name(generateIdempotencyKey, "generateIdempotencyKey");
570
742
 
571
743
  // src/v3/shared.ts
572
744
  function queue(options) {
@@ -583,40 +755,41 @@ function createTask(params) {
583
755
  }
584
756
  const taskMetadata = v3.taskCatalog.getTaskMetadata(params.id);
585
757
  const payloadPacket = await v3.stringifyIO(payload);
586
- const handle = await tracer.startActiveSpan(taskMetadata ? "Trigger" : `${params.id} trigger()`, async (span) => {
587
- const response = await apiClient.triggerTask(params.id, {
588
- payload: payloadPacket.data,
589
- options: {
590
- queue: options?.queue ?? params.queue,
591
- concurrencyKey: options?.concurrencyKey,
592
- test: v3.taskContext.ctx?.run.isTest,
593
- payloadType: payloadPacket.dataType,
594
- idempotencyKey: options?.idempotencyKey,
595
- delay: options?.delay,
596
- ttl: options?.ttl
597
- }
598
- }, {
599
- spanParentAsLink: true
600
- });
601
- span.setAttribute("messaging.message.id", response.id);
602
- return response;
758
+ const handle = await apiClient.triggerTask(params.id, {
759
+ payload: payloadPacket.data,
760
+ options: {
761
+ queue: options?.queue ?? params.queue,
762
+ concurrencyKey: options?.concurrencyKey,
763
+ test: v3.taskContext.ctx?.run.isTest,
764
+ payloadType: payloadPacket.dataType,
765
+ idempotencyKey: await makeKey(options?.idempotencyKey),
766
+ delay: options?.delay,
767
+ ttl: options?.ttl,
768
+ maxAttempts: options?.maxAttempts
769
+ }
603
770
  }, {
604
- kind: api.SpanKind.PRODUCER,
771
+ spanParentAsLink: true
772
+ }, {
773
+ name: taskMetadata ? `${taskMetadata.exportName}.trigger()` : `trigger()`,
774
+ tracer,
775
+ icon: "trigger",
605
776
  attributes: {
606
777
  [semanticConventions.SEMATTRS_MESSAGING_OPERATION]: "publish",
607
- [v3.SemanticInternalAttributes.STYLE_ICON]: "trigger",
608
778
  ["messaging.client_id"]: v3.taskContext.worker?.id,
609
779
  [semanticConventions.SEMATTRS_MESSAGING_DESTINATION]: params.queue?.name ?? params.id,
610
780
  [semanticConventions.SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev",
611
- ...taskMetadata ? v3.accessoryAttributes({
781
+ ...v3.accessoryAttributes({
612
782
  items: [
613
783
  {
614
- text: `${taskMetadata.exportName}.trigger()`,
784
+ text: params.id,
615
785
  variant: "normal"
616
786
  }
617
787
  ],
618
788
  style: "codepath"
619
- }) : {}
789
+ })
790
+ },
791
+ onResponseBody: (body, span) => {
792
+ body && typeof body === "object" && !Array.isArray(body) && "id" in body && typeof body.id === "string" && span.setAttribute("messaging.message.id", body.id);
620
793
  }
621
794
  });
622
795
  return handle;
@@ -627,55 +800,53 @@ function createTask(params) {
627
800
  throw apiClientMissingError();
628
801
  }
629
802
  const taskMetadata = v3.taskCatalog.getTaskMetadata(params.id);
630
- const response = await tracer.startActiveSpan(taskMetadata ? "Batch trigger" : `${params.id} batchTrigger()`, async (span) => {
631
- const response2 = await apiClient.batchTriggerTask(params.id, {
632
- items: await Promise.all(items.map(async (item) => {
633
- const payloadPacket = await v3.stringifyIO(item.payload);
634
- return {
635
- payload: payloadPacket.data,
636
- options: {
637
- queue: item.options?.queue ?? params.queue,
638
- concurrencyKey: item.options?.concurrencyKey,
639
- test: v3.taskContext.ctx?.run.isTest,
640
- payloadType: payloadPacket.dataType,
641
- idempotencyKey: item.options?.idempotencyKey,
642
- delay: item.options?.delay,
643
- ttl: item.options?.ttl
644
- }
645
- };
646
- }))
647
- }, {
648
- spanParentAsLink: true
649
- });
650
- span.setAttribute("messaging.message.id", response2.batchId);
651
- const handle = {
652
- batchId: response2.batchId,
653
- runs: response2.runs.map((id) => ({
654
- id
655
- }))
656
- };
657
- return handle;
803
+ const response = await apiClient.batchTriggerTask(params.id, {
804
+ items: await Promise.all(items.map(async (item) => {
805
+ const payloadPacket = await v3.stringifyIO(item.payload);
806
+ return {
807
+ payload: payloadPacket.data,
808
+ options: {
809
+ queue: item.options?.queue ?? params.queue,
810
+ concurrencyKey: item.options?.concurrencyKey,
811
+ test: v3.taskContext.ctx?.run.isTest,
812
+ payloadType: payloadPacket.dataType,
813
+ idempotencyKey: await makeKey(item.options?.idempotencyKey),
814
+ delay: item.options?.delay,
815
+ ttl: item.options?.ttl,
816
+ maxAttempts: item.options?.maxAttempts
817
+ }
818
+ };
819
+ }))
658
820
  }, {
659
- kind: api.SpanKind.PRODUCER,
821
+ spanParentAsLink: true
822
+ }, {
823
+ name: taskMetadata ? `${taskMetadata.exportName}.batchTrigger()` : `batchTrigger()`,
824
+ icon: "trigger",
825
+ tracer,
660
826
  attributes: {
661
827
  [semanticConventions.SEMATTRS_MESSAGING_OPERATION]: "publish",
662
828
  ["messaging.batch.message_count"]: items.length,
663
829
  ["messaging.client_id"]: v3.taskContext.worker?.id,
664
830
  [semanticConventions.SEMATTRS_MESSAGING_DESTINATION]: params.queue?.name ?? params.id,
665
831
  [semanticConventions.SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev",
666
- [v3.SemanticInternalAttributes.STYLE_ICON]: "trigger",
667
- ...taskMetadata ? v3.accessoryAttributes({
832
+ ...v3.accessoryAttributes({
668
833
  items: [
669
834
  {
670
- text: `${taskMetadata.exportName}.batchTrigger()`,
835
+ text: params.id,
671
836
  variant: "normal"
672
837
  }
673
838
  ],
674
839
  style: "codepath"
675
- }) : {}
840
+ })
676
841
  }
677
842
  });
678
- return response;
843
+ const handle = {
844
+ batchId: response.batchId,
845
+ runs: response.runs.map((id) => ({
846
+ id
847
+ }))
848
+ };
849
+ return handle;
679
850
  },
680
851
  triggerAndWait: async (payload, options) => {
681
852
  const ctx = v3.taskContext.ctx;
@@ -688,7 +859,7 @@ function createTask(params) {
688
859
  }
689
860
  const taskMetadata = v3.taskCatalog.getTaskMetadata(params.id);
690
861
  const payloadPacket = await v3.stringifyIO(payload);
691
- return await tracer.startActiveSpan(taskMetadata ? "Trigger" : `${params.id} triggerAndWait()`, async (span) => {
862
+ return await tracer.startActiveSpan(taskMetadata ? `${taskMetadata.exportName}.triggerAndWait()` : `triggerAndWait()`, async (span) => {
692
863
  const response = await apiClient.triggerTask(params.id, {
693
864
  payload: payloadPacket.data,
694
865
  options: {
@@ -698,9 +869,10 @@ function createTask(params) {
698
869
  concurrencyKey: options?.concurrencyKey,
699
870
  test: v3.taskContext.ctx?.run.isTest,
700
871
  payloadType: payloadPacket.dataType,
701
- idempotencyKey: options?.idempotencyKey,
872
+ idempotencyKey: await makeKey(options?.idempotencyKey),
702
873
  delay: options?.delay,
703
- ttl: options?.ttl
874
+ ttl: options?.ttl,
875
+ maxAttempts: options?.maxAttempts
704
876
  }
705
877
  });
706
878
  span.setAttribute("messaging.message.id", response.id);
@@ -727,15 +899,15 @@ function createTask(params) {
727
899
  ["messaging.client_id"]: v3.taskContext.worker?.id,
728
900
  [semanticConventions.SEMATTRS_MESSAGING_DESTINATION]: params.queue?.name ?? params.id,
729
901
  [semanticConventions.SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev",
730
- ...taskMetadata ? v3.accessoryAttributes({
902
+ ...v3.accessoryAttributes({
731
903
  items: [
732
904
  {
733
- text: `${taskMetadata.exportName}.triggerAndWait()`,
905
+ text: params.id,
734
906
  variant: "normal"
735
907
  }
736
908
  ],
737
909
  style: "codepath"
738
- }) : {}
910
+ })
739
911
  }
740
912
  });
741
913
  },
@@ -749,7 +921,7 @@ function createTask(params) {
749
921
  throw apiClientMissingError();
750
922
  }
751
923
  const taskMetadata = v3.taskCatalog.getTaskMetadata(params.id);
752
- return await tracer.startActiveSpan(taskMetadata ? "Batch trigger" : `${params.id} batchTriggerAndWait()`, async (span) => {
924
+ return await tracer.startActiveSpan(taskMetadata ? `${taskMetadata.exportName}.batchTriggerAndWait()` : `batchTriggerAndWait()`, async (span) => {
753
925
  const response = await apiClient.batchTriggerTask(params.id, {
754
926
  items: await Promise.all(items.map(async (item) => {
755
927
  const payloadPacket = await v3.stringifyIO(item.payload);
@@ -761,9 +933,10 @@ function createTask(params) {
761
933
  concurrencyKey: item.options?.concurrencyKey,
762
934
  test: v3.taskContext.ctx?.run.isTest,
763
935
  payloadType: payloadPacket.dataType,
764
- idempotencyKey: item.options?.idempotencyKey,
936
+ idempotencyKey: await makeKey(item.options?.idempotencyKey),
765
937
  delay: item.options?.delay,
766
- ttl: item.options?.ttl
938
+ ttl: item.options?.ttl,
939
+ maxAttempts: item.options?.maxAttempts
767
940
  }
768
941
  };
769
942
  })),
@@ -824,15 +997,15 @@ function createTask(params) {
824
997
  [semanticConventions.SEMATTRS_MESSAGING_DESTINATION]: params.queue?.name ?? params.id,
825
998
  [semanticConventions.SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev",
826
999
  [v3.SemanticInternalAttributes.STYLE_ICON]: "trigger",
827
- ...taskMetadata ? v3.accessoryAttributes({
1000
+ ...v3.accessoryAttributes({
828
1001
  items: [
829
1002
  {
830
- text: `${taskMetadata.exportName}.batchTriggerAndWait()`,
1003
+ text: params.id,
831
1004
  variant: "normal"
832
1005
  }
833
1006
  ],
834
1007
  style: "codepath"
835
- }) : {}
1008
+ })
836
1009
  }
837
1010
  });
838
1011
  }
@@ -860,7 +1033,7 @@ function createTask(params) {
860
1033
  return task3;
861
1034
  }
862
1035
  __name(createTask, "createTask");
863
- async function trigger(id, payload, options) {
1036
+ async function trigger(id, payload, options, requestOptions) {
864
1037
  const apiClient = v3.apiClientManager.client;
865
1038
  if (!apiClient) {
866
1039
  throw apiClientMissingError();
@@ -873,20 +1046,108 @@ async function trigger(id, payload, options) {
873
1046
  concurrencyKey: options?.concurrencyKey,
874
1047
  test: v3.taskContext.ctx?.run.isTest,
875
1048
  payloadType: payloadPacket.dataType,
876
- idempotencyKey: options?.idempotencyKey,
1049
+ idempotencyKey: await makeKey(options?.idempotencyKey),
877
1050
  delay: options?.delay,
878
- ttl: options?.ttl
1051
+ ttl: options?.ttl,
1052
+ maxAttempts: options?.maxAttempts
879
1053
  }
1054
+ }, {
1055
+ spanParentAsLink: true
1056
+ }, {
1057
+ name: `tasks.trigger()`,
1058
+ tracer,
1059
+ icon: "trigger",
1060
+ attributes: {
1061
+ [semanticConventions.SEMATTRS_MESSAGING_OPERATION]: "publish",
1062
+ ["messaging.client_id"]: v3.taskContext.worker?.id,
1063
+ [semanticConventions.SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev",
1064
+ ...v3.accessoryAttributes({
1065
+ items: [
1066
+ {
1067
+ text: id,
1068
+ variant: "normal"
1069
+ }
1070
+ ],
1071
+ style: "codepath"
1072
+ })
1073
+ },
1074
+ onResponseBody: (body, span) => {
1075
+ body && typeof body === "object" && !Array.isArray(body) && "id" in body && typeof body.id === "string" && span.setAttribute("messaging.message.id", body.id);
1076
+ },
1077
+ ...requestOptions
880
1078
  });
881
1079
  return handle;
882
1080
  }
883
1081
  __name(trigger, "trigger");
884
- async function triggerAndPoll(id, payload, options) {
885
- const handle = await trigger(id, payload, options);
886
- return runs.poll(handle);
1082
+ async function triggerAndWait(id, payload, options, requestOptions) {
1083
+ const ctx = v3.taskContext.ctx;
1084
+ if (!ctx) {
1085
+ throw new Error("tasks.triggerAndWait can only be used from inside a task.run()");
1086
+ }
1087
+ const apiClient = v3.apiClientManager.client;
1088
+ if (!apiClient) {
1089
+ throw apiClientMissingError();
1090
+ }
1091
+ const payloadPacket = await v3.stringifyIO(payload);
1092
+ return await tracer.startActiveSpan("tasks.triggerAndWait()", async (span) => {
1093
+ const response = await apiClient.triggerTask(id, {
1094
+ payload: payloadPacket.data,
1095
+ options: {
1096
+ dependentAttempt: ctx.attempt.id,
1097
+ lockToVersion: v3.taskContext.worker?.version,
1098
+ queue: options?.queue,
1099
+ concurrencyKey: options?.concurrencyKey,
1100
+ test: v3.taskContext.ctx?.run.isTest,
1101
+ payloadType: payloadPacket.dataType,
1102
+ idempotencyKey: await makeKey(options?.idempotencyKey),
1103
+ delay: options?.delay,
1104
+ ttl: options?.ttl,
1105
+ maxAttempts: options?.maxAttempts
1106
+ }
1107
+ }, {}, requestOptions);
1108
+ span.setAttribute("messaging.message.id", response.id);
1109
+ if (options?.idempotencyKey) {
1110
+ const result2 = await apiClient.getRunResult(response.id);
1111
+ if (result2) {
1112
+ v3.logger.log(`Result reused from previous task run with idempotency key '${options.idempotencyKey}'.`, {
1113
+ runId: response.id,
1114
+ idempotencyKey: options.idempotencyKey
1115
+ });
1116
+ return await handleTaskRunExecutionResult(result2);
1117
+ }
1118
+ }
1119
+ const result = await v3.runtime.waitForTask({
1120
+ id: response.id,
1121
+ ctx
1122
+ });
1123
+ return await handleTaskRunExecutionResult(result);
1124
+ }, {
1125
+ kind: api.SpanKind.PRODUCER,
1126
+ attributes: {
1127
+ [v3.SemanticInternalAttributes.STYLE_ICON]: "trigger",
1128
+ [semanticConventions.SEMATTRS_MESSAGING_OPERATION]: "publish",
1129
+ ["messaging.client_id"]: v3.taskContext.worker?.id,
1130
+ [semanticConventions.SEMATTRS_MESSAGING_DESTINATION]: id,
1131
+ [semanticConventions.SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev",
1132
+ ...v3.accessoryAttributes({
1133
+ items: [
1134
+ {
1135
+ text: id,
1136
+ variant: "normal"
1137
+ }
1138
+ ],
1139
+ style: "codepath"
1140
+ })
1141
+ }
1142
+ });
1143
+ }
1144
+ __name(triggerAndWait, "triggerAndWait");
1145
+ async function triggerAndPoll(id, payload, options, requestOptions) {
1146
+ const handle = await trigger(id, payload, options, requestOptions);
1147
+ return runs.poll(handle, options, requestOptions);
887
1148
  }
888
1149
  __name(triggerAndPoll, "triggerAndPoll");
889
- async function batchTrigger(id, items) {
1150
+ async function batchTrigger(id, items, requestOptions) {
890
1151
  const apiClient = v3.apiClientManager.client;
891
1152
  if (!apiClient) {
892
1153
  throw apiClientMissingError();
@@ -901,12 +1162,34 @@ async function batchTrigger(id, items) {
901
1162
  concurrencyKey: item.options?.concurrencyKey,
902
1163
  test: v3.taskContext.ctx?.run.isTest,
903
1164
  payloadType: payloadPacket.dataType,
904
- idempotencyKey: item.options?.idempotencyKey,
1165
+ idempotencyKey: await makeKey(item.options?.idempotencyKey),
905
1166
  delay: item.options?.delay,
906
- ttl: item.options?.ttl
1167
+ ttl: item.options?.ttl,
1168
+ maxAttempts: item.options?.maxAttempts
907
1169
  }
908
1170
  };
909
1171
  }))
1172
+ }, {
1173
+ spanParentAsLink: true
1174
+ }, {
1175
+ name: `tasks.batchTrigger()`,
1176
+ tracer,
1177
+ icon: "trigger",
1178
+ attributes: {
1179
+ [semanticConventions.SEMATTRS_MESSAGING_OPERATION]: "publish",
1180
+ ["messaging.client_id"]: v3.taskContext.worker?.id,
1181
+ [semanticConventions.SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev",
1182
+ ...v3.accessoryAttributes({
1183
+ items: [
1184
+ {
1185
+ text: id,
1186
+ variant: "normal"
1187
+ }
1188
+ ],
1189
+ style: "codepath"
1190
+ })
1191
+ },
1192
+ ...requestOptions
910
1193
  });
911
1194
  const handle = {
912
1195
  batchId: response.batchId,
@@ -970,6 +1253,18 @@ function apiClientMissingError() {
970
1253
  return `Unknown error`;
971
1254
  }
972
1255
  __name(apiClientMissingError, "apiClientMissingError");
1256
+ async function makeKey(idempotencyKey) {
1257
+ if (!idempotencyKey) {
1258
+ return;
1259
+ }
1260
+ if (isIdempotencyKey(idempotencyKey)) {
1261
+ return idempotencyKey;
1262
+ }
1263
+ return await idempotencyKeys.create(idempotencyKey, {
1264
+ scope: "global"
1265
+ });
1266
+ }
1267
+ __name(makeKey, "makeKey");
973
1268
 
974
1269
  // src/v3/tasks.ts
975
1270
  function task(options) {
@@ -979,7 +1274,8 @@ __name(task, "task");
979
1274
  var tasks = {
980
1275
  trigger,
981
1276
  triggerAndPoll,
982
- batchTrigger
1277
+ batchTrigger,
1278
+ triggerAndWait
983
1279
  };
984
1280
  var wait = {
985
1281
  for: async (options) => {
@@ -1207,60 +1503,166 @@ function task2(params) {
1207
1503
  return task3;
1208
1504
  }
1209
1505
  __name(task2, "task");
1210
- function create(options) {
1506
+ function create(options, requestOptions) {
1211
1507
  const apiClient = v3.apiClientManager.client;
1212
1508
  if (!apiClient) {
1213
1509
  throw apiClientMissingError();
1214
1510
  }
1215
- return apiClient.createSchedule(options);
1511
+ const $requestOptions = v3.mergeRequestOptions({
1512
+ tracer,
1513
+ name: "schedules.create()",
1514
+ icon: "clock",
1515
+ attributes: {
1516
+ ...v3.accessoryAttributes({
1517
+ items: [
1518
+ {
1519
+ text: options.cron,
1520
+ variant: "normal"
1521
+ }
1522
+ ],
1523
+ style: "codepath"
1524
+ })
1525
+ }
1526
+ }, requestOptions);
1527
+ return apiClient.createSchedule(options, $requestOptions);
1216
1528
  }
1217
1529
  __name(create, "create");
1218
- function retrieve(scheduleId) {
1530
+ function retrieve(scheduleId, requestOptions) {
1219
1531
  const apiClient = v3.apiClientManager.client;
1220
1532
  if (!apiClient) {
1221
1533
  throw apiClientMissingError();
1222
1534
  }
1223
- return apiClient.retrieveSchedule(scheduleId);
1535
+ const $requestOptions = v3.mergeRequestOptions({
1536
+ tracer,
1537
+ name: "schedules.retrieve()",
1538
+ icon: "clock",
1539
+ attributes: {
1540
+ scheduleId,
1541
+ ...v3.accessoryAttributes({
1542
+ items: [
1543
+ {
1544
+ text: scheduleId,
1545
+ variant: "normal"
1546
+ }
1547
+ ],
1548
+ style: "codepath"
1549
+ })
1550
+ }
1551
+ }, requestOptions);
1552
+ return apiClient.retrieveSchedule(scheduleId, $requestOptions);
1224
1553
  }
1225
1554
  __name(retrieve, "retrieve");
1226
- function update(scheduleId, options) {
1555
+ function update(scheduleId, options, requestOptions) {
1227
1556
  const apiClient = v3.apiClientManager.client;
1228
1557
  if (!apiClient) {
1229
1558
  throw apiClientMissingError();
1230
1559
  }
1231
- return apiClient.updateSchedule(scheduleId, options);
1560
+ const $requestOptions = v3.mergeRequestOptions({
1561
+ tracer,
1562
+ name: "schedules.update()",
1563
+ icon: "clock",
1564
+ attributes: {
1565
+ scheduleId,
1566
+ ...v3.accessoryAttributes({
1567
+ items: [
1568
+ {
1569
+ text: scheduleId,
1570
+ variant: "normal"
1571
+ }
1572
+ ],
1573
+ style: "codepath"
1574
+ })
1575
+ }
1576
+ }, requestOptions);
1577
+ return apiClient.updateSchedule(scheduleId, options, $requestOptions);
1232
1578
  }
1233
1579
  __name(update, "update");
1234
- function del(scheduleId) {
1580
+ function del(scheduleId, requestOptions) {
1235
1581
  const apiClient = v3.apiClientManager.client;
1236
1582
  if (!apiClient) {
1237
1583
  throw apiClientMissingError();
1238
1584
  }
1239
- return apiClient.deleteSchedule(scheduleId);
1585
+ const $requestOptions = v3.mergeRequestOptions({
1586
+ tracer,
1587
+ name: "schedules.delete()",
1588
+ icon: "clock",
1589
+ attributes: {
1590
+ scheduleId,
1591
+ ...v3.accessoryAttributes({
1592
+ items: [
1593
+ {
1594
+ text: scheduleId,
1595
+ variant: "normal"
1596
+ }
1597
+ ],
1598
+ style: "codepath"
1599
+ })
1600
+ }
1601
+ }, requestOptions);
1602
+ return apiClient.deleteSchedule(scheduleId, $requestOptions);
1240
1603
  }
1241
1604
  __name(del, "del");
1242
- function deactivate(scheduleId) {
1605
+ function deactivate(scheduleId, requestOptions) {
1243
1606
  const apiClient = v3.apiClientManager.client;
1244
1607
  if (!apiClient) {
1245
1608
  throw apiClientMissingError();
1246
1609
  }
1247
- return apiClient.deactivateSchedule(scheduleId);
1610
+ const $requestOptions = v3.mergeRequestOptions({
1611
+ tracer,
1612
+ name: "schedules.deactivate()",
1613
+ icon: "clock",
1614
+ attributes: {
1615
+ scheduleId,
1616
+ ...v3.accessoryAttributes({
1617
+ items: [
1618
+ {
1619
+ text: scheduleId,
1620
+ variant: "normal"
1621
+ }
1622
+ ],
1623
+ style: "codepath"
1624
+ })
1625
+ }
1626
+ }, requestOptions);
1627
+ return apiClient.deactivateSchedule(scheduleId, $requestOptions);
1248
1628
  }
1249
1629
  __name(deactivate, "deactivate");
1250
- function activate(scheduleId) {
1630
+ function activate(scheduleId, requestOptions) {
1251
1631
  const apiClient = v3.apiClientManager.client;
1252
1632
  if (!apiClient) {
1253
1633
  throw apiClientMissingError();
1254
1634
  }
1255
- return apiClient.activateSchedule(scheduleId);
1635
+ const $requestOptions = v3.mergeRequestOptions({
1636
+ tracer,
1637
+ name: "schedules.activate()",
1638
+ icon: "clock",
1639
+ attributes: {
1640
+ scheduleId,
1641
+ ...v3.accessoryAttributes({
1642
+ items: [
1643
+ {
1644
+ text: scheduleId,
1645
+ variant: "normal"
1646
+ }
1647
+ ],
1648
+ style: "codepath"
1649
+ })
1650
+ }
1651
+ }, requestOptions);
1652
+ return apiClient.activateSchedule(scheduleId, $requestOptions);
1256
1653
  }
1257
1654
  __name(activate, "activate");
1258
- function list(options) {
1655
+ function list(options, requestOptions) {
1259
1656
  const apiClient = v3.apiClientManager.client;
1260
1657
  if (!apiClient) {
1261
1658
  throw apiClientMissingError();
1262
1659
  }
1263
- return apiClient.listSchedules(options);
1660
+ const $requestOptions = v3.mergeRequestOptions({
1661
+ tracer,
1662
+ name: "schedules.list()",
1663
+ icon: "clock"
1664
+ }, requestOptions);
1665
+ return apiClient.listSchedules(options, $requestOptions);
1264
1666
  }
1265
1667
  __name(list, "list");
1266
1668
  function timezones(options) {
@@ -1287,14 +1689,15 @@ __export(envvars_exports, {
1287
1689
  update: () => update2,
1288
1690
  upload: () => upload
1289
1691
  });
1290
- function upload(projectRefOrParams, slug, params) {
1692
+ function upload(projectRefOrParams, slugOrRequestOptions, params, requestOptions) {
1291
1693
  let $projectRef;
1292
1694
  let $params;
1293
1695
  let $slug;
1696
+ const $requestOptions = overloadRequestOptions("upload", slugOrRequestOptions, requestOptions);
1294
1697
  if (v3.taskContext.ctx) {
1295
1698
  if (typeof projectRefOrParams === "string") {
1296
1699
  $projectRef = projectRefOrParams;
1297
- $slug = slug ?? v3.taskContext.ctx.environment.slug;
1700
+ $slug = typeof slugOrRequestOptions === "string" ? slugOrRequestOptions : v3.taskContext.ctx.environment.slug;
1298
1701
  if (!params) {
1299
1702
  throw new Error("params is required");
1300
1703
  }
@@ -1308,47 +1711,54 @@ function upload(projectRefOrParams, slug, params) {
1308
1711
  if (typeof projectRefOrParams !== "string") {
1309
1712
  throw new Error("projectRef is required");
1310
1713
  }
1311
- if (!slug) {
1714
+ if (!slugOrRequestOptions || typeof slugOrRequestOptions !== "string") {
1312
1715
  throw new Error("slug is required");
1313
1716
  }
1314
1717
  if (!params) {
1315
1718
  throw new Error("params is required");
1316
1719
  }
1317
1720
  $projectRef = projectRefOrParams;
1318
- $slug = slug;
1721
+ $slug = slugOrRequestOptions;
1319
1722
  $params = params;
1320
1723
  }
1321
1724
  const apiClient = v3.apiClientManager.client;
1322
1725
  if (!apiClient) {
1323
1726
  throw apiClientMissingError();
1324
1727
  }
1325
- return apiClient.importEnvVars($projectRef, $slug, $params);
1728
+ return apiClient.importEnvVars($projectRef, $slug, $params, $requestOptions);
1326
1729
  }
1327
1730
  __name(upload, "upload");
1328
- function list2(projectRef, slug) {
1329
- const $projectRef = projectRef ?? v3.taskContext.ctx?.project.ref;
1731
+ function list2(projectRefOrRequestOptions, slug, requestOptions) {
1732
+ const $projectRef = !v3.isRequestOptions(projectRefOrRequestOptions) ? projectRefOrRequestOptions : v3.taskContext.ctx?.project.ref;
1330
1733
  const $slug = slug ?? v3.taskContext.ctx?.environment.slug;
1734
+ let $requestOptions = v3.isRequestOptions(projectRefOrRequestOptions) ? projectRefOrRequestOptions : requestOptions;
1331
1735
  if (!$projectRef) {
1332
1736
  throw new Error("projectRef is required");
1333
1737
  }
1334
1738
  if (!$slug) {
1335
1739
  throw new Error("slug is required");
1336
1740
  }
1741
+ $requestOptions = v3.mergeRequestOptions({
1742
+ tracer,
1743
+ name: "envvars.list()",
1744
+ icon: "id-badge"
1745
+ }, $requestOptions);
1337
1746
  const apiClient = v3.apiClientManager.client;
1338
1747
  if (!apiClient) {
1339
1748
  throw apiClientMissingError();
1340
1749
  }
1341
- return apiClient.listEnvVars($projectRef, $slug);
1750
+ return apiClient.listEnvVars($projectRef, $slug, $requestOptions);
1342
1751
  }
1343
1752
  __name(list2, "list");
1344
- function create2(projectRefOrParams, slug, params) {
1753
+ function create2(projectRefOrParams, slugOrRequestOptions, params, requestOptions) {
1345
1754
  let $projectRef;
1346
1755
  let $slug;
1347
1756
  let $params;
1757
+ const $requestOptions = overloadRequestOptions("create", slugOrRequestOptions, requestOptions);
1348
1758
  if (v3.taskContext.ctx) {
1349
1759
  if (typeof projectRefOrParams === "string") {
1350
1760
  $projectRef = projectRefOrParams;
1351
- $slug = slug ?? v3.taskContext.ctx.environment.slug;
1761
+ $slug = typeof slugOrRequestOptions === "string" ? slugOrRequestOptions : v3.taskContext.ctx.environment.slug;
1352
1762
  if (!params) {
1353
1763
  throw new Error("params is required");
1354
1764
  }
@@ -1362,31 +1772,32 @@ function create2(projectRefOrParams, slug, params) {
1362
1772
  if (typeof projectRefOrParams !== "string") {
1363
1773
  throw new Error("projectRef is required");
1364
1774
  }
1365
- if (!slug) {
1775
+ if (!slugOrRequestOptions || typeof slugOrRequestOptions !== "string") {
1366
1776
  throw new Error("slug is required");
1367
1777
  }
1368
1778
  if (!params) {
1369
1779
  throw new Error("params is required");
1370
1780
  }
1371
1781
  $projectRef = projectRefOrParams;
1372
- $slug = slug;
1782
+ $slug = slugOrRequestOptions;
1373
1783
  $params = params;
1374
1784
  }
1375
1785
  const apiClient = v3.apiClientManager.client;
1376
1786
  if (!apiClient) {
1377
1787
  throw apiClientMissingError();
1378
1788
  }
1379
- return apiClient.createEnvVar($projectRef, $slug, $params);
1789
+ return apiClient.createEnvVar($projectRef, $slug, $params, $requestOptions);
1380
1790
  }
1381
1791
  __name(create2, "create");
1382
- function retrieve2(projectRefOrName, slug, name) {
1792
+ function retrieve2(projectRefOrName, slugOrRequestOptions, name1, requestOptions) {
1383
1793
  let $projectRef;
1384
1794
  let $slug;
1385
1795
  let $name;
1386
- if (typeof name === "string") {
1796
+ const $requestOptions = overloadRequestOptions("retrieve", slugOrRequestOptions, requestOptions);
1797
+ if (typeof name1 === "string") {
1387
1798
  $projectRef = projectRefOrName;
1388
- $slug = slug;
1389
- $name = name;
1799
+ $slug = typeof slugOrRequestOptions === "string" ? slugOrRequestOptions : v3.taskContext.ctx?.environment.slug;
1800
+ $name = name1;
1390
1801
  } else {
1391
1802
  $projectRef = v3.taskContext.ctx?.project.ref;
1392
1803
  $slug = v3.taskContext.ctx?.environment.slug;
@@ -1402,17 +1813,18 @@ function retrieve2(projectRefOrName, slug, name) {
1402
1813
  if (!apiClient) {
1403
1814
  throw apiClientMissingError();
1404
1815
  }
1405
- return apiClient.retrieveEnvVar($projectRef, $slug, $name);
1816
+ return apiClient.retrieveEnvVar($projectRef, $slug, $name, $requestOptions);
1406
1817
  }
1407
1818
  __name(retrieve2, "retrieve");
1408
- function del2(projectRefOrName, slug, name) {
1819
+ function del2(projectRefOrName, slugOrRequestOptions, name1, requestOptions) {
1409
1820
  let $projectRef;
1410
1821
  let $slug;
1411
1822
  let $name;
1412
- if (typeof name === "string") {
1823
+ const $requestOptions = overloadRequestOptions("del", slugOrRequestOptions, requestOptions);
1824
+ if (typeof name1 === "string") {
1413
1825
  $projectRef = projectRefOrName;
1414
- $slug = slug;
1415
- $name = name;
1826
+ $slug = typeof slugOrRequestOptions === "string" ? slugOrRequestOptions : v3.taskContext.ctx?.environment.slug;
1827
+ $name = name1;
1416
1828
  } else {
1417
1829
  $projectRef = v3.taskContext.ctx?.project.ref;
1418
1830
  $slug = v3.taskContext.ctx?.environment.slug;
@@ -1428,19 +1840,20 @@ function del2(projectRefOrName, slug, name) {
1428
1840
  if (!apiClient) {
1429
1841
  throw apiClientMissingError();
1430
1842
  }
1431
- return apiClient.deleteEnvVar($projectRef, $slug, $name);
1843
+ return apiClient.deleteEnvVar($projectRef, $slug, $name, $requestOptions);
1432
1844
  }
1433
1845
  __name(del2, "del");
1434
- function update2(projectRefOrName, slugOrParams, name, params) {
1846
+ function update2(projectRefOrName, slugOrParams, nameOrRequestOptions, params, requestOptions) {
1435
1847
  let $projectRef;
1436
1848
  let $slug;
1437
1849
  let $name;
1438
1850
  let $params;
1851
+ const $requestOptions = overloadRequestOptions("update", nameOrRequestOptions, requestOptions);
1439
1852
  if (v3.taskContext.ctx) {
1440
1853
  if (typeof slugOrParams === "string") {
1441
1854
  $projectRef = slugOrParams;
1442
1855
  $slug = slugOrParams ?? v3.taskContext.ctx.environment.slug;
1443
- $name = name;
1856
+ $name = typeof nameOrRequestOptions === "string" ? nameOrRequestOptions : v3.taskContext.ctx.environment.slug;
1444
1857
  if (!params) {
1445
1858
  throw new Error("params is required");
1446
1859
  }
@@ -1470,9 +1883,25 @@ function update2(projectRefOrName, slugOrParams, name, params) {
1470
1883
  if (!apiClient) {
1471
1884
  throw apiClientMissingError();
1472
1885
  }
1473
- return apiClient.updateEnvVar($projectRef, $slug, $name, $params);
1886
+ return apiClient.updateEnvVar($projectRef, $slug, $name, $params, $requestOptions);
1474
1887
  }
1475
1888
  __name(update2, "update");
1889
+ function overloadRequestOptions(name1, slugOrRequestOptions, requestOptions) {
1890
+ if (v3.isRequestOptions(slugOrRequestOptions)) {
1891
+ return v3.mergeRequestOptions({
1892
+ tracer,
1893
+ name: `envvars.${name1}()`,
1894
+ icon: "id-badge"
1895
+ }, slugOrRequestOptions);
1896
+ } else {
1897
+ return v3.mergeRequestOptions({
1898
+ tracer,
1899
+ name: `envvars.${name1}()`,
1900
+ icon: "id-badge"
1901
+ }, requestOptions);
1902
+ }
1903
+ }
1904
+ __name(overloadRequestOptions, "overloadRequestOptions");
1476
1905
 
1477
1906
  // src/v3/index.ts
1478
1907
  function configure(options) {
@@ -1524,6 +1953,8 @@ exports.InMemoryCache = InMemoryCache;
1524
1953
  exports.configure = configure;
1525
1954
  exports.createCache = createCache;
1526
1955
  exports.envvars = envvars_exports;
1956
+ exports.idempotencyKeys = idempotencyKeys;
1957
+ exports.isIdempotencyKey = isIdempotencyKey;
1527
1958
  exports.queue = queue;
1528
1959
  exports.retry = retry;
1529
1960
  exports.runs = runs;