@mastra/server 0.0.0-fix-message-list-merge-20250718043058 → 0.0.0-fix-tool-call-history-20250730195323

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 (43) hide show
  1. package/dist/_tsup-dts-rollup.d.cts +169 -16
  2. package/dist/_tsup-dts-rollup.d.ts +169 -16
  3. package/dist/{chunk-MEGCYGBU.js → chunk-36BLNJHS.js} +37 -37
  4. package/dist/chunk-424T5F2J.cjs +157 -0
  5. package/dist/{chunk-B2PAS2IB.cjs → chunk-6KP2OBYA.cjs} +47 -17
  6. package/dist/chunk-6T5JUKOQ.js +149 -0
  7. package/dist/{chunk-LI436ITD.cjs → chunk-HISYIDYH.cjs} +37 -37
  8. package/dist/{chunk-FRVBFMO2.cjs → chunk-HRSAZUIK.cjs} +5 -5
  9. package/dist/{chunk-LZ3VJXSO.cjs → chunk-I5JKUCGE.cjs} +36 -6
  10. package/dist/{chunk-KOHWJYJT.js → chunk-PIAJQ75M.js} +248 -5
  11. package/dist/{chunk-H7DMHBKY.js → chunk-T7HAZC2E.js} +31 -2
  12. package/dist/{chunk-JMLYCXMK.cjs → chunk-UCV4247U.cjs} +7 -7
  13. package/dist/{chunk-3CNDE7QY.cjs → chunk-UEVYOKAH.cjs} +251 -4
  14. package/dist/{chunk-BK4XT6EG.js → chunk-VTTN2FW3.js} +32 -3
  15. package/dist/{chunk-LRCAAFUA.js → chunk-WBRFIHSF.js} +1 -1
  16. package/dist/{chunk-5PQQ42EZ.js → chunk-ZYAFP2AV.js} +1 -1
  17. package/dist/server/handlers/agents.cjs +11 -7
  18. package/dist/server/handlers/agents.d.cts +1 -0
  19. package/dist/server/handlers/agents.d.ts +1 -0
  20. package/dist/server/handlers/agents.js +1 -1
  21. package/dist/server/handlers/legacyWorkflows.cjs +11 -11
  22. package/dist/server/handlers/legacyWorkflows.js +1 -1
  23. package/dist/server/handlers/memory.cjs +27 -11
  24. package/dist/server/handlers/memory.d.cts +4 -0
  25. package/dist/server/handlers/memory.d.ts +4 -0
  26. package/dist/server/handlers/memory.js +1 -1
  27. package/dist/server/handlers/scores.cjs +30 -0
  28. package/dist/server/handlers/scores.d.cts +6 -0
  29. package/dist/server/handlers/scores.d.ts +6 -0
  30. package/dist/server/handlers/scores.js +1 -0
  31. package/dist/server/handlers/tools.cjs +5 -5
  32. package/dist/server/handlers/tools.js +1 -1
  33. package/dist/server/handlers/vNextNetwork.cjs +5 -5
  34. package/dist/server/handlers/vNextNetwork.js +1 -1
  35. package/dist/server/handlers/workflows.cjs +19 -15
  36. package/dist/server/handlers/workflows.d.cts +1 -0
  37. package/dist/server/handlers/workflows.d.ts +1 -0
  38. package/dist/server/handlers/workflows.js +1 -1
  39. package/dist/server/handlers.cjs +18 -13
  40. package/dist/server/handlers.d.cts +3 -2
  41. package/dist/server/handlers.d.ts +3 -2
  42. package/dist/server/handlers.js +6 -5
  43. package/package.json +5 -4
@@ -8,13 +8,17 @@ import { generateEmptyFromSchema } from '@mastra/core';
8
8
  var memory_exports = {};
9
9
  __export(memory_exports, {
10
10
  createThreadHandler: () => createThreadHandler,
11
+ deleteMessagesHandler: () => deleteMessagesHandler,
11
12
  deleteThreadHandler: () => deleteThreadHandler,
13
+ getMemoryConfigHandler: () => getMemoryConfigHandler,
12
14
  getMemoryStatusHandler: () => getMemoryStatusHandler,
13
15
  getMessagesHandler: () => getMessagesHandler,
16
+ getMessagesPaginatedHandler: () => getMessagesPaginatedHandler,
14
17
  getThreadByIdHandler: () => getThreadByIdHandler,
15
18
  getThreadsHandler: () => getThreadsHandler,
16
19
  getWorkingMemoryHandler: () => getWorkingMemoryHandler,
17
20
  saveMessagesHandler: () => saveMessagesHandler,
21
+ searchMemoryHandler: () => searchMemoryHandler,
18
22
  updateThreadHandler: () => updateThreadHandler,
19
23
  updateWorkingMemoryHandler: () => updateWorkingMemoryHandler
20
24
  });
@@ -33,7 +37,7 @@ async function getMemoryFromContext({
33
37
  throw new HTTPException(404, { message: "Network not found" });
34
38
  }
35
39
  if (agent) {
36
- return agent?.getMemory() || mastra.getMemory();
40
+ return await agent?.getMemory() || mastra.getMemory();
37
41
  }
38
42
  if (network) {
39
43
  return await network?.getMemory({ runtimeContext }) || mastra.getMemory();
@@ -56,12 +60,31 @@ async function getMemoryStatusHandler({
56
60
  return handleError(error, "Error getting memory status");
57
61
  }
58
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
+ }
59
80
  async function getThreadsHandler({
60
81
  mastra,
61
82
  agentId,
62
83
  resourceId,
63
84
  networkId,
64
- runtimeContext
85
+ runtimeContext,
86
+ orderBy,
87
+ sortDirection
65
88
  }) {
66
89
  try {
67
90
  const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
@@ -69,7 +92,11 @@ async function getThreadsHandler({
69
92
  throw new HTTPException(400, { message: "Memory is not initialized" });
70
93
  }
71
94
  validateBody({ resourceId });
72
- const threads = await memory.getThreadsByResourceId({ resourceId });
95
+ const threads = await memory.getThreadsByResourceId({
96
+ resourceId,
97
+ orderBy,
98
+ sortDirection
99
+ });
73
100
  return threads;
74
101
  } catch (error) {
75
102
  return handleError(error, "Error getting threads");
@@ -115,9 +142,15 @@ async function saveMessagesHandler({
115
142
  if (!Array.isArray(body.messages)) {
116
143
  throw new HTTPException(400, { message: "Messages should be an array" });
117
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
+ }
118
151
  const processedMessages = body.messages.map((message) => ({
119
152
  ...message,
120
- id: memory.generateId(),
153
+ id: message.id || memory.generateId(),
121
154
  createdAt: message.createdAt ? new Date(message.createdAt) : /* @__PURE__ */ new Date()
122
155
  }));
123
156
  const result = await memory.saveMessages({ messages: processedMessages, memoryConfig: {} });
@@ -210,6 +243,29 @@ async function deleteThreadHandler({
210
243
  return handleError(error, "Error deleting thread");
211
244
  }
212
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
+ }
213
269
  async function getMessagesHandler({
214
270
  mastra,
215
271
  agentId,
@@ -292,5 +348,192 @@ async function updateWorkingMemoryHandler({
292
348
  return handleError(error, "Error updating working memory");
293
349
  }
294
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
+ }
295
538
 
296
- export { createThreadHandler, deleteThreadHandler, getMemoryStatusHandler, getMessagesHandler, getThreadByIdHandler, getThreadsHandler, getWorkingMemoryHandler, memory_exports, saveMessagesHandler, updateThreadHandler, updateWorkingMemoryHandler };
539
+ export { createThreadHandler, deleteMessagesHandler, deleteThreadHandler, getMemoryConfigHandler, getMemoryStatusHandler, getMessagesHandler, getMessagesPaginatedHandler, getThreadByIdHandler, getThreadsHandler, getWorkingMemoryHandler, memory_exports, saveMessagesHandler, searchMemoryHandler, updateThreadHandler, updateWorkingMemoryHandler };
@@ -1,4 +1,4 @@
1
- import { stringify, esm_default } from './chunk-MEGCYGBU.js';
1
+ import { stringify, esm_default } from './chunk-36BLNJHS.js';
2
2
  import { handleError } from './chunk-LF7P5PLR.js';
3
3
  import { HTTPException } from './chunk-LCM566I4.js';
4
4
  import { __export } from './chunk-MLKGABMK.js';
@@ -19,6 +19,7 @@ __export(workflows_exports, {
19
19
  sendWorkflowRunEventHandler: () => sendWorkflowRunEventHandler,
20
20
  startAsyncWorkflowHandler: () => startAsyncWorkflowHandler,
21
21
  startWorkflowRunHandler: () => startWorkflowRunHandler,
22
+ streamVNextWorkflowHandler: () => streamVNextWorkflowHandler,
22
23
  streamWorkflowHandler: () => streamWorkflowHandler,
23
24
  watchWorkflowHandler: () => watchWorkflowHandler
24
25
  });
@@ -340,6 +341,34 @@ async function streamWorkflowHandler({
340
341
  return handleError(error, "Error executing workflow");
341
342
  }
342
343
  }
344
+ async function streamVNextWorkflowHandler({
345
+ mastra,
346
+ runtimeContext,
347
+ workflowId,
348
+ runId,
349
+ inputData
350
+ }) {
351
+ try {
352
+ if (!workflowId) {
353
+ throw new HTTPException(400, { message: "Workflow ID is required" });
354
+ }
355
+ if (!runId) {
356
+ throw new HTTPException(400, { message: "runId required to stream workflow" });
357
+ }
358
+ const { workflow } = await getWorkflowsFromSystem({ mastra, workflowId });
359
+ if (!workflow) {
360
+ throw new HTTPException(404, { message: "Workflow not found" });
361
+ }
362
+ const run = await workflow.createRunAsync({ runId });
363
+ const result = run.streamVNext({
364
+ inputData,
365
+ runtimeContext
366
+ });
367
+ return result;
368
+ } catch (error) {
369
+ return handleError(error, "Error streaming workflow");
370
+ }
371
+ }
343
372
  async function resumeAsyncWorkflowHandler({
344
373
  mastra,
345
374
  workflowId,
@@ -495,4 +524,4 @@ async function sendWorkflowRunEventHandler({
495
524
  }
496
525
  }
497
526
 
498
- export { cancelWorkflowRunHandler, createWorkflowRunHandler, getWorkflowByIdHandler, getWorkflowRunByIdHandler, getWorkflowRunExecutionResultHandler, getWorkflowRunsHandler, getWorkflowsHandler, resumeAsyncWorkflowHandler, resumeWorkflowHandler, sendWorkflowRunEventHandler, startAsyncWorkflowHandler, startWorkflowRunHandler, streamWorkflowHandler, watchWorkflowHandler, workflows_exports };
527
+ export { cancelWorkflowRunHandler, createWorkflowRunHandler, getWorkflowByIdHandler, getWorkflowRunByIdHandler, getWorkflowRunExecutionResultHandler, getWorkflowRunsHandler, getWorkflowsHandler, resumeAsyncWorkflowHandler, resumeWorkflowHandler, sendWorkflowRunEventHandler, startAsyncWorkflowHandler, startWorkflowRunHandler, streamVNextWorkflowHandler, streamWorkflowHandler, watchWorkflowHandler, workflows_exports };
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var chunkLI436ITD_cjs = require('./chunk-LI436ITD.cjs');
3
+ var chunkHISYIDYH_cjs = require('./chunk-HISYIDYH.cjs');
4
4
  var chunkPZQDCRPV_cjs = require('./chunk-PZQDCRPV.cjs');
5
5
  var chunk2KZFMI6P_cjs = require('./chunk-2KZFMI6P.cjs');
6
6
  var chunk75ZPJI57_cjs = require('./chunk-75ZPJI57.cjs');
@@ -31,15 +31,15 @@ async function getLegacyWorkflowsHandler({ mastra }) {
31
31
  serializedStepGraph: workflow.serializedStepGraph,
32
32
  serializedStepSubscriberGraph: workflow.serializedStepSubscriberGraph,
33
33
  name: workflow.name,
34
- triggerSchema: workflow.triggerSchema ? chunkLI436ITD_cjs.stringify(chunkLI436ITD_cjs.esm_default(workflow.triggerSchema)) : void 0,
34
+ triggerSchema: workflow.triggerSchema ? chunkHISYIDYH_cjs.stringify(chunkHISYIDYH_cjs.esm_default(workflow.triggerSchema)) : void 0,
35
35
  steps: Object.entries(workflow.steps).reduce((acc2, [key2, step]) => {
36
36
  const _step = step;
37
37
  acc2[key2] = {
38
38
  id: _step.id,
39
39
  description: _step.description,
40
40
  workflowId: _step.workflowId,
41
- inputSchema: _step.inputSchema ? chunkLI436ITD_cjs.stringify(chunkLI436ITD_cjs.esm_default(_step.inputSchema)) : void 0,
42
- outputSchema: _step.outputSchema ? chunkLI436ITD_cjs.stringify(chunkLI436ITD_cjs.esm_default(_step.outputSchema)) : void 0
41
+ inputSchema: _step.inputSchema ? chunkHISYIDYH_cjs.stringify(chunkHISYIDYH_cjs.esm_default(_step.inputSchema)) : void 0,
42
+ outputSchema: _step.outputSchema ? chunkHISYIDYH_cjs.stringify(chunkHISYIDYH_cjs.esm_default(_step.outputSchema)) : void 0
43
43
  };
44
44
  return acc2;
45
45
  }, {})
@@ -66,15 +66,15 @@ async function getLegacyWorkflowByIdHandler({ mastra, workflowId }) {
66
66
  serializedStepGraph: workflow.serializedStepGraph,
67
67
  serializedStepSubscriberGraph: workflow.serializedStepSubscriberGraph,
68
68
  name: workflow.name,
69
- triggerSchema: workflow.triggerSchema ? chunkLI436ITD_cjs.stringify(chunkLI436ITD_cjs.esm_default(workflow.triggerSchema)) : void 0,
69
+ triggerSchema: workflow.triggerSchema ? chunkHISYIDYH_cjs.stringify(chunkHISYIDYH_cjs.esm_default(workflow.triggerSchema)) : void 0,
70
70
  steps: Object.entries(workflow.steps).reduce((acc, [key, step]) => {
71
71
  const _step = step;
72
72
  acc[key] = {
73
73
  id: _step.id,
74
74
  description: _step.description,
75
75
  workflowId: _step.workflowId,
76
- inputSchema: _step.inputSchema ? chunkLI436ITD_cjs.stringify(chunkLI436ITD_cjs.esm_default(_step.inputSchema)) : void 0,
77
- outputSchema: _step.outputSchema ? chunkLI436ITD_cjs.stringify(chunkLI436ITD_cjs.esm_default(_step.outputSchema)) : void 0
76
+ inputSchema: _step.inputSchema ? chunkHISYIDYH_cjs.stringify(chunkHISYIDYH_cjs.esm_default(_step.inputSchema)) : void 0,
77
+ outputSchema: _step.outputSchema ? chunkHISYIDYH_cjs.stringify(chunkHISYIDYH_cjs.esm_default(_step.outputSchema)) : void 0
78
78
  };
79
79
  return acc;
80
80
  }, {})