@mastra/server 1.18.1-alpha.0 → 1.19.0

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 (31) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/dist/{chunk-U5XW3WOS.js → chunk-5CDCKTHB.js} +38 -20
  3. package/dist/chunk-5CDCKTHB.js.map +1 -0
  4. package/dist/{chunk-POOOTRUM.cjs → chunk-DAEHQAZC.cjs} +38 -20
  5. package/dist/chunk-DAEHQAZC.cjs.map +1 -0
  6. package/dist/{chunk-FTGC7KXM.js → chunk-TV5D5RRD.js} +132 -17
  7. package/dist/chunk-TV5D5RRD.js.map +1 -0
  8. package/dist/{chunk-MS7GNCN3.cjs → chunk-YSCEBPUJ.cjs} +145 -30
  9. package/dist/chunk-YSCEBPUJ.cjs.map +1 -0
  10. package/dist/docs/SKILL.md +1 -1
  11. package/dist/docs/assets/SOURCE_MAP.json +1 -1
  12. package/dist/docs/references/docs-server-custom-adapters.md +3 -1
  13. package/dist/{observational-memory-SASGM6OW-3S6D4TAF.js → observational-memory-B25SASRW-L4GYD2ZN.js} +3 -3
  14. package/dist/{observational-memory-SASGM6OW-3S6D4TAF.js.map → observational-memory-B25SASRW-L4GYD2ZN.js.map} +1 -1
  15. package/dist/{observational-memory-SASGM6OW-2G3RBRTW.cjs → observational-memory-B25SASRW-T76NSRNU.cjs} +26 -26
  16. package/dist/{observational-memory-SASGM6OW-2G3RBRTW.cjs.map → observational-memory-B25SASRW-T76NSRNU.cjs.map} +1 -1
  17. package/dist/server/handlers/agent-builder.cjs +16 -16
  18. package/dist/server/handlers/agent-builder.js +1 -1
  19. package/dist/server/handlers.cjs +2 -2
  20. package/dist/server/handlers.js +1 -1
  21. package/dist/server/server-adapter/index.cjs +24 -16
  22. package/dist/server/server-adapter/index.cjs.map +1 -1
  23. package/dist/server/server-adapter/index.js +10 -2
  24. package/dist/server/server-adapter/index.js.map +1 -1
  25. package/dist/server/server-adapter/routes/observability.d.ts +881 -119
  26. package/dist/server/server-adapter/routes/observability.d.ts.map +1 -1
  27. package/package.json +8 -8
  28. package/dist/chunk-FTGC7KXM.js.map +0 -1
  29. package/dist/chunk-MS7GNCN3.cjs.map +0 -1
  30. package/dist/chunk-POOOTRUM.cjs.map +0 -1
  31. package/dist/chunk-U5XW3WOS.js.map +0 -1
@@ -2,11 +2,11 @@
2
2
 
3
3
  var chunkR2ODPDKE_cjs = require('./chunk-R2ODPDKE.cjs');
4
4
  var chunk5N66PU3H_cjs = require('./chunk-5N66PU3H.cjs');
5
+ var chunkDOHUOYZS_cjs = require('./chunk-DOHUOYZS.cjs');
5
6
  var chunkEXKS4QPI_cjs = require('./chunk-EXKS4QPI.cjs');
6
7
  var chunkFPURK3UW_cjs = require('./chunk-FPURK3UW.cjs');
7
- var chunkPOOOTRUM_cjs = require('./chunk-POOOTRUM.cjs');
8
+ var chunkDAEHQAZC_cjs = require('./chunk-DAEHQAZC.cjs');
8
9
  var chunk3W54ZNYP_cjs = require('./chunk-3W54ZNYP.cjs');
9
- var chunkDOHUOYZS_cjs = require('./chunk-DOHUOYZS.cjs');
10
10
  var chunkHITLRKIU_cjs = require('./chunk-HITLRKIU.cjs');
11
11
  var chunkVTPTMQFA_cjs = require('./chunk-VTPTMQFA.cjs');
12
12
  var chunkWC4OPIB4_cjs = require('./chunk-WC4OPIB4.cjs');
@@ -16178,18 +16178,54 @@ async function resolveCursorMessage(memory, cursor, access) {
16178
16178
  }
16179
16179
  const memoryStore = await memory.getMemoryStore();
16180
16180
  const result = await memoryStore.listMessagesById({ messageIds: [normalized] });
16181
- const message = result.messages.find((message2) => message2.id === normalized);
16181
+ let message = result.messages.find((message2) => message2.id === normalized) ?? null;
16182
+ if (!message) {
16183
+ message = await resolveCursorMessageByRecall(memory, normalized, access);
16184
+ }
16182
16185
  if (!message) {
16183
16186
  throw new Error(`Could not resolve cursor message: ${cursor}`);
16184
16187
  }
16185
16188
  if (access?.resourceId && message.resourceId !== access.resourceId) {
16186
16189
  throw new Error(`Could not resolve cursor message: ${cursor}`);
16187
16190
  }
16188
- if (access?.threadScope && message.threadId !== access.threadScope) {
16191
+ if (access?.enforceThreadScope && access.threadScope && message.threadId !== access.threadScope) {
16189
16192
  throw new Error(`Could not resolve cursor message: ${cursor}`);
16190
16193
  }
16191
16194
  return message;
16192
16195
  }
16196
+ async function resolveCursorMessageByRecall(memory, cursor, access) {
16197
+ if (access?.enforceThreadScope && access.threadScope) {
16198
+ const result = await memory.recall({
16199
+ threadId: access.threadScope,
16200
+ resourceId: access.resourceId,
16201
+ page: 0,
16202
+ perPage: false
16203
+ });
16204
+ return result.messages.find((message) => message.id === cursor) ?? null;
16205
+ }
16206
+ if (!access?.resourceId) {
16207
+ return null;
16208
+ }
16209
+ const threads = await memory.listThreads({
16210
+ page: 0,
16211
+ perPage: 100,
16212
+ orderBy: { field: "updatedAt", direction: "DESC" },
16213
+ filter: { resourceId: access.resourceId }
16214
+ });
16215
+ for (const thread of threads.threads) {
16216
+ const result = await memory.recall({
16217
+ threadId: thread.id,
16218
+ resourceId: access.resourceId,
16219
+ page: 0,
16220
+ perPage: false
16221
+ });
16222
+ const message = result.messages.find((message2) => message2.id === cursor);
16223
+ if (message) {
16224
+ return message;
16225
+ }
16226
+ }
16227
+ return null;
16228
+ }
16193
16229
  async function listThreadsForResource({
16194
16230
  memory,
16195
16231
  resourceId,
@@ -16356,8 +16392,8 @@ function formatTimestamp(date) {
16356
16392
  return date.toISOString().replace("T", " ").replace(/\.\d{3}Z$/, "Z");
16357
16393
  }
16358
16394
  function truncateByTokens(text42, maxTokens, hint) {
16359
- if (chunkPOOOTRUM_cjs.estimateTokenCount(text42) <= maxTokens) return { text: text42, wasTruncated: false };
16360
- const truncated = chunkPOOOTRUM_cjs.truncateStringByTokens(text42, maxTokens);
16395
+ if (chunkDAEHQAZC_cjs.estimateTokenCount(text42) <= maxTokens) return { text: text42, wasTruncated: false };
16396
+ const truncated = chunkDAEHQAZC_cjs.truncateStringByTokens(text42, maxTokens);
16361
16397
  const suffix = hint ? ` [${hint} for more]` : "";
16362
16398
  return { text: truncated + suffix, wasTruncated: true };
16363
16399
  }
@@ -16390,11 +16426,11 @@ function formatMessageParts(msg, detail) {
16390
16426
  } else if (partType === "tool-invocation") {
16391
16427
  const inv = part.toolInvocation;
16392
16428
  if (inv.state === "result") {
16393
- const { value: resultValue } = chunkPOOOTRUM_cjs.resolveToolResultValue(
16429
+ const { value: resultValue } = chunkDAEHQAZC_cjs.resolveToolResultValue(
16394
16430
  part,
16395
16431
  inv.result
16396
16432
  );
16397
- const resultStr = chunkPOOOTRUM_cjs.formatToolResultForObserver(resultValue, { maxTokens: HIGH_DETAIL_TOOL_RESULT_TOKENS });
16433
+ const resultStr = chunkDAEHQAZC_cjs.formatToolResultForObserver(resultValue, { maxTokens: HIGH_DETAIL_TOOL_RESULT_TOKENS });
16398
16434
  const fullText = `[Tool Result: ${inv.toolName}]
16399
16435
  ${resultStr}`;
16400
16436
  parts.push(makePart(msg, i, "tool-result", fullText, detail));
@@ -16441,6 +16477,27 @@ function buildRenderedText(parts, timestamps) {
16441
16477
  }
16442
16478
  return lines.join("\n");
16443
16479
  }
16480
+ async function getNextVisibleMessage({
16481
+ memory,
16482
+ threadId,
16483
+ resourceId,
16484
+ after
16485
+ }) {
16486
+ const result = await memory.recall({
16487
+ threadId,
16488
+ resourceId,
16489
+ page: 0,
16490
+ perPage: 50,
16491
+ orderBy: { field: "createdAt", direction: "ASC" },
16492
+ filter: {
16493
+ dateRange: {
16494
+ start: after,
16495
+ startExclusive: true
16496
+ }
16497
+ }
16498
+ });
16499
+ return result.messages.find(hasVisibleParts) ?? null;
16500
+ }
16444
16501
  var MAX_EXPAND_USER_TEXT_TOKENS = 200;
16445
16502
  var MAX_EXPAND_OTHER_TOKENS = 50;
16446
16503
  function expandLimit(part) {
@@ -16456,9 +16513,9 @@ function expandPriority(part) {
16456
16513
  }
16457
16514
  function renderFormattedParts(parts, timestamps, options) {
16458
16515
  const text42 = buildRenderedText(parts, timestamps);
16459
- let totalTokens = chunkPOOOTRUM_cjs.estimateTokenCount(text42);
16516
+ let totalTokens = chunkDAEHQAZC_cjs.estimateTokenCount(text42);
16460
16517
  if (totalTokens > options.maxTokens) {
16461
- const truncated = chunkPOOOTRUM_cjs.truncateStringByTokens(text42, options.maxTokens);
16518
+ const truncated = chunkDAEHQAZC_cjs.truncateStringByTokens(text42, options.maxTokens);
16462
16519
  return { text: truncated, truncated: true, tokenOffset: totalTokens - options.maxTokens };
16463
16520
  }
16464
16521
  const truncatedIndices = parts.map((p, i) => ({ part: p, index: i })).filter(({ part }) => part.text !== part.fullText).sort((a, b) => expandPriority(a.part) - expandPriority(b.part));
@@ -16469,8 +16526,8 @@ function renderFormattedParts(parts, timestamps, options) {
16469
16526
  for (const { part, index } of truncatedIndices) {
16470
16527
  if (remaining <= 0) break;
16471
16528
  const maxTokens = expandLimit(part);
16472
- const fullTokens = chunkPOOOTRUM_cjs.estimateTokenCount(part.fullText);
16473
- const currentTokens = chunkPOOOTRUM_cjs.estimateTokenCount(part.text);
16529
+ const fullTokens = chunkDAEHQAZC_cjs.estimateTokenCount(part.fullText);
16530
+ const currentTokens = chunkDAEHQAZC_cjs.estimateTokenCount(part.text);
16474
16531
  const targetTokens = Math.min(fullTokens, maxTokens);
16475
16532
  const delta = targetTokens - currentTokens;
16476
16533
  if (delta <= 0) continue;
@@ -16481,17 +16538,17 @@ function renderFormattedParts(parts, timestamps, options) {
16481
16538
  const expandedLimit = Math.min(currentTokens + remaining, maxTokens);
16482
16539
  const hint = `recall cursor="${part.messageId}" partIndex=${part.partIndex} detail="high"`;
16483
16540
  const { text: expanded2 } = truncateByTokens(part.fullText, expandedLimit, hint);
16484
- const expandedDelta = chunkPOOOTRUM_cjs.estimateTokenCount(expanded2) - currentTokens;
16541
+ const expandedDelta = chunkDAEHQAZC_cjs.estimateTokenCount(expanded2) - currentTokens;
16485
16542
  parts[index] = { ...part, text: expanded2 };
16486
16543
  remaining -= expandedDelta;
16487
16544
  }
16488
16545
  }
16489
16546
  const expanded = buildRenderedText(parts, timestamps);
16490
- const expandedTokens = chunkPOOOTRUM_cjs.estimateTokenCount(expanded);
16547
+ const expandedTokens = chunkDAEHQAZC_cjs.estimateTokenCount(expanded);
16491
16548
  if (expandedTokens <= options.maxTokens) {
16492
16549
  return { text: expanded, truncated: false, tokenOffset: 0 };
16493
16550
  }
16494
- const hardTruncated = chunkPOOOTRUM_cjs.truncateStringByTokens(expanded, options.maxTokens);
16551
+ const hardTruncated = chunkDAEHQAZC_cjs.truncateStringByTokens(expanded, options.maxTokens);
16495
16552
  return { text: hardTruncated, truncated: true, tokenOffset: expandedTokens - options.maxTokens };
16496
16553
  }
16497
16554
  async function recallPart({
@@ -16509,7 +16566,11 @@ async function recallPart({
16509
16566
  if (!threadId) {
16510
16567
  throw new Error("Thread ID is required for recall");
16511
16568
  }
16512
- const resolved = await resolveCursorMessage(memory, cursor, { resourceId, threadScope });
16569
+ const resolved = await resolveCursorMessage(memory, cursor, {
16570
+ resourceId,
16571
+ threadScope,
16572
+ enforceThreadScope: false
16573
+ });
16513
16574
  if ("hint" in resolved) {
16514
16575
  throw new Error(resolved.hint);
16515
16576
  }
@@ -16521,11 +16582,39 @@ async function recallPart({
16521
16582
  }
16522
16583
  const target = allParts.find((p) => p.partIndex === partIndex);
16523
16584
  if (!target) {
16524
- throw new Error(
16525
- `Part index ${partIndex} not found in message ${cursor}. Available indices: ${allParts.map((p) => p.partIndex).join(", ")}`
16526
- );
16585
+ const availableIndices = allParts.map((p) => p.partIndex).join(", ");
16586
+ const highestVisiblePartIndex = Math.max(...allParts.map((p) => p.partIndex));
16587
+ if (partIndex > highestVisiblePartIndex) {
16588
+ const nextMessage = await getNextVisibleMessage({
16589
+ memory,
16590
+ threadId,
16591
+ resourceId,
16592
+ after: resolved.createdAt
16593
+ });
16594
+ if (nextMessage) {
16595
+ const nextParts = formatMessageParts(nextMessage, "high");
16596
+ const firstNextPart = nextParts[0];
16597
+ if (firstNextPart) {
16598
+ const fallbackNote = `Part index ${partIndex} not found in message ${cursor}; showing partIndex ${firstNextPart.partIndex} from next message ${firstNextPart.messageId}.
16599
+
16600
+ `;
16601
+ const fallbackText = `${fallbackNote}${firstNextPart.text}`;
16602
+ const truncatedText2 = chunkDAEHQAZC_cjs.truncateStringByTokens(fallbackText, maxTokens);
16603
+ const wasTruncated2 = truncatedText2 !== fallbackText;
16604
+ return {
16605
+ text: truncatedText2,
16606
+ messageId: firstNextPart.messageId,
16607
+ partIndex: firstNextPart.partIndex,
16608
+ role: firstNextPart.role,
16609
+ type: firstNextPart.type,
16610
+ truncated: wasTruncated2
16611
+ };
16612
+ }
16613
+ }
16614
+ }
16615
+ throw new Error(`Part index ${partIndex} not found in message ${cursor}. Available indices: ${availableIndices}`);
16527
16616
  }
16528
- const truncatedText = chunkPOOOTRUM_cjs.truncateStringByTokens(target.text, maxTokens);
16617
+ const truncatedText = chunkDAEHQAZC_cjs.truncateStringByTokens(target.text, maxTokens);
16529
16618
  const wasTruncated = truncatedText !== target.text;
16530
16619
  return {
16531
16620
  text: truncatedText,
@@ -16561,7 +16650,11 @@ async function recallMessages({
16561
16650
  const rawPage = page === 0 ? 1 : page;
16562
16651
  const normalizedPage = Math.max(Math.min(rawPage, MAX_PAGE), -MAX_PAGE);
16563
16652
  const normalizedLimit = Math.min(limit, MAX_LIMIT);
16564
- const resolved = await resolveCursorMessage(memory, cursor, { resourceId, threadScope });
16653
+ const resolved = await resolveCursorMessage(memory, cursor, {
16654
+ resourceId,
16655
+ threadScope,
16656
+ enforceThreadScope: false
16657
+ });
16565
16658
  if ("hint" in resolved) {
16566
16659
  return {
16567
16660
  messages: resolved.hint,
@@ -16577,9 +16670,10 @@ async function recallMessages({
16577
16670
  };
16578
16671
  }
16579
16672
  const anchor = resolved;
16580
- if (anchor.threadId && anchor.threadId !== threadId) {
16673
+ const crossThreadId = anchor.threadId && anchor.threadId !== threadId ? anchor.threadId : void 0;
16674
+ if (crossThreadId && threadScope) {
16581
16675
  return {
16582
- messages: `Cursor does not belong to the active thread. Expected thread "${threadId}" but cursor "${cursor}" belongs to "${anchor.threadId}".`,
16676
+ messages: `Cursor does not belong to the active thread. Expected thread "${threadId}" but cursor "${cursor}" belongs to "${anchor.threadId}". Pass threadId="${anchor.threadId}" to browse that thread, or omit threadId and use this cursor directly in resource scope.`,
16583
16677
  count: 0,
16584
16678
  cursor,
16585
16679
  page: normalizedPage,
@@ -16591,7 +16685,10 @@ async function recallMessages({
16591
16685
  tokenOffset: 0
16592
16686
  };
16593
16687
  }
16594
- const resolvedThreadId = threadId;
16688
+ const resolvedThreadId = crossThreadId ?? threadId;
16689
+ if (!resolvedThreadId) {
16690
+ throw new Error("Thread ID is required for recall");
16691
+ }
16595
16692
  const isForward = normalizedPage > 0;
16596
16693
  const pageIndex = Math.max(Math.abs(normalizedPage), 1) - 1;
16597
16694
  const skip = pageIndex * normalizedLimit;
@@ -16873,7 +16970,25 @@ var recallTool = (_memoryConfig, options) => {
16873
16970
  threadScope = currentThreadId || void 0;
16874
16971
  }
16875
16972
  if (hasCursor && !hasExplicitThreadId && !currentThreadId) {
16876
- throw new Error("Current thread is required when browsing by cursor");
16973
+ if (!isResourceScope) {
16974
+ throw new Error("Current thread is required when browsing by cursor");
16975
+ }
16976
+ const resolved = await resolveCursorMessage(memory, cursor, { resourceId });
16977
+ if ("hint" in resolved) {
16978
+ return {
16979
+ messages: resolved.hint,
16980
+ count: 0,
16981
+ cursor,
16982
+ page: page ?? 1,
16983
+ limit: Math.min(limit ?? 20, 20),
16984
+ detail: detail ?? "low",
16985
+ hasNextPage: false,
16986
+ hasPrevPage: false,
16987
+ truncated: false,
16988
+ tokenOffset: 0
16989
+ };
16990
+ }
16991
+ targetThreadId = resolved.threadId;
16877
16992
  }
16878
16993
  if (!targetThreadId) {
16879
16994
  throw new Error("Thread ID is required for recall");
@@ -17559,7 +17674,7 @@ ${workingMemory}`;
17559
17674
  }
17560
17675
  return chunks;
17561
17676
  }
17562
- hasher = chunkPOOOTRUM_cjs.e();
17677
+ hasher = chunkDAEHQAZC_cjs.e();
17563
17678
  // embedding is computationally expensive so cache content -> embeddings/chunks
17564
17679
  embeddingCache = /* @__PURE__ */ new Map();
17565
17680
  firstEmbed;
@@ -17940,7 +18055,7 @@ ${workingMemory}`;
17940
18055
  "Observational memory requires @mastra/core support for request-response-id-rotation. Please bump @mastra/core to a newer version."
17941
18056
  );
17942
18057
  }
17943
- const { ObservationalMemory: OMClass } = await import('./observational-memory-SASGM6OW-2G3RBRTW.cjs');
18058
+ const { ObservationalMemory: OMClass } = await import('./observational-memory-B25SASRW-T76NSRNU.cjs');
17944
18059
  const onIndexObservations = this.hasRetrievalSearch(omConfig.retrieval) ? async (observation) => {
17945
18060
  await this.indexObservation(observation);
17946
18061
  } : void 0;
@@ -18828,7 +18943,7 @@ Notes:
18828
18943
  if (!effectiveConfig) return null;
18829
18944
  const engine = await this.omEngine;
18830
18945
  if (!engine) return null;
18831
- const { ObservationalMemoryProcessor } = await import('./observational-memory-SASGM6OW-2G3RBRTW.cjs');
18946
+ const { ObservationalMemoryProcessor } = await import('./observational-memory-B25SASRW-T76NSRNU.cjs');
18832
18947
  return new ObservationalMemoryProcessor(engine, this);
18833
18948
  }
18834
18949
  };
@@ -30044,5 +30159,5 @@ exports.START_ASYNC_AGENT_BUILDER_ACTION_ROUTE = START_ASYNC_AGENT_BUILDER_ACTIO
30044
30159
  exports.STREAM_AGENT_BUILDER_ACTION_ROUTE = STREAM_AGENT_BUILDER_ACTION_ROUTE;
30045
30160
  exports.STREAM_LEGACY_AGENT_BUILDER_ACTION_ROUTE = STREAM_LEGACY_AGENT_BUILDER_ACTION_ROUTE;
30046
30161
  exports.agent_builder_exports = agent_builder_exports;
30047
- //# sourceMappingURL=chunk-MS7GNCN3.cjs.map
30048
- //# sourceMappingURL=chunk-MS7GNCN3.cjs.map
30162
+ //# sourceMappingURL=chunk-YSCEBPUJ.cjs.map
30163
+ //# sourceMappingURL=chunk-YSCEBPUJ.cjs.map