@agentshouse/mdruntime 0.1.0-c30ed2b8e04d5 → 0.1.0-c36e0a098a0e3

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 (198) hide show
  1. package/AGENTS.md +266 -183
  2. package/README.md +20 -9
  3. package/dist/acquisition.d.ts +14 -0
  4. package/dist/acquisition.d.ts.map +1 -0
  5. package/dist/acquisition.js +24 -0
  6. package/dist/adapter.d.ts +101 -19
  7. package/dist/adapter.d.ts.map +1 -1
  8. package/dist/attached-model.d.ts.map +1 -1
  9. package/dist/attached-model.js +15 -2
  10. package/dist/attempt.d.ts +16 -0
  11. package/dist/attempt.d.ts.map +1 -0
  12. package/dist/attempt.js +70 -0
  13. package/dist/binary.d.ts +23 -0
  14. package/dist/binary.d.ts.map +1 -0
  15. package/dist/binary.js +8 -0
  16. package/dist/bootstrap.d.ts +17 -5
  17. package/dist/bootstrap.d.ts.map +1 -1
  18. package/dist/bootstrap.js +110 -37
  19. package/dist/change-set.d.ts +2 -0
  20. package/dist/change-set.d.ts.map +1 -1
  21. package/dist/change-set.js +48 -16
  22. package/dist/change.d.ts +15 -0
  23. package/dist/change.d.ts.map +1 -1
  24. package/dist/change.js +26 -1
  25. package/dist/chat/acts.d.ts +33 -0
  26. package/dist/chat/acts.d.ts.map +1 -0
  27. package/dist/chat/acts.js +36 -0
  28. package/dist/chat/context.d.ts +3 -0
  29. package/dist/chat/context.d.ts.map +1 -0
  30. package/dist/chat/context.js +10 -0
  31. package/dist/chat/input.d.ts +9 -0
  32. package/dist/chat/input.d.ts.map +1 -0
  33. package/dist/chat/input.js +99 -0
  34. package/dist/chat/kit.d.ts +60 -0
  35. package/dist/chat/kit.d.ts.map +1 -0
  36. package/dist/chat/kit.js +123 -0
  37. package/dist/chat/lasting.d.ts +6 -0
  38. package/dist/chat/lasting.d.ts.map +1 -0
  39. package/dist/chat/lasting.js +19 -0
  40. package/dist/chat/list.d.ts +26 -0
  41. package/dist/chat/list.d.ts.map +1 -0
  42. package/dist/chat/list.js +62 -0
  43. package/dist/chat/markdown.d.ts +20 -0
  44. package/dist/chat/markdown.d.ts.map +1 -0
  45. package/dist/chat/markdown.js +188 -0
  46. package/dist/chat/message.d.ts +6 -0
  47. package/dist/chat/message.d.ts.map +1 -0
  48. package/dist/chat/message.js +192 -0
  49. package/dist/chat/messages.d.ts +21 -0
  50. package/dist/chat/messages.d.ts.map +1 -0
  51. package/dist/chat/messages.js +151 -0
  52. package/dist/chat/name.d.ts +6 -0
  53. package/dist/chat/name.d.ts.map +1 -0
  54. package/dist/chat/name.js +37 -0
  55. package/dist/chat/opened.d.ts +11 -0
  56. package/dist/chat/opened.d.ts.map +1 -0
  57. package/dist/chat/opened.js +17 -0
  58. package/dist/chat/overlay.d.ts +31 -0
  59. package/dist/chat/overlay.d.ts.map +1 -0
  60. package/dist/chat/overlay.js +97 -0
  61. package/dist/chat/parts.d.ts +57 -0
  62. package/dist/chat/parts.d.ts.map +1 -0
  63. package/dist/chat/parts.js +119 -0
  64. package/dist/chat/process.d.ts +20 -0
  65. package/dist/chat/process.d.ts.map +1 -0
  66. package/dist/chat/process.js +32 -0
  67. package/dist/chat/provider.d.ts +63 -0
  68. package/dist/chat/provider.d.ts.map +1 -0
  69. package/dist/chat/provider.js +620 -0
  70. package/dist/chat/question.d.ts +9 -0
  71. package/dist/chat/question.d.ts.map +1 -0
  72. package/dist/chat/question.js +80 -0
  73. package/dist/chat/questions.d.ts +31 -0
  74. package/dist/chat/questions.d.ts.map +1 -0
  75. package/dist/chat/questions.js +111 -0
  76. package/dist/chat/read.d.ts +5 -0
  77. package/dist/chat/read.d.ts.map +1 -0
  78. package/dist/chat/read.js +13 -0
  79. package/dist/chat/ready.d.ts +33 -0
  80. package/dist/chat/ready.d.ts.map +1 -0
  81. package/dist/chat/ready.js +91 -0
  82. package/dist/chat/style.d.ts +4 -0
  83. package/dist/chat/style.d.ts.map +1 -0
  84. package/dist/chat/style.js +302 -0
  85. package/dist/chat.d.ts +14 -0
  86. package/dist/chat.d.ts.map +1 -0
  87. package/dist/chat.js +8 -0
  88. package/dist/client.d.ts +27 -14
  89. package/dist/client.d.ts.map +1 -1
  90. package/dist/client.js +68 -16
  91. package/dist/collection.d.ts +21 -12
  92. package/dist/collection.d.ts.map +1 -1
  93. package/dist/collection.js +271 -23
  94. package/dist/connection.d.ts +45 -17
  95. package/dist/connection.d.ts.map +1 -1
  96. package/dist/connection.js +1669 -397
  97. package/dist/conversation.d.ts +39 -14
  98. package/dist/conversation.d.ts.map +1 -1
  99. package/dist/conversation.js +468 -208
  100. package/dist/durability.d.ts +7 -1
  101. package/dist/durability.d.ts.map +1 -1
  102. package/dist/entry.d.ts +17 -0
  103. package/dist/entry.d.ts.map +1 -0
  104. package/dist/entry.js +156 -0
  105. package/dist/expectation.d.ts +21 -0
  106. package/dist/expectation.d.ts.map +1 -0
  107. package/dist/expectation.js +70 -0
  108. package/dist/failure.d.ts +13 -0
  109. package/dist/failure.d.ts.map +1 -0
  110. package/dist/failure.js +19 -0
  111. package/dist/fence.d.ts +6 -0
  112. package/dist/fence.d.ts.map +1 -0
  113. package/dist/fence.js +10 -0
  114. package/dist/host.d.ts +13 -2
  115. package/dist/host.d.ts.map +1 -1
  116. package/dist/host.js +51 -17
  117. package/dist/identity.d.ts +5 -0
  118. package/dist/identity.d.ts.map +1 -1
  119. package/dist/identity.js +19 -1
  120. package/dist/index.d.ts +14 -7
  121. package/dist/index.d.ts.map +1 -1
  122. package/dist/index.js +4 -0
  123. package/dist/memory.d.ts +7 -3
  124. package/dist/memory.d.ts.map +1 -1
  125. package/dist/memory.js +89 -23
  126. package/dist/model.d.ts +14 -8
  127. package/dist/model.d.ts.map +1 -1
  128. package/dist/model.js +15 -3
  129. package/dist/observation.d.ts +5 -2
  130. package/dist/observation.d.ts.map +1 -1
  131. package/dist/observation.js +22 -11
  132. package/dist/open.d.ts +2 -0
  133. package/dist/open.d.ts.map +1 -0
  134. package/dist/open.js +1 -0
  135. package/dist/outcome.d.ts +32 -3
  136. package/dist/outcome.d.ts.map +1 -1
  137. package/dist/outcome.js +42 -1
  138. package/dist/overlay.d.ts +15 -3
  139. package/dist/overlay.d.ts.map +1 -1
  140. package/dist/overlay.js +94 -77
  141. package/dist/paths.d.ts +0 -1
  142. package/dist/paths.d.ts.map +1 -1
  143. package/dist/paths.js +0 -16
  144. package/dist/query.d.ts +33 -4
  145. package/dist/query.d.ts.map +1 -1
  146. package/dist/query.js +74 -33
  147. package/dist/reach.d.ts +8 -2
  148. package/dist/reach.d.ts.map +1 -1
  149. package/dist/reach.js +68 -39
  150. package/dist/react.d.ts +43 -0
  151. package/dist/react.d.ts.map +1 -0
  152. package/dist/react.js +64 -0
  153. package/dist/read-side.d.ts +13 -6
  154. package/dist/read-side.d.ts.map +1 -1
  155. package/dist/read-side.js +391 -129
  156. package/dist/readiness.d.ts +53 -0
  157. package/dist/readiness.d.ts.map +1 -0
  158. package/dist/readiness.js +33 -0
  159. package/dist/recovery.d.ts +25 -0
  160. package/dist/recovery.d.ts.map +1 -0
  161. package/dist/recovery.js +197 -0
  162. package/dist/refresh.d.ts +10 -0
  163. package/dist/refresh.d.ts.map +1 -1
  164. package/dist/refresh.js +43 -38
  165. package/dist/replica.d.ts +9 -6
  166. package/dist/replica.d.ts.map +1 -1
  167. package/dist/replica.js +116 -68
  168. package/dist/room-draft.d.ts +26 -9
  169. package/dist/room-draft.d.ts.map +1 -1
  170. package/dist/room-draft.js +132 -23
  171. package/dist/rows.d.ts +10 -14
  172. package/dist/rows.d.ts.map +1 -1
  173. package/dist/rows.js +85 -50
  174. package/dist/save.d.ts +32 -1
  175. package/dist/save.d.ts.map +1 -1
  176. package/dist/save.js +464 -89
  177. package/dist/search.d.ts +31 -8
  178. package/dist/search.d.ts.map +1 -1
  179. package/dist/search.js +108 -45
  180. package/dist/shared.d.ts +11 -0
  181. package/dist/shared.d.ts.map +1 -0
  182. package/dist/shared.js +368 -0
  183. package/dist/status.d.ts +6 -2
  184. package/dist/status.d.ts.map +1 -1
  185. package/dist/status.js +0 -8
  186. package/dist/store.d.ts +74 -17
  187. package/dist/store.d.ts.map +1 -1
  188. package/dist/store.js +531 -198
  189. package/dist/turn.d.ts +7 -0
  190. package/dist/turn.d.ts.map +1 -0
  191. package/dist/turn.js +45 -0
  192. package/dist/wire.d.ts +142 -33
  193. package/dist/wire.d.ts.map +1 -1
  194. package/dist/wire.js +62 -31
  195. package/package.json +31 -7
  196. package/dist/attachment.d.ts +0 -3
  197. package/dist/attachment.d.ts.map +0 -1
  198. package/dist/attachment.js +0 -8
package/dist/query.js CHANGED
@@ -1,7 +1,12 @@
1
+ import { pointerFacts, } from '@agentshouse/mdmodel';
2
+ import { referenceField } from './model.js';
1
3
  import { normalize } from './paths.js';
2
4
  export class QueryError extends Error {
5
+ code;
6
+ retryable = false;
3
7
  constructor(code, at, problem) {
4
8
  super(`${code} at ${at}: ${problem}`);
9
+ this.code = code;
5
10
  }
6
11
  }
7
12
  const RANGE = ['$gt', '$gte', '$lt', '$lte'];
@@ -11,6 +16,7 @@ const RELATED = ['types', 'where', 'pathPrefix', 'field'];
11
16
  const PARTS = ['fields', 'sections', 'preamble', 'markdown', 'backlinks', 'change'];
12
17
  const ORDER = ['by', 'direction', 'nulls', 'stringSort', 'locale', 'localeOptions'];
13
18
  const CALENDAR_DATE = /^(\d{4})-(\d{2})-(\d{2})$/;
19
+ const STRAY_ESCAPE = /~(?![01])/u;
14
20
  export function invalid(at, problem) {
15
21
  throw new QueryError('invalid-query', at, problem);
16
22
  }
@@ -68,23 +74,29 @@ function calendarDate(value) {
68
74
  const date = new Date(Date.UTC(year, month - 1, day));
69
75
  return (date.getUTCFullYear() === year && date.getUTCMonth() === month - 1 && date.getUTCDate() === day);
70
76
  }
71
- function fits(spec, value) {
72
- const item = spec.kind === 'list' ? spec.of : spec;
73
- if (item.kind === 'number')
77
+ function itemOf(facts) {
78
+ return facts.type === 'array' ? facts.items : facts;
79
+ }
80
+ function fits(facts, value) {
81
+ const item = itemOf(facts);
82
+ if (item?.type === undefined)
83
+ return true;
84
+ if (item.values !== undefined)
85
+ return item.values.includes(value);
86
+ if (item.type === 'number')
74
87
  return typeof value === 'number';
75
- if (typeof value !== 'string')
88
+ if (item.type === 'integer')
89
+ return Number.isSafeInteger(value);
90
+ if (item.type === 'boolean')
91
+ return typeof value === 'boolean';
92
+ if (item.type !== 'string' || typeof value !== 'string')
76
93
  return false;
77
- if (item.kind === 'enum')
78
- return item.values.includes(value);
79
- if (item.kind === 'date')
80
- return calendarDate(value);
81
- if (item.kind === 'reference')
94
+ if (item.reference !== undefined)
82
95
  return value !== '' && normalize(value) === value;
96
+ if (item.format === 'date')
97
+ return calendarDate(value);
83
98
  return true;
84
99
  }
85
- function reference(spec) {
86
- return spec.kind === 'reference' || (spec.kind === 'list' && spec.of.kind === 'reference');
87
- }
88
100
  function typing(facts, rooms, types, at) {
89
101
  const read = rooms === null ? facts.rooms : facts.rooms.filter((room) => rooms.includes(room.locator));
90
102
  const acquiring = !facts.listed &&
@@ -102,22 +114,46 @@ function typing(facts, rooms, types, at) {
102
114
  types: types === null ? declared : declared.filter((type) => types.includes(type.name)),
103
115
  };
104
116
  }
105
- function specs(typed, field, at) {
106
- const declared = typed.types.flatMap((type) => Object.hasOwn(type.fields, field) ? [type.fields[field]] : []);
117
+ function pointer(input, at) {
118
+ if (typeof input !== 'string' || !input.startsWith('/') || STRAY_ESCAPE.test(input)) {
119
+ invalid(at, 'expects a JSON Pointer');
120
+ }
121
+ return input;
122
+ }
123
+ function specs(typed, input, at) {
124
+ const field = pointer(input, at);
125
+ const declared = typed.types.flatMap((type) => {
126
+ const spec = pointerFacts(type, field);
127
+ return spec === undefined ? [] : [spec];
128
+ });
107
129
  if (declared.length === 0 && !typed.untyped) {
108
130
  throw new QueryError('unknown-field', at, `no MD Type it reads declares ${field}`);
109
131
  }
110
- return declared;
132
+ if (!declared.some((spec) => spec.conditional))
133
+ return declared;
134
+ return declared.map((spec) => ({
135
+ facts: { prose: false },
136
+ through: several(spec),
137
+ conditional: true,
138
+ }));
139
+ }
140
+ function several(spec) {
141
+ return spec.through || spec.facts.type === 'array';
142
+ }
143
+ function single(spec) {
144
+ return spec.facts.type !== undefined && spec.facts.type !== 'array';
145
+ }
146
+ function every(declared, holds) {
147
+ return declared.length > 0 && declared.every(holds);
111
148
  }
112
149
  function kind(declared, checked, at) {
113
- if (checked === null || declared.length === 0 || declared.some((spec) => fits(spec, checked))) {
150
+ if (checked === null ||
151
+ declared.length === 0 ||
152
+ declared.some((spec) => fits(spec.facts, checked))) {
114
153
  return;
115
154
  }
116
155
  throw new QueryError('wrong-kind', at, `${JSON.stringify(checked)} is not of its field's kind`);
117
156
  }
118
- function listed(declared, list) {
119
- return declared.length > 0 && declared.every((spec) => (spec.kind === 'list') === list);
120
- }
121
157
  function test(declared, input, at) {
122
158
  if (!plain(input)) {
123
159
  const equal = value(input, at);
@@ -131,8 +167,11 @@ function test(declared, input, at) {
131
167
  const stray = operators.find((operator) => !RANGE.includes(operator));
132
168
  if (stray !== undefined)
133
169
  invalid(place(at, stray), 'cannot join a range');
134
- if (listed(declared, true))
170
+ if (every(declared, (spec) => spec.facts.type === 'array'))
135
171
  invalid(at, 'a range on a list');
172
+ if (every(declared, (spec) => spec.facts.type === 'boolean' || spec.facts.type === 'object')) {
173
+ throw new QueryError('wrong-kind', at, 'its field does not compare in order');
174
+ }
136
175
  return {
137
176
  op: 'range',
138
177
  bounds: RANGE.filter((operator) => operators.includes(operator)).map((operator) => {
@@ -165,7 +204,7 @@ function test(declared, input, at) {
165
204
  return { op: 'nin', values: compared };
166
205
  if (compared.includes(null))
167
206
  invalid(where, 'a list holds no null');
168
- if (listed(declared, false))
207
+ if (every(declared, single))
169
208
  invalid(where, 'the field is not a list');
170
209
  return { op: 'all', values: compared };
171
210
  }
@@ -173,7 +212,7 @@ function test(declared, input, at) {
173
212
  if (!Number.isSafeInteger(operand) || operand < 0) {
174
213
  invalid(where, 'expects a nonnegative integer');
175
214
  }
176
- if (listed(declared, false))
215
+ if (every(declared, single))
177
216
  invalid(where, 'the field is not a list');
178
217
  return { op: 'size', size: operand };
179
218
  }
@@ -186,7 +225,7 @@ function condition(typed, input, at) {
186
225
  invalid(at, 'expects an object of field predicates');
187
226
  const conditions = [];
188
227
  for (const name of Object.keys(input).sort()) {
189
- const where = place(at, name);
228
+ const where = name.startsWith('/') ? `${at}${name}` : place(at, name);
190
229
  const entry = input[name];
191
230
  if (name === '$and' || name === '$or') {
192
231
  const nested = list(entry, where).map((item, index) => condition(typed, item, `${where}[${index}]`));
@@ -215,19 +254,19 @@ function scope(facts, rooms, input, at) {
215
254
  pathPrefix: input.pathPrefix === undefined ? null : directory(input.pathPrefix, place(at, 'pathPrefix')),
216
255
  };
217
256
  }
218
- function referenceField(typed, field, at) {
219
- if (typeof field !== 'string')
220
- invalid(at, 'expects a field name');
257
+ function referenceName(typed, field, at) {
221
258
  const declared = specs(typed, field, at);
222
- if (declared.length > 0 && !declared.some(reference))
259
+ if (declared.length > 0 &&
260
+ !declared.some((spec) => spec.conditional || referenceField(spec.facts))) {
223
261
  invalid(at, 'not a reference field');
262
+ }
224
263
  return field;
225
264
  }
226
265
  function hopOf(facts, rooms, root, input) {
227
266
  if (!plain(input))
228
267
  invalid('hop', 'expects an object');
229
268
  known(input, RELATED, 'hop');
230
- const field = referenceField(root, input.field, 'hop.field');
269
+ const field = referenceName(root, input.field, 'hop.field');
231
270
  const { types, where, pathPrefix } = scope(facts, rooms, input, 'hop');
232
271
  return { types, where, pathPrefix, field };
233
272
  }
@@ -236,14 +275,16 @@ function backlinksOf(facts, rooms, input) {
236
275
  invalid('backlinks', 'expects an object');
237
276
  known(input, RELATED, 'backlinks');
238
277
  const { typed, types, where, pathPrefix } = scope(facts, rooms, input, 'backlinks');
239
- const field = input.field === undefined ? null : referenceField(typed, input.field, 'backlinks.field');
278
+ const field = input.field === undefined ? null : referenceName(typed, input.field, 'backlinks.field');
240
279
  return { types, where, pathPrefix, field };
241
280
  }
242
281
  function keyPlan(typed, input, at, path) {
243
282
  if (input === 'room' || input === 'type' || (path && input === 'path'))
244
283
  return { of: input };
245
- if (plain(input) && typeof input.field === 'string' && Object.keys(input).length === 1) {
246
- specs(typed, input.field, place(at, 'field'));
284
+ if (plain(input) && Object.hasOwn(input, 'field') && Object.keys(input).length === 1) {
285
+ if (specs(typed, input.field, place(at, 'field')).some(several)) {
286
+ invalid(place(at, 'field'), 'a path that can hold several values');
287
+ }
247
288
  return { of: 'field', field: input.field };
248
289
  }
249
290
  return invalid(at, path ? 'expects room, path, type or a field' : 'expects room, type or a field');
@@ -301,10 +342,10 @@ function parts(typed, input, at, fields) {
301
342
  return 'all';
302
343
  const named = list(input, at);
303
344
  named.forEach((name, index) => {
304
- if (typeof name !== 'string')
305
- invalid(`${at}[${index}]`, 'expects a name');
306
345
  if (fields)
307
346
  specs(typed, name, `${at}[${index}]`);
347
+ else if (typeof name !== 'string')
348
+ invalid(`${at}[${index}]`, 'expects a name');
308
349
  });
309
350
  return named;
310
351
  }
package/dist/reach.d.ts CHANGED
@@ -1,5 +1,7 @@
1
- import type { MdRuntime, RuntimeTopic } from './connection.js';
1
+ import type { ReferrerScope } from './adapter.js';
2
+ import type { MdRuntime, ReadinessPort, RuntimeTopic } from './connection.js';
2
3
  import type { Plan, PlanReach, QueryResult } from './query.js';
4
+ import type { Readiness } from './readiness.js';
3
5
  import type { SearchResult } from './search.js';
4
6
  export type ReachedDocument = {
5
7
  readonly room: string;
@@ -18,12 +20,16 @@ export type RuntimeView = {
18
20
  };
19
21
  export type NarrowedView = RuntimeView & {
20
22
  narrow(reach: PortReach | null): void;
23
+ facts(port: ReadinessPort): void;
21
24
  };
22
25
  export type Reachable = RuntimeView & {
23
26
  plan(input: unknown, reach: PortReach | null): Plan;
24
27
  query(plan: Plan): Promise<QueryResult>;
25
28
  watchPlan(plan: Plan, deliver: (message: unknown) => void): () => void;
26
29
  search(input: unknown, reach: PortReach | null): Promise<SearchResult>;
30
+ searching(target: string): void;
31
+ watchReadiness(port: ReadinessPort, reach: PortReach | null, deliver: (readiness: Readiness) => void): () => void;
32
+ referrerScope(referrer: unknown): ReferrerScope | null;
27
33
  };
28
34
  export declare function holds(reach: PortReach | null, room: string): boolean;
29
35
  export declare function governs(reach: PortReach | null, room: {
@@ -36,5 +42,5 @@ export declare function governs(reach: PortReach | null, room: {
36
42
  readonly selection: boolean;
37
43
  };
38
44
  export declare function planReach(reach: PortReach | null): PlanReach | null;
39
- export declare function narrowed(runtime: Reachable, given: PortReach | null): NarrowedView;
45
+ export declare function narrowed(runtime: Reachable, given: PortReach | null, told: ReadinessPort): NarrowedView;
40
46
  //# sourceMappingURL=reach.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"reach.d.ts","sourceRoot":"","sources":["../src/reach.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAW,SAAS,EAAa,YAAY,EAAc,MAAM,iBAAiB,CAAC;AAM/F,OAAO,KAAK,EAAE,IAAI,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAI/D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAGhD,MAAM,MAAM,eAAe,GAAG;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAE/E,MAAM,MAAM,SAAS,GAAG;IACtB,QAAQ,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,CAAC;IAClC,QAAQ,CAAC,SAAS,EAAE,SAAS,eAAe,EAAE,CAAC;IAC/C,QAAQ,CAAC,aAAa,EAAE,OAAO,CAAC;CACjC,CAAC;AAEF,MAAM,MAAM,OAAO,GAAG,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,CAAC;AAEjD,MAAM,MAAM,KAAK,GAAG,CAClB,KAAK,EAAE,YAAY,EACnB,KAAK,EAAE,SAAS,OAAO,EAAE,EACzB,OAAO,EAAE,OAA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AAE,SAAS,GAAG,IAAI,GAAG,SAAS,GAAG,IAAI,CASnE;AA8ED,wBAAgB,QAAQ,CACtB,OAAO,EAAE,SAAS,EAClB,KAAK,EAAE,SAAS,GAAG,IAAI,EACvB,IAAI,EAAE,aAAa,GAClB,YAAY,CAuLd"}
package/dist/reach.js CHANGED
@@ -1,25 +1,26 @@
1
1
  import { unheldView } from './conversation.js';
2
2
  import { modelTypes, NO_MODEL } from './model.js';
3
3
  import { applied, observing } from './observation.js';
4
+ import { runtimeError } from './failure.js';
5
+ import { failure, undeclared, } from './outcome.js';
4
6
  import { beneath } from './paths.js';
5
7
  import { reconciled } from './read-side.js';
6
- import { moveTarget, readParts } from './room-draft.js';
8
+ import { moveTarget, readParts, staleWrite, } from './room-draft.js';
7
9
  import { documentKey } from './rows.js';
8
10
  const UNHELD_TYPES = { collection: modelTypes(NO_MODEL), typedSelection: null };
9
- const UNDECLARED = { kind: 'undeclared' };
10
- function undeclared() {
11
- return Promise.resolve(UNDECLARED);
11
+ function unconversed(name) {
12
+ return () => Promise.resolve(undeclared(name));
12
13
  }
13
14
  const NO_CONVERSATION = {
14
15
  list: () => Promise.resolve([]),
15
16
  read: (conversation) => Promise.resolve(unheldView(conversation)),
16
- create: undeclared,
17
- send: undeclared,
18
- answer: undeclared,
19
- interrupt: undeclared,
20
- kill: undeclared,
21
- cancel: undeclared,
22
- delete: undeclared,
17
+ create: unconversed('conversation.create'),
18
+ send: unconversed('conversation.send'),
19
+ answer: unconversed('conversation.answer'),
20
+ interrupt: unconversed('conversation.interrupt'),
21
+ kill: unconversed('conversation.kill'),
22
+ cancel: unconversed('conversation.cancel'),
23
+ delete: unconversed('conversation.delete'),
23
24
  };
24
25
  const NO_CHAT_DRAFT = {
25
26
  write: () => Promise.resolve(),
@@ -51,6 +52,20 @@ function covers(reach, room, path) {
51
52
  function conversing(reach) {
52
53
  return reach === null || reach.conversations;
53
54
  }
55
+ function refers(reach, scope) {
56
+ if (reach === null)
57
+ return true;
58
+ if (scope === null)
59
+ return false;
60
+ if (scope.kind === 'conversations')
61
+ return reach.conversations;
62
+ return scope.path === undefined
63
+ ? wholly(reach, scope.locator)
64
+ : covers(reach, scope.locator, scope.path);
65
+ }
66
+ function unreachedReference(fileRef) {
67
+ return { kind: 'absent', code: 'binary-file-absent', fields: { fileRef }, retryable: false };
68
+ }
54
69
  export function planReach(reach) {
55
70
  return reach === null
56
71
  ? null
@@ -62,22 +77,7 @@ export function planReach(reach) {
62
77
  };
63
78
  }
64
79
  function roomsWithin(reach, rooms) {
65
- if (reach === null)
66
- return rooms;
67
- return rooms
68
- .filter((room) => holds(reach, room.locator))
69
- .map((room) => {
70
- if (wholly(reach, room.locator))
71
- return room;
72
- const governed = governs(reach, room);
73
- return {
74
- ...room,
75
- model: governed.collection ? room.model : modelTypes(NO_MODEL),
76
- typedSelection: governed.selection ? room.typedSelection : null,
77
- roomVersion: null,
78
- attachments: [],
79
- };
80
- });
80
+ return rooms.filter((room) => wholly(reach, room.locator));
81
81
  }
82
82
  function typesWithin(reach, room, types) {
83
83
  if (room === undefined)
@@ -103,7 +103,7 @@ function shownDraft(reach, draft) {
103
103
  function writeWithin(reach, write) {
104
104
  return write.outcome === 'written'
105
105
  ? { outcome: 'written', roomDraft: shownDraft(reach, write.roomDraft) }
106
- : { outcome: 'stale', current: shownDraft(reach, write.current) };
106
+ : staleWrite(shownDraft(reach, write.current));
107
107
  }
108
108
  function savedWithin(reach, room, result) {
109
109
  if (reach === null || result.outcome === 'saved')
@@ -122,7 +122,7 @@ function savedWithin(reach, room, result) {
122
122
  function statusWithin(reach, status) {
123
123
  return {
124
124
  ...status,
125
- attentionRequired: status.attentionRequired.filter((entry) => covers(reach, entry.room, entry.path)),
125
+ attentionRequired: status.attentionRequired.filter((entry) => 'refusal' in entry ? wholly(reach, entry.room) : covers(reach, entry.room, entry.path)),
126
126
  };
127
127
  }
128
128
  function queueWithin(reach, room, entries) {
@@ -130,11 +130,12 @@ function queueWithin(reach, room, entries) {
130
130
  (change.kind !== 'move' || covers(reach, room, change.to))));
131
131
  }
132
132
  function unreached(room) {
133
- return new Error(`No Local Replica for ${room}`);
133
+ return runtimeError('room-not-held', `No Local Replica for ${room}`);
134
134
  }
135
- export function narrowed(runtime, given) {
135
+ export function narrowed(runtime, given, told) {
136
136
  const { md, watch } = runtime;
137
137
  let reach = given;
138
+ let port = told;
138
139
  const open = new Set();
139
140
  function following(begin) {
140
141
  let stop = begin();
@@ -152,6 +153,9 @@ export function narrowed(runtime, given) {
152
153
  if (topic === 'rooms') {
153
154
  return following(() => watch(topic, input, (rooms) => deliver(roomsWithin(reach, rooms))));
154
155
  }
156
+ if (topic === 'readiness') {
157
+ return following(() => runtime.watchReadiness(port, reach, deliver));
158
+ }
155
159
  if (topic === 'sync-status') {
156
160
  return following(() => watch(topic, input, (status) => deliver(statusWithin(reach, status))));
157
161
  }
@@ -189,7 +193,7 @@ export function narrowed(runtime, given) {
189
193
  if (next === null)
190
194
  return;
191
195
  held = next.keyed;
192
- deliver(next.change);
196
+ deliver(next.message);
193
197
  });
194
198
  });
195
199
  }
@@ -201,6 +205,14 @@ export function narrowed(runtime, given) {
201
205
  return () => { };
202
206
  });
203
207
  }
208
+ if (topic === 'conversations' || topic === 'agents') {
209
+ return following(() => {
210
+ if (conversing(reach))
211
+ return watch(topic, input, deliver);
212
+ deliver([]);
213
+ return () => { };
214
+ });
215
+ }
204
216
  return watch(topic, input, deliver);
205
217
  };
206
218
  const conversation = () => (conversing(reach) ? md.conversation : NO_CONVERSATION);
@@ -209,7 +221,10 @@ export function narrowed(runtime, given) {
209
221
  ...md,
210
222
  rooms: () => roomsWithin(reach, md.rooms()),
211
223
  query: ((input) => runtime.query(runtime.plan(input, reach))),
212
- search: (input) => runtime.search(input, reach),
224
+ search(input) {
225
+ runtime.searching(port.target);
226
+ return runtime.search(input, reach);
227
+ },
213
228
  types(room) {
214
229
  if (wholly(reach, room))
215
230
  return md.types(room);
@@ -223,13 +238,22 @@ export function narrowed(runtime, given) {
223
238
  throw unreached(room);
224
239
  const outside = roomDrafts.find(({ path }) => !covers(reach, room, path) ||
225
240
  !moves(reach, room, md.roomDraft.read(room, path)?.parts.move));
226
- if (outside !== undefined)
227
- return { outcome: 'refused', rule: 'stale', file: outside.path };
241
+ if (outside !== undefined) {
242
+ return { outcome: 'refused', code: 'stale', retryable: false, file: outside.path };
243
+ }
228
244
  return savedWithin(reach, room, await md.save(room, roomDrafts, writtenBy));
229
245
  },
230
246
  history: (room, path, query) => covers(reach, room, path)
231
247
  ? md.history(room, path, query)
232
- : Promise.resolve({ kind: 'room-not-readable', locator: room }),
248
+ : Promise.resolve(failure({ kind: 'room-not-readable', locator: room })),
249
+ binaryFile: {
250
+ read: (room, path, revision) => covers(reach, room, path)
251
+ ? md.binaryFile.read(room, path, revision)
252
+ : Promise.resolve(failure({ kind: 'room-not-readable', locator: room })),
253
+ readReference: (reference) => refers(reach, runtime.referrerScope(reference.referrer))
254
+ ? md.binaryFile.readReference(reference)
255
+ : Promise.resolve(unreachedReference(reference.fileRef)),
256
+ },
233
257
  reached: (room, roomVersion, path) => (path === undefined ? wholly(reach, room) : covers(reach, room, path)) &&
234
258
  md.reached(room, roomVersion, path),
235
259
  log: () => (reach === null ? md.log() : ''),
@@ -244,7 +268,7 @@ export function narrowed(runtime, given) {
244
268
  },
245
269
  async discard(room, path, revision) {
246
270
  if (!covers(reach, room, path))
247
- return { outcome: 'stale', current: null };
271
+ return staleWrite(null);
248
272
  return writeWithin(reach, await md.roomDraft.discard(room, path, revision));
249
273
  },
250
274
  },
@@ -252,7 +276,7 @@ export function narrowed(runtime, given) {
252
276
  list: () => conversation().list(),
253
277
  read: (id, options) => conversation().read(id, options),
254
278
  create: (input) => conversation().create(input),
255
- send: (id) => conversation().send(id),
279
+ send: (id, operation, draft) => conversation().send(id, operation, draft),
256
280
  answer: (id, answer) => conversation().answer(id, answer),
257
281
  interrupt: (id) => conversation().interrupt(id),
258
282
  kill: (id) => conversation().kill(id),
@@ -260,7 +284,7 @@ export function narrowed(runtime, given) {
260
284
  delete: (id) => conversation().delete(id),
261
285
  },
262
286
  chatDraft: {
263
- write: (id, draft) => chatDraft().write(id, draft),
287
+ write: (id, draft, revision) => chatDraft().write(id, draft, revision),
264
288
  discard: (id) => chatDraft().discard(id),
265
289
  },
266
290
  observe: observing(within),
@@ -273,5 +297,10 @@ export function narrowed(runtime, given) {
273
297
  for (const restart of [...open])
274
298
  restart();
275
299
  },
300
+ facts(next) {
301
+ port = next;
302
+ for (const restart of [...open])
303
+ restart();
304
+ },
276
305
  };
277
306
  }
@@ -0,0 +1,43 @@
1
+ import type { MdRuntimeClient } from './client.js';
2
+ import type { AppRoom, ConnectionRefusal, SaveResult } from './connection.js';
3
+ import type { Member, ServiceMode } from './adapter.js';
4
+ import type { ConversationAgent, ConversationEntry, ConversationOptions, ConversationView } from './conversation.js';
5
+ import { type RuntimeError } from './failure.js';
6
+ import type { QueueEntry } from './outcome.js';
7
+ import type { DocumentQuery, DocumentResult, GroupQuery, GroupResult, PendingResult } from './query.js';
8
+ import type { Readiness } from './readiness.js';
9
+ import type { RoomDraft, RoomDraftParts } from './room-draft.js';
10
+ import type { SyncStatus } from './status.js';
11
+ type Client = MdRuntimeClient | null;
12
+ export type Observed<T> = {
13
+ readonly state: 'loading';
14
+ } | {
15
+ readonly state: 'ready';
16
+ readonly value: T;
17
+ };
18
+ export declare function useObserve(client: Client, topic: 'deployment-facts'): Observed<unknown>;
19
+ export declare function useObserve(client: Client, topic: 'rooms'): Observed<readonly AppRoom[]>;
20
+ export declare function useObserve(client: Client, topic: 'readiness'): Observed<Readiness>;
21
+ export declare function useObserve(client: Client, topic: 'connection-refusal'): Observed<ConnectionRefusal | null>;
22
+ export declare function useObserve(client: Client, topic: 'storage-loss'): Observed<string | null>;
23
+ export declare function useObserve(client: Client, topic: 'sync-status'): Observed<SyncStatus>;
24
+ export declare function useObserve(client: Client, topic: 'service-mode'): Observed<ServiceMode>;
25
+ export declare function useObserve(client: Client, topic: 'member'): Observed<Member | null>;
26
+ export declare function useObserve(client: Client, topic: 'query', query: DocumentQuery): Observed<DocumentResult | PendingResult>;
27
+ export declare function useObserve(client: Client, topic: 'query', query: GroupQuery): Observed<GroupResult | PendingResult>;
28
+ export declare function useObserve(client: Client, topic: 'queue', locator: string): Observed<readonly QueueEntry[]>;
29
+ export declare function useObserve(client: Client, topic: 'room-drafts'): Observed<readonly RoomDraft[]>;
30
+ export declare function useObserve(client: Client, topic: 'presentation'): Observed<unknown>;
31
+ export declare function useObserve(client: Client, topic: 'conversation', conversation: string, options: ConversationOptions): Observed<ConversationView>;
32
+ export declare function useObserve(client: Client, topic: 'conversations'): Observed<readonly ConversationEntry[]>;
33
+ export declare function useObserve(client: Client, topic: 'agents'): Observed<readonly ConversationAgent[]>;
34
+ export type Saving = {
35
+ readonly busy: boolean;
36
+ readonly outcome: SaveResult | null;
37
+ readonly failure: RuntimeError | null;
38
+ save(room: string, roomDrafts: ReadonlyMap<string, RoomDraftParts>): Promise<SaveResult>;
39
+ };
40
+ export declare function saveRoomDrafts(client: MdRuntimeClient, room: string, roomDrafts: ReadonlyMap<string, RoomDraftParts>): Promise<SaveResult>;
41
+ export declare function useSave(client: MdRuntimeClient): Saving;
42
+ export {};
43
+ //# sourceMappingURL=react.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../src/react.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,KAAK,EAAE,OAAO,EAAE,iBAAiB,EAAkB,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC9F,OAAO,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AACxD,OAAO,KAAK,EACV,iBAAiB,EACjB,iBAAiB,EACjB,mBAAmB,EACnB,gBAAgB,EACjB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAwB,KAAK,YAAY,EAAE,MAAM,cAAc,CA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AK,EAAE,QAAQ,GAAG,QAAQ,CAAC,SAAS,iBAAiB,EAAE,CAAC,CAAC;AAsCpG,MAAM,MAAM,MAAM,GAAG;IACnB,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IACvB,QAAQ,CAAC,OAAO,EAAE,UAAU,GAAG,IAAI,CAAC;IACpC,QAAQ,CAAC,OAAO,EAAE,YAAY,GAAG,IAAI,CAAC;IACtC,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,WAAW,CAAC,MAAM,EAAE,cAAc,CAAC,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;CAC1F,CAAC;AAMF,wBAAsB,cAAc,CAClC,MAAM,EAAE,eAAe,EACvB,IAAI,EAAE,MAAM,EACZ,UAAU,EAAE,WAAW,CAAC,MAAM,EAAE,cAAc,CAAC,GAC9C,OAAO,CAAC,UAAU,CAAC,CAcrB;AAED,wBAAgB,OAAO,CAAC,MAAM,EAAE,eAAe,GAAG,MAAM,CAkBvD"}
package/dist/react.js ADDED
@@ -0,0 +1,64 @@
1
+ import { useCallback, useLayoutEffect, useRef, useState } from 'react';
2
+ import { failed, runtimeError } from './failure.js';
3
+ const LOADING = { state: 'loading' };
4
+ export function useObserve(client, topic, ...input) {
5
+ const key = JSON.stringify([topic, ...input]);
6
+ const [held, setHeld] = useState(null);
7
+ const opened = useRef(input);
8
+ opened.current = input;
9
+ useLayoutEffect(() => {
10
+ if (client === null)
11
+ return;
12
+ let live = true;
13
+ let observation = null;
14
+ const observe = client.observe;
15
+ void observe(topic, ...opened.current).then((open) => {
16
+ if (!live) {
17
+ open.close();
18
+ return;
19
+ }
20
+ observation = open;
21
+ const take = (value) => {
22
+ if (live)
23
+ setHeld({ client, key, observed: { state: 'ready', value } });
24
+ };
25
+ take(open.current());
26
+ open.subscribe(take);
27
+ });
28
+ return () => {
29
+ live = false;
30
+ observation?.close();
31
+ };
32
+ }, [client, key]);
33
+ return held !== null && held.client === client && held.key === key ? held.observed : LOADING;
34
+ }
35
+ const IDLE = { busy: false, outcome: null, failure: null };
36
+ export async function saveRoomDrafts(client, room, roomDrafts) {
37
+ const kept = [];
38
+ for (const [path, parts] of roomDrafts) {
39
+ const prior = await client.roomDraft.read(room, path);
40
+ const write = await client.roomDraft.write(room, path, prior?.revision ?? null, parts);
41
+ if (write.outcome !== 'written' || write.roomDraft === null) {
42
+ throw runtimeError(write.outcome === 'stale' ? write.code : 'room-draft-unwritten', `the Room Draft at ${path} was not written`);
43
+ }
44
+ kept.push({ path, revision: write.roomDraft.revision });
45
+ }
46
+ return client.save(room, kept);
47
+ }
48
+ export function useSave(client) {
49
+ const [state, setState] = useState(IDLE);
50
+ const save = useCallback(async (room, roomDrafts) => {
51
+ setState({ busy: true, outcome: null, failure: null });
52
+ try {
53
+ const outcome = await saveRoomDrafts(client, room, roomDrafts);
54
+ setState({ busy: false, outcome, failure: null });
55
+ return outcome;
56
+ }
57
+ catch (error) {
58
+ const failure = failed(error);
59
+ setState({ busy: false, outcome: null, failure });
60
+ throw failure;
61
+ }
62
+ }, [client]);
63
+ return { ...state, save };
64
+ }
@@ -1,18 +1,25 @@
1
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';
2
+ import { type KeyedResult, type QueryMessage } from './observation.js';
3
+ import type { PendingResult, Plan, QueryCoverage, QueryRows } from './query.js';
4
4
  import type { SearchedDocument } from './search.js';
5
+ export type Cover = {
6
+ coverage: QueryCoverage;
7
+ } | PendingResult;
5
8
  export type ReadSide = {
6
9
  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
+ recover(): void;
11
+ query<T>(plan: Plan, at: () => T): Promise<{
12
+ rows: QueryRows;
13
+ at: T;
14
+ }>;
15
+ watch(plan: Plan, identity: string, cover: () => Cover, deliver: (message: QueryMessage) => void): () => void;
16
+ room(room: string): readonly string[];
10
17
  document(room: string, path: string): SearchedDocument | null;
11
18
  close(): void;
12
19
  };
13
20
  export declare function reconciled(previous: KeyedResult, next: KeyedResult): {
14
21
  keyed: KeyedResult;
15
- change: QueryChange;
22
+ message: QueryMessage;
16
23
  } | null;
17
24
  export declare function createReadSide(): ReadSide;
18
25
  //# sourceMappingURL=read-side.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"read-side.d.ts","sourceRoot":"","sources":["../src/read-side.ts"],"names":[],"mappings":"AA2BA,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;AAqDpD,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;AA8RF,wBAAgB,UAAU,CACxB,QAAQ,EAAE,WAAW,EACrB,IAAI,EAAE,WAAW,GAChB;IAAE,KAAK,EAAE,WAAW,CAAC;IAAC,MAAM,EAAE,WAAW,CAAA;CAAE,GAAG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