@mastra/ai-sdk 0.0.0-fix-9244-clickhouse-metadata-20251105010900 → 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
@@ -24,6 +24,12 @@ function safeParseErrorObject(obj) {
24
24
  return String(obj);
25
25
  }
26
26
  }
27
+ var isAgentExecutionDataChunkType = (chunk) => {
28
+ return chunk && typeof chunk === "object" && "type" in chunk && chunk.type?.startsWith("agent-execution-event-") && "payload" in chunk && typeof chunk.payload === "object" && "type" in chunk.payload && chunk.payload.type?.startsWith("data-");
29
+ };
30
+ var isWorkflowExecutionDataChunkType = (chunk) => {
31
+ return chunk && typeof chunk === "object" && "type" in chunk && chunk.type?.startsWith("workflow-execution-event-") && "payload" in chunk && typeof chunk.payload === "object" && "type" in chunk.payload && chunk.payload.type?.startsWith("data-");
32
+ };
27
33
 
28
34
  // src/helpers.ts
29
35
  function convertMastraChunkToAISDKv5({
@@ -134,6 +140,28 @@ function convertMastraChunkToAISDKv5({
134
140
  toolName: chunk.payload.toolName,
135
141
  input: chunk.payload.args
136
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
+ };
137
165
  case "tool-call-input-streaming-start":
138
166
  return {
139
167
  type: "tool-input-start",
@@ -224,6 +252,13 @@ function convertMastraChunkToAISDKv5({
224
252
  type: "object",
225
253
  object: chunk.object
226
254
  };
255
+ case "tripwire":
256
+ return {
257
+ type: "data-tripwire",
258
+ data: {
259
+ tripwireReason: chunk.payload.tripwireReason
260
+ }
261
+ };
227
262
  default:
228
263
  if (chunk.type && "payload" in chunk && chunk.payload) {
229
264
  return {
@@ -279,6 +314,14 @@ function convertFullStreamChunkToUIMessageStream({
279
314
  };
280
315
  }
281
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
+ }
282
325
  return;
283
326
  }
284
327
  case "reasoning-end": {
@@ -296,6 +339,25 @@ function convertFullStreamChunkToUIMessageStream({
296
339
  };
297
340
  }
298
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
+ }
299
361
  return;
300
362
  }
301
363
  case "tool-input-start": {
@@ -353,6 +415,14 @@ function convertFullStreamChunkToUIMessageStream({
353
415
  toolCallId: part.toolCallId,
354
416
  payload: part.output
355
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;
356
426
  }
357
427
  return;
358
428
  }
@@ -378,21 +448,23 @@ function convertFullStreamChunkToUIMessageStream({
378
448
  return { type: "finish-step" };
379
449
  }
380
450
  case "start": {
381
- {
451
+ if (sendStart) {
382
452
  return {
383
453
  type: "start",
384
454
  ...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {},
385
455
  ...responseMessageId != null ? { messageId: responseMessageId } : {}
386
456
  };
387
457
  }
458
+ return;
388
459
  }
389
460
  case "finish": {
390
- {
461
+ if (sendFinish) {
391
462
  return {
392
463
  type: "finish",
393
464
  ...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {}
394
465
  };
395
466
  }
467
+ return;
396
468
  }
397
469
  case "abort": {
398
470
  return part;
@@ -457,20 +529,37 @@ function AgentNetworkToAISDKTransformer() {
457
529
  }
458
530
  });
459
531
  }
460
- function AgentStreamToAISDKTransformer(lastMessageId) {
532
+ function AgentStreamToAISDKTransformer({
533
+ lastMessageId,
534
+ sendStart,
535
+ sendFinish,
536
+ sendReasoning,
537
+ sendSources,
538
+ messageMetadata,
539
+ onError
540
+ }) {
461
541
  let bufferedSteps = /* @__PURE__ */ new Map();
542
+ let tripwireOccurred = false;
543
+ let finishEventSent = false;
462
544
  return new TransformStream({
463
545
  transform(chunk, controller) {
546
+ if (chunk.type === "tripwire") {
547
+ tripwireOccurred = true;
548
+ }
549
+ if (chunk.type === "finish") {
550
+ finishEventSent = true;
551
+ }
464
552
  const part = convertMastraChunkToAISDKv5({ chunk, mode: "stream" });
465
553
  const transformedChunk = convertFullStreamChunkToUIMessageStream({
466
554
  part,
467
- sendReasoning: false,
468
- sendSources: false,
469
- sendStart: true,
470
- sendFinish: true,
555
+ sendReasoning,
556
+ sendSources,
557
+ messageMetadataValue: messageMetadata?.({ part }),
558
+ sendStart,
559
+ sendFinish,
471
560
  responseMessageId: lastMessageId,
472
561
  onError(error) {
473
- return safeParseErrorObject(error);
562
+ return onError ? onError(error) : safeParseErrorObject(error);
474
563
  }
475
564
  });
476
565
  if (transformedChunk) {
@@ -490,6 +579,14 @@ function AgentStreamToAISDKTransformer(lastMessageId) {
490
579
  controller.enqueue(transformedChunk);
491
580
  }
492
581
  }
582
+ },
583
+ flush(controller) {
584
+ if (tripwireOccurred && !finishEventSent && sendFinish) {
585
+ controller.enqueue({
586
+ type: "finish",
587
+ finishReason: "other"
588
+ });
589
+ }
493
590
  }
494
591
  });
495
592
  }
@@ -952,13 +1049,35 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
952
1049
  }
953
1050
  return payload;
954
1051
  }
1052
+ if (isAgentExecutionDataChunkType(payload)) {
1053
+ if (!("data" in payload.payload)) {
1054
+ throw new Error(
1055
+ `UI Messages require a data property when using data- prefixed chunks
1056
+ ${JSON.stringify(payload)}`
1057
+ );
1058
+ }
1059
+ return payload.payload;
1060
+ }
1061
+ if (isWorkflowExecutionDataChunkType(payload)) {
1062
+ if (!("data" in payload.payload)) {
1063
+ throw new Error(
1064
+ `UI Messages require a data property when using data- prefixed chunks
1065
+ ${JSON.stringify(payload)}`
1066
+ );
1067
+ }
1068
+ return payload.payload;
1069
+ }
955
1070
  return null;
956
1071
  }
957
1072
  }
958
1073
  }
959
1074
 
960
1075
  // src/convert-streams.ts
961
- function toAISdkV5Stream(stream, options = { from: "agent" }) {
1076
+ function toAISdkV5Stream(stream, options = {
1077
+ from: "agent",
1078
+ sendStart: true,
1079
+ sendFinish: true
1080
+ }) {
962
1081
  const from = options?.from;
963
1082
  if (from === "workflow") {
964
1083
  return stream.pipeThrough(WorkflowStreamToAISDKTransformer());
@@ -967,14 +1086,28 @@ function toAISdkV5Stream(stream, options = { from: "agent" }) {
967
1086
  return stream.pipeThrough(AgentNetworkToAISDKTransformer());
968
1087
  }
969
1088
  const agentReadable = "fullStream" in stream ? stream.fullStream : stream;
970
- 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
+ );
971
1100
  }
972
1101
 
973
1102
  // src/chat-route.ts
974
1103
  function chatRoute({
975
1104
  path = "/chat/:agentId",
976
1105
  agent,
977
- defaultOptions
1106
+ defaultOptions,
1107
+ sendStart = true,
1108
+ sendFinish = true,
1109
+ sendReasoning = false,
1110
+ sendSources = false
978
1111
  }) {
979
1112
  if (!agent && !path.includes("/:agentId")) {
980
1113
  throw new Error("Path must include :agentId to route to the correct agent or pass the agent explicitly");
@@ -1108,7 +1241,14 @@ function chatRoute({
1108
1241
  const uiMessageStream = ai.createUIMessageStream({
1109
1242
  originalMessages: messages,
1110
1243
  execute: async ({ writer }) => {
1111
- 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
+ })) {
1112
1252
  writer.write(part);
1113
1253
  }
1114
1254
  }
@@ -1148,9 +1288,13 @@ function workflowRoute({
1148
1288
  schema: {
1149
1289
  type: "object",
1150
1290
  properties: {
1291
+ runId: { type: "string" },
1292
+ resourceId: { type: "string" },
1151
1293
  inputData: { type: "object", additionalProperties: true },
1294
+ resumeData: { type: "object", additionalProperties: true },
1152
1295
  requestContext: { type: "object", additionalProperties: true },
1153
- tracingOptions: { type: "object", additionalProperties: true }
1296
+ tracingOptions: { type: "object", additionalProperties: true },
1297
+ step: { type: "string" }
1154
1298
  }
1155
1299
  }
1156
1300
  }
@@ -1168,7 +1312,7 @@ function workflowRoute({
1168
1312
  }
1169
1313
  },
1170
1314
  handler: async (c) => {
1171
- const { inputData, resumeData, ...rest } = await c.req.json();
1315
+ const { runId, resourceId, inputData, resumeData, ...rest } = await c.req.json();
1172
1316
  const mastra = c.get("mastra");
1173
1317
  let workflowToUse = workflow;
1174
1318
  if (!workflow) {
@@ -1187,7 +1331,7 @@ function workflowRoute({
1187
1331
  if (!workflowObj) {
1188
1332
  throw new Error(`Workflow ${workflowToUse} not found`);
1189
1333
  }
1190
- const run = await workflowObj.createRun();
1334
+ const run = await workflowObj.createRun({ runId, resourceId, ...rest });
1191
1335
  const stream = resumeData ? run.resumeStream({ resumeData, ...rest }) : run.stream({ inputData, ...rest });
1192
1336
  const uiMessageStream = ai.createUIMessageStream({
1193
1337
  execute: async ({ writer }) => {