@agentshouse/mdruntime 0.1.0-c96e80d38e5f0 → 0.1.0-c98426a9ee68e
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 +1 -1
- package/dist/acceptance.d.ts +6 -0
- package/dist/acceptance.d.ts.map +1 -0
- package/dist/acceptance.js +389 -0
- package/dist/actions.d.ts +20 -0
- package/dist/actions.d.ts.map +1 -0
- package/dist/actions.js +248 -0
- package/dist/adapter.d.ts +22 -25
- 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 +8 -4
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +209 -78
- 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/change.d.ts +1 -0
- package/dist/change.d.ts.map +1 -1
- package/dist/chat-types.d.ts +3 -0
- package/dist/chat-types.d.ts.map +1 -0
- package/dist/chat-types.js +2 -0
- package/dist/client.d.ts +17 -2
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +3 -0
- package/dist/collection.d.ts +2 -1
- package/dist/collection.d.ts.map +1 -1
- package/dist/compile.d.ts +7 -7
- package/dist/compile.d.ts.map +1 -1
- package/dist/compile.js +229 -301
- package/dist/connection.d.ts +5 -141
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +98 -5258
- package/dist/context.d.ts +2 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +7 -0
- package/dist/conversation-row.d.ts +33 -0
- package/dist/conversation-row.d.ts.map +1 -0
- package/dist/conversation-row.js +47 -0
- package/dist/conversation.d.ts +15 -6
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +363 -216
- 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 -0
- 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 -8
- package/dist/figures.d.ts +48 -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 +2 -2
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +22 -15
- 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 +20 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/install.d.ts +56 -0
- package/dist/install.d.ts.map +1 -0
- package/dist/install.js +560 -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 +267 -0
- package/dist/listing.d.ts +53 -0
- package/dist/listing.d.ts.map +1 -0
- package/dist/listing.js +175 -0
- package/dist/live.d.ts +5 -4
- package/dist/live.d.ts.map +1 -1
- package/dist/live.js +62 -20
- 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/memory.d.ts +4 -1
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +43 -4
- package/dist/messages.d.ts +25 -1
- package/dist/messages.d.ts.map +1 -1
- package/dist/messages.js +84 -5
- package/dist/model.d.ts +2 -9
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +0 -9
- package/dist/models.d.ts +14 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.js +193 -0
- package/dist/observation.d.ts +2 -2
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +7 -2
- package/dist/opening.d.ts +4 -0
- package/dist/opening.d.ts.map +1 -0
- package/dist/opening.js +56 -0
- package/dist/outcome.d.ts +1 -0
- 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 +59 -0
- package/dist/parts.d.ts.map +1 -0
- package/dist/parts.js +118 -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 +13 -212
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +3 -524
- package/dist/questions.d.ts +32 -0
- package/dist/questions.d.ts.map +1 -0
- package/dist/questions.js +118 -0
- package/dist/reach.d.ts +12 -10
- package/dist/reach.d.ts.map +1 -1
- package/dist/reach.js +165 -109
- package/dist/react.d.ts +5 -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/reads.d.ts +36 -0
- package/dist/reads.d.ts.map +1 -0
- package/dist/reads.js +136 -0
- package/dist/recovery.d.ts.map +1 -1
- package/dist/recovery.js +9 -1
- package/dist/refresh.d.ts +1 -1
- package/dist/refresh.d.ts.map +1 -1
- package/dist/refresh.js +8 -20
- 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 +8 -8
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +50 -18
- package/dist/room-draft.d.ts +3 -2
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +4 -4
- package/dist/room-drafts.d.ts +24 -0
- package/dist/room-drafts.d.ts.map +1 -0
- package/dist/room-drafts.js +216 -0
- package/dist/rows.d.ts +14 -22
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +48 -189
- package/dist/runtime.d.ts +42 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +1 -0
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +11 -8
- package/dist/saves.d.ts +70 -0
- package/dist/saves.d.ts.map +1 -0
- package/dist/saves.js +1004 -0
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +1 -1
- package/dist/seed.d.ts +11 -2
- package/dist/seed.d.ts.map +1 -1
- package/dist/seed.js +71 -0
- package/dist/shown.d.ts +31 -0
- package/dist/shown.d.ts.map +1 -0
- package/dist/shown.js +294 -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 +43 -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 +81 -0
- package/dist/status.d.ts +0 -2
- 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 +408 -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 +43 -0
- package/dist/store/watchers.d.ts +17 -0
- package/dist/store/watchers.d.ts.map +1 -0
- package/dist/store/watchers.js +57 -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 -153
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +143 -396
- package/dist/stored.d.ts +58 -64
- package/dist/stored.d.ts.map +1 -1
- package/dist/stored.js +244 -156
- package/dist/surface.d.ts +88 -0
- package/dist/surface.d.ts.map +1 -0
- package/dist/surface.js +222 -0
- package/dist/sync.d.ts +20 -0
- package/dist/sync.d.ts.map +1 -0
- package/dist/sync.js +531 -0
- package/dist/tables.d.ts +110 -48
- package/dist/tables.d.ts.map +1 -1
- package/dist/tables.js +209 -54
- package/dist/tallies.d.ts +18 -0
- package/dist/tallies.d.ts.map +1 -0
- package/dist/tallies.js +129 -0
- package/dist/validation.d.ts.map +1 -1
- package/dist/validation.js +7 -4
- 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 +16 -1
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +6 -0
- 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/phrase.d.ts +0 -3
- package/dist/phrase.d.ts.map +0 -1
- package/dist/phrase.js +0 -8
package/dist/conversation.js
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
|
+
import { cursorOf, decodedCursor } from '@agentshouse/mdmodel';
|
|
1
2
|
import { conversationChangeSet } from './change-set.js';
|
|
3
|
+
import { heldFacts } from './conversation-row.js';
|
|
2
4
|
import { runtimeError } from './failure.js';
|
|
3
5
|
import { refusedForSpace } from './durability.js';
|
|
4
6
|
import { changeBatchIdentity, chatDraftRevision } from './identity.js';
|
|
5
7
|
import { OFFLINE } from './outcome.js';
|
|
8
|
+
import { jobsOf, runningLine } from './parts.js';
|
|
9
|
+
import { openQuestion } from './questions.js';
|
|
6
10
|
import { untilAnswered } from './recovery.js';
|
|
7
|
-
import { capped,
|
|
11
|
+
import { capped, FROM_START, messageOrder, precedes } from './windows.js';
|
|
8
12
|
const QUIET = {
|
|
9
13
|
turn: null,
|
|
10
14
|
questions: [],
|
|
@@ -33,6 +37,7 @@ export function unheldView(conversation) {
|
|
|
33
37
|
turn: null,
|
|
34
38
|
questions: [],
|
|
35
39
|
process: null,
|
|
40
|
+
processEnded: null,
|
|
36
41
|
commands: [],
|
|
37
42
|
options: [],
|
|
38
43
|
starting: null,
|
|
@@ -40,21 +45,22 @@ export function unheldView(conversation) {
|
|
|
40
45
|
link: null,
|
|
41
46
|
deliveryUnknown: [],
|
|
42
47
|
chatDraft: null,
|
|
48
|
+
jobs: [],
|
|
43
49
|
};
|
|
44
50
|
}
|
|
45
51
|
export const SESSION_STARTED = 'Session started';
|
|
46
52
|
const NEW_CHAT = 'new:';
|
|
47
53
|
export const HISTORY_CONVERSATIONS = 64;
|
|
54
|
+
// A row is read at a glance and each telling clones the list to every observing tab, while a running line changes at each Kit flush.
|
|
55
|
+
const ROW_INTERVAL_MS = 1_000;
|
|
48
56
|
export function newChat(agent) {
|
|
49
57
|
return `${NEW_CHAT}${agent}`;
|
|
50
58
|
}
|
|
51
59
|
function creationOf(input) {
|
|
52
60
|
if (typeof input !== 'object' || input === null)
|
|
53
61
|
return null;
|
|
54
|
-
const { agent_id: agent, operation_id: operation
|
|
55
|
-
return typeof agent === 'string' && typeof operation === 'string'
|
|
56
|
-
? { agent, operation }
|
|
57
|
-
: null;
|
|
62
|
+
const { agent_id: agent, operation_id: operation } = input;
|
|
63
|
+
return typeof agent === 'string' && typeof operation === 'string' ? { agent, operation } : null;
|
|
58
64
|
}
|
|
59
65
|
function startedTurn(turn, operation) {
|
|
60
66
|
if (operation.op === 'begin')
|
|
@@ -221,7 +227,6 @@ function phraseOf(text) {
|
|
|
221
227
|
}
|
|
222
228
|
return text;
|
|
223
229
|
}
|
|
224
|
-
const EXPIRED = { kind: 'rejected', code: 'expired', fields: {}, retryable: false };
|
|
225
230
|
const DURABLE_WRITE = {
|
|
226
231
|
kind: 'rejected',
|
|
227
232
|
code: 'durable-write',
|
|
@@ -255,10 +260,6 @@ function returned(draft, submission, others) {
|
|
|
255
260
|
}, others);
|
|
256
261
|
}
|
|
257
262
|
export function createConversationStore(deps) {
|
|
258
|
-
const stored = new Set();
|
|
259
|
-
const reads = new Map();
|
|
260
|
-
const partials = new Set();
|
|
261
|
-
const drafts = new Map();
|
|
262
263
|
const sending = new Map();
|
|
263
264
|
const behind = new Set();
|
|
264
265
|
const catching = new Map();
|
|
@@ -266,15 +267,16 @@ export function createConversationStore(deps) {
|
|
|
266
267
|
const lives = new Map();
|
|
267
268
|
const unsent = new Map();
|
|
268
269
|
const views = new Map();
|
|
269
|
-
const
|
|
270
|
+
const heardNewest = new Map();
|
|
270
271
|
let relisting = null;
|
|
271
|
-
let listCursor = null;
|
|
272
272
|
const creations = new Set();
|
|
273
273
|
const listWatchers = new Set();
|
|
274
274
|
const unlisted = new Set();
|
|
275
275
|
let listKnown = false;
|
|
276
|
-
let
|
|
277
|
-
let
|
|
276
|
+
let listOwed = false;
|
|
277
|
+
let listTelling = false;
|
|
278
|
+
let rowTimer = null;
|
|
279
|
+
const closing = new AbortController();
|
|
278
280
|
const agentWatchers = new Set();
|
|
279
281
|
const unnamed = new Set();
|
|
280
282
|
let agentsShown = null;
|
|
@@ -285,8 +287,7 @@ export function createConversationStore(deps) {
|
|
|
285
287
|
function liveOf(conversation) {
|
|
286
288
|
return lives.get(conversation) ?? QUIET;
|
|
287
289
|
}
|
|
288
|
-
function viewOf(conversation, held, frame) {
|
|
289
|
-
const draft = drafts.get(conversation);
|
|
290
|
+
function viewOf(conversation, held, frame, draft, ended, marked) {
|
|
290
291
|
const facts = liveOf(conversation);
|
|
291
292
|
return {
|
|
292
293
|
conversation,
|
|
@@ -300,15 +301,17 @@ export function createConversationStore(deps) {
|
|
|
300
301
|
turn: structuredClone(facts.turn),
|
|
301
302
|
questions: structuredClone(facts.questions),
|
|
302
303
|
process: facts.process === null ? null : { ...facts.process },
|
|
304
|
+
processEnded: ended,
|
|
303
305
|
commands: structuredClone(facts.commands),
|
|
304
306
|
options: structuredClone(facts.options),
|
|
305
307
|
starting: facts.starting,
|
|
306
308
|
subagents: structuredClone(facts.subagents),
|
|
307
309
|
link: facts.link === null ? null : { ...facts.link },
|
|
308
310
|
deliveryUnknown: [...facts.deliveryUnknown],
|
|
309
|
-
chatDraft: draft ===
|
|
310
|
-
|
|
311
|
-
|
|
311
|
+
chatDraft: draft === null || unwritten(draft) ? null : { text: draft.text, revision: draft.revision },
|
|
312
|
+
jobs: facts.process?.running === false
|
|
313
|
+
? []
|
|
314
|
+
: jobsOf(marked.filter(({ at }) => ended === null || at > ended), facts.turn),
|
|
312
315
|
};
|
|
313
316
|
}
|
|
314
317
|
function emit(conversation) {
|
|
@@ -320,7 +323,7 @@ export function createConversationStore(deps) {
|
|
|
320
323
|
const ready = [...view.watchers].filter((watcher) => watcher.ready);
|
|
321
324
|
if (ready.length === 0)
|
|
322
325
|
continue;
|
|
323
|
-
const value = viewOf(conversation, view.window, sharedFrame(view, view.window));
|
|
326
|
+
const value = viewOf(conversation, view.window, sharedFrame(view, view.window), view.draft, view.ended, view.marked);
|
|
324
327
|
for (const watcher of ready)
|
|
325
328
|
watcher.notify(value);
|
|
326
329
|
}
|
|
@@ -330,11 +333,13 @@ export function createConversationStore(deps) {
|
|
|
330
333
|
}
|
|
331
334
|
async function slide(local, conversation, arriving) {
|
|
332
335
|
let count;
|
|
336
|
+
let marked;
|
|
333
337
|
for (const view of views.values()) {
|
|
334
338
|
if (view.conversation !== conversation || view.window === null)
|
|
335
339
|
continue;
|
|
336
340
|
if (arriving === null) {
|
|
337
341
|
view.window = [];
|
|
342
|
+
view.marked = [];
|
|
338
343
|
view.more = false;
|
|
339
344
|
if (view.total !== null)
|
|
340
345
|
view.total = 0;
|
|
@@ -344,6 +349,10 @@ export function createConversationStore(deps) {
|
|
|
344
349
|
const next = windowOf(view.window, joining, view.asked.limit);
|
|
345
350
|
view.window = next.messages;
|
|
346
351
|
view.more ||= next.dropped;
|
|
352
|
+
if (arriving.length > 0) {
|
|
353
|
+
marked ??= await local.jobMarkers(conversation);
|
|
354
|
+
view.marked = marked;
|
|
355
|
+
}
|
|
347
356
|
if (view.total !== null && arriving.length > 0) {
|
|
348
357
|
count ??= await local.messageCount(conversation);
|
|
349
358
|
view.total = count;
|
|
@@ -356,79 +365,178 @@ export function createConversationStore(deps) {
|
|
|
356
365
|
return 'asking';
|
|
357
366
|
return facts.turn === null ? 'idle' : 'working';
|
|
358
367
|
}
|
|
359
|
-
function
|
|
360
|
-
|
|
368
|
+
function rowOf(conversation) {
|
|
369
|
+
const turn = lives.get(conversation)?.turn ?? null;
|
|
370
|
+
return {
|
|
371
|
+
state: stateOf(conversation),
|
|
372
|
+
started: turn?.started ?? null,
|
|
373
|
+
line: turn === null ? null : runningLine(turn.parts),
|
|
374
|
+
question: openQuestion(liveOf(conversation).questions),
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
async function entries() {
|
|
378
|
+
const list = await heldList();
|
|
379
|
+
if (list === null)
|
|
380
|
+
return null;
|
|
381
|
+
const held = list.length === 0
|
|
382
|
+
? []
|
|
383
|
+
: await deps.durably((local) => local.conversationRows(list.map(({ conversation }) => conversation)));
|
|
384
|
+
const rows = new Map(held.map((one) => [one.conversation, one]));
|
|
385
|
+
return list.map((entry) => ({
|
|
361
386
|
conversation: entry.conversation,
|
|
362
|
-
agent: { identity: entry.agent, name: entry.agentName },
|
|
387
|
+
agent: { identity: entry.agent, name: entry.agentName, retired: entry.agentRetired },
|
|
363
388
|
name: entry.name,
|
|
364
389
|
created: entry.created,
|
|
365
390
|
finished: entry.finished,
|
|
366
391
|
outcome: entry.outcome,
|
|
367
392
|
waiting: entry.waiting,
|
|
368
|
-
|
|
369
|
-
|
|
393
|
+
...rowOf(entry.conversation),
|
|
394
|
+
...heldFacts(rows.get(entry.conversation), entry),
|
|
370
395
|
}));
|
|
371
396
|
}
|
|
372
|
-
function
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
397
|
+
async function heldList() {
|
|
398
|
+
const stored = (await (await deps.local()).facts()).conversationList;
|
|
399
|
+
if (stored !== null)
|
|
400
|
+
return stored;
|
|
401
|
+
return !listKnown && deps.stream() !== undefined && deps.online() ? null : [];
|
|
402
|
+
}
|
|
403
|
+
async function endedIn(conversation) {
|
|
404
|
+
const list = await heldList();
|
|
405
|
+
return list?.find((entry) => entry.conversation === conversation)?.processEnded ?? null;
|
|
406
|
+
}
|
|
407
|
+
function listChanged(change) {
|
|
408
|
+
void deps
|
|
409
|
+
.durably(async (local) => {
|
|
410
|
+
const { conversationList, conversationCursor } = await local.facts();
|
|
411
|
+
const kept = change.replacing ? [] : (conversationList ?? []);
|
|
412
|
+
const held = new Map(kept.map((entry) => [entry.conversation, entry]));
|
|
413
|
+
for (const entry of change.set) {
|
|
414
|
+
held.set(entry.conversation, { ...entry });
|
|
415
|
+
creations.delete(entry.conversation);
|
|
416
|
+
}
|
|
417
|
+
if (change.gone !== null)
|
|
418
|
+
held.delete(change.gone);
|
|
419
|
+
const list = [...held.values()];
|
|
420
|
+
if (JSON.stringify(list) !== JSON.stringify(conversationList)) {
|
|
421
|
+
await local.fact('conversationList', list);
|
|
422
|
+
}
|
|
423
|
+
if (change.cursor !== undefined) {
|
|
424
|
+
if (change.cursor !== conversationCursor) {
|
|
425
|
+
await local.fact('conversationCursor', change.cursor);
|
|
426
|
+
}
|
|
427
|
+
deps.stream()?.hold(change.cursor);
|
|
428
|
+
}
|
|
429
|
+
return list;
|
|
430
|
+
})
|
|
431
|
+
.catch(async () => (await (await deps.local()).facts()).conversationList ?? [])
|
|
432
|
+
.then((list) => {
|
|
433
|
+
if (deps.closed() || change.paged === true)
|
|
379
434
|
return;
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
}
|
|
387
|
-
|
|
435
|
+
endedAnew(list);
|
|
436
|
+
if (change.cursor !== undefined) {
|
|
437
|
+
listKnown = true;
|
|
438
|
+
settleBehind(list);
|
|
439
|
+
openingEnded();
|
|
440
|
+
deps.settled();
|
|
441
|
+
}
|
|
442
|
+
emitList();
|
|
443
|
+
if (!listKnown)
|
|
444
|
+
return;
|
|
445
|
+
retiredDrafts(list);
|
|
446
|
+
if (change.cursor === undefined &&
|
|
447
|
+
(list.length !== replicated.size ||
|
|
448
|
+
list.some(({ conversation }) => !replicated.has(conversation)))) {
|
|
449
|
+
deps.again();
|
|
450
|
+
}
|
|
451
|
+
}, () => { });
|
|
452
|
+
}
|
|
453
|
+
function endedAnew(list) {
|
|
454
|
+
const entries = new Map(list.map((entry) => [entry.conversation, entry]));
|
|
455
|
+
for (const view of views.values()) {
|
|
456
|
+
const ended = entries.get(view.conversation)?.processEnded ?? null;
|
|
457
|
+
if (view.window === null || ended === view.ended)
|
|
458
|
+
continue;
|
|
459
|
+
view.ended = ended;
|
|
460
|
+
emit(view.conversation);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
let listShown = Promise.resolve();
|
|
464
|
+
function shownList(work) {
|
|
465
|
+
const reading = heldList();
|
|
466
|
+
listShown = listShown.then(async () => {
|
|
467
|
+
const list = await reading.catch(() => null);
|
|
468
|
+
if (list === null || deps.closed())
|
|
469
|
+
return;
|
|
470
|
+
work(list);
|
|
388
471
|
});
|
|
389
472
|
}
|
|
390
|
-
function
|
|
391
|
-
|
|
392
|
-
|
|
473
|
+
async function told() {
|
|
474
|
+
try {
|
|
475
|
+
while (listOwed && !deps.closed()) {
|
|
476
|
+
listOwed = false;
|
|
477
|
+
const value = await untilAnswered(entries, closing.signal);
|
|
478
|
+
if (deps.closed())
|
|
479
|
+
return;
|
|
480
|
+
if (value === null)
|
|
481
|
+
continue;
|
|
482
|
+
for (const watcher of listWatchers)
|
|
483
|
+
watcher(value);
|
|
484
|
+
for (const watcher of [...unlisted]) {
|
|
485
|
+
unlisted.delete(watcher);
|
|
486
|
+
listWatchers.add(watcher);
|
|
487
|
+
watcher(value);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
finally {
|
|
492
|
+
listTelling = false;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
function tellList() {
|
|
496
|
+
if (deps.closed() || (listWatchers.size === 0 && unlisted.size === 0))
|
|
497
|
+
return;
|
|
498
|
+
listOwed = true;
|
|
499
|
+
if (listTelling)
|
|
500
|
+
return;
|
|
501
|
+
listTelling = true;
|
|
502
|
+
void told().catch(() => { });
|
|
503
|
+
}
|
|
504
|
+
function tellSoon() {
|
|
505
|
+
if (rowTimer !== null)
|
|
506
|
+
return;
|
|
507
|
+
rowTimer = setTimeout(() => {
|
|
508
|
+
rowTimer = null;
|
|
509
|
+
tellList();
|
|
510
|
+
}, ROW_INTERVAL_MS);
|
|
393
511
|
}
|
|
394
512
|
function emitList() {
|
|
395
513
|
if (deps.closed())
|
|
396
514
|
return;
|
|
397
|
-
|
|
398
|
-
const value = entries();
|
|
399
|
-
for (const watcher of listWatchers)
|
|
400
|
-
watcher(value);
|
|
401
|
-
}
|
|
515
|
+
tellList();
|
|
402
516
|
emitAgents();
|
|
403
517
|
learned();
|
|
404
518
|
}
|
|
405
519
|
function learned() {
|
|
406
520
|
if (deps.closed())
|
|
407
521
|
return;
|
|
408
|
-
if (
|
|
522
|
+
if (unlisted.size > 0)
|
|
523
|
+
tellList();
|
|
524
|
+
if (unnamed.size === 0)
|
|
409
525
|
return;
|
|
410
|
-
|
|
411
|
-
const value =
|
|
412
|
-
for (const watcher of [...unlisted]) {
|
|
413
|
-
unlisted.delete(watcher);
|
|
414
|
-
listWatchers.add(watcher);
|
|
415
|
-
watcher(value);
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
if (unnamed.size > 0) {
|
|
419
|
-
const value = agents();
|
|
526
|
+
shownList((list) => {
|
|
527
|
+
const value = agents(list);
|
|
420
528
|
for (const watcher of [...unnamed]) {
|
|
421
529
|
unnamed.delete(watcher);
|
|
422
530
|
agentWatchers.add(watcher);
|
|
423
531
|
watcher(value);
|
|
424
532
|
}
|
|
425
|
-
}
|
|
533
|
+
});
|
|
426
534
|
}
|
|
427
|
-
function agents() {
|
|
535
|
+
function agents(list) {
|
|
428
536
|
const named = new Map();
|
|
429
537
|
for (const agent of listedAgents ?? [])
|
|
430
538
|
named.set(agent.agent, agent);
|
|
431
|
-
for (const entry of
|
|
539
|
+
for (const entry of list) {
|
|
432
540
|
named.set(entry.agent, { name: entry.agentName, retired: entry.agentRetired });
|
|
433
541
|
}
|
|
434
542
|
return [...named]
|
|
@@ -458,31 +566,33 @@ export function createConversationStore(deps) {
|
|
|
458
566
|
function emitAgents() {
|
|
459
567
|
if (deps.closed() || agentWatchers.size === 0)
|
|
460
568
|
return;
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
watcher
|
|
569
|
+
shownList((list) => {
|
|
570
|
+
const value = agents(list);
|
|
571
|
+
const shown = JSON.stringify(value);
|
|
572
|
+
if (shown === agentsShown)
|
|
573
|
+
return;
|
|
574
|
+
agentsShown = shown;
|
|
575
|
+
for (const watcher of agentWatchers)
|
|
576
|
+
watcher(value);
|
|
577
|
+
});
|
|
468
578
|
}
|
|
469
579
|
function setLive(conversation, next) {
|
|
470
|
-
const before =
|
|
471
|
-
const began = lives.get(conversation)?.turn?.started;
|
|
580
|
+
const before = rowOf(conversation);
|
|
472
581
|
if (next === null)
|
|
473
582
|
lives.delete(conversation);
|
|
474
583
|
else
|
|
475
584
|
lives.set(conversation, next);
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
if (stateOf(conversation) !== before || next?.turn?.started !== began)
|
|
585
|
+
const after = rowOf(conversation);
|
|
586
|
+
if (after.state !== before.state || after.started !== before.started)
|
|
479
587
|
emitList();
|
|
588
|
+
else if (after.line !== before.line || after.question !== before.question)
|
|
589
|
+
tellSoon();
|
|
480
590
|
}
|
|
481
|
-
function retiredChat(conversation) {
|
|
591
|
+
function retiredChat(conversation, list) {
|
|
482
592
|
if (!conversation.startsWith(NEW_CHAT))
|
|
483
593
|
return false;
|
|
484
594
|
const agent = conversation.slice(NEW_CHAT.length);
|
|
485
|
-
return
|
|
595
|
+
return list.some((entry) => entry.agent === agent && entry.agentRetired);
|
|
486
596
|
}
|
|
487
597
|
function live(conversation, change) {
|
|
488
598
|
const owed = unsent.get(conversation);
|
|
@@ -540,17 +650,23 @@ export function createConversationStore(deps) {
|
|
|
540
650
|
}
|
|
541
651
|
async function remove(leaving) {
|
|
542
652
|
await deps.durably(async (local) => {
|
|
653
|
+
const kept = new Set([
|
|
654
|
+
...(await local.conversations(leaving)).map(({ conversation }) => conversation),
|
|
655
|
+
...(await local.chatDrafts())
|
|
656
|
+
.filter(({ conversation }) => leaving.includes(conversation))
|
|
657
|
+
.map(({ conversation }) => conversation),
|
|
658
|
+
]);
|
|
543
659
|
await local.dropConversations(leaving);
|
|
544
660
|
const changed = [];
|
|
545
661
|
for (const conversation of leaving) {
|
|
546
|
-
if (
|
|
662
|
+
if (kept.has(conversation) || lives.has(conversation))
|
|
547
663
|
changed.push(conversation);
|
|
664
|
+
for (const view of views.values()) {
|
|
665
|
+
if (view.conversation === conversation)
|
|
666
|
+
view.draft = null;
|
|
548
667
|
}
|
|
549
|
-
stored.delete(conversation);
|
|
550
|
-
reads.delete(conversation);
|
|
551
|
-
partials.delete(conversation);
|
|
552
|
-
drafts.delete(conversation);
|
|
553
668
|
behind.delete(conversation);
|
|
669
|
+
heardNewest.delete(conversation);
|
|
554
670
|
unsent.delete(conversation);
|
|
555
671
|
setLive(conversation, null);
|
|
556
672
|
await slide(local, conversation, null);
|
|
@@ -559,19 +675,68 @@ export function createConversationStore(deps) {
|
|
|
559
675
|
deps.publish(conversationChangeSet(changed));
|
|
560
676
|
});
|
|
561
677
|
}
|
|
562
|
-
function
|
|
563
|
-
|
|
564
|
-
|
|
678
|
+
function newestOfListed(entry) {
|
|
679
|
+
if (entry === undefined)
|
|
680
|
+
return undefined;
|
|
681
|
+
const heard = heardNewest.get(entry.conversation);
|
|
682
|
+
if (heard === undefined)
|
|
683
|
+
return entry.newest;
|
|
684
|
+
return entry.newest === null || heard > entry.newest ? heard : entry.newest;
|
|
685
|
+
}
|
|
686
|
+
function missed(conversation, held, entry) {
|
|
687
|
+
const read = held?.read;
|
|
688
|
+
const newest = newestOfListed(entry);
|
|
565
689
|
return (read === undefined ||
|
|
566
690
|
newest === undefined ||
|
|
567
|
-
|
|
691
|
+
held?.partial === true ||
|
|
568
692
|
(newest !== null && (read === null || newest > read)));
|
|
569
693
|
}
|
|
570
|
-
function
|
|
571
|
-
|
|
572
|
-
|
|
694
|
+
async function readsOf(local, conversations) {
|
|
695
|
+
return new Map((await local.conversations(conversations)).map((held) => [held.conversation, held]));
|
|
696
|
+
}
|
|
697
|
+
function retiredDrafts(list) {
|
|
698
|
+
if (!list.some((entry) => entry.agentRetired))
|
|
573
699
|
return;
|
|
574
|
-
|
|
700
|
+
void deps
|
|
701
|
+
.local()
|
|
702
|
+
.then((local) => local.chatDrafts())
|
|
703
|
+
.then((held) => {
|
|
704
|
+
if (!deps.closed() && held.some(({ conversation }) => retiredChat(conversation, list))) {
|
|
705
|
+
deps.again();
|
|
706
|
+
}
|
|
707
|
+
}, () => { });
|
|
708
|
+
}
|
|
709
|
+
function settleBehind(list) {
|
|
710
|
+
void deps
|
|
711
|
+
.durably(async (local) => {
|
|
712
|
+
const entries = new Map(list.map((entry) => [entry.conversation, entry]));
|
|
713
|
+
const named = [...replicated].filter((conversation) => entries.has(conversation));
|
|
714
|
+
const held = await readsOf(local, named);
|
|
715
|
+
let owed = false;
|
|
716
|
+
for (const conversation of named) {
|
|
717
|
+
if (!missed(conversation, held.get(conversation), entries.get(conversation))) {
|
|
718
|
+
behind.delete(conversation);
|
|
719
|
+
}
|
|
720
|
+
else if (!behind.has(conversation)) {
|
|
721
|
+
gap(conversation);
|
|
722
|
+
owed = true;
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
return owed;
|
|
726
|
+
})
|
|
727
|
+
.then((owed) => {
|
|
728
|
+
if (deps.closed())
|
|
729
|
+
return;
|
|
730
|
+
deps.settled();
|
|
731
|
+
if (owed)
|
|
732
|
+
deps.again();
|
|
733
|
+
}, () => { });
|
|
734
|
+
}
|
|
735
|
+
function noted(conversation, message) {
|
|
736
|
+
const heard = heardNewest.get(conversation);
|
|
737
|
+
if (heard === undefined || message.position > heard) {
|
|
738
|
+
heardNewest.set(conversation, message.position);
|
|
739
|
+
}
|
|
575
740
|
}
|
|
576
741
|
function gap(conversation) {
|
|
577
742
|
gaps.set(conversation, (gaps.get(conversation) ?? 0) + 1);
|
|
@@ -581,7 +746,7 @@ export function createConversationStore(deps) {
|
|
|
581
746
|
return deps.durably(async (local) => {
|
|
582
747
|
if (!replicated.has(conversation))
|
|
583
748
|
return;
|
|
584
|
-
const read =
|
|
749
|
+
const read = (await readsOf(local, [conversation])).get(conversation)?.read;
|
|
585
750
|
const advances = read !== undefined &&
|
|
586
751
|
!behind.has(conversation) &&
|
|
587
752
|
(read === null || message.position > read);
|
|
@@ -591,19 +756,18 @@ export function createConversationStore(deps) {
|
|
|
591
756
|
messages: [message],
|
|
592
757
|
...(advances ? { read: { at: message.position, partial: false } } : {}),
|
|
593
758
|
});
|
|
594
|
-
stored.add(conversation);
|
|
595
|
-
if (advances)
|
|
596
|
-
reads.set(conversation, message.position);
|
|
597
759
|
sealTurns(conversation, new Set([message.turn]));
|
|
598
760
|
await slide(local, conversation, [message]);
|
|
599
761
|
deps.publish(conversationChangeSet([conversation]));
|
|
762
|
+
tellList();
|
|
600
763
|
});
|
|
601
764
|
}
|
|
602
765
|
async function readHistories(conversations, ended) {
|
|
603
766
|
const generations = new Map(conversations.map((one) => [one, gaps.get(one) ?? 0]));
|
|
767
|
+
const held = await readsOf(await deps.local(), conversations);
|
|
604
768
|
let waiting = conversations.map((conversation) => ({
|
|
605
769
|
conversation,
|
|
606
|
-
after:
|
|
770
|
+
after: held.get(conversation)?.read ?? undefined,
|
|
607
771
|
}));
|
|
608
772
|
while (waiting.length > 0) {
|
|
609
773
|
const asking = waiting.slice(0, HISTORY_CONVERSATIONS);
|
|
@@ -659,15 +823,10 @@ export function createConversationStore(deps) {
|
|
|
659
823
|
return;
|
|
660
824
|
const whole = next === undefined;
|
|
661
825
|
const cursor = next ?? newestOf(arriving, from);
|
|
662
|
-
const
|
|
826
|
+
const held = (await readsOf(local, [conversation])).get(conversation);
|
|
827
|
+
const partial = !whole && (held?.partial === true || held?.read === undefined);
|
|
663
828
|
const sealing = await sealed(local, conversation, new Set(arriving.map((message) => message.turn)));
|
|
664
829
|
await local.keepMessages({ conversation, messages: arriving, read: { at: cursor, partial } });
|
|
665
|
-
stored.add(conversation);
|
|
666
|
-
reads.set(conversation, cursor);
|
|
667
|
-
if (partial)
|
|
668
|
-
partials.add(conversation);
|
|
669
|
-
else
|
|
670
|
-
partials.delete(conversation);
|
|
671
830
|
sealTurns(conversation, new Set(arriving.map((message) => message.turn)));
|
|
672
831
|
if (whole && (gaps.get(conversation) ?? 0) === generation)
|
|
673
832
|
behind.delete(conversation);
|
|
@@ -675,6 +834,8 @@ export function createConversationStore(deps) {
|
|
|
675
834
|
if (arriving.length > 0 || sealing || whole) {
|
|
676
835
|
deps.publish(conversationChangeSet([conversation]));
|
|
677
836
|
}
|
|
837
|
+
if (arriving.length > 0)
|
|
838
|
+
tellSoon();
|
|
678
839
|
});
|
|
679
840
|
}
|
|
680
841
|
function heard(event) {
|
|
@@ -741,17 +902,24 @@ export function createConversationStore(deps) {
|
|
|
741
902
|
emit(conversation);
|
|
742
903
|
}
|
|
743
904
|
async function kept(local, conversation, following) {
|
|
744
|
-
|
|
905
|
+
const shown = following === null || empty(following) ? null : following;
|
|
906
|
+
if (shown === null)
|
|
745
907
|
await local.dropChatDraft(conversation);
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
908
|
+
else
|
|
909
|
+
await local.keepChatDrafts([shown]);
|
|
910
|
+
shownDraft(conversation, shown);
|
|
911
|
+
}
|
|
912
|
+
function shownDraft(conversation, draft) {
|
|
913
|
+
for (const view of views.values()) {
|
|
914
|
+
if (view.conversation === conversation)
|
|
915
|
+
view.draft = draft;
|
|
751
916
|
}
|
|
752
917
|
}
|
|
918
|
+
async function draftAt(local, conversation) {
|
|
919
|
+
return (await local.chatDraft(conversation)) ?? undefined;
|
|
920
|
+
}
|
|
753
921
|
async function sealed(local, conversation, turns) {
|
|
754
|
-
const held =
|
|
922
|
+
const held = await draftAt(local, conversation);
|
|
755
923
|
const submitted = held?.submitted ?? [];
|
|
756
924
|
const left = submitted.filter((one) => one.turn === null || !turns.has(one.turn));
|
|
757
925
|
if (held === undefined || left.length === submitted.length)
|
|
@@ -762,8 +930,8 @@ export function createConversationStore(deps) {
|
|
|
762
930
|
function moved(agent, operation, to) {
|
|
763
931
|
const from = newChat(agent);
|
|
764
932
|
return deps.durably(async (local) => {
|
|
765
|
-
const held =
|
|
766
|
-
const earlier =
|
|
933
|
+
const held = await draftAt(local, from);
|
|
934
|
+
const earlier = held?.moves ?? [];
|
|
767
935
|
if (earlier.includes(operation))
|
|
768
936
|
return;
|
|
769
937
|
const blank = {
|
|
@@ -772,18 +940,20 @@ export function createConversationStore(deps) {
|
|
|
772
940
|
revision: chatDraftRevision(),
|
|
773
941
|
};
|
|
774
942
|
const { moves: _answered, ...unsent } = held ?? blank;
|
|
775
|
-
const taken =
|
|
943
|
+
const taken = (await draftAt(local, to)) !== undefined || empty(unsent)
|
|
944
|
+
? []
|
|
945
|
+
: [{ ...unsent, conversation: to }];
|
|
776
946
|
const left = { ...(taken.length === 0 ? unsent : blank), moves: [...earlier, operation] };
|
|
777
947
|
await local.keepChatDrafts([left, ...taken]);
|
|
778
|
-
|
|
948
|
+
shownDraft(from, left);
|
|
779
949
|
for (const one of taken)
|
|
780
|
-
|
|
950
|
+
shownDraft(to, one);
|
|
781
951
|
deps.publish(conversationChangeSet([from, to]));
|
|
782
952
|
});
|
|
783
953
|
}
|
|
784
954
|
function withDraft(conversation, next) {
|
|
785
955
|
return deps.durably(async (local) => {
|
|
786
|
-
const following = await next(
|
|
956
|
+
const following = await next(await draftAt(local, conversation), local);
|
|
787
957
|
if (following === undefined)
|
|
788
958
|
return;
|
|
789
959
|
await kept(local, conversation, following);
|
|
@@ -792,10 +962,8 @@ export function createConversationStore(deps) {
|
|
|
792
962
|
}
|
|
793
963
|
async function submitted(conversation, operation, draft) {
|
|
794
964
|
let submission;
|
|
795
|
-
let earlier = false;
|
|
796
965
|
await withDraft(conversation, (current) => {
|
|
797
966
|
submission = current?.submitted?.find((one) => one.operation === operation);
|
|
798
|
-
earlier = submission !== undefined;
|
|
799
967
|
if (submission !== undefined)
|
|
800
968
|
return undefined;
|
|
801
969
|
const said = draft ?? current;
|
|
@@ -808,15 +976,7 @@ export function createConversationStore(deps) {
|
|
|
808
976
|
submitted: [...(current?.submitted ?? []), submission],
|
|
809
977
|
};
|
|
810
978
|
});
|
|
811
|
-
return
|
|
812
|
-
}
|
|
813
|
-
function forgotten(conversation, submission) {
|
|
814
|
-
return withDraft(conversation, (current) => {
|
|
815
|
-
const held = current?.submitted ?? [];
|
|
816
|
-
if (current === undefined || !held.includes(submission))
|
|
817
|
-
return undefined;
|
|
818
|
-
return withSubmitted(current, held.filter((one) => one !== submission));
|
|
819
|
-
});
|
|
979
|
+
return submission;
|
|
820
980
|
}
|
|
821
981
|
async function dispatched(conversation, submission) {
|
|
822
982
|
const dispatch = { answered: false };
|
|
@@ -875,7 +1035,8 @@ export function createConversationStore(deps) {
|
|
|
875
1035
|
return deps.durably(async (local) => {
|
|
876
1036
|
const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before, phrase);
|
|
877
1037
|
const total = asked.total ? await local.messageCount(conversation, phrase) : null;
|
|
878
|
-
|
|
1038
|
+
const marked = await local.jobMarkers(conversation);
|
|
1039
|
+
return structuredClone(viewOf(conversation, messages, frameOf(asked, messages, more, total), (await local.chatDraft(conversation)) ?? null, await endedIn(conversation), marked));
|
|
879
1040
|
});
|
|
880
1041
|
},
|
|
881
1042
|
async create(input) {
|
|
@@ -883,8 +1044,10 @@ export function createConversationStore(deps) {
|
|
|
883
1044
|
const conversation = answered(result, 'conversation_id');
|
|
884
1045
|
if (conversation === null)
|
|
885
1046
|
return result;
|
|
886
|
-
|
|
1047
|
+
const listed = (await heldList()) ?? [];
|
|
1048
|
+
if (!listed.some((entry) => entry.conversation === conversation)) {
|
|
887
1049
|
creations.add(conversation);
|
|
1050
|
+
}
|
|
888
1051
|
const creation = creationOf(input);
|
|
889
1052
|
if (creation !== null)
|
|
890
1053
|
await moved(creation.agent, creation.operation, conversation);
|
|
@@ -901,11 +1064,7 @@ export function createConversationStore(deps) {
|
|
|
901
1064
|
});
|
|
902
1065
|
if (held === null)
|
|
903
1066
|
return DURABLE_WRITE;
|
|
904
|
-
|
|
905
|
-
if (!earlier || !deps.expired(operation))
|
|
906
|
-
return dispatched(conversation, submission);
|
|
907
|
-
await forgotten(conversation, submission);
|
|
908
|
-
return EXPIRED;
|
|
1067
|
+
return dispatched(conversation, held);
|
|
909
1068
|
}
|
|
910
1069
|
if (draft !== undefined) {
|
|
911
1070
|
await withDraft(conversation, (current) => current?.submitted?.some((one) => one.operation === operation) === true
|
|
@@ -956,43 +1115,40 @@ export function createConversationStore(deps) {
|
|
|
956
1115
|
return {
|
|
957
1116
|
surface,
|
|
958
1117
|
chatDraft,
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
listCursor = cursor;
|
|
966
|
-
deps.stream()?.hold(cursor);
|
|
967
|
-
}
|
|
968
|
-
stored.clear();
|
|
969
|
-
reads.clear();
|
|
970
|
-
partials.clear();
|
|
971
|
-
for (const entry of held) {
|
|
972
|
-
stored.add(entry.conversation);
|
|
973
|
-
if (entry.read !== undefined)
|
|
974
|
-
reads.set(entry.conversation, entry.read);
|
|
975
|
-
if (entry.partial)
|
|
976
|
-
partials.add(entry.conversation);
|
|
977
|
-
}
|
|
978
|
-
drafts.clear();
|
|
979
|
-
for (const record of kept)
|
|
980
|
-
drafts.set(record.conversation, record);
|
|
981
|
-
if (listHeld)
|
|
982
|
-
learned();
|
|
1118
|
+
async opened(local) {
|
|
1119
|
+
const { conversationList, conversationCursor } = await local.facts();
|
|
1120
|
+
if (conversationList === null || listKnown)
|
|
1121
|
+
return;
|
|
1122
|
+
deps.stream()?.hold(conversationCursor);
|
|
1123
|
+
learned();
|
|
983
1124
|
},
|
|
984
1125
|
async reconcile() {
|
|
985
1126
|
if (!listKnown)
|
|
986
1127
|
return;
|
|
987
|
-
const
|
|
1128
|
+
const local = await deps.local();
|
|
1129
|
+
const [held, kept, { conversationList }] = await Promise.all([
|
|
1130
|
+
local.conversations(),
|
|
1131
|
+
local.chatDrafts(),
|
|
1132
|
+
local.facts(),
|
|
1133
|
+
]);
|
|
1134
|
+
const list = conversationList ?? [];
|
|
1135
|
+
const entries = new Map(list.map((entry) => [entry.conversation, entry]));
|
|
1136
|
+
const named = new Set(entries.keys());
|
|
1137
|
+
const reads = new Map(held.map((one) => [one.conversation, one]));
|
|
988
1138
|
const leaving = [
|
|
989
|
-
...new Set([
|
|
1139
|
+
...new Set([
|
|
1140
|
+
...replicated,
|
|
1141
|
+
...reads.keys(),
|
|
1142
|
+
...kept.map(({ conversation }) => conversation),
|
|
1143
|
+
]),
|
|
990
1144
|
].filter((conversation) => !named.has(conversation) &&
|
|
991
1145
|
!creations.has(conversation) &&
|
|
992
|
-
(!conversation.startsWith(NEW_CHAT) || retiredChat(conversation)));
|
|
1146
|
+
(!conversation.startsWith(NEW_CHAT) || retiredChat(conversation, list)));
|
|
993
1147
|
for (const conversation of named) {
|
|
994
|
-
if (!replicated.has(conversation) &&
|
|
1148
|
+
if (!replicated.has(conversation) &&
|
|
1149
|
+
missed(conversation, reads.get(conversation), entries.get(conversation))) {
|
|
995
1150
|
behind.add(conversation);
|
|
1151
|
+
}
|
|
996
1152
|
}
|
|
997
1153
|
replicated = named;
|
|
998
1154
|
handReplicated();
|
|
@@ -1023,16 +1179,16 @@ export function createConversationStore(deps) {
|
|
|
1023
1179
|
progress.complete = false;
|
|
1024
1180
|
},
|
|
1025
1181
|
async resend(progress) {
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1182
|
+
const local = await deps.local();
|
|
1183
|
+
const tried = new Set();
|
|
1184
|
+
for (const { conversation } of await local.chatDrafts()) {
|
|
1185
|
+
for (;;) {
|
|
1186
|
+
const submission = (await local.chatDraft(conversation))?.submitted?.find((one) => one.turn === null && !sending.has(one.operation) && !tried.has(one.operation));
|
|
1187
|
+
if (submission === undefined)
|
|
1188
|
+
break;
|
|
1030
1189
|
if (deps.closed())
|
|
1031
1190
|
return;
|
|
1032
|
-
|
|
1033
|
-
await forgotten(conversation, submission);
|
|
1034
|
-
continue;
|
|
1035
|
-
}
|
|
1191
|
+
tried.add(submission.operation);
|
|
1036
1192
|
const result = await dispatched(conversation, submission);
|
|
1037
1193
|
if (result.kind === 'revoked-authorization')
|
|
1038
1194
|
return;
|
|
@@ -1070,6 +1226,9 @@ export function createConversationStore(deps) {
|
|
|
1070
1226
|
asked,
|
|
1071
1227
|
watchers: new Set(),
|
|
1072
1228
|
window: null,
|
|
1229
|
+
draft: null,
|
|
1230
|
+
ended: null,
|
|
1231
|
+
marked: [],
|
|
1073
1232
|
more: false,
|
|
1074
1233
|
total: null,
|
|
1075
1234
|
};
|
|
@@ -1081,7 +1240,10 @@ export function createConversationStore(deps) {
|
|
|
1081
1240
|
if (view.window !== null)
|
|
1082
1241
|
return;
|
|
1083
1242
|
const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before);
|
|
1243
|
+
view.draft = (await local.chatDraft(conversation)) ?? null;
|
|
1244
|
+
view.ended = await endedIn(conversation);
|
|
1084
1245
|
view.window = messages;
|
|
1246
|
+
view.marked = await local.jobMarkers(conversation);
|
|
1085
1247
|
view.more = more;
|
|
1086
1248
|
view.total = asked.total ? await local.messageCount(conversation) : null;
|
|
1087
1249
|
}), reading.signal)
|
|
@@ -1090,7 +1252,7 @@ export function createConversationStore(deps) {
|
|
|
1090
1252
|
return;
|
|
1091
1253
|
watcher.ready = true;
|
|
1092
1254
|
const window = view.window ?? [];
|
|
1093
|
-
watcher.notify(viewOf(conversation, window, sharedFrame(view, window)));
|
|
1255
|
+
watcher.notify(viewOf(conversation, window, sharedFrame(view, window), view.draft, view.ended, view.marked));
|
|
1094
1256
|
})
|
|
1095
1257
|
.catch(() => { });
|
|
1096
1258
|
return () => {
|
|
@@ -1125,58 +1287,43 @@ export function createConversationStore(deps) {
|
|
|
1125
1287
|
},
|
|
1126
1288
|
receive(event) {
|
|
1127
1289
|
if (deps.closed())
|
|
1128
|
-
return
|
|
1129
|
-
if (event.kind === 'list'
|
|
1130
|
-
if (event.
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
for (const entry of event.entries)
|
|
1134
|
-
|
|
1135
|
-
listed(entry);
|
|
1136
|
-
else
|
|
1137
|
-
relisting.set(entry.conversation, { ...entry });
|
|
1138
|
-
}
|
|
1139
|
-
if (event.more)
|
|
1140
|
-
return false;
|
|
1141
|
-
if (relisting !== null) {
|
|
1142
|
-
listing.clear();
|
|
1143
|
-
for (const entry of relisting.values())
|
|
1144
|
-
listed(entry);
|
|
1145
|
-
relisting = null;
|
|
1146
|
-
}
|
|
1147
|
-
listCursor = event.cursor;
|
|
1148
|
-
deps.stream()?.hold(event.cursor);
|
|
1149
|
-
listKnown = true;
|
|
1150
|
-
for (const conversation of replicated) {
|
|
1151
|
-
if (!listing.has(conversation))
|
|
1152
|
-
continue;
|
|
1153
|
-
if (!missed(conversation))
|
|
1154
|
-
behind.delete(conversation);
|
|
1155
|
-
else if (!behind.has(conversation))
|
|
1156
|
-
gap(conversation);
|
|
1157
|
-
}
|
|
1158
|
-
openingEnded();
|
|
1159
|
-
}
|
|
1160
|
-
else if (event.kind === 'listed') {
|
|
1161
|
-
listed(event.entry);
|
|
1162
|
-
relisting?.set(event.entry.conversation, { ...event.entry });
|
|
1163
|
-
}
|
|
1164
|
-
else {
|
|
1165
|
-
listing.delete(event.conversation);
|
|
1166
|
-
relisting?.delete(event.conversation);
|
|
1290
|
+
return;
|
|
1291
|
+
if (event.kind === 'list') {
|
|
1292
|
+
if (event.whole)
|
|
1293
|
+
relisting ??= new Map();
|
|
1294
|
+
if (relisting !== null) {
|
|
1295
|
+
for (const entry of event.entries)
|
|
1296
|
+
relisting.set(entry.conversation, { ...entry });
|
|
1167
1297
|
}
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1298
|
+
if (event.more && relisting !== null)
|
|
1299
|
+
return;
|
|
1300
|
+
const whole = relisting;
|
|
1301
|
+
relisting = null;
|
|
1302
|
+
listChanged({
|
|
1303
|
+
replacing: whole !== null,
|
|
1304
|
+
set: whole === null ? event.entries : [...whole.values()],
|
|
1305
|
+
gone: null,
|
|
1306
|
+
...(event.more ? { paged: true } : { cursor: event.cursor }),
|
|
1307
|
+
});
|
|
1308
|
+
return;
|
|
1309
|
+
}
|
|
1310
|
+
if (event.kind === 'listed') {
|
|
1311
|
+
relisting?.set(event.entry.conversation, { ...event.entry });
|
|
1312
|
+
listChanged({ replacing: false, set: [event.entry], gone: null });
|
|
1313
|
+
return;
|
|
1314
|
+
}
|
|
1315
|
+
if (event.kind === 'unlisted') {
|
|
1316
|
+
relisting?.delete(event.conversation);
|
|
1317
|
+
listChanged({ replacing: false, set: [], gone: event.conversation });
|
|
1318
|
+
return;
|
|
1175
1319
|
}
|
|
1176
1320
|
heard(event);
|
|
1177
|
-
return false;
|
|
1178
1321
|
},
|
|
1179
1322
|
close() {
|
|
1323
|
+
closing.abort();
|
|
1324
|
+
if (rowTimer !== null)
|
|
1325
|
+
clearTimeout(rowTimer);
|
|
1326
|
+
rowTimer = null;
|
|
1180
1327
|
views.clear();
|
|
1181
1328
|
lives.clear();
|
|
1182
1329
|
unsent.clear();
|