@agentshouse/mdruntime 0.1.0-cede5a74284be → 0.1.0-cee9c8aa4cd86

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 (223) hide show
  1. package/AGENTS.md +294 -292
  2. package/README.md +9 -8
  3. package/dist/adapter.d.ts +91 -23
  4. package/dist/adapter.d.ts.map +1 -1
  5. package/dist/attached-model.d.ts +4 -1
  6. package/dist/attached-model.d.ts.map +1 -1
  7. package/dist/attached-model.js +13 -3
  8. package/dist/attempt.d.ts.map +1 -1
  9. package/dist/attempt.js +14 -1
  10. package/dist/bootstrap.d.ts +16 -8
  11. package/dist/bootstrap.d.ts.map +1 -1
  12. package/dist/bootstrap.js +102 -47
  13. package/dist/chain.d.ts +11 -0
  14. package/dist/chain.d.ts.map +1 -0
  15. package/dist/chain.js +64 -0
  16. package/dist/change-set.d.ts +9 -14
  17. package/dist/change-set.d.ts.map +1 -1
  18. package/dist/change-set.js +11 -63
  19. package/dist/change.d.ts +31 -1
  20. package/dist/change.d.ts.map +1 -1
  21. package/dist/change.js +10 -1
  22. package/dist/chat/acts.d.ts +3 -4
  23. package/dist/chat/acts.d.ts.map +1 -1
  24. package/dist/chat/acts.js +10 -10
  25. package/dist/chat/earlier.d.ts +10 -0
  26. package/dist/chat/earlier.d.ts.map +1 -0
  27. package/dist/chat/earlier.js +54 -0
  28. package/dist/chat/house.d.ts +25 -0
  29. package/dist/chat/house.d.ts.map +1 -0
  30. package/dist/chat/house.js +38 -0
  31. package/dist/chat/input.d.ts +3 -1
  32. package/dist/chat/input.d.ts.map +1 -1
  33. package/dist/chat/input.js +11 -18
  34. package/dist/chat/kit.d.ts +2 -1
  35. package/dist/chat/kit.d.ts.map +1 -1
  36. package/dist/chat/kit.js +3 -3
  37. package/dist/chat/list.d.ts +9 -1
  38. package/dist/chat/list.d.ts.map +1 -1
  39. package/dist/chat/list.js +53 -9
  40. package/dist/chat/markdown.d.ts +3 -1
  41. package/dist/chat/markdown.d.ts.map +1 -1
  42. package/dist/chat/markdown.js +85 -26
  43. package/dist/chat/message.d.ts +1 -0
  44. package/dist/chat/message.d.ts.map +1 -1
  45. package/dist/chat/message.js +75 -13
  46. package/dist/chat/messages.d.ts +15 -1
  47. package/dist/chat/messages.d.ts.map +1 -1
  48. package/dist/chat/messages.js +82 -23
  49. package/dist/chat/name.d.ts +1 -0
  50. package/dist/chat/name.d.ts.map +1 -1
  51. package/dist/chat/name.js +1 -1
  52. package/dist/chat/opened.d.ts +1 -0
  53. package/dist/chat/opened.d.ts.map +1 -1
  54. package/dist/chat/opened.js +2 -0
  55. package/dist/chat/overlay.d.ts +1 -0
  56. package/dist/chat/overlay.d.ts.map +1 -1
  57. package/dist/chat/overlay.js +11 -5
  58. package/dist/chat/parts.d.ts +1 -0
  59. package/dist/chat/parts.d.ts.map +1 -1
  60. package/dist/chat/parts.js +3 -2
  61. package/dist/chat/pieces.d.ts +62 -0
  62. package/dist/chat/pieces.d.ts.map +1 -0
  63. package/dist/chat/pieces.js +1110 -0
  64. package/dist/chat/provider.d.ts +9 -2
  65. package/dist/chat/provider.d.ts.map +1 -1
  66. package/dist/chat/provider.js +146 -156
  67. package/dist/chat/question.d.ts +1 -1
  68. package/dist/chat/question.d.ts.map +1 -1
  69. package/dist/chat/question.js +7 -18
  70. package/dist/chat/ready.d.ts +5 -1
  71. package/dist/chat/ready.d.ts.map +1 -1
  72. package/dist/chat/ready.js +9 -7
  73. package/dist/chat/room-file.d.ts +8 -0
  74. package/dist/chat/room-file.d.ts.map +1 -0
  75. package/dist/chat/room-file.js +72 -0
  76. package/dist/chat/steady.d.ts +3 -0
  77. package/dist/chat/steady.d.ts.map +1 -0
  78. package/dist/chat/steady.js +26 -0
  79. package/dist/chat/style.d.ts.map +1 -1
  80. package/dist/chat/style.js +9 -4
  81. package/dist/chat.d.ts +3 -2
  82. package/dist/chat.d.ts.map +1 -1
  83. package/dist/chat.js +2 -2
  84. package/dist/client.d.ts +22 -9
  85. package/dist/client.d.ts.map +1 -1
  86. package/dist/client.js +48 -8
  87. package/dist/collection.d.ts +7 -6
  88. package/dist/collection.d.ts.map +1 -1
  89. package/dist/collection.js +31 -77
  90. package/dist/compile.d.ts +16 -0
  91. package/dist/compile.d.ts.map +1 -0
  92. package/dist/compile.js +718 -0
  93. package/dist/connection.d.ts +41 -13
  94. package/dist/connection.d.ts.map +1 -1
  95. package/dist/connection.js +3120 -702
  96. package/dist/conversation.d.ts +28 -9
  97. package/dist/conversation.d.ts.map +1 -1
  98. package/dist/conversation.js +513 -178
  99. package/dist/digest.d.ts +7 -0
  100. package/dist/digest.d.ts.map +1 -0
  101. package/dist/digest.js +113 -0
  102. package/dist/durability.d.ts +8 -1
  103. package/dist/durability.d.ts.map +1 -1
  104. package/dist/entry.d.ts +19 -0
  105. package/dist/entry.d.ts.map +1 -0
  106. package/dist/entry.js +151 -0
  107. package/dist/fence.d.ts +6 -0
  108. package/dist/fence.d.ts.map +1 -0
  109. package/dist/fence.js +10 -0
  110. package/dist/file.d.ts +2 -10
  111. package/dist/file.d.ts.map +1 -1
  112. package/dist/file.js +3 -120
  113. package/dist/fulltext.d.ts +27 -0
  114. package/dist/fulltext.d.ts.map +1 -0
  115. package/dist/fulltext.js +368 -0
  116. package/dist/host.d.ts +14 -3
  117. package/dist/host.d.ts.map +1 -1
  118. package/dist/host.js +102 -13
  119. package/dist/identity.d.ts +1 -0
  120. package/dist/identity.d.ts.map +1 -1
  121. package/dist/identity.js +3 -0
  122. package/dist/index.d.ts +15 -9
  123. package/dist/index.d.ts.map +1 -1
  124. package/dist/index.js +5 -0
  125. package/dist/live.d.ts +22 -0
  126. package/dist/live.d.ts.map +1 -0
  127. package/dist/live.js +123 -0
  128. package/dist/memory.d.ts +9 -2
  129. package/dist/memory.d.ts.map +1 -1
  130. package/dist/memory.js +55 -12
  131. package/dist/messages.d.ts +28 -0
  132. package/dist/messages.d.ts.map +1 -0
  133. package/dist/messages.js +90 -0
  134. package/dist/model.d.ts +4 -2
  135. package/dist/model.d.ts.map +1 -1
  136. package/dist/model.js +10 -1
  137. package/dist/name.d.ts +4 -0
  138. package/dist/name.d.ts.map +1 -0
  139. package/dist/name.js +122 -0
  140. package/dist/observation.d.ts +19 -2
  141. package/dist/observation.d.ts.map +1 -1
  142. package/dist/observation.js +113 -12
  143. package/dist/opened.d.ts +22 -0
  144. package/dist/opened.d.ts.map +1 -0
  145. package/dist/opened.js +107 -0
  146. package/dist/outcome.d.ts +39 -4
  147. package/dist/outcome.d.ts.map +1 -1
  148. package/dist/outcome.js +29 -0
  149. package/dist/overlay.d.ts +3 -4
  150. package/dist/overlay.d.ts.map +1 -1
  151. package/dist/overlay.js +99 -181
  152. package/dist/query.d.ts +57 -4
  153. package/dist/query.d.ts.map +1 -1
  154. package/dist/query.js +37 -7
  155. package/dist/reach.d.ts +11 -4
  156. package/dist/reach.d.ts.map +1 -1
  157. package/dist/reach.js +182 -51
  158. package/dist/react.d.ts +8 -6
  159. package/dist/react.d.ts.map +1 -1
  160. package/dist/react.js +7 -17
  161. package/dist/readiness.d.ts +53 -0
  162. package/dist/readiness.d.ts.map +1 -0
  163. package/dist/readiness.js +33 -0
  164. package/dist/reads.d.ts +11 -0
  165. package/dist/reads.d.ts.map +1 -0
  166. package/dist/reads.js +98 -0
  167. package/dist/recovery.d.ts +25 -0
  168. package/dist/recovery.d.ts.map +1 -0
  169. package/dist/recovery.js +198 -0
  170. package/dist/refresh.d.ts +12 -15
  171. package/dist/refresh.d.ts.map +1 -1
  172. package/dist/refresh.js +74 -44
  173. package/dist/replica.d.ts +55 -33
  174. package/dist/replica.d.ts.map +1 -1
  175. package/dist/replica.js +295 -149
  176. package/dist/room-draft.d.ts +16 -2
  177. package/dist/room-draft.d.ts.map +1 -1
  178. package/dist/room-draft.js +32 -25
  179. package/dist/rows.d.ts +28 -37
  180. package/dist/rows.d.ts.map +1 -1
  181. package/dist/rows.js +113 -72
  182. package/dist/save.d.ts +50 -9
  183. package/dist/save.d.ts.map +1 -1
  184. package/dist/save.js +528 -166
  185. package/dist/search.d.ts +39 -28
  186. package/dist/search.d.ts.map +1 -1
  187. package/dist/search.js +37 -105
  188. package/dist/seed.d.ts +16 -0
  189. package/dist/seed.d.ts.map +1 -0
  190. package/dist/seed.js +75 -0
  191. package/dist/shared.d.ts +16 -0
  192. package/dist/shared.d.ts.map +1 -0
  193. package/dist/shared.js +431 -0
  194. package/dist/status.d.ts +4 -0
  195. package/dist/status.d.ts.map +1 -1
  196. package/dist/store.d.ts +108 -75
  197. package/dist/store.d.ts.map +1 -1
  198. package/dist/store.js +345 -450
  199. package/dist/stored.d.ts +138 -0
  200. package/dist/stored.d.ts.map +1 -0
  201. package/dist/stored.js +544 -0
  202. package/dist/tables.d.ts +332 -0
  203. package/dist/tables.d.ts.map +1 -0
  204. package/dist/tables.js +570 -0
  205. package/dist/turn.d.ts +9 -0
  206. package/dist/turn.d.ts.map +1 -0
  207. package/dist/turn.js +75 -0
  208. package/dist/validation.d.ts +30 -0
  209. package/dist/validation.d.ts.map +1 -0
  210. package/dist/validation.js +162 -0
  211. package/dist/windows.d.ts +22 -0
  212. package/dist/windows.d.ts.map +1 -0
  213. package/dist/windows.js +89 -0
  214. package/dist/wire.d.ts +168 -32
  215. package/dist/wire.d.ts.map +1 -1
  216. package/dist/wire.js +56 -31
  217. package/dist/words.d.ts +19 -0
  218. package/dist/words.d.ts.map +1 -0
  219. package/dist/words.js +150 -0
  220. package/package.json +14 -4
  221. package/dist/read-side.d.ts +0 -18
  222. package/dist/read-side.d.ts.map +0 -1
  223. package/dist/read-side.js +0 -440
package/dist/words.js ADDED
@@ -0,0 +1,150 @@
1
+ const SPACELESS = /(?:[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Thai}\p{Script=Lao}\p{Script=Khmer}\p{Script=Myanmar}\p{Script=Hangul}]\p{M}*)+/gu;
2
+ const SPACELESS_TERM = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Thai}\p{Script=Lao}\p{Script=Khmer}\p{Script=Myanmar}\p{Script=Hangul}]/u;
3
+ const TOKEN = /[\p{L}\p{N}\p{M}\p{Co}]+/gu;
4
+ const MARKED = /([\p{Script=Latin}\p{Script=Greek}\p{Script=Cyrillic}])\p{M}+/gu;
5
+ const INFLECTED = /^[\p{Script=Cyrillic}\p{Script=Greek}]+$/u;
6
+ const FOLD = /\p{M}+/gu;
7
+ const ASCII = /^[\x00-\x7f]$/;
8
+ const PREFIXED_LETTERS = 2;
9
+ const INFLECTED_LETTERS = 4;
10
+ const PHRASE_TERMS = { least: 2, most: 6 };
11
+ const GRAPHEMES = new Intl.Segmenter(undefined, { granularity: 'grapheme' });
12
+ function bigrams(run) {
13
+ const characters = Array.from(run);
14
+ if (characters.length < 2)
15
+ return ` ${run} `;
16
+ const pairs = [];
17
+ for (let index = 0; index + 1 < characters.length; index += 1) {
18
+ pairs.push(characters[index] + characters[index + 1]);
19
+ }
20
+ return ` ${pairs.join(' ')} `;
21
+ }
22
+ export function indexed(text) {
23
+ return text.normalize('NFKD').replace(MARKED, '$1').normalize('NFKC').replace(SPACELESS, bigrams);
24
+ }
25
+ function tokens(text) {
26
+ return [...text.matchAll(TOKEN)].map(([token]) => token.toLowerCase());
27
+ }
28
+ function quoted(token) {
29
+ return `"${token.replaceAll('"', '""')}"`;
30
+ }
31
+ function truncated(term) {
32
+ const letters = Array.from(term);
33
+ if (!INFLECTED.test(term) || letters.length < INFLECTED_LETTERS)
34
+ return null;
35
+ return letters.slice(0, letters.length - (letters.length >= 6 ? 2 : 1)).join('');
36
+ }
37
+ export function matchClauses(text) {
38
+ const terms = tokens(indexed(text));
39
+ if (terms.length === 0)
40
+ return null;
41
+ const unique = [...new Set(terms)];
42
+ const clauses = unique.map((term) => ({ text: quoted(term), kind: 'term', term }));
43
+ for (const term of unique) {
44
+ const stem = truncated(term);
45
+ if (stem !== null)
46
+ clauses.push({ text: `${quoted(stem)}*`, kind: 'stem', term });
47
+ }
48
+ const last = terms[terms.length - 1];
49
+ if (!/\s$/u.test(text) && Array.from(last).length >= PREFIXED_LETTERS) {
50
+ clauses.push({ text: `${quoted(last)}*`, kind: 'prefix', term: last });
51
+ }
52
+ if (terms.length >= PHRASE_TERMS.least && terms.length <= PHRASE_TERMS.most) {
53
+ clauses.push({ text: quoted(terms.join(' ')), kind: 'phrase', term: terms.join(' ') });
54
+ }
55
+ return clauses;
56
+ }
57
+ export function matchExpression(clauses) {
58
+ return clauses.map(({ text }) => text).join(' OR ');
59
+ }
60
+ function folded(text) {
61
+ return text.normalize('NFKD').replace(FOLD, '').toLowerCase();
62
+ }
63
+ export function queryTerms(text) {
64
+ return [...new Set(tokens(text.normalize('NFKC')).map(folded))];
65
+ }
66
+ function agrees(token, term) {
67
+ if (SPACELESS_TERM.test(term))
68
+ return token.includes(term);
69
+ if (token.startsWith(term))
70
+ return true;
71
+ return token.length >= 3 && term.startsWith(token.slice(0, Math.max(3, token.length - 2)));
72
+ }
73
+ function inside(token, offset, term) {
74
+ const pieces = [...GRAPHEMES.segment(token)];
75
+ const owner = [];
76
+ let joined = '';
77
+ for (const [at, { segment }] of pieces.entries()) {
78
+ const part = folded(segment);
79
+ joined += part;
80
+ for (let index = 0; index < part.length; index += 1)
81
+ owner.push(at);
82
+ }
83
+ const found = joined.indexOf(term);
84
+ if (found < 0)
85
+ return null;
86
+ const first = pieces[owner[found]];
87
+ const last = pieces[owner[found + term.length - 1]];
88
+ return { start: offset + first.index, end: offset + last.index + last.segment.length };
89
+ }
90
+ function locate(part, terms) {
91
+ for (const match of part.matchAll(TOKEN)) {
92
+ const token = folded(match[0]);
93
+ for (const term of terms) {
94
+ if (!agrees(token, term))
95
+ continue;
96
+ if (!SPACELESS_TERM.test(term)) {
97
+ return { start: match.index, end: match.index + match[0].length };
98
+ }
99
+ const found = inside(match[0], match.index, term);
100
+ if (found !== null)
101
+ return found;
102
+ }
103
+ }
104
+ return null;
105
+ }
106
+ function settled(part, index) {
107
+ if (index <= 0 || index >= part.length)
108
+ return true;
109
+ const before = part[index - 1];
110
+ const after = part[index];
111
+ if (before === '\n')
112
+ return true;
113
+ return ASCII.test(before) && ASCII.test(after) && !(before === '\r' && after === '\n');
114
+ }
115
+ function edgesAround(part, low, high) {
116
+ let from = Math.max(0, low);
117
+ while (!settled(part, from))
118
+ from -= 1;
119
+ let to = Math.min(part.length, high);
120
+ while (!settled(part, to))
121
+ to += 1;
122
+ const edges = [...GRAPHEMES.segment(part.slice(from, to))].map(({ index }) => from + index);
123
+ edges.push(to);
124
+ return edges;
125
+ }
126
+ export function snippet(part, terms, reach) {
127
+ const located = locate(part, terms);
128
+ if (located === null)
129
+ return null;
130
+ const edges = edgesAround(part, located.start - 2 * reach, located.end + 2 * reach);
131
+ const last = (limit) => edges.filter((edge) => edge <= limit).at(-1) ?? edges[0];
132
+ const first = (limit) => edges.find((edge) => edge >= limit) ?? edges[edges.length - 1];
133
+ const from = last(located.start);
134
+ const to = Math.max(first(Math.min(located.end, from + reach)), first(from + 1));
135
+ const start = first(Math.max(0, from - reach));
136
+ const end = last(Math.min(part.length, to + reach));
137
+ return { text: part.slice(start, end), at: [from - start, to - start] };
138
+ }
139
+ export function matched(part, terms) {
140
+ const found = new Set();
141
+ for (const match of part.matchAll(TOKEN)) {
142
+ const token = folded(match[0]);
143
+ for (const term of terms)
144
+ if (agrees(token, term))
145
+ found.add(term);
146
+ if (found.size === terms.length)
147
+ break;
148
+ }
149
+ return [...found];
150
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentshouse/mdruntime",
3
- "version": "0.1.0-cede5a74284be",
3
+ "version": "0.1.0-cee9c8aa4cd86",
4
4
  "description": "The headless core of MD App",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -24,10 +24,18 @@
24
24
  "types": "./dist/client.d.ts",
25
25
  "default": "./dist/client.js"
26
26
  },
27
+ "./entry": {
28
+ "types": "./dist/entry.d.ts",
29
+ "default": "./dist/entry.js"
30
+ },
27
31
  "./memory": {
28
32
  "types": "./dist/memory.d.ts",
29
33
  "default": "./dist/memory.js"
30
34
  },
35
+ "./tables": {
36
+ "types": "./dist/tables.d.ts",
37
+ "default": "./dist/tables.js"
38
+ },
31
39
  "./react": {
32
40
  "types": "./dist/react.d.ts",
33
41
  "default": "./dist/react.js"
@@ -36,6 +44,10 @@
36
44
  "types": "./dist/chat.d.ts",
37
45
  "default": "./dist/chat.js"
38
46
  },
47
+ "./digest": {
48
+ "types": "./dist/digest.d.ts",
49
+ "default": "./dist/digest.js"
50
+ },
39
51
  "./package.json": "./package.json"
40
52
  },
41
53
  "files": [
@@ -45,14 +57,12 @@
45
57
  "LICENSE"
46
58
  ],
47
59
  "dependencies": {
48
- "@agentshouse/mdmodel": "0.1.0-c64451b2127b4",
60
+ "@agentshouse/mdmodel": "0.1.0-c679f403bf3fb",
49
61
  "@assistant-ui/react-markdown": "0.14.20",
50
- "@tanstack/db": "0.9.2",
51
62
  "lucide-react": "1.47.0",
52
63
  "mdast-util-from-markdown": "2.0.3",
53
64
  "mdast-util-gfm": "3.1.0",
54
65
  "micromark-extension-gfm": "3.0.0",
55
- "minisearch": "7.2.0",
56
66
  "radix-ui": "1.8.0",
57
67
  "remark-gfm": "4.0.1"
58
68
  },
@@ -1,18 +0,0 @@
1
- import type { ChangeSet } from './change-set.js';
2
- import { type KeyedResult, type QueryChange } from './observation.js';
3
- import type { Plan, QueryResult } from './query.js';
4
- import type { SearchedDocument } from './search.js';
5
- export type ReadSide = {
6
- apply(set: ChangeSet): void;
7
- query(plan: Plan): Promise<QueryResult>;
8
- watch(plan: Plan, identity: string, deliver: (message: KeyedResult | QueryChange) => void): () => void;
9
- documents(): readonly SearchedDocument[];
10
- document(room: string, path: string): SearchedDocument | null;
11
- close(): void;
12
- };
13
- export declare function reconciled(previous: KeyedResult, next: KeyedResult): {
14
- keyed: KeyedResult;
15
- change: QueryChange;
16
- } | null;
17
- export declare function createReadSide(): ReadSide;
18
- //# sourceMappingURL=read-side.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"read-side.d.ts","sourceRoot":"","sources":["../src/read-side.ts"],"names":[],"mappings":"AAsBA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAU,KAAK,WAAW,EAAE,KAAK,WAAW,EAAe,MAAM,kBAAkB,CAAC;AAC3F,OAAO,KAAK,EAOV,IAAI,EAIJ,WAAW,EAIZ,MAAM,YAAY,CAAC;AAEpB,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAuDpD,MAAM,MAAM,QAAQ,GAAG;IACrB,KAAK,CAAC,GAAG,EAAE,SAAS,GAAG,IAAI,CAAC;IAC5B,KAAK,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;IACxC,KAAK,CACH,IAAI,EAAE,IAAI,EACV,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,CAAC,OAAO,EAAE,WAAW,GAAG,WAAW,KAAK,IAAI,GACpD,MAAM,IAAI,CAAC;IACd,SAAS,IAAI,SAAS,gBAAgB,EAAE,CAAC;IACzC,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,gBAAgB,GAAG,IAAI,CAAC;IAC9D,KAAK,IAAI,IAAI,CAAC;CACf,CAAC;AAuVF,wBAAgB,UAAU,CACxB,QAAQ,EAAE,WAAW,EACrB,IAAI,EAAE,WAAW,GAChB;IAAE,KAAK,EAAE,WAAW,CAAC;IAAC,MAAM,EAAE,WAAW,CAAA;CAAE,GAAG,IAAI,CA4BpD;AAED,wBAAgB,cAAc,IAAI,QAAQ,CA4HzC"}
package/dist/read-side.js DELETED
@@ -1,440 +0,0 @@
1
- import { and, BasicIndex, configureIndexDevMode, count, createCollection, createLiveQueryCollection, deepEquals, eq, inArray, or, toArray, } from '@tanstack/db';
2
- import { ARRAY_INDEX, pointerSegments } from '@agentshouse/mdmodel';
3
- import { rowsOf } from './observation.js';
4
- import { documentKey } from './rows.js';
5
- function every(expressions) {
6
- const [first, second, ...rest] = expressions;
7
- return second === undefined ? first : and(first, second, ...rest);
8
- }
9
- function same(held, value) {
10
- return Array.isArray(held) ? held.includes(value) : held === value;
11
- }
12
- function ordered(held, [operator, bound]) {
13
- if (typeof held !== typeof bound || held === null || typeof held === 'object')
14
- return false;
15
- const left = held;
16
- const right = bound;
17
- if (operator === 'gt')
18
- return left > right;
19
- if (operator === 'gte')
20
- return left >= right;
21
- if (operator === 'lt')
22
- return left < right;
23
- return left <= right;
24
- }
25
- function satisfied(test, held) {
26
- if (test.op === 'eq')
27
- return same(held, test.value);
28
- if (test.op === 'ne')
29
- return !same(held, test.value);
30
- if (test.op === 'in')
31
- return test.values.some((value) => same(held, value));
32
- if (test.op === 'nin')
33
- return !test.values.some((value) => same(held, value));
34
- if (test.op === 'range')
35
- return test.bounds.every((bound) => ordered(held, bound));
36
- if (test.op === 'all')
37
- return Array.isArray(held) && test.values.every((value) => same(held, value));
38
- return Array.isArray(held) && held.length === test.size;
39
- }
40
- function tested(test, reached) {
41
- if (test.op === 'exists')
42
- return test.exists === (reached !== undefined);
43
- if (reached === undefined)
44
- return test.op === 'ne' || test.op === 'nin';
45
- return reached.some((held) => satisfied(test, held));
46
- }
47
- function conditioned(condition, row) {
48
- if ('every' in condition)
49
- return condition.every.every((item) => conditioned(item, row));
50
- if ('some' in condition)
51
- return condition.some.some((item) => conditioned(item, row));
52
- return tested(condition.test, row.reached[condition.field]);
53
- }
54
- function narrowed(query, where, alias) {
55
- return where === null ? query : query.fn.where((row) => conditioned(where, row[alias]));
56
- }
57
- function reached(reach, room, document) {
58
- return reach === null
59
- ? []
60
- : [or(inArray(room, [...reach.rooms]), inArray(document, [...reach.documents]))];
61
- }
62
- function scoped(scope, row, rooms, reach) {
63
- return every([
64
- eq(row.kind, 'document'),
65
- ...(rooms === null ? [] : [inArray(row.room, [...rooms])]),
66
- ...reached(reach, row.room, row.key),
67
- ...(scope.types === null ? [] : [inArray(row.type, [...scope.types])]),
68
- ...(scope.pathPrefix === null ? [] : [inArray(scope.pathPrefix, row.directories)]),
69
- ]);
70
- }
71
- function keyed(key, row) {
72
- return key.of === 'field' ? row.values[key.field] : row[key.of];
73
- }
74
- function live(query) {
75
- return createLiveQueryCollection({
76
- query: query,
77
- startSync: true,
78
- });
79
- }
80
- function related(q, rows, scope, field, kept, reach) {
81
- const matched = kept === 'source' ? 'target' : 'source';
82
- const from = q.from({ r: rows });
83
- const joined = from
84
- .where(({ r }) => every([eq(r.kind, 'reference'), ...(field === null ? [] : [inArray(field, r.forms)])]))
85
- .innerJoin({ m: rows }, ({ r, m }) => eq(r[matched], m.key))
86
- .where(({ m }) => scoped(scope, m, null, reach));
87
- return narrowed(joined, scope.where, 'm')
88
- .groupBy(({ r }) => r[kept])
89
- .select(({ r }) => ({ key: r[kept] }));
90
- }
91
- function filtered(q, rows, plan) {
92
- const from = q.from({ d: rows });
93
- let query = narrowed(from.where(({ d }) => scoped(plan.root, d, plan.rooms, plan.reach)), plan.root.where, 'd');
94
- if (plan.hop !== null) {
95
- const hop = related(q, rows, plan.hop, plan.hop.field, 'source', plan.reach);
96
- query = query.innerJoin({ h: hop }, ({ d, h }) => eq(d.key, h.key));
97
- }
98
- if (plan.backlinks !== null) {
99
- const backlinks = related(q, rows, plan.backlinks, plan.backlinks.field, 'target', plan.reach);
100
- query = query.innerJoin({ k: backlinks }, ({ d, k }) => eq(d.key, k.key));
101
- }
102
- return query;
103
- }
104
- function named(prefix, names, source) {
105
- return Object.fromEntries(names.map((name, index) => [`${prefix}${index}`, source[name]]));
106
- }
107
- function documents(q, rows, plan, shape) {
108
- let query = filtered(q, rows, plan);
109
- if (shape.sections !== null || shape.preamble || shape.markdown) {
110
- query = query.leftJoin({ b: rows }, ({ d, b }) => eq(d.body, b.key));
111
- }
112
- for (const { key, options } of shape.orderBy) {
113
- query = query.orderBy(({ d }) => keyed(key, d), options);
114
- }
115
- if (shape.orderBy.length === 0 && plan.limit !== null) {
116
- query = query.orderBy(({ d }) => d.key, { stringSort: 'lexical' });
117
- }
118
- if (plan.limit !== null)
119
- query = query.limit(plan.limit);
120
- return query.select(({ d, b }) => ({
121
- room: d.room,
122
- path: d.path,
123
- type: d.type,
124
- binary: d.binary,
125
- ...(shape.fields === null ? {} : { fields: d.fields }),
126
- ...(shape.sections === 'all' ? { sections: b.sections } : {}),
127
- ...(Array.isArray(shape.sections) ? named('section', shape.sections, b.sections) : {}),
128
- ...(shape.preamble ? { preamble: b.preamble } : {}),
129
- ...(shape.markdown ? { markdown: b.markdown } : {}),
130
- ...(shape.backlinks ? { backlinks: toArray(backlinked(q, rows, d, plan.reach)) } : {}),
131
- ...(shape.change ? { change: d.change } : {}),
132
- }));
133
- }
134
- function backlinked(q, rows, document, reach) {
135
- const from = q.from({ r: rows });
136
- return from
137
- .where(({ r }) => every([eq(r.target, document.key), ...reached(reach, r.room, r.source)]))
138
- .orderBy(({ r }) => r.path, { stringSort: 'lexical' })
139
- .orderBy(({ r }) => r.field, { stringSort: 'lexical' })
140
- .select(({ r }) => ({ path: r.path, type: r.type, field: r.field }));
141
- }
142
- function groups(q, stage, shape, limit) {
143
- const keys = shape.groupBy.map((_, index) => `key${index}`);
144
- const from = q.from({ m: stage });
145
- let query = from.groupBy(({ m }) => keys.map((key) => m[key]));
146
- for (const { by, direction } of shape.orderBy) {
147
- query = query.orderBy(({ m }) => (by === 'count' ? count(m.key) : m[keys[by]]), direction);
148
- }
149
- if (limit !== null && shape.orderBy.length > 0)
150
- query = query.limit(limit);
151
- return query.select(({ m }) => ({
152
- ...Object.fromEntries(keys.map((key) => [key, m[key]])),
153
- count: count(m.key),
154
- }));
155
- }
156
- function built(rows, plan) {
157
- const shape = plan.shape;
158
- if (shape.kind === 'documents') {
159
- const collection = live((q) => documents(q, rows, plan, shape));
160
- return { live: collection, dispose: () => void collection.cleanup() };
161
- }
162
- const stage = live((q) => filtered(q, rows, plan).select(({ d }) => ({
163
- key: d.key,
164
- ...Object.fromEntries(shape.groupBy.map((key, index) => [`key${index}`, keyed(key, d)])),
165
- })));
166
- const collection = live((q) => groups(q, stage, shape, plan.limit));
167
- return {
168
- live: collection,
169
- dispose: () => {
170
- void collection.cleanup();
171
- void stage.cleanup();
172
- },
173
- };
174
- }
175
- function present(entries) {
176
- return Object.fromEntries(entries.filter(([, value]) => value !== undefined));
177
- }
178
- function part(row, prefix, names, whole) {
179
- if (names === 'all')
180
- return whole;
181
- return present(names.map((name, index) => [name, row[`${prefix}${index}`]]));
182
- }
183
- function masked(pointers) {
184
- const root = { whole: false, members: new Map() };
185
- for (const pointer of pointers) {
186
- let mask = root;
187
- for (const token of pointerSegments(pointer)) {
188
- const next = mask.members.get(token) ?? { whole: false, members: new Map() };
189
- mask.members.set(token, next);
190
- mask = next;
191
- }
192
- mask.whole = true;
193
- }
194
- return root;
195
- }
196
- function merged(left, right) {
197
- if (left === undefined)
198
- return right;
199
- const members = new Map(left.members);
200
- for (const [token, mask] of right.members)
201
- members.set(token, merged(members.get(token), mask));
202
- return { whole: left.whole || right.whole, members };
203
- }
204
- function cut(value, mask) {
205
- if (mask.whole)
206
- return value;
207
- if (Array.isArray(value)) {
208
- const each = {
209
- whole: false,
210
- members: new Map([...mask.members].filter(([token]) => !ARRAY_INDEX.test(token))),
211
- };
212
- const kept = [];
213
- value.forEach((item, index) => {
214
- const part = cut(item, merged(mask.members.get(String(index)), each));
215
- if (part !== undefined)
216
- kept[index] = part;
217
- });
218
- return kept.length === 0 ? undefined : kept;
219
- }
220
- if (typeof value !== 'object' || value === null)
221
- return undefined;
222
- const kept = Object.entries(value).flatMap(([key, held]) => {
223
- const member = mask.members.get(key);
224
- const part = member === undefined ? undefined : cut(held, member);
225
- return part === undefined ? [] : [[key, part]];
226
- });
227
- return kept.length === 0 ? undefined : Object.fromEntries(kept);
228
- }
229
- function selected(fields, pointers) {
230
- return (cut(fields, masked(pointers)) ?? {});
231
- }
232
- function documentOf(row, shape) {
233
- if (row.type === null) {
234
- const binary = row.binary;
235
- return {
236
- room: row.room,
237
- path: row.path,
238
- ...(binary === null ? {} : { binaryFile: binary }),
239
- };
240
- }
241
- return {
242
- room: row.room,
243
- path: row.path,
244
- type: row.type,
245
- ...(shape.fields === null
246
- ? {}
247
- : {
248
- fields: shape.fields === 'all'
249
- ? row.fields
250
- : selected(row.fields, shape.fields),
251
- }),
252
- ...(shape.sections === null
253
- ? {}
254
- : {
255
- sections: part(row, 'section', shape.sections, row.sections),
256
- }),
257
- ...(shape.preamble ? { preamble: row.preamble } : {}),
258
- ...(shape.markdown ? { markdown: row.markdown } : {}),
259
- ...(shape.backlinks
260
- ? {
261
- backlinks: row.backlinks.map(({ path, type, field }) => ({ path, type, field })),
262
- }
263
- : {}),
264
- ...(shape.change ? { change: row.change } : {}),
265
- };
266
- }
267
- function keyedOf(plan, entries) {
268
- const shape = plan.shape;
269
- if (shape.kind === 'documents') {
270
- return {
271
- result: structuredClone({ documents: entries.map(([, row]) => documentOf(row, shape)) }),
272
- keys: entries.map(([key]) => key),
273
- };
274
- }
275
- const bounded = shape.orderBy.length === 0 && plan.limit !== null ? entries.slice(0, plan.limit) : entries;
276
- return {
277
- result: structuredClone({
278
- groups: bounded.map(([, row]) => ({
279
- key: shape.groupBy.map((_, index) => row[`key${index}`]),
280
- count: row.count,
281
- })),
282
- }),
283
- keys: bounded.map(([key]) => key),
284
- };
285
- }
286
- export function reconciled(previous, next) {
287
- const before = rowsOf(previous.result);
288
- const held = new Map(previous.keys.map((key, index) => [key, before[index]]));
289
- const changed = [];
290
- const rows = rowsOf(next.result).map((row, index) => {
291
- const key = next.keys[index];
292
- const kept = held.get(key);
293
- if (kept !== undefined && deepEquals(kept, row))
294
- return kept;
295
- changed.push([key, row]);
296
- return row;
297
- });
298
- const present = new Set(next.keys);
299
- const survivors = previous.keys.filter((key) => present.has(key));
300
- const removed = previous.keys.filter((key) => !present.has(key));
301
- const moved = survivors.length !== next.keys.length ||
302
- survivors.some((key, index) => key !== next.keys[index]);
303
- if (changed.length === 0 && removed.length === 0 && !moved)
304
- return null;
305
- return {
306
- keyed: {
307
- result: 'groups' in next.result
308
- ? { groups: rows }
309
- : { documents: rows },
310
- keys: next.keys,
311
- },
312
- change: { changed, removed, order: moved ? next.keys : null },
313
- };
314
- }
315
- export function createReadSide() {
316
- configureIndexDevMode({ enabled: false });
317
- let sync;
318
- const rows = createCollection({
319
- getKey: (row) => row.key,
320
- gcTime: 0,
321
- startSync: true,
322
- autoIndex: 'eager',
323
- defaultIndexType: BasicIndex,
324
- sync: {
325
- rowUpdateMode: 'full',
326
- sync: (params) => {
327
- sync = params;
328
- params.markReady();
329
- },
330
- },
331
- });
332
- const shared = new Map();
333
- function searched(row) {
334
- const body = rows.get(row.body);
335
- return {
336
- room: row.room,
337
- path: row.path,
338
- type: row.type,
339
- fields: row.fields,
340
- preamble: body.preamble,
341
- sections: body.sections,
342
- };
343
- }
344
- function publish(entry) {
345
- const next = reconciled(entry.result, keyedOf(entry.plan, [...entry.built.live.entries()]));
346
- if (next === null)
347
- return;
348
- entry.result = next.keyed;
349
- for (const listener of entry.listeners)
350
- listener(next.change);
351
- }
352
- return {
353
- apply(set) {
354
- const next = new Map();
355
- for (const change of set.documents) {
356
- for (const row of change.before)
357
- next.set(row.key, null);
358
- for (const row of change.after)
359
- next.set(row.key, row);
360
- }
361
- if (next.size === 0)
362
- return;
363
- sync.begin();
364
- for (const [key, row] of next) {
365
- if (row === null) {
366
- if (rows.has(key))
367
- sync.write({ type: 'delete', key });
368
- continue;
369
- }
370
- sync.write({ type: rows.has(key) ? 'update' : 'insert', value: row });
371
- }
372
- sync.commit();
373
- },
374
- async query(plan) {
375
- const { live: collection, dispose } = built(rows, plan);
376
- try {
377
- await collection.toArrayWhenReady();
378
- return keyedOf(plan, [...collection.entries()]).result;
379
- }
380
- finally {
381
- dispose();
382
- }
383
- },
384
- watch(plan, identity, deliver) {
385
- let entry = shared.get(identity);
386
- if (entry === undefined) {
387
- const created = {
388
- plan,
389
- built: built(rows, plan),
390
- result: undefined,
391
- listeners: new Set(),
392
- subscription: null,
393
- };
394
- created.built.live.on('status:change', ({ status }) => {
395
- if (status === 'cleaned-up' && created.listeners.size === 0)
396
- shared.delete(identity);
397
- });
398
- shared.set(identity, created);
399
- entry = created;
400
- }
401
- const observed = entry;
402
- observed.listeners.add(deliver);
403
- observed.built.live.onFirstReady(() => {
404
- if (!observed.listeners.has(deliver))
405
- return;
406
- if (observed.subscription === null) {
407
- observed.result = keyedOf(observed.plan, [...observed.built.live.entries()]);
408
- observed.subscription = observed.built.live.subscribeChanges(() => {
409
- publish(observed);
410
- }, { includeInitialState: false });
411
- }
412
- deliver(observed.result);
413
- });
414
- return () => {
415
- if (!observed.listeners.delete(deliver) || observed.listeners.size > 0)
416
- return;
417
- observed.subscription?.unsubscribe();
418
- observed.subscription = null;
419
- };
420
- },
421
- documents() {
422
- return rows.toArray
423
- .filter((row) => row.kind === 'document' && row.type !== null)
424
- .map(searched);
425
- },
426
- document(room, path) {
427
- const row = rows.get(documentKey(room, path));
428
- return row === undefined || row.type === null ? null : searched(row);
429
- },
430
- close() {
431
- for (const entry of shared.values()) {
432
- entry.listeners.clear();
433
- entry.subscription?.unsubscribe();
434
- entry.built.dispose();
435
- }
436
- shared.clear();
437
- void rows.cleanup();
438
- },
439
- };
440
- }