@agentshouse/mdruntime 0.1.0-c1db74d14e5fa → 0.1.0-c24132a516ded

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 (184) hide show
  1. package/AGENTS.md +246 -242
  2. package/README.md +9 -8
  3. package/dist/adapter.d.ts +53 -23
  4. package/dist/adapter.d.ts.map +1 -1
  5. package/dist/attached-model.d.ts +4 -1
  6. package/dist/attached-model.d.ts.map +1 -1
  7. package/dist/attached-model.js +13 -3
  8. package/dist/bootstrap.d.ts +2 -1
  9. package/dist/bootstrap.d.ts.map +1 -1
  10. package/dist/bootstrap.js +62 -27
  11. package/dist/change-set.d.ts +5 -2
  12. package/dist/change-set.d.ts.map +1 -1
  13. package/dist/change-set.js +10 -8
  14. package/dist/change.d.ts +31 -1
  15. package/dist/change.d.ts.map +1 -1
  16. package/dist/change.js +10 -1
  17. package/dist/chat/earlier.d.ts +10 -0
  18. package/dist/chat/earlier.d.ts.map +1 -0
  19. package/dist/chat/earlier.js +54 -0
  20. package/dist/chat/house.d.ts +25 -0
  21. package/dist/chat/house.d.ts.map +1 -0
  22. package/dist/chat/house.js +38 -0
  23. package/dist/chat/input.d.ts +3 -1
  24. package/dist/chat/input.d.ts.map +1 -1
  25. package/dist/chat/input.js +11 -18
  26. package/dist/chat/kit.d.ts +2 -1
  27. package/dist/chat/kit.d.ts.map +1 -1
  28. package/dist/chat/kit.js +3 -3
  29. package/dist/chat/list.d.ts +3 -1
  30. package/dist/chat/list.d.ts.map +1 -1
  31. package/dist/chat/list.js +47 -6
  32. package/dist/chat/markdown.d.ts +1 -0
  33. package/dist/chat/markdown.d.ts.map +1 -1
  34. package/dist/chat/markdown.js +78 -20
  35. package/dist/chat/message.d.ts +1 -0
  36. package/dist/chat/message.d.ts.map +1 -1
  37. package/dist/chat/message.js +55 -10
  38. package/dist/chat/messages.d.ts +9 -1
  39. package/dist/chat/messages.d.ts.map +1 -1
  40. package/dist/chat/messages.js +67 -28
  41. package/dist/chat/name.d.ts +1 -0
  42. package/dist/chat/name.d.ts.map +1 -1
  43. package/dist/chat/name.js +1 -1
  44. package/dist/chat/opened.d.ts +1 -0
  45. package/dist/chat/opened.d.ts.map +1 -1
  46. package/dist/chat/opened.js +2 -0
  47. package/dist/chat/overlay.d.ts +1 -0
  48. package/dist/chat/overlay.d.ts.map +1 -1
  49. package/dist/chat/overlay.js +11 -5
  50. package/dist/chat/parts.d.ts +1 -0
  51. package/dist/chat/parts.d.ts.map +1 -1
  52. package/dist/chat/parts.js +3 -2
  53. package/dist/chat/pieces.d.ts +62 -0
  54. package/dist/chat/pieces.d.ts.map +1 -0
  55. package/dist/chat/pieces.js +1110 -0
  56. package/dist/chat/provider.d.ts +7 -2
  57. package/dist/chat/provider.d.ts.map +1 -1
  58. package/dist/chat/provider.js +104 -105
  59. package/dist/chat/question.d.ts +1 -1
  60. package/dist/chat/question.d.ts.map +1 -1
  61. package/dist/chat/question.js +7 -18
  62. package/dist/chat/ready.d.ts +5 -1
  63. package/dist/chat/ready.d.ts.map +1 -1
  64. package/dist/chat/ready.js +7 -5
  65. package/dist/chat/room-file.d.ts +8 -0
  66. package/dist/chat/room-file.d.ts.map +1 -0
  67. package/dist/chat/room-file.js +67 -0
  68. package/dist/chat/steady.d.ts +3 -0
  69. package/dist/chat/steady.d.ts.map +1 -0
  70. package/dist/chat/steady.js +26 -0
  71. package/dist/chat/style.d.ts.map +1 -1
  72. package/dist/chat/style.js +5 -1
  73. package/dist/chat.d.ts +3 -2
  74. package/dist/chat.d.ts.map +1 -1
  75. package/dist/chat.js +2 -2
  76. package/dist/client.d.ts +11 -3
  77. package/dist/client.d.ts.map +1 -1
  78. package/dist/client.js +19 -5
  79. package/dist/collection.d.ts +6 -5
  80. package/dist/collection.d.ts.map +1 -1
  81. package/dist/collection.js +166 -85
  82. package/dist/connection.d.ts +17 -5
  83. package/dist/connection.d.ts.map +1 -1
  84. package/dist/connection.js +1212 -368
  85. package/dist/conversation.d.ts +15 -2
  86. package/dist/conversation.d.ts.map +1 -1
  87. package/dist/conversation.js +278 -74
  88. package/dist/digest.d.ts +2 -0
  89. package/dist/digest.d.ts.map +1 -0
  90. package/dist/digest.js +100 -0
  91. package/dist/durability.d.ts +1 -1
  92. package/dist/entry.d.ts +2 -1
  93. package/dist/entry.d.ts.map +1 -1
  94. package/dist/entry.js +5 -4
  95. package/dist/file.d.ts +2 -10
  96. package/dist/file.d.ts.map +1 -1
  97. package/dist/file.js +3 -120
  98. package/dist/host.d.ts +9 -7
  99. package/dist/host.d.ts.map +1 -1
  100. package/dist/host.js +67 -14
  101. package/dist/index.d.ts +11 -7
  102. package/dist/index.d.ts.map +1 -1
  103. package/dist/index.js +4 -0
  104. package/dist/memory.d.ts +7 -3
  105. package/dist/memory.d.ts.map +1 -1
  106. package/dist/memory.js +33 -16
  107. package/dist/model.d.ts +3 -2
  108. package/dist/model.d.ts.map +1 -1
  109. package/dist/model.js +1 -1
  110. package/dist/name.d.ts +4 -0
  111. package/dist/name.d.ts.map +1 -0
  112. package/dist/name.js +122 -0
  113. package/dist/observation.d.ts +2 -0
  114. package/dist/observation.d.ts.map +1 -1
  115. package/dist/observation.js +9 -3
  116. package/dist/outcome.d.ts +27 -2
  117. package/dist/outcome.d.ts.map +1 -1
  118. package/dist/outcome.js +25 -0
  119. package/dist/overlay.d.ts +6 -5
  120. package/dist/overlay.d.ts.map +1 -1
  121. package/dist/overlay.js +223 -134
  122. package/dist/query.d.ts +30 -2
  123. package/dist/query.d.ts.map +1 -1
  124. package/dist/query.js +37 -7
  125. package/dist/reach.d.ts +6 -3
  126. package/dist/reach.d.ts.map +1 -1
  127. package/dist/reach.js +156 -38
  128. package/dist/react.d.ts +3 -3
  129. package/dist/react.d.ts.map +1 -1
  130. package/dist/react.js +7 -17
  131. package/dist/read-side.d.ts +6 -4
  132. package/dist/read-side.d.ts.map +1 -1
  133. package/dist/read-side.js +755 -215
  134. package/dist/readiness.d.ts +1 -1
  135. package/dist/readiness.d.ts.map +1 -1
  136. package/dist/reads.d.ts +11 -0
  137. package/dist/reads.d.ts.map +1 -0
  138. package/dist/reads.js +98 -0
  139. package/dist/recovery.d.ts +2 -2
  140. package/dist/recovery.d.ts.map +1 -1
  141. package/dist/recovery.js +8 -7
  142. package/dist/refresh.d.ts +4 -4
  143. package/dist/refresh.d.ts.map +1 -1
  144. package/dist/refresh.js +41 -16
  145. package/dist/replica.d.ts +12 -7
  146. package/dist/replica.d.ts.map +1 -1
  147. package/dist/replica.js +133 -74
  148. package/dist/room-draft.d.ts +16 -2
  149. package/dist/room-draft.d.ts.map +1 -1
  150. package/dist/room-draft.js +32 -25
  151. package/dist/rows.d.ts +3 -1
  152. package/dist/rows.d.ts.map +1 -1
  153. package/dist/rows.js +8 -1
  154. package/dist/save.d.ts +47 -9
  155. package/dist/save.d.ts.map +1 -1
  156. package/dist/save.js +501 -165
  157. package/dist/search.d.ts +29 -8
  158. package/dist/search.d.ts.map +1 -1
  159. package/dist/search.js +150 -17
  160. package/dist/seed.d.ts +16 -0
  161. package/dist/seed.d.ts.map +1 -0
  162. package/dist/seed.js +75 -0
  163. package/dist/shared.d.ts +9 -1
  164. package/dist/shared.d.ts.map +1 -1
  165. package/dist/shared.js +134 -41
  166. package/dist/store.d.ts +30 -17
  167. package/dist/store.d.ts.map +1 -1
  168. package/dist/store.js +489 -430
  169. package/dist/tables.d.ts +181 -0
  170. package/dist/tables.d.ts.map +1 -0
  171. package/dist/tables.js +309 -0
  172. package/dist/turn.d.ts +5 -3
  173. package/dist/turn.d.ts.map +1 -1
  174. package/dist/turn.js +35 -13
  175. package/dist/windows.d.ts +21 -0
  176. package/dist/windows.d.ts.map +1 -0
  177. package/dist/windows.js +84 -0
  178. package/dist/wire.d.ts +159 -33
  179. package/dist/wire.d.ts.map +1 -1
  180. package/dist/wire.js +53 -32
  181. package/package.json +6 -2
  182. package/dist/acquisition.d.ts +0 -14
  183. package/dist/acquisition.d.ts.map +0 -1
  184. package/dist/acquisition.js +0 -24
@@ -3,13 +3,26 @@ import { runtimeError } from './failure.js';
3
3
  import { declaredFile } from './file.js';
4
4
  import { changeBatchIdentity, chatDraftRevision } from './identity.js';
5
5
  import { OFFLINE } from './outcome.js';
6
+ import { capped, cursorOf, decodedCursor, FROM_START, messageOrder, precedes } from './windows.js';
6
7
  const QUIET = {
7
8
  turn: null,
8
9
  questions: [],
9
10
  process: null,
10
11
  commands: [],
11
12
  options: [],
13
+ starting: null,
14
+ link: null,
15
+ deliveryUnknown: [],
12
16
  };
17
+ const FACTS = [
18
+ 'questions',
19
+ 'process',
20
+ 'commands',
21
+ 'options',
22
+ 'starting',
23
+ 'link',
24
+ 'deliveryUnknown',
25
+ ];
13
26
  export function unheldView(conversation) {
14
27
  return {
15
28
  conversation,
@@ -19,10 +32,23 @@ export function unheldView(conversation) {
19
32
  process: null,
20
33
  commands: [],
21
34
  options: [],
35
+ starting: null,
36
+ link: null,
37
+ deliveryUnknown: [],
22
38
  chatDraft: null,
23
39
  };
24
40
  }
25
41
  export const SESSION_STARTED = 'Session started';
42
+ const NEW_CHAT = 'new:';
43
+ export function newChat(agent) {
44
+ return `${NEW_CHAT}${agent}`;
45
+ }
46
+ function creationOf(input) {
47
+ if (typeof input !== 'object' || input === null)
48
+ return null;
49
+ const { agent_id: agent, operation_id: operation } = input;
50
+ return typeof agent === 'string' && typeof operation === 'string' ? { agent, operation } : null;
51
+ }
26
52
  function startedTurn(turn, operation) {
27
53
  if (operation.op === 'begin')
28
54
  return { turn, started: operation.at, parts: [], context: null };
@@ -72,19 +98,25 @@ function appliedTurn(held, operation) {
72
98
  parts: held.parts.map((kept, at) => (at === operation.index ? changed : kept)),
73
99
  };
74
100
  }
75
- function ordered(left, right) {
76
- if (left.position !== right.position)
77
- return left.position < right.position ? -1 : 1;
78
- if (left.identity !== right.identity)
79
- return left.identity < right.identity ? -1 : 1;
80
- return 0;
81
- }
82
101
  function windowOf(held, arriving, limit) {
83
102
  const byIdentity = new Map(held.map((message) => [message.identity, message]));
84
103
  for (const message of arriving)
85
104
  byIdentity.set(message.identity, message);
86
- const next = [...byIdentity.values()].sort(ordered);
87
- return limit === undefined ? next : next.slice(Math.max(0, next.length - limit));
105
+ const next = [...byIdentity.values()].sort(messageOrder);
106
+ const start = Math.max(0, next.length - limit);
107
+ return { messages: next.slice(start), dropped: start > 0 };
108
+ }
109
+ function frameOf(asked, messages, more, total) {
110
+ const oldest = messages[0];
111
+ const next = !more
112
+ ? undefined
113
+ : oldest === undefined
114
+ ? (asked.after ?? FROM_START)
115
+ : cursorOf([oldest.position, oldest.identity]);
116
+ return {
117
+ ...(next === undefined ? {} : { next }),
118
+ ...(total === null ? {} : { total }),
119
+ };
88
120
  }
89
121
  function newestOf(arriving, from) {
90
122
  return arriving.reduce((top, message) => (top === null || message.position > top ? message.position : top), from);
@@ -146,40 +178,58 @@ function historyPages(result) {
146
178
  }
147
179
  return pages;
148
180
  }
149
- function turnOf(result) {
181
+ function answered(result, field) {
150
182
  if (result.kind !== 'action')
151
183
  return null;
152
184
  const answer = result.result;
153
185
  if (typeof answer !== 'object' || answer === null)
154
186
  return null;
155
- const record = answer;
156
- return typeof record.turn === 'string' ? record.turn : null;
187
+ const value = answer[field];
188
+ return typeof value === 'string' ? value : null;
189
+ }
190
+ function beforeOf(after) {
191
+ if (after === undefined)
192
+ return null;
193
+ const decoded = typeof after === 'string' ? decodedCursor(after) : null;
194
+ if (decoded?.length === 0)
195
+ return null;
196
+ const [position, identity] = decoded ?? [];
197
+ if (decoded?.length !== 2 || typeof position !== 'string' || typeof identity !== 'string') {
198
+ throw runtimeError('invalid-after', `conversation after must be the next an earlier view returned, got ${String(after)}`);
199
+ }
200
+ return [position, identity];
157
201
  }
158
- function limitOf(options) {
159
- const { limit } = options;
160
- if (limit === undefined)
161
- return undefined;
162
- if (!Number.isInteger(limit) || limit < 0) {
202
+ function askedOf(options) {
203
+ const { limit, after, total } = options;
204
+ if (limit !== undefined && (!Number.isInteger(limit) || limit < 0)) {
163
205
  throw runtimeError('invalid-limit', `conversation limit must be a non-negative integer, got ${String(limit)}`);
164
206
  }
165
- return limit;
207
+ return { limit: capped(limit), after, before: beforeOf(after), total: total === true };
166
208
  }
167
209
  const EXPIRED = { kind: 'rejected', code: 'expired', fields: {}, retryable: false };
168
210
  function unwritten(draft) {
169
- return draft.text === '' && draft.files.length === 0;
211
+ return draft.text === '';
170
212
  }
171
213
  function pending(draft) {
172
- return draft.submitted === undefined ? {} : { submitted: draft.submitted };
214
+ return {
215
+ ...(draft.submitted === undefined ? {} : { submitted: draft.submitted }),
216
+ ...(draft.moves === undefined ? {} : { moves: draft.moves }),
217
+ };
218
+ }
219
+ function empty(draft) {
220
+ return unwritten(draft) && draft.submitted === undefined && draft.moves === undefined;
173
221
  }
174
222
  function withSubmitted(draft, submitted) {
175
223
  const { submitted: _held, ...unsent } = draft;
176
224
  return submitted.length === 0 ? unsent : { ...unsent, submitted };
177
225
  }
226
+ function prepended(draft, said) {
227
+ return { text: [said.text, draft.text].filter((one) => one !== '').join('\n') };
228
+ }
178
229
  function returned(draft, submission, others) {
179
230
  return withSubmitted({
180
231
  conversation: draft.conversation,
181
- text: [submission.text, draft.text].filter((one) => one !== '').join('\n'),
182
- files: [...submission.files, ...draft.files],
232
+ ...prepended(draft, submission),
183
233
  revision: chatDraftRevision(),
184
234
  }, others);
185
235
  }
@@ -190,10 +240,13 @@ export function createConversationStore(deps) {
190
240
  const drafts = new Map();
191
241
  const sending = new Map();
192
242
  const behind = new Set();
243
+ const catching = new Map();
193
244
  const gaps = new Map();
194
245
  const lives = new Map();
246
+ const unsent = new Map();
195
247
  const views = new Map();
196
248
  const listing = new Map();
249
+ const creations = new Set();
197
250
  const listWatchers = new Set();
198
251
  let listKnown = false;
199
252
  const agentWatchers = new Set();
@@ -205,7 +258,7 @@ export function createConversationStore(deps) {
205
258
  function liveOf(conversation) {
206
259
  return lives.get(conversation) ?? QUIET;
207
260
  }
208
- function viewOf(conversation, held) {
261
+ function viewOf(conversation, held, frame) {
209
262
  const draft = drafts.get(conversation);
210
263
  const facts = liveOf(conversation);
211
264
  return {
@@ -216,14 +269,18 @@ export function createConversationStore(deps) {
216
269
  text,
217
270
  message,
218
271
  })),
272
+ ...frame,
219
273
  turn: structuredClone(facts.turn),
220
274
  questions: structuredClone(facts.questions),
221
275
  process: facts.process === null ? null : { ...facts.process },
222
276
  commands: structuredClone(facts.commands),
223
277
  options: structuredClone(facts.options),
278
+ starting: facts.starting,
279
+ link: facts.link === null ? null : { ...facts.link },
280
+ deliveryUnknown: [...facts.deliveryUnknown],
224
281
  chatDraft: draft === undefined || unwritten(draft)
225
282
  ? null
226
- : { text: draft.text, files: [...draft.files], revision: draft.revision },
283
+ : { text: draft.text, revision: draft.revision },
227
284
  };
228
285
  }
229
286
  function emit(conversation) {
@@ -235,16 +292,34 @@ export function createConversationStore(deps) {
235
292
  const ready = [...view.watchers].filter((watcher) => watcher.ready);
236
293
  if (ready.length === 0)
237
294
  continue;
238
- const value = viewOf(conversation, view.window);
295
+ const value = viewOf(conversation, view.window, sharedFrame(view, view.window));
239
296
  for (const watcher of ready)
240
297
  watcher.notify(value);
241
298
  }
242
299
  }
243
- function slide(conversation, arriving) {
300
+ function sharedFrame(view, window) {
301
+ return frameOf(view.asked, window, view.more, view.total);
302
+ }
303
+ async function slide(local, conversation, arriving) {
304
+ let count;
244
305
  for (const view of views.values()) {
245
306
  if (view.conversation !== conversation || view.window === null)
246
307
  continue;
247
- view.window = arriving === null ? [] : windowOf(view.window, arriving, view.limit);
308
+ if (arriving === null) {
309
+ view.window = [];
310
+ view.more = false;
311
+ if (view.total !== null)
312
+ view.total = 0;
313
+ continue;
314
+ }
315
+ const joining = arriving.filter((message) => precedes(message, view.asked.before));
316
+ const next = windowOf(view.window, joining, view.asked.limit);
317
+ view.window = next.messages;
318
+ view.more ||= next.dropped;
319
+ if (view.total !== null && arriving.length > 0) {
320
+ count ??= await local.messageCount(conversation);
321
+ view.total = count;
322
+ }
248
323
  }
249
324
  }
250
325
  function changesOf(conversation, arriving) {
@@ -264,6 +339,7 @@ export function createConversationStore(deps) {
264
339
  created: entry.created,
265
340
  finished: entry.finished,
266
341
  outcome: entry.outcome,
342
+ waiting: entry.waiting,
267
343
  state: stateOf(entry.conversation),
268
344
  started: lives.get(entry.conversation)?.turn?.started ?? null,
269
345
  }));
@@ -332,6 +408,12 @@ export function createConversationStore(deps) {
332
408
  if (named)
333
409
  emitList();
334
410
  emitAgents();
411
+ const ended = [...drafts.keys()].filter(retiredChat);
412
+ if (ended.length > 0)
413
+ await remove(ended);
414
+ }
415
+ function retiredChat(conversation) {
416
+ return (conversation.startsWith(NEW_CHAT) && retiredAgents.has(conversation.slice(NEW_CHAT.length)));
335
417
  }
336
418
  function agentsReady() {
337
419
  agentsRead ??= readAgents().catch(() => {
@@ -340,9 +422,34 @@ export function createConversationStore(deps) {
340
422
  return agentsRead;
341
423
  }
342
424
  function live(conversation, change) {
425
+ const owed = unsent.get(conversation);
426
+ for (const fact of FACTS.filter((one) => one in change))
427
+ owed?.delete(fact);
343
428
  setLive(conversation, { ...liveOf(conversation), ...change });
344
429
  emit(conversation);
345
430
  }
431
+ function openingEnded() {
432
+ const owed = [...unsent];
433
+ unsent.clear();
434
+ for (const [conversation, facts] of owed) {
435
+ const held = lives.get(conversation);
436
+ if (held === undefined || facts.size === 0)
437
+ continue;
438
+ setLive(conversation, {
439
+ turn: held.turn,
440
+ questions: facts.has('questions') ? QUIET.questions : held.questions,
441
+ process: facts.has('process') ? QUIET.process : held.process,
442
+ commands: facts.has('commands') ? QUIET.commands : held.commands,
443
+ options: facts.has('options') ? QUIET.options : held.options,
444
+ starting: facts.has('starting') ? QUIET.starting : held.starting,
445
+ link: facts.has('link') ? QUIET.link : held.link,
446
+ deliveryUnknown: facts.has('deliveryUnknown')
447
+ ? QUIET.deliveryUnknown
448
+ : held.deliveryUnknown,
449
+ });
450
+ emit(conversation);
451
+ }
452
+ }
346
453
  function relayed(conversation, turn, operation) {
347
454
  const held = liveOf(conversation).turn;
348
455
  const next = held?.turn === turn ? appliedTurn(held, operation) : startedTurn(turn, operation);
@@ -386,8 +493,9 @@ export function createConversationStore(deps) {
386
493
  partials.delete(conversation);
387
494
  drafts.delete(conversation);
388
495
  behind.delete(conversation);
496
+ unsent.delete(conversation);
389
497
  setLive(conversation, null);
390
- slide(conversation, null);
498
+ await slide(local, conversation, null);
391
499
  }
392
500
  if (changed.length > 0)
393
501
  deps.publish(conversationChangeSet(removed, changed));
@@ -415,11 +523,11 @@ export function createConversationStore(deps) {
415
523
  if (advances)
416
524
  reads.set(conversation, message.position);
417
525
  sealTurns(conversation, new Set([message.turn]));
418
- slide(conversation, [message]);
526
+ await slide(local, conversation, [message]);
419
527
  deps.publish(conversationChangeSet(changesOf(conversation, [message]), [conversation]));
420
528
  });
421
529
  }
422
- async function readHistories(conversations) {
530
+ async function readHistories(conversations, ended) {
423
531
  const generations = new Map(conversations.map((one) => [one, gaps.get(one) ?? 0]));
424
532
  let asking = conversations.map((conversation) => ({
425
533
  conversation,
@@ -445,10 +553,33 @@ export function createConversationStore(deps) {
445
553
  following.push({ conversation: page.conversation, after: page.next });
446
554
  }
447
555
  }
556
+ for (const { conversation } of asking) {
557
+ if (!following.some((next) => next.conversation === conversation))
558
+ ended(conversation);
559
+ }
448
560
  asking = following;
449
561
  }
450
562
  return null;
451
563
  }
564
+ function caughtUp(conversations) {
565
+ const ends = new Map();
566
+ for (const conversation of conversations) {
567
+ const held = new Promise((end) => ends.set(conversation, end));
568
+ catching.set(conversation, held);
569
+ void held.then(() => {
570
+ if (catching.get(conversation) === held)
571
+ catching.delete(conversation);
572
+ });
573
+ }
574
+ const ended = (conversation) => ends.get(conversation)?.();
575
+ const read = readHistories(conversations, ended);
576
+ const endAll = () => {
577
+ for (const conversation of conversations)
578
+ ended(conversation);
579
+ };
580
+ read.then(endAll, endAll);
581
+ return read;
582
+ }
452
583
  function keepPage(conversation, arriving, from, next, generation) {
453
584
  return deps.durably(async (local) => {
454
585
  if (!replicated.has(conversation))
@@ -467,7 +598,7 @@ export function createConversationStore(deps) {
467
598
  sealTurns(conversation, new Set(arriving.map((message) => message.turn)));
468
599
  if (whole && (gaps.get(conversation) ?? 0) === generation)
469
600
  behind.delete(conversation);
470
- slide(conversation, arriving);
601
+ await slide(local, conversation, arriving);
471
602
  if (arriving.length > 0 || sealing || whole) {
472
603
  deps.publish(conversationChangeSet(changesOf(conversation, arriving), [conversation]));
473
604
  }
@@ -478,11 +609,8 @@ export function createConversationStore(deps) {
478
609
  if (event.kind === 'gap') {
479
610
  if (replicated.has(conversation))
480
611
  gap(conversation);
481
- const held = lives.get(conversation);
482
- if (held === undefined)
483
- return;
484
- setLive(conversation, { ...QUIET, turn: held.turn });
485
- emit(conversation);
612
+ if (lives.has(conversation))
613
+ unsent.set(conversation, new Set(FACTS));
486
614
  return;
487
615
  }
488
616
  if (event.kind === 'turn') {
@@ -505,6 +633,18 @@ export function createConversationStore(deps) {
505
633
  live(conversation, { options: structuredClone(event.options) });
506
634
  return;
507
635
  }
636
+ if (event.kind === 'starting') {
637
+ live(conversation, { starting: event.step });
638
+ return;
639
+ }
640
+ if (event.kind === 'link') {
641
+ live(conversation, { link: { connected: event.connected } });
642
+ return;
643
+ }
644
+ if (event.kind === 'deliveries') {
645
+ live(conversation, { deliveryUnknown: [...event.unknown] });
646
+ return;
647
+ }
508
648
  const arriving = acceptedMessage(event.message);
509
649
  if (arriving === null)
510
650
  return;
@@ -516,12 +656,12 @@ export function createConversationStore(deps) {
516
656
  emit(conversation);
517
657
  }
518
658
  async function kept(local, conversation, following) {
519
- if (following === null || (unwritten(following) && following.submitted === undefined)) {
659
+ if (following === null || empty(following)) {
520
660
  await local.dropChatDraft(conversation);
521
661
  drafts.delete(conversation);
522
662
  }
523
663
  else {
524
- await local.keepChatDraft(following);
664
+ await local.keepChatDrafts([following]);
525
665
  drafts.set(conversation, following);
526
666
  }
527
667
  }
@@ -534,6 +674,28 @@ export function createConversationStore(deps) {
534
674
  await kept(local, conversation, withSubmitted(held, left));
535
675
  return true;
536
676
  }
677
+ function moved(agent, operation, to) {
678
+ const from = newChat(agent);
679
+ return deps.durably(async (local) => {
680
+ const held = drafts.get(from);
681
+ const earlier = (held?.moves ?? []).filter((one) => !deps.expired(one));
682
+ if (earlier.includes(operation))
683
+ return;
684
+ const blank = {
685
+ conversation: from,
686
+ text: '',
687
+ revision: chatDraftRevision(),
688
+ };
689
+ const { moves: _answered, ...unsent } = held ?? blank;
690
+ const taken = drafts.has(to) || empty(unsent) ? [] : [{ ...unsent, conversation: to }];
691
+ const left = { ...(taken.length === 0 ? unsent : blank), moves: [...earlier, operation] };
692
+ await local.keepChatDrafts([left, ...taken]);
693
+ drafts.set(from, left);
694
+ for (const one of taken)
695
+ drafts.set(to, one);
696
+ deps.publish(conversationChangeSet([], [from, to]));
697
+ });
698
+ }
537
699
  function withDraft(conversation, next) {
538
700
  return deps.durably(async (local) => {
539
701
  const following = await next(drafts.get(conversation), local);
@@ -552,17 +714,12 @@ export function createConversationStore(deps) {
552
714
  if (submission !== undefined)
553
715
  return undefined;
554
716
  const said = draft ?? current;
555
- submission = {
556
- operation,
557
- text: said?.text ?? '',
558
- files: [...(said?.files ?? [])],
559
- turn: null,
560
- };
717
+ submission = { operation, text: said?.text ?? '', turn: null };
561
718
  return {
562
719
  conversation,
563
720
  ...(draft === undefined || current === undefined
564
- ? { text: '', files: [], revision: chatDraftRevision() }
565
- : { text: current.text, files: current.files, revision: current.revision }),
721
+ ? { text: '', revision: chatDraftRevision() }
722
+ : { text: current.text, revision: current.revision }),
566
723
  submitted: [...(current?.submitted ?? []), submission],
567
724
  };
568
725
  });
@@ -580,10 +737,8 @@ export function createConversationStore(deps) {
580
737
  const dispatch = {};
581
738
  sending.set(submission.operation, dispatch);
582
739
  try {
583
- const files = await Promise.all(submission.files.map(async (file) => ({ ...(await declaredFile(file)), blob: file.blob })));
584
740
  const result = await invoke('conversation.send', conversation, {
585
741
  text: submission.text,
586
- files,
587
742
  operation_id: submission.operation,
588
743
  });
589
744
  if (result.kind === 'revoked-authorization')
@@ -592,7 +747,7 @@ export function createConversationStore(deps) {
592
747
  deps.unanswered();
593
748
  return result;
594
749
  }
595
- const turn = turnOf(result);
750
+ const turn = answered(result, 'turn');
596
751
  await withDraft(conversation, async (current, local) => {
597
752
  const held = current?.submitted?.find((one) => one.operation === submission.operation);
598
753
  if (current === undefined || held === undefined || held.turn !== null)
@@ -621,11 +776,24 @@ export function createConversationStore(deps) {
621
776
  return entries();
622
777
  },
623
778
  async read(conversation, options = {}) {
624
- const limit = limitOf(options);
625
- return deps.durably(async (local) => structuredClone(viewOf(conversation, await local.messageWindow(conversation, limit))));
779
+ const asked = askedOf(options);
780
+ return deps.durably(async (local) => {
781
+ const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before);
782
+ const total = asked.total ? await local.messageCount(conversation) : null;
783
+ return structuredClone(viewOf(conversation, messages, frameOf(asked, messages, more, total)));
784
+ });
626
785
  },
627
- create(input) {
628
- return deps.call('conversation.create', input);
786
+ async create(input) {
787
+ const result = await deps.call('conversation.create', input);
788
+ const conversation = answered(result, 'conversation');
789
+ if (conversation === null)
790
+ return result;
791
+ if (!listing.has(conversation))
792
+ creations.add(conversation);
793
+ const creation = creationOf(input);
794
+ if (creation !== null)
795
+ await moved(creation.agent, creation.operation, conversation);
796
+ return result;
629
797
  },
630
798
  async send(conversation, operation = changeBatchIdentity(), draft) {
631
799
  if (deps.online()) {
@@ -638,10 +806,19 @@ export function createConversationStore(deps) {
638
806
  if (draft !== undefined) {
639
807
  await withDraft(conversation, (current) => current?.submitted?.some((one) => one.operation === operation) === true
640
808
  ? undefined
641
- : returned(current ?? { conversation, text: '', files: [], revision: '' }, { operation, ...draft, turn: null }, current?.submitted ?? []));
809
+ : returned(current ?? { conversation, text: '', revision: '' }, { operation, ...draft, turn: null }, current?.submitted ?? []));
642
810
  }
643
811
  return OFFLINE;
644
812
  },
813
+ async attach(room, file) {
814
+ if (!deps.online())
815
+ return OFFLINE;
816
+ return deps.call('conversation.attach', {
817
+ room,
818
+ ...(await declaredFile(file)),
819
+ blob: file.blob,
820
+ });
821
+ },
645
822
  answer(conversation, answer) {
646
823
  return invoke('conversation.answer', conversation, { answer });
647
824
  },
@@ -672,21 +849,22 @@ export function createConversationStore(deps) {
672
849
  return withDraft(conversation, (current) => ({
673
850
  conversation,
674
851
  text: draft.text,
675
- files: [...draft.files],
676
852
  revision,
677
- ...(current?.submitted === undefined ? {} : { submitted: current.submitted }),
853
+ ...(current === undefined ? {} : pending(current)),
854
+ }));
855
+ },
856
+ prepend(conversation, draft) {
857
+ return withDraft(conversation, (current) => ({
858
+ conversation,
859
+ ...(current === undefined ? { text: draft.text } : prepended(current, draft)),
860
+ revision: chatDraftRevision(),
861
+ ...(current === undefined ? {} : pending(current)),
678
862
  }));
679
863
  },
680
864
  discard(conversation) {
681
865
  return withDraft(conversation, (current) => current === undefined
682
866
  ? undefined
683
- : {
684
- conversation,
685
- text: '',
686
- files: [],
687
- revision: chatDraftRevision(),
688
- ...pending(current),
689
- });
867
+ : { conversation, text: '', revision: chatDraftRevision(), ...pending(current) });
690
868
  },
691
869
  };
692
870
  return {
@@ -713,7 +891,9 @@ export function createConversationStore(deps) {
713
891
  const named = new Set(listing.keys());
714
892
  const leaving = [
715
893
  ...new Set([...replicated, ...stored, ...reads.keys(), ...drafts.keys()]),
716
- ].filter((conversation) => !named.has(conversation));
894
+ ].filter((conversation) => !named.has(conversation) &&
895
+ !creations.has(conversation) &&
896
+ (!conversation.startsWith(NEW_CHAT) || retiredChat(conversation)));
717
897
  for (const conversation of named) {
718
898
  if (!replicated.has(conversation))
719
899
  behind.add(conversation);
@@ -724,12 +904,20 @@ export function createConversationStore(deps) {
724
904
  await remove(leaving);
725
905
  },
726
906
  async catchUp(progress) {
727
- const reading = [...behind].filter((conversation) => replicated.has(conversation)).sort();
728
- if (reading.length > 0 && !deps.closed()) {
729
- const failed = await readHistories(reading);
907
+ const owed = (conversation) => behind.has(conversation) && replicated.has(conversation);
908
+ const read = async (conversations) => {
909
+ if (conversations.length === 0 || deps.closed())
910
+ return;
911
+ const failed = await caughtUp(conversations);
730
912
  if (failed?.kind === 'transport-uncertainty')
731
913
  progress.reachable = false;
732
- }
914
+ };
915
+ const asking = [...behind].filter(owed).sort();
916
+ const awaited = asking.filter((conversation) => catching.has(conversation));
917
+ await Promise.all([
918
+ read(asking.filter((conversation) => !catching.has(conversation))),
919
+ Promise.all(awaited.map((conversation) => catching.get(conversation))).then(() => read(awaited.filter((conversation) => owed(conversation) && !catching.has(conversation)))),
920
+ ]);
733
921
  if (behind.size > 0)
734
922
  progress.complete = false;
735
923
  },
@@ -771,27 +959,38 @@ export function createConversationStore(deps) {
771
959
  reopened() {
772
960
  sending.clear();
773
961
  },
962
+ lost() {
963
+ catching.clear();
964
+ },
774
965
  watch(conversation, options, deliver) {
775
- const limit = limitOf(options);
776
- const key = JSON.stringify([conversation, limit ?? null]);
966
+ const asked = askedOf(options);
967
+ const key = JSON.stringify([conversation, asked.limit, asked.after ?? null, asked.total]);
777
968
  const view = views.get(key) ?? {
778
969
  conversation,
779
- limit,
970
+ asked,
780
971
  watchers: new Set(),
781
972
  window: null,
973
+ more: false,
974
+ total: null,
782
975
  };
783
976
  views.set(key, view);
784
977
  const watcher = { notify: deliver, ready: false };
785
978
  view.watchers.add(watcher);
786
979
  void deps
787
980
  .durably(async (local) => {
788
- view.window ??= await local.messageWindow(conversation, limit);
981
+ if (view.window !== null)
982
+ return;
983
+ const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before);
984
+ view.window = messages;
985
+ view.more = more;
986
+ view.total = asked.total ? await local.messageCount(conversation) : null;
789
987
  })
790
988
  .then(() => {
791
989
  if (deps.closed() || !view.watchers.has(watcher))
792
990
  return;
793
991
  watcher.ready = true;
794
- watcher.notify(viewOf(conversation, view.window ?? []));
992
+ const window = view.window ?? [];
993
+ watcher.notify(viewOf(conversation, window, sharedFrame(view, window)));
795
994
  })
796
995
  .catch(() => { });
797
996
  return () => {
@@ -838,12 +1037,16 @@ export function createConversationStore(deps) {
838
1037
  if (event.kind === 'list' || event.kind === 'listed' || event.kind === 'unlisted') {
839
1038
  if (event.kind === 'list') {
840
1039
  listing.clear();
841
- for (const entry of event.entries)
1040
+ for (const entry of event.entries) {
842
1041
  listing.set(entry.conversation, { ...entry });
1042
+ creations.delete(entry.conversation);
1043
+ }
843
1044
  listKnown = true;
1045
+ openingEnded();
844
1046
  }
845
1047
  else if (event.kind === 'listed') {
846
1048
  listing.set(event.entry.conversation, { ...event.entry });
1049
+ creations.delete(event.entry.conversation);
847
1050
  }
848
1051
  else {
849
1052
  listing.delete(event.conversation);
@@ -866,6 +1069,7 @@ export function createConversationStore(deps) {
866
1069
  close() {
867
1070
  views.clear();
868
1071
  lives.clear();
1072
+ unsent.clear();
869
1073
  listWatchers.clear();
870
1074
  agentWatchers.clear();
871
1075
  },
@@ -0,0 +1,2 @@
1
+ export declare function sha256(blob: Blob): Promise<string>;
2
+ //# sourceMappingURL=digest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"digest.d.ts","sourceRoot":"","sources":["../src/digest.ts"],"names":[],"mappings":"AA0EA,wBAAsB,MAAM,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CA8BxD"}