@agentshouse/mdruntime 0.1.0-cee9c8aa4cd86 → 0.1.0-cef76ea952695

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 (376) hide show
  1. package/README.md +19 -17
  2. package/dist/acceptance.d.ts +6 -0
  3. package/dist/acceptance.d.ts.map +1 -0
  4. package/dist/acceptance.js +385 -0
  5. package/dist/actions.d.ts +20 -0
  6. package/dist/actions.d.ts.map +1 -0
  7. package/dist/actions.js +251 -0
  8. package/dist/adapter.d.ts +111 -109
  9. package/dist/adapter.d.ts.map +1 -1
  10. package/dist/attached-model.d.ts +5 -11
  11. package/dist/attached-model.d.ts.map +1 -1
  12. package/dist/attached-model.js +36 -47
  13. package/dist/binary.d.ts +0 -5
  14. package/dist/binary.d.ts.map +1 -1
  15. package/dist/binary.js +1 -1
  16. package/dist/bootstrap.d.ts +22 -10
  17. package/dist/bootstrap.d.ts.map +1 -1
  18. package/dist/bootstrap.js +303 -123
  19. package/dist/cell.d.ts +6 -0
  20. package/dist/cell.d.ts.map +1 -0
  21. package/dist/cell.js +9 -0
  22. package/dist/chain.d.ts +2 -1
  23. package/dist/chain.d.ts.map +1 -1
  24. package/dist/change.d.ts +1 -0
  25. package/dist/change.d.ts.map +1 -1
  26. package/dist/change.js +4 -2
  27. package/dist/chat-types.d.ts +5 -0
  28. package/dist/chat-types.d.ts.map +1 -0
  29. package/dist/chat-types.js +13 -0
  30. package/dist/client.d.ts +30 -14
  31. package/dist/client.d.ts.map +1 -1
  32. package/dist/client.js +61 -10
  33. package/dist/collection.d.ts +7 -4
  34. package/dist/collection.d.ts.map +1 -1
  35. package/dist/collection.js +24 -19
  36. package/dist/compile.d.ts +6 -6
  37. package/dist/compile.d.ts.map +1 -1
  38. package/dist/compile.js +290 -297
  39. package/dist/connection.d.ts +6 -142
  40. package/dist/connection.d.ts.map +1 -1
  41. package/dist/connection.js +105 -4827
  42. package/dist/context.d.ts +2 -0
  43. package/dist/context.d.ts.map +1 -0
  44. package/dist/{chat/context.js → context.js} +1 -4
  45. package/dist/conversation-live.d.ts +135 -0
  46. package/dist/conversation-live.d.ts.map +1 -0
  47. package/dist/conversation-live.js +247 -0
  48. package/dist/conversation-row.d.ts +33 -0
  49. package/dist/conversation-row.d.ts.map +1 -0
  50. package/dist/conversation-row.js +53 -0
  51. package/dist/conversation.d.ts +42 -46
  52. package/dist/conversation.d.ts.map +1 -1
  53. package/dist/conversation.js +901 -815
  54. package/dist/discovery.d.ts +10 -0
  55. package/dist/discovery.d.ts.map +1 -0
  56. package/dist/discovery.js +175 -0
  57. package/dist/durability.d.ts +15 -4
  58. package/dist/durability.d.ts.map +1 -1
  59. package/dist/durability.js +5 -0
  60. package/dist/entry.d.ts +3 -2
  61. package/dist/entry.d.ts.map +1 -1
  62. package/dist/entry.js +18 -8
  63. package/dist/expectation.d.ts +4 -2
  64. package/dist/expectation.d.ts.map +1 -1
  65. package/dist/expectation.js +33 -11
  66. package/dist/failure.d.ts +1 -0
  67. package/dist/failure.d.ts.map +1 -1
  68. package/dist/failure.js +3 -0
  69. package/dist/figures.d.ts +49 -0
  70. package/dist/figures.d.ts.map +1 -0
  71. package/dist/figures.js +197 -0
  72. package/dist/file.d.ts +2 -11
  73. package/dist/file.d.ts.map +1 -1
  74. package/dist/file.js +6 -4
  75. package/dist/holdings.d.ts +29 -0
  76. package/dist/holdings.d.ts.map +1 -0
  77. package/dist/holdings.js +95 -0
  78. package/dist/host.d.ts +4 -6
  79. package/dist/host.d.ts.map +1 -1
  80. package/dist/host.js +59 -38
  81. package/dist/identity.d.ts +0 -1
  82. package/dist/identity.d.ts.map +1 -1
  83. package/dist/identity.js +0 -3
  84. package/dist/index.d.ts +30 -13
  85. package/dist/index.d.ts.map +1 -1
  86. package/dist/index.js +10 -3
  87. package/dist/install.d.ts +56 -0
  88. package/dist/install.d.ts.map +1 -0
  89. package/dist/install.js +556 -0
  90. package/dist/job.d.ts +5 -0
  91. package/dist/job.d.ts.map +1 -0
  92. package/dist/job.js +7 -0
  93. package/dist/lifecycle.d.ts +7 -0
  94. package/dist/lifecycle.d.ts.map +1 -0
  95. package/dist/lifecycle.js +270 -0
  96. package/dist/listing.d.ts +53 -0
  97. package/dist/listing.d.ts.map +1 -0
  98. package/dist/listing.js +176 -0
  99. package/dist/live.d.ts +5 -4
  100. package/dist/live.d.ts.map +1 -1
  101. package/dist/live.js +63 -21
  102. package/dist/locks.d.ts +9 -0
  103. package/dist/locks.d.ts.map +1 -0
  104. package/dist/locks.js +62 -0
  105. package/dist/log.d.ts +1 -0
  106. package/dist/log.d.ts.map +1 -1
  107. package/dist/lost.d.ts +5 -0
  108. package/dist/lost.d.ts.map +1 -0
  109. package/dist/lost.js +61 -0
  110. package/dist/media.d.ts +8 -0
  111. package/dist/media.d.ts.map +1 -0
  112. package/dist/media.js +218 -0
  113. package/dist/memory.d.ts +13 -6
  114. package/dist/memory.d.ts.map +1 -1
  115. package/dist/memory.js +219 -102
  116. package/dist/messages.d.ts +24 -3
  117. package/dist/messages.d.ts.map +1 -1
  118. package/dist/messages.js +89 -31
  119. package/dist/model.d.ts +8 -10
  120. package/dist/model.d.ts.map +1 -1
  121. package/dist/model.js +6 -18
  122. package/dist/models.d.ts +14 -0
  123. package/dist/models.d.ts.map +1 -0
  124. package/dist/models.js +197 -0
  125. package/dist/observation.d.ts +3 -2
  126. package/dist/observation.d.ts.map +1 -1
  127. package/dist/observation.js +15 -15
  128. package/dist/opened.js +3 -3
  129. package/dist/opening.d.ts +4 -0
  130. package/dist/opening.d.ts.map +1 -0
  131. package/dist/opening.js +60 -0
  132. package/dist/outcome.d.ts +17 -7
  133. package/dist/outcome.d.ts.map +1 -1
  134. package/dist/outcome.js +19 -5
  135. package/dist/overlay.d.ts +1 -0
  136. package/dist/overlay.d.ts.map +1 -1
  137. package/dist/overlay.js +12 -1
  138. package/dist/parts.d.ts +11 -0
  139. package/dist/parts.d.ts.map +1 -0
  140. package/dist/parts.js +21 -0
  141. package/dist/paths.d.ts +2 -0
  142. package/dist/paths.d.ts.map +1 -1
  143. package/dist/paths.js +26 -0
  144. package/dist/preparation.d.ts +20 -0
  145. package/dist/preparation.d.ts.map +1 -0
  146. package/dist/preparation.js +233 -0
  147. package/dist/queries.d.ts +16 -0
  148. package/dist/queries.d.ts.map +1 -0
  149. package/dist/queries.js +272 -0
  150. package/dist/query.d.ts +19 -206
  151. package/dist/query.d.ts.map +1 -1
  152. package/dist/query.js +3 -494
  153. package/dist/{chat/questions.d.ts → questions.d.ts} +5 -2
  154. package/dist/questions.d.ts.map +1 -0
  155. package/dist/{chat/questions.js → questions.js} +13 -2
  156. package/dist/reach.d.ts +20 -13
  157. package/dist/reach.d.ts.map +1 -1
  158. package/dist/reach.js +204 -135
  159. package/dist/react.d.ts +14 -2
  160. package/dist/react.d.ts.map +1 -1
  161. package/dist/react.js +10 -1
  162. package/dist/{chat/read.d.ts → read.d.ts} +2 -0
  163. package/dist/read.d.ts.map +1 -0
  164. package/dist/{chat/read.js → read.js} +4 -0
  165. package/dist/readiness.d.ts +12 -16
  166. package/dist/readiness.d.ts.map +1 -1
  167. package/dist/readiness.js +2 -8
  168. package/dist/reads.d.ts +36 -10
  169. package/dist/reads.d.ts.map +1 -1
  170. package/dist/reads.js +159 -83
  171. package/dist/recovery.d.ts +1 -0
  172. package/dist/recovery.d.ts.map +1 -1
  173. package/dist/recovery.js +35 -1
  174. package/dist/refresh.d.ts +10 -4
  175. package/dist/refresh.d.ts.map +1 -1
  176. package/dist/refresh.js +92 -30
  177. package/dist/refusal.d.ts +5 -0
  178. package/dist/refusal.d.ts.map +1 -0
  179. package/dist/refusal.js +57 -0
  180. package/dist/replica.d.ts +11 -10
  181. package/dist/replica.d.ts.map +1 -1
  182. package/dist/replica.js +56 -23
  183. package/dist/room-draft.d.ts +22 -7
  184. package/dist/room-draft.d.ts.map +1 -1
  185. package/dist/room-draft.js +134 -54
  186. package/dist/room-drafts.d.ts +24 -0
  187. package/dist/room-drafts.d.ts.map +1 -0
  188. package/dist/room-drafts.js +217 -0
  189. package/dist/rows.d.ts +18 -16
  190. package/dist/rows.d.ts.map +1 -1
  191. package/dist/rows.js +45 -153
  192. package/dist/runtime.d.ts +43 -0
  193. package/dist/runtime.d.ts.map +1 -0
  194. package/dist/runtime.js +1 -0
  195. package/dist/save.d.ts +7 -1
  196. package/dist/save.d.ts.map +1 -1
  197. package/dist/save.js +46 -27
  198. package/dist/saves.d.ts +70 -0
  199. package/dist/saves.d.ts.map +1 -0
  200. package/dist/saves.js +1005 -0
  201. package/dist/search.d.ts +12 -63
  202. package/dist/search.d.ts.map +1 -1
  203. package/dist/search.js +9 -52
  204. package/dist/seed.d.ts +11 -2
  205. package/dist/seed.d.ts.map +1 -1
  206. package/dist/seed.js +71 -1
  207. package/dist/shared.d.ts +1 -7
  208. package/dist/shared.d.ts.map +1 -1
  209. package/dist/shared.js +13 -105
  210. package/dist/shown.d.ts +31 -0
  211. package/dist/shown.d.ts.map +1 -0
  212. package/dist/shown.js +290 -0
  213. package/dist/socket/admission.d.ts +7 -0
  214. package/dist/socket/admission.d.ts.map +1 -0
  215. package/dist/socket/admission.js +39 -0
  216. package/dist/socket/answers.d.ts +6 -0
  217. package/dist/socket/answers.d.ts.map +1 -0
  218. package/dist/socket/answers.js +20 -0
  219. package/dist/socket/lanes.d.ts +20 -0
  220. package/dist/socket/lanes.d.ts.map +1 -0
  221. package/dist/socket/lanes.js +77 -0
  222. package/dist/socket/reads.d.ts +10 -0
  223. package/dist/socket/reads.d.ts.map +1 -0
  224. package/dist/socket/reads.js +31 -0
  225. package/dist/socket/rooms.d.ts +44 -0
  226. package/dist/socket/rooms.d.ts.map +1 -0
  227. package/dist/socket/rooms.js +90 -0
  228. package/dist/socket/session.d.ts +38 -0
  229. package/dist/socket/session.d.ts.map +1 -0
  230. package/dist/socket/session.js +23 -0
  231. package/dist/socket.d.ts +70 -0
  232. package/dist/socket.d.ts.map +1 -0
  233. package/dist/socket.js +175 -0
  234. package/dist/source.d.ts +61 -0
  235. package/dist/source.d.ts.map +1 -0
  236. package/dist/source.js +82 -0
  237. package/dist/status.d.ts +4 -5
  238. package/dist/status.d.ts.map +1 -1
  239. package/dist/status.js +3 -7
  240. package/dist/store/fences.d.ts +16 -0
  241. package/dist/store/fences.d.ts.map +1 -0
  242. package/dist/store/fences.js +26 -0
  243. package/dist/store/installs.d.ts +30 -0
  244. package/dist/store/installs.d.ts.map +1 -0
  245. package/dist/store/installs.js +34 -0
  246. package/dist/store/readiness.d.ts +10 -0
  247. package/dist/store/readiness.d.ts.map +1 -0
  248. package/dist/store/readiness.js +52 -0
  249. package/dist/store/records.d.ts +210 -0
  250. package/dist/store/records.d.ts.map +1 -0
  251. package/dist/store/records.js +428 -0
  252. package/dist/store/room-held.d.ts +8 -0
  253. package/dist/store/room-held.d.ts.map +1 -0
  254. package/dist/store/room-held.js +17 -0
  255. package/dist/store/space.d.ts +11 -0
  256. package/dist/store/space.d.ts.map +1 -0
  257. package/dist/store/space.js +41 -0
  258. package/dist/store/watchers.d.ts +17 -0
  259. package/dist/store/watchers.d.ts.map +1 -0
  260. package/dist/store/watchers.js +56 -0
  261. package/dist/store/writes.d.ts +24 -0
  262. package/dist/store/writes.d.ts.map +1 -0
  263. package/dist/store/writes.js +125 -0
  264. package/dist/store.d.ts +105 -149
  265. package/dist/store.d.ts.map +1 -1
  266. package/dist/store.js +143 -390
  267. package/dist/stored.d.ts +67 -49
  268. package/dist/stored.d.ts.map +1 -1
  269. package/dist/stored.js +358 -253
  270. package/dist/surface.d.ts +88 -0
  271. package/dist/surface.d.ts.map +1 -0
  272. package/dist/surface.js +224 -0
  273. package/dist/sync.d.ts +19 -0
  274. package/dist/sync.d.ts.map +1 -0
  275. package/dist/sync.js +532 -0
  276. package/dist/tables.d.ts +140 -60
  277. package/dist/tables.d.ts.map +1 -1
  278. package/dist/tables.js +268 -101
  279. package/dist/tallies.d.ts +18 -0
  280. package/dist/tallies.d.ts.map +1 -0
  281. package/dist/tallies.js +129 -0
  282. package/dist/transfer.d.ts +0 -5
  283. package/dist/transfer.d.ts.map +1 -1
  284. package/dist/validation.d.ts +3 -4
  285. package/dist/validation.d.ts.map +1 -1
  286. package/dist/validation.js +42 -100
  287. package/dist/windows.d.ts +1 -12
  288. package/dist/windows.d.ts.map +1 -1
  289. package/dist/windows.js +2 -75
  290. package/dist/wire.d.ts +22 -10
  291. package/dist/wire.d.ts.map +1 -1
  292. package/dist/wire.js +9 -3
  293. package/package.json +12 -18
  294. package/AGENTS.md +0 -320
  295. package/dist/chat/acts.d.ts +0 -33
  296. package/dist/chat/acts.d.ts.map +0 -1
  297. package/dist/chat/acts.js +0 -36
  298. package/dist/chat/context.d.ts +0 -3
  299. package/dist/chat/context.d.ts.map +0 -1
  300. package/dist/chat/earlier.d.ts +0 -10
  301. package/dist/chat/earlier.d.ts.map +0 -1
  302. package/dist/chat/earlier.js +0 -54
  303. package/dist/chat/house.d.ts +0 -25
  304. package/dist/chat/house.d.ts.map +0 -1
  305. package/dist/chat/house.js +0 -38
  306. package/dist/chat/input.d.ts +0 -11
  307. package/dist/chat/input.d.ts.map +0 -1
  308. package/dist/chat/input.js +0 -92
  309. package/dist/chat/kit.d.ts +0 -61
  310. package/dist/chat/kit.d.ts.map +0 -1
  311. package/dist/chat/kit.js +0 -123
  312. package/dist/chat/lasting.d.ts +0 -6
  313. package/dist/chat/lasting.d.ts.map +0 -1
  314. package/dist/chat/lasting.js +0 -19
  315. package/dist/chat/list.d.ts +0 -28
  316. package/dist/chat/list.d.ts.map +0 -1
  317. package/dist/chat/list.js +0 -103
  318. package/dist/chat/markdown.d.ts +0 -21
  319. package/dist/chat/markdown.d.ts.map +0 -1
  320. package/dist/chat/markdown.js +0 -246
  321. package/dist/chat/message.d.ts +0 -7
  322. package/dist/chat/message.d.ts.map +0 -1
  323. package/dist/chat/message.js +0 -254
  324. package/dist/chat/messages.d.ts +0 -35
  325. package/dist/chat/messages.d.ts.map +0 -1
  326. package/dist/chat/messages.js +0 -210
  327. package/dist/chat/name.d.ts +0 -7
  328. package/dist/chat/name.d.ts.map +0 -1
  329. package/dist/chat/name.js +0 -37
  330. package/dist/chat/opened.d.ts +0 -12
  331. package/dist/chat/opened.d.ts.map +0 -1
  332. package/dist/chat/opened.js +0 -19
  333. package/dist/chat/overlay.d.ts +0 -32
  334. package/dist/chat/overlay.d.ts.map +0 -1
  335. package/dist/chat/overlay.js +0 -103
  336. package/dist/chat/parts.d.ts +0 -58
  337. package/dist/chat/parts.d.ts.map +0 -1
  338. package/dist/chat/parts.js +0 -120
  339. package/dist/chat/pieces.d.ts +0 -62
  340. package/dist/chat/pieces.d.ts.map +0 -1
  341. package/dist/chat/pieces.js +0 -1110
  342. package/dist/chat/process.d.ts +0 -20
  343. package/dist/chat/process.d.ts.map +0 -1
  344. package/dist/chat/process.js +0 -32
  345. package/dist/chat/provider.d.ts +0 -70
  346. package/dist/chat/provider.d.ts.map +0 -1
  347. package/dist/chat/provider.js +0 -635
  348. package/dist/chat/question.d.ts +0 -9
  349. package/dist/chat/question.d.ts.map +0 -1
  350. package/dist/chat/question.js +0 -69
  351. package/dist/chat/questions.d.ts.map +0 -1
  352. package/dist/chat/read.d.ts.map +0 -1
  353. package/dist/chat/ready.d.ts +0 -37
  354. package/dist/chat/ready.d.ts.map +0 -1
  355. package/dist/chat/ready.js +0 -93
  356. package/dist/chat/room-file.d.ts +0 -8
  357. package/dist/chat/room-file.d.ts.map +0 -1
  358. package/dist/chat/room-file.js +0 -72
  359. package/dist/chat/steady.d.ts +0 -3
  360. package/dist/chat/steady.d.ts.map +0 -1
  361. package/dist/chat/steady.js +0 -26
  362. package/dist/chat/style.d.ts +0 -4
  363. package/dist/chat/style.d.ts.map +0 -1
  364. package/dist/chat/style.js +0 -307
  365. package/dist/chat.d.ts +0 -15
  366. package/dist/chat.d.ts.map +0 -1
  367. package/dist/chat.js +0 -8
  368. package/dist/fence.d.ts +0 -6
  369. package/dist/fence.d.ts.map +0 -1
  370. package/dist/fence.js +0 -10
  371. package/dist/fulltext.d.ts +0 -27
  372. package/dist/fulltext.d.ts.map +0 -1
  373. package/dist/fulltext.js +0 -368
  374. package/dist/words.d.ts +0 -19
  375. package/dist/words.d.ts.map +0 -1
  376. package/dist/words.js +0 -150
package/dist/store.js CHANGED
@@ -1,399 +1,152 @@
1
- import { binaryLocalId } from './identity.js';
2
- const FILE = '\u0000file';
3
- function batchFiles(batch) {
4
- return batch.changes.flatMap((change) => (change.kind === 'binary' ? [change.localId] : []));
5
- }
6
- function draftFiles(drafts) {
7
- return drafts.flatMap(({ parts }) => (parts.file === undefined ? [] : [parts.file.localId]));
8
- }
9
- function roomDraftRow(record) {
10
- return { path: record.path, record: JSON.stringify(record), files: draftFiles([record]) };
11
- }
12
- function authoredRows({ locator, queue, founded, saved, roomDrafts, consumedRoomDrafts, accepted }, held) {
13
- return {
14
- locator,
15
- ...(queue === undefined
16
- ? {}
17
- : {
18
- order: queue.map(({ id }) => id),
19
- added: queue
20
- .filter(({ id }) => held?.has(id) !== true)
21
- .map((batch) => {
22
- const drafts = saved?.get(batch.id) ?? [];
23
- return {
24
- id: batch.id,
25
- batch: JSON.stringify(batch),
26
- state: JSON.stringify({
27
- drafts,
28
- submission: { state: 'waiting' },
29
- lost: false,
30
- }),
31
- files: [...batchFiles(batch), ...draftFiles(drafts)],
32
- };
33
- }),
34
- }),
35
- ...(founded === undefined
36
- ? {}
37
- : {
38
- founded: founded.map((batch) => ({
39
- id: batch.id,
40
- batch: JSON.stringify(batch),
41
- files: batchFiles(batch),
42
- })),
43
- }),
44
- ...(roomDrafts === undefined ? {} : { roomDrafts: roomDrafts.map(roomDraftRow) }),
45
- ...(consumedRoomDrafts === undefined ? {} : { consumedRoomDrafts }),
46
- ...(accepted === undefined
47
- ? {}
48
- : {
49
- amendments: accepted.map(({ id, roomVersion }) => ({
50
- id,
51
- amendment: JSON.stringify({ accepted: roomVersion }),
52
- })),
53
- }),
54
- };
55
- }
56
- function marked(value) {
57
- return typeof value === 'object' && value !== null && FILE in value;
58
- }
59
- function keyed(keys, value) {
60
- const stored = [];
61
- const files = [];
62
- const record = JSON.stringify(value, (_name, held) => {
63
- if (!(held instanceof Blob))
64
- return held;
65
- let key = keys.get(held);
66
- if (key === undefined) {
67
- key = binaryLocalId();
68
- keys.set(held, key);
69
- }
70
- stored.push({ key, blob: held });
71
- files.push(key);
72
- return { [FILE]: key, type: held.type };
1
+ import { schemaTypes } from './attached-model.js';
2
+ import { createCell } from './cell.js';
3
+ import { openedFiles } from './opened.js';
4
+ import { compositionOf } from './replica.js';
5
+ import { STARTING } from './status.js';
6
+ import { createFences } from './store/fences.js';
7
+ import { createInstalls } from './store/installs.js';
8
+ import { openRuntimeStore, } from './store/records.js';
9
+ import { createReadinessQueue } from './store/readiness.js';
10
+ import { createRoomHeld } from './store/room-held.js';
11
+ import { createSpace } from './store/space.js';
12
+ import { createWatchers } from './store/watchers.js';
13
+ import { createWrites } from './store/writes.js';
14
+ export { NO_INSTALL } from './store/installs.js';
15
+ export function createStore(options) {
16
+ const writes = createWrites();
17
+ const watchers = createWatchers(options.unpublished);
18
+ const fences = createFences();
19
+ const installs = createInstalls();
20
+ const seeds = new Map();
21
+ const landed = createRoomHeld();
22
+ const attached = createRoomHeld();
23
+ const typesHeld = new Map();
24
+ const device = createCell({
25
+ persistence: options.persistence,
26
+ storageLoss: null,
27
+ heldWhenOpened: false,
28
+ preparing: false,
29
+ sweeping: false,
30
+ unpreparedDemand: new Set(),
31
+ establishing: new Set(),
32
+ status: STARTING,
33
+ roomsShown: null,
73
34
  });
74
- return { record, files, stored };
75
- }
76
- async function filed(value, read) {
77
- if (Array.isArray(value))
78
- return Promise.all(value.map((item) => filed(item, read)));
79
- if (marked(value))
80
- return read(value[FILE], value.type);
81
- if (typeof value !== 'object' || value === null)
82
- return value;
83
- const restored = {};
84
- for (const [name, item] of Object.entries(value))
85
- restored[name] = await filed(item, read);
86
- return restored;
87
- }
88
- function json(value, fallback) {
89
- return value === undefined ? fallback : JSON.parse(value);
90
- }
91
- export function openRuntimeStore(fenced, authored) {
92
- const keys = new WeakMap();
93
- const read = async (key, type) => {
94
- const blob = await authored.ask({ kind: 'authored.file', key, type });
95
- keys.set(blob, key);
96
- return blob;
97
- };
98
- const restored = async (records) => Promise.all(records.map(async (record) => (await filed(JSON.parse(record), read))));
99
- const amend = async (locator, id, amendment) => {
100
- if (fenced(locator))
101
- return;
102
- await authored.ask({
103
- kind: 'authored.amend',
104
- locator,
105
- id,
106
- amendment: JSON.stringify(amendment),
35
+ const space = createSpace(options.database, options.spaceFreed);
36
+ const readiness = createReadinessQueue(options.deliverReadiness, options.unpublished);
37
+ let opening;
38
+ function open() {
39
+ opening ??= options.opening(openRuntimeStore(options.database)).catch((error) => {
40
+ opening = undefined;
41
+ throw error;
107
42
  });
108
- };
109
- const fact = (name) => authored.ask({ kind: 'authored.fact', name });
110
- const queued = new Map();
111
- const changed = new Map();
112
- const change = (key) => changed.set(key, (changed.get(key) ?? 0) + 1);
113
- const rows = (writes) => writes.map((write) => authoredRows(write, queued.get(write.locator)));
114
- const noted = (writes) => {
115
- for (const { locator, queue } of writes) {
116
- if (queue !== undefined)
117
- queued.set(locator, new Set(queue.map(({ id }) => id)));
118
- }
119
- };
43
+ return opening;
44
+ }
45
+ function typed(locator, persisted) {
46
+ if (persisted.status === 'unavailable')
47
+ return persisted;
48
+ const schema = persisted.snapshot.schema;
49
+ const held = typesHeld.get(locator);
50
+ if (held !== undefined && held.schema === schema)
51
+ return { ...persisted, types: held.types };
52
+ const types = schemaTypes(schema, options.roomTypes);
53
+ typesHeld.set(locator, { schema, types });
54
+ return { ...persisted, types };
55
+ }
56
+ function recordOf(stored) {
57
+ return { ...stored, model: typed(stored.locator, stored.model) };
58
+ }
59
+ async function rooms(locators) {
60
+ const held = await (await open()).rooms(locators);
61
+ return new Map(held
62
+ .filter(({ record }) => !fences.has(record.locator))
63
+ .map(({ record, indexed, documents }) => [
64
+ record.locator,
65
+ { record: recordOf(record), indexed, documents },
66
+ ]));
67
+ }
68
+ async function work(locator) {
69
+ if (fences.has(locator))
70
+ return { replica: undefined, queue: [], states: new Map() };
71
+ const { room, queue } = await (await open()).room(locator);
72
+ const batches = queue.map(({ batch }) => batch);
73
+ const states = new Map(queue.map(({ batch, state }) => [batch.id, state]));
74
+ return {
75
+ replica: room === null
76
+ ? undefined
77
+ : { record: recordOf(room.record), queue: batches, states, overlaid: room.overlaid },
78
+ queue: batches,
79
+ states,
80
+ };
81
+ }
82
+ async function drafts(locator, paths) {
83
+ const held = await (await open()).roomDrafts(locator, paths);
84
+ return held.filter((record) => !fences.has(record.locator));
85
+ }
120
86
  return {
121
- async opened() {
122
- const held = await authored.ask({ kind: 'replica.open' });
123
- const unprepared = new Set(held.keys.flatMap(({ key, rederiving }) => (rederiving === null ? [] : [key])));
124
- const overlaid = new Map();
125
- for (const { key, path } of held.overlaid) {
126
- const paths = overlaid.get(key) ?? new Set();
127
- paths.add(path);
128
- overlaid.set(key, paths);
129
- }
87
+ device,
88
+ space,
89
+ readiness,
90
+ ask: options.database.ask,
91
+ changed: options.changed,
92
+ open,
93
+ durably: (work) => writes.exclusive(async () => {
94
+ const local = await open();
95
+ return work(local);
96
+ }),
97
+ exclusive: writes.exclusive,
98
+ gathered: writes.gathered,
99
+ written: writes.written,
100
+ startingWrite: writes.startingWrite,
101
+ finishingWrite: writes.finishingWrite,
102
+ ahead: writes.ahead,
103
+ rederive: writes.rederive,
104
+ refreshPage: writes.refreshPage,
105
+ stageBytes: writes.stageBytes,
106
+ deriveBytes: writes.deriveBytes,
107
+ rooms,
108
+ record: async (locator) => (await rooms([locator])).get(locator)?.record,
109
+ work,
110
+ replica: async (locator) => (await work(locator)).replica,
111
+ async covering() {
112
+ const held = await (await open()).covering();
130
113
  return {
131
- rooms: new Map(held.rooms
132
- .filter(({ locator }) => !fenced(locator))
133
- .map(({ locator, key, record }) => [
134
- locator,
135
- { ...JSON.parse(record), key },
136
- ])),
137
- transfers: new Map(held.transfers
138
- .filter(({ locator }) => !fenced(locator))
139
- .map(({ locator, key, record }) => [
140
- locator,
141
- { ...JSON.parse(record), key },
142
- ])),
143
- unprepared,
144
- overlaid,
145
- erasing: held.erasing.length > 0,
146
- orphans: held.orphans,
147
- lastCompleted: json(held.facts.lastCompleted, null),
148
- providerLimits: json(held.facts.providerLimits, []),
149
- capabilities: json(held.facts.capabilities, []),
150
- member: json(held.facts.member, null),
151
- targets: json(held.facts.targets, {}),
152
- presentation: held.presentation.map(({ app, value }) => ({
153
- app,
154
- value: JSON.parse(value),
155
- })),
114
+ rooms: new Map([...held.rooms].filter(([locator]) => !fences.has(locator))),
115
+ queues: new Map([...held.queues].filter(([locator]) => !fences.has(locator))),
156
116
  };
157
117
  },
158
- counts(rooms) {
159
- return authored.ask({ kind: 'replica.counts', rooms });
160
- },
161
- query(plan, rooms, budget) {
162
- return authored.ask({ kind: 'replica.query', plan, rooms, budget });
163
- },
164
- changes(keys) {
165
- return keys.reduce((total, key) => total + (changed.get(key) ?? 0), 0);
166
- },
167
- entries(key, view, paths) {
168
- return authored.ask({ kind: 'replica.entries', key, view, paths });
169
- },
170
- typed(key, view, paths) {
171
- return authored.ask({ kind: 'replica.typed', key, view, paths });
172
- },
173
- referrers(key, view, targets, kinds) {
174
- return authored.ask({ kind: 'replica.referrers', key, view, targets, kinds });
175
- },
176
- walk(key, view, after, bytes) {
177
- return authored.ask({ kind: 'replica.walk', key, view, after, bytes });
178
- },
179
- files(key, view, after, limit) {
180
- return authored.ask({ kind: 'replica.files', key, view, after, limit });
181
- },
182
- typedWithin(key, view, roots, within) {
183
- return authored.ask({ kind: 'replica.typedWithin', key, view, roots, within });
184
- },
185
- search(ask) {
186
- return authored.ask({ kind: 'replica.search', ...ask });
187
- },
188
- async stage(record, composition, page, key, last) {
189
- if (fenced(record.locator))
190
- return null;
191
- const { key: _key, ...kept } = record;
192
- return authored.ask({
193
- kind: 'replica.stage',
194
- locator: record.locator,
195
- transfer: record.transfer,
196
- record: last ? JSON.stringify(kept) : null,
197
- key,
198
- composition,
199
- entries: page.entries,
200
- folders: page.folders,
201
- });
202
- },
203
- async refresh(key, page) {
204
- await authored.ask({
205
- kind: 'replica.refresh',
206
- key,
207
- entries: page.entries,
208
- removals: page.removals,
209
- folders: page.folders,
210
- folderRemovals: page.folderRemovals,
211
- });
212
- },
213
- async unrefresh(key) {
214
- await authored.ask({ kind: 'replica.unrefresh', key });
215
- },
216
- async commit({ rooms, writes = [], files = [], whole = false, discards = [] }) {
217
- const kept = whole ? writes : writes.filter(({ locator }) => !fenced(locator));
218
- const refused = whole ? kept.find(({ locator }) => fenced(locator)) : undefined;
219
- if (refused !== undefined) {
220
- return { fenced: refused.locator, counts: {}, changed: {}, prepared: {} };
221
- }
222
- for (const room of rooms)
223
- change(room.key);
224
- const committed = await authored.ask({
225
- kind: 'replica.commit',
226
- ...(kept.length === 0 && files.length === 0 && discards.length === 0
227
- ? {}
228
- : { authored: { writes: rows(kept), files, whole, discards } }),
229
- rooms: rooms.filter(({ locator }) => !fenced(locator)),
230
- });
231
- if (committed.fenced === null || !whole)
232
- noted(kept);
233
- for (const locator of discards)
234
- queued.delete(locator);
235
- return committed;
236
- },
237
- async fence(locators) {
238
- await authored.ask({ kind: 'replica.fence', locators });
239
- for (const locator of locators)
240
- queued.delete(locator);
241
- },
242
- async erase(locator, limit) {
243
- return (await authored.ask({ kind: 'replica.erase', locator, limit })).remaining;
244
- },
245
- async abandon(locator, kept = false) {
246
- if (fenced(locator))
247
- return null;
248
- return authored.ask({ kind: 'replica.abandon', locator, kept });
249
- },
250
- async release(key) {
251
- await authored.ask({ kind: 'replica.release', key });
252
- },
253
- async rederive(key, limit) {
254
- change(key);
255
- return (await authored.ask({ kind: 'replica.rederive', key, limit })).done;
256
- },
257
- async fact(name, value) {
258
- await authored.ask({ kind: 'replica.fact', name, value: JSON.stringify(value) });
259
- },
260
- async queues() {
261
- const { queues, pending } = await authored.ask({ kind: 'authored.queues' });
262
- queued.clear();
263
- for (const queue of queues)
264
- queued.set(queue.locator, new Set(queue.ids));
265
- const byId = new Map(pending.map((record) => [`${record.locator}\0${record.id}`, record]));
266
- return queues
267
- .filter((queue) => !fenced(queue.locator))
268
- .map((queue) => ({
269
- locator: queue.locator,
270
- pending: queue.ids.map((id) => {
271
- const record = byId.get(`${queue.locator}\0${id}`);
272
- if (record === undefined)
273
- throw new Error(`Missing Pending Change ${queue.locator}/${id}`);
274
- return {
275
- batch: JSON.parse(record.batch),
276
- state: JSON.parse(record.state),
277
- };
278
- }),
279
- }));
280
- },
281
- submit(locator, id, submission, lost) {
282
- return amend(locator, id, { submission, lost });
283
- },
284
- keepUpload(locator, id, upload) {
285
- return amend(locator, id, { upload });
286
- },
287
- async roomDrafts() {
288
- const held = await authored.ask({ kind: 'authored.roomDrafts' });
289
- return held
290
- .map((record) => JSON.parse(record))
291
- .filter((record) => !fenced(record.locator));
292
- },
293
- async present(record) {
294
- await authored.ask({
295
- kind: 'replica.present',
296
- app: record.app,
297
- value: JSON.stringify(record.value ?? null),
298
- });
299
- },
300
- conversations() {
301
- return authored.ask({ kind: 'conversation.held' });
302
- },
303
- async keepMessages(write) {
304
- await authored.ask({ kind: 'conversation.keep', ...write });
305
- },
306
- messageWindow(conversation, limit, before) {
307
- return authored.ask({ kind: 'conversation.window', conversation, limit, before });
308
- },
309
- messageCount(conversation) {
310
- return authored.ask({ kind: 'conversation.count', conversation });
311
- },
312
- turnArrived(conversation, turn) {
313
- return authored.ask({ kind: 'conversation.turn', conversation, turn });
314
- },
315
- async dropConversations(conversations) {
316
- await authored.ask({ kind: 'conversation.drop', conversations });
317
- },
318
- async chatDrafts() {
319
- const records = await authored.ask({ kind: 'authored.chatDrafts' });
320
- return records.map((record) => JSON.parse(record));
321
- },
322
- async keepChatDrafts(records) {
323
- await authored.ask({
324
- kind: 'authored.chatDraft',
325
- kept: records.map((record) => ({
326
- conversation: record.conversation,
327
- record: JSON.stringify(record),
328
- })),
329
- dropped: [],
330
- });
331
- },
332
- async dropChatDraft(conversation) {
333
- await authored.ask({ kind: 'authored.chatDraft', kept: [], dropped: [conversation] });
334
- },
335
- async calls() {
336
- return restored(await authored.ask({ kind: 'authored.calls' }));
337
- },
338
- async keepCall(record) {
339
- const { stored, ...kept } = keyed(keys, record);
340
- await authored.ask({
341
- kind: 'authored.call',
342
- operation: record.operation,
343
- kept,
344
- files: stored,
345
- });
346
- },
347
- async dropCall(operation) {
348
- await authored.ask({ kind: 'authored.call', operation, kept: null, files: [] });
349
- },
350
- async keepRoomDraft(record, files) {
351
- if (fenced(record.locator))
352
- return;
353
- const { path, ...kept } = roomDraftRow(record);
354
- await authored.ask({
355
- kind: 'authored.roomDraft',
356
- locator: record.locator,
357
- path,
358
- kept,
359
- files,
360
- });
361
- },
362
- async discardRoomDraft(locator, path) {
363
- if (fenced(locator))
364
- return;
365
- await authored.ask({ kind: 'authored.roomDraft', locator, path, kept: null, files: [] });
366
- },
367
- fences() {
368
- return authored.ask({ kind: 'authored.fences' });
369
- },
370
- receivedHeads() {
371
- return authored.ask({ kind: 'replica.received' });
372
- },
373
- receivedParts(key, count) {
374
- return authored.ask({ kind: 'replica.receivedParts', key, count });
375
- },
376
- async keepReceived(key, head, part) {
377
- await authored.ask({
378
- kind: 'replica.receive',
379
- key,
380
- head: JSON.stringify(head),
381
- ...(part === undefined
382
- ? {}
383
- : { part: { index: part.index, value: JSON.stringify(part.value) } }),
384
- });
385
- },
386
- async dropReceived(keys) {
387
- await authored.ask({ kind: 'replica.unreceive', keys });
388
- },
389
- generation() {
390
- return fact('generation');
391
- },
392
- async rememberGeneration(value) {
393
- await authored.ask({ kind: 'authored.remember', name: 'generation', value });
394
- },
395
- file(key, type) {
396
- return authored.ask({ kind: 'authored.file', key, type });
118
+ drafts,
119
+ draft: async (locator, path) => (await drafts(locator, [path]))[0],
120
+ composed: (record) => compositionOf(record, options.builtIn, options.typedPaths),
121
+ publication: watchers.publication,
122
+ owing: watchers.owing,
123
+ watch: watchers.watch,
124
+ watching: watchers.watching,
125
+ tell: watchers.tell,
126
+ fences,
127
+ installs,
128
+ seeds: {
129
+ of: (key) => seeds.get(key),
130
+ keep(key, seed) {
131
+ seeds.set(key, seed);
132
+ },
133
+ drop(key) {
134
+ seeds.delete(key);
135
+ },
136
+ },
137
+ landed,
138
+ attached,
139
+ opened: openedFiles(options.database, fences.has),
140
+ part: (file, part) => options.database.ask({ kind: 'file.part', key: file, part }),
141
+ forget(locator) {
142
+ typesHeld.delete(locator);
143
+ landed.forget(locator);
144
+ attached.forget(locator);
145
+ },
146
+ close() {
147
+ space.stop();
148
+ readiness.close();
149
+ watchers.close();
397
150
  },
398
151
  };
399
152
  }