diff --git a/.changeset/fix-batch-by-idempotency-key-options.md b/.changeset/fix-batch-by-idempotency-key-options.md new file mode 100644 index 00000000000..46c452f937f --- /dev/null +++ b/.changeset/fix-batch-by-idempotency-key-options.md @@ -0,0 +1,5 @@ +--- +"@trigger.dev/sdk": patch +--- + +Attach idempotencyKeyOptions on batch.triggerBy* and related batch stream paths so scope and original keys are preserved diff --git a/packages/trigger-sdk/src/v3/shared.ts b/packages/trigger-sdk/src/v3/shared.ts index 203f83bc77b..224ad6d80d0 100644 --- a/packages/trigger-sdk/src/v3/shared.ts +++ b/packages/trigger-sdk/src/v3/shared.ts @@ -724,6 +724,13 @@ export async function batchTriggerById( flattenIdempotencyKey([options?.idempotencyKey, `${index}`]) ); + // Process item-specific idempotency key and extract options for storage + const itemIdempotencyKey = await makeIdempotencyKey(item.options?.idempotencyKey); + const finalIdempotencyKey = itemIdempotencyKey ?? batchItemIdempotencyKey; + const idempotencyKeyOptions = itemIdempotencyKey + ? getIdempotencyKeyOptions(itemIdempotencyKey) + : undefined; + return { index, task: item.id, @@ -739,9 +746,9 @@ export async function batchTriggerById( maxAttempts: item.options?.maxAttempts, metadata: item.options?.metadata, maxDuration: item.options?.maxDuration, - idempotencyKey: - (await makeIdempotencyKey(item.options?.idempotencyKey)) ?? batchItemIdempotencyKey, + idempotencyKey: finalIdempotencyKey?.toString(), idempotencyKeyTTL: item.options?.idempotencyKeyTTL ?? options?.idempotencyKeyTTL, + idempotencyKeyOptions, machine: item.options?.machine, priority: item.options?.priority, region: item.options?.region, @@ -980,6 +987,13 @@ export async function batchTriggerByIdAndWait( flattenIdempotencyKey([options?.idempotencyKey, `${index}`]) ); + // Process item-specific idempotency key and extract options for storage + const itemIdempotencyKey = await makeIdempotencyKey(item.options?.idempotencyKey); + const finalIdempotencyKey = itemIdempotencyKey ?? batchItemIdempotencyKey; + const idempotencyKeyOptions = itemIdempotencyKey + ? getIdempotencyKeyOptions(itemIdempotencyKey) + : undefined; + return { index, task: item.id, @@ -996,9 +1010,9 @@ export async function batchTriggerByIdAndWait( maxAttempts: item.options?.maxAttempts, metadata: item.options?.metadata, maxDuration: item.options?.maxDuration, - idempotencyKey: - (await makeIdempotencyKey(item.options?.idempotencyKey)) ?? batchItemIdempotencyKey, + idempotencyKey: finalIdempotencyKey?.toString(), idempotencyKeyTTL: item.options?.idempotencyKeyTTL ?? options?.idempotencyKeyTTL, + idempotencyKeyOptions, machine: item.options?.machine, priority: item.options?.priority, region: item.options?.region, @@ -1246,6 +1260,13 @@ export async function batchTriggerTasks( flattenIdempotencyKey([options?.idempotencyKey, `${index}`]) ); + // Process item-specific idempotency key and extract options for storage + const itemIdempotencyKey = await makeIdempotencyKey(item.options?.idempotencyKey); + const finalIdempotencyKey = itemIdempotencyKey ?? batchItemIdempotencyKey; + const idempotencyKeyOptions = itemIdempotencyKey + ? getIdempotencyKeyOptions(itemIdempotencyKey) + : undefined; + return { index, task: item.task.id, @@ -1261,9 +1282,9 @@ export async function batchTriggerTasks( maxAttempts: item.options?.maxAttempts, metadata: item.options?.metadata, maxDuration: item.options?.maxDuration, - idempotencyKey: - (await makeIdempotencyKey(item.options?.idempotencyKey)) ?? batchItemIdempotencyKey, + idempotencyKey: finalIdempotencyKey?.toString(), idempotencyKeyTTL: item.options?.idempotencyKeyTTL ?? options?.idempotencyKeyTTL, + idempotencyKeyOptions, machine: item.options?.machine, priority: item.options?.priority, region: item.options?.region, @@ -1507,6 +1528,13 @@ export async function batchTriggerAndWaitTasks( flattenIdempotencyKey([options?.idempotencyKey, `${index}`]) ); + // Process item-specific idempotency key and extract options for storage + const itemIdempotencyKey = await makeIdempotencyKey(item.options?.idempotencyKey); + const finalIdempotencyKey = itemIdempotencyKey ?? batchItemIdempotencyKey; + const idempotencyKeyOptions = itemIdempotencyKey + ? getIdempotencyKeyOptions(itemIdempotencyKey) + : undefined; + yield { index: index++, task: item.id, @@ -1914,9 +1949,9 @@ async function* transformBatchItemsStream( maxAttempts: item.options?.maxAttempts, metadata: item.options?.metadata, maxDuration: item.options?.maxDuration, - idempotencyKey: - (await makeIdempotencyKey(item.options?.idempotencyKey)) ?? batchItemIdempotencyKey, + idempotencyKey: finalIdempotencyKey?.toString(), idempotencyKeyTTL: item.options?.idempotencyKeyTTL ?? options?.idempotencyKeyTTL, + idempotencyKeyOptions, machine: item.options?.machine, priority: item.options?.priority, region: item.options?.region, @@ -1951,6 +1986,13 @@ async function* transformBatchItemsStreamForWait( flattenIdempotencyKey([options?.idempotencyKey, `${index}`]) ); + // Process item-specific idempotency key and extract options for storage + const itemIdempotencyKey = await makeIdempotencyKey(item.options?.idempotencyKey); + const finalIdempotencyKey = itemIdempotencyKey ?? batchItemIdempotencyKey; + const idempotencyKeyOptions = itemIdempotencyKey + ? getIdempotencyKeyOptions(itemIdempotencyKey) + : undefined; + yield { index: index++, task: item.id, @@ -1967,9 +2009,9 @@ async function* transformBatchItemsStreamForWait( maxAttempts: item.options?.maxAttempts, metadata: item.options?.metadata, maxDuration: item.options?.maxDuration, - idempotencyKey: - (await makeIdempotencyKey(item.options?.idempotencyKey)) ?? batchItemIdempotencyKey, + idempotencyKey: finalIdempotencyKey?.toString(), idempotencyKeyTTL: item.options?.idempotencyKeyTTL ?? options?.idempotencyKeyTTL, + idempotencyKeyOptions, machine: item.options?.machine, priority: item.options?.priority, region: item.options?.region, @@ -2002,6 +2044,13 @@ async function* transformBatchByTaskItemsStream( flattenIdempotencyKey([options?.idempotencyKey, `${index}`]) ); + // Process item-specific idempotency key and extract options for storage + const itemIdempotencyKey = await makeIdempotencyKey(item.options?.idempotencyKey); + const finalIdempotencyKey = itemIdempotencyKey ?? batchItemIdempotencyKey; + const idempotencyKeyOptions = itemIdempotencyKey + ? getIdempotencyKeyOptions(itemIdempotencyKey) + : undefined; + yield { index: index++, task: taskIdentifier, @@ -2121,9 +2184,9 @@ async function* transformSingleTaskBatchItemsStream( maxAttempts: item.options?.maxAttempts, metadata: item.options?.metadata, maxDuration: item.options?.maxDuration, - idempotencyKey: - (await makeIdempotencyKey(item.options?.idempotencyKey)) ?? batchItemIdempotencyKey, + idempotencyKey: finalIdempotencyKey?.toString(), idempotencyKeyTTL: item.options?.idempotencyKeyTTL ?? options?.idempotencyKeyTTL, + idempotencyKeyOptions, machine: item.options?.machine, priority: item.options?.priority, region: item.options?.region,