@agentshouse/mdruntime 0.1.0-c2aaa9c0e3ca4 → 0.1.0-c2c52158b7381

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 (199) hide show
  1. package/AGENTS.md +285 -276
  2. package/README.md +9 -8
  3. package/dist/adapter.d.ts +80 -21
  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 +3 -2
  11. package/dist/bootstrap.d.ts.map +1 -1
  12. package/dist/bootstrap.js +83 -26
  13. package/dist/change-set.d.ts +7 -2
  14. package/dist/change-set.d.ts.map +1 -1
  15. package/dist/change-set.js +37 -10
  16. package/dist/change.d.ts +31 -1
  17. package/dist/change.d.ts.map +1 -1
  18. package/dist/change.js +10 -1
  19. package/dist/chat/acts.d.ts +3 -4
  20. package/dist/chat/acts.d.ts.map +1 -1
  21. package/dist/chat/acts.js +10 -10
  22. package/dist/chat/earlier.d.ts +10 -0
  23. package/dist/chat/earlier.d.ts.map +1 -0
  24. package/dist/chat/earlier.js +54 -0
  25. package/dist/chat/house.d.ts +25 -0
  26. package/dist/chat/house.d.ts.map +1 -0
  27. package/dist/chat/house.js +38 -0
  28. package/dist/chat/input.d.ts +3 -1
  29. package/dist/chat/input.d.ts.map +1 -1
  30. package/dist/chat/input.js +11 -18
  31. package/dist/chat/kit.d.ts +2 -1
  32. package/dist/chat/kit.d.ts.map +1 -1
  33. package/dist/chat/kit.js +3 -3
  34. package/dist/chat/list.d.ts +9 -1
  35. package/dist/chat/list.d.ts.map +1 -1
  36. package/dist/chat/list.js +53 -9
  37. package/dist/chat/markdown.d.ts +3 -1
  38. package/dist/chat/markdown.d.ts.map +1 -1
  39. package/dist/chat/markdown.js +85 -26
  40. package/dist/chat/message.d.ts +1 -0
  41. package/dist/chat/message.d.ts.map +1 -1
  42. package/dist/chat/message.js +57 -12
  43. package/dist/chat/messages.d.ts +9 -1
  44. package/dist/chat/messages.d.ts.map +1 -1
  45. package/dist/chat/messages.js +67 -28
  46. package/dist/chat/name.d.ts +1 -0
  47. package/dist/chat/name.d.ts.map +1 -1
  48. package/dist/chat/name.js +1 -1
  49. package/dist/chat/opened.d.ts +1 -0
  50. package/dist/chat/opened.d.ts.map +1 -1
  51. package/dist/chat/opened.js +2 -0
  52. package/dist/chat/overlay.d.ts +1 -0
  53. package/dist/chat/overlay.d.ts.map +1 -1
  54. package/dist/chat/overlay.js +11 -5
  55. package/dist/chat/parts.d.ts +1 -0
  56. package/dist/chat/parts.d.ts.map +1 -1
  57. package/dist/chat/parts.js +3 -2
  58. package/dist/chat/pieces.d.ts +62 -0
  59. package/dist/chat/pieces.d.ts.map +1 -0
  60. package/dist/chat/pieces.js +1110 -0
  61. package/dist/chat/provider.d.ts +7 -2
  62. package/dist/chat/provider.d.ts.map +1 -1
  63. package/dist/chat/provider.js +129 -155
  64. package/dist/chat/question.d.ts +1 -1
  65. package/dist/chat/question.d.ts.map +1 -1
  66. package/dist/chat/question.js +7 -18
  67. package/dist/chat/ready.d.ts +5 -1
  68. package/dist/chat/ready.d.ts.map +1 -1
  69. package/dist/chat/ready.js +9 -7
  70. package/dist/chat/room-file.d.ts +8 -0
  71. package/dist/chat/room-file.d.ts.map +1 -0
  72. package/dist/chat/room-file.js +72 -0
  73. package/dist/chat/steady.d.ts +3 -0
  74. package/dist/chat/steady.d.ts.map +1 -0
  75. package/dist/chat/steady.js +26 -0
  76. package/dist/chat/style.d.ts.map +1 -1
  77. package/dist/chat/style.js +8 -4
  78. package/dist/chat.d.ts +3 -2
  79. package/dist/chat.d.ts.map +1 -1
  80. package/dist/chat.js +2 -2
  81. package/dist/client.d.ts +21 -9
  82. package/dist/client.d.ts.map +1 -1
  83. package/dist/client.js +47 -8
  84. package/dist/collection.d.ts +21 -6
  85. package/dist/collection.d.ts.map +1 -1
  86. package/dist/collection.js +332 -40
  87. package/dist/connection.d.ts +39 -11
  88. package/dist/connection.d.ts.map +1 -1
  89. package/dist/connection.js +2294 -492
  90. package/dist/conversation.d.ts +28 -9
  91. package/dist/conversation.d.ts.map +1 -1
  92. package/dist/conversation.js +500 -165
  93. package/dist/digest.d.ts +7 -0
  94. package/dist/digest.d.ts.map +1 -0
  95. package/dist/digest.js +113 -0
  96. package/dist/durability.d.ts +8 -1
  97. package/dist/durability.d.ts.map +1 -1
  98. package/dist/entry.d.ts +18 -0
  99. package/dist/entry.d.ts.map +1 -0
  100. package/dist/entry.js +157 -0
  101. package/dist/fence.d.ts +6 -0
  102. package/dist/fence.d.ts.map +1 -0
  103. package/dist/fence.js +10 -0
  104. package/dist/file.d.ts +2 -10
  105. package/dist/file.d.ts.map +1 -1
  106. package/dist/file.js +3 -120
  107. package/dist/host.d.ts +15 -3
  108. package/dist/host.d.ts.map +1 -1
  109. package/dist/host.js +103 -13
  110. package/dist/identity.d.ts +2 -0
  111. package/dist/identity.d.ts.map +1 -1
  112. package/dist/identity.js +6 -0
  113. package/dist/index.d.ts +15 -9
  114. package/dist/index.d.ts.map +1 -1
  115. package/dist/index.js +5 -0
  116. package/dist/memory.d.ts +8 -2
  117. package/dist/memory.d.ts.map +1 -1
  118. package/dist/memory.js +43 -12
  119. package/dist/model.d.ts +4 -2
  120. package/dist/model.d.ts.map +1 -1
  121. package/dist/model.js +10 -1
  122. package/dist/name.d.ts +4 -0
  123. package/dist/name.d.ts.map +1 -0
  124. package/dist/name.js +122 -0
  125. package/dist/observation.d.ts +7 -2
  126. package/dist/observation.d.ts.map +1 -1
  127. package/dist/observation.js +29 -12
  128. package/dist/opened.d.ts +22 -0
  129. package/dist/opened.d.ts.map +1 -0
  130. package/dist/opened.js +107 -0
  131. package/dist/outcome.d.ts +39 -4
  132. package/dist/outcome.d.ts.map +1 -1
  133. package/dist/outcome.js +29 -0
  134. package/dist/overlay.d.ts +6 -4
  135. package/dist/overlay.d.ts.map +1 -1
  136. package/dist/overlay.js +229 -161
  137. package/dist/query.d.ts +57 -4
  138. package/dist/query.d.ts.map +1 -1
  139. package/dist/query.js +37 -7
  140. package/dist/reach.d.ts +11 -4
  141. package/dist/reach.d.ts.map +1 -1
  142. package/dist/reach.js +192 -41
  143. package/dist/react.d.ts +8 -6
  144. package/dist/react.d.ts.map +1 -1
  145. package/dist/react.js +7 -17
  146. package/dist/read-side.d.ts +18 -9
  147. package/dist/read-side.d.ts.map +1 -1
  148. package/dist/read-side.js +894 -155
  149. package/dist/readiness.d.ts +53 -0
  150. package/dist/readiness.d.ts.map +1 -0
  151. package/dist/readiness.js +33 -0
  152. package/dist/reads.d.ts +11 -0
  153. package/dist/reads.d.ts.map +1 -0
  154. package/dist/reads.js +98 -0
  155. package/dist/recovery.d.ts +25 -0
  156. package/dist/recovery.d.ts.map +1 -0
  157. package/dist/recovery.js +198 -0
  158. package/dist/refresh.d.ts +4 -4
  159. package/dist/refresh.d.ts.map +1 -1
  160. package/dist/refresh.js +46 -20
  161. package/dist/replica.d.ts +12 -7
  162. package/dist/replica.d.ts.map +1 -1
  163. package/dist/replica.js +151 -64
  164. package/dist/room-draft.d.ts +16 -2
  165. package/dist/room-draft.d.ts.map +1 -1
  166. package/dist/room-draft.js +32 -25
  167. package/dist/rows.d.ts +6 -13
  168. package/dist/rows.d.ts.map +1 -1
  169. package/dist/rows.js +10 -13
  170. package/dist/save.d.ts +52 -10
  171. package/dist/save.d.ts.map +1 -1
  172. package/dist/save.js +519 -159
  173. package/dist/search.d.ts +49 -6
  174. package/dist/search.d.ts.map +1 -1
  175. package/dist/search.js +242 -46
  176. package/dist/seed.d.ts +16 -0
  177. package/dist/seed.d.ts.map +1 -0
  178. package/dist/seed.js +75 -0
  179. package/dist/shared.d.ts +19 -0
  180. package/dist/shared.d.ts.map +1 -0
  181. package/dist/shared.js +461 -0
  182. package/dist/status.d.ts +4 -0
  183. package/dist/status.d.ts.map +1 -1
  184. package/dist/store.d.ts +61 -17
  185. package/dist/store.d.ts.map +1 -1
  186. package/dist/store.js +581 -310
  187. package/dist/tables.d.ts +242 -0
  188. package/dist/tables.d.ts.map +1 -0
  189. package/dist/tables.js +406 -0
  190. package/dist/turn.d.ts +9 -0
  191. package/dist/turn.d.ts.map +1 -0
  192. package/dist/turn.js +67 -0
  193. package/dist/windows.d.ts +21 -0
  194. package/dist/windows.d.ts.map +1 -0
  195. package/dist/windows.js +84 -0
  196. package/dist/wire.d.ts +168 -32
  197. package/dist/wire.d.ts.map +1 -1
  198. package/dist/wire.js +56 -31
  199. package/package.json +10 -2
@@ -0,0 +1,242 @@
1
+ export type Value = string | number | bigint | Uint8Array | null;
2
+ export type Row = Readonly<Record<string, Value>>;
3
+ export interface Connection {
4
+ run(sql: string, bind?: readonly Value[]): void;
5
+ rows(sql: string, bind?: readonly Value[]): readonly Row[];
6
+ }
7
+ export declare const FILE_PART_BYTES = 1048576;
8
+ export declare const RUNTIME_TABLES: readonly string[];
9
+ export type AuthoredFact = 'holder' | 'binding' | 'generation';
10
+ export type StoredFile = {
11
+ readonly key: string;
12
+ readonly blob: Blob;
13
+ };
14
+ export interface FileSpool {
15
+ held(): Iterable<string>;
16
+ spool(key: string, type: string, parts: Iterable<Uint8Array>): Promise<Blob>;
17
+ drop(key: string): Promise<void>;
18
+ }
19
+ export type KeptPending = {
20
+ readonly id: string;
21
+ readonly batch: string;
22
+ readonly state: string;
23
+ readonly files: readonly string[];
24
+ };
25
+ export type KeptRecord = {
26
+ readonly record: string;
27
+ readonly files: readonly string[];
28
+ };
29
+ export type AuthoredRows = {
30
+ readonly locator: string;
31
+ readonly order?: readonly string[];
32
+ readonly added?: readonly KeptPending[];
33
+ readonly founded?: readonly Omit<KeptPending, 'state'>[];
34
+ readonly roomDrafts?: readonly (KeptRecord & {
35
+ readonly path: string;
36
+ })[];
37
+ readonly consumedRoomDrafts?: readonly string[];
38
+ };
39
+ export type HeldQueues = {
40
+ readonly queues: readonly {
41
+ readonly locator: string;
42
+ readonly ids: readonly string[];
43
+ }[];
44
+ readonly pending: readonly {
45
+ readonly locator: string;
46
+ readonly id: string;
47
+ readonly batch: string;
48
+ readonly state: string;
49
+ }[];
50
+ };
51
+ export type OpenedFile = {
52
+ readonly key: string;
53
+ readonly byteLength: number;
54
+ readonly mediaType: string;
55
+ };
56
+ export interface AuthoredRequests {
57
+ readonly 'authored.queues': {
58
+ readonly asked: Readonly<Record<never, never>>;
59
+ readonly answer: HeldQueues;
60
+ };
61
+ readonly 'authored.roomDrafts': {
62
+ readonly asked: Readonly<Record<never, never>>;
63
+ readonly answer: readonly string[];
64
+ };
65
+ readonly 'authored.chatDrafts': {
66
+ readonly asked: Readonly<Record<never, never>>;
67
+ readonly answer: readonly string[];
68
+ };
69
+ readonly 'authored.calls': {
70
+ readonly asked: Readonly<Record<never, never>>;
71
+ readonly answer: readonly string[];
72
+ };
73
+ readonly 'authored.fences': {
74
+ readonly asked: Readonly<Record<never, never>>;
75
+ readonly answer: readonly string[];
76
+ };
77
+ readonly 'authored.fact': {
78
+ readonly asked: {
79
+ readonly name: AuthoredFact;
80
+ };
81
+ readonly answer: string | null;
82
+ };
83
+ readonly 'authored.file': {
84
+ readonly asked: {
85
+ readonly key: string;
86
+ readonly type: string;
87
+ };
88
+ readonly answer: Blob;
89
+ };
90
+ readonly 'authored.keep': {
91
+ readonly asked: {
92
+ readonly writes: readonly AuthoredRows[];
93
+ readonly files: readonly StoredFile[];
94
+ readonly whole: boolean;
95
+ };
96
+ readonly answer: string | null;
97
+ };
98
+ readonly 'authored.amend': {
99
+ readonly asked: {
100
+ readonly locator: string;
101
+ readonly id: string;
102
+ readonly amendment: string;
103
+ };
104
+ readonly answer: null;
105
+ };
106
+ readonly 'authored.roomDraft': {
107
+ readonly asked: {
108
+ readonly locator: string;
109
+ readonly path: string;
110
+ readonly kept: KeptRecord | null;
111
+ readonly files: readonly StoredFile[];
112
+ };
113
+ readonly answer: string | null;
114
+ };
115
+ readonly 'authored.chatDraft': {
116
+ readonly asked: {
117
+ readonly kept: readonly {
118
+ readonly conversation: string;
119
+ readonly record: string;
120
+ }[];
121
+ readonly dropped: readonly string[];
122
+ };
123
+ readonly answer: null;
124
+ };
125
+ readonly 'authored.call': {
126
+ readonly asked: {
127
+ readonly operation: string;
128
+ readonly kept: KeptRecord | null;
129
+ readonly files: readonly StoredFile[];
130
+ };
131
+ readonly answer: null;
132
+ };
133
+ readonly 'authored.fence': {
134
+ readonly asked: {
135
+ readonly locators: readonly string[];
136
+ };
137
+ readonly answer: null;
138
+ };
139
+ readonly 'authored.unfence': {
140
+ readonly asked: {
141
+ readonly locators: readonly string[];
142
+ };
143
+ readonly answer: null;
144
+ };
145
+ readonly 'authored.discard': {
146
+ readonly asked: {
147
+ readonly locator: string;
148
+ };
149
+ readonly answer: null;
150
+ };
151
+ readonly 'authored.remember': {
152
+ readonly asked: {
153
+ readonly name: AuthoredFact;
154
+ readonly value: string;
155
+ };
156
+ readonly answer: null;
157
+ };
158
+ readonly 'authored.claim': {
159
+ readonly asked: {
160
+ readonly holder: string;
161
+ };
162
+ readonly answer: boolean;
163
+ };
164
+ readonly 'authored.end': {
165
+ readonly asked: {
166
+ readonly holder: string | null;
167
+ };
168
+ readonly answer: boolean;
169
+ };
170
+ }
171
+ export interface OpenedRequests {
172
+ readonly 'opened.find': {
173
+ readonly asked: {
174
+ readonly locator: string;
175
+ readonly path: string;
176
+ readonly revision: string;
177
+ };
178
+ readonly answer: OpenedFile | null;
179
+ };
180
+ readonly 'opened.begin': {
181
+ readonly asked: {
182
+ readonly key: string;
183
+ readonly locator: string;
184
+ readonly path: string;
185
+ readonly revision: string;
186
+ readonly mediaType: string;
187
+ };
188
+ readonly answer: boolean;
189
+ };
190
+ readonly 'opened.part': {
191
+ readonly asked: {
192
+ readonly key: string;
193
+ readonly part: number;
194
+ readonly bytes: Uint8Array;
195
+ };
196
+ readonly answer: boolean;
197
+ };
198
+ readonly 'opened.keep': {
199
+ readonly asked: {
200
+ readonly key: string;
201
+ readonly sha256: string;
202
+ readonly byteLength: number;
203
+ readonly parts: number;
204
+ };
205
+ readonly answer: boolean;
206
+ };
207
+ readonly 'opened.drop': {
208
+ readonly asked: {
209
+ readonly key: string;
210
+ };
211
+ readonly answer: null;
212
+ };
213
+ readonly 'opened.clean': {
214
+ readonly asked: Readonly<Record<never, never>>;
215
+ readonly answer: number;
216
+ };
217
+ readonly 'file.part': {
218
+ readonly asked: {
219
+ readonly key: string;
220
+ readonly part: number;
221
+ };
222
+ readonly answer: Uint8Array | null;
223
+ };
224
+ }
225
+ export interface RuntimeRequests extends AuthoredRequests, OpenedRequests {
226
+ }
227
+ export type RuntimeKind = keyof RuntimeRequests;
228
+ export type RuntimeRequest<K extends RuntimeKind = RuntimeKind> = {
229
+ readonly [Kind in K]: {
230
+ readonly kind: Kind;
231
+ } & RuntimeRequests[Kind]['asked'];
232
+ }[K];
233
+ export type RuntimeAnswer<K extends RuntimeKind> = RuntimeRequests[K]['answer'];
234
+ export interface RuntimeDatabase {
235
+ ask<K extends RuntimeKind>(request: RuntimeRequest<K>): Promise<RuntimeAnswer<K>>;
236
+ }
237
+ export declare function isRuntime(request: {
238
+ readonly kind: string;
239
+ }): request is RuntimeRequest;
240
+ export declare function readsRuntime(request: RuntimeRequest): boolean;
241
+ export declare function answerRuntime(connection: Connection, request: RuntimeRequest, spool: FileSpool): unknown;
242
+ //# sourceMappingURL=tables.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tables.d.ts","sourceRoot":"","sources":["../src/tables.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,KAAK,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,UAAU,GAAG,IAAI,CAAC;AAEjE,MAAM,MAAM,GAAG,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,CAAC;AAElD,MAAM,WAAW,UAAU;IACzB,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,C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CAAC,CAAC,CAAC;CACnF;AAkCD,wBAAgB,SAAS,CAAC,OAAO,EAAE;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,OAAO,IAAI,cAAc,CAEvF;AAED,wBAAgB,YAAY,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAE7D;AAmYD,wBAAgB,aAAa,CAC3B,UAAU,EAAE,UAAU,EACtB,OAAO,EAAE,cAAc,EACvB,KAAK,EAAE,SAAS,GACf,OAAO,CAqDT"}
package/dist/tables.js ADDED
@@ -0,0 +1,406 @@
1
+ export const FILE_PART_BYTES = 1_048_576;
2
+ const TABLES = [
3
+ ['authored_facts', 'name TEXT PRIMARY KEY, value TEXT NOT NULL'],
4
+ ['authored_queues', 'locator TEXT PRIMARY KEY, ids TEXT NOT NULL'],
5
+ [
6
+ 'authored_pending',
7
+ 'locator TEXT NOT NULL, id TEXT NOT NULL, batch TEXT NOT NULL, state TEXT NOT NULL, files TEXT NOT NULL, PRIMARY KEY (locator, id)',
8
+ ],
9
+ [
10
+ 'authored_room_drafts',
11
+ 'locator TEXT NOT NULL, path TEXT NOT NULL, record TEXT NOT NULL, files TEXT NOT NULL, PRIMARY KEY (locator, path)',
12
+ ],
13
+ ['authored_chat_drafts', 'conversation TEXT PRIMARY KEY, record TEXT NOT NULL'],
14
+ ['authored_calls', 'operation TEXT PRIMARY KEY, record TEXT NOT NULL, files TEXT NOT NULL'],
15
+ ['authored_fences', 'locator TEXT PRIMARY KEY'],
16
+ ];
17
+ export const RUNTIME_TABLES = [
18
+ ...TABLES.map(([name, columns]) => `CREATE TABLE ${name} (${columns}) WITHOUT ROWID`),
19
+ 'CREATE TABLE file_parts (key TEXT NOT NULL, part INTEGER NOT NULL, bytes BLOB NOT NULL, PRIMARY KEY (key, part))',
20
+ 'CREATE TABLE opened_files (key TEXT PRIMARY KEY, locator TEXT NOT NULL, path TEXT NOT NULL, revision TEXT NOT NULL, media_type TEXT NOT NULL, sha256 TEXT, byte_length INTEGER) WITHOUT ROWID',
21
+ 'CREATE INDEX opened_files_name ON opened_files (locator, path, revision)',
22
+ ];
23
+ const OWNERS = ['authored_pending', 'authored_room_drafts', 'authored_calls'];
24
+ const AUTHORED_KEYS = OWNERS.map((table) => `SELECT value FROM ${table}, json_each(${table}.files)`).join(' UNION ');
25
+ const UNOWNED_PARTS = `DELETE FROM file_parts WHERE key NOT IN (${AUTHORED_KEYS} UNION SELECT key FROM opened_files)`;
26
+ const READS = new Set([
27
+ 'authored.queues',
28
+ 'authored.roomDrafts',
29
+ 'authored.chatDrafts',
30
+ 'authored.calls',
31
+ 'authored.fences',
32
+ 'authored.fact',
33
+ 'authored.file',
34
+ 'opened.find',
35
+ 'file.part',
36
+ ]);
37
+ const KINDS = new Set([
38
+ ...READS,
39
+ 'authored.keep',
40
+ 'authored.amend',
41
+ 'authored.roomDraft',
42
+ 'authored.chatDraft',
43
+ 'authored.call',
44
+ 'authored.fence',
45
+ 'authored.unfence',
46
+ 'authored.discard',
47
+ 'authored.remember',
48
+ 'authored.claim',
49
+ 'authored.end',
50
+ 'opened.begin',
51
+ 'opened.part',
52
+ 'opened.keep',
53
+ 'opened.drop',
54
+ 'opened.clean',
55
+ ]);
56
+ export function isRuntime(request) {
57
+ return KINDS.has(request.kind);
58
+ }
59
+ export function readsRuntime(request) {
60
+ return READS.has(request.kind);
61
+ }
62
+ function text(value) {
63
+ return typeof value === 'string' ? value : String(value);
64
+ }
65
+ function texts(rows, column) {
66
+ return rows.map((row) => text(row[column]));
67
+ }
68
+ function owned(files) {
69
+ return JSON.stringify([...new Set(files)]);
70
+ }
71
+ function fact(connection, name) {
72
+ const [held] = connection.rows('SELECT value FROM authored_facts WHERE name = ?', [name]);
73
+ return held === undefined ? null : text(held.value);
74
+ }
75
+ function remember(connection, name, value) {
76
+ connection.run('INSERT OR REPLACE INTO authored_facts (name, value) VALUES (?, ?)', [
77
+ name,
78
+ value,
79
+ ]);
80
+ return null;
81
+ }
82
+ function fenced(connection, locator) {
83
+ return connection.rows('SELECT 1 FROM authored_fences WHERE locator = ?', [locator]).length > 0;
84
+ }
85
+ function stored(connection, key) {
86
+ return connection.rows('SELECT 1 FROM file_parts WHERE key = ? LIMIT 1', [key]).length > 0;
87
+ }
88
+ function storedPart(connection, key, part, bytes) {
89
+ connection.run('INSERT INTO file_parts (key, part, bytes) VALUES (?, ?, ?)', [key, part, bytes]);
90
+ }
91
+ async function storedFiles(connection, files) {
92
+ for (const { key, blob } of files) {
93
+ if (stored(connection, key))
94
+ continue;
95
+ for (let at = 0, part = 0; at < blob.size; at += FILE_PART_BYTES, part += 1) {
96
+ const bytes = new Uint8Array(await blob.slice(at, at + FILE_PART_BYTES).arrayBuffer());
97
+ storedPart(connection, key, part, bytes);
98
+ }
99
+ }
100
+ }
101
+ function filePart(connection, key, part) {
102
+ const [held] = connection.rows('SELECT bytes FROM file_parts WHERE key = ? AND part = ?', [
103
+ key,
104
+ part,
105
+ ]);
106
+ return held?.bytes instanceof Uint8Array ? held.bytes : null;
107
+ }
108
+ function* fileParts(connection, key) {
109
+ for (let part = 0;; part += 1) {
110
+ const bytes = filePart(connection, key, part);
111
+ if (bytes === null)
112
+ return;
113
+ yield bytes;
114
+ }
115
+ }
116
+ async function spooled(connection, spool, { key, type }) {
117
+ for (const held of [...spool.held()]) {
118
+ if (held !== key && !stored(connection, held))
119
+ await spool.drop(held);
120
+ }
121
+ return spool.spool(key, type, fileParts(connection, key));
122
+ }
123
+ function unowned(connection) {
124
+ connection.run(UNOWNED_PARTS);
125
+ }
126
+ function queued(connection) {
127
+ return {
128
+ queues: connection.rows('SELECT locator, ids FROM authored_queues').map((row) => ({
129
+ locator: text(row.locator),
130
+ ids: JSON.parse(text(row.ids)),
131
+ })),
132
+ pending: connection
133
+ .rows('SELECT locator, id, batch, state FROM authored_pending')
134
+ .map((row) => ({
135
+ locator: text(row.locator),
136
+ id: text(row.id),
137
+ batch: text(row.batch),
138
+ state: text(row.state),
139
+ })),
140
+ };
141
+ }
142
+ function heldOrder(connection, locator) {
143
+ const [held] = connection.rows('SELECT ids FROM authored_queues WHERE locator = ?', [locator]);
144
+ return held === undefined ? [] : JSON.parse(text(held.ids));
145
+ }
146
+ function keptRows(connection, { locator, order, added, founded, roomDrafts, consumedRoomDrafts }) {
147
+ for (const path of consumedRoomDrafts ?? []) {
148
+ connection.run('DELETE FROM authored_room_drafts WHERE locator = ? AND path = ?', [
149
+ locator,
150
+ path,
151
+ ]);
152
+ }
153
+ for (const { path, record, files } of roomDrafts ?? []) {
154
+ connection.run('INSERT OR REPLACE INTO authored_room_drafts (locator, path, record, files) VALUES (?, ?, ?, ?)', [locator, path, record, owned(files)]);
155
+ }
156
+ if (order === undefined)
157
+ return;
158
+ const before = new Set(heldOrder(connection, locator));
159
+ const after = new Set(order);
160
+ if (after.size === 0)
161
+ connection.run('DELETE FROM authored_queues WHERE locator = ?', [locator]);
162
+ else {
163
+ connection.run('INSERT OR REPLACE INTO authored_queues (locator, ids) VALUES (?, ?)', [
164
+ locator,
165
+ JSON.stringify([...after]),
166
+ ]);
167
+ }
168
+ for (const id of before) {
169
+ if (!after.has(id)) {
170
+ connection.run('DELETE FROM authored_pending WHERE locator = ? AND id = ?', [locator, id]);
171
+ }
172
+ }
173
+ for (const { id, batch, state, files } of added ?? []) {
174
+ if (before.has(id) || !after.has(id))
175
+ continue;
176
+ connection.run('INSERT OR REPLACE INTO authored_pending (locator, id, batch, state, files) VALUES (?, ?, ?, ?, ?)', [locator, id, batch, state, owned(files)]);
177
+ }
178
+ for (const { id, batch, files } of founded ?? []) {
179
+ if (!before.has(id))
180
+ continue;
181
+ connection.run('UPDATE authored_pending SET batch = ?, files = ? WHERE locator = ? AND id = ?', [batch, owned(files), locator, id]);
182
+ }
183
+ }
184
+ async function kept(connection, { writes, files, whole }) {
185
+ const refused = writes.find(({ locator }) => fenced(connection, locator));
186
+ if (refused !== undefined && whole)
187
+ return refused.locator;
188
+ await storedFiles(connection, files);
189
+ for (const write of writes) {
190
+ if (!fenced(connection, write.locator))
191
+ keptRows(connection, write);
192
+ }
193
+ unowned(connection);
194
+ return refused?.locator ?? null;
195
+ }
196
+ function amended(connection, { locator, id, amendment }) {
197
+ if (fenced(connection, locator))
198
+ return null;
199
+ const [held] = connection.rows('SELECT state FROM authored_pending WHERE locator = ? AND id = ?', [locator, id]);
200
+ if (held === undefined)
201
+ return null;
202
+ connection.run('UPDATE authored_pending SET state = ? WHERE locator = ? AND id = ?', [
203
+ JSON.stringify({
204
+ ...JSON.parse(text(held.state)),
205
+ ...JSON.parse(amendment),
206
+ }),
207
+ locator,
208
+ id,
209
+ ]);
210
+ return null;
211
+ }
212
+ async function roomDraft(connection, { locator, path, kept: draft, files }) {
213
+ if (fenced(connection, locator))
214
+ return locator;
215
+ await storedFiles(connection, files);
216
+ if (draft === null) {
217
+ connection.run('DELETE FROM authored_room_drafts WHERE locator = ? AND path = ?', [
218
+ locator,
219
+ path,
220
+ ]);
221
+ }
222
+ else {
223
+ connection.run('INSERT OR REPLACE INTO authored_room_drafts (locator, path, record, files) VALUES (?, ?, ?, ?)', [locator, path, draft.record, owned(draft.files)]);
224
+ }
225
+ unowned(connection);
226
+ return null;
227
+ }
228
+ function chatDraft(connection, { kept: drafts, dropped }) {
229
+ for (const conversation of dropped) {
230
+ connection.run('DELETE FROM authored_chat_drafts WHERE conversation = ?', [conversation]);
231
+ }
232
+ for (const { conversation, record } of drafts) {
233
+ connection.run('INSERT OR REPLACE INTO authored_chat_drafts (conversation, record) VALUES (?, ?)', [conversation, record]);
234
+ }
235
+ return null;
236
+ }
237
+ async function call(connection, { operation, kept: held, files }) {
238
+ await storedFiles(connection, files);
239
+ if (held === null)
240
+ connection.run('DELETE FROM authored_calls WHERE operation = ?', [operation]);
241
+ else {
242
+ connection.run('INSERT OR REPLACE INTO authored_calls (operation, record, files) VALUES (?, ?, ?)', [operation, held.record, owned(held.files)]);
243
+ }
244
+ unowned(connection);
245
+ return null;
246
+ }
247
+ function roomWork(connection, locator) {
248
+ connection.run('DELETE FROM authored_queues WHERE locator = ?', [locator]);
249
+ connection.run('DELETE FROM authored_pending WHERE locator = ?', [locator]);
250
+ }
251
+ function fence(connection, locators) {
252
+ for (const locator of locators) {
253
+ connection.run('INSERT OR REPLACE INTO authored_fences (locator) VALUES (?)', [locator]);
254
+ roomWork(connection, locator);
255
+ connection.run('DELETE FROM authored_room_drafts WHERE locator = ?', [locator]);
256
+ connection.run('DELETE FROM opened_files WHERE locator = ?', [locator]);
257
+ }
258
+ unowned(connection);
259
+ return null;
260
+ }
261
+ function unfence(connection, locators) {
262
+ for (const locator of locators) {
263
+ connection.run('DELETE FROM authored_fences WHERE locator = ?', [locator]);
264
+ }
265
+ return null;
266
+ }
267
+ function discard(connection, locator) {
268
+ roomWork(connection, locator);
269
+ unowned(connection);
270
+ return null;
271
+ }
272
+ function claim(connection, holder) {
273
+ const held = fact(connection, 'holder');
274
+ if (held !== null)
275
+ return held === holder;
276
+ remember(connection, 'holder', holder);
277
+ return true;
278
+ }
279
+ function end(connection, holder) {
280
+ if (holder !== null && claim(connection, holder))
281
+ return true;
282
+ for (const [table] of TABLES)
283
+ connection.run(`DELETE FROM ${table}`);
284
+ connection.run('DELETE FROM opened_files');
285
+ connection.run('DELETE FROM file_parts');
286
+ return false;
287
+ }
288
+ function count(value) {
289
+ return typeof value === 'bigint' ? Number(value) : typeof value === 'number' ? value : 0;
290
+ }
291
+ function openedFile(connection, { locator, path, revision }) {
292
+ const [held] = connection.rows('SELECT key, media_type, byte_length FROM opened_files WHERE locator = ? AND path = ? AND revision = ? AND byte_length IS NOT NULL', [locator, path, revision]);
293
+ return held === undefined
294
+ ? null
295
+ : {
296
+ key: text(held.key),
297
+ byteLength: count(held.byte_length),
298
+ mediaType: text(held.media_type),
299
+ };
300
+ }
301
+ function opening(connection, { key, locator, path, revision, mediaType }) {
302
+ if (fenced(connection, locator))
303
+ return false;
304
+ connection.run('DELETE FROM opened_files WHERE locator = ? AND path = ? AND revision = ? AND byte_length IS NULL', [locator, path, revision]);
305
+ unowned(connection);
306
+ connection.run('INSERT INTO opened_files (key, locator, path, revision, media_type) VALUES (?, ?, ?, ?, ?)', [key, locator, path, revision, mediaType]);
307
+ return true;
308
+ }
309
+ function fetching(connection, key) {
310
+ const [held] = connection.rows('SELECT locator, path, revision FROM opened_files WHERE key = ? AND byte_length IS NULL', [key]);
311
+ return held === undefined
312
+ ? null
313
+ : { locator: text(held.locator), path: text(held.path), revision: text(held.revision) };
314
+ }
315
+ function openedPart(connection, { key, part, bytes }) {
316
+ if (fetching(connection, key) === null)
317
+ return false;
318
+ storedPart(connection, key, part, bytes);
319
+ return true;
320
+ }
321
+ function dropped(connection, key) {
322
+ connection.run('DELETE FROM opened_files WHERE key = ?', [key]);
323
+ connection.run('DELETE FROM file_parts WHERE key = ?', [key]);
324
+ return null;
325
+ }
326
+ function keptOpened(connection, { key, sha256, byteLength, parts }) {
327
+ const fetched = fetching(connection, key);
328
+ if (fetched === null)
329
+ return false;
330
+ const [held] = connection.rows('SELECT count(*) AS parts, min(part) AS first, max(part) AS last, coalesce(sum(length(bytes)), 0) AS bytes FROM file_parts WHERE key = ?', [key]);
331
+ const whole = count(held?.parts) === parts &&
332
+ count(held?.bytes) === byteLength &&
333
+ (parts === 0 || (count(held?.first) === 0 && count(held?.last) === parts - 1));
334
+ if (!whole) {
335
+ dropped(connection, key);
336
+ return false;
337
+ }
338
+ connection.run('UPDATE opened_files SET sha256 = ?, byte_length = ? WHERE key = ?', [
339
+ sha256,
340
+ byteLength,
341
+ key,
342
+ ]);
343
+ connection.run('DELETE FROM opened_files WHERE locator = ? AND path = ? AND revision = ? AND key <> ?', [fetched.locator, fetched.path, fetched.revision, key]);
344
+ unowned(connection);
345
+ return true;
346
+ }
347
+ function cleaned(connection) {
348
+ connection.run('DELETE FROM opened_files');
349
+ const [freed] = connection.rows(`SELECT coalesce(sum(length(bytes)), 0) AS bytes FROM file_parts WHERE key NOT IN (${AUTHORED_KEYS})`);
350
+ unowned(connection);
351
+ return count(freed?.bytes);
352
+ }
353
+ export function answerRuntime(connection, request, spool) {
354
+ switch (request.kind) {
355
+ case 'authored.queues':
356
+ return queued(connection);
357
+ case 'authored.roomDrafts':
358
+ return texts(connection.rows('SELECT record FROM authored_room_drafts'), 'record');
359
+ case 'authored.chatDrafts':
360
+ return texts(connection.rows('SELECT record FROM authored_chat_drafts'), 'record');
361
+ case 'authored.calls':
362
+ return texts(connection.rows('SELECT record FROM authored_calls'), 'record');
363
+ case 'authored.fences':
364
+ return texts(connection.rows('SELECT locator FROM authored_fences'), 'locator');
365
+ case 'authored.fact':
366
+ return fact(connection, request.name);
367
+ case 'authored.file':
368
+ return spooled(connection, spool, request);
369
+ case 'authored.keep':
370
+ return kept(connection, request);
371
+ case 'authored.amend':
372
+ return amended(connection, request);
373
+ case 'authored.roomDraft':
374
+ return roomDraft(connection, request);
375
+ case 'authored.chatDraft':
376
+ return chatDraft(connection, request);
377
+ case 'authored.call':
378
+ return call(connection, request);
379
+ case 'authored.fence':
380
+ return fence(connection, request.locators);
381
+ case 'authored.unfence':
382
+ return unfence(connection, request.locators);
383
+ case 'authored.discard':
384
+ return discard(connection, request.locator);
385
+ case 'authored.remember':
386
+ return remember(connection, request.name, request.value);
387
+ case 'authored.claim':
388
+ return claim(connection, request.holder);
389
+ case 'authored.end':
390
+ return end(connection, request.holder);
391
+ case 'opened.find':
392
+ return openedFile(connection, request);
393
+ case 'opened.begin':
394
+ return opening(connection, request);
395
+ case 'opened.part':
396
+ return openedPart(connection, request);
397
+ case 'opened.keep':
398
+ return keptOpened(connection, request);
399
+ case 'opened.drop':
400
+ return dropped(connection, request.key);
401
+ case 'opened.clean':
402
+ return cleaned(connection);
403
+ case 'file.part':
404
+ return filePart(connection, request.key, request.part);
405
+ }
406
+ }
package/dist/turn.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ export declare function yielded(): Promise<void>;
2
+ export declare const ROW_WEIGHT = 256;
3
+ export type Later<T> = T | Promise<T>;
4
+ export declare function after<T, R>(value: Later<T>, next: (value: T) => Later<R>): Later<R>;
5
+ export declare function inUnits<T>(items: Iterable<T>, each: (item: T) => number, ended?: () => void, paused?: () => Promise<void>): Later<void>;
6
+ export declare function finished<R>(steps: Generator<number, R, void>): R;
7
+ export declare function stepped<R>(steps: Generator<number, R, void>): Later<R>;
8
+ export declare function inChunks<T>(items: Iterable<T>, weigh: (item: T) => number, each: (chunk: readonly T[]) => Later<void>): Promise<void>;
9
+ //# sourceMappingURL=turn.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"turn.d.ts","sourceRoot":"","sources":["../src/turn.ts"],"names":[],"mappings":"AAAA,wBAAgB,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CASvC;AAID,eAAO,MAAM,UAAU,MAAM,CAAC;AAE9B,MAAM,MAAM,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AAEtC,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAEnF;AAqBD,wBAAgB,OAAO,CAAC,CAAC,EACvB,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,EAClB,IAAI,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,MAAM,EACzB,KAAK,GAAE,MAAM,IAAe,EAC5B,MAAM,GAAE,MAAM,OAAO,CAAC,IAAI,CAAW,GACpC,KAAK,CAAC,IAAI,CAAC,CAWb;AAED,wBAAgB,QAAQ,CAAC,CAAC,EAAE,KAAK,EAAE,SAAS,CAAC,MAAM,EAAE,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAIhE;AAED,wBAAgB,OAAO,CAAC,CAAC,EAAE,KAAK,EAAE,SAAS,CAAC,MAAM,EAAE,CAAC,EAAE,IAAI,CAAC,GAAG,KAA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