@agentshouse/mdruntime 0.1.0-ca9afacd5aebd → 0.1.0-cb0eb0d87ff49

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 (104) hide show
  1. package/AGENTS.md +114 -123
  2. package/dist/adapter.d.ts +26 -3
  3. package/dist/adapter.d.ts.map +1 -1
  4. package/dist/attempt.d.ts.map +1 -1
  5. package/dist/attempt.js +14 -1
  6. package/dist/bootstrap.d.ts +2 -2
  7. package/dist/bootstrap.d.ts.map +1 -1
  8. package/dist/bootstrap.js +28 -6
  9. package/dist/change-set.d.ts +2 -0
  10. package/dist/change-set.d.ts.map +1 -1
  11. package/dist/change-set.js +34 -9
  12. package/dist/chat/acts.d.ts +3 -4
  13. package/dist/chat/acts.d.ts.map +1 -1
  14. package/dist/chat/acts.js +10 -10
  15. package/dist/chat/list.d.ts +6 -0
  16. package/dist/chat/list.d.ts.map +1 -1
  17. package/dist/chat/list.js +6 -3
  18. package/dist/chat/markdown.d.ts +2 -1
  19. package/dist/chat/markdown.d.ts.map +1 -1
  20. package/dist/chat/markdown.js +7 -6
  21. package/dist/chat/message.d.ts.map +1 -1
  22. package/dist/chat/message.js +2 -2
  23. package/dist/chat/provider.d.ts.map +1 -1
  24. package/dist/chat/provider.js +25 -50
  25. package/dist/chat/ready.d.ts.map +1 -1
  26. package/dist/chat/ready.js +2 -2
  27. package/dist/chat/style.js +3 -3
  28. package/dist/client.d.ts +7 -7
  29. package/dist/client.d.ts.map +1 -1
  30. package/dist/client.js +1 -1
  31. package/dist/collection.d.ts +17 -3
  32. package/dist/collection.d.ts.map +1 -1
  33. package/dist/collection.js +230 -19
  34. package/dist/connection.d.ts +8 -5
  35. package/dist/connection.d.ts.map +1 -1
  36. package/dist/connection.js +858 -301
  37. package/dist/conversation.d.ts +12 -7
  38. package/dist/conversation.d.ts.map +1 -1
  39. package/dist/conversation.js +258 -130
  40. package/dist/entry.d.ts +17 -0
  41. package/dist/entry.d.ts.map +1 -0
  42. package/dist/entry.js +156 -0
  43. package/dist/fence.d.ts +6 -0
  44. package/dist/fence.d.ts.map +1 -0
  45. package/dist/fence.js +10 -0
  46. package/dist/host.d.ts +4 -2
  47. package/dist/host.d.ts.map +1 -1
  48. package/dist/host.js +9 -5
  49. package/dist/identity.d.ts +2 -0
  50. package/dist/identity.d.ts.map +1 -1
  51. package/dist/identity.js +6 -0
  52. package/dist/index.d.ts +5 -4
  53. package/dist/index.d.ts.map +1 -1
  54. package/dist/index.js +1 -0
  55. package/dist/memory.d.ts +1 -0
  56. package/dist/memory.d.ts.map +1 -1
  57. package/dist/memory.js +10 -1
  58. package/dist/model.d.ts +1 -0
  59. package/dist/model.d.ts.map +1 -1
  60. package/dist/model.js +9 -0
  61. package/dist/observation.d.ts +5 -2
  62. package/dist/observation.d.ts.map +1 -1
  63. package/dist/observation.js +22 -11
  64. package/dist/outcome.d.ts +4 -0
  65. package/dist/outcome.d.ts.map +1 -1
  66. package/dist/outcome.js +1 -0
  67. package/dist/overlay.d.ts +1 -0
  68. package/dist/overlay.d.ts.map +1 -1
  69. package/dist/overlay.js +33 -54
  70. package/dist/query.d.ts +28 -3
  71. package/dist/query.d.ts.map +1 -1
  72. package/dist/reach.js +2 -2
  73. package/dist/react.d.ts +3 -3
  74. package/dist/react.d.ts.map +1 -1
  75. package/dist/read-side.d.ts +13 -6
  76. package/dist/read-side.d.ts.map +1 -1
  77. package/dist/read-side.js +282 -83
  78. package/dist/recovery.d.ts +25 -0
  79. package/dist/recovery.d.ts.map +1 -0
  80. package/dist/recovery.js +197 -0
  81. package/dist/refresh.d.ts.map +1 -1
  82. package/dist/refresh.js +11 -10
  83. package/dist/replica.d.ts +1 -1
  84. package/dist/replica.d.ts.map +1 -1
  85. package/dist/replica.js +49 -32
  86. package/dist/rows.d.ts +4 -13
  87. package/dist/rows.d.ts.map +1 -1
  88. package/dist/rows.js +2 -12
  89. package/dist/save.d.ts +6 -2
  90. package/dist/save.d.ts.map +1 -1
  91. package/dist/save.js +42 -18
  92. package/dist/search.d.ts +28 -6
  93. package/dist/search.d.ts.map +1 -1
  94. package/dist/search.js +109 -44
  95. package/dist/shared.d.ts +11 -0
  96. package/dist/shared.d.ts.map +1 -0
  97. package/dist/shared.js +368 -0
  98. package/dist/store.d.ts +35 -10
  99. package/dist/store.d.ts.map +1 -1
  100. package/dist/store.js +275 -74
  101. package/dist/turn.d.ts +7 -0
  102. package/dist/turn.d.ts.map +1 -0
  103. package/dist/turn.js +45 -0
  104. package/package.json +12 -8
package/dist/save.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { definitionOf } from './model.js';
2
2
  import { apply, ATTACHMENT, links, parse, pointerSegments, references, relative, relativize, retarget, serialize, target, } from '@agentshouse/mdmodel';
3
3
  import { immutable, queuedFile, departure, } from './change.js';
4
- import { collectionOf, documentsOf, governing, validateCollection, } from './collection.js';
4
+ import { available, changedCleanly, collectionOf, documentsOf, governing, referring, validateCollection, } from './collection.js';
5
5
  import { changeBatchIdentity } from './identity.js';
6
6
  import { overlay } from './overlay.js';
7
7
  import { covered, normalize } from './paths.js';
@@ -177,7 +177,7 @@ export function providerRewritten(batch, visible) {
177
177
  ...anticipated(batch.changes, batch.preconditions, rewritten.changes, visible),
178
178
  };
179
179
  }
180
- function incoming(collection, removed) {
180
+ function incoming(collection, removed, candidates) {
181
181
  const found = new Map();
182
182
  const note = (target, file) => {
183
183
  if (!removed.has(target))
@@ -186,7 +186,13 @@ function incoming(collection, removed) {
186
186
  referrers.add(file);
187
187
  found.set(target, referrers);
188
188
  };
189
- for (const [path, document] of documentsOf(collection)) {
189
+ const read = candidates === null
190
+ ? documentsOf(collection)
191
+ : [...new Set(candidates)].sort().flatMap((path) => {
192
+ const document = collection.document(path);
193
+ return document === undefined ? [] : [[path, document]];
194
+ });
195
+ for (const [path, document] of read) {
190
196
  const definition = definitionOf(governing(collection.composition, path), document.type);
191
197
  for (const { value, to } of definition === undefined ? [] : references(document, definition)) {
192
198
  if (to.includes(ATTACHMENT))
@@ -217,10 +223,13 @@ function affected(change) {
217
223
  return change.kind === 'move' ? [change.path, change.to] : [change.path];
218
224
  }
219
225
  function guard(changes, input) {
220
- const existing = new Set([
221
- ...Object.keys(input.visible.documents),
222
- ...Object.keys(input.visible.binaries),
223
- ]);
226
+ const overridden = new Map();
227
+ const existing = {
228
+ has: (path) => overridden.get(path) ??
229
+ (Object.hasOwn(input.visible.documents, path) || Object.hasOwn(input.visible.binaries, path)),
230
+ add: (path) => void overridden.set(path, true),
231
+ delete: (path) => void overridden.set(path, false),
232
+ };
224
233
  for (const [index, change] of changes.entries()) {
225
234
  for (const path of affected(change)) {
226
235
  const normalized = normalize(path);
@@ -364,8 +373,8 @@ function preconditionsOf(changes, visible, bases) {
364
373
  function depth(path) {
365
374
  return path.split('/').length;
366
375
  }
367
- function exceeded(limits, changes, resulting) {
368
- const written = changes.map((change) => (change.kind === 'move' ? change.to : change.path));
376
+ function exceeded(limits, changes, effective, resulting) {
377
+ const written = effective.map((change) => (change.kind === 'move' ? change.to : change.path));
369
378
  const created = changes
370
379
  .filter((change) => change.kind === 'create' || change.kind === 'move' || change.kind === 'binary')
371
380
  .map((change) => (change.kind === 'move' ? change.to : change.path));
@@ -381,7 +390,7 @@ function exceeded(limits, changes, resulting) {
381
390
  return refuse('provider-limit', undefined, limit.name);
382
391
  }
383
392
  if (limit.name === 'document-size') {
384
- const large = written.find((path) => (resulting.documents[path] ?? '').length > value);
393
+ const large = written.find((path) => new TextEncoder().encode(resulting.documents[path] ?? '').byteLength > value);
385
394
  if (large !== undefined)
386
395
  return refuse('provider-limit', large, limit.name);
387
396
  }
@@ -450,6 +459,8 @@ function relativeChanges(input) {
450
459
  });
451
460
  }
452
461
  function unmodeledType(collection, composition) {
462
+ if (available(composition))
463
+ return false;
453
464
  for (const [path, document] of documentsOf(collection)) {
454
465
  if (document.type !== undefined &&
455
466
  document.type !== 'any' &&
@@ -718,7 +729,8 @@ export function joined(queue, owner, extra) {
718
729
  return { kind: 'joined', batch, queue: queue.map((item) => (item === owner ? batch : item)) };
719
730
  }
720
731
  export function limitedIn(queue, batch, limits, state) {
721
- const limited = exceeded(limits, batch.changes, state(queue.slice(0, queue.indexOf(batch) + 1)));
732
+ const at = queue.indexOf(batch);
733
+ const limited = exceeded(limits, batch.changes, providerRewritten(batch, state(queue.slice(0, at))).changes, state(queue.slice(0, at + 1)));
722
734
  return limited?.kind === 'refused' ? limited.refusal : null;
723
735
  }
724
736
  export function buildBatch(input) {
@@ -739,9 +751,16 @@ export function buildBatch(input) {
739
751
  const resulting = overlay(input.visible, [
740
752
  { id: '', baseRoomVersion: input.baseRoomVersion, ...predicted },
741
753
  ]);
754
+ const clean = input.clean !== undefined &&
755
+ input.clean.text === input.visible.documents &&
756
+ input.clean.binaries === input.visible.binaries &&
757
+ available(input.composition)
758
+ ? input.clean
759
+ : null;
742
760
  const removed = new Set(changes.filter((change) => change.kind === 'remove').map((change) => change.path));
743
761
  if (removed.size > 0) {
744
- const referenced = incoming(collectionOf(resulting.documents, input.composition), removed);
762
+ const referrers = clean === null ? null : referring(clean, input.composition, removed);
763
+ const referenced = incoming(collectionOf(resulting.documents, input.composition), removed, referrers === null ? null : [...referrers, ...predicted.changes.flatMap(affected)]);
745
764
  if (referenced !== null)
746
765
  return referenced;
747
766
  }
@@ -751,15 +770,20 @@ export function buildBatch(input) {
751
770
  const parsed = parse(content);
752
771
  return parsed.ok && parsed.document.type !== undefined && parsed.document.type !== 'any';
753
772
  };
754
- const typedPaths = [...new Set(predicted.changes.flatMap(affected))].filter((path) => typed(input.visible.documents[path]) || typed(resulting.documents[path]));
773
+ const touched = [...new Set(predicted.changes.flatMap(affected))];
774
+ const typedPaths = touched.filter((path) => typed(input.visible.documents[path]) || typed(resulting.documents[path]));
755
775
  const needsModel = typedPaths.length > 0;
756
776
  const withoutModel = typedPaths.find((path) => governing(input.composition, path).status === 'unavailable');
757
777
  if (withoutModel !== undefined)
758
778
  return refuse('model-unavailable', withoutModel);
759
- const validationText = needsModel
760
- ? resulting.documents
761
- : Object.fromEntries(Object.entries(resulting.documents).filter(([path]) => predicted.changes.some((change) => affected(change).includes(path))));
762
- const verdict = validateCollection(validationText, Object.keys(resulting.binaries), input.composition);
779
+ const verdict = clean !== null &&
780
+ changedCleanly({ text: resulting.documents, binaries: resulting.binaries }, clean, input.composition)
781
+ ? { verdict: 'accept' }
782
+ : validateCollection(needsModel
783
+ ? resulting.documents
784
+ : Object.fromEntries(touched.flatMap((path) => resulting.documents[path] === undefined
785
+ ? []
786
+ : [[path, resulting.documents[path]]])), Object.keys(resulting.binaries), input.composition);
763
787
  if (verdict.verdict === 'refuse') {
764
788
  return {
765
789
  kind: 'refused',
@@ -772,7 +796,7 @@ export function buildBatch(input) {
772
796
  },
773
797
  };
774
798
  }
775
- const limited = exceeded(input.providerLimits, changes, resulting);
799
+ const limited = exceeded(input.providerLimits, changes, predicted.changes, resulting);
776
800
  if (limited !== null)
777
801
  return limited;
778
802
  const validated = typedPaths
package/dist/search.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { ChangeSet } from './change-set.js';
2
- import { type Filter } from './query.js';
2
+ import { type CoveredRoom, type Filter, type PendingRoom } from './query.js';
3
3
  import type { Open } from './open.js';
4
4
  export type DocumentPart = Open<'fields' | 'sections' | 'preamble' | 'path'>;
5
5
  export type SearchRequest = {
@@ -21,10 +21,26 @@ export type MessageHit = {
21
21
  score: number;
22
22
  matches: Readonly<Record<string, readonly Open<'text'>[]>>;
23
23
  };
24
- export type SearchResult = {
24
+ export type SearchCoverage = {
25
+ rooms: readonly CoveredRoom[];
26
+ conversations: readonly string[];
27
+ index: string;
28
+ overlay: readonly string[];
29
+ };
30
+ export type PendingConversation = {
31
+ conversation: string;
32
+ awaits: 'index';
33
+ };
34
+ export type SearchPending = {
35
+ pending: readonly (PendingRoom | PendingConversation)[];
36
+ };
37
+ export type SearchHits = {
25
38
  documents: readonly DocumentHit[];
26
39
  messages: readonly MessageHit[];
27
40
  };
41
+ export type SearchResult = (SearchHits & {
42
+ coverage: SearchCoverage;
43
+ }) | SearchPending;
28
44
  export type SearchedDocument = {
29
45
  room: string;
30
46
  path: string;
@@ -39,10 +55,8 @@ export type SearchedMessage = {
39
55
  text: string;
40
56
  };
41
57
  export type SearchSource = {
42
- documents(): readonly SearchedDocument[];
58
+ room(room: string): readonly string[];
43
59
  document(room: string, path: string): SearchedDocument | null;
44
- messages(): readonly SearchedMessage[];
45
- message(conversation: string, identity: string): SearchedMessage | null;
46
60
  };
47
61
  export type SearchPlan = {
48
62
  text: string;
@@ -51,11 +65,19 @@ export type SearchPlan = {
51
65
  limit: number | null;
52
66
  };
53
67
  export type SearchIndex = {
68
+ generation(): string;
69
+ rooms(): ReadonlySet<string>;
70
+ conversations(): ReadonlySet<string>;
71
+ indexRoom(room: string): Promise<void>;
72
+ indexMessages(conversation: string, messages: readonly SearchedMessage[], whole: boolean): void;
73
+ forget(rooms: readonly string[]): void;
54
74
  changed(set: ChangeSet): void;
75
+ changes(): number;
76
+ caughtUp(): Promise<void>;
55
77
  search(plan: SearchPlan, documents: readonly {
56
78
  room: string;
57
79
  path: string;
58
- }[] | null): SearchResult;
80
+ }[] | null, conversations: readonly string[]): SearchHits;
59
81
  };
60
82
  export declare function searchPlanOf(input: unknown): SearchPlan;
61
83
  export declare function createSearchIndex(source: SearchSource): SearchIndex;
@@ -1 +1 @@
1
- {"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../src/search.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAkC,KAAK,MAAM,EAAE,MAAM,YAAY,CAAC;AACzE,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEtC,MAAM,MAAM,YAAY,GAAG,IAAI,CAAC,QAAQ,GAAG,UAAU,GAAG,UAAU,GAAG,MAAM,CAAC,CAAC;AAE7E,MAAM,MAAM,aAAa,GAAG;IAC1B,IAAI,EAAE,MAA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;IACvC,aAAa,CAAC,YAAY,EAAE,MAAM,EAAE,QAAQ,EAAE,SAAS,eAAe,EAAE,EAAE,KAAK,EAAE,OAAO,GAAG,IAAI,CAAC;IAChG,MAAM,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,IAAI,CAAC;IACvC,OAAO,CAAC,GAAG,EAAE,SAAS,GAAG,IAAI,CAAC;IAC9B,OAAO,IAAI,MAAM,CAAC;IAClB,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1B,MAAM,CACJ,IAAI,EAAE,UAAU,EAChB,SAAS,EAAE,SAAS;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,EAAE,GAAG,IAAI,EAC3D,aAAa,EAAE,SAAS,MAAM,EAAE,GAC/B,UAAU,CAAC;CACf,CAAC;AA4BF,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,UAAU,CAiBvD;AAMD,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,YAAY,GAAG,WAAW,CAiKnE"}
package/dist/search.js CHANGED
@@ -1,5 +1,7 @@
1
1
  import MiniSearch from 'minisearch';
2
- import { invalid, known, limitOf, plain } from './query.js';
2
+ import { indexGeneration } from './identity.js';
3
+ import { invalid, known, limitOf, plain, } from './query.js';
4
+ import { inUnits, ROW_WEIGHT } from './turn.js';
3
5
  const FIELDS = ['fields', 'sections', 'preamble', 'path', 'text'];
4
6
  function documentId(room, path) {
5
7
  return JSON.stringify(['document', room, path]);
@@ -35,15 +37,22 @@ export function searchPlanOf(input) {
35
37
  };
36
38
  }
37
39
  export function createSearchIndex(source) {
38
- let index = null;
40
+ const generation = indexGeneration();
41
+ const index = new MiniSearch({ fields: FIELDS });
39
42
  const documents = new Map();
40
43
  const messages = new Map();
44
+ const rooms = new Set();
45
+ const filling = new Set();
46
+ const conversations = new Set();
47
+ const acquiring = new Set();
41
48
  const changedDocuments = new Map();
42
- const changedMessages = new Map();
43
- function addDocument(built, document) {
49
+ let changes = 0;
50
+ function addDocument(document) {
44
51
  const id = documentId(document.room, document.path);
52
+ if (documents.has(id))
53
+ index.discard(id);
45
54
  documents.set(id, { room: document.room, path: document.path, type: document.type });
46
- built.add({
55
+ index.add({
47
56
  id,
48
57
  fields: scalars(document.fields).join('\n'),
49
58
  sections: Object.entries(document.sections)
@@ -53,42 +62,47 @@ export function createSearchIndex(source) {
53
62
  path: document.path,
54
63
  });
55
64
  }
56
- function addMessage(built, message) {
65
+ function addMessage(message) {
57
66
  const id = messageId(message.conversation, message.identity);
67
+ if (messages.has(id))
68
+ index.discard(id);
58
69
  messages.set(id, { conversation: message.conversation, identity: message.identity });
59
- built.add({ id, text: message.text });
70
+ index.add({ id, text: message.text });
60
71
  }
61
- function current() {
62
- if (index === null) {
63
- const built = new MiniSearch({ fields: FIELDS });
64
- for (const document of source.documents())
65
- addDocument(built, document);
66
- for (const message of source.messages())
67
- addMessage(built, message);
68
- index = built;
69
- return built;
72
+ function discard(id, entries) {
73
+ if (entries.delete(id))
74
+ index.discard(id);
75
+ }
76
+ function* pending() {
77
+ for (;;) {
78
+ const next = changedDocuments.entries().next();
79
+ if (next.done === true)
80
+ return;
81
+ changedDocuments.delete(next.value[0]);
82
+ yield next.value;
70
83
  }
71
- const built = index;
72
- for (const [id, { room, path }] of changedDocuments) {
73
- if (documents.delete(id))
74
- built.discard(id);
84
+ }
85
+ let catching = null;
86
+ function caughtUp() {
87
+ if (catching !== null)
88
+ return catching;
89
+ const running = inUnits(pending(), ([id, { room, path }]) => {
90
+ discard(id, documents);
75
91
  const document = source.document(room, path);
76
- if (document !== null)
77
- addDocument(built, document);
78
- }
79
- changedDocuments.clear();
80
- for (const [id, { conversation, identity }] of changedMessages) {
81
- if (messages.delete(id))
82
- built.discard(id);
83
- const message = source.message(conversation, identity);
84
- if (message !== null)
85
- addMessage(built, message);
86
- }
87
- changedMessages.clear();
88
- return built;
92
+ if (document === null)
93
+ return ROW_WEIGHT;
94
+ addDocument(document);
95
+ return Object.values(document.sections).reduce((total, text) => total + text.length, ROW_WEIGHT + document.preamble.length);
96
+ });
97
+ if (!(running instanceof Promise))
98
+ return Promise.resolve();
99
+ catching = running.finally(() => {
100
+ catching = null;
101
+ });
102
+ return catching;
89
103
  }
90
- function ranked(built, text, entries, keep, limit) {
91
- const found = built.search(text, {
104
+ function ranked(text, entries, keep, limit) {
105
+ const found = index.search(text, {
92
106
  filter: ({ id }) => {
93
107
  const entry = entries.get(id);
94
108
  return entry !== undefined && keep(entry, id);
@@ -101,24 +115,75 @@ export function createSearchIndex(source) {
101
115
  }));
102
116
  }
103
117
  return {
104
- changed(set) {
105
- if (index === null)
118
+ generation: () => generation,
119
+ rooms: () => rooms,
120
+ conversations: () => conversations,
121
+ async indexRoom(room) {
122
+ if (rooms.has(room) || filling.has(room))
106
123
  return;
124
+ filling.add(room);
125
+ try {
126
+ await inUnits(source.room(room), (path) => {
127
+ const document = filling.has(room) ? source.document(room, path) : null;
128
+ if (document === null)
129
+ return ROW_WEIGHT;
130
+ addDocument(document);
131
+ return Object.values(document.sections).reduce((total, text) => total + text.length, ROW_WEIGHT + document.preamble.length);
132
+ });
133
+ if (filling.has(room))
134
+ rooms.add(room);
135
+ }
136
+ finally {
137
+ filling.delete(room);
138
+ }
139
+ },
140
+ indexMessages(conversation, arriving, whole) {
141
+ for (const message of arriving)
142
+ addMessage(message);
143
+ if (!whole) {
144
+ acquiring.add(conversation);
145
+ return;
146
+ }
147
+ acquiring.delete(conversation);
148
+ conversations.add(conversation);
149
+ },
150
+ forget(leaving) {
151
+ const gone = new Set(leaving);
152
+ for (const [id, entry] of documents)
153
+ if (gone.has(entry.room))
154
+ discard(id, documents);
155
+ for (const room of leaving) {
156
+ rooms.delete(room);
157
+ filling.delete(room);
158
+ }
159
+ },
160
+ changed(set) {
161
+ changes += 1;
107
162
  for (const { locator, path } of set.documents) {
108
- changedDocuments.set(documentId(locator, path), { room: locator, path });
163
+ if (rooms.has(locator) || filling.has(locator)) {
164
+ changedDocuments.set(documentId(locator, path), { room: locator, path });
165
+ }
109
166
  }
110
- for (const { conversation, identity } of set.messages) {
111
- changedMessages.set(messageId(conversation, identity), { conversation, identity });
167
+ for (const { conversation, identity, text } of set.messages) {
168
+ if (!conversations.has(conversation) && !acquiring.has(conversation))
169
+ continue;
170
+ const id = messageId(conversation, identity);
171
+ if (text === null)
172
+ discard(id, messages);
173
+ else
174
+ addMessage({ conversation, identity, text });
112
175
  }
113
176
  },
114
- search({ text, conversations, limit }, selected) {
115
- const built = current();
177
+ changes: () => changes,
178
+ caughtUp,
179
+ search({ text, limit }, selected, named) {
116
180
  const chosen = selected === null
117
181
  ? null
118
182
  : new Set(selected.map(({ room, path }) => documentId(room, path)));
183
+ const asked = new Set(named);
119
184
  return {
120
- documents: ranked(built, text, documents, (_entry, id) => chosen === null || chosen.has(id), limit).map(({ match, ...hit }) => ({ ...hit, matches: match })),
121
- messages: ranked(built, text, messages, ({ conversation }) => conversations === null || conversations.includes(conversation), limit).map(({ match, ...hit }) => ({ ...hit, matches: match })),
185
+ documents: ranked(text, documents, (_entry, id) => chosen === null || chosen.has(id), limit).map(({ match, ...hit }) => ({ ...hit, matches: match })),
186
+ messages: ranked(text, messages, ({ conversation }) => asked.has(conversation), limit).map(({ match, ...hit }) => ({ ...hit, matches: match })),
122
187
  };
123
188
  },
124
189
  };
@@ -0,0 +1,11 @@
1
+ export type Paths<V> = Readonly<Record<string, V>>;
2
+ type Weigh<V> = (key: string, value: V) => number;
3
+ export declare function shared<V>(map: Paths<V>): Paths<V>;
4
+ export declare function patched<V>(base: Paths<V>, set: Iterable<readonly [string, V]>, removed?: Iterable<string>): Paths<V>;
5
+ export declare function tally<V>(map: Paths<V>, weigh: Weigh<V>): number;
6
+ export declare function sizeOf<V>(map: Paths<V>): number;
7
+ export declare function differing<V>(left: Paths<V>, right: Paths<V>, same?: (left: V, right: V) => boolean): ReadonlySet<string>;
8
+ export declare function drafted<V>(base: Paths<V>): Record<string, V>;
9
+ export declare function settled<V>(draft: Record<string, V>): Paths<V>;
10
+ export {};
11
+ //# sourceMappingURL=shared.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,KAAK,CAAC,CAAC,IAAI,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC;AAkBnD,KAAK,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,KAAK,MAAM,CAAC;AA6LlD,wBAAgB,MAAM,CAAC,CAAC,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAEjD;AAED,wBAAgB,OAAO,CAAC,CAAC,EACvB,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,EACd,GAAG,EAAE,QAAQ,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,EACnC,OAAO,GAAE,QAAQ,CAAC,MAAM,CAAM,GAC7B,KAAK,CAAC,CAAC,CAAC,CA8BV;AAED,wBAAgB,KAAK,CAAC,CAAC,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,GAAG,MAAM,CAY/D;AAED,wBAAgB,MAAM,CAAC,CAAC,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC,GAAG,MAAM,CAE/C;AAuCD,wBAAgB,SAAS,CAAC,CAAC,EACzB,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,EACd,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,EACf,IAAI,GAAE,CAAC,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,KAAK,OAAmB,GAC/C,WAAW,CAAC,MAAM,CAAC,CAcrB;AAMD,wBAAgB,OAAO,CAAC,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,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