@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.
package/dist/esm/v3/ai.js CHANGED
@@ -41,6 +41,7 @@ import { withResolvedExternalDeploymentId } from "./externalDeploymentId.js";
41
41
  import { resolvePinToFollow } from "./chatVersionSkew.js";
42
42
  import { sessions, } from "./sessions.js";
43
43
  import { createTask } from "./shared.js";
44
+ import { withJsonToolResults } from "./toolOutputJson.js";
44
45
  import { triggerConcurrencyBody } from "./concurrency-shared.js";
45
46
  import { markChatAgentRunForStreamsWarning } from "./streams.js";
46
47
  import { tracer } from "./tracer.js";
@@ -57,10 +58,10 @@ function toModelMessages(messages, context) {
57
58
  // conditional spread keeps the options object byte-identical to the no-tools
58
59
  // path when nothing was declared.
59
60
  const tools = locals.get(chatResolvedToolsKey);
60
- return convertSteeredMessages(messages, async (batch) => convertToModelMessages(batch, {
61
+ return convertSteeredMessages(messages, async (batch) => withJsonToolResults(await convertToModelMessages(batch, {
61
62
  ignoreIncompleteToolCalls: true,
62
63
  ...(tools ? { tools } : {}),
63
- }), locals.get(chatSteeringInjectionsKey) ?? new Map(), context ?? locals.get(chatCurrentUIMessagesKey) ?? messages, context !== undefined || locals.get(chatCurrentUIMessagesKey) !== undefined);
64
+ })), locals.get(chatSteeringInjectionsKey) ?? new Map(), context ?? locals.get(chatCurrentUIMessagesKey) ?? messages, context !== undefined || locals.get(chatCurrentUIMessagesKey) !== undefined);
64
65
  }
65
66
  const chatTurnContextKey = locals.create("chat.turnContext");
66
67
  /**
@@ -2059,6 +2060,33 @@ function emptyUsage() {
2059
2060
  outputTokenDetails: { textTokens: undefined, reasoningTokens: undefined },
2060
2061
  };
2061
2062
  }
2063
+ /**
2064
+ * The last step's usage of a `streamText` result: what the model's context window held on
2065
+ * its final call. It comes from `steps` because the result's `usage` is the last step's on
2066
+ * AI SDK 6 but the turn's sum on AI SDK 7. A custom stream source with no `steps` falls
2067
+ * back to its `usage`, which such a source supplies as the last step's. Raced against a 2s
2068
+ * timeout like the turn's `totalUsage`, since the promises can hang on a stop-abort.
2069
+ * Returns undefined when neither resolves in time.
2070
+ */
2071
+ async function readLastStepUsage(result) {
2072
+ const stepsPromise = result != null ? result.steps : undefined;
2073
+ const hasSteps = typeof stepsPromise?.then === "function";
2074
+ const source = hasSteps ? stepsPromise : result != null ? result.usage : undefined;
2075
+ if (typeof source?.then !== "function")
2076
+ return undefined;
2077
+ try {
2078
+ const resolved = await Promise.race([
2079
+ source,
2080
+ new Promise((r) => setTimeout(() => r(undefined), 2_000)),
2081
+ ]);
2082
+ return hasSteps
2083
+ ? resolved?.at(-1)?.usage
2084
+ : resolved;
2085
+ }
2086
+ catch {
2087
+ return undefined;
2088
+ }
2089
+ }
2062
2090
  function addUsage(a, b) {
2063
2091
  const add = (x, y) => x != null || y != null ? (x ?? 0) + (y ?? 0) : undefined;
2064
2092
  return {
@@ -3679,13 +3707,27 @@ function toStreamTextOptions(options, retain = true) {
3679
3707
  // 4. Parallel tool results in call order, the order the next turn's history
3680
3708
  // will list them in. See `withToolResultsInCallOrder`.
3681
3709
  const base = resultMessages ?? messages;
3682
- const ordered = withToolResultsInCallOrder(base);
3683
- return resultMessages || ordered !== base ? { messages: ordered } : undefined;
3710
+ // 5. Tool results as plain JSON, which AI SDK 7 before 7.0.101 checks on
3711
+ // every step. See `withJsonToolResults`.
3712
+ const prepared = await withJsonToolResults(withToolResultsInCallOrder(base));
3713
+ return resultMessages || prepared !== base ? { messages: prepared } : undefined;
3684
3714
  };
3685
3715
  }
3686
3716
  if (retain && typeof result.prepareStep === "function") {
3687
3717
  result.prepareStep = retainStepMessages(result.prepareStep);
3688
3718
  }
3719
+ /**
3720
+ * A steer that `pendingMessages.prepare` turns into a `system` message lands in
3721
+ * the model lane in that form, and so replays in every later turn's `messages`.
3722
+ * AI SDK 7 rejects a system message in `messages` unless this is set (its default
3723
+ * is `false`), so without it the step fails with an invalid-prompt error on the
3724
+ * turn that was steered and on every turn after it. Routing the note to
3725
+ * `instructions` for one step would not help the later turns, because the lane
3726
+ * is what the caller's own `streamText({ messages })` reads. A caller that wants
3727
+ * the strict behaviour passes `allowSystemInMessages: false` after the spread.
3728
+ * AI SDK 5 and 6 ignore the key and accept system messages already.
3729
+ */
3730
+ result.allowSystemInMessages = true;
3689
3731
  const channelDownload = locals.get(chatChannelDownloadKey);
3690
3732
  if (channelDownload) {
3691
3733
  result.experimental_download = channelAwareDownload(channelDownload);
@@ -3739,17 +3781,35 @@ function isActionTurn(value) {
3739
3781
  * tail does not match the old message's conversion, nothing is changed and
3740
3782
  * `false` is returned so the caller can fall back to a full reconversion.
3741
3783
  */
3742
- async function replaceModelRun(lane, oldUi, newUi, tailAfter, knownOldRun) {
3784
+ async function replaceModelRun(lane, oldUi, newUi, tailAfter, knownOldRun, oldIsLastUiMessage = false) {
3743
3785
  const oldRun = knownOldRun ?? (await toModelMessages([stripProviderMetadata(oldUi)]));
3744
3786
  const newRun = await toModelMessages([stripProviderMetadata(newUi)]);
3745
- // A message that converts to nothing (a pending tool call with no output yet,
3746
- // which `ignoreIncompleteToolCalls` drops) locates no run in the lane. Matching
3747
- // an empty slice would splice the new run in without removing what the message
3748
- // actually contributed, such as a spliced head-start partial, and the lane would
3749
- // then carry the same tool call twice.
3750
- if (oldRun.length === 0)
3751
- return false;
3752
3787
  const end = lane.length - tailAfter;
3788
+ // A message that converts to nothing (a tool call awaiting approval on AI SDK 7,
3789
+ // or a pending one with no output yet, which `ignoreIncompleteToolCalls` drops)
3790
+ // locates no run in the lane. When it is the last UI message and the lane holds
3791
+ // none of its tool calls, it contributed nothing, so the new run goes in at the
3792
+ // tail. A message that is not the last one has later turns after its place, which
3793
+ // the tail is not. When the lane does hold one of its calls, such as a spliced
3794
+ // head-start partial, matching an empty slice would leave the old form behind and
3795
+ // the lane would carry the same tool call twice.
3796
+ if (oldRun.length === 0) {
3797
+ if (!oldIsLastUiMessage || end < 0 || end > lane.length)
3798
+ return false;
3799
+ const oldCallIds = new Set();
3800
+ for (const part of oldUi.parts) {
3801
+ const id = part.toolCallId;
3802
+ if (typeof id === "string")
3803
+ oldCallIds.add(id);
3804
+ }
3805
+ // Tool-call, tool-result and approval parts of a lane message all carry `toolCallId`.
3806
+ const holdsOldCall = lane.some((message) => Array.isArray(message.content) &&
3807
+ message.content.some((part) => oldCallIds.has(part.toolCallId)));
3808
+ if (holdsOldCall)
3809
+ return false;
3810
+ lane.splice(end, 0, ...newRun);
3811
+ return true;
3812
+ }
3753
3813
  const start = end - oldRun.length;
3754
3814
  if (start < 0 || end > lane.length)
3755
3815
  return false;
@@ -4043,6 +4103,8 @@ function makeChannelDownloader(connectors, handleFor) {
4043
4103
  return null;
4044
4104
  };
4045
4105
  return (url) => {
4106
+ if (isInlineDataUrl(url))
4107
+ return Promise.resolve(null);
4046
4108
  const key = url.href;
4047
4109
  const cached = cache.get(key);
4048
4110
  if (cached)
@@ -4065,17 +4127,32 @@ function makeChannelDownloader(connectors, handleFor) {
4065
4127
  }
4066
4128
  function fileUrlOf(part) {
4067
4129
  const p = part;
4068
- const raw = p?.type === "file" ? p.data : p?.type === "image" ? p.image : undefined;
4069
- if (raw instanceof URL)
4070
- return raw;
4071
- if (typeof raw !== "string" || !/^https?:\/\//i.test(raw))
4072
- return undefined;
4073
- try {
4074
- return new URL(raw);
4130
+ let raw = p?.type === "file" ? p.data : p?.type === "image" ? p.image : undefined;
4131
+ // AI SDK 7 tags the data (`{ type: "url", url }`); AI SDK 6 puts a URL or string there directly.
4132
+ if (raw && typeof raw === "object" && !(raw instanceof URL)) {
4133
+ const tagged = raw;
4134
+ raw = tagged.type === "url" ? tagged.url : undefined;
4075
4135
  }
4076
- catch {
4136
+ let url;
4137
+ if (raw instanceof URL) {
4138
+ url = raw;
4139
+ }
4140
+ else if (typeof raw === "string" && /^https?:\/\//i.test(raw)) {
4141
+ try {
4142
+ url = new URL(raw);
4143
+ }
4144
+ catch {
4145
+ return undefined;
4146
+ }
4147
+ }
4148
+ else {
4077
4149
  return undefined;
4078
4150
  }
4151
+ return isInlineDataUrl(url) ? undefined : url;
4152
+ }
4153
+ /** An inline `data:` upload carries its bytes in the URL, so it is never handed to a connector, cached or logged. */
4154
+ function isInlineDataUrl(url) {
4155
+ return url.protocol === "data:";
4079
4156
  }
4080
4157
  /**
4081
4158
  * Fetch the channel-owned files in the messages a turn hands to `run()`, so the model call reads
@@ -4142,7 +4219,8 @@ async function resolveChannelFiles(messages, download) {
4142
4219
  /**
4143
4220
  * The AI SDK `experimental_download` for a turn: channel-owned URLs come from the connectors, and
4144
4221
  * every other URL goes to `fallback`, or to the AI SDK's default behaviour (left for a model that
4145
- * reads the URL itself, fetched from a public host otherwise).
4222
+ * reads the URL itself, fetched from a public host otherwise). An inline `data:` URL is neither: it
4223
+ * is left to the AI SDK to decode, and reaches no connector, fallback or fetch.
4146
4224
  */
4147
4225
  function channelAwareDownload(download, fallback) {
4148
4226
  return async (requests) => {
@@ -4150,7 +4228,9 @@ function channelAwareDownload(download, fallback) {
4150
4228
  const file = await download(request.url);
4151
4229
  return file ? { data: file.data, mediaType: file.mediaType } : null;
4152
4230
  }));
4153
- const misses = requests.flatMap((_, i) => (results[i] ? [] : [i]));
4231
+ // An inline `data:` file is left to the AI SDK, which decodes it itself: it is no connector's
4232
+ // file, and `fetchPublicFile` would refuse it with the whole upload in the error message.
4233
+ const misses = requests.flatMap((request, i) => results[i] || request.url.protocol === "data:" ? [] : [i]);
4154
4234
  if (misses.length === 0)
4155
4235
  return results;
4156
4236
  const rest = fallback
@@ -7076,7 +7156,11 @@ function chatAgent(options) {
7076
7156
  if (idx !== -1) {
7077
7157
  const previous = accumulatedUIMessages[idx];
7078
7158
  accumulatedUIMessages[idx] = mergeIncomingIntoHydrated(previous, incoming);
7079
- replacedPairs.push({ previous, merged: accumulatedUIMessages[idx] });
7159
+ replacedPairs.push({
7160
+ previous,
7161
+ merged: accumulatedUIMessages[idx],
7162
+ isLast: idx === accumulatedUIMessages.length - 1,
7163
+ });
7080
7164
  replaced = true;
7081
7165
  }
7082
7166
  else {
@@ -7086,8 +7170,8 @@ function chatAgent(options) {
7086
7170
  }
7087
7171
  if (replaced) {
7088
7172
  let inPlace = true;
7089
- for (const { previous, merged } of replacedPairs) {
7090
- if (!(await replaceModelRun(accumulatedMessages, previous, merged, 0))) {
7173
+ for (const { previous, merged, isLast } of replacedPairs) {
7174
+ if (!(await replaceModelRun(accumulatedMessages, previous, merged, 0, undefined, isLast))) {
7091
7175
  inPlace = false;
7092
7176
  break;
7093
7177
  }
@@ -7621,6 +7705,21 @@ function chatAgent(options) {
7621
7705
  new Promise((r) => setTimeout(r, 2_000)),
7622
7706
  ]);
7623
7707
  }
7708
+ /**
7709
+ * A stream-level failure (an `error` chunk) is the turn's finish
7710
+ * reason. A step rejected before the model runs (an invalid prompt,
7711
+ * a throwing `prepareStep`) ends the stream with one but no finish
7712
+ * reason of its own, or the previous step's. Tool errors do not
7713
+ * count: they are `tool-input-error` / `tool-output-error` parts
7714
+ * that go back to the model, and the turn finishes normally.
7715
+ */
7716
+ // Looked up once per turn; the buffer is only scanned again from
7717
+ // `errorScanLength` on, in case a chunk lands after this point.
7718
+ const errorScanLength = turnBufferedChunks.length;
7719
+ const earlyErrorChunk = turnBufferedChunks.find(isErrorUIMessageChunk);
7720
+ if (earlyErrorChunk) {
7721
+ capturedFinishReason = "error";
7722
+ }
7624
7723
  capturedResponseMessage =
7625
7724
  (await locals.get(chatManagedResponseKey)?.snapshot()) ?? capturedResponseMessage;
7626
7725
  if (capturedResponseMessage)
@@ -7645,18 +7744,7 @@ function chatAgent(options) {
7645
7744
  /* non-fatal — usage capture failed */
7646
7745
  }
7647
7746
  }
7648
- const lastStepUsagePromise = runResult != null ? runResult.usage : undefined;
7649
- if (typeof lastStepUsagePromise?.then === "function") {
7650
- try {
7651
- lastStepUsage = (await Promise.race([
7652
- lastStepUsagePromise,
7653
- new Promise((r) => setTimeout(() => r(undefined), 2_000)),
7654
- ]));
7655
- }
7656
- catch {
7657
- /* non-fatal — usage capture failed */
7658
- }
7659
- }
7747
+ lastStepUsage = await readLastStepUsage(runResult);
7660
7748
  if (turnUsage) {
7661
7749
  cumulativeUsage = addUsage(cumulativeUsage, turnUsage);
7662
7750
  previousTurnUsage = turnUsage;
@@ -7847,7 +7935,8 @@ function chatAgent(options) {
7847
7935
  if (capturedResponseMessage) {
7848
7936
  responseCommitted = true;
7849
7937
  capturedPartialResponse = capturedResponseMessage;
7850
- const errorChunk = turnBufferedChunks.find((chunk) => chunk.type === "error");
7938
+ const errorChunk = (earlyErrorChunk ??
7939
+ turnBufferedChunks.slice(errorScanLength).find(isErrorUIMessageChunk));
7851
7940
  channelStreamErrorText = errorChunk?.errorText;
7852
7941
  channelTurnText = turnBufferedChunks
7853
7942
  .map((chunk) => {
@@ -9778,6 +9867,10 @@ async function flushDeferredChatCustomAgentClientDataErrors() {
9778
9867
  }
9779
9868
  }
9780
9869
  }
9870
+ /** Whether a buffered chunk is a stream-level `error` chunk. */
9871
+ function isErrorUIMessageChunk(chunk) {
9872
+ return chunk.type === "error";
9873
+ }
9781
9874
  /**
9782
9875
  * Pass every chunk through untouched while recording it in `buffer`. Handles
9783
9876
  * both the `AsyncIterable` and `ReadableStream` shapes `toUIMessageStream()`
@@ -9959,6 +10052,7 @@ class ChatMessageAccumulator {
9959
10052
  const ui = replace ? [] : [...this.uiMessages];
9960
10053
  const lane = replace ? [] : [...this.modelMessages];
9961
10054
  let appendedModelCount = 0;
10055
+ let appendedUiCount = 0;
9962
10056
  let rebuild = replace;
9963
10057
  for (const raw of messages) {
9964
10058
  const message = raw.role === "assistant" ? cleanupAbortedParts(raw) : raw;
@@ -9971,13 +10065,18 @@ class ChatMessageAccumulator {
9971
10065
  const idx = ui.findIndex((m) => m.id === resolved.id);
9972
10066
  const previous = ui[idx];
9973
10067
  ui[idx] = mergeIncomingIntoHydrated(previous, resolved);
9974
- if (!rebuild && !(await replaceModelRun(lane, previous, ui[idx], appendedModelCount))) {
10068
+ // The lane's tail is only this message's place when nothing came after it
10069
+ // before this call's own appends.
10070
+ const isLast = idx === ui.length - 1 - appendedUiCount;
10071
+ if (!rebuild &&
10072
+ !(await replaceModelRun(lane, previous, ui[idx], appendedModelCount, undefined, isLast))) {
9975
10073
  rebuild = true;
9976
10074
  }
9977
10075
  continue;
9978
10076
  }
9979
10077
  ui.push(message);
9980
10078
  if (!rebuild) {
10079
+ appendedUiCount++;
9981
10080
  const converted = await toModelMessages([message]);
9982
10081
  lane.push(...converted);
9983
10082
  appendedModelCount += converted.length;
@@ -10176,9 +10275,11 @@ class ChatMessageAccumulator {
10176
10275
  }
10177
10276
  /**
10178
10277
  * Run outer-loop compaction if needed. Call after adding the response
10179
- * and capturing usage. Pass the LAST step's usage (`result.usage`), which is
10180
- * the context the model held on its final call; `result.totalUsage` sums every
10181
- * step of a tool-using turn and belongs in `context.turnUsage`. Applies
10278
+ * and capturing usage. Pass the LAST step's usage
10279
+ * (`(await result.steps).at(-1)?.usage`), which is the context the model held on
10280
+ * its final call. `result.usage` is that on AI SDK 6 but the turn's sum on AI SDK 7,
10281
+ * so read `steps`. The turn's sum (`result.totalUsage` on AI SDK 6, `result.usage`
10282
+ * on AI SDK 7) belongs in `context.turnUsage`. Applies
10182
10283
  * `compactModelMessages` and `compactUIMessages` callbacks if configured.
10183
10284
  *
10184
10285
  * @returns `true` if compaction was performed, `false` otherwise.
@@ -10674,18 +10775,7 @@ function createChatSession(payload, options) {
10674
10775
  /* non-fatal */
10675
10776
  }
10676
10777
  }
10677
- const lastStepUsagePromise = source.usage;
10678
- if (typeof lastStepUsagePromise?.then === "function") {
10679
- try {
10680
- lastStepUsage = (await Promise.race([
10681
- lastStepUsagePromise,
10682
- new Promise((r) => setTimeout(() => r(undefined), 2_000)),
10683
- ]));
10684
- }
10685
- catch {
10686
- /* non-fatal */
10687
- }
10688
- }
10778
+ lastStepUsage = await readLastStepUsage(source);
10689
10779
  // Outer-loop compaction (same logic as chat.agent)
10690
10780
  if (sessionCompaction && turnUsage && !turnObj.stopped) {
10691
10781
  const contextUsage = lastStepUsage ?? turnUsage;