@mastra/server 0.0.0-extend-clickhouse-20250418135620 → 0.0.0-feat-tool-input-validation-20250731232758

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