@agentshouse/mdruntime 0.1.0-cf08ada271a7f → 0.1.0-cf08df9b663d8

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 (305) hide show
  1. package/README.md +21 -19
  2. package/dist/acceptance.d.ts.map +1 -1
  3. package/dist/acceptance.js +57 -38
  4. package/dist/act-lanes.d.ts +30 -0
  5. package/dist/act-lanes.d.ts.map +1 -0
  6. package/dist/act-lanes.js +225 -0
  7. package/dist/actions.d.ts +8 -2
  8. package/dist/actions.d.ts.map +1 -1
  9. package/dist/actions.js +173 -83
  10. package/dist/adapter.d.ts +210 -30
  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 +2 -7
  15. package/dist/{binary.d.ts → attachment.d.ts} +8 -4
  16. package/dist/attachment.d.ts.map +1 -0
  17. package/dist/attachment.js +15 -0
  18. package/dist/authored.d.ts +88 -0
  19. package/dist/authored.d.ts.map +1 -0
  20. package/dist/authored.js +345 -0
  21. package/dist/chain.d.ts +1 -1
  22. package/dist/chain.d.ts.map +1 -1
  23. package/dist/chain.js +5 -2
  24. package/dist/change.d.ts +6 -2
  25. package/dist/change.d.ts.map +1 -1
  26. package/dist/change.js +3 -0
  27. package/dist/chat-types.d.ts +0 -2
  28. package/dist/chat-types.d.ts.map +1 -1
  29. package/dist/chat-types.js +2 -4
  30. package/dist/client.d.ts +29 -24
  31. package/dist/client.d.ts.map +1 -1
  32. package/dist/client.js +49 -64
  33. package/dist/collection.d.ts +6 -1
  34. package/dist/collection.d.ts.map +1 -1
  35. package/dist/collection.js +23 -9
  36. package/dist/column.d.ts +5 -0
  37. package/dist/column.d.ts.map +1 -0
  38. package/dist/column.js +9 -0
  39. package/dist/compile.d.ts +4 -1
  40. package/dist/compile.d.ts.map +1 -1
  41. package/dist/compile.js +88 -44
  42. package/dist/connection.d.ts +1 -1
  43. package/dist/connection.d.ts.map +1 -1
  44. package/dist/connection.js +32 -17
  45. package/dist/context.d.ts.map +1 -1
  46. package/dist/context.js +1 -1
  47. package/dist/conversation-live.d.ts +26 -11
  48. package/dist/conversation-live.d.ts.map +1 -1
  49. package/dist/conversation-live.js +24 -11
  50. package/dist/conversation-row.d.ts +23 -1
  51. package/dist/conversation-row.d.ts.map +1 -1
  52. package/dist/conversation-row.js +37 -8
  53. package/dist/conversation.d.ts +45 -11
  54. package/dist/conversation.d.ts.map +1 -1
  55. package/dist/conversation.js +337 -203
  56. package/dist/declaration.js +3 -3
  57. package/dist/discovery.d.ts +3 -1
  58. package/dist/discovery.d.ts.map +1 -1
  59. package/dist/discovery.js +46 -48
  60. package/dist/download.d.ts +6 -0
  61. package/dist/download.d.ts.map +1 -0
  62. package/dist/download.js +42 -0
  63. package/dist/durability.d.ts +6 -1
  64. package/dist/durability.d.ts.map +1 -1
  65. package/dist/durability.js +7 -0
  66. package/dist/entry.d.ts +26 -1
  67. package/dist/entry.d.ts.map +1 -1
  68. package/dist/entry.js +126 -36
  69. package/dist/expectation.d.ts +25 -14
  70. package/dist/expectation.d.ts.map +1 -1
  71. package/dist/expectation.js +51 -20
  72. package/dist/failure.d.ts +2 -0
  73. package/dist/failure.d.ts.map +1 -1
  74. package/dist/failure.js +6 -1
  75. package/dist/figures.d.ts +1 -1
  76. package/dist/figures.d.ts.map +1 -1
  77. package/dist/figures.js +6 -4
  78. package/dist/file.d.ts +1 -1
  79. package/dist/file.d.ts.map +1 -1
  80. package/dist/file.js +3 -2
  81. package/dist/holdings.d.ts.map +1 -1
  82. package/dist/holdings.js +5 -3
  83. package/dist/host.d.ts +7 -2
  84. package/dist/host.d.ts.map +1 -1
  85. package/dist/host.js +94 -30
  86. package/dist/house-query.d.ts +24 -0
  87. package/dist/house-query.d.ts.map +1 -0
  88. package/dist/house-query.js +102 -0
  89. package/dist/identity.d.ts +1 -1
  90. package/dist/identity.d.ts.map +1 -1
  91. package/dist/identity.js +1 -1
  92. package/dist/index.d.ts +27 -16
  93. package/dist/index.d.ts.map +1 -1
  94. package/dist/index.js +17 -9
  95. package/dist/install.d.ts +10 -5
  96. package/dist/install.d.ts.map +1 -1
  97. package/dist/install.js +49 -55
  98. package/dist/lifecycle.d.ts +2 -2
  99. package/dist/lifecycle.d.ts.map +1 -1
  100. package/dist/lifecycle.js +74 -51
  101. package/dist/linked-file.d.ts +7 -0
  102. package/dist/linked-file.d.ts.map +1 -0
  103. package/dist/linked-file.js +13 -0
  104. package/dist/listing.d.ts +27 -20
  105. package/dist/listing.d.ts.map +1 -1
  106. package/dist/listing.js +68 -69
  107. package/dist/live.d.ts +19 -3
  108. package/dist/live.d.ts.map +1 -1
  109. package/dist/live.js +94 -36
  110. package/dist/loaded.d.ts +7 -0
  111. package/dist/loaded.d.ts.map +1 -0
  112. package/dist/loaded.js +46 -0
  113. package/dist/locks.d.ts +2 -4
  114. package/dist/locks.d.ts.map +1 -1
  115. package/dist/locks.js +10 -38
  116. package/dist/lost.d.ts.map +1 -1
  117. package/dist/lost.js +8 -20
  118. package/dist/media.d.ts +19 -3
  119. package/dist/media.d.ts.map +1 -1
  120. package/dist/media.js +181 -34
  121. package/dist/memory.d.ts +7 -3
  122. package/dist/memory.d.ts.map +1 -1
  123. package/dist/memory.js +99 -37
  124. package/dist/messages.d.ts +21 -3
  125. package/dist/messages.d.ts.map +1 -1
  126. package/dist/messages.js +48 -16
  127. package/dist/modal.d.ts +2 -0
  128. package/dist/modal.d.ts.map +1 -0
  129. package/dist/modal.js +11 -0
  130. package/dist/model.d.ts +10 -7
  131. package/dist/model.d.ts.map +1 -1
  132. package/dist/model.js +7 -1
  133. package/dist/models.d.ts.map +1 -1
  134. package/dist/models.js +18 -35
  135. package/dist/name.d.ts +0 -1
  136. package/dist/name.d.ts.map +1 -1
  137. package/dist/name.js +3 -7
  138. package/dist/observation.d.ts +29 -12
  139. package/dist/observation.d.ts.map +1 -1
  140. package/dist/observation.js +206 -69
  141. package/dist/online.d.ts +7 -0
  142. package/dist/online.d.ts.map +1 -0
  143. package/dist/online.js +29 -0
  144. package/dist/opened.d.ts +6 -6
  145. package/dist/opened.d.ts.map +1 -1
  146. package/dist/opened.js +4 -4
  147. package/dist/opening.d.ts.map +1 -1
  148. package/dist/opening.js +22 -10
  149. package/dist/outcome.d.ts +33 -13
  150. package/dist/outcome.d.ts.map +1 -1
  151. package/dist/outcome.js +36 -6
  152. package/dist/overlay.d.ts +21 -7
  153. package/dist/overlay.d.ts.map +1 -1
  154. package/dist/overlay.js +112 -39
  155. package/dist/page-metadata.d.ts +8 -0
  156. package/dist/page-metadata.d.ts.map +1 -0
  157. package/dist/page-metadata.js +19 -0
  158. package/dist/parts.d.ts +5 -9
  159. package/dist/parts.d.ts.map +1 -1
  160. package/dist/parts.js +30 -15
  161. package/dist/paths.d.ts +1 -1
  162. package/dist/paths.js +1 -1
  163. package/dist/preparation.d.ts +33 -7
  164. package/dist/preparation.d.ts.map +1 -1
  165. package/dist/preparation.js +207 -96
  166. package/dist/presence.d.ts +58 -0
  167. package/dist/presence.d.ts.map +1 -0
  168. package/dist/presence.js +231 -0
  169. package/dist/queries.d.ts +2 -2
  170. package/dist/queries.d.ts.map +1 -1
  171. package/dist/queries.js +137 -79
  172. package/dist/query.d.ts +2 -2
  173. package/dist/query.d.ts.map +1 -1
  174. package/dist/questions.d.ts +1 -5
  175. package/dist/questions.d.ts.map +1 -1
  176. package/dist/questions.js +3 -8
  177. package/dist/queue.d.ts +2 -1
  178. package/dist/queue.d.ts.map +1 -1
  179. package/dist/queue.js +76 -49
  180. package/dist/reach.d.ts +15 -6
  181. package/dist/reach.d.ts.map +1 -1
  182. package/dist/reach.js +196 -37
  183. package/dist/react.d.ts +17 -5
  184. package/dist/react.d.ts.map +1 -1
  185. package/dist/react.js +28 -5
  186. package/dist/readiness.d.ts +23 -4
  187. package/dist/readiness.d.ts.map +1 -1
  188. package/dist/readiness.js +18 -10
  189. package/dist/reads.d.ts +5 -3
  190. package/dist/reads.d.ts.map +1 -1
  191. package/dist/reads.js +26 -14
  192. package/dist/recovery.d.ts +4 -1
  193. package/dist/recovery.d.ts.map +1 -1
  194. package/dist/recovery.js +31 -8
  195. package/dist/refresh.d.ts +1 -1
  196. package/dist/refresh.js +4 -4
  197. package/dist/refusal.d.ts.map +1 -1
  198. package/dist/refusal.js +2 -4
  199. package/dist/replica.d.ts +3 -8
  200. package/dist/replica.d.ts.map +1 -1
  201. package/dist/replica.js +5 -5
  202. package/dist/retried.d.ts +30 -0
  203. package/dist/retried.d.ts.map +1 -0
  204. package/dist/retried.js +72 -0
  205. package/dist/room-draft.d.ts +31 -6
  206. package/dist/room-draft.d.ts.map +1 -1
  207. package/dist/room-draft.js +262 -36
  208. package/dist/room-drafts.d.ts.map +1 -1
  209. package/dist/room-drafts.js +12 -7
  210. package/dist/room-list.d.ts +10 -0
  211. package/dist/room-list.d.ts.map +1 -0
  212. package/dist/room-list.js +47 -0
  213. package/dist/rows.d.ts +11 -3
  214. package/dist/rows.d.ts.map +1 -1
  215. package/dist/rows.js +25 -11
  216. package/dist/run-tables.d.ts +25 -0
  217. package/dist/run-tables.d.ts.map +1 -0
  218. package/dist/run-tables.js +98 -0
  219. package/dist/running.d.ts +5 -0
  220. package/dist/running.d.ts.map +1 -0
  221. package/dist/running.js +10 -0
  222. package/dist/runs.d.ts +27 -0
  223. package/dist/runs.d.ts.map +1 -0
  224. package/dist/runs.js +92 -0
  225. package/dist/runtime.d.ts +11 -5
  226. package/dist/runtime.d.ts.map +1 -1
  227. package/dist/save.d.ts +2 -0
  228. package/dist/save.d.ts.map +1 -1
  229. package/dist/save.js +120 -135
  230. package/dist/saves.d.ts +7 -5
  231. package/dist/saves.d.ts.map +1 -1
  232. package/dist/saves.js +116 -65
  233. package/dist/seed.d.ts.map +1 -1
  234. package/dist/seed.js +4 -4
  235. package/dist/selector.d.ts +3 -0
  236. package/dist/selector.d.ts.map +1 -0
  237. package/dist/selector.js +22 -0
  238. package/dist/shown.d.ts +8 -5
  239. package/dist/shown.d.ts.map +1 -1
  240. package/dist/shown.js +95 -67
  241. package/dist/socket/lanes.d.ts +12 -4
  242. package/dist/socket/lanes.d.ts.map +1 -1
  243. package/dist/socket/lanes.js +34 -8
  244. package/dist/socket/session.d.ts +4 -1
  245. package/dist/socket/session.d.ts.map +1 -1
  246. package/dist/socket/session.js +3 -0
  247. package/dist/socket.d.ts +9 -6
  248. package/dist/socket.d.ts.map +1 -1
  249. package/dist/socket.js +17 -16
  250. package/dist/source.d.ts +42 -7
  251. package/dist/source.d.ts.map +1 -1
  252. package/dist/source.js +76 -9
  253. package/dist/store/installs.d.ts +2 -2
  254. package/dist/store/installs.d.ts.map +1 -1
  255. package/dist/store/installs.js +10 -4
  256. package/dist/store/readiness.d.ts +5 -4
  257. package/dist/store/readiness.d.ts.map +1 -1
  258. package/dist/store/readiness.js +28 -12
  259. package/dist/store/records.d.ts +31 -7
  260. package/dist/store/records.d.ts.map +1 -1
  261. package/dist/store/records.js +70 -24
  262. package/dist/store/rooms-shown.d.ts +8 -0
  263. package/dist/store/rooms-shown.d.ts.map +1 -0
  264. package/dist/store/rooms-shown.js +13 -0
  265. package/dist/store.d.ts +24 -14
  266. package/dist/store.d.ts.map +1 -1
  267. package/dist/store.js +49 -17
  268. package/dist/stored.d.ts +41 -16
  269. package/dist/stored.d.ts.map +1 -1
  270. package/dist/stored.js +215 -57
  271. package/dist/surface.d.ts +20 -8
  272. package/dist/surface.d.ts.map +1 -1
  273. package/dist/surface.js +66 -32
  274. package/dist/sync.d.ts +4 -2
  275. package/dist/sync.d.ts.map +1 -1
  276. package/dist/sync.js +109 -82
  277. package/dist/tables.d.ts +40 -52
  278. package/dist/tables.d.ts.map +1 -1
  279. package/dist/tables.js +109 -326
  280. package/dist/tallies.d.ts.map +1 -1
  281. package/dist/tallies.js +3 -4
  282. package/dist/validation.d.ts +2 -0
  283. package/dist/validation.d.ts.map +1 -1
  284. package/dist/validation.js +50 -10
  285. package/dist/wire.d.ts +68 -8
  286. package/dist/wire.d.ts.map +1 -1
  287. package/dist/wire.js +28 -11
  288. package/package.json +4 -4
  289. package/dist/attempt.d.ts +0 -16
  290. package/dist/attempt.d.ts.map +0 -1
  291. package/dist/attempt.js +0 -70
  292. package/dist/binary.d.ts.map +0 -1
  293. package/dist/binary.js +0 -8
  294. package/dist/job.d.ts +0 -5
  295. package/dist/job.d.ts.map +0 -1
  296. package/dist/job.js +0 -7
  297. package/dist/log.d.ts +0 -6
  298. package/dist/log.d.ts.map +0 -1
  299. package/dist/log.js +0 -14
  300. package/dist/socket/rooms.d.ts +0 -41
  301. package/dist/socket/rooms.d.ts.map +0 -1
  302. package/dist/socket/rooms.js +0 -90
  303. package/dist/store/fences.d.ts +0 -16
  304. package/dist/store/fences.d.ts.map +0 -1
  305. package/dist/store/fences.js +0 -26
@@ -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 {
@@ -24,13 +25,17 @@ export function unheldView(conversation) {
24
25
  options: [],
25
26
  starting: null,
26
27
  state: null,
27
- link: null,
28
+ deliveryWaiting: [],
28
29
  deliveryUnknown: [],
29
30
  chatDraft: null,
31
+ dismissedSuggestion: null,
32
+ pins: [],
30
33
  };
31
34
  }
32
35
  export const SESSION_STARTED = 'Session started';
33
36
  const NEW_CHAT = 'new:';
37
+ const LISTING = 'listing';
38
+ export const CATCHING_UP = 'catching-up';
34
39
  export const HISTORY_CONVERSATIONS = 64;
35
40
  export function newChat(agent) {
36
41
  return `${NEW_CHAT}${agent}`;
@@ -74,7 +79,7 @@ function frameOf(asked, messages, more, total) {
74
79
  };
75
80
  }
76
81
  function newestOf(arriving, from) {
77
- return arriving.reduce((top, message) => (top === null || message.position > top ? message.position : top), from);
82
+ return arriving.reduce((top, message) => (top === null || message.change > top ? message.change : top), from);
78
83
  }
79
84
  function acceptedMessage(value) {
80
85
  if (typeof value !== 'object' || value === null)
@@ -83,6 +88,7 @@ function acceptedMessage(value) {
83
88
  if (typeof record.identity !== 'string' ||
84
89
  typeof record.turn !== 'string' ||
85
90
  typeof record.position !== 'string' ||
91
+ typeof record.change !== 'string' ||
86
92
  typeof record.text !== 'string') {
87
93
  return null;
88
94
  }
@@ -90,6 +96,7 @@ function acceptedMessage(value) {
90
96
  identity: record.identity,
91
97
  turn: record.turn,
92
98
  position: record.position,
99
+ change: record.change,
93
100
  text: record.text,
94
101
  message: structuredClone(record.message),
95
102
  };
@@ -191,17 +198,17 @@ function withSubmitted(draft, submitted) {
191
198
  const { submitted: _held, ...unsent } = draft;
192
199
  return submitted.length === 0 ? unsent : { ...unsent, submitted };
193
200
  }
194
- function prepended(draft, said) {
195
- return { text: [said.text, draft.text].filter((one) => one !== '').join('\n') };
201
+ export function joinedDraft(first, second) {
202
+ return [first, second].filter((one) => one !== '').join('\n');
196
203
  }
197
204
  function returned(draft, submission, others) {
198
205
  return withSubmitted({
199
206
  conversation: draft.conversation,
200
- ...prepended(draft, submission),
207
+ text: joinedDraft(submission.text, draft.text),
201
208
  revision: chatDraftRevision(),
202
209
  }, others);
203
210
  }
204
- function viewOf(alive, conversation, held, frame, draft, ended) {
211
+ function viewOf(alive, conversation, held, frame, { draft, ended, dismissed, pins }) {
205
212
  const facts = alive.liveOf(conversation);
206
213
  return {
207
214
  conversation,
@@ -220,35 +227,44 @@ function viewOf(alive, conversation, held, frame, draft, ended) {
220
227
  options: structuredClone(facts.options),
221
228
  starting: facts.starting,
222
229
  state: sessionOf(facts, held, ended),
223
- link: facts.link === null ? null : { ...facts.link },
230
+ deliveryWaiting: [...facts.deliveryWaiting],
224
231
  deliveryUnknown: [...facts.deliveryUnknown],
225
232
  chatDraft: draft === null || unwritten(draft) ? null : { text: draft.text, revision: draft.revision },
233
+ dismissedSuggestion: dismissed,
234
+ pins: [...pins],
226
235
  };
227
236
  }
228
- function sessionOf(facts, held, ended) {
237
+ function standing(facts, saved, ended) {
229
238
  if (facts.state !== null)
230
239
  return structuredClone(facts.state);
240
+ if (saved === null)
241
+ return null;
242
+ const down = facts.process?.running === false;
243
+ return structuredClone(standingState(saved.state, saved.at, down ? saved.at : ended));
244
+ }
245
+ function sessionOf(facts, held, ended) {
231
246
  const saved = [...held]
232
247
  .reverse()
233
248
  .map(({ message }) => record(message))
234
249
  .find(({ content }) => isTurn(content));
235
250
  const at = saved === undefined ? null : text(saved, 'accepted_at');
236
- if (saved === undefined || !isTurn(saved.content) || at === null)
237
- return null;
238
- const down = facts.process?.running === false;
239
- return structuredClone(standingState(saved.content.state, at, down ? at : ended));
251
+ return standing(facts, saved === undefined || !isTurn(saved.content) || at === null
252
+ ? null
253
+ : { state: saved.content.state, at }, ended);
240
254
  }
241
255
  function emit(alive, conversation) {
242
256
  if (alive.deps.closed())
243
257
  return;
244
258
  for (const view of alive.views()) {
245
- const { window, draft, ended } = view.held.now();
259
+ const held = view.held.now();
260
+ const { window } = held;
246
261
  if (view.conversation !== conversation || window === null)
247
262
  continue;
248
263
  const ready = view.ready();
249
264
  if (ready.length === 0)
250
265
  continue;
251
- const value = viewOf(alive, conversation, window, sharedFrame(view, window), draft, ended);
266
+ const frame = sharedFrame(view, window);
267
+ const value = viewOf(alive, conversation, window, frame, held);
252
268
  for (const watcher of ready)
253
269
  watcher.notify(value);
254
270
  }
@@ -277,83 +293,119 @@ async function slide(alive, local, conversation, arriving) {
277
293
  }
278
294
  }
279
295
  }
280
- function stateOf(alive, conversation) {
281
- const facts = alive.liveOf(conversation);
282
- if (facts.questions.length > 0)
296
+ function stateOf({ activity }) {
297
+ if (activity === null)
298
+ return 'idle';
299
+ if (activity.turn === 'asking')
283
300
  return 'asking';
284
- return facts.turn === null ? 'idle' : 'working';
301
+ return activity.turn === 'running' || activity.background !== false ? 'working' : 'idle';
285
302
  }
286
303
  function rowOf(alive, conversation) {
287
- const { turn, questions } = alive.liveOf(conversation);
304
+ const { turn, questions, deliveryWaiting } = alive.liveOf(conversation);
288
305
  return {
289
- state: stateOf(alive, conversation),
290
306
  started: turn?.started ?? null,
291
307
  line: turn === null ? null : runningLine(turn.parts),
292
308
  question: openQuestion(questions),
293
309
  asked: questionAsked(questions),
310
+ deliveryWaiting: [...deliveryWaiting],
294
311
  };
295
312
  }
296
313
  async function entries(alive) {
297
- const list = await heldList(alive);
298
- if (list === null)
314
+ const listing = await heldListing(alive);
315
+ if (listing === null)
316
+ return null;
317
+ const list = listing.conversations;
318
+ const agents = new Map(listing.agents.map((agent) => [agent.id, agent]));
319
+ if (list.some((entry) => !agents.has(entry.agent)))
299
320
  return null;
300
321
  const held = list.length === 0
301
322
  ? []
302
323
  : await alive.deps.durably((local) => local.conversationRows(list.map(({ conversation }) => conversation)));
303
324
  const rows = new Map(held.map((one) => [one.conversation, one]));
304
- return list.map((entry) => ({
305
- conversation: entry.conversation,
306
- agent: { identity: entry.agent, name: entry.agentName, retired: entry.agentRetired },
307
- name: entry.name,
308
- created: entry.created,
309
- finished: entry.finished,
310
- outcome: entry.outcome,
311
- waiting: entry.waiting,
312
- ...rowOf(alive, entry.conversation),
313
- ...heldFacts(rows.get(entry.conversation), entry),
314
- }));
325
+ return list.map((entry) => {
326
+ const agent = agents.get(entry.agent);
327
+ return {
328
+ conversation: entry.conversation,
329
+ agent: { identity: agent.id, name: agent.name, retired: agent.retired_at !== null },
330
+ name: entry.name,
331
+ conversation_name: entry.conversationName,
332
+ created: entry.created,
333
+ outcome: entry.outcome,
334
+ waiting: entry.waiting,
335
+ subject: entry.subject,
336
+ room: entry.room,
337
+ model: entry.model,
338
+ effort: entry.effort,
339
+ state: stateOf(entry),
340
+ ...rowOf(alive, entry.conversation),
341
+ ...heldFacts(rows.get(entry.conversation), entry),
342
+ jobs: standing(alive.liveOf(entry.conversation), rows.get(entry.conversation)?.saved ?? null, entry.processEnded)?.running ?? [],
343
+ };
344
+ });
315
345
  }
316
- async function heldList(alive) {
317
- const stored = (await (await alive.deps.local()).facts()).conversationList;
318
- if (stored !== null)
319
- return stored;
320
- return !alive.known() && alive.deps.stream() !== undefined && alive.deps.online() ? null : [];
346
+ async function heldListing(alive) {
347
+ const { conversationList, agentList } = await (await alive.deps.local()).facts();
348
+ if (conversationList !== null && agentList !== null)
349
+ return { conversations: conversationList, agents: agentList };
350
+ return !alive.known() && alive.deps.stream() !== undefined && alive.deps.online()
351
+ ? null
352
+ : { conversations: [], agents: [] };
321
353
  }
322
354
  async function endedIn(alive, conversation) {
323
- const list = await heldList(alive);
324
- return list?.find((entry) => entry.conversation === conversation)?.processEnded ?? null;
355
+ const listing = await heldListing(alive);
356
+ return (listing?.conversations.find((entry) => entry.conversation === conversation)?.processEnded ??
357
+ null);
358
+ }
359
+ function upserted(kept, set, key) {
360
+ const held = new Map(kept.map((one) => [key(one), one]));
361
+ for (const one of set)
362
+ held.set(key(one), { ...one });
363
+ return held;
364
+ }
365
+ async function listStored(alive, local, change) {
366
+ const { conversationList, agentList, conversationCursor, readUntil } = await local.facts();
367
+ if (readUntil === null)
368
+ await local.fact('readUntil', new Date().toISOString());
369
+ const held = upserted(change.replacing ? [] : (conversationList ?? []), change.set, (entry) => entry.conversation);
370
+ for (const entry of change.set)
371
+ alive.created(entry.conversation);
372
+ if (change.gone !== null)
373
+ held.delete(change.gone);
374
+ const list = [...held.values()];
375
+ const agents = [
376
+ ...upserted(change.replacing ? [] : (agentList ?? []), change.agents, (agent) => agent.id).values(),
377
+ ];
378
+ if (JSON.stringify(list) !== JSON.stringify(conversationList)) {
379
+ await local.fact('conversationList', list);
380
+ }
381
+ if (JSON.stringify(agents) !== JSON.stringify(agentList)) {
382
+ await local.fact('agentList', agents);
383
+ }
384
+ if (change.cursor !== undefined && !alive.held.now().listRefused) {
385
+ if (change.cursor !== conversationCursor) {
386
+ await local.fact('conversationCursor', change.cursor);
387
+ }
388
+ alive.deps.stream()?.hold(change.cursor);
389
+ }
390
+ return { conversations: list, agents };
325
391
  }
326
392
  function listChanged(alive, change) {
327
393
  void alive.deps
328
- .durably(async (local) => {
329
- const { conversationList, conversationCursor } = await local.facts();
330
- const kept = change.replacing ? [] : (conversationList ?? []);
331
- const held = new Map(kept.map((entry) => [entry.conversation, entry]));
332
- for (const entry of change.set) {
333
- held.set(entry.conversation, { ...entry });
334
- alive.created(entry.conversation);
335
- }
336
- if (change.gone !== null)
337
- held.delete(change.gone);
338
- const list = [...held.values()];
339
- if (JSON.stringify(list) !== JSON.stringify(conversationList)) {
340
- await local.fact('conversationList', list);
341
- }
342
- if (change.cursor !== undefined) {
343
- if (change.cursor !== conversationCursor) {
344
- await local.fact('conversationCursor', change.cursor);
345
- }
346
- alive.deps.stream()?.hold(change.cursor);
347
- }
348
- return list;
394
+ .durably((local) => listStored(alive, local, change).catch((error) => {
395
+ alive.held.set({ listRefused: true });
396
+ throw error;
397
+ }))
398
+ .catch(async () => {
399
+ const { conversationList, agentList } = await (await alive.deps.local()).facts();
400
+ return { conversations: conversationList ?? [], agents: agentList ?? [] };
349
401
  })
350
- .catch(async () => (await (await alive.deps.local()).facts()).conversationList ?? [])
351
- .then((list) => {
402
+ .then((listing) => {
352
403
  if (alive.deps.closed() || change.paged === true)
353
404
  return;
405
+ const list = listing.conversations;
354
406
  endedAnew(alive, list);
355
407
  if (change.cursor !== undefined) {
356
- alive.held.set({ listKnown: true });
408
+ alive.held.set({ listKnown: true, listHeld: true, openingListed: true });
357
409
  settleBehind(alive, list);
358
410
  openingEnded(alive);
359
411
  alive.deps.settled();
@@ -361,11 +413,12 @@ function listChanged(alive, change) {
361
413
  emitList(alive);
362
414
  if (!alive.known())
363
415
  return;
364
- retiredDrafts(alive, list);
365
- const { replicated } = alive.held.now();
366
- if (change.cursor === undefined &&
367
- (list.length !== replicated.size ||
368
- list.some(({ conversation }) => !replicated.has(conversation)))) {
416
+ retiredDrafts(alive, listing);
417
+ const { replicated, openingRead } = alive.held.now();
418
+ if (change.cursor === undefined
419
+ ? list.length !== replicated.size ||
420
+ list.some(({ conversation }) => !replicated.has(conversation))
421
+ : openingRead) {
369
422
  alive.deps.again();
370
423
  }
371
424
  }, uncaught);
@@ -381,23 +434,23 @@ function endedAnew(alive, list) {
381
434
  emit(alive, view.conversation);
382
435
  }
383
436
  }
384
- function shownList(alive, work) {
385
- const reading = heldList(alive);
437
+ function shownListing(alive, work) {
438
+ const reading = heldListing(alive);
386
439
  alive.inOrder(async () => {
387
- const list = await reading.catch((error) => {
440
+ const listing = await reading.catch((error) => {
388
441
  uncaught(error);
389
442
  return null;
390
443
  });
391
- if (list === null || alive.deps.closed())
444
+ if (listing === null || alive.deps.closed())
392
445
  return;
393
- work(list);
446
+ work(listing);
394
447
  });
395
448
  }
396
449
  async function told(alive) {
397
450
  try {
398
451
  while (alive.held.now().listOwed && !alive.deps.closed()) {
399
452
  alive.held.set({ listOwed: false });
400
- const value = await untilAnswered(() => entries(alive), alive.closing);
453
+ const value = await untilAnswered(retrying(alive.deps.retries, 'conversations', LISTING, () => entries(alive)), alive.closing, () => true, alive.deps.recovery);
401
454
  if (alive.deps.closed())
402
455
  return;
403
456
  if (value === null)
@@ -435,75 +488,73 @@ export function conversationsLearned(alive) {
435
488
  return;
436
489
  if (alive.listeners.waiting())
437
490
  tellList(alive);
438
- if (!alive.agentListeners.waiting())
491
+ if (!alive.agentListeners.waiting() && !alive.listedListeners.waiting())
439
492
  return;
440
- shownList(alive, (list) => alive.agentListeners.welcome(agents(alive, list)));
441
- }
442
- function agents(alive, list) {
443
- const named = new Map();
444
- for (const agent of alive.held.now().listedAgents ?? [])
445
- named.set(agent.agent, agent);
446
- for (const entry of list) {
447
- named.set(entry.agent, { name: entry.agentName, retired: entry.agentRetired });
448
- }
449
- return [...named]
450
- .filter(([, agent]) => !agent.retired)
451
- .map(([identity, agent]) => ({ identity, name: agent.name }))
493
+ shownListing(alive, (listing) => {
494
+ alive.agentListeners.welcome(agentsOf(listing));
495
+ alive.listedListeners.welcome(structuredClone(listing.agents));
496
+ });
497
+ }
498
+ function agentsOf(listing) {
499
+ return listing.agents
500
+ .filter((agent) => agent.retired_at === null)
501
+ .map((agent) => ({
502
+ identity: agent.id,
503
+ name: agent.name,
504
+ cli: agent.cli,
505
+ model: agent.model,
506
+ effort: agent.effort,
507
+ model_fixed: agent.model_fixed,
508
+ environment: {
509
+ identity: agent.environment,
510
+ name: agent.environment_name,
511
+ state: agent.environment_connected ? 'online' : 'offline',
512
+ },
513
+ }))
452
514
  .sort((left, right) => left.name === right.name
453
515
  ? left.identity.localeCompare(right.identity)
454
516
  : left.name.localeCompare(right.name));
455
517
  }
456
- function readAgents(alive) {
457
- const stream = alive.deps.stream();
458
- if (stream?.agents === undefined || !alive.deps.online() || alive.held.now().agentsReading)
459
- return;
460
- const { agents: read } = stream;
461
- const { agentListeners } = alive;
462
- alive.held.set({ agentsReading: true });
463
- void alive.deps.recovery
464
- .attempt('agents', async () => {
465
- if (!alive.deps.online() || (!agentListeners.waiting() && !agentListeners.told())) {
466
- return 'done';
467
- }
468
- const listed = await read();
469
- if (listed === null)
470
- return 'done';
471
- alive.held.set({ listedAgents: listed });
472
- emitAgents(alive);
473
- return 'done';
474
- })
475
- .finally(() => {
476
- alive.held.set({ agentsReading: false });
477
- });
478
- }
479
518
  function emitAgents(alive) {
480
- if (alive.deps.closed() || !alive.agentListeners.told())
519
+ const { agentListeners, listedListeners } = alive;
520
+ if (alive.deps.closed() || (!agentListeners.told() && !listedListeners.told()))
481
521
  return;
482
- shownList(alive, (list) => {
483
- const value = agents(alive, list);
522
+ shownListing(alive, (listing) => {
523
+ const value = agentsOf(listing);
484
524
  const shown = JSON.stringify(value);
485
- if (shown === alive.held.now().agentsShown)
486
- return;
487
- alive.held.set({ agentsShown: shown });
488
- alive.agentListeners.tell(value);
525
+ if (shown !== alive.held.now().agentsShown) {
526
+ alive.held.set({ agentsShown: shown });
527
+ agentListeners.tell(value);
528
+ }
529
+ const listed = JSON.stringify(listing.agents);
530
+ if (listed !== alive.held.now().listedShown) {
531
+ alive.held.set({ listedShown: listed });
532
+ listedListeners.tell(structuredClone(listing.agents));
533
+ }
489
534
  });
490
535
  }
536
+ function ran({ state, process }) {
537
+ return JSON.stringify([state?.running ?? null, process?.running ?? null]);
538
+ }
491
539
  function setLive(alive, conversation, next) {
540
+ const was = ran(alive.liveOf(conversation));
492
541
  const before = rowOf(alive, conversation);
493
542
  alive.holdLive(conversation, next);
494
543
  const after = rowOf(alive, conversation);
495
- if (after.state !== before.state || after.started !== before.started)
544
+ if (after.started !== before.started ||
545
+ ran(alive.liveOf(conversation)) !== was ||
546
+ after.deliveryWaiting.join() !== before.deliveryWaiting.join())
496
547
  emitList(alive);
497
548
  else if (after.line !== before.line ||
498
549
  after.question !== before.question ||
499
550
  after.asked !== before.asked)
500
551
  tellSoon(alive);
501
552
  }
502
- function retiredChat(conversation, list) {
553
+ function retiredChat(conversation, listing) {
503
554
  if (!conversation.startsWith(NEW_CHAT))
504
555
  return false;
505
556
  const agent = conversation.slice(NEW_CHAT.length);
506
- return list.some((entry) => entry.agent === agent && entry.agentRetired);
557
+ return listing.agents.some((one) => one.id === agent && one.retired_at !== null);
507
558
  }
508
559
  function live(alive, conversation, change) {
509
560
  alive.factsHeard(conversation, FACTS.filter((one) => one in change));
@@ -523,7 +574,7 @@ function openingEnded(alive) {
523
574
  options: facts.has('options') ? QUIET.options : held.options,
524
575
  starting: facts.has('starting') ? QUIET.starting : held.starting,
525
576
  state: facts.has('state') ? QUIET.state : held.state,
526
- link: facts.has('link') ? QUIET.link : held.link,
577
+ deliveryWaiting: facts.has('deliveryWaiting') ? QUIET.deliveryWaiting : held.deliveryWaiting,
527
578
  deliveryUnknown: facts.has('deliveryUnknown') ? QUIET.deliveryUnknown : held.deliveryUnknown,
528
579
  });
529
580
  emit(alive, conversation);
@@ -555,19 +606,13 @@ function handReplicated(alive) {
555
606
  }
556
607
  async function remove(alive, leaving) {
557
608
  await alive.deps.durably(async (local) => {
558
- const kept = new Set([
559
- ...(await local.conversations(leaving)).map(({ conversation }) => conversation),
560
- ...(await local.chatDrafts())
561
- .filter(({ conversation }) => leaving.includes(conversation))
562
- .map(({ conversation }) => conversation),
563
- ]);
564
- await local.dropConversations(leaving);
609
+ const kept = new Set(await local.dropConversations(leaving));
565
610
  const changed = [];
566
611
  for (const conversation of leaving) {
567
612
  if (kept.has(conversation) || alive.liveHeld(conversation) !== undefined) {
568
613
  changed.push(conversation);
569
614
  }
570
- shownDraft(alive, conversation, null);
615
+ heldInViews(alive, conversation, { draft: null, dismissed: null });
571
616
  alive.forget(conversation);
572
617
  setLive(alive, conversation, null);
573
618
  await slide(alive, local, conversation, null);
@@ -596,7 +641,7 @@ async function readsOf(local, conversations) {
596
641
  return new Map((await local.conversations(conversations)).map((held) => [held.conversation, held]));
597
642
  }
598
643
  function retiredDrafts(alive, list) {
599
- if (!list.some((entry) => entry.agentRetired))
644
+ if (!list.agents.some((agent) => agent.retired_at !== null))
600
645
  return;
601
646
  void alive.deps
602
647
  .local()
@@ -639,14 +684,12 @@ function keep(alive, conversation, message) {
639
684
  if (!alive.held.now().replicated.has(conversation))
640
685
  return;
641
686
  const read = (await readsOf(local, [conversation])).get(conversation)?.read;
642
- const advances = read !== undefined &&
643
- !alive.behind(conversation) &&
644
- (read === null || message.position > read);
687
+ const advances = read !== undefined && !alive.behind(conversation) && (read === null || message.change > read);
645
688
  await sealed(alive, local, conversation, new Set([message.turn]));
646
689
  await local.keepMessages({
647
690
  conversation,
648
691
  messages: [message],
649
- ...(advances ? { read: { at: message.position, partial: false } } : {}),
692
+ ...(advances ? { read: { at: message.change, partial: false } } : {}),
650
693
  });
651
694
  sealTurns(alive, conversation, new Set([message.turn]));
652
695
  await slide(alive, local, conversation, [message]);
@@ -762,18 +805,17 @@ function heard(alive, event) {
762
805
  live(alive, conversation, { state: structuredClone(event.state) });
763
806
  return;
764
807
  }
765
- if (event.kind === 'link') {
766
- live(alive, conversation, { link: { connected: event.connected } });
767
- return;
768
- }
769
808
  if (event.kind === 'deliveries') {
770
- live(alive, conversation, { deliveryUnknown: [...event.unknown] });
809
+ live(alive, conversation, {
810
+ deliveryWaiting: [...event.waiting],
811
+ deliveryUnknown: [...event.unknown],
812
+ });
771
813
  return;
772
814
  }
773
815
  const arriving = acceptedMessage(event.message);
774
816
  if (arriving === null)
775
817
  return;
776
- alive.heardAt(conversation, arriving.position);
818
+ alive.heardAt(conversation, arriving.change);
777
819
  if (alive.held.now().replicated.has(conversation)) {
778
820
  if (alive.deps.full()) {
779
821
  alive.gapOpened(conversation);
@@ -791,19 +833,27 @@ function heard(alive, event) {
791
833
  emit(alive, conversation);
792
834
  }
793
835
  async function kept(alive, local, conversation, following) {
794
- const shown = following === null || empty(following) ? null : following;
795
- if (shown === null)
836
+ const draft = following === null || empty(following) ? null : following;
837
+ if (draft === null)
796
838
  await local.dropChatDraft(conversation);
797
839
  else
798
- await local.keepChatDrafts([shown]);
799
- shownDraft(alive, conversation, shown);
840
+ await local.keepChatDrafts([draft]);
841
+ heldInViews(alive, conversation, { draft });
800
842
  }
801
- function shownDraft(alive, conversation, draft) {
843
+ function heldInViews(alive, conversation, held) {
802
844
  for (const view of alive.views()) {
803
845
  if (view.conversation === conversation)
804
- view.held.set({ draft });
846
+ view.held.set(held);
805
847
  }
806
848
  }
849
+ async function factOf(local, name) {
850
+ return Object.fromEntries((await local.conversationFacts())
851
+ .filter((fact) => fact.name === name)
852
+ .map(({ conversation, value }) => [conversation, value]));
853
+ }
854
+ async function dismissedIn(local, conversation) {
855
+ return (await factOf(local, 'dismissedSuggestion'))[conversation] ?? null;
856
+ }
807
857
  async function draftAt(local, conversation) {
808
858
  return (await local.chatDraft(conversation)) ?? undefined;
809
859
  }
@@ -834,9 +884,9 @@ function moved(alive, agent, operation, to) {
834
884
  : [{ ...unsent, conversation: to }];
835
885
  const left = { ...(taken.length === 0 ? unsent : blank), moves: [...earlier, operation] };
836
886
  await local.keepChatDrafts([left, ...taken]);
837
- shownDraft(alive, from, left);
887
+ heldInViews(alive, from, { draft: left });
838
888
  for (const one of taken)
839
- shownDraft(alive, to, one);
889
+ heldInViews(alive, to, { draft: one });
840
890
  alive.deps.publish(conversationChangeSet([from, to]));
841
891
  });
842
892
  }
@@ -867,13 +917,10 @@ async function submitted(alive, conversation, operation, draft) {
867
917
  });
868
918
  return submission;
869
919
  }
870
- async function dispatched(alive, conversation, submission) {
920
+ async function dispatched(alive, conversation, submission, resent = false) {
871
921
  const dispatch = alive.sendingBegan(submission.operation);
872
922
  try {
873
- const result = await invoke(alive, 'conversations/send', conversation, {
874
- operation_id: submission.operation,
875
- text: submission.text,
876
- });
923
+ const result = await invoke(alive, 'conversations/send', conversation, submission.operation, { text: submission.text }, { step: 'sending', instance: `message ${submission.operation}` });
877
924
  dispatch.set({ answered: result.kind !== 'transport-uncertainty' });
878
925
  if (result.kind === 'revoked-authorization')
879
926
  return result;
@@ -881,6 +928,7 @@ async function dispatched(alive, conversation, submission) {
881
928
  alive.deps.unanswered();
882
929
  return result;
883
930
  }
931
+ alive.deps.heard(submission.operation, resent);
884
932
  const turn = answered(result, 'message_id');
885
933
  await withDraft(alive, conversation, async (current, local) => {
886
934
  const held = current?.submitted?.find((one) => one.operation === submission.operation);
@@ -900,11 +948,12 @@ async function dispatched(alive, conversation, submission) {
900
948
  alive.sendingEnded(submission.operation, dispatch);
901
949
  }
902
950
  }
903
- function invoke(alive, name, conversation, extra = { operation_id: changeBatchIdentity() }) {
904
- return alive.deps.call(name, { conversation_id: conversation, ...extra });
951
+ function invoke(alive, name, conversation, operation, extra = {}, attempt) {
952
+ return alive.deps.call(name, { conversation_id: conversation, operation_id: operation, ...extra }, attempt);
905
953
  }
906
- export function conversationSurface(alive) {
954
+ export function conversationSurface(alive, files) {
907
955
  return {
956
+ ...files,
908
957
  list() {
909
958
  return new Promise((settle) => {
910
959
  const answer = (listed) => {
@@ -922,7 +971,12 @@ export function conversationSurface(alive) {
922
971
  return alive.deps.durably(async (local) => {
923
972
  const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before, phrase);
924
973
  const total = asked.total ? await local.messageCount(conversation, phrase) : null;
925
- return structuredClone(viewOf(alive, conversation, messages, frameOf(asked, messages, more, total), (await local.chatDraft(conversation)) ?? null, await endedIn(alive, conversation)));
974
+ return structuredClone(viewOf(alive, conversation, messages, frameOf(asked, messages, more, total), {
975
+ draft: (await local.chatDraft(conversation)) ?? null,
976
+ ended: await endedIn(alive, conversation),
977
+ dismissed: await dismissedIn(local, conversation),
978
+ pins: (await local.facts()).chatPins,
979
+ }));
926
980
  });
927
981
  },
928
982
  async create(input) {
@@ -930,7 +984,7 @@ export function conversationSurface(alive) {
930
984
  const conversation = answered(result, 'conversation_id');
931
985
  if (conversation === null)
932
986
  return result;
933
- const listed = (await heldList(alive)) ?? [];
987
+ const listed = (await heldListing(alive))?.conversations ?? [];
934
988
  if (!listed.some((entry) => entry.conversation === conversation)) {
935
989
  alive.creating(conversation);
936
990
  }
@@ -959,40 +1013,90 @@ export function conversationSurface(alive) {
959
1013
  }
960
1014
  return OFFLINE;
961
1015
  },
962
- answer(conversation, answer) {
963
- return invoke(alive, 'conversations/interaction/answer', conversation, {
964
- operation_id: changeBatchIdentity(),
965
- ...answer,
1016
+ answer(conversation, answer, operation) {
1017
+ return invoke(alive, 'conversations/interaction/answer', conversation, operation, answer);
1018
+ },
1019
+ interrupt(conversation, operation) {
1020
+ return invoke(alive, 'conversations/interrupt', conversation, operation);
1021
+ },
1022
+ kill(conversation, operation) {
1023
+ return invoke(alive, 'conversations/kill', conversation, operation);
1024
+ },
1025
+ delete(conversation, operation) {
1026
+ return invoke(alive, 'conversations/delete', conversation, operation);
1027
+ },
1028
+ takeOut(conversation, message, operation) {
1029
+ return invoke(alive, 'conversations/take-out', conversation, operation, {
1030
+ message_id: message,
966
1031
  });
967
1032
  },
968
- interrupt(conversation) {
969
- return invoke(alive, 'conversations/interrupt', conversation);
1033
+ decide({ request, room, message }, decision, operation) {
1034
+ return alive.deps.call('agent-functions/decide', {
1035
+ room_ref: room,
1036
+ request,
1037
+ ...(message === undefined ? {} : { message }),
1038
+ decision,
1039
+ operation_id: operation,
1040
+ });
970
1041
  },
971
- kill(conversation) {
972
- return invoke(alive, 'conversations/kill', conversation);
1042
+ rename(conversation, name, operation) {
1043
+ return invoke(alive, 'conversations/rename', conversation, operation, { name });
973
1044
  },
974
- delete(conversation) {
975
- return invoke(alive, 'conversations/delete', conversation);
1045
+ set(conversation, settings, operation) {
1046
+ return invoke(alive, 'conversations/set', conversation, operation, settings);
1047
+ },
1048
+ retry(conversation, message, operation) {
1049
+ return invoke(alive, 'conversations/retry', conversation, operation, {
1050
+ message_id: message,
1051
+ });
1052
+ },
1053
+ async pin(figures) {
1054
+ const pins = [...figures];
1055
+ await alive.deps.durably((local) => local.fact('chatPins', pins));
1056
+ const views = [...alive.views()];
1057
+ for (const view of views)
1058
+ view.held.set({ pins });
1059
+ for (const conversation of new Set(views.map((view) => view.conversation))) {
1060
+ emit(alive, conversation);
1061
+ }
1062
+ },
1063
+ async openTurn(conversation, turn) {
1064
+ await alive.deps.durably((local) => local.keepConversationFact({ conversation, name: 'openedTurn', value: turn }));
1065
+ tellList(alive);
1066
+ },
1067
+ dismissSuggestion(conversation, suggestion) {
1068
+ return alive.deps.durably(async (local) => {
1069
+ if ((await dismissedIn(local, conversation)) === suggestion)
1070
+ return;
1071
+ await local.keepConversationFact({
1072
+ conversation,
1073
+ name: 'dismissedSuggestion',
1074
+ value: suggestion,
1075
+ });
1076
+ heldInViews(alive, conversation, { dismissed: suggestion });
1077
+ alive.deps.publish(conversationChangeSet([conversation]));
1078
+ });
976
1079
  },
977
1080
  };
978
1081
  }
1082
+ function rewritten(alive, conversation, text, revision = chatDraftRevision()) {
1083
+ return withDraft(alive, conversation, (current) => ({
1084
+ conversation,
1085
+ text: text(current?.text ?? ''),
1086
+ revision,
1087
+ ...(current === undefined ? {} : pending(current)),
1088
+ }));
1089
+ }
979
1090
  export function chatDraftSurface(alive) {
980
1091
  return {
981
- write(conversation, draft, revision = chatDraftRevision()) {
982
- return withDraft(alive, conversation, (current) => ({
983
- conversation,
984
- text: draft.text,
985
- revision,
986
- ...(current === undefined ? {} : pending(current)),
987
- }));
1092
+ write(conversation, draft, revision) {
1093
+ return rewritten(alive, conversation, () => draft.text, revision);
988
1094
  },
989
1095
  prepend(conversation, draft) {
990
- return withDraft(alive, conversation, (current) => ({
991
- conversation,
992
- ...(current === undefined ? { text: draft.text } : prepended(current, draft)),
993
- revision: chatDraftRevision(),
994
- ...(current === undefined ? {} : pending(current)),
995
- }));
1096
+ return rewritten(alive, conversation, (held) => joinedDraft(draft.text, held));
1097
+ },
1098
+ append(conversation, draft) {
1099
+ return rewritten(alive, conversation, (held) => joinedDraft(held, draft.text));
996
1100
  },
997
1101
  discard(conversation) {
998
1102
  return withDraft(alive, conversation, (current) => current === undefined
@@ -1002,31 +1106,37 @@ export function chatDraftSurface(alive) {
1002
1106
  };
1003
1107
  }
1004
1108
  export async function conversationsOpened(alive, local) {
1005
- const { conversationList, conversationCursor } = await local.facts();
1006
- if (conversationList === null || alive.known())
1109
+ const { conversationList, agentList, conversationCursor } = await local.facts();
1110
+ if (conversationList === null || agentList === null)
1111
+ return;
1112
+ alive.held.set({ listHeld: true });
1113
+ if (alive.known())
1007
1114
  return;
1008
1115
  alive.deps.stream()?.hold(conversationCursor);
1009
1116
  conversationsLearned(alive);
1010
1117
  }
1118
+ export function reconcileOpening(alive) {
1119
+ alive.held.set({ openingRead: true });
1120
+ return reconcileConversations(alive);
1121
+ }
1011
1122
  export async function reconcileConversations(alive) {
1012
1123
  if (!alive.known())
1013
1124
  return;
1014
1125
  const local = await alive.deps.local();
1015
- const [held, kept, { conversationList }] = await Promise.all([
1126
+ const [held, stored, { conversationList, agentList }] = await Promise.all([
1016
1127
  local.conversations(),
1017
- local.chatDrafts(),
1128
+ local.storedConversations(),
1018
1129
  local.facts(),
1019
1130
  ]);
1020
1131
  const list = conversationList ?? [];
1132
+ const listing = { conversations: list, agents: agentList ?? [] };
1021
1133
  const entries = new Map(list.map((entry) => [entry.conversation, entry]));
1022
1134
  const named = new Set(entries.keys());
1023
1135
  const reads = new Map(held.map((one) => [one.conversation, one]));
1024
1136
  const { replicated } = alive.held.now();
1025
- const leaving = [
1026
- ...new Set([...replicated, ...reads.keys(), ...kept.map(({ conversation }) => conversation)]),
1027
- ].filter((conversation) => !named.has(conversation) &&
1137
+ const leaving = [...new Set([...replicated, ...stored])].filter((conversation) => !named.has(conversation) &&
1028
1138
  !alive.isCreating(conversation) &&
1029
- (!conversation.startsWith(NEW_CHAT) || retiredChat(conversation, list)));
1139
+ (!conversation.startsWith(NEW_CHAT) || retiredChat(conversation, listing)));
1030
1140
  for (const conversation of named) {
1031
1141
  if (!replicated.has(conversation) &&
1032
1142
  missed(alive, conversation, reads.get(conversation), entries.get(conversation))) {
@@ -1061,6 +1171,17 @@ export async function catchUpConversations(alive, progress) {
1061
1171
  if (!alive.current())
1062
1172
  progress.complete = false;
1063
1173
  }
1174
+ export async function holdsSubmission(alive, operation) {
1175
+ if (alive.sending(operation))
1176
+ return true;
1177
+ const local = await alive.deps.local();
1178
+ for (const { conversation } of await local.chatDrafts()) {
1179
+ const held = (await local.chatDraft(conversation))?.submitted;
1180
+ if (held?.some((one) => one.operation === operation && one.turn === null) === true)
1181
+ return true;
1182
+ }
1183
+ return false;
1184
+ }
1064
1185
  export async function resendConversations(alive, progress) {
1065
1186
  const local = await alive.deps.local();
1066
1187
  const tried = new Set();
@@ -1072,7 +1193,7 @@ export async function resendConversations(alive, progress) {
1072
1193
  if (alive.deps.closed())
1073
1194
  return;
1074
1195
  tried.add(submission.operation);
1075
- const result = await dispatched(alive, conversation, submission);
1196
+ const result = await dispatched(alive, conversation, submission, true);
1076
1197
  if (result.kind === 'revoked-authorization')
1077
1198
  return;
1078
1199
  if (result.kind === 'transport-uncertainty') {
@@ -1092,22 +1213,25 @@ export function watchConversation(alive, conversation, options, deliver) {
1092
1213
  const { view, watcher } = alive.viewJoined(key, conversation, asked, deliver);
1093
1214
  const { held } = view;
1094
1215
  const reading = new AbortController();
1095
- void untilAnswered(() => alive.deps.durably(async (local) => {
1216
+ void untilAnswered(retrying(alive.deps.retries, 'reading', `window ${key}`, () => alive.deps.durably(async (local) => {
1096
1217
  if (held.now().window !== null)
1097
1218
  return;
1098
1219
  const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before);
1099
1220
  held.set({ draft: (await local.chatDraft(conversation)) ?? null });
1100
1221
  held.set({ ended: await endedIn(alive, conversation) });
1222
+ held.set({ dismissed: await dismissedIn(local, conversation) });
1223
+ held.set({ pins: (await local.facts()).chatPins });
1101
1224
  held.set({ window: messages });
1102
1225
  held.set({ more });
1103
1226
  held.set({ total: asked.total ? await local.messageCount(conversation) : null });
1104
- }), reading.signal)
1227
+ })), reading.signal)
1105
1228
  .then(() => {
1106
1229
  if (alive.deps.closed() || !view.admit(watcher))
1107
1230
  return;
1108
- const { window, draft, ended } = held.now();
1109
- const shown = window ?? [];
1110
- watcher.notify(viewOf(alive, conversation, shown, sharedFrame(view, shown), draft, ended));
1231
+ const now = held.now();
1232
+ const shown = now.window ?? [];
1233
+ const frame = sharedFrame(view, shown);
1234
+ watcher.notify(viewOf(alive, conversation, shown, frame, now));
1111
1235
  })
1112
1236
  .catch(() => { });
1113
1237
  return () => {
@@ -1123,9 +1247,13 @@ export function watchConversationList(alive, deliver) {
1123
1247
  export function watchConversationAgents(alive, deliver) {
1124
1248
  alive.agentListeners.join(deliver);
1125
1249
  conversationsLearned(alive);
1126
- readAgents(alive);
1127
1250
  return () => alive.agentListeners.leave(deliver);
1128
1251
  }
1252
+ export function watchListedAgents(alive, deliver) {
1253
+ alive.listedListeners.join(deliver);
1254
+ conversationsLearned(alive);
1255
+ return () => alive.listedListeners.leave(deliver);
1256
+ }
1129
1257
  export function conversationsChanged(alive, set) {
1130
1258
  for (const conversation of set.conversations)
1131
1259
  emit(alive, conversation);
@@ -1137,26 +1265,32 @@ export function receiveConversationEvent(alive, event) {
1137
1265
  if (event.whole)
1138
1266
  alive.relistBegun();
1139
1267
  if (alive.relisting())
1140
- alive.relistHeard(event.entries);
1268
+ alive.relistHeard(event.entries, event.agents);
1141
1269
  if (event.more && alive.relisting())
1142
1270
  return;
1143
1271
  const whole = alive.relistTaken();
1144
1272
  listChanged(alive, {
1145
1273
  replacing: whole !== null,
1146
- set: whole === null ? event.entries : [...whole.values()],
1274
+ set: whole === null ? event.entries : [...whole.conversations.values()],
1275
+ agents: whole === null ? event.agents : [...whole.agents.values()],
1147
1276
  gone: null,
1148
1277
  ...(event.more ? { paged: true } : { cursor: event.cursor }),
1149
1278
  });
1150
1279
  return;
1151
1280
  }
1152
1281
  if (event.kind === 'listed') {
1153
- alive.relistHeard([event.entry]);
1154
- listChanged(alive, { replacing: false, set: [event.entry], gone: null });
1282
+ alive.relistHeard([event.entry], []);
1283
+ listChanged(alive, { replacing: false, set: [event.entry], agents: [], gone: null });
1284
+ return;
1285
+ }
1286
+ if (event.kind === 'agent') {
1287
+ alive.relistHeard([], [event.agent]);
1288
+ listChanged(alive, { replacing: false, set: [], agents: [event.agent], gone: null });
1155
1289
  return;
1156
1290
  }
1157
1291
  if (event.kind === 'unlisted') {
1158
1292
  alive.relistDropped(event.conversation);
1159
- listChanged(alive, { replacing: false, set: [], gone: event.conversation });
1293
+ listChanged(alive, { replacing: false, set: [], agents: [], gone: event.conversation });
1160
1294
  return;
1161
1295
  }
1162
1296
  heard(alive, event);