@mastra/ai-sdk 1.0.0-beta.1 → 1.0.0-beta.2

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/CHANGELOG.md CHANGED
@@ -1,5 +1,30 @@
1
1
  # @mastra/ai-sdk
2
2
 
3
+ ## 1.0.0-beta.2
4
+
5
+ ### Major Changes
6
+
7
+ - Add sendStart, sendFinish, sendReasoning, and sendSources options to toAISdkV5Stream function, allowing fine-grained control over which message chunks are included in the converted stream. Previously, these values were hardcoded in the transformer. ([#10127](https://github.com/mastra-ai/mastra/pull/10127))
8
+
9
+ BREAKING CHANGE: AgentStreamToAISDKTransformer now accepts an options object instead of a single lastMessageId parameter
10
+
11
+ Also, add sendStart, sendFinish, sendReasoning, and sendSources parameters to
12
+ chatRoute function, enabling fine-grained control over which chunks are
13
+ included in the AI SDK stream output.
14
+
15
+ ### Patch Changes
16
+
17
+ - Extend the workflow route to accept optional runId and resourceId ([#10034](https://github.com/mastra-ai/mastra/pull/10034))
18
+ parameters, allowing clients to specify custom identifiers when
19
+ creating workflow runs. These parameters are now properly validated
20
+ in the OpenAPI schema and passed through to the createRun method.
21
+
22
+ Also updates the OpenAPI schema to include previously undocumented
23
+ resumeData and step fields.
24
+
25
+ - Updated dependencies [[`2319326`](https://github.com/mastra-ai/mastra/commit/2319326f8c64e503a09bbcf14be2dd65405445e0), [`d629361`](https://github.com/mastra-ai/mastra/commit/d629361a60f6565b5bfb11976fdaf7308af858e2), [`08c31c1`](https://github.com/mastra-ai/mastra/commit/08c31c188ebccd598acaf55e888b6397d01f7eae), [`fd3d338`](https://github.com/mastra-ai/mastra/commit/fd3d338a2c362174ed5b383f1f011ad9fb0302aa), [`c30400a`](https://github.com/mastra-ai/mastra/commit/c30400a49b994b1b97256fe785eb6c906fc2b232), [`69e0a87`](https://github.com/mastra-ai/mastra/commit/69e0a878896a2da9494945d86e056a5f8f05b851), [`01f8878`](https://github.com/mastra-ai/mastra/commit/01f88783de25e4de048c1c8aace43e26373c6ea5), [`4c77209`](https://github.com/mastra-ai/mastra/commit/4c77209e6c11678808b365d545845918c40045c8), [`d827d08`](https://github.com/mastra-ai/mastra/commit/d827d0808ffe1f3553a84e975806cc989b9735dd), [`23c10a1`](https://github.com/mastra-ai/mastra/commit/23c10a1efdd9a693c405511ab2dc8a1236603162), [`676ccc7`](https://github.com/mastra-ai/mastra/commit/676ccc7fe92468d2d45d39c31a87825c89fd1ea0), [`c10398d`](https://github.com/mastra-ai/mastra/commit/c10398d5b88f1d4af556f4267ff06f1d11e89179), [`00c2387`](https://github.com/mastra-ai/mastra/commit/00c2387f5f04a365316f851e58666ac43f8c4edf), [`ad6250d`](https://github.com/mastra-ai/mastra/commit/ad6250dbdaad927e29f74a27b83f6c468b50a705), [`3a73998`](https://github.com/mastra-ai/mastra/commit/3a73998fa4ebeb7f3dc9301afe78095fc63e7999), [`e16d553`](https://github.com/mastra-ai/mastra/commit/e16d55338403c7553531cc568125c63d53653dff), [`4d59f58`](https://github.com/mastra-ai/mastra/commit/4d59f58de2d90d6e2810a19d4518e38ddddb9038), [`e1bb9c9`](https://github.com/mastra-ai/mastra/commit/e1bb9c94b4eb68b019ae275981be3feb769b5365), [`351a11f`](https://github.com/mastra-ai/mastra/commit/351a11fcaf2ed1008977fa9b9a489fc422e51cd4)]:
26
+ - @mastra/core@1.0.0-beta.3
27
+
3
28
  ## 1.0.0-beta.1
4
29
 
5
30
  ### Patch Changes
@@ -9,6 +9,56 @@ export type chatRouteOptions<OUTPUT extends OutputSchema = undefined> = {
9
9
  } | {
10
10
  path: string;
11
11
  agent: string;
12
- });
13
- export declare function chatRoute<OUTPUT extends OutputSchema = undefined>({ path, agent, defaultOptions, }: chatRouteOptions<OUTPUT>): ReturnType<typeof registerApiRoute>;
12
+ }) & {
13
+ sendStart?: boolean;
14
+ sendFinish?: boolean;
15
+ sendReasoning?: boolean;
16
+ sendSources?: boolean;
17
+ };
18
+ /**
19
+ * Creates a chat route handler for streaming agent conversations using the AI SDK format.
20
+ *
21
+ * This function registers an HTTP POST endpoint that accepts messages, executes an agent,
22
+ * and streams the response back to the client in AI SDK v5 compatible format.
23
+ * *
24
+ * @param {chatRouteOptions} options - Configuration options for the chat route
25
+ * @param {string} [options.path='/chat/:agentId'] - The route path. Include `:agentId` for dynamic routing
26
+ * @param {string} [options.agent] - Fixed agent ID when not using dynamic routing
27
+ * @param {AgentExecutionOptions} [options.defaultOptions] - Default options passed to agent execution
28
+ * @param {boolean} [options.sendStart=true] - Whether to send start events in the stream
29
+ * @param {boolean} [options.sendFinish=true] - Whether to send finish events in the stream
30
+ * @param {boolean} [options.sendReasoning=false] - Whether to include reasoning steps in the stream
31
+ * @param {boolean} [options.sendSources=false] - Whether to include source citations in the stream
32
+ *
33
+ * @returns {ReturnType<typeof registerApiRoute>} A registered API route handler
34
+ *
35
+ * @throws {Error} When path doesn't include `:agentId` and no fixed agent is specified
36
+ * @throws {Error} When agent ID is missing at runtime
37
+ * @throws {Error} When specified agent is not found in Mastra instance
38
+ *
39
+ * @example
40
+ * // Dynamic agent routing
41
+ * chatRoute({
42
+ * path: '/chat/:agentId',
43
+ * sendReasoning: true,
44
+ * });
45
+ *
46
+ * @example
47
+ * // Fixed agent with custom path
48
+ * chatRoute({
49
+ * path: '/api/support-chat',
50
+ * agent: 'support-agent',
51
+ * defaultOptions: {
52
+ * maxSteps: 5,
53
+ * },
54
+ * });
55
+ *
56
+ * @remarks
57
+ * - The route handler expects a JSON body with a `messages` array
58
+ * - Messages should follow the format: `{ role: 'user' | 'assistant' | 'system', content: string }`
59
+ * - The response is a Server-Sent Events (SSE) stream compatible with AI SDK v5
60
+ * - If both `agent` and `:agentId` are present, a warning is logged and the fixed `agent` takes precedence
61
+ * - Request context from the incoming request overrides `defaultOptions.requestContext` if both are present
62
+ */
63
+ export declare function chatRoute<OUTPUT extends OutputSchema = undefined>({ path, agent, defaultOptions, sendStart, sendFinish, sendReasoning, sendSources, }: chatRouteOptions<OUTPUT>): ReturnType<typeof registerApiRoute>;
14
64
  //# sourceMappingURL=chat-route.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"chat-route.d.ts","sourceRoot":"","sources":["../src/chat-route.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAEhE,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAIxD,MAAM,MAAM,gBAAgB,CAAC,MAAM,SAAS,YAAY,GAAG,SAAS,IAAI;IACtE,cAAc,CAAC,EAAE,qBAAqB,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACzD,GAAG,CACA;IACE,IAAI,EAAE,GAAG,MAAM,WAAW,MAAM,EAAE,CAAC;IACnC,KAAK,CAAC,EAAE,KAAK,CAAC;CACf,GACD;IACE,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACf,CACJ,CAAC;AAEF,wBAAgB,SAAS,CAAC,MAAM,SAAS,YAAY,GAAG,SAAS,EAAE,EACjE,IAAuB,EACvB,KAAK,EACL,cAAc,GACf,EAAE,gBAAgB,CAAC,MAAM,CAAC,GAAG,UAAU,CAAC,OAAO,gBAAgB,CAAC,CA6JhE"}
1
+ {"version":3,"file":"chat-route.d.ts","sourceRoot":"","sources":["../src/chat-route.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAEhE,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAIxD,MAAM,MAAM,gBAAgB,CAAC,MAAM,SAAS,YAAY,GAAG,SAAS,IAAI;IACtE,cAAc,CAAC,EAAE,qBAAqB,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACzD,GAAG,CACA;IACE,IAAI,EAAE,GAAG,MAAM,WAAW,MAAM,EAAE,CAAC;IACnC,KAAK,CAAC,EAAE,KAAK,CAAC;CACf,GACD;IACE,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACf,CACJ,GAAG;IACA,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB,CAAC;AAEJ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH,wBAAgB,SAAS,CAAC,MAAM,SAAS,YAAY,GAAG,SAAS,EAAE,EACjE,IAAuB,EACvB,KAAK,EACL,cAAc,EACd,SAAgB,EAChB,UAAiB,EACjB,aAAqB,EACrB,WAAmB,GACpB,EAAE,gBAAgB,CAAC,MAAM,CAAC,GAAG,UAAU,CAAC,OAAO,gBAAgB,CAAC,CAoKhE"}
@@ -2,7 +2,7 @@ import type { MessageListInput } from '@mastra/core/agent/message-list';
2
2
  /**
3
3
  * Converts messages to AI SDK V5 UI format
4
4
  */
5
- export declare function toAISdkV5Messages(messages: MessageListInput): import("ai").UIMessage<unknown, import("ai").UIDataTypes, import("ai").UITools>[];
5
+ export declare function toAISdkV5Messages(messages: MessageListInput): import("ai-v5").UIMessage<unknown, import("ai-v5").UIDataTypes, import("ai-v5").UITools>[];
6
6
  /**
7
7
  * Converts messages to AI SDK V4 UI format
8
8
  */
@@ -1 +1 @@
1
- {"version":3,"file":"convert-messages.d.ts","sourceRoot":"","sources":["../src/convert-messages.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AAExE;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,gBAAgB,qFAE3D;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,gBAAgB,wDAE3D"}
1
+ {"version":3,"file":"convert-messages.d.ts","sourceRoot":"","sources":["../src/convert-messages.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AAExE;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,gBAAgB,8FAE3D;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,gBAAgB,wDAE3D"}
@@ -2,6 +2,50 @@ import type { MastraModelOutput, OutputSchema, MastraAgentNetworkStream, Workflo
2
2
  import type { MastraWorkflowStream, Step, WorkflowResult } from '@mastra/core/workflows';
3
3
  import type { InferUIMessageChunk, UIMessage } from 'ai';
4
4
  import type { ZodObject, ZodType } from 'zod';
5
+ /**
6
+ * Converts Mastra streams (workflow, agent network, or agent) to AI SDK v5 compatible streams.
7
+ *
8
+ * This function transforms various Mastra stream types into ReadableStream objects that are compatible
9
+ * with the AI SDK v5, enabling seamless integration with AI SDK's streaming capabilities.
10
+ *
11
+ *
12
+ * @param {MastraWorkflowStream | WorkflowRunOutput | MastraAgentNetworkStream | MastraModelOutput} stream
13
+ * The Mastra stream to convert. Can be one of:
14
+ * - MastraWorkflowStream: A workflow execution stream
15
+ * - WorkflowRunOutput: The output of a workflow run
16
+ * - MastraAgentNetworkStream: An agent network execution stream
17
+ * - MastraModelOutput: An agent model output stream
18
+ *
19
+ * @param {Object} options - Conversion options
20
+ * @param {'workflow' | 'network' | 'agent'} options.from - The type of stream being converted. Defaults to 'agent'
21
+ * @param {string} [options.lastMessageId] - (Agent only) The ID of the last message in the conversation
22
+ * @param {boolean} [options.sendStart=true] - (Agent only) Whether to send start events. Defaults to true
23
+ * @param {boolean} [options.sendFinish=true] - (Agent only) Whether to send finish events. Defaults to true
24
+ * @param {boolean} [options.sendReasoning] - (Agent only) Whether to include reasoning in the output
25
+ * @param {boolean} [options.sendSources] - (Agent only) Whether to include sources in the output
26
+ *
27
+ * @returns {ReadableStream<InferUIMessageChunk<UIMessage>>} A ReadableStream compatible with AI SDK v5
28
+ *
29
+ * @example
30
+ * // Convert a workflow stream
31
+ * const workflowStream = await workflowRun.stream(...);
32
+ * const aiSDKStream = toAISdkV5Stream(workflowStream, { from: 'workflow' });
33
+ *
34
+ * @example
35
+ * // Convert an agent network stream
36
+ * const networkStream = await agentNetwork.network(...);
37
+ * const aiSDKStream = toAISdkV5Stream(networkStream, { from: 'network' });
38
+ *
39
+ * @example
40
+ * // Convert an agent stream with custom options
41
+ * const agentStream = await agent.stream(...);
42
+ * const aiSDKStream = toAISdkV5Stream(agentStream, {
43
+ * from: 'agent',
44
+ * lastMessageId: 'msg-123',
45
+ * sendReasoning: true,
46
+ * sendSources: true
47
+ * });
48
+ */
5
49
  export declare function toAISdkV5Stream<TOutput extends ZodType<any>, TInput extends ZodType<any>, TSteps extends Step<string, any, any, any, any, any>[], TState extends ZodObject<any>>(stream: MastraWorkflowStream<TState, TInput, TOutput, TSteps>, options: {
6
50
  from: 'workflow';
7
51
  }): ReadableStream<InferUIMessageChunk<UIMessage>>;
@@ -14,5 +58,9 @@ export declare function toAISdkV5Stream(stream: MastraAgentNetworkStream, option
14
58
  export declare function toAISdkV5Stream<TOutput extends OutputSchema>(stream: MastraModelOutput<TOutput>, options: {
15
59
  from: 'agent';
16
60
  lastMessageId?: string;
61
+ sendStart?: boolean;
62
+ sendFinish?: boolean;
63
+ sendReasoning?: boolean;
64
+ sendSources?: boolean;
17
65
  }): ReadableStream<InferUIMessageChunk<UIMessage>>;
18
66
  //# sourceMappingURL=convert-streams.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"convert-streams.d.ts","sourceRoot":"","sources":["../src/convert-streams.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,EAEjB,YAAY,EACZ,wBAAwB,EACxB,iBAAiB,EAClB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,KAAK,EAAE,oBAAoB,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,wBAAwB,CA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CAAC,GAAG,CAAC,EAC3B,MAAM,SAAS,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,EAAE,EACtD,MAAM,SAAS,SAAS,CAAC,GAAG,CAAC,EAE7B,MAAM,EAAE,iBAAiB,CAAC,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC,EAC1E,OAAO,EAAE;IAAE,IAAI,EAAE,UAAU,CAAA;CAAE,GAC5B,cAAc,CAAC,mBAAmB,CAAC,SAAS,CAAC,CAAC,CAAC;AAClD,wBAAgB,eAAe,CAC7B,MAAM,EAAE,wBAAwB,EAChC,OAAO,EAAE;IAAE,IAAI,EAAE,SAAS,CAAA;CAAE,GAC3B,cAAc,CAAC,mBAAmB,CAAC,SAAS,CAAC,CAAC,CAAC;AAClD,wBAAgB,eAAe,CAAC,OAAO,SAAS,YAAY,EAC1D,MAAM,EAAE,iBAAiB,CAAC,OAAO,CAAC,EAClC,OAAO,EAAE;IACP,IAAI,EAAE,OAAO,CAAC;IACd,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB,GACA,cAAc,CAAC,mBAAmB,CAAC,SAAS,CAAC,CAAC,CAAC"}
package/dist/index.cjs CHANGED
@@ -285,6 +285,14 @@ function convertFullStreamChunkToUIMessageStream({
285
285
  };
286
286
  }
287
287
  case "reasoning-delta": {
288
+ if (sendReasoning) {
289
+ return {
290
+ type: "reasoning-delta",
291
+ id: part.id,
292
+ delta: part.text,
293
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
294
+ };
295
+ }
288
296
  return;
289
297
  }
290
298
  case "reasoning-end": {
@@ -302,6 +310,25 @@ function convertFullStreamChunkToUIMessageStream({
302
310
  };
303
311
  }
304
312
  case "source": {
313
+ if (sendSources && part.sourceType === "url") {
314
+ return {
315
+ type: "source-url",
316
+ sourceId: part.id,
317
+ url: part.url,
318
+ title: part.title,
319
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
320
+ };
321
+ }
322
+ if (sendSources && part.sourceType === "document") {
323
+ return {
324
+ type: "source-document",
325
+ sourceId: part.id,
326
+ mediaType: part.mediaType,
327
+ title: part.title,
328
+ filename: part.filename,
329
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
330
+ };
331
+ }
305
332
  return;
306
333
  }
307
334
  case "tool-input-start": {
@@ -392,21 +419,23 @@ function convertFullStreamChunkToUIMessageStream({
392
419
  return { type: "finish-step" };
393
420
  }
394
421
  case "start": {
395
- {
422
+ if (sendStart) {
396
423
  return {
397
424
  type: "start",
398
425
  ...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {},
399
426
  ...responseMessageId != null ? { messageId: responseMessageId } : {}
400
427
  };
401
428
  }
429
+ return;
402
430
  }
403
431
  case "finish": {
404
- {
432
+ if (sendFinish) {
405
433
  return {
406
434
  type: "finish",
407
435
  ...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {}
408
436
  };
409
437
  }
438
+ return;
410
439
  }
411
440
  case "abort": {
412
441
  return part;
@@ -471,17 +500,23 @@ function AgentNetworkToAISDKTransformer() {
471
500
  }
472
501
  });
473
502
  }
474
- function AgentStreamToAISDKTransformer(lastMessageId) {
503
+ function AgentStreamToAISDKTransformer({
504
+ lastMessageId,
505
+ sendStart,
506
+ sendFinish,
507
+ sendReasoning,
508
+ sendSources
509
+ }) {
475
510
  let bufferedSteps = /* @__PURE__ */ new Map();
476
511
  return new TransformStream({
477
512
  transform(chunk, controller) {
478
513
  const part = convertMastraChunkToAISDKv5({ chunk, mode: "stream" });
479
514
  const transformedChunk = convertFullStreamChunkToUIMessageStream({
480
515
  part,
481
- sendReasoning: false,
482
- sendSources: false,
483
- sendStart: true,
484
- sendFinish: true,
516
+ sendReasoning,
517
+ sendSources,
518
+ sendStart,
519
+ sendFinish,
485
520
  responseMessageId: lastMessageId,
486
521
  onError(error) {
487
522
  return safeParseErrorObject(error);
@@ -990,7 +1025,11 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
990
1025
  }
991
1026
 
992
1027
  // src/convert-streams.ts
993
- function toAISdkV5Stream(stream, options = { from: "agent" }) {
1028
+ function toAISdkV5Stream(stream, options = {
1029
+ from: "agent",
1030
+ sendStart: true,
1031
+ sendFinish: true
1032
+ }) {
994
1033
  const from = options?.from;
995
1034
  if (from === "workflow") {
996
1035
  return stream.pipeThrough(WorkflowStreamToAISDKTransformer());
@@ -999,14 +1038,26 @@ function toAISdkV5Stream(stream, options = { from: "agent" }) {
999
1038
  return stream.pipeThrough(AgentNetworkToAISDKTransformer());
1000
1039
  }
1001
1040
  const agentReadable = "fullStream" in stream ? stream.fullStream : stream;
1002
- return agentReadable.pipeThrough(AgentStreamToAISDKTransformer(options?.lastMessageId));
1041
+ return agentReadable.pipeThrough(
1042
+ AgentStreamToAISDKTransformer({
1043
+ lastMessageId: options?.lastMessageId,
1044
+ sendStart: options?.sendStart,
1045
+ sendFinish: options?.sendFinish,
1046
+ sendReasoning: options?.sendReasoning,
1047
+ sendSources: options?.sendSources
1048
+ })
1049
+ );
1003
1050
  }
1004
1051
 
1005
1052
  // src/chat-route.ts
1006
1053
  function chatRoute({
1007
1054
  path = "/chat/:agentId",
1008
1055
  agent,
1009
- defaultOptions
1056
+ defaultOptions,
1057
+ sendStart = true,
1058
+ sendFinish = true,
1059
+ sendReasoning = false,
1060
+ sendSources = false
1010
1061
  }) {
1011
1062
  if (!agent && !path.includes("/:agentId")) {
1012
1063
  throw new Error("Path must include :agentId to route to the correct agent or pass the agent explicitly");
@@ -1140,7 +1191,14 @@ function chatRoute({
1140
1191
  const uiMessageStream = ai.createUIMessageStream({
1141
1192
  originalMessages: messages,
1142
1193
  execute: async ({ writer }) => {
1143
- for await (const part of toAISdkV5Stream(result, { from: "agent", lastMessageId })) {
1194
+ for await (const part of toAISdkV5Stream(result, {
1195
+ from: "agent",
1196
+ lastMessageId,
1197
+ sendStart,
1198
+ sendFinish,
1199
+ sendReasoning,
1200
+ sendSources
1201
+ })) {
1144
1202
  writer.write(part);
1145
1203
  }
1146
1204
  }
@@ -1180,9 +1238,13 @@ function workflowRoute({
1180
1238
  schema: {
1181
1239
  type: "object",
1182
1240
  properties: {
1241
+ runId: { type: "string" },
1242
+ resourceId: { type: "string" },
1183
1243
  inputData: { type: "object", additionalProperties: true },
1244
+ resumeData: { type: "object", additionalProperties: true },
1184
1245
  requestContext: { type: "object", additionalProperties: true },
1185
- tracingOptions: { type: "object", additionalProperties: true }
1246
+ tracingOptions: { type: "object", additionalProperties: true },
1247
+ step: { type: "string" }
1186
1248
  }
1187
1249
  }
1188
1250
  }
@@ -1200,7 +1262,7 @@ function workflowRoute({
1200
1262
  }
1201
1263
  },
1202
1264
  handler: async (c) => {
1203
- const { inputData, resumeData, ...rest } = await c.req.json();
1265
+ const { runId, resourceId, inputData, resumeData, ...rest } = await c.req.json();
1204
1266
  const mastra = c.get("mastra");
1205
1267
  let workflowToUse = workflow;
1206
1268
  if (!workflow) {
@@ -1219,7 +1281,7 @@ function workflowRoute({
1219
1281
  if (!workflowObj) {
1220
1282
  throw new Error(`Workflow ${workflowToUse} not found`);
1221
1283
  }
1222
- const run = await workflowObj.createRun();
1284
+ const run = await workflowObj.createRun({ runId, resourceId, ...rest });
1223
1285
  const stream = resumeData ? run.resumeStream({ resumeData, ...rest }) : run.stream({ inputData, ...rest });
1224
1286
  const uiMessageStream = ai.createUIMessageStream({
1225
1287
  execute: async ({ writer }) => {