@agentshouse/mdruntime 0.1.0-c8e110144a30a → 0.1.0-c91a238f9703e
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.
- package/AGENTS.md +281 -275
- package/README.md +8 -8
- package/dist/acquisition.d.ts +14 -0
- package/dist/acquisition.d.ts.map +1 -0
- package/dist/acquisition.js +24 -0
- package/dist/adapter.d.ts +56 -10
- package/dist/adapter.d.ts.map +1 -1
- package/dist/attempt.d.ts.map +1 -1
- package/dist/attempt.js +14 -1
- package/dist/bootstrap.d.ts +3 -2
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +32 -6
- package/dist/change-set.d.ts +2 -0
- package/dist/change-set.d.ts.map +1 -1
- package/dist/change-set.js +36 -10
- package/dist/chat/acts.d.ts +3 -4
- package/dist/chat/acts.d.ts.map +1 -1
- package/dist/chat/acts.js +10 -10
- package/dist/chat/earlier.d.ts +10 -0
- package/dist/chat/earlier.d.ts.map +1 -0
- package/dist/chat/earlier.js +54 -0
- package/dist/chat/kit.d.ts +2 -1
- package/dist/chat/kit.d.ts.map +1 -1
- package/dist/chat/kit.js +3 -3
- package/dist/chat/list.d.ts +8 -1
- package/dist/chat/list.d.ts.map +1 -1
- package/dist/chat/list.js +48 -8
- package/dist/chat/markdown.d.ts +2 -1
- package/dist/chat/markdown.d.ts.map +1 -1
- package/dist/chat/markdown.js +7 -6
- package/dist/chat/message.d.ts.map +1 -1
- package/dist/chat/message.js +2 -2
- package/dist/chat/messages.d.ts +1 -0
- package/dist/chat/messages.d.ts.map +1 -1
- package/dist/chat/messages.js +4 -0
- package/dist/chat/provider.d.ts +1 -1
- package/dist/chat/provider.d.ts.map +1 -1
- package/dist/chat/provider.js +62 -79
- package/dist/chat/question.d.ts +1 -1
- package/dist/chat/question.d.ts.map +1 -1
- package/dist/chat/question.js +7 -18
- package/dist/chat/ready.d.ts +1 -0
- package/dist/chat/ready.d.ts.map +1 -1
- package/dist/chat/ready.js +4 -4
- package/dist/chat/style.js +4 -4
- package/dist/client.d.ts +17 -9
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +19 -6
- package/dist/collection.d.ts +17 -3
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +230 -19
- package/dist/connection.d.ts +33 -9
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +1630 -372
- package/dist/conversation.d.ts +20 -7
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +448 -159
- package/dist/durability.d.ts +7 -1
- package/dist/durability.d.ts.map +1 -1
- package/dist/entry.d.ts +17 -0
- package/dist/entry.d.ts.map +1 -0
- package/dist/entry.js +156 -0
- package/dist/fence.d.ts +6 -0
- package/dist/fence.d.ts.map +1 -0
- package/dist/fence.js +10 -0
- package/dist/host.d.ts +17 -2
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +94 -9
- package/dist/identity.d.ts +2 -0
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +6 -0
- package/dist/index.d.ts +10 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/memory.d.ts +9 -2
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +39 -7
- package/dist/model.d.ts +4 -2
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +10 -1
- package/dist/observation.d.ts +7 -2
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +29 -12
- package/dist/outcome.d.ts +12 -2
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +4 -0
- package/dist/overlay.d.ts +2 -0
- package/dist/overlay.d.ts.map +1 -1
- package/dist/overlay.js +58 -55
- package/dist/query.d.ts +56 -4
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +36 -7
- package/dist/reach.d.ts +6 -3
- package/dist/reach.d.ts.map +1 -1
- package/dist/reach.js +30 -5
- package/dist/react.d.ts +8 -6
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +7 -17
- package/dist/read-side.d.ts +13 -6
- package/dist/read-side.d.ts.map +1 -1
- package/dist/read-side.js +419 -92
- package/dist/readiness.d.ts +53 -0
- package/dist/readiness.d.ts.map +1 -0
- package/dist/readiness.js +33 -0
- package/dist/reads.d.ts +11 -0
- package/dist/reads.d.ts.map +1 -0
- package/dist/reads.js +98 -0
- package/dist/recovery.d.ts +25 -0
- package/dist/recovery.d.ts.map +1 -0
- package/dist/recovery.js +197 -0
- package/dist/refresh.d.ts +4 -4
- package/dist/refresh.d.ts.map +1 -1
- package/dist/refresh.js +17 -10
- package/dist/replica.d.ts +6 -2
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +76 -23
- package/dist/room-draft.d.ts +13 -1
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +2 -2
- package/dist/rows.d.ts +6 -13
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +10 -13
- package/dist/save.d.ts +7 -2
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +59 -19
- package/dist/search.d.ts +49 -6
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +192 -47
- package/dist/seed.d.ts +16 -0
- package/dist/seed.d.ts.map +1 -0
- package/dist/seed.js +75 -0
- package/dist/shared.d.ts +11 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +368 -0
- package/dist/status.d.ts +4 -0
- package/dist/status.d.ts.map +1 -1
- package/dist/store.d.ts +56 -11
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +311 -84
- package/dist/turn.d.ts +7 -0
- package/dist/turn.d.ts.map +1 -0
- package/dist/turn.js +45 -0
- package/dist/windows.d.ts +26 -0
- package/dist/windows.d.ts.map +1 -0
- package/dist/windows.js +93 -0
- package/dist/wire.d.ts +152 -32
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +52 -31
- package/package.json +6 -2
package/dist/conversation.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { conversationChangeSet } from './change-set.js';
|
|
2
2
|
import { runtimeError } from './failure.js';
|
|
3
3
|
import { declaredFile } from './file.js';
|
|
4
|
-
import { chatDraftRevision } from './identity.js';
|
|
4
|
+
import { changeBatchIdentity, chatDraftRevision } from './identity.js';
|
|
5
|
+
import { OFFLINE } from './outcome.js';
|
|
6
|
+
import { capped, cursorOf, decodedCursor, FROM_START, messageOrder, precedes } from './windows.js';
|
|
5
7
|
const QUIET = {
|
|
6
8
|
turn: null,
|
|
7
9
|
questions: [],
|
|
@@ -21,6 +23,17 @@ export function unheldView(conversation) {
|
|
|
21
23
|
chatDraft: null,
|
|
22
24
|
};
|
|
23
25
|
}
|
|
26
|
+
export const SESSION_STARTED = 'Session started';
|
|
27
|
+
const NEW_CHAT = 'new:';
|
|
28
|
+
export function newChat(agent) {
|
|
29
|
+
return `${NEW_CHAT}${agent}`;
|
|
30
|
+
}
|
|
31
|
+
function creationOf(input) {
|
|
32
|
+
if (typeof input !== 'object' || input === null)
|
|
33
|
+
return null;
|
|
34
|
+
const { agent_id: agent, operation_id: operation } = input;
|
|
35
|
+
return typeof agent === 'string' && typeof operation === 'string' ? { agent, operation } : null;
|
|
36
|
+
}
|
|
24
37
|
function startedTurn(turn, operation) {
|
|
25
38
|
if (operation.op === 'begin')
|
|
26
39
|
return { turn, started: operation.at, parts: [], context: null };
|
|
@@ -70,18 +83,28 @@ function appliedTurn(held, operation) {
|
|
|
70
83
|
parts: held.parts.map((kept, at) => (at === operation.index ? changed : kept)),
|
|
71
84
|
};
|
|
72
85
|
}
|
|
73
|
-
function
|
|
74
|
-
if (left.position !== right.position)
|
|
75
|
-
return left.position < right.position ? -1 : 1;
|
|
76
|
-
if (left.identity !== right.identity)
|
|
77
|
-
return left.identity < right.identity ? -1 : 1;
|
|
78
|
-
return 0;
|
|
79
|
-
}
|
|
80
|
-
function merged(held, arriving) {
|
|
86
|
+
function windowOf(held, arriving, limit) {
|
|
81
87
|
const byIdentity = new Map(held.map((message) => [message.identity, message]));
|
|
82
88
|
for (const message of arriving)
|
|
83
89
|
byIdentity.set(message.identity, message);
|
|
84
|
-
|
|
90
|
+
const next = [...byIdentity.values()].sort(messageOrder);
|
|
91
|
+
const start = Math.max(0, next.length - limit);
|
|
92
|
+
return { messages: next.slice(start), dropped: start > 0 };
|
|
93
|
+
}
|
|
94
|
+
function frameOf(asked, messages, more, total) {
|
|
95
|
+
const oldest = messages[0];
|
|
96
|
+
const next = !more
|
|
97
|
+
? undefined
|
|
98
|
+
: oldest === undefined
|
|
99
|
+
? (asked.after ?? FROM_START)
|
|
100
|
+
: cursorOf([oldest.position, oldest.identity]);
|
|
101
|
+
return {
|
|
102
|
+
...(next === undefined ? {} : { next }),
|
|
103
|
+
...(total === null ? {} : { total }),
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function newestOf(arriving, from) {
|
|
107
|
+
return arriving.reduce((top, message) => (top === null || message.position > top ? message.position : top), from);
|
|
85
108
|
}
|
|
86
109
|
function acceptedMessage(value) {
|
|
87
110
|
if (typeof value !== 'object' || value === null)
|
|
@@ -140,34 +163,77 @@ function historyPages(result) {
|
|
|
140
163
|
}
|
|
141
164
|
return pages;
|
|
142
165
|
}
|
|
143
|
-
function
|
|
166
|
+
function answered(result, field) {
|
|
144
167
|
if (result.kind !== 'action')
|
|
145
168
|
return null;
|
|
146
169
|
const answer = result.result;
|
|
147
170
|
if (typeof answer !== 'object' || answer === null)
|
|
148
171
|
return null;
|
|
149
|
-
const
|
|
150
|
-
return typeof
|
|
172
|
+
const value = answer[field];
|
|
173
|
+
return typeof value === 'string' ? value : null;
|
|
151
174
|
}
|
|
152
|
-
function
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
if (
|
|
175
|
+
function beforeOf(after) {
|
|
176
|
+
if (after === undefined)
|
|
177
|
+
return null;
|
|
178
|
+
const decoded = typeof after === 'string' ? decodedCursor(after) : null;
|
|
179
|
+
if (decoded?.length === 0)
|
|
180
|
+
return null;
|
|
181
|
+
const [position, identity] = decoded ?? [];
|
|
182
|
+
if (decoded?.length !== 2 || typeof position !== 'string' || typeof identity !== 'string') {
|
|
183
|
+
throw runtimeError('invalid-after', `conversation after must be the next an earlier view returned, got ${String(after)}`);
|
|
184
|
+
}
|
|
185
|
+
return [position, identity];
|
|
186
|
+
}
|
|
187
|
+
function askedOf(options) {
|
|
188
|
+
const { limit, after, total } = options;
|
|
189
|
+
if (limit !== undefined && (!Number.isInteger(limit) || limit < 0)) {
|
|
157
190
|
throw runtimeError('invalid-limit', `conversation limit must be a non-negative integer, got ${String(limit)}`);
|
|
158
191
|
}
|
|
159
|
-
return limit;
|
|
192
|
+
return { limit: capped(limit), after, before: beforeOf(after), total: total === true };
|
|
193
|
+
}
|
|
194
|
+
const EXPIRED = { kind: 'rejected', code: 'expired', fields: {}, retryable: false };
|
|
195
|
+
function unwritten(draft) {
|
|
196
|
+
return draft.text === '' && draft.files.length === 0;
|
|
197
|
+
}
|
|
198
|
+
function pending(draft) {
|
|
199
|
+
return {
|
|
200
|
+
...(draft.submitted === undefined ? {} : { submitted: draft.submitted }),
|
|
201
|
+
...(draft.moves === undefined ? {} : { moves: draft.moves }),
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
function empty(draft) {
|
|
205
|
+
return unwritten(draft) && draft.submitted === undefined && draft.moves === undefined;
|
|
206
|
+
}
|
|
207
|
+
function withSubmitted(draft, submitted) {
|
|
208
|
+
const { submitted: _held, ...unsent } = draft;
|
|
209
|
+
return submitted.length === 0 ? unsent : { ...unsent, submitted };
|
|
210
|
+
}
|
|
211
|
+
function prepended(draft, said) {
|
|
212
|
+
return {
|
|
213
|
+
text: [said.text, draft.text].filter((one) => one !== '').join('\n'),
|
|
214
|
+
files: [...said.files, ...draft.files],
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
function returned(draft, submission, others) {
|
|
218
|
+
return withSubmitted({
|
|
219
|
+
conversation: draft.conversation,
|
|
220
|
+
...prepended(draft, submission),
|
|
221
|
+
revision: chatDraftRevision(),
|
|
222
|
+
}, others);
|
|
160
223
|
}
|
|
161
224
|
export function createConversationStore(deps) {
|
|
162
|
-
const
|
|
225
|
+
const stored = new Set();
|
|
163
226
|
const reads = new Map();
|
|
227
|
+
const partials = new Set();
|
|
164
228
|
const drafts = new Map();
|
|
165
229
|
const sending = new Map();
|
|
166
230
|
const behind = new Set();
|
|
231
|
+
const catching = new Map();
|
|
167
232
|
const gaps = new Map();
|
|
168
233
|
const lives = new Map();
|
|
169
234
|
const views = new Map();
|
|
170
235
|
const listing = new Map();
|
|
236
|
+
const creations = new Set();
|
|
171
237
|
const listWatchers = new Set();
|
|
172
238
|
let listKnown = false;
|
|
173
239
|
const agentWatchers = new Set();
|
|
@@ -179,49 +245,70 @@ export function createConversationStore(deps) {
|
|
|
179
245
|
function liveOf(conversation) {
|
|
180
246
|
return lives.get(conversation) ?? QUIET;
|
|
181
247
|
}
|
|
182
|
-
function
|
|
183
|
-
const held = messages.get(conversation) ?? [];
|
|
184
|
-
const shown = limit === undefined ? held : held.slice(Math.max(0, held.length - limit));
|
|
185
|
-
return shown.map(({ identity, turn, text, message }) => ({
|
|
186
|
-
identity,
|
|
187
|
-
turn,
|
|
188
|
-
text,
|
|
189
|
-
message: structuredClone(message),
|
|
190
|
-
}));
|
|
191
|
-
}
|
|
192
|
-
function viewOf(conversation, limit, shown = shownMessages(conversation, limit)) {
|
|
248
|
+
function viewOf(conversation, held, frame) {
|
|
193
249
|
const draft = drafts.get(conversation);
|
|
194
250
|
const facts = liveOf(conversation);
|
|
195
251
|
return {
|
|
196
252
|
conversation,
|
|
197
|
-
messages:
|
|
253
|
+
messages: held.map(({ identity, turn, text, message }) => ({
|
|
254
|
+
identity,
|
|
255
|
+
turn,
|
|
256
|
+
text,
|
|
257
|
+
message,
|
|
258
|
+
})),
|
|
259
|
+
...frame,
|
|
198
260
|
turn: structuredClone(facts.turn),
|
|
199
261
|
questions: structuredClone(facts.questions),
|
|
200
262
|
process: facts.process === null ? null : { ...facts.process },
|
|
201
263
|
commands: structuredClone(facts.commands),
|
|
202
264
|
options: structuredClone(facts.options),
|
|
203
|
-
chatDraft: draft === undefined || draft
|
|
265
|
+
chatDraft: draft === undefined || unwritten(draft)
|
|
204
266
|
? null
|
|
205
267
|
: { text: draft.text, files: [...draft.files], revision: draft.revision },
|
|
206
268
|
};
|
|
207
269
|
}
|
|
208
|
-
function emit(conversation
|
|
270
|
+
function emit(conversation) {
|
|
209
271
|
if (deps.closed())
|
|
210
272
|
return;
|
|
211
273
|
for (const view of views.values()) {
|
|
212
|
-
if (view.conversation !== conversation)
|
|
274
|
+
if (view.conversation !== conversation || view.window === null)
|
|
213
275
|
continue;
|
|
214
|
-
if (messagesChanged)
|
|
215
|
-
view.messages = null;
|
|
216
276
|
const ready = [...view.watchers].filter((watcher) => watcher.ready);
|
|
217
277
|
if (ready.length === 0)
|
|
218
278
|
continue;
|
|
219
|
-
|
|
220
|
-
const value = viewOf(conversation, view.limit, view.messages);
|
|
279
|
+
const value = viewOf(conversation, view.window, sharedFrame(view, view.window));
|
|
221
280
|
for (const watcher of ready)
|
|
222
281
|
watcher.notify(value);
|
|
223
282
|
}
|
|
224
283
|
}
|
|
284
|
+
function sharedFrame(view, window) {
|
|
285
|
+
return frameOf(view.asked, window, view.more, view.total);
|
|
286
|
+
}
|
|
287
|
+
async function slide(local, conversation, arriving) {
|
|
288
|
+
let count;
|
|
289
|
+
for (const view of views.values()) {
|
|
290
|
+
if (view.conversation !== conversation || view.window === null)
|
|
291
|
+
continue;
|
|
292
|
+
if (arriving === null) {
|
|
293
|
+
view.window = [];
|
|
294
|
+
view.more = false;
|
|
295
|
+
if (view.total !== null)
|
|
296
|
+
view.total = 0;
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
const joining = arriving.filter((message) => precedes(message, view.asked.before));
|
|
300
|
+
const next = windowOf(view.window, joining, view.asked.limit);
|
|
301
|
+
view.window = next.messages;
|
|
302
|
+
view.more ||= next.dropped;
|
|
303
|
+
if (view.total !== null && arriving.length > 0) {
|
|
304
|
+
count ??= await local.messageCount(conversation);
|
|
305
|
+
view.total = count;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
function changesOf(conversation, arriving) {
|
|
310
|
+
return arriving.map(({ identity, text }) => ({ conversation, identity, text }));
|
|
311
|
+
}
|
|
225
312
|
function stateOf(conversation) {
|
|
226
313
|
const facts = liveOf(conversation);
|
|
227
314
|
if (facts.questions.length > 0)
|
|
@@ -278,7 +365,10 @@ export function createConversationStore(deps) {
|
|
|
278
365
|
const declaration = deps.agents();
|
|
279
366
|
if (declaration === undefined)
|
|
280
367
|
return;
|
|
281
|
-
const
|
|
368
|
+
const held = await deps.documents(declaration);
|
|
369
|
+
if ('pending' in held)
|
|
370
|
+
return;
|
|
371
|
+
const { documents } = held;
|
|
282
372
|
const names = new Map();
|
|
283
373
|
const retired = new Set();
|
|
284
374
|
for (const document of documents) {
|
|
@@ -301,6 +391,12 @@ export function createConversationStore(deps) {
|
|
|
301
391
|
if (named)
|
|
302
392
|
emitList();
|
|
303
393
|
emitAgents();
|
|
394
|
+
const ended = [...drafts.keys()].filter(retiredChat);
|
|
395
|
+
if (ended.length > 0)
|
|
396
|
+
await remove(ended);
|
|
397
|
+
}
|
|
398
|
+
function retiredChat(conversation) {
|
|
399
|
+
return (conversation.startsWith(NEW_CHAT) && retiredAgents.has(conversation.slice(NEW_CHAT.length)));
|
|
304
400
|
}
|
|
305
401
|
function agentsReady() {
|
|
306
402
|
agentsRead ??= readAgents().catch(() => {
|
|
@@ -310,7 +406,7 @@ export function createConversationStore(deps) {
|
|
|
310
406
|
}
|
|
311
407
|
function live(conversation, change) {
|
|
312
408
|
setLive(conversation, { ...liveOf(conversation), ...change });
|
|
313
|
-
emit(conversation
|
|
409
|
+
emit(conversation);
|
|
314
410
|
}
|
|
315
411
|
function relayed(conversation, turn, operation) {
|
|
316
412
|
const held = liveOf(conversation).turn;
|
|
@@ -338,21 +434,25 @@ export function createConversationStore(deps) {
|
|
|
338
434
|
}
|
|
339
435
|
async function remove(leaving) {
|
|
340
436
|
await deps.durably(async (local) => {
|
|
341
|
-
await local.dropConversations(leaving);
|
|
342
437
|
const removed = [];
|
|
343
|
-
const changed = [];
|
|
344
438
|
for (const conversation of leaving) {
|
|
345
|
-
for (const
|
|
346
|
-
removed.push({ conversation, identity:
|
|
439
|
+
for (const identity of await local.messageIdentities(conversation)) {
|
|
440
|
+
removed.push({ conversation, identity, text: null });
|
|
347
441
|
}
|
|
348
|
-
|
|
442
|
+
}
|
|
443
|
+
await local.dropConversations(leaving);
|
|
444
|
+
const changed = [];
|
|
445
|
+
for (const conversation of leaving) {
|
|
446
|
+
if (stored.has(conversation) || drafts.has(conversation) || lives.has(conversation)) {
|
|
349
447
|
changed.push(conversation);
|
|
350
448
|
}
|
|
351
|
-
|
|
449
|
+
stored.delete(conversation);
|
|
352
450
|
reads.delete(conversation);
|
|
451
|
+
partials.delete(conversation);
|
|
353
452
|
drafts.delete(conversation);
|
|
354
453
|
behind.delete(conversation);
|
|
355
454
|
setLive(conversation, null);
|
|
455
|
+
await slide(local, conversation, null);
|
|
356
456
|
}
|
|
357
457
|
if (changed.length > 0)
|
|
358
458
|
deps.publish(conversationChangeSet(removed, changed));
|
|
@@ -370,30 +470,25 @@ export function createConversationStore(deps) {
|
|
|
370
470
|
const advances = read !== undefined &&
|
|
371
471
|
!behind.has(conversation) &&
|
|
372
472
|
(read === null || message.position > read);
|
|
373
|
-
|
|
473
|
+
await sealed(local, conversation, new Set([message.turn]));
|
|
374
474
|
await local.keepMessages({
|
|
375
475
|
conversation,
|
|
376
476
|
messages: [message],
|
|
377
|
-
...(advances ? { read: message.position } : {}),
|
|
378
|
-
sealed,
|
|
477
|
+
...(advances ? { read: { at: message.position, partial: false } } : {}),
|
|
379
478
|
});
|
|
380
|
-
|
|
479
|
+
stored.add(conversation);
|
|
381
480
|
if (advances)
|
|
382
481
|
reads.set(conversation, message.position);
|
|
383
|
-
if (sealed)
|
|
384
|
-
drafts.delete(conversation);
|
|
385
482
|
sealTurns(conversation, new Set([message.turn]));
|
|
386
|
-
|
|
483
|
+
await slide(local, conversation, [message]);
|
|
484
|
+
deps.publish(conversationChangeSet(changesOf(conversation, [message]), [conversation]));
|
|
387
485
|
});
|
|
388
486
|
}
|
|
389
|
-
async function readHistories(conversations) {
|
|
487
|
+
async function readHistories(conversations, ended) {
|
|
390
488
|
const generations = new Map(conversations.map((one) => [one, gaps.get(one) ?? 0]));
|
|
391
|
-
const starts = new Map(conversations.map((one) => [one, reads.get(one)]));
|
|
392
|
-
const arriving = new Map();
|
|
393
|
-
const complete = new Set();
|
|
394
489
|
let asking = conversations.map((conversation) => ({
|
|
395
490
|
conversation,
|
|
396
|
-
after:
|
|
491
|
+
after: reads.get(conversation) ?? undefined,
|
|
397
492
|
}));
|
|
398
493
|
while (asking.length > 0) {
|
|
399
494
|
const result = await deps.call('conversation.history', {
|
|
@@ -405,46 +500,64 @@ export function createConversationStore(deps) {
|
|
|
405
500
|
const pages = historyPages(result);
|
|
406
501
|
if (pages === null)
|
|
407
502
|
return result;
|
|
408
|
-
const asked = new
|
|
503
|
+
const asked = new Map(asking.map(({ conversation, after }) => [conversation, after]));
|
|
409
504
|
const following = [];
|
|
410
505
|
for (const page of pages) {
|
|
411
506
|
if (!asked.has(page.conversation))
|
|
412
507
|
continue;
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
...page.messages,
|
|
416
|
-
]);
|
|
417
|
-
if (page.next === undefined)
|
|
418
|
-
complete.add(page.conversation);
|
|
419
|
-
else
|
|
508
|
+
await keepPage(page.conversation, page.messages, asked.get(page.conversation) ?? null, page.next, generations.get(page.conversation) ?? 0);
|
|
509
|
+
if (page.next !== undefined) {
|
|
420
510
|
following.push({ conversation: page.conversation, after: page.next });
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
for (const { conversation } of asking) {
|
|
514
|
+
if (!following.some((next) => next.conversation === conversation))
|
|
515
|
+
ended(conversation);
|
|
421
516
|
}
|
|
422
517
|
asking = following;
|
|
423
518
|
}
|
|
424
|
-
for (const [conversation, held] of arriving) {
|
|
425
|
-
if (!complete.has(conversation))
|
|
426
|
-
continue;
|
|
427
|
-
await keepHistory(conversation, held, starts.get(conversation), generations.get(conversation) ?? 0);
|
|
428
|
-
}
|
|
429
519
|
return null;
|
|
430
520
|
}
|
|
431
|
-
function
|
|
521
|
+
function caughtUp(conversations) {
|
|
522
|
+
const ends = new Map();
|
|
523
|
+
for (const conversation of conversations) {
|
|
524
|
+
const held = new Promise((end) => ends.set(conversation, end));
|
|
525
|
+
catching.set(conversation, held);
|
|
526
|
+
void held.then(() => {
|
|
527
|
+
if (catching.get(conversation) === held)
|
|
528
|
+
catching.delete(conversation);
|
|
529
|
+
});
|
|
530
|
+
}
|
|
531
|
+
const ended = (conversation) => ends.get(conversation)?.();
|
|
532
|
+
const read = readHistories(conversations, ended);
|
|
533
|
+
const endAll = () => {
|
|
534
|
+
for (const conversation of conversations)
|
|
535
|
+
ended(conversation);
|
|
536
|
+
};
|
|
537
|
+
read.then(endAll, endAll);
|
|
538
|
+
return read;
|
|
539
|
+
}
|
|
540
|
+
function keepPage(conversation, arriving, from, next, generation) {
|
|
432
541
|
return deps.durably(async (local) => {
|
|
433
542
|
if (!replicated.has(conversation))
|
|
434
543
|
return;
|
|
435
|
-
const
|
|
436
|
-
const
|
|
437
|
-
const
|
|
438
|
-
await local
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
544
|
+
const whole = next === undefined;
|
|
545
|
+
const cursor = next ?? newestOf(arriving, from);
|
|
546
|
+
const partial = !whole && (partials.has(conversation) || !reads.has(conversation));
|
|
547
|
+
const sealing = await sealed(local, conversation, new Set(arriving.map((message) => message.turn)));
|
|
548
|
+
await local.keepMessages({ conversation, messages: arriving, read: { at: cursor, partial } });
|
|
549
|
+
stored.add(conversation);
|
|
550
|
+
reads.set(conversation, cursor);
|
|
551
|
+
if (partial)
|
|
552
|
+
partials.add(conversation);
|
|
553
|
+
else
|
|
554
|
+
partials.delete(conversation);
|
|
443
555
|
sealTurns(conversation, new Set(arriving.map((message) => message.turn)));
|
|
444
|
-
if ((gaps.get(conversation) ?? 0) === generation)
|
|
556
|
+
if (whole && (gaps.get(conversation) ?? 0) === generation)
|
|
445
557
|
behind.delete(conversation);
|
|
446
|
-
|
|
447
|
-
|
|
558
|
+
await slide(local, conversation, arriving);
|
|
559
|
+
if (arriving.length > 0 || sealing || whole) {
|
|
560
|
+
deps.publish(conversationChangeSet(changesOf(conversation, arriving), [conversation]));
|
|
448
561
|
}
|
|
449
562
|
});
|
|
450
563
|
}
|
|
@@ -457,7 +570,7 @@ export function createConversationStore(deps) {
|
|
|
457
570
|
if (held === undefined)
|
|
458
571
|
return;
|
|
459
572
|
setLive(conversation, { ...QUIET, turn: held.turn });
|
|
460
|
-
emit(conversation
|
|
573
|
+
emit(conversation);
|
|
461
574
|
return;
|
|
462
575
|
}
|
|
463
576
|
if (event.kind === 'turn') {
|
|
@@ -490,22 +603,126 @@ export function createConversationStore(deps) {
|
|
|
490
603
|
if (sealTurns(conversation, new Set([arriving.turn])))
|
|
491
604
|
emit(conversation);
|
|
492
605
|
}
|
|
606
|
+
async function kept(local, conversation, following) {
|
|
607
|
+
if (following === null || empty(following)) {
|
|
608
|
+
await local.dropChatDraft(conversation);
|
|
609
|
+
drafts.delete(conversation);
|
|
610
|
+
}
|
|
611
|
+
else {
|
|
612
|
+
await local.keepChatDrafts([following]);
|
|
613
|
+
drafts.set(conversation, following);
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
async function sealed(local, conversation, turns) {
|
|
617
|
+
const held = drafts.get(conversation);
|
|
618
|
+
const submitted = held?.submitted ?? [];
|
|
619
|
+
const left = submitted.filter((one) => one.turn === null || !turns.has(one.turn));
|
|
620
|
+
if (held === undefined || left.length === submitted.length)
|
|
621
|
+
return false;
|
|
622
|
+
await kept(local, conversation, withSubmitted(held, left));
|
|
623
|
+
return true;
|
|
624
|
+
}
|
|
625
|
+
function moved(agent, operation, to) {
|
|
626
|
+
const from = newChat(agent);
|
|
627
|
+
return deps.durably(async (local) => {
|
|
628
|
+
const held = drafts.get(from);
|
|
629
|
+
const earlier = (held?.moves ?? []).filter((one) => !deps.expired(one));
|
|
630
|
+
if (earlier.includes(operation))
|
|
631
|
+
return;
|
|
632
|
+
const blank = {
|
|
633
|
+
conversation: from,
|
|
634
|
+
text: '',
|
|
635
|
+
files: [],
|
|
636
|
+
revision: chatDraftRevision(),
|
|
637
|
+
};
|
|
638
|
+
const { moves: _answered, ...unsent } = held ?? blank;
|
|
639
|
+
const taken = drafts.has(to) || empty(unsent) ? [] : [{ ...unsent, conversation: to }];
|
|
640
|
+
const left = { ...(taken.length === 0 ? unsent : blank), moves: [...earlier, operation] };
|
|
641
|
+
await local.keepChatDrafts([left, ...taken]);
|
|
642
|
+
drafts.set(from, left);
|
|
643
|
+
for (const one of taken)
|
|
644
|
+
drafts.set(to, one);
|
|
645
|
+
deps.publish(conversationChangeSet([], [from, to]));
|
|
646
|
+
});
|
|
647
|
+
}
|
|
493
648
|
function withDraft(conversation, next) {
|
|
494
649
|
return deps.durably(async (local) => {
|
|
495
|
-
const following = next(drafts.get(conversation));
|
|
650
|
+
const following = await next(drafts.get(conversation), local);
|
|
496
651
|
if (following === undefined)
|
|
497
652
|
return;
|
|
498
|
-
|
|
499
|
-
await local.dropChatDraft(conversation);
|
|
500
|
-
drafts.delete(conversation);
|
|
501
|
-
}
|
|
502
|
-
else {
|
|
503
|
-
await local.keepChatDraft(following);
|
|
504
|
-
drafts.set(conversation, following);
|
|
505
|
-
}
|
|
653
|
+
await kept(local, conversation, following);
|
|
506
654
|
deps.publish(conversationChangeSet([], [conversation]));
|
|
507
655
|
});
|
|
508
656
|
}
|
|
657
|
+
async function submitted(conversation, operation, draft) {
|
|
658
|
+
let submission;
|
|
659
|
+
let earlier = false;
|
|
660
|
+
await withDraft(conversation, (current) => {
|
|
661
|
+
submission = current?.submitted?.find((one) => one.operation === operation);
|
|
662
|
+
earlier = submission !== undefined;
|
|
663
|
+
if (submission !== undefined)
|
|
664
|
+
return undefined;
|
|
665
|
+
const said = draft ?? current;
|
|
666
|
+
submission = {
|
|
667
|
+
operation,
|
|
668
|
+
text: said?.text ?? '',
|
|
669
|
+
files: [...(said?.files ?? [])],
|
|
670
|
+
turn: null,
|
|
671
|
+
};
|
|
672
|
+
return {
|
|
673
|
+
conversation,
|
|
674
|
+
...(draft === undefined || current === undefined
|
|
675
|
+
? { text: '', files: [], revision: chatDraftRevision() }
|
|
676
|
+
: { text: current.text, files: current.files, revision: current.revision }),
|
|
677
|
+
submitted: [...(current?.submitted ?? []), submission],
|
|
678
|
+
};
|
|
679
|
+
});
|
|
680
|
+
return { submission: submission, earlier };
|
|
681
|
+
}
|
|
682
|
+
function forgotten(conversation, submission) {
|
|
683
|
+
return withDraft(conversation, (current) => {
|
|
684
|
+
const held = current?.submitted ?? [];
|
|
685
|
+
if (current === undefined || !held.includes(submission))
|
|
686
|
+
return undefined;
|
|
687
|
+
return withSubmitted(current, held.filter((one) => one !== submission));
|
|
688
|
+
});
|
|
689
|
+
}
|
|
690
|
+
async function dispatched(conversation, submission) {
|
|
691
|
+
const dispatch = {};
|
|
692
|
+
sending.set(submission.operation, dispatch);
|
|
693
|
+
try {
|
|
694
|
+
const files = await Promise.all(submission.files.map(async (file) => ({ ...(await declaredFile(file)), blob: file.blob })));
|
|
695
|
+
const result = await invoke('conversation.send', conversation, {
|
|
696
|
+
text: submission.text,
|
|
697
|
+
files,
|
|
698
|
+
operation_id: submission.operation,
|
|
699
|
+
});
|
|
700
|
+
if (result.kind === 'revoked-authorization')
|
|
701
|
+
return result;
|
|
702
|
+
if (result.kind !== 'action' && result.retryable) {
|
|
703
|
+
deps.unanswered();
|
|
704
|
+
return result;
|
|
705
|
+
}
|
|
706
|
+
const turn = answered(result, 'turn');
|
|
707
|
+
await withDraft(conversation, async (current, local) => {
|
|
708
|
+
const held = current?.submitted?.find((one) => one.operation === submission.operation);
|
|
709
|
+
if (current === undefined || held === undefined || held.turn !== null)
|
|
710
|
+
return undefined;
|
|
711
|
+
const others = current.submitted.filter((one) => one !== held);
|
|
712
|
+
if (result.kind !== 'action')
|
|
713
|
+
return returned(current, held, others);
|
|
714
|
+
const arrived = turn !== null && (await local.turnArrived(conversation, turn));
|
|
715
|
+
return withSubmitted(current, turn === null || arrived
|
|
716
|
+
? others
|
|
717
|
+
: current.submitted.map((one) => (one === held ? { ...held, turn } : one)));
|
|
718
|
+
});
|
|
719
|
+
return result;
|
|
720
|
+
}
|
|
721
|
+
finally {
|
|
722
|
+
if (sending.get(submission.operation) === dispatch)
|
|
723
|
+
sending.delete(submission.operation);
|
|
724
|
+
}
|
|
725
|
+
}
|
|
509
726
|
function invoke(name, conversation, extra = {}) {
|
|
510
727
|
return deps.call(name, { conversation, ...extra });
|
|
511
728
|
}
|
|
@@ -515,47 +732,39 @@ export function createConversationStore(deps) {
|
|
|
515
732
|
return entries();
|
|
516
733
|
},
|
|
517
734
|
async read(conversation, options = {}) {
|
|
518
|
-
const
|
|
519
|
-
return deps.durably(() =>
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
735
|
+
const asked = askedOf(options);
|
|
736
|
+
return deps.durably(async (local) => {
|
|
737
|
+
const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before);
|
|
738
|
+
const total = asked.total ? await local.messageCount(conversation) : null;
|
|
739
|
+
return structuredClone(viewOf(conversation, messages, frameOf(asked, messages, more, total)));
|
|
740
|
+
});
|
|
523
741
|
},
|
|
524
|
-
async
|
|
525
|
-
const
|
|
526
|
-
const
|
|
527
|
-
if (
|
|
528
|
-
sending.set(conversation, pending);
|
|
529
|
-
deps.publish(conversationChangeSet([], [conversation]));
|
|
530
|
-
}
|
|
531
|
-
try {
|
|
532
|
-
const files = await Promise.all((draft?.files ?? []).map(async (file) => ({
|
|
533
|
-
...(await declaredFile(file)),
|
|
534
|
-
blob: file.blob,
|
|
535
|
-
})));
|
|
536
|
-
const result = await invoke('conversation.send', conversation, {
|
|
537
|
-
text: draft?.text ?? '',
|
|
538
|
-
files,
|
|
539
|
-
});
|
|
540
|
-
const turn = turnOf(result);
|
|
541
|
-
if (turn === null || draft === undefined)
|
|
542
|
-
return result;
|
|
543
|
-
await withDraft(conversation, (current) => {
|
|
544
|
-
if (current !== draft)
|
|
545
|
-
return undefined;
|
|
546
|
-
const sealed = (messages.get(conversation) ?? []).some((message) => message.turn === turn);
|
|
547
|
-
return sealed ? null : { ...draft, turn };
|
|
548
|
-
});
|
|
742
|
+
async create(input) {
|
|
743
|
+
const result = await deps.call('conversation.create', input);
|
|
744
|
+
const conversation = answered(result, 'conversation');
|
|
745
|
+
if (conversation === null)
|
|
549
746
|
return result;
|
|
747
|
+
if (!listing.has(conversation))
|
|
748
|
+
creations.add(conversation);
|
|
749
|
+
const creation = creationOf(input);
|
|
750
|
+
if (creation !== null)
|
|
751
|
+
await moved(creation.agent, creation.operation, conversation);
|
|
752
|
+
return result;
|
|
753
|
+
},
|
|
754
|
+
async send(conversation, operation = changeBatchIdentity(), draft) {
|
|
755
|
+
if (deps.online()) {
|
|
756
|
+
const { submission, earlier } = await submitted(conversation, operation, draft);
|
|
757
|
+
if (!earlier || !deps.expired(operation))
|
|
758
|
+
return dispatched(conversation, submission);
|
|
759
|
+
await forgotten(conversation, submission);
|
|
760
|
+
return EXPIRED;
|
|
550
761
|
}
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
deps.publish(conversationChangeSet([], [conversation]));
|
|
556
|
-
}
|
|
557
|
-
}
|
|
762
|
+
if (draft !== undefined) {
|
|
763
|
+
await withDraft(conversation, (current) => current?.submitted?.some((one) => one.operation === operation) === true
|
|
764
|
+
? undefined
|
|
765
|
+
: returned(current ?? { conversation, text: '', files: [], revision: '' }, { operation, ...draft, turn: null }, current?.submitted ?? []));
|
|
558
766
|
}
|
|
767
|
+
return OFFLINE;
|
|
559
768
|
},
|
|
560
769
|
answer(conversation, answer) {
|
|
561
770
|
return invoke('conversation.answer', conversation, { answer });
|
|
@@ -570,7 +779,12 @@ export function createConversationStore(deps) {
|
|
|
570
779
|
const result = await invoke('conversation.cancel', conversation, { turn });
|
|
571
780
|
if (result.kind !== 'action')
|
|
572
781
|
return result;
|
|
573
|
-
await withDraft(conversation, (current) =>
|
|
782
|
+
await withDraft(conversation, (current) => {
|
|
783
|
+
const held = current?.submitted?.find((one) => one.turn === turn);
|
|
784
|
+
if (current === undefined || held === undefined)
|
|
785
|
+
return undefined;
|
|
786
|
+
return returned(current, held, current.submitted.filter((one) => one !== held));
|
|
787
|
+
});
|
|
574
788
|
return result;
|
|
575
789
|
},
|
|
576
790
|
delete(conversation) {
|
|
@@ -579,33 +793,52 @@ export function createConversationStore(deps) {
|
|
|
579
793
|
};
|
|
580
794
|
const chatDraft = {
|
|
581
795
|
write(conversation, draft, revision = chatDraftRevision()) {
|
|
582
|
-
return withDraft(conversation, () => ({
|
|
796
|
+
return withDraft(conversation, (current) => ({
|
|
583
797
|
conversation,
|
|
584
798
|
text: draft.text,
|
|
585
799
|
files: [...draft.files],
|
|
586
|
-
turn: null,
|
|
587
800
|
revision,
|
|
801
|
+
...(current === undefined ? {} : pending(current)),
|
|
802
|
+
}));
|
|
803
|
+
},
|
|
804
|
+
prepend(conversation, draft) {
|
|
805
|
+
return withDraft(conversation, (current) => ({
|
|
806
|
+
conversation,
|
|
807
|
+
...(current === undefined
|
|
808
|
+
? { text: draft.text, files: [...draft.files] }
|
|
809
|
+
: prepended(current, draft)),
|
|
810
|
+
revision: chatDraftRevision(),
|
|
811
|
+
...(current === undefined ? {} : pending(current)),
|
|
588
812
|
}));
|
|
589
813
|
},
|
|
590
814
|
discard(conversation) {
|
|
591
|
-
return withDraft(conversation, (current) =>
|
|
815
|
+
return withDraft(conversation, (current) => current === undefined
|
|
816
|
+
? undefined
|
|
817
|
+
: {
|
|
818
|
+
conversation,
|
|
819
|
+
text: '',
|
|
820
|
+
files: [],
|
|
821
|
+
revision: chatDraftRevision(),
|
|
822
|
+
...pending(current),
|
|
823
|
+
});
|
|
592
824
|
},
|
|
593
825
|
};
|
|
594
826
|
return {
|
|
595
827
|
surface,
|
|
596
828
|
chatDraft,
|
|
597
|
-
hydrate(held,
|
|
598
|
-
|
|
829
|
+
hydrate(held, kept) {
|
|
830
|
+
stored.clear();
|
|
599
831
|
reads.clear();
|
|
600
|
-
|
|
601
|
-
view.messages = null;
|
|
832
|
+
partials.clear();
|
|
602
833
|
for (const entry of held) {
|
|
603
|
-
|
|
834
|
+
stored.add(entry.conversation);
|
|
604
835
|
if (entry.read !== undefined)
|
|
605
836
|
reads.set(entry.conversation, entry.read);
|
|
837
|
+
if (entry.partial)
|
|
838
|
+
partials.add(entry.conversation);
|
|
606
839
|
}
|
|
607
840
|
drafts.clear();
|
|
608
|
-
for (const record of
|
|
841
|
+
for (const record of kept)
|
|
609
842
|
drafts.set(record.conversation, record);
|
|
610
843
|
},
|
|
611
844
|
async reconcile() {
|
|
@@ -613,8 +846,10 @@ export function createConversationStore(deps) {
|
|
|
613
846
|
return;
|
|
614
847
|
const named = new Set(listing.keys());
|
|
615
848
|
const leaving = [
|
|
616
|
-
...new Set([...replicated, ...
|
|
617
|
-
].filter((conversation) => !named.has(conversation)
|
|
849
|
+
...new Set([...replicated, ...stored, ...reads.keys(), ...drafts.keys()]),
|
|
850
|
+
].filter((conversation) => !named.has(conversation) &&
|
|
851
|
+
!creations.has(conversation) &&
|
|
852
|
+
(!conversation.startsWith(NEW_CHAT) || retiredChat(conversation)));
|
|
618
853
|
for (const conversation of named) {
|
|
619
854
|
if (!replicated.has(conversation))
|
|
620
855
|
behind.add(conversation);
|
|
@@ -625,41 +860,93 @@ export function createConversationStore(deps) {
|
|
|
625
860
|
await remove(leaving);
|
|
626
861
|
},
|
|
627
862
|
async catchUp(progress) {
|
|
628
|
-
const
|
|
629
|
-
|
|
630
|
-
|
|
863
|
+
const owed = (conversation) => behind.has(conversation) && replicated.has(conversation);
|
|
864
|
+
const read = async (conversations) => {
|
|
865
|
+
if (conversations.length === 0 || deps.closed())
|
|
866
|
+
return;
|
|
867
|
+
const failed = await caughtUp(conversations);
|
|
631
868
|
if (failed?.kind === 'transport-uncertainty')
|
|
632
869
|
progress.reachable = false;
|
|
633
|
-
}
|
|
870
|
+
};
|
|
871
|
+
const asking = [...behind].filter(owed).sort();
|
|
872
|
+
const awaited = asking.filter((conversation) => catching.has(conversation));
|
|
873
|
+
await Promise.all([
|
|
874
|
+
read(asking.filter((conversation) => !catching.has(conversation))),
|
|
875
|
+
Promise.all(awaited.map((conversation) => catching.get(conversation))).then(() => read(awaited.filter((conversation) => owed(conversation) && !catching.has(conversation)))),
|
|
876
|
+
]);
|
|
634
877
|
if (behind.size > 0)
|
|
635
878
|
progress.complete = false;
|
|
636
879
|
},
|
|
880
|
+
async resend(progress) {
|
|
881
|
+
for (const [conversation, held] of [...drafts]) {
|
|
882
|
+
for (const submission of held.submitted ?? []) {
|
|
883
|
+
if (submission.turn !== null || sending.has(submission.operation))
|
|
884
|
+
continue;
|
|
885
|
+
if (deps.closed())
|
|
886
|
+
return;
|
|
887
|
+
if (deps.expired(submission.operation)) {
|
|
888
|
+
await forgotten(conversation, submission);
|
|
889
|
+
continue;
|
|
890
|
+
}
|
|
891
|
+
const result = await dispatched(conversation, submission);
|
|
892
|
+
if (result.kind === 'revoked-authorization')
|
|
893
|
+
return;
|
|
894
|
+
if (result.kind === 'transport-uncertainty') {
|
|
895
|
+
progress.reachable = false;
|
|
896
|
+
return;
|
|
897
|
+
}
|
|
898
|
+
if (result.kind !== 'action' && result.retryable) {
|
|
899
|
+
progress.complete = false;
|
|
900
|
+
return;
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
},
|
|
637
905
|
current() {
|
|
638
906
|
return behind.size === 0;
|
|
639
907
|
},
|
|
908
|
+
known() {
|
|
909
|
+
return listKnown;
|
|
910
|
+
},
|
|
640
911
|
reconnected() {
|
|
641
912
|
for (const conversation of replicated)
|
|
642
913
|
gap(conversation);
|
|
643
914
|
},
|
|
915
|
+
reopened() {
|
|
916
|
+
sending.clear();
|
|
917
|
+
},
|
|
918
|
+
lost() {
|
|
919
|
+
catching.clear();
|
|
920
|
+
},
|
|
644
921
|
watch(conversation, options, deliver) {
|
|
645
|
-
const
|
|
646
|
-
const key = JSON.stringify([conversation, limit ?? null]);
|
|
922
|
+
const asked = askedOf(options);
|
|
923
|
+
const key = JSON.stringify([conversation, asked.limit, asked.after ?? null, asked.total]);
|
|
647
924
|
const view = views.get(key) ?? {
|
|
648
925
|
conversation,
|
|
649
|
-
|
|
926
|
+
asked,
|
|
650
927
|
watchers: new Set(),
|
|
651
|
-
|
|
928
|
+
window: null,
|
|
929
|
+
more: false,
|
|
930
|
+
total: null,
|
|
652
931
|
};
|
|
653
932
|
views.set(key, view);
|
|
654
933
|
const watcher = { notify: deliver, ready: false };
|
|
655
934
|
view.watchers.add(watcher);
|
|
656
935
|
void deps
|
|
657
|
-
.durably(() =>
|
|
936
|
+
.durably(async (local) => {
|
|
937
|
+
if (view.window !== null)
|
|
938
|
+
return;
|
|
939
|
+
const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before);
|
|
940
|
+
view.window = messages;
|
|
941
|
+
view.more = more;
|
|
942
|
+
view.total = asked.total ? await local.messageCount(conversation) : null;
|
|
943
|
+
})
|
|
658
944
|
.then(() => {
|
|
659
945
|
if (deps.closed() || !view.watchers.has(watcher))
|
|
660
946
|
return;
|
|
661
947
|
watcher.ready = true;
|
|
662
|
-
|
|
948
|
+
const window = view.window ?? [];
|
|
949
|
+
watcher.notify(viewOf(conversation, window, sharedFrame(view, window)));
|
|
663
950
|
})
|
|
664
951
|
.catch(() => { });
|
|
665
952
|
return () => {
|
|
@@ -706,12 +993,15 @@ export function createConversationStore(deps) {
|
|
|
706
993
|
if (event.kind === 'list' || event.kind === 'listed' || event.kind === 'unlisted') {
|
|
707
994
|
if (event.kind === 'list') {
|
|
708
995
|
listing.clear();
|
|
709
|
-
for (const entry of event.entries)
|
|
996
|
+
for (const entry of event.entries) {
|
|
710
997
|
listing.set(entry.conversation, { ...entry });
|
|
998
|
+
creations.delete(entry.conversation);
|
|
999
|
+
}
|
|
711
1000
|
listKnown = true;
|
|
712
1001
|
}
|
|
713
1002
|
else if (event.kind === 'listed') {
|
|
714
1003
|
listing.set(event.entry.conversation, { ...event.entry });
|
|
1004
|
+
creations.delete(event.entry.conversation);
|
|
715
1005
|
}
|
|
716
1006
|
else {
|
|
717
1007
|
listing.delete(event.conversation);
|
|
@@ -725,12 +1015,11 @@ export function createConversationStore(deps) {
|
|
|
725
1015
|
heard(event);
|
|
726
1016
|
return false;
|
|
727
1017
|
},
|
|
728
|
-
|
|
729
|
-
return [...
|
|
1018
|
+
searchable() {
|
|
1019
|
+
return [...(listKnown ? listing.keys() : stored)].sort();
|
|
730
1020
|
},
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
return held === undefined ? null : { conversation, identity, text: held.text };
|
|
1021
|
+
acquired(conversation) {
|
|
1022
|
+
return reads.has(conversation) && !partials.has(conversation);
|
|
734
1023
|
},
|
|
735
1024
|
close() {
|
|
736
1025
|
views.clear();
|