@agentshouse/mdruntime 0.1.0-cfbf80d8d4644 → 0.1.0-cfcf93be6310b

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 (187) hide show
  1. package/README.md +24 -11
  2. package/dist/adapter.d.ts +182 -47
  3. package/dist/adapter.d.ts.map +1 -1
  4. package/dist/attached-model.d.ts +5 -1
  5. package/dist/attached-model.d.ts.map +1 -1
  6. package/dist/attached-model.js +53 -6
  7. package/dist/attempt.d.ts +16 -0
  8. package/dist/attempt.d.ts.map +1 -0
  9. package/dist/attempt.js +70 -0
  10. package/dist/binary.d.ts +18 -0
  11. package/dist/binary.d.ts.map +1 -0
  12. package/dist/binary.js +8 -0
  13. package/dist/bootstrap.d.ts +33 -5
  14. package/dist/bootstrap.d.ts.map +1 -1
  15. package/dist/bootstrap.js +202 -48
  16. package/dist/chain.d.ts +11 -0
  17. package/dist/chain.d.ts.map +1 -0
  18. package/dist/chain.js +64 -0
  19. package/dist/change-set.d.ts +9 -14
  20. package/dist/change-set.d.ts.map +1 -1
  21. package/dist/change-set.js +11 -56
  22. package/dist/change.d.ts +45 -0
  23. package/dist/change.d.ts.map +1 -1
  24. package/dist/change.js +34 -0
  25. package/dist/client.d.ts +38 -17
  26. package/dist/client.d.ts.map +1 -1
  27. package/dist/client.js +156 -20
  28. package/dist/collection.d.ts +13 -14
  29. package/dist/collection.d.ts.map +1 -1
  30. package/dist/collection.js +52 -56
  31. package/dist/compile.d.ts +16 -0
  32. package/dist/compile.d.ts.map +1 -0
  33. package/dist/compile.js +789 -0
  34. package/dist/connection.d.ts +59 -20
  35. package/dist/connection.d.ts.map +1 -1
  36. package/dist/connection.js +4080 -879
  37. package/dist/context.d.ts +2 -0
  38. package/dist/context.d.ts.map +1 -0
  39. package/dist/context.js +7 -0
  40. package/dist/conversation.d.ts +51 -20
  41. package/dist/conversation.d.ts.map +1 -1
  42. package/dist/conversation.js +732 -242
  43. package/dist/digest.d.ts +7 -0
  44. package/dist/digest.d.ts.map +1 -0
  45. package/dist/digest.js +113 -0
  46. package/dist/durability.d.ts +4 -1
  47. package/dist/durability.d.ts.map +1 -1
  48. package/dist/durability.js +5 -0
  49. package/dist/entry.d.ts +20 -0
  50. package/dist/entry.d.ts.map +1 -0
  51. package/dist/entry.js +152 -0
  52. package/dist/expectation.d.ts +21 -0
  53. package/dist/expectation.d.ts.map +1 -0
  54. package/dist/expectation.js +70 -0
  55. package/dist/failure.d.ts +13 -0
  56. package/dist/failure.d.ts.map +1 -0
  57. package/dist/failure.js +19 -0
  58. package/dist/fence.d.ts +6 -0
  59. package/dist/fence.d.ts.map +1 -0
  60. package/dist/fence.js +10 -0
  61. package/dist/file.d.ts +2 -19
  62. package/dist/file.d.ts.map +1 -1
  63. package/dist/file.js +7 -122
  64. package/dist/host.d.ts +11 -2
  65. package/dist/host.d.ts.map +1 -1
  66. package/dist/host.js +128 -24
  67. package/dist/identity.d.ts +4 -0
  68. package/dist/identity.d.ts.map +1 -1
  69. package/dist/identity.js +16 -1
  70. package/dist/index.d.ts +22 -9
  71. package/dist/index.d.ts.map +1 -1
  72. package/dist/index.js +11 -0
  73. package/dist/job.d.ts +14 -0
  74. package/dist/job.d.ts.map +1 -0
  75. package/dist/job.js +28 -0
  76. package/dist/live.d.ts +23 -0
  77. package/dist/live.d.ts.map +1 -0
  78. package/dist/live.js +127 -0
  79. package/dist/media.d.ts +8 -0
  80. package/dist/media.d.ts.map +1 -0
  81. package/dist/media.js +218 -0
  82. package/dist/memory.d.ts +20 -7
  83. package/dist/memory.d.ts.map +1 -1
  84. package/dist/memory.js +253 -92
  85. package/dist/messages.d.ts +28 -0
  86. package/dist/messages.d.ts.map +1 -0
  87. package/dist/messages.js +89 -0
  88. package/dist/model.d.ts +19 -4
  89. package/dist/model.d.ts.map +1 -1
  90. package/dist/model.js +10 -2
  91. package/dist/name.d.ts +4 -0
  92. package/dist/name.d.ts.map +1 -0
  93. package/dist/name.js +122 -0
  94. package/dist/observation.d.ts +22 -4
  95. package/dist/observation.d.ts.map +1 -1
  96. package/dist/observation.js +120 -14
  97. package/dist/open.d.ts +2 -0
  98. package/dist/open.d.ts.map +1 -0
  99. package/dist/open.js +1 -0
  100. package/dist/opened.d.ts +22 -0
  101. package/dist/opened.d.ts.map +1 -0
  102. package/dist/opened.js +107 -0
  103. package/dist/outcome.d.ts +72 -5
  104. package/dist/outcome.d.ts.map +1 -1
  105. package/dist/outcome.js +81 -1
  106. package/dist/overlay.d.ts +15 -5
  107. package/dist/overlay.d.ts.map +1 -1
  108. package/dist/overlay.js +136 -180
  109. package/dist/paths.d.ts +1 -0
  110. package/dist/paths.d.ts.map +1 -1
  111. package/dist/paths.js +4 -0
  112. package/dist/phrase.d.ts +3 -0
  113. package/dist/phrase.d.ts.map +1 -0
  114. package/dist/phrase.js +8 -0
  115. package/dist/query.d.ts +75 -5
  116. package/dist/query.d.ts.map +1 -1
  117. package/dist/query.js +119 -26
  118. package/dist/reach.d.ts +18 -4
  119. package/dist/reach.d.ts.map +1 -1
  120. package/dist/reach.js +234 -69
  121. package/dist/react.d.ts +32 -19
  122. package/dist/react.d.ts.map +1 -1
  123. package/dist/react.js +13 -21
  124. package/dist/readiness.d.ts +50 -0
  125. package/dist/readiness.d.ts.map +1 -0
  126. package/dist/readiness.js +27 -0
  127. package/dist/recovery.d.ts +26 -0
  128. package/dist/recovery.d.ts.map +1 -0
  129. package/dist/recovery.js +224 -0
  130. package/dist/refresh.d.ts +20 -7
  131. package/dist/refresh.d.ts.map +1 -1
  132. package/dist/refresh.js +167 -59
  133. package/dist/replica.d.ts +59 -33
  134. package/dist/replica.d.ts.map +1 -1
  135. package/dist/replica.js +324 -157
  136. package/dist/room-draft.d.ts +59 -11
  137. package/dist/room-draft.d.ts.map +1 -1
  138. package/dist/room-draft.js +276 -80
  139. package/dist/rows.d.ts +40 -34
  140. package/dist/rows.d.ts.map +1 -1
  141. package/dist/rows.js +202 -80
  142. package/dist/save.d.ts +73 -4
  143. package/dist/save.d.ts.map +1 -1
  144. package/dist/save.js +825 -152
  145. package/dist/search.d.ts +15 -54
  146. package/dist/search.d.ts.map +1 -1
  147. package/dist/search.js +10 -121
  148. package/dist/seed.d.ts +16 -0
  149. package/dist/seed.d.ts.map +1 -0
  150. package/dist/seed.js +75 -0
  151. package/dist/shared.d.ts +15 -0
  152. package/dist/shared.d.ts.map +1 -0
  153. package/dist/shared.js +421 -0
  154. package/dist/status.d.ts +7 -2
  155. package/dist/status.d.ts.map +1 -1
  156. package/dist/status.js +3 -9
  157. package/dist/store.d.ts +118 -59
  158. package/dist/store.d.ts.map +1 -1
  159. package/dist/store.js +354 -344
  160. package/dist/stored.d.ts +162 -0
  161. package/dist/stored.d.ts.map +1 -0
  162. package/dist/stored.js +549 -0
  163. package/dist/tables.d.ts +353 -0
  164. package/dist/tables.d.ts.map +1 -0
  165. package/dist/tables.js +585 -0
  166. package/dist/transfer.d.ts +0 -5
  167. package/dist/transfer.d.ts.map +1 -1
  168. package/dist/turn.d.ts +9 -0
  169. package/dist/turn.d.ts.map +1 -0
  170. package/dist/turn.js +75 -0
  171. package/dist/validation.d.ts +29 -0
  172. package/dist/validation.d.ts.map +1 -0
  173. package/dist/validation.js +104 -0
  174. package/dist/windows.d.ts +22 -0
  175. package/dist/windows.d.ts.map +1 -0
  176. package/dist/windows.js +89 -0
  177. package/dist/wire.d.ts +170 -33
  178. package/dist/wire.d.ts.map +1 -1
  179. package/dist/wire.js +69 -31
  180. package/package.json +26 -15
  181. package/AGENTS.md +0 -232
  182. package/dist/attachment.d.ts +0 -3
  183. package/dist/attachment.d.ts.map +0 -1
  184. package/dist/attachment.js +0 -8
  185. package/dist/read-side.d.ts +0 -18
  186. package/dist/read-side.d.ts.map +0 -1
  187. package/dist/read-side.js +0 -377
package/dist/read-side.js DELETED
@@ -1,377 +0,0 @@
1
- import { and, BasicIndex, configureIndexDevMode, count, createCollection, createLiveQueryCollection, deepEquals, eq, gt, gte, inArray, isNull, isUndefined, lt, lte, not, or, toArray, } from '@tanstack/db';
2
- import { rowsOf } from './observation.js';
3
- import { documentKey } from './rows.js';
4
- function every(expressions) {
5
- const [first, second, ...rest] = expressions;
6
- return second === undefined ? first : and(first, second, ...rest);
7
- }
8
- function some(expressions) {
9
- const [first, second, ...rest] = expressions;
10
- return second === undefined ? first : or(first, second, ...rest);
11
- }
12
- function equal(field, value) {
13
- return or(eq(field, value), inArray(value, field));
14
- }
15
- function tested(test, field, size, written) {
16
- if (test.op === 'eq')
17
- return test.value === null ? isNull(field) : equal(field, test.value);
18
- if (test.op === 'ne') {
19
- return test.value === null
20
- ? not(isNull(field))
21
- : or(isUndefined(field), isNull(field), not(equal(field, test.value)));
22
- }
23
- if (test.op === 'in' || test.op === 'nin') {
24
- const matches = test.values.length === 0
25
- ? inArray(field, [])
26
- : some(test.values.map((value) => (value === null ? isNull(field) : equal(field, value))));
27
- if (test.op === 'in')
28
- return matches;
29
- return some([
30
- isUndefined(field),
31
- ...(test.values.includes(null) ? [] : [isNull(field)]),
32
- not(matches),
33
- ]);
34
- }
35
- if (test.op === 'range') {
36
- const compare = { gt, gte, lt, lte };
37
- return every(test.bounds.map(([operator, bound]) => compare[operator](field, bound)));
38
- }
39
- if (test.op === 'all') {
40
- return every([not(isUndefined(size)), ...test.values.map((value) => inArray(value, field))]);
41
- }
42
- if (test.op === 'size')
43
- return eq(size, test.size);
44
- return test.exists ? not(isUndefined(written)) : isUndefined(written);
45
- }
46
- function conditioned(condition, row) {
47
- if ('every' in condition)
48
- return every(condition.every.map((item) => conditioned(item, row)));
49
- if ('some' in condition)
50
- return some(condition.some.map((item) => conditioned(item, row)));
51
- return tested(condition.test, row.values[condition.field], row.sizes[condition.field], row.fields[condition.field]);
52
- }
53
- function reached(reach, room, document) {
54
- return reach === null
55
- ? []
56
- : [or(inArray(room, [...reach.rooms]), inArray(document, [...reach.documents]))];
57
- }
58
- function scoped(scope, row, rooms, reach) {
59
- return every([
60
- eq(row.kind, 'document'),
61
- ...(rooms === null ? [] : [inArray(row.room, [...rooms])]),
62
- ...reached(reach, row.room, row.key),
63
- ...(scope.types === null ? [] : [inArray(row.type, [...scope.types])]),
64
- ...(scope.pathPrefix === null ? [] : [inArray(scope.pathPrefix, row.directories)]),
65
- ...(scope.where === null ? [] : [conditioned(scope.where, row)]),
66
- ]);
67
- }
68
- function keyed(key, row) {
69
- return key.of === 'field' ? row.values[key.field] : row[key.of];
70
- }
71
- function live(query) {
72
- return createLiveQueryCollection({
73
- query: query,
74
- startSync: true,
75
- });
76
- }
77
- function related(q, rows, scope, field, kept, reach) {
78
- const matched = kept === 'source' ? 'target' : 'source';
79
- const from = q.from({ r: rows });
80
- return from
81
- .where(({ r }) => every([eq(r.kind, 'reference'), ...(field === null ? [] : [eq(r.field, field)])]))
82
- .innerJoin({ m: rows }, ({ r, m }) => eq(r[matched], m.key))
83
- .where(({ m }) => scoped(scope, m, null, reach))
84
- .groupBy(({ r }) => r[kept])
85
- .select(({ r }) => ({ key: r[kept] }));
86
- }
87
- function filtered(q, rows, plan) {
88
- const from = q.from({ d: rows });
89
- let query = from.where(({ d }) => scoped(plan.root, d, plan.rooms, plan.reach));
90
- if (plan.hop !== null) {
91
- const hop = related(q, rows, plan.hop, plan.hop.field, 'source', plan.reach);
92
- query = query.innerJoin({ h: hop }, ({ d, h }) => eq(d.key, h.key));
93
- }
94
- if (plan.backlinks !== null) {
95
- const backlinks = related(q, rows, plan.backlinks, plan.backlinks.field, 'target', plan.reach);
96
- query = query.innerJoin({ k: backlinks }, ({ d, k }) => eq(d.key, k.key));
97
- }
98
- return query;
99
- }
100
- function named(prefix, names, source) {
101
- return Object.fromEntries(names.map((name, index) => [`${prefix}${index}`, source[name]]));
102
- }
103
- function documents(q, rows, plan, shape) {
104
- let query = filtered(q, rows, plan);
105
- if (shape.sections !== null || shape.preamble || shape.markdown) {
106
- query = query.leftJoin({ b: rows }, ({ d, b }) => eq(d.body, b.key));
107
- }
108
- for (const { key, options } of shape.orderBy) {
109
- query = query.orderBy(({ d }) => keyed(key, d), options);
110
- }
111
- if (shape.orderBy.length === 0 && plan.limit !== null) {
112
- query = query.orderBy(({ d }) => d.key, { stringSort: 'lexical' });
113
- }
114
- if (plan.limit !== null)
115
- query = query.limit(plan.limit);
116
- return query.select(({ d, b }) => ({
117
- room: d.room,
118
- path: d.path,
119
- type: d.type,
120
- ...(shape.fields === 'all' ? { fields: d.fields } : {}),
121
- ...(Array.isArray(shape.fields) ? named('field', shape.fields, d.fields) : {}),
122
- ...(shape.sections === 'all' ? { sections: b.sections } : {}),
123
- ...(Array.isArray(shape.sections) ? named('section', shape.sections, b.sections) : {}),
124
- ...(shape.preamble ? { preamble: b.preamble } : {}),
125
- ...(shape.markdown ? { markdown: b.markdown } : {}),
126
- ...(shape.backlinks ? { backlinks: toArray(backlinked(q, rows, d, plan.reach)) } : {}),
127
- ...(shape.change ? { change: d.change } : {}),
128
- }));
129
- }
130
- function backlinked(q, rows, document, reach) {
131
- const from = q.from({ r: rows });
132
- return from
133
- .where(({ r }) => every([eq(r.target, document.key), ...reached(reach, r.room, r.source)]))
134
- .orderBy(({ r }) => r.path, { stringSort: 'lexical' })
135
- .orderBy(({ r }) => r.field, { stringSort: 'lexical' })
136
- .select(({ r }) => ({ path: r.path, type: r.type, field: r.field }));
137
- }
138
- function groups(q, stage, shape, limit) {
139
- const keys = shape.groupBy.map((_, index) => `key${index}`);
140
- const from = q.from({ m: stage });
141
- let query = from.groupBy(({ m }) => keys.map((key) => m[key]));
142
- for (const { by, direction } of shape.orderBy) {
143
- query = query.orderBy(({ m }) => (by === 'count' ? count(m.key) : m[keys[by]]), direction);
144
- }
145
- if (limit !== null && shape.orderBy.length > 0)
146
- query = query.limit(limit);
147
- return query.select(({ m }) => ({
148
- ...Object.fromEntries(keys.map((key) => [key, m[key]])),
149
- count: count(m.key),
150
- }));
151
- }
152
- function built(rows, plan) {
153
- const shape = plan.shape;
154
- if (shape.kind === 'documents') {
155
- const collection = live((q) => documents(q, rows, plan, shape));
156
- return { live: collection, dispose: () => void collection.cleanup() };
157
- }
158
- const stage = live((q) => filtered(q, rows, plan).select(({ d }) => ({
159
- key: d.key,
160
- ...Object.fromEntries(shape.groupBy.map((key, index) => [`key${index}`, keyed(key, d)])),
161
- })));
162
- const collection = live((q) => groups(q, stage, shape, plan.limit));
163
- return {
164
- live: collection,
165
- dispose: () => {
166
- void collection.cleanup();
167
- void stage.cleanup();
168
- },
169
- };
170
- }
171
- function present(entries) {
172
- return Object.fromEntries(entries.filter(([, value]) => value !== undefined));
173
- }
174
- function part(row, prefix, names, whole) {
175
- if (names === 'all')
176
- return whole;
177
- return present(names.map((name, index) => [name, row[`${prefix}${index}`]]));
178
- }
179
- function documentOf(row, shape) {
180
- if (row.type === null)
181
- return { room: row.room, path: row.path };
182
- return {
183
- room: row.room,
184
- path: row.path,
185
- type: row.type,
186
- ...(shape.fields === null
187
- ? {}
188
- : { fields: part(row, 'field', shape.fields, row.fields) }),
189
- ...(shape.sections === null
190
- ? {}
191
- : {
192
- sections: part(row, 'section', shape.sections, row.sections),
193
- }),
194
- ...(shape.preamble ? { preamble: row.preamble } : {}),
195
- ...(shape.markdown ? { markdown: row.markdown } : {}),
196
- ...(shape.backlinks
197
- ? {
198
- backlinks: row.backlinks.map(({ path, type, field }) => ({ path, type, field })),
199
- }
200
- : {}),
201
- ...(shape.change ? { change: row.change } : {}),
202
- };
203
- }
204
- function keyedOf(plan, entries) {
205
- const shape = plan.shape;
206
- if (shape.kind === 'documents') {
207
- return {
208
- result: structuredClone({ documents: entries.map(([, row]) => documentOf(row, shape)) }),
209
- keys: entries.map(([key]) => key),
210
- };
211
- }
212
- const bounded = shape.orderBy.length === 0 && plan.limit !== null ? entries.slice(0, plan.limit) : entries;
213
- return {
214
- result: structuredClone({
215
- groups: bounded.map(([, row]) => ({
216
- key: shape.groupBy.map((_, index) => row[`key${index}`]),
217
- count: row.count,
218
- })),
219
- }),
220
- keys: bounded.map(([key]) => key),
221
- };
222
- }
223
- export function reconciled(previous, next) {
224
- const before = rowsOf(previous.result);
225
- const held = new Map(previous.keys.map((key, index) => [key, before[index]]));
226
- const changed = [];
227
- const rows = rowsOf(next.result).map((row, index) => {
228
- const key = next.keys[index];
229
- const kept = held.get(key);
230
- if (kept !== undefined && deepEquals(kept, row))
231
- return kept;
232
- changed.push([key, row]);
233
- return row;
234
- });
235
- const present = new Set(next.keys);
236
- const survivors = previous.keys.filter((key) => present.has(key));
237
- const removed = previous.keys.filter((key) => !present.has(key));
238
- const moved = survivors.length !== next.keys.length ||
239
- survivors.some((key, index) => key !== next.keys[index]);
240
- if (changed.length === 0 && removed.length === 0 && !moved)
241
- return null;
242
- return {
243
- keyed: {
244
- result: 'groups' in next.result
245
- ? { groups: rows }
246
- : { documents: rows },
247
- keys: next.keys,
248
- },
249
- change: { changed, removed, order: moved ? next.keys : null },
250
- };
251
- }
252
- export function createReadSide() {
253
- configureIndexDevMode({ enabled: false });
254
- let sync;
255
- const rows = createCollection({
256
- getKey: (row) => row.key,
257
- gcTime: 0,
258
- startSync: true,
259
- autoIndex: 'eager',
260
- defaultIndexType: BasicIndex,
261
- sync: {
262
- rowUpdateMode: 'full',
263
- sync: (params) => {
264
- sync = params;
265
- params.markReady();
266
- },
267
- },
268
- });
269
- const shared = new Map();
270
- function searched(row) {
271
- const body = rows.get(row.body);
272
- return {
273
- room: row.room,
274
- path: row.path,
275
- type: row.type,
276
- fields: row.fields,
277
- preamble: body.preamble,
278
- sections: body.sections,
279
- };
280
- }
281
- function publish(entry) {
282
- const next = reconciled(entry.result, keyedOf(entry.plan, [...entry.built.live.entries()]));
283
- if (next === null)
284
- return;
285
- entry.result = next.keyed;
286
- for (const listener of entry.listeners)
287
- listener(next.change);
288
- }
289
- return {
290
- apply(set) {
291
- const next = new Map();
292
- for (const change of set.documents) {
293
- for (const row of change.before)
294
- next.set(row.key, null);
295
- for (const row of change.after)
296
- next.set(row.key, row);
297
- }
298
- if (next.size === 0)
299
- return;
300
- sync.begin();
301
- for (const [key, row] of next) {
302
- if (row === null) {
303
- if (rows.has(key))
304
- sync.write({ type: 'delete', key });
305
- continue;
306
- }
307
- sync.write({ type: rows.has(key) ? 'update' : 'insert', value: row });
308
- }
309
- sync.commit();
310
- },
311
- async query(plan) {
312
- const { live: collection, dispose } = built(rows, plan);
313
- try {
314
- await collection.toArrayWhenReady();
315
- return keyedOf(plan, [...collection.entries()]).result;
316
- }
317
- finally {
318
- dispose();
319
- }
320
- },
321
- watch(plan, identity, deliver) {
322
- let entry = shared.get(identity);
323
- if (entry === undefined) {
324
- const created = {
325
- plan,
326
- built: built(rows, plan),
327
- result: undefined,
328
- listeners: new Set(),
329
- subscription: null,
330
- };
331
- created.built.live.on('status:change', ({ status }) => {
332
- if (status === 'cleaned-up' && created.listeners.size === 0)
333
- shared.delete(identity);
334
- });
335
- shared.set(identity, created);
336
- entry = created;
337
- }
338
- const observed = entry;
339
- observed.listeners.add(deliver);
340
- observed.built.live.onFirstReady(() => {
341
- if (!observed.listeners.has(deliver))
342
- return;
343
- if (observed.subscription === null) {
344
- observed.result = keyedOf(observed.plan, [...observed.built.live.entries()]);
345
- observed.subscription = observed.built.live.subscribeChanges(() => {
346
- publish(observed);
347
- }, { includeInitialState: false });
348
- }
349
- deliver(observed.result);
350
- });
351
- return () => {
352
- if (!observed.listeners.delete(deliver) || observed.listeners.size > 0)
353
- return;
354
- observed.subscription?.unsubscribe();
355
- observed.subscription = null;
356
- };
357
- },
358
- documents() {
359
- return rows.toArray
360
- .filter((row) => row.kind === 'document' && row.type !== null)
361
- .map(searched);
362
- },
363
- document(room, path) {
364
- const row = rows.get(documentKey(room, path));
365
- return row === undefined || row.type === null ? null : searched(row);
366
- },
367
- close() {
368
- for (const entry of shared.values()) {
369
- entry.listeners.clear();
370
- entry.subscription?.unsubscribe();
371
- entry.built.dispose();
372
- }
373
- shared.clear();
374
- void rows.cleanup();
375
- },
376
- };
377
- }