@agentshouse/mdruntime 0.1.0-cc5745cd9af96 → 0.1.0-cc8c6d652ec3b

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