@agentshouse/mdruntime 0.1.0-c96e80d38e5f0 → 0.1.0-c98426a9ee68e

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 (261) hide show
  1. package/README.md +1 -1
  2. package/dist/acceptance.d.ts +6 -0
  3. package/dist/acceptance.d.ts.map +1 -0
  4. package/dist/acceptance.js +389 -0
  5. package/dist/actions.d.ts +20 -0
  6. package/dist/actions.d.ts.map +1 -0
  7. package/dist/actions.js +248 -0
  8. package/dist/adapter.d.ts +22 -25
  9. package/dist/adapter.d.ts.map +1 -1
  10. package/dist/attached-model.d.ts +3 -10
  11. package/dist/attached-model.d.ts.map +1 -1
  12. package/dist/attached-model.js +21 -56
  13. package/dist/bootstrap.d.ts +8 -4
  14. package/dist/bootstrap.d.ts.map +1 -1
  15. package/dist/bootstrap.js +209 -78
  16. package/dist/cell.d.ts +6 -0
  17. package/dist/cell.d.ts.map +1 -0
  18. package/dist/cell.js +9 -0
  19. package/dist/chain.d.ts +2 -1
  20. package/dist/chain.d.ts.map +1 -1
  21. package/dist/change.d.ts +1 -0
  22. package/dist/change.d.ts.map +1 -1
  23. package/dist/chat-types.d.ts +3 -0
  24. package/dist/chat-types.d.ts.map +1 -0
  25. package/dist/chat-types.js +2 -0
  26. package/dist/client.d.ts +17 -2
  27. package/dist/client.d.ts.map +1 -1
  28. package/dist/client.js +3 -0
  29. package/dist/collection.d.ts +2 -1
  30. package/dist/collection.d.ts.map +1 -1
  31. package/dist/compile.d.ts +7 -7
  32. package/dist/compile.d.ts.map +1 -1
  33. package/dist/compile.js +229 -301
  34. package/dist/connection.d.ts +5 -141
  35. package/dist/connection.d.ts.map +1 -1
  36. package/dist/connection.js +98 -5258
  37. package/dist/context.d.ts +2 -0
  38. package/dist/context.d.ts.map +1 -0
  39. package/dist/context.js +7 -0
  40. package/dist/conversation-row.d.ts +33 -0
  41. package/dist/conversation-row.d.ts.map +1 -0
  42. package/dist/conversation-row.js +47 -0
  43. package/dist/conversation.d.ts +15 -6
  44. package/dist/conversation.d.ts.map +1 -1
  45. package/dist/conversation.js +363 -216
  46. package/dist/discovery.d.ts +10 -0
  47. package/dist/discovery.d.ts.map +1 -0
  48. package/dist/discovery.js +175 -0
  49. package/dist/durability.d.ts +15 -0
  50. package/dist/durability.d.ts.map +1 -1
  51. package/dist/entry.d.ts +2 -2
  52. package/dist/entry.d.ts.map +1 -1
  53. package/dist/entry.js +17 -8
  54. package/dist/figures.d.ts +48 -0
  55. package/dist/figures.d.ts.map +1 -0
  56. package/dist/figures.js +197 -0
  57. package/dist/holdings.d.ts +29 -0
  58. package/dist/holdings.d.ts.map +1 -0
  59. package/dist/holdings.js +95 -0
  60. package/dist/host.d.ts +2 -2
  61. package/dist/host.d.ts.map +1 -1
  62. package/dist/host.js +22 -15
  63. package/dist/identity.d.ts +0 -1
  64. package/dist/identity.d.ts.map +1 -1
  65. package/dist/identity.js +0 -3
  66. package/dist/index.d.ts +20 -5
  67. package/dist/index.d.ts.map +1 -1
  68. package/dist/index.js +5 -1
  69. package/dist/install.d.ts +56 -0
  70. package/dist/install.d.ts.map +1 -0
  71. package/dist/install.js +560 -0
  72. package/dist/job.d.ts +33 -0
  73. package/dist/job.d.ts.map +1 -0
  74. package/dist/job.js +54 -0
  75. package/dist/lifecycle.d.ts +7 -0
  76. package/dist/lifecycle.d.ts.map +1 -0
  77. package/dist/lifecycle.js +267 -0
  78. package/dist/listing.d.ts +53 -0
  79. package/dist/listing.d.ts.map +1 -0
  80. package/dist/listing.js +175 -0
  81. package/dist/live.d.ts +5 -4
  82. package/dist/live.d.ts.map +1 -1
  83. package/dist/live.js +62 -20
  84. package/dist/locks.d.ts +9 -0
  85. package/dist/locks.d.ts.map +1 -0
  86. package/dist/locks.js +61 -0
  87. package/dist/log.d.ts +1 -0
  88. package/dist/log.d.ts.map +1 -1
  89. package/dist/lost.d.ts +5 -0
  90. package/dist/lost.d.ts.map +1 -0
  91. package/dist/lost.js +61 -0
  92. package/dist/memory.d.ts +4 -1
  93. package/dist/memory.d.ts.map +1 -1
  94. package/dist/memory.js +43 -4
  95. package/dist/messages.d.ts +25 -1
  96. package/dist/messages.d.ts.map +1 -1
  97. package/dist/messages.js +84 -5
  98. package/dist/model.d.ts +2 -9
  99. package/dist/model.d.ts.map +1 -1
  100. package/dist/model.js +0 -9
  101. package/dist/models.d.ts +14 -0
  102. package/dist/models.d.ts.map +1 -0
  103. package/dist/models.js +193 -0
  104. package/dist/observation.d.ts +2 -2
  105. package/dist/observation.d.ts.map +1 -1
  106. package/dist/observation.js +7 -2
  107. package/dist/opening.d.ts +4 -0
  108. package/dist/opening.d.ts.map +1 -0
  109. package/dist/opening.js +56 -0
  110. package/dist/outcome.d.ts +1 -0
  111. package/dist/outcome.d.ts.map +1 -1
  112. package/dist/outcome.js +3 -0
  113. package/dist/overlay.d.ts +1 -0
  114. package/dist/overlay.d.ts.map +1 -1
  115. package/dist/overlay.js +11 -0
  116. package/dist/parts.d.ts +59 -0
  117. package/dist/parts.d.ts.map +1 -0
  118. package/dist/parts.js +118 -0
  119. package/dist/preparation.d.ts +20 -0
  120. package/dist/preparation.d.ts.map +1 -0
  121. package/dist/preparation.js +226 -0
  122. package/dist/queries.d.ts +16 -0
  123. package/dist/queries.d.ts.map +1 -0
  124. package/dist/queries.js +272 -0
  125. package/dist/query.d.ts +13 -212
  126. package/dist/query.d.ts.map +1 -1
  127. package/dist/query.js +3 -524
  128. package/dist/questions.d.ts +32 -0
  129. package/dist/questions.d.ts.map +1 -0
  130. package/dist/questions.js +118 -0
  131. package/dist/reach.d.ts +12 -10
  132. package/dist/reach.d.ts.map +1 -1
  133. package/dist/reach.js +165 -109
  134. package/dist/react.d.ts +5 -2
  135. package/dist/react.d.ts.map +1 -1
  136. package/dist/read.d.ts +7 -0
  137. package/dist/read.d.ts.map +1 -0
  138. package/dist/read.js +17 -0
  139. package/dist/reads.d.ts +36 -0
  140. package/dist/reads.d.ts.map +1 -0
  141. package/dist/reads.js +136 -0
  142. package/dist/recovery.d.ts.map +1 -1
  143. package/dist/recovery.js +9 -1
  144. package/dist/refresh.d.ts +1 -1
  145. package/dist/refresh.d.ts.map +1 -1
  146. package/dist/refresh.js +8 -20
  147. package/dist/refusal.d.ts +5 -0
  148. package/dist/refusal.d.ts.map +1 -0
  149. package/dist/refusal.js +56 -0
  150. package/dist/replica.d.ts +8 -8
  151. package/dist/replica.d.ts.map +1 -1
  152. package/dist/replica.js +50 -18
  153. package/dist/room-draft.d.ts +3 -2
  154. package/dist/room-draft.d.ts.map +1 -1
  155. package/dist/room-draft.js +4 -4
  156. package/dist/room-drafts.d.ts +24 -0
  157. package/dist/room-drafts.d.ts.map +1 -0
  158. package/dist/room-drafts.js +216 -0
  159. package/dist/rows.d.ts +14 -22
  160. package/dist/rows.d.ts.map +1 -1
  161. package/dist/rows.js +48 -189
  162. package/dist/runtime.d.ts +42 -0
  163. package/dist/runtime.d.ts.map +1 -0
  164. package/dist/runtime.js +1 -0
  165. package/dist/save.d.ts.map +1 -1
  166. package/dist/save.js +11 -8
  167. package/dist/saves.d.ts +70 -0
  168. package/dist/saves.d.ts.map +1 -0
  169. package/dist/saves.js +1004 -0
  170. package/dist/search.d.ts.map +1 -1
  171. package/dist/search.js +1 -1
  172. package/dist/seed.d.ts +11 -2
  173. package/dist/seed.d.ts.map +1 -1
  174. package/dist/seed.js +71 -0
  175. package/dist/shown.d.ts +31 -0
  176. package/dist/shown.d.ts.map +1 -0
  177. package/dist/shown.js +294 -0
  178. package/dist/socket/admission.d.ts +7 -0
  179. package/dist/socket/admission.d.ts.map +1 -0
  180. package/dist/socket/admission.js +39 -0
  181. package/dist/socket/answers.d.ts +6 -0
  182. package/dist/socket/answers.d.ts.map +1 -0
  183. package/dist/socket/answers.js +20 -0
  184. package/dist/socket/lanes.d.ts +20 -0
  185. package/dist/socket/lanes.d.ts.map +1 -0
  186. package/dist/socket/lanes.js +77 -0
  187. package/dist/socket/reads.d.ts +10 -0
  188. package/dist/socket/reads.d.ts.map +1 -0
  189. package/dist/socket/reads.js +31 -0
  190. package/dist/socket/rooms.d.ts +43 -0
  191. package/dist/socket/rooms.d.ts.map +1 -0
  192. package/dist/socket/rooms.js +90 -0
  193. package/dist/socket/session.d.ts +39 -0
  194. package/dist/socket/session.d.ts.map +1 -0
  195. package/dist/socket/session.js +23 -0
  196. package/dist/socket.d.ts +70 -0
  197. package/dist/socket.d.ts.map +1 -0
  198. package/dist/socket.js +175 -0
  199. package/dist/source.d.ts +61 -0
  200. package/dist/source.d.ts.map +1 -0
  201. package/dist/source.js +81 -0
  202. package/dist/status.d.ts +0 -2
  203. package/dist/status.d.ts.map +1 -1
  204. package/dist/status.js +0 -6
  205. package/dist/store/fences.d.ts +16 -0
  206. package/dist/store/fences.d.ts.map +1 -0
  207. package/dist/store/fences.js +26 -0
  208. package/dist/store/installs.d.ts +30 -0
  209. package/dist/store/installs.d.ts.map +1 -0
  210. package/dist/store/installs.js +34 -0
  211. package/dist/store/readiness.d.ts +10 -0
  212. package/dist/store/readiness.d.ts.map +1 -0
  213. package/dist/store/readiness.js +52 -0
  214. package/dist/store/records.d.ts +212 -0
  215. package/dist/store/records.d.ts.map +1 -0
  216. package/dist/store/records.js +408 -0
  217. package/dist/store/room-held.d.ts +8 -0
  218. package/dist/store/room-held.d.ts.map +1 -0
  219. package/dist/store/room-held.js +17 -0
  220. package/dist/store/space.d.ts +11 -0
  221. package/dist/store/space.d.ts.map +1 -0
  222. package/dist/store/space.js +43 -0
  223. package/dist/store/watchers.d.ts +17 -0
  224. package/dist/store/watchers.d.ts.map +1 -0
  225. package/dist/store/watchers.js +57 -0
  226. package/dist/store/writes.d.ts +24 -0
  227. package/dist/store/writes.d.ts.map +1 -0
  228. package/dist/store/writes.js +125 -0
  229. package/dist/store.d.ts +107 -153
  230. package/dist/store.d.ts.map +1 -1
  231. package/dist/store.js +143 -396
  232. package/dist/stored.d.ts +58 -64
  233. package/dist/stored.d.ts.map +1 -1
  234. package/dist/stored.js +244 -156
  235. package/dist/surface.d.ts +88 -0
  236. package/dist/surface.d.ts.map +1 -0
  237. package/dist/surface.js +222 -0
  238. package/dist/sync.d.ts +20 -0
  239. package/dist/sync.d.ts.map +1 -0
  240. package/dist/sync.js +531 -0
  241. package/dist/tables.d.ts +110 -48
  242. package/dist/tables.d.ts.map +1 -1
  243. package/dist/tables.js +209 -54
  244. package/dist/tallies.d.ts +18 -0
  245. package/dist/tallies.d.ts.map +1 -0
  246. package/dist/tallies.js +129 -0
  247. package/dist/validation.d.ts.map +1 -1
  248. package/dist/validation.js +7 -4
  249. package/dist/windows.d.ts +1 -12
  250. package/dist/windows.d.ts.map +1 -1
  251. package/dist/windows.js +2 -75
  252. package/dist/wire.d.ts +16 -1
  253. package/dist/wire.d.ts.map +1 -1
  254. package/dist/wire.js +6 -0
  255. package/package.json +2 -2
  256. package/dist/fence.d.ts +0 -6
  257. package/dist/fence.d.ts.map +0 -1
  258. package/dist/fence.js +0 -10
  259. package/dist/phrase.d.ts +0 -3
  260. package/dist/phrase.d.ts.map +0 -1
  261. package/dist/phrase.js +0 -8
@@ -1,10 +1,14 @@
1
+ import { cursorOf, decodedCursor } from '@agentshouse/mdmodel';
1
2
  import { conversationChangeSet } from './change-set.js';
3
+ import { heldFacts } from './conversation-row.js';
2
4
  import { runtimeError } from './failure.js';
3
5
  import { refusedForSpace } from './durability.js';
4
6
  import { changeBatchIdentity, chatDraftRevision } from './identity.js';
5
7
  import { OFFLINE } from './outcome.js';
8
+ import { jobsOf, runningLine } from './parts.js';
9
+ import { openQuestion } from './questions.js';
6
10
  import { untilAnswered } from './recovery.js';
7
- import { capped, cursorOf, decodedCursor, FROM_START, messageOrder, precedes } from './windows.js';
11
+ import { capped, FROM_START, messageOrder, precedes } from './windows.js';
8
12
  const QUIET = {
9
13
  turn: null,
10
14
  questions: [],
@@ -33,6 +37,7 @@ export function unheldView(conversation) {
33
37
  turn: null,
34
38
  questions: [],
35
39
  process: null,
40
+ processEnded: null,
36
41
  commands: [],
37
42
  options: [],
38
43
  starting: null,
@@ -40,21 +45,22 @@ export function unheldView(conversation) {
40
45
  link: null,
41
46
  deliveryUnknown: [],
42
47
  chatDraft: null,
48
+ jobs: [],
43
49
  };
44
50
  }
45
51
  export const SESSION_STARTED = 'Session started';
46
52
  const NEW_CHAT = 'new:';
47
53
  export const HISTORY_CONVERSATIONS = 64;
54
+ // A row is read at a glance and each telling clones the list to every observing tab, while a running line changes at each Kit flush.
55
+ const ROW_INTERVAL_MS = 1_000;
48
56
  export function newChat(agent) {
49
57
  return `${NEW_CHAT}${agent}`;
50
58
  }
51
59
  function creationOf(input) {
52
60
  if (typeof input !== 'object' || input === null)
53
61
  return null;
54
- const { agent_id: agent, operation_id: operation, replaces, } = input;
55
- return typeof agent === 'string' && typeof operation === 'string' && replaces === undefined
56
- ? { agent, operation }
57
- : null;
62
+ const { agent_id: agent, operation_id: operation } = input;
63
+ return typeof agent === 'string' && typeof operation === 'string' ? { agent, operation } : null;
58
64
  }
59
65
  function startedTurn(turn, operation) {
60
66
  if (operation.op === 'begin')
@@ -221,7 +227,6 @@ function phraseOf(text) {
221
227
  }
222
228
  return text;
223
229
  }
224
- const EXPIRED = { kind: 'rejected', code: 'expired', fields: {}, retryable: false };
225
230
  const DURABLE_WRITE = {
226
231
  kind: 'rejected',
227
232
  code: 'durable-write',
@@ -255,10 +260,6 @@ function returned(draft, submission, others) {
255
260
  }, others);
256
261
  }
257
262
  export function createConversationStore(deps) {
258
- const stored = new Set();
259
- const reads = new Map();
260
- const partials = new Set();
261
- const drafts = new Map();
262
263
  const sending = new Map();
263
264
  const behind = new Set();
264
265
  const catching = new Map();
@@ -266,15 +267,16 @@ export function createConversationStore(deps) {
266
267
  const lives = new Map();
267
268
  const unsent = new Map();
268
269
  const views = new Map();
269
- const listing = new Map();
270
+ const heardNewest = new Map();
270
271
  let relisting = null;
271
- let listCursor = null;
272
272
  const creations = new Set();
273
273
  const listWatchers = new Set();
274
274
  const unlisted = new Set();
275
275
  let listKnown = false;
276
- let listHeld = false;
277
- let listKept = false;
276
+ let listOwed = false;
277
+ let listTelling = false;
278
+ let rowTimer = null;
279
+ const closing = new AbortController();
278
280
  const agentWatchers = new Set();
279
281
  const unnamed = new Set();
280
282
  let agentsShown = null;
@@ -285,8 +287,7 @@ export function createConversationStore(deps) {
285
287
  function liveOf(conversation) {
286
288
  return lives.get(conversation) ?? QUIET;
287
289
  }
288
- function viewOf(conversation, held, frame) {
289
- const draft = drafts.get(conversation);
290
+ function viewOf(conversation, held, frame, draft, ended, marked) {
290
291
  const facts = liveOf(conversation);
291
292
  return {
292
293
  conversation,
@@ -300,15 +301,17 @@ export function createConversationStore(deps) {
300
301
  turn: structuredClone(facts.turn),
301
302
  questions: structuredClone(facts.questions),
302
303
  process: facts.process === null ? null : { ...facts.process },
304
+ processEnded: ended,
303
305
  commands: structuredClone(facts.commands),
304
306
  options: structuredClone(facts.options),
305
307
  starting: facts.starting,
306
308
  subagents: structuredClone(facts.subagents),
307
309
  link: facts.link === null ? null : { ...facts.link },
308
310
  deliveryUnknown: [...facts.deliveryUnknown],
309
- chatDraft: draft === undefined || unwritten(draft)
310
- ? null
311
- : { text: draft.text, revision: draft.revision },
311
+ chatDraft: draft === null || unwritten(draft) ? null : { text: draft.text, revision: draft.revision },
312
+ jobs: facts.process?.running === false
313
+ ? []
314
+ : jobsOf(marked.filter(({ at }) => ended === null || at > ended), facts.turn),
312
315
  };
313
316
  }
314
317
  function emit(conversation) {
@@ -320,7 +323,7 @@ export function createConversationStore(deps) {
320
323
  const ready = [...view.watchers].filter((watcher) => watcher.ready);
321
324
  if (ready.length === 0)
322
325
  continue;
323
- const value = viewOf(conversation, view.window, sharedFrame(view, view.window));
326
+ const value = viewOf(conversation, view.window, sharedFrame(view, view.window), view.draft, view.ended, view.marked);
324
327
  for (const watcher of ready)
325
328
  watcher.notify(value);
326
329
  }
@@ -330,11 +333,13 @@ export function createConversationStore(deps) {
330
333
  }
331
334
  async function slide(local, conversation, arriving) {
332
335
  let count;
336
+ let marked;
333
337
  for (const view of views.values()) {
334
338
  if (view.conversation !== conversation || view.window === null)
335
339
  continue;
336
340
  if (arriving === null) {
337
341
  view.window = [];
342
+ view.marked = [];
338
343
  view.more = false;
339
344
  if (view.total !== null)
340
345
  view.total = 0;
@@ -344,6 +349,10 @@ export function createConversationStore(deps) {
344
349
  const next = windowOf(view.window, joining, view.asked.limit);
345
350
  view.window = next.messages;
346
351
  view.more ||= next.dropped;
352
+ if (arriving.length > 0) {
353
+ marked ??= await local.jobMarkers(conversation);
354
+ view.marked = marked;
355
+ }
347
356
  if (view.total !== null && arriving.length > 0) {
348
357
  count ??= await local.messageCount(conversation);
349
358
  view.total = count;
@@ -356,79 +365,178 @@ export function createConversationStore(deps) {
356
365
  return 'asking';
357
366
  return facts.turn === null ? 'idle' : 'working';
358
367
  }
359
- function entries() {
360
- return [...listing.values()].map((entry) => ({
368
+ function rowOf(conversation) {
369
+ const turn = lives.get(conversation)?.turn ?? null;
370
+ return {
371
+ state: stateOf(conversation),
372
+ started: turn?.started ?? null,
373
+ line: turn === null ? null : runningLine(turn.parts),
374
+ question: openQuestion(liveOf(conversation).questions),
375
+ };
376
+ }
377
+ async function entries() {
378
+ const list = await heldList();
379
+ if (list === null)
380
+ return null;
381
+ const held = list.length === 0
382
+ ? []
383
+ : await deps.durably((local) => local.conversationRows(list.map(({ conversation }) => conversation)));
384
+ const rows = new Map(held.map((one) => [one.conversation, one]));
385
+ return list.map((entry) => ({
361
386
  conversation: entry.conversation,
362
- agent: { identity: entry.agent, name: entry.agentName },
387
+ agent: { identity: entry.agent, name: entry.agentName, retired: entry.agentRetired },
363
388
  name: entry.name,
364
389
  created: entry.created,
365
390
  finished: entry.finished,
366
391
  outcome: entry.outcome,
367
392
  waiting: entry.waiting,
368
- state: stateOf(entry.conversation),
369
- started: lives.get(entry.conversation)?.turn?.started ?? null,
393
+ ...rowOf(entry.conversation),
394
+ ...heldFacts(rows.get(entry.conversation), entry),
370
395
  }));
371
396
  }
372
- function keepList() {
373
- if (!listKnown || listKept)
374
- return;
375
- listKept = true;
376
- queueMicrotask(() => {
377
- listKept = false;
378
- if (deps.closed())
397
+ async function heldList() {
398
+ const stored = (await (await deps.local()).facts()).conversationList;
399
+ if (stored !== null)
400
+ return stored;
401
+ return !listKnown && deps.stream() !== undefined && deps.online() ? null : [];
402
+ }
403
+ async function endedIn(conversation) {
404
+ const list = await heldList();
405
+ return list?.find((entry) => entry.conversation === conversation)?.processEnded ?? null;
406
+ }
407
+ function listChanged(change) {
408
+ void deps
409
+ .durably(async (local) => {
410
+ const { conversationList, conversationCursor } = await local.facts();
411
+ const kept = change.replacing ? [] : (conversationList ?? []);
412
+ const held = new Map(kept.map((entry) => [entry.conversation, entry]));
413
+ for (const entry of change.set) {
414
+ held.set(entry.conversation, { ...entry });
415
+ creations.delete(entry.conversation);
416
+ }
417
+ if (change.gone !== null)
418
+ held.delete(change.gone);
419
+ const list = [...held.values()];
420
+ if (JSON.stringify(list) !== JSON.stringify(conversationList)) {
421
+ await local.fact('conversationList', list);
422
+ }
423
+ if (change.cursor !== undefined) {
424
+ if (change.cursor !== conversationCursor) {
425
+ await local.fact('conversationCursor', change.cursor);
426
+ }
427
+ deps.stream()?.hold(change.cursor);
428
+ }
429
+ return list;
430
+ })
431
+ .catch(async () => (await (await deps.local()).facts()).conversationList ?? [])
432
+ .then((list) => {
433
+ if (deps.closed() || change.paged === true)
379
434
  return;
380
- const entries = [...listing.values()];
381
- const cursor = listCursor;
382
- void deps
383
- .durably(async (local) => {
384
- await local.fact('conversationList', entries);
385
- await local.fact('conversationCursor', cursor);
386
- })
387
- .catch(() => { });
435
+ endedAnew(list);
436
+ if (change.cursor !== undefined) {
437
+ listKnown = true;
438
+ settleBehind(list);
439
+ openingEnded();
440
+ deps.settled();
441
+ }
442
+ emitList();
443
+ if (!listKnown)
444
+ return;
445
+ retiredDrafts(list);
446
+ if (change.cursor === undefined &&
447
+ (list.length !== replicated.size ||
448
+ list.some(({ conversation }) => !replicated.has(conversation)))) {
449
+ deps.again();
450
+ }
451
+ }, () => { });
452
+ }
453
+ function endedAnew(list) {
454
+ const entries = new Map(list.map((entry) => [entry.conversation, entry]));
455
+ for (const view of views.values()) {
456
+ const ended = entries.get(view.conversation)?.processEnded ?? null;
457
+ if (view.window === null || ended === view.ended)
458
+ continue;
459
+ view.ended = ended;
460
+ emit(view.conversation);
461
+ }
462
+ }
463
+ let listShown = Promise.resolve();
464
+ function shownList(work) {
465
+ const reading = heldList();
466
+ listShown = listShown.then(async () => {
467
+ const list = await reading.catch(() => null);
468
+ if (list === null || deps.closed())
469
+ return;
470
+ work(list);
388
471
  });
389
472
  }
390
- function listed(entry) {
391
- listing.set(entry.conversation, { ...entry });
392
- creations.delete(entry.conversation);
473
+ async function told() {
474
+ try {
475
+ while (listOwed && !deps.closed()) {
476
+ listOwed = false;
477
+ const value = await untilAnswered(entries, closing.signal);
478
+ if (deps.closed())
479
+ return;
480
+ if (value === null)
481
+ continue;
482
+ for (const watcher of listWatchers)
483
+ watcher(value);
484
+ for (const watcher of [...unlisted]) {
485
+ unlisted.delete(watcher);
486
+ listWatchers.add(watcher);
487
+ watcher(value);
488
+ }
489
+ }
490
+ }
491
+ finally {
492
+ listTelling = false;
493
+ }
494
+ }
495
+ function tellList() {
496
+ if (deps.closed() || (listWatchers.size === 0 && unlisted.size === 0))
497
+ return;
498
+ listOwed = true;
499
+ if (listTelling)
500
+ return;
501
+ listTelling = true;
502
+ void told().catch(() => { });
503
+ }
504
+ function tellSoon() {
505
+ if (rowTimer !== null)
506
+ return;
507
+ rowTimer = setTimeout(() => {
508
+ rowTimer = null;
509
+ tellList();
510
+ }, ROW_INTERVAL_MS);
393
511
  }
394
512
  function emitList() {
395
513
  if (deps.closed())
396
514
  return;
397
- if (listWatchers.size > 0) {
398
- const value = entries();
399
- for (const watcher of listWatchers)
400
- watcher(value);
401
- }
515
+ tellList();
402
516
  emitAgents();
403
517
  learned();
404
518
  }
405
519
  function learned() {
406
520
  if (deps.closed())
407
521
  return;
408
- if (!listKnown && !listHeld && deps.stream() !== undefined && deps.online())
522
+ if (unlisted.size > 0)
523
+ tellList();
524
+ if (unnamed.size === 0)
409
525
  return;
410
- if (unlisted.size > 0) {
411
- const value = entries();
412
- for (const watcher of [...unlisted]) {
413
- unlisted.delete(watcher);
414
- listWatchers.add(watcher);
415
- watcher(value);
416
- }
417
- }
418
- if (unnamed.size > 0) {
419
- const value = agents();
526
+ shownList((list) => {
527
+ const value = agents(list);
420
528
  for (const watcher of [...unnamed]) {
421
529
  unnamed.delete(watcher);
422
530
  agentWatchers.add(watcher);
423
531
  watcher(value);
424
532
  }
425
- }
533
+ });
426
534
  }
427
- function agents() {
535
+ function agents(list) {
428
536
  const named = new Map();
429
537
  for (const agent of listedAgents ?? [])
430
538
  named.set(agent.agent, agent);
431
- for (const entry of listing.values()) {
539
+ for (const entry of list) {
432
540
  named.set(entry.agent, { name: entry.agentName, retired: entry.agentRetired });
433
541
  }
434
542
  return [...named]
@@ -458,31 +566,33 @@ export function createConversationStore(deps) {
458
566
  function emitAgents() {
459
567
  if (deps.closed() || agentWatchers.size === 0)
460
568
  return;
461
- const value = agents();
462
- const shown = JSON.stringify(value);
463
- if (shown === agentsShown)
464
- return;
465
- agentsShown = shown;
466
- for (const watcher of agentWatchers)
467
- watcher(value);
569
+ shownList((list) => {
570
+ const value = agents(list);
571
+ const shown = JSON.stringify(value);
572
+ if (shown === agentsShown)
573
+ return;
574
+ agentsShown = shown;
575
+ for (const watcher of agentWatchers)
576
+ watcher(value);
577
+ });
468
578
  }
469
579
  function setLive(conversation, next) {
470
- const before = stateOf(conversation);
471
- const began = lives.get(conversation)?.turn?.started;
580
+ const before = rowOf(conversation);
472
581
  if (next === null)
473
582
  lives.delete(conversation);
474
583
  else
475
584
  lives.set(conversation, next);
476
- if (!listing.has(conversation))
477
- return;
478
- if (stateOf(conversation) !== before || next?.turn?.started !== began)
585
+ const after = rowOf(conversation);
586
+ if (after.state !== before.state || after.started !== before.started)
479
587
  emitList();
588
+ else if (after.line !== before.line || after.question !== before.question)
589
+ tellSoon();
480
590
  }
481
- function retiredChat(conversation) {
591
+ function retiredChat(conversation, list) {
482
592
  if (!conversation.startsWith(NEW_CHAT))
483
593
  return false;
484
594
  const agent = conversation.slice(NEW_CHAT.length);
485
- return [...listing.values()].some((entry) => entry.agent === agent && entry.agentRetired);
595
+ return list.some((entry) => entry.agent === agent && entry.agentRetired);
486
596
  }
487
597
  function live(conversation, change) {
488
598
  const owed = unsent.get(conversation);
@@ -540,17 +650,23 @@ export function createConversationStore(deps) {
540
650
  }
541
651
  async function remove(leaving) {
542
652
  await deps.durably(async (local) => {
653
+ const kept = new Set([
654
+ ...(await local.conversations(leaving)).map(({ conversation }) => conversation),
655
+ ...(await local.chatDrafts())
656
+ .filter(({ conversation }) => leaving.includes(conversation))
657
+ .map(({ conversation }) => conversation),
658
+ ]);
543
659
  await local.dropConversations(leaving);
544
660
  const changed = [];
545
661
  for (const conversation of leaving) {
546
- if (stored.has(conversation) || drafts.has(conversation) || lives.has(conversation)) {
662
+ if (kept.has(conversation) || lives.has(conversation))
547
663
  changed.push(conversation);
664
+ for (const view of views.values()) {
665
+ if (view.conversation === conversation)
666
+ view.draft = null;
548
667
  }
549
- stored.delete(conversation);
550
- reads.delete(conversation);
551
- partials.delete(conversation);
552
- drafts.delete(conversation);
553
668
  behind.delete(conversation);
669
+ heardNewest.delete(conversation);
554
670
  unsent.delete(conversation);
555
671
  setLive(conversation, null);
556
672
  await slide(local, conversation, null);
@@ -559,19 +675,68 @@ export function createConversationStore(deps) {
559
675
  deps.publish(conversationChangeSet(changed));
560
676
  });
561
677
  }
562
- function missed(conversation) {
563
- const read = reads.get(conversation);
564
- const newest = listing.get(conversation)?.newest;
678
+ function newestOfListed(entry) {
679
+ if (entry === undefined)
680
+ return undefined;
681
+ const heard = heardNewest.get(entry.conversation);
682
+ if (heard === undefined)
683
+ return entry.newest;
684
+ return entry.newest === null || heard > entry.newest ? heard : entry.newest;
685
+ }
686
+ function missed(conversation, held, entry) {
687
+ const read = held?.read;
688
+ const newest = newestOfListed(entry);
565
689
  return (read === undefined ||
566
690
  newest === undefined ||
567
- partials.has(conversation) ||
691
+ held?.partial === true ||
568
692
  (newest !== null && (read === null || newest > read)));
569
693
  }
570
- function noted(conversation, message) {
571
- const entry = listing.get(conversation);
572
- if (entry === undefined || (entry.newest !== null && entry.newest >= message.position))
694
+ async function readsOf(local, conversations) {
695
+ return new Map((await local.conversations(conversations)).map((held) => [held.conversation, held]));
696
+ }
697
+ function retiredDrafts(list) {
698
+ if (!list.some((entry) => entry.agentRetired))
573
699
  return;
574
- listing.set(conversation, { ...entry, newest: message.position });
700
+ void deps
701
+ .local()
702
+ .then((local) => local.chatDrafts())
703
+ .then((held) => {
704
+ if (!deps.closed() && held.some(({ conversation }) => retiredChat(conversation, list))) {
705
+ deps.again();
706
+ }
707
+ }, () => { });
708
+ }
709
+ function settleBehind(list) {
710
+ void deps
711
+ .durably(async (local) => {
712
+ const entries = new Map(list.map((entry) => [entry.conversation, entry]));
713
+ const named = [...replicated].filter((conversation) => entries.has(conversation));
714
+ const held = await readsOf(local, named);
715
+ let owed = false;
716
+ for (const conversation of named) {
717
+ if (!missed(conversation, held.get(conversation), entries.get(conversation))) {
718
+ behind.delete(conversation);
719
+ }
720
+ else if (!behind.has(conversation)) {
721
+ gap(conversation);
722
+ owed = true;
723
+ }
724
+ }
725
+ return owed;
726
+ })
727
+ .then((owed) => {
728
+ if (deps.closed())
729
+ return;
730
+ deps.settled();
731
+ if (owed)
732
+ deps.again();
733
+ }, () => { });
734
+ }
735
+ function noted(conversation, message) {
736
+ const heard = heardNewest.get(conversation);
737
+ if (heard === undefined || message.position > heard) {
738
+ heardNewest.set(conversation, message.position);
739
+ }
575
740
  }
576
741
  function gap(conversation) {
577
742
  gaps.set(conversation, (gaps.get(conversation) ?? 0) + 1);
@@ -581,7 +746,7 @@ export function createConversationStore(deps) {
581
746
  return deps.durably(async (local) => {
582
747
  if (!replicated.has(conversation))
583
748
  return;
584
- const read = reads.get(conversation);
749
+ const read = (await readsOf(local, [conversation])).get(conversation)?.read;
585
750
  const advances = read !== undefined &&
586
751
  !behind.has(conversation) &&
587
752
  (read === null || message.position > read);
@@ -591,19 +756,18 @@ export function createConversationStore(deps) {
591
756
  messages: [message],
592
757
  ...(advances ? { read: { at: message.position, partial: false } } : {}),
593
758
  });
594
- stored.add(conversation);
595
- if (advances)
596
- reads.set(conversation, message.position);
597
759
  sealTurns(conversation, new Set([message.turn]));
598
760
  await slide(local, conversation, [message]);
599
761
  deps.publish(conversationChangeSet([conversation]));
762
+ tellList();
600
763
  });
601
764
  }
602
765
  async function readHistories(conversations, ended) {
603
766
  const generations = new Map(conversations.map((one) => [one, gaps.get(one) ?? 0]));
767
+ const held = await readsOf(await deps.local(), conversations);
604
768
  let waiting = conversations.map((conversation) => ({
605
769
  conversation,
606
- after: reads.get(conversation) ?? undefined,
770
+ after: held.get(conversation)?.read ?? undefined,
607
771
  }));
608
772
  while (waiting.length > 0) {
609
773
  const asking = waiting.slice(0, HISTORY_CONVERSATIONS);
@@ -659,15 +823,10 @@ export function createConversationStore(deps) {
659
823
  return;
660
824
  const whole = next === undefined;
661
825
  const cursor = next ?? newestOf(arriving, from);
662
- const partial = !whole && (partials.has(conversation) || !reads.has(conversation));
826
+ const held = (await readsOf(local, [conversation])).get(conversation);
827
+ const partial = !whole && (held?.partial === true || held?.read === undefined);
663
828
  const sealing = await sealed(local, conversation, new Set(arriving.map((message) => message.turn)));
664
829
  await local.keepMessages({ conversation, messages: arriving, read: { at: cursor, partial } });
665
- stored.add(conversation);
666
- reads.set(conversation, cursor);
667
- if (partial)
668
- partials.add(conversation);
669
- else
670
- partials.delete(conversation);
671
830
  sealTurns(conversation, new Set(arriving.map((message) => message.turn)));
672
831
  if (whole && (gaps.get(conversation) ?? 0) === generation)
673
832
  behind.delete(conversation);
@@ -675,6 +834,8 @@ export function createConversationStore(deps) {
675
834
  if (arriving.length > 0 || sealing || whole) {
676
835
  deps.publish(conversationChangeSet([conversation]));
677
836
  }
837
+ if (arriving.length > 0)
838
+ tellSoon();
678
839
  });
679
840
  }
680
841
  function heard(event) {
@@ -741,17 +902,24 @@ export function createConversationStore(deps) {
741
902
  emit(conversation);
742
903
  }
743
904
  async function kept(local, conversation, following) {
744
- if (following === null || empty(following)) {
905
+ const shown = following === null || empty(following) ? null : following;
906
+ if (shown === null)
745
907
  await local.dropChatDraft(conversation);
746
- drafts.delete(conversation);
747
- }
748
- else {
749
- await local.keepChatDrafts([following]);
750
- drafts.set(conversation, following);
908
+ else
909
+ await local.keepChatDrafts([shown]);
910
+ shownDraft(conversation, shown);
911
+ }
912
+ function shownDraft(conversation, draft) {
913
+ for (const view of views.values()) {
914
+ if (view.conversation === conversation)
915
+ view.draft = draft;
751
916
  }
752
917
  }
918
+ async function draftAt(local, conversation) {
919
+ return (await local.chatDraft(conversation)) ?? undefined;
920
+ }
753
921
  async function sealed(local, conversation, turns) {
754
- const held = drafts.get(conversation);
922
+ const held = await draftAt(local, conversation);
755
923
  const submitted = held?.submitted ?? [];
756
924
  const left = submitted.filter((one) => one.turn === null || !turns.has(one.turn));
757
925
  if (held === undefined || left.length === submitted.length)
@@ -762,8 +930,8 @@ export function createConversationStore(deps) {
762
930
  function moved(agent, operation, to) {
763
931
  const from = newChat(agent);
764
932
  return deps.durably(async (local) => {
765
- const held = drafts.get(from);
766
- const earlier = (held?.moves ?? []).filter((one) => !deps.expired(one));
933
+ const held = await draftAt(local, from);
934
+ const earlier = held?.moves ?? [];
767
935
  if (earlier.includes(operation))
768
936
  return;
769
937
  const blank = {
@@ -772,18 +940,20 @@ export function createConversationStore(deps) {
772
940
  revision: chatDraftRevision(),
773
941
  };
774
942
  const { moves: _answered, ...unsent } = held ?? blank;
775
- const taken = drafts.has(to) || empty(unsent) ? [] : [{ ...unsent, conversation: to }];
943
+ const taken = (await draftAt(local, to)) !== undefined || empty(unsent)
944
+ ? []
945
+ : [{ ...unsent, conversation: to }];
776
946
  const left = { ...(taken.length === 0 ? unsent : blank), moves: [...earlier, operation] };
777
947
  await local.keepChatDrafts([left, ...taken]);
778
- drafts.set(from, left);
948
+ shownDraft(from, left);
779
949
  for (const one of taken)
780
- drafts.set(to, one);
950
+ shownDraft(to, one);
781
951
  deps.publish(conversationChangeSet([from, to]));
782
952
  });
783
953
  }
784
954
  function withDraft(conversation, next) {
785
955
  return deps.durably(async (local) => {
786
- const following = await next(drafts.get(conversation), local);
956
+ const following = await next(await draftAt(local, conversation), local);
787
957
  if (following === undefined)
788
958
  return;
789
959
  await kept(local, conversation, following);
@@ -792,10 +962,8 @@ export function createConversationStore(deps) {
792
962
  }
793
963
  async function submitted(conversation, operation, draft) {
794
964
  let submission;
795
- let earlier = false;
796
965
  await withDraft(conversation, (current) => {
797
966
  submission = current?.submitted?.find((one) => one.operation === operation);
798
- earlier = submission !== undefined;
799
967
  if (submission !== undefined)
800
968
  return undefined;
801
969
  const said = draft ?? current;
@@ -808,15 +976,7 @@ export function createConversationStore(deps) {
808
976
  submitted: [...(current?.submitted ?? []), submission],
809
977
  };
810
978
  });
811
- return { submission: submission, earlier };
812
- }
813
- function forgotten(conversation, submission) {
814
- return withDraft(conversation, (current) => {
815
- const held = current?.submitted ?? [];
816
- if (current === undefined || !held.includes(submission))
817
- return undefined;
818
- return withSubmitted(current, held.filter((one) => one !== submission));
819
- });
979
+ return submission;
820
980
  }
821
981
  async function dispatched(conversation, submission) {
822
982
  const dispatch = { answered: false };
@@ -875,7 +1035,8 @@ export function createConversationStore(deps) {
875
1035
  return deps.durably(async (local) => {
876
1036
  const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before, phrase);
877
1037
  const total = asked.total ? await local.messageCount(conversation, phrase) : null;
878
- return structuredClone(viewOf(conversation, messages, frameOf(asked, messages, more, total)));
1038
+ const marked = await local.jobMarkers(conversation);
1039
+ return structuredClone(viewOf(conversation, messages, frameOf(asked, messages, more, total), (await local.chatDraft(conversation)) ?? null, await endedIn(conversation), marked));
879
1040
  });
880
1041
  },
881
1042
  async create(input) {
@@ -883,8 +1044,10 @@ export function createConversationStore(deps) {
883
1044
  const conversation = answered(result, 'conversation_id');
884
1045
  if (conversation === null)
885
1046
  return result;
886
- if (!listing.has(conversation))
1047
+ const listed = (await heldList()) ?? [];
1048
+ if (!listed.some((entry) => entry.conversation === conversation)) {
887
1049
  creations.add(conversation);
1050
+ }
888
1051
  const creation = creationOf(input);
889
1052
  if (creation !== null)
890
1053
  await moved(creation.agent, creation.operation, conversation);
@@ -901,11 +1064,7 @@ export function createConversationStore(deps) {
901
1064
  });
902
1065
  if (held === null)
903
1066
  return DURABLE_WRITE;
904
- const { submission, earlier } = held;
905
- if (!earlier || !deps.expired(operation))
906
- return dispatched(conversation, submission);
907
- await forgotten(conversation, submission);
908
- return EXPIRED;
1067
+ return dispatched(conversation, held);
909
1068
  }
910
1069
  if (draft !== undefined) {
911
1070
  await withDraft(conversation, (current) => current?.submitted?.some((one) => one.operation === operation) === true
@@ -956,43 +1115,40 @@ export function createConversationStore(deps) {
956
1115
  return {
957
1116
  surface,
958
1117
  chatDraft,
959
- hydrate(held, kept, list, cursor) {
960
- if (list !== null && !listKnown) {
961
- listing.clear();
962
- for (const entry of list)
963
- listing.set(entry.conversation, { ...entry });
964
- listHeld = true;
965
- listCursor = cursor;
966
- deps.stream()?.hold(cursor);
967
- }
968
- stored.clear();
969
- reads.clear();
970
- partials.clear();
971
- for (const entry of held) {
972
- stored.add(entry.conversation);
973
- if (entry.read !== undefined)
974
- reads.set(entry.conversation, entry.read);
975
- if (entry.partial)
976
- partials.add(entry.conversation);
977
- }
978
- drafts.clear();
979
- for (const record of kept)
980
- drafts.set(record.conversation, record);
981
- if (listHeld)
982
- learned();
1118
+ async opened(local) {
1119
+ const { conversationList, conversationCursor } = await local.facts();
1120
+ if (conversationList === null || listKnown)
1121
+ return;
1122
+ deps.stream()?.hold(conversationCursor);
1123
+ learned();
983
1124
  },
984
1125
  async reconcile() {
985
1126
  if (!listKnown)
986
1127
  return;
987
- const named = new Set(listing.keys());
1128
+ const local = await deps.local();
1129
+ const [held, kept, { conversationList }] = await Promise.all([
1130
+ local.conversations(),
1131
+ local.chatDrafts(),
1132
+ local.facts(),
1133
+ ]);
1134
+ const list = conversationList ?? [];
1135
+ const entries = new Map(list.map((entry) => [entry.conversation, entry]));
1136
+ const named = new Set(entries.keys());
1137
+ const reads = new Map(held.map((one) => [one.conversation, one]));
988
1138
  const leaving = [
989
- ...new Set([...replicated, ...stored, ...reads.keys(), ...drafts.keys()]),
1139
+ ...new Set([
1140
+ ...replicated,
1141
+ ...reads.keys(),
1142
+ ...kept.map(({ conversation }) => conversation),
1143
+ ]),
990
1144
  ].filter((conversation) => !named.has(conversation) &&
991
1145
  !creations.has(conversation) &&
992
- (!conversation.startsWith(NEW_CHAT) || retiredChat(conversation)));
1146
+ (!conversation.startsWith(NEW_CHAT) || retiredChat(conversation, list)));
993
1147
  for (const conversation of named) {
994
- if (!replicated.has(conversation) && missed(conversation))
1148
+ if (!replicated.has(conversation) &&
1149
+ missed(conversation, reads.get(conversation), entries.get(conversation))) {
995
1150
  behind.add(conversation);
1151
+ }
996
1152
  }
997
1153
  replicated = named;
998
1154
  handReplicated();
@@ -1023,16 +1179,16 @@ export function createConversationStore(deps) {
1023
1179
  progress.complete = false;
1024
1180
  },
1025
1181
  async resend(progress) {
1026
- for (const [conversation, held] of [...drafts]) {
1027
- for (const submission of held.submitted ?? []) {
1028
- if (submission.turn !== null || sending.has(submission.operation))
1029
- continue;
1182
+ const local = await deps.local();
1183
+ const tried = new Set();
1184
+ for (const { conversation } of await local.chatDrafts()) {
1185
+ for (;;) {
1186
+ const submission = (await local.chatDraft(conversation))?.submitted?.find((one) => one.turn === null && !sending.has(one.operation) && !tried.has(one.operation));
1187
+ if (submission === undefined)
1188
+ break;
1030
1189
  if (deps.closed())
1031
1190
  return;
1032
- if (deps.expired(submission.operation)) {
1033
- await forgotten(conversation, submission);
1034
- continue;
1035
- }
1191
+ tried.add(submission.operation);
1036
1192
  const result = await dispatched(conversation, submission);
1037
1193
  if (result.kind === 'revoked-authorization')
1038
1194
  return;
@@ -1070,6 +1226,9 @@ export function createConversationStore(deps) {
1070
1226
  asked,
1071
1227
  watchers: new Set(),
1072
1228
  window: null,
1229
+ draft: null,
1230
+ ended: null,
1231
+ marked: [],
1073
1232
  more: false,
1074
1233
  total: null,
1075
1234
  };
@@ -1081,7 +1240,10 @@ export function createConversationStore(deps) {
1081
1240
  if (view.window !== null)
1082
1241
  return;
1083
1242
  const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before);
1243
+ view.draft = (await local.chatDraft(conversation)) ?? null;
1244
+ view.ended = await endedIn(conversation);
1084
1245
  view.window = messages;
1246
+ view.marked = await local.jobMarkers(conversation);
1085
1247
  view.more = more;
1086
1248
  view.total = asked.total ? await local.messageCount(conversation) : null;
1087
1249
  }), reading.signal)
@@ -1090,7 +1252,7 @@ export function createConversationStore(deps) {
1090
1252
  return;
1091
1253
  watcher.ready = true;
1092
1254
  const window = view.window ?? [];
1093
- watcher.notify(viewOf(conversation, window, sharedFrame(view, window)));
1255
+ watcher.notify(viewOf(conversation, window, sharedFrame(view, window), view.draft, view.ended, view.marked));
1094
1256
  })
1095
1257
  .catch(() => { });
1096
1258
  return () => {
@@ -1125,58 +1287,43 @@ export function createConversationStore(deps) {
1125
1287
  },
1126
1288
  receive(event) {
1127
1289
  if (deps.closed())
1128
- return false;
1129
- if (event.kind === 'list' || event.kind === 'listed' || event.kind === 'unlisted') {
1130
- if (event.kind === 'list') {
1131
- if (event.whole)
1132
- relisting ??= new Map();
1133
- for (const entry of event.entries) {
1134
- if (relisting === null)
1135
- listed(entry);
1136
- else
1137
- relisting.set(entry.conversation, { ...entry });
1138
- }
1139
- if (event.more)
1140
- return false;
1141
- if (relisting !== null) {
1142
- listing.clear();
1143
- for (const entry of relisting.values())
1144
- listed(entry);
1145
- relisting = null;
1146
- }
1147
- listCursor = event.cursor;
1148
- deps.stream()?.hold(event.cursor);
1149
- listKnown = true;
1150
- for (const conversation of replicated) {
1151
- if (!listing.has(conversation))
1152
- continue;
1153
- if (!missed(conversation))
1154
- behind.delete(conversation);
1155
- else if (!behind.has(conversation))
1156
- gap(conversation);
1157
- }
1158
- openingEnded();
1159
- }
1160
- else if (event.kind === 'listed') {
1161
- listed(event.entry);
1162
- relisting?.set(event.entry.conversation, { ...event.entry });
1163
- }
1164
- else {
1165
- listing.delete(event.conversation);
1166
- relisting?.delete(event.conversation);
1290
+ return;
1291
+ if (event.kind === 'list') {
1292
+ if (event.whole)
1293
+ relisting ??= new Map();
1294
+ if (relisting !== null) {
1295
+ for (const entry of event.entries)
1296
+ relisting.set(entry.conversation, { ...entry });
1167
1297
  }
1168
- keepList();
1169
- emitList();
1170
- return ((event.kind !== 'list' &&
1171
- listKnown &&
1172
- (listing.size !== replicated.size ||
1173
- [...listing.keys()].some((conversation) => !replicated.has(conversation)))) ||
1174
- (listKnown && [...drafts.keys()].some(retiredChat)));
1298
+ if (event.more && relisting !== null)
1299
+ return;
1300
+ const whole = relisting;
1301
+ relisting = null;
1302
+ listChanged({
1303
+ replacing: whole !== null,
1304
+ set: whole === null ? event.entries : [...whole.values()],
1305
+ gone: null,
1306
+ ...(event.more ? { paged: true } : { cursor: event.cursor }),
1307
+ });
1308
+ return;
1309
+ }
1310
+ if (event.kind === 'listed') {
1311
+ relisting?.set(event.entry.conversation, { ...event.entry });
1312
+ listChanged({ replacing: false, set: [event.entry], gone: null });
1313
+ return;
1314
+ }
1315
+ if (event.kind === 'unlisted') {
1316
+ relisting?.delete(event.conversation);
1317
+ listChanged({ replacing: false, set: [], gone: event.conversation });
1318
+ return;
1175
1319
  }
1176
1320
  heard(event);
1177
- return false;
1178
1321
  },
1179
1322
  close() {
1323
+ closing.abort();
1324
+ if (rowTimer !== null)
1325
+ clearTimeout(rowTimer);
1326
+ rowTimer = null;
1180
1327
  views.clear();
1181
1328
  lives.clear();
1182
1329
  unsent.clear();