@trigger.dev/sdk 3.0.0-beta.50 → 3.0.0-beta.52

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,7 +1,7 @@
1
- import { TriggerTracer, SemanticInternalAttributes, apiClientManager, isRequestOptions, mergeRequestOptions, accessoryAttributes, flattenAttributes, taskContext, runtime, usage as usage$1, taskCatalog, TimezonesResult, defaultRetryOptions, calculateNextRetryDelay, defaultFetchRetryOptions, calculateResetAt, eventFilterMatches, stringifyIO, logger, conditionallyImportPacket, parsePacket, createErrorTaskError } from '@trigger.dev/core/v3';
1
+ import { TriggerTracer, SemanticInternalAttributes, apiClientManager, isRequestOptions, mergeRequestOptions, accessoryAttributes, flattenAttributes, taskContext, runtime, usage as usage$1, UnprocessableEntityError, logger, taskCatalog, TimezonesResult, defaultRetryOptions, calculateNextRetryDelay, defaultFetchRetryOptions, calculateResetAt, eventFilterMatches, stringifyIO, conditionallyImportPacket, parsePacket, createErrorTaskError } from '@trigger.dev/core/v3';
2
2
  export { AbortTaskRunError, ApiError, AuthenticationError, BadRequestError, ConflictError, InternalServerError, NotFoundError, PermissionDeniedError, RateLimitError, UnprocessableEntityError, logger } from '@trigger.dev/core/v3';
3
3
  import { trace, context, SpanStatusCode, SpanKind } from '@opentelemetry/api';
4
- import { SEMATTRS_HTTP_STATUS_CODE, SEMATTRS_HTTP_METHOD, SEMATTRS_HTTP_URL, SEMATTRS_HTTP_HOST, SEMATTRS_HTTP_SCHEME, SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH, SEMATTRS_MESSAGING_OPERATION, SEMATTRS_MESSAGING_DESTINATION, SEMATTRS_MESSAGING_SYSTEM } from '@opentelemetry/semantic-conventions';
4
+ import { SEMATTRS_HTTP_STATUS_CODE, SEMATTRS_HTTP_METHOD, SEMATTRS_HTTP_URL, SEMATTRS_HTTP_HOST, SEMATTRS_HTTP_SCHEME, SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH, SEMATTRS_MESSAGING_OPERATION, SEMATTRS_MESSAGING_SYSTEM, SEMATTRS_MESSAGING_DESTINATION } from '@opentelemetry/semantic-conventions';
5
5
  import { AsyncLocalStorage } from 'node:async_hooks';
6
6
  import { zodfetch } from '@trigger.dev/core/v3/zodfetch';
7
7
 
@@ -18,7 +18,7 @@ var __publicField = (obj, key, value) => {
18
18
  };
19
19
 
20
20
  // package.json
21
- var version = "3.0.0-beta.50";
21
+ var version = "3.0.0-beta.52";
22
22
 
23
23
  // src/v3/tracer.ts
24
24
  var tracer = new TriggerTracer({
@@ -685,16 +685,16 @@ function rescheduleRun(runId, body, requestOptions) {
685
685
  }
686
686
  __name(rescheduleRun, "rescheduleRun");
687
687
  var MAX_POLL_ATTEMPTS = 500;
688
- async function poll(handle, options, requestOptions) {
688
+ async function poll(runId, options, requestOptions) {
689
689
  let attempts = 0;
690
690
  while (attempts++ < MAX_POLL_ATTEMPTS) {
691
- const run = await runs.retrieve(handle, requestOptions);
691
+ const run = await runs.retrieve(runId, requestOptions);
692
692
  if (run.isCompleted) {
693
693
  return run;
694
694
  }
695
695
  await new Promise((resolve) => setTimeout(resolve, options?.pollIntervalMs ?? 1e3));
696
696
  }
697
- throw new Error(`Run ${handle} did not complete after ${MAX_POLL_ATTEMPTS} attempts`);
697
+ throw new Error(`Run ${typeof runId === "string" ? runId : runId.id} did not complete after ${MAX_POLL_ATTEMPTS} attempts`);
698
698
  }
699
699
  __name(poll, "poll");
700
700
  var idempotencyKeys = {
@@ -745,268 +745,33 @@ function queue(options) {
745
745
  }
746
746
  __name(queue, "queue");
747
747
  function createTask(params) {
748
+ const customQueue = params.queue ? queue({
749
+ name: params.queue?.name ?? `task/${params.id}`,
750
+ ...params.queue
751
+ }) : void 0;
748
752
  const task3 = {
749
753
  id: params.id,
750
754
  trigger: async (payload, options) => {
751
- const apiClient = apiClientManager.client;
752
- if (!apiClient) {
753
- throw apiClientMissingError();
754
- }
755
755
  const taskMetadata = taskCatalog.getTaskMetadata(params.id);
756
- const payloadPacket = await stringifyIO(payload);
757
- const handle = await apiClient.triggerTask(params.id, {
758
- payload: payloadPacket.data,
759
- options: {
760
- queue: options?.queue ?? params.queue,
761
- concurrencyKey: options?.concurrencyKey,
762
- test: taskContext.ctx?.run.isTest,
763
- payloadType: payloadPacket.dataType,
764
- idempotencyKey: await makeKey(options?.idempotencyKey),
765
- delay: options?.delay,
766
- ttl: options?.ttl,
767
- maxAttempts: options?.maxAttempts
768
- }
769
- }, {
770
- spanParentAsLink: true
771
- }, {
772
- name: taskMetadata ? `${taskMetadata.exportName}.trigger()` : `trigger()`,
773
- tracer,
774
- icon: "trigger",
775
- attributes: {
776
- [SEMATTRS_MESSAGING_OPERATION]: "publish",
777
- ["messaging.client_id"]: taskContext.worker?.id,
778
- [SEMATTRS_MESSAGING_DESTINATION]: params.queue?.name ?? params.id,
779
- [SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev",
780
- ...accessoryAttributes({
781
- items: [
782
- {
783
- text: params.id,
784
- variant: "normal"
785
- }
786
- ],
787
- style: "codepath"
788
- })
789
- },
790
- onResponseBody: (body, span) => {
791
- body && typeof body === "object" && !Array.isArray(body) && "id" in body && typeof body.id === "string" && span.setAttribute("messaging.message.id", body.id);
792
- }
756
+ return await trigger_internal(taskMetadata && taskMetadata.exportName ? `${taskMetadata.exportName}.trigger()` : `trigger()`, params.id, payload, {
757
+ queue: customQueue,
758
+ ...options
793
759
  });
794
- return handle;
795
760
  },
796
761
  batchTrigger: async (items) => {
797
- const apiClient = apiClientManager.client;
798
- if (!apiClient) {
799
- throw apiClientMissingError();
800
- }
801
762
  const taskMetadata = taskCatalog.getTaskMetadata(params.id);
802
- const response = await apiClient.batchTriggerTask(params.id, {
803
- items: await Promise.all(items.map(async (item) => {
804
- const payloadPacket = await stringifyIO(item.payload);
805
- return {
806
- payload: payloadPacket.data,
807
- options: {
808
- queue: item.options?.queue ?? params.queue,
809
- concurrencyKey: item.options?.concurrencyKey,
810
- test: taskContext.ctx?.run.isTest,
811
- payloadType: payloadPacket.dataType,
812
- idempotencyKey: await makeKey(item.options?.idempotencyKey),
813
- delay: item.options?.delay,
814
- ttl: item.options?.ttl,
815
- maxAttempts: item.options?.maxAttempts
816
- }
817
- };
818
- }))
819
- }, {
820
- spanParentAsLink: true
821
- }, {
822
- name: taskMetadata ? `${taskMetadata.exportName}.batchTrigger()` : `batchTrigger()`,
823
- icon: "trigger",
824
- tracer,
825
- attributes: {
826
- [SEMATTRS_MESSAGING_OPERATION]: "publish",
827
- ["messaging.batch.message_count"]: items.length,
828
- ["messaging.client_id"]: taskContext.worker?.id,
829
- [SEMATTRS_MESSAGING_DESTINATION]: params.queue?.name ?? params.id,
830
- [SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev",
831
- ...accessoryAttributes({
832
- items: [
833
- {
834
- text: params.id,
835
- variant: "normal"
836
- }
837
- ],
838
- style: "codepath"
839
- })
840
- }
841
- });
842
- const handle = {
843
- batchId: response.batchId,
844
- runs: response.runs.map((id) => ({
845
- id
846
- }))
847
- };
848
- return handle;
763
+ return await batchTrigger_internal(taskMetadata && taskMetadata.exportName ? `${taskMetadata.exportName}.batchTrigger()` : `batchTrigger()`, params.id, items, void 0, customQueue);
849
764
  },
850
765
  triggerAndWait: async (payload, options) => {
851
- const ctx = taskContext.ctx;
852
- if (!ctx) {
853
- throw new Error("triggerAndWait can only be used from inside a task.run()");
854
- }
855
- const apiClient = apiClientManager.client;
856
- if (!apiClient) {
857
- throw apiClientMissingError();
858
- }
859
766
  const taskMetadata = taskCatalog.getTaskMetadata(params.id);
860
- const payloadPacket = await stringifyIO(payload);
861
- return await tracer.startActiveSpan(taskMetadata ? `${taskMetadata.exportName}.triggerAndWait()` : `triggerAndWait()`, async (span) => {
862
- const response = await apiClient.triggerTask(params.id, {
863
- payload: payloadPacket.data,
864
- options: {
865
- dependentAttempt: ctx.attempt.id,
866
- lockToVersion: taskContext.worker?.version,
867
- queue: options?.queue ?? params.queue,
868
- concurrencyKey: options?.concurrencyKey,
869
- test: taskContext.ctx?.run.isTest,
870
- payloadType: payloadPacket.dataType,
871
- idempotencyKey: await makeKey(options?.idempotencyKey),
872
- delay: options?.delay,
873
- ttl: options?.ttl,
874
- maxAttempts: options?.maxAttempts
875
- }
876
- });
877
- span.setAttribute("messaging.message.id", response.id);
878
- if (options?.idempotencyKey) {
879
- const result2 = await apiClient.getRunResult(response.id);
880
- if (result2) {
881
- logger.log(`Result reused from previous task run with idempotency key '${options.idempotencyKey}'.`, {
882
- runId: response.id,
883
- idempotencyKey: options.idempotencyKey
884
- });
885
- return await handleTaskRunExecutionResult(result2);
886
- }
887
- }
888
- const result = await runtime.waitForTask({
889
- id: response.id,
890
- ctx
891
- });
892
- return await handleTaskRunExecutionResult(result);
893
- }, {
894
- kind: SpanKind.PRODUCER,
895
- attributes: {
896
- [SemanticInternalAttributes.STYLE_ICON]: "trigger",
897
- [SEMATTRS_MESSAGING_OPERATION]: "publish",
898
- ["messaging.client_id"]: taskContext.worker?.id,
899
- [SEMATTRS_MESSAGING_DESTINATION]: params.queue?.name ?? params.id,
900
- [SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev",
901
- ...accessoryAttributes({
902
- items: [
903
- {
904
- text: params.id,
905
- variant: "normal"
906
- }
907
- ],
908
- style: "codepath"
909
- })
910
- }
767
+ return await triggerAndWait_internal(taskMetadata && taskMetadata.exportName ? `${taskMetadata.exportName}.triggerAndWait()` : `triggerAndWait()`, params.id, payload, {
768
+ queue: customQueue,
769
+ ...options
911
770
  });
912
771
  },
913
772
  batchTriggerAndWait: async (items) => {
914
- const ctx = taskContext.ctx;
915
- if (!ctx) {
916
- throw new Error("batchTriggerAndWait can only be used from inside a task.run()");
917
- }
918
- const apiClient = apiClientManager.client;
919
- if (!apiClient) {
920
- throw apiClientMissingError();
921
- }
922
773
  const taskMetadata = taskCatalog.getTaskMetadata(params.id);
923
- return await tracer.startActiveSpan(taskMetadata ? `${taskMetadata.exportName}.batchTriggerAndWait()` : `batchTriggerAndWait()`, async (span) => {
924
- const response = await apiClient.batchTriggerTask(params.id, {
925
- items: await Promise.all(items.map(async (item) => {
926
- const payloadPacket = await stringifyIO(item.payload);
927
- return {
928
- payload: payloadPacket.data,
929
- options: {
930
- lockToVersion: taskContext.worker?.version,
931
- queue: item.options?.queue ?? params.queue,
932
- concurrencyKey: item.options?.concurrencyKey,
933
- test: taskContext.ctx?.run.isTest,
934
- payloadType: payloadPacket.dataType,
935
- idempotencyKey: await makeKey(item.options?.idempotencyKey),
936
- delay: item.options?.delay,
937
- ttl: item.options?.ttl,
938
- maxAttempts: item.options?.maxAttempts
939
- }
940
- };
941
- })),
942
- dependentAttempt: ctx.attempt.id
943
- });
944
- span.setAttribute("messaging.message.id", response.batchId);
945
- const getBatchResults = /* @__PURE__ */ __name(async () => {
946
- const hasIdempotencyKey = items.some((item) => item.options?.idempotencyKey);
947
- if (hasIdempotencyKey) {
948
- const results = await apiClient.getBatchResults(response.batchId);
949
- if (results) {
950
- return results;
951
- }
952
- }
953
- return {
954
- id: response.batchId,
955
- items: []
956
- };
957
- }, "getBatchResults");
958
- const existingResults = await getBatchResults();
959
- const incompleteRuns = response.runs.filter((runId) => !existingResults.items.some((item) => item.id === runId));
960
- if (incompleteRuns.length === 0) {
961
- logger.log(`Results reused from previous task runs because of the provided idempotency keys.`);
962
- const runs3 = await handleBatchTaskRunExecutionResult(existingResults.items);
963
- return {
964
- id: existingResults.id,
965
- runs: runs3
966
- };
967
- }
968
- const result = await runtime.waitForBatch({
969
- id: response.batchId,
970
- runs: incompleteRuns,
971
- ctx
972
- });
973
- const combinedItems = [];
974
- for (const runId of response.runs) {
975
- const existingItem = existingResults.items.find((item) => item.id === runId);
976
- if (existingItem) {
977
- combinedItems.push(existingItem);
978
- } else {
979
- const newItem = result.items.find((item) => item.id === runId);
980
- if (newItem) {
981
- combinedItems.push(newItem);
982
- }
983
- }
984
- }
985
- const runs2 = await handleBatchTaskRunExecutionResult(combinedItems);
986
- return {
987
- id: result.id,
988
- runs: runs2
989
- };
990
- }, {
991
- kind: SpanKind.PRODUCER,
992
- attributes: {
993
- [SEMATTRS_MESSAGING_OPERATION]: "publish",
994
- ["messaging.batch.message_count"]: items.length,
995
- ["messaging.client_id"]: taskContext.worker?.id,
996
- [SEMATTRS_MESSAGING_DESTINATION]: params.queue?.name ?? params.id,
997
- [SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev",
998
- [SemanticInternalAttributes.STYLE_ICON]: "trigger",
999
- ...accessoryAttributes({
1000
- items: [
1001
- {
1002
- text: params.id,
1003
- variant: "normal"
1004
- }
1005
- ],
1006
- style: "codepath"
1007
- })
1008
- }
1009
- });
774
+ return await batchTriggerAndWait_internal(taskMetadata && taskMetadata.exportName ? `${taskMetadata.exportName}.batchTriggerAndWait()` : `batchTriggerAndWait()`, params.id, items, void 0, customQueue);
1010
775
  }
1011
776
  };
1012
777
  taskCatalog.registerTaskMetadata({
@@ -1033,6 +798,27 @@ function createTask(params) {
1033
798
  }
1034
799
  __name(createTask, "createTask");
1035
800
  async function trigger(id, payload, options, requestOptions) {
801
+ return await trigger_internal("tasks.trigger()", id, payload, options, requestOptions);
802
+ }
803
+ __name(trigger, "trigger");
804
+ async function triggerAndWait(id, payload, options, requestOptions) {
805
+ return await triggerAndWait_internal("tasks.triggerAndWait()", id, payload, options, requestOptions);
806
+ }
807
+ __name(triggerAndWait, "triggerAndWait");
808
+ async function batchTriggerAndWait(id, items, requestOptions) {
809
+ return await batchTriggerAndWait_internal("tasks.batchTriggerAndWait()", id, items, requestOptions);
810
+ }
811
+ __name(batchTriggerAndWait, "batchTriggerAndWait");
812
+ async function triggerAndPoll(id, payload, options, requestOptions) {
813
+ const handle = await trigger(id, payload, options, requestOptions);
814
+ return runs.poll(handle, options, requestOptions);
815
+ }
816
+ __name(triggerAndPoll, "triggerAndPoll");
817
+ async function batchTrigger(id, items, requestOptions) {
818
+ return await batchTrigger_internal("tasks.batchTrigger()", id, items, requestOptions);
819
+ }
820
+ __name(batchTrigger, "batchTrigger");
821
+ async function trigger_internal(name2, id, payload, options, requestOptions) {
1036
822
  const apiClient = apiClientManager.client;
1037
823
  if (!apiClient) {
1038
824
  throw apiClientMissingError();
@@ -1048,12 +834,13 @@ async function trigger(id, payload, options, requestOptions) {
1048
834
  idempotencyKey: await makeKey(options?.idempotencyKey),
1049
835
  delay: options?.delay,
1050
836
  ttl: options?.ttl,
837
+ tags: options?.tags,
1051
838
  maxAttempts: options?.maxAttempts
1052
839
  }
1053
840
  }, {
1054
841
  spanParentAsLink: true
1055
842
  }, {
1056
- name: `tasks.trigger()`,
843
+ name: name2,
1057
844
  tracer,
1058
845
  icon: "trigger",
1059
846
  attributes: {
@@ -1077,18 +864,72 @@ async function trigger(id, payload, options, requestOptions) {
1077
864
  });
1078
865
  return handle;
1079
866
  }
1080
- __name(trigger, "trigger");
1081
- async function triggerAndWait(id, payload, options, requestOptions) {
867
+ __name(trigger_internal, "trigger_internal");
868
+ async function batchTrigger_internal(name2, id, items, requestOptions, queue2) {
869
+ const apiClient = apiClientManager.client;
870
+ if (!apiClient) {
871
+ throw apiClientMissingError();
872
+ }
873
+ const response = await apiClient.batchTriggerTask(id, {
874
+ items: await Promise.all(items.map(async (item) => {
875
+ const payloadPacket = await stringifyIO(item.payload);
876
+ return {
877
+ payload: payloadPacket.data,
878
+ options: {
879
+ queue: item.options?.queue ?? queue2,
880
+ concurrencyKey: item.options?.concurrencyKey,
881
+ test: taskContext.ctx?.run.isTest,
882
+ payloadType: payloadPacket.dataType,
883
+ idempotencyKey: await makeKey(item.options?.idempotencyKey),
884
+ delay: item.options?.delay,
885
+ ttl: item.options?.ttl,
886
+ tags: item.options?.tags,
887
+ maxAttempts: item.options?.maxAttempts
888
+ }
889
+ };
890
+ }))
891
+ }, {
892
+ spanParentAsLink: true
893
+ }, {
894
+ name: name2,
895
+ tracer,
896
+ icon: "trigger",
897
+ attributes: {
898
+ [SEMATTRS_MESSAGING_OPERATION]: "publish",
899
+ ["messaging.client_id"]: taskContext.worker?.id,
900
+ [SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev",
901
+ ...accessoryAttributes({
902
+ items: [
903
+ {
904
+ text: id,
905
+ variant: "normal"
906
+ }
907
+ ],
908
+ style: "codepath"
909
+ })
910
+ },
911
+ ...requestOptions
912
+ });
913
+ const handle = {
914
+ batchId: response.batchId,
915
+ runs: response.runs.map((id2) => ({
916
+ id: id2
917
+ }))
918
+ };
919
+ return handle;
920
+ }
921
+ __name(batchTrigger_internal, "batchTrigger_internal");
922
+ async function triggerAndWait_internal(name2, id, payload, options, requestOptions) {
1082
923
  const ctx = taskContext.ctx;
1083
924
  if (!ctx) {
1084
- throw new Error("tasks.triggerAndWait can only be used from inside a task.run()");
925
+ throw new Error("triggerAndWait can only be used from inside a task.run()");
1085
926
  }
1086
927
  const apiClient = apiClientManager.client;
1087
928
  if (!apiClient) {
1088
929
  throw apiClientMissingError();
1089
930
  }
1090
931
  const payloadPacket = await stringifyIO(payload);
1091
- return await tracer.startActiveSpan("tasks.triggerAndWait()", async (span) => {
932
+ return await tracer.startActiveSpan(name2, async (span) => {
1092
933
  const response = await apiClient.triggerTask(id, {
1093
934
  payload: payloadPacket.data,
1094
935
  options: {
@@ -1101,6 +942,7 @@ async function triggerAndWait(id, payload, options, requestOptions) {
1101
942
  idempotencyKey: await makeKey(options?.idempotencyKey),
1102
943
  delay: options?.delay,
1103
944
  ttl: options?.ttl,
945
+ tags: options?.tags,
1104
946
  maxAttempts: options?.maxAttempts
1105
947
  }
1106
948
  }, {}, requestOptions);
@@ -1140,43 +982,92 @@ async function triggerAndWait(id, payload, options, requestOptions) {
1140
982
  }
1141
983
  });
1142
984
  }
1143
- __name(triggerAndWait, "triggerAndWait");
1144
- async function triggerAndPoll(id, payload, options, requestOptions) {
1145
- const handle = await trigger(id, payload, options, requestOptions);
1146
- return runs.poll(handle, options, requestOptions);
1147
- }
1148
- __name(triggerAndPoll, "triggerAndPoll");
1149
- async function batchTrigger(id, items, requestOptions) {
985
+ __name(triggerAndWait_internal, "triggerAndWait_internal");
986
+ async function batchTriggerAndWait_internal(name2, id, items, requestOptions, queue2) {
987
+ const ctx = taskContext.ctx;
988
+ if (!ctx) {
989
+ throw new Error("batchTriggerAndWait can only be used from inside a task.run()");
990
+ }
1150
991
  const apiClient = apiClientManager.client;
1151
992
  if (!apiClient) {
1152
993
  throw apiClientMissingError();
1153
994
  }
1154
- const response = await apiClient.batchTriggerTask(id, {
1155
- items: await Promise.all(items.map(async (item) => {
1156
- const payloadPacket = await stringifyIO(item.payload);
1157
- return {
1158
- payload: payloadPacket.data,
1159
- options: {
1160
- queue: item.options?.queue,
1161
- concurrencyKey: item.options?.concurrencyKey,
1162
- test: taskContext.ctx?.run.isTest,
1163
- payloadType: payloadPacket.dataType,
1164
- idempotencyKey: await makeKey(item.options?.idempotencyKey),
1165
- delay: item.options?.delay,
1166
- ttl: item.options?.ttl,
1167
- maxAttempts: item.options?.maxAttempts
995
+ return await tracer.startActiveSpan(name2, async (span) => {
996
+ const response = await apiClient.batchTriggerTask(id, {
997
+ items: await Promise.all(items.map(async (item) => {
998
+ const payloadPacket = await stringifyIO(item.payload);
999
+ return {
1000
+ payload: payloadPacket.data,
1001
+ options: {
1002
+ lockToVersion: taskContext.worker?.version,
1003
+ queue: item.options?.queue ?? queue2,
1004
+ concurrencyKey: item.options?.concurrencyKey,
1005
+ test: taskContext.ctx?.run.isTest,
1006
+ payloadType: payloadPacket.dataType,
1007
+ idempotencyKey: await makeKey(item.options?.idempotencyKey),
1008
+ delay: item.options?.delay,
1009
+ ttl: item.options?.ttl,
1010
+ tags: item.options?.tags,
1011
+ maxAttempts: item.options?.maxAttempts
1012
+ }
1013
+ };
1014
+ })),
1015
+ dependentAttempt: ctx.attempt.id
1016
+ }, {}, requestOptions);
1017
+ span.setAttribute("messaging.message.id", response.batchId);
1018
+ const getBatchResults = /* @__PURE__ */ __name(async () => {
1019
+ const hasIdempotencyKey = items.some((item) => item.options?.idempotencyKey);
1020
+ if (hasIdempotencyKey) {
1021
+ const results = await apiClient.getBatchResults(response.batchId);
1022
+ if (results) {
1023
+ return results;
1168
1024
  }
1025
+ }
1026
+ return {
1027
+ id: response.batchId,
1028
+ items: []
1169
1029
  };
1170
- }))
1171
- }, {
1172
- spanParentAsLink: true
1030
+ }, "getBatchResults");
1031
+ const existingResults = await getBatchResults();
1032
+ const incompleteRuns = response.runs.filter((runId) => !existingResults.items.some((item) => item.id === runId));
1033
+ if (incompleteRuns.length === 0) {
1034
+ logger.log(`Results reused from previous task runs because of the provided idempotency keys.`);
1035
+ const runs3 = await handleBatchTaskRunExecutionResult(existingResults.items);
1036
+ return {
1037
+ id: existingResults.id,
1038
+ runs: runs3
1039
+ };
1040
+ }
1041
+ const result = await runtime.waitForBatch({
1042
+ id: response.batchId,
1043
+ runs: incompleteRuns,
1044
+ ctx
1045
+ });
1046
+ const combinedItems = [];
1047
+ for (const runId of response.runs) {
1048
+ const existingItem = existingResults.items.find((item) => item.id === runId);
1049
+ if (existingItem) {
1050
+ combinedItems.push(existingItem);
1051
+ } else {
1052
+ const newItem = result.items.find((item) => item.id === runId);
1053
+ if (newItem) {
1054
+ combinedItems.push(newItem);
1055
+ }
1056
+ }
1057
+ }
1058
+ const runs2 = await handleBatchTaskRunExecutionResult(combinedItems);
1059
+ return {
1060
+ id: result.id,
1061
+ runs: runs2
1062
+ };
1173
1063
  }, {
1174
- name: `tasks.batchTrigger()`,
1175
- tracer,
1176
- icon: "trigger",
1064
+ kind: SpanKind.PRODUCER,
1177
1065
  attributes: {
1066
+ [SemanticInternalAttributes.STYLE_ICON]: "trigger",
1067
+ ["messaging.batch.message_count"]: items.length,
1178
1068
  [SEMATTRS_MESSAGING_OPERATION]: "publish",
1179
1069
  ["messaging.client_id"]: taskContext.worker?.id,
1070
+ [SEMATTRS_MESSAGING_DESTINATION]: id,
1180
1071
  [SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev",
1181
1072
  ...accessoryAttributes({
1182
1073
  items: [
@@ -1187,18 +1078,10 @@ async function batchTrigger(id, items, requestOptions) {
1187
1078
  ],
1188
1079
  style: "codepath"
1189
1080
  })
1190
- },
1191
- ...requestOptions
1081
+ }
1192
1082
  });
1193
- const handle = {
1194
- batchId: response.batchId,
1195
- runs: response.runs.map((id2) => ({
1196
- id: id2
1197
- }))
1198
- };
1199
- return handle;
1200
1083
  }
1201
- __name(batchTrigger, "batchTrigger");
1084
+ __name(batchTriggerAndWait_internal, "batchTriggerAndWait_internal");
1202
1085
  async function handleBatchTaskRunExecutionResult(items) {
1203
1086
  const someObjectStoreOutputs = items.some((item) => item.ok && item.outputType === "application/store");
1204
1087
  if (!someObjectStoreOutputs) {
@@ -1274,7 +1157,8 @@ var tasks = {
1274
1157
  trigger,
1275
1158
  triggerAndPoll,
1276
1159
  batchTrigger,
1277
- triggerAndWait
1160
+ triggerAndWait,
1161
+ batchTriggerAndWait
1278
1162
  };
1279
1163
  var wait = {
1280
1164
  for: async (options) => {
@@ -1480,6 +1364,53 @@ var usage = {
1480
1364
  };
1481
1365
  }
1482
1366
  };
1367
+ var tags = {
1368
+ add: addTags
1369
+ };
1370
+ async function addTags(tags2, requestOptions) {
1371
+ const apiClient = apiClientManager.client;
1372
+ if (!apiClient) {
1373
+ throw apiClientMissingError();
1374
+ }
1375
+ const run = taskContext.ctx?.run;
1376
+ if (!run) {
1377
+ throw new Error("Can't set tags outside of a run. You can trigger a task and set tags in the options.");
1378
+ }
1379
+ const $requestOptions = mergeRequestOptions({
1380
+ tracer,
1381
+ name: "tags.set()",
1382
+ icon: "tag",
1383
+ attributes: {
1384
+ ...accessoryAttributes({
1385
+ items: [
1386
+ {
1387
+ text: typeof tags2 === "string" ? tags2 : tags2.join(", "),
1388
+ variant: "normal"
1389
+ }
1390
+ ],
1391
+ style: "codepath"
1392
+ })
1393
+ }
1394
+ }, requestOptions);
1395
+ try {
1396
+ await apiClient.addTags(run.id, {
1397
+ tags: tags2
1398
+ }, $requestOptions);
1399
+ } catch (error) {
1400
+ if (error instanceof UnprocessableEntityError) {
1401
+ logger.error(error.message, {
1402
+ existingTags: run.tags,
1403
+ newTags: tags2
1404
+ });
1405
+ return;
1406
+ }
1407
+ logger.error("Failed to set tags", {
1408
+ error
1409
+ });
1410
+ throw error;
1411
+ }
1412
+ }
1413
+ __name(addTags, "addTags");
1483
1414
 
1484
1415
  // src/v3/schedules/index.ts
1485
1416
  var schedules_exports = {};
@@ -1914,6 +1845,6 @@ function configure(options) {
1914
1845
  }
1915
1846
  __name(configure, "configure");
1916
1847
 
1917
- export { InMemoryCache, configure, createCache, envvars_exports as envvars, idempotencyKeys, isIdempotencyKey, queue, retry, runs, schedules_exports as schedules, task, tasks, usage, wait };
1848
+ export { InMemoryCache, configure, createCache, envvars_exports as envvars, idempotencyKeys, isIdempotencyKey, queue, retry, runs, schedules_exports as schedules, tags, task, tasks, usage, wait };
1918
1849
  //# sourceMappingURL=out.js.map
1919
1850
  //# sourceMappingURL=index.mjs.map