@agentshouse/mdruntime 0.1.0-c806ecaf4afed → 0.1.0-c83bc1fac9b35

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