@trigger.dev/sdk 4.8.6 → 4.8.8

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.
@@ -3694,9 +3694,11 @@ declare class ChatMessageAccumulator {
3694
3694
  } | undefined>) | undefined;
3695
3695
  /**
3696
3696
  * Run outer-loop compaction if needed. Call after adding the response
3697
- * and capturing usage. Pass the LAST step's usage (`result.usage`), which is
3698
- * the context the model held on its final call; `result.totalUsage` sums every
3699
- * step of a tool-using turn and belongs in `context.turnUsage`. Applies
3697
+ * and capturing usage. Pass the LAST step's usage
3698
+ * (`(await result.steps).at(-1)?.usage`), which is the context the model held on
3699
+ * its final call. `result.usage` is that on AI SDK 6 but the turn's sum on AI SDK 7,
3700
+ * so read `steps`. The turn's sum (`result.totalUsage` on AI SDK 6, `result.usage`
3701
+ * on AI SDK 7) belongs in `context.turnUsage`. Applies
3700
3702
  * `compactModelMessages` and `compactUIMessages` callbacks if configured.
3701
3703
  *
3702
3704
  * @returns `true` if compaction was performed, `false` otherwise.
@@ -64,6 +64,7 @@ const externalDeploymentId_js_1 = require("./externalDeploymentId.js");
64
64
  const chatVersionSkew_js_1 = require("./chatVersionSkew.js");
65
65
  const sessions_js_1 = require("./sessions.js");
66
66
  const shared_js_1 = require("./shared.js");
67
+ const toolOutputJson_js_1 = require("./toolOutputJson.js");
67
68
  const concurrency_shared_js_1 = require("./concurrency-shared.js");
68
69
  const streams_js_1 = require("./streams.js");
69
70
  const tracer_js_1 = require("./tracer.js");
@@ -80,10 +81,10 @@ function toModelMessages(messages, context) {
80
81
  // conditional spread keeps the options object byte-identical to the no-tools
81
82
  // path when nothing was declared.
82
83
  const tools = locals_js_1.locals.get(chatResolvedToolsKey);
83
- return (0, steeringContext_js_1.convertSteeredMessages)(messages, async (batch) => (0, ai_runtime_js_1.convertToModelMessages)(batch, {
84
+ return (0, steeringContext_js_1.convertSteeredMessages)(messages, async (batch) => (0, toolOutputJson_js_1.withJsonToolResults)(await (0, ai_runtime_js_1.convertToModelMessages)(batch, {
84
85
  ignoreIncompleteToolCalls: true,
85
86
  ...(tools ? { tools } : {}),
86
- }), locals_js_1.locals.get(chatSteeringInjectionsKey) ?? new Map(), context ?? locals_js_1.locals.get(chatCurrentUIMessagesKey) ?? messages, context !== undefined || locals_js_1.locals.get(chatCurrentUIMessagesKey) !== undefined);
87
+ })), locals_js_1.locals.get(chatSteeringInjectionsKey) ?? new Map(), context ?? locals_js_1.locals.get(chatCurrentUIMessagesKey) ?? messages, context !== undefined || locals_js_1.locals.get(chatCurrentUIMessagesKey) !== undefined);
87
88
  }
88
89
  const chatTurnContextKey = locals_js_1.locals.create("chat.turnContext");
89
90
  /**
@@ -2090,6 +2091,33 @@ function emptyUsage() {
2090
2091
  outputTokenDetails: { textTokens: undefined, reasoningTokens: undefined },
2091
2092
  };
2092
2093
  }
2094
+ /**
2095
+ * The last step's usage of a `streamText` result: what the model's context window held on
2096
+ * its final call. It comes from `steps` because the result's `usage` is the last step's on
2097
+ * AI SDK 6 but the turn's sum on AI SDK 7. A custom stream source with no `steps` falls
2098
+ * back to its `usage`, which such a source supplies as the last step's. Raced against a 2s
2099
+ * timeout like the turn's `totalUsage`, since the promises can hang on a stop-abort.
2100
+ * Returns undefined when neither resolves in time.
2101
+ */
2102
+ async function readLastStepUsage(result) {
2103
+ const stepsPromise = result != null ? result.steps : undefined;
2104
+ const hasSteps = typeof stepsPromise?.then === "function";
2105
+ const source = hasSteps ? stepsPromise : result != null ? result.usage : undefined;
2106
+ if (typeof source?.then !== "function")
2107
+ return undefined;
2108
+ try {
2109
+ const resolved = await Promise.race([
2110
+ source,
2111
+ new Promise((r) => setTimeout(() => r(undefined), 2_000)),
2112
+ ]);
2113
+ return hasSteps
2114
+ ? resolved?.at(-1)?.usage
2115
+ : resolved;
2116
+ }
2117
+ catch {
2118
+ return undefined;
2119
+ }
2120
+ }
2093
2121
  function addUsage(a, b) {
2094
2122
  const add = (x, y) => x != null || y != null ? (x ?? 0) + (y ?? 0) : undefined;
2095
2123
  return {
@@ -3700,13 +3728,27 @@ function toStreamTextOptions(options, retain = true) {
3700
3728
  // 4. Parallel tool results in call order, the order the next turn's history
3701
3729
  // will list them in. See `withToolResultsInCallOrder`.
3702
3730
  const base = resultMessages ?? messages;
3703
- const ordered = (0, toolResultOrder_js_1.withToolResultsInCallOrder)(base);
3704
- return resultMessages || ordered !== base ? { messages: ordered } : undefined;
3731
+ // 5. Tool results as plain JSON, which AI SDK 7 before 7.0.101 checks on
3732
+ // every step. See `withJsonToolResults`.
3733
+ const prepared = await (0, toolOutputJson_js_1.withJsonToolResults)((0, toolResultOrder_js_1.withToolResultsInCallOrder)(base));
3734
+ return resultMessages || prepared !== base ? { messages: prepared } : undefined;
3705
3735
  };
3706
3736
  }
3707
3737
  if (retain && typeof result.prepareStep === "function") {
3708
3738
  result.prepareStep = (0, steeringContext_js_1.retainStepMessages)(result.prepareStep);
3709
3739
  }
3740
+ /**
3741
+ * A steer that `pendingMessages.prepare` turns into a `system` message lands in
3742
+ * the model lane in that form, and so replays in every later turn's `messages`.
3743
+ * AI SDK 7 rejects a system message in `messages` unless this is set (its default
3744
+ * is `false`), so without it the step fails with an invalid-prompt error on the
3745
+ * turn that was steered and on every turn after it. Routing the note to
3746
+ * `instructions` for one step would not help the later turns, because the lane
3747
+ * is what the caller's own `streamText({ messages })` reads. A caller that wants
3748
+ * the strict behaviour passes `allowSystemInMessages: false` after the spread.
3749
+ * AI SDK 5 and 6 ignore the key and accept system messages already.
3750
+ */
3751
+ result.allowSystemInMessages = true;
3710
3752
  const channelDownload = locals_js_1.locals.get(chatChannelDownloadKey);
3711
3753
  if (channelDownload) {
3712
3754
  result.experimental_download = channelAwareDownload(channelDownload);
@@ -3760,17 +3802,35 @@ function isActionTurn(value) {
3760
3802
  * tail does not match the old message's conversion, nothing is changed and
3761
3803
  * `false` is returned so the caller can fall back to a full reconversion.
3762
3804
  */
3763
- async function replaceModelRun(lane, oldUi, newUi, tailAfter, knownOldRun) {
3805
+ async function replaceModelRun(lane, oldUi, newUi, tailAfter, knownOldRun, oldIsLastUiMessage = false) {
3764
3806
  const oldRun = knownOldRun ?? (await toModelMessages([stripProviderMetadata(oldUi)]));
3765
3807
  const newRun = await toModelMessages([stripProviderMetadata(newUi)]);
3766
- // A message that converts to nothing (a pending tool call with no output yet,
3767
- // which `ignoreIncompleteToolCalls` drops) locates no run in the lane. Matching
3768
- // an empty slice would splice the new run in without removing what the message
3769
- // actually contributed, such as a spliced head-start partial, and the lane would
3770
- // then carry the same tool call twice.
3771
- if (oldRun.length === 0)
3772
- return false;
3773
3808
  const end = lane.length - tailAfter;
3809
+ // A message that converts to nothing (a tool call awaiting approval on AI SDK 7,
3810
+ // or a pending one with no output yet, which `ignoreIncompleteToolCalls` drops)
3811
+ // locates no run in the lane. When it is the last UI message and the lane holds
3812
+ // none of its tool calls, it contributed nothing, so the new run goes in at the
3813
+ // tail. A message that is not the last one has later turns after its place, which
3814
+ // the tail is not. When the lane does hold one of its calls, such as a spliced
3815
+ // head-start partial, matching an empty slice would leave the old form behind and
3816
+ // the lane would carry the same tool call twice.
3817
+ if (oldRun.length === 0) {
3818
+ if (!oldIsLastUiMessage || end < 0 || end > lane.length)
3819
+ return false;
3820
+ const oldCallIds = new Set();
3821
+ for (const part of oldUi.parts) {
3822
+ const id = part.toolCallId;
3823
+ if (typeof id === "string")
3824
+ oldCallIds.add(id);
3825
+ }
3826
+ // Tool-call, tool-result and approval parts of a lane message all carry `toolCallId`.
3827
+ const holdsOldCall = lane.some((message) => Array.isArray(message.content) &&
3828
+ message.content.some((part) => oldCallIds.has(part.toolCallId)));
3829
+ if (holdsOldCall)
3830
+ return false;
3831
+ lane.splice(end, 0, ...newRun);
3832
+ return true;
3833
+ }
3774
3834
  const start = end - oldRun.length;
3775
3835
  if (start < 0 || end > lane.length)
3776
3836
  return false;
@@ -4064,6 +4124,8 @@ function makeChannelDownloader(connectors, handleFor) {
4064
4124
  return null;
4065
4125
  };
4066
4126
  return (url) => {
4127
+ if (isInlineDataUrl(url))
4128
+ return Promise.resolve(null);
4067
4129
  const key = url.href;
4068
4130
  const cached = cache.get(key);
4069
4131
  if (cached)
@@ -4086,17 +4148,32 @@ function makeChannelDownloader(connectors, handleFor) {
4086
4148
  }
4087
4149
  function fileUrlOf(part) {
4088
4150
  const p = part;
4089
- const raw = p?.type === "file" ? p.data : p?.type === "image" ? p.image : undefined;
4090
- if (raw instanceof URL)
4091
- return raw;
4092
- if (typeof raw !== "string" || !/^https?:\/\//i.test(raw))
4093
- return undefined;
4094
- try {
4095
- return new URL(raw);
4151
+ let raw = p?.type === "file" ? p.data : p?.type === "image" ? p.image : undefined;
4152
+ // AI SDK 7 tags the data (`{ type: "url", url }`); AI SDK 6 puts a URL or string there directly.
4153
+ if (raw && typeof raw === "object" && !(raw instanceof URL)) {
4154
+ const tagged = raw;
4155
+ raw = tagged.type === "url" ? tagged.url : undefined;
4096
4156
  }
4097
- catch {
4157
+ let url;
4158
+ if (raw instanceof URL) {
4159
+ url = raw;
4160
+ }
4161
+ else if (typeof raw === "string" && /^https?:\/\//i.test(raw)) {
4162
+ try {
4163
+ url = new URL(raw);
4164
+ }
4165
+ catch {
4166
+ return undefined;
4167
+ }
4168
+ }
4169
+ else {
4098
4170
  return undefined;
4099
4171
  }
4172
+ return isInlineDataUrl(url) ? undefined : url;
4173
+ }
4174
+ /** An inline `data:` upload carries its bytes in the URL, so it is never handed to a connector, cached or logged. */
4175
+ function isInlineDataUrl(url) {
4176
+ return url.protocol === "data:";
4100
4177
  }
4101
4178
  /**
4102
4179
  * Fetch the channel-owned files in the messages a turn hands to `run()`, so the model call reads
@@ -4163,7 +4240,8 @@ async function resolveChannelFiles(messages, download) {
4163
4240
  /**
4164
4241
  * The AI SDK `experimental_download` for a turn: channel-owned URLs come from the connectors, and
4165
4242
  * every other URL goes to `fallback`, or to the AI SDK's default behaviour (left for a model that
4166
- * reads the URL itself, fetched from a public host otherwise).
4243
+ * reads the URL itself, fetched from a public host otherwise). An inline `data:` URL is neither: it
4244
+ * is left to the AI SDK to decode, and reaches no connector, fallback or fetch.
4167
4245
  */
4168
4246
  function channelAwareDownload(download, fallback) {
4169
4247
  return async (requests) => {
@@ -4171,7 +4249,9 @@ function channelAwareDownload(download, fallback) {
4171
4249
  const file = await download(request.url);
4172
4250
  return file ? { data: file.data, mediaType: file.mediaType } : null;
4173
4251
  }));
4174
- const misses = requests.flatMap((_, i) => (results[i] ? [] : [i]));
4252
+ // An inline `data:` file is left to the AI SDK, which decodes it itself: it is no connector's
4253
+ // file, and `fetchPublicFile` would refuse it with the whole upload in the error message.
4254
+ const misses = requests.flatMap((request, i) => results[i] || request.url.protocol === "data:" ? [] : [i]);
4175
4255
  if (misses.length === 0)
4176
4256
  return results;
4177
4257
  const rest = fallback
@@ -7096,7 +7176,11 @@ function chatAgent(options) {
7096
7176
  if (idx !== -1) {
7097
7177
  const previous = accumulatedUIMessages[idx];
7098
7178
  accumulatedUIMessages[idx] = mergeIncomingIntoHydrated(previous, incoming);
7099
- replacedPairs.push({ previous, merged: accumulatedUIMessages[idx] });
7179
+ replacedPairs.push({
7180
+ previous,
7181
+ merged: accumulatedUIMessages[idx],
7182
+ isLast: idx === accumulatedUIMessages.length - 1,
7183
+ });
7100
7184
  replaced = true;
7101
7185
  }
7102
7186
  else {
@@ -7106,8 +7190,8 @@ function chatAgent(options) {
7106
7190
  }
7107
7191
  if (replaced) {
7108
7192
  let inPlace = true;
7109
- for (const { previous, merged } of replacedPairs) {
7110
- if (!(await replaceModelRun(accumulatedMessages, previous, merged, 0))) {
7193
+ for (const { previous, merged, isLast } of replacedPairs) {
7194
+ if (!(await replaceModelRun(accumulatedMessages, previous, merged, 0, undefined, isLast))) {
7111
7195
  inPlace = false;
7112
7196
  break;
7113
7197
  }
@@ -7641,6 +7725,21 @@ function chatAgent(options) {
7641
7725
  new Promise((r) => setTimeout(r, 2_000)),
7642
7726
  ]);
7643
7727
  }
7728
+ /**
7729
+ * A stream-level failure (an `error` chunk) is the turn's finish
7730
+ * reason. A step rejected before the model runs (an invalid prompt,
7731
+ * a throwing `prepareStep`) ends the stream with one but no finish
7732
+ * reason of its own, or the previous step's. Tool errors do not
7733
+ * count: they are `tool-input-error` / `tool-output-error` parts
7734
+ * that go back to the model, and the turn finishes normally.
7735
+ */
7736
+ // Looked up once per turn; the buffer is only scanned again from
7737
+ // `errorScanLength` on, in case a chunk lands after this point.
7738
+ const errorScanLength = turnBufferedChunks.length;
7739
+ const earlyErrorChunk = turnBufferedChunks.find(isErrorUIMessageChunk);
7740
+ if (earlyErrorChunk) {
7741
+ capturedFinishReason = "error";
7742
+ }
7644
7743
  capturedResponseMessage =
7645
7744
  (await locals_js_1.locals.get(chatManagedResponseKey)?.snapshot()) ?? capturedResponseMessage;
7646
7745
  if (capturedResponseMessage)
@@ -7665,18 +7764,7 @@ function chatAgent(options) {
7665
7764
  /* non-fatal — usage capture failed */
7666
7765
  }
7667
7766
  }
7668
- const lastStepUsagePromise = runResult != null ? runResult.usage : undefined;
7669
- if (typeof lastStepUsagePromise?.then === "function") {
7670
- try {
7671
- lastStepUsage = (await Promise.race([
7672
- lastStepUsagePromise,
7673
- new Promise((r) => setTimeout(() => r(undefined), 2_000)),
7674
- ]));
7675
- }
7676
- catch {
7677
- /* non-fatal — usage capture failed */
7678
- }
7679
- }
7767
+ lastStepUsage = await readLastStepUsage(runResult);
7680
7768
  if (turnUsage) {
7681
7769
  cumulativeUsage = addUsage(cumulativeUsage, turnUsage);
7682
7770
  previousTurnUsage = turnUsage;
@@ -7867,7 +7955,8 @@ function chatAgent(options) {
7867
7955
  if (capturedResponseMessage) {
7868
7956
  responseCommitted = true;
7869
7957
  capturedPartialResponse = capturedResponseMessage;
7870
- const errorChunk = turnBufferedChunks.find((chunk) => chunk.type === "error");
7958
+ const errorChunk = (earlyErrorChunk ??
7959
+ turnBufferedChunks.slice(errorScanLength).find(isErrorUIMessageChunk));
7871
7960
  channelStreamErrorText = errorChunk?.errorText;
7872
7961
  channelTurnText = turnBufferedChunks
7873
7962
  .map((chunk) => {
@@ -9798,6 +9887,10 @@ async function flushDeferredChatCustomAgentClientDataErrors() {
9798
9887
  }
9799
9888
  }
9800
9889
  }
9890
+ /** Whether a buffered chunk is a stream-level `error` chunk. */
9891
+ function isErrorUIMessageChunk(chunk) {
9892
+ return chunk.type === "error";
9893
+ }
9801
9894
  /**
9802
9895
  * Pass every chunk through untouched while recording it in `buffer`. Handles
9803
9896
  * both the `AsyncIterable` and `ReadableStream` shapes `toUIMessageStream()`
@@ -9979,6 +10072,7 @@ class ChatMessageAccumulator {
9979
10072
  const ui = replace ? [] : [...this.uiMessages];
9980
10073
  const lane = replace ? [] : [...this.modelMessages];
9981
10074
  let appendedModelCount = 0;
10075
+ let appendedUiCount = 0;
9982
10076
  let rebuild = replace;
9983
10077
  for (const raw of messages) {
9984
10078
  const message = raw.role === "assistant" ? cleanupAbortedParts(raw) : raw;
@@ -9991,13 +10085,18 @@ class ChatMessageAccumulator {
9991
10085
  const idx = ui.findIndex((m) => m.id === resolved.id);
9992
10086
  const previous = ui[idx];
9993
10087
  ui[idx] = mergeIncomingIntoHydrated(previous, resolved);
9994
- if (!rebuild && !(await replaceModelRun(lane, previous, ui[idx], appendedModelCount))) {
10088
+ // The lane's tail is only this message's place when nothing came after it
10089
+ // before this call's own appends.
10090
+ const isLast = idx === ui.length - 1 - appendedUiCount;
10091
+ if (!rebuild &&
10092
+ !(await replaceModelRun(lane, previous, ui[idx], appendedModelCount, undefined, isLast))) {
9995
10093
  rebuild = true;
9996
10094
  }
9997
10095
  continue;
9998
10096
  }
9999
10097
  ui.push(message);
10000
10098
  if (!rebuild) {
10099
+ appendedUiCount++;
10001
10100
  const converted = await toModelMessages([message]);
10002
10101
  lane.push(...converted);
10003
10102
  appendedModelCount += converted.length;
@@ -10196,9 +10295,11 @@ class ChatMessageAccumulator {
10196
10295
  }
10197
10296
  /**
10198
10297
  * Run outer-loop compaction if needed. Call after adding the response
10199
- * and capturing usage. Pass the LAST step's usage (`result.usage`), which is
10200
- * the context the model held on its final call; `result.totalUsage` sums every
10201
- * step of a tool-using turn and belongs in `context.turnUsage`. Applies
10298
+ * and capturing usage. Pass the LAST step's usage
10299
+ * (`(await result.steps).at(-1)?.usage`), which is the context the model held on
10300
+ * its final call. `result.usage` is that on AI SDK 6 but the turn's sum on AI SDK 7,
10301
+ * so read `steps`. The turn's sum (`result.totalUsage` on AI SDK 6, `result.usage`
10302
+ * on AI SDK 7) belongs in `context.turnUsage`. Applies
10202
10303
  * `compactModelMessages` and `compactUIMessages` callbacks if configured.
10203
10304
  *
10204
10305
  * @returns `true` if compaction was performed, `false` otherwise.
@@ -10694,18 +10795,7 @@ function createChatSession(payload, options) {
10694
10795
  /* non-fatal */
10695
10796
  }
10696
10797
  }
10697
- const lastStepUsagePromise = source.usage;
10698
- if (typeof lastStepUsagePromise?.then === "function") {
10699
- try {
10700
- lastStepUsage = (await Promise.race([
10701
- lastStepUsagePromise,
10702
- new Promise((r) => setTimeout(() => r(undefined), 2_000)),
10703
- ]));
10704
- }
10705
- catch {
10706
- /* non-fatal */
10707
- }
10708
- }
10798
+ lastStepUsage = await readLastStepUsage(source);
10709
10799
  // Outer-loop compaction (same logic as chat.agent)
10710
10800
  if (sessionCompaction && turnUsage && !turnObj.stopped) {
10711
10801
  const contextUsage = lastStepUsage ?? turnUsage;