@agentshouse/mdruntime 0.1.0-c4ca57eefb98a → 0.1.0-c4d7cf2ec26a6
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/README.md +2 -2
- package/dist/acceptance.d.ts +6 -0
- package/dist/acceptance.d.ts.map +1 -0
- package/dist/acceptance.js +386 -0
- package/dist/actions.d.ts +20 -0
- package/dist/actions.d.ts.map +1 -0
- package/dist/actions.js +251 -0
- package/dist/adapter.d.ts +86 -67
- package/dist/adapter.d.ts.map +1 -1
- package/dist/attached-model.d.ts +3 -10
- package/dist/attached-model.d.ts.map +1 -1
- package/dist/attached-model.js +21 -56
- package/dist/bootstrap.d.ts +11 -10
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +219 -82
- package/dist/cell.d.ts +6 -0
- package/dist/cell.d.ts.map +1 -0
- package/dist/cell.js +9 -0
- package/dist/chain.d.ts +2 -1
- package/dist/chain.d.ts.map +1 -1
- package/dist/chain.js +3 -7
- package/dist/change.d.ts +1 -0
- package/dist/change.d.ts.map +1 -1
- package/dist/change.js +4 -2
- package/dist/chat-types.d.ts +5 -0
- package/dist/chat-types.d.ts.map +1 -0
- package/dist/chat-types.js +13 -0
- package/dist/client.d.ts +21 -6
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +3 -1
- package/dist/collection.d.ts +2 -4
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +1 -17
- package/dist/compile.d.ts +6 -6
- package/dist/compile.d.ts.map +1 -1
- package/dist/compile.js +267 -323
- package/dist/connection.d.ts +5 -143
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +102 -5311
- package/dist/context.d.ts +2 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +7 -0
- package/dist/conversation-live.d.ts +136 -0
- package/dist/conversation-live.d.ts.map +1 -0
- package/dist/conversation-live.js +248 -0
- package/dist/conversation-row.d.ts +33 -0
- package/dist/conversation-row.d.ts.map +1 -0
- package/dist/conversation-row.js +53 -0
- package/dist/conversation.d.ts +41 -45
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +860 -869
- package/dist/discovery.d.ts +10 -0
- package/dist/discovery.d.ts.map +1 -0
- package/dist/discovery.js +175 -0
- package/dist/durability.d.ts +15 -1
- package/dist/durability.d.ts.map +1 -1
- package/dist/entry.d.ts +2 -2
- package/dist/entry.d.ts.map +1 -1
- package/dist/entry.js +17 -7
- package/dist/figures.d.ts +49 -0
- package/dist/figures.d.ts.map +1 -0
- package/dist/figures.js +197 -0
- package/dist/holdings.d.ts +29 -0
- package/dist/holdings.d.ts.map +1 -0
- package/dist/holdings.js +95 -0
- package/dist/host.d.ts +4 -7
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +29 -31
- package/dist/identity.d.ts +0 -1
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +0 -3
- package/dist/index.d.ts +26 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -1
- package/dist/install.d.ts +56 -0
- package/dist/install.d.ts.map +1 -0
- package/dist/install.js +554 -0
- package/dist/job.d.ts +33 -0
- package/dist/job.d.ts.map +1 -0
- package/dist/job.js +54 -0
- package/dist/lifecycle.d.ts +7 -0
- package/dist/lifecycle.d.ts.map +1 -0
- package/dist/lifecycle.js +268 -0
- package/dist/listing.d.ts +53 -0
- package/dist/listing.d.ts.map +1 -0
- package/dist/listing.js +176 -0
- package/dist/live.d.ts +5 -4
- package/dist/live.d.ts.map +1 -1
- package/dist/live.js +62 -23
- package/dist/locks.d.ts +9 -0
- package/dist/locks.d.ts.map +1 -0
- package/dist/locks.js +61 -0
- package/dist/log.d.ts +1 -0
- package/dist/log.d.ts.map +1 -1
- package/dist/lost.d.ts +5 -0
- package/dist/lost.d.ts.map +1 -0
- package/dist/lost.js +61 -0
- package/dist/media.d.ts +1 -1
- package/dist/media.d.ts.map +1 -1
- package/dist/media.js +19 -9
- package/dist/memory.d.ts +11 -4
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +172 -82
- package/dist/messages.d.ts +27 -3
- package/dist/messages.d.ts.map +1 -1
- package/dist/messages.js +97 -19
- package/dist/model.d.ts +2 -10
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +0 -18
- package/dist/models.d.ts +14 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.js +195 -0
- package/dist/observation.d.ts +2 -2
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +14 -14
- package/dist/opened.js +3 -3
- package/dist/opening.d.ts +4 -0
- package/dist/opening.d.ts.map +1 -0
- package/dist/opening.js +57 -0
- package/dist/outcome.d.ts +4 -4
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +3 -0
- package/dist/overlay.d.ts +1 -0
- package/dist/overlay.d.ts.map +1 -1
- package/dist/overlay.js +11 -0
- package/dist/parts.d.ts +60 -0
- package/dist/parts.d.ts.map +1 -0
- package/dist/parts.js +126 -0
- package/dist/paths.d.ts +1 -0
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +22 -0
- package/dist/preparation.d.ts +20 -0
- package/dist/preparation.d.ts.map +1 -0
- package/dist/preparation.js +226 -0
- package/dist/queries.d.ts +16 -0
- package/dist/queries.d.ts.map +1 -0
- package/dist/queries.js +272 -0
- package/dist/query.d.ts +14 -209
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +3 -505
- package/dist/questions.d.ts +34 -0
- package/dist/questions.d.ts.map +1 -0
- package/dist/questions.js +122 -0
- package/dist/reach.d.ts +15 -13
- package/dist/reach.d.ts.map +1 -1
- package/dist/reach.js +183 -137
- package/dist/react.d.ts +10 -2
- package/dist/react.d.ts.map +1 -1
- package/dist/read.d.ts +7 -0
- package/dist/read.d.ts.map +1 -0
- package/dist/read.js +17 -0
- package/dist/readiness.d.ts +2 -16
- package/dist/readiness.d.ts.map +1 -1
- package/dist/readiness.js +4 -10
- package/dist/reads.d.ts +36 -10
- package/dist/reads.d.ts.map +1 -1
- package/dist/reads.js +158 -78
- package/dist/recovery.d.ts +1 -0
- package/dist/recovery.d.ts.map +1 -1
- package/dist/recovery.js +35 -1
- package/dist/refresh.d.ts +7 -5
- package/dist/refresh.d.ts.map +1 -1
- package/dist/refresh.js +33 -5
- package/dist/refusal.d.ts +5 -0
- package/dist/refusal.d.ts.map +1 -0
- package/dist/refusal.js +56 -0
- package/dist/replica.d.ts +9 -9
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +53 -21
- package/dist/room-draft.d.ts +4 -3
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +67 -54
- package/dist/room-drafts.d.ts +24 -0
- package/dist/room-drafts.d.ts.map +1 -0
- package/dist/room-drafts.js +217 -0
- package/dist/rows.d.ts +14 -21
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +48 -184
- package/dist/runtime.d.ts +43 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +1 -0
- package/dist/save.d.ts +5 -1
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +21 -20
- package/dist/saves.d.ts +70 -0
- package/dist/saves.d.ts.map +1 -0
- package/dist/saves.js +1005 -0
- package/dist/search.d.ts +12 -63
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +9 -52
- package/dist/seed.d.ts +11 -2
- package/dist/seed.d.ts.map +1 -1
- package/dist/seed.js +71 -0
- package/dist/shared.d.ts +1 -7
- package/dist/shared.d.ts.map +1 -1
- package/dist/shared.js +13 -105
- package/dist/shown.d.ts +31 -0
- package/dist/shown.d.ts.map +1 -0
- package/dist/shown.js +292 -0
- package/dist/socket/admission.d.ts +7 -0
- package/dist/socket/admission.d.ts.map +1 -0
- package/dist/socket/admission.js +39 -0
- package/dist/socket/answers.d.ts +6 -0
- package/dist/socket/answers.d.ts.map +1 -0
- package/dist/socket/answers.js +20 -0
- package/dist/socket/lanes.d.ts +20 -0
- package/dist/socket/lanes.d.ts.map +1 -0
- package/dist/socket/lanes.js +77 -0
- package/dist/socket/reads.d.ts +10 -0
- package/dist/socket/reads.d.ts.map +1 -0
- package/dist/socket/reads.js +31 -0
- package/dist/socket/rooms.d.ts +44 -0
- package/dist/socket/rooms.d.ts.map +1 -0
- package/dist/socket/rooms.js +90 -0
- package/dist/socket/session.d.ts +39 -0
- package/dist/socket/session.d.ts.map +1 -0
- package/dist/socket/session.js +23 -0
- package/dist/socket.d.ts +70 -0
- package/dist/socket.d.ts.map +1 -0
- package/dist/socket.js +175 -0
- package/dist/source.d.ts +61 -0
- package/dist/source.d.ts.map +1 -0
- package/dist/source.js +82 -0
- package/dist/status.d.ts +0 -6
- package/dist/status.d.ts.map +1 -1
- package/dist/status.js +0 -6
- package/dist/store/fences.d.ts +16 -0
- package/dist/store/fences.d.ts.map +1 -0
- package/dist/store/fences.js +26 -0
- package/dist/store/installs.d.ts +30 -0
- package/dist/store/installs.d.ts.map +1 -0
- package/dist/store/installs.js +34 -0
- package/dist/store/readiness.d.ts +10 -0
- package/dist/store/readiness.d.ts.map +1 -0
- package/dist/store/readiness.js +52 -0
- package/dist/store/records.d.ts +212 -0
- package/dist/store/records.d.ts.map +1 -0
- package/dist/store/records.js +431 -0
- package/dist/store/room-held.d.ts +8 -0
- package/dist/store/room-held.d.ts.map +1 -0
- package/dist/store/room-held.js +17 -0
- package/dist/store/space.d.ts +11 -0
- package/dist/store/space.d.ts.map +1 -0
- package/dist/store/space.js +41 -0
- package/dist/store/watchers.d.ts +17 -0
- package/dist/store/watchers.d.ts.map +1 -0
- package/dist/store/watchers.js +56 -0
- package/dist/store/writes.d.ts +24 -0
- package/dist/store/writes.d.ts.map +1 -0
- package/dist/store/writes.js +125 -0
- package/dist/store.d.ts +107 -157
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +143 -395
- package/dist/stored.d.ts +59 -38
- package/dist/stored.d.ts.map +1 -1
- package/dist/stored.js +285 -205
- package/dist/surface.d.ts +88 -0
- package/dist/surface.d.ts.map +1 -0
- package/dist/surface.js +223 -0
- package/dist/sync.d.ts +19 -0
- package/dist/sync.d.ts.map +1 -0
- package/dist/sync.js +532 -0
- package/dist/tables.d.ts +120 -56
- package/dist/tables.d.ts.map +1 -1
- package/dist/tables.js +217 -98
- package/dist/tallies.d.ts +18 -0
- package/dist/tallies.d.ts.map +1 -0
- package/dist/tallies.js +129 -0
- package/dist/transfer.d.ts +0 -5
- package/dist/transfer.d.ts.map +1 -1
- package/dist/validation.d.ts +3 -4
- package/dist/validation.d.ts.map +1 -1
- package/dist/validation.js +42 -100
- package/dist/windows.d.ts +1 -12
- package/dist/windows.d.ts.map +1 -1
- package/dist/windows.js +2 -75
- package/dist/wire.d.ts +17 -5
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +7 -1
- package/package.json +2 -2
- package/dist/fence.d.ts +0 -6
- package/dist/fence.d.ts.map +0 -1
- package/dist/fence.js +0 -10
- package/dist/fulltext.d.ts +0 -27
- package/dist/fulltext.d.ts.map +0 -1
- package/dist/fulltext.js +0 -370
- package/dist/words.d.ts +0 -19
- package/dist/words.d.ts.map +0 -1
- package/dist/words.js +0 -156
package/dist/conversation.js
CHANGED
|
@@ -1,28 +1,15 @@
|
|
|
1
|
+
import { cursorOf, decodedCursor } from '@agentshouse/mdmodel';
|
|
1
2
|
import { conversationChangeSet } from './change-set.js';
|
|
3
|
+
import { FACTS, QUIET, } from './conversation-live.js';
|
|
4
|
+
import { heldFacts } from './conversation-row.js';
|
|
2
5
|
import { runtimeError } from './failure.js';
|
|
3
6
|
import { refusedForSpace } from './durability.js';
|
|
4
7
|
import { changeBatchIdentity, chatDraftRevision } from './identity.js';
|
|
5
8
|
import { OFFLINE } from './outcome.js';
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
process: null,
|
|
11
|
-
commands: [],
|
|
12
|
-
options: [],
|
|
13
|
-
starting: null,
|
|
14
|
-
link: null,
|
|
15
|
-
deliveryUnknown: [],
|
|
16
|
-
};
|
|
17
|
-
const FACTS = [
|
|
18
|
-
'questions',
|
|
19
|
-
'process',
|
|
20
|
-
'commands',
|
|
21
|
-
'options',
|
|
22
|
-
'starting',
|
|
23
|
-
'link',
|
|
24
|
-
'deliveryUnknown',
|
|
25
|
-
];
|
|
9
|
+
import { jobsOf, runningLine } from './parts.js';
|
|
10
|
+
import { openQuestion, questionAsked } from './questions.js';
|
|
11
|
+
import { untilAnswered } from './recovery.js';
|
|
12
|
+
import { capped, FROM_START, messageOrder, precedes } from './windows.js';
|
|
26
13
|
export function unheldView(conversation) {
|
|
27
14
|
return {
|
|
28
15
|
conversation,
|
|
@@ -30,12 +17,15 @@ export function unheldView(conversation) {
|
|
|
30
17
|
turn: null,
|
|
31
18
|
questions: [],
|
|
32
19
|
process: null,
|
|
20
|
+
processEnded: null,
|
|
33
21
|
commands: [],
|
|
34
22
|
options: [],
|
|
35
23
|
starting: null,
|
|
24
|
+
subagents: [],
|
|
36
25
|
link: null,
|
|
37
26
|
deliveryUnknown: [],
|
|
38
27
|
chatDraft: null,
|
|
28
|
+
jobs: [],
|
|
39
29
|
};
|
|
40
30
|
}
|
|
41
31
|
export const SESSION_STARTED = 'Session started';
|
|
@@ -47,10 +37,8 @@ export function newChat(agent) {
|
|
|
47
37
|
function creationOf(input) {
|
|
48
38
|
if (typeof input !== 'object' || input === null)
|
|
49
39
|
return null;
|
|
50
|
-
const { agent_id: agent, operation_id: operation
|
|
51
|
-
return typeof agent === 'string' && typeof operation === 'string'
|
|
52
|
-
? { agent, operation }
|
|
53
|
-
: null;
|
|
40
|
+
const { agent_id: agent, operation_id: operation } = input;
|
|
41
|
+
return typeof agent === 'string' && typeof operation === 'string' ? { agent, operation } : null;
|
|
54
42
|
}
|
|
55
43
|
function startedTurn(turn, operation) {
|
|
56
44
|
if (operation.op === 'begin')
|
|
@@ -209,7 +197,14 @@ function askedOf(options) {
|
|
|
209
197
|
}
|
|
210
198
|
return { limit: capped(limit), after, before: beforeOf(after), total: total === true };
|
|
211
199
|
}
|
|
212
|
-
|
|
200
|
+
function phraseOf(text) {
|
|
201
|
+
if (text === undefined)
|
|
202
|
+
return null;
|
|
203
|
+
if (typeof text !== 'string' || text === '') {
|
|
204
|
+
throw runtimeError('invalid-text', 'a conversation is found by a phrase');
|
|
205
|
+
}
|
|
206
|
+
return text;
|
|
207
|
+
}
|
|
213
208
|
const DURABLE_WRITE = {
|
|
214
209
|
kind: 'rejected',
|
|
215
210
|
code: 'durable-write',
|
|
@@ -242,702 +237,776 @@ function returned(draft, submission, others) {
|
|
|
242
237
|
revision: chatDraftRevision(),
|
|
243
238
|
}, others);
|
|
244
239
|
}
|
|
245
|
-
|
|
246
|
-
const
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
return
|
|
240
|
+
function viewOf(alive, conversation, held, frame, draft, ended, marked) {
|
|
241
|
+
const facts = alive.liveOf(conversation);
|
|
242
|
+
return {
|
|
243
|
+
conversation,
|
|
244
|
+
messages: held.map(({ identity, turn, text, message }) => ({
|
|
245
|
+
identity,
|
|
246
|
+
turn,
|
|
247
|
+
text,
|
|
248
|
+
message,
|
|
249
|
+
})),
|
|
250
|
+
...frame,
|
|
251
|
+
turn: structuredClone(facts.turn),
|
|
252
|
+
questions: structuredClone(facts.questions),
|
|
253
|
+
process: facts.process === null ? null : { ...facts.process },
|
|
254
|
+
processEnded: ended,
|
|
255
|
+
commands: structuredClone(facts.commands),
|
|
256
|
+
options: structuredClone(facts.options),
|
|
257
|
+
starting: facts.starting,
|
|
258
|
+
subagents: structuredClone(facts.subagents),
|
|
259
|
+
link: facts.link === null ? null : { ...facts.link },
|
|
260
|
+
deliveryUnknown: [...facts.deliveryUnknown],
|
|
261
|
+
chatDraft: draft === null || unwritten(draft) ? null : { text: draft.text, revision: draft.revision },
|
|
262
|
+
jobs: facts.process?.running === false
|
|
263
|
+
? []
|
|
264
|
+
: jobsOf(marked.filter(({ at }) => ended === null || at > ended), facts.turn),
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
function emit(alive, conversation) {
|
|
268
|
+
if (alive.deps.closed())
|
|
269
|
+
return;
|
|
270
|
+
for (const view of alive.views()) {
|
|
271
|
+
const { window, draft, ended, marked } = view.held.now();
|
|
272
|
+
if (view.conversation !== conversation || window === null)
|
|
273
|
+
continue;
|
|
274
|
+
const ready = view.ready();
|
|
275
|
+
if (ready.length === 0)
|
|
276
|
+
continue;
|
|
277
|
+
const value = viewOf(alive, conversation, window, sharedFrame(view, window), draft, ended, marked);
|
|
278
|
+
for (const watcher of ready)
|
|
279
|
+
watcher.notify(value);
|
|
275
280
|
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
}
|
|
281
|
+
}
|
|
282
|
+
function sharedFrame(view, window) {
|
|
283
|
+
const { more, total } = view.held.now();
|
|
284
|
+
return frameOf(view.asked, window, more, total);
|
|
285
|
+
}
|
|
286
|
+
async function slide(alive, local, conversation, arriving) {
|
|
287
|
+
let count;
|
|
288
|
+
let marked;
|
|
289
|
+
for (const view of alive.views()) {
|
|
290
|
+
const { held } = view;
|
|
291
|
+
const { window, total } = held.now();
|
|
292
|
+
if (view.conversation !== conversation || window === null)
|
|
293
|
+
continue;
|
|
294
|
+
if (arriving === null) {
|
|
295
|
+
held.set({ window: [], marked: [], more: false, total: total === null ? null : 0 });
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
const joining = arriving.filter((message) => precedes(message, view.asked.before));
|
|
299
|
+
const next = windowOf(window, joining, view.asked.limit);
|
|
300
|
+
held.set({ window: next.messages, more: held.now().more || next.dropped });
|
|
301
|
+
if (arriving.length > 0) {
|
|
302
|
+
marked ??= await local.jobMarkers(conversation);
|
|
303
|
+
held.set({ marked });
|
|
304
|
+
}
|
|
305
|
+
if (held.now().total !== null && arriving.length > 0) {
|
|
306
|
+
count ??= await local.messageCount(conversation);
|
|
307
|
+
held.set({ total: count });
|
|
308
|
+
}
|
|
300
309
|
}
|
|
301
|
-
|
|
302
|
-
|
|
310
|
+
}
|
|
311
|
+
function stateOf(alive, conversation) {
|
|
312
|
+
const facts = alive.liveOf(conversation);
|
|
313
|
+
if (facts.questions.length > 0)
|
|
314
|
+
return 'asking';
|
|
315
|
+
return facts.turn === null ? 'idle' : 'working';
|
|
316
|
+
}
|
|
317
|
+
function rowOf(alive, conversation) {
|
|
318
|
+
const { turn, questions } = alive.liveOf(conversation);
|
|
319
|
+
return {
|
|
320
|
+
state: stateOf(alive, conversation),
|
|
321
|
+
started: turn?.started ?? null,
|
|
322
|
+
line: turn === null ? null : runningLine(turn.parts),
|
|
323
|
+
question: openQuestion(questions),
|
|
324
|
+
asked: questionAsked(questions),
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
async function entries(alive) {
|
|
328
|
+
const list = await heldList(alive);
|
|
329
|
+
if (list === null)
|
|
330
|
+
return null;
|
|
331
|
+
const held = list.length === 0
|
|
332
|
+
? []
|
|
333
|
+
: await alive.deps.durably((local) => local.conversationRows(list.map(({ conversation }) => conversation)));
|
|
334
|
+
const rows = new Map(held.map((one) => [one.conversation, one]));
|
|
335
|
+
return list.map((entry) => ({
|
|
336
|
+
conversation: entry.conversation,
|
|
337
|
+
agent: { identity: entry.agent, name: entry.agentName, retired: entry.agentRetired },
|
|
338
|
+
name: entry.name,
|
|
339
|
+
created: entry.created,
|
|
340
|
+
finished: entry.finished,
|
|
341
|
+
outcome: entry.outcome,
|
|
342
|
+
waiting: entry.waiting,
|
|
343
|
+
...rowOf(alive, entry.conversation),
|
|
344
|
+
...heldFacts(rows.get(entry.conversation), entry),
|
|
345
|
+
}));
|
|
346
|
+
}
|
|
347
|
+
async function heldList(alive) {
|
|
348
|
+
const stored = (await (await alive.deps.local()).facts()).conversationList;
|
|
349
|
+
if (stored !== null)
|
|
350
|
+
return stored;
|
|
351
|
+
return !alive.known() && alive.deps.stream() !== undefined && alive.deps.online() ? null : [];
|
|
352
|
+
}
|
|
353
|
+
async function endedIn(alive, conversation) {
|
|
354
|
+
const list = await heldList(alive);
|
|
355
|
+
return list?.find((entry) => entry.conversation === conversation)?.processEnded ?? null;
|
|
356
|
+
}
|
|
357
|
+
function listChanged(alive, change) {
|
|
358
|
+
void alive.deps
|
|
359
|
+
.durably(async (local) => {
|
|
360
|
+
const { conversationList, conversationCursor } = await local.facts();
|
|
361
|
+
const kept = change.replacing ? [] : (conversationList ?? []);
|
|
362
|
+
const held = new Map(kept.map((entry) => [entry.conversation, entry]));
|
|
363
|
+
for (const entry of change.set) {
|
|
364
|
+
held.set(entry.conversation, { ...entry });
|
|
365
|
+
alive.created(entry.conversation);
|
|
366
|
+
}
|
|
367
|
+
if (change.gone !== null)
|
|
368
|
+
held.delete(change.gone);
|
|
369
|
+
const list = [...held.values()];
|
|
370
|
+
if (JSON.stringify(list) !== JSON.stringify(conversationList)) {
|
|
371
|
+
await local.fact('conversationList', list);
|
|
372
|
+
}
|
|
373
|
+
if (change.cursor !== undefined) {
|
|
374
|
+
if (change.cursor !== conversationCursor) {
|
|
375
|
+
await local.fact('conversationCursor', change.cursor);
|
|
376
|
+
}
|
|
377
|
+
alive.deps.stream()?.hold(change.cursor);
|
|
378
|
+
}
|
|
379
|
+
return list;
|
|
380
|
+
})
|
|
381
|
+
.catch(async () => (await (await alive.deps.local()).facts()).conversationList ?? [])
|
|
382
|
+
.then((list) => {
|
|
383
|
+
if (alive.deps.closed() || change.paged === true)
|
|
303
384
|
return;
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
const value = viewOf(conversation, view.window, sharedFrame(view, view.window));
|
|
311
|
-
for (const watcher of ready)
|
|
312
|
-
watcher.notify(value);
|
|
385
|
+
endedAnew(alive, list);
|
|
386
|
+
if (change.cursor !== undefined) {
|
|
387
|
+
alive.held.set({ listKnown: true });
|
|
388
|
+
settleBehind(alive, list);
|
|
389
|
+
openingEnded(alive);
|
|
390
|
+
alive.deps.settled();
|
|
313
391
|
}
|
|
392
|
+
emitList(alive);
|
|
393
|
+
if (!alive.known())
|
|
394
|
+
return;
|
|
395
|
+
retiredDrafts(alive, list);
|
|
396
|
+
const { replicated } = alive.held.now();
|
|
397
|
+
if (change.cursor === undefined &&
|
|
398
|
+
(list.length !== replicated.size ||
|
|
399
|
+
list.some(({ conversation }) => !replicated.has(conversation)))) {
|
|
400
|
+
alive.deps.again();
|
|
401
|
+
}
|
|
402
|
+
}, () => { });
|
|
403
|
+
}
|
|
404
|
+
function endedAnew(alive, list) {
|
|
405
|
+
const entries = new Map(list.map((entry) => [entry.conversation, entry]));
|
|
406
|
+
for (const view of alive.views()) {
|
|
407
|
+
const ended = entries.get(view.conversation)?.processEnded ?? null;
|
|
408
|
+
const held = view.held.now();
|
|
409
|
+
if (held.window === null || ended === held.ended)
|
|
410
|
+
continue;
|
|
411
|
+
view.held.set({ ended });
|
|
412
|
+
emit(alive, view.conversation);
|
|
314
413
|
}
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
async
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
414
|
+
}
|
|
415
|
+
function shownList(alive, work) {
|
|
416
|
+
const reading = heldList(alive);
|
|
417
|
+
alive.inOrder(async () => {
|
|
418
|
+
const list = await reading.catch(() => null);
|
|
419
|
+
if (list === null || alive.deps.closed())
|
|
420
|
+
return;
|
|
421
|
+
work(list);
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
async function told(alive) {
|
|
425
|
+
try {
|
|
426
|
+
while (alive.held.now().listOwed && !alive.deps.closed()) {
|
|
427
|
+
alive.held.set({ listOwed: false });
|
|
428
|
+
const value = await untilAnswered(() => entries(alive), alive.closing);
|
|
429
|
+
if (alive.deps.closed())
|
|
430
|
+
return;
|
|
431
|
+
if (value === null)
|
|
328
432
|
continue;
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
const next = windowOf(view.window, joining, view.asked.limit);
|
|
332
|
-
view.window = next.messages;
|
|
333
|
-
view.more ||= next.dropped;
|
|
334
|
-
if (view.total !== null && arriving.length > 0) {
|
|
335
|
-
count ??= await local.messageCount(conversation);
|
|
336
|
-
view.total = count;
|
|
337
|
-
}
|
|
433
|
+
alive.listeners.tell(value);
|
|
434
|
+
alive.listeners.welcome(value);
|
|
338
435
|
}
|
|
339
436
|
}
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
if (facts.questions.length > 0)
|
|
343
|
-
return 'asking';
|
|
344
|
-
return facts.turn === null ? 'idle' : 'working';
|
|
437
|
+
finally {
|
|
438
|
+
alive.held.set({ listTelling: false });
|
|
345
439
|
}
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
440
|
+
}
|
|
441
|
+
function tellList(alive) {
|
|
442
|
+
const { listeners } = alive;
|
|
443
|
+
if (alive.deps.closed() || (!listeners.told() && !listeners.waiting()))
|
|
444
|
+
return;
|
|
445
|
+
alive.held.set({ listOwed: true });
|
|
446
|
+
if (alive.held.now().listTelling)
|
|
447
|
+
return;
|
|
448
|
+
alive.held.set({ listTelling: true });
|
|
449
|
+
void told(alive).catch(() => { });
|
|
450
|
+
}
|
|
451
|
+
function tellSoon(alive) {
|
|
452
|
+
alive.rowSoon(() => tellList(alive));
|
|
453
|
+
}
|
|
454
|
+
function emitList(alive) {
|
|
455
|
+
if (alive.deps.closed())
|
|
456
|
+
return;
|
|
457
|
+
tellList(alive);
|
|
458
|
+
emitAgents(alive);
|
|
459
|
+
conversationsLearned(alive);
|
|
460
|
+
}
|
|
461
|
+
export function conversationsLearned(alive) {
|
|
462
|
+
if (alive.deps.closed())
|
|
463
|
+
return;
|
|
464
|
+
if (alive.listeners.waiting())
|
|
465
|
+
tellList(alive);
|
|
466
|
+
if (!alive.agentListeners.waiting())
|
|
467
|
+
return;
|
|
468
|
+
shownList(alive, (list) => alive.agentListeners.welcome(agents(alive, list)));
|
|
469
|
+
}
|
|
470
|
+
function agents(alive, list) {
|
|
471
|
+
const named = new Map();
|
|
472
|
+
for (const agent of alive.held.now().listedAgents ?? [])
|
|
473
|
+
named.set(agent.agent, agent);
|
|
474
|
+
for (const entry of list) {
|
|
475
|
+
named.set(entry.agent, { name: entry.agentName, retired: entry.agentRetired });
|
|
358
476
|
}
|
|
359
|
-
|
|
360
|
-
|
|
477
|
+
return [...named]
|
|
478
|
+
.filter(([, agent]) => !agent.retired)
|
|
479
|
+
.map(([identity, agent]) => ({ identity, name: agent.name }))
|
|
480
|
+
.sort((left, right) => left.name === right.name
|
|
481
|
+
? left.identity.localeCompare(right.identity)
|
|
482
|
+
: left.name.localeCompare(right.name));
|
|
483
|
+
}
|
|
484
|
+
function readAgents(alive) {
|
|
485
|
+
const stream = alive.deps.stream();
|
|
486
|
+
if (stream?.agents === undefined || !alive.deps.online() || alive.held.now().agentsReading)
|
|
487
|
+
return;
|
|
488
|
+
const { agents: read } = stream;
|
|
489
|
+
const { agentListeners } = alive;
|
|
490
|
+
alive.held.set({ agentsReading: true });
|
|
491
|
+
void alive.deps.recovery
|
|
492
|
+
.attempt('agents', async () => {
|
|
493
|
+
if (!alive.deps.online() || (!agentListeners.waiting() && !agentListeners.told())) {
|
|
494
|
+
return 'done';
|
|
495
|
+
}
|
|
496
|
+
const listed = await read();
|
|
497
|
+
if (listed === null)
|
|
498
|
+
return 'done';
|
|
499
|
+
alive.held.set({ listedAgents: listed });
|
|
500
|
+
emitAgents(alive);
|
|
501
|
+
return 'done';
|
|
502
|
+
})
|
|
503
|
+
.finally(() => {
|
|
504
|
+
alive.held.set({ agentsReading: false });
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
function emitAgents(alive) {
|
|
508
|
+
if (alive.deps.closed() || !alive.agentListeners.told())
|
|
509
|
+
return;
|
|
510
|
+
shownList(alive, (list) => {
|
|
511
|
+
const value = agents(alive, list);
|
|
512
|
+
const shown = JSON.stringify(value);
|
|
513
|
+
if (shown === alive.held.now().agentsShown)
|
|
361
514
|
return;
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
515
|
+
alive.held.set({ agentsShown: shown });
|
|
516
|
+
alive.agentListeners.tell(value);
|
|
517
|
+
});
|
|
518
|
+
}
|
|
519
|
+
function setLive(alive, conversation, next) {
|
|
520
|
+
const before = rowOf(alive, conversation);
|
|
521
|
+
alive.holdLive(conversation, next);
|
|
522
|
+
const after = rowOf(alive, conversation);
|
|
523
|
+
if (after.state !== before.state || after.started !== before.started)
|
|
524
|
+
emitList(alive);
|
|
525
|
+
else if (after.line !== before.line ||
|
|
526
|
+
after.question !== before.question ||
|
|
527
|
+
after.asked !== before.asked)
|
|
528
|
+
tellSoon(alive);
|
|
529
|
+
}
|
|
530
|
+
function retiredChat(conversation, list) {
|
|
531
|
+
if (!conversation.startsWith(NEW_CHAT))
|
|
532
|
+
return false;
|
|
533
|
+
const agent = conversation.slice(NEW_CHAT.length);
|
|
534
|
+
return list.some((entry) => entry.agent === agent && entry.agentRetired);
|
|
535
|
+
}
|
|
536
|
+
function live(alive, conversation, change) {
|
|
537
|
+
alive.factsHeard(conversation, FACTS.filter((one) => one in change));
|
|
538
|
+
setLive(alive, conversation, { ...alive.liveOf(conversation), ...change });
|
|
539
|
+
emit(alive, conversation);
|
|
540
|
+
}
|
|
541
|
+
function openingEnded(alive) {
|
|
542
|
+
for (const [conversation, facts] of alive.factsTaken()) {
|
|
543
|
+
const held = alive.liveHeld(conversation);
|
|
544
|
+
if (held === undefined || facts.size === 0)
|
|
545
|
+
continue;
|
|
546
|
+
setLive(alive, conversation, {
|
|
547
|
+
turn: held.turn,
|
|
548
|
+
questions: facts.has('questions') ? QUIET.questions : held.questions,
|
|
549
|
+
process: facts.has('process') ? QUIET.process : held.process,
|
|
550
|
+
commands: facts.has('commands') ? QUIET.commands : held.commands,
|
|
551
|
+
options: facts.has('options') ? QUIET.options : held.options,
|
|
552
|
+
starting: facts.has('starting') ? QUIET.starting : held.starting,
|
|
553
|
+
subagents: facts.has('subagents') ? QUIET.subagents : held.subagents,
|
|
554
|
+
link: facts.has('link') ? QUIET.link : held.link,
|
|
555
|
+
deliveryUnknown: facts.has('deliveryUnknown') ? QUIET.deliveryUnknown : held.deliveryUnknown,
|
|
369
556
|
});
|
|
557
|
+
emit(alive, conversation);
|
|
370
558
|
}
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
559
|
+
}
|
|
560
|
+
function relayed(alive, conversation, turn, operation) {
|
|
561
|
+
const held = alive.liveOf(conversation).turn;
|
|
562
|
+
const next = held?.turn === turn ? appliedTurn(held, operation) : startedTurn(turn, operation);
|
|
563
|
+
if (next !== null)
|
|
564
|
+
live(alive, conversation, { turn: next });
|
|
565
|
+
}
|
|
566
|
+
function sealTurns(alive, conversation, turns) {
|
|
567
|
+
const held = alive.liveHeld(conversation);
|
|
568
|
+
if (held?.turn == null || !turns.has(held.turn.turn))
|
|
569
|
+
return false;
|
|
570
|
+
setLive(alive, conversation, { ...held, turn: null });
|
|
571
|
+
return true;
|
|
572
|
+
}
|
|
573
|
+
function handReplicated(alive) {
|
|
574
|
+
const stream = alive.deps.stream();
|
|
575
|
+
if (stream === undefined)
|
|
576
|
+
return;
|
|
577
|
+
const held = [...alive.held.now().replicated].sort();
|
|
578
|
+
const named = JSON.stringify(held);
|
|
579
|
+
if (named === alive.held.now().handedReplicated)
|
|
580
|
+
return;
|
|
581
|
+
alive.held.set({ handedReplicated: named });
|
|
582
|
+
stream.replicate(held);
|
|
583
|
+
}
|
|
584
|
+
async function remove(alive, leaving) {
|
|
585
|
+
await alive.deps.durably(async (local) => {
|
|
586
|
+
const kept = new Set([
|
|
587
|
+
...(await local.conversations(leaving)).map(({ conversation }) => conversation),
|
|
588
|
+
...(await local.chatDrafts())
|
|
589
|
+
.filter(({ conversation }) => leaving.includes(conversation))
|
|
590
|
+
.map(({ conversation }) => conversation),
|
|
591
|
+
]);
|
|
592
|
+
await local.dropConversations(leaving);
|
|
593
|
+
const changed = [];
|
|
594
|
+
for (const conversation of leaving) {
|
|
595
|
+
if (kept.has(conversation) || alive.liveHeld(conversation) !== undefined) {
|
|
596
|
+
changed.push(conversation);
|
|
396
597
|
}
|
|
598
|
+
shownDraft(alive, conversation, null);
|
|
599
|
+
alive.forget(conversation);
|
|
600
|
+
setLive(alive, conversation, null);
|
|
601
|
+
await slide(alive, local, conversation, null);
|
|
602
|
+
}
|
|
603
|
+
if (changed.length > 0)
|
|
604
|
+
alive.deps.publish(conversationChangeSet(changed));
|
|
605
|
+
});
|
|
606
|
+
}
|
|
607
|
+
function newestOfListed(alive, entry) {
|
|
608
|
+
if (entry === undefined)
|
|
609
|
+
return undefined;
|
|
610
|
+
const heard = alive.newestHeard(entry.conversation);
|
|
611
|
+
if (heard === undefined)
|
|
612
|
+
return entry.newest;
|
|
613
|
+
return entry.newest === null || heard > entry.newest ? heard : entry.newest;
|
|
614
|
+
}
|
|
615
|
+
function missed(alive, conversation, held, entry) {
|
|
616
|
+
const read = held?.read;
|
|
617
|
+
const newest = newestOfListed(alive, entry);
|
|
618
|
+
return (read === undefined ||
|
|
619
|
+
newest === undefined ||
|
|
620
|
+
held?.partial === true ||
|
|
621
|
+
(newest !== null && (read === null || newest > read)));
|
|
622
|
+
}
|
|
623
|
+
async function readsOf(local, conversations) {
|
|
624
|
+
return new Map((await local.conversations(conversations)).map((held) => [held.conversation, held]));
|
|
625
|
+
}
|
|
626
|
+
function retiredDrafts(alive, list) {
|
|
627
|
+
if (!list.some((entry) => entry.agentRetired))
|
|
628
|
+
return;
|
|
629
|
+
void alive.deps
|
|
630
|
+
.local()
|
|
631
|
+
.then((local) => local.chatDrafts())
|
|
632
|
+
.then((held) => {
|
|
633
|
+
if (!alive.deps.closed() &&
|
|
634
|
+
held.some(({ conversation }) => retiredChat(conversation, list))) {
|
|
635
|
+
alive.deps.again();
|
|
397
636
|
}
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
637
|
+
}, () => { });
|
|
638
|
+
}
|
|
639
|
+
function settleBehind(alive, list) {
|
|
640
|
+
void alive.deps
|
|
641
|
+
.durably(async (local) => {
|
|
642
|
+
const entries = new Map(list.map((entry) => [entry.conversation, entry]));
|
|
643
|
+
const named = [...alive.held.now().replicated].filter((conversation) => entries.has(conversation));
|
|
644
|
+
const held = await readsOf(local, named);
|
|
645
|
+
let owed = false;
|
|
646
|
+
for (const conversation of named) {
|
|
647
|
+
if (!missed(alive, conversation, held.get(conversation), entries.get(conversation))) {
|
|
648
|
+
alive.caughtUpTo(conversation);
|
|
649
|
+
}
|
|
650
|
+
else if (!alive.behind(conversation)) {
|
|
651
|
+
alive.gapOpened(conversation);
|
|
652
|
+
owed = true;
|
|
404
653
|
}
|
|
405
654
|
}
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
.map(([identity, name]) => ({ identity, name }))
|
|
411
|
-
.sort((left, right) => left.name === right.name
|
|
412
|
-
? left.identity.localeCompare(right.identity)
|
|
413
|
-
: left.name.localeCompare(right.name));
|
|
414
|
-
}
|
|
415
|
-
function emitAgents() {
|
|
416
|
-
if (deps.closed() || agentWatchers.size === 0)
|
|
417
|
-
return;
|
|
418
|
-
const value = agents();
|
|
419
|
-
for (const watcher of agentWatchers)
|
|
420
|
-
watcher(value);
|
|
421
|
-
}
|
|
422
|
-
function setLive(conversation, next) {
|
|
423
|
-
const before = stateOf(conversation);
|
|
424
|
-
const began = lives.get(conversation)?.turn?.started;
|
|
425
|
-
if (next === null)
|
|
426
|
-
lives.delete(conversation);
|
|
427
|
-
else
|
|
428
|
-
lives.set(conversation, next);
|
|
429
|
-
if (!listing.has(conversation))
|
|
655
|
+
return owed;
|
|
656
|
+
})
|
|
657
|
+
.then((owed) => {
|
|
658
|
+
if (alive.deps.closed())
|
|
430
659
|
return;
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
660
|
+
alive.deps.settled();
|
|
661
|
+
if (owed)
|
|
662
|
+
alive.deps.again();
|
|
663
|
+
}, () => { });
|
|
664
|
+
}
|
|
665
|
+
function keep(alive, conversation, message) {
|
|
666
|
+
return alive.deps.durably(async (local) => {
|
|
667
|
+
if (!alive.held.now().replicated.has(conversation))
|
|
438
668
|
return;
|
|
439
|
-
const
|
|
440
|
-
const
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
669
|
+
const read = (await readsOf(local, [conversation])).get(conversation)?.read;
|
|
670
|
+
const advances = read !== undefined &&
|
|
671
|
+
!alive.behind(conversation) &&
|
|
672
|
+
(read === null || message.position > read);
|
|
673
|
+
await sealed(alive, local, conversation, new Set([message.turn]));
|
|
674
|
+
await local.keepMessages({
|
|
675
|
+
conversation,
|
|
676
|
+
messages: [message],
|
|
677
|
+
...(advances ? { read: { at: message.position, partial: false } } : {}),
|
|
678
|
+
});
|
|
679
|
+
sealTurns(alive, conversation, new Set([message.turn]));
|
|
680
|
+
await slide(alive, local, conversation, [message]);
|
|
681
|
+
alive.deps.publish(conversationChangeSet([conversation]));
|
|
682
|
+
tellList(alive);
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
async function readHistories(alive, conversations, ended) {
|
|
686
|
+
const generations = new Map(conversations.map((one) => [one, alive.generation(one)]));
|
|
687
|
+
const held = await readsOf(await alive.deps.local(), conversations);
|
|
688
|
+
let waiting = conversations.map((conversation) => ({
|
|
689
|
+
conversation,
|
|
690
|
+
after: held.get(conversation)?.read ?? undefined,
|
|
691
|
+
}));
|
|
692
|
+
while (waiting.length > 0) {
|
|
693
|
+
const asking = waiting.slice(0, HISTORY_CONVERSATIONS);
|
|
694
|
+
const result = await alive.deps.history({
|
|
695
|
+
conversations: asking.map(({ conversation, after }) => ({
|
|
696
|
+
conversation,
|
|
697
|
+
...(after === undefined ? {} : { after }),
|
|
698
|
+
})),
|
|
699
|
+
});
|
|
700
|
+
const pages = historyPages(result);
|
|
701
|
+
if (pages === null)
|
|
702
|
+
return result;
|
|
703
|
+
const asked = new Map(asking.map(({ conversation, after }) => [conversation, after]));
|
|
704
|
+
const following = [];
|
|
705
|
+
for (const page of pages) {
|
|
706
|
+
if (!asked.has(page.conversation))
|
|
707
|
+
continue;
|
|
708
|
+
await keepPage(alive, page.conversation, page.messages, asked.get(page.conversation) ?? null, page.next, generations.get(page.conversation) ?? 0);
|
|
709
|
+
if (page.next !== undefined) {
|
|
710
|
+
following.push({ conversation: page.conversation, after: page.next });
|
|
459
711
|
}
|
|
460
|
-
if (read.next === undefined)
|
|
461
|
-
break;
|
|
462
|
-
after = read.next;
|
|
463
712
|
}
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
learned();
|
|
468
|
-
return;
|
|
713
|
+
for (const { conversation } of asking) {
|
|
714
|
+
if (!following.some((next) => next.conversation === conversation))
|
|
715
|
+
ended(conversation);
|
|
469
716
|
}
|
|
470
|
-
|
|
471
|
-
retiredAgents = retired;
|
|
472
|
-
emitAgents();
|
|
473
|
-
if (named)
|
|
474
|
-
emitList();
|
|
475
|
-
learned();
|
|
476
|
-
const ended = [...drafts.keys()].filter(retiredChat);
|
|
477
|
-
if (ended.length > 0)
|
|
478
|
-
await remove(ended);
|
|
717
|
+
waiting = [...waiting.slice(HISTORY_CONVERSATIONS), ...following];
|
|
479
718
|
}
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
});
|
|
487
|
-
return agentsRead;
|
|
488
|
-
}
|
|
489
|
-
function live(conversation, change) {
|
|
490
|
-
const owed = unsent.get(conversation);
|
|
491
|
-
for (const fact of FACTS.filter((one) => one in change))
|
|
492
|
-
owed?.delete(fact);
|
|
493
|
-
setLive(conversation, { ...liveOf(conversation), ...change });
|
|
494
|
-
emit(conversation);
|
|
719
|
+
return null;
|
|
720
|
+
}
|
|
721
|
+
function caughtUp(alive, conversations) {
|
|
722
|
+
const ends = new Map();
|
|
723
|
+
for (const conversation of conversations) {
|
|
724
|
+
alive.catchingUntil(conversation, new Promise((end) => ends.set(conversation, end)));
|
|
495
725
|
}
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
for (const
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
726
|
+
const ended = (conversation) => ends.get(conversation)?.();
|
|
727
|
+
const read = readHistories(alive, conversations, ended);
|
|
728
|
+
const endAll = () => {
|
|
729
|
+
for (const conversation of conversations)
|
|
730
|
+
ended(conversation);
|
|
731
|
+
};
|
|
732
|
+
read.then(endAll, endAll);
|
|
733
|
+
return read;
|
|
734
|
+
}
|
|
735
|
+
function keepPage(alive, conversation, arriving, from, next, generation) {
|
|
736
|
+
return alive.deps.durably(async (local) => {
|
|
737
|
+
if (!alive.held.now().replicated.has(conversation))
|
|
738
|
+
return;
|
|
739
|
+
const whole = next === undefined;
|
|
740
|
+
const cursor = next ?? newestOf(arriving, from);
|
|
741
|
+
const held = (await readsOf(local, [conversation])).get(conversation);
|
|
742
|
+
const partial = !whole && (held?.partial === true || held?.read === undefined);
|
|
743
|
+
const sealing = await sealed(alive, local, conversation, new Set(arriving.map((message) => message.turn)));
|
|
744
|
+
await local.keepMessages({ conversation, messages: arriving, read: { at: cursor, partial } });
|
|
745
|
+
sealTurns(alive, conversation, new Set(arriving.map((message) => message.turn)));
|
|
746
|
+
if (whole && alive.generation(conversation) === generation)
|
|
747
|
+
alive.caughtUpTo(conversation);
|
|
748
|
+
await slide(alive, local, conversation, arriving);
|
|
749
|
+
if (arriving.length > 0 || sealing || whole) {
|
|
750
|
+
alive.deps.publish(conversationChangeSet([conversation]));
|
|
516
751
|
}
|
|
752
|
+
if (arriving.length > 0)
|
|
753
|
+
tellSoon(alive);
|
|
754
|
+
});
|
|
755
|
+
}
|
|
756
|
+
function heard(alive, event) {
|
|
757
|
+
const { conversation } = event;
|
|
758
|
+
if (event.kind === 'gap') {
|
|
759
|
+
if (alive.held.now().replicated.has(conversation))
|
|
760
|
+
alive.gapOpened(conversation);
|
|
761
|
+
if (alive.liveHeld(conversation) !== undefined)
|
|
762
|
+
alive.factsOwed(conversation);
|
|
763
|
+
return;
|
|
517
764
|
}
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
if (next !== null)
|
|
522
|
-
live(conversation, { turn: next });
|
|
523
|
-
}
|
|
524
|
-
function sealTurns(conversation, turns) {
|
|
525
|
-
const held = lives.get(conversation);
|
|
526
|
-
if (held?.turn == null || !turns.has(held.turn.turn))
|
|
527
|
-
return false;
|
|
528
|
-
setLive(conversation, { ...held, turn: null });
|
|
529
|
-
return true;
|
|
530
|
-
}
|
|
531
|
-
function handReplicated() {
|
|
532
|
-
const stream = deps.stream();
|
|
533
|
-
if (stream === undefined)
|
|
534
|
-
return;
|
|
535
|
-
const held = [...replicated].sort();
|
|
536
|
-
const named = JSON.stringify(held);
|
|
537
|
-
if (named === handedReplicated)
|
|
538
|
-
return;
|
|
539
|
-
handedReplicated = named;
|
|
540
|
-
stream.replicate(held);
|
|
765
|
+
if (event.kind === 'turn') {
|
|
766
|
+
relayed(alive, conversation, event.turn, event.operation);
|
|
767
|
+
return;
|
|
541
768
|
}
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
const changed = [];
|
|
546
|
-
for (const conversation of leaving) {
|
|
547
|
-
if (stored.has(conversation) || drafts.has(conversation) || lives.has(conversation)) {
|
|
548
|
-
changed.push(conversation);
|
|
549
|
-
}
|
|
550
|
-
stored.delete(conversation);
|
|
551
|
-
reads.delete(conversation);
|
|
552
|
-
partials.delete(conversation);
|
|
553
|
-
drafts.delete(conversation);
|
|
554
|
-
behind.delete(conversation);
|
|
555
|
-
unsent.delete(conversation);
|
|
556
|
-
setLive(conversation, null);
|
|
557
|
-
await slide(local, conversation, null);
|
|
558
|
-
}
|
|
559
|
-
if (changed.length > 0)
|
|
560
|
-
deps.publish(conversationChangeSet(changed));
|
|
561
|
-
});
|
|
769
|
+
if (event.kind === 'questions') {
|
|
770
|
+
live(alive, conversation, { questions: structuredClone(event.questions) });
|
|
771
|
+
return;
|
|
562
772
|
}
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
return (read === undefined ||
|
|
567
|
-
newest === undefined ||
|
|
568
|
-
partials.has(conversation) ||
|
|
569
|
-
(newest !== null && (read === null || newest > read)));
|
|
773
|
+
if (event.kind === 'process') {
|
|
774
|
+
live(alive, conversation, { process: { running: event.running } });
|
|
775
|
+
return;
|
|
570
776
|
}
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
return;
|
|
575
|
-
listing.set(conversation, { ...entry, newest: message.position });
|
|
777
|
+
if (event.kind === 'commands') {
|
|
778
|
+
live(alive, conversation, { commands: structuredClone(event.commands) });
|
|
779
|
+
return;
|
|
576
780
|
}
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
781
|
+
if (event.kind === 'options') {
|
|
782
|
+
live(alive, conversation, { options: structuredClone(event.options) });
|
|
783
|
+
return;
|
|
580
784
|
}
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
return;
|
|
585
|
-
const read = reads.get(conversation);
|
|
586
|
-
const advances = read !== undefined &&
|
|
587
|
-
!behind.has(conversation) &&
|
|
588
|
-
(read === null || message.position > read);
|
|
589
|
-
await sealed(local, conversation, new Set([message.turn]));
|
|
590
|
-
await local.keepMessages({
|
|
591
|
-
conversation,
|
|
592
|
-
messages: [message],
|
|
593
|
-
...(advances ? { read: { at: message.position, partial: false } } : {}),
|
|
594
|
-
});
|
|
595
|
-
stored.add(conversation);
|
|
596
|
-
if (advances)
|
|
597
|
-
reads.set(conversation, message.position);
|
|
598
|
-
sealTurns(conversation, new Set([message.turn]));
|
|
599
|
-
await slide(local, conversation, [message]);
|
|
600
|
-
deps.publish(conversationChangeSet([conversation]));
|
|
601
|
-
});
|
|
785
|
+
if (event.kind === 'starting') {
|
|
786
|
+
live(alive, conversation, { starting: event.step });
|
|
787
|
+
return;
|
|
602
788
|
}
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
conversation,
|
|
607
|
-
after: reads.get(conversation) ?? undefined,
|
|
608
|
-
}));
|
|
609
|
-
while (waiting.length > 0) {
|
|
610
|
-
const asking = waiting.slice(0, HISTORY_CONVERSATIONS);
|
|
611
|
-
const result = await deps.call('conversation.history', {
|
|
612
|
-
conversations: asking.map(({ conversation, after }) => ({
|
|
613
|
-
conversation,
|
|
614
|
-
...(after === undefined ? {} : { after }),
|
|
615
|
-
})),
|
|
616
|
-
});
|
|
617
|
-
const pages = historyPages(result);
|
|
618
|
-
if (pages === null)
|
|
619
|
-
return result;
|
|
620
|
-
const asked = new Map(asking.map(({ conversation, after }) => [conversation, after]));
|
|
621
|
-
const following = [];
|
|
622
|
-
for (const page of pages) {
|
|
623
|
-
if (!asked.has(page.conversation))
|
|
624
|
-
continue;
|
|
625
|
-
await keepPage(page.conversation, page.messages, asked.get(page.conversation) ?? null, page.next, generations.get(page.conversation) ?? 0);
|
|
626
|
-
if (page.next !== undefined) {
|
|
627
|
-
following.push({ conversation: page.conversation, after: page.next });
|
|
628
|
-
}
|
|
629
|
-
}
|
|
630
|
-
for (const { conversation } of asking) {
|
|
631
|
-
if (!following.some((next) => next.conversation === conversation))
|
|
632
|
-
ended(conversation);
|
|
633
|
-
}
|
|
634
|
-
waiting = [...waiting.slice(HISTORY_CONVERSATIONS), ...following];
|
|
635
|
-
}
|
|
636
|
-
return null;
|
|
789
|
+
if (event.kind === 'subagents') {
|
|
790
|
+
live(alive, conversation, { subagents: structuredClone(event.subagents) });
|
|
791
|
+
return;
|
|
637
792
|
}
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
const held = new Promise((end) => ends.set(conversation, end));
|
|
642
|
-
catching.set(conversation, held);
|
|
643
|
-
void held.then(() => {
|
|
644
|
-
if (catching.get(conversation) === held)
|
|
645
|
-
catching.delete(conversation);
|
|
646
|
-
});
|
|
647
|
-
}
|
|
648
|
-
const ended = (conversation) => ends.get(conversation)?.();
|
|
649
|
-
const read = readHistories(conversations, ended);
|
|
650
|
-
const endAll = () => {
|
|
651
|
-
for (const conversation of conversations)
|
|
652
|
-
ended(conversation);
|
|
653
|
-
};
|
|
654
|
-
read.then(endAll, endAll);
|
|
655
|
-
return read;
|
|
793
|
+
if (event.kind === 'link') {
|
|
794
|
+
live(alive, conversation, { link: { connected: event.connected } });
|
|
795
|
+
return;
|
|
656
796
|
}
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
return;
|
|
661
|
-
const whole = next === undefined;
|
|
662
|
-
const cursor = next ?? newestOf(arriving, from);
|
|
663
|
-
const partial = !whole && (partials.has(conversation) || !reads.has(conversation));
|
|
664
|
-
const sealing = await sealed(local, conversation, new Set(arriving.map((message) => message.turn)));
|
|
665
|
-
await local.keepMessages({ conversation, messages: arriving, read: { at: cursor, partial } });
|
|
666
|
-
stored.add(conversation);
|
|
667
|
-
reads.set(conversation, cursor);
|
|
668
|
-
if (partial)
|
|
669
|
-
partials.add(conversation);
|
|
670
|
-
else
|
|
671
|
-
partials.delete(conversation);
|
|
672
|
-
sealTurns(conversation, new Set(arriving.map((message) => message.turn)));
|
|
673
|
-
if (whole && (gaps.get(conversation) ?? 0) === generation)
|
|
674
|
-
behind.delete(conversation);
|
|
675
|
-
await slide(local, conversation, arriving);
|
|
676
|
-
if (arriving.length > 0 || sealing || whole) {
|
|
677
|
-
deps.publish(conversationChangeSet([conversation]));
|
|
678
|
-
}
|
|
679
|
-
});
|
|
797
|
+
if (event.kind === 'deliveries') {
|
|
798
|
+
live(alive, conversation, { deliveryUnknown: [...event.unknown] });
|
|
799
|
+
return;
|
|
680
800
|
}
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
801
|
+
const arriving = acceptedMessage(event.message);
|
|
802
|
+
if (arriving === null)
|
|
803
|
+
return;
|
|
804
|
+
alive.heardAt(conversation, arriving.position);
|
|
805
|
+
if (alive.held.now().replicated.has(conversation)) {
|
|
806
|
+
if (alive.deps.full()) {
|
|
807
|
+
alive.gapOpened(conversation);
|
|
688
808
|
return;
|
|
689
809
|
}
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
}
|
|
694
|
-
if (event.kind === 'questions') {
|
|
695
|
-
live(conversation, { questions: structuredClone(event.questions) });
|
|
696
|
-
return;
|
|
697
|
-
}
|
|
698
|
-
if (event.kind === 'process') {
|
|
699
|
-
live(conversation, { process: { running: event.running } });
|
|
700
|
-
return;
|
|
701
|
-
}
|
|
702
|
-
if (event.kind === 'commands') {
|
|
703
|
-
live(conversation, { commands: structuredClone(event.commands) });
|
|
704
|
-
return;
|
|
705
|
-
}
|
|
706
|
-
if (event.kind === 'options') {
|
|
707
|
-
live(conversation, { options: structuredClone(event.options) });
|
|
708
|
-
return;
|
|
709
|
-
}
|
|
710
|
-
if (event.kind === 'starting') {
|
|
711
|
-
live(conversation, { starting: event.step });
|
|
712
|
-
return;
|
|
713
|
-
}
|
|
714
|
-
if (event.kind === 'link') {
|
|
715
|
-
live(conversation, { link: { connected: event.connected } });
|
|
716
|
-
return;
|
|
717
|
-
}
|
|
718
|
-
if (event.kind === 'deliveries') {
|
|
719
|
-
live(conversation, { deliveryUnknown: [...event.unknown] });
|
|
720
|
-
return;
|
|
721
|
-
}
|
|
722
|
-
const arriving = acceptedMessage(event.message);
|
|
723
|
-
if (arriving === null)
|
|
724
|
-
return;
|
|
725
|
-
noted(conversation, arriving);
|
|
726
|
-
if (replicated.has(conversation)) {
|
|
727
|
-
if (deps.full()) {
|
|
728
|
-
gap(conversation);
|
|
729
|
-
return;
|
|
730
|
-
}
|
|
731
|
-
void keep(conversation, arriving).catch((error) => {
|
|
732
|
-
if (refusedForSpace(error))
|
|
733
|
-
gap(conversation);
|
|
734
|
-
});
|
|
735
|
-
return;
|
|
736
|
-
}
|
|
737
|
-
if (sealTurns(conversation, new Set([arriving.turn])))
|
|
738
|
-
emit(conversation);
|
|
739
|
-
}
|
|
740
|
-
async function kept(local, conversation, following) {
|
|
741
|
-
if (following === null || empty(following)) {
|
|
742
|
-
await local.dropChatDraft(conversation);
|
|
743
|
-
drafts.delete(conversation);
|
|
744
|
-
}
|
|
745
|
-
else {
|
|
746
|
-
await local.keepChatDrafts([following]);
|
|
747
|
-
drafts.set(conversation, following);
|
|
748
|
-
}
|
|
749
|
-
}
|
|
750
|
-
async function sealed(local, conversation, turns) {
|
|
751
|
-
const held = drafts.get(conversation);
|
|
752
|
-
const submitted = held?.submitted ?? [];
|
|
753
|
-
const left = submitted.filter((one) => one.turn === null || !turns.has(one.turn));
|
|
754
|
-
if (held === undefined || left.length === submitted.length)
|
|
755
|
-
return false;
|
|
756
|
-
await kept(local, conversation, withSubmitted(held, left));
|
|
757
|
-
return true;
|
|
758
|
-
}
|
|
759
|
-
function moved(agent, operation, to) {
|
|
760
|
-
const from = newChat(agent);
|
|
761
|
-
return deps.durably(async (local) => {
|
|
762
|
-
const held = drafts.get(from);
|
|
763
|
-
const earlier = (held?.moves ?? []).filter((one) => !deps.expired(one));
|
|
764
|
-
if (earlier.includes(operation))
|
|
765
|
-
return;
|
|
766
|
-
const blank = {
|
|
767
|
-
conversation: from,
|
|
768
|
-
text: '',
|
|
769
|
-
revision: chatDraftRevision(),
|
|
770
|
-
};
|
|
771
|
-
const { moves: _answered, ...unsent } = held ?? blank;
|
|
772
|
-
const taken = drafts.has(to) || empty(unsent) ? [] : [{ ...unsent, conversation: to }];
|
|
773
|
-
const left = { ...(taken.length === 0 ? unsent : blank), moves: [...earlier, operation] };
|
|
774
|
-
await local.keepChatDrafts([left, ...taken]);
|
|
775
|
-
drafts.set(from, left);
|
|
776
|
-
for (const one of taken)
|
|
777
|
-
drafts.set(to, one);
|
|
778
|
-
deps.publish(conversationChangeSet([from, to]));
|
|
810
|
+
void keep(alive, conversation, arriving).catch((error) => {
|
|
811
|
+
if (refusedForSpace(error))
|
|
812
|
+
alive.gapOpened(conversation);
|
|
779
813
|
});
|
|
814
|
+
return;
|
|
780
815
|
}
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
if (submission !== undefined)
|
|
797
|
-
return undefined;
|
|
798
|
-
const said = draft ?? current;
|
|
799
|
-
submission = { operation, text: said?.text ?? '', turn: null };
|
|
800
|
-
return {
|
|
801
|
-
conversation,
|
|
802
|
-
...(draft === undefined || current === undefined
|
|
803
|
-
? { text: '', revision: chatDraftRevision() }
|
|
804
|
-
: { text: current.text, revision: current.revision }),
|
|
805
|
-
submitted: [...(current?.submitted ?? []), submission],
|
|
806
|
-
};
|
|
807
|
-
});
|
|
808
|
-
return { submission: submission, earlier };
|
|
816
|
+
if (sealTurns(alive, conversation, new Set([arriving.turn])))
|
|
817
|
+
emit(alive, conversation);
|
|
818
|
+
}
|
|
819
|
+
async function kept(alive, local, conversation, following) {
|
|
820
|
+
const shown = following === null || empty(following) ? null : following;
|
|
821
|
+
if (shown === null)
|
|
822
|
+
await local.dropChatDraft(conversation);
|
|
823
|
+
else
|
|
824
|
+
await local.keepChatDrafts([shown]);
|
|
825
|
+
shownDraft(alive, conversation, shown);
|
|
826
|
+
}
|
|
827
|
+
function shownDraft(alive, conversation, draft) {
|
|
828
|
+
for (const view of alive.views()) {
|
|
829
|
+
if (view.conversation === conversation)
|
|
830
|
+
view.held.set({ draft });
|
|
809
831
|
}
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
832
|
+
}
|
|
833
|
+
async function draftAt(local, conversation) {
|
|
834
|
+
return (await local.chatDraft(conversation)) ?? undefined;
|
|
835
|
+
}
|
|
836
|
+
async function sealed(alive, local, conversation, turns) {
|
|
837
|
+
const held = await draftAt(local, conversation);
|
|
838
|
+
const submitted = held?.submitted ?? [];
|
|
839
|
+
const left = submitted.filter((one) => one.turn === null || !turns.has(one.turn));
|
|
840
|
+
if (held === undefined || left.length === submitted.length)
|
|
841
|
+
return false;
|
|
842
|
+
await kept(alive, local, conversation, withSubmitted(held, left));
|
|
843
|
+
return true;
|
|
844
|
+
}
|
|
845
|
+
function moved(alive, agent, operation, to) {
|
|
846
|
+
const from = newChat(agent);
|
|
847
|
+
return alive.deps.durably(async (local) => {
|
|
848
|
+
const held = await draftAt(local, from);
|
|
849
|
+
const earlier = held?.moves ?? [];
|
|
850
|
+
if (earlier.includes(operation))
|
|
851
|
+
return;
|
|
852
|
+
const blank = {
|
|
853
|
+
conversation: from,
|
|
854
|
+
text: '',
|
|
855
|
+
revision: chatDraftRevision(),
|
|
856
|
+
};
|
|
857
|
+
const { moves: _answered, ...unsent } = held ?? blank;
|
|
858
|
+
const taken = (await draftAt(local, to)) !== undefined || empty(unsent)
|
|
859
|
+
? []
|
|
860
|
+
: [{ ...unsent, conversation: to }];
|
|
861
|
+
const left = { ...(taken.length === 0 ? unsent : blank), moves: [...earlier, operation] };
|
|
862
|
+
await local.keepChatDrafts([left, ...taken]);
|
|
863
|
+
shownDraft(alive, from, left);
|
|
864
|
+
for (const one of taken)
|
|
865
|
+
shownDraft(alive, to, one);
|
|
866
|
+
alive.deps.publish(conversationChangeSet([from, to]));
|
|
867
|
+
});
|
|
868
|
+
}
|
|
869
|
+
function withDraft(alive, conversation, next) {
|
|
870
|
+
return alive.deps.durably(async (local) => {
|
|
871
|
+
const following = await next(await draftAt(local, conversation), local);
|
|
872
|
+
if (following === undefined)
|
|
873
|
+
return;
|
|
874
|
+
await kept(alive, local, conversation, following);
|
|
875
|
+
alive.deps.publish(conversationChangeSet([conversation]));
|
|
876
|
+
});
|
|
877
|
+
}
|
|
878
|
+
async function submitted(alive, conversation, operation, draft) {
|
|
879
|
+
let submission;
|
|
880
|
+
await withDraft(alive, conversation, (current) => {
|
|
881
|
+
submission = current?.submitted?.find((one) => one.operation === operation);
|
|
882
|
+
if (submission !== undefined)
|
|
883
|
+
return undefined;
|
|
884
|
+
const said = draft ?? current;
|
|
885
|
+
submission = { operation, text: said?.text ?? '', turn: null };
|
|
886
|
+
return {
|
|
887
|
+
conversation,
|
|
888
|
+
...(draft === undefined || current === undefined
|
|
889
|
+
? { text: '', revision: chatDraftRevision() }
|
|
890
|
+
: { text: current.text, revision: current.revision }),
|
|
891
|
+
submitted: [...(current?.submitted ?? []), submission],
|
|
892
|
+
};
|
|
893
|
+
});
|
|
894
|
+
return submission;
|
|
895
|
+
}
|
|
896
|
+
async function dispatched(alive, conversation, submission) {
|
|
897
|
+
const dispatch = alive.sendingBegan(submission.operation);
|
|
898
|
+
try {
|
|
899
|
+
const result = await invoke(alive, 'conversations/send', conversation, {
|
|
900
|
+
operation_id: submission.operation,
|
|
901
|
+
text: submission.text,
|
|
816
902
|
});
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
const result = await invoke('conversation.send', conversation, {
|
|
823
|
-
text: submission.text,
|
|
824
|
-
operation_id: submission.operation,
|
|
825
|
-
});
|
|
826
|
-
dispatch.answered = result.kind !== 'transport-uncertainty';
|
|
827
|
-
if (result.kind === 'revoked-authorization')
|
|
828
|
-
return result;
|
|
829
|
-
if (result.kind !== 'action' && result.retryable) {
|
|
830
|
-
deps.unanswered();
|
|
831
|
-
return result;
|
|
832
|
-
}
|
|
833
|
-
const turn = answered(result, 'turn');
|
|
834
|
-
await withDraft(conversation, async (current, local) => {
|
|
835
|
-
const held = current?.submitted?.find((one) => one.operation === submission.operation);
|
|
836
|
-
if (current === undefined || held === undefined || held.turn !== null)
|
|
837
|
-
return undefined;
|
|
838
|
-
const others = current.submitted.filter((one) => one !== held);
|
|
839
|
-
if (result.kind !== 'action')
|
|
840
|
-
return returned(current, held, others);
|
|
841
|
-
const arrived = turn !== null && (await local.turnArrived(conversation, turn));
|
|
842
|
-
return withSubmitted(current, turn === null || arrived
|
|
843
|
-
? others
|
|
844
|
-
: current.submitted.map((one) => (one === held ? { ...held, turn } : one)));
|
|
845
|
-
});
|
|
903
|
+
dispatch.set({ answered: result.kind !== 'transport-uncertainty' });
|
|
904
|
+
if (result.kind === 'revoked-authorization')
|
|
905
|
+
return result;
|
|
906
|
+
if (result.kind !== 'action' && result.retryable) {
|
|
907
|
+
alive.deps.unanswered();
|
|
846
908
|
return result;
|
|
847
909
|
}
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
910
|
+
const turn = answered(result, 'message_id');
|
|
911
|
+
await withDraft(alive, conversation, async (current, local) => {
|
|
912
|
+
const held = current?.submitted?.find((one) => one.operation === submission.operation);
|
|
913
|
+
if (current === undefined || held === undefined || held.turn !== null)
|
|
914
|
+
return undefined;
|
|
915
|
+
const others = current.submitted.filter((one) => one !== held);
|
|
916
|
+
if (result.kind !== 'action')
|
|
917
|
+
return returned(current, held, others);
|
|
918
|
+
const arrived = turn !== null && (await local.turnArrived(conversation, turn));
|
|
919
|
+
return withSubmitted(current, turn === null || arrived
|
|
920
|
+
? others
|
|
921
|
+
: current.submitted.map((one) => (one === held ? { ...held, turn } : one)));
|
|
922
|
+
});
|
|
923
|
+
return result;
|
|
852
924
|
}
|
|
853
|
-
|
|
854
|
-
|
|
925
|
+
finally {
|
|
926
|
+
alive.sendingEnded(submission.operation, dispatch);
|
|
855
927
|
}
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
928
|
+
}
|
|
929
|
+
function invoke(alive, name, conversation, extra = { operation_id: changeBatchIdentity() }) {
|
|
930
|
+
return alive.deps.call(name, { conversation_id: conversation, ...extra });
|
|
931
|
+
}
|
|
932
|
+
export function conversationSurface(alive) {
|
|
933
|
+
return {
|
|
934
|
+
list() {
|
|
859
935
|
return new Promise((settle) => {
|
|
860
936
|
const answer = (listed) => {
|
|
861
|
-
|
|
862
|
-
listWatchers.delete(answer);
|
|
937
|
+
alive.listeners.leave(answer);
|
|
863
938
|
settle(listed);
|
|
864
939
|
};
|
|
865
|
-
|
|
866
|
-
|
|
940
|
+
alive.listeners.join(answer);
|
|
941
|
+
conversationsLearned(alive);
|
|
867
942
|
});
|
|
868
943
|
},
|
|
869
944
|
async read(conversation, options = {}) {
|
|
870
|
-
const
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
945
|
+
const { text, ...windowed } = options;
|
|
946
|
+
const asked = askedOf(windowed);
|
|
947
|
+
const phrase = phraseOf(text);
|
|
948
|
+
return alive.deps.durably(async (local) => {
|
|
949
|
+
const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before, phrase);
|
|
950
|
+
const total = asked.total ? await local.messageCount(conversation, phrase) : null;
|
|
951
|
+
const marked = await local.jobMarkers(conversation);
|
|
952
|
+
return structuredClone(viewOf(alive, conversation, messages, frameOf(asked, messages, more, total), (await local.chatDraft(conversation)) ?? null, await endedIn(alive, conversation), marked));
|
|
875
953
|
});
|
|
876
954
|
},
|
|
877
955
|
async create(input) {
|
|
878
|
-
const result = await deps.call('
|
|
879
|
-
const conversation = answered(result, '
|
|
956
|
+
const result = await alive.deps.call('conversations/create', input);
|
|
957
|
+
const conversation = answered(result, 'conversation_id');
|
|
880
958
|
if (conversation === null)
|
|
881
959
|
return result;
|
|
882
|
-
|
|
883
|
-
|
|
960
|
+
const listed = (await heldList(alive)) ?? [];
|
|
961
|
+
if (!listed.some((entry) => entry.conversation === conversation)) {
|
|
962
|
+
alive.creating(conversation);
|
|
963
|
+
}
|
|
884
964
|
const creation = creationOf(input);
|
|
885
965
|
if (creation !== null)
|
|
886
|
-
await moved(creation.agent, creation.operation, conversation);
|
|
966
|
+
await moved(alive, creation.agent, creation.operation, conversation);
|
|
887
967
|
return result;
|
|
888
968
|
},
|
|
889
969
|
async send(conversation, operation = changeBatchIdentity(), draft) {
|
|
890
|
-
if (deps.full())
|
|
970
|
+
if (alive.deps.full())
|
|
891
971
|
return DURABLE_WRITE;
|
|
892
|
-
if (deps.online()) {
|
|
893
|
-
const held = await submitted(conversation, operation, draft).catch((error) => {
|
|
972
|
+
if (alive.deps.online()) {
|
|
973
|
+
const held = await submitted(alive, conversation, operation, draft).catch((error) => {
|
|
894
974
|
if (refusedForSpace(error))
|
|
895
975
|
return null;
|
|
896
976
|
throw error;
|
|
897
977
|
});
|
|
898
978
|
if (held === null)
|
|
899
979
|
return DURABLE_WRITE;
|
|
900
|
-
|
|
901
|
-
if (!earlier || !deps.expired(operation))
|
|
902
|
-
return dispatched(conversation, submission);
|
|
903
|
-
await forgotten(conversation, submission);
|
|
904
|
-
return EXPIRED;
|
|
980
|
+
return dispatched(alive, conversation, held);
|
|
905
981
|
}
|
|
906
982
|
if (draft !== undefined) {
|
|
907
|
-
await withDraft(conversation, (current) => current?.submitted?.some((one) => one.operation === operation) === true
|
|
983
|
+
await withDraft(alive, conversation, (current) => current?.submitted?.some((one) => one.operation === operation) === true
|
|
908
984
|
? undefined
|
|
909
985
|
: returned(current ?? { conversation, text: '', revision: '' }, { operation, ...draft, turn: null }, current?.submitted ?? []));
|
|
910
986
|
}
|
|
911
987
|
return OFFLINE;
|
|
912
988
|
},
|
|
913
989
|
answer(conversation, answer) {
|
|
914
|
-
return invoke('
|
|
990
|
+
return invoke(alive, 'conversations/interaction/answer', conversation, {
|
|
991
|
+
operation_id: changeBatchIdentity(),
|
|
992
|
+
...answer,
|
|
993
|
+
});
|
|
915
994
|
},
|
|
916
995
|
interrupt(conversation) {
|
|
917
|
-
return invoke('
|
|
996
|
+
return invoke(alive, 'conversations/interrupt', conversation);
|
|
918
997
|
},
|
|
919
998
|
kill(conversation) {
|
|
920
|
-
return invoke('
|
|
921
|
-
},
|
|
922
|
-
async cancel(conversation, turn) {
|
|
923
|
-
const result = await invoke('conversation.cancel', conversation, { turn });
|
|
924
|
-
if (result.kind !== 'action')
|
|
925
|
-
return result;
|
|
926
|
-
await withDraft(conversation, (current) => {
|
|
927
|
-
const held = current?.submitted?.find((one) => one.turn === turn);
|
|
928
|
-
if (current === undefined || held === undefined)
|
|
929
|
-
return undefined;
|
|
930
|
-
return returned(current, held, current.submitted.filter((one) => one !== held));
|
|
931
|
-
});
|
|
932
|
-
return result;
|
|
999
|
+
return invoke(alive, 'conversations/kill', conversation);
|
|
933
1000
|
},
|
|
934
1001
|
delete(conversation) {
|
|
935
|
-
return invoke('
|
|
1002
|
+
return invoke(alive, 'conversations/delete', conversation);
|
|
936
1003
|
},
|
|
937
1004
|
};
|
|
938
|
-
|
|
1005
|
+
}
|
|
1006
|
+
export function chatDraftSurface(alive) {
|
|
1007
|
+
return {
|
|
939
1008
|
write(conversation, draft, revision = chatDraftRevision()) {
|
|
940
|
-
return withDraft(conversation, (current) => ({
|
|
1009
|
+
return withDraft(alive, conversation, (current) => ({
|
|
941
1010
|
conversation,
|
|
942
1011
|
text: draft.text,
|
|
943
1012
|
revision,
|
|
@@ -945,7 +1014,7 @@ export function createConversationStore(deps) {
|
|
|
945
1014
|
}));
|
|
946
1015
|
},
|
|
947
1016
|
prepend(conversation, draft) {
|
|
948
|
-
return withDraft(conversation, (current) => ({
|
|
1017
|
+
return withDraft(alive, conversation, (current) => ({
|
|
949
1018
|
conversation,
|
|
950
1019
|
...(current === undefined ? { text: draft.text } : prepended(current, draft)),
|
|
951
1020
|
revision: chatDraftRevision(),
|
|
@@ -953,248 +1022,170 @@ export function createConversationStore(deps) {
|
|
|
953
1022
|
}));
|
|
954
1023
|
},
|
|
955
1024
|
discard(conversation) {
|
|
956
|
-
return withDraft(conversation, (current) => current === undefined
|
|
1025
|
+
return withDraft(alive, conversation, (current) => current === undefined
|
|
957
1026
|
? undefined
|
|
958
1027
|
: { conversation, text: '', revision: chatDraftRevision(), ...pending(current) });
|
|
959
1028
|
},
|
|
960
1029
|
};
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
1030
|
+
}
|
|
1031
|
+
export async function conversationsOpened(alive, local) {
|
|
1032
|
+
const { conversationList, conversationCursor } = await local.facts();
|
|
1033
|
+
if (conversationList === null || alive.known())
|
|
1034
|
+
return;
|
|
1035
|
+
alive.deps.stream()?.hold(conversationCursor);
|
|
1036
|
+
conversationsLearned(alive);
|
|
1037
|
+
}
|
|
1038
|
+
export async function reconcileConversations(alive) {
|
|
1039
|
+
if (!alive.known())
|
|
1040
|
+
return;
|
|
1041
|
+
const local = await alive.deps.local();
|
|
1042
|
+
const [held, kept, { conversationList }] = await Promise.all([
|
|
1043
|
+
local.conversations(),
|
|
1044
|
+
local.chatDrafts(),
|
|
1045
|
+
local.facts(),
|
|
1046
|
+
]);
|
|
1047
|
+
const list = conversationList ?? [];
|
|
1048
|
+
const entries = new Map(list.map((entry) => [entry.conversation, entry]));
|
|
1049
|
+
const named = new Set(entries.keys());
|
|
1050
|
+
const reads = new Map(held.map((one) => [one.conversation, one]));
|
|
1051
|
+
const { replicated } = alive.held.now();
|
|
1052
|
+
const leaving = [
|
|
1053
|
+
...new Set([...replicated, ...reads.keys(), ...kept.map(({ conversation }) => conversation)]),
|
|
1054
|
+
].filter((conversation) => !named.has(conversation) &&
|
|
1055
|
+
!alive.isCreating(conversation) &&
|
|
1056
|
+
(!conversation.startsWith(NEW_CHAT) || retiredChat(conversation, list)));
|
|
1057
|
+
for (const conversation of named) {
|
|
1058
|
+
if (!replicated.has(conversation) &&
|
|
1059
|
+
missed(alive, conversation, reads.get(conversation), entries.get(conversation))) {
|
|
1060
|
+
alive.fellBehind(conversation);
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
alive.held.set({ replicated: named });
|
|
1064
|
+
handReplicated(alive);
|
|
1065
|
+
if (leaving.length > 0)
|
|
1066
|
+
await remove(alive, leaving);
|
|
1067
|
+
}
|
|
1068
|
+
export async function catchUpConversations(alive, progress) {
|
|
1069
|
+
if (alive.deps.full()) {
|
|
1070
|
+
if (!alive.current())
|
|
1071
|
+
progress.complete = false;
|
|
1072
|
+
return;
|
|
1073
|
+
}
|
|
1074
|
+
const owed = (conversation) => alive.behind(conversation) && alive.held.now().replicated.has(conversation);
|
|
1075
|
+
const read = async (conversations) => {
|
|
1076
|
+
if (conversations.length === 0 || alive.deps.closed())
|
|
1077
|
+
return;
|
|
1078
|
+
const failed = await caughtUp(alive, conversations);
|
|
1079
|
+
if (failed?.kind === 'transport-uncertainty')
|
|
1080
|
+
progress.reachable = false;
|
|
1081
|
+
};
|
|
1082
|
+
const asking = alive.behindAll().filter(owed).sort();
|
|
1083
|
+
const awaited = asking.filter((conversation) => alive.catching(conversation) !== undefined);
|
|
1084
|
+
await Promise.all([
|
|
1085
|
+
read(asking.filter((conversation) => alive.catching(conversation) === undefined)),
|
|
1086
|
+
Promise.all(awaited.map((conversation) => alive.catching(conversation))).then(() => read(awaited.filter((conversation) => owed(conversation) && alive.catching(conversation) === undefined))),
|
|
1087
|
+
]);
|
|
1088
|
+
if (!alive.current())
|
|
1089
|
+
progress.complete = false;
|
|
1090
|
+
}
|
|
1091
|
+
export async function resendConversations(alive, progress) {
|
|
1092
|
+
const local = await alive.deps.local();
|
|
1093
|
+
const tried = new Set();
|
|
1094
|
+
for (const { conversation } of await local.chatDrafts()) {
|
|
1095
|
+
for (;;) {
|
|
1096
|
+
const submission = (await local.chatDraft(conversation))?.submitted?.find((one) => one.turn === null && !alive.sending(one.operation) && !tried.has(one.operation));
|
|
1097
|
+
if (submission === undefined)
|
|
1098
|
+
break;
|
|
1099
|
+
if (alive.deps.closed())
|
|
989
1100
|
return;
|
|
990
|
-
|
|
991
|
-
const
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
for (const conversation of named) {
|
|
997
|
-
if (!replicated.has(conversation) && missed(conversation))
|
|
998
|
-
behind.add(conversation);
|
|
999
|
-
}
|
|
1000
|
-
replicated = named;
|
|
1001
|
-
handReplicated();
|
|
1002
|
-
if (leaving.length > 0)
|
|
1003
|
-
await remove(leaving);
|
|
1004
|
-
},
|
|
1005
|
-
async catchUp(progress) {
|
|
1006
|
-
if (deps.full()) {
|
|
1007
|
-
if (behind.size > 0)
|
|
1008
|
-
progress.complete = false;
|
|
1101
|
+
tried.add(submission.operation);
|
|
1102
|
+
const result = await dispatched(alive, conversation, submission);
|
|
1103
|
+
if (result.kind === 'revoked-authorization')
|
|
1104
|
+
return;
|
|
1105
|
+
if (result.kind === 'transport-uncertainty') {
|
|
1106
|
+
progress.reachable = false;
|
|
1009
1107
|
return;
|
|
1010
1108
|
}
|
|
1011
|
-
|
|
1012
|
-
const read = async (conversations) => {
|
|
1013
|
-
if (conversations.length === 0 || deps.closed())
|
|
1014
|
-
return;
|
|
1015
|
-
const failed = await caughtUp(conversations);
|
|
1016
|
-
if (failed?.kind === 'transport-uncertainty')
|
|
1017
|
-
progress.reachable = false;
|
|
1018
|
-
};
|
|
1019
|
-
const asking = [...behind].filter(owed).sort();
|
|
1020
|
-
const awaited = asking.filter((conversation) => catching.has(conversation));
|
|
1021
|
-
await Promise.all([
|
|
1022
|
-
read(asking.filter((conversation) => !catching.has(conversation))),
|
|
1023
|
-
Promise.all(awaited.map((conversation) => catching.get(conversation))).then(() => read(awaited.filter((conversation) => owed(conversation) && !catching.has(conversation)))),
|
|
1024
|
-
]);
|
|
1025
|
-
if (behind.size > 0)
|
|
1109
|
+
if (result.kind !== 'action' && result.retryable) {
|
|
1026
1110
|
progress.complete = false;
|
|
1027
|
-
},
|
|
1028
|
-
async resend(progress) {
|
|
1029
|
-
for (const [conversation, held] of [...drafts]) {
|
|
1030
|
-
for (const submission of held.submitted ?? []) {
|
|
1031
|
-
if (submission.turn !== null || sending.has(submission.operation))
|
|
1032
|
-
continue;
|
|
1033
|
-
if (deps.closed())
|
|
1034
|
-
return;
|
|
1035
|
-
if (deps.expired(submission.operation)) {
|
|
1036
|
-
await forgotten(conversation, submission);
|
|
1037
|
-
continue;
|
|
1038
|
-
}
|
|
1039
|
-
const result = await dispatched(conversation, submission);
|
|
1040
|
-
if (result.kind === 'revoked-authorization')
|
|
1041
|
-
return;
|
|
1042
|
-
if (result.kind === 'transport-uncertainty') {
|
|
1043
|
-
progress.reachable = false;
|
|
1044
|
-
return;
|
|
1045
|
-
}
|
|
1046
|
-
if (result.kind !== 'action' && result.retryable) {
|
|
1047
|
-
progress.complete = false;
|
|
1048
|
-
return;
|
|
1049
|
-
}
|
|
1050
|
-
}
|
|
1051
|
-
}
|
|
1052
|
-
},
|
|
1053
|
-
current() {
|
|
1054
|
-
return behind.size === 0;
|
|
1055
|
-
},
|
|
1056
|
-
known() {
|
|
1057
|
-
return listKnown;
|
|
1058
|
-
},
|
|
1059
|
-
reopened() {
|
|
1060
|
-
for (const [operation, dispatch] of sending)
|
|
1061
|
-
if (!dispatch.answered)
|
|
1062
|
-
sending.delete(operation);
|
|
1063
|
-
},
|
|
1064
|
-
lost() {
|
|
1065
|
-
catching.clear();
|
|
1066
|
-
},
|
|
1067
|
-
watch(conversation, options, deliver) {
|
|
1068
|
-
const asked = askedOf(options);
|
|
1069
|
-
const key = JSON.stringify([conversation, asked.limit, asked.after ?? null, asked.total]);
|
|
1070
|
-
const view = views.get(key) ?? {
|
|
1071
|
-
conversation,
|
|
1072
|
-
asked,
|
|
1073
|
-
watchers: new Set(),
|
|
1074
|
-
window: null,
|
|
1075
|
-
more: false,
|
|
1076
|
-
total: null,
|
|
1077
|
-
};
|
|
1078
|
-
views.set(key, view);
|
|
1079
|
-
const watcher = { notify: deliver, ready: false };
|
|
1080
|
-
view.watchers.add(watcher);
|
|
1081
|
-
void deps
|
|
1082
|
-
.durably(async (local) => {
|
|
1083
|
-
if (view.window !== null)
|
|
1084
|
-
return;
|
|
1085
|
-
const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before);
|
|
1086
|
-
view.window = messages;
|
|
1087
|
-
view.more = more;
|
|
1088
|
-
view.total = asked.total ? await local.messageCount(conversation) : null;
|
|
1089
|
-
})
|
|
1090
|
-
.then(() => {
|
|
1091
|
-
if (deps.closed() || !view.watchers.has(watcher))
|
|
1092
|
-
return;
|
|
1093
|
-
watcher.ready = true;
|
|
1094
|
-
const window = view.window ?? [];
|
|
1095
|
-
watcher.notify(viewOf(conversation, window, sharedFrame(view, window)));
|
|
1096
|
-
})
|
|
1097
|
-
.catch(() => { });
|
|
1098
|
-
return () => {
|
|
1099
|
-
if (!view.watchers.delete(watcher))
|
|
1100
|
-
return;
|
|
1101
|
-
if (view.watchers.size === 0)
|
|
1102
|
-
views.delete(key);
|
|
1103
|
-
};
|
|
1104
|
-
},
|
|
1105
|
-
watchList(deliver) {
|
|
1106
|
-
let ended = false;
|
|
1107
|
-
void agentsReady().then(() => {
|
|
1108
|
-
if (ended)
|
|
1109
|
-
return;
|
|
1110
|
-
unlisted.add(deliver);
|
|
1111
|
-
learned();
|
|
1112
|
-
});
|
|
1113
|
-
return () => {
|
|
1114
|
-
ended = true;
|
|
1115
|
-
unlisted.delete(deliver);
|
|
1116
|
-
listWatchers.delete(deliver);
|
|
1117
|
-
};
|
|
1118
|
-
},
|
|
1119
|
-
watchAgents(deliver) {
|
|
1120
|
-
unnamed.add(deliver);
|
|
1121
|
-
void agentsReady().then(learned);
|
|
1122
|
-
return () => {
|
|
1123
|
-
unnamed.delete(deliver);
|
|
1124
|
-
agentWatchers.delete(deliver);
|
|
1125
|
-
};
|
|
1126
|
-
},
|
|
1127
|
-
learned,
|
|
1128
|
-
declared() {
|
|
1129
|
-
if (JSON.stringify(deps.agents()) === agentsFrom)
|
|
1130
1111
|
return;
|
|
1131
|
-
agentsRead = readAgents().catch(() => {
|
|
1132
|
-
agentsRead = null;
|
|
1133
|
-
});
|
|
1134
|
-
},
|
|
1135
|
-
changed(set) {
|
|
1136
|
-
for (const conversation of set.conversations)
|
|
1137
|
-
emit(conversation);
|
|
1138
|
-
const declaration = deps.agents();
|
|
1139
|
-
if (declaration !== undefined &&
|
|
1140
|
-
set.documents.some((change) => change.locator === declaration.locator)) {
|
|
1141
|
-
agentsRead = readAgents().catch(() => {
|
|
1142
|
-
agentsRead = null;
|
|
1143
|
-
});
|
|
1144
1112
|
}
|
|
1145
|
-
}
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
return (event.kind !== 'list' &&
|
|
1177
|
-
listKnown &&
|
|
1178
|
-
(listing.size !== replicated.size ||
|
|
1179
|
-
[...listing.keys()].some((conversation) => !replicated.has(conversation))));
|
|
1180
|
-
}
|
|
1181
|
-
heard(event);
|
|
1182
|
-
return false;
|
|
1183
|
-
},
|
|
1184
|
-
searchable() {
|
|
1185
|
-
return [...(listKnown ? listing.keys() : stored)].sort();
|
|
1186
|
-
},
|
|
1187
|
-
acquired(conversation) {
|
|
1188
|
-
return reads.has(conversation) && !partials.has(conversation);
|
|
1189
|
-
},
|
|
1190
|
-
close() {
|
|
1191
|
-
views.clear();
|
|
1192
|
-
lives.clear();
|
|
1193
|
-
unsent.clear();
|
|
1194
|
-
listWatchers.clear();
|
|
1195
|
-
unlisted.clear();
|
|
1196
|
-
agentWatchers.clear();
|
|
1197
|
-
unnamed.clear();
|
|
1198
|
-
},
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
export function watchConversation(alive, conversation, options, deliver) {
|
|
1117
|
+
const asked = askedOf(options);
|
|
1118
|
+
const key = JSON.stringify([conversation, asked.limit, asked.after ?? null, asked.total]);
|
|
1119
|
+
const { view, watcher } = alive.viewJoined(key, conversation, asked, deliver);
|
|
1120
|
+
const { held } = view;
|
|
1121
|
+
const reading = new AbortController();
|
|
1122
|
+
void untilAnswered(() => alive.deps.durably(async (local) => {
|
|
1123
|
+
if (held.now().window !== null)
|
|
1124
|
+
return;
|
|
1125
|
+
const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before);
|
|
1126
|
+
held.set({ draft: (await local.chatDraft(conversation)) ?? null });
|
|
1127
|
+
held.set({ ended: await endedIn(alive, conversation) });
|
|
1128
|
+
held.set({ window: messages });
|
|
1129
|
+
held.set({ marked: await local.jobMarkers(conversation) });
|
|
1130
|
+
held.set({ more });
|
|
1131
|
+
held.set({ total: asked.total ? await local.messageCount(conversation) : null });
|
|
1132
|
+
}), reading.signal)
|
|
1133
|
+
.then(() => {
|
|
1134
|
+
if (alive.deps.closed() || !view.admit(watcher))
|
|
1135
|
+
return;
|
|
1136
|
+
const { window, draft, ended, marked } = held.now();
|
|
1137
|
+
const shown = window ?? [];
|
|
1138
|
+
watcher.notify(viewOf(alive, conversation, shown, sharedFrame(view, shown), draft, ended, marked));
|
|
1139
|
+
})
|
|
1140
|
+
.catch(() => { });
|
|
1141
|
+
return () => {
|
|
1142
|
+
reading.abort();
|
|
1143
|
+
alive.viewLeft(key, view, watcher);
|
|
1199
1144
|
};
|
|
1200
1145
|
}
|
|
1146
|
+
export function watchConversationList(alive, deliver) {
|
|
1147
|
+
alive.listeners.join(deliver);
|
|
1148
|
+
conversationsLearned(alive);
|
|
1149
|
+
return () => alive.listeners.leave(deliver);
|
|
1150
|
+
}
|
|
1151
|
+
export function watchConversationAgents(alive, deliver) {
|
|
1152
|
+
alive.agentListeners.join(deliver);
|
|
1153
|
+
conversationsLearned(alive);
|
|
1154
|
+
readAgents(alive);
|
|
1155
|
+
return () => alive.agentListeners.leave(deliver);
|
|
1156
|
+
}
|
|
1157
|
+
export function conversationsChanged(alive, set) {
|
|
1158
|
+
for (const conversation of set.conversations)
|
|
1159
|
+
emit(alive, conversation);
|
|
1160
|
+
}
|
|
1161
|
+
export function receiveConversationEvent(alive, event) {
|
|
1162
|
+
if (alive.deps.closed())
|
|
1163
|
+
return;
|
|
1164
|
+
if (event.kind === 'list') {
|
|
1165
|
+
if (event.whole)
|
|
1166
|
+
alive.relistBegun();
|
|
1167
|
+
if (alive.relisting())
|
|
1168
|
+
alive.relistHeard(event.entries);
|
|
1169
|
+
if (event.more && alive.relisting())
|
|
1170
|
+
return;
|
|
1171
|
+
const whole = alive.relistTaken();
|
|
1172
|
+
listChanged(alive, {
|
|
1173
|
+
replacing: whole !== null,
|
|
1174
|
+
set: whole === null ? event.entries : [...whole.values()],
|
|
1175
|
+
gone: null,
|
|
1176
|
+
...(event.more ? { paged: true } : { cursor: event.cursor }),
|
|
1177
|
+
});
|
|
1178
|
+
return;
|
|
1179
|
+
}
|
|
1180
|
+
if (event.kind === 'listed') {
|
|
1181
|
+
alive.relistHeard([event.entry]);
|
|
1182
|
+
listChanged(alive, { replacing: false, set: [event.entry], gone: null });
|
|
1183
|
+
return;
|
|
1184
|
+
}
|
|
1185
|
+
if (event.kind === 'unlisted') {
|
|
1186
|
+
alive.relistDropped(event.conversation);
|
|
1187
|
+
listChanged(alive, { replacing: false, set: [], gone: event.conversation });
|
|
1188
|
+
return;
|
|
1189
|
+
}
|
|
1190
|
+
heard(alive, event);
|
|
1191
|
+
}
|