@mastra/ai-sdk 1.0.0-beta.6 → 1.0.0-beta.8
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 +64 -0
- package/dist/helpers.d.ts +8 -2
- package/dist/helpers.d.ts.map +1 -1
- package/dist/index.cjs +507 -23
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +509 -26
- package/dist/index.js.map +1 -1
- package/dist/middleware.d.ts +157 -0
- package/dist/middleware.d.ts.map +1 -0
- package/dist/transformers.d.ts.map +1 -1
- package/dist/utils.d.ts.map +1 -1
- package/package.json +4 -2
package/dist/index.d.ts
CHANGED
|
@@ -8,5 +8,7 @@ export type { NetworkRouteOptions, NetworkStreamHandlerParams, NetworkStreamHand
|
|
|
8
8
|
export type { NetworkDataPart } from './transformers.js';
|
|
9
9
|
export type { AgentDataPart } from './transformers.js';
|
|
10
10
|
export { toAISdkV5Stream as toAISdkStream } from './convert-streams.js';
|
|
11
|
+
export { withMastra } from './middleware.js';
|
|
12
|
+
export type { WithMastraOptions, WithMastraMemoryOptions, WithMastraSemanticRecallOptions } from './middleware.js';
|
|
11
13
|
export { toAISdkFormat } from './to-ai-sdk-format.js';
|
|
12
14
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAC3D,YAAY,EAAE,gBAAgB,EAAE,uBAAuB,EAAE,wBAAwB,EAAE,MAAM,cAAc,CAAC;AACxG,OAAO,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AACvE,YAAY,EAAE,oBAAoB,EAAE,2BAA2B,EAAE,4BAA4B,EAAE,MAAM,kBAAkB,CAAC;AACxH,YAAY,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AACvD,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AACpE,YAAY,EAAE,mBAAmB,EAAE,0BAA0B,EAAE,2BAA2B,EAAE,MAAM,iBAAiB,CAAC;AACpH,YAAY,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACtD,YAAY,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAEpD,OAAO,EAAE,eAAe,IAAI,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAGrE,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAC3D,YAAY,EAAE,gBAAgB,EAAE,uBAAuB,EAAE,wBAAwB,EAAE,MAAM,cAAc,CAAC;AACxG,OAAO,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AACvE,YAAY,EAAE,oBAAoB,EAAE,2BAA2B,EAAE,4BAA4B,EAAE,MAAM,kBAAkB,CAAC;AACxH,YAAY,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AACvD,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AACpE,YAAY,EAAE,mBAAmB,EAAE,0BAA0B,EAAE,2BAA2B,EAAE,MAAM,iBAAiB,CAAC;AACpH,YAAY,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACtD,YAAY,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAEpD,OAAO,EAAE,eAAe,IAAI,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAGrE,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,YAAY,EAAE,iBAAiB,EAAE,uBAAuB,EAAE,+BAA+B,EAAE,MAAM,cAAc,CAAC;AAGhH,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { registerApiRoute } from '@mastra/core/server';
|
|
2
|
-
import { createUIMessageStream, createUIMessageStreamResponse } from 'ai';
|
|
3
|
-
import { DefaultGeneratedFile, DefaultGeneratedFileWithType } from '@mastra/core/stream';
|
|
2
|
+
import { createUIMessageStream, createUIMessageStreamResponse, wrapLanguageModel } from 'ai';
|
|
3
|
+
import { convertFullStreamChunkToMastra, DefaultGeneratedFile, DefaultGeneratedFileWithType } from '@mastra/core/stream';
|
|
4
|
+
import { TripWire, MessageList } from '@mastra/core/agent';
|
|
5
|
+
import { RequestContext } from '@mastra/core/di';
|
|
6
|
+
import { WorkingMemory, MessageHistory, SemanticRecall } from '@mastra/core/processors';
|
|
4
7
|
|
|
5
8
|
// src/chat-route.ts
|
|
6
9
|
|
|
@@ -20,6 +23,8 @@ var isMastraTextStreamChunk = (chunk) => {
|
|
|
20
23
|
"source",
|
|
21
24
|
"tool-input-start",
|
|
22
25
|
"tool-input-delta",
|
|
26
|
+
"tool-call-approval",
|
|
27
|
+
"tool-call-suspended",
|
|
23
28
|
"tool-call",
|
|
24
29
|
"tool-result",
|
|
25
30
|
"tool-error",
|
|
@@ -30,6 +35,8 @@ var isMastraTextStreamChunk = (chunk) => {
|
|
|
30
35
|
"finish",
|
|
31
36
|
"abort",
|
|
32
37
|
"tool-input-end",
|
|
38
|
+
"object",
|
|
39
|
+
"tripwire",
|
|
33
40
|
"raw"
|
|
34
41
|
].includes(chunk.type);
|
|
35
42
|
};
|
|
@@ -55,6 +62,12 @@ var isWorkflowExecutionDataChunkType = (chunk) => {
|
|
|
55
62
|
};
|
|
56
63
|
|
|
57
64
|
// src/helpers.ts
|
|
65
|
+
function toAISDKFinishReason(reason) {
|
|
66
|
+
if (reason === "tripwire" || reason === "retry") {
|
|
67
|
+
return "other";
|
|
68
|
+
}
|
|
69
|
+
return reason;
|
|
70
|
+
}
|
|
58
71
|
function convertMastraChunkToAISDKv5({
|
|
59
72
|
chunk,
|
|
60
73
|
mode = "stream"
|
|
@@ -79,7 +92,7 @@ function convertMastraChunkToAISDKv5({
|
|
|
79
92
|
case "finish": {
|
|
80
93
|
return {
|
|
81
94
|
type: "finish",
|
|
82
|
-
finishReason: chunk.payload.stepResult.reason,
|
|
95
|
+
finishReason: toAISDKFinishReason(chunk.payload.stepResult.reason),
|
|
83
96
|
totalUsage: chunk.payload.output.usage
|
|
84
97
|
};
|
|
85
98
|
}
|
|
@@ -218,7 +231,7 @@ function convertMastraChunkToAISDKv5({
|
|
|
218
231
|
...rest2
|
|
219
232
|
},
|
|
220
233
|
usage: chunk.payload.output.usage,
|
|
221
|
-
finishReason: chunk.payload.stepResult.reason,
|
|
234
|
+
finishReason: toAISDKFinishReason(chunk.payload.stepResult.reason),
|
|
222
235
|
providerMetadata
|
|
223
236
|
};
|
|
224
237
|
}
|
|
@@ -279,7 +292,10 @@ function convertMastraChunkToAISDKv5({
|
|
|
279
292
|
return {
|
|
280
293
|
type: "data-tripwire",
|
|
281
294
|
data: {
|
|
282
|
-
|
|
295
|
+
reason: chunk.payload.reason,
|
|
296
|
+
retry: chunk.payload.retry,
|
|
297
|
+
metadata: chunk.payload.metadata,
|
|
298
|
+
processorId: chunk.payload.processorId
|
|
283
299
|
}
|
|
284
300
|
};
|
|
285
301
|
default:
|
|
@@ -1132,6 +1148,26 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
1132
1148
|
};
|
|
1133
1149
|
}
|
|
1134
1150
|
default: {
|
|
1151
|
+
if (isAgentExecutionDataChunkType(payload)) {
|
|
1152
|
+
if (!("data" in payload.payload)) {
|
|
1153
|
+
throw new Error(
|
|
1154
|
+
`UI Messages require a data property when using data- prefixed chunks
|
|
1155
|
+
${JSON.stringify(payload)}`
|
|
1156
|
+
);
|
|
1157
|
+
}
|
|
1158
|
+
const { type, data } = payload.payload;
|
|
1159
|
+
return { type, data };
|
|
1160
|
+
}
|
|
1161
|
+
if (isWorkflowExecutionDataChunkType(payload)) {
|
|
1162
|
+
if (!("data" in payload.payload)) {
|
|
1163
|
+
throw new Error(
|
|
1164
|
+
`UI Messages require a data property when using data- prefixed chunks
|
|
1165
|
+
${JSON.stringify(payload)}`
|
|
1166
|
+
);
|
|
1167
|
+
}
|
|
1168
|
+
const { type, data } = payload.payload;
|
|
1169
|
+
return { type, data };
|
|
1170
|
+
}
|
|
1135
1171
|
if (payload.type.startsWith("agent-execution-event-")) {
|
|
1136
1172
|
const stepId = payload.payload.runId;
|
|
1137
1173
|
const current = bufferedNetworks.get(payload.runId);
|
|
@@ -1146,6 +1182,15 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
1146
1182
|
const { request, response, ...data } = result.data;
|
|
1147
1183
|
step.task = data;
|
|
1148
1184
|
}
|
|
1185
|
+
bufferedNetworks.set(payload.runId, current);
|
|
1186
|
+
return {
|
|
1187
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
1188
|
+
id: payload.runId,
|
|
1189
|
+
data: {
|
|
1190
|
+
...current,
|
|
1191
|
+
status: "running"
|
|
1192
|
+
}
|
|
1193
|
+
};
|
|
1149
1194
|
}
|
|
1150
1195
|
if (payload.type.startsWith("workflow-execution-event-")) {
|
|
1151
1196
|
const stepId = payload.payload.runId;
|
|
@@ -1164,6 +1209,15 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
1164
1209
|
step.task.id = data.name;
|
|
1165
1210
|
}
|
|
1166
1211
|
}
|
|
1212
|
+
bufferedNetworks.set(payload.runId, current);
|
|
1213
|
+
return {
|
|
1214
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
1215
|
+
id: payload.runId,
|
|
1216
|
+
data: {
|
|
1217
|
+
...current,
|
|
1218
|
+
status: "running"
|
|
1219
|
+
}
|
|
1220
|
+
};
|
|
1167
1221
|
}
|
|
1168
1222
|
if (isDataChunkType(payload)) {
|
|
1169
1223
|
if (!("data" in payload)) {
|
|
@@ -1175,26 +1229,6 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
1175
1229
|
const { type, data } = payload;
|
|
1176
1230
|
return { type, data };
|
|
1177
1231
|
}
|
|
1178
|
-
if (isAgentExecutionDataChunkType(payload)) {
|
|
1179
|
-
if (!("data" in payload.payload)) {
|
|
1180
|
-
throw new Error(
|
|
1181
|
-
`UI Messages require a data property when using data- prefixed chunks
|
|
1182
|
-
${JSON.stringify(payload)}`
|
|
1183
|
-
);
|
|
1184
|
-
}
|
|
1185
|
-
const { type, data } = payload.payload;
|
|
1186
|
-
return { type, data };
|
|
1187
|
-
}
|
|
1188
|
-
if (isWorkflowExecutionDataChunkType(payload)) {
|
|
1189
|
-
if (!("data" in payload.payload)) {
|
|
1190
|
-
throw new Error(
|
|
1191
|
-
`UI Messages require a data property when using data- prefixed chunks
|
|
1192
|
-
${JSON.stringify(payload)}`
|
|
1193
|
-
);
|
|
1194
|
-
}
|
|
1195
|
-
const { type, data } = payload.payload;
|
|
1196
|
-
return { type, data };
|
|
1197
|
-
}
|
|
1198
1232
|
return null;
|
|
1199
1233
|
}
|
|
1200
1234
|
}
|
|
@@ -1651,6 +1685,455 @@ function networkRoute({
|
|
|
1651
1685
|
}
|
|
1652
1686
|
});
|
|
1653
1687
|
}
|
|
1688
|
+
function withMastra(model, options = {}) {
|
|
1689
|
+
const { memory, inputProcessors = [], outputProcessors = [] } = options;
|
|
1690
|
+
const allInputProcessors = [...inputProcessors];
|
|
1691
|
+
const allOutputProcessors = [...outputProcessors];
|
|
1692
|
+
if (memory) {
|
|
1693
|
+
const { storage, lastMessages, semanticRecall, workingMemory } = memory;
|
|
1694
|
+
const isWorkingMemoryEnabled = typeof workingMemory === "object" && workingMemory.enabled !== false;
|
|
1695
|
+
if (isWorkingMemoryEnabled && typeof workingMemory === "object") {
|
|
1696
|
+
let template;
|
|
1697
|
+
if (workingMemory.template) {
|
|
1698
|
+
template = {
|
|
1699
|
+
format: "markdown",
|
|
1700
|
+
content: workingMemory.template
|
|
1701
|
+
};
|
|
1702
|
+
}
|
|
1703
|
+
const workingMemoryProcessor = new WorkingMemory({
|
|
1704
|
+
storage,
|
|
1705
|
+
template,
|
|
1706
|
+
scope: workingMemory.scope,
|
|
1707
|
+
useVNext: "version" in workingMemory && workingMemory.version === "vnext"
|
|
1708
|
+
});
|
|
1709
|
+
allInputProcessors.push(workingMemoryProcessor);
|
|
1710
|
+
}
|
|
1711
|
+
if (lastMessages !== false && lastMessages !== void 0) {
|
|
1712
|
+
const messageHistory = new MessageHistory({
|
|
1713
|
+
storage,
|
|
1714
|
+
lastMessages: typeof lastMessages === "number" ? lastMessages : void 0
|
|
1715
|
+
});
|
|
1716
|
+
allInputProcessors.push(messageHistory);
|
|
1717
|
+
allOutputProcessors.push(messageHistory);
|
|
1718
|
+
}
|
|
1719
|
+
if (semanticRecall) {
|
|
1720
|
+
const { vector, embedder, indexName, ...semanticConfig } = semanticRecall;
|
|
1721
|
+
const semanticRecallProcessor = new SemanticRecall({
|
|
1722
|
+
storage,
|
|
1723
|
+
vector,
|
|
1724
|
+
embedder,
|
|
1725
|
+
indexName: indexName || "memory_messages",
|
|
1726
|
+
...semanticConfig
|
|
1727
|
+
});
|
|
1728
|
+
allInputProcessors.push(semanticRecallProcessor);
|
|
1729
|
+
allOutputProcessors.push(semanticRecallProcessor);
|
|
1730
|
+
}
|
|
1731
|
+
}
|
|
1732
|
+
return wrapLanguageModel({
|
|
1733
|
+
model,
|
|
1734
|
+
middleware: createProcessorMiddleware({
|
|
1735
|
+
inputProcessors: allInputProcessors,
|
|
1736
|
+
outputProcessors: allOutputProcessors,
|
|
1737
|
+
memory: memory ? {
|
|
1738
|
+
threadId: memory.threadId,
|
|
1739
|
+
resourceId: memory.resourceId
|
|
1740
|
+
} : void 0
|
|
1741
|
+
})
|
|
1742
|
+
});
|
|
1743
|
+
}
|
|
1744
|
+
function createProcessorMiddleware(options) {
|
|
1745
|
+
const { inputProcessors = [], outputProcessors = [], memory } = options;
|
|
1746
|
+
const requestContext = new RequestContext();
|
|
1747
|
+
if (memory) {
|
|
1748
|
+
requestContext.set("MastraMemory", {
|
|
1749
|
+
thread: memory.threadId ? { id: memory.threadId } : void 0,
|
|
1750
|
+
resourceId: memory.resourceId,
|
|
1751
|
+
memoryConfig: memory.config
|
|
1752
|
+
});
|
|
1753
|
+
}
|
|
1754
|
+
return {
|
|
1755
|
+
middlewareVersion: "v2",
|
|
1756
|
+
/**
|
|
1757
|
+
* Transform params runs input processors (processInput)
|
|
1758
|
+
*/
|
|
1759
|
+
async transformParams({ params }) {
|
|
1760
|
+
const messageList = new MessageList({
|
|
1761
|
+
threadId: memory?.threadId,
|
|
1762
|
+
resourceId: memory?.resourceId
|
|
1763
|
+
});
|
|
1764
|
+
for (const msg of params.prompt) {
|
|
1765
|
+
if (msg.role === "system") {
|
|
1766
|
+
messageList.addSystem(msg.content);
|
|
1767
|
+
} else {
|
|
1768
|
+
messageList.add(msg, "input");
|
|
1769
|
+
}
|
|
1770
|
+
}
|
|
1771
|
+
for (const processor of inputProcessors) {
|
|
1772
|
+
if (processor.processInput) {
|
|
1773
|
+
try {
|
|
1774
|
+
await processor.processInput({
|
|
1775
|
+
messages: messageList.get.input.db(),
|
|
1776
|
+
systemMessages: messageList.getAllSystemMessages(),
|
|
1777
|
+
messageList,
|
|
1778
|
+
requestContext,
|
|
1779
|
+
abort: (reason) => {
|
|
1780
|
+
throw new TripWire(reason || "Aborted by processor");
|
|
1781
|
+
}
|
|
1782
|
+
});
|
|
1783
|
+
} catch (error) {
|
|
1784
|
+
if (error instanceof TripWire) {
|
|
1785
|
+
return {
|
|
1786
|
+
...params,
|
|
1787
|
+
providerOptions: {
|
|
1788
|
+
...params.providerOptions,
|
|
1789
|
+
mastraProcessors: {
|
|
1790
|
+
tripwire: true,
|
|
1791
|
+
reason: error.message
|
|
1792
|
+
}
|
|
1793
|
+
}
|
|
1794
|
+
};
|
|
1795
|
+
}
|
|
1796
|
+
throw error;
|
|
1797
|
+
}
|
|
1798
|
+
}
|
|
1799
|
+
}
|
|
1800
|
+
const newPrompt = messageList.get.all.aiV5.prompt().map(MessageList.aiV5ModelMessageToV2PromptMessage);
|
|
1801
|
+
return {
|
|
1802
|
+
...params,
|
|
1803
|
+
prompt: newPrompt
|
|
1804
|
+
};
|
|
1805
|
+
},
|
|
1806
|
+
/**
|
|
1807
|
+
* Wrap generate for non-streaming output processing
|
|
1808
|
+
*/
|
|
1809
|
+
async wrapGenerate({ doGenerate, params }) {
|
|
1810
|
+
const processorState = params.providerOptions?.mastraProcessors;
|
|
1811
|
+
if (processorState?.tripwire) {
|
|
1812
|
+
const reason = processorState.reason || "Blocked by processor";
|
|
1813
|
+
return {
|
|
1814
|
+
content: [{ type: "text", text: reason }],
|
|
1815
|
+
finishReason: "stop",
|
|
1816
|
+
usage: { inputTokens: 0, outputTokens: 0, totalTokens: 0 },
|
|
1817
|
+
warnings: [{ type: "other", message: `Tripwire: ${reason}` }]
|
|
1818
|
+
};
|
|
1819
|
+
}
|
|
1820
|
+
const result = await doGenerate();
|
|
1821
|
+
if (!outputProcessors.length) return result;
|
|
1822
|
+
const messageList = new MessageList({
|
|
1823
|
+
threadId: memory?.threadId,
|
|
1824
|
+
resourceId: memory?.resourceId
|
|
1825
|
+
});
|
|
1826
|
+
for (const msg of params.prompt) {
|
|
1827
|
+
if (msg.role === "system") {
|
|
1828
|
+
messageList.addSystem(msg.content);
|
|
1829
|
+
} else {
|
|
1830
|
+
messageList.add(msg, "input");
|
|
1831
|
+
}
|
|
1832
|
+
}
|
|
1833
|
+
const textContent = result.content.filter((c) => c.type === "text").map((c) => c.text).join("");
|
|
1834
|
+
const responseMessage = {
|
|
1835
|
+
id: crypto.randomUUID(),
|
|
1836
|
+
role: "assistant",
|
|
1837
|
+
content: {
|
|
1838
|
+
format: 2,
|
|
1839
|
+
parts: [{ type: "text", text: textContent }]
|
|
1840
|
+
},
|
|
1841
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
1842
|
+
...memory?.threadId && { threadId: memory.threadId },
|
|
1843
|
+
...memory?.resourceId && { resourceId: memory.resourceId }
|
|
1844
|
+
};
|
|
1845
|
+
messageList.add(responseMessage, "response");
|
|
1846
|
+
for (const processor of outputProcessors) {
|
|
1847
|
+
if (processor.processOutputResult) {
|
|
1848
|
+
try {
|
|
1849
|
+
await processor.processOutputResult({
|
|
1850
|
+
messages: messageList.get.all.db(),
|
|
1851
|
+
messageList,
|
|
1852
|
+
requestContext,
|
|
1853
|
+
abort: (reason) => {
|
|
1854
|
+
throw new TripWire(reason || "Aborted by processor");
|
|
1855
|
+
}
|
|
1856
|
+
});
|
|
1857
|
+
} catch (error) {
|
|
1858
|
+
if (error instanceof TripWire) {
|
|
1859
|
+
return {
|
|
1860
|
+
content: [{ type: "text", text: error.message }],
|
|
1861
|
+
finishReason: "stop",
|
|
1862
|
+
usage: result.usage,
|
|
1863
|
+
warnings: [{ type: "other", message: `Output blocked: ${error.message}` }]
|
|
1864
|
+
};
|
|
1865
|
+
}
|
|
1866
|
+
throw error;
|
|
1867
|
+
}
|
|
1868
|
+
}
|
|
1869
|
+
}
|
|
1870
|
+
const processedText = messageList.get.response.db().map((m) => extractTextFromMastraMessage(m)).join("");
|
|
1871
|
+
return {
|
|
1872
|
+
...result,
|
|
1873
|
+
content: [{ type: "text", text: processedText }]
|
|
1874
|
+
};
|
|
1875
|
+
},
|
|
1876
|
+
/**
|
|
1877
|
+
* Wrap stream for streaming output processing
|
|
1878
|
+
*/
|
|
1879
|
+
async wrapStream({ doStream, params }) {
|
|
1880
|
+
const processorState = params.providerOptions?.mastraProcessors;
|
|
1881
|
+
if (processorState?.tripwire) {
|
|
1882
|
+
const reason = processorState.reason || "Blocked by processor";
|
|
1883
|
+
return {
|
|
1884
|
+
stream: createBlockedStream(reason)
|
|
1885
|
+
};
|
|
1886
|
+
}
|
|
1887
|
+
const { stream, ...rest } = await doStream();
|
|
1888
|
+
if (!outputProcessors.length) return { stream, ...rest };
|
|
1889
|
+
const processorStates = /* @__PURE__ */ new Map();
|
|
1890
|
+
const runId = crypto.randomUUID();
|
|
1891
|
+
const transformedStream = stream.pipeThrough(
|
|
1892
|
+
new TransformStream({
|
|
1893
|
+
async transform(chunk, controller) {
|
|
1894
|
+
let mastraChunk = convertFullStreamChunkToMastra(
|
|
1895
|
+
chunk,
|
|
1896
|
+
{ runId }
|
|
1897
|
+
);
|
|
1898
|
+
if (!mastraChunk) {
|
|
1899
|
+
controller.enqueue(chunk);
|
|
1900
|
+
return;
|
|
1901
|
+
}
|
|
1902
|
+
for (const processor of outputProcessors) {
|
|
1903
|
+
if (processor.processOutputStream && mastraChunk) {
|
|
1904
|
+
let state = processorStates.get(processor.id);
|
|
1905
|
+
if (!state) {
|
|
1906
|
+
state = { streamParts: [], customState: {} };
|
|
1907
|
+
processorStates.set(processor.id, state);
|
|
1908
|
+
}
|
|
1909
|
+
state.streamParts.push(mastraChunk);
|
|
1910
|
+
try {
|
|
1911
|
+
const result = await processor.processOutputStream({
|
|
1912
|
+
part: mastraChunk,
|
|
1913
|
+
streamParts: state.streamParts,
|
|
1914
|
+
state: state.customState,
|
|
1915
|
+
requestContext,
|
|
1916
|
+
abort: (reason) => {
|
|
1917
|
+
throw new TripWire(reason || "Aborted by processor");
|
|
1918
|
+
}
|
|
1919
|
+
});
|
|
1920
|
+
if (result === null || result === void 0) {
|
|
1921
|
+
mastraChunk = void 0;
|
|
1922
|
+
} else {
|
|
1923
|
+
mastraChunk = result;
|
|
1924
|
+
}
|
|
1925
|
+
} catch (error) {
|
|
1926
|
+
if (error instanceof TripWire) {
|
|
1927
|
+
controller.enqueue({
|
|
1928
|
+
type: "error",
|
|
1929
|
+
error: new Error(error.message)
|
|
1930
|
+
});
|
|
1931
|
+
controller.terminate();
|
|
1932
|
+
return;
|
|
1933
|
+
}
|
|
1934
|
+
throw error;
|
|
1935
|
+
}
|
|
1936
|
+
}
|
|
1937
|
+
}
|
|
1938
|
+
if (mastraChunk) {
|
|
1939
|
+
const aiChunk = convertMastraChunkToAISDKStreamPart(mastraChunk);
|
|
1940
|
+
if (aiChunk) {
|
|
1941
|
+
controller.enqueue(aiChunk);
|
|
1942
|
+
}
|
|
1943
|
+
}
|
|
1944
|
+
}
|
|
1945
|
+
})
|
|
1946
|
+
);
|
|
1947
|
+
return { stream: transformedStream, ...rest };
|
|
1948
|
+
}
|
|
1949
|
+
};
|
|
1950
|
+
}
|
|
1951
|
+
function createBlockedStream(reason) {
|
|
1952
|
+
return new ReadableStream({
|
|
1953
|
+
start(controller) {
|
|
1954
|
+
const id = crypto.randomUUID();
|
|
1955
|
+
controller.enqueue({
|
|
1956
|
+
type: "text-start",
|
|
1957
|
+
id
|
|
1958
|
+
});
|
|
1959
|
+
controller.enqueue({
|
|
1960
|
+
type: "text-delta",
|
|
1961
|
+
id,
|
|
1962
|
+
delta: reason
|
|
1963
|
+
});
|
|
1964
|
+
controller.enqueue({
|
|
1965
|
+
type: "text-end",
|
|
1966
|
+
id
|
|
1967
|
+
});
|
|
1968
|
+
controller.enqueue({
|
|
1969
|
+
type: "finish",
|
|
1970
|
+
finishReason: "stop",
|
|
1971
|
+
usage: { inputTokens: 0, outputTokens: 0, totalTokens: 0 }
|
|
1972
|
+
});
|
|
1973
|
+
controller.close();
|
|
1974
|
+
}
|
|
1975
|
+
});
|
|
1976
|
+
}
|
|
1977
|
+
function extractTextFromMastraMessage(msg) {
|
|
1978
|
+
const content = msg.content;
|
|
1979
|
+
if (typeof content === "string") {
|
|
1980
|
+
return content;
|
|
1981
|
+
}
|
|
1982
|
+
if (content?.parts) {
|
|
1983
|
+
return content.parts.filter((p) => p.type === "text" && "text" in p).map((p) => p.text).join("");
|
|
1984
|
+
}
|
|
1985
|
+
return "";
|
|
1986
|
+
}
|
|
1987
|
+
function convertMastraChunkToAISDKStreamPart(chunk) {
|
|
1988
|
+
switch (chunk.type) {
|
|
1989
|
+
// Text streaming
|
|
1990
|
+
case "text-start":
|
|
1991
|
+
return {
|
|
1992
|
+
type: "text-start",
|
|
1993
|
+
id: chunk.payload.id || crypto.randomUUID(),
|
|
1994
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
1995
|
+
};
|
|
1996
|
+
case "text-delta":
|
|
1997
|
+
return {
|
|
1998
|
+
type: "text-delta",
|
|
1999
|
+
id: chunk.payload.id || crypto.randomUUID(),
|
|
2000
|
+
delta: chunk.payload.text,
|
|
2001
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
2002
|
+
};
|
|
2003
|
+
case "text-end":
|
|
2004
|
+
return {
|
|
2005
|
+
type: "text-end",
|
|
2006
|
+
id: chunk.payload.id || crypto.randomUUID(),
|
|
2007
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
2008
|
+
};
|
|
2009
|
+
// Reasoning streaming
|
|
2010
|
+
case "reasoning-start":
|
|
2011
|
+
return {
|
|
2012
|
+
type: "reasoning-start",
|
|
2013
|
+
id: chunk.payload.id || crypto.randomUUID(),
|
|
2014
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
2015
|
+
};
|
|
2016
|
+
case "reasoning-delta":
|
|
2017
|
+
return {
|
|
2018
|
+
type: "reasoning-delta",
|
|
2019
|
+
id: chunk.payload.id || crypto.randomUUID(),
|
|
2020
|
+
delta: chunk.payload.text,
|
|
2021
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
2022
|
+
};
|
|
2023
|
+
case "reasoning-end":
|
|
2024
|
+
return {
|
|
2025
|
+
type: "reasoning-end",
|
|
2026
|
+
id: chunk.payload.id || crypto.randomUUID(),
|
|
2027
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
2028
|
+
};
|
|
2029
|
+
// Tool call (complete)
|
|
2030
|
+
case "tool-call":
|
|
2031
|
+
return {
|
|
2032
|
+
type: "tool-call",
|
|
2033
|
+
toolCallId: chunk.payload.toolCallId,
|
|
2034
|
+
toolName: chunk.payload.toolName,
|
|
2035
|
+
input: JSON.stringify(chunk.payload.args),
|
|
2036
|
+
providerExecuted: chunk.payload.providerExecuted,
|
|
2037
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
2038
|
+
};
|
|
2039
|
+
// Tool call input streaming
|
|
2040
|
+
case "tool-call-input-streaming-start":
|
|
2041
|
+
return {
|
|
2042
|
+
type: "tool-input-start",
|
|
2043
|
+
id: chunk.payload.toolCallId,
|
|
2044
|
+
toolName: chunk.payload.toolName,
|
|
2045
|
+
providerExecuted: chunk.payload.providerExecuted,
|
|
2046
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
2047
|
+
};
|
|
2048
|
+
case "tool-call-delta":
|
|
2049
|
+
return {
|
|
2050
|
+
type: "tool-input-delta",
|
|
2051
|
+
id: chunk.payload.toolCallId,
|
|
2052
|
+
delta: chunk.payload.argsTextDelta,
|
|
2053
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
2054
|
+
};
|
|
2055
|
+
case "tool-call-input-streaming-end":
|
|
2056
|
+
return {
|
|
2057
|
+
type: "tool-input-end",
|
|
2058
|
+
id: chunk.payload.toolCallId,
|
|
2059
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
2060
|
+
};
|
|
2061
|
+
// Tool result
|
|
2062
|
+
case "tool-result":
|
|
2063
|
+
return {
|
|
2064
|
+
type: "tool-result",
|
|
2065
|
+
toolCallId: chunk.payload.toolCallId,
|
|
2066
|
+
toolName: chunk.payload.toolName,
|
|
2067
|
+
result: { type: "json", value: chunk.payload.result },
|
|
2068
|
+
isError: chunk.payload.isError,
|
|
2069
|
+
providerExecuted: chunk.payload.providerExecuted,
|
|
2070
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
2071
|
+
};
|
|
2072
|
+
// Source (citations)
|
|
2073
|
+
case "source":
|
|
2074
|
+
if (chunk.payload.sourceType === "url") {
|
|
2075
|
+
return {
|
|
2076
|
+
type: "source",
|
|
2077
|
+
sourceType: "url",
|
|
2078
|
+
id: chunk.payload.id,
|
|
2079
|
+
url: chunk.payload.url,
|
|
2080
|
+
title: chunk.payload.title,
|
|
2081
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
2082
|
+
};
|
|
2083
|
+
} else {
|
|
2084
|
+
return {
|
|
2085
|
+
type: "source",
|
|
2086
|
+
sourceType: "document",
|
|
2087
|
+
id: chunk.payload.id,
|
|
2088
|
+
mediaType: chunk.payload.mimeType,
|
|
2089
|
+
title: chunk.payload.title,
|
|
2090
|
+
filename: chunk.payload.filename,
|
|
2091
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
2092
|
+
};
|
|
2093
|
+
}
|
|
2094
|
+
// File output
|
|
2095
|
+
case "file":
|
|
2096
|
+
return {
|
|
2097
|
+
type: "file",
|
|
2098
|
+
data: chunk.payload.data || chunk.payload.base64,
|
|
2099
|
+
mediaType: chunk.payload.mimeType
|
|
2100
|
+
};
|
|
2101
|
+
// Response metadata
|
|
2102
|
+
case "response-metadata":
|
|
2103
|
+
return {
|
|
2104
|
+
type: "response-metadata",
|
|
2105
|
+
...chunk.payload
|
|
2106
|
+
};
|
|
2107
|
+
// Raw provider data
|
|
2108
|
+
case "raw":
|
|
2109
|
+
return {
|
|
2110
|
+
type: "raw",
|
|
2111
|
+
rawValue: chunk.payload
|
|
2112
|
+
};
|
|
2113
|
+
// Finish
|
|
2114
|
+
case "finish": {
|
|
2115
|
+
const usage = chunk.payload.output?.usage;
|
|
2116
|
+
return {
|
|
2117
|
+
type: "finish",
|
|
2118
|
+
finishReason: toAISDKFinishReason(chunk.payload.stepResult?.reason || "stop"),
|
|
2119
|
+
usage: usage ? {
|
|
2120
|
+
inputTokens: usage.inputTokens || 0,
|
|
2121
|
+
outputTokens: usage.outputTokens || 0,
|
|
2122
|
+
totalTokens: usage.totalTokens || 0
|
|
2123
|
+
} : { inputTokens: 0, outputTokens: 0, totalTokens: 0 },
|
|
2124
|
+
providerMetadata: chunk.payload.metadata?.providerMetadata
|
|
2125
|
+
};
|
|
2126
|
+
}
|
|
2127
|
+
// Error
|
|
2128
|
+
case "error":
|
|
2129
|
+
return {
|
|
2130
|
+
type: "error",
|
|
2131
|
+
error: chunk.payload.error || chunk.payload
|
|
2132
|
+
};
|
|
2133
|
+
default:
|
|
2134
|
+
return null;
|
|
2135
|
+
}
|
|
2136
|
+
}
|
|
1654
2137
|
|
|
1655
2138
|
// src/to-ai-sdk-format.ts
|
|
1656
2139
|
function toAISdkFormat() {
|
|
@@ -1659,6 +2142,6 @@ function toAISdkFormat() {
|
|
|
1659
2142
|
);
|
|
1660
2143
|
}
|
|
1661
2144
|
|
|
1662
|
-
export { chatRoute, handleChatStream, handleNetworkStream, handleWorkflowStream, networkRoute, toAISdkFormat, toAISdkV5Stream as toAISdkStream, workflowRoute };
|
|
2145
|
+
export { chatRoute, handleChatStream, handleNetworkStream, handleWorkflowStream, networkRoute, toAISdkFormat, toAISdkV5Stream as toAISdkStream, withMastra, workflowRoute };
|
|
1663
2146
|
//# sourceMappingURL=index.js.map
|
|
1664
2147
|
//# sourceMappingURL=index.js.map
|