@agentshouse/mdruntime 0.1.0-cf281e8224a5d → 0.1.0-cf4cf1851ebaa

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 (280) hide show
  1. package/README.md +21 -19
  2. package/dist/acceptance.d.ts +0 -3
  3. package/dist/acceptance.d.ts.map +1 -1
  4. package/dist/acceptance.js +72 -158
  5. package/dist/act-lanes.d.ts +30 -0
  6. package/dist/act-lanes.d.ts.map +1 -0
  7. package/dist/act-lanes.js +225 -0
  8. package/dist/actions.d.ts +8 -2
  9. package/dist/actions.d.ts.map +1 -1
  10. package/dist/actions.js +142 -72
  11. package/dist/adapter.d.ts +120 -58
  12. package/dist/adapter.d.ts.map +1 -1
  13. package/dist/attached-model.d.ts +0 -1
  14. package/dist/attached-model.d.ts.map +1 -1
  15. package/dist/attached-model.js +0 -5
  16. package/dist/authored.d.ts +88 -0
  17. package/dist/authored.d.ts.map +1 -0
  18. package/dist/authored.js +345 -0
  19. package/dist/bootstrap.d.ts +1 -1
  20. package/dist/bootstrap.d.ts.map +1 -1
  21. package/dist/bootstrap.js +6 -19
  22. package/dist/change-set.d.ts +4 -0
  23. package/dist/change-set.d.ts.map +1 -1
  24. package/dist/change-set.js +6 -0
  25. package/dist/change.d.ts +4 -0
  26. package/dist/change.d.ts.map +1 -1
  27. package/dist/change.js +3 -0
  28. package/dist/chat-types.d.ts +0 -2
  29. package/dist/chat-types.d.ts.map +1 -1
  30. package/dist/chat-types.js +0 -2
  31. package/dist/client.d.ts +16 -20
  32. package/dist/client.d.ts.map +1 -1
  33. package/dist/client.js +41 -56
  34. package/dist/collection.d.ts +26 -4
  35. package/dist/collection.d.ts.map +1 -1
  36. package/dist/collection.js +28 -10
  37. package/dist/column.d.ts +5 -0
  38. package/dist/column.d.ts.map +1 -0
  39. package/dist/column.js +9 -0
  40. package/dist/compile.d.ts +2 -0
  41. package/dist/compile.d.ts.map +1 -1
  42. package/dist/compile.js +17 -5
  43. package/dist/connection.d.ts +1 -1
  44. package/dist/connection.d.ts.map +1 -1
  45. package/dist/connection.js +35 -26
  46. package/dist/context.d.ts.map +1 -1
  47. package/dist/context.js +1 -1
  48. package/dist/conversation-live.d.ts +28 -12
  49. package/dist/conversation-live.d.ts.map +1 -1
  50. package/dist/conversation-live.js +25 -12
  51. package/dist/conversation-row.d.ts +8 -1
  52. package/dist/conversation-row.d.ts.map +1 -1
  53. package/dist/conversation-row.js +20 -7
  54. package/dist/conversation.d.ts +37 -12
  55. package/dist/conversation.d.ts.map +1 -1
  56. package/dist/conversation.js +325 -234
  57. package/dist/declaration.d.ts +10 -0
  58. package/dist/declaration.d.ts.map +1 -0
  59. package/dist/declaration.js +24 -0
  60. package/dist/discovery.d.ts +3 -1
  61. package/dist/discovery.d.ts.map +1 -1
  62. package/dist/discovery.js +47 -47
  63. package/dist/download.d.ts +6 -0
  64. package/dist/download.d.ts.map +1 -0
  65. package/dist/download.js +42 -0
  66. package/dist/durability.d.ts +5 -0
  67. package/dist/durability.d.ts.map +1 -1
  68. package/dist/durability.js +7 -0
  69. package/dist/entry.d.ts +14 -0
  70. package/dist/entry.d.ts.map +1 -1
  71. package/dist/entry.js +45 -16
  72. package/dist/expectation.d.ts +25 -14
  73. package/dist/expectation.d.ts.map +1 -1
  74. package/dist/expectation.js +51 -20
  75. package/dist/failure.d.ts +3 -0
  76. package/dist/failure.d.ts.map +1 -1
  77. package/dist/failure.js +8 -0
  78. package/dist/figures.d.ts.map +1 -1
  79. package/dist/figures.js +4 -2
  80. package/dist/file.d.ts +1 -1
  81. package/dist/file.d.ts.map +1 -1
  82. package/dist/file.js +3 -2
  83. package/dist/host.d.ts +23 -4
  84. package/dist/host.d.ts.map +1 -1
  85. package/dist/host.js +84 -19
  86. package/dist/house-query.d.ts +15 -0
  87. package/dist/house-query.d.ts.map +1 -0
  88. package/dist/house-query.js +43 -0
  89. package/dist/index.d.ts +20 -11
  90. package/dist/index.d.ts.map +1 -1
  91. package/dist/index.js +15 -7
  92. package/dist/install.d.ts +10 -6
  93. package/dist/install.d.ts.map +1 -1
  94. package/dist/install.js +92 -109
  95. package/dist/lifecycle.d.ts +2 -2
  96. package/dist/lifecycle.d.ts.map +1 -1
  97. package/dist/lifecycle.js +75 -53
  98. package/dist/listing.d.ts +29 -22
  99. package/dist/listing.d.ts.map +1 -1
  100. package/dist/listing.js +71 -79
  101. package/dist/live.d.ts +5 -3
  102. package/dist/live.d.ts.map +1 -1
  103. package/dist/live.js +74 -31
  104. package/dist/loaded.d.ts +5 -0
  105. package/dist/loaded.d.ts.map +1 -0
  106. package/dist/loaded.js +40 -0
  107. package/dist/locks.d.ts +2 -4
  108. package/dist/locks.d.ts.map +1 -1
  109. package/dist/locks.js +12 -39
  110. package/dist/lost.d.ts.map +1 -1
  111. package/dist/lost.js +11 -23
  112. package/dist/media.d.ts +18 -1
  113. package/dist/media.d.ts.map +1 -1
  114. package/dist/media.js +114 -24
  115. package/dist/memory.d.ts +9 -3
  116. package/dist/memory.d.ts.map +1 -1
  117. package/dist/memory.js +93 -32
  118. package/dist/messages.d.ts +12 -6
  119. package/dist/messages.d.ts.map +1 -1
  120. package/dist/messages.js +29 -27
  121. package/dist/modal.d.ts +2 -0
  122. package/dist/modal.d.ts.map +1 -0
  123. package/dist/modal.js +11 -0
  124. package/dist/model.d.ts +4 -3
  125. package/dist/model.d.ts.map +1 -1
  126. package/dist/model.js +6 -5
  127. package/dist/models.d.ts +4 -8
  128. package/dist/models.d.ts.map +1 -1
  129. package/dist/models.js +97 -139
  130. package/dist/observation.d.ts +29 -12
  131. package/dist/observation.d.ts.map +1 -1
  132. package/dist/observation.js +206 -69
  133. package/dist/online.d.ts +7 -0
  134. package/dist/online.d.ts.map +1 -0
  135. package/dist/online.js +29 -0
  136. package/dist/opened.d.ts +1 -1
  137. package/dist/opened.d.ts.map +1 -1
  138. package/dist/opened.js +2 -2
  139. package/dist/opening.d.ts.map +1 -1
  140. package/dist/opening.js +22 -10
  141. package/dist/outcome.d.ts +13 -1
  142. package/dist/outcome.d.ts.map +1 -1
  143. package/dist/outcome.js +32 -2
  144. package/dist/overlay.d.ts +6 -2
  145. package/dist/overlay.d.ts.map +1 -1
  146. package/dist/overlay.js +37 -24
  147. package/dist/page-metadata.d.ts +8 -0
  148. package/dist/page-metadata.d.ts.map +1 -0
  149. package/dist/page-metadata.js +19 -0
  150. package/dist/parts.d.ts +5 -59
  151. package/dist/parts.d.ts.map +1 -1
  152. package/dist/parts.js +32 -122
  153. package/dist/preparation.d.ts +33 -7
  154. package/dist/preparation.d.ts.map +1 -1
  155. package/dist/preparation.js +210 -92
  156. package/dist/presence.d.ts +58 -0
  157. package/dist/presence.d.ts.map +1 -0
  158. package/dist/presence.js +231 -0
  159. package/dist/queries.d.ts +2 -2
  160. package/dist/queries.d.ts.map +1 -1
  161. package/dist/queries.js +85 -70
  162. package/dist/questions.d.ts +1 -5
  163. package/dist/questions.d.ts.map +1 -1
  164. package/dist/questions.js +3 -8
  165. package/dist/queue.d.ts +40 -0
  166. package/dist/queue.d.ts.map +1 -0
  167. package/dist/queue.js +480 -0
  168. package/dist/reach.d.ts +15 -6
  169. package/dist/reach.d.ts.map +1 -1
  170. package/dist/reach.js +177 -33
  171. package/dist/react.d.ts +11 -5
  172. package/dist/react.d.ts.map +1 -1
  173. package/dist/react.js +28 -5
  174. package/dist/readiness.d.ts +20 -4
  175. package/dist/readiness.d.ts.map +1 -1
  176. package/dist/readiness.js +17 -10
  177. package/dist/reads.d.ts +4 -2
  178. package/dist/reads.d.ts.map +1 -1
  179. package/dist/reads.js +11 -4
  180. package/dist/recovery.d.ts +4 -1
  181. package/dist/recovery.d.ts.map +1 -1
  182. package/dist/recovery.js +31 -8
  183. package/dist/refresh.js +2 -2
  184. package/dist/refusal.d.ts.map +1 -1
  185. package/dist/refusal.js +5 -5
  186. package/dist/replica.d.ts +5 -11
  187. package/dist/replica.d.ts.map +1 -1
  188. package/dist/replica.js +14 -16
  189. package/dist/retried.d.ts +30 -0
  190. package/dist/retried.d.ts.map +1 -0
  191. package/dist/retried.js +72 -0
  192. package/dist/room-draft.d.ts +30 -5
  193. package/dist/room-draft.d.ts.map +1 -1
  194. package/dist/room-draft.js +270 -63
  195. package/dist/room-drafts.d.ts.map +1 -1
  196. package/dist/room-drafts.js +8 -5
  197. package/dist/room-list.d.ts +10 -0
  198. package/dist/room-list.d.ts.map +1 -0
  199. package/dist/room-list.js +45 -0
  200. package/dist/rows.d.ts +2 -1
  201. package/dist/rows.d.ts.map +1 -1
  202. package/dist/rows.js +9 -10
  203. package/dist/running.d.ts +5 -0
  204. package/dist/running.d.ts.map +1 -0
  205. package/dist/running.js +10 -0
  206. package/dist/runtime.d.ts +11 -19
  207. package/dist/runtime.d.ts.map +1 -1
  208. package/dist/save.d.ts +42 -90
  209. package/dist/save.d.ts.map +1 -1
  210. package/dist/save.js +326 -223
  211. package/dist/saves.d.ts +4 -40
  212. package/dist/saves.d.ts.map +1 -1
  213. package/dist/saves.js +142 -710
  214. package/dist/seed.d.ts.map +1 -1
  215. package/dist/seed.js +4 -4
  216. package/dist/shown.d.ts +9 -5
  217. package/dist/shown.d.ts.map +1 -1
  218. package/dist/shown.js +94 -60
  219. package/dist/socket/lanes.d.ts +12 -4
  220. package/dist/socket/lanes.d.ts.map +1 -1
  221. package/dist/socket/lanes.js +34 -8
  222. package/dist/socket/session.d.ts +4 -1
  223. package/dist/socket/session.d.ts.map +1 -1
  224. package/dist/socket/session.js +3 -0
  225. package/dist/socket.d.ts +4 -6
  226. package/dist/socket.d.ts.map +1 -1
  227. package/dist/socket.js +4 -14
  228. package/dist/source.d.ts +5 -7
  229. package/dist/source.d.ts.map +1 -1
  230. package/dist/source.js +13 -2
  231. package/dist/store/installs.d.ts +2 -2
  232. package/dist/store/installs.d.ts.map +1 -1
  233. package/dist/store/installs.js +10 -4
  234. package/dist/store/readiness.d.ts +5 -4
  235. package/dist/store/readiness.d.ts.map +1 -1
  236. package/dist/store/readiness.js +28 -12
  237. package/dist/store/records.d.ts +21 -6
  238. package/dist/store/records.d.ts.map +1 -1
  239. package/dist/store/records.js +59 -22
  240. package/dist/store/rooms-shown.d.ts +8 -0
  241. package/dist/store/rooms-shown.d.ts.map +1 -0
  242. package/dist/store/rooms-shown.js +13 -0
  243. package/dist/store.d.ts +25 -15
  244. package/dist/store.d.ts.map +1 -1
  245. package/dist/store.js +50 -18
  246. package/dist/stored.d.ts +32 -14
  247. package/dist/stored.d.ts.map +1 -1
  248. package/dist/stored.js +187 -51
  249. package/dist/surface.d.ts +12 -6
  250. package/dist/surface.d.ts.map +1 -1
  251. package/dist/surface.js +66 -34
  252. package/dist/sync.d.ts +4 -2
  253. package/dist/sync.d.ts.map +1 -1
  254. package/dist/sync.js +126 -101
  255. package/dist/tables.d.ts +16 -56
  256. package/dist/tables.d.ts.map +1 -1
  257. package/dist/tables.js +86 -327
  258. package/dist/tallies.d.ts.map +1 -1
  259. package/dist/tallies.js +1 -2
  260. package/dist/validation.d.ts.map +1 -1
  261. package/dist/validation.js +4 -10
  262. package/dist/wire.d.ts +66 -7
  263. package/dist/wire.d.ts.map +1 -1
  264. package/dist/wire.js +25 -9
  265. package/package.json +3 -2
  266. package/dist/attempt.d.ts +0 -16
  267. package/dist/attempt.d.ts.map +0 -1
  268. package/dist/attempt.js +0 -70
  269. package/dist/job.d.ts +0 -33
  270. package/dist/job.d.ts.map +0 -1
  271. package/dist/job.js +0 -54
  272. package/dist/log.d.ts +0 -6
  273. package/dist/log.d.ts.map +0 -1
  274. package/dist/log.js +0 -14
  275. package/dist/socket/rooms.d.ts +0 -44
  276. package/dist/socket/rooms.d.ts.map +0 -1
  277. package/dist/socket/rooms.js +0 -90
  278. package/dist/store/fences.d.ts +0 -16
  279. package/dist/store/fences.d.ts.map +0 -1
  280. package/dist/store/fences.js +0 -26
@@ -1,14 +1,17 @@
1
1
  import { cursorOf, decodedCursor } from '@agentshouse/mdmodel';
2
+ import { applied, isTurn, standingState, } from '@agentshouse/session-content';
2
3
  import { conversationChangeSet } from './change-set.js';
3
4
  import { FACTS, QUIET, } from './conversation-live.js';
4
5
  import { heldFacts } from './conversation-row.js';
5
- import { runtimeError } from './failure.js';
6
+ import { runtimeError, uncaught } from './failure.js';
6
7
  import { refusedForSpace } from './durability.js';
7
8
  import { changeBatchIdentity, chatDraftRevision } from './identity.js';
8
- import { OFFLINE } from './outcome.js';
9
- import { jobsOf, runningLine } from './parts.js';
9
+ import { OFFLINE, } from './outcome.js';
10
+ import { runningLine } from './parts.js';
11
+ import { record, text } from './read.js';
10
12
  import { openQuestion, questionAsked } from './questions.js';
11
13
  import { untilAnswered } from './recovery.js';
14
+ import { retrying } from './retried.js';
12
15
  import { capped, FROM_START, messageOrder, precedes } from './windows.js';
13
16
  export function unheldView(conversation) {
14
17
  return {
@@ -21,15 +24,19 @@ export function unheldView(conversation) {
21
24
  commands: [],
22
25
  options: [],
23
26
  starting: null,
24
- subagents: [],
27
+ state: null,
25
28
  link: null,
29
+ deliveryWaiting: [],
26
30
  deliveryUnknown: [],
27
31
  chatDraft: null,
28
- jobs: [],
32
+ dismissedSuggestion: null,
33
+ pins: [],
29
34
  };
30
35
  }
31
36
  export const SESSION_STARTED = 'Session started';
32
37
  const NEW_CHAT = 'new:';
38
+ const LISTING = 'listing';
39
+ export const CATCHING_UP = 'catching-up';
33
40
  export const HISTORY_CONVERSATIONS = 64;
34
41
  export function newChat(agent) {
35
42
  return `${NEW_CHAT}${agent}`;
@@ -42,52 +49,15 @@ function creationOf(input) {
42
49
  }
43
50
  function startedTurn(turn, operation) {
44
51
  if (operation.op === 'begin')
45
- return { turn, started: operation.at, parts: [], context: null };
46
- if (operation.op === 'context') {
47
- return {
48
- turn,
49
- started: null,
50
- parts: [],
51
- context: { used: operation.used, window: operation.window },
52
- };
53
- }
54
- return operation.op === 'start' && operation.index === 0
55
- ? { turn, started: null, parts: [structuredClone(operation.part)], context: null }
56
- : null;
57
- }
58
- function changedFields(part, operation) {
59
- if (operation.op === 'append') {
60
- return { text: `${String(part.text ?? '')}${operation.text}` };
61
- }
62
- if (operation.op === 'status') {
63
- return { status: operation.status, result: structuredClone(operation.result) };
64
- }
65
- if (operation.op === 'count')
66
- return { count: operation.count };
67
- return { entries: structuredClone(operation.entries) };
52
+ return { turn, started: operation.at, parts: [] };
53
+ const parts = applied([], operation);
54
+ return parts === null ? null : { turn, started: null, parts };
68
55
  }
69
56
  function appliedTurn(held, operation) {
70
57
  if (operation.op === 'begin')
71
58
  return { ...held, started: operation.at };
72
- if (operation.op === 'context') {
73
- return { ...held, context: { used: operation.used, window: operation.window } };
74
- }
75
- if (operation.op === 'start') {
76
- if (operation.index !== 0 && operation.index !== held.parts.length)
77
- return null;
78
- return {
79
- ...held,
80
- parts: [...held.parts.slice(0, operation.index), structuredClone(operation.part)],
81
- };
82
- }
83
- const part = held.parts[operation.index];
84
- if (typeof part !== 'object' || part === null)
85
- return null;
86
- const changed = { ...part, ...changedFields(part, operation) };
87
- return {
88
- ...held,
89
- parts: held.parts.map((kept, at) => (at === operation.index ? changed : kept)),
90
- };
59
+ const parts = applied(held.parts, operation);
60
+ return parts === null ? null : { ...held, parts };
91
61
  }
92
62
  function windowOf(held, arriving, limit) {
93
63
  const byIdentity = new Map(held.map((message) => [message.identity, message]));
@@ -110,7 +80,7 @@ function frameOf(asked, messages, more, total) {
110
80
  };
111
81
  }
112
82
  function newestOf(arriving, from) {
113
- return arriving.reduce((top, message) => (top === null || message.position > top ? message.position : top), from);
83
+ return arriving.reduce((top, message) => (top === null || message.change > top ? message.change : top), from);
114
84
  }
115
85
  function acceptedMessage(value) {
116
86
  if (typeof value !== 'object' || value === null)
@@ -119,6 +89,7 @@ function acceptedMessage(value) {
119
89
  if (typeof record.identity !== 'string' ||
120
90
  typeof record.turn !== 'string' ||
121
91
  typeof record.position !== 'string' ||
92
+ typeof record.change !== 'string' ||
122
93
  typeof record.text !== 'string') {
123
94
  return null;
124
95
  }
@@ -126,6 +97,7 @@ function acceptedMessage(value) {
126
97
  identity: record.identity,
127
98
  turn: record.turn,
128
99
  position: record.position,
100
+ change: record.change,
129
101
  text: record.text,
130
102
  message: structuredClone(record.message),
131
103
  };
@@ -227,17 +199,17 @@ function withSubmitted(draft, submitted) {
227
199
  const { submitted: _held, ...unsent } = draft;
228
200
  return submitted.length === 0 ? unsent : { ...unsent, submitted };
229
201
  }
230
- function prepended(draft, said) {
231
- 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');
232
204
  }
233
205
  function returned(draft, submission, others) {
234
206
  return withSubmitted({
235
207
  conversation: draft.conversation,
236
- ...prepended(draft, submission),
208
+ text: joinedDraft(submission.text, draft.text),
237
209
  revision: chatDraftRevision(),
238
210
  }, others);
239
211
  }
240
- function viewOf(alive, conversation, held, frame, draft, ended, marked) {
212
+ function viewOf(alive, conversation, held, frame, { draft, ended, dismissed, pins }) {
241
213
  const facts = alive.liveOf(conversation);
242
214
  return {
243
215
  conversation,
@@ -255,26 +227,41 @@ function viewOf(alive, conversation, held, frame, draft, ended, marked) {
255
227
  commands: structuredClone(facts.commands),
256
228
  options: structuredClone(facts.options),
257
229
  starting: facts.starting,
258
- subagents: structuredClone(facts.subagents),
230
+ state: sessionOf(facts, held, ended),
259
231
  link: facts.link === null ? null : { ...facts.link },
232
+ deliveryWaiting: [...facts.deliveryWaiting],
260
233
  deliveryUnknown: [...facts.deliveryUnknown],
261
234
  chatDraft: draft === null || unwritten(draft) ? null : { text: draft.text, revision: draft.revision },
262
- jobs: facts.process?.running === false
263
- ? []
264
- : jobsOf(marked.filter(({ at }) => ended === null || at > ended), facts.turn),
235
+ dismissedSuggestion: dismissed,
236
+ pins: [...pins],
265
237
  };
266
238
  }
239
+ function sessionOf(facts, held, ended) {
240
+ if (facts.state !== null)
241
+ return structuredClone(facts.state);
242
+ const saved = [...held]
243
+ .reverse()
244
+ .map(({ message }) => record(message))
245
+ .find(({ content }) => isTurn(content));
246
+ const at = saved === undefined ? null : text(saved, 'accepted_at');
247
+ if (saved === undefined || !isTurn(saved.content) || at === null)
248
+ return null;
249
+ const down = facts.process?.running === false;
250
+ return structuredClone(standingState(saved.content.state, at, down ? at : ended));
251
+ }
267
252
  function emit(alive, conversation) {
268
253
  if (alive.deps.closed())
269
254
  return;
270
255
  for (const view of alive.views()) {
271
- const { window, draft, ended, marked } = view.held.now();
256
+ const held = view.held.now();
257
+ const { window } = held;
272
258
  if (view.conversation !== conversation || window === null)
273
259
  continue;
274
260
  const ready = view.ready();
275
261
  if (ready.length === 0)
276
262
  continue;
277
- const value = viewOf(alive, conversation, window, sharedFrame(view, window), draft, ended, marked);
263
+ const frame = sharedFrame(view, window);
264
+ const value = viewOf(alive, conversation, window, frame, held);
278
265
  for (const watcher of ready)
279
266
  watcher.notify(value);
280
267
  }
@@ -285,23 +272,18 @@ function sharedFrame(view, window) {
285
272
  }
286
273
  async function slide(alive, local, conversation, arriving) {
287
274
  let count;
288
- let marked;
289
275
  for (const view of alive.views()) {
290
276
  const { held } = view;
291
277
  const { window, total } = held.now();
292
278
  if (view.conversation !== conversation || window === null)
293
279
  continue;
294
280
  if (arriving === null) {
295
- held.set({ window: [], marked: [], more: false, total: total === null ? null : 0 });
281
+ held.set({ window: [], more: false, total: total === null ? null : 0 });
296
282
  continue;
297
283
  }
298
284
  const joining = arriving.filter((message) => precedes(message, view.asked.before));
299
285
  const next = windowOf(window, joining, view.asked.limit);
300
286
  held.set({ window: next.messages, more: held.now().more || next.dropped });
301
- if (arriving.length > 0) {
302
- marked ??= await local.jobMarkers(conversation);
303
- held.set({ marked });
304
- }
305
287
  if (held.now().total !== null && arriving.length > 0) {
306
288
  count ??= await local.messageCount(conversation);
307
289
  held.set({ total: count });
@@ -325,66 +307,92 @@ function rowOf(alive, conversation) {
325
307
  };
326
308
  }
327
309
  async function entries(alive) {
328
- const list = await heldList(alive);
329
- 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)))
330
316
  return null;
331
317
  const held = list.length === 0
332
318
  ? []
333
319
  : await alive.deps.durably((local) => local.conversationRows(list.map(({ conversation }) => conversation)));
334
320
  const rows = new Map(held.map((one) => [one.conversation, one]));
335
- return list.map((entry) => ({
336
- conversation: entry.conversation,
337
- agent: { identity: entry.agent, name: entry.agentName, retired: entry.agentRetired },
338
- name: entry.name,
339
- created: entry.created,
340
- finished: entry.finished,
341
- outcome: entry.outcome,
342
- waiting: entry.waiting,
343
- ...rowOf(alive, entry.conversation),
344
- ...heldFacts(rows.get(entry.conversation), entry),
345
- }));
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
+ });
346
336
  }
347
- async function heldList(alive) {
348
- const stored = (await (await alive.deps.local()).facts()).conversationList;
349
- if (stored !== null)
350
- return stored;
351
- 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: [] };
352
344
  }
353
345
  async function endedIn(alive, conversation) {
354
- const list = await heldList(alive);
355
- 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;
356
355
  }
357
356
  function listChanged(alive, change) {
358
357
  void alive.deps
359
358
  .durably(async (local) => {
360
- const { conversationList, conversationCursor } = await local.facts();
361
- const kept = change.replacing ? [] : (conversationList ?? []);
362
- const held = new Map(kept.map((entry) => [entry.conversation, entry]));
363
- for (const entry of change.set) {
364
- 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)
365
364
  alive.created(entry.conversation);
366
- }
367
365
  if (change.gone !== null)
368
366
  held.delete(change.gone);
369
367
  const list = [...held.values()];
368
+ const agents = [
369
+ ...upserted(change.replacing ? [] : (agentList ?? []), change.agents, (agent) => agent.id).values(),
370
+ ];
370
371
  if (JSON.stringify(list) !== JSON.stringify(conversationList)) {
371
372
  await local.fact('conversationList', list);
372
373
  }
374
+ if (JSON.stringify(agents) !== JSON.stringify(agentList)) {
375
+ await local.fact('agentList', agents);
376
+ }
373
377
  if (change.cursor !== undefined) {
374
378
  if (change.cursor !== conversationCursor) {
375
379
  await local.fact('conversationCursor', change.cursor);
376
380
  }
377
381
  alive.deps.stream()?.hold(change.cursor);
378
382
  }
379
- return list;
383
+ return { conversations: list, agents };
380
384
  })
381
- .catch(async () => (await (await alive.deps.local()).facts()).conversationList ?? [])
382
- .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) => {
383
390
  if (alive.deps.closed() || change.paged === true)
384
391
  return;
392
+ const list = listing.conversations;
385
393
  endedAnew(alive, list);
386
394
  if (change.cursor !== undefined) {
387
- alive.held.set({ listKnown: true });
395
+ alive.held.set({ listKnown: true, listHeld: true, openingListed: true });
388
396
  settleBehind(alive, list);
389
397
  openingEnded(alive);
390
398
  alive.deps.settled();
@@ -392,14 +400,15 @@ function listChanged(alive, change) {
392
400
  emitList(alive);
393
401
  if (!alive.known())
394
402
  return;
395
- retiredDrafts(alive, list);
396
- const { replicated } = alive.held.now();
397
- if (change.cursor === undefined &&
398
- (list.length !== replicated.size ||
399
- 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) {
400
409
  alive.deps.again();
401
410
  }
402
- }, () => { });
411
+ }, uncaught);
403
412
  }
404
413
  function endedAnew(alive, list) {
405
414
  const entries = new Map(list.map((entry) => [entry.conversation, entry]));
@@ -412,20 +421,23 @@ function endedAnew(alive, list) {
412
421
  emit(alive, view.conversation);
413
422
  }
414
423
  }
415
- function shownList(alive, work) {
416
- const reading = heldList(alive);
424
+ function shownListing(alive, work) {
425
+ const reading = heldListing(alive);
417
426
  alive.inOrder(async () => {
418
- const list = await reading.catch(() => null);
419
- if (list === null || alive.deps.closed())
427
+ const listing = await reading.catch((error) => {
428
+ uncaught(error);
429
+ return null;
430
+ });
431
+ if (listing === null || alive.deps.closed())
420
432
  return;
421
- work(list);
433
+ work(listing);
422
434
  });
423
435
  }
424
436
  async function told(alive) {
425
437
  try {
426
438
  while (alive.held.now().listOwed && !alive.deps.closed()) {
427
439
  alive.held.set({ listOwed: false });
428
- 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);
429
441
  if (alive.deps.closed())
430
442
  return;
431
443
  if (value === null)
@@ -463,57 +475,48 @@ export function conversationsLearned(alive) {
463
475
  return;
464
476
  if (alive.listeners.waiting())
465
477
  tellList(alive);
466
- if (!alive.agentListeners.waiting())
478
+ if (!alive.agentListeners.waiting() && !alive.listedListeners.waiting())
467
479
  return;
468
- shownList(alive, (list) => alive.agentListeners.welcome(agents(alive, list)));
469
- }
470
- function agents(alive, list) {
471
- const named = new Map();
472
- for (const agent of alive.held.now().listedAgents ?? [])
473
- named.set(agent.agent, agent);
474
- for (const entry of list) {
475
- named.set(entry.agent, { name: entry.agentName, retired: entry.agentRetired });
476
- }
477
- return [...named]
478
- .filter(([, agent]) => !agent.retired)
479
- .map(([identity, agent]) => ({ identity, name: agent.name }))
480
+ shownListing(alive, (listing) => {
481
+ alive.agentListeners.welcome(agentsOf(listing));
482
+ alive.listedListeners.welcome(structuredClone(listing.agents));
483
+ });
484
+ }
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
+ }))
480
500
  .sort((left, right) => left.name === right.name
481
501
  ? left.identity.localeCompare(right.identity)
482
502
  : left.name.localeCompare(right.name));
483
503
  }
484
- function readAgents(alive) {
485
- const stream = alive.deps.stream();
486
- if (stream?.agents === undefined || !alive.deps.online() || alive.held.now().agentsReading)
487
- return;
488
- const { agents: read } = stream;
489
- const { agentListeners } = alive;
490
- alive.held.set({ agentsReading: true });
491
- void alive.deps.recovery
492
- .attempt('agents', async () => {
493
- if (!alive.deps.online() || (!agentListeners.waiting() && !agentListeners.told())) {
494
- return 'done';
495
- }
496
- const listed = await read();
497
- if (listed === null)
498
- return 'done';
499
- alive.held.set({ listedAgents: listed });
500
- emitAgents(alive);
501
- return 'done';
502
- })
503
- .finally(() => {
504
- alive.held.set({ agentsReading: false });
505
- });
506
- }
507
504
  function emitAgents(alive) {
508
- if (alive.deps.closed() || !alive.agentListeners.told())
505
+ const { agentListeners, listedListeners } = alive;
506
+ if (alive.deps.closed() || (!agentListeners.told() && !listedListeners.told()))
509
507
  return;
510
- shownList(alive, (list) => {
511
- const value = agents(alive, list);
508
+ shownListing(alive, (listing) => {
509
+ const value = agentsOf(listing);
512
510
  const shown = JSON.stringify(value);
513
- if (shown === alive.held.now().agentsShown)
514
- return;
515
- alive.held.set({ agentsShown: shown });
516
- 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
+ }
517
520
  });
518
521
  }
519
522
  function setLive(alive, conversation, next) {
@@ -527,11 +530,11 @@ function setLive(alive, conversation, next) {
527
530
  after.asked !== before.asked)
528
531
  tellSoon(alive);
529
532
  }
530
- function retiredChat(conversation, list) {
533
+ function retiredChat(conversation, listing) {
531
534
  if (!conversation.startsWith(NEW_CHAT))
532
535
  return false;
533
536
  const agent = conversation.slice(NEW_CHAT.length);
534
- return list.some((entry) => entry.agent === agent && entry.agentRetired);
537
+ return listing.agents.some((one) => one.id === agent && one.retired_at !== null);
535
538
  }
536
539
  function live(alive, conversation, change) {
537
540
  alive.factsHeard(conversation, FACTS.filter((one) => one in change));
@@ -550,8 +553,9 @@ function openingEnded(alive) {
550
553
  commands: facts.has('commands') ? QUIET.commands : held.commands,
551
554
  options: facts.has('options') ? QUIET.options : held.options,
552
555
  starting: facts.has('starting') ? QUIET.starting : held.starting,
553
- subagents: facts.has('subagents') ? QUIET.subagents : held.subagents,
556
+ state: facts.has('state') ? QUIET.state : held.state,
554
557
  link: facts.has('link') ? QUIET.link : held.link,
558
+ deliveryWaiting: facts.has('deliveryWaiting') ? QUIET.deliveryWaiting : held.deliveryWaiting,
555
559
  deliveryUnknown: facts.has('deliveryUnknown') ? QUIET.deliveryUnknown : held.deliveryUnknown,
556
560
  });
557
561
  emit(alive, conversation);
@@ -583,19 +587,13 @@ function handReplicated(alive) {
583
587
  }
584
588
  async function remove(alive, leaving) {
585
589
  await alive.deps.durably(async (local) => {
586
- const kept = new Set([
587
- ...(await local.conversations(leaving)).map(({ conversation }) => conversation),
588
- ...(await local.chatDrafts())
589
- .filter(({ conversation }) => leaving.includes(conversation))
590
- .map(({ conversation }) => conversation),
591
- ]);
592
- await local.dropConversations(leaving);
590
+ const kept = new Set(await local.dropConversations(leaving));
593
591
  const changed = [];
594
592
  for (const conversation of leaving) {
595
593
  if (kept.has(conversation) || alive.liveHeld(conversation) !== undefined) {
596
594
  changed.push(conversation);
597
595
  }
598
- shownDraft(alive, conversation, null);
596
+ heldInViews(alive, conversation, { draft: null, dismissed: null });
599
597
  alive.forget(conversation);
600
598
  setLive(alive, conversation, null);
601
599
  await slide(alive, local, conversation, null);
@@ -624,7 +622,7 @@ async function readsOf(local, conversations) {
624
622
  return new Map((await local.conversations(conversations)).map((held) => [held.conversation, held]));
625
623
  }
626
624
  function retiredDrafts(alive, list) {
627
- if (!list.some((entry) => entry.agentRetired))
625
+ if (!list.agents.some((agent) => agent.retired_at !== null))
628
626
  return;
629
627
  void alive.deps
630
628
  .local()
@@ -634,7 +632,7 @@ function retiredDrafts(alive, list) {
634
632
  held.some(({ conversation }) => retiredChat(conversation, list))) {
635
633
  alive.deps.again();
636
634
  }
637
- }, () => { });
635
+ }, uncaught);
638
636
  }
639
637
  function settleBehind(alive, list) {
640
638
  void alive.deps
@@ -660,21 +658,19 @@ function settleBehind(alive, list) {
660
658
  alive.deps.settled();
661
659
  if (owed)
662
660
  alive.deps.again();
663
- }, () => { });
661
+ }, uncaught);
664
662
  }
665
663
  function keep(alive, conversation, message) {
666
664
  return alive.deps.durably(async (local) => {
667
665
  if (!alive.held.now().replicated.has(conversation))
668
666
  return;
669
667
  const read = (await readsOf(local, [conversation])).get(conversation)?.read;
670
- const advances = read !== undefined &&
671
- !alive.behind(conversation) &&
672
- (read === null || message.position > read);
668
+ const advances = read !== undefined && !alive.behind(conversation) && (read === null || message.change > read);
673
669
  await sealed(alive, local, conversation, new Set([message.turn]));
674
670
  await local.keepMessages({
675
671
  conversation,
676
672
  messages: [message],
677
- ...(advances ? { read: { at: message.position, partial: false } } : {}),
673
+ ...(advances ? { read: { at: message.change, partial: false } } : {}),
678
674
  });
679
675
  sealTurns(alive, conversation, new Set([message.turn]));
680
676
  await slide(alive, local, conversation, [message]);
@@ -786,8 +782,8 @@ function heard(alive, event) {
786
782
  live(alive, conversation, { starting: event.step });
787
783
  return;
788
784
  }
789
- if (event.kind === 'subagents') {
790
- live(alive, conversation, { subagents: structuredClone(event.subagents) });
785
+ if (event.kind === 'state') {
786
+ live(alive, conversation, { state: structuredClone(event.state) });
791
787
  return;
792
788
  }
793
789
  if (event.kind === 'link') {
@@ -795,13 +791,16 @@ function heard(alive, event) {
795
791
  return;
796
792
  }
797
793
  if (event.kind === 'deliveries') {
798
- live(alive, conversation, { deliveryUnknown: [...event.unknown] });
794
+ live(alive, conversation, {
795
+ deliveryWaiting: [...event.waiting],
796
+ deliveryUnknown: [...event.unknown],
797
+ });
799
798
  return;
800
799
  }
801
800
  const arriving = acceptedMessage(event.message);
802
801
  if (arriving === null)
803
802
  return;
804
- alive.heardAt(conversation, arriving.position);
803
+ alive.heardAt(conversation, arriving.change);
805
804
  if (alive.held.now().replicated.has(conversation)) {
806
805
  if (alive.deps.full()) {
807
806
  alive.gapOpened(conversation);
@@ -810,6 +809,8 @@ function heard(alive, event) {
810
809
  void keep(alive, conversation, arriving).catch((error) => {
811
810
  if (refusedForSpace(error))
812
811
  alive.gapOpened(conversation);
812
+ else
813
+ uncaught(error);
813
814
  });
814
815
  return;
815
816
  }
@@ -817,19 +818,27 @@ function heard(alive, event) {
817
818
  emit(alive, conversation);
818
819
  }
819
820
  async function kept(alive, local, conversation, following) {
820
- const shown = following === null || empty(following) ? null : following;
821
- if (shown === null)
821
+ const draft = following === null || empty(following) ? null : following;
822
+ if (draft === null)
822
823
  await local.dropChatDraft(conversation);
823
824
  else
824
- await local.keepChatDrafts([shown]);
825
- shownDraft(alive, conversation, shown);
825
+ await local.keepChatDrafts([draft]);
826
+ heldInViews(alive, conversation, { draft });
826
827
  }
827
- function shownDraft(alive, conversation, draft) {
828
+ function heldInViews(alive, conversation, held) {
828
829
  for (const view of alive.views()) {
829
830
  if (view.conversation === conversation)
830
- view.held.set({ draft });
831
+ view.held.set(held);
831
832
  }
832
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
+ }
833
842
  async function draftAt(local, conversation) {
834
843
  return (await local.chatDraft(conversation)) ?? undefined;
835
844
  }
@@ -860,9 +869,9 @@ function moved(alive, agent, operation, to) {
860
869
  : [{ ...unsent, conversation: to }];
861
870
  const left = { ...(taken.length === 0 ? unsent : blank), moves: [...earlier, operation] };
862
871
  await local.keepChatDrafts([left, ...taken]);
863
- shownDraft(alive, from, left);
872
+ heldInViews(alive, from, { draft: left });
864
873
  for (const one of taken)
865
- shownDraft(alive, to, one);
874
+ heldInViews(alive, to, { draft: one });
866
875
  alive.deps.publish(conversationChangeSet([from, to]));
867
876
  });
868
877
  }
@@ -893,13 +902,10 @@ async function submitted(alive, conversation, operation, draft) {
893
902
  });
894
903
  return submission;
895
904
  }
896
- async function dispatched(alive, conversation, submission) {
905
+ async function dispatched(alive, conversation, submission, resent = false) {
897
906
  const dispatch = alive.sendingBegan(submission.operation);
898
907
  try {
899
- const result = await invoke(alive, 'conversations/send', conversation, {
900
- operation_id: submission.operation,
901
- text: submission.text,
902
- });
908
+ const result = await invoke(alive, 'conversations/send', conversation, submission.operation, { text: submission.text }, { step: 'sending', instance: `message ${submission.operation}` });
903
909
  dispatch.set({ answered: result.kind !== 'transport-uncertainty' });
904
910
  if (result.kind === 'revoked-authorization')
905
911
  return result;
@@ -907,6 +913,7 @@ async function dispatched(alive, conversation, submission) {
907
913
  alive.deps.unanswered();
908
914
  return result;
909
915
  }
916
+ alive.deps.heard(submission.operation, resent);
910
917
  const turn = answered(result, 'message_id');
911
918
  await withDraft(alive, conversation, async (current, local) => {
912
919
  const held = current?.submitted?.find((one) => one.operation === submission.operation);
@@ -926,11 +933,12 @@ async function dispatched(alive, conversation, submission) {
926
933
  alive.sendingEnded(submission.operation, dispatch);
927
934
  }
928
935
  }
929
- function invoke(alive, name, conversation, extra = { operation_id: changeBatchIdentity() }) {
930
- return alive.deps.call(name, { conversation_id: conversation, ...extra });
936
+ function invoke(alive, name, conversation, operation, extra = {}, attempt) {
937
+ return alive.deps.call(name, { conversation_id: conversation, operation_id: operation, ...extra }, attempt);
931
938
  }
932
- export function conversationSurface(alive) {
939
+ export function conversationSurface(alive, files) {
933
940
  return {
941
+ ...files,
934
942
  list() {
935
943
  return new Promise((settle) => {
936
944
  const answer = (listed) => {
@@ -948,8 +956,12 @@ export function conversationSurface(alive) {
948
956
  return alive.deps.durably(async (local) => {
949
957
  const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before, phrase);
950
958
  const total = asked.total ? await local.messageCount(conversation, phrase) : null;
951
- const marked = await local.jobMarkers(conversation);
952
- return structuredClone(viewOf(alive, conversation, messages, frameOf(asked, messages, more, total), (await local.chatDraft(conversation)) ?? null, await endedIn(alive, conversation), marked));
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
+ }));
953
965
  });
954
966
  },
955
967
  async create(input) {
@@ -957,7 +969,7 @@ export function conversationSurface(alive) {
957
969
  const conversation = answered(result, 'conversation_id');
958
970
  if (conversation === null)
959
971
  return result;
960
- const listed = (await heldList(alive)) ?? [];
972
+ const listed = (await heldListing(alive))?.conversations ?? [];
961
973
  if (!listed.some((entry) => entry.conversation === conversation)) {
962
974
  alive.creating(conversation);
963
975
  }
@@ -986,40 +998,90 @@ export function conversationSurface(alive) {
986
998
  }
987
999
  return OFFLINE;
988
1000
  },
989
- answer(conversation, answer) {
990
- return invoke(alive, 'conversations/interaction/answer', conversation, {
991
- operation_id: changeBatchIdentity(),
992
- ...answer,
1001
+ answer(conversation, answer, operation) {
1002
+ return invoke(alive, 'conversations/interaction/answer', conversation, operation, answer);
1003
+ },
1004
+ interrupt(conversation, operation) {
1005
+ return invoke(alive, 'conversations/interrupt', conversation, operation);
1006
+ },
1007
+ kill(conversation, operation) {
1008
+ return invoke(alive, 'conversations/kill', conversation, operation);
1009
+ },
1010
+ delete(conversation, operation) {
1011
+ return invoke(alive, 'conversations/delete', conversation, operation);
1012
+ },
1013
+ takeOut(conversation, message, operation) {
1014
+ return invoke(alive, 'conversations/take-out', conversation, operation, {
1015
+ message_id: message,
1016
+ });
1017
+ },
1018
+ decide({ request, room, message }, decision, operation) {
1019
+ return alive.deps.call('agent-functions/decide', {
1020
+ room_ref: room,
1021
+ request,
1022
+ ...(message === undefined ? {} : { message }),
1023
+ decision,
1024
+ operation_id: operation,
993
1025
  });
994
1026
  },
995
- interrupt(conversation) {
996
- return invoke(alive, 'conversations/interrupt', conversation);
1027
+ rename(conversation, name, operation) {
1028
+ return invoke(alive, 'conversations/rename', conversation, operation, { name });
997
1029
  },
998
- kill(conversation) {
999
- return invoke(alive, 'conversations/kill', conversation);
1030
+ option(conversation, option, value, operation) {
1031
+ return invoke(alive, 'conversations/option/set', conversation, operation, { option, value });
1000
1032
  },
1001
- delete(conversation) {
1002
- return invoke(alive, 'conversations/delete', conversation);
1033
+ retry(conversation, message, operation) {
1034
+ return invoke(alive, 'conversations/retry', conversation, operation, {
1035
+ message_id: message,
1036
+ });
1037
+ },
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
+ }
1047
+ },
1048
+ async openTurn(conversation, turn) {
1049
+ await alive.deps.durably((local) => local.keepConversationFact({ conversation, name: 'openedTurn', value: turn }));
1050
+ tellList(alive);
1051
+ },
1052
+ dismissSuggestion(conversation, suggestion) {
1053
+ return alive.deps.durably(async (local) => {
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]));
1063
+ });
1003
1064
  },
1004
1065
  };
1005
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
+ }
1006
1075
  export function chatDraftSurface(alive) {
1007
1076
  return {
1008
- write(conversation, draft, revision = chatDraftRevision()) {
1009
- return withDraft(alive, conversation, (current) => ({
1010
- conversation,
1011
- text: draft.text,
1012
- revision,
1013
- ...(current === undefined ? {} : pending(current)),
1014
- }));
1077
+ write(conversation, draft, revision) {
1078
+ return rewritten(alive, conversation, () => draft.text, revision);
1015
1079
  },
1016
1080
  prepend(conversation, draft) {
1017
- return withDraft(alive, conversation, (current) => ({
1018
- conversation,
1019
- ...(current === undefined ? { text: draft.text } : prepended(current, draft)),
1020
- revision: chatDraftRevision(),
1021
- ...(current === undefined ? {} : pending(current)),
1022
- }));
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));
1023
1085
  },
1024
1086
  discard(conversation) {
1025
1087
  return withDraft(alive, conversation, (current) => current === undefined
@@ -1029,31 +1091,37 @@ export function chatDraftSurface(alive) {
1029
1091
  };
1030
1092
  }
1031
1093
  export async function conversationsOpened(alive, local) {
1032
- const { conversationList, conversationCursor } = await local.facts();
1033
- 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())
1034
1099
  return;
1035
1100
  alive.deps.stream()?.hold(conversationCursor);
1036
1101
  conversationsLearned(alive);
1037
1102
  }
1103
+ export function reconcileOpening(alive) {
1104
+ alive.held.set({ openingRead: true });
1105
+ return reconcileConversations(alive);
1106
+ }
1038
1107
  export async function reconcileConversations(alive) {
1039
1108
  if (!alive.known())
1040
1109
  return;
1041
1110
  const local = await alive.deps.local();
1042
- const [held, kept, { conversationList }] = await Promise.all([
1111
+ const [held, stored, { conversationList, agentList }] = await Promise.all([
1043
1112
  local.conversations(),
1044
- local.chatDrafts(),
1113
+ local.storedConversations(),
1045
1114
  local.facts(),
1046
1115
  ]);
1047
1116
  const list = conversationList ?? [];
1117
+ const listing = { conversations: list, agents: agentList ?? [] };
1048
1118
  const entries = new Map(list.map((entry) => [entry.conversation, entry]));
1049
1119
  const named = new Set(entries.keys());
1050
1120
  const reads = new Map(held.map((one) => [one.conversation, one]));
1051
1121
  const { replicated } = alive.held.now();
1052
- const leaving = [
1053
- ...new Set([...replicated, ...reads.keys(), ...kept.map(({ conversation }) => conversation)]),
1054
- ].filter((conversation) => !named.has(conversation) &&
1122
+ const leaving = [...new Set([...replicated, ...stored])].filter((conversation) => !named.has(conversation) &&
1055
1123
  !alive.isCreating(conversation) &&
1056
- (!conversation.startsWith(NEW_CHAT) || retiredChat(conversation, list)));
1124
+ (!conversation.startsWith(NEW_CHAT) || retiredChat(conversation, listing)));
1057
1125
  for (const conversation of named) {
1058
1126
  if (!replicated.has(conversation) &&
1059
1127
  missed(alive, conversation, reads.get(conversation), entries.get(conversation))) {
@@ -1088,6 +1156,17 @@ export async function catchUpConversations(alive, progress) {
1088
1156
  if (!alive.current())
1089
1157
  progress.complete = false;
1090
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
+ }
1091
1170
  export async function resendConversations(alive, progress) {
1092
1171
  const local = await alive.deps.local();
1093
1172
  const tried = new Set();
@@ -1099,7 +1178,7 @@ export async function resendConversations(alive, progress) {
1099
1178
  if (alive.deps.closed())
1100
1179
  return;
1101
1180
  tried.add(submission.operation);
1102
- const result = await dispatched(alive, conversation, submission);
1181
+ const result = await dispatched(alive, conversation, submission, true);
1103
1182
  if (result.kind === 'revoked-authorization')
1104
1183
  return;
1105
1184
  if (result.kind === 'transport-uncertainty') {
@@ -1119,23 +1198,25 @@ export function watchConversation(alive, conversation, options, deliver) {
1119
1198
  const { view, watcher } = alive.viewJoined(key, conversation, asked, deliver);
1120
1199
  const { held } = view;
1121
1200
  const reading = new AbortController();
1122
- void untilAnswered(() => alive.deps.durably(async (local) => {
1201
+ void untilAnswered(retrying(alive.deps.retries, 'reading', `window ${key}`, () => alive.deps.durably(async (local) => {
1123
1202
  if (held.now().window !== null)
1124
1203
  return;
1125
1204
  const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before);
1126
1205
  held.set({ draft: (await local.chatDraft(conversation)) ?? null });
1127
1206
  held.set({ ended: await endedIn(alive, conversation) });
1207
+ held.set({ dismissed: await dismissedIn(local, conversation) });
1208
+ held.set({ pins: (await local.facts()).chatPins });
1128
1209
  held.set({ window: messages });
1129
- held.set({ marked: await local.jobMarkers(conversation) });
1130
1210
  held.set({ more });
1131
1211
  held.set({ total: asked.total ? await local.messageCount(conversation) : null });
1132
- }), reading.signal)
1212
+ })), reading.signal)
1133
1213
  .then(() => {
1134
1214
  if (alive.deps.closed() || !view.admit(watcher))
1135
1215
  return;
1136
- const { window, draft, ended, marked } = held.now();
1137
- const shown = window ?? [];
1138
- watcher.notify(viewOf(alive, conversation, shown, sharedFrame(view, shown), draft, ended, marked));
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));
1139
1220
  })
1140
1221
  .catch(() => { });
1141
1222
  return () => {
@@ -1151,9 +1232,13 @@ export function watchConversationList(alive, deliver) {
1151
1232
  export function watchConversationAgents(alive, deliver) {
1152
1233
  alive.agentListeners.join(deliver);
1153
1234
  conversationsLearned(alive);
1154
- readAgents(alive);
1155
1235
  return () => alive.agentListeners.leave(deliver);
1156
1236
  }
1237
+ export function watchListedAgents(alive, deliver) {
1238
+ alive.listedListeners.join(deliver);
1239
+ conversationsLearned(alive);
1240
+ return () => alive.listedListeners.leave(deliver);
1241
+ }
1157
1242
  export function conversationsChanged(alive, set) {
1158
1243
  for (const conversation of set.conversations)
1159
1244
  emit(alive, conversation);
@@ -1165,26 +1250,32 @@ export function receiveConversationEvent(alive, event) {
1165
1250
  if (event.whole)
1166
1251
  alive.relistBegun();
1167
1252
  if (alive.relisting())
1168
- alive.relistHeard(event.entries);
1253
+ alive.relistHeard(event.entries, event.agents);
1169
1254
  if (event.more && alive.relisting())
1170
1255
  return;
1171
1256
  const whole = alive.relistTaken();
1172
1257
  listChanged(alive, {
1173
1258
  replacing: whole !== null,
1174
- 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()],
1175
1261
  gone: null,
1176
1262
  ...(event.more ? { paged: true } : { cursor: event.cursor }),
1177
1263
  });
1178
1264
  return;
1179
1265
  }
1180
1266
  if (event.kind === 'listed') {
1181
- alive.relistHeard([event.entry]);
1182
- 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 });
1183
1274
  return;
1184
1275
  }
1185
1276
  if (event.kind === 'unlisted') {
1186
1277
  alive.relistDropped(event.conversation);
1187
- listChanged(alive, { replacing: false, set: [], gone: event.conversation });
1278
+ listChanged(alive, { replacing: false, set: [], agents: [], gone: event.conversation });
1188
1279
  return;
1189
1280
  }
1190
1281
  heard(alive, event);