@mastra/server 0.0.0-ai-v5-20250718021026 → 0.0.0-ai-v5-20250729181825
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/dist/_tsup-dts-rollup.d.cts +172 -19
- package/dist/_tsup-dts-rollup.d.ts +172 -19
- package/dist/chunk-424T5F2J.cjs +157 -0
- package/dist/{chunk-3TMDONWY.cjs → chunk-6KP2OBYA.cjs} +30 -0
- package/dist/chunk-6T5JUKOQ.js +149 -0
- package/dist/{chunk-34GEZJPU.cjs → chunk-AJRURO6F.cjs} +31 -1
- package/dist/{chunk-OKL5JVWU.js → chunk-EZXKTV2W.js} +31 -2
- package/dist/{chunk-VDTQM2M5.cjs → chunk-FC5QBMOM.cjs} +259 -4
- package/dist/{chunk-N6KVUULV.js → chunk-MKHRYD4K.js} +256 -5
- package/dist/{chunk-ORHUZSM2.js → chunk-T7HAZC2E.js} +30 -1
- package/dist/server/handlers/agents.cjs +11 -7
- package/dist/server/handlers/agents.d.cts +1 -0
- package/dist/server/handlers/agents.d.ts +1 -0
- package/dist/server/handlers/agents.js +1 -1
- package/dist/server/handlers/memory.cjs +27 -11
- package/dist/server/handlers/memory.d.cts +4 -0
- package/dist/server/handlers/memory.d.ts +4 -0
- package/dist/server/handlers/memory.js +1 -1
- package/dist/server/handlers/scores.cjs +30 -0
- package/dist/server/handlers/scores.d.cts +6 -0
- package/dist/server/handlers/scores.d.ts +6 -0
- package/dist/server/handlers/scores.js +1 -0
- package/dist/server/handlers/workflows.cjs +19 -15
- package/dist/server/handlers/workflows.d.cts +1 -0
- package/dist/server/handlers/workflows.d.ts +1 -0
- package/dist/server/handlers/workflows.js +1 -1
- package/dist/server/handlers.cjs +14 -9
- package/dist/server/handlers.d.cts +3 -2
- package/dist/server/handlers.d.ts +3 -2
- package/dist/server/handlers.js +4 -3
- package/package.json +5 -4
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { handleError } from './chunk-LF7P5PLR.js';
|
|
2
|
+
import { __export } from './chunk-MLKGABMK.js';
|
|
3
|
+
|
|
4
|
+
// src/server/handlers/scores.ts
|
|
5
|
+
var scores_exports = {};
|
|
6
|
+
__export(scores_exports, {
|
|
7
|
+
getScorerHandler: () => getScorerHandler,
|
|
8
|
+
getScorersHandler: () => getScorersHandler,
|
|
9
|
+
getScoresByEntityIdHandler: () => getScoresByEntityIdHandler,
|
|
10
|
+
getScoresByRunIdHandler: () => getScoresByRunIdHandler,
|
|
11
|
+
getScoresByScorerIdHandler: () => getScoresByScorerIdHandler,
|
|
12
|
+
saveScoreHandler: () => saveScoreHandler
|
|
13
|
+
});
|
|
14
|
+
async function getScorersFromSystem({
|
|
15
|
+
mastra,
|
|
16
|
+
runtimeContext
|
|
17
|
+
}) {
|
|
18
|
+
const agents = mastra.getAgents();
|
|
19
|
+
const workflows = mastra.getWorkflows();
|
|
20
|
+
const scorersMap = /* @__PURE__ */ new Map();
|
|
21
|
+
for (const [agentId, agent] of Object.entries(agents)) {
|
|
22
|
+
const scorers = await agent.getScorers({
|
|
23
|
+
runtimeContext
|
|
24
|
+
}) || {};
|
|
25
|
+
if (Object.keys(scorers).length > 0) {
|
|
26
|
+
for (const [scorerId, scorer] of Object.entries(scorers)) {
|
|
27
|
+
if (scorersMap.has(scorerId)) {
|
|
28
|
+
scorersMap.get(scorerId)?.agentIds.push(agentId);
|
|
29
|
+
} else {
|
|
30
|
+
scorersMap.set(scorerId, {
|
|
31
|
+
workflowIds: [],
|
|
32
|
+
...scorer,
|
|
33
|
+
agentIds: [agent.name]
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
for (const [workflowId, workflow] of Object.entries(workflows)) {
|
|
40
|
+
const scorers = await workflow.getScorers({
|
|
41
|
+
runtimeContext
|
|
42
|
+
}) || {};
|
|
43
|
+
if (Object.keys(scorers).length > 0) {
|
|
44
|
+
for (const [scorerId, scorer] of Object.entries(scorers)) {
|
|
45
|
+
if (scorersMap.has(scorerId)) {
|
|
46
|
+
scorersMap.get(scorerId)?.workflowIds.push(workflowId);
|
|
47
|
+
} else {
|
|
48
|
+
scorersMap.set(scorerId, {
|
|
49
|
+
agentIds: [],
|
|
50
|
+
...scorer,
|
|
51
|
+
workflowIds: [workflowId]
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return Object.fromEntries(scorersMap.entries());
|
|
58
|
+
}
|
|
59
|
+
async function getScorersHandler({ mastra, runtimeContext }) {
|
|
60
|
+
const scorers = await getScorersFromSystem({
|
|
61
|
+
mastra,
|
|
62
|
+
runtimeContext
|
|
63
|
+
});
|
|
64
|
+
return scorers;
|
|
65
|
+
}
|
|
66
|
+
async function getScorerHandler({
|
|
67
|
+
mastra,
|
|
68
|
+
scorerId,
|
|
69
|
+
runtimeContext
|
|
70
|
+
}) {
|
|
71
|
+
const scorers = await getScorersFromSystem({
|
|
72
|
+
mastra,
|
|
73
|
+
runtimeContext
|
|
74
|
+
});
|
|
75
|
+
const scorer = scorers[scorerId];
|
|
76
|
+
if (!scorer) {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
return scorer;
|
|
80
|
+
}
|
|
81
|
+
async function getScoresByRunIdHandler({
|
|
82
|
+
mastra,
|
|
83
|
+
runId,
|
|
84
|
+
pagination
|
|
85
|
+
}) {
|
|
86
|
+
try {
|
|
87
|
+
const scores = await mastra.getStorage()?.getScoresByRunId?.({
|
|
88
|
+
runId,
|
|
89
|
+
pagination
|
|
90
|
+
}) || [];
|
|
91
|
+
return scores;
|
|
92
|
+
} catch (error) {
|
|
93
|
+
return handleError(error, "Error getting scores by run id");
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
async function getScoresByScorerIdHandler({
|
|
97
|
+
mastra,
|
|
98
|
+
scorerId,
|
|
99
|
+
pagination,
|
|
100
|
+
entityId,
|
|
101
|
+
entityType
|
|
102
|
+
}) {
|
|
103
|
+
try {
|
|
104
|
+
const scores = await mastra.getStorage()?.getScoresByScorerId?.({
|
|
105
|
+
scorerId,
|
|
106
|
+
pagination,
|
|
107
|
+
entityId,
|
|
108
|
+
entityType
|
|
109
|
+
}) || [];
|
|
110
|
+
return scores;
|
|
111
|
+
} catch (error) {
|
|
112
|
+
return handleError(error, "Error getting scores by scorer id");
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
async function getScoresByEntityIdHandler({
|
|
116
|
+
mastra,
|
|
117
|
+
entityId,
|
|
118
|
+
entityType,
|
|
119
|
+
pagination
|
|
120
|
+
}) {
|
|
121
|
+
try {
|
|
122
|
+
let entityIdToUse = entityId;
|
|
123
|
+
if (entityType === "AGENT") {
|
|
124
|
+
const agent = mastra.getAgentById(entityId);
|
|
125
|
+
entityIdToUse = agent.id;
|
|
126
|
+
} else if (entityType === "WORKFLOW") {
|
|
127
|
+
const workflow = mastra.getWorkflowById(entityId);
|
|
128
|
+
entityIdToUse = workflow.id;
|
|
129
|
+
}
|
|
130
|
+
const scores = await mastra.getStorage()?.getScoresByEntityId?.({
|
|
131
|
+
entityId: entityIdToUse,
|
|
132
|
+
entityType,
|
|
133
|
+
pagination
|
|
134
|
+
}) || [];
|
|
135
|
+
return scores;
|
|
136
|
+
} catch (error) {
|
|
137
|
+
return handleError(error, "Error getting scores by entity id");
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
async function saveScoreHandler({ mastra, score }) {
|
|
141
|
+
try {
|
|
142
|
+
const scores = await mastra.getStorage()?.saveScore?.(score) || [];
|
|
143
|
+
return scores;
|
|
144
|
+
} catch (error) {
|
|
145
|
+
return handleError(error, "Error saving score");
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export { getScorerHandler, getScorersHandler, getScoresByEntityIdHandler, getScoresByRunIdHandler, getScoresByScorerIdHandler, saveScoreHandler, scores_exports };
|
|
@@ -16,7 +16,8 @@ chunk75ZPJI57_cjs.__export(agents_exports, {
|
|
|
16
16
|
getAgentsHandler: () => getAgentsHandler,
|
|
17
17
|
getEvalsByAgentIdHandler: () => getEvalsByAgentIdHandler,
|
|
18
18
|
getLiveEvalsByAgentIdHandler: () => getLiveEvalsByAgentIdHandler,
|
|
19
|
-
streamGenerateHandler: () => streamGenerateHandler
|
|
19
|
+
streamGenerateHandler: () => streamGenerateHandler,
|
|
20
|
+
streamVNextGenerateHandler: () => streamVNextGenerateHandler
|
|
20
21
|
});
|
|
21
22
|
async function getAgentsHandler({ mastra, runtimeContext }) {
|
|
22
23
|
try {
|
|
@@ -273,6 +274,34 @@ async function streamGenerateHandler({
|
|
|
273
274
|
return chunkPZQDCRPV_cjs.handleError(error, "error streaming agent response");
|
|
274
275
|
}
|
|
275
276
|
}
|
|
277
|
+
function streamVNextGenerateHandler({
|
|
278
|
+
mastra,
|
|
279
|
+
runtimeContext: runtimeContext$1,
|
|
280
|
+
agentId,
|
|
281
|
+
body,
|
|
282
|
+
abortSignal
|
|
283
|
+
}) {
|
|
284
|
+
try {
|
|
285
|
+
const agent = mastra.getAgent(agentId);
|
|
286
|
+
if (!agent) {
|
|
287
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Agent not found" });
|
|
288
|
+
}
|
|
289
|
+
const { messages, runtimeContext: agentRuntimeContext, ...rest } = body;
|
|
290
|
+
const finalRuntimeContext = new runtimeContext.RuntimeContext([
|
|
291
|
+
...Array.from(runtimeContext$1.entries()),
|
|
292
|
+
...Array.from(Object.entries(agentRuntimeContext ?? {}))
|
|
293
|
+
]);
|
|
294
|
+
chunkCCGRCYWJ_cjs.validateBody({ messages });
|
|
295
|
+
const streamResult = agent.streamVNext(messages, {
|
|
296
|
+
...rest,
|
|
297
|
+
runtimeContext: finalRuntimeContext,
|
|
298
|
+
abortSignal
|
|
299
|
+
});
|
|
300
|
+
return streamResult;
|
|
301
|
+
} catch (error) {
|
|
302
|
+
return chunkPZQDCRPV_cjs.handleError(error, "error streaming agent response");
|
|
303
|
+
}
|
|
304
|
+
}
|
|
276
305
|
|
|
277
306
|
exports.agents_exports = agents_exports;
|
|
278
307
|
exports.generateHandler = generateHandler;
|
|
@@ -281,3 +310,4 @@ exports.getAgentsHandler = getAgentsHandler;
|
|
|
281
310
|
exports.getEvalsByAgentIdHandler = getEvalsByAgentIdHandler;
|
|
282
311
|
exports.getLiveEvalsByAgentIdHandler = getLiveEvalsByAgentIdHandler;
|
|
283
312
|
exports.streamGenerateHandler = streamGenerateHandler;
|
|
313
|
+
exports.streamVNextGenerateHandler = streamVNextGenerateHandler;
|
|
@@ -14,7 +14,8 @@ __export(agents_exports, {
|
|
|
14
14
|
getAgentsHandler: () => getAgentsHandler,
|
|
15
15
|
getEvalsByAgentIdHandler: () => getEvalsByAgentIdHandler,
|
|
16
16
|
getLiveEvalsByAgentIdHandler: () => getLiveEvalsByAgentIdHandler,
|
|
17
|
-
streamGenerateHandler: () => streamGenerateHandler
|
|
17
|
+
streamGenerateHandler: () => streamGenerateHandler,
|
|
18
|
+
streamVNextGenerateHandler: () => streamVNextGenerateHandler
|
|
18
19
|
});
|
|
19
20
|
async function getAgentsHandler({ mastra, runtimeContext }) {
|
|
20
21
|
try {
|
|
@@ -271,5 +272,33 @@ async function streamGenerateHandler({
|
|
|
271
272
|
return handleError(error, "error streaming agent response");
|
|
272
273
|
}
|
|
273
274
|
}
|
|
275
|
+
function streamVNextGenerateHandler({
|
|
276
|
+
mastra,
|
|
277
|
+
runtimeContext,
|
|
278
|
+
agentId,
|
|
279
|
+
body,
|
|
280
|
+
abortSignal
|
|
281
|
+
}) {
|
|
282
|
+
try {
|
|
283
|
+
const agent = mastra.getAgent(agentId);
|
|
284
|
+
if (!agent) {
|
|
285
|
+
throw new HTTPException(404, { message: "Agent not found" });
|
|
286
|
+
}
|
|
287
|
+
const { messages, runtimeContext: agentRuntimeContext, ...rest } = body;
|
|
288
|
+
const finalRuntimeContext = new RuntimeContext([
|
|
289
|
+
...Array.from(runtimeContext.entries()),
|
|
290
|
+
...Array.from(Object.entries(agentRuntimeContext ?? {}))
|
|
291
|
+
]);
|
|
292
|
+
validateBody({ messages });
|
|
293
|
+
const streamResult = agent.streamVNext(messages, {
|
|
294
|
+
...rest,
|
|
295
|
+
runtimeContext: finalRuntimeContext,
|
|
296
|
+
abortSignal
|
|
297
|
+
});
|
|
298
|
+
return streamResult;
|
|
299
|
+
} catch (error) {
|
|
300
|
+
return handleError(error, "error streaming agent response");
|
|
301
|
+
}
|
|
302
|
+
}
|
|
274
303
|
|
|
275
|
-
export { agents_exports, generateHandler, getAgentByIdHandler, getAgentsHandler, getEvalsByAgentIdHandler, getLiveEvalsByAgentIdHandler, streamGenerateHandler };
|
|
304
|
+
export { agents_exports, generateHandler, getAgentByIdHandler, getAgentsHandler, getEvalsByAgentIdHandler, getLiveEvalsByAgentIdHandler, streamGenerateHandler, streamVNextGenerateHandler };
|
|
@@ -10,13 +10,17 @@ var core = require('@mastra/core');
|
|
|
10
10
|
var memory_exports = {};
|
|
11
11
|
chunk75ZPJI57_cjs.__export(memory_exports, {
|
|
12
12
|
createThreadHandler: () => createThreadHandler,
|
|
13
|
+
deleteMessagesHandler: () => deleteMessagesHandler,
|
|
13
14
|
deleteThreadHandler: () => deleteThreadHandler,
|
|
15
|
+
getMemoryConfigHandler: () => getMemoryConfigHandler,
|
|
14
16
|
getMemoryStatusHandler: () => getMemoryStatusHandler,
|
|
15
17
|
getMessagesHandler: () => getMessagesHandler,
|
|
18
|
+
getMessagesPaginatedHandler: () => getMessagesPaginatedHandler,
|
|
16
19
|
getThreadByIdHandler: () => getThreadByIdHandler,
|
|
17
20
|
getThreadsHandler: () => getThreadsHandler,
|
|
18
21
|
getWorkingMemoryHandler: () => getWorkingMemoryHandler,
|
|
19
22
|
saveMessagesHandler: () => saveMessagesHandler,
|
|
23
|
+
searchMemoryHandler: () => searchMemoryHandler,
|
|
20
24
|
updateThreadHandler: () => updateThreadHandler,
|
|
21
25
|
updateWorkingMemoryHandler: () => updateWorkingMemoryHandler
|
|
22
26
|
});
|
|
@@ -35,7 +39,7 @@ async function getMemoryFromContext({
|
|
|
35
39
|
throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Network not found" });
|
|
36
40
|
}
|
|
37
41
|
if (agent) {
|
|
38
|
-
return agent?.getMemory() || mastra.getMemory();
|
|
42
|
+
return await agent?.getMemory() || mastra.getMemory();
|
|
39
43
|
}
|
|
40
44
|
if (network) {
|
|
41
45
|
return await network?.getMemory({ runtimeContext }) || mastra.getMemory();
|
|
@@ -58,12 +62,31 @@ async function getMemoryStatusHandler({
|
|
|
58
62
|
return chunkPZQDCRPV_cjs.handleError(error, "Error getting memory status");
|
|
59
63
|
}
|
|
60
64
|
}
|
|
65
|
+
async function getMemoryConfigHandler({
|
|
66
|
+
mastra,
|
|
67
|
+
agentId,
|
|
68
|
+
networkId,
|
|
69
|
+
runtimeContext
|
|
70
|
+
}) {
|
|
71
|
+
try {
|
|
72
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
73
|
+
if (!memory) {
|
|
74
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Memory is not initialized" });
|
|
75
|
+
}
|
|
76
|
+
const config = memory.getMergedThreadConfig({});
|
|
77
|
+
return { config };
|
|
78
|
+
} catch (error) {
|
|
79
|
+
return chunkPZQDCRPV_cjs.handleError(error, "Error getting memory configuration");
|
|
80
|
+
}
|
|
81
|
+
}
|
|
61
82
|
async function getThreadsHandler({
|
|
62
83
|
mastra,
|
|
63
84
|
agentId,
|
|
64
85
|
resourceId,
|
|
65
86
|
networkId,
|
|
66
|
-
runtimeContext
|
|
87
|
+
runtimeContext,
|
|
88
|
+
orderBy,
|
|
89
|
+
sortDirection
|
|
67
90
|
}) {
|
|
68
91
|
try {
|
|
69
92
|
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
@@ -71,7 +94,11 @@ async function getThreadsHandler({
|
|
|
71
94
|
throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Memory is not initialized" });
|
|
72
95
|
}
|
|
73
96
|
chunkCCGRCYWJ_cjs.validateBody({ resourceId });
|
|
74
|
-
const threads = await memory.getThreadsByResourceId({
|
|
97
|
+
const threads = await memory.getThreadsByResourceId({
|
|
98
|
+
resourceId,
|
|
99
|
+
orderBy,
|
|
100
|
+
sortDirection
|
|
101
|
+
});
|
|
75
102
|
return threads;
|
|
76
103
|
} catch (error) {
|
|
77
104
|
return chunkPZQDCRPV_cjs.handleError(error, "Error getting threads");
|
|
@@ -117,9 +144,15 @@ async function saveMessagesHandler({
|
|
|
117
144
|
if (!Array.isArray(body.messages)) {
|
|
118
145
|
throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Messages should be an array" });
|
|
119
146
|
}
|
|
147
|
+
const invalidMessages = body.messages.filter((message) => !message.threadId || !message.resourceId);
|
|
148
|
+
if (invalidMessages.length > 0) {
|
|
149
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(400, {
|
|
150
|
+
message: `All messages must have threadId and resourceId fields. Found ${invalidMessages.length} invalid message(s).`
|
|
151
|
+
});
|
|
152
|
+
}
|
|
120
153
|
const processedMessages = body.messages.map((message) => ({
|
|
121
154
|
...message,
|
|
122
|
-
id: memory.generateId(),
|
|
155
|
+
id: message.id || memory.generateId(),
|
|
123
156
|
createdAt: message.createdAt ? new Date(message.createdAt) : /* @__PURE__ */ new Date()
|
|
124
157
|
}));
|
|
125
158
|
const result = await memory.saveMessages({ messages: processedMessages, memoryConfig: {} });
|
|
@@ -212,6 +245,29 @@ async function deleteThreadHandler({
|
|
|
212
245
|
return chunkPZQDCRPV_cjs.handleError(error, "Error deleting thread");
|
|
213
246
|
}
|
|
214
247
|
}
|
|
248
|
+
async function getMessagesPaginatedHandler({
|
|
249
|
+
mastra,
|
|
250
|
+
threadId,
|
|
251
|
+
resourceId,
|
|
252
|
+
selectBy,
|
|
253
|
+
format
|
|
254
|
+
}) {
|
|
255
|
+
try {
|
|
256
|
+
chunkCCGRCYWJ_cjs.validateBody({ threadId });
|
|
257
|
+
const storage = mastra.getStorage();
|
|
258
|
+
if (!storage) {
|
|
259
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Storage is not initialized" });
|
|
260
|
+
}
|
|
261
|
+
const thread = await storage.getThreadById({ threadId });
|
|
262
|
+
if (!thread) {
|
|
263
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Thread not found" });
|
|
264
|
+
}
|
|
265
|
+
const result = await storage.getMessagesPaginated({ threadId, resourceId, selectBy, format });
|
|
266
|
+
return result;
|
|
267
|
+
} catch (error) {
|
|
268
|
+
return chunkPZQDCRPV_cjs.handleError(error, "Error getting messages");
|
|
269
|
+
}
|
|
270
|
+
}
|
|
215
271
|
async function getMessagesHandler({
|
|
216
272
|
mastra,
|
|
217
273
|
agentId,
|
|
@@ -307,15 +363,214 @@ async function updateWorkingMemoryHandler({
|
|
|
307
363
|
return chunkPZQDCRPV_cjs.handleError(error, "Error updating working memory");
|
|
308
364
|
}
|
|
309
365
|
}
|
|
366
|
+
async function deleteMessagesHandler({
|
|
367
|
+
mastra,
|
|
368
|
+
agentId,
|
|
369
|
+
messageIds,
|
|
370
|
+
networkId,
|
|
371
|
+
runtimeContext
|
|
372
|
+
}) {
|
|
373
|
+
try {
|
|
374
|
+
if (messageIds === void 0 || messageIds === null) {
|
|
375
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "messageIds is required" });
|
|
376
|
+
}
|
|
377
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
378
|
+
if (!memory) {
|
|
379
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Memory is not initialized" });
|
|
380
|
+
}
|
|
381
|
+
await memory.deleteMessages(messageIds);
|
|
382
|
+
let count = 1;
|
|
383
|
+
if (Array.isArray(messageIds)) {
|
|
384
|
+
count = messageIds.length;
|
|
385
|
+
}
|
|
386
|
+
return { success: true, message: `${count} message${count === 1 ? "" : "s"} deleted successfully` };
|
|
387
|
+
} catch (error) {
|
|
388
|
+
return chunkPZQDCRPV_cjs.handleError(error, "Error deleting messages");
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
async function searchMemoryHandler({
|
|
392
|
+
mastra,
|
|
393
|
+
agentId,
|
|
394
|
+
searchQuery,
|
|
395
|
+
resourceId,
|
|
396
|
+
threadId,
|
|
397
|
+
limit = 20,
|
|
398
|
+
networkId,
|
|
399
|
+
runtimeContext,
|
|
400
|
+
memoryConfig
|
|
401
|
+
}) {
|
|
402
|
+
try {
|
|
403
|
+
chunkCCGRCYWJ_cjs.validateBody({ searchQuery, resourceId });
|
|
404
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
405
|
+
if (!memory) {
|
|
406
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Memory is not initialized" });
|
|
407
|
+
}
|
|
408
|
+
const config = memory.getMergedThreadConfig(memoryConfig || {});
|
|
409
|
+
const hasSemanticRecall = !!config?.semanticRecall;
|
|
410
|
+
const resourceScope = typeof config?.semanticRecall === "object" && config?.semanticRecall?.scope === "resource";
|
|
411
|
+
if (threadId && !resourceScope) {
|
|
412
|
+
const thread = await memory.getThreadById({ threadId });
|
|
413
|
+
if (!thread) {
|
|
414
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Thread not found" });
|
|
415
|
+
}
|
|
416
|
+
if (thread.resourceId !== resourceId) {
|
|
417
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(403, { message: "Thread does not belong to the specified resource" });
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
const searchResults = [];
|
|
421
|
+
const messageMap = /* @__PURE__ */ new Map();
|
|
422
|
+
if (threadId && !resourceScope) {
|
|
423
|
+
const thread = await memory.getThreadById({ threadId });
|
|
424
|
+
if (!thread) {
|
|
425
|
+
return {
|
|
426
|
+
results: [],
|
|
427
|
+
count: 0,
|
|
428
|
+
query: searchQuery,
|
|
429
|
+
searchScope: "thread",
|
|
430
|
+
searchType: hasSemanticRecall ? "semantic" : "text"
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
if (!threadId || resourceScope) {
|
|
435
|
+
const threads = await memory.getThreadsByResourceId({ resourceId });
|
|
436
|
+
if (threads.length === 0) {
|
|
437
|
+
return {
|
|
438
|
+
results: [],
|
|
439
|
+
count: 0,
|
|
440
|
+
query: searchQuery,
|
|
441
|
+
searchScope: "resource",
|
|
442
|
+
searchType: hasSemanticRecall ? "semantic" : "text"
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
for (const thread of threads) {
|
|
446
|
+
const result = await memory.rememberMessages({
|
|
447
|
+
threadId: thread.id,
|
|
448
|
+
resourceId,
|
|
449
|
+
vectorMessageSearch: searchQuery,
|
|
450
|
+
config
|
|
451
|
+
});
|
|
452
|
+
const threadMessages = (await memory.query({ threadId: thread.id, format: "v2" })).uiMessages;
|
|
453
|
+
result.messagesV2.forEach((msg) => {
|
|
454
|
+
if (messageMap.has(msg.id)) return;
|
|
455
|
+
messageMap.set(msg.id, true);
|
|
456
|
+
const content = msg.content.content || msg.content.parts?.map((p) => p.type === "text" ? p.text : "").join(" ") || "";
|
|
457
|
+
if (!hasSemanticRecall && !content.toLowerCase().includes(searchQuery.toLowerCase())) {
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
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: msg.threadId || thread.id,
|
|
467
|
+
threadTitle: thread.title || msg.threadId || thread.id
|
|
468
|
+
};
|
|
469
|
+
if (messageIndex !== -1) {
|
|
470
|
+
searchResult.context = {
|
|
471
|
+
before: threadMessages.slice(Math.max(0, messageIndex - 2), messageIndex).map((m) => ({
|
|
472
|
+
id: m.id,
|
|
473
|
+
role: m.role,
|
|
474
|
+
content: m.parts.map((p) => p.type === `text` ? p.text : null).filter(Boolean).join(`
|
|
475
|
+
`),
|
|
476
|
+
// @ts-ignore
|
|
477
|
+
createdAt: m.createdAt || /* @__PURE__ */ new Date()
|
|
478
|
+
})),
|
|
479
|
+
after: threadMessages.slice(messageIndex + 1, messageIndex + 3).map((m) => ({
|
|
480
|
+
id: m.id,
|
|
481
|
+
role: m.role,
|
|
482
|
+
content: m.parts.map((p) => p.type === `text` ? p.text : null).filter(Boolean).join(`
|
|
483
|
+
`),
|
|
484
|
+
// @ts-ignore
|
|
485
|
+
createdAt: m.createdAt || /* @__PURE__ */ new Date()
|
|
486
|
+
}))
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
searchResults.push(searchResult);
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
} else if (threadId) {
|
|
493
|
+
const thread = await memory.getThreadById({ threadId });
|
|
494
|
+
if (!thread) {
|
|
495
|
+
return {
|
|
496
|
+
results: [],
|
|
497
|
+
count: 0,
|
|
498
|
+
query: searchQuery,
|
|
499
|
+
searchScope: "thread",
|
|
500
|
+
searchType: hasSemanticRecall ? "semantic" : "text"
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
const result = await memory.rememberMessages({
|
|
504
|
+
threadId,
|
|
505
|
+
resourceId,
|
|
506
|
+
vectorMessageSearch: searchQuery,
|
|
507
|
+
config
|
|
508
|
+
});
|
|
509
|
+
const threadMessages = (await memory.query({ threadId, format: "v2" })).uiMessages;
|
|
510
|
+
result.messagesV2.forEach((msg) => {
|
|
511
|
+
if (messageMap.has(msg.id)) return;
|
|
512
|
+
messageMap.set(msg.id, true);
|
|
513
|
+
const content = msg.content.content || msg.content.parts?.map((p) => p.type === "text" ? p.text : "").join(" ") || "";
|
|
514
|
+
if (!hasSemanticRecall && !content.toLowerCase().includes(searchQuery.toLowerCase())) {
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
const messageIndex = threadMessages.findIndex((m) => m.id === msg.id);
|
|
518
|
+
const searchResult = {
|
|
519
|
+
id: msg.id,
|
|
520
|
+
role: msg.role,
|
|
521
|
+
content,
|
|
522
|
+
createdAt: msg.createdAt,
|
|
523
|
+
threadId,
|
|
524
|
+
threadTitle: thread?.title || threadId
|
|
525
|
+
};
|
|
526
|
+
if (messageIndex !== -1) {
|
|
527
|
+
searchResult.context = {
|
|
528
|
+
before: threadMessages.slice(Math.max(0, messageIndex - 2), messageIndex).map((m) => ({
|
|
529
|
+
id: m.id,
|
|
530
|
+
role: m.role,
|
|
531
|
+
content: m.parts.map((p) => p.type === `text` ? p.text : null).filter(Boolean).join(`
|
|
532
|
+
`),
|
|
533
|
+
// @ts-ignore
|
|
534
|
+
createdAt: m.createdAt || /* @__PURE__ */ new Date()
|
|
535
|
+
})),
|
|
536
|
+
after: threadMessages.slice(messageIndex + 1, messageIndex + 3).map((m) => ({
|
|
537
|
+
id: m.id,
|
|
538
|
+
role: m.role,
|
|
539
|
+
content: m.parts.map((p) => p.type === `text` ? p.text : null).filter(Boolean).join(`
|
|
540
|
+
`),
|
|
541
|
+
// @ts-ignore
|
|
542
|
+
createdAt: m.createdAt || /* @__PURE__ */ new Date()
|
|
543
|
+
}))
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
searchResults.push(searchResult);
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
const sortedResults = searchResults.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()).slice(0, limit);
|
|
550
|
+
return {
|
|
551
|
+
results: sortedResults,
|
|
552
|
+
count: sortedResults.length,
|
|
553
|
+
query: searchQuery,
|
|
554
|
+
searchScope: resourceScope ? "resource" : "thread",
|
|
555
|
+
searchType: hasSemanticRecall ? "semantic" : "text"
|
|
556
|
+
};
|
|
557
|
+
} catch (error) {
|
|
558
|
+
return chunkPZQDCRPV_cjs.handleError(error, "Error searching memory");
|
|
559
|
+
}
|
|
560
|
+
}
|
|
310
561
|
|
|
311
562
|
exports.createThreadHandler = createThreadHandler;
|
|
563
|
+
exports.deleteMessagesHandler = deleteMessagesHandler;
|
|
312
564
|
exports.deleteThreadHandler = deleteThreadHandler;
|
|
565
|
+
exports.getMemoryConfigHandler = getMemoryConfigHandler;
|
|
313
566
|
exports.getMemoryStatusHandler = getMemoryStatusHandler;
|
|
314
567
|
exports.getMessagesHandler = getMessagesHandler;
|
|
568
|
+
exports.getMessagesPaginatedHandler = getMessagesPaginatedHandler;
|
|
315
569
|
exports.getThreadByIdHandler = getThreadByIdHandler;
|
|
316
570
|
exports.getThreadsHandler = getThreadsHandler;
|
|
317
571
|
exports.getWorkingMemoryHandler = getWorkingMemoryHandler;
|
|
318
572
|
exports.memory_exports = memory_exports;
|
|
319
573
|
exports.saveMessagesHandler = saveMessagesHandler;
|
|
574
|
+
exports.searchMemoryHandler = searchMemoryHandler;
|
|
320
575
|
exports.updateThreadHandler = updateThreadHandler;
|
|
321
576
|
exports.updateWorkingMemoryHandler = updateWorkingMemoryHandler;
|