@mastra/ai-sdk 1.0.0-beta.4 → 1.0.0-beta.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/CHANGELOG.md +66 -0
- package/README.md +60 -0
- package/dist/chat-route.d.ts +33 -1
- package/dist/chat-route.d.ts.map +1 -1
- package/dist/convert-streams.d.ts +2 -0
- package/dist/convert-streams.d.ts.map +1 -1
- package/dist/index.cjs +185 -66
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +6 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +183 -67
- package/dist/index.js.map +1 -1
- package/dist/network-route.d.ts +30 -2
- package/dist/network-route.d.ts.map +1 -1
- package/dist/transformers.d.ts +118 -7
- package/dist/transformers.d.ts.map +1 -1
- package/dist/utils.d.ts +2 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/workflow-route.d.ts +38 -1
- package/dist/workflow-route.d.ts.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,71 @@
|
|
|
1
1
|
# @mastra/ai-sdk
|
|
2
2
|
|
|
3
|
+
## 1.0.0-beta.5
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Add framework-agnostic stream handlers for use outside of Hono/Mastra server ([#10628](https://github.com/mastra-ai/mastra/pull/10628))
|
|
8
|
+
- `handleChatStream`: Standalone handler for streaming agent chat in AI SDK format
|
|
9
|
+
- `handleWorkflowStream`: Standalone handler for streaming workflow execution in AI SDK format
|
|
10
|
+
- `handleNetworkStream`: Standalone handler for streaming agent network execution in AI SDK format
|
|
11
|
+
These functions accept all arguments explicitly and return a `ReadableStream`, making them usable in any framework (Next.js App Router, Express, etc.) without depending on Hono context.
|
|
12
|
+
|
|
13
|
+
Example usage:
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { handleChatStream } from '@mastra/ai-sdk';
|
|
17
|
+
import { createUIMessageStreamResponse } from 'ai';
|
|
18
|
+
export async function POST(req: Request) {
|
|
19
|
+
const params = await req.json();
|
|
20
|
+
const stream = await handleChatStream({
|
|
21
|
+
mastra,
|
|
22
|
+
agentId: 'weatherAgent',
|
|
23
|
+
params,
|
|
24
|
+
});
|
|
25
|
+
return createUIMessageStreamResponse({ stream });
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
New exports:
|
|
30
|
+
- handleChatStream, ChatStreamHandlerParams, ChatStreamHandlerOptions
|
|
31
|
+
- handleWorkflowStream, WorkflowStreamHandlerParams, WorkflowStreamHandlerOptions
|
|
32
|
+
- handleNetworkStream, NetworkStreamHandlerParams, NetworkStreamHandlerOptions
|
|
33
|
+
|
|
34
|
+
### Patch Changes
|
|
35
|
+
|
|
36
|
+
- Support streaming agent text chunks from workflow-step-output ([#10540](https://github.com/mastra-ai/mastra/pull/10540))
|
|
37
|
+
|
|
38
|
+
Adds support for streaming text and tool call chunks from agents running inside workflows via the workflow-step-output event. When you pipe an agent's stream into a workflow step's writer, the text chunks, tool calls, and other streaming events are automatically included in the workflow stream and converted to UI messages.
|
|
39
|
+
|
|
40
|
+
**Features:**
|
|
41
|
+
- Added `includeTextStreamParts` option to `WorkflowStreamToAISDKTransformer` (defaults to `true`)
|
|
42
|
+
- Added `isMastraTextStreamChunk` type guard to identify Mastra chunks with text streaming data
|
|
43
|
+
- Support for streaming text chunks: `text-start`, `text-delta`, `text-end`
|
|
44
|
+
- Support for streaming tool calls: `tool-call`, `tool-result`
|
|
45
|
+
- Comprehensive test coverage in `transformers.test.ts`
|
|
46
|
+
- Updated documentation for workflow streaming and `workflowRoute()`
|
|
47
|
+
|
|
48
|
+
**Example:**
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
const planActivities = createStep({
|
|
52
|
+
execute: async ({ mastra, writer }) => {
|
|
53
|
+
const agent = mastra?.getAgent('weatherAgent');
|
|
54
|
+
const response = await agent.stream('Plan activities');
|
|
55
|
+
await response.fullStream.pipeTo(writer);
|
|
56
|
+
|
|
57
|
+
return { activities: await response.text };
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
When served via `workflowRoute()`, the UI receives incremental text updates as the agent generates its response, providing a smooth streaming experience.
|
|
63
|
+
|
|
64
|
+
- Added support for resuming agent streams in the chat route. You can now pass `resumeData` in the request body to continue a previous agent stream, enabling long-running conversations and multi-step agent workflows. ([#10448](https://github.com/mastra-ai/mastra/pull/10448))
|
|
65
|
+
|
|
66
|
+
- Updated dependencies [[`ac0d2f4`](https://github.com/mastra-ai/mastra/commit/ac0d2f4ff8831f72c1c66c2be809706d17f65789), [`1a0d3fc`](https://github.com/mastra-ai/mastra/commit/1a0d3fc811482c9c376cdf79ee615c23bae9b2d6), [`85a628b`](https://github.com/mastra-ai/mastra/commit/85a628b1224a8f64cd82ea7f033774bf22df7a7e), [`c237233`](https://github.com/mastra-ai/mastra/commit/c23723399ccedf7f5744b3f40997b79246bfbe64), [`15f9e21`](https://github.com/mastra-ai/mastra/commit/15f9e216177201ea6e3f6d0bfb063fcc0953444f), [`ff94dea`](https://github.com/mastra-ai/mastra/commit/ff94dea935f4e34545c63bcb6c29804732698809), [`5b2ff46`](https://github.com/mastra-ai/mastra/commit/5b2ff4651df70c146523a7fca773f8eb0a2272f8), [`db41688`](https://github.com/mastra-ai/mastra/commit/db4168806d007417e2e60b4f68656dca4e5f40c9), [`5ca599d`](https://github.com/mastra-ai/mastra/commit/5ca599d0bb59a1595f19f58473fcd67cc71cef58), [`bff1145`](https://github.com/mastra-ai/mastra/commit/bff114556b3cbadad9b2768488708f8ad0e91475), [`5c8ca24`](https://github.com/mastra-ai/mastra/commit/5c8ca247094e0cc2cdbd7137822fb47241f86e77), [`e191844`](https://github.com/mastra-ai/mastra/commit/e1918444ca3f80e82feef1dad506cd4ec6e2875f), [`22553f1`](https://github.com/mastra-ai/mastra/commit/22553f11c63ee5e966a9c034a349822249584691), [`7237163`](https://github.com/mastra-ai/mastra/commit/72371635dbf96a87df4b073cc48fc655afbdce3d), [`2500740`](https://github.com/mastra-ai/mastra/commit/2500740ea23da067d6e50ec71c625ab3ce275e64), [`873ecbb`](https://github.com/mastra-ai/mastra/commit/873ecbb517586aa17d2f1e99283755b3ebb2863f), [`4f9bbe5`](https://github.com/mastra-ai/mastra/commit/4f9bbe5968f42c86f4930b8193de3c3c17e5bd36), [`02e51fe`](https://github.com/mastra-ai/mastra/commit/02e51feddb3d4155cfbcc42624fd0d0970d032c0), [`8f3fa3a`](https://github.com/mastra-ai/mastra/commit/8f3fa3a652bb77da092f913ec51ae46e3a7e27dc), [`cd29ad2`](https://github.com/mastra-ai/mastra/commit/cd29ad23a255534e8191f249593849ed29160886), [`bdf4d8c`](https://github.com/mastra-ai/mastra/commit/bdf4d8cdc656d8a2c21d81834bfa3bfa70f56c16), [`854e3da`](https://github.com/mastra-ai/mastra/commit/854e3dad5daac17a91a20986399d3a51f54bf68b), [`ce18d38`](https://github.com/mastra-ai/mastra/commit/ce18d38678c65870350d123955014a8432075fd9), [`cccf9c8`](https://github.com/mastra-ai/mastra/commit/cccf9c8b2d2dfc1a5e63919395b83d78c89682a0), [`61a5705`](https://github.com/mastra-ai/mastra/commit/61a570551278b6743e64243b3ce7d73de915ca8a), [`db70a48`](https://github.com/mastra-ai/mastra/commit/db70a48aeeeeb8e5f92007e8ede52c364ce15287), [`f0fdc14`](https://github.com/mastra-ai/mastra/commit/f0fdc14ee233d619266b3d2bbdeea7d25cfc6d13), [`db18bc9`](https://github.com/mastra-ai/mastra/commit/db18bc9c3825e2c1a0ad9a183cc9935f6691bfa1), [`9b37b56`](https://github.com/mastra-ai/mastra/commit/9b37b565e1f2a76c24f728945cc740c2b09be9da), [`41a23c3`](https://github.com/mastra-ai/mastra/commit/41a23c32f9877d71810f37e24930515df2ff7a0f), [`5d171ad`](https://github.com/mastra-ai/mastra/commit/5d171ad9ef340387276b77c2bb3e83e83332d729), [`f03ae60`](https://github.com/mastra-ai/mastra/commit/f03ae60500fe350c9d828621006cdafe1975fdd8), [`d1e74a0`](https://github.com/mastra-ai/mastra/commit/d1e74a0a293866dece31022047f5dbab65a304d0), [`39e7869`](https://github.com/mastra-ai/mastra/commit/39e7869bc7d0ee391077ce291474d8a84eedccff), [`5761926`](https://github.com/mastra-ai/mastra/commit/57619260c4a2cdd598763abbacd90de594c6bc76), [`c900fdd`](https://github.com/mastra-ai/mastra/commit/c900fdd504c41348efdffb205cfe80d48c38fa33), [`604a79f`](https://github.com/mastra-ai/mastra/commit/604a79fecf276e26a54a3fe01bb94e65315d2e0e), [`887f0b4`](https://github.com/mastra-ai/mastra/commit/887f0b4746cdbd7cb7d6b17ac9f82aeb58037ea5), [`2562143`](https://github.com/mastra-ai/mastra/commit/256214336b4faa78646c9c1776612393790d8784), [`ef11a61`](https://github.com/mastra-ai/mastra/commit/ef11a61920fa0ed08a5b7ceedd192875af119749)]:
|
|
67
|
+
- @mastra/core@1.0.0-beta.6
|
|
68
|
+
|
|
3
69
|
## 1.0.0-beta.4
|
|
4
70
|
|
|
5
71
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -93,6 +93,64 @@ export const mastra = new Mastra({
|
|
|
93
93
|
});
|
|
94
94
|
```
|
|
95
95
|
|
|
96
|
+
## Framework-agnostic handlers
|
|
97
|
+
|
|
98
|
+
For use outside of the Mastra server (e.g., Next.js App Router, Express), you can use the standalone handler functions directly. These handlers return a `ReadableStream` that you can wrap with `createUIMessageStreamResponse`:
|
|
99
|
+
|
|
100
|
+
### handleChatStream
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
import { handleChatStream } from '@mastra/ai-sdk';
|
|
104
|
+
import { createUIMessageStreamResponse } from 'ai';
|
|
105
|
+
import { mastra } from '@/src/mastra';
|
|
106
|
+
|
|
107
|
+
export async function POST(req: Request) {
|
|
108
|
+
const params = await req.json();
|
|
109
|
+
const stream = await handleChatStream({
|
|
110
|
+
mastra,
|
|
111
|
+
agentId: 'weatherAgent',
|
|
112
|
+
params,
|
|
113
|
+
});
|
|
114
|
+
return createUIMessageStreamResponse({ stream });
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### handleWorkflowStream
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
import { handleWorkflowStream } from '@mastra/ai-sdk';
|
|
122
|
+
import { createUIMessageStreamResponse } from 'ai';
|
|
123
|
+
import { mastra } from '@/src/mastra';
|
|
124
|
+
|
|
125
|
+
export async function POST(req: Request) {
|
|
126
|
+
const params = await req.json();
|
|
127
|
+
const stream = await handleWorkflowStream({
|
|
128
|
+
mastra,
|
|
129
|
+
workflowId: 'myWorkflow',
|
|
130
|
+
params,
|
|
131
|
+
});
|
|
132
|
+
return createUIMessageStreamResponse({ stream });
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### handleNetworkStream
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
import { handleNetworkStream } from '@mastra/ai-sdk';
|
|
140
|
+
import { createUIMessageStreamResponse } from 'ai';
|
|
141
|
+
import { mastra } from '@/src/mastra';
|
|
142
|
+
|
|
143
|
+
export async function POST(req: Request) {
|
|
144
|
+
const params = await req.json();
|
|
145
|
+
const stream = await handleNetworkStream({
|
|
146
|
+
mastra,
|
|
147
|
+
agentId: 'routingAgent',
|
|
148
|
+
params,
|
|
149
|
+
});
|
|
150
|
+
return createUIMessageStreamResponse({ stream });
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
96
154
|
## Manual transformation
|
|
97
155
|
|
|
98
156
|
If you have a raw Mastra `stream`, you can manually transform it to AI SDK UI message parts:
|
|
@@ -106,7 +164,9 @@ export async function POST(req: Request) {
|
|
|
106
164
|
const agent = mastra.getAgent('weatherAgent');
|
|
107
165
|
const stream = await agent.stream(messages);
|
|
108
166
|
|
|
167
|
+
// deduplicate messages https://ai-sdk.dev/docs/troubleshooting/repeated-assistant-messages
|
|
109
168
|
const uiMessageStream = createUIMessageStream({
|
|
169
|
+
originalMessages: messages,
|
|
110
170
|
execute: async ({ writer }) => {
|
|
111
171
|
for await (const part of toAISdkFormat(stream, { from: 'agent' })!) {
|
|
112
172
|
writer.write(part);
|
package/dist/chat-route.d.ts
CHANGED
|
@@ -1,8 +1,40 @@
|
|
|
1
1
|
import type { AgentExecutionOptions } from '@mastra/core/agent';
|
|
2
|
+
import type { Mastra } from '@mastra/core/mastra';
|
|
2
3
|
import { registerApiRoute } from '@mastra/core/server';
|
|
3
4
|
import type { OutputSchema } from '@mastra/core/stream';
|
|
5
|
+
import type { InferUIMessageChunk, UIMessage } from 'ai';
|
|
6
|
+
export type ChatStreamHandlerParams<UI_MESSAGE extends UIMessage, OUTPUT extends OutputSchema = undefined> = AgentExecutionOptions<OUTPUT, 'mastra'> & {
|
|
7
|
+
messages: UI_MESSAGE[];
|
|
8
|
+
resumeData?: Record<string, any>;
|
|
9
|
+
};
|
|
10
|
+
export type ChatStreamHandlerOptions<UI_MESSAGE extends UIMessage, OUTPUT extends OutputSchema = undefined> = {
|
|
11
|
+
mastra: Mastra;
|
|
12
|
+
agentId: string;
|
|
13
|
+
params: ChatStreamHandlerParams<UI_MESSAGE, OUTPUT>;
|
|
14
|
+
defaultOptions?: AgentExecutionOptions<OUTPUT, 'mastra'>;
|
|
15
|
+
sendStart?: boolean;
|
|
16
|
+
sendFinish?: boolean;
|
|
17
|
+
sendReasoning?: boolean;
|
|
18
|
+
sendSources?: boolean;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Framework-agnostic handler for streaming agent chat in AI SDK format.
|
|
22
|
+
* Use this function directly when you need to handle chat streaming outside of Hono/registerApiRoute.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* // Next.js App Router
|
|
26
|
+
* export async function POST(req: Request) {
|
|
27
|
+
* const params = await req.json();
|
|
28
|
+
* return handleChatStream({
|
|
29
|
+
* mastra,
|
|
30
|
+
* agentId: 'my-agent',
|
|
31
|
+
* params,
|
|
32
|
+
* });
|
|
33
|
+
* }
|
|
34
|
+
*/
|
|
35
|
+
export declare function handleChatStream<UI_MESSAGE extends UIMessage, OUTPUT extends OutputSchema = undefined>({ mastra, agentId, params, defaultOptions, sendStart, sendFinish, sendReasoning, sendSources, }: ChatStreamHandlerOptions<UI_MESSAGE, OUTPUT>): Promise<ReadableStream<InferUIMessageChunk<UI_MESSAGE>>>;
|
|
4
36
|
export type chatRouteOptions<OUTPUT extends OutputSchema = undefined> = {
|
|
5
|
-
defaultOptions?: AgentExecutionOptions<OUTPUT, '
|
|
37
|
+
defaultOptions?: AgentExecutionOptions<OUTPUT, 'mastra'>;
|
|
6
38
|
} & ({
|
|
7
39
|
path: `${string}:agentId${string}`;
|
|
8
40
|
agent?: never;
|
package/dist/chat-route.d.ts.map
CHANGED
|
@@ -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;
|
|
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,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAElD,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAExD,OAAO,KAAK,EAAE,mBAAmB,EAAE,SAAS,EAAE,MAAM,IAAI,CAAC;AAGzD,MAAM,MAAM,uBAAuB,CACjC,UAAU,SAAS,SAAS,EAC5B,MAAM,SAAS,YAAY,GAAG,SAAS,IACrC,qBAAqB,CAAC,MAAM,EAAE,QAAQ,CAAC,GAAG;IAC5C,QAAQ,EAAE,UAAU,EAAE,CAAC;IACvB,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAClC,CAAC;AAEF,MAAM,MAAM,wBAAwB,CAAC,UAAU,SAAS,SAAS,EAAE,MAAM,SAAS,YAAY,GAAG,SAAS,IAAI;IAC5G,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,uBAAuB,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;IACpD,cAAc,CAAC,EAAE,qBAAqB,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IACzD,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;AAEF;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,gBAAgB,CAAC,UAAU,SAAS,SAAS,EAAE,MAAM,SAAS,YAAY,GAAG,SAAS,EAAE,EAC5G,MAAM,EACN,OAAO,EACP,MAAM,EACN,cAAc,EACd,SAAgB,EAChB,UAAiB,EACjB,aAAqB,EACrB,WAAmB,GACpB,EAAE,wBAAwB,CAAC,UAAU,EAAE,MAAM,CAAC,GAAG,OAAO,CAAC,cAAc,CAAC,mBAAmB,CAAC,UAAU,CAAC,CAAC,CAAC,CAkDzG;AAED,MAAM,MAAM,gBAAgB,CAAC,MAAM,SAAS,YAAY,GAAG,SAAS,IAAI;IACtE,cAAc,CAAC,EAAE,qBAAqB,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;CAC1D,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,CA0JhE"}
|
|
@@ -60,9 +60,11 @@ import type { ZodObject, ZodType } from 'zod';
|
|
|
60
60
|
*/
|
|
61
61
|
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: {
|
|
62
62
|
from: 'workflow';
|
|
63
|
+
includeTextStreamParts?: boolean;
|
|
63
64
|
}): ReadableStream<InferUIMessageChunk<UIMessage>>;
|
|
64
65
|
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: WorkflowRunOutput<WorkflowResult<TState, TInput, TOutput, TSteps>>, options: {
|
|
65
66
|
from: 'workflow';
|
|
67
|
+
includeTextStreamParts?: boolean;
|
|
66
68
|
}): ReadableStream<InferUIMessageChunk<UIMessage>>;
|
|
67
69
|
export declare function toAISdkV5Stream(stream: MastraAgentNetworkStream, options: {
|
|
68
70
|
from: 'network';
|
|
@@ -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,CAAC;AACzF,OAAO,KAAK,EAAE,mBAAmB,EAAE,SAAS,EAAE,sBAAsB,EAAE,MAAM,IAAI,CAAC;AACjF,OAAO,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC;AAS9C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AACH,wBAAgB,eAAe,CAC7B,OAAO,SAAS,OAAO,CAAC,GAAG,CAAC,EAC5B,MAAM,SAAS,OAAO,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,oBAAoB,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,EAC7D,OAAO,EAAE;IAAE,IAAI,EAAE,UAAU,CAAA;CAAE,
|
|
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,CAAC;AACzF,OAAO,KAAK,EAAE,mBAAmB,EAAE,SAAS,EAAE,sBAAsB,EAAE,MAAM,IAAI,CAAC;AACjF,OAAO,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC;AAS9C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AACH,wBAAgB,eAAe,CAC7B,OAAO,SAAS,OAAO,CAAC,GAAG,CAAC,EAC5B,MAAM,SAAS,OAAO,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,oBAAoB,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,EAC7D,OAAO,EAAE;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,sBAAsB,CAAC,EAAE,OAAO,CAAA;CAAE,GAC9D,cAAc,CAAC,mBAAmB,CAAC,SAAS,CAAC,CAAC,CAAC;AAClD,wBAAgB,eAAe,CAC7B,OAAO,SAAS,OAAO,CAAC,GAAG,CAAC,EAC5B,MAAM,SAAS,OAAO,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,CAAC;IAAC,sBAAsB,CAAC,EAAE,OAAO,CAAA;CAAE,GAC9D,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;IACtB,eAAe,CAAC,EAAE,sBAAsB,CAAC,SAAS,CAAC,CAAC,iBAAiB,CAAC,CAAC;IACvE,OAAO,CAAC,EAAE,sBAAsB,CAAC,SAAS,CAAC,CAAC,SAAS,CAAC,CAAC;CACxD,GACA,cAAc,CAAC,mBAAmB,CAAC,SAAS,CAAC,CAAC,CAAC"}
|
package/dist/index.cjs
CHANGED
|
@@ -10,6 +10,31 @@ var stream = require('@mastra/core/stream');
|
|
|
10
10
|
var isDataChunkType = (chunk) => {
|
|
11
11
|
return chunk && typeof chunk === "object" && "type" in chunk && chunk.type?.startsWith("data-");
|
|
12
12
|
};
|
|
13
|
+
var isMastraTextStreamChunk = (chunk) => {
|
|
14
|
+
return chunk && typeof chunk === "object" && "type" in chunk && typeof chunk.type === "string" && [
|
|
15
|
+
"text-start",
|
|
16
|
+
"text-delta",
|
|
17
|
+
"text-end",
|
|
18
|
+
"reasoning-start",
|
|
19
|
+
"reasoning-delta",
|
|
20
|
+
"reasoning-end",
|
|
21
|
+
"file",
|
|
22
|
+
"source",
|
|
23
|
+
"tool-input-start",
|
|
24
|
+
"tool-input-delta",
|
|
25
|
+
"tool-call",
|
|
26
|
+
"tool-result",
|
|
27
|
+
"tool-error",
|
|
28
|
+
"error",
|
|
29
|
+
"start-step",
|
|
30
|
+
"finish-step",
|
|
31
|
+
"start",
|
|
32
|
+
"finish",
|
|
33
|
+
"abort",
|
|
34
|
+
"tool-input-end",
|
|
35
|
+
"raw"
|
|
36
|
+
].includes(chunk.type);
|
|
37
|
+
};
|
|
13
38
|
function safeParseErrorObject(obj) {
|
|
14
39
|
if (typeof obj !== "object" || obj === null) {
|
|
15
40
|
return String(obj);
|
|
@@ -492,7 +517,9 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
492
517
|
|
|
493
518
|
// src/transformers.ts
|
|
494
519
|
var PRIMITIVE_CACHE_SYMBOL = Symbol("primitive-cache");
|
|
495
|
-
function WorkflowStreamToAISDKTransformer(
|
|
520
|
+
function WorkflowStreamToAISDKTransformer({
|
|
521
|
+
includeTextStreamParts
|
|
522
|
+
} = {}) {
|
|
496
523
|
const bufferedWorkflows = /* @__PURE__ */ new Map();
|
|
497
524
|
return new TransformStream({
|
|
498
525
|
start(controller) {
|
|
@@ -506,7 +533,7 @@ function WorkflowStreamToAISDKTransformer() {
|
|
|
506
533
|
});
|
|
507
534
|
},
|
|
508
535
|
transform(chunk, controller) {
|
|
509
|
-
const transformed = transformWorkflow(chunk, bufferedWorkflows);
|
|
536
|
+
const transformed = transformWorkflow(chunk, bufferedWorkflows, false, includeTextStreamParts);
|
|
510
537
|
if (transformed) controller.enqueue(transformed);
|
|
511
538
|
}
|
|
512
539
|
});
|
|
@@ -727,7 +754,7 @@ function transformAgent(payload, bufferedSteps) {
|
|
|
727
754
|
}
|
|
728
755
|
return null;
|
|
729
756
|
}
|
|
730
|
-
function transformWorkflow(payload, bufferedWorkflows, isNested) {
|
|
757
|
+
function transformWorkflow(payload, bufferedWorkflows, isNested, includeTextStreamParts) {
|
|
731
758
|
switch (payload.type) {
|
|
732
759
|
case "workflow-start":
|
|
733
760
|
bufferedWorkflows.set(payload.runId, {
|
|
@@ -822,6 +849,16 @@ function transformWorkflow(payload, bufferedWorkflows, isNested) {
|
|
|
822
849
|
}
|
|
823
850
|
case "workflow-step-output": {
|
|
824
851
|
const output = payload.payload.output;
|
|
852
|
+
if (includeTextStreamParts && output && isMastraTextStreamChunk(output)) {
|
|
853
|
+
const part = convertMastraChunkToAISDKv5({ chunk: output, mode: "stream" });
|
|
854
|
+
const transformedChunk = convertFullStreamChunkToUIMessageStream({
|
|
855
|
+
part,
|
|
856
|
+
onError(error) {
|
|
857
|
+
return safeParseErrorObject(error);
|
|
858
|
+
}
|
|
859
|
+
});
|
|
860
|
+
return transformedChunk;
|
|
861
|
+
}
|
|
825
862
|
if (output && isDataChunkType(output)) {
|
|
826
863
|
if (!("data" in output)) {
|
|
827
864
|
throw new Error(
|
|
@@ -1122,7 +1159,7 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
1122
1159
|
}
|
|
1123
1160
|
step[PRIMITIVE_CACHE_SYMBOL] = step[PRIMITIVE_CACHE_SYMBOL] || /* @__PURE__ */ new Map();
|
|
1124
1161
|
const result = transformWorkflow(payload.payload, step[PRIMITIVE_CACHE_SYMBOL]);
|
|
1125
|
-
if (result) {
|
|
1162
|
+
if (result && "data" in result) {
|
|
1126
1163
|
const data = result.data;
|
|
1127
1164
|
step.task = data;
|
|
1128
1165
|
if (data.name && step.task) {
|
|
@@ -1173,7 +1210,10 @@ function toAISdkV5Stream(stream, options = {
|
|
|
1173
1210
|
}) {
|
|
1174
1211
|
const from = options?.from;
|
|
1175
1212
|
if (from === "workflow") {
|
|
1176
|
-
|
|
1213
|
+
const includeTextStreamParts = options?.includeTextStreamParts ?? true;
|
|
1214
|
+
return stream.pipeThrough(
|
|
1215
|
+
WorkflowStreamToAISDKTransformer({ includeTextStreamParts })
|
|
1216
|
+
);
|
|
1177
1217
|
}
|
|
1178
1218
|
if (from === "network") {
|
|
1179
1219
|
return stream.pipeThrough(AgentNetworkToAISDKTransformer());
|
|
@@ -1193,6 +1233,57 @@ function toAISdkV5Stream(stream, options = {
|
|
|
1193
1233
|
}
|
|
1194
1234
|
|
|
1195
1235
|
// src/chat-route.ts
|
|
1236
|
+
async function handleChatStream({
|
|
1237
|
+
mastra,
|
|
1238
|
+
agentId,
|
|
1239
|
+
params,
|
|
1240
|
+
defaultOptions,
|
|
1241
|
+
sendStart = true,
|
|
1242
|
+
sendFinish = true,
|
|
1243
|
+
sendReasoning = false,
|
|
1244
|
+
sendSources = false
|
|
1245
|
+
}) {
|
|
1246
|
+
const { messages, resumeData, runId, requestContext, ...rest } = params;
|
|
1247
|
+
if (resumeData && !runId) {
|
|
1248
|
+
throw new Error("runId is required when resumeData is provided");
|
|
1249
|
+
}
|
|
1250
|
+
const agentObj = mastra.getAgentById(agentId);
|
|
1251
|
+
if (!agentObj) {
|
|
1252
|
+
throw new Error(`Agent ${agentId} not found`);
|
|
1253
|
+
}
|
|
1254
|
+
if (!Array.isArray(messages)) {
|
|
1255
|
+
throw new Error("Messages must be an array of UIMessage objects");
|
|
1256
|
+
}
|
|
1257
|
+
const mergedOptions = {
|
|
1258
|
+
...defaultOptions,
|
|
1259
|
+
...rest,
|
|
1260
|
+
...runId && { runId },
|
|
1261
|
+
requestContext: requestContext || defaultOptions?.requestContext
|
|
1262
|
+
};
|
|
1263
|
+
const result = resumeData ? await agentObj.resumeStream(resumeData, mergedOptions) : await agentObj.stream(messages, mergedOptions);
|
|
1264
|
+
let lastMessageId;
|
|
1265
|
+
if (messages.length) {
|
|
1266
|
+
const lastMessage = messages[messages.length - 1];
|
|
1267
|
+
if (lastMessage?.role === "assistant") {
|
|
1268
|
+
lastMessageId = lastMessage.id;
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1271
|
+
return ai.createUIMessageStream({
|
|
1272
|
+
originalMessages: messages,
|
|
1273
|
+
execute: async ({ writer }) => {
|
|
1274
|
+
for await (const part of toAISdkV5Stream(result, {
|
|
1275
|
+
from: "agent",
|
|
1276
|
+
lastMessageId,
|
|
1277
|
+
sendStart,
|
|
1278
|
+
sendFinish,
|
|
1279
|
+
sendReasoning,
|
|
1280
|
+
sendSources
|
|
1281
|
+
})) {
|
|
1282
|
+
writer.write(part);
|
|
1283
|
+
}
|
|
1284
|
+
}
|
|
1285
|
+
});
|
|
1286
|
+
}
|
|
1196
1287
|
function chatRoute({
|
|
1197
1288
|
path = "/chat/:agentId",
|
|
1198
1289
|
agent,
|
|
@@ -1229,6 +1320,14 @@ function chatRoute({
|
|
|
1229
1320
|
schema: {
|
|
1230
1321
|
type: "object",
|
|
1231
1322
|
properties: {
|
|
1323
|
+
resumeData: {
|
|
1324
|
+
type: "object",
|
|
1325
|
+
description: "Resume data for the agent"
|
|
1326
|
+
},
|
|
1327
|
+
runId: {
|
|
1328
|
+
type: "string",
|
|
1329
|
+
description: "The run ID required when resuming an agent execution"
|
|
1330
|
+
},
|
|
1232
1331
|
messages: {
|
|
1233
1332
|
type: "array",
|
|
1234
1333
|
description: "Array of messages in the conversation",
|
|
@@ -1299,9 +1398,9 @@ function chatRoute({
|
|
|
1299
1398
|
}
|
|
1300
1399
|
},
|
|
1301
1400
|
handler: async (c) => {
|
|
1302
|
-
const
|
|
1401
|
+
const params = await c.req.json();
|
|
1303
1402
|
const mastra = c.get("mastra");
|
|
1304
|
-
const
|
|
1403
|
+
const contextRequestContext = c.get("requestContext");
|
|
1305
1404
|
let agentToUse = agent;
|
|
1306
1405
|
if (!agent) {
|
|
1307
1406
|
const agentId = c.req.param("agentId");
|
|
@@ -1312,39 +1411,24 @@ function chatRoute({
|
|
|
1312
1411
|
`Fixed agent ID was set together with an agentId path parameter. This can lead to unexpected behavior.`
|
|
1313
1412
|
);
|
|
1314
1413
|
}
|
|
1315
|
-
if (
|
|
1414
|
+
if (contextRequestContext && defaultOptions?.requestContext) {
|
|
1316
1415
|
mastra.getLogger()?.warn(`"requestContext" set in the route options will be overridden by the request's "requestContext".`);
|
|
1317
1416
|
}
|
|
1318
1417
|
if (!agentToUse) {
|
|
1319
1418
|
throw new Error("Agent ID is required");
|
|
1320
1419
|
}
|
|
1321
|
-
const
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
}
|
|
1334
|
-
const uiMessageStream = ai.createUIMessageStream({
|
|
1335
|
-
originalMessages: messages,
|
|
1336
|
-
execute: async ({ writer }) => {
|
|
1337
|
-
for await (const part of toAISdkV5Stream(result, {
|
|
1338
|
-
from: "agent",
|
|
1339
|
-
lastMessageId,
|
|
1340
|
-
sendStart,
|
|
1341
|
-
sendFinish,
|
|
1342
|
-
sendReasoning,
|
|
1343
|
-
sendSources
|
|
1344
|
-
})) {
|
|
1345
|
-
writer.write(part);
|
|
1346
|
-
}
|
|
1347
|
-
}
|
|
1420
|
+
const uiMessageStream = await handleChatStream({
|
|
1421
|
+
mastra,
|
|
1422
|
+
agentId: agentToUse,
|
|
1423
|
+
params: {
|
|
1424
|
+
...params,
|
|
1425
|
+
requestContext: contextRequestContext || params.requestContext
|
|
1426
|
+
},
|
|
1427
|
+
defaultOptions,
|
|
1428
|
+
sendStart,
|
|
1429
|
+
sendFinish,
|
|
1430
|
+
sendReasoning,
|
|
1431
|
+
sendSources
|
|
1348
1432
|
});
|
|
1349
1433
|
return ai.createUIMessageStreamResponse({
|
|
1350
1434
|
stream: uiMessageStream
|
|
@@ -1352,9 +1436,31 @@ function chatRoute({
|
|
|
1352
1436
|
}
|
|
1353
1437
|
});
|
|
1354
1438
|
}
|
|
1439
|
+
async function handleWorkflowStream({
|
|
1440
|
+
mastra,
|
|
1441
|
+
workflowId,
|
|
1442
|
+
params,
|
|
1443
|
+
includeTextStreamParts = true
|
|
1444
|
+
}) {
|
|
1445
|
+
const { runId, resourceId, inputData, resumeData, requestContext, ...rest } = params;
|
|
1446
|
+
const workflowObj = mastra.getWorkflowById(workflowId);
|
|
1447
|
+
if (!workflowObj) {
|
|
1448
|
+
throw new Error(`Workflow ${workflowId} not found`);
|
|
1449
|
+
}
|
|
1450
|
+
const run = await workflowObj.createRun({ runId, resourceId, ...rest });
|
|
1451
|
+
const stream = resumeData ? run.resumeStream({ resumeData, ...rest, requestContext }) : run.stream({ inputData, ...rest, requestContext });
|
|
1452
|
+
return ai.createUIMessageStream({
|
|
1453
|
+
execute: async ({ writer }) => {
|
|
1454
|
+
for await (const part of toAISdkV5Stream(stream, { from: "workflow", includeTextStreamParts })) {
|
|
1455
|
+
writer.write(part);
|
|
1456
|
+
}
|
|
1457
|
+
}
|
|
1458
|
+
});
|
|
1459
|
+
}
|
|
1355
1460
|
function workflowRoute({
|
|
1356
1461
|
path = "/api/workflows/:workflowId/stream",
|
|
1357
|
-
workflow
|
|
1462
|
+
workflow,
|
|
1463
|
+
includeTextStreamParts = true
|
|
1358
1464
|
}) {
|
|
1359
1465
|
if (!workflow && !path.includes("/:workflowId")) {
|
|
1360
1466
|
throw new Error("Path must include :workflowId to route to the correct workflow or pass the workflow explicitly");
|
|
@@ -1405,9 +1511,9 @@ function workflowRoute({
|
|
|
1405
1511
|
}
|
|
1406
1512
|
},
|
|
1407
1513
|
handler: async (c) => {
|
|
1408
|
-
const
|
|
1514
|
+
const params = await c.req.json();
|
|
1409
1515
|
const mastra = c.get("mastra");
|
|
1410
|
-
const
|
|
1516
|
+
const contextRequestContext = c.get("requestContext");
|
|
1411
1517
|
let workflowToUse = workflow;
|
|
1412
1518
|
if (!workflow) {
|
|
1413
1519
|
const workflowId = c.req.param("workflowId");
|
|
@@ -1421,28 +1527,47 @@ function workflowRoute({
|
|
|
1421
1527
|
if (!workflowToUse) {
|
|
1422
1528
|
throw new Error("Workflow ID is required");
|
|
1423
1529
|
}
|
|
1424
|
-
|
|
1425
|
-
if (!workflowObj) {
|
|
1426
|
-
throw new Error(`Workflow ${workflowToUse} not found`);
|
|
1427
|
-
}
|
|
1428
|
-
if (requestContext && rest.requestContext) {
|
|
1530
|
+
if (contextRequestContext && params.requestContext) {
|
|
1429
1531
|
mastra.getLogger()?.warn(
|
|
1430
1532
|
`"requestContext" from the request body will be ignored because "requestContext" is already set in the route options.`
|
|
1431
1533
|
);
|
|
1432
1534
|
}
|
|
1433
|
-
const
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1535
|
+
const uiMessageStream = await handleWorkflowStream({
|
|
1536
|
+
mastra,
|
|
1537
|
+
workflowId: workflowToUse,
|
|
1538
|
+
params: {
|
|
1539
|
+
...params,
|
|
1540
|
+
requestContext: contextRequestContext || params.requestContext
|
|
1541
|
+
},
|
|
1542
|
+
includeTextStreamParts
|
|
1441
1543
|
});
|
|
1442
1544
|
return ai.createUIMessageStreamResponse({ stream: uiMessageStream });
|
|
1443
1545
|
}
|
|
1444
1546
|
});
|
|
1445
1547
|
}
|
|
1548
|
+
async function handleNetworkStream({
|
|
1549
|
+
mastra,
|
|
1550
|
+
agentId,
|
|
1551
|
+
params,
|
|
1552
|
+
defaultOptions
|
|
1553
|
+
}) {
|
|
1554
|
+
const { messages, ...rest } = params;
|
|
1555
|
+
const agentObj = mastra.getAgentById(agentId);
|
|
1556
|
+
if (!agentObj) {
|
|
1557
|
+
throw new Error(`Agent ${agentId} not found`);
|
|
1558
|
+
}
|
|
1559
|
+
const result = await agentObj.network(messages, {
|
|
1560
|
+
...defaultOptions,
|
|
1561
|
+
...rest
|
|
1562
|
+
});
|
|
1563
|
+
return ai.createUIMessageStream({
|
|
1564
|
+
execute: async ({ writer }) => {
|
|
1565
|
+
for await (const part of toAISdkV5Stream(result, { from: "network" })) {
|
|
1566
|
+
writer.write(part);
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
});
|
|
1570
|
+
}
|
|
1446
1571
|
function networkRoute({
|
|
1447
1572
|
path = "/network/:agentId",
|
|
1448
1573
|
agent,
|
|
@@ -1503,7 +1628,7 @@ function networkRoute({
|
|
|
1503
1628
|
}
|
|
1504
1629
|
},
|
|
1505
1630
|
handler: async (c) => {
|
|
1506
|
-
const
|
|
1631
|
+
const params = await c.req.json();
|
|
1507
1632
|
const mastra = c.get("mastra");
|
|
1508
1633
|
let agentToUse = agent;
|
|
1509
1634
|
if (!agent) {
|
|
@@ -1518,20 +1643,11 @@ function networkRoute({
|
|
|
1518
1643
|
if (!agentToUse) {
|
|
1519
1644
|
throw new Error("Agent ID is required");
|
|
1520
1645
|
}
|
|
1521
|
-
const
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
...defaultOptions,
|
|
1527
|
-
...rest
|
|
1528
|
-
});
|
|
1529
|
-
const uiMessageStream = ai.createUIMessageStream({
|
|
1530
|
-
execute: async ({ writer }) => {
|
|
1531
|
-
for await (const part of toAISdkV5Stream(result, { from: "network" })) {
|
|
1532
|
-
writer.write(part);
|
|
1533
|
-
}
|
|
1534
|
-
}
|
|
1646
|
+
const uiMessageStream = await handleNetworkStream({
|
|
1647
|
+
mastra,
|
|
1648
|
+
agentId: agentToUse,
|
|
1649
|
+
params,
|
|
1650
|
+
defaultOptions
|
|
1535
1651
|
});
|
|
1536
1652
|
return ai.createUIMessageStreamResponse({ stream: uiMessageStream });
|
|
1537
1653
|
}
|
|
@@ -1546,6 +1662,9 @@ function toAISdkFormat() {
|
|
|
1546
1662
|
}
|
|
1547
1663
|
|
|
1548
1664
|
exports.chatRoute = chatRoute;
|
|
1665
|
+
exports.handleChatStream = handleChatStream;
|
|
1666
|
+
exports.handleNetworkStream = handleNetworkStream;
|
|
1667
|
+
exports.handleWorkflowStream = handleWorkflowStream;
|
|
1549
1668
|
exports.networkRoute = networkRoute;
|
|
1550
1669
|
exports.toAISdkFormat = toAISdkFormat;
|
|
1551
1670
|
exports.toAISdkStream = toAISdkV5Stream;
|