@mastra/server 0.0.0-tool-call-parts-20250630193309 → 0.0.0-transpile-packages-20250730132657

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.
Files changed (78) hide show
  1. package/LICENSE.md +11 -42
  2. package/dist/_tsup-dts-rollup.d.cts +212 -23
  3. package/dist/_tsup-dts-rollup.d.ts +212 -23
  4. package/dist/{chunk-OCWPVYNI.cjs → chunk-2KZFMI6P.cjs} +1 -0
  5. package/dist/{chunk-VPNDC2DI.cjs → chunk-2SLFAFTR.cjs} +12 -12
  6. package/dist/{chunk-MEGCYGBU.js → chunk-36BLNJHS.js} +37 -37
  7. package/dist/chunk-424T5F2J.cjs +157 -0
  8. package/dist/{chunk-QLG2PFHE.js → chunk-4D66QEKC.js} +2 -2
  9. package/dist/{chunk-L3VURYKY.cjs → chunk-6KP2OBYA.cjs} +177 -68
  10. package/dist/chunk-6T5JUKOQ.js +149 -0
  11. package/dist/{chunk-BNEY4P4P.cjs → chunk-7TP2LX5L.cjs} +16 -16
  12. package/dist/{chunk-57CJTIPW.cjs → chunk-CCGRCYWJ.cjs} +2 -2
  13. package/dist/{chunk-C7564HUT.js → chunk-CRCR3ZUK.js} +3 -3
  14. package/dist/{chunk-B4MQFJ7G.cjs → chunk-G7KH752Y.cjs} +5 -5
  15. package/dist/{chunk-LI436ITD.cjs → chunk-HISYIDYH.cjs} +37 -37
  16. package/dist/{chunk-Z3PTO2AK.cjs → chunk-HRSAZUIK.cjs} +20 -20
  17. package/dist/{chunk-EP3XVEVC.cjs → chunk-I5JKUCGE.cjs} +50 -20
  18. package/dist/{chunk-R5VGYBV6.js → chunk-KHZKYUNR.js} +3 -3
  19. package/dist/{chunk-NYN7KFXL.js → chunk-LCM566I4.js} +1 -0
  20. package/dist/chunk-LF7P5PLR.js +14 -0
  21. package/dist/chunk-PZQDCRPV.cjs +16 -0
  22. package/dist/{chunk-IMBY5XUG.cjs → chunk-QGX47B5D.cjs} +7 -7
  23. package/dist/chunk-R3I7AJVL.js +507 -0
  24. package/dist/{chunk-BFOA2QQY.js → chunk-RG473F6Y.js} +2 -2
  25. package/dist/{chunk-WJY57THV.cjs → chunk-RHSWAXKB.cjs} +22 -22
  26. package/dist/{chunk-H5PTF3Y4.js → chunk-RSEO4XPX.js} +1 -1
  27. package/dist/{chunk-72LOJGUV.js → chunk-T7HAZC2E.js} +118 -12
  28. package/dist/chunk-TTPJHP42.cjs +522 -0
  29. package/dist/{chunk-NGHSYB5B.cjs → chunk-UCV4247U.cjs} +43 -43
  30. package/dist/{chunk-2BCWG3EZ.js → chunk-VTTN2FW3.js} +36 -7
  31. package/dist/{chunk-EWLR2PNT.js → chunk-WBRFIHSF.js} +4 -4
  32. package/dist/{chunk-55DOQLP6.js → chunk-WNVFNNWN.js} +2 -2
  33. package/dist/{chunk-E2LP4J6K.js → chunk-ZYAFP2AV.js} +8 -8
  34. package/dist/server/handlers/agents.cjs +11 -7
  35. package/dist/server/handlers/agents.d.cts +1 -0
  36. package/dist/server/handlers/agents.d.ts +1 -0
  37. package/dist/server/handlers/agents.js +1 -1
  38. package/dist/server/handlers/error.cjs +2 -2
  39. package/dist/server/handlers/error.js +1 -1
  40. package/dist/server/handlers/legacyWorkflows.cjs +11 -11
  41. package/dist/server/handlers/legacyWorkflows.js +1 -1
  42. package/dist/server/handlers/logs.cjs +4 -4
  43. package/dist/server/handlers/logs.js +1 -1
  44. package/dist/server/handlers/memory.cjs +29 -9
  45. package/dist/server/handlers/memory.d.cts +5 -0
  46. package/dist/server/handlers/memory.d.ts +5 -0
  47. package/dist/server/handlers/memory.js +1 -1
  48. package/dist/server/handlers/network.cjs +5 -5
  49. package/dist/server/handlers/network.js +1 -1
  50. package/dist/server/handlers/scores.cjs +30 -0
  51. package/dist/server/handlers/scores.d.cts +6 -0
  52. package/dist/server/handlers/scores.d.ts +6 -0
  53. package/dist/server/handlers/scores.js +1 -0
  54. package/dist/server/handlers/telemetry.cjs +3 -3
  55. package/dist/server/handlers/telemetry.js +1 -1
  56. package/dist/server/handlers/tools.cjs +5 -5
  57. package/dist/server/handlers/tools.js +1 -1
  58. package/dist/server/handlers/utils.cjs +2 -2
  59. package/dist/server/handlers/utils.js +1 -1
  60. package/dist/server/handlers/vNextNetwork.cjs +23 -23
  61. package/dist/server/handlers/vNextNetwork.js +4 -4
  62. package/dist/server/handlers/vector.cjs +7 -7
  63. package/dist/server/handlers/vector.js +1 -1
  64. package/dist/server/handlers/voice.cjs +5 -5
  65. package/dist/server/handlers/voice.js +1 -1
  66. package/dist/server/handlers/workflows.cjs +25 -13
  67. package/dist/server/handlers/workflows.d.cts +3 -0
  68. package/dist/server/handlers/workflows.d.ts +3 -0
  69. package/dist/server/handlers/workflows.js +1 -1
  70. package/dist/server/handlers.cjs +28 -23
  71. package/dist/server/handlers.d.cts +3 -2
  72. package/dist/server/handlers.d.ts +3 -2
  73. package/dist/server/handlers.js +11 -10
  74. package/package.json +8 -7
  75. package/dist/chunk-64U3UDTH.cjs +0 -13
  76. package/dist/chunk-M5ABIP7D.js +0 -11
  77. package/dist/chunk-RQ2Z56EC.cjs +0 -251
  78. package/dist/chunk-XUIJ7WRT.js +0 -241
@@ -0,0 +1,522 @@
1
+ 'use strict';
2
+
3
+ var chunkCCGRCYWJ_cjs = require('./chunk-CCGRCYWJ.cjs');
4
+ var chunkPZQDCRPV_cjs = require('./chunk-PZQDCRPV.cjs');
5
+ var chunk2KZFMI6P_cjs = require('./chunk-2KZFMI6P.cjs');
6
+ var chunk75ZPJI57_cjs = require('./chunk-75ZPJI57.cjs');
7
+ var core = require('@mastra/core');
8
+
9
+ // src/server/handlers/memory.ts
10
+ var memory_exports = {};
11
+ chunk75ZPJI57_cjs.__export(memory_exports, {
12
+ createThreadHandler: () => createThreadHandler,
13
+ deleteThreadHandler: () => deleteThreadHandler,
14
+ getMemoryConfigHandler: () => getMemoryConfigHandler,
15
+ getMemoryStatusHandler: () => getMemoryStatusHandler,
16
+ getMessagesHandler: () => getMessagesHandler,
17
+ getMessagesPaginatedHandler: () => getMessagesPaginatedHandler,
18
+ getThreadByIdHandler: () => getThreadByIdHandler,
19
+ getThreadsHandler: () => getThreadsHandler,
20
+ getWorkingMemoryHandler: () => getWorkingMemoryHandler,
21
+ saveMessagesHandler: () => saveMessagesHandler,
22
+ searchMemoryHandler: () => searchMemoryHandler,
23
+ updateThreadHandler: () => updateThreadHandler,
24
+ updateWorkingMemoryHandler: () => updateWorkingMemoryHandler
25
+ });
26
+ async function getMemoryFromContext({
27
+ mastra,
28
+ agentId,
29
+ networkId,
30
+ runtimeContext
31
+ }) {
32
+ const agent = agentId ? mastra.getAgent(agentId) : null;
33
+ if (agentId && !agent) {
34
+ throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Agent not found" });
35
+ }
36
+ const network = networkId ? mastra.vnext_getNetwork(networkId) : null;
37
+ if (networkId && !network) {
38
+ throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Network not found" });
39
+ }
40
+ if (agent) {
41
+ return await agent?.getMemory() || mastra.getMemory();
42
+ }
43
+ if (network) {
44
+ return await network?.getMemory({ runtimeContext }) || mastra.getMemory();
45
+ }
46
+ return mastra.getMemory();
47
+ }
48
+ async function getMemoryStatusHandler({
49
+ mastra,
50
+ agentId,
51
+ networkId,
52
+ runtimeContext
53
+ }) {
54
+ try {
55
+ const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
56
+ if (!memory) {
57
+ return { result: false };
58
+ }
59
+ return { result: true };
60
+ } catch (error) {
61
+ return chunkPZQDCRPV_cjs.handleError(error, "Error getting memory status");
62
+ }
63
+ }
64
+ async function getMemoryConfigHandler({
65
+ mastra,
66
+ agentId,
67
+ networkId,
68
+ runtimeContext
69
+ }) {
70
+ try {
71
+ const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
72
+ if (!memory) {
73
+ throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Memory is not initialized" });
74
+ }
75
+ const config = memory.getMergedThreadConfig({});
76
+ return { config };
77
+ } catch (error) {
78
+ return chunkPZQDCRPV_cjs.handleError(error, "Error getting memory configuration");
79
+ }
80
+ }
81
+ async function getThreadsHandler({
82
+ mastra,
83
+ agentId,
84
+ resourceId,
85
+ networkId,
86
+ runtimeContext
87
+ }) {
88
+ try {
89
+ const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
90
+ if (!memory) {
91
+ throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Memory is not initialized" });
92
+ }
93
+ chunkCCGRCYWJ_cjs.validateBody({ resourceId });
94
+ const threads = await memory.getThreadsByResourceId({ resourceId });
95
+ return threads;
96
+ } catch (error) {
97
+ return chunkPZQDCRPV_cjs.handleError(error, "Error getting threads");
98
+ }
99
+ }
100
+ async function getThreadByIdHandler({
101
+ mastra,
102
+ agentId,
103
+ threadId,
104
+ networkId,
105
+ runtimeContext
106
+ }) {
107
+ try {
108
+ chunkCCGRCYWJ_cjs.validateBody({ threadId });
109
+ const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
110
+ if (!memory) {
111
+ throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Memory is not initialized" });
112
+ }
113
+ const thread = await memory.getThreadById({ threadId });
114
+ if (!thread) {
115
+ throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Thread not found" });
116
+ }
117
+ return thread;
118
+ } catch (error) {
119
+ return chunkPZQDCRPV_cjs.handleError(error, "Error getting thread");
120
+ }
121
+ }
122
+ async function saveMessagesHandler({
123
+ mastra,
124
+ agentId,
125
+ body,
126
+ networkId,
127
+ runtimeContext
128
+ }) {
129
+ try {
130
+ const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
131
+ if (!memory) {
132
+ throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Memory is not initialized" });
133
+ }
134
+ if (!body?.messages) {
135
+ throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Messages are required" });
136
+ }
137
+ if (!Array.isArray(body.messages)) {
138
+ throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Messages should be an array" });
139
+ }
140
+ const invalidMessages = body.messages.filter((message) => !message.threadId || !message.resourceId);
141
+ if (invalidMessages.length > 0) {
142
+ throw new chunk2KZFMI6P_cjs.HTTPException(400, {
143
+ message: `All messages must have threadId and resourceId fields. Found ${invalidMessages.length} invalid message(s).`
144
+ });
145
+ }
146
+ const processedMessages = body.messages.map((message) => ({
147
+ ...message,
148
+ id: message.id || memory.generateId(),
149
+ createdAt: message.createdAt ? new Date(message.createdAt) : /* @__PURE__ */ new Date()
150
+ }));
151
+ const result = await memory.saveMessages({ messages: processedMessages, memoryConfig: {} });
152
+ return result;
153
+ } catch (error) {
154
+ return chunkPZQDCRPV_cjs.handleError(error, "Error saving messages");
155
+ }
156
+ }
157
+ async function createThreadHandler({
158
+ mastra,
159
+ agentId,
160
+ body,
161
+ networkId,
162
+ runtimeContext
163
+ }) {
164
+ try {
165
+ const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
166
+ if (!memory) {
167
+ throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Memory is not initialized" });
168
+ }
169
+ chunkCCGRCYWJ_cjs.validateBody({ resourceId: body?.resourceId });
170
+ const result = await memory.createThread({
171
+ resourceId: body?.resourceId,
172
+ title: body?.title,
173
+ metadata: body?.metadata,
174
+ threadId: body?.threadId
175
+ });
176
+ return result;
177
+ } catch (error) {
178
+ return chunkPZQDCRPV_cjs.handleError(error, "Error saving thread to memory");
179
+ }
180
+ }
181
+ async function updateThreadHandler({
182
+ mastra,
183
+ agentId,
184
+ threadId,
185
+ body,
186
+ networkId,
187
+ runtimeContext
188
+ }) {
189
+ try {
190
+ const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
191
+ if (!body) {
192
+ throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Body is required" });
193
+ }
194
+ const { title, metadata, resourceId } = body;
195
+ const updatedAt = /* @__PURE__ */ new Date();
196
+ chunkCCGRCYWJ_cjs.validateBody({ threadId });
197
+ if (!memory) {
198
+ throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Memory is not initialized" });
199
+ }
200
+ const thread = await memory.getThreadById({ threadId });
201
+ if (!thread) {
202
+ throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Thread not found" });
203
+ }
204
+ const updatedThread = {
205
+ ...thread,
206
+ title: title || thread.title,
207
+ metadata: metadata || thread.metadata,
208
+ resourceId: resourceId || thread.resourceId,
209
+ createdAt: thread.createdAt,
210
+ updatedAt
211
+ };
212
+ const result = await memory.saveThread({ thread: updatedThread });
213
+ return result;
214
+ } catch (error) {
215
+ return chunkPZQDCRPV_cjs.handleError(error, "Error updating thread");
216
+ }
217
+ }
218
+ async function deleteThreadHandler({
219
+ mastra,
220
+ agentId,
221
+ threadId,
222
+ networkId,
223
+ runtimeContext
224
+ }) {
225
+ try {
226
+ chunkCCGRCYWJ_cjs.validateBody({ threadId });
227
+ const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
228
+ if (!memory) {
229
+ throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Memory is not initialized" });
230
+ }
231
+ const thread = await memory.getThreadById({ threadId });
232
+ if (!thread) {
233
+ throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Thread not found" });
234
+ }
235
+ await memory.deleteThread(threadId);
236
+ return { result: "Thread deleted" };
237
+ } catch (error) {
238
+ return chunkPZQDCRPV_cjs.handleError(error, "Error deleting thread");
239
+ }
240
+ }
241
+ async function getMessagesPaginatedHandler({
242
+ mastra,
243
+ threadId,
244
+ resourceId,
245
+ selectBy,
246
+ format
247
+ }) {
248
+ try {
249
+ chunkCCGRCYWJ_cjs.validateBody({ threadId });
250
+ const storage = mastra.getStorage();
251
+ if (!storage) {
252
+ throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Storage is not initialized" });
253
+ }
254
+ const thread = await storage.getThreadById({ threadId });
255
+ if (!thread) {
256
+ throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Thread not found" });
257
+ }
258
+ const result = await storage.getMessagesPaginated({ threadId, resourceId, selectBy, format });
259
+ return result;
260
+ } catch (error) {
261
+ return chunkPZQDCRPV_cjs.handleError(error, "Error getting messages");
262
+ }
263
+ }
264
+ async function getMessagesHandler({
265
+ mastra,
266
+ agentId,
267
+ threadId,
268
+ limit,
269
+ networkId,
270
+ runtimeContext
271
+ }) {
272
+ if (limit !== void 0 && (!Number.isInteger(limit) || limit <= 0)) {
273
+ throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Invalid limit: must be a positive integer" });
274
+ }
275
+ try {
276
+ chunkCCGRCYWJ_cjs.validateBody({ threadId });
277
+ const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
278
+ if (!memory) {
279
+ throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Memory is not initialized" });
280
+ }
281
+ const thread = await memory.getThreadById({ threadId });
282
+ if (!thread) {
283
+ throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Thread not found" });
284
+ }
285
+ const result = await memory.query({
286
+ threadId,
287
+ ...limit && { selectBy: { last: limit } }
288
+ });
289
+ return { messages: result.messages, uiMessages: result.uiMessages };
290
+ } catch (error) {
291
+ return chunkPZQDCRPV_cjs.handleError(error, "Error getting messages");
292
+ }
293
+ }
294
+ async function getWorkingMemoryHandler({
295
+ mastra,
296
+ agentId,
297
+ threadId,
298
+ resourceId,
299
+ networkId,
300
+ runtimeContext,
301
+ memoryConfig
302
+ }) {
303
+ try {
304
+ const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
305
+ chunkCCGRCYWJ_cjs.validateBody({ threadId });
306
+ if (!memory) {
307
+ throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Memory is not initialized" });
308
+ }
309
+ const thread = await memory.getThreadById({ threadId });
310
+ const threadExists = !!thread;
311
+ const template = await memory.getWorkingMemoryTemplate({ memoryConfig });
312
+ const workingMemoryTemplate = template?.format === "json" ? { ...template, content: JSON.stringify(core.generateEmptyFromSchema(template.content)) } : template;
313
+ const workingMemory = await memory.getWorkingMemory({ threadId, resourceId, memoryConfig });
314
+ const config = memory.getMergedThreadConfig(memoryConfig || {});
315
+ const source = config.workingMemory?.scope === "resource" && resourceId ? "resource" : "thread";
316
+ return { workingMemory, source, workingMemoryTemplate, threadExists };
317
+ } catch (error) {
318
+ return chunkPZQDCRPV_cjs.handleError(error, "Error getting working memory");
319
+ }
320
+ }
321
+ async function updateWorkingMemoryHandler({
322
+ mastra,
323
+ agentId,
324
+ threadId,
325
+ body,
326
+ networkId,
327
+ runtimeContext
328
+ }) {
329
+ try {
330
+ chunkCCGRCYWJ_cjs.validateBody({ threadId });
331
+ const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
332
+ const { resourceId, memoryConfig, workingMemory } = body;
333
+ if (!memory) {
334
+ throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Memory is not initialized" });
335
+ }
336
+ const thread = await memory.getThreadById({ threadId });
337
+ if (!thread) {
338
+ throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Thread not found" });
339
+ }
340
+ await memory.updateWorkingMemory({ threadId, resourceId, workingMemory, memoryConfig });
341
+ return { success: true };
342
+ } catch (error) {
343
+ return chunkPZQDCRPV_cjs.handleError(error, "Error updating working memory");
344
+ }
345
+ }
346
+ async function searchMemoryHandler({
347
+ mastra,
348
+ agentId,
349
+ searchQuery,
350
+ resourceId,
351
+ threadId,
352
+ limit = 20,
353
+ networkId,
354
+ runtimeContext,
355
+ memoryConfig
356
+ }) {
357
+ try {
358
+ chunkCCGRCYWJ_cjs.validateBody({ searchQuery, resourceId });
359
+ const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
360
+ if (!memory) {
361
+ throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Memory is not initialized" });
362
+ }
363
+ const config = memory.getMergedThreadConfig(memoryConfig || {});
364
+ const hasSemanticRecall = !!config?.semanticRecall;
365
+ const resourceScope = typeof config?.semanticRecall === "object" && config?.semanticRecall?.scope === "resource";
366
+ if (threadId && !resourceScope) {
367
+ const thread = await memory.getThreadById({ threadId });
368
+ if (!thread) {
369
+ throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Thread not found" });
370
+ }
371
+ if (thread.resourceId !== resourceId) {
372
+ throw new chunk2KZFMI6P_cjs.HTTPException(403, { message: "Thread does not belong to the specified resource" });
373
+ }
374
+ }
375
+ const searchResults = [];
376
+ const messageMap = /* @__PURE__ */ new Map();
377
+ if (threadId && !resourceScope) {
378
+ const thread = await memory.getThreadById({ threadId });
379
+ if (!thread) {
380
+ return {
381
+ results: [],
382
+ count: 0,
383
+ query: searchQuery,
384
+ searchScope: "thread",
385
+ searchType: hasSemanticRecall ? "semantic" : "text"
386
+ };
387
+ }
388
+ }
389
+ if (!threadId || resourceScope) {
390
+ const threads = await memory.getThreadsByResourceId({ resourceId });
391
+ if (threads.length === 0) {
392
+ return {
393
+ results: [],
394
+ count: 0,
395
+ query: searchQuery,
396
+ searchScope: "resource",
397
+ searchType: hasSemanticRecall ? "semantic" : "text"
398
+ };
399
+ }
400
+ for (const thread of threads) {
401
+ const result = await memory.rememberMessages({
402
+ threadId: thread.id,
403
+ resourceId,
404
+ vectorMessageSearch: searchQuery,
405
+ config
406
+ });
407
+ const threadMessages = (await memory.query({ threadId: thread.id })).uiMessages;
408
+ result.messagesV2.forEach((msg) => {
409
+ if (messageMap.has(msg.id)) return;
410
+ messageMap.set(msg.id, true);
411
+ const content = msg.content.content || msg.content.parts?.map((p) => p.type === "text" ? p.text : "").join(" ") || "";
412
+ if (!hasSemanticRecall && !content.toLowerCase().includes(searchQuery.toLowerCase())) {
413
+ return;
414
+ }
415
+ const messageIndex = threadMessages.findIndex((m) => m.id === msg.id);
416
+ const searchResult = {
417
+ id: msg.id,
418
+ role: msg.role,
419
+ content,
420
+ createdAt: msg.createdAt,
421
+ threadId: msg.threadId || thread.id,
422
+ threadTitle: thread.title || msg.threadId || thread.id
423
+ };
424
+ if (messageIndex !== -1) {
425
+ searchResult.context = {
426
+ before: threadMessages.slice(Math.max(0, messageIndex - 2), messageIndex).map((m) => ({
427
+ id: m.id,
428
+ role: m.role,
429
+ content: m.content,
430
+ createdAt: m.createdAt || /* @__PURE__ */ new Date()
431
+ })),
432
+ after: threadMessages.slice(messageIndex + 1, messageIndex + 3).map((m) => ({
433
+ id: m.id,
434
+ role: m.role,
435
+ content: m.content,
436
+ createdAt: m.createdAt || /* @__PURE__ */ new Date()
437
+ }))
438
+ };
439
+ }
440
+ searchResults.push(searchResult);
441
+ });
442
+ }
443
+ } else if (threadId) {
444
+ const thread = await memory.getThreadById({ threadId });
445
+ if (!thread) {
446
+ return {
447
+ results: [],
448
+ count: 0,
449
+ query: searchQuery,
450
+ searchScope: "thread",
451
+ searchType: hasSemanticRecall ? "semantic" : "text"
452
+ };
453
+ }
454
+ const result = await memory.rememberMessages({
455
+ threadId,
456
+ resourceId,
457
+ vectorMessageSearch: searchQuery,
458
+ config
459
+ });
460
+ const threadMessages = (await memory.query({ threadId })).uiMessages;
461
+ result.messagesV2.forEach((msg) => {
462
+ if (messageMap.has(msg.id)) return;
463
+ messageMap.set(msg.id, true);
464
+ const content = msg.content.content || msg.content.parts?.map((p) => p.type === "text" ? p.text : "").join(" ") || "";
465
+ if (!hasSemanticRecall && !content.toLowerCase().includes(searchQuery.toLowerCase())) {
466
+ return;
467
+ }
468
+ const messageIndex = threadMessages.findIndex((m) => m.id === msg.id);
469
+ const searchResult = {
470
+ id: msg.id,
471
+ role: msg.role,
472
+ content,
473
+ createdAt: msg.createdAt,
474
+ threadId,
475
+ threadTitle: thread?.title || threadId
476
+ };
477
+ if (messageIndex !== -1) {
478
+ searchResult.context = {
479
+ before: threadMessages.slice(Math.max(0, messageIndex - 2), messageIndex).map((m) => ({
480
+ id: m.id,
481
+ role: m.role,
482
+ content: m.content,
483
+ createdAt: m.createdAt || /* @__PURE__ */ new Date()
484
+ })),
485
+ after: threadMessages.slice(messageIndex + 1, messageIndex + 3).map((m) => ({
486
+ id: m.id,
487
+ role: m.role,
488
+ content: m.content,
489
+ createdAt: m.createdAt || /* @__PURE__ */ new Date()
490
+ }))
491
+ };
492
+ }
493
+ searchResults.push(searchResult);
494
+ });
495
+ }
496
+ const sortedResults = searchResults.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()).slice(0, limit);
497
+ return {
498
+ results: sortedResults,
499
+ count: sortedResults.length,
500
+ query: searchQuery,
501
+ searchScope: resourceScope ? "resource" : "thread",
502
+ searchType: hasSemanticRecall ? "semantic" : "text"
503
+ };
504
+ } catch (error) {
505
+ return chunkPZQDCRPV_cjs.handleError(error, "Error searching memory");
506
+ }
507
+ }
508
+
509
+ exports.createThreadHandler = createThreadHandler;
510
+ exports.deleteThreadHandler = deleteThreadHandler;
511
+ exports.getMemoryConfigHandler = getMemoryConfigHandler;
512
+ exports.getMemoryStatusHandler = getMemoryStatusHandler;
513
+ exports.getMessagesHandler = getMessagesHandler;
514
+ exports.getMessagesPaginatedHandler = getMessagesPaginatedHandler;
515
+ exports.getThreadByIdHandler = getThreadByIdHandler;
516
+ exports.getThreadsHandler = getThreadsHandler;
517
+ exports.getWorkingMemoryHandler = getWorkingMemoryHandler;
518
+ exports.memory_exports = memory_exports;
519
+ exports.saveMessagesHandler = saveMessagesHandler;
520
+ exports.searchMemoryHandler = searchMemoryHandler;
521
+ exports.updateThreadHandler = updateThreadHandler;
522
+ exports.updateWorkingMemoryHandler = updateWorkingMemoryHandler;