@mastra/core 0.10.12 → 0.10.13
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/package.json +1 -1
- package/a2a.d.ts +0 -1
- package/agent.d.ts +0 -1
- package/base.d.ts +0 -1
- package/bundler.d.ts +0 -1
- package/deployer.d.ts +0 -1
- package/di.d.ts +0 -1
- package/dist/a2a/index.cjs +0 -79
- package/dist/a2a/index.d.cts +0 -780
- package/dist/a2a/index.d.ts +0 -780
- package/dist/a2a/index.js +0 -68
- package/dist/agent/index.cjs +0 -15
- package/dist/agent/index.d.cts +0 -29
- package/dist/agent/index.d.ts +0 -29
- package/dist/agent/index.js +0 -2
- package/dist/base-B_y9sMg0.d.cts +0 -162
- package/dist/base-CS5eSXbL.d.cts +0 -4117
- package/dist/base-ClrXcCRx.d.ts +0 -162
- package/dist/base-DBhKcre4.d.ts +0 -4117
- package/dist/base.cjs +0 -10
- package/dist/base.d.cts +0 -6
- package/dist/base.d.ts +0 -6
- package/dist/base.js +0 -1
- package/dist/bundler/index.cjs +0 -10
- package/dist/bundler/index.d.cts +0 -30
- package/dist/bundler/index.d.ts +0 -30
- package/dist/bundler/index.js +0 -1
- package/dist/chunk-2GRYVZ2O.cjs +0 -244
- package/dist/chunk-2HDFKWFU.js +0 -2249
- package/dist/chunk-32C7JDIZ.js +0 -1
- package/dist/chunk-4UBOJFSL.js +0 -989
- package/dist/chunk-4UWPFBC6.js +0 -88
- package/dist/chunk-4Z3OU5RY.cjs +0 -31
- package/dist/chunk-5HTMDAXP.js +0 -359
- package/dist/chunk-5IEKR756.js +0 -53
- package/dist/chunk-5YDTZN2X.js +0 -114
- package/dist/chunk-6UNGH46J.js +0 -75
- package/dist/chunk-6Y4UL5Z6.cjs +0 -94
- package/dist/chunk-7F6BQXE2.cjs +0 -425
- package/dist/chunk-7H2GET5Z.cjs +0 -668
- package/dist/chunk-7HZ6NIAF.cjs +0 -2
- package/dist/chunk-7MZNOW6W.cjs +0 -2263
- package/dist/chunk-7XQIPES3.js +0 -668
- package/dist/chunk-AKYTYALY.js +0 -70
- package/dist/chunk-ATXBSEFT.js +0 -22
- package/dist/chunk-B6TOBUS6.cjs +0 -80
- package/dist/chunk-B7SQOKEC.cjs +0 -91
- package/dist/chunk-BB4KXGBU.js +0 -83
- package/dist/chunk-BB6DPGIV.cjs +0 -6
- package/dist/chunk-C4LMN2IR.js +0 -27
- package/dist/chunk-DFFVEKIG.js +0 -407
- package/dist/chunk-E7AX3U6M.cjs +0 -659
- package/dist/chunk-EWCOOO3H.js +0 -5007
- package/dist/chunk-F2WMR75C.cjs +0 -183
- package/dist/chunk-FBKJ3652.cjs +0 -5034
- package/dist/chunk-FEYYOBBG.cjs +0 -24
- package/dist/chunk-FL5SZ2XU.js +0 -181
- package/dist/chunk-GH2KM66J.js +0 -37
- package/dist/chunk-GWFS5DAR.cjs +0 -37
- package/dist/chunk-HNEE7IF4.js +0 -60
- package/dist/chunk-HSVOEWAM.cjs +0 -2
- package/dist/chunk-J52TXHZV.cjs +0 -73
- package/dist/chunk-JNMQKJH4.js +0 -10
- package/dist/chunk-JQOMTERC.js +0 -89
- package/dist/chunk-LABUWBKX.cjs +0 -71
- package/dist/chunk-LXFZUKP3.cjs +0 -34
- package/dist/chunk-MP2QBLUJ.cjs +0 -70
- package/dist/chunk-MUNFCOMB.cjs +0 -62
- package/dist/chunk-NH5WJNNS.js +0 -1
- package/dist/chunk-P3Q73CAW.cjs +0 -55
- package/dist/chunk-PA2YIVIT.js +0 -61
- package/dist/chunk-QFTBW7ZZ.cjs +0 -2
- package/dist/chunk-QQ5K5TZE.cjs +0 -619
- package/dist/chunk-QUSEDVYI.cjs +0 -991
- package/dist/chunk-R4V75T7J.js +0 -1
- package/dist/chunk-SGGPJWRQ.js +0 -69
- package/dist/chunk-ST5RMVLG.cjs +0 -87
- package/dist/chunk-TC2SCOTE.js +0 -605
- package/dist/chunk-U64IJDC5.cjs +0 -109
- package/dist/chunk-UX3B6S2I.cjs +0 -65
- package/dist/chunk-V5D2LIF5.js +0 -68
- package/dist/chunk-VG4OPO2R.js +0 -240
- package/dist/chunk-WQNOATKB.js +0 -103
- package/dist/chunk-Y7D2JLKS.js +0 -4
- package/dist/chunk-YJEHXYK5.js +0 -657
- package/dist/chunk-YOQP5T77.js +0 -32
- package/dist/chunk-ZIZ3CVHN.cjs +0 -120
- package/dist/chunk-ZPOUMTTH.cjs +0 -362
- package/dist/chunk-ZZLBNB3U.cjs +0 -12
- package/dist/deployer/index.cjs +0 -10
- package/dist/deployer/index.d.cts +0 -19
- package/dist/deployer/index.d.ts +0 -19
- package/dist/deployer/index.js +0 -1
- package/dist/di/index.cjs +0 -10
- package/dist/di/index.d.cts +0 -1
- package/dist/di/index.d.ts +0 -1
- package/dist/di/index.js +0 -1
- package/dist/error/index.cjs +0 -22
- package/dist/error/index.d.cts +0 -86
- package/dist/error/index.d.ts +0 -86
- package/dist/error/index.js +0 -1
- package/dist/eval/index.cjs +0 -14
- package/dist/eval/index.d.cts +0 -43
- package/dist/eval/index.d.ts +0 -43
- package/dist/eval/index.js +0 -1
- package/dist/hooks/index.cjs +0 -18
- package/dist/hooks/index.d.cts +0 -33
- package/dist/hooks/index.d.ts +0 -33
- package/dist/hooks/index.js +0 -1
- package/dist/index.cjs +0 -281
- package/dist/index.d.cts +0 -92
- package/dist/index.d.ts +0 -92
- package/dist/index.js +0 -112
- package/dist/integration/index.cjs +0 -14
- package/dist/integration/index.d.cts +0 -65
- package/dist/integration/index.d.ts +0 -65
- package/dist/integration/index.js +0 -1
- package/dist/llm/index.cjs +0 -10
- package/dist/llm/index.d.cts +0 -29
- package/dist/llm/index.d.ts +0 -29
- package/dist/llm/index.js +0 -1
- package/dist/logger/index.cjs +0 -43
- package/dist/logger/index.d.cts +0 -96
- package/dist/logger/index.d.ts +0 -96
- package/dist/logger/index.js +0 -2
- package/dist/logger-B8XXh6ya.d.cts +0 -159
- package/dist/logger-Bpa2oLL4.d.ts +0 -159
- package/dist/mastra/index.cjs +0 -10
- package/dist/mastra/index.d.cts +0 -29
- package/dist/mastra/index.d.ts +0 -29
- package/dist/mastra/index.js +0 -1
- package/dist/mcp/index.cjs +0 -106
- package/dist/mcp/index.d.cts +0 -29
- package/dist/mcp/index.d.ts +0 -29
- package/dist/mcp/index.js +0 -100
- package/dist/memory/index.cjs +0 -18
- package/dist/memory/index.d.cts +0 -29
- package/dist/memory/index.d.ts +0 -29
- package/dist/memory/index.js +0 -1
- package/dist/network/index.cjs +0 -311
- package/dist/network/index.d.cts +0 -29
- package/dist/network/index.d.ts +0 -29
- package/dist/network/index.js +0 -309
- package/dist/network/vNext/index.cjs +0 -873
- package/dist/network/vNext/index.d.cts +0 -29
- package/dist/network/vNext/index.d.ts +0 -29
- package/dist/network/vNext/index.js +0 -871
- package/dist/relevance/index.cjs +0 -18
- package/dist/relevance/index.d.cts +0 -49
- package/dist/relevance/index.d.ts +0 -49
- package/dist/relevance/index.js +0 -1
- package/dist/runtime-context/index.cjs +0 -10
- package/dist/runtime-context/index.d.cts +0 -52
- package/dist/runtime-context/index.d.ts +0 -52
- package/dist/runtime-context/index.js +0 -1
- package/dist/server/index.cjs +0 -62
- package/dist/server/index.d.cts +0 -52
- package/dist/server/index.d.ts +0 -52
- package/dist/server/index.js +0 -59
- package/dist/storage/index.cjs +0 -336
- package/dist/storage/index.d.cts +0 -149
- package/dist/storage/index.d.ts +0 -149
- package/dist/storage/index.js +0 -303
- package/dist/telemetry/index.cjs +0 -30
- package/dist/telemetry/index.d.cts +0 -75
- package/dist/telemetry/index.d.ts +0 -75
- package/dist/telemetry/index.js +0 -1
- package/dist/telemetry/otel-vendor.cjs +0 -103
- package/dist/telemetry/otel-vendor.d.cts +0 -20
- package/dist/telemetry/otel-vendor.d.ts +0 -20
- package/dist/telemetry/otel-vendor.js +0 -57
- package/dist/tools/index.cjs +0 -18
- package/dist/tools/index.d.cts +0 -41
- package/dist/tools/index.d.ts +0 -41
- package/dist/tools/index.js +0 -1
- package/dist/tts/index.cjs +0 -10
- package/dist/tts/index.d.cts +0 -28
- package/dist/tts/index.d.ts +0 -28
- package/dist/tts/index.js +0 -1
- package/dist/types-Bo1uigWx.d.cts +0 -17
- package/dist/types-Bo1uigWx.d.ts +0 -17
- package/dist/utils.cjs +0 -58
- package/dist/utils.d.cts +0 -149
- package/dist/utils.d.ts +0 -149
- package/dist/utils.js +0 -1
- package/dist/vector/filter/index.cjs +0 -192
- package/dist/vector/filter/index.d.cts +0 -128
- package/dist/vector/filter/index.d.ts +0 -128
- package/dist/vector/filter/index.js +0 -190
- package/dist/vector/index.cjs +0 -10
- package/dist/vector/index.d.cts +0 -77
- package/dist/vector/index.d.ts +0 -77
- package/dist/vector/index.js +0 -1
- package/dist/voice/index.cjs +0 -18
- package/dist/voice/index.d.cts +0 -29
- package/dist/voice/index.d.ts +0 -29
- package/dist/voice/index.js +0 -1
- package/dist/workflows/constants.cjs +0 -10
- package/dist/workflows/constants.d.cts +0 -3
- package/dist/workflows/constants.d.ts +0 -3
- package/dist/workflows/constants.js +0 -1
- package/dist/workflows/index.cjs +0 -42
- package/dist/workflows/index.d.cts +0 -282
- package/dist/workflows/index.d.ts +0 -282
- package/dist/workflows/index.js +0 -1
- package/dist/workflows/legacy/index.cjs +0 -90
- package/dist/workflows/legacy/index.d.cts +0 -91
- package/dist/workflows/legacy/index.d.ts +0 -91
- package/dist/workflows/legacy/index.js +0 -1
- package/error.d.ts +0 -1
- package/eval.d.ts +0 -1
- package/hooks.d.ts +0 -1
- package/integration.d.ts +0 -1
- package/llm.d.ts +0 -1
- package/logger.d.ts +0 -1
- package/mastra.d.ts +0 -1
- package/mcp.d.ts +0 -1
- package/memory.d.ts +0 -1
- package/network/vNext.d.ts +0 -1
- package/network.d.ts +0 -1
- package/relevance.d.ts +0 -1
- package/runtime-context.d.ts +0 -1
- package/server.d.ts +0 -1
- package/storage.d.ts +0 -1
- package/telemetry/otel-vendor.d.ts +0 -1
- package/telemetry.d.ts +0 -1
- package/tools.d.ts +0 -1
- package/tts.d.ts +0 -1
- package/utils.d.ts +0 -1
- package/vector/filter.d.ts +0 -1
- package/vector.d.ts +0 -1
- package/voice.d.ts +0 -1
- package/workflows/_constants.d.ts +0 -1
- package/workflows/legacy.d.ts +0 -1
- package/workflows.d.ts +0 -1
package/dist/chunk-QUSEDVYI.cjs
DELETED
|
@@ -1,991 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var chunk7F6BQXE2_cjs = require('./chunk-7F6BQXE2.cjs');
|
|
4
|
-
var chunkB6TOBUS6_cjs = require('./chunk-B6TOBUS6.cjs');
|
|
5
|
-
var crypto = require('crypto');
|
|
6
|
-
var ai = require('ai');
|
|
7
|
-
var providerUtils = require('@ai-sdk/provider-utils');
|
|
8
|
-
var zod = require('zod');
|
|
9
|
-
|
|
10
|
-
// src/agent/message-list/prompt/attachments-to-parts.ts
|
|
11
|
-
function attachmentsToParts(attachments) {
|
|
12
|
-
const parts = [];
|
|
13
|
-
for (const attachment of attachments) {
|
|
14
|
-
let url;
|
|
15
|
-
try {
|
|
16
|
-
url = new URL(attachment.url);
|
|
17
|
-
} catch {
|
|
18
|
-
throw new Error(`Invalid URL: ${attachment.url}`);
|
|
19
|
-
}
|
|
20
|
-
switch (url.protocol) {
|
|
21
|
-
case "http:":
|
|
22
|
-
case "https:": {
|
|
23
|
-
if (attachment.contentType?.startsWith("image/")) {
|
|
24
|
-
parts.push({ type: "image", image: url.toString(), mimeType: attachment.contentType });
|
|
25
|
-
} else {
|
|
26
|
-
if (!attachment.contentType) {
|
|
27
|
-
throw new Error("If the attachment is not an image, it must specify a content type");
|
|
28
|
-
}
|
|
29
|
-
parts.push({
|
|
30
|
-
type: "file",
|
|
31
|
-
data: url.toString(),
|
|
32
|
-
mimeType: attachment.contentType
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
break;
|
|
36
|
-
}
|
|
37
|
-
case "data:": {
|
|
38
|
-
if (attachment.contentType?.startsWith("image/")) {
|
|
39
|
-
parts.push({
|
|
40
|
-
type: "image",
|
|
41
|
-
image: attachment.url,
|
|
42
|
-
mimeType: attachment.contentType
|
|
43
|
-
});
|
|
44
|
-
} else if (attachment.contentType?.startsWith("text/")) {
|
|
45
|
-
parts.push({
|
|
46
|
-
type: "file",
|
|
47
|
-
data: attachment.url,
|
|
48
|
-
mimeType: attachment.contentType
|
|
49
|
-
});
|
|
50
|
-
} else {
|
|
51
|
-
if (!attachment.contentType) {
|
|
52
|
-
throw new Error("If the attachment is not an image or text, it must specify a content type");
|
|
53
|
-
}
|
|
54
|
-
parts.push({
|
|
55
|
-
type: "file",
|
|
56
|
-
data: attachment.url,
|
|
57
|
-
mimeType: attachment.contentType
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
break;
|
|
61
|
-
}
|
|
62
|
-
default: {
|
|
63
|
-
throw new Error(`Unsupported URL protocol: ${url.protocol}`);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
return parts;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// src/agent/message-list/prompt/convert-to-mastra-v1.ts
|
|
71
|
-
var makePushOrCombine = (v1Messages) => (msg) => {
|
|
72
|
-
const previousMessage = v1Messages.at(-1);
|
|
73
|
-
if (msg.role === previousMessage?.role && Array.isArray(previousMessage.content) && Array.isArray(msg.content) && // we were creating new messages for tool calls before and not appending to the assistant message
|
|
74
|
-
// so don't append here so everything works as before
|
|
75
|
-
(msg.role !== `assistant` || msg.role === `assistant` && msg.content.at(-1)?.type !== `tool-call`)) {
|
|
76
|
-
for (const part of msg.content) {
|
|
77
|
-
previousMessage.content.push(part);
|
|
78
|
-
}
|
|
79
|
-
} else {
|
|
80
|
-
v1Messages.push(msg);
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
function convertToV1Messages(messages) {
|
|
84
|
-
const v1Messages = [];
|
|
85
|
-
const pushOrCombine = makePushOrCombine(v1Messages);
|
|
86
|
-
for (let i = 0; i < messages.length; i++) {
|
|
87
|
-
const message = messages[i];
|
|
88
|
-
const isLastMessage = i === messages.length - 1;
|
|
89
|
-
if (!message?.content) continue;
|
|
90
|
-
const { content, experimental_attachments: inputAttachments = [], parts: inputParts } = message.content;
|
|
91
|
-
const { role } = message;
|
|
92
|
-
const fields = {
|
|
93
|
-
id: message.id,
|
|
94
|
-
createdAt: message.createdAt,
|
|
95
|
-
resourceId: message.resourceId,
|
|
96
|
-
threadId: message.threadId
|
|
97
|
-
};
|
|
98
|
-
const experimental_attachments = [...inputAttachments];
|
|
99
|
-
const parts = [];
|
|
100
|
-
for (const part of inputParts) {
|
|
101
|
-
if (part.type === "file") {
|
|
102
|
-
experimental_attachments.push({
|
|
103
|
-
url: part.data,
|
|
104
|
-
contentType: part.mimeType
|
|
105
|
-
});
|
|
106
|
-
} else {
|
|
107
|
-
parts.push(part);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
switch (role) {
|
|
111
|
-
case "user": {
|
|
112
|
-
if (parts == null) {
|
|
113
|
-
const userContent = experimental_attachments ? [{ type: "text", text: content || "" }, ...attachmentsToParts(experimental_attachments)] : { type: "text", text: content || "" };
|
|
114
|
-
pushOrCombine({
|
|
115
|
-
role: "user",
|
|
116
|
-
...fields,
|
|
117
|
-
type: "text",
|
|
118
|
-
// @ts-ignore
|
|
119
|
-
content: userContent
|
|
120
|
-
});
|
|
121
|
-
} else {
|
|
122
|
-
const textParts = message.content.parts.filter((part) => part.type === "text").map((part) => ({
|
|
123
|
-
type: "text",
|
|
124
|
-
text: part.text
|
|
125
|
-
}));
|
|
126
|
-
const userContent = experimental_attachments ? [...textParts, ...attachmentsToParts(experimental_attachments)] : textParts;
|
|
127
|
-
pushOrCombine({
|
|
128
|
-
role: "user",
|
|
129
|
-
...fields,
|
|
130
|
-
type: "text",
|
|
131
|
-
content: Array.isArray(userContent) && userContent.length === 1 && userContent[0]?.type === `text` && typeof content !== `undefined` ? content : userContent
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
break;
|
|
135
|
-
}
|
|
136
|
-
case "assistant": {
|
|
137
|
-
if (message.content.parts != null) {
|
|
138
|
-
let processBlock2 = function() {
|
|
139
|
-
const content2 = [];
|
|
140
|
-
for (const part of block) {
|
|
141
|
-
switch (part.type) {
|
|
142
|
-
case "file":
|
|
143
|
-
case "text": {
|
|
144
|
-
content2.push(part);
|
|
145
|
-
break;
|
|
146
|
-
}
|
|
147
|
-
case "reasoning": {
|
|
148
|
-
for (const detail of part.details) {
|
|
149
|
-
switch (detail.type) {
|
|
150
|
-
case "text":
|
|
151
|
-
content2.push({
|
|
152
|
-
type: "reasoning",
|
|
153
|
-
text: detail.text,
|
|
154
|
-
signature: detail.signature
|
|
155
|
-
});
|
|
156
|
-
break;
|
|
157
|
-
case "redacted":
|
|
158
|
-
content2.push({
|
|
159
|
-
type: "redacted-reasoning",
|
|
160
|
-
data: detail.data
|
|
161
|
-
});
|
|
162
|
-
break;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
break;
|
|
166
|
-
}
|
|
167
|
-
case "tool-invocation":
|
|
168
|
-
content2.push({
|
|
169
|
-
type: "tool-call",
|
|
170
|
-
toolCallId: part.toolInvocation.toolCallId,
|
|
171
|
-
toolName: part.toolInvocation.toolName,
|
|
172
|
-
args: part.toolInvocation.args
|
|
173
|
-
});
|
|
174
|
-
break;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
pushOrCombine({
|
|
178
|
-
role: "assistant",
|
|
179
|
-
...fields,
|
|
180
|
-
type: content2.some((c) => c.type === `tool-call`) ? "tool-call" : "text",
|
|
181
|
-
// content: content,
|
|
182
|
-
content: typeof content2 !== `string` && Array.isArray(content2) && content2.length === 1 && content2[0]?.type === `text` ? message?.content?.content || content2 : content2
|
|
183
|
-
});
|
|
184
|
-
const stepInvocations = block.filter((part) => `type` in part && part.type === "tool-invocation").map((part) => part.toolInvocation);
|
|
185
|
-
if (stepInvocations.length > 0) {
|
|
186
|
-
pushOrCombine({
|
|
187
|
-
role: "tool",
|
|
188
|
-
...fields,
|
|
189
|
-
type: "tool-result",
|
|
190
|
-
// @ts-ignore
|
|
191
|
-
content: stepInvocations.map((toolInvocation) => {
|
|
192
|
-
const { toolCallId, toolName } = toolInvocation;
|
|
193
|
-
return {
|
|
194
|
-
type: "tool-result",
|
|
195
|
-
toolCallId,
|
|
196
|
-
toolName,
|
|
197
|
-
// @ts-ignore
|
|
198
|
-
result: toolInvocation.result
|
|
199
|
-
};
|
|
200
|
-
})
|
|
201
|
-
});
|
|
202
|
-
}
|
|
203
|
-
block = [];
|
|
204
|
-
blockHasToolInvocations = false;
|
|
205
|
-
currentStep++;
|
|
206
|
-
};
|
|
207
|
-
let currentStep = 0;
|
|
208
|
-
let blockHasToolInvocations = false;
|
|
209
|
-
let block = [];
|
|
210
|
-
for (const part of message.content.parts) {
|
|
211
|
-
switch (part.type) {
|
|
212
|
-
case "text": {
|
|
213
|
-
if (blockHasToolInvocations) {
|
|
214
|
-
processBlock2();
|
|
215
|
-
}
|
|
216
|
-
block.push(part);
|
|
217
|
-
break;
|
|
218
|
-
}
|
|
219
|
-
case "file":
|
|
220
|
-
case "reasoning": {
|
|
221
|
-
block.push(part);
|
|
222
|
-
break;
|
|
223
|
-
}
|
|
224
|
-
case "tool-invocation": {
|
|
225
|
-
if ((part.toolInvocation.step ?? 0) !== currentStep) {
|
|
226
|
-
processBlock2();
|
|
227
|
-
}
|
|
228
|
-
block.push(part);
|
|
229
|
-
blockHasToolInvocations = true;
|
|
230
|
-
break;
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
processBlock2();
|
|
235
|
-
break;
|
|
236
|
-
}
|
|
237
|
-
const toolInvocations = message.content.toolInvocations;
|
|
238
|
-
if (toolInvocations == null || toolInvocations.length === 0) {
|
|
239
|
-
pushOrCombine({ role: "assistant", ...fields, content: content || "", type: "text" });
|
|
240
|
-
break;
|
|
241
|
-
}
|
|
242
|
-
const maxStep = toolInvocations.reduce((max, toolInvocation) => {
|
|
243
|
-
return Math.max(max, toolInvocation.step ?? 0);
|
|
244
|
-
}, 0);
|
|
245
|
-
for (let i2 = 0; i2 <= maxStep; i2++) {
|
|
246
|
-
const stepInvocations = toolInvocations.filter((toolInvocation) => (toolInvocation.step ?? 0) === i2);
|
|
247
|
-
if (stepInvocations.length === 0) {
|
|
248
|
-
continue;
|
|
249
|
-
}
|
|
250
|
-
pushOrCombine({
|
|
251
|
-
role: "assistant",
|
|
252
|
-
...fields,
|
|
253
|
-
type: "tool-call",
|
|
254
|
-
content: [
|
|
255
|
-
...isLastMessage && content && i2 === 0 ? [{ type: "text", text: content }] : [],
|
|
256
|
-
...stepInvocations.map(({ toolCallId, toolName, args }) => ({
|
|
257
|
-
type: "tool-call",
|
|
258
|
-
toolCallId,
|
|
259
|
-
toolName,
|
|
260
|
-
args
|
|
261
|
-
}))
|
|
262
|
-
]
|
|
263
|
-
});
|
|
264
|
-
pushOrCombine({
|
|
265
|
-
role: "tool",
|
|
266
|
-
...fields,
|
|
267
|
-
type: "tool-result",
|
|
268
|
-
content: stepInvocations.map((toolInvocation) => {
|
|
269
|
-
if (!("result" in toolInvocation)) {
|
|
270
|
-
return toolInvocation;
|
|
271
|
-
}
|
|
272
|
-
const { toolCallId, toolName, result } = toolInvocation;
|
|
273
|
-
return {
|
|
274
|
-
type: "tool-result",
|
|
275
|
-
toolCallId,
|
|
276
|
-
toolName,
|
|
277
|
-
result
|
|
278
|
-
};
|
|
279
|
-
})
|
|
280
|
-
});
|
|
281
|
-
}
|
|
282
|
-
if (content && !isLastMessage) {
|
|
283
|
-
pushOrCombine({ role: "assistant", ...fields, type: "text", content: content || "" });
|
|
284
|
-
}
|
|
285
|
-
break;
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
return v1Messages;
|
|
290
|
-
}
|
|
291
|
-
zod.z.union([
|
|
292
|
-
zod.z.string(),
|
|
293
|
-
zod.z.instanceof(Uint8Array),
|
|
294
|
-
zod.z.instanceof(ArrayBuffer),
|
|
295
|
-
zod.z.custom(
|
|
296
|
-
// Buffer might not be available in some environments such as CloudFlare:
|
|
297
|
-
(value) => globalThis.Buffer?.isBuffer(value) ?? false,
|
|
298
|
-
{ message: "Must be a Buffer" }
|
|
299
|
-
)
|
|
300
|
-
]);
|
|
301
|
-
function convertDataContentToBase64String(content) {
|
|
302
|
-
if (typeof content === "string") {
|
|
303
|
-
return content;
|
|
304
|
-
}
|
|
305
|
-
if (content instanceof ArrayBuffer) {
|
|
306
|
-
return providerUtils.convertUint8ArrayToBase64(new Uint8Array(content));
|
|
307
|
-
}
|
|
308
|
-
return providerUtils.convertUint8ArrayToBase64(content);
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
// src/agent/message-list/index.ts
|
|
312
|
-
function isToolCallMessage(message) {
|
|
313
|
-
if (message.role === "tool") {
|
|
314
|
-
return true;
|
|
315
|
-
}
|
|
316
|
-
if (message.role === "assistant" && Array.isArray(message.content)) {
|
|
317
|
-
return message.content.some((part) => part.type === "tool-call");
|
|
318
|
-
}
|
|
319
|
-
return false;
|
|
320
|
-
}
|
|
321
|
-
var MessageList = class _MessageList {
|
|
322
|
-
messages = [];
|
|
323
|
-
// passed in by dev in input or context
|
|
324
|
-
systemMessages = [];
|
|
325
|
-
// passed in by us for a specific purpose, eg memory system message
|
|
326
|
-
taggedSystemMessages = {};
|
|
327
|
-
memoryInfo = null;
|
|
328
|
-
// used to filter this.messages by how it was added: input/response/memory
|
|
329
|
-
memoryMessages = /* @__PURE__ */ new Set();
|
|
330
|
-
newUserMessages = /* @__PURE__ */ new Set();
|
|
331
|
-
newResponseMessages = /* @__PURE__ */ new Set();
|
|
332
|
-
userContextMessages = /* @__PURE__ */ new Set();
|
|
333
|
-
generateMessageId;
|
|
334
|
-
constructor({
|
|
335
|
-
threadId,
|
|
336
|
-
resourceId,
|
|
337
|
-
generateMessageId
|
|
338
|
-
} = {}) {
|
|
339
|
-
if (threadId) {
|
|
340
|
-
this.memoryInfo = { threadId, resourceId };
|
|
341
|
-
this.generateMessageId = generateMessageId;
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
add(messages, messageSource) {
|
|
345
|
-
if (!messages) return this;
|
|
346
|
-
for (const message of Array.isArray(messages) ? messages : [messages]) {
|
|
347
|
-
this.addOne(
|
|
348
|
-
typeof message === `string` ? {
|
|
349
|
-
role: "user",
|
|
350
|
-
content: message
|
|
351
|
-
} : message,
|
|
352
|
-
messageSource
|
|
353
|
-
);
|
|
354
|
-
}
|
|
355
|
-
return this;
|
|
356
|
-
}
|
|
357
|
-
getLatestUserContent() {
|
|
358
|
-
const currentUserMessages = this.all.core().filter((m) => m.role === "user");
|
|
359
|
-
const content = currentUserMessages.at(-1)?.content;
|
|
360
|
-
if (!content) return null;
|
|
361
|
-
return _MessageList.coreContentToString(content);
|
|
362
|
-
}
|
|
363
|
-
get get() {
|
|
364
|
-
return {
|
|
365
|
-
all: this.all,
|
|
366
|
-
remembered: this.remembered,
|
|
367
|
-
input: this.input,
|
|
368
|
-
response: this.response
|
|
369
|
-
};
|
|
370
|
-
}
|
|
371
|
-
all = {
|
|
372
|
-
v2: () => this.messages,
|
|
373
|
-
v1: () => convertToV1Messages(this.messages),
|
|
374
|
-
ui: () => this.messages.map(_MessageList.toUIMessage),
|
|
375
|
-
core: () => this.convertToCoreMessages(this.all.ui()),
|
|
376
|
-
prompt: () => {
|
|
377
|
-
const coreMessages = this.all.core();
|
|
378
|
-
while (coreMessages[0] && isToolCallMessage(coreMessages[0])) {
|
|
379
|
-
coreMessages.shift();
|
|
380
|
-
}
|
|
381
|
-
const messages = [...this.systemMessages, ...Object.values(this.taggedSystemMessages).flat(), ...coreMessages];
|
|
382
|
-
return messages;
|
|
383
|
-
}
|
|
384
|
-
};
|
|
385
|
-
remembered = {
|
|
386
|
-
v2: () => this.messages.filter((m) => this.memoryMessages.has(m)),
|
|
387
|
-
v1: () => convertToV1Messages(this.remembered.v2()),
|
|
388
|
-
ui: () => this.remembered.v2().map(_MessageList.toUIMessage),
|
|
389
|
-
core: () => this.convertToCoreMessages(this.remembered.ui())
|
|
390
|
-
};
|
|
391
|
-
input = {
|
|
392
|
-
v2: () => this.messages.filter((m) => this.newUserMessages.has(m)),
|
|
393
|
-
v1: () => convertToV1Messages(this.input.v2()),
|
|
394
|
-
ui: () => this.input.v2().map(_MessageList.toUIMessage),
|
|
395
|
-
core: () => this.convertToCoreMessages(this.input.ui())
|
|
396
|
-
};
|
|
397
|
-
response = {
|
|
398
|
-
v2: () => this.messages.filter((m) => this.newResponseMessages.has(m))
|
|
399
|
-
};
|
|
400
|
-
drainUnsavedMessages() {
|
|
401
|
-
const messages = this.messages.filter((m) => this.newUserMessages.has(m) || this.newResponseMessages.has(m));
|
|
402
|
-
this.newUserMessages.clear();
|
|
403
|
-
this.newResponseMessages.clear();
|
|
404
|
-
return messages;
|
|
405
|
-
}
|
|
406
|
-
getEarliestUnsavedMessageTimestamp() {
|
|
407
|
-
const unsavedMessages = this.messages.filter((m) => this.newUserMessages.has(m) || this.newResponseMessages.has(m));
|
|
408
|
-
if (unsavedMessages.length === 0) return void 0;
|
|
409
|
-
return Math.min(...unsavedMessages.map((m) => new Date(m.createdAt).getTime()));
|
|
410
|
-
}
|
|
411
|
-
getSystemMessages(tag) {
|
|
412
|
-
if (tag) {
|
|
413
|
-
return this.taggedSystemMessages[tag] || [];
|
|
414
|
-
}
|
|
415
|
-
return this.systemMessages;
|
|
416
|
-
}
|
|
417
|
-
addSystem(messages, tag) {
|
|
418
|
-
if (!messages) return this;
|
|
419
|
-
for (const message of Array.isArray(messages) ? messages : [messages]) {
|
|
420
|
-
this.addOneSystem(message, tag);
|
|
421
|
-
}
|
|
422
|
-
return this;
|
|
423
|
-
}
|
|
424
|
-
convertToCoreMessages(messages) {
|
|
425
|
-
return ai.convertToCoreMessages(this.sanitizeUIMessages(messages));
|
|
426
|
-
}
|
|
427
|
-
sanitizeUIMessages(messages) {
|
|
428
|
-
const msgs = messages.map((m) => {
|
|
429
|
-
if (m.parts.length === 0) return false;
|
|
430
|
-
const safeParts = m.parts.filter(
|
|
431
|
-
(p) => p.type !== `tool-invocation` || // calls and partial-calls should be updated to be results at this point
|
|
432
|
-
// if they haven't we can't send them back to the llm and need to remove them.
|
|
433
|
-
p.toolInvocation.state !== `call` && p.toolInvocation.state !== `partial-call`
|
|
434
|
-
);
|
|
435
|
-
if (!safeParts.length) return false;
|
|
436
|
-
const sanitized = {
|
|
437
|
-
...m,
|
|
438
|
-
parts: safeParts
|
|
439
|
-
};
|
|
440
|
-
if (`toolInvocations` in m && m.toolInvocations) {
|
|
441
|
-
sanitized.toolInvocations = m.toolInvocations.filter((t) => t.state === `result`);
|
|
442
|
-
}
|
|
443
|
-
return sanitized;
|
|
444
|
-
}).filter((m) => Boolean(m));
|
|
445
|
-
return msgs;
|
|
446
|
-
}
|
|
447
|
-
addOneSystem(message, tag) {
|
|
448
|
-
if (typeof message === `string`) message = { role: "system", content: message };
|
|
449
|
-
if (tag && !this.isDuplicateSystem(message, tag)) {
|
|
450
|
-
this.taggedSystemMessages[tag] ||= [];
|
|
451
|
-
this.taggedSystemMessages[tag].push(message);
|
|
452
|
-
} else if (!this.isDuplicateSystem(message)) {
|
|
453
|
-
this.systemMessages.push(message);
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
isDuplicateSystem(message, tag) {
|
|
457
|
-
if (tag) {
|
|
458
|
-
if (!this.taggedSystemMessages[tag]) return false;
|
|
459
|
-
return this.taggedSystemMessages[tag].some(
|
|
460
|
-
(m) => _MessageList.cacheKeyFromContent(m.content) === _MessageList.cacheKeyFromContent(message.content)
|
|
461
|
-
);
|
|
462
|
-
}
|
|
463
|
-
return this.systemMessages.some(
|
|
464
|
-
(m) => _MessageList.cacheKeyFromContent(m.content) === _MessageList.cacheKeyFromContent(message.content)
|
|
465
|
-
);
|
|
466
|
-
}
|
|
467
|
-
static toUIMessage(m) {
|
|
468
|
-
const experimentalAttachments = m.content.experimental_attachments ? [...m.content.experimental_attachments] : [];
|
|
469
|
-
const contentString = typeof m.content.content === `string` && m.content.content !== "" ? m.content.content : m.content.parts.reduce((prev, part) => {
|
|
470
|
-
if (part.type === `text`) {
|
|
471
|
-
return part.text;
|
|
472
|
-
}
|
|
473
|
-
return prev;
|
|
474
|
-
}, "");
|
|
475
|
-
const parts = [];
|
|
476
|
-
if (m.content.parts.length) {
|
|
477
|
-
for (const part of m.content.parts) {
|
|
478
|
-
if (part.type === `file`) {
|
|
479
|
-
experimentalAttachments.push({
|
|
480
|
-
contentType: part.mimeType,
|
|
481
|
-
url: part.data
|
|
482
|
-
});
|
|
483
|
-
} else if (part.type === "tool-invocation" && (part.toolInvocation.state === "call" || part.toolInvocation.state === "partial-call")) {
|
|
484
|
-
continue;
|
|
485
|
-
} else {
|
|
486
|
-
parts.push(part);
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
if (parts.length === 0 && experimentalAttachments.length > 0) {
|
|
491
|
-
parts.push({ type: "text", text: "" });
|
|
492
|
-
}
|
|
493
|
-
if (m.role === `user`) {
|
|
494
|
-
return {
|
|
495
|
-
id: m.id,
|
|
496
|
-
role: m.role,
|
|
497
|
-
content: m.content.content || contentString,
|
|
498
|
-
createdAt: m.createdAt,
|
|
499
|
-
parts,
|
|
500
|
-
experimental_attachments: experimentalAttachments
|
|
501
|
-
};
|
|
502
|
-
} else if (m.role === `assistant`) {
|
|
503
|
-
return {
|
|
504
|
-
id: m.id,
|
|
505
|
-
role: m.role,
|
|
506
|
-
content: m.content.content || contentString,
|
|
507
|
-
createdAt: m.createdAt,
|
|
508
|
-
parts,
|
|
509
|
-
reasoning: void 0,
|
|
510
|
-
toolInvocations: `toolInvocations` in m.content ? m.content.toolInvocations?.filter((t) => t.state === "result") : void 0
|
|
511
|
-
};
|
|
512
|
-
}
|
|
513
|
-
return {
|
|
514
|
-
id: m.id,
|
|
515
|
-
role: m.role,
|
|
516
|
-
content: m.content.content || contentString,
|
|
517
|
-
createdAt: m.createdAt,
|
|
518
|
-
parts,
|
|
519
|
-
experimental_attachments: experimentalAttachments
|
|
520
|
-
};
|
|
521
|
-
}
|
|
522
|
-
getMessageById(id) {
|
|
523
|
-
return this.messages.find((m) => m.id === id);
|
|
524
|
-
}
|
|
525
|
-
shouldReplaceMessage(message) {
|
|
526
|
-
if (!this.messages.length) return { exists: false };
|
|
527
|
-
if (!(`id` in message) || !message?.id) {
|
|
528
|
-
return { exists: false };
|
|
529
|
-
}
|
|
530
|
-
const existingMessage = this.getMessageById(message.id);
|
|
531
|
-
if (!existingMessage) return { exists: false };
|
|
532
|
-
return {
|
|
533
|
-
exists: true,
|
|
534
|
-
shouldReplace: !_MessageList.messagesAreEqual(existingMessage, message),
|
|
535
|
-
id: existingMessage.id
|
|
536
|
-
};
|
|
537
|
-
}
|
|
538
|
-
addOne(message, messageSource) {
|
|
539
|
-
if ((!(`content` in message) || !message.content && // allow empty strings
|
|
540
|
-
typeof message.content !== "string") && (!(`parts` in message) || !message.parts)) {
|
|
541
|
-
throw new chunkB6TOBUS6_cjs.MastraError({
|
|
542
|
-
id: "INVALID_MESSAGE_CONTENT",
|
|
543
|
-
domain: "AGENT" /* AGENT */,
|
|
544
|
-
category: "USER" /* USER */,
|
|
545
|
-
text: `Message with role "${message.role}" must have either a 'content' property (string or array) or a 'parts' property (array) that is not empty, null, or undefined. Received message: ${JSON.stringify(message, null, 2)}`,
|
|
546
|
-
details: {
|
|
547
|
-
role: message.role,
|
|
548
|
-
messageSource,
|
|
549
|
-
hasContent: "content" in message,
|
|
550
|
-
hasParts: "parts" in message
|
|
551
|
-
}
|
|
552
|
-
});
|
|
553
|
-
}
|
|
554
|
-
if (message.role === `system` && _MessageList.isVercelCoreMessage(message)) return this.addSystem(message);
|
|
555
|
-
if (message.role === `system`) {
|
|
556
|
-
throw new chunkB6TOBUS6_cjs.MastraError({
|
|
557
|
-
id: "INVALID_SYSTEM_MESSAGE_FORMAT",
|
|
558
|
-
domain: "AGENT" /* AGENT */,
|
|
559
|
-
category: "USER" /* USER */,
|
|
560
|
-
text: `Invalid system message format. System messages must be CoreMessage format with 'role' and 'content' properties. The content should be a string or valid content array.`,
|
|
561
|
-
details: {
|
|
562
|
-
messageSource,
|
|
563
|
-
receivedMessage: JSON.stringify(message, null, 2)
|
|
564
|
-
}
|
|
565
|
-
});
|
|
566
|
-
}
|
|
567
|
-
const messageV2 = this.inputToMastraMessageV2(message, messageSource);
|
|
568
|
-
const { exists, shouldReplace, id } = this.shouldReplaceMessage(messageV2);
|
|
569
|
-
const latestMessage = this.messages.at(-1);
|
|
570
|
-
const singleToolResult = messageV2.role === `assistant` && messageV2.content.parts.length === 1 && messageV2.content.parts[0]?.type === `tool-invocation` && messageV2.content.parts[0].toolInvocation.state === `result` && messageV2.content.parts[0];
|
|
571
|
-
if (singleToolResult && (latestMessage?.role !== `assistant` || !latestMessage.content.parts.some(
|
|
572
|
-
(p) => p.type === `tool-invocation` && p.toolInvocation.toolCallId === singleToolResult.toolInvocation.toolCallId
|
|
573
|
-
))) {
|
|
574
|
-
return;
|
|
575
|
-
}
|
|
576
|
-
if (messageSource === `memory`) {
|
|
577
|
-
for (const existingMessage of this.messages) {
|
|
578
|
-
if (_MessageList.messagesAreEqual(existingMessage, messageV2)) {
|
|
579
|
-
return;
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
const latestMessagePartType = latestMessage?.content?.parts?.filter((p) => p.type !== `step-start`)?.at?.(-1)?.type;
|
|
584
|
-
const newMessageFirstPartType = messageV2.content.parts.filter((p) => p.type !== `step-start`).at(0)?.type;
|
|
585
|
-
const shouldAppendToLastAssistantMessage = latestMessage?.role === "assistant" && messageV2.role === "assistant" && latestMessage.threadId === messageV2.threadId;
|
|
586
|
-
const shouldMergeNewMessages = latestMessage && !this.memoryMessages.has(latestMessage) && messageSource !== "memory";
|
|
587
|
-
const shouldAppendToLastAssistantMessageParts = shouldAppendToLastAssistantMessage && newMessageFirstPartType && (newMessageFirstPartType === `tool-invocation` && latestMessagePartType !== `text` || newMessageFirstPartType === latestMessagePartType && shouldMergeNewMessages);
|
|
588
|
-
if (
|
|
589
|
-
// backwards compat check!
|
|
590
|
-
// this condition can technically be removed and it will make it so all new assistant parts will be added to the last assistant message parts instead of creating new db entries.
|
|
591
|
-
// however, for any downstream code that isn't based around using message parts yet, this may cause tool invocations to show up in the wrong order in their UI, because they use the message.toolInvocations and message.content properties which do not indicate how each is ordered in relation to each other.
|
|
592
|
-
// this code check then causes any tool invocation to be created as a new message and not update the previous assistant message parts.
|
|
593
|
-
// without this condition we will see something like
|
|
594
|
-
// parts: [{type:"step-start"}, {type: "text", text: "let me check the weather"}, {type: "tool-invocation", toolInvocation: x}, {type: "text", text: "the weather in x is y"}]
|
|
595
|
-
// with this condition we will see
|
|
596
|
-
// message1.parts: [{type:"step-start"}, {type: "text", text: "let me check the weather"}]
|
|
597
|
-
// message2.parts: [{type: "tool-invocation", toolInvocation: x}]
|
|
598
|
-
// message3.parts: [{type: "text", text: "the weather in x is y"}]
|
|
599
|
-
shouldAppendToLastAssistantMessageParts
|
|
600
|
-
) {
|
|
601
|
-
latestMessage.createdAt = messageV2.createdAt || latestMessage.createdAt;
|
|
602
|
-
for (const [index, part] of messageV2.content.parts.entries()) {
|
|
603
|
-
if (part.type === "tool-invocation" && part.toolInvocation.state === "result") {
|
|
604
|
-
const existingCallPart = [...latestMessage.content.parts].reverse().find((p) => p.type === "tool-invocation" && p.toolInvocation.toolCallId === part.toolInvocation.toolCallId);
|
|
605
|
-
if (existingCallPart && existingCallPart.type === "tool-invocation") {
|
|
606
|
-
existingCallPart.toolInvocation = {
|
|
607
|
-
...existingCallPart.toolInvocation,
|
|
608
|
-
state: "result",
|
|
609
|
-
result: part.toolInvocation.result
|
|
610
|
-
};
|
|
611
|
-
if (!latestMessage.content.toolInvocations) {
|
|
612
|
-
latestMessage.content.toolInvocations = [];
|
|
613
|
-
}
|
|
614
|
-
if (!latestMessage.content.toolInvocations.some(
|
|
615
|
-
(t) => t.toolCallId === existingCallPart.toolInvocation.toolCallId
|
|
616
|
-
)) {
|
|
617
|
-
latestMessage.content.toolInvocations.push(existingCallPart.toolInvocation);
|
|
618
|
-
}
|
|
619
|
-
}
|
|
620
|
-
} else if (
|
|
621
|
-
// if there's no part at this index yet in the existing message we're merging into
|
|
622
|
-
!latestMessage.content.parts[index] || // or there is and the parts are not identical
|
|
623
|
-
_MessageList.cacheKeyFromParts([latestMessage.content.parts[index]]) !== _MessageList.cacheKeyFromParts([part])
|
|
624
|
-
) {
|
|
625
|
-
latestMessage.content.parts.push(part);
|
|
626
|
-
}
|
|
627
|
-
}
|
|
628
|
-
if (latestMessage.createdAt.getTime() < messageV2.createdAt.getTime()) {
|
|
629
|
-
latestMessage.createdAt = messageV2.createdAt;
|
|
630
|
-
}
|
|
631
|
-
if (!latestMessage.content.content && messageV2.content.content) {
|
|
632
|
-
latestMessage.content.content = messageV2.content.content;
|
|
633
|
-
}
|
|
634
|
-
if (latestMessage.content.content && messageV2.content.content && latestMessage.content.content !== messageV2.content.content) {
|
|
635
|
-
latestMessage.content.content = messageV2.content.content;
|
|
636
|
-
}
|
|
637
|
-
} else {
|
|
638
|
-
if (messageV2.role === "assistant" && messageV2.content.parts[0]?.type !== `step-start`) {
|
|
639
|
-
messageV2.content.parts.unshift({ type: "step-start" });
|
|
640
|
-
}
|
|
641
|
-
const existingIndex = shouldReplace && this.messages.findIndex((m) => m.id === id) || -1;
|
|
642
|
-
const existingMessage = existingIndex !== -1 && this.messages[existingIndex];
|
|
643
|
-
if (shouldReplace && existingMessage) {
|
|
644
|
-
this.messages[existingIndex] = messageV2;
|
|
645
|
-
} else if (!exists) {
|
|
646
|
-
this.messages.push(messageV2);
|
|
647
|
-
}
|
|
648
|
-
if (messageSource === `memory`) {
|
|
649
|
-
this.memoryMessages.add(messageV2);
|
|
650
|
-
} else if (messageSource === `response`) {
|
|
651
|
-
this.newResponseMessages.add(messageV2);
|
|
652
|
-
} else if (messageSource === `user`) {
|
|
653
|
-
this.newUserMessages.add(messageV2);
|
|
654
|
-
} else if (messageSource === `context`) {
|
|
655
|
-
this.userContextMessages.add(messageV2);
|
|
656
|
-
} else {
|
|
657
|
-
throw new Error(`Missing message source for message ${messageV2}`);
|
|
658
|
-
}
|
|
659
|
-
}
|
|
660
|
-
this.messages.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime());
|
|
661
|
-
return this;
|
|
662
|
-
}
|
|
663
|
-
inputToMastraMessageV2(message, messageSource) {
|
|
664
|
-
if (
|
|
665
|
-
// we can't throw if the threadId doesn't match and this message came from memory
|
|
666
|
-
// this is because per-user semantic recall can retrieve messages from other threads
|
|
667
|
-
messageSource !== `memory` && `threadId` in message && message.threadId && this.memoryInfo && message.threadId !== this.memoryInfo.threadId
|
|
668
|
-
) {
|
|
669
|
-
throw new Error(
|
|
670
|
-
`Received input message with wrong threadId. Input ${message.threadId}, expected ${this.memoryInfo.threadId}`
|
|
671
|
-
);
|
|
672
|
-
}
|
|
673
|
-
if (`resourceId` in message && message.resourceId && this.memoryInfo?.resourceId && message.resourceId !== this.memoryInfo.resourceId) {
|
|
674
|
-
throw new Error(
|
|
675
|
-
`Received input message with wrong resourceId. Input ${message.resourceId}, expected ${this.memoryInfo.resourceId}`
|
|
676
|
-
);
|
|
677
|
-
}
|
|
678
|
-
if (_MessageList.isMastraMessageV1(message)) {
|
|
679
|
-
return this.mastraMessageV1ToMastraMessageV2(message, messageSource);
|
|
680
|
-
}
|
|
681
|
-
if (_MessageList.isMastraMessageV2(message)) {
|
|
682
|
-
return this.hydrateMastraMessageV2Fields(message);
|
|
683
|
-
}
|
|
684
|
-
if (_MessageList.isVercelCoreMessage(message)) {
|
|
685
|
-
return this.vercelCoreMessageToMastraMessageV2(message, messageSource);
|
|
686
|
-
}
|
|
687
|
-
if (_MessageList.isVercelUIMessage(message)) {
|
|
688
|
-
return this.vercelUIMessageToMastraMessageV2(message, messageSource);
|
|
689
|
-
}
|
|
690
|
-
throw new Error(`Found unhandled message ${JSON.stringify(message)}`);
|
|
691
|
-
}
|
|
692
|
-
lastCreatedAt;
|
|
693
|
-
// this makes sure messages added in order will always have a date atleast 1ms apart.
|
|
694
|
-
generateCreatedAt(messageSource, start) {
|
|
695
|
-
start = start instanceof Date ? start : start ? new Date(start) : void 0;
|
|
696
|
-
if (start && !this.lastCreatedAt) {
|
|
697
|
-
this.lastCreatedAt = start.getTime();
|
|
698
|
-
return start;
|
|
699
|
-
}
|
|
700
|
-
if (start && messageSource === `memory`) {
|
|
701
|
-
return start;
|
|
702
|
-
}
|
|
703
|
-
const now = /* @__PURE__ */ new Date();
|
|
704
|
-
const nowTime = start?.getTime() || now.getTime();
|
|
705
|
-
const lastTime = this.messages.reduce((p, m) => {
|
|
706
|
-
if (m.createdAt.getTime() > p) return m.createdAt.getTime();
|
|
707
|
-
return p;
|
|
708
|
-
}, this.lastCreatedAt || 0);
|
|
709
|
-
if (nowTime <= lastTime) {
|
|
710
|
-
const newDate = new Date(lastTime + 1);
|
|
711
|
-
this.lastCreatedAt = newDate.getTime();
|
|
712
|
-
return newDate;
|
|
713
|
-
}
|
|
714
|
-
this.lastCreatedAt = nowTime;
|
|
715
|
-
return now;
|
|
716
|
-
}
|
|
717
|
-
newMessageId() {
|
|
718
|
-
if (this.generateMessageId) {
|
|
719
|
-
return this.generateMessageId();
|
|
720
|
-
}
|
|
721
|
-
return crypto.randomUUID();
|
|
722
|
-
}
|
|
723
|
-
mastraMessageV1ToMastraMessageV2(message, messageSource) {
|
|
724
|
-
const coreV2 = this.vercelCoreMessageToMastraMessageV2(
|
|
725
|
-
{
|
|
726
|
-
content: message.content,
|
|
727
|
-
role: message.role
|
|
728
|
-
},
|
|
729
|
-
messageSource
|
|
730
|
-
);
|
|
731
|
-
return {
|
|
732
|
-
id: message.id,
|
|
733
|
-
role: coreV2.role,
|
|
734
|
-
createdAt: this.generateCreatedAt(messageSource, message.createdAt),
|
|
735
|
-
threadId: message.threadId,
|
|
736
|
-
resourceId: message.resourceId,
|
|
737
|
-
content: coreV2.content
|
|
738
|
-
};
|
|
739
|
-
}
|
|
740
|
-
hydrateMastraMessageV2Fields(message) {
|
|
741
|
-
if (!(message.createdAt instanceof Date)) message.createdAt = new Date(message.createdAt);
|
|
742
|
-
return message;
|
|
743
|
-
}
|
|
744
|
-
vercelUIMessageToMastraMessageV2(message, messageSource) {
|
|
745
|
-
const content = {
|
|
746
|
-
format: 2,
|
|
747
|
-
parts: message.parts
|
|
748
|
-
};
|
|
749
|
-
if (message.toolInvocations) content.toolInvocations = message.toolInvocations;
|
|
750
|
-
if (message.reasoning) content.reasoning = message.reasoning;
|
|
751
|
-
if (message.annotations) content.annotations = message.annotations;
|
|
752
|
-
if (message.experimental_attachments) {
|
|
753
|
-
content.experimental_attachments = message.experimental_attachments;
|
|
754
|
-
}
|
|
755
|
-
return {
|
|
756
|
-
id: message.id || this.newMessageId(),
|
|
757
|
-
role: _MessageList.getRole(message),
|
|
758
|
-
createdAt: this.generateCreatedAt(messageSource, message.createdAt),
|
|
759
|
-
threadId: this.memoryInfo?.threadId,
|
|
760
|
-
resourceId: this.memoryInfo?.resourceId,
|
|
761
|
-
content
|
|
762
|
-
};
|
|
763
|
-
}
|
|
764
|
-
vercelCoreMessageToMastraMessageV2(coreMessage, messageSource) {
|
|
765
|
-
const id = `id` in coreMessage ? coreMessage.id : this.newMessageId();
|
|
766
|
-
const parts = [];
|
|
767
|
-
const experimentalAttachments = [];
|
|
768
|
-
const toolInvocations = [];
|
|
769
|
-
if (typeof coreMessage.content === "string") {
|
|
770
|
-
parts.push({ type: "step-start" });
|
|
771
|
-
parts.push({
|
|
772
|
-
type: "text",
|
|
773
|
-
text: coreMessage.content
|
|
774
|
-
});
|
|
775
|
-
} else if (Array.isArray(coreMessage.content)) {
|
|
776
|
-
for (const part of coreMessage.content) {
|
|
777
|
-
switch (part.type) {
|
|
778
|
-
case "text":
|
|
779
|
-
parts.push({
|
|
780
|
-
type: "text",
|
|
781
|
-
text: part.text
|
|
782
|
-
});
|
|
783
|
-
break;
|
|
784
|
-
case "tool-call":
|
|
785
|
-
parts.push({
|
|
786
|
-
type: "tool-invocation",
|
|
787
|
-
toolInvocation: {
|
|
788
|
-
state: "call",
|
|
789
|
-
toolCallId: part.toolCallId,
|
|
790
|
-
toolName: part.toolName,
|
|
791
|
-
args: part.args
|
|
792
|
-
}
|
|
793
|
-
});
|
|
794
|
-
break;
|
|
795
|
-
case "tool-result":
|
|
796
|
-
const invocation = {
|
|
797
|
-
state: "result",
|
|
798
|
-
toolCallId: part.toolCallId,
|
|
799
|
-
toolName: part.toolName,
|
|
800
|
-
result: part.result ?? "",
|
|
801
|
-
// undefined will cause AI SDK to throw an error, but for client side tool calls this really could be undefined
|
|
802
|
-
args: {}
|
|
803
|
-
// when we combine this invocation onto the existing tool-call part it will have args already
|
|
804
|
-
};
|
|
805
|
-
parts.push({
|
|
806
|
-
type: "tool-invocation",
|
|
807
|
-
toolInvocation: invocation
|
|
808
|
-
});
|
|
809
|
-
toolInvocations.push(invocation);
|
|
810
|
-
break;
|
|
811
|
-
case "reasoning":
|
|
812
|
-
parts.push({
|
|
813
|
-
type: "reasoning",
|
|
814
|
-
reasoning: "",
|
|
815
|
-
// leave this blank so we aren't double storing it in the db along with details
|
|
816
|
-
details: [{ type: "text", text: part.text, signature: part.signature }]
|
|
817
|
-
});
|
|
818
|
-
break;
|
|
819
|
-
case "redacted-reasoning":
|
|
820
|
-
parts.push({
|
|
821
|
-
type: "reasoning",
|
|
822
|
-
reasoning: "",
|
|
823
|
-
// No text reasoning for redacted parts
|
|
824
|
-
details: [{ type: "redacted", data: part.data }]
|
|
825
|
-
});
|
|
826
|
-
break;
|
|
827
|
-
case "image":
|
|
828
|
-
parts.push({ type: "file", data: part.image.toString(), mimeType: part.mimeType });
|
|
829
|
-
break;
|
|
830
|
-
case "file":
|
|
831
|
-
if (part.data instanceof URL) {
|
|
832
|
-
parts.push({
|
|
833
|
-
type: "file",
|
|
834
|
-
data: part.data.toString(),
|
|
835
|
-
mimeType: part.mimeType
|
|
836
|
-
});
|
|
837
|
-
} else {
|
|
838
|
-
try {
|
|
839
|
-
parts.push({
|
|
840
|
-
type: "file",
|
|
841
|
-
mimeType: part.mimeType,
|
|
842
|
-
data: convertDataContentToBase64String(part.data)
|
|
843
|
-
});
|
|
844
|
-
} catch (error) {
|
|
845
|
-
console.error(`Failed to convert binary data to base64 in CoreMessage file part: ${error}`, error);
|
|
846
|
-
}
|
|
847
|
-
}
|
|
848
|
-
break;
|
|
849
|
-
}
|
|
850
|
-
}
|
|
851
|
-
}
|
|
852
|
-
const content = {
|
|
853
|
-
format: 2,
|
|
854
|
-
parts
|
|
855
|
-
};
|
|
856
|
-
if (toolInvocations.length) content.toolInvocations = toolInvocations;
|
|
857
|
-
if (typeof coreMessage.content === `string`) content.content = coreMessage.content;
|
|
858
|
-
if (experimentalAttachments.length) content.experimental_attachments = experimentalAttachments;
|
|
859
|
-
return {
|
|
860
|
-
id,
|
|
861
|
-
role: _MessageList.getRole(coreMessage),
|
|
862
|
-
createdAt: this.generateCreatedAt(messageSource),
|
|
863
|
-
threadId: this.memoryInfo?.threadId,
|
|
864
|
-
resourceId: this.memoryInfo?.resourceId,
|
|
865
|
-
content
|
|
866
|
-
};
|
|
867
|
-
}
|
|
868
|
-
static isVercelUIMessage(msg) {
|
|
869
|
-
return !_MessageList.isMastraMessage(msg) && chunk7F6BQXE2_cjs.isUiMessage(msg);
|
|
870
|
-
}
|
|
871
|
-
static isVercelCoreMessage(msg) {
|
|
872
|
-
return !_MessageList.isMastraMessage(msg) && chunk7F6BQXE2_cjs.isCoreMessage(msg);
|
|
873
|
-
}
|
|
874
|
-
static isMastraMessage(msg) {
|
|
875
|
-
return _MessageList.isMastraMessageV2(msg) || _MessageList.isMastraMessageV1(msg);
|
|
876
|
-
}
|
|
877
|
-
static isMastraMessageV1(msg) {
|
|
878
|
-
return !_MessageList.isMastraMessageV2(msg) && (`threadId` in msg || `resourceId` in msg);
|
|
879
|
-
}
|
|
880
|
-
static isMastraMessageV2(msg) {
|
|
881
|
-
return Boolean(
|
|
882
|
-
msg.content && !Array.isArray(msg.content) && typeof msg.content !== `string` && // any newly saved Mastra message v2 shape will have content: { format: 2 }
|
|
883
|
-
`format` in msg.content && msg.content.format === 2
|
|
884
|
-
);
|
|
885
|
-
}
|
|
886
|
-
static getRole(message) {
|
|
887
|
-
if (message.role === `assistant` || message.role === `tool`) return `assistant`;
|
|
888
|
-
if (message.role === `user`) return `user`;
|
|
889
|
-
throw new Error(
|
|
890
|
-
`BUG: add handling for message role ${message.role} in message ${JSON.stringify(message, null, 2)}`
|
|
891
|
-
);
|
|
892
|
-
}
|
|
893
|
-
static cacheKeyFromParts(parts) {
|
|
894
|
-
let key = ``;
|
|
895
|
-
for (const part of parts) {
|
|
896
|
-
key += part.type;
|
|
897
|
-
if (part.type === `text`) {
|
|
898
|
-
key += part.text.length;
|
|
899
|
-
}
|
|
900
|
-
if (part.type === `tool-invocation`) {
|
|
901
|
-
key += part.toolInvocation.toolCallId;
|
|
902
|
-
key += part.toolInvocation.state;
|
|
903
|
-
}
|
|
904
|
-
if (part.type === `reasoning`) {
|
|
905
|
-
key += part.reasoning.length;
|
|
906
|
-
key += part.details.reduce((prev, current) => {
|
|
907
|
-
if (current.type === `text`) {
|
|
908
|
-
return prev + current.text.length + (current.signature?.length || 0);
|
|
909
|
-
}
|
|
910
|
-
return prev;
|
|
911
|
-
}, 0);
|
|
912
|
-
}
|
|
913
|
-
if (part.type === `file`) {
|
|
914
|
-
key += part.data.length;
|
|
915
|
-
key += part.mimeType;
|
|
916
|
-
}
|
|
917
|
-
}
|
|
918
|
-
return key;
|
|
919
|
-
}
|
|
920
|
-
static coreContentToString(content) {
|
|
921
|
-
if (typeof content === `string`) return content;
|
|
922
|
-
return content.reduce((p, c) => {
|
|
923
|
-
if (c.type === `text`) {
|
|
924
|
-
p += c.text;
|
|
925
|
-
}
|
|
926
|
-
return p;
|
|
927
|
-
}, "");
|
|
928
|
-
}
|
|
929
|
-
static cacheKeyFromContent(content) {
|
|
930
|
-
if (typeof content === `string`) return content;
|
|
931
|
-
let key = ``;
|
|
932
|
-
for (const part of content) {
|
|
933
|
-
key += part.type;
|
|
934
|
-
if (part.type === `text`) {
|
|
935
|
-
key += part.text.length;
|
|
936
|
-
}
|
|
937
|
-
if (part.type === `reasoning`) {
|
|
938
|
-
key += part.text.length;
|
|
939
|
-
}
|
|
940
|
-
if (part.type === `tool-call`) {
|
|
941
|
-
key += part.toolCallId;
|
|
942
|
-
key += part.toolName;
|
|
943
|
-
}
|
|
944
|
-
if (part.type === `tool-result`) {
|
|
945
|
-
key += part.toolCallId;
|
|
946
|
-
key += part.toolName;
|
|
947
|
-
}
|
|
948
|
-
if (part.type === `file`) {
|
|
949
|
-
key += part.filename;
|
|
950
|
-
key += part.mimeType;
|
|
951
|
-
}
|
|
952
|
-
if (part.type === `image`) {
|
|
953
|
-
key += part.image instanceof URL ? part.image.toString() : part.image.toString().length;
|
|
954
|
-
key += part.mimeType;
|
|
955
|
-
}
|
|
956
|
-
if (part.type === `redacted-reasoning`) {
|
|
957
|
-
key += part.data.length;
|
|
958
|
-
}
|
|
959
|
-
}
|
|
960
|
-
return key;
|
|
961
|
-
}
|
|
962
|
-
static messagesAreEqual(one, two) {
|
|
963
|
-
const oneUI = _MessageList.isVercelUIMessage(one) && one;
|
|
964
|
-
const twoUI = _MessageList.isVercelUIMessage(two) && two;
|
|
965
|
-
if (oneUI && !twoUI) return false;
|
|
966
|
-
if (oneUI && twoUI) {
|
|
967
|
-
return _MessageList.cacheKeyFromParts(one.parts) === _MessageList.cacheKeyFromParts(two.parts);
|
|
968
|
-
}
|
|
969
|
-
const oneCM = _MessageList.isVercelCoreMessage(one) && one;
|
|
970
|
-
const twoCM = _MessageList.isVercelCoreMessage(two) && two;
|
|
971
|
-
if (oneCM && !twoCM) return false;
|
|
972
|
-
if (oneCM && twoCM) {
|
|
973
|
-
return _MessageList.cacheKeyFromContent(oneCM.content) === _MessageList.cacheKeyFromContent(twoCM.content);
|
|
974
|
-
}
|
|
975
|
-
const oneMM1 = _MessageList.isMastraMessageV1(one) && one;
|
|
976
|
-
const twoMM1 = _MessageList.isMastraMessageV1(two) && two;
|
|
977
|
-
if (oneMM1 && !twoMM1) return false;
|
|
978
|
-
if (oneMM1 && twoMM1) {
|
|
979
|
-
return oneMM1.id === twoMM1.id && _MessageList.cacheKeyFromContent(oneMM1.content) === _MessageList.cacheKeyFromContent(twoMM1.content);
|
|
980
|
-
}
|
|
981
|
-
const oneMM2 = _MessageList.isMastraMessageV2(one) && one;
|
|
982
|
-
const twoMM2 = _MessageList.isMastraMessageV2(two) && two;
|
|
983
|
-
if (oneMM2 && !twoMM2) return false;
|
|
984
|
-
if (oneMM2 && twoMM2) {
|
|
985
|
-
return oneMM2.id === twoMM2.id && _MessageList.cacheKeyFromParts(oneMM2.content.parts) === _MessageList.cacheKeyFromParts(twoMM2.content.parts);
|
|
986
|
-
}
|
|
987
|
-
return true;
|
|
988
|
-
}
|
|
989
|
-
};
|
|
990
|
-
|
|
991
|
-
exports.MessageList = MessageList;
|