@agentshouse/mdruntime 0.1.0-caf080a9a9c60 → 0.1.0-cb0700ba4630b

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 (236) hide show
  1. package/README.md +18 -16
  2. package/dist/acceptance.d.ts.map +1 -1
  3. package/dist/acceptance.js +2 -21
  4. package/dist/act-lanes.d.ts +1 -0
  5. package/dist/act-lanes.d.ts.map +1 -1
  6. package/dist/act-lanes.js +53 -5
  7. package/dist/actions.d.ts +1 -0
  8. package/dist/actions.d.ts.map +1 -1
  9. package/dist/actions.js +102 -50
  10. package/dist/adapter.d.ts +85 -14
  11. package/dist/adapter.d.ts.map +1 -1
  12. package/dist/attached-model.d.ts +0 -1
  13. package/dist/attached-model.d.ts.map +1 -1
  14. package/dist/attached-model.js +0 -5
  15. package/dist/authored.d.ts +88 -0
  16. package/dist/authored.d.ts.map +1 -0
  17. package/dist/authored.js +345 -0
  18. package/dist/change.d.ts +4 -0
  19. package/dist/change.d.ts.map +1 -1
  20. package/dist/change.js +3 -0
  21. package/dist/chat-types.d.ts +0 -2
  22. package/dist/chat-types.d.ts.map +1 -1
  23. package/dist/chat-types.js +0 -2
  24. package/dist/client.d.ts +14 -11
  25. package/dist/client.d.ts.map +1 -1
  26. package/dist/client.js +18 -46
  27. package/dist/collection.d.ts +5 -1
  28. package/dist/collection.d.ts.map +1 -1
  29. package/dist/collection.js +16 -8
  30. package/dist/column.d.ts +5 -0
  31. package/dist/column.d.ts.map +1 -0
  32. package/dist/column.js +9 -0
  33. package/dist/compile.d.ts.map +1 -1
  34. package/dist/compile.js +2 -3
  35. package/dist/connection.d.ts.map +1 -1
  36. package/dist/connection.js +11 -12
  37. package/dist/context.d.ts.map +1 -1
  38. package/dist/context.js +1 -1
  39. package/dist/conversation-live.d.ts +18 -6
  40. package/dist/conversation-live.d.ts.map +1 -1
  41. package/dist/conversation-live.js +16 -6
  42. package/dist/conversation-row.d.ts +8 -1
  43. package/dist/conversation-row.d.ts.map +1 -1
  44. package/dist/conversation-row.js +20 -7
  45. package/dist/conversation.d.ts +19 -7
  46. package/dist/conversation.d.ts.map +1 -1
  47. package/dist/conversation.js +226 -163
  48. package/dist/discovery.d.ts +2 -0
  49. package/dist/discovery.d.ts.map +1 -1
  50. package/dist/discovery.js +15 -6
  51. package/dist/download.d.ts +6 -0
  52. package/dist/download.d.ts.map +1 -0
  53. package/dist/download.js +42 -0
  54. package/dist/durability.d.ts +2 -0
  55. package/dist/durability.d.ts.map +1 -1
  56. package/dist/durability.js +7 -0
  57. package/dist/entry.d.ts +5 -0
  58. package/dist/entry.d.ts.map +1 -1
  59. package/dist/entry.js +22 -11
  60. package/dist/failure.d.ts.map +1 -1
  61. package/dist/failure.js +3 -1
  62. package/dist/figures.d.ts.map +1 -1
  63. package/dist/figures.js +4 -2
  64. package/dist/file.d.ts +1 -1
  65. package/dist/file.d.ts.map +1 -1
  66. package/dist/file.js +3 -2
  67. package/dist/host.d.ts +3 -0
  68. package/dist/host.d.ts.map +1 -1
  69. package/dist/host.js +32 -10
  70. package/dist/house-query.d.ts +21 -0
  71. package/dist/house-query.d.ts.map +1 -0
  72. package/dist/house-query.js +41 -0
  73. package/dist/index.d.ts +11 -6
  74. package/dist/index.d.ts.map +1 -1
  75. package/dist/index.js +7 -3
  76. package/dist/install.d.ts +9 -3
  77. package/dist/install.d.ts.map +1 -1
  78. package/dist/install.js +44 -46
  79. package/dist/lifecycle.d.ts.map +1 -1
  80. package/dist/lifecycle.js +26 -18
  81. package/dist/listing.d.ts +24 -12
  82. package/dist/listing.d.ts.map +1 -1
  83. package/dist/listing.js +50 -21
  84. package/dist/live.d.ts +5 -3
  85. package/dist/live.d.ts.map +1 -1
  86. package/dist/live.js +71 -30
  87. package/dist/loaded.d.ts +5 -0
  88. package/dist/loaded.d.ts.map +1 -0
  89. package/dist/loaded.js +40 -0
  90. package/dist/locks.d.ts.map +1 -1
  91. package/dist/locks.js +2 -3
  92. package/dist/lost.d.ts.map +1 -1
  93. package/dist/lost.js +2 -3
  94. package/dist/media.d.ts +18 -1
  95. package/dist/media.d.ts.map +1 -1
  96. package/dist/media.js +102 -24
  97. package/dist/memory.d.ts +5 -1
  98. package/dist/memory.d.ts.map +1 -1
  99. package/dist/memory.js +55 -15
  100. package/dist/messages.d.ts +12 -3
  101. package/dist/messages.d.ts.map +1 -1
  102. package/dist/messages.js +24 -10
  103. package/dist/modal.d.ts +2 -0
  104. package/dist/modal.d.ts.map +1 -0
  105. package/dist/modal.js +11 -0
  106. package/dist/model.d.ts +4 -2
  107. package/dist/model.d.ts.map +1 -1
  108. package/dist/model.js +7 -1
  109. package/dist/models.d.ts.map +1 -1
  110. package/dist/models.js +5 -8
  111. package/dist/observation.d.ts +29 -12
  112. package/dist/observation.d.ts.map +1 -1
  113. package/dist/observation.js +206 -69
  114. package/dist/online.d.ts +7 -0
  115. package/dist/online.d.ts.map +1 -0
  116. package/dist/online.js +29 -0
  117. package/dist/opening.d.ts.map +1 -1
  118. package/dist/opening.js +4 -9
  119. package/dist/outcome.d.ts +10 -1
  120. package/dist/outcome.d.ts.map +1 -1
  121. package/dist/outcome.js +17 -2
  122. package/dist/overlay.d.ts +1 -1
  123. package/dist/overlay.d.ts.map +1 -1
  124. package/dist/overlay.js +3 -5
  125. package/dist/parts.d.ts +3 -0
  126. package/dist/parts.d.ts.map +1 -1
  127. package/dist/parts.js +31 -5
  128. package/dist/preparation.d.ts +27 -6
  129. package/dist/preparation.d.ts.map +1 -1
  130. package/dist/preparation.js +137 -56
  131. package/dist/presence.d.ts +8 -2
  132. package/dist/presence.d.ts.map +1 -1
  133. package/dist/presence.js +24 -13
  134. package/dist/queries.d.ts +1 -1
  135. package/dist/queries.d.ts.map +1 -1
  136. package/dist/queries.js +63 -61
  137. package/dist/questions.d.ts +1 -0
  138. package/dist/questions.d.ts.map +1 -1
  139. package/dist/questions.js +1 -0
  140. package/dist/queue.d.ts +1 -1
  141. package/dist/queue.d.ts.map +1 -1
  142. package/dist/queue.js +54 -39
  143. package/dist/reach.d.ts +14 -6
  144. package/dist/reach.d.ts.map +1 -1
  145. package/dist/reach.js +140 -29
  146. package/dist/react.d.ts +6 -1
  147. package/dist/react.d.ts.map +1 -1
  148. package/dist/readiness.d.ts +9 -3
  149. package/dist/readiness.d.ts.map +1 -1
  150. package/dist/readiness.js +3 -5
  151. package/dist/reads.d.ts +1 -1
  152. package/dist/reads.d.ts.map +1 -1
  153. package/dist/reads.js +2 -2
  154. package/dist/recovery.d.ts +1 -0
  155. package/dist/recovery.d.ts.map +1 -1
  156. package/dist/recovery.js +2 -1
  157. package/dist/refusal.d.ts.map +1 -1
  158. package/dist/refusal.js +1 -3
  159. package/dist/replica.d.ts.map +1 -1
  160. package/dist/replica.js +2 -2
  161. package/dist/room-draft.d.ts +28 -3
  162. package/dist/room-draft.d.ts.map +1 -1
  163. package/dist/room-draft.js +230 -12
  164. package/dist/room-drafts.d.ts.map +1 -1
  165. package/dist/room-drafts.js +5 -2
  166. package/dist/room-list.d.ts +10 -0
  167. package/dist/room-list.d.ts.map +1 -0
  168. package/dist/room-list.js +45 -0
  169. package/dist/rows.d.ts +2 -1
  170. package/dist/rows.d.ts.map +1 -1
  171. package/dist/rows.js +6 -6
  172. package/dist/running.d.ts +5 -0
  173. package/dist/running.d.ts.map +1 -0
  174. package/dist/running.js +10 -0
  175. package/dist/runtime.d.ts +1 -2
  176. package/dist/runtime.d.ts.map +1 -1
  177. package/dist/save.d.ts.map +1 -1
  178. package/dist/save.js +17 -86
  179. package/dist/saves.d.ts +2 -0
  180. package/dist/saves.d.ts.map +1 -1
  181. package/dist/saves.js +42 -18
  182. package/dist/seed.d.ts.map +1 -1
  183. package/dist/seed.js +2 -2
  184. package/dist/shown.d.ts +5 -2
  185. package/dist/shown.d.ts.map +1 -1
  186. package/dist/shown.js +62 -39
  187. package/dist/socket/lanes.d.ts +10 -3
  188. package/dist/socket/lanes.d.ts.map +1 -1
  189. package/dist/socket/lanes.js +29 -6
  190. package/dist/socket/session.d.ts +2 -1
  191. package/dist/socket/session.d.ts.map +1 -1
  192. package/dist/socket/session.js +1 -0
  193. package/dist/socket.d.ts +3 -3
  194. package/dist/socket.d.ts.map +1 -1
  195. package/dist/socket.js +3 -9
  196. package/dist/source.d.ts +5 -0
  197. package/dist/source.d.ts.map +1 -1
  198. package/dist/source.js +13 -2
  199. package/dist/store/installs.d.ts +2 -2
  200. package/dist/store/installs.d.ts.map +1 -1
  201. package/dist/store/installs.js +10 -4
  202. package/dist/store/readiness.d.ts +5 -4
  203. package/dist/store/readiness.d.ts.map +1 -1
  204. package/dist/store/readiness.js +28 -12
  205. package/dist/store/records.d.ts +19 -5
  206. package/dist/store/records.d.ts.map +1 -1
  207. package/dist/store/records.js +60 -22
  208. package/dist/store/rooms-shown.d.ts +8 -0
  209. package/dist/store/rooms-shown.d.ts.map +1 -0
  210. package/dist/store/rooms-shown.js +13 -0
  211. package/dist/store.d.ts +14 -9
  212. package/dist/store.d.ts.map +1 -1
  213. package/dist/store.js +9 -4
  214. package/dist/stored.d.ts +24 -13
  215. package/dist/stored.d.ts.map +1 -1
  216. package/dist/stored.js +78 -27
  217. package/dist/surface.d.ts +12 -6
  218. package/dist/surface.d.ts.map +1 -1
  219. package/dist/surface.js +57 -25
  220. package/dist/sync.d.ts +3 -2
  221. package/dist/sync.d.ts.map +1 -1
  222. package/dist/sync.js +27 -18
  223. package/dist/tables.d.ts +16 -52
  224. package/dist/tables.d.ts.map +1 -1
  225. package/dist/tables.js +86 -324
  226. package/dist/tallies.d.ts.map +1 -1
  227. package/dist/tallies.js +1 -2
  228. package/dist/validation.d.ts.map +1 -1
  229. package/dist/validation.js +3 -9
  230. package/dist/wire.d.ts +33 -10
  231. package/dist/wire.d.ts.map +1 -1
  232. package/dist/wire.js +15 -8
  233. package/package.json +3 -3
  234. package/dist/log.d.ts +0 -6
  235. package/dist/log.d.ts.map +0 -1
  236. package/dist/log.js +0 -14
@@ -6,7 +6,7 @@ import { heldFacts } from './conversation-row.js';
6
6
  import { runtimeError, uncaught } from './failure.js';
7
7
  import { refusedForSpace } from './durability.js';
8
8
  import { changeBatchIdentity, chatDraftRevision } from './identity.js';
9
- import { OFFLINE } from './outcome.js';
9
+ import { OFFLINE, } from './outcome.js';
10
10
  import { runningLine } from './parts.js';
11
11
  import { record, text } from './read.js';
12
12
  import { openQuestion, questionAsked } from './questions.js';
@@ -29,12 +29,13 @@ export function unheldView(conversation) {
29
29
  deliveryWaiting: [],
30
30
  deliveryUnknown: [],
31
31
  chatDraft: null,
32
+ dismissedSuggestion: null,
33
+ pins: [],
32
34
  };
33
35
  }
34
36
  export const SESSION_STARTED = 'Session started';
35
37
  const NEW_CHAT = 'new:';
36
38
  const LISTING = 'listing';
37
- const AGENTS = 'agents';
38
39
  export const CATCHING_UP = 'catching-up';
39
40
  export const HISTORY_CONVERSATIONS = 64;
40
41
  export function newChat(agent) {
@@ -198,17 +199,17 @@ function withSubmitted(draft, submitted) {
198
199
  const { submitted: _held, ...unsent } = draft;
199
200
  return submitted.length === 0 ? unsent : { ...unsent, submitted };
200
201
  }
201
- function prepended(draft, said) {
202
- return { text: [said.text, draft.text].filter((one) => one !== '').join('\n') };
202
+ export function joinedDraft(first, second) {
203
+ return [first, second].filter((one) => one !== '').join('\n');
203
204
  }
204
205
  function returned(draft, submission, others) {
205
206
  return withSubmitted({
206
207
  conversation: draft.conversation,
207
- ...prepended(draft, submission),
208
+ text: joinedDraft(submission.text, draft.text),
208
209
  revision: chatDraftRevision(),
209
210
  }, others);
210
211
  }
211
- function viewOf(alive, conversation, held, frame, draft, ended) {
212
+ function viewOf(alive, conversation, held, frame, { draft, ended, dismissed, pins }) {
212
213
  const facts = alive.liveOf(conversation);
213
214
  return {
214
215
  conversation,
@@ -231,6 +232,8 @@ function viewOf(alive, conversation, held, frame, draft, ended) {
231
232
  deliveryWaiting: [...facts.deliveryWaiting],
232
233
  deliveryUnknown: [...facts.deliveryUnknown],
233
234
  chatDraft: draft === null || unwritten(draft) ? null : { text: draft.text, revision: draft.revision },
235
+ dismissedSuggestion: dismissed,
236
+ pins: [...pins],
234
237
  };
235
238
  }
236
239
  function sessionOf(facts, held, ended) {
@@ -250,13 +253,15 @@ function emit(alive, conversation) {
250
253
  if (alive.deps.closed())
251
254
  return;
252
255
  for (const view of alive.views()) {
253
- const { window, draft, ended } = view.held.now();
256
+ const held = view.held.now();
257
+ const { window } = held;
254
258
  if (view.conversation !== conversation || window === null)
255
259
  continue;
256
260
  const ready = view.ready();
257
261
  if (ready.length === 0)
258
262
  continue;
259
- const value = viewOf(alive, conversation, window, sharedFrame(view, window), draft, ended);
263
+ const frame = sharedFrame(view, window);
264
+ const value = viewOf(alive, conversation, window, frame, held);
260
265
  for (const watcher of ready)
261
266
  watcher.notify(value);
262
267
  }
@@ -302,66 +307,92 @@ function rowOf(alive, conversation) {
302
307
  };
303
308
  }
304
309
  async function entries(alive) {
305
- const list = await heldList(alive);
306
- if (list === null)
310
+ const listing = await heldListing(alive);
311
+ if (listing === null)
312
+ return null;
313
+ const list = listing.conversations;
314
+ const agents = new Map(listing.agents.map((agent) => [agent.id, agent]));
315
+ if (list.some((entry) => !agents.has(entry.agent)))
307
316
  return null;
308
317
  const held = list.length === 0
309
318
  ? []
310
319
  : await alive.deps.durably((local) => local.conversationRows(list.map(({ conversation }) => conversation)));
311
320
  const rows = new Map(held.map((one) => [one.conversation, one]));
312
- return list.map((entry) => ({
313
- conversation: entry.conversation,
314
- agent: { identity: entry.agent, name: entry.agentName, retired: entry.agentRetired },
315
- name: entry.name,
316
- created: entry.created,
317
- finished: entry.finished,
318
- outcome: entry.outcome,
319
- waiting: entry.waiting,
320
- ...rowOf(alive, entry.conversation),
321
- ...heldFacts(rows.get(entry.conversation), entry),
322
- }));
321
+ return list.map((entry) => {
322
+ const agent = agents.get(entry.agent);
323
+ return {
324
+ conversation: entry.conversation,
325
+ agent: { identity: agent.id, name: agent.name, retired: agent.retired_at !== null },
326
+ name: entry.name,
327
+ created: entry.created,
328
+ finished: entry.finished,
329
+ outcome: entry.outcome,
330
+ waiting: entry.waiting,
331
+ subject: entry.subject,
332
+ ...rowOf(alive, entry.conversation),
333
+ ...heldFacts(rows.get(entry.conversation), entry),
334
+ };
335
+ });
323
336
  }
324
- async function heldList(alive) {
325
- const stored = (await (await alive.deps.local()).facts()).conversationList;
326
- if (stored !== null)
327
- return stored;
328
- return !alive.known() && alive.deps.stream() !== undefined && alive.deps.online() ? null : [];
337
+ async function heldListing(alive) {
338
+ const { conversationList, agentList } = await (await alive.deps.local()).facts();
339
+ if (conversationList !== null && agentList !== null)
340
+ return { conversations: conversationList, agents: agentList };
341
+ return !alive.known() && alive.deps.stream() !== undefined && alive.deps.online()
342
+ ? null
343
+ : { conversations: [], agents: [] };
329
344
  }
330
345
  async function endedIn(alive, conversation) {
331
- const list = await heldList(alive);
332
- return list?.find((entry) => entry.conversation === conversation)?.processEnded ?? null;
346
+ const listing = await heldListing(alive);
347
+ return (listing?.conversations.find((entry) => entry.conversation === conversation)?.processEnded ??
348
+ null);
349
+ }
350
+ function upserted(kept, set, key) {
351
+ const held = new Map(kept.map((one) => [key(one), one]));
352
+ for (const one of set)
353
+ held.set(key(one), { ...one });
354
+ return held;
333
355
  }
334
356
  function listChanged(alive, change) {
335
357
  void alive.deps
336
358
  .durably(async (local) => {
337
- const { conversationList, conversationCursor } = await local.facts();
338
- const kept = change.replacing ? [] : (conversationList ?? []);
339
- const held = new Map(kept.map((entry) => [entry.conversation, entry]));
340
- for (const entry of change.set) {
341
- held.set(entry.conversation, { ...entry });
359
+ const { conversationList, agentList, conversationCursor, readUntil } = await local.facts();
360
+ if (readUntil === null)
361
+ await local.fact('readUntil', new Date().toISOString());
362
+ const held = upserted(change.replacing ? [] : (conversationList ?? []), change.set, (entry) => entry.conversation);
363
+ for (const entry of change.set)
342
364
  alive.created(entry.conversation);
343
- }
344
365
  if (change.gone !== null)
345
366
  held.delete(change.gone);
346
367
  const list = [...held.values()];
368
+ const agents = [
369
+ ...upserted(change.replacing ? [] : (agentList ?? []), change.agents, (agent) => agent.id).values(),
370
+ ];
347
371
  if (JSON.stringify(list) !== JSON.stringify(conversationList)) {
348
372
  await local.fact('conversationList', list);
349
373
  }
374
+ if (JSON.stringify(agents) !== JSON.stringify(agentList)) {
375
+ await local.fact('agentList', agents);
376
+ }
350
377
  if (change.cursor !== undefined) {
351
378
  if (change.cursor !== conversationCursor) {
352
379
  await local.fact('conversationCursor', change.cursor);
353
380
  }
354
381
  alive.deps.stream()?.hold(change.cursor);
355
382
  }
356
- return list;
383
+ return { conversations: list, agents };
357
384
  })
358
- .catch(async () => (await (await alive.deps.local()).facts()).conversationList ?? [])
359
- .then((list) => {
385
+ .catch(async () => {
386
+ const { conversationList, agentList } = await (await alive.deps.local()).facts();
387
+ return { conversations: conversationList ?? [], agents: agentList ?? [] };
388
+ })
389
+ .then((listing) => {
360
390
  if (alive.deps.closed() || change.paged === true)
361
391
  return;
392
+ const list = listing.conversations;
362
393
  endedAnew(alive, list);
363
394
  if (change.cursor !== undefined) {
364
- alive.held.set({ listKnown: true });
395
+ alive.held.set({ listKnown: true, openingListed: true });
365
396
  settleBehind(alive, list);
366
397
  openingEnded(alive);
367
398
  alive.deps.settled();
@@ -369,11 +400,12 @@ function listChanged(alive, change) {
369
400
  emitList(alive);
370
401
  if (!alive.known())
371
402
  return;
372
- retiredDrafts(alive, list);
373
- const { replicated } = alive.held.now();
374
- if (change.cursor === undefined &&
375
- (list.length !== replicated.size ||
376
- list.some(({ conversation }) => !replicated.has(conversation)))) {
403
+ retiredDrafts(alive, listing);
404
+ const { replicated, openingRead } = alive.held.now();
405
+ if (change.cursor === undefined
406
+ ? list.length !== replicated.size ||
407
+ list.some(({ conversation }) => !replicated.has(conversation))
408
+ : openingRead) {
377
409
  alive.deps.again();
378
410
  }
379
411
  }, uncaught);
@@ -389,16 +421,16 @@ function endedAnew(alive, list) {
389
421
  emit(alive, view.conversation);
390
422
  }
391
423
  }
392
- function shownList(alive, work) {
393
- const reading = heldList(alive);
424
+ function shownListing(alive, work) {
425
+ const reading = heldListing(alive);
394
426
  alive.inOrder(async () => {
395
- const list = await reading.catch((error) => {
427
+ const listing = await reading.catch((error) => {
396
428
  uncaught(error);
397
429
  return null;
398
430
  });
399
- if (list === null || alive.deps.closed())
431
+ if (listing === null || alive.deps.closed())
400
432
  return;
401
- work(list);
433
+ work(listing);
402
434
  });
403
435
  }
404
436
  async function told(alive) {
@@ -443,66 +475,48 @@ export function conversationsLearned(alive) {
443
475
  return;
444
476
  if (alive.listeners.waiting())
445
477
  tellList(alive);
446
- if (!alive.agentListeners.waiting())
478
+ if (!alive.agentListeners.waiting() && !alive.listedListeners.waiting())
447
479
  return;
448
- shownList(alive, (list) => alive.agentListeners.welcome(agents(alive, list)));
480
+ shownListing(alive, (listing) => {
481
+ alive.agentListeners.welcome(agentsOf(listing));
482
+ alive.listedListeners.welcome(structuredClone(listing.agents));
483
+ });
449
484
  }
450
- function agents(alive, list) {
451
- const read = new Map((alive.held.now().listedAgents ?? []).map((one) => [one.agent, one]));
452
- const named = new Map(read);
453
- for (const entry of list) {
454
- named.set(entry.agent, { name: entry.agentName, retired: entry.agentRetired });
455
- }
456
- return [...named]
457
- .filter(([, agent]) => !agent.retired)
458
- .map(([identity, agent]) => {
459
- const held = read.get(identity);
460
- return held === undefined
461
- ? { identity, name: agent.name }
462
- : {
463
- identity,
464
- name: agent.name,
465
- cli: held.cli,
466
- environment: structuredClone(held.environment),
467
- };
468
- })
485
+ function agentsOf(listing) {
486
+ return listing.agents
487
+ .filter((agent) => agent.retired_at === null)
488
+ .map((agent) => ({
489
+ identity: agent.id,
490
+ name: agent.name,
491
+ cli: agent.cli,
492
+ model: agent.model,
493
+ effort: agent.effort,
494
+ environment: {
495
+ identity: agent.environment,
496
+ name: agent.environment_name,
497
+ state: agent.environment_connected ? 'online' : 'offline',
498
+ },
499
+ }))
469
500
  .sort((left, right) => left.name === right.name
470
501
  ? left.identity.localeCompare(right.identity)
471
502
  : left.name.localeCompare(right.name));
472
503
  }
473
- function readAgents(alive) {
474
- const stream = alive.deps.stream();
475
- if (stream?.agents === undefined || !alive.deps.online() || alive.held.now().agentsReading)
476
- return;
477
- const { agents: read } = stream;
478
- const { agentListeners } = alive;
479
- alive.held.set({ agentsReading: true });
480
- void alive.deps.recovery
481
- .attempt('agents', async () => {
482
- if (!alive.deps.online() || (!agentListeners.waiting() && !agentListeners.told())) {
483
- return 'done';
484
- }
485
- const listed = await retrying(alive.deps.retries, 'conversations', AGENTS, read)();
486
- if (listed === null)
487
- return 'done';
488
- alive.held.set({ listedAgents: listed });
489
- emitAgents(alive);
490
- return 'done';
491
- })
492
- .finally(() => {
493
- alive.held.set({ agentsReading: false });
494
- });
495
- }
496
504
  function emitAgents(alive) {
497
- if (alive.deps.closed() || !alive.agentListeners.told())
505
+ const { agentListeners, listedListeners } = alive;
506
+ if (alive.deps.closed() || (!agentListeners.told() && !listedListeners.told()))
498
507
  return;
499
- shownList(alive, (list) => {
500
- const value = agents(alive, list);
508
+ shownListing(alive, (listing) => {
509
+ const value = agentsOf(listing);
501
510
  const shown = JSON.stringify(value);
502
- if (shown === alive.held.now().agentsShown)
503
- return;
504
- alive.held.set({ agentsShown: shown });
505
- alive.agentListeners.tell(value);
511
+ if (shown !== alive.held.now().agentsShown) {
512
+ alive.held.set({ agentsShown: shown });
513
+ agentListeners.tell(value);
514
+ }
515
+ const listed = JSON.stringify(listing.agents);
516
+ if (listed !== alive.held.now().listedShown) {
517
+ alive.held.set({ listedShown: listed });
518
+ listedListeners.tell(structuredClone(listing.agents));
519
+ }
506
520
  });
507
521
  }
508
522
  function setLive(alive, conversation, next) {
@@ -516,11 +530,11 @@ function setLive(alive, conversation, next) {
516
530
  after.asked !== before.asked)
517
531
  tellSoon(alive);
518
532
  }
519
- function retiredChat(conversation, list) {
533
+ function retiredChat(conversation, listing) {
520
534
  if (!conversation.startsWith(NEW_CHAT))
521
535
  return false;
522
536
  const agent = conversation.slice(NEW_CHAT.length);
523
- return list.some((entry) => entry.agent === agent && entry.agentRetired);
537
+ return listing.agents.some((one) => one.id === agent && one.retired_at !== null);
524
538
  }
525
539
  function live(alive, conversation, change) {
526
540
  alive.factsHeard(conversation, FACTS.filter((one) => one in change));
@@ -573,19 +587,13 @@ function handReplicated(alive) {
573
587
  }
574
588
  async function remove(alive, leaving) {
575
589
  await alive.deps.durably(async (local) => {
576
- const kept = new Set([
577
- ...(await local.conversations(leaving)).map(({ conversation }) => conversation),
578
- ...(await local.chatDrafts())
579
- .filter(({ conversation }) => leaving.includes(conversation))
580
- .map(({ conversation }) => conversation),
581
- ]);
582
- await local.dropConversations(leaving);
590
+ const kept = new Set(await local.dropConversations(leaving));
583
591
  const changed = [];
584
592
  for (const conversation of leaving) {
585
593
  if (kept.has(conversation) || alive.liveHeld(conversation) !== undefined) {
586
594
  changed.push(conversation);
587
595
  }
588
- shownDraft(alive, conversation, null);
596
+ heldInViews(alive, conversation, { draft: null, dismissed: null });
589
597
  alive.forget(conversation);
590
598
  setLive(alive, conversation, null);
591
599
  await slide(alive, local, conversation, null);
@@ -614,7 +622,7 @@ async function readsOf(local, conversations) {
614
622
  return new Map((await local.conversations(conversations)).map((held) => [held.conversation, held]));
615
623
  }
616
624
  function retiredDrafts(alive, list) {
617
- if (!list.some((entry) => entry.agentRetired))
625
+ if (!list.agents.some((agent) => agent.retired_at !== null))
618
626
  return;
619
627
  void alive.deps
620
628
  .local()
@@ -810,19 +818,27 @@ function heard(alive, event) {
810
818
  emit(alive, conversation);
811
819
  }
812
820
  async function kept(alive, local, conversation, following) {
813
- const shown = following === null || empty(following) ? null : following;
814
- if (shown === null)
821
+ const draft = following === null || empty(following) ? null : following;
822
+ if (draft === null)
815
823
  await local.dropChatDraft(conversation);
816
824
  else
817
- await local.keepChatDrafts([shown]);
818
- shownDraft(alive, conversation, shown);
825
+ await local.keepChatDrafts([draft]);
826
+ heldInViews(alive, conversation, { draft });
819
827
  }
820
- function shownDraft(alive, conversation, draft) {
828
+ function heldInViews(alive, conversation, held) {
821
829
  for (const view of alive.views()) {
822
830
  if (view.conversation === conversation)
823
- view.held.set({ draft });
831
+ view.held.set(held);
824
832
  }
825
833
  }
834
+ async function factOf(local, name) {
835
+ return Object.fromEntries((await local.conversationFacts())
836
+ .filter((fact) => fact.name === name)
837
+ .map(({ conversation, value }) => [conversation, value]));
838
+ }
839
+ async function dismissedIn(local, conversation) {
840
+ return (await factOf(local, 'dismissedSuggestion'))[conversation] ?? null;
841
+ }
826
842
  async function draftAt(local, conversation) {
827
843
  return (await local.chatDraft(conversation)) ?? undefined;
828
844
  }
@@ -853,9 +869,9 @@ function moved(alive, agent, operation, to) {
853
869
  : [{ ...unsent, conversation: to }];
854
870
  const left = { ...(taken.length === 0 ? unsent : blank), moves: [...earlier, operation] };
855
871
  await local.keepChatDrafts([left, ...taken]);
856
- shownDraft(alive, from, left);
872
+ heldInViews(alive, from, { draft: left });
857
873
  for (const one of taken)
858
- shownDraft(alive, to, one);
874
+ heldInViews(alive, to, { draft: one });
859
875
  alive.deps.publish(conversationChangeSet([from, to]));
860
876
  });
861
877
  }
@@ -886,7 +902,7 @@ async function submitted(alive, conversation, operation, draft) {
886
902
  });
887
903
  return submission;
888
904
  }
889
- async function dispatched(alive, conversation, submission) {
905
+ async function dispatched(alive, conversation, submission, resent = false) {
890
906
  const dispatch = alive.sendingBegan(submission.operation);
891
907
  try {
892
908
  const result = await invoke(alive, 'conversations/send', conversation, submission.operation, { text: submission.text }, { step: 'sending', instance: `message ${submission.operation}` });
@@ -897,6 +913,7 @@ async function dispatched(alive, conversation, submission) {
897
913
  alive.deps.unanswered();
898
914
  return result;
899
915
  }
916
+ alive.deps.heard(submission.operation, resent);
900
917
  const turn = answered(result, 'message_id');
901
918
  await withDraft(alive, conversation, async (current, local) => {
902
919
  const held = current?.submitted?.find((one) => one.operation === submission.operation);
@@ -919,8 +936,9 @@ async function dispatched(alive, conversation, submission) {
919
936
  function invoke(alive, name, conversation, operation, extra = {}, attempt) {
920
937
  return alive.deps.call(name, { conversation_id: conversation, operation_id: operation, ...extra }, attempt);
921
938
  }
922
- export function conversationSurface(alive) {
939
+ export function conversationSurface(alive, files) {
923
940
  return {
941
+ ...files,
924
942
  list() {
925
943
  return new Promise((settle) => {
926
944
  const answer = (listed) => {
@@ -938,7 +956,12 @@ export function conversationSurface(alive) {
938
956
  return alive.deps.durably(async (local) => {
939
957
  const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before, phrase);
940
958
  const total = asked.total ? await local.messageCount(conversation, phrase) : null;
941
- return structuredClone(viewOf(alive, conversation, messages, frameOf(asked, messages, more, total), (await local.chatDraft(conversation)) ?? null, await endedIn(alive, conversation)));
959
+ return structuredClone(viewOf(alive, conversation, messages, frameOf(asked, messages, more, total), {
960
+ draft: (await local.chatDraft(conversation)) ?? null,
961
+ ended: await endedIn(alive, conversation),
962
+ dismissed: await dismissedIn(local, conversation),
963
+ pins: (await local.facts()).chatPins,
964
+ }));
942
965
  });
943
966
  },
944
967
  async create(input) {
@@ -946,7 +969,7 @@ export function conversationSurface(alive) {
946
969
  const conversation = answered(result, 'conversation_id');
947
970
  if (conversation === null)
948
971
  return result;
949
- const listed = (await heldList(alive)) ?? [];
972
+ const listed = (await heldListing(alive))?.conversations ?? [];
950
973
  if (!listed.some((entry) => entry.conversation === conversation)) {
951
974
  alive.creating(conversation);
952
975
  }
@@ -1012,40 +1035,53 @@ export function conversationSurface(alive) {
1012
1035
  message_id: message,
1013
1036
  });
1014
1037
  },
1015
- async pins() {
1016
- return (await (await alive.deps.local()).facts()).chatPins;
1017
- },
1018
- pin(figures) {
1019
- return alive.deps.durably((local) => local.fact('chatPins', [...figures]));
1038
+ async pin(figures) {
1039
+ const pins = [...figures];
1040
+ await alive.deps.durably((local) => local.fact('chatPins', pins));
1041
+ const views = [...alive.views()];
1042
+ for (const view of views)
1043
+ view.held.set({ pins });
1044
+ for (const conversation of new Set(views.map((view) => view.conversation))) {
1045
+ emit(alive, conversation);
1046
+ }
1020
1047
  },
1021
- async openedTurns() {
1022
- return (await (await alive.deps.local()).facts()).openedTurns;
1048
+ async openTurn(conversation, turn) {
1049
+ await alive.deps.durably((local) => local.keepConversationFact({ conversation, name: 'openedTurn', value: turn }));
1050
+ tellList(alive);
1023
1051
  },
1024
- openTurn(conversation, turn) {
1052
+ dismissSuggestion(conversation, suggestion) {
1025
1053
  return alive.deps.durably(async (local) => {
1026
- const { openedTurns } = await local.facts();
1027
- await local.fact('openedTurns', { ...openedTurns, [conversation]: turn });
1054
+ if ((await dismissedIn(local, conversation)) === suggestion)
1055
+ return;
1056
+ await local.keepConversationFact({
1057
+ conversation,
1058
+ name: 'dismissedSuggestion',
1059
+ value: suggestion,
1060
+ });
1061
+ heldInViews(alive, conversation, { dismissed: suggestion });
1062
+ alive.deps.publish(conversationChangeSet([conversation]));
1028
1063
  });
1029
1064
  },
1030
1065
  };
1031
1066
  }
1067
+ function rewritten(alive, conversation, text, revision = chatDraftRevision()) {
1068
+ return withDraft(alive, conversation, (current) => ({
1069
+ conversation,
1070
+ text: text(current?.text ?? ''),
1071
+ revision,
1072
+ ...(current === undefined ? {} : pending(current)),
1073
+ }));
1074
+ }
1032
1075
  export function chatDraftSurface(alive) {
1033
1076
  return {
1034
- write(conversation, draft, revision = chatDraftRevision()) {
1035
- return withDraft(alive, conversation, (current) => ({
1036
- conversation,
1037
- text: draft.text,
1038
- revision,
1039
- ...(current === undefined ? {} : pending(current)),
1040
- }));
1077
+ write(conversation, draft, revision) {
1078
+ return rewritten(alive, conversation, () => draft.text, revision);
1041
1079
  },
1042
1080
  prepend(conversation, draft) {
1043
- return withDraft(alive, conversation, (current) => ({
1044
- conversation,
1045
- ...(current === undefined ? { text: draft.text } : prepended(current, draft)),
1046
- revision: chatDraftRevision(),
1047
- ...(current === undefined ? {} : pending(current)),
1048
- }));
1081
+ return rewritten(alive, conversation, (held) => joinedDraft(draft.text, held));
1082
+ },
1083
+ append(conversation, draft) {
1084
+ return rewritten(alive, conversation, (held) => joinedDraft(held, draft.text));
1049
1085
  },
1050
1086
  discard(conversation) {
1051
1087
  return withDraft(alive, conversation, (current) => current === undefined
@@ -1055,31 +1091,34 @@ export function chatDraftSurface(alive) {
1055
1091
  };
1056
1092
  }
1057
1093
  export async function conversationsOpened(alive, local) {
1058
- const { conversationList, conversationCursor } = await local.facts();
1059
- if (conversationList === null || alive.known())
1094
+ const { conversationList, agentList, conversationCursor } = await local.facts();
1095
+ if (conversationList === null || agentList === null || alive.known())
1060
1096
  return;
1061
1097
  alive.deps.stream()?.hold(conversationCursor);
1062
1098
  conversationsLearned(alive);
1063
1099
  }
1100
+ export function reconcileOpening(alive) {
1101
+ alive.held.set({ openingRead: true });
1102
+ return reconcileConversations(alive);
1103
+ }
1064
1104
  export async function reconcileConversations(alive) {
1065
1105
  if (!alive.known())
1066
1106
  return;
1067
1107
  const local = await alive.deps.local();
1068
- const [held, kept, { conversationList }] = await Promise.all([
1108
+ const [held, stored, { conversationList, agentList }] = await Promise.all([
1069
1109
  local.conversations(),
1070
- local.chatDrafts(),
1110
+ local.storedConversations(),
1071
1111
  local.facts(),
1072
1112
  ]);
1073
1113
  const list = conversationList ?? [];
1114
+ const listing = { conversations: list, agents: agentList ?? [] };
1074
1115
  const entries = new Map(list.map((entry) => [entry.conversation, entry]));
1075
1116
  const named = new Set(entries.keys());
1076
1117
  const reads = new Map(held.map((one) => [one.conversation, one]));
1077
1118
  const { replicated } = alive.held.now();
1078
- const leaving = [
1079
- ...new Set([...replicated, ...reads.keys(), ...kept.map(({ conversation }) => conversation)]),
1080
- ].filter((conversation) => !named.has(conversation) &&
1119
+ const leaving = [...new Set([...replicated, ...stored])].filter((conversation) => !named.has(conversation) &&
1081
1120
  !alive.isCreating(conversation) &&
1082
- (!conversation.startsWith(NEW_CHAT) || retiredChat(conversation, list)));
1121
+ (!conversation.startsWith(NEW_CHAT) || retiredChat(conversation, listing)));
1083
1122
  for (const conversation of named) {
1084
1123
  if (!replicated.has(conversation) &&
1085
1124
  missed(alive, conversation, reads.get(conversation), entries.get(conversation))) {
@@ -1114,6 +1153,17 @@ export async function catchUpConversations(alive, progress) {
1114
1153
  if (!alive.current())
1115
1154
  progress.complete = false;
1116
1155
  }
1156
+ export async function holdsSubmission(alive, operation) {
1157
+ if (alive.sending(operation))
1158
+ return true;
1159
+ const local = await alive.deps.local();
1160
+ for (const { conversation } of await local.chatDrafts()) {
1161
+ const held = (await local.chatDraft(conversation))?.submitted;
1162
+ if (held?.some((one) => one.operation === operation && one.turn === null) === true)
1163
+ return true;
1164
+ }
1165
+ return false;
1166
+ }
1117
1167
  export async function resendConversations(alive, progress) {
1118
1168
  const local = await alive.deps.local();
1119
1169
  const tried = new Set();
@@ -1125,7 +1175,7 @@ export async function resendConversations(alive, progress) {
1125
1175
  if (alive.deps.closed())
1126
1176
  return;
1127
1177
  tried.add(submission.operation);
1128
- const result = await dispatched(alive, conversation, submission);
1178
+ const result = await dispatched(alive, conversation, submission, true);
1129
1179
  if (result.kind === 'revoked-authorization')
1130
1180
  return;
1131
1181
  if (result.kind === 'transport-uncertainty') {
@@ -1151,6 +1201,8 @@ export function watchConversation(alive, conversation, options, deliver) {
1151
1201
  const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before);
1152
1202
  held.set({ draft: (await local.chatDraft(conversation)) ?? null });
1153
1203
  held.set({ ended: await endedIn(alive, conversation) });
1204
+ held.set({ dismissed: await dismissedIn(local, conversation) });
1205
+ held.set({ pins: (await local.facts()).chatPins });
1154
1206
  held.set({ window: messages });
1155
1207
  held.set({ more });
1156
1208
  held.set({ total: asked.total ? await local.messageCount(conversation) : null });
@@ -1158,9 +1210,10 @@ export function watchConversation(alive, conversation, options, deliver) {
1158
1210
  .then(() => {
1159
1211
  if (alive.deps.closed() || !view.admit(watcher))
1160
1212
  return;
1161
- const { window, draft, ended } = held.now();
1162
- const shown = window ?? [];
1163
- watcher.notify(viewOf(alive, conversation, shown, sharedFrame(view, shown), draft, ended));
1213
+ const now = held.now();
1214
+ const shown = now.window ?? [];
1215
+ const frame = sharedFrame(view, shown);
1216
+ watcher.notify(viewOf(alive, conversation, shown, frame, now));
1164
1217
  })
1165
1218
  .catch(() => { });
1166
1219
  return () => {
@@ -1176,9 +1229,13 @@ export function watchConversationList(alive, deliver) {
1176
1229
  export function watchConversationAgents(alive, deliver) {
1177
1230
  alive.agentListeners.join(deliver);
1178
1231
  conversationsLearned(alive);
1179
- readAgents(alive);
1180
1232
  return () => alive.agentListeners.leave(deliver);
1181
1233
  }
1234
+ export function watchListedAgents(alive, deliver) {
1235
+ alive.listedListeners.join(deliver);
1236
+ conversationsLearned(alive);
1237
+ return () => alive.listedListeners.leave(deliver);
1238
+ }
1182
1239
  export function conversationsChanged(alive, set) {
1183
1240
  for (const conversation of set.conversations)
1184
1241
  emit(alive, conversation);
@@ -1190,26 +1247,32 @@ export function receiveConversationEvent(alive, event) {
1190
1247
  if (event.whole)
1191
1248
  alive.relistBegun();
1192
1249
  if (alive.relisting())
1193
- alive.relistHeard(event.entries);
1250
+ alive.relistHeard(event.entries, event.agents);
1194
1251
  if (event.more && alive.relisting())
1195
1252
  return;
1196
1253
  const whole = alive.relistTaken();
1197
1254
  listChanged(alive, {
1198
1255
  replacing: whole !== null,
1199
- set: whole === null ? event.entries : [...whole.values()],
1256
+ set: whole === null ? event.entries : [...whole.conversations.values()],
1257
+ agents: whole === null ? event.agents : [...whole.agents.values()],
1200
1258
  gone: null,
1201
1259
  ...(event.more ? { paged: true } : { cursor: event.cursor }),
1202
1260
  });
1203
1261
  return;
1204
1262
  }
1205
1263
  if (event.kind === 'listed') {
1206
- alive.relistHeard([event.entry]);
1207
- listChanged(alive, { replacing: false, set: [event.entry], gone: null });
1264
+ alive.relistHeard([event.entry], []);
1265
+ listChanged(alive, { replacing: false, set: [event.entry], agents: [], gone: null });
1266
+ return;
1267
+ }
1268
+ if (event.kind === 'agent') {
1269
+ alive.relistHeard([], [event.agent]);
1270
+ listChanged(alive, { replacing: false, set: [], agents: [event.agent], gone: null });
1208
1271
  return;
1209
1272
  }
1210
1273
  if (event.kind === 'unlisted') {
1211
1274
  alive.relistDropped(event.conversation);
1212
- listChanged(alive, { replacing: false, set: [], gone: event.conversation });
1275
+ listChanged(alive, { replacing: false, set: [], agents: [], gone: event.conversation });
1213
1276
  return;
1214
1277
  }
1215
1278
  heard(alive, event);