@mastra/server 0.24.0 → 1.0.0-beta.1
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 +308 -57
- package/README.md +1 -1
- package/dist/{chunk-OQPXME7E.js → chunk-2IS5WICF.js} +4569 -1062
- package/dist/chunk-2IS5WICF.js.map +1 -0
- package/dist/{chunk-SPRRK3P7.cjs → chunk-2YZH5PH6.cjs} +47 -47
- package/dist/chunk-2YZH5PH6.cjs.map +1 -0
- package/dist/{chunk-BATEJLED.js → chunk-3AMNUUZF.js} +20 -20
- package/dist/chunk-3AMNUUZF.js.map +1 -0
- package/dist/{chunk-AW5BU3RQ.js → chunk-3F52QCI4.js} +40 -40
- package/dist/chunk-3F52QCI4.js.map +1 -0
- package/dist/{chunk-PBXWFGEH.js → chunk-4JF5WXPL.js} +130 -207
- package/dist/chunk-4JF5WXPL.js.map +1 -0
- package/dist/{chunk-7NADHFD2.cjs → chunk-64ITUOXI.cjs} +2 -2
- package/dist/chunk-64ITUOXI.cjs.map +1 -0
- package/dist/{chunk-MMROOK5J.js → chunk-6QWQZI4Q.js} +2 -2
- package/dist/{chunk-7NADHFD2.cjs.map → chunk-6QWQZI4Q.js.map} +1 -1
- package/dist/{chunk-ABRFV4XP.js → chunk-73PAWDM5.js} +13 -13
- package/dist/chunk-73PAWDM5.js.map +1 -0
- package/dist/{chunk-2S4IMB6E.cjs → chunk-A24TSVEZ.cjs} +39 -39
- package/dist/chunk-A24TSVEZ.cjs.map +1 -0
- package/dist/{chunk-NT5XW5PI.cjs → chunk-A2NPD5N6.cjs} +176 -255
- package/dist/chunk-A2NPD5N6.cjs.map +1 -0
- package/dist/{chunk-QQXMIP6C.js → chunk-B3Z6J745.js} +17 -17
- package/dist/chunk-B3Z6J745.js.map +1 -0
- package/dist/{chunk-G4PUALCE.cjs → chunk-BTWIR2B7.cjs} +4 -4
- package/dist/{chunk-G4PUALCE.cjs.map → chunk-BTWIR2B7.cjs.map} +1 -1
- package/dist/{chunk-YWOK4F5A.js → chunk-D7T74TVR.js} +4 -6
- package/dist/chunk-D7T74TVR.js.map +1 -0
- package/dist/{chunk-IJSDPAUY.cjs → chunk-EHACNWDL.cjs} +156 -202
- package/dist/chunk-EHACNWDL.cjs.map +1 -0
- package/dist/{chunk-HPXAM2PG.js → chunk-FPBYKMIS.js} +13 -13
- package/dist/chunk-FPBYKMIS.js.map +1 -0
- package/dist/{chunk-3SNGNFUJ.cjs → chunk-GLAZTMX3.cjs} +37 -37
- package/dist/chunk-GLAZTMX3.cjs.map +1 -0
- package/dist/{chunk-TEOUDAN5.cjs → chunk-ID6JYDNL.cjs} +4590 -1080
- package/dist/chunk-ID6JYDNL.cjs.map +1 -0
- package/dist/{chunk-TRGAMKHX.cjs → chunk-KF3RI45U.cjs} +46 -41
- package/dist/chunk-KF3RI45U.cjs.map +1 -0
- package/dist/{chunk-TOP25AIO.cjs → chunk-KJIDZQRA.cjs} +14 -14
- package/dist/chunk-KJIDZQRA.cjs.map +1 -0
- package/dist/{chunk-MYR4PVGN.js → chunk-KWH5QBXP.js} +4 -4
- package/dist/{chunk-MYR4PVGN.js.map → chunk-KWH5QBXP.js.map} +1 -1
- package/dist/{chunk-AHB4JCIQ.js → chunk-ND5OKOMT.js} +31 -26
- package/dist/chunk-ND5OKOMT.js.map +1 -0
- package/dist/{chunk-XN74I6VW.cjs → chunk-PPMIB3FQ.cjs} +20 -20
- package/dist/chunk-PPMIB3FQ.cjs.map +1 -0
- package/dist/{chunk-EP3JQDPD.cjs → chunk-S54HNARD.cjs} +18 -18
- package/dist/{chunk-EP3JQDPD.cjs.map → chunk-S54HNARD.cjs.map} +1 -1
- package/dist/{chunk-4QCXUEAT.js → chunk-SZIFSF4T.js} +3 -3
- package/dist/{chunk-4QCXUEAT.js.map → chunk-SZIFSF4T.js.map} +1 -1
- package/dist/{chunk-CY4TP3FK.js → chunk-UXGQZUYZ.js} +3 -3
- package/dist/{chunk-CY4TP3FK.js.map → chunk-UXGQZUYZ.js.map} +1 -1
- package/dist/{chunk-RE4RPXT2.cjs → chunk-V5WWQN7P.cjs} +4 -4
- package/dist/{chunk-RE4RPXT2.cjs.map → chunk-V5WWQN7P.cjs.map} +1 -1
- package/dist/{chunk-VY4ENABS.cjs → chunk-W2KMU354.cjs} +4 -6
- package/dist/chunk-W2KMU354.cjs.map +1 -0
- package/dist/{chunk-H33KYEMY.cjs → chunk-X3MICMI2.cjs} +140 -240
- package/dist/chunk-X3MICMI2.cjs.map +1 -0
- package/dist/{chunk-Y4VKB6KC.js → chunk-Z2O5YVHY.js} +121 -165
- package/dist/chunk-Z2O5YVHY.js.map +1 -0
- package/dist/{chunk-PY2K7VNW.js → chunk-ZJ6KEY6H.js} +67 -165
- package/dist/chunk-ZJ6KEY6H.js.map +1 -0
- package/dist/server/handlers/a2a.cjs +7 -7
- package/dist/server/handlers/a2a.d.ts +10 -10
- package/dist/server/handlers/a2a.d.ts.map +1 -1
- package/dist/server/handlers/a2a.js +1 -1
- package/dist/server/handlers/agent-builder.cjs +31 -19
- package/dist/server/handlers/agent-builder.d.ts +38 -47
- package/dist/server/handlers/agent-builder.d.ts.map +1 -1
- package/dist/server/handlers/agent-builder.js +1 -1
- package/dist/server/handlers/agents.cjs +22 -30
- package/dist/server/handlers/agents.d.ts +36 -55
- package/dist/server/handlers/agents.d.ts.map +1 -1
- package/dist/server/handlers/agents.js +1 -1
- package/dist/server/handlers/error.cjs +2 -2
- package/dist/server/handlers/error.js +1 -1
- package/dist/server/handlers/logs.cjs +7 -7
- package/dist/server/handlers/logs.d.ts +3 -3
- package/dist/server/handlers/logs.d.ts.map +1 -1
- package/dist/server/handlers/logs.js +1 -1
- package/dist/server/handlers/memory.cjs +17 -25
- package/dist/server/handlers/memory.d.ts +28 -34
- package/dist/server/handlers/memory.d.ts.map +1 -1
- package/dist/server/handlers/memory.js +1 -1
- package/dist/server/handlers/observability.cjs +8 -8
- package/dist/server/handlers/observability.d.ts +12 -12
- package/dist/server/handlers/observability.d.ts.map +1 -1
- package/dist/server/handlers/observability.js +1 -1
- package/dist/server/handlers/scores.cjs +11 -11
- package/dist/server/handlers/scores.d.ts +24 -24
- package/dist/server/handlers/scores.d.ts.map +1 -1
- package/dist/server/handlers/scores.js +1 -1
- package/dist/server/handlers/tools.cjs +7 -7
- package/dist/server/handlers/tools.d.ts +8 -8
- package/dist/server/handlers/tools.d.ts.map +1 -1
- package/dist/server/handlers/tools.js +1 -1
- package/dist/server/handlers/utils.cjs +3 -3
- package/dist/server/handlers/utils.js +1 -1
- package/dist/server/handlers/vector.cjs +7 -7
- package/dist/server/handlers/vector.js +1 -1
- package/dist/server/handlers/voice.cjs +5 -5
- package/dist/server/handlers/voice.d.ts +6 -6
- package/dist/server/handlers/voice.js +1 -1
- package/dist/server/handlers/workflows.cjs +21 -29
- package/dist/server/handlers/workflows.d.ts +27 -36
- package/dist/server/handlers/workflows.d.ts.map +1 -1
- package/dist/server/handlers/workflows.js +1 -1
- package/dist/server/handlers.cjs +26 -36
- package/dist/server/handlers.d.ts +0 -2
- package/dist/server/handlers.d.ts.map +1 -1
- package/dist/server/handlers.js +11 -13
- package/dist/server/http-exception.d.ts +0 -5
- package/dist/server/http-exception.d.ts.map +1 -1
- package/dist/server/utils.d.ts +3 -2
- package/dist/server/utils.d.ts.map +1 -1
- package/package.json +9 -13
- package/dist/chunk-2S4IMB6E.cjs.map +0 -1
- package/dist/chunk-3SNGNFUJ.cjs.map +0 -1
- package/dist/chunk-67GYDFSB.js +0 -160
- package/dist/chunk-67GYDFSB.js.map +0 -1
- package/dist/chunk-6ZR275MD.cjs +0 -165
- package/dist/chunk-6ZR275MD.cjs.map +0 -1
- package/dist/chunk-ABRFV4XP.js.map +0 -1
- package/dist/chunk-AHB4JCIQ.js.map +0 -1
- package/dist/chunk-AW5BU3RQ.js.map +0 -1
- package/dist/chunk-BATEJLED.js.map +0 -1
- package/dist/chunk-H33KYEMY.cjs.map +0 -1
- package/dist/chunk-HPXAM2PG.js.map +0 -1
- package/dist/chunk-I3C4ODGV.cjs +0 -335
- package/dist/chunk-I3C4ODGV.cjs.map +0 -1
- package/dist/chunk-IJSDPAUY.cjs.map +0 -1
- package/dist/chunk-LLUOPR3J.js +0 -323
- package/dist/chunk-LLUOPR3J.js.map +0 -1
- package/dist/chunk-MMROOK5J.js.map +0 -1
- package/dist/chunk-NT5XW5PI.cjs.map +0 -1
- package/dist/chunk-OQPXME7E.js.map +0 -1
- package/dist/chunk-PBXWFGEH.js.map +0 -1
- package/dist/chunk-PY2K7VNW.js.map +0 -1
- package/dist/chunk-QQXMIP6C.js.map +0 -1
- package/dist/chunk-SPRRK3P7.cjs.map +0 -1
- package/dist/chunk-TEOUDAN5.cjs.map +0 -1
- package/dist/chunk-TOP25AIO.cjs.map +0 -1
- package/dist/chunk-TRGAMKHX.cjs.map +0 -1
- package/dist/chunk-VY4ENABS.cjs.map +0 -1
- package/dist/chunk-XN74I6VW.cjs.map +0 -1
- package/dist/chunk-Y4VKB6KC.js.map +0 -1
- package/dist/chunk-YWOK4F5A.js.map +0 -1
- package/dist/server/handlers/legacyWorkflows.cjs +0 -48
- package/dist/server/handlers/legacyWorkflows.cjs.map +0 -1
- package/dist/server/handlers/legacyWorkflows.d.ts +0 -59
- package/dist/server/handlers/legacyWorkflows.d.ts.map +0 -1
- package/dist/server/handlers/legacyWorkflows.js +0 -3
- package/dist/server/handlers/legacyWorkflows.js.map +0 -1
- package/dist/server/handlers/telemetry.cjs +0 -20
- package/dist/server/handlers/telemetry.cjs.map +0 -1
- package/dist/server/handlers/telemetry.d.ts +0 -33
- package/dist/server/handlers/telemetry.d.ts.map +0 -1
- package/dist/server/handlers/telemetry.js +0 -3
- package/dist/server/handlers/telemetry.js.map +0 -1
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { validateBody } from './chunk-
|
|
2
|
-
import { handleError } from './chunk-
|
|
3
|
-
import { HTTPException } from './chunk-
|
|
1
|
+
import { validateBody } from './chunk-SZIFSF4T.js';
|
|
2
|
+
import { handleError } from './chunk-UXGQZUYZ.js';
|
|
3
|
+
import { HTTPException } from './chunk-6QWQZI4Q.js';
|
|
4
4
|
import { __export } from './chunk-PR4QN5HX.js';
|
|
5
|
-
import {
|
|
6
|
-
import { RuntimeContext } from '@mastra/core/di';
|
|
5
|
+
import { RequestContext } from '@mastra/core/di';
|
|
7
6
|
import { generateEmptyFromSchema } from '@mastra/core/utils';
|
|
8
7
|
|
|
9
8
|
// src/server/handlers/memory.ts
|
|
@@ -14,12 +13,10 @@ __export(memory_exports, {
|
|
|
14
13
|
deleteThreadHandler: () => deleteThreadHandler,
|
|
15
14
|
getMemoryConfigHandler: () => getMemoryConfigHandler,
|
|
16
15
|
getMemoryStatusHandler: () => getMemoryStatusHandler,
|
|
17
|
-
getMessagesHandler: () => getMessagesHandler,
|
|
18
|
-
getMessagesPaginatedHandler: () => getMessagesPaginatedHandler,
|
|
19
16
|
getThreadByIdHandler: () => getThreadByIdHandler,
|
|
20
|
-
getThreadsHandler: () => getThreadsHandler,
|
|
21
|
-
getThreadsPaginatedHandler: () => getThreadsPaginatedHandler,
|
|
22
17
|
getWorkingMemoryHandler: () => getWorkingMemoryHandler,
|
|
18
|
+
listMessagesHandler: () => listMessagesHandler,
|
|
19
|
+
listThreadsHandler: () => listThreadsHandler,
|
|
23
20
|
saveMessagesHandler: () => saveMessagesHandler,
|
|
24
21
|
searchMemoryHandler: () => searchMemoryHandler,
|
|
25
22
|
updateThreadHandler: () => updateThreadHandler,
|
|
@@ -28,20 +25,20 @@ __export(memory_exports, {
|
|
|
28
25
|
async function getMemoryFromContext({
|
|
29
26
|
mastra,
|
|
30
27
|
agentId,
|
|
31
|
-
|
|
28
|
+
requestContext
|
|
32
29
|
}) {
|
|
33
30
|
const logger = mastra.getLogger();
|
|
34
31
|
let agent;
|
|
35
32
|
if (agentId) {
|
|
36
33
|
try {
|
|
37
|
-
agent = mastra.
|
|
34
|
+
agent = mastra.getAgentById(agentId);
|
|
38
35
|
} catch (error) {
|
|
39
36
|
logger.debug("Error getting agent from mastra, searching agents for agent", error);
|
|
40
37
|
}
|
|
41
38
|
}
|
|
42
39
|
if (agentId && !agent) {
|
|
43
40
|
logger.debug("Agent not found, searching agents for agent", { agentId });
|
|
44
|
-
const agents = mastra.
|
|
41
|
+
const agents = mastra.listAgents();
|
|
45
42
|
if (Object.keys(agents || {}).length) {
|
|
46
43
|
for (const [_, ag] of Object.entries(agents)) {
|
|
47
44
|
try {
|
|
@@ -61,18 +58,17 @@ async function getMemoryFromContext({
|
|
|
61
58
|
}
|
|
62
59
|
if (agent) {
|
|
63
60
|
return await agent?.getMemory({
|
|
64
|
-
|
|
65
|
-
})
|
|
61
|
+
requestContext: requestContext ?? new RequestContext()
|
|
62
|
+
});
|
|
66
63
|
}
|
|
67
|
-
return mastra.getMemory();
|
|
68
64
|
}
|
|
69
65
|
async function getMemoryStatusHandler({
|
|
70
66
|
mastra,
|
|
71
67
|
agentId,
|
|
72
|
-
|
|
68
|
+
requestContext
|
|
73
69
|
}) {
|
|
74
70
|
try {
|
|
75
|
-
const memory = await getMemoryFromContext({ mastra, agentId,
|
|
71
|
+
const memory = await getMemoryFromContext({ mastra, agentId, requestContext });
|
|
76
72
|
if (!memory) {
|
|
77
73
|
return { result: false };
|
|
78
74
|
}
|
|
@@ -84,10 +80,10 @@ async function getMemoryStatusHandler({
|
|
|
84
80
|
async function getMemoryConfigHandler({
|
|
85
81
|
mastra,
|
|
86
82
|
agentId,
|
|
87
|
-
|
|
83
|
+
requestContext
|
|
88
84
|
}) {
|
|
89
85
|
try {
|
|
90
|
-
const memory = await getMemoryFromContext({ mastra, agentId,
|
|
86
|
+
const memory = await getMemoryFromContext({ mastra, agentId, requestContext });
|
|
91
87
|
if (!memory) {
|
|
92
88
|
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
93
89
|
}
|
|
@@ -97,67 +93,41 @@ async function getMemoryConfigHandler({
|
|
|
97
93
|
return handleError(error, "Error getting memory configuration");
|
|
98
94
|
}
|
|
99
95
|
}
|
|
100
|
-
async function
|
|
101
|
-
mastra,
|
|
102
|
-
agentId,
|
|
103
|
-
resourceId,
|
|
104
|
-
runtimeContext,
|
|
105
|
-
orderBy,
|
|
106
|
-
sortDirection
|
|
107
|
-
}) {
|
|
108
|
-
try {
|
|
109
|
-
const memory = await getMemoryFromContext({ mastra, agentId, runtimeContext });
|
|
110
|
-
if (!memory) {
|
|
111
|
-
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
112
|
-
}
|
|
113
|
-
validateBody({ resourceId });
|
|
114
|
-
const threads = await memory.getThreadsByResourceId({
|
|
115
|
-
resourceId,
|
|
116
|
-
orderBy,
|
|
117
|
-
sortDirection
|
|
118
|
-
});
|
|
119
|
-
return threads;
|
|
120
|
-
} catch (error) {
|
|
121
|
-
return handleError(error, "Error getting threads");
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
async function getThreadsPaginatedHandler({
|
|
96
|
+
async function listThreadsHandler({
|
|
125
97
|
mastra,
|
|
126
98
|
agentId,
|
|
127
99
|
resourceId,
|
|
128
|
-
|
|
100
|
+
requestContext,
|
|
129
101
|
page,
|
|
130
102
|
perPage,
|
|
131
|
-
orderBy
|
|
132
|
-
sortDirection
|
|
103
|
+
orderBy
|
|
133
104
|
}) {
|
|
134
105
|
try {
|
|
135
|
-
const memory = await getMemoryFromContext({ mastra, agentId,
|
|
106
|
+
const memory = await getMemoryFromContext({ mastra, agentId, requestContext });
|
|
136
107
|
if (!memory) {
|
|
137
108
|
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
138
109
|
}
|
|
139
110
|
validateBody({ resourceId });
|
|
140
|
-
const result = await memory.
|
|
111
|
+
const result = await memory.listThreadsByResourceId({
|
|
141
112
|
resourceId,
|
|
142
113
|
page,
|
|
143
114
|
perPage,
|
|
144
|
-
orderBy
|
|
145
|
-
sortDirection
|
|
115
|
+
orderBy
|
|
146
116
|
});
|
|
147
117
|
return result;
|
|
148
118
|
} catch (error) {
|
|
149
|
-
return handleError(error, "Error
|
|
119
|
+
return handleError(error, "Error listing threads");
|
|
150
120
|
}
|
|
151
121
|
}
|
|
152
122
|
async function getThreadByIdHandler({
|
|
153
123
|
mastra,
|
|
154
124
|
agentId,
|
|
155
125
|
threadId,
|
|
156
|
-
|
|
126
|
+
requestContext
|
|
157
127
|
}) {
|
|
158
128
|
try {
|
|
159
129
|
validateBody({ threadId });
|
|
160
|
-
const memory = await getMemoryFromContext({ mastra, agentId,
|
|
130
|
+
const memory = await getMemoryFromContext({ mastra, agentId, requestContext });
|
|
161
131
|
if (!memory) {
|
|
162
132
|
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
163
133
|
}
|
|
@@ -174,10 +144,10 @@ async function saveMessagesHandler({
|
|
|
174
144
|
mastra,
|
|
175
145
|
agentId,
|
|
176
146
|
body,
|
|
177
|
-
|
|
147
|
+
requestContext
|
|
178
148
|
}) {
|
|
179
149
|
try {
|
|
180
|
-
const memory = await getMemoryFromContext({ mastra, agentId,
|
|
150
|
+
const memory = await getMemoryFromContext({ mastra, agentId, requestContext });
|
|
181
151
|
if (!memory) {
|
|
182
152
|
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
183
153
|
}
|
|
@@ -208,10 +178,10 @@ async function createThreadHandler({
|
|
|
208
178
|
mastra,
|
|
209
179
|
agentId,
|
|
210
180
|
body,
|
|
211
|
-
|
|
181
|
+
requestContext
|
|
212
182
|
}) {
|
|
213
183
|
try {
|
|
214
|
-
const memory = await getMemoryFromContext({ mastra, agentId,
|
|
184
|
+
const memory = await getMemoryFromContext({ mastra, agentId, requestContext });
|
|
215
185
|
if (!memory) {
|
|
216
186
|
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
217
187
|
}
|
|
@@ -232,10 +202,10 @@ async function updateThreadHandler({
|
|
|
232
202
|
agentId,
|
|
233
203
|
threadId,
|
|
234
204
|
body,
|
|
235
|
-
|
|
205
|
+
requestContext
|
|
236
206
|
}) {
|
|
237
207
|
try {
|
|
238
|
-
const memory = await getMemoryFromContext({ mastra, agentId,
|
|
208
|
+
const memory = await getMemoryFromContext({ mastra, agentId, requestContext });
|
|
239
209
|
if (!body) {
|
|
240
210
|
throw new HTTPException(400, { message: "Body is required" });
|
|
241
211
|
}
|
|
@@ -267,11 +237,11 @@ async function deleteThreadHandler({
|
|
|
267
237
|
mastra,
|
|
268
238
|
agentId,
|
|
269
239
|
threadId,
|
|
270
|
-
|
|
240
|
+
requestContext
|
|
271
241
|
}) {
|
|
272
242
|
try {
|
|
273
243
|
validateBody({ threadId });
|
|
274
|
-
const memory = await getMemoryFromContext({ mastra, agentId,
|
|
244
|
+
const memory = await getMemoryFromContext({ mastra, agentId, requestContext });
|
|
275
245
|
if (!memory) {
|
|
276
246
|
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
277
247
|
}
|
|
@@ -285,55 +255,41 @@ async function deleteThreadHandler({
|
|
|
285
255
|
return handleError(error, "Error deleting thread");
|
|
286
256
|
}
|
|
287
257
|
}
|
|
288
|
-
async function
|
|
289
|
-
mastra,
|
|
290
|
-
threadId,
|
|
291
|
-
resourceId,
|
|
292
|
-
selectBy,
|
|
293
|
-
format
|
|
294
|
-
}) {
|
|
295
|
-
try {
|
|
296
|
-
validateBody({ threadId });
|
|
297
|
-
const storage = mastra.getStorage();
|
|
298
|
-
if (!storage) {
|
|
299
|
-
throw new HTTPException(400, { message: "Storage is not initialized" });
|
|
300
|
-
}
|
|
301
|
-
const thread = await storage.getThreadById({ threadId });
|
|
302
|
-
if (!thread) {
|
|
303
|
-
throw new HTTPException(404, { message: "Thread not found" });
|
|
304
|
-
}
|
|
305
|
-
const result = await storage.getMessagesPaginated({ threadId, resourceId, selectBy, format });
|
|
306
|
-
return result;
|
|
307
|
-
} catch (error) {
|
|
308
|
-
return handleError(error, "Error getting messages");
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
async function getMessagesHandler({
|
|
258
|
+
async function listMessagesHandler({
|
|
312
259
|
mastra,
|
|
313
260
|
agentId,
|
|
314
261
|
threadId,
|
|
315
|
-
|
|
316
|
-
|
|
262
|
+
resourceId,
|
|
263
|
+
perPage,
|
|
264
|
+
page,
|
|
265
|
+
orderBy,
|
|
266
|
+
include,
|
|
267
|
+
filter,
|
|
268
|
+
requestContext
|
|
317
269
|
}) {
|
|
318
|
-
if (limit !== void 0 && (!Number.isInteger(limit) || limit <= 0)) {
|
|
319
|
-
throw new HTTPException(400, { message: "Invalid limit: must be a positive integer" });
|
|
320
|
-
}
|
|
321
270
|
try {
|
|
322
271
|
validateBody({ threadId });
|
|
323
|
-
const memory = await getMemoryFromContext({ mastra, agentId,
|
|
272
|
+
const memory = await getMemoryFromContext({ mastra, agentId, requestContext });
|
|
324
273
|
if (!memory) {
|
|
325
274
|
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
326
275
|
}
|
|
276
|
+
if (!threadId) {
|
|
277
|
+
throw new HTTPException(400, { message: "No threadId found" });
|
|
278
|
+
}
|
|
327
279
|
const thread = await memory.getThreadById({ threadId });
|
|
328
280
|
if (!thread) {
|
|
329
281
|
throw new HTTPException(404, { message: "Thread not found" });
|
|
330
282
|
}
|
|
331
|
-
const result = await memory.
|
|
283
|
+
const result = await memory.recall({
|
|
332
284
|
threadId,
|
|
333
|
-
|
|
285
|
+
resourceId,
|
|
286
|
+
perPage,
|
|
287
|
+
page,
|
|
288
|
+
orderBy,
|
|
289
|
+
include,
|
|
290
|
+
filter
|
|
334
291
|
});
|
|
335
|
-
|
|
336
|
-
return { messages: result.messages, uiMessages, legacyMessages: result.uiMessages };
|
|
292
|
+
return result;
|
|
337
293
|
} catch (error) {
|
|
338
294
|
return handleError(error, "Error getting messages");
|
|
339
295
|
}
|
|
@@ -343,11 +299,11 @@ async function getWorkingMemoryHandler({
|
|
|
343
299
|
agentId,
|
|
344
300
|
threadId,
|
|
345
301
|
resourceId,
|
|
346
|
-
|
|
302
|
+
requestContext,
|
|
347
303
|
memoryConfig
|
|
348
304
|
}) {
|
|
349
305
|
try {
|
|
350
|
-
const memory = await getMemoryFromContext({ mastra, agentId,
|
|
306
|
+
const memory = await getMemoryFromContext({ mastra, agentId, requestContext });
|
|
351
307
|
validateBody({ threadId });
|
|
352
308
|
if (!memory) {
|
|
353
309
|
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
@@ -358,7 +314,7 @@ async function getWorkingMemoryHandler({
|
|
|
358
314
|
const workingMemoryTemplate = template?.format === "json" ? { ...template, content: JSON.stringify(generateEmptyFromSchema(template.content)) } : template;
|
|
359
315
|
const workingMemory = await memory.getWorkingMemory({ threadId, resourceId, memoryConfig });
|
|
360
316
|
const config = memory.getMergedThreadConfig(memoryConfig || {});
|
|
361
|
-
const source = config.workingMemory?.scope
|
|
317
|
+
const source = config.workingMemory?.scope !== "thread" && resourceId ? "resource" : "thread";
|
|
362
318
|
return { workingMemory, source, workingMemoryTemplate, threadExists };
|
|
363
319
|
} catch (error) {
|
|
364
320
|
return handleError(error, "Error getting working memory");
|
|
@@ -369,11 +325,11 @@ async function updateWorkingMemoryHandler({
|
|
|
369
325
|
agentId,
|
|
370
326
|
threadId,
|
|
371
327
|
body,
|
|
372
|
-
|
|
328
|
+
requestContext
|
|
373
329
|
}) {
|
|
374
330
|
try {
|
|
375
331
|
validateBody({ threadId });
|
|
376
|
-
const memory = await getMemoryFromContext({ mastra, agentId,
|
|
332
|
+
const memory = await getMemoryFromContext({ mastra, agentId, requestContext });
|
|
377
333
|
const { resourceId, memoryConfig, workingMemory } = body;
|
|
378
334
|
if (!memory) {
|
|
379
335
|
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
@@ -392,13 +348,13 @@ async function deleteMessagesHandler({
|
|
|
392
348
|
mastra,
|
|
393
349
|
agentId,
|
|
394
350
|
messageIds,
|
|
395
|
-
|
|
351
|
+
requestContext
|
|
396
352
|
}) {
|
|
397
353
|
try {
|
|
398
354
|
if (messageIds === void 0 || messageIds === null) {
|
|
399
355
|
throw new HTTPException(400, { message: "messageIds is required" });
|
|
400
356
|
}
|
|
401
|
-
const memory = await getMemoryFromContext({ mastra, agentId,
|
|
357
|
+
const memory = await getMemoryFromContext({ mastra, agentId, requestContext });
|
|
402
358
|
if (!memory) {
|
|
403
359
|
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
404
360
|
}
|
|
@@ -419,18 +375,18 @@ async function searchMemoryHandler({
|
|
|
419
375
|
resourceId,
|
|
420
376
|
threadId,
|
|
421
377
|
limit = 20,
|
|
422
|
-
|
|
378
|
+
requestContext,
|
|
423
379
|
memoryConfig
|
|
424
380
|
}) {
|
|
425
381
|
try {
|
|
426
382
|
validateBody({ searchQuery, resourceId });
|
|
427
|
-
const memory = await getMemoryFromContext({ mastra, agentId,
|
|
383
|
+
const memory = await getMemoryFromContext({ mastra, agentId, requestContext });
|
|
428
384
|
if (!memory) {
|
|
429
385
|
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
430
386
|
}
|
|
431
387
|
const config = memory.getMergedThreadConfig(memoryConfig || {});
|
|
432
388
|
const hasSemanticRecall = !!config?.semanticRecall;
|
|
433
|
-
const resourceScope = typeof config?.semanticRecall === "object"
|
|
389
|
+
const resourceScope = typeof config?.semanticRecall === "object" ? config?.semanticRecall?.scope !== "thread" : true;
|
|
434
390
|
if (threadId && !resourceScope) {
|
|
435
391
|
const thread = await memory.getThreadById({ threadId });
|
|
436
392
|
if (!thread) {
|
|
@@ -441,7 +397,6 @@ async function searchMemoryHandler({
|
|
|
441
397
|
}
|
|
442
398
|
}
|
|
443
399
|
const searchResults = [];
|
|
444
|
-
const messageMap = /* @__PURE__ */ new Map();
|
|
445
400
|
if (threadId && !resourceScope) {
|
|
446
401
|
const thread = await memory.getThreadById({ threadId });
|
|
447
402
|
if (!thread) {
|
|
@@ -449,117 +404,85 @@ async function searchMemoryHandler({
|
|
|
449
404
|
results: [],
|
|
450
405
|
count: 0,
|
|
451
406
|
query: searchQuery,
|
|
452
|
-
searchScope: "thread",
|
|
407
|
+
searchScope: resourceScope ? "resource" : "thread",
|
|
453
408
|
searchType: hasSemanticRecall ? "semantic" : "text"
|
|
454
409
|
};
|
|
455
410
|
}
|
|
456
411
|
}
|
|
457
|
-
if (!threadId
|
|
458
|
-
const threads = await memory.
|
|
412
|
+
if (!threadId) {
|
|
413
|
+
const { threads } = await memory.listThreadsByResourceId({
|
|
414
|
+
resourceId,
|
|
415
|
+
page: 0,
|
|
416
|
+
perPage: 1,
|
|
417
|
+
orderBy: { field: "updatedAt", direction: "DESC" }
|
|
418
|
+
});
|
|
459
419
|
if (threads.length === 0) {
|
|
460
420
|
return {
|
|
461
421
|
results: [],
|
|
462
422
|
count: 0,
|
|
463
423
|
query: searchQuery,
|
|
464
|
-
searchScope: "resource",
|
|
424
|
+
searchScope: resourceScope ? "resource" : "thread",
|
|
465
425
|
searchType: hasSemanticRecall ? "semantic" : "text"
|
|
466
426
|
};
|
|
467
427
|
}
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
428
|
+
threadId = threads[0].id;
|
|
429
|
+
}
|
|
430
|
+
const beforeRange = typeof config.semanticRecall === `boolean` ? 2 : typeof config.semanticRecall?.messageRange === `number` ? config.semanticRecall.messageRange : config.semanticRecall?.messageRange.before || 2;
|
|
431
|
+
const afterRange = typeof config.semanticRecall === `boolean` ? 2 : typeof config.semanticRecall?.messageRange === `number` ? config.semanticRecall.messageRange : config.semanticRecall?.messageRange.after || 2;
|
|
432
|
+
if (resourceScope && config.semanticRecall) {
|
|
433
|
+
config.semanticRecall = typeof config.semanticRecall === `boolean` ? (
|
|
434
|
+
// make message range 0 so we can highlight the matches in search, message range will include other messages, not the matching ones
|
|
435
|
+
// and we add prev/next messages in a special section on each message anyway
|
|
436
|
+
{ messageRange: 0, topK: 2, scope: "resource" }
|
|
437
|
+
) : { ...config.semanticRecall, messageRange: 0 };
|
|
438
|
+
}
|
|
439
|
+
const threadConfig = memory.getMergedThreadConfig(config || {});
|
|
440
|
+
if (!threadConfig.lastMessages && !threadConfig.semanticRecall) {
|
|
441
|
+
return { results: [], count: 0, query: searchQuery };
|
|
442
|
+
}
|
|
443
|
+
const result = await memory.recall({
|
|
444
|
+
threadId,
|
|
445
|
+
resourceId,
|
|
446
|
+
perPage: threadConfig.lastMessages,
|
|
447
|
+
threadConfig: config,
|
|
448
|
+
vectorSearchString: threadConfig.semanticRecall && searchQuery ? searchQuery : void 0
|
|
449
|
+
});
|
|
450
|
+
const threadIds = Array.from(
|
|
451
|
+
new Set(result.messages.map((m) => m.threadId || threadId).filter(Boolean))
|
|
452
|
+
);
|
|
453
|
+
const fetched = await Promise.all(threadIds.map((id) => memory.getThreadById({ threadId: id })));
|
|
454
|
+
const threadMap = new Map(fetched.filter(Boolean).map((t) => [t.id, t]));
|
|
455
|
+
for (const msg of result.messages) {
|
|
456
|
+
const content = typeof msg.content.content === `string` ? msg.content.content : msg.content.parts?.map((p) => p.type === "text" ? p.text : "").join(" ") || "";
|
|
457
|
+
const msgThreadId = msg.threadId || threadId;
|
|
458
|
+
const thread = threadMap.get(msgThreadId);
|
|
459
|
+
const threadMessages = (await memory.recall({ threadId: msgThreadId })).messages;
|
|
460
|
+
const messageIndex = threadMessages.findIndex((m) => m.id === msg.id);
|
|
461
|
+
const searchResult = {
|
|
462
|
+
id: msg.id,
|
|
463
|
+
role: msg.role,
|
|
464
|
+
content,
|
|
465
|
+
createdAt: msg.createdAt,
|
|
466
|
+
threadId: msgThreadId,
|
|
467
|
+
threadTitle: thread?.title || msgThreadId
|
|
468
|
+
};
|
|
469
|
+
if (messageIndex !== -1) {
|
|
470
|
+
searchResult.context = {
|
|
471
|
+
before: threadMessages.slice(Math.max(0, messageIndex - beforeRange), messageIndex).map((m) => ({
|
|
472
|
+
id: m.id,
|
|
473
|
+
role: m.role,
|
|
474
|
+
content: m.content,
|
|
475
|
+
createdAt: m.createdAt || /* @__PURE__ */ new Date()
|
|
476
|
+
})),
|
|
477
|
+
after: threadMessages.slice(messageIndex + 1, messageIndex + afterRange + 1).map((m) => ({
|
|
478
|
+
id: m.id,
|
|
479
|
+
role: m.role,
|
|
480
|
+
content: m.content,
|
|
481
|
+
createdAt: m.createdAt || /* @__PURE__ */ new Date()
|
|
482
|
+
}))
|
|
520
483
|
};
|
|
521
484
|
}
|
|
522
|
-
|
|
523
|
-
threadId,
|
|
524
|
-
resourceId,
|
|
525
|
-
vectorMessageSearch: searchQuery,
|
|
526
|
-
config
|
|
527
|
-
});
|
|
528
|
-
const threadMessages = (await memory.query({ threadId })).uiMessages;
|
|
529
|
-
result.messagesV2.forEach((msg) => {
|
|
530
|
-
if (messageMap.has(msg.id)) return;
|
|
531
|
-
messageMap.set(msg.id, true);
|
|
532
|
-
const content = msg.content.content || msg.content.parts?.map((p) => p.type === "text" ? p.text : "").join(" ") || "";
|
|
533
|
-
if (!hasSemanticRecall && !content.toLowerCase().includes(searchQuery.toLowerCase())) {
|
|
534
|
-
return;
|
|
535
|
-
}
|
|
536
|
-
const messageIndex = threadMessages.findIndex((m) => m.id === msg.id);
|
|
537
|
-
const searchResult = {
|
|
538
|
-
id: msg.id,
|
|
539
|
-
role: msg.role,
|
|
540
|
-
content,
|
|
541
|
-
createdAt: msg.createdAt,
|
|
542
|
-
threadId,
|
|
543
|
-
threadTitle: thread?.title || threadId
|
|
544
|
-
};
|
|
545
|
-
if (messageIndex !== -1) {
|
|
546
|
-
searchResult.context = {
|
|
547
|
-
before: threadMessages.slice(Math.max(0, messageIndex - 2), messageIndex).map((m) => ({
|
|
548
|
-
id: m.id,
|
|
549
|
-
role: m.role,
|
|
550
|
-
content: m.content,
|
|
551
|
-
createdAt: m.createdAt || /* @__PURE__ */ new Date()
|
|
552
|
-
})),
|
|
553
|
-
after: threadMessages.slice(messageIndex + 1, messageIndex + 3).map((m) => ({
|
|
554
|
-
id: m.id,
|
|
555
|
-
role: m.role,
|
|
556
|
-
content: m.content,
|
|
557
|
-
createdAt: m.createdAt || /* @__PURE__ */ new Date()
|
|
558
|
-
}))
|
|
559
|
-
};
|
|
560
|
-
}
|
|
561
|
-
searchResults.push(searchResult);
|
|
562
|
-
});
|
|
485
|
+
searchResults.push(searchResult);
|
|
563
486
|
}
|
|
564
487
|
const sortedResults = searchResults.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()).slice(0, limit);
|
|
565
488
|
return {
|
|
@@ -574,6 +497,6 @@ async function searchMemoryHandler({
|
|
|
574
497
|
}
|
|
575
498
|
}
|
|
576
499
|
|
|
577
|
-
export { createThreadHandler, deleteMessagesHandler, deleteThreadHandler, getMemoryConfigHandler, getMemoryStatusHandler,
|
|
578
|
-
//# sourceMappingURL=chunk-
|
|
579
|
-
//# sourceMappingURL=chunk-
|
|
500
|
+
export { createThreadHandler, deleteMessagesHandler, deleteThreadHandler, getMemoryConfigHandler, getMemoryStatusHandler, getThreadByIdHandler, getWorkingMemoryHandler, listMessagesHandler, listThreadsHandler, memory_exports, saveMessagesHandler, searchMemoryHandler, updateThreadHandler, updateWorkingMemoryHandler };
|
|
501
|
+
//# sourceMappingURL=chunk-4JF5WXPL.js.map
|
|
502
|
+
//# sourceMappingUR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,YACrF,IAAI,CAAA,CAAE,EAAA;AAAA,YACN,MAAM,CAAA,CAAE,IAAA;AAAA,YACR,SAAS,CAAA,CAAE,OAAA;AAAA,YACX,SAAA,EAAW,CAAA,CAAE,SAAA,oBAAa,IAAI,IAAA;AAAK,WACrC,CAAE;AAAA,SACJ;AAAA,MACF;AAEA,MAAA,aAAA,CAAc,KAAK,YAAY,CAAA;AAAA,IACjC;AAGA,IAAA,MAAM,aAAA,GAAgB,cACnB,IAAA,CAAK,CAAC,GAAG,CAAA,KAAM,IAAI,IAAA,CAAK,CAAA,CAAE,SAAS,CAAA,CAAE,SAAQ,GAAI,IAAI,IAAA,CAAK,CAAA,CAAE,SAAS,CAAA,CAAE,SAAS,CAAA,CAChF,KAAA,CAAM,CAAA,EAAG,KAAK,CAAA;AAEjB,IAAA,OAAO;AAAA,MACL,OAAA,EAAS,aAAA;AAAA,MACT,OAAO,aAAA,CAAc,MAAA;AAAA,MACrB,KAAA,EAAO,WAAA;AAAA,MACP,WAAA,EAAa,gBAAgB,UAAA,GAAa,QAAA;AAAA,MAC1C,UAAA,EAAY,oBAAoB,UAAA,GAAa;AAAA,KAC/C;AAAA,EACF,SAAS,KAAA,EAAO;AACd,IAAA,OAAO,WAAA,CAAY,OAAO,wBAAwB,CAAA;AAAA,EACpD;AACF","file":"chunk-4JF5WXPL.js","sourcesContent":["import type { MastraDBMessage } from '@mastra/core/agent';\nimport { RequestContext } from '@mastra/core/di';\nimport type { MastraMemory } from '@mastra/core/memory';\nimport type { StorageListMessagesInput, StorageOrderBy } from '@mastra/core/storage';\nimport { generateEmptyFromSchema } from '@mastra/core/utils';\nimport { HTTPException } from '../http-exception';\nimport type { Context } from '../types';\n\nimport { handleError } from './error';\nimport { validateBody } from './utils';\n\ninterface MemoryContext extends Context {\n agentId?: string;\n resourceId?: string;\n threadId?: string;\n requestContext?: RequestContext;\n}\n\nasync function getMemoryFromContext({\n mastra,\n agentId,\n requestContext,\n}: Pick<MemoryContext, 'mastra' | 'agentId' | 'requestContext'>): Promise<MastraMemory | null | undefined> {\n const logger = mastra.getLogger();\n let agent;\n if (agentId) {\n try {\n agent = mastra.getAgentById(agentId);\n } catch (error) {\n logger.debug('Error getting agent from mastra, searching agents for agent', error);\n }\n }\n if (agentId && !agent) {\n logger.debug('Agent not found, searching agents for agent', { agentId });\n const agents = mastra.listAgents();\n if (Object.keys(agents || {}).length) {\n for (const [_, ag] of Object.entries(agents)) {\n try {\n const agents = await ag.listAgents();\n\n if (agents[agentId]) {\n agent = agents[agentId];\n break;\n }\n } catch (error) {\n logger.debug('Error getting agent from agent', error);\n }\n }\n }\n\n if (!agent) {\n throw new HTTPException(404, { message: 'Agent not found' });\n }\n }\n\n if (agent) {\n return await agent?.getMemory({\n requestContext: requestContext ?? new RequestContext(),\n });\n }\n}\n\n// Memory handlers\nexport async function getMemoryStatusHandler({\n mastra,\n agentId,\n requestContext,\n}: Pick<MemoryContext, 'mastra' | 'agentId' | 'requestContext'>) {\n try {\n const memory = await getMemoryFromContext({ mastra, agentId, requestContext });\n\n if (!memory) {\n return { result: false };\n }\n\n return { result: true };\n } catch (error) {\n return handleError(error, 'Error getting memory status');\n }\n}\n\nexport async function getMemoryConfigHandler({\n mastra,\n agentId,\n requestContext,\n}: Pick<MemoryContext, 'mastra' | 'agentId' | 'requestContext'>) {\n try {\n const memory = await getMemoryFromContext({ mastra, agentId, requestContext });\n\n if (!memory) {\n throw new HTTPException(400, { message: 'Memory is not initialized' });\n }\n\n // Get the merged configuration (defaults + custom)\n const config = memory.getMergedThreadConfig({});\n\n return { config };\n } catch (error) {\n return handleError(error, 'Error getting memory configuration');\n }\n}\n\nexport async function listThreadsHandler({\n mastra,\n agentId,\n resourceId,\n requestContext,\n page,\n perPage,\n orderBy,\n}: Pick<MemoryContext, 'mastra' | 'agentId' | 'resourceId' | 'requestContext'> & {\n page: number;\n perPage: number | false;\n orderBy?: StorageOrderBy;\n}) {\n try {\n const memory = await getMemoryFromContext({ mastra, agentId, requestContext });\n\n if (!memory) {\n throw new HTTPException(400, { message: 'Memory is not initialized' });\n }\n\n validateBody({ resourceId });\n\n const result = await memory.listThreadsByResourceId({\n resourceId: resourceId!,\n page,\n perPage,\n orderBy,\n });\n return result;\n } catch (error) {\n return handleError(error, 'Error listing threads');\n }\n}\n\nexport async function getThreadByIdHandler({\n mastra,\n agentId,\n threadId,\n requestContext,\n}: Pick<MemoryContext, 'mastra' | 'agentId' | 'threadId' | 'requestContext'>) {\n try {\n validateBody({ threadId });\n\n const memory = await getMemoryFromContext({ mastra, agentId, requestContext });\n if (!memory) {\n throw new HTTPException(400, { message: 'Memory is not initialized' });\n }\n\n const thread = await memory.getThreadById({ threadId: threadId! });\n if (!thread) {\n throw new HTTPException(404, { message: 'Thread not found' });\n }\n\n return thread;\n } catch (error) {\n return handleError(error, 'Error getting thread');\n }\n}\n\nexport async function saveMessagesHandler({\n mastra,\n agentId,\n body,\n requestContext,\n}: Pick<MemoryContext, 'mastra' | 'agentId' | 'requestContext'> & {\n body: {\n messages: Parameters<MastraMemory['saveMessages']>[0]['messages'];\n };\n}) {\n try {\n const memory = await getMemoryFromContext({ mastra, agentId, requestContext });\n\n if (!memory) {\n throw new HTTPException(400, { message: 'Memory is not initialized' });\n }\n\n if (!body?.messages) {\n throw new HTTPException(400, { message: 'Messages are required' });\n }\n\n if (!Array.isArray(body.messages)) {\n throw new HTTPException(400, { message: 'Messages should be an array' });\n }\n\n // Validate that all messages have threadId and resourceId\n const invalidMessages = body.messages.filter(message => !message.threadId || !message.resourceId);\n if (invalidMessages.length > 0) {\n throw new HTTPException(400, {\n message: `All messages must have threadId and resourceId fields. Found ${invalidMessages.length} invalid message(s).`,\n });\n }\n\n const processedMessages = body.messages.map(message => ({\n ...message,\n id: message.id || memory.generateId(),\n createdAt: message.createdAt ? new Date(message.createdAt) : new Date(),\n }));\n\n const result = await memory.saveMessages({ messages: processedMessages, memoryConfig: {} });\n return result;\n } catch (error) {\n return handleError(error, 'Error saving messages');\n }\n}\n\nexport async function createThreadHandler({\n mastra,\n agentId,\n body,\n requestContext,\n}: Pick<MemoryContext, 'mastra' | 'agentId' | 'requestContext'> & {\n body?: Omit<Parameters<MastraMemory['createThread']>[0], 'resourceId'> & { resourceId?: string };\n}) {\n try {\n const memory = await getMemoryFromContext({ mastra, agentId, requestContext });\n\n if (!memory) {\n throw new HTTPException(400, { message: 'Memory is not initialized' });\n }\n\n validateBody({ resourceId: body?.resourceId });\n\n const result = await memory.createThread({\n resourceId: body?.resourceId!,\n title: body?.title,\n metadata: body?.metadata,\n threadId: body?.threadId,\n });\n return result;\n } catch (error) {\n return handleError(error, 'Error saving thread to memory');\n }\n}\n\nexport async function updateThreadHandler({\n mastra,\n agentId,\n threadId,\n body,\n requestContext,\n}: Pick<MemoryContext, 'mastra' | 'agentId' | 'threadId' | 'requestContext'> & {\n body?: Parameters<MastraMemory['saveThread']>[0]['thread'];\n}) {\n try {\n const memory = await getMemoryFromContext({ mastra, agentId, requestContext });\n\n if (!body) {\n throw new HTTPException(400, { message: 'Body is required' });\n }\n\n const { title, metadata, resourceId } = body;\n const updatedAt = new Date();\n\n validateBody({ threadId });\n\n if (!memory) {\n throw new HTTPException(400, { message: 'Memory is not initialized' });\n }\n\n const thread = await memory.getThreadById({ threadId: threadId! });\n if (!thread) {\n throw new HTTPException(404, { message: 'Thread not found' });\n }\n\n const updatedThread = {\n ...thread,\n title: title || thread.title,\n metadata: metadata || thread.metadata,\n resourceId: resourceId || thread.resourceId,\n createdAt: thread.createdAt,\n updatedAt,\n };\n\n const result = await memory.saveThread({ thread: updatedThread });\n return result;\n } catch (error) {\n return handleError(error, 'Error updating thread');\n }\n}\n\nexport async function deleteThreadHandler({\n mastra,\n agentId,\n threadId,\n requestContext,\n}: Pick<MemoryContext, 'mastra' | 'agentId' | 'threadId' | 'requestContext'>) {\n try {\n validateBody({ threadId });\n\n const memory = await getMemoryFromContext({ mastra, agentId, requestContext });\n if (!memory) {\n throw new HTTPException(400, { message: 'Memory is not initialized' });\n }\n\n const thread = await memory.getThreadById({ threadId: threadId! });\n if (!thread) {\n throw new HTTPException(404, { message: 'Thread not found' });\n }\n\n await memory.deleteThread(threadId!);\n return { result: 'Thread deleted' };\n } catch (error) {\n return handleError(error, 'Error deleting thread');\n }\n}\n\nexport async function listMessagesHandler({\n mastra,\n agentId,\n threadId,\n resourceId,\n perPage,\n page,\n orderBy,\n include,\n filter,\n requestContext,\n}: Pick<MemoryContext, 'mastra' | 'agentId' | 'threadId' | 'requestContext'> &\n Omit<StorageListMessagesInput, 'threadId'>) {\n try {\n validateBody({ threadId });\n\n const memory = await getMemoryFromContext({ mastra, agentId, requestContext });\n\n if (!memory) {\n throw new HTTPException(400, { message: 'Memory is not initialized' });\n }\n\n if (!threadId) {\n throw new HTTPException(400, { message: 'No threadId found' });\n }\n\n const thread = await memory.getThreadById({ threadId: threadId });\n if (!thread) {\n throw new HTTPException(404, { message: 'Thread not found' });\n }\n\n const result = await memory.recall({\n threadId: threadId,\n resourceId,\n perPage,\n page,\n orderBy,\n include,\n filter,\n });\n return result;\n } catch (error) {\n return handleError(error, 'Error getting messages');\n }\n}\n\n/**\n * Handler to get the working memory for a thread (optionally resource-scoped).\n * @returns workingMemory - the working memory for the thread\n * @returns source - thread or resource\n */\nexport async function getWorkingMemoryHandler({\n mastra,\n agentId,\n threadId,\n resourceId,\n requestContext,\n memoryConfig,\n}: Pick<MemoryContext, 'mastra' | 'agentId' | 'threadId' | 'requestContext'> & {\n resourceId?: Parameters<MastraMemory['getWorkingMemory']>[0]['resourceId'];\n memoryConfig?: Parameters<MastraMemory['getWorkingMemory']>[0]['memoryConfig'];\n}) {\n try {\n const memory = await getMemoryFromContext({ mastra, agentId, requestContext });\n validateBody({ threadId });\n if (!memory) {\n throw new HTTPException(400, { message: 'Memory is not initialized' });\n }\n const thread = await memory.getThreadById({ threadId: threadId! });\n const threadExists = !!thread;\n const template = await memory.getWorkingMemoryTemplate({ memoryConfig });\n const workingMemoryTemplate =\n template?.format === 'json'\n ? { ...template, content: JSON.stringify(generateEmptyFromSchema(template.content)) }\n : template;\n const workingMemory = await memory.getWorkingMemory({ threadId: threadId!, resourceId, memoryConfig });\n const config = memory.getMergedThreadConfig(memoryConfig || {});\n const source = config.workingMemory?.scope !== 'thread' && resourceId ? 'resource' : 'thread';\n return { workingMemory, source, workingMemoryTemplate, threadExists };\n } catch (error) {\n return handleError(error, 'Error getting working memory');\n }\n}\n\n/**\n * Handler to update the working memory for a thread (optionally resource-scoped).\n * @param threadId - the thread id\n * @param body - the body containing the working memory to update and the resource id (optional)\n */\nexport async function updateWorkingMemoryHandler({\n mastra,\n agentId,\n threadId,\n body,\n requestContext,\n}: Pick<MemoryContext, 'mastra' | 'agentId' | 'threadId' | 'requestContext'> & {\n body: Omit<Parameters<MastraMemory['updateWorkingMemory']>[0], 'threadId'>;\n}) {\n try {\n validateBody({ threadId });\n const memory = await getMemoryFromContext({ mastra, agentId, requestContext });\n const { resourceId, memoryConfig, workingMemory } = body;\n if (!memory) {\n throw new HTTPException(400, { message: 'Memory is not initialized' });\n }\n const thread = await memory.getThreadById({ threadId: threadId! });\n if (!thread) {\n throw new HTTPException(404, { message: 'Thread not found' });\n }\n\n await memory.updateWorkingMemory({ threadId: threadId!, resourceId, workingMemory, memoryConfig });\n return { success: true };\n } catch (error) {\n return handleError(error, 'Error updating working memory');\n }\n}\n\ninterface SearchResult {\n id: string;\n role: string;\n content: any;\n createdAt: Date;\n threadId?: string;\n threadTitle?: string;\n score?: number;\n context?: {\n before?: SearchResult[];\n after?: SearchResult[];\n };\n}\n\ninterface SearchResponse {\n results: SearchResult[];\n count: number;\n query: string;\n searchScope?: string;\n searchType?: string;\n}\n\n/**\n * Handler to delete one or more messages.\n * @param messageIds - Can be a single ID, array of IDs, or objects with ID property\n */\nexport async function deleteMessagesHandler({\n mastra,\n agentId,\n messageIds,\n requestContext,\n}: Pick<MemoryContext, 'mastra' | 'agentId' | 'requestContext'> & {\n messageIds: string | string[] | { id: string } | { id: string }[];\n}) {\n try {\n if (messageIds === undefined || messageIds === null) {\n throw new HTTPException(400, { message: 'messageIds is required' });\n }\n\n const memory = await getMemoryFromContext({ mastra, agentId, requestContext });\n if (!memory) {\n throw new HTTPException(400, { message: 'Memory is not initialized' });\n }\n\n // Delete the messages - let the memory method handle validation\n await memory.deleteMessages(messageIds as any);\n\n // Count messages for response\n let count = 1;\n if (Array.isArray(messageIds)) {\n count = messageIds.length;\n }\n\n return { success: true, message: `${count} message${count === 1 ? '' : 's'} deleted successfully` };\n } catch (error) {\n return handleError(error, 'Error deleting messages');\n }\n}\n\nexport async function searchMemoryHandler({\n mastra,\n agentId,\n searchQuery,\n resourceId,\n threadId,\n limit = 20,\n requestContext,\n memoryConfig,\n}: Pick<MemoryContext, 'mastra' | 'agentId' | 'requestContext'> & {\n searchQuery: string;\n resourceId: string;\n threadId?: string;\n limit?: number;\n memoryConfig?: any;\n}): Promise<SearchResponse | ReturnType<typeof handleError>> {\n try {\n validateBody({ searchQuery, resourceId });\n\n const memory = await getMemoryFromContext({ mastra, agentId, requestContext });\n if (!memory) {\n throw new HTTPException(400, { message: 'Memory is not initialized' });\n }\n\n // Get memory configuration first to check scope\n const config = memory.getMergedThreadConfig(memoryConfig || {});\n const hasSemanticRecall = !!config?.semanticRecall;\n const resourceScope =\n typeof config?.semanticRecall === 'object' ? config?.semanticRecall?.scope !== 'thread' : true;\n\n // Only validate thread ownership if we're in thread scope\n if (threadId && !resourceScope) {\n const thread = await memory.getThreadById({ threadId });\n if (!thread) {\n throw new HTTPException(404, { message: 'Thread not found' });\n }\n if (thread.resourceId !== resourceId) {\n throw new HTTPException(403, { message: 'Thread does not belong to the specified resource' });\n }\n }\n\n const searchResults: SearchResult[] = [];\n\n // If threadId is provided and scope is thread-based, check if the thread exists\n if (threadId && !resourceScope) {\n const thread = await memory.getThreadById({ threadId });\n if (!thread) {\n // Thread doesn't exist yet (new unsaved thread) - return empty results\n return {\n results: [],\n count: 0,\n query: searchQuery,\n searchScope: resourceScope ? 'resource' : 'thread',\n searchType: hasSemanticRecall ? 'semantic' : 'text',\n };\n }\n }\n\n // If no threadId provided, get one from the resource\n if (!threadId) {\n const { threads } = await memory.listThreadsByResourceId({\n resourceId,\n page: 0,\n perPage: 1,\n orderBy: { field: 'updatedAt', direction: 'DESC' },\n });\n\n if (threads.length === 0) {\n return {\n results: [],\n count: 0,\n query: searchQuery,\n searchScope: resourceScope ? 'resource' : 'thread',\n searchType: hasSemanticRecall ? 'semantic' : 'text',\n };\n }\n\n // Use first thread - Memory class will handle scope internally\n threadId = threads[0]!.id;\n }\n\n const beforeRange =\n typeof config.semanticRecall === `boolean`\n ? 2\n : typeof config.semanticRecall?.messageRange === `number`\n ? config.semanticRecall.messageRange\n : config.semanticRecall?.messageRange.before || 2;\n const afterRange =\n typeof config.semanticRecall === `boolean`\n ? 2\n : typeof config.semanticRecall?.messageRange === `number`\n ? config.semanticRecall.messageRange\n : config.semanticRecall?.messageRange.after || 2;\n\n if (resourceScope && config.semanticRecall) {\n config.semanticRecall =\n typeof config.semanticRecall === `boolean`\n ? // make message range 0 so we can highlight the matches in search, message range will include other messages, not the matching ones\n // and we add prev/next messages in a special section on each message anyway\n { messageRange: 0, topK: 2, scope: 'resource' }\n : { ...config.semanticRecall, messageRange: 0 };\n }\n\n // Single call to recall - just like the agent does\n // The Memory class handles scope (thread vs resource) internally\n const threadConfig = memory.getMergedThreadConfig(config || {});\n if (!threadConfig.lastMessages && !threadConfig.semanticRecall) {\n return { results: [], count: 0, query: searchQuery };\n }\n\n const result = await memory.recall({\n threadId,\n resourceId,\n perPage: threadConfig.lastMessages,\n threadConfig: config,\n vectorSearchString: threadConfig.semanticRecall && searchQuery ? searchQuery : undefined,\n });\n\n // Get all threads to build context and show which thread each message is from\n // Fetch threads by IDs from the actual messages to avoid truncation\n const threadIds = Array.from(\n new Set(result.messages.map((m: MastraDBMessage) => m.threadId || threadId!).filter(Boolean)),\n );\n const fetched = await Promise.all(threadIds.map((id: string) => memory.getThreadById({ threadId: id })));\n const threadMap = new Map(fetched.filter(Boolean).map(t => [t!.id, t!]));\n\n // Process each message in the results\n for (const msg of result.messages) {\n const content =\n typeof msg.content.content === `string`\n ? msg.content.content\n : msg.content.parts?.map((p: any) => (p.type === 'text' ? p.text : '')).join(' ') || '';\n\n const msgThreadId = msg.threadId || threadId;\n const thread = threadMap.get(msgThreadId);\n\n // Get thread messages for context\n const threadMessages = (await memory.recall({ threadId: msgThreadId })).messages;\n const messageIndex = threadMessages.findIndex(m => m.id === msg.id);\n\n const searchResult: SearchResult = {\n id: msg.id,\n role: msg.role,\n content,\n createdAt: msg.createdAt,\n threadId: msgThreadId,\n threadTitle: thread?.title || msgThreadId,\n };\n\n if (messageIndex !== -1) {\n searchResult.context = {\n before: threadMessages.slice(Math.max(0, messageIndex - beforeRange), messageIndex).map(m => ({\n id: m.id,\n role: m.role,\n content: m.content,\n createdAt: m.createdAt || new Date(),\n })),\n after: threadMessages.slice(messageIndex + 1, messageIndex + afterRange + 1).map(m => ({\n id: m.id,\n role: m.role,\n content: m.content,\n createdAt: m.createdAt || new Date(),\n })),\n };\n }\n\n searchResults.push(searchResult);\n }\n\n // Sort by date (newest first) and limit\n const sortedResults = searchResults\n .sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime())\n .slice(0, limit);\n\n return {\n results: sortedResults,\n count: sortedResults.length,\n query: searchQuery,\n searchScope: resourceScope ? 'resource' : 'thread',\n searchType: hasSemanticRecall ? 'semantic' : 'text',\n };\n } catch (error) {\n return handleError(error, 'Error searching memory');\n }\n}\n"]}
|