@agentshouse/mdruntime 0.1.0-c26d7e158e11b → 0.1.0-c28842bf4495f

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