@agentshouse/mdruntime 0.1.0-cb9c79aee537a → 0.1.0-cba3c6127c178

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 (233) hide show
  1. package/README.md +19 -16
  2. package/dist/adapter.d.ts +135 -85
  3. package/dist/adapter.d.ts.map +1 -1
  4. package/dist/attached-model.d.ts +7 -10
  5. package/dist/attached-model.d.ts.map +1 -1
  6. package/dist/attached-model.js +40 -41
  7. package/dist/binary.d.ts +0 -5
  8. package/dist/binary.d.ts.map +1 -1
  9. package/dist/binary.js +1 -1
  10. package/dist/bootstrap.d.ts +30 -11
  11. package/dist/bootstrap.d.ts.map +1 -1
  12. package/dist/bootstrap.js +319 -111
  13. package/dist/chain.d.ts +12 -0
  14. package/dist/chain.d.ts.map +1 -0
  15. package/dist/chain.js +64 -0
  16. package/dist/change-set.d.ts +9 -16
  17. package/dist/change-set.d.ts.map +1 -1
  18. package/dist/change-set.js +11 -89
  19. package/dist/change.d.ts +31 -1
  20. package/dist/change.d.ts.map +1 -1
  21. package/dist/change.js +10 -1
  22. package/dist/client.d.ts +18 -10
  23. package/dist/client.d.ts.map +1 -1
  24. package/dist/client.js +82 -7
  25. package/dist/collection.d.ts +11 -22
  26. package/dist/collection.d.ts.map +1 -1
  27. package/dist/collection.js +39 -356
  28. package/dist/compile.d.ts +11 -0
  29. package/dist/compile.d.ts.map +1 -0
  30. package/dist/compile.js +586 -0
  31. package/dist/connection.d.ts +23 -15
  32. package/dist/connection.d.ts.map +1 -1
  33. package/dist/connection.js +2892 -1380
  34. package/dist/context.d.ts +2 -0
  35. package/dist/context.d.ts.map +1 -0
  36. package/dist/{chat/context.js → context.js} +1 -4
  37. package/dist/conversation.d.ts +24 -15
  38. package/dist/conversation.d.ts.map +1 -1
  39. package/dist/conversation.js +316 -195
  40. package/dist/digest.d.ts +7 -0
  41. package/dist/digest.d.ts.map +1 -0
  42. package/dist/digest.js +113 -0
  43. package/dist/durability.d.ts +7 -4
  44. package/dist/durability.d.ts.map +1 -1
  45. package/dist/durability.js +5 -0
  46. package/dist/entry.d.ts +5 -2
  47. package/dist/entry.d.ts.map +1 -1
  48. package/dist/entry.js +32 -29
  49. package/dist/file.d.ts +2 -19
  50. package/dist/file.d.ts.map +1 -1
  51. package/dist/file.js +7 -122
  52. package/dist/host.d.ts +3 -7
  53. package/dist/host.d.ts.map +1 -1
  54. package/dist/host.js +61 -36
  55. package/dist/identity.d.ts +0 -2
  56. package/dist/identity.d.ts.map +1 -1
  57. package/dist/identity.js +0 -6
  58. package/dist/index.d.ts +18 -12
  59. package/dist/index.d.ts.map +1 -1
  60. package/dist/index.js +8 -2
  61. package/dist/job.d.ts +14 -0
  62. package/dist/job.d.ts.map +1 -0
  63. package/dist/job.js +28 -0
  64. package/dist/live.d.ts +23 -0
  65. package/dist/live.d.ts.map +1 -0
  66. package/dist/live.js +127 -0
  67. package/dist/media.d.ts +8 -0
  68. package/dist/media.d.ts.map +1 -0
  69. package/dist/media.js +218 -0
  70. package/dist/memory.d.ts +13 -6
  71. package/dist/memory.d.ts.map +1 -1
  72. package/dist/memory.js +230 -105
  73. package/dist/messages.d.ts +28 -0
  74. package/dist/messages.d.ts.map +1 -0
  75. package/dist/messages.js +89 -0
  76. package/dist/model.d.ts +8 -8
  77. package/dist/model.d.ts.map +1 -1
  78. package/dist/model.js +7 -13
  79. package/dist/name.d.ts +4 -0
  80. package/dist/name.d.ts.map +1 -0
  81. package/dist/name.js +122 -0
  82. package/dist/observation.d.ts +15 -2
  83. package/dist/observation.d.ts.map +1 -1
  84. package/dist/observation.js +91 -2
  85. package/dist/opened.d.ts +22 -0
  86. package/dist/opened.d.ts.map +1 -0
  87. package/dist/opened.js +107 -0
  88. package/dist/outcome.d.ts +40 -8
  89. package/dist/outcome.d.ts.map +1 -1
  90. package/dist/outcome.js +38 -2
  91. package/dist/overlay.d.ts +2 -6
  92. package/dist/overlay.d.ts.map +1 -1
  93. package/dist/overlay.js +87 -176
  94. package/dist/paths.d.ts +1 -0
  95. package/dist/paths.d.ts.map +1 -1
  96. package/dist/paths.js +4 -0
  97. package/dist/query.d.ts +11 -206
  98. package/dist/query.d.ts.map +1 -1
  99. package/dist/query.js +3 -493
  100. package/dist/reach.d.ts +17 -8
  101. package/dist/reach.d.ts.map +1 -1
  102. package/dist/reach.js +212 -94
  103. package/dist/react.d.ts +3 -1
  104. package/dist/react.d.ts.map +1 -1
  105. package/dist/readiness.d.ts +14 -17
  106. package/dist/readiness.d.ts.map +1 -1
  107. package/dist/readiness.js +2 -8
  108. package/dist/recovery.d.ts +3 -2
  109. package/dist/recovery.d.ts.map +1 -1
  110. package/dist/recovery.js +34 -7
  111. package/dist/refresh.d.ts +18 -15
  112. package/dist/refresh.d.ts.map +1 -1
  113. package/dist/refresh.js +139 -58
  114. package/dist/replica.d.ts +58 -38
  115. package/dist/replica.d.ts.map +1 -1
  116. package/dist/replica.js +295 -197
  117. package/dist/room-draft.d.ts +25 -6
  118. package/dist/room-draft.d.ts.map +1 -1
  119. package/dist/room-draft.js +158 -71
  120. package/dist/rows.d.ts +31 -31
  121. package/dist/rows.d.ts.map +1 -1
  122. package/dist/rows.js +59 -123
  123. package/dist/save.d.ts +52 -15
  124. package/dist/save.d.ts.map +1 -1
  125. package/dist/save.js +508 -184
  126. package/dist/search.d.ts +12 -95
  127. package/dist/search.d.ts.map +1 -1
  128. package/dist/search.js +10 -266
  129. package/dist/shared.d.ts +4 -3
  130. package/dist/shared.d.ts.map +1 -1
  131. package/dist/shared.js +62 -35
  132. package/dist/status.d.ts +4 -3
  133. package/dist/status.d.ts.map +1 -1
  134. package/dist/status.js +3 -1
  135. package/dist/store.d.ts +85 -104
  136. package/dist/store.d.ts.map +1 -1
  137. package/dist/store.js +313 -692
  138. package/dist/stored.d.ts +138 -0
  139. package/dist/stored.d.ts.map +1 -0
  140. package/dist/stored.js +576 -0
  141. package/dist/tables.d.ts +326 -0
  142. package/dist/tables.d.ts.map +1 -0
  143. package/dist/tables.js +587 -0
  144. package/dist/transfer.d.ts +0 -5
  145. package/dist/transfer.d.ts.map +1 -1
  146. package/dist/turn.d.ts +2 -2
  147. package/dist/turn.d.ts.map +1 -1
  148. package/dist/turn.js +34 -23
  149. package/dist/validation.d.ts +29 -0
  150. package/dist/validation.d.ts.map +1 -0
  151. package/dist/validation.js +104 -0
  152. package/dist/windows.d.ts +1 -16
  153. package/dist/windows.d.ts.map +1 -1
  154. package/dist/windows.js +2 -79
  155. package/dist/wire.d.ts +26 -5
  156. package/dist/wire.d.ts.map +1 -1
  157. package/dist/wire.js +8 -2
  158. package/package.json +18 -19
  159. package/AGENTS.md +0 -299
  160. package/dist/chat/acts.d.ts +0 -33
  161. package/dist/chat/acts.d.ts.map +0 -1
  162. package/dist/chat/acts.js +0 -36
  163. package/dist/chat/context.d.ts +0 -3
  164. package/dist/chat/context.d.ts.map +0 -1
  165. package/dist/chat/earlier.d.ts +0 -10
  166. package/dist/chat/earlier.d.ts.map +0 -1
  167. package/dist/chat/earlier.js +0 -54
  168. package/dist/chat/input.d.ts +0 -9
  169. package/dist/chat/input.d.ts.map +0 -1
  170. package/dist/chat/input.js +0 -99
  171. package/dist/chat/kit.d.ts +0 -61
  172. package/dist/chat/kit.d.ts.map +0 -1
  173. package/dist/chat/kit.js +0 -123
  174. package/dist/chat/lasting.d.ts +0 -6
  175. package/dist/chat/lasting.d.ts.map +0 -1
  176. package/dist/chat/lasting.js +0 -19
  177. package/dist/chat/list.d.ts +0 -27
  178. package/dist/chat/list.d.ts.map +0 -1
  179. package/dist/chat/list.js +0 -99
  180. package/dist/chat/markdown.d.ts +0 -21
  181. package/dist/chat/markdown.d.ts.map +0 -1
  182. package/dist/chat/markdown.js +0 -246
  183. package/dist/chat/message.d.ts +0 -6
  184. package/dist/chat/message.d.ts.map +0 -1
  185. package/dist/chat/message.js +0 -192
  186. package/dist/chat/messages.d.ts +0 -22
  187. package/dist/chat/messages.d.ts.map +0 -1
  188. package/dist/chat/messages.js +0 -155
  189. package/dist/chat/name.d.ts +0 -6
  190. package/dist/chat/name.d.ts.map +0 -1
  191. package/dist/chat/name.js +0 -37
  192. package/dist/chat/opened.d.ts +0 -11
  193. package/dist/chat/opened.d.ts.map +0 -1
  194. package/dist/chat/opened.js +0 -17
  195. package/dist/chat/overlay.d.ts +0 -31
  196. package/dist/chat/overlay.d.ts.map +0 -1
  197. package/dist/chat/overlay.js +0 -97
  198. package/dist/chat/parts.d.ts +0 -57
  199. package/dist/chat/parts.d.ts.map +0 -1
  200. package/dist/chat/parts.js +0 -119
  201. package/dist/chat/pieces.d.ts +0 -51
  202. package/dist/chat/pieces.d.ts.map +0 -1
  203. package/dist/chat/pieces.js +0 -797
  204. package/dist/chat/process.d.ts +0 -20
  205. package/dist/chat/process.d.ts.map +0 -1
  206. package/dist/chat/process.js +0 -32
  207. package/dist/chat/provider.d.ts +0 -63
  208. package/dist/chat/provider.d.ts.map +0 -1
  209. package/dist/chat/provider.js +0 -628
  210. package/dist/chat/question.d.ts +0 -9
  211. package/dist/chat/question.d.ts.map +0 -1
  212. package/dist/chat/question.js +0 -69
  213. package/dist/chat/questions.d.ts +0 -31
  214. package/dist/chat/questions.d.ts.map +0 -1
  215. package/dist/chat/questions.js +0 -111
  216. package/dist/chat/read.d.ts +0 -5
  217. package/dist/chat/read.d.ts.map +0 -1
  218. package/dist/chat/read.js +0 -13
  219. package/dist/chat/ready.d.ts +0 -34
  220. package/dist/chat/ready.d.ts.map +0 -1
  221. package/dist/chat/ready.js +0 -91
  222. package/dist/chat/style.d.ts +0 -4
  223. package/dist/chat/style.d.ts.map +0 -1
  224. package/dist/chat/style.js +0 -302
  225. package/dist/chat.d.ts +0 -14
  226. package/dist/chat.d.ts.map +0 -1
  227. package/dist/chat.js +0 -8
  228. package/dist/read-side.d.ts +0 -27
  229. package/dist/read-side.d.ts.map +0 -1
  230. package/dist/read-side.js +0 -824
  231. package/dist/reads.d.ts +0 -11
  232. package/dist/reads.d.ts.map +0 -1
  233. package/dist/reads.js +0 -98
@@ -1,17 +1,32 @@
1
+ import { cursorOf, decodedCursor } from '@agentshouse/mdmodel';
1
2
  import { conversationChangeSet } from './change-set.js';
2
3
  import { runtimeError } from './failure.js';
3
- import { declaredFile } from './file.js';
4
+ import { refusedForSpace } from './durability.js';
4
5
  import { changeBatchIdentity, chatDraftRevision } from './identity.js';
5
6
  import { OFFLINE } from './outcome.js';
6
- import { capped, cursorOf, decodedCursor, FROM_START, messageOrder, precedes } from './windows.js';
7
+ import { untilAnswered } from './recovery.js';
8
+ import { capped, FROM_START, messageOrder, precedes } from './windows.js';
7
9
  const QUIET = {
8
10
  turn: null,
9
11
  questions: [],
10
12
  process: null,
11
13
  commands: [],
12
14
  options: [],
15
+ starting: null,
16
+ subagents: [],
17
+ link: null,
18
+ deliveryUnknown: [],
13
19
  };
14
- const FACTS = ['questions', 'process', 'commands', 'options'];
20
+ const FACTS = [
21
+ 'questions',
22
+ 'process',
23
+ 'commands',
24
+ 'options',
25
+ 'starting',
26
+ 'subagents',
27
+ 'link',
28
+ 'deliveryUnknown',
29
+ ];
15
30
  export function unheldView(conversation) {
16
31
  return {
17
32
  conversation,
@@ -19,13 +34,19 @@ export function unheldView(conversation) {
19
34
  turn: null,
20
35
  questions: [],
21
36
  process: null,
37
+ processEnded: null,
22
38
  commands: [],
23
39
  options: [],
40
+ starting: null,
41
+ subagents: [],
42
+ link: null,
43
+ deliveryUnknown: [],
24
44
  chatDraft: null,
25
45
  };
26
46
  }
27
47
  export const SESSION_STARTED = 'Session started';
28
48
  const NEW_CHAT = 'new:';
49
+ export const HISTORY_CONVERSATIONS = 64;
29
50
  export function newChat(agent) {
30
51
  return `${NEW_CHAT}${agent}`;
31
52
  }
@@ -192,9 +213,22 @@ function askedOf(options) {
192
213
  }
193
214
  return { limit: capped(limit), after, before: beforeOf(after), total: total === true };
194
215
  }
195
- const EXPIRED = { kind: 'rejected', code: 'expired', fields: {}, retryable: false };
216
+ function phraseOf(text) {
217
+ if (text === undefined)
218
+ return null;
219
+ if (typeof text !== 'string' || text === '') {
220
+ throw runtimeError('invalid-text', 'a conversation is found by a phrase');
221
+ }
222
+ return text;
223
+ }
224
+ const DURABLE_WRITE = {
225
+ kind: 'rejected',
226
+ code: 'durable-write',
227
+ fields: {},
228
+ retryable: true,
229
+ };
196
230
  function unwritten(draft) {
197
- return draft.text === '' && draft.files.length === 0;
231
+ return draft.text === '';
198
232
  }
199
233
  function pending(draft) {
200
234
  return {
@@ -210,10 +244,7 @@ function withSubmitted(draft, submitted) {
210
244
  return submitted.length === 0 ? unsent : { ...unsent, submitted };
211
245
  }
212
246
  function prepended(draft, said) {
213
- return {
214
- text: [said.text, draft.text].filter((one) => one !== '').join('\n'),
215
- files: [...said.files, ...draft.files],
216
- };
247
+ return { text: [said.text, draft.text].filter((one) => one !== '').join('\n') };
217
248
  }
218
249
  function returned(draft, submission, others) {
219
250
  return withSubmitted({
@@ -235,13 +266,19 @@ export function createConversationStore(deps) {
235
266
  const unsent = new Map();
236
267
  const views = new Map();
237
268
  const listing = new Map();
269
+ let relisting = null;
270
+ let listCursor = null;
238
271
  const creations = new Set();
239
272
  const listWatchers = new Set();
273
+ const unlisted = new Set();
240
274
  let listKnown = false;
275
+ let listHeld = false;
276
+ let listKept = false;
241
277
  const agentWatchers = new Set();
242
- let agentNames = new Map();
243
- let retiredAgents = new Set();
244
- let agentsRead = null;
278
+ const unnamed = new Set();
279
+ let agentsShown = null;
280
+ let listedAgents = null;
281
+ let agentsReading = false;
245
282
  let replicated = new Set();
246
283
  let handedReplicated;
247
284
  function liveOf(conversation) {
@@ -262,11 +299,16 @@ export function createConversationStore(deps) {
262
299
  turn: structuredClone(facts.turn),
263
300
  questions: structuredClone(facts.questions),
264
301
  process: facts.process === null ? null : { ...facts.process },
302
+ processEnded: listing.get(conversation)?.processEnded ?? null,
265
303
  commands: structuredClone(facts.commands),
266
304
  options: structuredClone(facts.options),
305
+ starting: facts.starting,
306
+ subagents: structuredClone(facts.subagents),
307
+ link: facts.link === null ? null : { ...facts.link },
308
+ deliveryUnknown: [...facts.deliveryUnknown],
267
309
  chatDraft: draft === undefined || unwritten(draft)
268
310
  ? null
269
- : { text: draft.text, files: [...draft.files], revision: draft.revision },
311
+ : { text: draft.text, revision: draft.revision },
270
312
  };
271
313
  }
272
314
  function emit(conversation) {
@@ -308,9 +350,6 @@ export function createConversationStore(deps) {
308
350
  }
309
351
  }
310
352
  }
311
- function changesOf(conversation, arriving) {
312
- return arriving.map(({ identity, text }) => ({ conversation, identity, text }));
313
- }
314
353
  function stateOf(conversation) {
315
354
  const facts = liveOf(conversation);
316
355
  if (facts.questions.length > 0)
@@ -320,34 +359,113 @@ export function createConversationStore(deps) {
320
359
  function entries() {
321
360
  return [...listing.values()].map((entry) => ({
322
361
  conversation: entry.conversation,
323
- agent: { identity: entry.agent, name: agentNames.get(entry.agent) ?? null },
362
+ agent: { identity: entry.agent, name: entry.agentName },
324
363
  name: entry.name,
325
364
  created: entry.created,
326
365
  finished: entry.finished,
327
366
  outcome: entry.outcome,
367
+ waiting: entry.waiting,
328
368
  state: stateOf(entry.conversation),
329
369
  started: lives.get(entry.conversation)?.turn?.started ?? null,
330
370
  }));
331
371
  }
372
+ function keepList() {
373
+ if (!listKnown || listKept)
374
+ return;
375
+ listKept = true;
376
+ queueMicrotask(() => {
377
+ listKept = false;
378
+ if (deps.closed())
379
+ return;
380
+ const entries = [...listing.values()];
381
+ const cursor = listCursor;
382
+ void deps
383
+ .durably(async (local) => {
384
+ await local.fact('conversationList', entries);
385
+ await local.fact('conversationCursor', cursor);
386
+ })
387
+ .catch(() => { });
388
+ });
389
+ }
390
+ function listed(entry) {
391
+ const ended = listing.get(entry.conversation)?.processEnded ?? null;
392
+ listing.set(entry.conversation, { ...entry });
393
+ creations.delete(entry.conversation);
394
+ if (entry.processEnded !== ended)
395
+ emit(entry.conversation);
396
+ }
332
397
  function emitList() {
333
- if (deps.closed() || listWatchers.size === 0)
398
+ if (deps.closed())
334
399
  return;
335
- const value = entries();
336
- for (const watcher of listWatchers)
337
- watcher(value);
400
+ if (listWatchers.size > 0) {
401
+ const value = entries();
402
+ for (const watcher of listWatchers)
403
+ watcher(value);
404
+ }
405
+ emitAgents();
406
+ learned();
407
+ }
408
+ function learned() {
409
+ if (deps.closed())
410
+ return;
411
+ if (!listKnown && !listHeld && deps.stream() !== undefined && deps.online())
412
+ return;
413
+ if (unlisted.size > 0) {
414
+ const value = entries();
415
+ for (const watcher of [...unlisted]) {
416
+ unlisted.delete(watcher);
417
+ listWatchers.add(watcher);
418
+ watcher(value);
419
+ }
420
+ }
421
+ if (unnamed.size > 0) {
422
+ const value = agents();
423
+ for (const watcher of [...unnamed]) {
424
+ unnamed.delete(watcher);
425
+ agentWatchers.add(watcher);
426
+ watcher(value);
427
+ }
428
+ }
338
429
  }
339
430
  function agents() {
340
- return [...agentNames]
341
- .filter(([identity]) => !retiredAgents.has(identity))
342
- .map(([identity, name]) => ({ identity, name }))
431
+ const named = new Map();
432
+ for (const agent of listedAgents ?? [])
433
+ named.set(agent.agent, agent);
434
+ for (const entry of listing.values()) {
435
+ named.set(entry.agent, { name: entry.agentName, retired: entry.agentRetired });
436
+ }
437
+ return [...named]
438
+ .filter(([, agent]) => !agent.retired)
439
+ .map(([identity, agent]) => ({ identity, name: agent.name }))
343
440
  .sort((left, right) => left.name === right.name
344
441
  ? left.identity.localeCompare(right.identity)
345
442
  : left.name.localeCompare(right.name));
346
443
  }
444
+ function readAgents() {
445
+ const stream = deps.stream();
446
+ if (stream?.agents === undefined || !deps.online() || agentsReading)
447
+ return;
448
+ agentsReading = true;
449
+ void stream
450
+ .agents()
451
+ .then((read) => {
452
+ if (read === null)
453
+ return;
454
+ listedAgents = read;
455
+ emitAgents();
456
+ }, () => { })
457
+ .finally(() => {
458
+ agentsReading = false;
459
+ });
460
+ }
347
461
  function emitAgents() {
348
462
  if (deps.closed() || agentWatchers.size === 0)
349
463
  return;
350
464
  const value = agents();
465
+ const shown = JSON.stringify(value);
466
+ if (shown === agentsShown)
467
+ return;
468
+ agentsShown = shown;
351
469
  for (const watcher of agentWatchers)
352
470
  watcher(value);
353
471
  }
@@ -363,48 +481,11 @@ export function createConversationStore(deps) {
363
481
  if (stateOf(conversation) !== before || next?.turn?.started !== began)
364
482
  emitList();
365
483
  }
366
- async function readAgents() {
367
- const declaration = deps.agents();
368
- if (declaration === undefined)
369
- return;
370
- const held = await deps.documents(declaration);
371
- if ('pending' in held)
372
- return;
373
- const { documents } = held;
374
- const names = new Map();
375
- const retired = new Set();
376
- for (const document of documents) {
377
- if (document.type !== declaration.type)
378
- continue;
379
- const identity = document.fields?.[declaration.identity];
380
- const name = document.fields?.[declaration.name];
381
- if (typeof identity !== 'string' || typeof name !== 'string')
382
- continue;
383
- names.set(identity, name);
384
- const ended = declaration.retired === undefined ? null : document.fields?.[declaration.retired];
385
- if (ended !== undefined && ended !== null)
386
- retired.add(identity);
387
- }
388
- const named = JSON.stringify([...names]) !== JSON.stringify([...agentNames]);
389
- if (!named && JSON.stringify([...retired]) === JSON.stringify([...retiredAgents]))
390
- return;
391
- agentNames = names;
392
- retiredAgents = retired;
393
- if (named)
394
- emitList();
395
- emitAgents();
396
- const ended = [...drafts.keys()].filter(retiredChat);
397
- if (ended.length > 0)
398
- await remove(ended);
399
- }
400
484
  function retiredChat(conversation) {
401
- return (conversation.startsWith(NEW_CHAT) && retiredAgents.has(conversation.slice(NEW_CHAT.length)));
402
- }
403
- function agentsReady() {
404
- agentsRead ??= readAgents().catch(() => {
405
- agentsRead = null;
406
- });
407
- return agentsRead;
485
+ if (!conversation.startsWith(NEW_CHAT))
486
+ return false;
487
+ const agent = conversation.slice(NEW_CHAT.length);
488
+ return [...listing.values()].some((entry) => entry.agent === agent && entry.agentRetired);
408
489
  }
409
490
  function live(conversation, change) {
410
491
  const owed = unsent.get(conversation);
@@ -426,6 +507,12 @@ export function createConversationStore(deps) {
426
507
  process: facts.has('process') ? QUIET.process : held.process,
427
508
  commands: facts.has('commands') ? QUIET.commands : held.commands,
428
509
  options: facts.has('options') ? QUIET.options : held.options,
510
+ starting: facts.has('starting') ? QUIET.starting : held.starting,
511
+ subagents: facts.has('subagents') ? QUIET.subagents : held.subagents,
512
+ link: facts.has('link') ? QUIET.link : held.link,
513
+ deliveryUnknown: facts.has('deliveryUnknown')
514
+ ? QUIET.deliveryUnknown
515
+ : held.deliveryUnknown,
429
516
  });
430
517
  emit(conversation);
431
518
  }
@@ -456,12 +543,6 @@ export function createConversationStore(deps) {
456
543
  }
457
544
  async function remove(leaving) {
458
545
  await deps.durably(async (local) => {
459
- const removed = [];
460
- for (const conversation of leaving) {
461
- for (const identity of await local.messageIdentities(conversation)) {
462
- removed.push({ conversation, identity, text: null });
463
- }
464
- }
465
546
  await local.dropConversations(leaving);
466
547
  const changed = [];
467
548
  for (const conversation of leaving) {
@@ -478,9 +559,23 @@ export function createConversationStore(deps) {
478
559
  await slide(local, conversation, null);
479
560
  }
480
561
  if (changed.length > 0)
481
- deps.publish(conversationChangeSet(removed, changed));
562
+ deps.publish(conversationChangeSet(changed));
482
563
  });
483
564
  }
565
+ function missed(conversation) {
566
+ const read = reads.get(conversation);
567
+ const newest = listing.get(conversation)?.newest;
568
+ return (read === undefined ||
569
+ newest === undefined ||
570
+ partials.has(conversation) ||
571
+ (newest !== null && (read === null || newest > read)));
572
+ }
573
+ function noted(conversation, message) {
574
+ const entry = listing.get(conversation);
575
+ if (entry === undefined || (entry.newest !== null && entry.newest >= message.position))
576
+ return;
577
+ listing.set(conversation, { ...entry, newest: message.position });
578
+ }
484
579
  function gap(conversation) {
485
580
  gaps.set(conversation, (gaps.get(conversation) ?? 0) + 1);
486
581
  behind.add(conversation);
@@ -504,17 +599,18 @@ export function createConversationStore(deps) {
504
599
  reads.set(conversation, message.position);
505
600
  sealTurns(conversation, new Set([message.turn]));
506
601
  await slide(local, conversation, [message]);
507
- deps.publish(conversationChangeSet(changesOf(conversation, [message]), [conversation]));
602
+ deps.publish(conversationChangeSet([conversation]));
508
603
  });
509
604
  }
510
605
  async function readHistories(conversations, ended) {
511
606
  const generations = new Map(conversations.map((one) => [one, gaps.get(one) ?? 0]));
512
- let asking = conversations.map((conversation) => ({
607
+ let waiting = conversations.map((conversation) => ({
513
608
  conversation,
514
609
  after: reads.get(conversation) ?? undefined,
515
610
  }));
516
- while (asking.length > 0) {
517
- const result = await deps.call('conversation.history', {
611
+ while (waiting.length > 0) {
612
+ const asking = waiting.slice(0, HISTORY_CONVERSATIONS);
613
+ const result = await deps.history({
518
614
  conversations: asking.map(({ conversation, after }) => ({
519
615
  conversation,
520
616
  ...(after === undefined ? {} : { after }),
@@ -537,7 +633,7 @@ export function createConversationStore(deps) {
537
633
  if (!following.some((next) => next.conversation === conversation))
538
634
  ended(conversation);
539
635
  }
540
- asking = following;
636
+ waiting = [...waiting.slice(HISTORY_CONVERSATIONS), ...following];
541
637
  }
542
638
  return null;
543
639
  }
@@ -580,7 +676,7 @@ export function createConversationStore(deps) {
580
676
  behind.delete(conversation);
581
677
  await slide(local, conversation, arriving);
582
678
  if (arriving.length > 0 || sealing || whole) {
583
- deps.publish(conversationChangeSet(changesOf(conversation, arriving), [conversation]));
679
+ deps.publish(conversationChangeSet([conversation]));
584
680
  }
585
681
  });
586
682
  }
@@ -613,11 +709,35 @@ export function createConversationStore(deps) {
613
709
  live(conversation, { options: structuredClone(event.options) });
614
710
  return;
615
711
  }
712
+ if (event.kind === 'starting') {
713
+ live(conversation, { starting: event.step });
714
+ return;
715
+ }
716
+ if (event.kind === 'subagents') {
717
+ live(conversation, { subagents: structuredClone(event.subagents) });
718
+ return;
719
+ }
720
+ if (event.kind === 'link') {
721
+ live(conversation, { link: { connected: event.connected } });
722
+ return;
723
+ }
724
+ if (event.kind === 'deliveries') {
725
+ live(conversation, { deliveryUnknown: [...event.unknown] });
726
+ return;
727
+ }
616
728
  const arriving = acceptedMessage(event.message);
617
729
  if (arriving === null)
618
730
  return;
731
+ noted(conversation, arriving);
619
732
  if (replicated.has(conversation)) {
620
- void keep(conversation, arriving).catch(() => { });
733
+ if (deps.full()) {
734
+ gap(conversation);
735
+ return;
736
+ }
737
+ void keep(conversation, arriving).catch((error) => {
738
+ if (refusedForSpace(error))
739
+ gap(conversation);
740
+ });
621
741
  return;
622
742
  }
623
743
  if (sealTurns(conversation, new Set([arriving.turn])))
@@ -646,13 +766,12 @@ export function createConversationStore(deps) {
646
766
  const from = newChat(agent);
647
767
  return deps.durably(async (local) => {
648
768
  const held = drafts.get(from);
649
- const earlier = (held?.moves ?? []).filter((one) => !deps.expired(one));
769
+ const earlier = held?.moves ?? [];
650
770
  if (earlier.includes(operation))
651
771
  return;
652
772
  const blank = {
653
773
  conversation: from,
654
774
  text: '',
655
- files: [],
656
775
  revision: chatDraftRevision(),
657
776
  };
658
777
  const { moves: _answered, ...unsent } = held ?? blank;
@@ -662,7 +781,7 @@ export function createConversationStore(deps) {
662
781
  drafts.set(from, left);
663
782
  for (const one of taken)
664
783
  drafts.set(to, one);
665
- deps.publish(conversationChangeSet([], [from, to]));
784
+ deps.publish(conversationChangeSet([from, to]));
666
785
  });
667
786
  }
668
787
  function withDraft(conversation, next) {
@@ -671,59 +790,43 @@ export function createConversationStore(deps) {
671
790
  if (following === undefined)
672
791
  return;
673
792
  await kept(local, conversation, following);
674
- deps.publish(conversationChangeSet([], [conversation]));
793
+ deps.publish(conversationChangeSet([conversation]));
675
794
  });
676
795
  }
677
796
  async function submitted(conversation, operation, draft) {
678
797
  let submission;
679
- let earlier = false;
680
798
  await withDraft(conversation, (current) => {
681
799
  submission = current?.submitted?.find((one) => one.operation === operation);
682
- earlier = submission !== undefined;
683
800
  if (submission !== undefined)
684
801
  return undefined;
685
802
  const said = draft ?? current;
686
- submission = {
687
- operation,
688
- text: said?.text ?? '',
689
- files: [...(said?.files ?? [])],
690
- turn: null,
691
- };
803
+ submission = { operation, text: said?.text ?? '', turn: null };
692
804
  return {
693
805
  conversation,
694
806
  ...(draft === undefined || current === undefined
695
- ? { text: '', files: [], revision: chatDraftRevision() }
696
- : { text: current.text, files: current.files, revision: current.revision }),
807
+ ? { text: '', revision: chatDraftRevision() }
808
+ : { text: current.text, revision: current.revision }),
697
809
  submitted: [...(current?.submitted ?? []), submission],
698
810
  };
699
811
  });
700
- return { submission: submission, earlier };
701
- }
702
- function forgotten(conversation, submission) {
703
- return withDraft(conversation, (current) => {
704
- const held = current?.submitted ?? [];
705
- if (current === undefined || !held.includes(submission))
706
- return undefined;
707
- return withSubmitted(current, held.filter((one) => one !== submission));
708
- });
812
+ return submission;
709
813
  }
710
814
  async function dispatched(conversation, submission) {
711
- const dispatch = {};
815
+ const dispatch = { answered: false };
712
816
  sending.set(submission.operation, dispatch);
713
817
  try {
714
- const files = await Promise.all(submission.files.map(async (file) => ({ ...(await declaredFile(file)), blob: file.blob })));
715
- const result = await invoke('conversation.send', conversation, {
716
- text: submission.text,
717
- files,
818
+ const result = await invoke('conversations/send', conversation, {
718
819
  operation_id: submission.operation,
820
+ text: submission.text,
719
821
  });
822
+ dispatch.answered = result.kind !== 'transport-uncertainty';
720
823
  if (result.kind === 'revoked-authorization')
721
824
  return result;
722
825
  if (result.kind !== 'action' && result.retryable) {
723
826
  deps.unanswered();
724
827
  return result;
725
828
  }
726
- const turn = answered(result, 'turn');
829
+ const turn = answered(result, 'message_id');
727
830
  await withDraft(conversation, async (current, local) => {
728
831
  const held = current?.submitted?.find((one) => one.operation === submission.operation);
729
832
  if (current === undefined || held === undefined || held.turn !== null)
@@ -743,25 +846,34 @@ export function createConversationStore(deps) {
743
846
  sending.delete(submission.operation);
744
847
  }
745
848
  }
746
- function invoke(name, conversation, extra = {}) {
747
- return deps.call(name, { conversation, ...extra });
849
+ function invoke(name, conversation, extra = { operation_id: changeBatchIdentity() }) {
850
+ return deps.call(name, { conversation_id: conversation, ...extra });
748
851
  }
749
852
  const surface = {
750
- async list() {
751
- await agentsReady();
752
- return entries();
853
+ list() {
854
+ return new Promise((settle) => {
855
+ const answer = (listed) => {
856
+ unlisted.delete(answer);
857
+ listWatchers.delete(answer);
858
+ settle(listed);
859
+ };
860
+ unlisted.add(answer);
861
+ learned();
862
+ });
753
863
  },
754
864
  async read(conversation, options = {}) {
755
- const asked = askedOf(options);
865
+ const { text, ...windowed } = options;
866
+ const asked = askedOf(windowed);
867
+ const phrase = phraseOf(text);
756
868
  return deps.durably(async (local) => {
757
- const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before);
758
- const total = asked.total ? await local.messageCount(conversation) : null;
869
+ const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before, phrase);
870
+ const total = asked.total ? await local.messageCount(conversation, phrase) : null;
759
871
  return structuredClone(viewOf(conversation, messages, frameOf(asked, messages, more, total)));
760
872
  });
761
873
  },
762
874
  async create(input) {
763
- const result = await deps.call('conversation.create', input);
764
- const conversation = answered(result, 'conversation');
875
+ const result = await deps.call('conversations/create', input);
876
+ const conversation = answered(result, 'conversation_id');
765
877
  if (conversation === null)
766
878
  return result;
767
879
  if (!listing.has(conversation))
@@ -772,43 +884,39 @@ export function createConversationStore(deps) {
772
884
  return result;
773
885
  },
774
886
  async send(conversation, operation = changeBatchIdentity(), draft) {
887
+ if (deps.full())
888
+ return DURABLE_WRITE;
775
889
  if (deps.online()) {
776
- const { submission, earlier } = await submitted(conversation, operation, draft);
777
- if (!earlier || !deps.expired(operation))
778
- return dispatched(conversation, submission);
779
- await forgotten(conversation, submission);
780
- return EXPIRED;
890
+ const held = await submitted(conversation, operation, draft).catch((error) => {
891
+ if (refusedForSpace(error))
892
+ return null;
893
+ throw error;
894
+ });
895
+ if (held === null)
896
+ return DURABLE_WRITE;
897
+ return dispatched(conversation, held);
781
898
  }
782
899
  if (draft !== undefined) {
783
900
  await withDraft(conversation, (current) => current?.submitted?.some((one) => one.operation === operation) === true
784
901
  ? undefined
785
- : returned(current ?? { conversation, text: '', files: [], revision: '' }, { operation, ...draft, turn: null }, current?.submitted ?? []));
902
+ : returned(current ?? { conversation, text: '', revision: '' }, { operation, ...draft, turn: null }, current?.submitted ?? []));
786
903
  }
787
904
  return OFFLINE;
788
905
  },
789
906
  answer(conversation, answer) {
790
- return invoke('conversation.answer', conversation, { answer });
907
+ return invoke('conversations/interaction/answer', conversation, {
908
+ operation_id: changeBatchIdentity(),
909
+ ...answer,
910
+ });
791
911
  },
792
912
  interrupt(conversation) {
793
- return invoke('conversation.interrupt', conversation);
913
+ return invoke('conversations/interrupt', conversation);
794
914
  },
795
915
  kill(conversation) {
796
- return invoke('conversation.kill', conversation);
797
- },
798
- async cancel(conversation, turn) {
799
- const result = await invoke('conversation.cancel', conversation, { turn });
800
- if (result.kind !== 'action')
801
- return result;
802
- await withDraft(conversation, (current) => {
803
- const held = current?.submitted?.find((one) => one.turn === turn);
804
- if (current === undefined || held === undefined)
805
- return undefined;
806
- return returned(current, held, current.submitted.filter((one) => one !== held));
807
- });
808
- return result;
916
+ return invoke('conversations/kill', conversation);
809
917
  },
810
918
  delete(conversation) {
811
- return invoke('conversation.delete', conversation);
919
+ return invoke('conversations/delete', conversation);
812
920
  },
813
921
  };
814
922
  const chatDraft = {
@@ -816,7 +924,6 @@ export function createConversationStore(deps) {
816
924
  return withDraft(conversation, (current) => ({
817
925
  conversation,
818
926
  text: draft.text,
819
- files: [...draft.files],
820
927
  revision,
821
928
  ...(current === undefined ? {} : pending(current)),
822
929
  }));
@@ -824,9 +931,7 @@ export function createConversationStore(deps) {
824
931
  prepend(conversation, draft) {
825
932
  return withDraft(conversation, (current) => ({
826
933
  conversation,
827
- ...(current === undefined
828
- ? { text: draft.text, files: [...draft.files] }
829
- : prepended(current, draft)),
934
+ ...(current === undefined ? { text: draft.text } : prepended(current, draft)),
830
935
  revision: chatDraftRevision(),
831
936
  ...(current === undefined ? {} : pending(current)),
832
937
  }));
@@ -834,19 +939,21 @@ export function createConversationStore(deps) {
834
939
  discard(conversation) {
835
940
  return withDraft(conversation, (current) => current === undefined
836
941
  ? undefined
837
- : {
838
- conversation,
839
- text: '',
840
- files: [],
841
- revision: chatDraftRevision(),
842
- ...pending(current),
843
- });
942
+ : { conversation, text: '', revision: chatDraftRevision(), ...pending(current) });
844
943
  },
845
944
  };
846
945
  return {
847
946
  surface,
848
947
  chatDraft,
849
- hydrate(held, kept) {
948
+ hydrate(held, kept, list, cursor) {
949
+ if (list !== null && !listKnown) {
950
+ listing.clear();
951
+ for (const entry of list)
952
+ listing.set(entry.conversation, { ...entry });
953
+ listHeld = true;
954
+ listCursor = cursor;
955
+ deps.stream()?.hold(cursor);
956
+ }
850
957
  stored.clear();
851
958
  reads.clear();
852
959
  partials.clear();
@@ -860,6 +967,8 @@ export function createConversationStore(deps) {
860
967
  drafts.clear();
861
968
  for (const record of kept)
862
969
  drafts.set(record.conversation, record);
970
+ if (listHeld)
971
+ learned();
863
972
  },
864
973
  async reconcile() {
865
974
  if (!listKnown)
@@ -871,7 +980,7 @@ export function createConversationStore(deps) {
871
980
  !creations.has(conversation) &&
872
981
  (!conversation.startsWith(NEW_CHAT) || retiredChat(conversation)));
873
982
  for (const conversation of named) {
874
- if (!replicated.has(conversation))
983
+ if (!replicated.has(conversation) && missed(conversation))
875
984
  behind.add(conversation);
876
985
  }
877
986
  replicated = named;
@@ -880,6 +989,11 @@ export function createConversationStore(deps) {
880
989
  await remove(leaving);
881
990
  },
882
991
  async catchUp(progress) {
992
+ if (deps.full()) {
993
+ if (behind.size > 0)
994
+ progress.complete = false;
995
+ return;
996
+ }
883
997
  const owed = (conversation) => behind.has(conversation) && replicated.has(conversation);
884
998
  const read = async (conversations) => {
885
999
  if (conversations.length === 0 || deps.closed())
@@ -904,10 +1018,6 @@ export function createConversationStore(deps) {
904
1018
  continue;
905
1019
  if (deps.closed())
906
1020
  return;
907
- if (deps.expired(submission.operation)) {
908
- await forgotten(conversation, submission);
909
- continue;
910
- }
911
1021
  const result = await dispatched(conversation, submission);
912
1022
  if (result.kind === 'revoked-authorization')
913
1023
  return;
@@ -928,12 +1038,11 @@ export function createConversationStore(deps) {
928
1038
  known() {
929
1039
  return listKnown;
930
1040
  },
931
- reconnected() {
932
- for (const conversation of replicated)
933
- gap(conversation);
934
- },
935
1041
  reopened() {
936
- sending.clear();
1042
+ relisting = null;
1043
+ for (const [operation, dispatch] of sending)
1044
+ if (!dispatch.answered)
1045
+ sending.delete(operation);
937
1046
  },
938
1047
  lost() {
939
1048
  catching.clear();
@@ -952,15 +1061,15 @@ export function createConversationStore(deps) {
952
1061
  views.set(key, view);
953
1062
  const watcher = { notify: deliver, ready: false };
954
1063
  view.watchers.add(watcher);
955
- void deps
956
- .durably(async (local) => {
1064
+ const reading = new AbortController();
1065
+ void untilAnswered(() => deps.durably(async (local) => {
957
1066
  if (view.window !== null)
958
1067
  return;
959
1068
  const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before);
960
1069
  view.window = messages;
961
1070
  view.more = more;
962
1071
  view.total = asked.total ? await local.messageCount(conversation) : null;
963
- })
1072
+ }), reading.signal)
964
1073
  .then(() => {
965
1074
  if (deps.closed() || !view.watchers.has(watcher))
966
1075
  return;
@@ -970,6 +1079,7 @@ export function createConversationStore(deps) {
970
1079
  })
971
1080
  .catch(() => { });
972
1081
  return () => {
1082
+ reading.abort();
973
1083
  if (!view.watchers.delete(watcher))
974
1084
  return;
975
1085
  if (view.watchers.size === 0)
@@ -977,77 +1087,88 @@ export function createConversationStore(deps) {
977
1087
  };
978
1088
  },
979
1089
  watchList(deliver) {
980
- listWatchers.add(deliver);
981
- void agentsReady().then(() => {
982
- if (!deps.closed() && listWatchers.has(deliver))
983
- deliver(entries());
984
- });
1090
+ unlisted.add(deliver);
1091
+ learned();
985
1092
  return () => {
1093
+ unlisted.delete(deliver);
986
1094
  listWatchers.delete(deliver);
987
1095
  };
988
1096
  },
989
1097
  watchAgents(deliver) {
990
- agentWatchers.add(deliver);
991
- void agentsReady().then(() => {
992
- if (!deps.closed() && agentWatchers.has(deliver))
993
- deliver(agents());
994
- });
1098
+ unnamed.add(deliver);
1099
+ learned();
1100
+ readAgents();
995
1101
  return () => {
1102
+ unnamed.delete(deliver);
996
1103
  agentWatchers.delete(deliver);
997
1104
  };
998
1105
  },
1106
+ learned,
999
1107
  changed(set) {
1000
1108
  for (const conversation of set.conversations)
1001
1109
  emit(conversation);
1002
- const declaration = deps.agents();
1003
- if (declaration !== undefined &&
1004
- set.documents.some((change) => change.locator === declaration.locator)) {
1005
- agentsRead = readAgents().catch(() => {
1006
- agentsRead = null;
1007
- });
1008
- }
1009
1110
  },
1010
1111
  receive(event) {
1011
1112
  if (deps.closed())
1012
1113
  return false;
1013
1114
  if (event.kind === 'list' || event.kind === 'listed' || event.kind === 'unlisted') {
1014
1115
  if (event.kind === 'list') {
1015
- listing.clear();
1116
+ if (event.whole)
1117
+ relisting ??= new Map();
1016
1118
  for (const entry of event.entries) {
1017
- listing.set(entry.conversation, { ...entry });
1018
- creations.delete(entry.conversation);
1119
+ if (relisting === null)
1120
+ listed(entry);
1121
+ else
1122
+ relisting.set(entry.conversation, { ...entry });
1123
+ }
1124
+ if (event.more)
1125
+ return false;
1126
+ if (relisting !== null) {
1127
+ listing.clear();
1128
+ for (const entry of relisting.values())
1129
+ listed(entry);
1130
+ relisting = null;
1019
1131
  }
1132
+ listCursor = event.cursor;
1133
+ deps.stream()?.hold(event.cursor);
1020
1134
  listKnown = true;
1135
+ for (const conversation of replicated) {
1136
+ if (!listing.has(conversation))
1137
+ continue;
1138
+ if (!missed(conversation))
1139
+ behind.delete(conversation);
1140
+ else if (!behind.has(conversation))
1141
+ gap(conversation);
1142
+ }
1021
1143
  openingEnded();
1022
1144
  }
1023
1145
  else if (event.kind === 'listed') {
1024
- listing.set(event.entry.conversation, { ...event.entry });
1025
- creations.delete(event.entry.conversation);
1146
+ listed(event.entry);
1147
+ relisting?.set(event.entry.conversation, { ...event.entry });
1026
1148
  }
1027
1149
  else {
1028
1150
  listing.delete(event.conversation);
1151
+ relisting?.delete(event.conversation);
1029
1152
  }
1153
+ keepList();
1030
1154
  emitList();
1031
- return (event.kind !== 'list' &&
1155
+ return ((event.kind !== 'list' &&
1032
1156
  listKnown &&
1033
1157
  (listing.size !== replicated.size ||
1034
- [...listing.keys()].some((conversation) => !replicated.has(conversation))));
1158
+ [...listing.keys()].some((conversation) => !replicated.has(conversation)))) ||
1159
+ (listKnown && [...drafts.keys()].some(retiredChat)));
1035
1160
  }
1036
1161
  heard(event);
1037
1162
  return false;
1038
1163
  },
1039
- searchable() {
1040
- return [...(listKnown ? listing.keys() : stored)].sort();
1041
- },
1042
- acquired(conversation) {
1043
- return reads.has(conversation) && !partials.has(conversation);
1044
- },
1045
1164
  close() {
1046
1165
  views.clear();
1047
1166
  lives.clear();
1048
1167
  unsent.clear();
1049
1168
  listWatchers.clear();
1169
+ unlisted.clear();
1050
1170
  agentWatchers.clear();
1171
+ unnamed.clear();
1051
1172
  },
1052
1173
  };
1053
1174
  }