@agentshouse/mdruntime 0.1.0-cf04ede9fb796 → 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 (322) 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 +105 -166
  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 +175 -85
  11. package/dist/adapter.d.ts +226 -74
  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 +2 -7
  16. package/dist/{binary.d.ts → attachment.d.ts} +8 -4
  17. package/dist/attachment.d.ts.map +1 -0
  18. package/dist/attachment.js +15 -0
  19. package/dist/authored.d.ts +88 -0
  20. package/dist/authored.d.ts.map +1 -0
  21. package/dist/authored.js +345 -0
  22. package/dist/bootstrap.d.ts +1 -1
  23. package/dist/bootstrap.d.ts.map +1 -1
  24. package/dist/bootstrap.js +6 -19
  25. package/dist/chain.d.ts +1 -1
  26. package/dist/chain.d.ts.map +1 -1
  27. package/dist/chain.js +5 -2
  28. package/dist/change-set.d.ts +4 -0
  29. package/dist/change-set.d.ts.map +1 -1
  30. package/dist/change-set.js +6 -0
  31. package/dist/change.d.ts +6 -2
  32. package/dist/change.d.ts.map +1 -1
  33. package/dist/change.js +7 -2
  34. package/dist/chat-types.d.ts +0 -2
  35. package/dist/chat-types.d.ts.map +1 -1
  36. package/dist/chat-types.js +2 -4
  37. package/dist/client.d.ts +30 -23
  38. package/dist/client.d.ts.map +1 -1
  39. package/dist/client.js +55 -68
  40. package/dist/collection.d.ts +27 -4
  41. package/dist/collection.d.ts.map +1 -1
  42. package/dist/collection.js +34 -10
  43. package/dist/column.d.ts +5 -0
  44. package/dist/column.d.ts.map +1 -0
  45. package/dist/column.js +9 -0
  46. package/dist/compile.d.ts +4 -1
  47. package/dist/compile.d.ts.map +1 -1
  48. package/dist/compile.js +101 -63
  49. package/dist/connection.d.ts +1 -1
  50. package/dist/connection.d.ts.map +1 -1
  51. package/dist/connection.js +42 -27
  52. package/dist/context.d.ts.map +1 -1
  53. package/dist/context.js +1 -1
  54. package/dist/conversation-live.d.ts +29 -15
  55. package/dist/conversation-live.d.ts.map +1 -1
  56. package/dist/conversation-live.js +26 -14
  57. package/dist/conversation-row.d.ts +23 -1
  58. package/dist/conversation-row.d.ts.map +1 -1
  59. package/dist/conversation-row.js +37 -8
  60. package/dist/conversation.d.ts +48 -16
  61. package/dist/conversation.d.ts.map +1 -1
  62. package/dist/conversation.js +368 -262
  63. package/dist/declaration.d.ts +10 -0
  64. package/dist/declaration.d.ts.map +1 -0
  65. package/dist/declaration.js +24 -0
  66. package/dist/discovery.d.ts +3 -1
  67. package/dist/discovery.d.ts.map +1 -1
  68. package/dist/discovery.js +47 -47
  69. package/dist/download.d.ts +6 -0
  70. package/dist/download.d.ts.map +1 -0
  71. package/dist/download.js +42 -0
  72. package/dist/durability.d.ts +6 -1
  73. package/dist/durability.d.ts.map +1 -1
  74. package/dist/durability.js +7 -0
  75. package/dist/entry.d.ts +26 -1
  76. package/dist/entry.d.ts.map +1 -1
  77. package/dist/entry.js +126 -36
  78. package/dist/expectation.d.ts +26 -13
  79. package/dist/expectation.d.ts.map +1 -1
  80. package/dist/expectation.js +72 -19
  81. package/dist/failure.d.ts +3 -0
  82. package/dist/failure.d.ts.map +1 -1
  83. package/dist/failure.js +8 -0
  84. package/dist/figures.d.ts +1 -1
  85. package/dist/figures.d.ts.map +1 -1
  86. package/dist/figures.js +6 -4
  87. package/dist/file.d.ts +1 -1
  88. package/dist/file.d.ts.map +1 -1
  89. package/dist/file.js +3 -2
  90. package/dist/holdings.d.ts.map +1 -1
  91. package/dist/holdings.js +5 -3
  92. package/dist/host.d.ts +24 -4
  93. package/dist/host.d.ts.map +1 -1
  94. package/dist/host.js +108 -32
  95. package/dist/house-query.d.ts +24 -0
  96. package/dist/house-query.d.ts.map +1 -0
  97. package/dist/house-query.js +102 -0
  98. package/dist/identity.d.ts +1 -1
  99. package/dist/identity.d.ts.map +1 -1
  100. package/dist/identity.js +1 -1
  101. package/dist/index.d.ts +29 -16
  102. package/dist/index.d.ts.map +1 -1
  103. package/dist/index.js +19 -9
  104. package/dist/install.d.ts +10 -6
  105. package/dist/install.d.ts.map +1 -1
  106. package/dist/install.js +92 -111
  107. package/dist/lifecycle.d.ts +2 -2
  108. package/dist/lifecycle.d.ts.map +1 -1
  109. package/dist/lifecycle.js +80 -54
  110. package/dist/linked-file.d.ts +7 -0
  111. package/dist/linked-file.d.ts.map +1 -0
  112. package/dist/linked-file.js +13 -0
  113. package/dist/listing.d.ts +29 -22
  114. package/dist/listing.d.ts.map +1 -1
  115. package/dist/listing.js +71 -79
  116. package/dist/live.d.ts +19 -3
  117. package/dist/live.d.ts.map +1 -1
  118. package/dist/live.js +97 -39
  119. package/dist/loaded.d.ts +7 -0
  120. package/dist/loaded.d.ts.map +1 -0
  121. package/dist/loaded.js +46 -0
  122. package/dist/locks.d.ts +2 -4
  123. package/dist/locks.d.ts.map +1 -1
  124. package/dist/locks.js +12 -39
  125. package/dist/lost.d.ts.map +1 -1
  126. package/dist/lost.js +11 -23
  127. package/dist/media.d.ts +20 -4
  128. package/dist/media.d.ts.map +1 -1
  129. package/dist/media.js +184 -37
  130. package/dist/memory.d.ts +10 -4
  131. package/dist/memory.d.ts.map +1 -1
  132. package/dist/memory.js +105 -41
  133. package/dist/messages.d.ts +21 -6
  134. package/dist/messages.d.ts.map +1 -1
  135. package/dist/messages.js +53 -41
  136. package/dist/modal.d.ts +2 -0
  137. package/dist/modal.d.ts.map +1 -0
  138. package/dist/modal.js +11 -0
  139. package/dist/model.d.ts +10 -8
  140. package/dist/model.d.ts.map +1 -1
  141. package/dist/model.js +6 -5
  142. package/dist/models.d.ts +4 -8
  143. package/dist/models.d.ts.map +1 -1
  144. package/dist/models.js +97 -139
  145. package/dist/name.d.ts +0 -1
  146. package/dist/name.d.ts.map +1 -1
  147. package/dist/name.js +3 -7
  148. package/dist/observation.d.ts +29 -12
  149. package/dist/observation.d.ts.map +1 -1
  150. package/dist/observation.js +217 -85
  151. package/dist/online.d.ts +7 -0
  152. package/dist/online.d.ts.map +1 -0
  153. package/dist/online.js +29 -0
  154. package/dist/opened.d.ts +6 -6
  155. package/dist/opened.d.ts.map +1 -1
  156. package/dist/opened.js +7 -7
  157. package/dist/opening.d.ts.map +1 -1
  158. package/dist/opening.js +24 -10
  159. package/dist/outcome.d.ts +33 -13
  160. package/dist/outcome.d.ts.map +1 -1
  161. package/dist/outcome.js +36 -6
  162. package/dist/overlay.d.ts +23 -8
  163. package/dist/overlay.d.ts.map +1 -1
  164. package/dist/overlay.js +132 -48
  165. package/dist/page-metadata.d.ts +8 -0
  166. package/dist/page-metadata.d.ts.map +1 -0
  167. package/dist/page-metadata.js +19 -0
  168. package/dist/parts.d.ts +6 -59
  169. package/dist/parts.d.ts.map +1 -1
  170. package/dist/parts.js +32 -122
  171. package/dist/paths.d.ts +1 -1
  172. package/dist/paths.js +1 -1
  173. package/dist/preparation.d.ts +33 -7
  174. package/dist/preparation.d.ts.map +1 -1
  175. package/dist/preparation.js +210 -92
  176. package/dist/presence.d.ts +58 -0
  177. package/dist/presence.d.ts.map +1 -0
  178. package/dist/presence.js +231 -0
  179. package/dist/queries.d.ts +2 -2
  180. package/dist/queries.d.ts.map +1 -1
  181. package/dist/queries.js +138 -80
  182. package/dist/query.d.ts +2 -3
  183. package/dist/query.d.ts.map +1 -1
  184. package/dist/questions.d.ts +1 -5
  185. package/dist/questions.d.ts.map +1 -1
  186. package/dist/questions.js +3 -8
  187. package/dist/queue.d.ts +40 -0
  188. package/dist/queue.d.ts.map +1 -0
  189. package/dist/queue.js +482 -0
  190. package/dist/reach.d.ts +15 -6
  191. package/dist/reach.d.ts.map +1 -1
  192. package/dist/reach.js +200 -40
  193. package/dist/react.d.ts +17 -1
  194. package/dist/react.d.ts.map +1 -1
  195. package/dist/react.js +33 -1
  196. package/dist/readiness.d.ts +23 -4
  197. package/dist/readiness.d.ts.map +1 -1
  198. package/dist/readiness.js +18 -10
  199. package/dist/reads.d.ts +5 -3
  200. package/dist/reads.d.ts.map +1 -1
  201. package/dist/reads.js +30 -15
  202. package/dist/recovery.d.ts +4 -1
  203. package/dist/recovery.d.ts.map +1 -1
  204. package/dist/recovery.js +31 -8
  205. package/dist/refresh.d.ts +1 -1
  206. package/dist/refresh.js +6 -6
  207. package/dist/refusal.d.ts.map +1 -1
  208. package/dist/refusal.js +5 -6
  209. package/dist/replica.d.ts +6 -17
  210. package/dist/replica.d.ts.map +1 -1
  211. package/dist/replica.js +18 -20
  212. package/dist/retried.d.ts +30 -0
  213. package/dist/retried.d.ts.map +1 -0
  214. package/dist/retried.js +72 -0
  215. package/dist/room-draft.d.ts +32 -9
  216. package/dist/room-draft.d.ts.map +1 -1
  217. package/dist/room-draft.js +282 -72
  218. package/dist/room-drafts.d.ts.map +1 -1
  219. package/dist/room-drafts.js +29 -23
  220. package/dist/room-list.d.ts +10 -0
  221. package/dist/room-list.d.ts.map +1 -0
  222. package/dist/room-list.js +47 -0
  223. package/dist/rows.d.ts +11 -3
  224. package/dist/rows.d.ts.map +1 -1
  225. package/dist/rows.js +28 -15
  226. package/dist/run-tables.d.ts +25 -0
  227. package/dist/run-tables.d.ts.map +1 -0
  228. package/dist/run-tables.js +98 -0
  229. package/dist/running.d.ts +5 -0
  230. package/dist/running.d.ts.map +1 -0
  231. package/dist/running.js +10 -0
  232. package/dist/runs.d.ts +27 -0
  233. package/dist/runs.d.ts.map +1 -0
  234. package/dist/runs.js +92 -0
  235. package/dist/runtime.d.ts +15 -20
  236. package/dist/runtime.d.ts.map +1 -1
  237. package/dist/save.d.ts +43 -90
  238. package/dist/save.d.ts.map +1 -1
  239. package/dist/save.js +338 -236
  240. package/dist/saves.d.ts +9 -45
  241. package/dist/saves.d.ts.map +1 -1
  242. package/dist/saves.js +208 -752
  243. package/dist/seed.d.ts.map +1 -1
  244. package/dist/seed.js +4 -5
  245. package/dist/selector.d.ts +3 -0
  246. package/dist/selector.d.ts.map +1 -0
  247. package/dist/selector.js +22 -0
  248. package/dist/shared.d.ts +0 -5
  249. package/dist/shared.d.ts.map +1 -1
  250. package/dist/shared.js +4 -90
  251. package/dist/shown.d.ts +10 -6
  252. package/dist/shown.d.ts.map +1 -1
  253. package/dist/shown.js +105 -73
  254. package/dist/socket/lanes.d.ts +12 -4
  255. package/dist/socket/lanes.d.ts.map +1 -1
  256. package/dist/socket/lanes.js +34 -8
  257. package/dist/socket/session.d.ts +4 -2
  258. package/dist/socket/session.d.ts.map +1 -1
  259. package/dist/socket/session.js +3 -0
  260. package/dist/socket.d.ts +9 -7
  261. package/dist/socket.d.ts.map +1 -1
  262. package/dist/socket.js +19 -18
  263. package/dist/source.d.ts +42 -7
  264. package/dist/source.d.ts.map +1 -1
  265. package/dist/source.js +81 -13
  266. package/dist/store/installs.d.ts +2 -2
  267. package/dist/store/installs.d.ts.map +1 -1
  268. package/dist/store/installs.js +10 -4
  269. package/dist/store/readiness.d.ts +5 -4
  270. package/dist/store/readiness.d.ts.map +1 -1
  271. package/dist/store/readiness.js +28 -12
  272. package/dist/store/records.d.ts +32 -10
  273. package/dist/store/records.d.ts.map +1 -1
  274. package/dist/store/records.js +71 -28
  275. package/dist/store/rooms-shown.d.ts +8 -0
  276. package/dist/store/rooms-shown.d.ts.map +1 -0
  277. package/dist/store/rooms-shown.js +13 -0
  278. package/dist/store/space.d.ts +1 -1
  279. package/dist/store/space.d.ts.map +1 -1
  280. package/dist/store/space.js +0 -2
  281. package/dist/store/watchers.js +1 -2
  282. package/dist/store.d.ts +25 -15
  283. package/dist/store.d.ts.map +1 -1
  284. package/dist/store.js +50 -18
  285. package/dist/stored.d.ts +42 -17
  286. package/dist/stored.d.ts.map +1 -1
  287. package/dist/stored.js +220 -62
  288. package/dist/surface.d.ts +20 -8
  289. package/dist/surface.d.ts.map +1 -1
  290. package/dist/surface.js +70 -35
  291. package/dist/sync.d.ts +4 -2
  292. package/dist/sync.d.ts.map +1 -1
  293. package/dist/sync.js +134 -109
  294. package/dist/tables.d.ts +40 -57
  295. package/dist/tables.d.ts.map +1 -1
  296. package/dist/tables.js +114 -334
  297. package/dist/tallies.d.ts.map +1 -1
  298. package/dist/tallies.js +8 -9
  299. package/dist/validation.d.ts +2 -0
  300. package/dist/validation.d.ts.map +1 -1
  301. package/dist/validation.js +51 -11
  302. package/dist/wire.d.ts +72 -12
  303. package/dist/wire.d.ts.map +1 -1
  304. package/dist/wire.js +31 -14
  305. package/package.json +4 -3
  306. package/dist/attempt.d.ts +0 -16
  307. package/dist/attempt.d.ts.map +0 -1
  308. package/dist/attempt.js +0 -70
  309. package/dist/binary.d.ts.map +0 -1
  310. package/dist/binary.js +0 -8
  311. package/dist/job.d.ts +0 -33
  312. package/dist/job.d.ts.map +0 -1
  313. package/dist/job.js +0 -54
  314. package/dist/log.d.ts +0 -6
  315. package/dist/log.d.ts.map +0 -1
  316. package/dist/log.js +0 -14
  317. package/dist/socket/rooms.d.ts +0 -44
  318. package/dist/socket/rooms.d.ts.map +0 -1
  319. package/dist/socket/rooms.js +0 -90
  320. package/dist/store/fences.d.ts +0 -16
  321. package/dist/store/fences.d.ts.map +0 -1
  322. 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,18 @@ export function unheldView(conversation) {
21
24
  commands: [],
22
25
  options: [],
23
26
  starting: null,
24
- subagents: [],
25
- link: null,
27
+ state: null,
28
+ deliveryWaiting: [],
26
29
  deliveryUnknown: [],
27
30
  chatDraft: null,
28
- jobs: [],
31
+ dismissedSuggestion: null,
32
+ pins: [],
29
33
  };
30
34
  }
31
35
  export const SESSION_STARTED = 'Session started';
32
36
  const NEW_CHAT = 'new:';
37
+ const LISTING = 'listing';
38
+ export const CATCHING_UP = 'catching-up';
33
39
  export const HISTORY_CONVERSATIONS = 64;
34
40
  export function newChat(agent) {
35
41
  return `${NEW_CHAT}${agent}`;
@@ -42,52 +48,15 @@ function creationOf(input) {
42
48
  }
43
49
  function startedTurn(turn, operation) {
44
50
  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) };
51
+ return { turn, started: operation.at, parts: [] };
52
+ const parts = applied([], operation);
53
+ return parts === null ? null : { turn, started: null, parts };
68
54
  }
69
55
  function appliedTurn(held, operation) {
70
56
  if (operation.op === 'begin')
71
57
  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
- };
58
+ const parts = applied(held.parts, operation);
59
+ return parts === null ? null : { ...held, parts };
91
60
  }
92
61
  function windowOf(held, arriving, limit) {
93
62
  const byIdentity = new Map(held.map((message) => [message.identity, message]));
@@ -110,7 +79,7 @@ function frameOf(asked, messages, more, total) {
110
79
  };
111
80
  }
112
81
  function newestOf(arriving, from) {
113
- 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);
114
83
  }
115
84
  function acceptedMessage(value) {
116
85
  if (typeof value !== 'object' || value === null)
@@ -119,6 +88,7 @@ function acceptedMessage(value) {
119
88
  if (typeof record.identity !== 'string' ||
120
89
  typeof record.turn !== 'string' ||
121
90
  typeof record.position !== 'string' ||
91
+ typeof record.change !== 'string' ||
122
92
  typeof record.text !== 'string') {
123
93
  return null;
124
94
  }
@@ -126,6 +96,7 @@ function acceptedMessage(value) {
126
96
  identity: record.identity,
127
97
  turn: record.turn,
128
98
  position: record.position,
99
+ change: record.change,
129
100
  text: record.text,
130
101
  message: structuredClone(record.message),
131
102
  };
@@ -227,17 +198,17 @@ function withSubmitted(draft, submitted) {
227
198
  const { submitted: _held, ...unsent } = draft;
228
199
  return submitted.length === 0 ? unsent : { ...unsent, submitted };
229
200
  }
230
- function prepended(draft, said) {
231
- 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');
232
203
  }
233
204
  function returned(draft, submission, others) {
234
205
  return withSubmitted({
235
206
  conversation: draft.conversation,
236
- ...prepended(draft, submission),
207
+ text: joinedDraft(submission.text, draft.text),
237
208
  revision: chatDraftRevision(),
238
209
  }, others);
239
210
  }
240
- function viewOf(alive, conversation, held, frame, draft, ended, marked) {
211
+ function viewOf(alive, conversation, held, frame, { draft, ended, dismissed, pins }) {
241
212
  const facts = alive.liveOf(conversation);
242
213
  return {
243
214
  conversation,
@@ -255,26 +226,45 @@ function viewOf(alive, conversation, held, frame, draft, ended, marked) {
255
226
  commands: structuredClone(facts.commands),
256
227
  options: structuredClone(facts.options),
257
228
  starting: facts.starting,
258
- subagents: structuredClone(facts.subagents),
259
- link: facts.link === null ? null : { ...facts.link },
229
+ state: sessionOf(facts, held, ended),
230
+ deliveryWaiting: [...facts.deliveryWaiting],
260
231
  deliveryUnknown: [...facts.deliveryUnknown],
261
232
  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),
233
+ dismissedSuggestion: dismissed,
234
+ pins: [...pins],
265
235
  };
266
236
  }
237
+ function standing(facts, saved, ended) {
238
+ if (facts.state !== null)
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) {
246
+ const saved = [...held]
247
+ .reverse()
248
+ .map(({ message }) => record(message))
249
+ .find(({ content }) => isTurn(content));
250
+ const at = saved === undefined ? null : text(saved, 'accepted_at');
251
+ return standing(facts, saved === undefined || !isTurn(saved.content) || at === null
252
+ ? null
253
+ : { state: saved.content.state, at }, ended);
254
+ }
267
255
  function emit(alive, conversation) {
268
256
  if (alive.deps.closed())
269
257
  return;
270
258
  for (const view of alive.views()) {
271
- const { window, draft, ended, marked } = view.held.now();
259
+ const held = view.held.now();
260
+ const { window } = held;
272
261
  if (view.conversation !== conversation || window === null)
273
262
  continue;
274
263
  const ready = view.ready();
275
264
  if (ready.length === 0)
276
265
  continue;
277
- const value = viewOf(alive, conversation, window, sharedFrame(view, window), draft, ended, marked);
266
+ const frame = sharedFrame(view, window);
267
+ const value = viewOf(alive, conversation, window, frame, held);
278
268
  for (const watcher of ready)
279
269
  watcher.notify(value);
280
270
  }
@@ -285,106 +275,137 @@ function sharedFrame(view, window) {
285
275
  }
286
276
  async function slide(alive, local, conversation, arriving) {
287
277
  let count;
288
- let marked;
289
278
  for (const view of alive.views()) {
290
279
  const { held } = view;
291
280
  const { window, total } = held.now();
292
281
  if (view.conversation !== conversation || window === null)
293
282
  continue;
294
283
  if (arriving === null) {
295
- held.set({ window: [], marked: [], more: false, total: total === null ? null : 0 });
284
+ held.set({ window: [], more: false, total: total === null ? null : 0 });
296
285
  continue;
297
286
  }
298
287
  const joining = arriving.filter((message) => precedes(message, view.asked.before));
299
288
  const next = windowOf(window, joining, view.asked.limit);
300
289
  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
290
  if (held.now().total !== null && arriving.length > 0) {
306
291
  count ??= await local.messageCount(conversation);
307
292
  held.set({ total: count });
308
293
  }
309
294
  }
310
295
  }
311
- function stateOf(alive, conversation) {
312
- const facts = alive.liveOf(conversation);
313
- if (facts.questions.length > 0)
296
+ function stateOf({ activity }) {
297
+ if (activity === null)
298
+ return 'idle';
299
+ if (activity.turn === 'asking')
314
300
  return 'asking';
315
- return facts.turn === null ? 'idle' : 'working';
301
+ return activity.turn === 'running' || activity.background !== false ? 'working' : 'idle';
316
302
  }
317
303
  function rowOf(alive, conversation) {
318
- const { turn, questions } = alive.liveOf(conversation);
304
+ const { turn, questions, deliveryWaiting } = alive.liveOf(conversation);
319
305
  return {
320
- state: stateOf(alive, conversation),
321
306
  started: turn?.started ?? null,
322
307
  line: turn === null ? null : runningLine(turn.parts),
323
308
  question: openQuestion(questions),
324
309
  asked: questionAsked(questions),
310
+ deliveryWaiting: [...deliveryWaiting],
325
311
  };
326
312
  }
327
313
  async function entries(alive) {
328
- const list = await heldList(alive);
329
- 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)))
330
320
  return null;
331
321
  const held = list.length === 0
332
322
  ? []
333
323
  : await alive.deps.durably((local) => local.conversationRows(list.map(({ conversation }) => conversation)));
334
324
  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
- }));
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
+ });
346
345
  }
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 : [];
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: [] };
352
353
  }
353
354
  async function endedIn(alive, conversation) {
354
- const list = await heldList(alive);
355
- 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 };
356
391
  }
357
392
  function listChanged(alive, change) {
358
393
  void alive.deps
359
- .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 });
365
- alive.created(entry.conversation);
366
- }
367
- if (change.gone !== null)
368
- held.delete(change.gone);
369
- const list = [...held.values()];
370
- if (JSON.stringify(list) !== JSON.stringify(conversationList)) {
371
- await local.fact('conversationList', list);
372
- }
373
- if (change.cursor !== undefined) {
374
- if (change.cursor !== conversationCursor) {
375
- await local.fact('conversationCursor', change.cursor);
376
- }
377
- alive.deps.stream()?.hold(change.cursor);
378
- }
379
- 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 ?? [] };
380
401
  })
381
- .catch(async () => (await (await alive.deps.local()).facts()).conversationList ?? [])
382
- .then((list) => {
402
+ .then((listing) => {
383
403
  if (alive.deps.closed() || change.paged === true)
384
404
  return;
405
+ const list = listing.conversations;
385
406
  endedAnew(alive, list);
386
407
  if (change.cursor !== undefined) {
387
- alive.held.set({ listKnown: true });
408
+ alive.held.set({ listKnown: true, listHeld: true, openingListed: true });
388
409
  settleBehind(alive, list);
389
410
  openingEnded(alive);
390
411
  alive.deps.settled();
@@ -392,14 +413,15 @@ function listChanged(alive, change) {
392
413
  emitList(alive);
393
414
  if (!alive.known())
394
415
  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)))) {
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) {
400
422
  alive.deps.again();
401
423
  }
402
- }, () => { });
424
+ }, uncaught);
403
425
  }
404
426
  function endedAnew(alive, list) {
405
427
  const entries = new Map(list.map((entry) => [entry.conversation, entry]));
@@ -412,20 +434,23 @@ function endedAnew(alive, list) {
412
434
  emit(alive, view.conversation);
413
435
  }
414
436
  }
415
- function shownList(alive, work) {
416
- const reading = heldList(alive);
437
+ function shownListing(alive, work) {
438
+ const reading = heldListing(alive);
417
439
  alive.inOrder(async () => {
418
- const list = await reading.catch(() => null);
419
- if (list === null || alive.deps.closed())
440
+ const listing = await reading.catch((error) => {
441
+ uncaught(error);
442
+ return null;
443
+ });
444
+ if (listing === null || alive.deps.closed())
420
445
  return;
421
- work(list);
446
+ work(listing);
422
447
  });
423
448
  }
424
449
  async function told(alive) {
425
450
  try {
426
451
  while (alive.held.now().listOwed && !alive.deps.closed()) {
427
452
  alive.held.set({ listOwed: false });
428
- 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);
429
454
  if (alive.deps.closed())
430
455
  return;
431
456
  if (value === null)
@@ -463,75 +488,73 @@ export function conversationsLearned(alive) {
463
488
  return;
464
489
  if (alive.listeners.waiting())
465
490
  tellList(alive);
466
- if (!alive.agentListeners.waiting())
491
+ if (!alive.agentListeners.waiting() && !alive.listedListeners.waiting())
467
492
  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 }))
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
+ }))
480
514
  .sort((left, right) => left.name === right.name
481
515
  ? left.identity.localeCompare(right.identity)
482
516
  : left.name.localeCompare(right.name));
483
517
  }
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
518
  function emitAgents(alive) {
508
- if (alive.deps.closed() || !alive.agentListeners.told())
519
+ const { agentListeners, listedListeners } = alive;
520
+ if (alive.deps.closed() || (!agentListeners.told() && !listedListeners.told()))
509
521
  return;
510
- shownList(alive, (list) => {
511
- const value = agents(alive, list);
522
+ shownListing(alive, (listing) => {
523
+ const value = agentsOf(listing);
512
524
  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);
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
+ }
517
534
  });
518
535
  }
536
+ function ran({ state, process }) {
537
+ return JSON.stringify([state?.running ?? null, process?.running ?? null]);
538
+ }
519
539
  function setLive(alive, conversation, next) {
540
+ const was = ran(alive.liveOf(conversation));
520
541
  const before = rowOf(alive, conversation);
521
542
  alive.holdLive(conversation, next);
522
543
  const after = rowOf(alive, conversation);
523
- 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())
524
547
  emitList(alive);
525
548
  else if (after.line !== before.line ||
526
549
  after.question !== before.question ||
527
550
  after.asked !== before.asked)
528
551
  tellSoon(alive);
529
552
  }
530
- function retiredChat(conversation, list) {
553
+ function retiredChat(conversation, listing) {
531
554
  if (!conversation.startsWith(NEW_CHAT))
532
555
  return false;
533
556
  const agent = conversation.slice(NEW_CHAT.length);
534
- return list.some((entry) => entry.agent === agent && entry.agentRetired);
557
+ return listing.agents.some((one) => one.id === agent && one.retired_at !== null);
535
558
  }
536
559
  function live(alive, conversation, change) {
537
560
  alive.factsHeard(conversation, FACTS.filter((one) => one in change));
@@ -550,8 +573,8 @@ function openingEnded(alive) {
550
573
  commands: facts.has('commands') ? QUIET.commands : held.commands,
551
574
  options: facts.has('options') ? QUIET.options : held.options,
552
575
  starting: facts.has('starting') ? QUIET.starting : held.starting,
553
- subagents: facts.has('subagents') ? QUIET.subagents : held.subagents,
554
- link: facts.has('link') ? QUIET.link : held.link,
576
+ state: facts.has('state') ? QUIET.state : held.state,
577
+ deliveryWaiting: facts.has('deliveryWaiting') ? QUIET.deliveryWaiting : held.deliveryWaiting,
555
578
  deliveryUnknown: facts.has('deliveryUnknown') ? QUIET.deliveryUnknown : held.deliveryUnknown,
556
579
  });
557
580
  emit(alive, conversation);
@@ -583,19 +606,13 @@ function handReplicated(alive) {
583
606
  }
584
607
  async function remove(alive, leaving) {
585
608
  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);
609
+ const kept = new Set(await local.dropConversations(leaving));
593
610
  const changed = [];
594
611
  for (const conversation of leaving) {
595
612
  if (kept.has(conversation) || alive.liveHeld(conversation) !== undefined) {
596
613
  changed.push(conversation);
597
614
  }
598
- shownDraft(alive, conversation, null);
615
+ heldInViews(alive, conversation, { draft: null, dismissed: null });
599
616
  alive.forget(conversation);
600
617
  setLive(alive, conversation, null);
601
618
  await slide(alive, local, conversation, null);
@@ -624,7 +641,7 @@ async function readsOf(local, conversations) {
624
641
  return new Map((await local.conversations(conversations)).map((held) => [held.conversation, held]));
625
642
  }
626
643
  function retiredDrafts(alive, list) {
627
- if (!list.some((entry) => entry.agentRetired))
644
+ if (!list.agents.some((agent) => agent.retired_at !== null))
628
645
  return;
629
646
  void alive.deps
630
647
  .local()
@@ -634,7 +651,7 @@ function retiredDrafts(alive, list) {
634
651
  held.some(({ conversation }) => retiredChat(conversation, list))) {
635
652
  alive.deps.again();
636
653
  }
637
- }, () => { });
654
+ }, uncaught);
638
655
  }
639
656
  function settleBehind(alive, list) {
640
657
  void alive.deps
@@ -660,21 +677,19 @@ function settleBehind(alive, list) {
660
677
  alive.deps.settled();
661
678
  if (owed)
662
679
  alive.deps.again();
663
- }, () => { });
680
+ }, uncaught);
664
681
  }
665
682
  function keep(alive, conversation, message) {
666
683
  return alive.deps.durably(async (local) => {
667
684
  if (!alive.held.now().replicated.has(conversation))
668
685
  return;
669
686
  const read = (await readsOf(local, [conversation])).get(conversation)?.read;
670
- const advances = read !== undefined &&
671
- !alive.behind(conversation) &&
672
- (read === null || message.position > read);
687
+ const advances = read !== undefined && !alive.behind(conversation) && (read === null || message.change > read);
673
688
  await sealed(alive, local, conversation, new Set([message.turn]));
674
689
  await local.keepMessages({
675
690
  conversation,
676
691
  messages: [message],
677
- ...(advances ? { read: { at: message.position, partial: false } } : {}),
692
+ ...(advances ? { read: { at: message.change, partial: false } } : {}),
678
693
  });
679
694
  sealTurns(alive, conversation, new Set([message.turn]));
680
695
  await slide(alive, local, conversation, [message]);
@@ -786,22 +801,21 @@ function heard(alive, event) {
786
801
  live(alive, conversation, { starting: event.step });
787
802
  return;
788
803
  }
789
- if (event.kind === 'subagents') {
790
- live(alive, conversation, { subagents: structuredClone(event.subagents) });
791
- return;
792
- }
793
- if (event.kind === 'link') {
794
- live(alive, conversation, { link: { connected: event.connected } });
804
+ if (event.kind === 'state') {
805
+ live(alive, conversation, { state: structuredClone(event.state) });
795
806
  return;
796
807
  }
797
808
  if (event.kind === 'deliveries') {
798
- live(alive, conversation, { deliveryUnknown: [...event.unknown] });
809
+ live(alive, conversation, {
810
+ deliveryWaiting: [...event.waiting],
811
+ deliveryUnknown: [...event.unknown],
812
+ });
799
813
  return;
800
814
  }
801
815
  const arriving = acceptedMessage(event.message);
802
816
  if (arriving === null)
803
817
  return;
804
- alive.heardAt(conversation, arriving.position);
818
+ alive.heardAt(conversation, arriving.change);
805
819
  if (alive.held.now().replicated.has(conversation)) {
806
820
  if (alive.deps.full()) {
807
821
  alive.gapOpened(conversation);
@@ -810,6 +824,8 @@ function heard(alive, event) {
810
824
  void keep(alive, conversation, arriving).catch((error) => {
811
825
  if (refusedForSpace(error))
812
826
  alive.gapOpened(conversation);
827
+ else
828
+ uncaught(error);
813
829
  });
814
830
  return;
815
831
  }
@@ -817,19 +833,27 @@ function heard(alive, event) {
817
833
  emit(alive, conversation);
818
834
  }
819
835
  async function kept(alive, local, conversation, following) {
820
- const shown = following === null || empty(following) ? null : following;
821
- if (shown === null)
836
+ const draft = following === null || empty(following) ? null : following;
837
+ if (draft === null)
822
838
  await local.dropChatDraft(conversation);
823
839
  else
824
- await local.keepChatDrafts([shown]);
825
- shownDraft(alive, conversation, shown);
840
+ await local.keepChatDrafts([draft]);
841
+ heldInViews(alive, conversation, { draft });
826
842
  }
827
- function shownDraft(alive, conversation, draft) {
843
+ function heldInViews(alive, conversation, held) {
828
844
  for (const view of alive.views()) {
829
845
  if (view.conversation === conversation)
830
- view.held.set({ draft });
846
+ view.held.set(held);
831
847
  }
832
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
+ }
833
857
  async function draftAt(local, conversation) {
834
858
  return (await local.chatDraft(conversation)) ?? undefined;
835
859
  }
@@ -860,9 +884,9 @@ function moved(alive, agent, operation, to) {
860
884
  : [{ ...unsent, conversation: to }];
861
885
  const left = { ...(taken.length === 0 ? unsent : blank), moves: [...earlier, operation] };
862
886
  await local.keepChatDrafts([left, ...taken]);
863
- shownDraft(alive, from, left);
887
+ heldInViews(alive, from, { draft: left });
864
888
  for (const one of taken)
865
- shownDraft(alive, to, one);
889
+ heldInViews(alive, to, { draft: one });
866
890
  alive.deps.publish(conversationChangeSet([from, to]));
867
891
  });
868
892
  }
@@ -893,13 +917,10 @@ async function submitted(alive, conversation, operation, draft) {
893
917
  });
894
918
  return submission;
895
919
  }
896
- async function dispatched(alive, conversation, submission) {
920
+ async function dispatched(alive, conversation, submission, resent = false) {
897
921
  const dispatch = alive.sendingBegan(submission.operation);
898
922
  try {
899
- const result = await invoke(alive, 'conversations/send', conversation, {
900
- operation_id: submission.operation,
901
- text: submission.text,
902
- });
923
+ const result = await invoke(alive, 'conversations/send', conversation, submission.operation, { text: submission.text }, { step: 'sending', instance: `message ${submission.operation}` });
903
924
  dispatch.set({ answered: result.kind !== 'transport-uncertainty' });
904
925
  if (result.kind === 'revoked-authorization')
905
926
  return result;
@@ -907,6 +928,7 @@ async function dispatched(alive, conversation, submission) {
907
928
  alive.deps.unanswered();
908
929
  return result;
909
930
  }
931
+ alive.deps.heard(submission.operation, resent);
910
932
  const turn = answered(result, 'message_id');
911
933
  await withDraft(alive, conversation, async (current, local) => {
912
934
  const held = current?.submitted?.find((one) => one.operation === submission.operation);
@@ -926,11 +948,12 @@ async function dispatched(alive, conversation, submission) {
926
948
  alive.sendingEnded(submission.operation, dispatch);
927
949
  }
928
950
  }
929
- function invoke(alive, name, conversation, extra = { operation_id: changeBatchIdentity() }) {
930
- 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);
931
953
  }
932
- export function conversationSurface(alive) {
954
+ export function conversationSurface(alive, files) {
933
955
  return {
956
+ ...files,
934
957
  list() {
935
958
  return new Promise((settle) => {
936
959
  const answer = (listed) => {
@@ -948,8 +971,12 @@ export function conversationSurface(alive) {
948
971
  return alive.deps.durably(async (local) => {
949
972
  const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before, phrase);
950
973
  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));
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
+ }));
953
980
  });
954
981
  },
955
982
  async create(input) {
@@ -957,7 +984,7 @@ export function conversationSurface(alive) {
957
984
  const conversation = answered(result, 'conversation_id');
958
985
  if (conversation === null)
959
986
  return result;
960
- const listed = (await heldList(alive)) ?? [];
987
+ const listed = (await heldListing(alive))?.conversations ?? [];
961
988
  if (!listed.some((entry) => entry.conversation === conversation)) {
962
989
  alive.creating(conversation);
963
990
  }
@@ -986,40 +1013,90 @@ export function conversationSurface(alive) {
986
1013
  }
987
1014
  return OFFLINE;
988
1015
  },
989
- answer(conversation, answer) {
990
- return invoke(alive, 'conversations/interaction/answer', conversation, {
991
- operation_id: changeBatchIdentity(),
992
- ...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,
1031
+ });
1032
+ },
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
+ });
1041
+ },
1042
+ rename(conversation, name, operation) {
1043
+ return invoke(alive, 'conversations/rename', conversation, operation, { name });
1044
+ },
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,
993
1051
  });
994
1052
  },
995
- interrupt(conversation) {
996
- return invoke(alive, 'conversations/interrupt', conversation);
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
+ }
997
1062
  },
998
- kill(conversation) {
999
- return invoke(alive, 'conversations/kill', conversation);
1063
+ async openTurn(conversation, turn) {
1064
+ await alive.deps.durably((local) => local.keepConversationFact({ conversation, name: 'openedTurn', value: turn }));
1065
+ tellList(alive);
1000
1066
  },
1001
- delete(conversation) {
1002
- return invoke(alive, 'conversations/delete', conversation);
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
+ });
1003
1079
  },
1004
1080
  };
1005
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
+ }
1006
1090
  export function chatDraftSurface(alive) {
1007
1091
  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
- }));
1092
+ write(conversation, draft, revision) {
1093
+ return rewritten(alive, conversation, () => draft.text, revision);
1015
1094
  },
1016
1095
  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
- }));
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));
1023
1100
  },
1024
1101
  discard(conversation) {
1025
1102
  return withDraft(alive, conversation, (current) => current === undefined
@@ -1029,31 +1106,37 @@ export function chatDraftSurface(alive) {
1029
1106
  };
1030
1107
  }
1031
1108
  export async function conversationsOpened(alive, local) {
1032
- const { conversationList, conversationCursor } = await local.facts();
1033
- 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())
1034
1114
  return;
1035
1115
  alive.deps.stream()?.hold(conversationCursor);
1036
1116
  conversationsLearned(alive);
1037
1117
  }
1118
+ export function reconcileOpening(alive) {
1119
+ alive.held.set({ openingRead: true });
1120
+ return reconcileConversations(alive);
1121
+ }
1038
1122
  export async function reconcileConversations(alive) {
1039
1123
  if (!alive.known())
1040
1124
  return;
1041
1125
  const local = await alive.deps.local();
1042
- const [held, kept, { conversationList }] = await Promise.all([
1126
+ const [held, stored, { conversationList, agentList }] = await Promise.all([
1043
1127
  local.conversations(),
1044
- local.chatDrafts(),
1128
+ local.storedConversations(),
1045
1129
  local.facts(),
1046
1130
  ]);
1047
1131
  const list = conversationList ?? [];
1132
+ const listing = { conversations: list, agents: agentList ?? [] };
1048
1133
  const entries = new Map(list.map((entry) => [entry.conversation, entry]));
1049
1134
  const named = new Set(entries.keys());
1050
1135
  const reads = new Map(held.map((one) => [one.conversation, one]));
1051
1136
  const { replicated } = alive.held.now();
1052
- const leaving = [
1053
- ...new Set([...replicated, ...reads.keys(), ...kept.map(({ conversation }) => conversation)]),
1054
- ].filter((conversation) => !named.has(conversation) &&
1137
+ const leaving = [...new Set([...replicated, ...stored])].filter((conversation) => !named.has(conversation) &&
1055
1138
  !alive.isCreating(conversation) &&
1056
- (!conversation.startsWith(NEW_CHAT) || retiredChat(conversation, list)));
1139
+ (!conversation.startsWith(NEW_CHAT) || retiredChat(conversation, listing)));
1057
1140
  for (const conversation of named) {
1058
1141
  if (!replicated.has(conversation) &&
1059
1142
  missed(alive, conversation, reads.get(conversation), entries.get(conversation))) {
@@ -1088,6 +1171,17 @@ export async function catchUpConversations(alive, progress) {
1088
1171
  if (!alive.current())
1089
1172
  progress.complete = false;
1090
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
+ }
1091
1185
  export async function resendConversations(alive, progress) {
1092
1186
  const local = await alive.deps.local();
1093
1187
  const tried = new Set();
@@ -1099,7 +1193,7 @@ export async function resendConversations(alive, progress) {
1099
1193
  if (alive.deps.closed())
1100
1194
  return;
1101
1195
  tried.add(submission.operation);
1102
- const result = await dispatched(alive, conversation, submission);
1196
+ const result = await dispatched(alive, conversation, submission, true);
1103
1197
  if (result.kind === 'revoked-authorization')
1104
1198
  return;
1105
1199
  if (result.kind === 'transport-uncertainty') {
@@ -1119,23 +1213,25 @@ export function watchConversation(alive, conversation, options, deliver) {
1119
1213
  const { view, watcher } = alive.viewJoined(key, conversation, asked, deliver);
1120
1214
  const { held } = view;
1121
1215
  const reading = new AbortController();
1122
- void untilAnswered(() => alive.deps.durably(async (local) => {
1216
+ void untilAnswered(retrying(alive.deps.retries, 'reading', `window ${key}`, () => alive.deps.durably(async (local) => {
1123
1217
  if (held.now().window !== null)
1124
1218
  return;
1125
1219
  const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before);
1126
1220
  held.set({ draft: (await local.chatDraft(conversation)) ?? null });
1127
1221
  held.set({ ended: await endedIn(alive, conversation) });
1222
+ held.set({ dismissed: await dismissedIn(local, conversation) });
1223
+ held.set({ pins: (await local.facts()).chatPins });
1128
1224
  held.set({ window: messages });
1129
- held.set({ marked: await local.jobMarkers(conversation) });
1130
1225
  held.set({ more });
1131
1226
  held.set({ total: asked.total ? await local.messageCount(conversation) : null });
1132
- }), reading.signal)
1227
+ })), reading.signal)
1133
1228
  .then(() => {
1134
1229
  if (alive.deps.closed() || !view.admit(watcher))
1135
1230
  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));
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));
1139
1235
  })
1140
1236
  .catch(() => { });
1141
1237
  return () => {
@@ -1151,9 +1247,13 @@ export function watchConversationList(alive, deliver) {
1151
1247
  export function watchConversationAgents(alive, deliver) {
1152
1248
  alive.agentListeners.join(deliver);
1153
1249
  conversationsLearned(alive);
1154
- readAgents(alive);
1155
1250
  return () => alive.agentListeners.leave(deliver);
1156
1251
  }
1252
+ export function watchListedAgents(alive, deliver) {
1253
+ alive.listedListeners.join(deliver);
1254
+ conversationsLearned(alive);
1255
+ return () => alive.listedListeners.leave(deliver);
1256
+ }
1157
1257
  export function conversationsChanged(alive, set) {
1158
1258
  for (const conversation of set.conversations)
1159
1259
  emit(alive, conversation);
@@ -1165,26 +1265,32 @@ export function receiveConversationEvent(alive, event) {
1165
1265
  if (event.whole)
1166
1266
  alive.relistBegun();
1167
1267
  if (alive.relisting())
1168
- alive.relistHeard(event.entries);
1268
+ alive.relistHeard(event.entries, event.agents);
1169
1269
  if (event.more && alive.relisting())
1170
1270
  return;
1171
1271
  const whole = alive.relistTaken();
1172
1272
  listChanged(alive, {
1173
1273
  replacing: whole !== null,
1174
- 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()],
1175
1276
  gone: null,
1176
1277
  ...(event.more ? { paged: true } : { cursor: event.cursor }),
1177
1278
  });
1178
1279
  return;
1179
1280
  }
1180
1281
  if (event.kind === 'listed') {
1181
- alive.relistHeard([event.entry]);
1182
- 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 });
1183
1289
  return;
1184
1290
  }
1185
1291
  if (event.kind === 'unlisted') {
1186
1292
  alive.relistDropped(event.conversation);
1187
- listChanged(alive, { replacing: false, set: [], gone: event.conversation });
1293
+ listChanged(alive, { replacing: false, set: [], agents: [], gone: event.conversation });
1188
1294
  return;
1189
1295
  }
1190
1296
  heard(alive, event);