@mastra/ai-sdk 0.0.0-fix-thread-list-20251105222841 → 0.0.0-fix-ai-sdk-dependency-20251124104209

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.cjs CHANGED
@@ -140,6 +140,28 @@ function convertMastraChunkToAISDKv5({
140
140
  toolName: chunk.payload.toolName,
141
141
  input: chunk.payload.args
142
142
  };
143
+ case "tool-call-approval":
144
+ return {
145
+ type: "data-tool-call-approval",
146
+ id: chunk.payload.toolCallId,
147
+ data: {
148
+ runId: chunk.runId,
149
+ toolCallId: chunk.payload.toolCallId,
150
+ toolName: chunk.payload.toolName,
151
+ args: chunk.payload.args
152
+ }
153
+ };
154
+ case "tool-call-suspended":
155
+ return {
156
+ type: "data-tool-call-suspended",
157
+ id: chunk.payload.toolCallId,
158
+ data: {
159
+ runId: chunk.runId,
160
+ toolCallId: chunk.payload.toolCallId,
161
+ toolName: chunk.payload.toolName,
162
+ suspendPayload: chunk.payload.suspendPayload
163
+ }
164
+ };
143
165
  case "tool-call-input-streaming-start":
144
166
  return {
145
167
  type: "tool-input-start",
@@ -230,6 +252,13 @@ function convertMastraChunkToAISDKv5({
230
252
  type: "object",
231
253
  object: chunk.object
232
254
  };
255
+ case "tripwire":
256
+ return {
257
+ type: "data-tripwire",
258
+ data: {
259
+ tripwireReason: chunk.payload.tripwireReason
260
+ }
261
+ };
233
262
  default:
234
263
  if (chunk.type && "payload" in chunk && chunk.payload) {
235
264
  return {
@@ -285,6 +314,14 @@ function convertFullStreamChunkToUIMessageStream({
285
314
  };
286
315
  }
287
316
  case "reasoning-delta": {
317
+ if (sendReasoning) {
318
+ return {
319
+ type: "reasoning-delta",
320
+ id: part.id,
321
+ delta: part.text,
322
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
323
+ };
324
+ }
288
325
  return;
289
326
  }
290
327
  case "reasoning-end": {
@@ -302,6 +339,25 @@ function convertFullStreamChunkToUIMessageStream({
302
339
  };
303
340
  }
304
341
  case "source": {
342
+ if (sendSources && part.sourceType === "url") {
343
+ return {
344
+ type: "source-url",
345
+ sourceId: part.id,
346
+ url: part.url,
347
+ title: part.title,
348
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
349
+ };
350
+ }
351
+ if (sendSources && part.sourceType === "document") {
352
+ return {
353
+ type: "source-document",
354
+ sourceId: part.id,
355
+ mediaType: part.mediaType,
356
+ title: part.title,
357
+ filename: part.filename,
358
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
359
+ };
360
+ }
305
361
  return;
306
362
  }
307
363
  case "tool-input-start": {
@@ -359,6 +415,14 @@ function convertFullStreamChunkToUIMessageStream({
359
415
  toolCallId: part.toolCallId,
360
416
  payload: part.output
361
417
  };
418
+ } else if (isDataChunkType(part.output)) {
419
+ if (!("data" in part.output)) {
420
+ throw new Error(
421
+ `UI Messages require a data property when using data- prefixed chunks
422
+ ${JSON.stringify(part)}`
423
+ );
424
+ }
425
+ return part.output;
362
426
  }
363
427
  return;
364
428
  }
@@ -384,21 +448,23 @@ function convertFullStreamChunkToUIMessageStream({
384
448
  return { type: "finish-step" };
385
449
  }
386
450
  case "start": {
387
- {
451
+ if (sendStart) {
388
452
  return {
389
453
  type: "start",
390
454
  ...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {},
391
455
  ...responseMessageId != null ? { messageId: responseMessageId } : {}
392
456
  };
393
457
  }
458
+ return;
394
459
  }
395
460
  case "finish": {
396
- {
461
+ if (sendFinish) {
397
462
  return {
398
463
  type: "finish",
399
464
  ...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {}
400
465
  };
401
466
  }
467
+ return;
402
468
  }
403
469
  case "abort": {
404
470
  return part;
@@ -463,20 +529,37 @@ function AgentNetworkToAISDKTransformer() {
463
529
  }
464
530
  });
465
531
  }
466
- function AgentStreamToAISDKTransformer(lastMessageId) {
532
+ function AgentStreamToAISDKTransformer({
533
+ lastMessageId,
534
+ sendStart,
535
+ sendFinish,
536
+ sendReasoning,
537
+ sendSources,
538
+ messageMetadata,
539
+ onError
540
+ }) {
467
541
  let bufferedSteps = /* @__PURE__ */ new Map();
542
+ let tripwireOccurred = false;
543
+ let finishEventSent = false;
468
544
  return new TransformStream({
469
545
  transform(chunk, controller) {
546
+ if (chunk.type === "tripwire") {
547
+ tripwireOccurred = true;
548
+ }
549
+ if (chunk.type === "finish") {
550
+ finishEventSent = true;
551
+ }
470
552
  const part = convertMastraChunkToAISDKv5({ chunk, mode: "stream" });
471
553
  const transformedChunk = convertFullStreamChunkToUIMessageStream({
472
554
  part,
473
- sendReasoning: false,
474
- sendSources: false,
475
- sendStart: true,
476
- sendFinish: true,
555
+ sendReasoning,
556
+ sendSources,
557
+ messageMetadataValue: messageMetadata?.({ part }),
558
+ sendStart,
559
+ sendFinish,
477
560
  responseMessageId: lastMessageId,
478
561
  onError(error) {
479
- return safeParseErrorObject(error);
562
+ return onError ? onError(error) : safeParseErrorObject(error);
480
563
  }
481
564
  });
482
565
  if (transformedChunk) {
@@ -496,6 +579,14 @@ function AgentStreamToAISDKTransformer(lastMessageId) {
496
579
  controller.enqueue(transformedChunk);
497
580
  }
498
581
  }
582
+ },
583
+ flush(controller) {
584
+ if (tripwireOccurred && !finishEventSent && sendFinish) {
585
+ controller.enqueue({
586
+ type: "finish",
587
+ finishReason: "other"
588
+ });
589
+ }
499
590
  }
500
591
  });
501
592
  }
@@ -982,7 +1073,11 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
982
1073
  }
983
1074
 
984
1075
  // src/convert-streams.ts
985
- function toAISdkV5Stream(stream, options = { from: "agent" }) {
1076
+ function toAISdkV5Stream(stream, options = {
1077
+ from: "agent",
1078
+ sendStart: true,
1079
+ sendFinish: true
1080
+ }) {
986
1081
  const from = options?.from;
987
1082
  if (from === "workflow") {
988
1083
  return stream.pipeThrough(WorkflowStreamToAISDKTransformer());
@@ -991,14 +1086,28 @@ function toAISdkV5Stream(stream, options = { from: "agent" }) {
991
1086
  return stream.pipeThrough(AgentNetworkToAISDKTransformer());
992
1087
  }
993
1088
  const agentReadable = "fullStream" in stream ? stream.fullStream : stream;
994
- return agentReadable.pipeThrough(AgentStreamToAISDKTransformer(options?.lastMessageId));
1089
+ return agentReadable.pipeThrough(
1090
+ AgentStreamToAISDKTransformer({
1091
+ lastMessageId: options?.lastMessageId,
1092
+ sendStart: options?.sendStart,
1093
+ sendFinish: options?.sendFinish,
1094
+ sendReasoning: options?.sendReasoning,
1095
+ sendSources: options?.sendSources,
1096
+ messageMetadata: options?.messageMetadata,
1097
+ onError: options?.onError
1098
+ })
1099
+ );
995
1100
  }
996
1101
 
997
1102
  // src/chat-route.ts
998
1103
  function chatRoute({
999
1104
  path = "/chat/:agentId",
1000
1105
  agent,
1001
- defaultOptions
1106
+ defaultOptions,
1107
+ sendStart = true,
1108
+ sendFinish = true,
1109
+ sendReasoning = false,
1110
+ sendSources = false
1002
1111
  }) {
1003
1112
  if (!agent && !path.includes("/:agentId")) {
1004
1113
  throw new Error("Path must include :agentId to route to the correct agent or pass the agent explicitly");
@@ -1132,7 +1241,14 @@ function chatRoute({
1132
1241
  const uiMessageStream = ai.createUIMessageStream({
1133
1242
  originalMessages: messages,
1134
1243
  execute: async ({ writer }) => {
1135
- for await (const part of toAISdkV5Stream(result, { from: "agent", lastMessageId })) {
1244
+ for await (const part of toAISdkV5Stream(result, {
1245
+ from: "agent",
1246
+ lastMessageId,
1247
+ sendStart,
1248
+ sendFinish,
1249
+ sendReasoning,
1250
+ sendSources
1251
+ })) {
1136
1252
  writer.write(part);
1137
1253
  }
1138
1254
  }
@@ -1172,9 +1288,13 @@ function workflowRoute({
1172
1288
  schema: {
1173
1289
  type: "object",
1174
1290
  properties: {
1291
+ runId: { type: "string" },
1292
+ resourceId: { type: "string" },
1175
1293
  inputData: { type: "object", additionalProperties: true },
1294
+ resumeData: { type: "object", additionalProperties: true },
1176
1295
  requestContext: { type: "object", additionalProperties: true },
1177
- tracingOptions: { type: "object", additionalProperties: true }
1296
+ tracingOptions: { type: "object", additionalProperties: true },
1297
+ step: { type: "string" }
1178
1298
  }
1179
1299
  }
1180
1300
  }
@@ -1192,7 +1312,7 @@ function workflowRoute({
1192
1312
  }
1193
1313
  },
1194
1314
  handler: async (c) => {
1195
- const { inputData, resumeData, ...rest } = await c.req.json();
1315
+ const { runId, resourceId, inputData, resumeData, ...rest } = await c.req.json();
1196
1316
  const mastra = c.get("mastra");
1197
1317
  let workflowToUse = workflow;
1198
1318
  if (!workflow) {
@@ -1211,7 +1331,7 @@ function workflowRoute({
1211
1331
  if (!workflowObj) {
1212
1332
  throw new Error(`Workflow ${workflowToUse} not found`);
1213
1333
  }
1214
- const run = await workflowObj.createRun();
1334
+ const run = await workflowObj.createRun({ runId, resourceId, ...rest });
1215
1335
  const stream = resumeData ? run.resumeStream({ resumeData, ...rest }) : run.stream({ inputData, ...rest });
1216
1336
  const uiMessageStream = ai.createUIMessageStream({
1217
1337
  execute: async ({ writer }) => {