@mastra/ai-sdk 1.4.4 → 1.4.5

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/index.js CHANGED
@@ -9,7 +9,7 @@ import { TripWire, MessageList, aiV5ModelMessageToV2PromptMessage } from '@mastr
9
9
  import { RequestContext } from '@mastra/core/di';
10
10
  import { WorkingMemory, MessageHistory, SemanticRecall } from '@mastra/core/processors';
11
11
 
12
- // ../../packages/_vendored/ai_v5/dist/chunk-TQ7JQ3AX.js
12
+ // ../../packages/_vendored/ai_v5/dist/chunk-LPCSE373.js
13
13
  var marker = "vercel.ai.error";
14
14
  var symbol = Symbol.for(marker);
15
15
  var _a;
@@ -188,6 +188,9 @@ var ParseError = class extends Error {
188
188
  super(message), this.name = "ParseError", this.type = options.type, this.field = options.field, this.value = options.value, this.line = options.line;
189
189
  }
190
190
  };
191
+ var LF = 10;
192
+ var CR = 13;
193
+ var SPACE = 32;
191
194
  function noop(_arg) {
192
195
  }
193
196
  function createParser(callbacks) {
@@ -195,39 +198,109 @@ function createParser(callbacks) {
195
198
  throw new TypeError(
196
199
  "`callbacks` must be an object, got a function instead. Did you mean `{onEvent: fn}`?"
197
200
  );
198
- const { onEvent = noop, onError = noop, onRetry = noop, onComment } = callbacks;
199
- let incompleteLine = "", isFirstChunk = true, id, data = "", eventType = "";
200
- function feed(newChunk) {
201
- const chunk = isFirstChunk ? newChunk.replace(/^\xEF\xBB\xBF/, "") : newChunk, [complete, incomplete] = splitLines(`${incompleteLine}${chunk}`);
202
- for (const line of complete)
203
- parseLine(line);
204
- incompleteLine = incomplete, isFirstChunk = false;
205
- }
206
- function parseLine(line) {
207
- if (line === "") {
201
+ const { onEvent = noop, onError = noop, onRetry = noop, onComment } = callbacks, pendingFragments = [];
202
+ let isFirstChunk = true, id, data = "", dataLines = 0, eventType;
203
+ function feed(chunk) {
204
+ if (isFirstChunk && (isFirstChunk = false, chunk.charCodeAt(0) === 239 && chunk.charCodeAt(1) === 187 && chunk.charCodeAt(2) === 191 && (chunk = chunk.slice(3))), pendingFragments.length === 0) {
205
+ const trailing2 = processLines(chunk);
206
+ trailing2 !== "" && pendingFragments.push(trailing2);
207
+ return;
208
+ }
209
+ if (chunk.indexOf(`
210
+ `) === -1 && chunk.indexOf("\r") === -1) {
211
+ pendingFragments.push(chunk);
212
+ return;
213
+ }
214
+ pendingFragments.push(chunk);
215
+ const input = pendingFragments.join("");
216
+ pendingFragments.length = 0;
217
+ const trailing = processLines(input);
218
+ trailing !== "" && pendingFragments.push(trailing);
219
+ }
220
+ function processLines(chunk) {
221
+ let searchIndex = 0;
222
+ if (chunk.indexOf("\r") === -1) {
223
+ let lfIndex = chunk.indexOf(`
224
+ `, searchIndex);
225
+ for (; lfIndex !== -1; ) {
226
+ if (searchIndex === lfIndex) {
227
+ dataLines > 0 && onEvent({ id, event: eventType, data }), id = void 0, data = "", dataLines = 0, eventType = void 0, searchIndex = lfIndex + 1, lfIndex = chunk.indexOf(`
228
+ `, searchIndex);
229
+ continue;
230
+ }
231
+ const firstCharCode = chunk.charCodeAt(searchIndex);
232
+ if (isDataPrefix(chunk, searchIndex, firstCharCode)) {
233
+ const valueStart = chunk.charCodeAt(searchIndex + 5) === SPACE ? searchIndex + 6 : searchIndex + 5, value = chunk.slice(valueStart, lfIndex);
234
+ if (dataLines === 0 && chunk.charCodeAt(lfIndex + 1) === LF) {
235
+ onEvent({ id, event: eventType, data: value }), id = void 0, data = "", eventType = void 0, searchIndex = lfIndex + 2, lfIndex = chunk.indexOf(`
236
+ `, searchIndex);
237
+ continue;
238
+ }
239
+ data = dataLines === 0 ? value : `${data}
240
+ ${value}`, dataLines++;
241
+ } else isEventPrefix(chunk, searchIndex, firstCharCode) ? eventType = chunk.slice(
242
+ chunk.charCodeAt(searchIndex + 6) === SPACE ? searchIndex + 7 : searchIndex + 6,
243
+ lfIndex
244
+ ) || void 0 : parseLine(chunk, searchIndex, lfIndex);
245
+ searchIndex = lfIndex + 1, lfIndex = chunk.indexOf(`
246
+ `, searchIndex);
247
+ }
248
+ return chunk.slice(searchIndex);
249
+ }
250
+ for (; searchIndex < chunk.length; ) {
251
+ const crIndex = chunk.indexOf("\r", searchIndex), lfIndex = chunk.indexOf(`
252
+ `, searchIndex);
253
+ let lineEnd = -1;
254
+ if (crIndex !== -1 && lfIndex !== -1 ? lineEnd = crIndex < lfIndex ? crIndex : lfIndex : crIndex !== -1 ? crIndex === chunk.length - 1 ? lineEnd = -1 : lineEnd = crIndex : lfIndex !== -1 && (lineEnd = lfIndex), lineEnd === -1)
255
+ break;
256
+ parseLine(chunk, searchIndex, lineEnd), searchIndex = lineEnd + 1, chunk.charCodeAt(searchIndex - 1) === CR && chunk.charCodeAt(searchIndex) === LF && searchIndex++;
257
+ }
258
+ return chunk.slice(searchIndex);
259
+ }
260
+ function parseLine(chunk, start, end) {
261
+ if (start === end) {
208
262
  dispatchEvent();
209
263
  return;
210
264
  }
211
- if (line.startsWith(":")) {
212
- onComment && onComment(line.slice(line.startsWith(": ") ? 2 : 1));
265
+ const firstCharCode = chunk.charCodeAt(start);
266
+ if (isDataPrefix(chunk, start, firstCharCode)) {
267
+ const valueStart = chunk.charCodeAt(start + 5) === SPACE ? start + 6 : start + 5, value2 = chunk.slice(valueStart, end);
268
+ data = dataLines === 0 ? value2 : `${data}
269
+ ${value2}`, dataLines++;
270
+ return;
271
+ }
272
+ if (isEventPrefix(chunk, start, firstCharCode)) {
273
+ eventType = chunk.slice(chunk.charCodeAt(start + 6) === SPACE ? start + 7 : start + 6, end) || void 0;
274
+ return;
275
+ }
276
+ if (firstCharCode === 105 && chunk.charCodeAt(start + 1) === 100 && chunk.charCodeAt(start + 2) === 58) {
277
+ const value2 = chunk.slice(chunk.charCodeAt(start + 3) === SPACE ? start + 4 : start + 3, end);
278
+ id = value2.includes("\0") ? void 0 : value2;
213
279
  return;
214
280
  }
215
- const fieldSeparatorIndex = line.indexOf(":");
216
- if (fieldSeparatorIndex !== -1) {
217
- const field = line.slice(0, fieldSeparatorIndex), offset = line[fieldSeparatorIndex + 1] === " " ? 2 : 1, value = line.slice(fieldSeparatorIndex + offset);
218
- processField(field, value, line);
281
+ if (firstCharCode === 58) {
282
+ if (onComment) {
283
+ const line2 = chunk.slice(start, end);
284
+ onComment(line2.slice(chunk.charCodeAt(start + 1) === SPACE ? 2 : 1));
285
+ }
286
+ return;
287
+ }
288
+ const line = chunk.slice(start, end), fieldSeparatorIndex = line.indexOf(":");
289
+ if (fieldSeparatorIndex === -1) {
290
+ processField(line, "", line);
219
291
  return;
220
292
  }
221
- processField(line, "", line);
293
+ const field = line.slice(0, fieldSeparatorIndex), offset = line.charCodeAt(fieldSeparatorIndex + 1) === SPACE ? 2 : 1, value = line.slice(fieldSeparatorIndex + offset);
294
+ processField(field, value, line);
222
295
  }
223
296
  function processField(field, value, line) {
224
297
  switch (field) {
225
298
  case "event":
226
- eventType = value;
299
+ eventType = value || void 0;
227
300
  break;
228
301
  case "data":
229
- data = `${data}${value}
230
- `;
302
+ data = dataLines === 0 ? value : `${data}
303
+ ${value}`, dataLines++;
231
304
  break;
232
305
  case "id":
233
306
  id = value.includes("\0") ? void 0 : value;
@@ -252,37 +325,26 @@ function createParser(callbacks) {
252
325
  }
253
326
  }
254
327
  function dispatchEvent() {
255
- data.length > 0 && onEvent({
328
+ dataLines > 0 && onEvent({
256
329
  id,
257
- event: eventType || void 0,
258
- // If the data buffer's last character is a U+000A LINE FEED (LF) character,
259
- // then remove the last character from the data buffer.
260
- data: data.endsWith(`
261
- `) ? data.slice(0, -1) : data
262
- }), id = void 0, data = "", eventType = "";
330
+ event: eventType,
331
+ data
332
+ }), id = void 0, data = "", dataLines = 0, eventType = void 0;
263
333
  }
264
334
  function reset(options = {}) {
265
- incompleteLine && options.consume && parseLine(incompleteLine), isFirstChunk = true, id = void 0, data = "", eventType = "", incompleteLine = "";
335
+ if (options.consume && pendingFragments.length > 0) {
336
+ const incompleteLine = pendingFragments.join("");
337
+ parseLine(incompleteLine, 0, incompleteLine.length);
338
+ }
339
+ isFirstChunk = true, id = void 0, data = "", dataLines = 0, eventType = void 0, pendingFragments.length = 0;
266
340
  }
267
341
  return { feed, reset };
268
342
  }
269
- function splitLines(chunk) {
270
- const lines = [];
271
- let incompleteLine = "", searchIndex = 0;
272
- for (; searchIndex < chunk.length; ) {
273
- const crIndex = chunk.indexOf("\r", searchIndex), lfIndex = chunk.indexOf(`
274
- `, searchIndex);
275
- let lineEnd = -1;
276
- if (crIndex !== -1 && lfIndex !== -1 ? lineEnd = Math.min(crIndex, lfIndex) : crIndex !== -1 ? crIndex === chunk.length - 1 ? lineEnd = -1 : lineEnd = crIndex : lfIndex !== -1 && (lineEnd = lfIndex), lineEnd === -1) {
277
- incompleteLine = chunk.slice(searchIndex);
278
- break;
279
- } else {
280
- const line = chunk.slice(searchIndex, lineEnd);
281
- lines.push(line), searchIndex = lineEnd + 1, chunk[searchIndex - 1] === "\r" && chunk[searchIndex] === `
282
- ` && searchIndex++;
283
- }
284
- }
285
- return [lines, incompleteLine];
343
+ function isDataPrefix(chunk, i, firstCharCode) {
344
+ return firstCharCode === 100 && chunk.charCodeAt(i + 1) === 97 && chunk.charCodeAt(i + 2) === 116 && chunk.charCodeAt(i + 3) === 97 && chunk.charCodeAt(i + 4) === 58;
345
+ }
346
+ function isEventPrefix(chunk, i, firstCharCode) {
347
+ return firstCharCode === 101 && chunk.charCodeAt(i + 1) === 118 && chunk.charCodeAt(i + 2) === 101 && chunk.charCodeAt(i + 3) === 110 && chunk.charCodeAt(i + 4) === 116 && chunk.charCodeAt(i + 5) === 58;
286
348
  }
287
349
  var EventSourceParserStream = class extends TransformStream {
288
350
  constructor({ onError, onRetry, onComment } = {}) {
@@ -5692,9 +5754,9 @@ var ParseError2 = class extends Error {
5692
5754
  super(message), this.name = "ParseError", this.type = options.type, this.field = options.field, this.value = options.value, this.line = options.line;
5693
5755
  }
5694
5756
  };
5695
- var LF = 10;
5696
- var CR = 13;
5697
- var SPACE = 32;
5757
+ var LF2 = 10;
5758
+ var CR2 = 13;
5759
+ var SPACE2 = 32;
5698
5760
  function noop2(_arg) {
5699
5761
  }
5700
5762
  function createParser2(callbacks) {
@@ -5733,17 +5795,17 @@ function createParser2(callbacks) {
5733
5795
  continue;
5734
5796
  }
5735
5797
  const firstCharCode = chunk.charCodeAt(searchIndex);
5736
- if (isDataPrefix(chunk, searchIndex, firstCharCode)) {
5737
- const valueStart = chunk.charCodeAt(searchIndex + 5) === SPACE ? searchIndex + 6 : searchIndex + 5, value = chunk.slice(valueStart, lfIndex);
5738
- if (dataLines === 0 && chunk.charCodeAt(lfIndex + 1) === LF) {
5798
+ if (isDataPrefix2(chunk, searchIndex, firstCharCode)) {
5799
+ const valueStart = chunk.charCodeAt(searchIndex + 5) === SPACE2 ? searchIndex + 6 : searchIndex + 5, value = chunk.slice(valueStart, lfIndex);
5800
+ if (dataLines === 0 && chunk.charCodeAt(lfIndex + 1) === LF2) {
5739
5801
  onEvent({ id, event: eventType, data: value }), id = void 0, data = "", eventType = void 0, searchIndex = lfIndex + 2, lfIndex = chunk.indexOf(`
5740
5802
  `, searchIndex);
5741
5803
  continue;
5742
5804
  }
5743
5805
  data = dataLines === 0 ? value : `${data}
5744
5806
  ${value}`, dataLines++;
5745
- } else isEventPrefix(chunk, searchIndex, firstCharCode) ? eventType = chunk.slice(
5746
- chunk.charCodeAt(searchIndex + 6) === SPACE ? searchIndex + 7 : searchIndex + 6,
5807
+ } else isEventPrefix2(chunk, searchIndex, firstCharCode) ? eventType = chunk.slice(
5808
+ chunk.charCodeAt(searchIndex + 6) === SPACE2 ? searchIndex + 7 : searchIndex + 6,
5747
5809
  lfIndex
5748
5810
  ) || void 0 : parseLine(chunk, searchIndex, lfIndex);
5749
5811
  searchIndex = lfIndex + 1, lfIndex = chunk.indexOf(`
@@ -5757,7 +5819,7 @@ ${value}`, dataLines++;
5757
5819
  let lineEnd = -1;
5758
5820
  if (crIndex !== -1 && lfIndex !== -1 ? lineEnd = crIndex < lfIndex ? crIndex : lfIndex : crIndex !== -1 ? crIndex === chunk.length - 1 ? lineEnd = -1 : lineEnd = crIndex : lfIndex !== -1 && (lineEnd = lfIndex), lineEnd === -1)
5759
5821
  break;
5760
- parseLine(chunk, searchIndex, lineEnd), searchIndex = lineEnd + 1, chunk.charCodeAt(searchIndex - 1) === CR && chunk.charCodeAt(searchIndex) === LF && searchIndex++;
5822
+ parseLine(chunk, searchIndex, lineEnd), searchIndex = lineEnd + 1, chunk.charCodeAt(searchIndex - 1) === CR2 && chunk.charCodeAt(searchIndex) === LF2 && searchIndex++;
5761
5823
  }
5762
5824
  return chunk.slice(searchIndex);
5763
5825
  }
@@ -5767,25 +5829,25 @@ ${value}`, dataLines++;
5767
5829
  return;
5768
5830
  }
5769
5831
  const firstCharCode = chunk.charCodeAt(start);
5770
- if (isDataPrefix(chunk, start, firstCharCode)) {
5771
- const valueStart = chunk.charCodeAt(start + 5) === SPACE ? start + 6 : start + 5, value2 = chunk.slice(valueStart, end);
5832
+ if (isDataPrefix2(chunk, start, firstCharCode)) {
5833
+ const valueStart = chunk.charCodeAt(start + 5) === SPACE2 ? start + 6 : start + 5, value2 = chunk.slice(valueStart, end);
5772
5834
  data = dataLines === 0 ? value2 : `${data}
5773
5835
  ${value2}`, dataLines++;
5774
5836
  return;
5775
5837
  }
5776
- if (isEventPrefix(chunk, start, firstCharCode)) {
5777
- eventType = chunk.slice(chunk.charCodeAt(start + 6) === SPACE ? start + 7 : start + 6, end) || void 0;
5838
+ if (isEventPrefix2(chunk, start, firstCharCode)) {
5839
+ eventType = chunk.slice(chunk.charCodeAt(start + 6) === SPACE2 ? start + 7 : start + 6, end) || void 0;
5778
5840
  return;
5779
5841
  }
5780
5842
  if (firstCharCode === 105 && chunk.charCodeAt(start + 1) === 100 && chunk.charCodeAt(start + 2) === 58) {
5781
- const value2 = chunk.slice(chunk.charCodeAt(start + 3) === SPACE ? start + 4 : start + 3, end);
5843
+ const value2 = chunk.slice(chunk.charCodeAt(start + 3) === SPACE2 ? start + 4 : start + 3, end);
5782
5844
  id = value2.includes("\0") ? void 0 : value2;
5783
5845
  return;
5784
5846
  }
5785
5847
  if (firstCharCode === 58) {
5786
5848
  if (onComment) {
5787
5849
  const line2 = chunk.slice(start, end);
5788
- onComment(line2.slice(chunk.charCodeAt(start + 1) === SPACE ? 2 : 1));
5850
+ onComment(line2.slice(chunk.charCodeAt(start + 1) === SPACE2 ? 2 : 1));
5789
5851
  }
5790
5852
  return;
5791
5853
  }
@@ -5794,7 +5856,7 @@ ${value2}`, dataLines++;
5794
5856
  processField(line, "", line);
5795
5857
  return;
5796
5858
  }
5797
- const field = line.slice(0, fieldSeparatorIndex), offset = line.charCodeAt(fieldSeparatorIndex + 1) === SPACE ? 2 : 1, value = line.slice(fieldSeparatorIndex + offset);
5859
+ const field = line.slice(0, fieldSeparatorIndex), offset = line.charCodeAt(fieldSeparatorIndex + 1) === SPACE2 ? 2 : 1, value = line.slice(fieldSeparatorIndex + offset);
5798
5860
  processField(field, value, line);
5799
5861
  }
5800
5862
  function processField(field, value, line) {
@@ -5844,10 +5906,10 @@ ${value}`, dataLines++;
5844
5906
  }
5845
5907
  return { feed, reset };
5846
5908
  }
5847
- function isDataPrefix(chunk, i, firstCharCode) {
5909
+ function isDataPrefix2(chunk, i, firstCharCode) {
5848
5910
  return firstCharCode === 100 && chunk.charCodeAt(i + 1) === 97 && chunk.charCodeAt(i + 2) === 116 && chunk.charCodeAt(i + 3) === 97 && chunk.charCodeAt(i + 4) === 58;
5849
5911
  }
5850
- function isEventPrefix(chunk, i, firstCharCode) {
5912
+ function isEventPrefix2(chunk, i, firstCharCode) {
5851
5913
  return firstCharCode === 101 && chunk.charCodeAt(i + 1) === 118 && chunk.charCodeAt(i + 2) === 101 && chunk.charCodeAt(i + 3) === 110 && chunk.charCodeAt(i + 4) === 116 && chunk.charCodeAt(i + 5) === 58;
5852
5914
  }
5853
5915
  var EventSourceParserStream2 = class extends TransformStream {
@@ -12133,7 +12195,8 @@ function convertMastraChunkToAISDKBase({
12133
12195
  providerMetadata: chunk.payload.providerMetadata,
12134
12196
  providerExecuted: chunk.payload.providerExecuted,
12135
12197
  toolName: chunk.payload.toolName,
12136
- input: hasTransformedToolPayload(displayInputTransform) ? displayInputTransform.transformed : chunk.payload.args
12198
+ input: hasTransformedToolPayload(displayInputTransform) ? displayInputTransform.transformed : chunk.payload.args,
12199
+ ...chunk.payload.observability ? { observability: chunk.payload.observability } : {}
12137
12200
  };
12138
12201
  case "tool-call-approval":
12139
12202
  return {
@@ -12168,7 +12231,8 @@ function convertMastraChunkToAISDKBase({
12168
12231
  toolName: chunk.payload.toolName,
12169
12232
  dynamic: !!chunk.payload.dynamic,
12170
12233
  providerMetadata: chunk.payload.providerMetadata,
12171
- providerExecuted: chunk.payload.providerExecuted
12234
+ providerExecuted: chunk.payload.providerExecuted,
12235
+ ...chunk.payload.observability ? { observability: chunk.payload.observability } : {}
12172
12236
  };
12173
12237
  case "tool-call-input-streaming-end":
12174
12238
  return {
@@ -12495,6 +12559,7 @@ function convertFullStreamChunkToUIMessageStream({
12495
12559
  };
12496
12560
  }
12497
12561
  case "tool-call": {
12562
+ const observability = part.observability;
12498
12563
  return {
12499
12564
  type: "tool-input-available",
12500
12565
  toolCallId: part.toolCallId,
@@ -12502,7 +12567,12 @@ function convertFullStreamChunkToUIMessageStream({
12502
12567
  input: part.input,
12503
12568
  ...part.providerExecuted != null ? { providerExecuted: part.providerExecuted } : {},
12504
12569
  ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {},
12505
- ...part.dynamic != null ? { dynamic: part.dynamic } : {}
12570
+ ...part.dynamic != null ? { dynamic: part.dynamic } : {},
12571
+ ...observability != null ? {
12572
+ toolMetadata: {
12573
+ __mastraObservability: observability
12574
+ }
12575
+ } : {}
12506
12576
  };
12507
12577
  }
12508
12578
  case "tool-approval-request": {
@@ -14209,11 +14279,6 @@ function workflowRoute({
14209
14279
  if (!workflowToUse) {
14210
14280
  throw new Error("Workflow ID is required");
14211
14281
  }
14212
- if (contextRequestContext && params.requestContext) {
14213
- mastra.getLogger()?.warn(
14214
- `"requestContext" from the request body will be ignored because "requestContext" is already set in the route options.`
14215
- );
14216
- }
14217
14282
  const handlerOptions = {
14218
14283
  mastra,
14219
14284
  workflowId: workflowToUse,