@mastra/server 1.62.0-alpha.8 → 1.62.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 (37) hide show
  1. package/CHANGELOG.md +79 -0
  2. package/dist/{dist-DkFSqWZ7.js → dist-L0EogwX_.js} +165 -151
  3. package/dist/dist-L0EogwX_.js.map +1 -0
  4. package/dist/{dist-Czh_Btxb.cjs → dist-Xj9TvH1A.cjs} +165 -151
  5. package/dist/dist-Xj9TvH1A.cjs.map +1 -0
  6. package/dist/docs/SKILL.md +1 -1
  7. package/dist/docs/assets/SOURCE_MAP.json +1 -1
  8. package/dist/docs/references/docs-server-middleware.md +26 -0
  9. package/dist/server/handlers/agent-builder.cjs +1 -1
  10. package/dist/server/handlers/agent-builder.js +1 -1
  11. package/dist/server/handlers/mcp-client-versions.cjs +1 -1
  12. package/dist/server/handlers/mcp-client-versions.js +1 -1
  13. package/dist/server/handlers/prompt-block-versions.cjs +1 -1
  14. package/dist/server/handlers/prompt-block-versions.js +1 -1
  15. package/dist/server/handlers/scorer-versions.cjs +1 -1
  16. package/dist/server/handlers/scorer-versions.js +1 -1
  17. package/dist/server/handlers/stored-mcp-clients.cjs +1 -1
  18. package/dist/server/handlers/stored-mcp-clients.js +1 -1
  19. package/dist/server/handlers/stored-prompt-blocks.cjs +1 -1
  20. package/dist/server/handlers/stored-prompt-blocks.js +1 -1
  21. package/dist/server/handlers/stored-scorers.cjs +1 -1
  22. package/dist/server/handlers/stored-scorers.js +1 -1
  23. package/dist/server/handlers/stored-skill-favorites.cjs +1 -1
  24. package/dist/server/handlers/stored-skill-favorites.js +1 -1
  25. package/dist/server/handlers/stored-skills.cjs +1 -1
  26. package/dist/server/handlers/stored-skills.js +1 -1
  27. package/dist/server/schemas/index.cjs +1 -1
  28. package/dist/server/schemas/index.js +1 -1
  29. package/dist/server/server-adapter/index.cjs +1 -0
  30. package/dist/server/server-adapter/index.cjs.map +1 -1
  31. package/dist/server/server-adapter/index.d.ts +1 -0
  32. package/dist/server/server-adapter/index.d.ts.map +1 -1
  33. package/dist/server/server-adapter/index.js +1 -1
  34. package/dist/server/server-adapter/index.js.map +1 -1
  35. package/package.json +8 -8
  36. package/dist/dist-Czh_Btxb.cjs.map +0 -1
  37. package/dist/dist-DkFSqWZ7.js.map +0 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,84 @@
1
1
  # @mastra/server
2
2
 
3
+ ## 1.62.0
4
+
5
+ ### Patch Changes
6
+
7
+ - Fixed `server.middleware` and middleware added via `mastra.setServerMiddleware()` being silently ignored when Mastra is served through a server adapter instead of `mastra dev` / `mastra build`. ([#22161](https://github.com/mastra-ai/mastra/pull/22161))
8
+
9
+ Hono-based adapters (`@mastra/hono`, and `@mastra/next` / `@mastra/tanstack-start` which build on it) now register the configured middleware during `init()`, with the same guarantee as the built-in server: user middleware never runs on routes declared public with `requiresAuth: false`. Adapters for other frameworks (Express, Fastify, Koa) cannot run Hono middleware handlers and now log a warning at startup instead of silently ignoring the configuration.
10
+
11
+ Also fixed custom routes with `requiresAuth: false` not being treated as framework-public when the adapter derives its route auth configuration from the Mastra instance instead of receiving it in the constructor.
12
+
13
+ Fixes https://github.com/mastra-ai/mastra/issues/21869
14
+
15
+ - Experiment results now include isolated metadata snapshots from the dataset items that ran. ([#22005](https://github.com/mastra-ai/mastra/pull/22005))
16
+
17
+ - Added the `convertCustomRoutesToOpenAPIPaths` export to `@mastra/server/server-adapter` so server adapters can include custom API routes in generated OpenAPI documents. ([#22274](https://github.com/mastra-ai/mastra/pull/22274))
18
+
19
+ - Added an Elysia server adapter. Use the new @mastra/elysia package to run a Mastra server inside an Elysia app. ([#22274](https://github.com/mastra-ai/mastra/pull/22274))
20
+
21
+ ```typescript
22
+ import { Elysia } from 'elysia';
23
+ import { MastraServer } from '@mastra/elysia';
24
+ import { mastra } from './mastra';
25
+
26
+ const app = new Elysia();
27
+ const server = new MastraServer({ app, mastra });
28
+
29
+ await server.init();
30
+
31
+ app.listen(4111);
32
+ ```
33
+
34
+ - Updated dependencies [[`79f04a7`](https://github.com/mastra-ai/mastra/commit/79f04a7f6c6829da541139f638f2f1d267916e08), [`65edab1`](https://github.com/mastra-ai/mastra/commit/65edab1c233d17b8f163bad12fca410d0e6f16b1), [`1e47b75`](https://github.com/mastra-ai/mastra/commit/1e47b7520cab4cfaa8daed52f17e2e6d14ff7539), [`ab20a38`](https://github.com/mastra-ai/mastra/commit/ab20a38d0275f8d85e0f3833bd87ef487bcc609f), [`fd4d5fe`](https://github.com/mastra-ai/mastra/commit/fd4d5fe4f943699b85db5e74404f190d5a6b8c2a), [`ae8790c`](https://github.com/mastra-ai/mastra/commit/ae8790c4bfaa088d2ab279d1dcc06f326b9fd109), [`2c85f42`](https://github.com/mastra-ai/mastra/commit/2c85f428e04ccd63ea31a7ec80b5b327afdad555), [`11bbeb9`](https://github.com/mastra-ai/mastra/commit/11bbeb9b108ef2264e05acefc6dafb9cbb342921), [`48ef1f1`](https://github.com/mastra-ai/mastra/commit/48ef1f1d24eedafbb07f64e659a81b52b67b8bf6), [`aa3a85d`](https://github.com/mastra-ai/mastra/commit/aa3a85daf094c683bb97efdf4b6a696d2e474af5), [`d29d06f`](https://github.com/mastra-ai/mastra/commit/d29d06fe00bbd35b4571150ea04c59d2ed783c71), [`e6516df`](https://github.com/mastra-ai/mastra/commit/e6516dfcdae4f4ac0e7971d84359a81385ee602f), [`1a485f3`](https://github.com/mastra-ai/mastra/commit/1a485f3538f5ec64d58bd8b5e1e99de0c695c87b), [`0d37487`](https://github.com/mastra-ai/mastra/commit/0d37487d9f349388a3f1cef6a536cf9dcc4b6273), [`8661d7d`](https://github.com/mastra-ai/mastra/commit/8661d7d7179f0a024456aabdd8679bcecd09ac28), [`dbbfeb8`](https://github.com/mastra-ai/mastra/commit/dbbfeb85ec949dc9ebc0755e1ad262e4f5eba8db), [`575e343`](https://github.com/mastra-ai/mastra/commit/575e343900451021d96110916497d334af7bc252), [`0b2a3d1`](https://github.com/mastra-ai/mastra/commit/0b2a3d1783875c5b97b7b36ab3d03d7360e0dde7), [`6bb5d71`](https://github.com/mastra-ai/mastra/commit/6bb5d7193fe9166b219f0fccae17db7a5ae86e65), [`3cc9d00`](https://github.com/mastra-ai/mastra/commit/3cc9d00b2b4333e0377a5e9df5eff92c17ce7630), [`cacb839`](https://github.com/mastra-ai/mastra/commit/cacb8392d9e74189b56d857290b0615f98a2683d), [`57de7d6`](https://github.com/mastra-ai/mastra/commit/57de7d644ba7146edb4e9e6111ec4fa98c3a59e9), [`c8e4cea`](https://github.com/mastra-ai/mastra/commit/c8e4ceac9a390d78c8327dff3cdb2861dd71957f), [`ed01e9a`](https://github.com/mastra-ai/mastra/commit/ed01e9a807514a904374bf687a7b8f18750f6f78), [`b47b26e`](https://github.com/mastra-ai/mastra/commit/b47b26e6fe95cb8a3482be2c5e52de157fe59d0b), [`0d37487`](https://github.com/mastra-ai/mastra/commit/0d37487d9f349388a3f1cef6a536cf9dcc4b6273), [`733a537`](https://github.com/mastra-ai/mastra/commit/733a537489a858b5880b2e98809334fba895a221), [`e8e299c`](https://github.com/mastra-ai/mastra/commit/e8e299cc6abdfc39947e2fec25803493015d3882), [`edfc548`](https://github.com/mastra-ai/mastra/commit/edfc548886bc7bae17b681f8b6b41a47eb32bcd2), [`b05f486`](https://github.com/mastra-ai/mastra/commit/b05f48612984d5fe2447ea2d6cdd5c604d285b97), [`a8a4871`](https://github.com/mastra-ai/mastra/commit/a8a4871215f51da95c47129602157ce5372f634a), [`eb9ecaa`](https://github.com/mastra-ai/mastra/commit/eb9ecaa89c36e889749e3b825cfc507ce7f7980b), [`4ff3ee2`](https://github.com/mastra-ai/mastra/commit/4ff3ee2bff7ed07528b4817f8f49639031c72a4d), [`9207dfa`](https://github.com/mastra-ai/mastra/commit/9207dfab8062e5fc68b751684797ff86fe0b4e70), [`5165cdc`](https://github.com/mastra-ai/mastra/commit/5165cdcdcf50e144bb8113278535196cc9b07065), [`e737014`](https://github.com/mastra-ai/mastra/commit/e737014e0fc7035759762bb5b48baef1d6c0f6a7), [`6bb5d71`](https://github.com/mastra-ai/mastra/commit/6bb5d7193fe9166b219f0fccae17db7a5ae86e65), [`f591643`](https://github.com/mastra-ai/mastra/commit/f591643becdf0be9bddce6ba1748e64bc30d77f1), [`63796ba`](https://github.com/mastra-ai/mastra/commit/63796ba0fda60253be17535e68f6bbbf1e6ffa09), [`b1ad324`](https://github.com/mastra-ai/mastra/commit/b1ad324d657f3544b0701332aef7eb10e9a36258), [`61c566d`](https://github.com/mastra-ai/mastra/commit/61c566dd2f2cde2b23ed8f139924e530d4202214), [`c24754c`](https://github.com/mastra-ai/mastra/commit/c24754c1fb6fe144e5051e536e98c8a18b0214ac), [`12c61d2`](https://github.com/mastra-ai/mastra/commit/12c61d280c8cb208bc3c8dbcbe5dcc60cf9d1cd0), [`c46eb09`](https://github.com/mastra-ai/mastra/commit/c46eb09ce4987509af57a0ac582c61241a6dd2f1), [`9ee8120`](https://github.com/mastra-ai/mastra/commit/9ee8120ce17f76b9f617489e05a283353742690a), [`d975e92`](https://github.com/mastra-ai/mastra/commit/d975e924d4936f46c386bd3dee39c671720289f6), [`45dd6ee`](https://github.com/mastra-ai/mastra/commit/45dd6ee089bd7df0d0c98a10098e483fd388e04a), [`4e9a228`](https://github.com/mastra-ai/mastra/commit/4e9a2283d5fd6ed1b70a2751eb3dc2cbf82ada20), [`d6ce34a`](https://github.com/mastra-ai/mastra/commit/d6ce34aeceb06ddf3d595a1eed5cc74f481a46a1), [`f95f468`](https://github.com/mastra-ai/mastra/commit/f95f468cf1e7c2b924a13826494f98b8f2ccd581), [`30ed33e`](https://github.com/mastra-ai/mastra/commit/30ed33ee14084a26019aba15fceadda6d6ddefaf), [`04a815f`](https://github.com/mastra-ai/mastra/commit/04a815fc8971d29e97fcdcc5008a1eb472fc00ff), [`1cfa878`](https://github.com/mastra-ai/mastra/commit/1cfa8784d8da0dfaa0317e5048bc48b6084a5ea5), [`9a12ef3`](https://github.com/mastra-ai/mastra/commit/9a12ef3fccf3f4186db0f294f4ee1f02cf4d8db2), [`32d3583`](https://github.com/mastra-ai/mastra/commit/32d358332cb8ac2306b83b73cf3536e74dbd435e), [`7960688`](https://github.com/mastra-ai/mastra/commit/7960688828e04eaf3106e34f7758fa580257eef6), [`91ad69d`](https://github.com/mastra-ai/mastra/commit/91ad69d64994c89199b0c55399e64ed91c61df2f), [`8dc408d`](https://github.com/mastra-ai/mastra/commit/8dc408d34438f9e13297f792c11a5cfd6cf952e1), [`c92def1`](https://github.com/mastra-ai/mastra/commit/c92def10a13c822972c96f0a4ca6ffc1f4258aed), [`63041eb`](https://github.com/mastra-ai/mastra/commit/63041eb4c50b520a0a80e03d4cd6ea99f67715a0), [`c118318`](https://github.com/mastra-ai/mastra/commit/c1183181c9804303db4b511c2e2648f8b714712b), [`c5eaec5`](https://github.com/mastra-ai/mastra/commit/c5eaec5a860d80d0e3805e67db0414b87ac8cbed), [`fc07c64`](https://github.com/mastra-ai/mastra/commit/fc07c6465043e08e99193a6751a01c56ffc2e7a1), [`cced745`](https://github.com/mastra-ai/mastra/commit/cced745a056ec2225c5bc702e32d848847aa8b65), [`542dee2`](https://github.com/mastra-ai/mastra/commit/542dee254167f974ff8cbbbfc0ce10f9a2616a7b), [`3c19dce`](https://github.com/mastra-ai/mastra/commit/3c19dcef8e73062a80627a4927eae3ec11145afd), [`aca2869`](https://github.com/mastra-ai/mastra/commit/aca2869b2031982f3c4a2f52525c9be7cf123ef8), [`a58483c`](https://github.com/mastra-ai/mastra/commit/a58483cff1a9d41fce7c931843f48cb0ac450f64), [`a58483c`](https://github.com/mastra-ai/mastra/commit/a58483cff1a9d41fce7c931843f48cb0ac450f64), [`e6f8450`](https://github.com/mastra-ai/mastra/commit/e6f845074d478527026b18d85031b23353e1d0a4), [`895e9df`](https://github.com/mastra-ai/mastra/commit/895e9dfc17d6f34299eca64e317ded9e5f5e5ef8), [`e66b2ba`](https://github.com/mastra-ai/mastra/commit/e66b2ba100db63eaeab6e21e1ea34b113f2ec781), [`3e8727e`](https://github.com/mastra-ai/mastra/commit/3e8727e11ec1a5d733acedb5c872896394be18c1)]:
35
+ - @mastra/core@1.62.0
36
+
37
+ ## 1.62.0-alpha.12
38
+
39
+ ### Patch Changes
40
+
41
+ - Updated dependencies [[`48ef1f1`](https://github.com/mastra-ai/mastra/commit/48ef1f1d24eedafbb07f64e659a81b52b67b8bf6), [`63796ba`](https://github.com/mastra-ai/mastra/commit/63796ba0fda60253be17535e68f6bbbf1e6ffa09), [`3c19dce`](https://github.com/mastra-ai/mastra/commit/3c19dcef8e73062a80627a4927eae3ec11145afd)]:
42
+ - @mastra/core@1.62.0-alpha.12
43
+
44
+ ## 1.62.0-alpha.11
45
+
46
+ ### Patch Changes
47
+
48
+ - Updated dependencies [[`4ff3ee2`](https://github.com/mastra-ai/mastra/commit/4ff3ee2bff7ed07528b4817f8f49639031c72a4d), [`c24754c`](https://github.com/mastra-ai/mastra/commit/c24754c1fb6fe144e5051e536e98c8a18b0214ac), [`45dd6ee`](https://github.com/mastra-ai/mastra/commit/45dd6ee089bd7df0d0c98a10098e483fd388e04a), [`32d3583`](https://github.com/mastra-ai/mastra/commit/32d358332cb8ac2306b83b73cf3536e74dbd435e), [`aca2869`](https://github.com/mastra-ai/mastra/commit/aca2869b2031982f3c4a2f52525c9be7cf123ef8)]:
49
+ - @mastra/core@1.62.0-alpha.11
50
+
51
+ ## 1.62.0-alpha.10
52
+
53
+ ### Patch Changes
54
+
55
+ - Added the `convertCustomRoutesToOpenAPIPaths` export to `@mastra/server/server-adapter` so server adapters can include custom API routes in generated OpenAPI documents. ([#22274](https://github.com/mastra-ai/mastra/pull/22274))
56
+
57
+ - Added an Elysia server adapter. Use the new @mastra/elysia package to run a Mastra server inside an Elysia app. ([#22274](https://github.com/mastra-ai/mastra/pull/22274))
58
+
59
+ ```typescript
60
+ import { Elysia } from 'elysia';
61
+ import { MastraServer } from '@mastra/elysia';
62
+ import { mastra } from './mastra';
63
+
64
+ const app = new Elysia();
65
+ const server = new MastraServer({ app, mastra });
66
+
67
+ await server.init();
68
+
69
+ app.listen(4111);
70
+ ```
71
+
72
+ - Updated dependencies [[`b05f486`](https://github.com/mastra-ai/mastra/commit/b05f48612984d5fe2447ea2d6cdd5c604d285b97), [`7960688`](https://github.com/mastra-ai/mastra/commit/7960688828e04eaf3106e34f7758fa580257eef6)]:
73
+ - @mastra/core@1.62.0-alpha.10
74
+
75
+ ## 1.62.0-alpha.9
76
+
77
+ ### Patch Changes
78
+
79
+ - Updated dependencies [[`eb9ecaa`](https://github.com/mastra-ai/mastra/commit/eb9ecaa89c36e889749e3b825cfc507ce7f7980b), [`3e8727e`](https://github.com/mastra-ai/mastra/commit/3e8727e11ec1a5d733acedb5c872896394be18c1)]:
80
+ - @mastra/core@1.62.0-alpha.9
81
+
3
82
  ## 1.62.0-alpha.8
4
83
 
5
84
  ### Patch Changes
@@ -4693,7 +4693,7 @@ function splitLines(text) {
4693
4693
  return result;
4694
4694
  }
4695
4695
  //#endregion
4696
- //#region ../memory/dist/src-BTHCV1ov.js
4696
+ //#region ../memory/dist/src-BATJenuZ.js
4697
4697
  var __defProp$3 = Object.defineProperty;
4698
4698
  var __exportAll = (all, no_symbols) => {
4699
4699
  let target = {};
@@ -26969,6 +26969,149 @@ function createWorkingMemoryTool(config, options = {}) {
26969
26969
  tool
26970
26970
  };
26971
26971
  }
26972
+ /**
26973
+ * WorkingMemoryStateProcessor
26974
+ *
26975
+ * Experimental: delivers working memory to the model as a state signal instead
26976
+ * of folding it into the system message. Storage and the `setWorkingMemory`
26977
+ * tool are unchanged — this processor only changes the delivery path.
26978
+ *
26979
+ * Pattern matches `BrowserContextProcessor` in `@mastra/core/browser`:
26980
+ * - `stateId` namespaces the state lane on the thread.
26981
+ * - `cacheKey` is derived from the rendered payload so dedup is automatic.
26982
+ * - `contextWindow.hasSnapshot` re-injection ensures the model still sees the
26983
+ * current snapshot after older messages drop out of the window.
26984
+ *
26985
+ * Delta emission (markdown mode only): when a prior snapshot exists in the
26986
+ * context window, the processor emits a unified-diff delta against that
26987
+ * snapshot's contents. Schema mode and the snapshot fallback always emit a
26988
+ * full snapshot.
26989
+ *
26990
+ * @example
26991
+ * ```ts
26992
+ * new Memory({
26993
+ * options: {
26994
+ * workingMemory: {
26995
+ * enabled: true,
26996
+ * template: '...',
26997
+ * useStateSignals: true, // auto-attaches this processor
26998
+ * },
26999
+ * },
27000
+ * });
27001
+ * ```
27002
+ */
27003
+ const WORKING_MEMORY_STATE_ID = "working-memory";
27004
+ const WORKING_MEMORY_STATE_PROCESSOR_ID = "working-memory-state";
27005
+ var WorkingMemoryStateProcessor = class {
27006
+ memory;
27007
+ memoryConfig;
27008
+ id = WORKING_MEMORY_STATE_PROCESSOR_ID;
27009
+ stateId = WORKING_MEMORY_STATE_ID;
27010
+ constructor(memory, memoryConfig) {
27011
+ this.memory = memory;
27012
+ this.memoryConfig = memoryConfig;
27013
+ }
27014
+ async computeStateSignal(args) {
27015
+ const template = await this.memory.getWorkingMemoryTemplate({ memoryConfig: this.memoryConfig });
27016
+ if (!template) return;
27017
+ const contents = (await this.memory.getWorkingMemory({
27018
+ threadId: args.threadId,
27019
+ resourceId: args.resourceId,
27020
+ memoryConfig: this.memoryConfig
27021
+ }))?.trim();
27022
+ if (!contents) return;
27023
+ const cacheKey = stableWorkingMemoryCacheKey({
27024
+ format: template.format,
27025
+ data: contents
27026
+ });
27027
+ const shouldMakeSnapshot = !args.contextWindow.hasSnapshot;
27028
+ if (args.tracking?.currentCacheKey === cacheKey && !shouldMakeSnapshot) return;
27029
+ const scope = this.memory.getMergedThreadConfig(this.memoryConfig).workingMemory?.scope ?? "resource";
27030
+ const deltaCandidate = template.format === "markdown" && !shouldMakeSnapshot ? buildMarkdownDelta({
27031
+ lastSnapshot: args.lastSnapshot,
27032
+ deltasSinceSnapshot: args.deltasSinceSnapshot,
27033
+ nextContents: contents
27034
+ }) : void 0;
27035
+ if (deltaCandidate) return {
27036
+ id: WORKING_MEMORY_STATE_ID,
27037
+ mode: "delta",
27038
+ cacheKey,
27039
+ tagName: "working-memory",
27040
+ contents: deltaCandidate.contents,
27041
+ delta: deltaCandidate.contents,
27042
+ value: contents,
27043
+ attributes: {
27044
+ format: template.format,
27045
+ scope,
27046
+ patch: "unified-diff"
27047
+ }
27048
+ };
27049
+ return {
27050
+ id: WORKING_MEMORY_STATE_ID,
27051
+ mode: "snapshot",
27052
+ cacheKey,
27053
+ tagName: "working-memory",
27054
+ contents,
27055
+ value: contents,
27056
+ attributes: {
27057
+ format: template.format,
27058
+ scope
27059
+ }
27060
+ };
27061
+ }
27062
+ };
27063
+ /**
27064
+ * Stable cache key for the rendered working memory payload. Returns a SHA-256
27065
+ * digest so dedup metadata stays compact regardless of payload size (working
27066
+ * memory blobs can grow arbitrarily long).
27067
+ */
27068
+ function stableWorkingMemoryCacheKey(input) {
27069
+ const hash = createHash("sha256");
27070
+ hash.update(input.format);
27071
+ hash.update("\0");
27072
+ hash.update(input.data ?? "");
27073
+ return `sha256:${hash.digest("hex")}`;
27074
+ }
27075
+ /**
27076
+ * Build a unified-diff delta against the most recently emitted state. Prefers
27077
+ * the latest delta's `value` (the post-edit full text) when available, falling
27078
+ * back to the snapshot's `value` and finally the snapshot's `contents`. This
27079
+ * keeps deltas incremental (B→C) instead of cumulative against a stale
27080
+ * snapshot (A→C), which matters when many small edits land between snapshots.
27081
+ *
27082
+ * Returns undefined when:
27083
+ * - there's no prior state to diff against
27084
+ * - the prior state isn't a plain string (multimodal signal)
27085
+ *
27086
+ * In either case the caller falls back to emitting a full snapshot.
27087
+ */
27088
+ function buildMarkdownDelta(args) {
27089
+ const { lastSnapshot, deltasSinceSnapshot, nextContents } = args;
27090
+ const prior = pickStringValue(readSignalValue(deltasSinceSnapshot.at(-1))) ?? pickStringValue(readSignalValue(lastSnapshot)) ?? (typeof lastSnapshot?.contents === "string" ? lastSnapshot.contents : void 0);
27091
+ if (!prior) return;
27092
+ return { contents: renderHunksOnly(prior, nextContents) };
27093
+ }
27094
+ function pickStringValue(value) {
27095
+ return typeof value === "string" ? value : void 0;
27096
+ }
27097
+ function readSignalValue(signal) {
27098
+ return (signal?.metadata)?.value;
27099
+ }
27100
+ /**
27101
+ * Render a unified-diff-style patch body containing only `@@` hunks and their
27102
+ * lines — dropping the filename preamble (`Index:` / `===` / `---` / `+++`)
27103
+ * that `createPatch` emits and the `` trailer.
27104
+ * The preamble exists for tooling like `patch -p1` to know which file to
27105
+ * apply to; we only ever diff a single working-memory blob. The newline
27106
+ * trailer is semantically meaningless to the model and adds noise to the
27107
+ * state signal.
27108
+ */
27109
+ function renderHunksOnly(prior, next) {
27110
+ const { hunks } = structuredPatch("", "", prior, next, "", "", { context: 0 });
27111
+ return hunks.map((hunk) => {
27112
+ return [`@@ -${hunk.oldStart},${hunk.oldLines} +${hunk.newStart},${hunk.newLines} @@`, ...hunk.lines.filter((line) => !line.startsWith("\"))].join("\n");
27113
+ }).join("\n");
27114
+ }
26972
27115
  const MINUTE = 6e4;
26973
27116
  const HOUR = 60 * MINUTE;
26974
27117
  const SHORT_TTL = 5 * MINUTE;
@@ -30946,10 +31089,24 @@ function getLatestStepParts(parts) {
30946
31089
  return parts;
30947
31090
  }
30948
31091
  /**
30949
- * Returns true when a message contains at least one part with visible user/assistant
30950
- * content (text, tool-invocation, reasoning, image, file). Messages that only carry
30951
- * internal `data-*` parts (buffering markers, observation markers, etc.) return false.
31092
+ * Returns true for persisted Working Memory state signals (`role: 'signal'`,
31093
+ * `signal.type: 'state'`, `state.id: 'working-memory'`). OM must not observe these:
31094
+ * they mirror memory OM itself manages, and re-observing them creates a self-feedback
31095
+ * loop that can overwrite stored working memory (#21961). Only the working-memory lane
31096
+ * is filtered — other state lanes are outside OM's write path, so re-observing them
31097
+ * cannot corrupt OM-managed state; excluding them broadly is a separate decision.
30952
31098
  */
31099
+ function isWorkingMemoryStateSignal(message) {
31100
+ if (message.role !== "signal") return false;
31101
+ const signal = message.content.metadata?.signal;
31102
+ if (!signal || typeof signal !== "object" || Array.isArray(signal)) return false;
31103
+ const signalRecord = signal;
31104
+ if ((signalRecord.type ?? message.type) !== "state") return false;
31105
+ const metadata = signalRecord.metadata;
31106
+ if (!metadata || typeof metadata !== "object" || Array.isArray(metadata)) return false;
31107
+ const state = metadata.state;
31108
+ return !!state && typeof state === "object" && !Array.isArray(state) && state.id === "working-memory";
31109
+ }
30953
31110
  function messageHasVisibleContent(msg) {
30954
31111
  const content = msg.content;
30955
31112
  if (content?.parts && Array.isArray(content.parts)) return content.parts.some((p) => {
@@ -31720,7 +31877,7 @@ var ObservationalMemory = class ObservationalMemory {
31720
31877
  }
31721
31878
  const result = [];
31722
31879
  for (const msg of allMessages) {
31723
- if (msg.role === "system") continue;
31880
+ if (msg.role === "system" || isWorkingMemoryStateSignal(msg)) continue;
31724
31881
  if (observedMessageIds?.has(msg.id)) continue;
31725
31882
  const endMarkerIndex = findLastCompletedObservationBoundary(msg);
31726
31883
  if (this.hasInProgressObservation(msg)) result.push(msg);
@@ -31942,7 +32099,7 @@ var ObservationalMemory = class ObservationalMemory {
31942
32099
  },
31943
32100
  filter: startDate ? { dateRange: { start: startDate } } : void 0
31944
32101
  });
31945
- return result.messages.filter((msg) => msg.role !== "system");
32102
+ return result.messages.filter((msg) => msg.role !== "system" && !isWorkingMemoryStateSignal(msg));
31946
32103
  }
31947
32104
  /**
31948
32105
  * Format unobserved messages from other threads as <unobserved-context> blocks.
@@ -32401,7 +32558,7 @@ ${formattedMessages}
32401
32558
  direction: "ASC"
32402
32559
  },
32403
32560
  filter: startDate ? { dateRange: { start: startDate } } : void 0
32404
- })).messages.filter((m) => !this.observedMessageIds.has(m.id));
32561
+ })).messages.filter((message) => !this.observedMessageIds.has(message.id) && !isWorkingMemoryStateSignal(message));
32405
32562
  if (filtered.length > 0) messagesByThread.set(thread.id, filtered);
32406
32563
  }
32407
32564
  if (messagesByThread.size === 0) return void 0;
@@ -33818,149 +33975,6 @@ var observational_memory_exports = /* @__PURE__ */ __exportAll({
33818
33975
  summarizeConversation: () => summarizeConversation,
33819
33976
  wrapInObservationGroup: () => wrapInObservationGroup
33820
33977
  });
33821
- /**
33822
- * WorkingMemoryStateProcessor
33823
- *
33824
- * Experimental: delivers working memory to the model as a state signal instead
33825
- * of folding it into the system message. Storage and the `setWorkingMemory`
33826
- * tool are unchanged — this processor only changes the delivery path.
33827
- *
33828
- * Pattern matches `BrowserContextProcessor` in `@mastra/core/browser`:
33829
- * - `stateId` namespaces the state lane on the thread.
33830
- * - `cacheKey` is derived from the rendered payload so dedup is automatic.
33831
- * - `contextWindow.hasSnapshot` re-injection ensures the model still sees the
33832
- * current snapshot after older messages drop out of the window.
33833
- *
33834
- * Delta emission (markdown mode only): when a prior snapshot exists in the
33835
- * context window, the processor emits a unified-diff delta against that
33836
- * snapshot's contents. Schema mode and the snapshot fallback always emit a
33837
- * full snapshot.
33838
- *
33839
- * @example
33840
- * ```ts
33841
- * new Memory({
33842
- * options: {
33843
- * workingMemory: {
33844
- * enabled: true,
33845
- * template: '...',
33846
- * useStateSignals: true, // auto-attaches this processor
33847
- * },
33848
- * },
33849
- * });
33850
- * ```
33851
- */
33852
- const WORKING_MEMORY_STATE_ID = "working-memory";
33853
- const WORKING_MEMORY_STATE_PROCESSOR_ID = "working-memory-state";
33854
- var WorkingMemoryStateProcessor = class {
33855
- memory;
33856
- memoryConfig;
33857
- id = WORKING_MEMORY_STATE_PROCESSOR_ID;
33858
- stateId = WORKING_MEMORY_STATE_ID;
33859
- constructor(memory, memoryConfig) {
33860
- this.memory = memory;
33861
- this.memoryConfig = memoryConfig;
33862
- }
33863
- async computeStateSignal(args) {
33864
- const template = await this.memory.getWorkingMemoryTemplate({ memoryConfig: this.memoryConfig });
33865
- if (!template) return;
33866
- const contents = (await this.memory.getWorkingMemory({
33867
- threadId: args.threadId,
33868
- resourceId: args.resourceId,
33869
- memoryConfig: this.memoryConfig
33870
- }))?.trim();
33871
- if (!contents) return;
33872
- const cacheKey = stableWorkingMemoryCacheKey({
33873
- format: template.format,
33874
- data: contents
33875
- });
33876
- const shouldMakeSnapshot = !args.contextWindow.hasSnapshot;
33877
- if (args.tracking?.currentCacheKey === cacheKey && !shouldMakeSnapshot) return;
33878
- const scope = this.memory.getMergedThreadConfig(this.memoryConfig).workingMemory?.scope ?? "resource";
33879
- const deltaCandidate = template.format === "markdown" && !shouldMakeSnapshot ? buildMarkdownDelta({
33880
- lastSnapshot: args.lastSnapshot,
33881
- deltasSinceSnapshot: args.deltasSinceSnapshot,
33882
- nextContents: contents
33883
- }) : void 0;
33884
- if (deltaCandidate) return {
33885
- id: WORKING_MEMORY_STATE_ID,
33886
- mode: "delta",
33887
- cacheKey,
33888
- tagName: "working-memory",
33889
- contents: deltaCandidate.contents,
33890
- delta: deltaCandidate.contents,
33891
- value: contents,
33892
- attributes: {
33893
- format: template.format,
33894
- scope,
33895
- patch: "unified-diff"
33896
- }
33897
- };
33898
- return {
33899
- id: WORKING_MEMORY_STATE_ID,
33900
- mode: "snapshot",
33901
- cacheKey,
33902
- tagName: "working-memory",
33903
- contents,
33904
- value: contents,
33905
- attributes: {
33906
- format: template.format,
33907
- scope
33908
- }
33909
- };
33910
- }
33911
- };
33912
- /**
33913
- * Stable cache key for the rendered working memory payload. Returns a SHA-256
33914
- * digest so dedup metadata stays compact regardless of payload size (working
33915
- * memory blobs can grow arbitrarily long).
33916
- */
33917
- function stableWorkingMemoryCacheKey(input) {
33918
- const hash = createHash("sha256");
33919
- hash.update(input.format);
33920
- hash.update("\0");
33921
- hash.update(input.data ?? "");
33922
- return `sha256:${hash.digest("hex")}`;
33923
- }
33924
- /**
33925
- * Build a unified-diff delta against the most recently emitted state. Prefers
33926
- * the latest delta's `value` (the post-edit full text) when available, falling
33927
- * back to the snapshot's `value` and finally the snapshot's `contents`. This
33928
- * keeps deltas incremental (B→C) instead of cumulative against a stale
33929
- * snapshot (A→C), which matters when many small edits land between snapshots.
33930
- *
33931
- * Returns undefined when:
33932
- * - there's no prior state to diff against
33933
- * - the prior state isn't a plain string (multimodal signal)
33934
- *
33935
- * In either case the caller falls back to emitting a full snapshot.
33936
- */
33937
- function buildMarkdownDelta(args) {
33938
- const { lastSnapshot, deltasSinceSnapshot, nextContents } = args;
33939
- const prior = pickStringValue(readSignalValue(deltasSinceSnapshot.at(-1))) ?? pickStringValue(readSignalValue(lastSnapshot)) ?? (typeof lastSnapshot?.contents === "string" ? lastSnapshot.contents : void 0);
33940
- if (!prior) return;
33941
- return { contents: renderHunksOnly(prior, nextContents) };
33942
- }
33943
- function pickStringValue(value) {
33944
- return typeof value === "string" ? value : void 0;
33945
- }
33946
- function readSignalValue(signal) {
33947
- return (signal?.metadata)?.value;
33948
- }
33949
- /**
33950
- * Render a unified-diff-style patch body containing only `@@` hunks and their
33951
- * lines — dropping the filename preamble (`Index:` / `===` / `---` / `+++`)
33952
- * that `createPatch` emits and the `` trailer.
33953
- * The preamble exists for tooling like `patch -p1` to know which file to
33954
- * apply to; we only ever diff a single working-memory blob. The newline
33955
- * trailer is semantically meaningless to the model and adds noise to the
33956
- * state signal.
33957
- */
33958
- function renderHunksOnly(prior, next) {
33959
- const { hunks } = structuredPatch("", "", prior, next, "", "", { context: 0 });
33960
- return hunks.map((hunk) => {
33961
- return [`@@ -${hunk.oldStart},${hunk.oldLines} +${hunk.newStart},${hunk.newLines} @@`, ...hunk.lines.filter((line) => !line.startsWith("\"))].join("\n");
33962
- }).join("\n");
33963
- }
33964
33978
  var working_memory_state_exports = /* @__PURE__ */ __exportAll({
33965
33979
  WORKING_MEMORY_STATE_ID: () => WORKING_MEMORY_STATE_ID,
33966
33980
  WORKING_MEMORY_STATE_PROCESSOR_ID: () => WORKING_MEMORY_STATE_PROCESSOR_ID,
@@ -46347,4 +46361,4 @@ const agentBuilderWorkflows = {
46347
46361
  //#endregion
46348
46362
  export { agentBuilderWorkflows };
46349
46363
 
46350
- //# sourceMappingURL=dist-DkFSqWZ7.js.map
46364
+ //# sourceMappingURL=dist-L0EogwX_.js.map