@agentshouse/mdruntime 0.1.0-c7f53cda5b000 → 0.1.0-c835af48a82d3
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/adapter.d.ts +85 -70
- 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 +29 -47
- package/dist/bootstrap.d.ts +14 -17
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +258 -152
- 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 +14 -5
- 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 +263 -311
- package/dist/connection.d.ts +16 -10
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +2110 -1920
- 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 +27 -16
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +484 -248
- package/dist/durability.d.ts +6 -1
- package/dist/durability.d.ts.map +1 -1
- package/dist/entry.d.ts +3 -2
- package/dist/entry.d.ts.map +1 -1
- package/dist/entry.js +18 -8
- package/dist/figures.d.ts +48 -0
- package/dist/figures.d.ts.map +1 -0
- package/dist/figures.js +197 -0
- package/dist/host.d.ts +3 -7
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +25 -30
- 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 +16 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -1
- package/dist/job.d.ts +33 -0
- package/dist/job.d.ts.map +1 -0
- package/dist/job.js +54 -0
- package/dist/live.d.ts +5 -4
- package/dist/live.d.ts.map +1 -1
- package/dist/live.js +63 -22
- 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 +12 -5
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +174 -83
- 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/observation.d.ts +2 -2
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +7 -2
- package/dist/outcome.d.ts +1 -3
- 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/query.d.ts +14 -209
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +3 -505
- 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 +13 -12
- package/dist/reach.d.ts.map +1 -1
- package/dist/reach.js +184 -139
- package/dist/react.d.ts +3 -1
- 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 +3 -17
- package/dist/readiness.d.ts.map +1 -1
- package/dist/readiness.js +4 -10
- package/dist/reads.d.ts +34 -10
- package/dist/reads.d.ts.map +1 -1
- package/dist/reads.js +114 -86
- 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/replica.d.ts +9 -7
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +50 -19
- package/dist/room-draft.d.ts +21 -5
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +98 -51
- package/dist/rows.d.ts +14 -21
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +48 -184
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +11 -8
- package/dist/search.d.ts +12 -63
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +9 -52
- package/dist/shared.d.ts +0 -1
- package/dist/shared.d.ts.map +1 -1
- package/dist/shared.js +0 -10
- 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 -6
- package/dist/status.d.ts.map +1 -1
- package/dist/status.js +0 -6
- package/dist/store.d.ts +94 -59
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +144 -166
- package/dist/stored.d.ts +58 -56
- package/dist/stored.d.ts.map +1 -1
- package/dist/stored.js +295 -236
- package/dist/tables.d.ts +107 -62
- package/dist/tables.d.ts.map +1 -1
- package/dist/tables.js +195 -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 +15 -4
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +6 -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 -368
- package/dist/words.d.ts +0 -19
- package/dist/words.d.ts.map +0 -1
- package/dist/words.js +0 -150
package/dist/conversation.js
CHANGED
|
@@ -1,9 +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';
|
|
6
|
-
import {
|
|
8
|
+
import { jobsOf, runningLine } from './parts.js';
|
|
9
|
+
import { openQuestion } from './questions.js';
|
|
10
|
+
import { untilAnswered } from './recovery.js';
|
|
11
|
+
import { capped, FROM_START, messageOrder, precedes } from './windows.js';
|
|
7
12
|
const QUIET = {
|
|
8
13
|
turn: null,
|
|
9
14
|
questions: [],
|
|
@@ -11,6 +16,7 @@ const QUIET = {
|
|
|
11
16
|
commands: [],
|
|
12
17
|
options: [],
|
|
13
18
|
starting: null,
|
|
19
|
+
subagents: [],
|
|
14
20
|
link: null,
|
|
15
21
|
deliveryUnknown: [],
|
|
16
22
|
};
|
|
@@ -20,6 +26,7 @@ const FACTS = [
|
|
|
20
26
|
'commands',
|
|
21
27
|
'options',
|
|
22
28
|
'starting',
|
|
29
|
+
'subagents',
|
|
23
30
|
'link',
|
|
24
31
|
'deliveryUnknown',
|
|
25
32
|
];
|
|
@@ -30,27 +37,30 @@ export function unheldView(conversation) {
|
|
|
30
37
|
turn: null,
|
|
31
38
|
questions: [],
|
|
32
39
|
process: null,
|
|
40
|
+
processEnded: null,
|
|
33
41
|
commands: [],
|
|
34
42
|
options: [],
|
|
35
43
|
starting: null,
|
|
44
|
+
subagents: [],
|
|
36
45
|
link: null,
|
|
37
46
|
deliveryUnknown: [],
|
|
38
47
|
chatDraft: null,
|
|
48
|
+
jobs: [],
|
|
39
49
|
};
|
|
40
50
|
}
|
|
41
51
|
export const SESSION_STARTED = 'Session started';
|
|
42
52
|
const NEW_CHAT = 'new:';
|
|
43
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;
|
|
44
56
|
export function newChat(agent) {
|
|
45
57
|
return `${NEW_CHAT}${agent}`;
|
|
46
58
|
}
|
|
47
59
|
function creationOf(input) {
|
|
48
60
|
if (typeof input !== 'object' || input === null)
|
|
49
61
|
return null;
|
|
50
|
-
const { agent_id: agent, operation_id: operation
|
|
51
|
-
return typeof agent === 'string' && typeof operation === 'string'
|
|
52
|
-
? { agent, operation }
|
|
53
|
-
: null;
|
|
62
|
+
const { agent_id: agent, operation_id: operation } = input;
|
|
63
|
+
return typeof agent === 'string' && typeof operation === 'string' ? { agent, operation } : null;
|
|
54
64
|
}
|
|
55
65
|
function startedTurn(turn, operation) {
|
|
56
66
|
if (operation.op === 'begin')
|
|
@@ -209,7 +219,14 @@ function askedOf(options) {
|
|
|
209
219
|
}
|
|
210
220
|
return { limit: capped(limit), after, before: beforeOf(after), total: total === true };
|
|
211
221
|
}
|
|
212
|
-
|
|
222
|
+
function phraseOf(text) {
|
|
223
|
+
if (text === undefined)
|
|
224
|
+
return null;
|
|
225
|
+
if (typeof text !== 'string' || text === '') {
|
|
226
|
+
throw runtimeError('invalid-text', 'a conversation is found by a phrase');
|
|
227
|
+
}
|
|
228
|
+
return text;
|
|
229
|
+
}
|
|
213
230
|
const DURABLE_WRITE = {
|
|
214
231
|
kind: 'rejected',
|
|
215
232
|
code: 'durable-write',
|
|
@@ -243,10 +260,6 @@ function returned(draft, submission, others) {
|
|
|
243
260
|
}, others);
|
|
244
261
|
}
|
|
245
262
|
export function createConversationStore(deps) {
|
|
246
|
-
const stored = new Set();
|
|
247
|
-
const reads = new Map();
|
|
248
|
-
const partials = new Set();
|
|
249
|
-
const drafts = new Map();
|
|
250
263
|
const sending = new Map();
|
|
251
264
|
const behind = new Set();
|
|
252
265
|
const catching = new Map();
|
|
@@ -254,21 +267,27 @@ export function createConversationStore(deps) {
|
|
|
254
267
|
const lives = new Map();
|
|
255
268
|
const unsent = new Map();
|
|
256
269
|
const views = new Map();
|
|
257
|
-
const
|
|
270
|
+
const heardNewest = new Map();
|
|
271
|
+
let relisting = null;
|
|
258
272
|
const creations = new Set();
|
|
259
273
|
const listWatchers = new Set();
|
|
274
|
+
const unlisted = new Set();
|
|
260
275
|
let listKnown = false;
|
|
276
|
+
let listOwed = false;
|
|
277
|
+
let listTelling = false;
|
|
278
|
+
let rowTimer = null;
|
|
279
|
+
const closing = new AbortController();
|
|
261
280
|
const agentWatchers = new Set();
|
|
262
|
-
|
|
263
|
-
let
|
|
264
|
-
let
|
|
281
|
+
const unnamed = new Set();
|
|
282
|
+
let agentsShown = null;
|
|
283
|
+
let listedAgents = null;
|
|
284
|
+
let agentsReading = false;
|
|
265
285
|
let replicated = new Set();
|
|
266
286
|
let handedReplicated;
|
|
267
287
|
function liveOf(conversation) {
|
|
268
288
|
return lives.get(conversation) ?? QUIET;
|
|
269
289
|
}
|
|
270
|
-
function viewOf(conversation, held, frame) {
|
|
271
|
-
const draft = drafts.get(conversation);
|
|
290
|
+
function viewOf(conversation, held, frame, draft, ended, marked) {
|
|
272
291
|
const facts = liveOf(conversation);
|
|
273
292
|
return {
|
|
274
293
|
conversation,
|
|
@@ -282,14 +301,17 @@ export function createConversationStore(deps) {
|
|
|
282
301
|
turn: structuredClone(facts.turn),
|
|
283
302
|
questions: structuredClone(facts.questions),
|
|
284
303
|
process: facts.process === null ? null : { ...facts.process },
|
|
304
|
+
processEnded: ended,
|
|
285
305
|
commands: structuredClone(facts.commands),
|
|
286
306
|
options: structuredClone(facts.options),
|
|
287
307
|
starting: facts.starting,
|
|
308
|
+
subagents: structuredClone(facts.subagents),
|
|
288
309
|
link: facts.link === null ? null : { ...facts.link },
|
|
289
310
|
deliveryUnknown: [...facts.deliveryUnknown],
|
|
290
|
-
chatDraft: draft ===
|
|
291
|
-
|
|
292
|
-
|
|
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),
|
|
293
315
|
};
|
|
294
316
|
}
|
|
295
317
|
function emit(conversation) {
|
|
@@ -301,7 +323,7 @@ export function createConversationStore(deps) {
|
|
|
301
323
|
const ready = [...view.watchers].filter((watcher) => watcher.ready);
|
|
302
324
|
if (ready.length === 0)
|
|
303
325
|
continue;
|
|
304
|
-
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);
|
|
305
327
|
for (const watcher of ready)
|
|
306
328
|
watcher.notify(value);
|
|
307
329
|
}
|
|
@@ -311,11 +333,13 @@ export function createConversationStore(deps) {
|
|
|
311
333
|
}
|
|
312
334
|
async function slide(local, conversation, arriving) {
|
|
313
335
|
let count;
|
|
336
|
+
let marked;
|
|
314
337
|
for (const view of views.values()) {
|
|
315
338
|
if (view.conversation !== conversation || view.window === null)
|
|
316
339
|
continue;
|
|
317
340
|
if (arriving === null) {
|
|
318
341
|
view.window = [];
|
|
342
|
+
view.marked = [];
|
|
319
343
|
view.more = false;
|
|
320
344
|
if (view.total !== null)
|
|
321
345
|
view.total = 0;
|
|
@@ -325,6 +349,10 @@ export function createConversationStore(deps) {
|
|
|
325
349
|
const next = windowOf(view.window, joining, view.asked.limit);
|
|
326
350
|
view.window = next.messages;
|
|
327
351
|
view.more ||= next.dropped;
|
|
352
|
+
if (arriving.length > 0) {
|
|
353
|
+
marked ??= await local.jobMarkers(conversation);
|
|
354
|
+
view.marked = marked;
|
|
355
|
+
}
|
|
328
356
|
if (view.total !== null && arriving.length > 0) {
|
|
329
357
|
count ??= await local.messageCount(conversation);
|
|
330
358
|
view.total = count;
|
|
@@ -337,102 +365,234 @@ export function createConversationStore(deps) {
|
|
|
337
365
|
return 'asking';
|
|
338
366
|
return facts.turn === null ? 'idle' : 'working';
|
|
339
367
|
}
|
|
340
|
-
function
|
|
341
|
-
|
|
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) => ({
|
|
342
386
|
conversation: entry.conversation,
|
|
343
|
-
agent: { identity: entry.agent, name:
|
|
387
|
+
agent: { identity: entry.agent, name: entry.agentName },
|
|
344
388
|
name: entry.name,
|
|
345
389
|
created: entry.created,
|
|
346
390
|
finished: entry.finished,
|
|
347
391
|
outcome: entry.outcome,
|
|
348
392
|
waiting: entry.waiting,
|
|
349
|
-
|
|
350
|
-
|
|
393
|
+
...rowOf(entry.conversation),
|
|
394
|
+
...heldFacts(rows.get(entry.conversation), entry),
|
|
351
395
|
}));
|
|
352
396
|
}
|
|
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)
|
|
434
|
+
return;
|
|
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);
|
|
471
|
+
});
|
|
472
|
+
}
|
|
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);
|
|
511
|
+
}
|
|
353
512
|
function emitList() {
|
|
354
|
-
if (deps.closed()
|
|
513
|
+
if (deps.closed())
|
|
355
514
|
return;
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
515
|
+
tellList();
|
|
516
|
+
emitAgents();
|
|
517
|
+
learned();
|
|
518
|
+
}
|
|
519
|
+
function learned() {
|
|
520
|
+
if (deps.closed())
|
|
521
|
+
return;
|
|
522
|
+
if (unlisted.size > 0)
|
|
523
|
+
tellList();
|
|
524
|
+
if (unnamed.size === 0)
|
|
525
|
+
return;
|
|
526
|
+
shownList((list) => {
|
|
527
|
+
const value = agents(list);
|
|
528
|
+
for (const watcher of [...unnamed]) {
|
|
529
|
+
unnamed.delete(watcher);
|
|
530
|
+
agentWatchers.add(watcher);
|
|
531
|
+
watcher(value);
|
|
532
|
+
}
|
|
533
|
+
});
|
|
359
534
|
}
|
|
360
|
-
function agents() {
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
.
|
|
535
|
+
function agents(list) {
|
|
536
|
+
const named = new Map();
|
|
537
|
+
for (const agent of listedAgents ?? [])
|
|
538
|
+
named.set(agent.agent, agent);
|
|
539
|
+
for (const entry of list) {
|
|
540
|
+
named.set(entry.agent, { name: entry.agentName, retired: entry.agentRetired });
|
|
541
|
+
}
|
|
542
|
+
return [...named]
|
|
543
|
+
.filter(([, agent]) => !agent.retired)
|
|
544
|
+
.map(([identity, agent]) => ({ identity, name: agent.name }))
|
|
364
545
|
.sort((left, right) => left.name === right.name
|
|
365
546
|
? left.identity.localeCompare(right.identity)
|
|
366
547
|
: left.name.localeCompare(right.name));
|
|
367
548
|
}
|
|
549
|
+
function readAgents() {
|
|
550
|
+
const stream = deps.stream();
|
|
551
|
+
if (stream?.agents === undefined || !deps.online() || agentsReading)
|
|
552
|
+
return;
|
|
553
|
+
agentsReading = true;
|
|
554
|
+
void stream
|
|
555
|
+
.agents()
|
|
556
|
+
.then((read) => {
|
|
557
|
+
if (read === null)
|
|
558
|
+
return;
|
|
559
|
+
listedAgents = read;
|
|
560
|
+
emitAgents();
|
|
561
|
+
}, () => { })
|
|
562
|
+
.finally(() => {
|
|
563
|
+
agentsReading = false;
|
|
564
|
+
});
|
|
565
|
+
}
|
|
368
566
|
function emitAgents() {
|
|
369
567
|
if (deps.closed() || agentWatchers.size === 0)
|
|
370
568
|
return;
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
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
|
+
});
|
|
374
578
|
}
|
|
375
579
|
function setLive(conversation, next) {
|
|
376
|
-
const before =
|
|
377
|
-
const began = lives.get(conversation)?.turn?.started;
|
|
580
|
+
const before = rowOf(conversation);
|
|
378
581
|
if (next === null)
|
|
379
582
|
lives.delete(conversation);
|
|
380
583
|
else
|
|
381
584
|
lives.set(conversation, next);
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
if (stateOf(conversation) !== before || next?.turn?.started !== began)
|
|
585
|
+
const after = rowOf(conversation);
|
|
586
|
+
if (after.state !== before.state || after.started !== before.started)
|
|
385
587
|
emitList();
|
|
588
|
+
else if (after.line !== before.line || after.question !== before.question)
|
|
589
|
+
tellSoon();
|
|
386
590
|
}
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
const retired = new Set();
|
|
393
|
-
let covered = null;
|
|
394
|
-
for (let after;;) {
|
|
395
|
-
const read = await deps.documents(declaration, after);
|
|
396
|
-
if ('pending' in read)
|
|
397
|
-
return;
|
|
398
|
-
const coverage = JSON.stringify(read.coverage);
|
|
399
|
-
if (covered !== null && coverage !== covered)
|
|
400
|
-
return readAgents();
|
|
401
|
-
covered = coverage;
|
|
402
|
-
for (const document of read.documents) {
|
|
403
|
-
const identity = document.fields?.[declaration.identity];
|
|
404
|
-
const name = document.fields?.[declaration.name];
|
|
405
|
-
if (typeof identity !== 'string' || typeof name !== 'string')
|
|
406
|
-
continue;
|
|
407
|
-
names.set(identity, name);
|
|
408
|
-
const ended = declaration.retired === undefined ? null : document.fields?.[declaration.retired];
|
|
409
|
-
if (ended !== undefined && ended !== null)
|
|
410
|
-
retired.add(identity);
|
|
411
|
-
}
|
|
412
|
-
if (read.next === undefined)
|
|
413
|
-
break;
|
|
414
|
-
after = read.next;
|
|
415
|
-
}
|
|
416
|
-
const named = JSON.stringify([...names]) !== JSON.stringify([...agentNames]);
|
|
417
|
-
if (!named && JSON.stringify([...retired]) === JSON.stringify([...retiredAgents]))
|
|
418
|
-
return;
|
|
419
|
-
agentNames = names;
|
|
420
|
-
retiredAgents = retired;
|
|
421
|
-
if (named)
|
|
422
|
-
emitList();
|
|
423
|
-
emitAgents();
|
|
424
|
-
const ended = [...drafts.keys()].filter(retiredChat);
|
|
425
|
-
if (ended.length > 0)
|
|
426
|
-
await remove(ended);
|
|
427
|
-
}
|
|
428
|
-
function retiredChat(conversation) {
|
|
429
|
-
return (conversation.startsWith(NEW_CHAT) && retiredAgents.has(conversation.slice(NEW_CHAT.length)));
|
|
430
|
-
}
|
|
431
|
-
function agentsReady() {
|
|
432
|
-
agentsRead ??= readAgents().catch(() => {
|
|
433
|
-
agentsRead = null;
|
|
434
|
-
});
|
|
435
|
-
return agentsRead;
|
|
591
|
+
function retiredChat(conversation, list) {
|
|
592
|
+
if (!conversation.startsWith(NEW_CHAT))
|
|
593
|
+
return false;
|
|
594
|
+
const agent = conversation.slice(NEW_CHAT.length);
|
|
595
|
+
return list.some((entry) => entry.agent === agent && entry.agentRetired);
|
|
436
596
|
}
|
|
437
597
|
function live(conversation, change) {
|
|
438
598
|
const owed = unsent.get(conversation);
|
|
@@ -455,6 +615,7 @@ export function createConversationStore(deps) {
|
|
|
455
615
|
commands: facts.has('commands') ? QUIET.commands : held.commands,
|
|
456
616
|
options: facts.has('options') ? QUIET.options : held.options,
|
|
457
617
|
starting: facts.has('starting') ? QUIET.starting : held.starting,
|
|
618
|
+
subagents: facts.has('subagents') ? QUIET.subagents : held.subagents,
|
|
458
619
|
link: facts.has('link') ? QUIET.link : held.link,
|
|
459
620
|
deliveryUnknown: facts.has('deliveryUnknown')
|
|
460
621
|
? QUIET.deliveryUnknown
|
|
@@ -489,17 +650,23 @@ export function createConversationStore(deps) {
|
|
|
489
650
|
}
|
|
490
651
|
async function remove(leaving) {
|
|
491
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
|
+
]);
|
|
492
659
|
await local.dropConversations(leaving);
|
|
493
660
|
const changed = [];
|
|
494
661
|
for (const conversation of leaving) {
|
|
495
|
-
if (
|
|
662
|
+
if (kept.has(conversation) || lives.has(conversation))
|
|
496
663
|
changed.push(conversation);
|
|
664
|
+
for (const view of views.values()) {
|
|
665
|
+
if (view.conversation === conversation)
|
|
666
|
+
view.draft = null;
|
|
497
667
|
}
|
|
498
|
-
stored.delete(conversation);
|
|
499
|
-
reads.delete(conversation);
|
|
500
|
-
partials.delete(conversation);
|
|
501
|
-
drafts.delete(conversation);
|
|
502
668
|
behind.delete(conversation);
|
|
669
|
+
heardNewest.delete(conversation);
|
|
503
670
|
unsent.delete(conversation);
|
|
504
671
|
setLive(conversation, null);
|
|
505
672
|
await slide(local, conversation, null);
|
|
@@ -508,6 +675,69 @@ export function createConversationStore(deps) {
|
|
|
508
675
|
deps.publish(conversationChangeSet(changed));
|
|
509
676
|
});
|
|
510
677
|
}
|
|
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);
|
|
689
|
+
return (read === undefined ||
|
|
690
|
+
newest === undefined ||
|
|
691
|
+
held?.partial === true ||
|
|
692
|
+
(newest !== null && (read === null || newest > read)));
|
|
693
|
+
}
|
|
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))
|
|
699
|
+
return;
|
|
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
|
+
}
|
|
740
|
+
}
|
|
511
741
|
function gap(conversation) {
|
|
512
742
|
gaps.set(conversation, (gaps.get(conversation) ?? 0) + 1);
|
|
513
743
|
behind.add(conversation);
|
|
@@ -516,7 +746,7 @@ export function createConversationStore(deps) {
|
|
|
516
746
|
return deps.durably(async (local) => {
|
|
517
747
|
if (!replicated.has(conversation))
|
|
518
748
|
return;
|
|
519
|
-
const read =
|
|
749
|
+
const read = (await readsOf(local, [conversation])).get(conversation)?.read;
|
|
520
750
|
const advances = read !== undefined &&
|
|
521
751
|
!behind.has(conversation) &&
|
|
522
752
|
(read === null || message.position > read);
|
|
@@ -526,23 +756,22 @@ export function createConversationStore(deps) {
|
|
|
526
756
|
messages: [message],
|
|
527
757
|
...(advances ? { read: { at: message.position, partial: false } } : {}),
|
|
528
758
|
});
|
|
529
|
-
stored.add(conversation);
|
|
530
|
-
if (advances)
|
|
531
|
-
reads.set(conversation, message.position);
|
|
532
759
|
sealTurns(conversation, new Set([message.turn]));
|
|
533
760
|
await slide(local, conversation, [message]);
|
|
534
761
|
deps.publish(conversationChangeSet([conversation]));
|
|
762
|
+
tellList();
|
|
535
763
|
});
|
|
536
764
|
}
|
|
537
765
|
async function readHistories(conversations, ended) {
|
|
538
766
|
const generations = new Map(conversations.map((one) => [one, gaps.get(one) ?? 0]));
|
|
767
|
+
const held = await readsOf(await deps.local(), conversations);
|
|
539
768
|
let waiting = conversations.map((conversation) => ({
|
|
540
769
|
conversation,
|
|
541
|
-
after:
|
|
770
|
+
after: held.get(conversation)?.read ?? undefined,
|
|
542
771
|
}));
|
|
543
772
|
while (waiting.length > 0) {
|
|
544
773
|
const asking = waiting.slice(0, HISTORY_CONVERSATIONS);
|
|
545
|
-
const result = await deps.
|
|
774
|
+
const result = await deps.history({
|
|
546
775
|
conversations: asking.map(({ conversation, after }) => ({
|
|
547
776
|
conversation,
|
|
548
777
|
...(after === undefined ? {} : { after }),
|
|
@@ -594,15 +823,10 @@ export function createConversationStore(deps) {
|
|
|
594
823
|
return;
|
|
595
824
|
const whole = next === undefined;
|
|
596
825
|
const cursor = next ?? newestOf(arriving, from);
|
|
597
|
-
const
|
|
826
|
+
const held = (await readsOf(local, [conversation])).get(conversation);
|
|
827
|
+
const partial = !whole && (held?.partial === true || held?.read === undefined);
|
|
598
828
|
const sealing = await sealed(local, conversation, new Set(arriving.map((message) => message.turn)));
|
|
599
829
|
await local.keepMessages({ conversation, messages: arriving, read: { at: cursor, partial } });
|
|
600
|
-
stored.add(conversation);
|
|
601
|
-
reads.set(conversation, cursor);
|
|
602
|
-
if (partial)
|
|
603
|
-
partials.add(conversation);
|
|
604
|
-
else
|
|
605
|
-
partials.delete(conversation);
|
|
606
830
|
sealTurns(conversation, new Set(arriving.map((message) => message.turn)));
|
|
607
831
|
if (whole && (gaps.get(conversation) ?? 0) === generation)
|
|
608
832
|
behind.delete(conversation);
|
|
@@ -610,6 +834,8 @@ export function createConversationStore(deps) {
|
|
|
610
834
|
if (arriving.length > 0 || sealing || whole) {
|
|
611
835
|
deps.publish(conversationChangeSet([conversation]));
|
|
612
836
|
}
|
|
837
|
+
if (arriving.length > 0)
|
|
838
|
+
tellSoon();
|
|
613
839
|
});
|
|
614
840
|
}
|
|
615
841
|
function heard(event) {
|
|
@@ -645,6 +871,10 @@ export function createConversationStore(deps) {
|
|
|
645
871
|
live(conversation, { starting: event.step });
|
|
646
872
|
return;
|
|
647
873
|
}
|
|
874
|
+
if (event.kind === 'subagents') {
|
|
875
|
+
live(conversation, { subagents: structuredClone(event.subagents) });
|
|
876
|
+
return;
|
|
877
|
+
}
|
|
648
878
|
if (event.kind === 'link') {
|
|
649
879
|
live(conversation, { link: { connected: event.connected } });
|
|
650
880
|
return;
|
|
@@ -656,6 +886,7 @@ export function createConversationStore(deps) {
|
|
|
656
886
|
const arriving = acceptedMessage(event.message);
|
|
657
887
|
if (arriving === null)
|
|
658
888
|
return;
|
|
889
|
+
noted(conversation, arriving);
|
|
659
890
|
if (replicated.has(conversation)) {
|
|
660
891
|
if (deps.full()) {
|
|
661
892
|
gap(conversation);
|
|
@@ -671,17 +902,24 @@ export function createConversationStore(deps) {
|
|
|
671
902
|
emit(conversation);
|
|
672
903
|
}
|
|
673
904
|
async function kept(local, conversation, following) {
|
|
674
|
-
|
|
905
|
+
const shown = following === null || empty(following) ? null : following;
|
|
906
|
+
if (shown === null)
|
|
675
907
|
await local.dropChatDraft(conversation);
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
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;
|
|
681
916
|
}
|
|
682
917
|
}
|
|
918
|
+
async function draftAt(local, conversation) {
|
|
919
|
+
return (await local.chatDraft(conversation)) ?? undefined;
|
|
920
|
+
}
|
|
683
921
|
async function sealed(local, conversation, turns) {
|
|
684
|
-
const held =
|
|
922
|
+
const held = await draftAt(local, conversation);
|
|
685
923
|
const submitted = held?.submitted ?? [];
|
|
686
924
|
const left = submitted.filter((one) => one.turn === null || !turns.has(one.turn));
|
|
687
925
|
if (held === undefined || left.length === submitted.length)
|
|
@@ -692,8 +930,8 @@ export function createConversationStore(deps) {
|
|
|
692
930
|
function moved(agent, operation, to) {
|
|
693
931
|
const from = newChat(agent);
|
|
694
932
|
return deps.durably(async (local) => {
|
|
695
|
-
const held =
|
|
696
|
-
const earlier =
|
|
933
|
+
const held = await draftAt(local, from);
|
|
934
|
+
const earlier = held?.moves ?? [];
|
|
697
935
|
if (earlier.includes(operation))
|
|
698
936
|
return;
|
|
699
937
|
const blank = {
|
|
@@ -702,18 +940,20 @@ export function createConversationStore(deps) {
|
|
|
702
940
|
revision: chatDraftRevision(),
|
|
703
941
|
};
|
|
704
942
|
const { moves: _answered, ...unsent } = held ?? blank;
|
|
705
|
-
const taken =
|
|
943
|
+
const taken = (await draftAt(local, to)) !== undefined || empty(unsent)
|
|
944
|
+
? []
|
|
945
|
+
: [{ ...unsent, conversation: to }];
|
|
706
946
|
const left = { ...(taken.length === 0 ? unsent : blank), moves: [...earlier, operation] };
|
|
707
947
|
await local.keepChatDrafts([left, ...taken]);
|
|
708
|
-
|
|
948
|
+
shownDraft(from, left);
|
|
709
949
|
for (const one of taken)
|
|
710
|
-
|
|
950
|
+
shownDraft(to, one);
|
|
711
951
|
deps.publish(conversationChangeSet([from, to]));
|
|
712
952
|
});
|
|
713
953
|
}
|
|
714
954
|
function withDraft(conversation, next) {
|
|
715
955
|
return deps.durably(async (local) => {
|
|
716
|
-
const following = await next(
|
|
956
|
+
const following = await next(await draftAt(local, conversation), local);
|
|
717
957
|
if (following === undefined)
|
|
718
958
|
return;
|
|
719
959
|
await kept(local, conversation, following);
|
|
@@ -722,10 +962,8 @@ export function createConversationStore(deps) {
|
|
|
722
962
|
}
|
|
723
963
|
async function submitted(conversation, operation, draft) {
|
|
724
964
|
let submission;
|
|
725
|
-
let earlier = false;
|
|
726
965
|
await withDraft(conversation, (current) => {
|
|
727
966
|
submission = current?.submitted?.find((one) => one.operation === operation);
|
|
728
|
-
earlier = submission !== undefined;
|
|
729
967
|
if (submission !== undefined)
|
|
730
968
|
return undefined;
|
|
731
969
|
const said = draft ?? current;
|
|
@@ -738,23 +976,15 @@ export function createConversationStore(deps) {
|
|
|
738
976
|
submitted: [...(current?.submitted ?? []), submission],
|
|
739
977
|
};
|
|
740
978
|
});
|
|
741
|
-
return
|
|
742
|
-
}
|
|
743
|
-
function forgotten(conversation, submission) {
|
|
744
|
-
return withDraft(conversation, (current) => {
|
|
745
|
-
const held = current?.submitted ?? [];
|
|
746
|
-
if (current === undefined || !held.includes(submission))
|
|
747
|
-
return undefined;
|
|
748
|
-
return withSubmitted(current, held.filter((one) => one !== submission));
|
|
749
|
-
});
|
|
979
|
+
return submission;
|
|
750
980
|
}
|
|
751
981
|
async function dispatched(conversation, submission) {
|
|
752
982
|
const dispatch = { answered: false };
|
|
753
983
|
sending.set(submission.operation, dispatch);
|
|
754
984
|
try {
|
|
755
|
-
const result = await invoke('
|
|
756
|
-
text: submission.text,
|
|
985
|
+
const result = await invoke('conversations/send', conversation, {
|
|
757
986
|
operation_id: submission.operation,
|
|
987
|
+
text: submission.text,
|
|
758
988
|
});
|
|
759
989
|
dispatch.answered = result.kind !== 'transport-uncertainty';
|
|
760
990
|
if (result.kind === 'revoked-authorization')
|
|
@@ -763,7 +993,7 @@ export function createConversationStore(deps) {
|
|
|
763
993
|
deps.unanswered();
|
|
764
994
|
return result;
|
|
765
995
|
}
|
|
766
|
-
const turn = answered(result, '
|
|
996
|
+
const turn = answered(result, 'message_id');
|
|
767
997
|
await withDraft(conversation, async (current, local) => {
|
|
768
998
|
const held = current?.submitted?.find((one) => one.operation === submission.operation);
|
|
769
999
|
if (current === undefined || held === undefined || held.turn !== null)
|
|
@@ -783,29 +1013,41 @@ export function createConversationStore(deps) {
|
|
|
783
1013
|
sending.delete(submission.operation);
|
|
784
1014
|
}
|
|
785
1015
|
}
|
|
786
|
-
function invoke(name, conversation, extra = {}) {
|
|
787
|
-
return deps.call(name, { conversation, ...extra });
|
|
1016
|
+
function invoke(name, conversation, extra = { operation_id: changeBatchIdentity() }) {
|
|
1017
|
+
return deps.call(name, { conversation_id: conversation, ...extra });
|
|
788
1018
|
}
|
|
789
1019
|
const surface = {
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
1020
|
+
list() {
|
|
1021
|
+
return new Promise((settle) => {
|
|
1022
|
+
const answer = (listed) => {
|
|
1023
|
+
unlisted.delete(answer);
|
|
1024
|
+
listWatchers.delete(answer);
|
|
1025
|
+
settle(listed);
|
|
1026
|
+
};
|
|
1027
|
+
unlisted.add(answer);
|
|
1028
|
+
learned();
|
|
1029
|
+
});
|
|
793
1030
|
},
|
|
794
1031
|
async read(conversation, options = {}) {
|
|
795
|
-
const
|
|
1032
|
+
const { text, ...windowed } = options;
|
|
1033
|
+
const asked = askedOf(windowed);
|
|
1034
|
+
const phrase = phraseOf(text);
|
|
796
1035
|
return deps.durably(async (local) => {
|
|
797
|
-
const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before);
|
|
798
|
-
const total = asked.total ? await local.messageCount(conversation) : null;
|
|
799
|
-
|
|
1036
|
+
const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before, phrase);
|
|
1037
|
+
const total = asked.total ? await local.messageCount(conversation, phrase) : null;
|
|
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));
|
|
800
1040
|
});
|
|
801
1041
|
},
|
|
802
1042
|
async create(input) {
|
|
803
|
-
const result = await deps.call('
|
|
804
|
-
const conversation = answered(result, '
|
|
1043
|
+
const result = await deps.call('conversations/create', input);
|
|
1044
|
+
const conversation = answered(result, 'conversation_id');
|
|
805
1045
|
if (conversation === null)
|
|
806
1046
|
return result;
|
|
807
|
-
|
|
1047
|
+
const listed = (await heldList()) ?? [];
|
|
1048
|
+
if (!listed.some((entry) => entry.conversation === conversation)) {
|
|
808
1049
|
creations.add(conversation);
|
|
1050
|
+
}
|
|
809
1051
|
const creation = creationOf(input);
|
|
810
1052
|
if (creation !== null)
|
|
811
1053
|
await moved(creation.agent, creation.operation, conversation);
|
|
@@ -822,11 +1064,7 @@ export function createConversationStore(deps) {
|
|
|
822
1064
|
});
|
|
823
1065
|
if (held === null)
|
|
824
1066
|
return DURABLE_WRITE;
|
|
825
|
-
|
|
826
|
-
if (!earlier || !deps.expired(operation))
|
|
827
|
-
return dispatched(conversation, submission);
|
|
828
|
-
await forgotten(conversation, submission);
|
|
829
|
-
return EXPIRED;
|
|
1067
|
+
return dispatched(conversation, held);
|
|
830
1068
|
}
|
|
831
1069
|
if (draft !== undefined) {
|
|
832
1070
|
await withDraft(conversation, (current) => current?.submitted?.some((one) => one.operation === operation) === true
|
|
@@ -836,28 +1074,19 @@ export function createConversationStore(deps) {
|
|
|
836
1074
|
return OFFLINE;
|
|
837
1075
|
},
|
|
838
1076
|
answer(conversation, answer) {
|
|
839
|
-
return invoke('
|
|
1077
|
+
return invoke('conversations/interaction/answer', conversation, {
|
|
1078
|
+
operation_id: changeBatchIdentity(),
|
|
1079
|
+
...answer,
|
|
1080
|
+
});
|
|
840
1081
|
},
|
|
841
1082
|
interrupt(conversation) {
|
|
842
|
-
return invoke('
|
|
1083
|
+
return invoke('conversations/interrupt', conversation);
|
|
843
1084
|
},
|
|
844
1085
|
kill(conversation) {
|
|
845
|
-
return invoke('
|
|
846
|
-
},
|
|
847
|
-
async cancel(conversation, turn) {
|
|
848
|
-
const result = await invoke('conversation.cancel', conversation, { turn });
|
|
849
|
-
if (result.kind !== 'action')
|
|
850
|
-
return result;
|
|
851
|
-
await withDraft(conversation, (current) => {
|
|
852
|
-
const held = current?.submitted?.find((one) => one.turn === turn);
|
|
853
|
-
if (current === undefined || held === undefined)
|
|
854
|
-
return undefined;
|
|
855
|
-
return returned(current, held, current.submitted.filter((one) => one !== held));
|
|
856
|
-
});
|
|
857
|
-
return result;
|
|
1086
|
+
return invoke('conversations/kill', conversation);
|
|
858
1087
|
},
|
|
859
1088
|
delete(conversation) {
|
|
860
|
-
return invoke('
|
|
1089
|
+
return invoke('conversations/delete', conversation);
|
|
861
1090
|
},
|
|
862
1091
|
};
|
|
863
1092
|
const chatDraft = {
|
|
@@ -886,33 +1115,40 @@ export function createConversationStore(deps) {
|
|
|
886
1115
|
return {
|
|
887
1116
|
surface,
|
|
888
1117
|
chatDraft,
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
if (entry.read !== undefined)
|
|
896
|
-
reads.set(entry.conversation, entry.read);
|
|
897
|
-
if (entry.partial)
|
|
898
|
-
partials.add(entry.conversation);
|
|
899
|
-
}
|
|
900
|
-
drafts.clear();
|
|
901
|
-
for (const record of kept)
|
|
902
|
-
drafts.set(record.conversation, record);
|
|
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();
|
|
903
1124
|
},
|
|
904
1125
|
async reconcile() {
|
|
905
1126
|
if (!listKnown)
|
|
906
1127
|
return;
|
|
907
|
-
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]));
|
|
908
1138
|
const leaving = [
|
|
909
|
-
...new Set([
|
|
1139
|
+
...new Set([
|
|
1140
|
+
...replicated,
|
|
1141
|
+
...reads.keys(),
|
|
1142
|
+
...kept.map(({ conversation }) => conversation),
|
|
1143
|
+
]),
|
|
910
1144
|
].filter((conversation) => !named.has(conversation) &&
|
|
911
1145
|
!creations.has(conversation) &&
|
|
912
|
-
(!conversation.startsWith(NEW_CHAT) || retiredChat(conversation)));
|
|
1146
|
+
(!conversation.startsWith(NEW_CHAT) || retiredChat(conversation, list)));
|
|
913
1147
|
for (const conversation of named) {
|
|
914
|
-
if (!replicated.has(conversation)
|
|
1148
|
+
if (!replicated.has(conversation) &&
|
|
1149
|
+
missed(conversation, reads.get(conversation), entries.get(conversation))) {
|
|
915
1150
|
behind.add(conversation);
|
|
1151
|
+
}
|
|
916
1152
|
}
|
|
917
1153
|
replicated = named;
|
|
918
1154
|
handReplicated();
|
|
@@ -943,16 +1179,16 @@ export function createConversationStore(deps) {
|
|
|
943
1179
|
progress.complete = false;
|
|
944
1180
|
},
|
|
945
1181
|
async resend(progress) {
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
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;
|
|
950
1189
|
if (deps.closed())
|
|
951
1190
|
return;
|
|
952
|
-
|
|
953
|
-
await forgotten(conversation, submission);
|
|
954
|
-
continue;
|
|
955
|
-
}
|
|
1191
|
+
tried.add(submission.operation);
|
|
956
1192
|
const result = await dispatched(conversation, submission);
|
|
957
1193
|
if (result.kind === 'revoked-authorization')
|
|
958
1194
|
return;
|
|
@@ -973,11 +1209,8 @@ export function createConversationStore(deps) {
|
|
|
973
1209
|
known() {
|
|
974
1210
|
return listKnown;
|
|
975
1211
|
},
|
|
976
|
-
reconnected() {
|
|
977
|
-
for (const conversation of replicated)
|
|
978
|
-
gap(conversation);
|
|
979
|
-
},
|
|
980
1212
|
reopened() {
|
|
1213
|
+
relisting = null;
|
|
981
1214
|
for (const [operation, dispatch] of sending)
|
|
982
1215
|
if (!dispatch.answered)
|
|
983
1216
|
sending.delete(operation);
|
|
@@ -993,30 +1226,37 @@ export function createConversationStore(deps) {
|
|
|
993
1226
|
asked,
|
|
994
1227
|
watchers: new Set(),
|
|
995
1228
|
window: null,
|
|
1229
|
+
draft: null,
|
|
1230
|
+
ended: null,
|
|
1231
|
+
marked: [],
|
|
996
1232
|
more: false,
|
|
997
1233
|
total: null,
|
|
998
1234
|
};
|
|
999
1235
|
views.set(key, view);
|
|
1000
1236
|
const watcher = { notify: deliver, ready: false };
|
|
1001
1237
|
view.watchers.add(watcher);
|
|
1002
|
-
|
|
1003
|
-
|
|
1238
|
+
const reading = new AbortController();
|
|
1239
|
+
void untilAnswered(() => deps.durably(async (local) => {
|
|
1004
1240
|
if (view.window !== null)
|
|
1005
1241
|
return;
|
|
1006
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);
|
|
1007
1245
|
view.window = messages;
|
|
1246
|
+
view.marked = await local.jobMarkers(conversation);
|
|
1008
1247
|
view.more = more;
|
|
1009
1248
|
view.total = asked.total ? await local.messageCount(conversation) : null;
|
|
1010
|
-
})
|
|
1249
|
+
}), reading.signal)
|
|
1011
1250
|
.then(() => {
|
|
1012
1251
|
if (deps.closed() || !view.watchers.has(watcher))
|
|
1013
1252
|
return;
|
|
1014
1253
|
watcher.ready = true;
|
|
1015
1254
|
const window = view.window ?? [];
|
|
1016
|
-
watcher.notify(viewOf(conversation, window, sharedFrame(view, window)));
|
|
1255
|
+
watcher.notify(viewOf(conversation, window, sharedFrame(view, window), view.draft, view.ended, view.marked));
|
|
1017
1256
|
})
|
|
1018
1257
|
.catch(() => { });
|
|
1019
1258
|
return () => {
|
|
1259
|
+
reading.abort();
|
|
1020
1260
|
if (!view.watchers.delete(watcher))
|
|
1021
1261
|
return;
|
|
1022
1262
|
if (view.watchers.size === 0)
|
|
@@ -1024,77 +1264,73 @@ export function createConversationStore(deps) {
|
|
|
1024
1264
|
};
|
|
1025
1265
|
},
|
|
1026
1266
|
watchList(deliver) {
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
if (!deps.closed() && listWatchers.has(deliver))
|
|
1030
|
-
deliver(entries());
|
|
1031
|
-
});
|
|
1267
|
+
unlisted.add(deliver);
|
|
1268
|
+
learned();
|
|
1032
1269
|
return () => {
|
|
1270
|
+
unlisted.delete(deliver);
|
|
1033
1271
|
listWatchers.delete(deliver);
|
|
1034
1272
|
};
|
|
1035
1273
|
},
|
|
1036
1274
|
watchAgents(deliver) {
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
deliver(agents());
|
|
1041
|
-
});
|
|
1275
|
+
unnamed.add(deliver);
|
|
1276
|
+
learned();
|
|
1277
|
+
readAgents();
|
|
1042
1278
|
return () => {
|
|
1279
|
+
unnamed.delete(deliver);
|
|
1043
1280
|
agentWatchers.delete(deliver);
|
|
1044
1281
|
};
|
|
1045
1282
|
},
|
|
1283
|
+
learned,
|
|
1046
1284
|
changed(set) {
|
|
1047
1285
|
for (const conversation of set.conversations)
|
|
1048
1286
|
emit(conversation);
|
|
1049
|
-
const declaration = deps.agents();
|
|
1050
|
-
if (declaration !== undefined &&
|
|
1051
|
-
set.documents.some((change) => change.locator === declaration.locator)) {
|
|
1052
|
-
agentsRead = readAgents().catch(() => {
|
|
1053
|
-
agentsRead = null;
|
|
1054
|
-
});
|
|
1055
|
-
}
|
|
1056
1287
|
},
|
|
1057
1288
|
receive(event) {
|
|
1058
1289
|
if (deps.closed())
|
|
1059
|
-
return
|
|
1060
|
-
if (event.kind === 'list'
|
|
1061
|
-
if (event.
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
}
|
|
1067
|
-
listKnown = true;
|
|
1068
|
-
openingEnded();
|
|
1069
|
-
}
|
|
1070
|
-
else if (event.kind === 'listed') {
|
|
1071
|
-
listing.set(event.entry.conversation, { ...event.entry });
|
|
1072
|
-
creations.delete(event.entry.conversation);
|
|
1073
|
-
}
|
|
1074
|
-
else {
|
|
1075
|
-
listing.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 });
|
|
1076
1297
|
}
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
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;
|
|
1082
1319
|
}
|
|
1083
1320
|
heard(event);
|
|
1084
|
-
return false;
|
|
1085
|
-
},
|
|
1086
|
-
searchable() {
|
|
1087
|
-
return [...(listKnown ? listing.keys() : stored)].sort();
|
|
1088
|
-
},
|
|
1089
|
-
acquired(conversation) {
|
|
1090
|
-
return reads.has(conversation) && !partials.has(conversation);
|
|
1091
1321
|
},
|
|
1092
1322
|
close() {
|
|
1323
|
+
closing.abort();
|
|
1324
|
+
if (rowTimer !== null)
|
|
1325
|
+
clearTimeout(rowTimer);
|
|
1326
|
+
rowTimer = null;
|
|
1093
1327
|
views.clear();
|
|
1094
1328
|
lives.clear();
|
|
1095
1329
|
unsent.clear();
|
|
1096
1330
|
listWatchers.clear();
|
|
1331
|
+
unlisted.clear();
|
|
1097
1332
|
agentWatchers.clear();
|
|
1333
|
+
unnamed.clear();
|
|
1098
1334
|
},
|
|
1099
1335
|
};
|
|
1100
1336
|
}
|