@graphrefly/ts 0.0.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 (210) hide show
  1. package/LICENSE +21 -0
  2. package/dist/adapters/index.cjs +9015 -0
  3. package/dist/adapters/index.cjs.map +1 -0
  4. package/dist/adapters/index.d.cts +678 -0
  5. package/dist/adapters/index.d.ts +678 -0
  6. package/dist/adapters/index.js +2894 -0
  7. package/dist/adapters/index.js.map +1 -0
  8. package/dist/adapters/observe-storage.cjs +260 -0
  9. package/dist/adapters/observe-storage.cjs.map +1 -0
  10. package/dist/adapters/observe-storage.d.cts +77 -0
  11. package/dist/adapters/observe-storage.d.ts +77 -0
  12. package/dist/adapters/observe-storage.js +161 -0
  13. package/dist/adapters/observe-storage.js.map +1 -0
  14. package/dist/append-log-DisvEReb.d.cts +189 -0
  15. package/dist/append-log-DzKMmjxx.d.ts +189 -0
  16. package/dist/chunk-2ILL7HQI.js +1189 -0
  17. package/dist/chunk-2ILL7HQI.js.map +1 -0
  18. package/dist/chunk-4VLAQHZI.js +330 -0
  19. package/dist/chunk-4VLAQHZI.js.map +1 -0
  20. package/dist/chunk-6PHB5CJS.js +229 -0
  21. package/dist/chunk-6PHB5CJS.js.map +1 -0
  22. package/dist/chunk-COHO25CZ.js +2449 -0
  23. package/dist/chunk-COHO25CZ.js.map +1 -0
  24. package/dist/chunk-D6BG7U6A.js +161 -0
  25. package/dist/chunk-D6BG7U6A.js.map +1 -0
  26. package/dist/chunk-DJC4CHLQ.js +818 -0
  27. package/dist/chunk-DJC4CHLQ.js.map +1 -0
  28. package/dist/chunk-DK554R7P.js +67 -0
  29. package/dist/chunk-DK554R7P.js.map +1 -0
  30. package/dist/chunk-DKP56CSW.js +322 -0
  31. package/dist/chunk-DKP56CSW.js.map +1 -0
  32. package/dist/chunk-G4M3NHRJ.js +688 -0
  33. package/dist/chunk-G4M3NHRJ.js.map +1 -0
  34. package/dist/chunk-GAJW5I5P.js +1 -0
  35. package/dist/chunk-GAJW5I5P.js.map +1 -0
  36. package/dist/chunk-GDL26N2T.js +282 -0
  37. package/dist/chunk-GDL26N2T.js.map +1 -0
  38. package/dist/chunk-GTPCOEOZ.js +741 -0
  39. package/dist/chunk-GTPCOEOZ.js.map +1 -0
  40. package/dist/chunk-H2HTHFVN.js +1 -0
  41. package/dist/chunk-H2HTHFVN.js.map +1 -0
  42. package/dist/chunk-HDRL4VQQ.js +218 -0
  43. package/dist/chunk-HDRL4VQQ.js.map +1 -0
  44. package/dist/chunk-I3IHOHCJ.js +2267 -0
  45. package/dist/chunk-I3IHOHCJ.js.map +1 -0
  46. package/dist/chunk-JIMUDQN3.js +965 -0
  47. package/dist/chunk-JIMUDQN3.js.map +1 -0
  48. package/dist/chunk-JR4IKCR2.js +462 -0
  49. package/dist/chunk-JR4IKCR2.js.map +1 -0
  50. package/dist/chunk-K3FXTC3I.js +165 -0
  51. package/dist/chunk-K3FXTC3I.js.map +1 -0
  52. package/dist/chunk-OOEN33FB.js +65 -0
  53. package/dist/chunk-OOEN33FB.js.map +1 -0
  54. package/dist/chunk-PKVBHGBM.js +554 -0
  55. package/dist/chunk-PKVBHGBM.js.map +1 -0
  56. package/dist/chunk-RIIOUTGM.js +1857 -0
  57. package/dist/chunk-RIIOUTGM.js.map +1 -0
  58. package/dist/chunk-SCRFRCGR.js +33 -0
  59. package/dist/chunk-SCRFRCGR.js.map +1 -0
  60. package/dist/chunk-SWTLEQL6.js +53 -0
  61. package/dist/chunk-SWTLEQL6.js.map +1 -0
  62. package/dist/chunk-SZAETNNA.js +1 -0
  63. package/dist/chunk-SZAETNNA.js.map +1 -0
  64. package/dist/chunk-TEW47IVR.js +49 -0
  65. package/dist/chunk-TEW47IVR.js.map +1 -0
  66. package/dist/chunk-Y3ZLRGHC.js +1482 -0
  67. package/dist/chunk-Y3ZLRGHC.js.map +1 -0
  68. package/dist/chunk-Z2ANQ6JW.js +149 -0
  69. package/dist/chunk-Z2ANQ6JW.js.map +1 -0
  70. package/dist/codec-BB7p8fJc.d.cts +38 -0
  71. package/dist/codec-BB7p8fJc.d.ts +38 -0
  72. package/dist/composition/index.cjs +2505 -0
  73. package/dist/composition/index.cjs.map +1 -0
  74. package/dist/composition/index.d.cts +95 -0
  75. package/dist/composition/index.d.ts +95 -0
  76. package/dist/composition/index.js +19 -0
  77. package/dist/composition/index.js.map +1 -0
  78. package/dist/core/index.cjs +2475 -0
  79. package/dist/core/index.cjs.map +1 -0
  80. package/dist/core/index.d.cts +25 -0
  81. package/dist/core/index.d.ts +25 -0
  82. package/dist/core/index.js +74 -0
  83. package/dist/core/index.js.map +1 -0
  84. package/dist/cqrs/index.cjs +595 -0
  85. package/dist/cqrs/index.cjs.map +1 -0
  86. package/dist/cqrs/index.d.cts +200 -0
  87. package/dist/cqrs/index.d.ts +200 -0
  88. package/dist/cqrs/index.js +557 -0
  89. package/dist/cqrs/index.js.map +1 -0
  90. package/dist/data-structures/index.cjs +4277 -0
  91. package/dist/data-structures/index.cjs.map +1 -0
  92. package/dist/data-structures/index.d.cts +22 -0
  93. package/dist/data-structures/index.d.ts +22 -0
  94. package/dist/data-structures/index.js +31 -0
  95. package/dist/data-structures/index.js.map +1 -0
  96. package/dist/describe-BZb7AS2y.d.ts +57 -0
  97. package/dist/describe-Ciu8SUnN.d.cts +57 -0
  98. package/dist/graph/index.cjs +7832 -0
  99. package/dist/graph/index.cjs.map +1 -0
  100. package/dist/graph/index.d.cts +251 -0
  101. package/dist/graph/index.d.ts +251 -0
  102. package/dist/graph/index.js +131 -0
  103. package/dist/graph/index.js.map +1 -0
  104. package/dist/graph-DfdF9zy4.d.ts +666 -0
  105. package/dist/graph-s23RbbFO.d.cts +666 -0
  106. package/dist/index-CAEmLCYF.d.cts +166 -0
  107. package/dist/index-SnzZu3x7.d.ts +166 -0
  108. package/dist/index.cjs +15409 -0
  109. package/dist/index.cjs.map +1 -0
  110. package/dist/index.d.cts +23 -0
  111. package/dist/index.d.ts +23 -0
  112. package/dist/index.js +582 -0
  113. package/dist/index.js.map +1 -0
  114. package/dist/messages-BZqrGGm1.d.cts +55 -0
  115. package/dist/messages-BZqrGGm1.d.ts +55 -0
  116. package/dist/messaging/index.cjs +818 -0
  117. package/dist/messaging/index.cjs.map +1 -0
  118. package/dist/messaging/index.d.cts +235 -0
  119. package/dist/messaging/index.d.ts +235 -0
  120. package/dist/messaging/index.js +767 -0
  121. package/dist/messaging/index.js.map +1 -0
  122. package/dist/observe-event-log-0SVsx7JG.d.cts +88 -0
  123. package/dist/observe-event-log-Dnm76owp.d.ts +88 -0
  124. package/dist/operators/index.cjs +3774 -0
  125. package/dist/operators/index.cjs.map +1 -0
  126. package/dist/operators/index.d.cts +308 -0
  127. package/dist/operators/index.d.ts +308 -0
  128. package/dist/operators/index.js +112 -0
  129. package/dist/operators/index.js.map +1 -0
  130. package/dist/orchestration/index.cjs +3541 -0
  131. package/dist/orchestration/index.cjs.map +1 -0
  132. package/dist/orchestration/index.d.cts +302 -0
  133. package/dist/orchestration/index.d.ts +302 -0
  134. package/dist/orchestration/index.js +1004 -0
  135. package/dist/orchestration/index.js.map +1 -0
  136. package/dist/patterns/index.cjs +1045 -0
  137. package/dist/patterns/index.cjs.map +1 -0
  138. package/dist/patterns/index.d.cts +39 -0
  139. package/dist/patterns/index.d.ts +39 -0
  140. package/dist/patterns/index.js +33 -0
  141. package/dist/patterns/index.js.map +1 -0
  142. package/dist/reactive-collection-BxBqtIOE.d.cts +344 -0
  143. package/dist/reactive-collection-Cb1II1Fi.d.ts +344 -0
  144. package/dist/reactive-collection-storage-BNiEc-Gd.d.ts +217 -0
  145. package/dist/reactive-collection-storage-D-KmjE0J.d.cts +217 -0
  146. package/dist/reactive-map-BcB4KsQR.d.ts +256 -0
  147. package/dist/reactive-map-D-g3dv8I.d.cts +256 -0
  148. package/dist/read-through-B1CYPfq_.d.cts +82 -0
  149. package/dist/read-through-DwyJhGs1.d.ts +82 -0
  150. package/dist/render/index.cjs +250 -0
  151. package/dist/render/index.cjs.map +1 -0
  152. package/dist/render/index.d.cts +58 -0
  153. package/dist/render/index.d.ts +58 -0
  154. package/dist/render/index.js +20 -0
  155. package/dist/render/index.js.map +1 -0
  156. package/dist/resilience-Dig3Q7n0.d.cts +45 -0
  157. package/dist/resilience-Dig3Q7n0.d.ts +45 -0
  158. package/dist/semantic-memory-graph-BYoIlP35.d.cts +356 -0
  159. package/dist/semantic-memory-graph-CxyXLms3.d.ts +356 -0
  160. package/dist/solutions/index.cjs +3249 -0
  161. package/dist/solutions/index.cjs.map +1 -0
  162. package/dist/solutions/index.d.cts +520 -0
  163. package/dist/solutions/index.d.ts +520 -0
  164. package/dist/solutions/index.js +31 -0
  165. package/dist/solutions/index.js.map +1 -0
  166. package/dist/sources/browser.cjs +3095 -0
  167. package/dist/sources/browser.cjs.map +1 -0
  168. package/dist/sources/browser.d.cts +39 -0
  169. package/dist/sources/browser.d.ts +39 -0
  170. package/dist/sources/browser.js +137 -0
  171. package/dist/sources/browser.js.map +1 -0
  172. package/dist/sources/index.cjs +3021 -0
  173. package/dist/sources/index.cjs.map +1 -0
  174. package/dist/sources/index.d.cts +7 -0
  175. package/dist/sources/index.d.ts +7 -0
  176. package/dist/sources/index.js +63 -0
  177. package/dist/sources/index.js.map +1 -0
  178. package/dist/sources/node.cjs +675 -0
  179. package/dist/sources/node.cjs.map +1 -0
  180. package/dist/sources/node.d.cts +109 -0
  181. package/dist/sources/node.d.ts +109 -0
  182. package/dist/sources/node.js +639 -0
  183. package/dist/sources/node.js.map +1 -0
  184. package/dist/storage/browser.cjs +777 -0
  185. package/dist/storage/browser.cjs.map +1 -0
  186. package/dist/storage/browser.d.cts +40 -0
  187. package/dist/storage/browser.d.ts +40 -0
  188. package/dist/storage/browser.js +275 -0
  189. package/dist/storage/browser.js.map +1 -0
  190. package/dist/storage/index.cjs +1989 -0
  191. package/dist/storage/index.cjs.map +1 -0
  192. package/dist/storage/index.d.cts +92 -0
  193. package/dist/storage/index.d.ts +92 -0
  194. package/dist/storage/index.js +161 -0
  195. package/dist/storage/index.js.map +1 -0
  196. package/dist/storage/node.cjs +898 -0
  197. package/dist/storage/node.cjs.map +1 -0
  198. package/dist/storage/node.d.cts +66 -0
  199. package/dist/storage/node.d.ts +66 -0
  200. package/dist/storage/node.js +402 -0
  201. package/dist/storage/node.js.map +1 -0
  202. package/dist/testing/index.cjs +95 -0
  203. package/dist/testing/index.cjs.map +1 -0
  204. package/dist/testing/index.d.cts +20 -0
  205. package/dist/testing/index.d.ts +20 -0
  206. package/dist/testing/index.js +8 -0
  207. package/dist/testing/index.js.map +1 -0
  208. package/dist/types-bNMSWKqF.d.cts +732 -0
  209. package/dist/types-h12mNYbq.d.ts +732 -0
  210. package/package.json +250 -0
@@ -0,0 +1,818 @@
1
+ import {
2
+ assertReactiveCollectionChangeFrame,
3
+ assertReactiveCollectionSnapshotFrame,
4
+ collectionSnapshotKey,
5
+ loadReactiveIndexState,
6
+ loadReactiveListState,
7
+ loadReactiveLogState,
8
+ loadReactiveMapState,
9
+ reactiveCollectionChangeFrame,
10
+ reactiveCollectionSnapshotFrame
11
+ } from "./chunk-GTPCOEOZ.js";
12
+ import {
13
+ agenticMemoryRecordCodec,
14
+ agenticMemoryRecordFrame,
15
+ agenticMemoryRecordFrameCodec,
16
+ assertAgenticMemoryRecordFrame
17
+ } from "./chunk-COHO25CZ.js";
18
+ import {
19
+ assertGraphLocalNode
20
+ } from "./chunk-2ILL7HQI.js";
21
+ import {
22
+ restoreReactiveIndex,
23
+ restoreReactiveList,
24
+ restoreReactiveLog,
25
+ restoreReactiveMap
26
+ } from "./chunk-RIIOUTGM.js";
27
+
28
+ // src/adapters/agentic-memory-storage.ts
29
+ var AGENTIC_MEMORY_RECORD_SNAPSHOT_FORMAT = "graphrefly.agenticMemory.records.snapshot";
30
+ var AGENTIC_MEMORY_RECORD_CHANGE_FORMAT = "graphrefly.agenticMemory.records.change";
31
+ var AGENTIC_MEMORY_RECORD_STORAGE_FRAME_VERSION = 1;
32
+ function agenticMemoryRecordsSnapshotKey(storagePrefix = "agentic-memory") {
33
+ if (storagePrefix.length === 0) throw new TypeError("storagePrefix must be non-empty");
34
+ return `${storagePrefix}/records.snapshot`;
35
+ }
36
+ function agenticMemoryRecordSnapshotFrame(records, opts = {}) {
37
+ const changeCursor = validateChangeCursor(opts.changeCursor ?? -1);
38
+ return assertAgenticMemoryRecordSnapshotFrame({
39
+ format: AGENTIC_MEMORY_RECORD_SNAPSHOT_FORMAT,
40
+ version: AGENTIC_MEMORY_RECORD_STORAGE_FRAME_VERSION,
41
+ changeCursor,
42
+ records: records.map((record) => agenticMemoryRecordFrame(record))
43
+ });
44
+ }
45
+ function agenticMemoryRecordChangeFrame(records) {
46
+ return assertAgenticMemoryRecordChangeFrame({
47
+ format: AGENTIC_MEMORY_RECORD_CHANGE_FORMAT,
48
+ version: AGENTIC_MEMORY_RECORD_STORAGE_FRAME_VERSION,
49
+ change: {
50
+ kind: "replaceAll",
51
+ records: records.map((record) => agenticMemoryRecordFrame(record))
52
+ }
53
+ });
54
+ }
55
+ function assertAgenticMemoryRecordSnapshotFrame(value) {
56
+ if (!isPlainRecord(value)) throw new TypeError("agentic memory snapshot frame must be an object");
57
+ assertKeys(
58
+ value,
59
+ ["changeCursor", "format", "records", "version"],
60
+ "agentic memory snapshot frame"
61
+ );
62
+ if (value.format !== AGENTIC_MEMORY_RECORD_SNAPSHOT_FORMAT) {
63
+ throw new TypeError("agentic memory snapshot frame: invalid format");
64
+ }
65
+ if (value.version !== AGENTIC_MEMORY_RECORD_STORAGE_FRAME_VERSION) {
66
+ throw new TypeError("agentic memory snapshot frame: invalid version");
67
+ }
68
+ const changeCursor = validateChangeCursor(value.changeCursor);
69
+ if (!Array.isArray(value.records)) {
70
+ throw new TypeError("agentic memory snapshot frame: records must be an array");
71
+ }
72
+ const records = assertDenseRecordFrames(
73
+ value.records,
74
+ "agentic memory snapshot frame records"
75
+ );
76
+ return Object.freeze({
77
+ format: AGENTIC_MEMORY_RECORD_SNAPSHOT_FORMAT,
78
+ version: AGENTIC_MEMORY_RECORD_STORAGE_FRAME_VERSION,
79
+ changeCursor,
80
+ records: Object.freeze(records)
81
+ });
82
+ }
83
+ function assertAgenticMemoryRecordChangeFrame(value) {
84
+ if (!isPlainRecord(value)) throw new TypeError("agentic memory change frame must be an object");
85
+ assertKeys(value, ["change", "format", "version"], "agentic memory change frame");
86
+ if (value.format !== AGENTIC_MEMORY_RECORD_CHANGE_FORMAT) {
87
+ throw new TypeError("agentic memory change frame: invalid format");
88
+ }
89
+ if (value.version !== AGENTIC_MEMORY_RECORD_STORAGE_FRAME_VERSION) {
90
+ throw new TypeError("agentic memory change frame: invalid version");
91
+ }
92
+ if (!isPlainRecord(value.change)) {
93
+ throw new TypeError("agentic memory change frame: change must be an object");
94
+ }
95
+ assertKeys(value.change, ["kind", "records"], "agentic memory change frame change");
96
+ if (value.change.kind !== "replaceAll") {
97
+ throw new TypeError("agentic memory change frame: change.kind must be replaceAll");
98
+ }
99
+ if (!Array.isArray(value.change.records)) {
100
+ throw new TypeError("agentic memory change frame: change.records must be an array");
101
+ }
102
+ const records = assertDenseRecordFrames(
103
+ value.change.records,
104
+ "agentic memory change frame records"
105
+ );
106
+ return Object.freeze({
107
+ format: AGENTIC_MEMORY_RECORD_CHANGE_FORMAT,
108
+ version: AGENTIC_MEMORY_RECORD_STORAGE_FRAME_VERSION,
109
+ change: Object.freeze({
110
+ kind: "replaceAll",
111
+ records: Object.freeze(records)
112
+ })
113
+ });
114
+ }
115
+ function loadAgenticMemoryRecordsState(opts) {
116
+ const storagePrefix = opts.storagePrefix ?? "agentic-memory";
117
+ const snapshotKey = opts.snapshotKey ?? agenticMemoryRecordsSnapshotKey(storagePrefix);
118
+ return opts.snapshotStore.get(snapshotKey).then((rawSnapshot) => {
119
+ let records = [];
120
+ let source = "empty";
121
+ let changeCursor = -1;
122
+ if (rawSnapshot !== void 0) {
123
+ const snapshot = assertAgenticMemoryRecordSnapshotFrame(rawSnapshot);
124
+ records = snapshot.records.map(recordFromFrame);
125
+ changeCursor = snapshot.changeCursor;
126
+ source = "snapshot";
127
+ }
128
+ if (opts.changeLog === void 0) {
129
+ return {
130
+ records,
131
+ source,
132
+ snapshot: { found: rawSnapshot !== void 0, changeCursor },
133
+ changes: { applied: 0, cursor: changeCursor }
134
+ };
135
+ }
136
+ return opts.changeLog.read({ after: changeCursor }).then((entries) => {
137
+ let applied = 0;
138
+ let cursor = changeCursor;
139
+ for (const entry of entries) {
140
+ if (entry.seq !== cursor + 1) {
141
+ throw new Error("agentic memory records change log is non-contiguous");
142
+ }
143
+ const change = assertAgenticMemoryRecordChangeFrame(entry.value);
144
+ records = change.change.records.map(recordFromFrame);
145
+ cursor = entry.seq;
146
+ applied += 1;
147
+ }
148
+ return {
149
+ records,
150
+ source: rawSnapshot !== void 0 && applied > 0 ? "snapshot+changes" : rawSnapshot !== void 0 ? "snapshot" : applied > 0 ? "changes" : "empty",
151
+ snapshot: { found: rawSnapshot !== void 0, changeCursor },
152
+ changes: { applied, cursor }
153
+ };
154
+ });
155
+ });
156
+ }
157
+ function persistAgenticMemoryRecords(graph, records, opts) {
158
+ const name = opts.name ?? "agenticMemoryRecords.persistence";
159
+ const snapshotKey = opts.snapshotKey ?? agenticMemoryRecordsSnapshotKey(opts.storagePrefix ?? name);
160
+ const snapshotEveryChanges = validateSnapshotEveryChanges(opts.snapshotEveryChanges);
161
+ assertGraphLocalNode(graph, records, `${name}.records`);
162
+ let cursorValue = {
163
+ kind: "persistence.cursor",
164
+ changeSeq: validateChangeCursor(opts.initialChangeCursor ?? -1),
165
+ snapshotWrites: 0,
166
+ changeWrites: 0
167
+ };
168
+ let statusValue = statusOf("starting", 0, 0, 0, cursorValue);
169
+ let errorCount = 0;
170
+ let writes = 0;
171
+ let running = false;
172
+ let disposed = false;
173
+ let disposeRequested = false;
174
+ let subscribing = true;
175
+ let observedChangesSinceSnapshot = 0;
176
+ let latestRecords = records.cache ?? [];
177
+ let stop;
178
+ const queue = [];
179
+ const ready = graph.state(false, { name: `${name}/ready` });
180
+ const status = graph.state(statusValue, {
181
+ name: `${name}/status`
182
+ });
183
+ const error = graph.state(null, {
184
+ name: `${name}/error`
185
+ });
186
+ const cursor = graph.state(cursorValue, {
187
+ name: `${name}/cursor`
188
+ });
189
+ function publish(state) {
190
+ const nextState = statusValue.state === "errored" && state !== "disposed" ? "errored" : state;
191
+ statusValue = statusOf(
192
+ nextState,
193
+ queue.length + (running ? 1 : 0),
194
+ writes,
195
+ errorCount,
196
+ cursorValue
197
+ );
198
+ cursor.set(cursorValue);
199
+ status.set(statusValue);
200
+ if (nextState === "ready") ready.set(true);
201
+ if (nextState === "errored" || nextState === "disposed") ready.set(false);
202
+ }
203
+ function report(phase, caught) {
204
+ errorCount += 1;
205
+ error.set({ phase, message: errorMessage(caught), cursor: cursorValue });
206
+ }
207
+ function callDone(done) {
208
+ try {
209
+ done?.(statusValue);
210
+ } catch {
211
+ }
212
+ }
213
+ function finish(item) {
214
+ if (item.phase === "dispose") {
215
+ disposed = true;
216
+ running = false;
217
+ publish("disposed");
218
+ } else if (statusValue.state !== "errored") {
219
+ running = false;
220
+ publish("ready");
221
+ } else {
222
+ running = false;
223
+ publish("errored");
224
+ }
225
+ callDone(item.done);
226
+ drain();
227
+ }
228
+ function fail(item, caught) {
229
+ if (item.phase === "snapshot" || item.phase === "change") report(item.phase, caught);
230
+ running = false;
231
+ if (item.phase === "snapshot" || item.phase === "change") publish("errored");
232
+ callDone(item.done);
233
+ drain();
234
+ }
235
+ function chainThenable(item, value) {
236
+ if (typeof value !== "object" && typeof value !== "function" || value === null) return false;
237
+ const then = value.then;
238
+ if (typeof then !== "function") return false;
239
+ let settled = false;
240
+ const settle = (next) => {
241
+ if (settled) return;
242
+ settled = true;
243
+ next();
244
+ };
245
+ try {
246
+ then.call(
247
+ value,
248
+ () => settle(() => finish(item)),
249
+ (caught) => settle(() => fail(item, caught))
250
+ );
251
+ } catch (caught) {
252
+ settle(() => fail(item, caught));
253
+ }
254
+ return true;
255
+ }
256
+ function enqueue(item) {
257
+ if (disposed || disposeRequested && item.phase !== "dispose" && item.phase !== "flush") {
258
+ callDone(item.done);
259
+ return;
260
+ }
261
+ queue.push(item);
262
+ drain();
263
+ }
264
+ function drain() {
265
+ if (running) return;
266
+ const item = queue.shift();
267
+ if (item === void 0) return;
268
+ running = true;
269
+ publish(
270
+ item.phase === "flush" ? "flushing" : statusValue.state === "starting" ? "starting" : "ready"
271
+ );
272
+ let result;
273
+ try {
274
+ result = item.run();
275
+ } catch (caught) {
276
+ fail(item, caught);
277
+ return;
278
+ }
279
+ if (chainThenable(item, result)) return;
280
+ finish(item);
281
+ }
282
+ function enqueueSnapshot(done) {
283
+ const snapshotRecords = latestRecords;
284
+ observedChangesSinceSnapshot = 0;
285
+ enqueue({
286
+ phase: "snapshot",
287
+ done,
288
+ run() {
289
+ const records2 = Object.freeze(
290
+ snapshotRecords.map((record) => agenticMemoryRecordFrame(record))
291
+ );
292
+ const frame = assertAgenticMemoryRecordSnapshotFrame({
293
+ format: AGENTIC_MEMORY_RECORD_SNAPSHOT_FORMAT,
294
+ version: AGENTIC_MEMORY_RECORD_STORAGE_FRAME_VERSION,
295
+ changeCursor: cursorValue.changeSeq,
296
+ records: records2
297
+ });
298
+ return opts.snapshotStore.set(snapshotKey, frame).then(() => {
299
+ cursorValue = { ...cursorValue, snapshotWrites: cursorValue.snapshotWrites + 1 };
300
+ writes += 1;
301
+ publish("ready");
302
+ });
303
+ }
304
+ });
305
+ }
306
+ function enqueueChange(nextRecords) {
307
+ const changeLog = opts.changeLog;
308
+ if (changeLog === void 0) return;
309
+ enqueue({
310
+ phase: "change",
311
+ run() {
312
+ const frame = agenticMemoryRecordChangeFrame(nextRecords);
313
+ return changeLog.append(frame).then((written) => {
314
+ const entry = written;
315
+ cursorValue = {
316
+ ...cursorValue,
317
+ changeSeq: entry.seq,
318
+ changeWrites: cursorValue.changeWrites + 1
319
+ };
320
+ writes += 1;
321
+ publish("ready");
322
+ });
323
+ }
324
+ });
325
+ }
326
+ function cancelQueuedForDispose() {
327
+ const pending = queue.splice(0);
328
+ for (const item of pending) callDone(item.done);
329
+ }
330
+ stop = records.subscribe((msg, delivery) => {
331
+ if (disposeRequested) return;
332
+ const nextRecords = dataOf(msg);
333
+ if (nextRecords === void 0) return;
334
+ latestRecords = nextRecords;
335
+ if (subscribing && delivery === void 0) return;
336
+ observedChangesSinceSnapshot += 1;
337
+ enqueueChange(nextRecords);
338
+ if (snapshotEveryChanges !== void 0 && observedChangesSinceSnapshot >= snapshotEveryChanges) {
339
+ enqueueSnapshot();
340
+ }
341
+ });
342
+ subscribing = false;
343
+ if (opts.snapshotOnAttach !== false) enqueueSnapshot();
344
+ else publish("ready");
345
+ return {
346
+ ready,
347
+ status,
348
+ error,
349
+ cursor,
350
+ flush(done) {
351
+ if (disposed) {
352
+ callDone(done);
353
+ return;
354
+ }
355
+ enqueue({ phase: "flush", done, run: () => void 0 });
356
+ },
357
+ snapshot(done) {
358
+ if (disposed || disposeRequested) {
359
+ callDone(done);
360
+ return;
361
+ }
362
+ enqueueSnapshot(done);
363
+ },
364
+ dispose(done) {
365
+ if (disposed) {
366
+ callDone(done);
367
+ return;
368
+ }
369
+ disposeRequested = true;
370
+ stop?.();
371
+ cancelQueuedForDispose();
372
+ enqueue({ phase: "dispose", done, run: () => void 0 });
373
+ }
374
+ };
375
+ }
376
+ function openPersistentAgenticMemoryRecords(opts) {
377
+ const storagePrefix = opts.storagePrefix ?? opts.name ?? "agentic-memory";
378
+ const snapshotKey = opts.snapshotKey ?? agenticMemoryRecordsSnapshotKey(storagePrefix);
379
+ return loadAgenticMemoryRecordsState({ ...opts, snapshotKey, storagePrefix }).then(
380
+ (loaded) => {
381
+ const records = loaded.source === "empty" && opts.initial !== void 0 ? opts.initial : loaded.records;
382
+ const node = opts.graph.state(records, {
383
+ name: opts.name
384
+ });
385
+ const persistence = persistAgenticMemoryRecords(opts.graph, node, {
386
+ ...opts,
387
+ name: opts.name ? `${opts.name}/persistence` : void 0,
388
+ snapshotKey,
389
+ storagePrefix,
390
+ initialChangeCursor: loaded.changes.cursor
391
+ });
392
+ return { records: node, persistence, loaded };
393
+ }
394
+ );
395
+ }
396
+ function recordFromFrame(frame) {
397
+ return agenticMemoryRecordCodec().decode(
398
+ agenticMemoryRecordFrameCodec().encode(frame)
399
+ );
400
+ }
401
+ function statusOf(state, pending, writes, errors, cursor) {
402
+ return { state, pending, writes, errors, cursor };
403
+ }
404
+ function dataOf(msg) {
405
+ return msg[0] === "DATA" ? msg[1] : void 0;
406
+ }
407
+ function validateChangeCursor(value) {
408
+ if (typeof value !== "number" || !Number.isSafeInteger(value) || value < -1) {
409
+ throw new RangeError("changeCursor must be a safe integer >= -1");
410
+ }
411
+ return value;
412
+ }
413
+ function validateSnapshotEveryChanges(value) {
414
+ if (value === void 0) return void 0;
415
+ if (!Number.isSafeInteger(value) || value < 1) {
416
+ throw new RangeError("snapshotEveryChanges must be a positive safe integer");
417
+ }
418
+ return value;
419
+ }
420
+ function isPlainRecord(value) {
421
+ return value !== null && typeof value === "object" && !Array.isArray(value);
422
+ }
423
+ function assertKeys(value, expected, label) {
424
+ const actual = Object.keys(value).sort();
425
+ const want = [...expected].sort();
426
+ if (actual.length !== want.length || actual.some((key, i) => key !== want[i])) {
427
+ throw new TypeError(`${label}: unexpected frame fields ${actual.join(",")}`);
428
+ }
429
+ }
430
+ function assertDenseRecordFrames(value, label) {
431
+ const records = [];
432
+ for (let i = 0; i < value.length; i += 1) {
433
+ if (!Object.hasOwn(value, i)) {
434
+ throw new TypeError(`${label}: sparse array hole at ${i}`);
435
+ }
436
+ records.push(assertAgenticMemoryRecordFrame(value[i]));
437
+ }
438
+ return Object.freeze(records);
439
+ }
440
+ function errorMessage(error) {
441
+ return error instanceof Error ? error.message : String(error);
442
+ }
443
+
444
+ // src/adapters/reactive-collection-storage.ts
445
+ function emptyCursor(collection) {
446
+ return {
447
+ kind: "persistence.cursor",
448
+ collection,
449
+ changeSeq: -1,
450
+ snapshotWrites: 0,
451
+ changeWrites: 0
452
+ };
453
+ }
454
+ function errorMessage2(error) {
455
+ return error instanceof Error ? error.message : String(error);
456
+ }
457
+ function validateSnapshotEveryChanges2(value) {
458
+ if (value === void 0) return void 0;
459
+ if (!Number.isSafeInteger(value) || value < 1) {
460
+ throw new RangeError("snapshotEveryChanges must be a positive safe integer");
461
+ }
462
+ return value;
463
+ }
464
+ function validateInitialChangeCursor(value) {
465
+ if (value === void 0) return -1;
466
+ if (!Number.isSafeInteger(value) || value < -1) {
467
+ throw new RangeError("initialChangeCursor must be a safe integer >= -1");
468
+ }
469
+ return value;
470
+ }
471
+ function statusOf2(state, pending, writes, errors, cursor) {
472
+ return { state, pending, writes, errors, cursor };
473
+ }
474
+ function dataOf2(msg) {
475
+ return msg[0] === "DATA" ? msg[1] : void 0;
476
+ }
477
+ function persistReactiveCollection(graph, collection, opts) {
478
+ const kind = opts.kind;
479
+ const name = opts.name ?? `${kind}.persistence`;
480
+ const snapshotKey = opts.snapshotKey ?? collectionSnapshotKey(opts.storagePrefix ?? name);
481
+ const snapshotEveryChanges = validateSnapshotEveryChanges2(opts.snapshotEveryChanges);
482
+ assertGraphLocalNode(graph, collection.delta, `${name}.collection.delta`);
483
+ assertGraphLocalNode(graph, collection.snapshot, `${name}.collection.snapshot`);
484
+ let cursorValue = {
485
+ ...emptyCursor(kind),
486
+ changeSeq: validateInitialChangeCursor(opts.initialChangeCursor)
487
+ };
488
+ let statusValue = statusOf2("starting", 0, 0, 0, cursorValue);
489
+ let errorCount = 0;
490
+ let writes = 0;
491
+ let observedChangesSinceSnapshot = 0;
492
+ let running = false;
493
+ let disposed = false;
494
+ let disposeRequested = false;
495
+ let subscribing = true;
496
+ let stop;
497
+ const queue = [];
498
+ const ready = graph.state(false, { name: `${name}/ready` });
499
+ const status = graph.state(statusValue, {
500
+ name: `${name}/status`
501
+ });
502
+ const error = graph.state(null, {
503
+ name: `${name}/error`
504
+ });
505
+ const cursor = graph.state(cursorValue, {
506
+ name: `${name}/cursor`
507
+ });
508
+ function publish(state) {
509
+ const nextState = statusValue.state === "errored" && state !== "disposed" ? "errored" : state;
510
+ statusValue = statusOf2(
511
+ nextState,
512
+ queue.length + (running ? 1 : 0),
513
+ writes,
514
+ errorCount,
515
+ cursorValue
516
+ );
517
+ cursor.set(cursorValue);
518
+ status.set(statusValue);
519
+ if (nextState === "ready") ready.set(true);
520
+ if (nextState === "errored" || nextState === "disposed") ready.set(false);
521
+ }
522
+ function report(phase, caught) {
523
+ errorCount += 1;
524
+ const fact = { phase, message: errorMessage2(caught), cursor: cursorValue };
525
+ error.set(fact);
526
+ }
527
+ function callDone(done) {
528
+ try {
529
+ done?.(statusValue);
530
+ } catch {
531
+ }
532
+ }
533
+ function finish(item) {
534
+ if (item.phase === "dispose") {
535
+ disposed = true;
536
+ running = false;
537
+ publish("disposed");
538
+ } else if (statusValue.state !== "errored") {
539
+ running = false;
540
+ publish("ready");
541
+ } else {
542
+ running = false;
543
+ publish("errored");
544
+ }
545
+ callDone(item.done);
546
+ drain();
547
+ }
548
+ function fail(item, caught) {
549
+ if (item.phase === "snapshot" || item.phase === "change") report(item.phase, caught);
550
+ running = false;
551
+ if (item.phase === "snapshot" || item.phase === "change") publish("errored");
552
+ callDone(item.done);
553
+ drain();
554
+ }
555
+ function chainThenable(item, value) {
556
+ if (typeof value !== "object" && typeof value !== "function" || value === null) return false;
557
+ let then;
558
+ try {
559
+ then = value.then;
560
+ } catch (caught) {
561
+ fail(item, caught);
562
+ return true;
563
+ }
564
+ if (typeof then !== "function") return false;
565
+ let settled = false;
566
+ const settle = (next) => {
567
+ if (settled) return;
568
+ settled = true;
569
+ next();
570
+ };
571
+ try {
572
+ then.call(
573
+ value,
574
+ () => settle(() => finish(item)),
575
+ (caught) => settle(() => fail(item, caught))
576
+ );
577
+ } catch (caught) {
578
+ settle(() => fail(item, caught));
579
+ }
580
+ return true;
581
+ }
582
+ function enqueue(item) {
583
+ if (disposed || disposeRequested && item.phase !== "dispose" && item.phase !== "flush") {
584
+ callDone(item.done);
585
+ return;
586
+ }
587
+ queue.push(item);
588
+ drain();
589
+ }
590
+ function drain() {
591
+ if (running) return;
592
+ const item = queue.shift();
593
+ if (item === void 0) return;
594
+ running = true;
595
+ publish(
596
+ item.phase === "flush" ? "flushing" : statusValue.state === "starting" ? "starting" : "ready"
597
+ );
598
+ let result;
599
+ try {
600
+ result = item.run();
601
+ } catch (caught) {
602
+ fail(item, caught);
603
+ return;
604
+ }
605
+ if (chainThenable(item, result)) return;
606
+ finish(item);
607
+ }
608
+ function enqueueSnapshot(done) {
609
+ const snapshot = collectionSnapshot(collection, kind);
610
+ observedChangesSinceSnapshot = 0;
611
+ enqueue({
612
+ phase: "snapshot",
613
+ done,
614
+ run() {
615
+ const frame = reactiveCollectionSnapshotFrame(kind, snapshot, {
616
+ changeCursor: cursorValue.changeSeq
617
+ });
618
+ assertReactiveCollectionSnapshotFrame(frame, kind);
619
+ return opts.snapshotStore.set(snapshotKey, frame).then(() => {
620
+ cursorValue = {
621
+ ...cursorValue,
622
+ snapshotWrites: cursorValue.snapshotWrites + 1
623
+ };
624
+ writes += 1;
625
+ publish("ready");
626
+ });
627
+ }
628
+ });
629
+ }
630
+ function enqueueChange(change) {
631
+ const changeLog = opts.changeLog;
632
+ if (changeLog === void 0) return;
633
+ enqueue({
634
+ phase: "change",
635
+ run() {
636
+ const frame = reactiveCollectionChangeFrame(kind, change);
637
+ assertReactiveCollectionChangeFrame(frame, kind);
638
+ return changeLog.append(frame).then((written) => {
639
+ const entry = written;
640
+ cursorValue = {
641
+ ...cursorValue,
642
+ changeSeq: entry.seq,
643
+ changeWrites: cursorValue.changeWrites + 1
644
+ };
645
+ writes += 1;
646
+ publish("ready");
647
+ });
648
+ }
649
+ });
650
+ }
651
+ function cancelQueuedForDispose() {
652
+ const pending = queue.splice(0);
653
+ for (const item of pending) callDone(item.done);
654
+ }
655
+ stop = collection.delta.subscribe(
656
+ (msg, delivery) => {
657
+ if (disposeRequested) return;
658
+ const change = dataOf2(msg);
659
+ if (change === void 0) return;
660
+ if (subscribing && delivery === void 0) return;
661
+ observedChangesSinceSnapshot += 1;
662
+ enqueueChange(change);
663
+ if (snapshotEveryChanges !== void 0 && observedChangesSinceSnapshot >= snapshotEveryChanges) {
664
+ enqueueSnapshot();
665
+ }
666
+ }
667
+ );
668
+ subscribing = false;
669
+ if (opts.snapshotOnAttach !== false) enqueueSnapshot();
670
+ else publish("ready");
671
+ return {
672
+ ready,
673
+ status,
674
+ error,
675
+ cursor,
676
+ flush(done) {
677
+ if (disposed) {
678
+ callDone(done);
679
+ return;
680
+ }
681
+ enqueue({ phase: "flush", done, run: () => void 0 });
682
+ },
683
+ snapshot(done) {
684
+ if (disposed || disposeRequested) {
685
+ callDone(done);
686
+ return;
687
+ }
688
+ enqueueSnapshot(done);
689
+ },
690
+ dispose(done) {
691
+ if (disposed) {
692
+ callDone(done);
693
+ return;
694
+ }
695
+ disposeRequested = true;
696
+ stop?.();
697
+ cancelQueuedForDispose();
698
+ enqueue({ phase: "dispose", done, run: () => void 0 });
699
+ }
700
+ };
701
+ }
702
+ function collectionSnapshot(collection, kind) {
703
+ if (kind === "reactiveMap") {
704
+ return [...collection.toMap()];
705
+ }
706
+ if (kind === "reactiveIndex") {
707
+ return collection.toArray();
708
+ }
709
+ return collection.toArray();
710
+ }
711
+ function openPersistentReactiveList(opts) {
712
+ const storagePrefix = opts.storagePrefix ?? opts.name ?? "reactive-collection";
713
+ const snapshotKey = opts.snapshotKey ?? collectionSnapshotKey(storagePrefix);
714
+ const loadOpts = { ...opts, snapshotKey, storagePrefix };
715
+ return loadReactiveListState(loadOpts).then((loaded) => {
716
+ const state = loaded.source === "empty" && opts.initial !== void 0 ? { ...loaded, state: opts.initial } : loaded;
717
+ const collection = restoreReactiveList(state, {
718
+ ...opts.collection ?? {},
719
+ graph: opts.graph,
720
+ name: opts.name
721
+ });
722
+ const persistence = persistReactiveCollection(opts.graph, collection, {
723
+ ...opts,
724
+ kind: "reactiveList",
725
+ name: opts.name ? `${opts.name}/persistence` : void 0,
726
+ snapshotKey,
727
+ storagePrefix,
728
+ initialChangeCursor: loaded.changes.cursor
729
+ });
730
+ return { collection, persistence, loaded };
731
+ });
732
+ }
733
+ function openPersistentReactiveLog(opts) {
734
+ const storagePrefix = opts.storagePrefix ?? opts.name ?? "reactive-collection";
735
+ const snapshotKey = opts.snapshotKey ?? collectionSnapshotKey(storagePrefix);
736
+ const loadOpts = { ...opts, snapshotKey, storagePrefix };
737
+ return loadReactiveLogState(loadOpts).then((loaded) => {
738
+ const state = loaded.source === "empty" && opts.initial !== void 0 ? { ...loaded, state: opts.initial } : loaded;
739
+ const collection = restoreReactiveLog(state, {
740
+ ...opts.collection ?? {},
741
+ graph: opts.graph,
742
+ name: opts.name
743
+ });
744
+ const persistence = persistReactiveCollection(opts.graph, collection, {
745
+ ...opts,
746
+ kind: "reactiveLog",
747
+ name: opts.name ? `${opts.name}/persistence` : void 0,
748
+ snapshotKey,
749
+ storagePrefix,
750
+ initialChangeCursor: loaded.changes.cursor
751
+ });
752
+ return { collection, persistence, loaded };
753
+ });
754
+ }
755
+ function openPersistentReactiveMap(opts) {
756
+ const storagePrefix = opts.storagePrefix ?? opts.name ?? "reactive-collection";
757
+ const snapshotKey = opts.snapshotKey ?? collectionSnapshotKey(storagePrefix);
758
+ const loadOpts = { ...opts, snapshotKey, storagePrefix };
759
+ return loadReactiveMapState(loadOpts).then((loaded) => {
760
+ const state = loaded.source === "empty" && opts.initial !== void 0 ? { ...loaded, state: opts.initial } : loaded;
761
+ const collection = restoreReactiveMap(state, {
762
+ ...opts.collection ?? {},
763
+ graph: opts.graph,
764
+ name: opts.name
765
+ });
766
+ const persistence = persistReactiveCollection(opts.graph, collection, {
767
+ ...opts,
768
+ kind: "reactiveMap",
769
+ name: opts.name ? `${opts.name}/persistence` : void 0,
770
+ snapshotKey,
771
+ storagePrefix,
772
+ initialChangeCursor: loaded.changes.cursor
773
+ });
774
+ return { collection, persistence, loaded };
775
+ });
776
+ }
777
+ function openPersistentReactiveIndex(opts) {
778
+ const storagePrefix = opts.storagePrefix ?? opts.name ?? "reactive-collection";
779
+ const snapshotKey = opts.snapshotKey ?? collectionSnapshotKey(storagePrefix);
780
+ const loadOpts = { ...opts, snapshotKey, storagePrefix };
781
+ return loadReactiveIndexState(loadOpts).then((loaded) => {
782
+ const state = loaded.source === "empty" && opts.initial !== void 0 ? { ...loaded, state: opts.initial } : loaded;
783
+ const collection = restoreReactiveIndex(state, {
784
+ ...opts.collection ?? {},
785
+ graph: opts.graph,
786
+ name: opts.name
787
+ });
788
+ const persistence = persistReactiveCollection(opts.graph, collection, {
789
+ ...opts,
790
+ kind: "reactiveIndex",
791
+ name: opts.name ? `${opts.name}/persistence` : void 0,
792
+ snapshotKey,
793
+ storagePrefix,
794
+ initialChangeCursor: loaded.changes.cursor
795
+ });
796
+ return { collection, persistence, loaded };
797
+ });
798
+ }
799
+
800
+ export {
801
+ AGENTIC_MEMORY_RECORD_SNAPSHOT_FORMAT,
802
+ AGENTIC_MEMORY_RECORD_CHANGE_FORMAT,
803
+ AGENTIC_MEMORY_RECORD_STORAGE_FRAME_VERSION,
804
+ agenticMemoryRecordsSnapshotKey,
805
+ agenticMemoryRecordSnapshotFrame,
806
+ agenticMemoryRecordChangeFrame,
807
+ assertAgenticMemoryRecordSnapshotFrame,
808
+ assertAgenticMemoryRecordChangeFrame,
809
+ loadAgenticMemoryRecordsState,
810
+ persistAgenticMemoryRecords,
811
+ openPersistentAgenticMemoryRecords,
812
+ persistReactiveCollection,
813
+ openPersistentReactiveList,
814
+ openPersistentReactiveLog,
815
+ openPersistentReactiveMap,
816
+ openPersistentReactiveIndex
817
+ };
818
+ //# sourceMappingURL=chunk-DJC4CHLQ.js.map