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