@agentshouse/mdruntime 0.1.0-c3903a68dcbbf → 0.1.0-c3a5b012b4a8c
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 +26 -13
- package/dist/adapter.d.ts +244 -88
- package/dist/adapter.d.ts.map +1 -1
- package/dist/adapter.js +1 -9
- package/dist/attached-model.d.ts +8 -0
- package/dist/attached-model.d.ts.map +1 -0
- package/dist/attached-model.js +46 -0
- package/dist/attempt.d.ts +16 -0
- package/dist/attempt.d.ts.map +1 -0
- package/dist/attempt.js +70 -0
- package/dist/binary.d.ts +18 -0
- package/dist/binary.d.ts.map +1 -0
- package/dist/binary.js +8 -0
- package/dist/bootstrap.d.ts +40 -6
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +336 -52
- package/dist/chain.d.ts +12 -0
- package/dist/chain.d.ts.map +1 -0
- package/dist/chain.js +64 -0
- package/dist/change-set.d.ts +9 -14
- package/dist/change-set.d.ts.map +1 -1
- package/dist/change-set.js +11 -60
- package/dist/change.d.ts +46 -0
- package/dist/change.d.ts.map +1 -1
- package/dist/change.js +34 -0
- package/dist/client.d.ts +44 -24
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +161 -36
- package/dist/collection.d.ts +13 -16
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +52 -66
- package/dist/compile.d.ts +11 -0
- package/dist/compile.d.ts.map +1 -0
- package/dist/compile.js +717 -0
- package/dist/connection.d.ts +69 -44
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +4581 -1472
- 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 +76 -27
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +1052 -273
- package/dist/digest.d.ts +7 -0
- package/dist/digest.d.ts.map +1 -0
- package/dist/digest.js +113 -0
- package/dist/durability.d.ts +10 -1
- package/dist/durability.d.ts.map +1 -1
- package/dist/durability.js +5 -0
- package/dist/entry.d.ts +20 -0
- package/dist/entry.d.ts.map +1 -0
- package/dist/entry.js +161 -0
- package/dist/expectation.d.ts +21 -0
- package/dist/expectation.d.ts.map +1 -0
- package/dist/expectation.js +70 -0
- package/dist/failure.d.ts +13 -0
- package/dist/failure.d.ts.map +1 -0
- package/dist/failure.js +19 -0
- package/dist/file.d.ts +2 -19
- package/dist/file.d.ts.map +1 -1
- package/dist/file.js +7 -122
- package/dist/host.d.ts +17 -3
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +163 -75
- package/dist/identity.d.ts +3 -0
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +13 -1
- package/dist/index.d.ts +26 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -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 +23 -0
- package/dist/live.d.ts.map +1 -0
- package/dist/live.js +165 -0
- package/dist/media.d.ts +8 -0
- package/dist/media.d.ts.map +1 -0
- package/dist/media.js +218 -0
- package/dist/memory.d.ts +30 -9
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +337 -87
- package/dist/messages.d.ts +52 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/messages.js +168 -0
- package/dist/model.d.ts +17 -16
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +9 -9
- package/dist/name.d.ts +4 -0
- package/dist/name.d.ts.map +1 -0
- package/dist/name.js +122 -0
- package/dist/observation.d.ts +22 -2
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +133 -17
- package/dist/open.d.ts +2 -0
- package/dist/open.d.ts.map +1 -0
- package/dist/open.js +1 -0
- package/dist/opened.d.ts +22 -0
- package/dist/opened.d.ts.map +1 -0
- package/dist/opened.js +107 -0
- package/dist/outcome.d.ts +72 -21
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +81 -1
- package/dist/overlay.d.ts +16 -5
- package/dist/overlay.d.ts.map +1 -1
- package/dist/overlay.js +147 -180
- package/dist/parts.d.ts +59 -0
- package/dist/parts.d.ts.map +1 -0
- package/dist/parts.js +118 -0
- package/dist/paths.d.ts +1 -1
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +4 -16
- package/dist/query.d.ts +56 -180
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +3 -422
- 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 +54 -0
- package/dist/reach.d.ts.map +1 -0
- package/dist/reach.js +483 -0
- package/dist/react.d.ts +45 -0
- package/dist/react.d.ts.map +1 -0
- package/dist/react.js +54 -0
- 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 +50 -0
- package/dist/readiness.d.ts.map +1 -0
- package/dist/readiness.js +27 -0
- package/dist/recovery.d.ts +26 -0
- package/dist/recovery.d.ts.map +1 -0
- package/dist/recovery.js +232 -0
- package/dist/refresh.d.ts +21 -7
- package/dist/refresh.d.ts.map +1 -1
- package/dist/refresh.js +155 -55
- package/dist/replica.d.ts +61 -33
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +355 -162
- package/dist/room-draft.d.ts +62 -11
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +281 -83
- package/dist/rows.d.ts +32 -34
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +65 -87
- package/dist/save.d.ts +74 -6
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +902 -183
- package/dist/search.d.ts +15 -54
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +10 -121
- package/dist/seed.d.ts +16 -0
- package/dist/seed.d.ts.map +1 -0
- package/dist/seed.js +75 -0
- package/dist/shared.d.ts +15 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +421 -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 +7 -4
- package/dist/status.d.ts.map +1 -1
- package/dist/status.js +3 -15
- package/dist/store.d.ts +178 -80
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +372 -381
- package/dist/stored.d.ts +156 -0
- package/dist/stored.d.ts.map +1 -0
- package/dist/stored.js +637 -0
- package/dist/tables.d.ts +388 -0
- package/dist/tables.d.ts.map +1 -0
- package/dist/tables.js +706 -0
- package/dist/transfer.d.ts +0 -5
- package/dist/transfer.d.ts.map +1 -1
- package/dist/turn.d.ts +9 -0
- package/dist/turn.d.ts.map +1 -0
- package/dist/turn.js +75 -0
- package/dist/validation.d.ts +29 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +104 -0
- package/dist/windows.d.ts +11 -0
- package/dist/windows.d.ts.map +1 -0
- package/dist/windows.js +16 -0
- package/dist/wire.d.ts +199 -19
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +92 -67
- package/package.json +33 -10
- package/AGENTS.md +0 -210
- package/dist/attachment.d.ts +0 -3
- package/dist/attachment.d.ts.map +0 -1
- package/dist/attachment.js +0 -8
- package/dist/read-side.d.ts +0 -14
- package/dist/read-side.d.ts.map +0 -1
- package/dist/read-side.js +0 -371
package/dist/conversation.js
CHANGED
|
@@ -1,26 +1,138 @@
|
|
|
1
|
+
import { cursorOf, decodedCursor } from '@agentshouse/mdmodel';
|
|
1
2
|
import { conversationChangeSet } from './change-set.js';
|
|
2
|
-
import {
|
|
3
|
+
import { heldFacts } from './conversation-row.js';
|
|
4
|
+
import { runtimeError } from './failure.js';
|
|
5
|
+
import { refusedForSpace } from './durability.js';
|
|
6
|
+
import { changeBatchIdentity, chatDraftRevision } from './identity.js';
|
|
7
|
+
import { OFFLINE } from './outcome.js';
|
|
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';
|
|
3
12
|
const QUIET = {
|
|
4
|
-
|
|
5
|
-
plan: null,
|
|
6
|
-
workingNote: null,
|
|
13
|
+
turn: null,
|
|
7
14
|
questions: [],
|
|
8
15
|
process: null,
|
|
9
16
|
commands: [],
|
|
10
17
|
options: [],
|
|
18
|
+
starting: null,
|
|
19
|
+
subagents: [],
|
|
20
|
+
link: null,
|
|
21
|
+
deliveryUnknown: [],
|
|
11
22
|
};
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
23
|
+
const FACTS = [
|
|
24
|
+
'questions',
|
|
25
|
+
'process',
|
|
26
|
+
'commands',
|
|
27
|
+
'options',
|
|
28
|
+
'starting',
|
|
29
|
+
'subagents',
|
|
30
|
+
'link',
|
|
31
|
+
'deliveryUnknown',
|
|
32
|
+
];
|
|
33
|
+
export function unheldView(conversation) {
|
|
34
|
+
return {
|
|
35
|
+
conversation,
|
|
36
|
+
messages: [],
|
|
37
|
+
turn: null,
|
|
38
|
+
questions: [],
|
|
39
|
+
process: null,
|
|
40
|
+
processEnded: null,
|
|
41
|
+
commands: [],
|
|
42
|
+
options: [],
|
|
43
|
+
starting: null,
|
|
44
|
+
subagents: [],
|
|
45
|
+
link: null,
|
|
46
|
+
deliveryUnknown: [],
|
|
47
|
+
chatDraft: null,
|
|
48
|
+
jobs: [],
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
export const SESSION_STARTED = 'Session started';
|
|
52
|
+
const NEW_CHAT = 'new:';
|
|
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;
|
|
56
|
+
export function newChat(agent) {
|
|
57
|
+
return `${NEW_CHAT}${agent}`;
|
|
58
|
+
}
|
|
59
|
+
function creationOf(input) {
|
|
60
|
+
if (typeof input !== 'object' || input === null)
|
|
61
|
+
return null;
|
|
62
|
+
const { agent_id: agent, operation_id: operation } = input;
|
|
63
|
+
return typeof agent === 'string' && typeof operation === 'string' ? { agent, operation } : null;
|
|
64
|
+
}
|
|
65
|
+
function startedTurn(turn, operation) {
|
|
66
|
+
if (operation.op === 'begin')
|
|
67
|
+
return { turn, started: operation.at, parts: [], context: null };
|
|
68
|
+
if (operation.op === 'context') {
|
|
69
|
+
return {
|
|
70
|
+
turn,
|
|
71
|
+
started: null,
|
|
72
|
+
parts: [],
|
|
73
|
+
context: { used: operation.used, window: operation.window },
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
return operation.op === 'start' && operation.index === 0
|
|
77
|
+
? { turn, started: null, parts: [structuredClone(operation.part)], context: null }
|
|
78
|
+
: null;
|
|
79
|
+
}
|
|
80
|
+
function changedFields(part, operation) {
|
|
81
|
+
if (operation.op === 'append') {
|
|
82
|
+
return { text: `${String(part.text ?? '')}${operation.text}` };
|
|
83
|
+
}
|
|
84
|
+
if (operation.op === 'status') {
|
|
85
|
+
return { status: operation.status, result: structuredClone(operation.result) };
|
|
86
|
+
}
|
|
87
|
+
if (operation.op === 'count')
|
|
88
|
+
return { count: operation.count };
|
|
89
|
+
return { entries: structuredClone(operation.entries) };
|
|
90
|
+
}
|
|
91
|
+
function appliedTurn(held, operation) {
|
|
92
|
+
if (operation.op === 'begin')
|
|
93
|
+
return { ...held, started: operation.at };
|
|
94
|
+
if (operation.op === 'context') {
|
|
95
|
+
return { ...held, context: { used: operation.used, window: operation.window } };
|
|
96
|
+
}
|
|
97
|
+
if (operation.op === 'start') {
|
|
98
|
+
if (operation.index !== 0 && operation.index !== held.parts.length)
|
|
99
|
+
return null;
|
|
100
|
+
return {
|
|
101
|
+
...held,
|
|
102
|
+
parts: [...held.parts.slice(0, operation.index), structuredClone(operation.part)],
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
const part = held.parts[operation.index];
|
|
106
|
+
if (typeof part !== 'object' || part === null)
|
|
107
|
+
return null;
|
|
108
|
+
const changed = { ...part, ...changedFields(part, operation) };
|
|
109
|
+
return {
|
|
110
|
+
...held,
|
|
111
|
+
parts: held.parts.map((kept, at) => (at === operation.index ? changed : kept)),
|
|
112
|
+
};
|
|
18
113
|
}
|
|
19
|
-
function
|
|
114
|
+
function windowOf(held, arriving, limit) {
|
|
20
115
|
const byIdentity = new Map(held.map((message) => [message.identity, message]));
|
|
21
116
|
for (const message of arriving)
|
|
22
117
|
byIdentity.set(message.identity, message);
|
|
23
|
-
|
|
118
|
+
const next = [...byIdentity.values()].sort(messageOrder);
|
|
119
|
+
const start = Math.max(0, next.length - limit);
|
|
120
|
+
return { messages: next.slice(start), dropped: start > 0 };
|
|
121
|
+
}
|
|
122
|
+
function frameOf(asked, messages, more, total) {
|
|
123
|
+
const oldest = messages[0];
|
|
124
|
+
const next = !more
|
|
125
|
+
? undefined
|
|
126
|
+
: oldest === undefined
|
|
127
|
+
? (asked.after ?? FROM_START)
|
|
128
|
+
: cursorOf([oldest.position, oldest.identity]);
|
|
129
|
+
return {
|
|
130
|
+
...(next === undefined ? {} : { next }),
|
|
131
|
+
...(total === null ? {} : { total }),
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
function newestOf(arriving, from) {
|
|
135
|
+
return arriving.reduce((top, message) => (top === null || message.position > top ? message.position : top), from);
|
|
24
136
|
}
|
|
25
137
|
function acceptedMessage(value) {
|
|
26
138
|
if (typeof value !== 'object' || value === null)
|
|
@@ -79,90 +191,450 @@ function historyPages(result) {
|
|
|
79
191
|
}
|
|
80
192
|
return pages;
|
|
81
193
|
}
|
|
82
|
-
function
|
|
194
|
+
function answered(result, field) {
|
|
83
195
|
if (result.kind !== 'action')
|
|
84
196
|
return null;
|
|
85
197
|
const answer = result.result;
|
|
86
198
|
if (typeof answer !== 'object' || answer === null)
|
|
87
199
|
return null;
|
|
88
|
-
const
|
|
89
|
-
return typeof
|
|
200
|
+
const value = answer[field];
|
|
201
|
+
return typeof value === 'string' ? value : null;
|
|
90
202
|
}
|
|
91
|
-
function
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
if (
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
203
|
+
function beforeOf(after) {
|
|
204
|
+
if (after === undefined)
|
|
205
|
+
return null;
|
|
206
|
+
const decoded = typeof after === 'string' ? decodedCursor(after) : null;
|
|
207
|
+
if (decoded?.length === 0)
|
|
208
|
+
return null;
|
|
209
|
+
const [position, identity] = decoded ?? [];
|
|
210
|
+
if (decoded?.length !== 2 || typeof position !== 'string' || typeof identity !== 'string') {
|
|
211
|
+
throw runtimeError('invalid-after', `conversation after must be the next an earlier view returned, got ${String(after)}`);
|
|
212
|
+
}
|
|
213
|
+
return [position, identity];
|
|
214
|
+
}
|
|
215
|
+
function askedOf(options) {
|
|
216
|
+
const { limit, after, total } = options;
|
|
217
|
+
if (limit !== undefined && (!Number.isInteger(limit) || limit < 0)) {
|
|
218
|
+
throw runtimeError('invalid-limit', `conversation limit must be a non-negative integer, got ${String(limit)}`);
|
|
219
|
+
}
|
|
220
|
+
return { limit: capped(limit), after, before: beforeOf(after), total: total === true };
|
|
221
|
+
}
|
|
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
|
+
}
|
|
230
|
+
const DURABLE_WRITE = {
|
|
231
|
+
kind: 'rejected',
|
|
232
|
+
code: 'durable-write',
|
|
233
|
+
fields: {},
|
|
234
|
+
retryable: true,
|
|
235
|
+
};
|
|
236
|
+
function unwritten(draft) {
|
|
237
|
+
return draft.text === '';
|
|
238
|
+
}
|
|
239
|
+
function pending(draft) {
|
|
240
|
+
return {
|
|
241
|
+
...(draft.submitted === undefined ? {} : { submitted: draft.submitted }),
|
|
242
|
+
...(draft.moves === undefined ? {} : { moves: draft.moves }),
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
function empty(draft) {
|
|
246
|
+
return unwritten(draft) && draft.submitted === undefined && draft.moves === undefined;
|
|
247
|
+
}
|
|
248
|
+
function withSubmitted(draft, submitted) {
|
|
249
|
+
const { submitted: _held, ...unsent } = draft;
|
|
250
|
+
return submitted.length === 0 ? unsent : { ...unsent, submitted };
|
|
251
|
+
}
|
|
252
|
+
function prepended(draft, said) {
|
|
253
|
+
return { text: [said.text, draft.text].filter((one) => one !== '').join('\n') };
|
|
254
|
+
}
|
|
255
|
+
function returned(draft, submission, others) {
|
|
256
|
+
return withSubmitted({
|
|
257
|
+
conversation: draft.conversation,
|
|
258
|
+
...prepended(draft, submission),
|
|
259
|
+
revision: chatDraftRevision(),
|
|
260
|
+
}, others);
|
|
99
261
|
}
|
|
100
262
|
export function createConversationStore(deps) {
|
|
101
|
-
const messages = new Map();
|
|
102
|
-
const reads = new Map();
|
|
103
|
-
const drafts = new Map();
|
|
104
263
|
const sending = new Map();
|
|
105
264
|
const behind = new Set();
|
|
265
|
+
const catching = new Map();
|
|
106
266
|
const gaps = new Map();
|
|
107
267
|
const lives = new Map();
|
|
268
|
+
const unsent = new Map();
|
|
108
269
|
const views = new Map();
|
|
270
|
+
const heardNewest = new Map();
|
|
271
|
+
let relisting = null;
|
|
272
|
+
const creations = new Set();
|
|
273
|
+
const listWatchers = new Set();
|
|
274
|
+
const unlisted = new Set();
|
|
275
|
+
let listKnown = false;
|
|
276
|
+
let listOwed = false;
|
|
277
|
+
let listTelling = false;
|
|
278
|
+
let rowTimer = null;
|
|
279
|
+
const closing = new AbortController();
|
|
280
|
+
const agentWatchers = new Set();
|
|
281
|
+
const unnamed = new Set();
|
|
282
|
+
let agentsShown = null;
|
|
283
|
+
let listedAgents = null;
|
|
284
|
+
let agentsReading = false;
|
|
109
285
|
let replicated = new Set();
|
|
110
286
|
let handedReplicated;
|
|
111
287
|
function liveOf(conversation) {
|
|
112
288
|
return lives.get(conversation) ?? QUIET;
|
|
113
289
|
}
|
|
114
|
-
function viewOf(conversation,
|
|
115
|
-
const held = messages.get(conversation) ?? [];
|
|
116
|
-
const shown = limit === undefined ? held : held.slice(Math.max(0, held.length - limit));
|
|
117
|
-
const draft = drafts.get(conversation);
|
|
290
|
+
function viewOf(conversation, held, frame, draft, ended, marked) {
|
|
118
291
|
const facts = liveOf(conversation);
|
|
119
292
|
return {
|
|
120
293
|
conversation,
|
|
121
|
-
messages:
|
|
294
|
+
messages: held.map(({ identity, turn, text, message }) => ({
|
|
122
295
|
identity,
|
|
123
296
|
turn,
|
|
124
297
|
text,
|
|
125
|
-
message
|
|
298
|
+
message,
|
|
126
299
|
})),
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
plan: structuredClone(facts.plan?.plan ?? null),
|
|
300
|
+
...frame,
|
|
301
|
+
turn: structuredClone(facts.turn),
|
|
130
302
|
questions: structuredClone(facts.questions),
|
|
131
303
|
process: facts.process === null ? null : { ...facts.process },
|
|
304
|
+
processEnded: ended,
|
|
132
305
|
commands: structuredClone(facts.commands),
|
|
133
306
|
options: structuredClone(facts.options),
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
307
|
+
starting: facts.starting,
|
|
308
|
+
subagents: structuredClone(facts.subagents),
|
|
309
|
+
link: facts.link === null ? null : { ...facts.link },
|
|
310
|
+
deliveryUnknown: [...facts.deliveryUnknown],
|
|
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),
|
|
137
315
|
};
|
|
138
316
|
}
|
|
139
317
|
function emit(conversation) {
|
|
140
318
|
if (deps.closed())
|
|
141
319
|
return;
|
|
142
320
|
for (const view of views.values()) {
|
|
143
|
-
if (view.conversation !== conversation)
|
|
321
|
+
if (view.conversation !== conversation || view.window === null)
|
|
144
322
|
continue;
|
|
145
323
|
const ready = [...view.watchers].filter((watcher) => watcher.ready);
|
|
146
324
|
if (ready.length === 0)
|
|
147
325
|
continue;
|
|
148
|
-
const value = viewOf(conversation, view.
|
|
326
|
+
const value = viewOf(conversation, view.window, sharedFrame(view, view.window), view.draft, view.ended, view.marked);
|
|
149
327
|
for (const watcher of ready)
|
|
150
328
|
watcher.notify(value);
|
|
151
329
|
}
|
|
152
330
|
}
|
|
331
|
+
function sharedFrame(view, window) {
|
|
332
|
+
return frameOf(view.asked, window, view.more, view.total);
|
|
333
|
+
}
|
|
334
|
+
async function slide(local, conversation, arriving) {
|
|
335
|
+
let count;
|
|
336
|
+
let marked;
|
|
337
|
+
for (const view of views.values()) {
|
|
338
|
+
if (view.conversation !== conversation || view.window === null)
|
|
339
|
+
continue;
|
|
340
|
+
if (arriving === null) {
|
|
341
|
+
view.window = [];
|
|
342
|
+
view.marked = [];
|
|
343
|
+
view.more = false;
|
|
344
|
+
if (view.total !== null)
|
|
345
|
+
view.total = 0;
|
|
346
|
+
continue;
|
|
347
|
+
}
|
|
348
|
+
const joining = arriving.filter((message) => precedes(message, view.asked.before));
|
|
349
|
+
const next = windowOf(view.window, joining, view.asked.limit);
|
|
350
|
+
view.window = next.messages;
|
|
351
|
+
view.more ||= next.dropped;
|
|
352
|
+
if (arriving.length > 0) {
|
|
353
|
+
marked ??= await local.jobMarkers(conversation);
|
|
354
|
+
view.marked = marked;
|
|
355
|
+
}
|
|
356
|
+
if (view.total !== null && arriving.length > 0) {
|
|
357
|
+
count ??= await local.messageCount(conversation);
|
|
358
|
+
view.total = count;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
function stateOf(conversation) {
|
|
363
|
+
const facts = liveOf(conversation);
|
|
364
|
+
if (facts.questions.length > 0)
|
|
365
|
+
return 'asking';
|
|
366
|
+
return facts.turn === null ? 'idle' : 'working';
|
|
367
|
+
}
|
|
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) => ({
|
|
386
|
+
conversation: entry.conversation,
|
|
387
|
+
agent: { identity: entry.agent, name: entry.agentName },
|
|
388
|
+
name: entry.name,
|
|
389
|
+
created: entry.created,
|
|
390
|
+
finished: entry.finished,
|
|
391
|
+
outcome: entry.outcome,
|
|
392
|
+
waiting: entry.waiting,
|
|
393
|
+
...rowOf(entry.conversation),
|
|
394
|
+
...heldFacts(rows.get(entry.conversation), entry),
|
|
395
|
+
}));
|
|
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
|
+
}
|
|
512
|
+
function emitList() {
|
|
513
|
+
if (deps.closed())
|
|
514
|
+
return;
|
|
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
|
+
});
|
|
534
|
+
}
|
|
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 }))
|
|
545
|
+
.sort((left, right) => left.name === right.name
|
|
546
|
+
? left.identity.localeCompare(right.identity)
|
|
547
|
+
: left.name.localeCompare(right.name));
|
|
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
|
+
}
|
|
566
|
+
function emitAgents() {
|
|
567
|
+
if (deps.closed() || agentWatchers.size === 0)
|
|
568
|
+
return;
|
|
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
|
+
});
|
|
578
|
+
}
|
|
579
|
+
function setLive(conversation, next) {
|
|
580
|
+
const before = rowOf(conversation);
|
|
581
|
+
if (next === null)
|
|
582
|
+
lives.delete(conversation);
|
|
583
|
+
else
|
|
584
|
+
lives.set(conversation, next);
|
|
585
|
+
const after = rowOf(conversation);
|
|
586
|
+
if (after.state !== before.state || after.started !== before.started)
|
|
587
|
+
emitList();
|
|
588
|
+
else if (after.line !== before.line || after.question !== before.question)
|
|
589
|
+
tellSoon();
|
|
590
|
+
}
|
|
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);
|
|
596
|
+
}
|
|
153
597
|
function live(conversation, change) {
|
|
154
|
-
|
|
598
|
+
const owed = unsent.get(conversation);
|
|
599
|
+
for (const fact of FACTS.filter((one) => one in change))
|
|
600
|
+
owed?.delete(fact);
|
|
601
|
+
setLive(conversation, { ...liveOf(conversation), ...change });
|
|
155
602
|
emit(conversation);
|
|
156
603
|
}
|
|
604
|
+
function openingEnded() {
|
|
605
|
+
const owed = [...unsent];
|
|
606
|
+
unsent.clear();
|
|
607
|
+
for (const [conversation, facts] of owed) {
|
|
608
|
+
const held = lives.get(conversation);
|
|
609
|
+
if (held === undefined || facts.size === 0)
|
|
610
|
+
continue;
|
|
611
|
+
setLive(conversation, {
|
|
612
|
+
turn: held.turn,
|
|
613
|
+
questions: facts.has('questions') ? QUIET.questions : held.questions,
|
|
614
|
+
process: facts.has('process') ? QUIET.process : held.process,
|
|
615
|
+
commands: facts.has('commands') ? QUIET.commands : held.commands,
|
|
616
|
+
options: facts.has('options') ? QUIET.options : held.options,
|
|
617
|
+
starting: facts.has('starting') ? QUIET.starting : held.starting,
|
|
618
|
+
subagents: facts.has('subagents') ? QUIET.subagents : held.subagents,
|
|
619
|
+
link: facts.has('link') ? QUIET.link : held.link,
|
|
620
|
+
deliveryUnknown: facts.has('deliveryUnknown')
|
|
621
|
+
? QUIET.deliveryUnknown
|
|
622
|
+
: held.deliveryUnknown,
|
|
623
|
+
});
|
|
624
|
+
emit(conversation);
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
function relayed(conversation, turn, operation) {
|
|
628
|
+
const held = liveOf(conversation).turn;
|
|
629
|
+
const next = held?.turn === turn ? appliedTurn(held, operation) : startedTurn(turn, operation);
|
|
630
|
+
if (next !== null)
|
|
631
|
+
live(conversation, { turn: next });
|
|
632
|
+
}
|
|
157
633
|
function sealTurns(conversation, turns) {
|
|
158
634
|
const held = lives.get(conversation);
|
|
159
|
-
if (held
|
|
160
|
-
return false;
|
|
161
|
-
const growingText = held.growingText !== null && turns.has(held.growingText.turn) ? null : held.growingText;
|
|
162
|
-
const plan = held.plan !== null && turns.has(held.plan.turn) ? null : held.plan;
|
|
163
|
-
if (growingText === held.growingText && plan === held.plan)
|
|
635
|
+
if (held?.turn == null || !turns.has(held.turn.turn))
|
|
164
636
|
return false;
|
|
165
|
-
|
|
637
|
+
setLive(conversation, { ...held, turn: null });
|
|
166
638
|
return true;
|
|
167
639
|
}
|
|
168
640
|
function handReplicated() {
|
|
@@ -176,49 +648,96 @@ export function createConversationStore(deps) {
|
|
|
176
648
|
handedReplicated = named;
|
|
177
649
|
stream.replicate(held);
|
|
178
650
|
}
|
|
179
|
-
async function documented() {
|
|
180
|
-
const declaration = deps.cards();
|
|
181
|
-
if (declaration === undefined)
|
|
182
|
-
return [];
|
|
183
|
-
const { documents } = await deps.documents(declaration.locator);
|
|
184
|
-
return documents.filter((document) => typeof document.type === 'string');
|
|
185
|
-
}
|
|
186
|
-
function carded(documents) {
|
|
187
|
-
const declaration = deps.cards();
|
|
188
|
-
if (declaration === undefined)
|
|
189
|
-
return [];
|
|
190
|
-
const cards = [];
|
|
191
|
-
for (const card of documents) {
|
|
192
|
-
if (card.type !== declaration.type)
|
|
193
|
-
continue;
|
|
194
|
-
const identity = card.fields[declaration.identity];
|
|
195
|
-
if (typeof identity === 'string')
|
|
196
|
-
cards.push({ conversation: identity, card });
|
|
197
|
-
}
|
|
198
|
-
return cards;
|
|
199
|
-
}
|
|
200
651
|
async function remove(leaving) {
|
|
201
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
|
+
]);
|
|
202
659
|
await local.dropConversations(leaving);
|
|
203
|
-
const removed = [];
|
|
204
660
|
const changed = [];
|
|
205
661
|
for (const conversation of leaving) {
|
|
206
|
-
|
|
207
|
-
removed.push({ conversation, identity: message.identity });
|
|
208
|
-
}
|
|
209
|
-
if (messages.has(conversation) || drafts.has(conversation) || lives.has(conversation)) {
|
|
662
|
+
if (kept.has(conversation) || lives.has(conversation))
|
|
210
663
|
changed.push(conversation);
|
|
664
|
+
for (const view of views.values()) {
|
|
665
|
+
if (view.conversation === conversation)
|
|
666
|
+
view.draft = null;
|
|
211
667
|
}
|
|
212
|
-
messages.delete(conversation);
|
|
213
|
-
reads.delete(conversation);
|
|
214
|
-
drafts.delete(conversation);
|
|
215
668
|
behind.delete(conversation);
|
|
216
|
-
|
|
669
|
+
heardNewest.delete(conversation);
|
|
670
|
+
unsent.delete(conversation);
|
|
671
|
+
setLive(conversation, null);
|
|
672
|
+
await slide(local, conversation, null);
|
|
217
673
|
}
|
|
218
674
|
if (changed.length > 0)
|
|
219
|
-
deps.publish(conversationChangeSet(
|
|
675
|
+
deps.publish(conversationChangeSet(changed));
|
|
220
676
|
});
|
|
221
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
|
+
}
|
|
222
741
|
function gap(conversation) {
|
|
223
742
|
gaps.set(conversation, (gaps.get(conversation) ?? 0) + 1);
|
|
224
743
|
behind.add(conversation);
|
|
@@ -227,37 +746,32 @@ export function createConversationStore(deps) {
|
|
|
227
746
|
return deps.durably(async (local) => {
|
|
228
747
|
if (!replicated.has(conversation))
|
|
229
748
|
return;
|
|
230
|
-
const read =
|
|
749
|
+
const read = (await readsOf(local, [conversation])).get(conversation)?.read;
|
|
231
750
|
const advances = read !== undefined &&
|
|
232
751
|
!behind.has(conversation) &&
|
|
233
752
|
(read === null || message.position > read);
|
|
234
|
-
|
|
753
|
+
await sealed(local, conversation, new Set([message.turn]));
|
|
235
754
|
await local.keepMessages({
|
|
236
755
|
conversation,
|
|
237
756
|
messages: [message],
|
|
238
|
-
...(advances ? { read: message.position } : {}),
|
|
239
|
-
sealed,
|
|
757
|
+
...(advances ? { read: { at: message.position, partial: false } } : {}),
|
|
240
758
|
});
|
|
241
|
-
messages.set(conversation, merged(messages.get(conversation) ?? [], [message]));
|
|
242
|
-
if (advances)
|
|
243
|
-
reads.set(conversation, message.position);
|
|
244
|
-
if (sealed)
|
|
245
|
-
drafts.delete(conversation);
|
|
246
759
|
sealTurns(conversation, new Set([message.turn]));
|
|
247
|
-
|
|
760
|
+
await slide(local, conversation, [message]);
|
|
761
|
+
deps.publish(conversationChangeSet([conversation]));
|
|
762
|
+
tellList();
|
|
248
763
|
});
|
|
249
764
|
}
|
|
250
|
-
async function readHistories(conversations) {
|
|
765
|
+
async function readHistories(conversations, ended) {
|
|
251
766
|
const generations = new Map(conversations.map((one) => [one, gaps.get(one) ?? 0]));
|
|
252
|
-
const
|
|
253
|
-
|
|
254
|
-
const complete = new Set();
|
|
255
|
-
let asking = conversations.map((conversation) => ({
|
|
767
|
+
const held = await readsOf(await deps.local(), conversations);
|
|
768
|
+
let waiting = conversations.map((conversation) => ({
|
|
256
769
|
conversation,
|
|
257
|
-
after:
|
|
770
|
+
after: held.get(conversation)?.read ?? undefined,
|
|
258
771
|
}));
|
|
259
|
-
while (
|
|
260
|
-
const
|
|
772
|
+
while (waiting.length > 0) {
|
|
773
|
+
const asking = waiting.slice(0, HISTORY_CONVERSATIONS);
|
|
774
|
+
const result = await deps.history({
|
|
261
775
|
conversations: asking.map(({ conversation, after }) => ({
|
|
262
776
|
conversation,
|
|
263
777
|
...(after === undefined ? {} : { after }),
|
|
@@ -266,181 +780,375 @@ export function createConversationStore(deps) {
|
|
|
266
780
|
const pages = historyPages(result);
|
|
267
781
|
if (pages === null)
|
|
268
782
|
return result;
|
|
269
|
-
const asked = new
|
|
783
|
+
const asked = new Map(asking.map(({ conversation, after }) => [conversation, after]));
|
|
270
784
|
const following = [];
|
|
271
785
|
for (const page of pages) {
|
|
272
786
|
if (!asked.has(page.conversation))
|
|
273
787
|
continue;
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
...page.messages,
|
|
277
|
-
]);
|
|
278
|
-
if (page.next === undefined)
|
|
279
|
-
complete.add(page.conversation);
|
|
280
|
-
else
|
|
788
|
+
await keepPage(page.conversation, page.messages, asked.get(page.conversation) ?? null, page.next, generations.get(page.conversation) ?? 0);
|
|
789
|
+
if (page.next !== undefined) {
|
|
281
790
|
following.push({ conversation: page.conversation, after: page.next });
|
|
791
|
+
}
|
|
282
792
|
}
|
|
283
|
-
asking
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
await keepHistory(conversation, held, starts.get(conversation), generations.get(conversation) ?? 0);
|
|
793
|
+
for (const { conversation } of asking) {
|
|
794
|
+
if (!following.some((next) => next.conversation === conversation))
|
|
795
|
+
ended(conversation);
|
|
796
|
+
}
|
|
797
|
+
waiting = [...waiting.slice(HISTORY_CONVERSATIONS), ...following];
|
|
289
798
|
}
|
|
290
799
|
return null;
|
|
291
800
|
}
|
|
292
|
-
function
|
|
801
|
+
function caughtUp(conversations) {
|
|
802
|
+
const ends = new Map();
|
|
803
|
+
for (const conversation of conversations) {
|
|
804
|
+
const held = new Promise((end) => ends.set(conversation, end));
|
|
805
|
+
catching.set(conversation, held);
|
|
806
|
+
void held.then(() => {
|
|
807
|
+
if (catching.get(conversation) === held)
|
|
808
|
+
catching.delete(conversation);
|
|
809
|
+
});
|
|
810
|
+
}
|
|
811
|
+
const ended = (conversation) => ends.get(conversation)?.();
|
|
812
|
+
const read = readHistories(conversations, ended);
|
|
813
|
+
const endAll = () => {
|
|
814
|
+
for (const conversation of conversations)
|
|
815
|
+
ended(conversation);
|
|
816
|
+
};
|
|
817
|
+
read.then(endAll, endAll);
|
|
818
|
+
return read;
|
|
819
|
+
}
|
|
820
|
+
function keepPage(conversation, arriving, from, next, generation) {
|
|
293
821
|
return deps.durably(async (local) => {
|
|
294
822
|
if (!replicated.has(conversation))
|
|
295
823
|
return;
|
|
296
|
-
const
|
|
297
|
-
const
|
|
298
|
-
const
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
if (sealed)
|
|
303
|
-
drafts.delete(conversation);
|
|
824
|
+
const whole = next === undefined;
|
|
825
|
+
const cursor = next ?? newestOf(arriving, from);
|
|
826
|
+
const held = (await readsOf(local, [conversation])).get(conversation);
|
|
827
|
+
const partial = !whole && (held?.partial === true || held?.read === undefined);
|
|
828
|
+
const sealing = await sealed(local, conversation, new Set(arriving.map((message) => message.turn)));
|
|
829
|
+
await local.keepMessages({ conversation, messages: arriving, read: { at: cursor, partial } });
|
|
304
830
|
sealTurns(conversation, new Set(arriving.map((message) => message.turn)));
|
|
305
|
-
if ((gaps.get(conversation) ?? 0) === generation)
|
|
831
|
+
if (whole && (gaps.get(conversation) ?? 0) === generation)
|
|
306
832
|
behind.delete(conversation);
|
|
307
|
-
|
|
308
|
-
|
|
833
|
+
await slide(local, conversation, arriving);
|
|
834
|
+
if (arriving.length > 0 || sealing || whole) {
|
|
835
|
+
deps.publish(conversationChangeSet([conversation]));
|
|
309
836
|
}
|
|
837
|
+
if (arriving.length > 0)
|
|
838
|
+
tellSoon();
|
|
839
|
+
});
|
|
840
|
+
}
|
|
841
|
+
function heard(event) {
|
|
842
|
+
const { conversation } = event;
|
|
843
|
+
if (event.kind === 'gap') {
|
|
844
|
+
if (replicated.has(conversation))
|
|
845
|
+
gap(conversation);
|
|
846
|
+
if (lives.has(conversation))
|
|
847
|
+
unsent.set(conversation, new Set(FACTS));
|
|
848
|
+
return;
|
|
849
|
+
}
|
|
850
|
+
if (event.kind === 'turn') {
|
|
851
|
+
relayed(conversation, event.turn, event.operation);
|
|
852
|
+
return;
|
|
853
|
+
}
|
|
854
|
+
if (event.kind === 'questions') {
|
|
855
|
+
live(conversation, { questions: structuredClone(event.questions) });
|
|
856
|
+
return;
|
|
857
|
+
}
|
|
858
|
+
if (event.kind === 'process') {
|
|
859
|
+
live(conversation, { process: { running: event.running } });
|
|
860
|
+
return;
|
|
861
|
+
}
|
|
862
|
+
if (event.kind === 'commands') {
|
|
863
|
+
live(conversation, { commands: structuredClone(event.commands) });
|
|
864
|
+
return;
|
|
865
|
+
}
|
|
866
|
+
if (event.kind === 'options') {
|
|
867
|
+
live(conversation, { options: structuredClone(event.options) });
|
|
868
|
+
return;
|
|
869
|
+
}
|
|
870
|
+
if (event.kind === 'starting') {
|
|
871
|
+
live(conversation, { starting: event.step });
|
|
872
|
+
return;
|
|
873
|
+
}
|
|
874
|
+
if (event.kind === 'subagents') {
|
|
875
|
+
live(conversation, { subagents: structuredClone(event.subagents) });
|
|
876
|
+
return;
|
|
877
|
+
}
|
|
878
|
+
if (event.kind === 'link') {
|
|
879
|
+
live(conversation, { link: { connected: event.connected } });
|
|
880
|
+
return;
|
|
881
|
+
}
|
|
882
|
+
if (event.kind === 'deliveries') {
|
|
883
|
+
live(conversation, { deliveryUnknown: [...event.unknown] });
|
|
884
|
+
return;
|
|
885
|
+
}
|
|
886
|
+
const arriving = acceptedMessage(event.message);
|
|
887
|
+
if (arriving === null)
|
|
888
|
+
return;
|
|
889
|
+
noted(conversation, arriving);
|
|
890
|
+
if (replicated.has(conversation)) {
|
|
891
|
+
if (deps.full()) {
|
|
892
|
+
gap(conversation);
|
|
893
|
+
return;
|
|
894
|
+
}
|
|
895
|
+
void keep(conversation, arriving).catch((error) => {
|
|
896
|
+
if (refusedForSpace(error))
|
|
897
|
+
gap(conversation);
|
|
898
|
+
});
|
|
899
|
+
return;
|
|
900
|
+
}
|
|
901
|
+
if (sealTurns(conversation, new Set([arriving.turn])))
|
|
902
|
+
emit(conversation);
|
|
903
|
+
}
|
|
904
|
+
async function kept(local, conversation, following) {
|
|
905
|
+
const shown = following === null || empty(following) ? null : following;
|
|
906
|
+
if (shown === null)
|
|
907
|
+
await local.dropChatDraft(conversation);
|
|
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;
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
async function draftAt(local, conversation) {
|
|
919
|
+
return (await local.chatDraft(conversation)) ?? undefined;
|
|
920
|
+
}
|
|
921
|
+
async function sealed(local, conversation, turns) {
|
|
922
|
+
const held = await draftAt(local, conversation);
|
|
923
|
+
const submitted = held?.submitted ?? [];
|
|
924
|
+
const left = submitted.filter((one) => one.turn === null || !turns.has(one.turn));
|
|
925
|
+
if (held === undefined || left.length === submitted.length)
|
|
926
|
+
return false;
|
|
927
|
+
await kept(local, conversation, withSubmitted(held, left));
|
|
928
|
+
return true;
|
|
929
|
+
}
|
|
930
|
+
function moved(agent, operation, to) {
|
|
931
|
+
const from = newChat(agent);
|
|
932
|
+
return deps.durably(async (local) => {
|
|
933
|
+
const held = await draftAt(local, from);
|
|
934
|
+
const earlier = held?.moves ?? [];
|
|
935
|
+
if (earlier.includes(operation))
|
|
936
|
+
return;
|
|
937
|
+
const blank = {
|
|
938
|
+
conversation: from,
|
|
939
|
+
text: '',
|
|
940
|
+
revision: chatDraftRevision(),
|
|
941
|
+
};
|
|
942
|
+
const { moves: _answered, ...unsent } = held ?? blank;
|
|
943
|
+
const taken = (await draftAt(local, to)) !== undefined || empty(unsent)
|
|
944
|
+
? []
|
|
945
|
+
: [{ ...unsent, conversation: to }];
|
|
946
|
+
const left = { ...(taken.length === 0 ? unsent : blank), moves: [...earlier, operation] };
|
|
947
|
+
await local.keepChatDrafts([left, ...taken]);
|
|
948
|
+
shownDraft(from, left);
|
|
949
|
+
for (const one of taken)
|
|
950
|
+
shownDraft(to, one);
|
|
951
|
+
deps.publish(conversationChangeSet([from, to]));
|
|
310
952
|
});
|
|
311
953
|
}
|
|
312
954
|
function withDraft(conversation, next) {
|
|
313
955
|
return deps.durably(async (local) => {
|
|
314
|
-
const following = next(
|
|
956
|
+
const following = await next(await draftAt(local, conversation), local);
|
|
315
957
|
if (following === undefined)
|
|
316
958
|
return;
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
959
|
+
await kept(local, conversation, following);
|
|
960
|
+
deps.publish(conversationChangeSet([conversation]));
|
|
961
|
+
});
|
|
962
|
+
}
|
|
963
|
+
async function submitted(conversation, operation, draft) {
|
|
964
|
+
let submission;
|
|
965
|
+
await withDraft(conversation, (current) => {
|
|
966
|
+
submission = current?.submitted?.find((one) => one.operation === operation);
|
|
967
|
+
if (submission !== undefined)
|
|
968
|
+
return undefined;
|
|
969
|
+
const said = draft ?? current;
|
|
970
|
+
submission = { operation, text: said?.text ?? '', turn: null };
|
|
971
|
+
return {
|
|
972
|
+
conversation,
|
|
973
|
+
...(draft === undefined || current === undefined
|
|
974
|
+
? { text: '', revision: chatDraftRevision() }
|
|
975
|
+
: { text: current.text, revision: current.revision }),
|
|
976
|
+
submitted: [...(current?.submitted ?? []), submission],
|
|
977
|
+
};
|
|
326
978
|
});
|
|
979
|
+
return submission;
|
|
980
|
+
}
|
|
981
|
+
async function dispatched(conversation, submission) {
|
|
982
|
+
const dispatch = { answered: false };
|
|
983
|
+
sending.set(submission.operation, dispatch);
|
|
984
|
+
try {
|
|
985
|
+
const result = await invoke('conversations/send', conversation, {
|
|
986
|
+
operation_id: submission.operation,
|
|
987
|
+
text: submission.text,
|
|
988
|
+
});
|
|
989
|
+
dispatch.answered = result.kind !== 'transport-uncertainty';
|
|
990
|
+
if (result.kind === 'revoked-authorization')
|
|
991
|
+
return result;
|
|
992
|
+
if (result.kind !== 'action' && result.retryable) {
|
|
993
|
+
deps.unanswered();
|
|
994
|
+
return result;
|
|
995
|
+
}
|
|
996
|
+
const turn = answered(result, 'message_id');
|
|
997
|
+
await withDraft(conversation, async (current, local) => {
|
|
998
|
+
const held = current?.submitted?.find((one) => one.operation === submission.operation);
|
|
999
|
+
if (current === undefined || held === undefined || held.turn !== null)
|
|
1000
|
+
return undefined;
|
|
1001
|
+
const others = current.submitted.filter((one) => one !== held);
|
|
1002
|
+
if (result.kind !== 'action')
|
|
1003
|
+
return returned(current, held, others);
|
|
1004
|
+
const arrived = turn !== null && (await local.turnArrived(conversation, turn));
|
|
1005
|
+
return withSubmitted(current, turn === null || arrived
|
|
1006
|
+
? others
|
|
1007
|
+
: current.submitted.map((one) => (one === held ? { ...held, turn } : one)));
|
|
1008
|
+
});
|
|
1009
|
+
return result;
|
|
1010
|
+
}
|
|
1011
|
+
finally {
|
|
1012
|
+
if (sending.get(submission.operation) === dispatch)
|
|
1013
|
+
sending.delete(submission.operation);
|
|
1014
|
+
}
|
|
327
1015
|
}
|
|
328
|
-
function invoke(name, conversation, extra = {}) {
|
|
329
|
-
return deps.call(name, { conversation, ...extra });
|
|
1016
|
+
function invoke(name, conversation, extra = { operation_id: changeBatchIdentity() }) {
|
|
1017
|
+
return deps.call(name, { conversation_id: conversation, ...extra });
|
|
330
1018
|
}
|
|
331
1019
|
const surface = {
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
})
|
|
342
|
-
entries.sort((left, right) => (left.conversation < right.conversation ? -1 : 1));
|
|
343
|
-
return entries;
|
|
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
|
+
});
|
|
344
1030
|
},
|
|
345
1031
|
async read(conversation, options = {}) {
|
|
346
|
-
const
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
1032
|
+
const { text, ...windowed } = options;
|
|
1033
|
+
const asked = askedOf(windowed);
|
|
1034
|
+
const phrase = phraseOf(text);
|
|
1035
|
+
return deps.durably(async (local) => {
|
|
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));
|
|
1040
|
+
});
|
|
351
1041
|
},
|
|
352
|
-
async
|
|
353
|
-
const
|
|
354
|
-
const
|
|
355
|
-
if (
|
|
356
|
-
|
|
357
|
-
|
|
1042
|
+
async create(input) {
|
|
1043
|
+
const result = await deps.call('conversations/create', input);
|
|
1044
|
+
const conversation = answered(result, 'conversation_id');
|
|
1045
|
+
if (conversation === null)
|
|
1046
|
+
return result;
|
|
1047
|
+
const listed = (await heldList()) ?? [];
|
|
1048
|
+
if (!listed.some((entry) => entry.conversation === conversation)) {
|
|
1049
|
+
creations.add(conversation);
|
|
358
1050
|
}
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
const
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
if (current !== draft)
|
|
373
|
-
return undefined;
|
|
374
|
-
const sealed = (messages.get(conversation) ?? []).some((message) => message.turn === turn);
|
|
375
|
-
return sealed ? null : { ...draft, turn };
|
|
1051
|
+
const creation = creationOf(input);
|
|
1052
|
+
if (creation !== null)
|
|
1053
|
+
await moved(creation.agent, creation.operation, conversation);
|
|
1054
|
+
return result;
|
|
1055
|
+
},
|
|
1056
|
+
async send(conversation, operation = changeBatchIdentity(), draft) {
|
|
1057
|
+
if (deps.full())
|
|
1058
|
+
return DURABLE_WRITE;
|
|
1059
|
+
if (deps.online()) {
|
|
1060
|
+
const held = await submitted(conversation, operation, draft).catch((error) => {
|
|
1061
|
+
if (refusedForSpace(error))
|
|
1062
|
+
return null;
|
|
1063
|
+
throw error;
|
|
376
1064
|
});
|
|
377
|
-
|
|
1065
|
+
if (held === null)
|
|
1066
|
+
return DURABLE_WRITE;
|
|
1067
|
+
return dispatched(conversation, held);
|
|
378
1068
|
}
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
deps.publish(conversationChangeSet([], [conversation]));
|
|
384
|
-
}
|
|
385
|
-
}
|
|
1069
|
+
if (draft !== undefined) {
|
|
1070
|
+
await withDraft(conversation, (current) => current?.submitted?.some((one) => one.operation === operation) === true
|
|
1071
|
+
? undefined
|
|
1072
|
+
: returned(current ?? { conversation, text: '', revision: '' }, { operation, ...draft, turn: null }, current?.submitted ?? []));
|
|
386
1073
|
}
|
|
1074
|
+
return OFFLINE;
|
|
387
1075
|
},
|
|
388
1076
|
answer(conversation, answer) {
|
|
389
|
-
return invoke('
|
|
1077
|
+
return invoke('conversations/interaction/answer', conversation, {
|
|
1078
|
+
operation_id: changeBatchIdentity(),
|
|
1079
|
+
...answer,
|
|
1080
|
+
});
|
|
390
1081
|
},
|
|
391
1082
|
interrupt(conversation) {
|
|
392
|
-
return invoke('
|
|
1083
|
+
return invoke('conversations/interrupt', conversation);
|
|
393
1084
|
},
|
|
394
1085
|
kill(conversation) {
|
|
395
|
-
return invoke('
|
|
396
|
-
},
|
|
397
|
-
async cancel(conversation, turn) {
|
|
398
|
-
const result = await invoke('conversation.cancel', conversation, { turn });
|
|
399
|
-
if (result.kind !== 'action')
|
|
400
|
-
return result;
|
|
401
|
-
await withDraft(conversation, (current) => current === undefined || current.turn !== turn ? undefined : { ...current, turn: null });
|
|
402
|
-
return result;
|
|
1086
|
+
return invoke('conversations/kill', conversation);
|
|
403
1087
|
},
|
|
404
1088
|
delete(conversation) {
|
|
405
|
-
return invoke('
|
|
1089
|
+
return invoke('conversations/delete', conversation);
|
|
406
1090
|
},
|
|
407
1091
|
};
|
|
408
1092
|
const chatDraft = {
|
|
409
|
-
write(conversation, draft) {
|
|
410
|
-
return withDraft(conversation, () => ({
|
|
1093
|
+
write(conversation, draft, revision = chatDraftRevision()) {
|
|
1094
|
+
return withDraft(conversation, (current) => ({
|
|
411
1095
|
conversation,
|
|
412
1096
|
text: draft.text,
|
|
413
|
-
|
|
414
|
-
|
|
1097
|
+
revision,
|
|
1098
|
+
...(current === undefined ? {} : pending(current)),
|
|
1099
|
+
}));
|
|
1100
|
+
},
|
|
1101
|
+
prepend(conversation, draft) {
|
|
1102
|
+
return withDraft(conversation, (current) => ({
|
|
1103
|
+
conversation,
|
|
1104
|
+
...(current === undefined ? { text: draft.text } : prepended(current, draft)),
|
|
1105
|
+
revision: chatDraftRevision(),
|
|
1106
|
+
...(current === undefined ? {} : pending(current)),
|
|
415
1107
|
}));
|
|
416
1108
|
},
|
|
417
1109
|
discard(conversation) {
|
|
418
|
-
return withDraft(conversation, (current) =>
|
|
1110
|
+
return withDraft(conversation, (current) => current === undefined
|
|
1111
|
+
? undefined
|
|
1112
|
+
: { conversation, text: '', revision: chatDraftRevision(), ...pending(current) });
|
|
419
1113
|
},
|
|
420
1114
|
};
|
|
421
1115
|
return {
|
|
422
1116
|
surface,
|
|
423
1117
|
chatDraft,
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
reads.set(entry.conversation, entry.read);
|
|
431
|
-
}
|
|
432
|
-
drafts.clear();
|
|
433
|
-
for (const record of stored)
|
|
434
|
-
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();
|
|
435
1124
|
},
|
|
436
1125
|
async reconcile() {
|
|
437
|
-
|
|
1126
|
+
if (!listKnown)
|
|
1127
|
+
return;
|
|
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]));
|
|
438
1138
|
const leaving = [
|
|
439
|
-
...new Set([
|
|
440
|
-
|
|
1139
|
+
...new Set([
|
|
1140
|
+
...replicated,
|
|
1141
|
+
...reads.keys(),
|
|
1142
|
+
...kept.map(({ conversation }) => conversation),
|
|
1143
|
+
]),
|
|
1144
|
+
].filter((conversation) => !named.has(conversation) &&
|
|
1145
|
+
!creations.has(conversation) &&
|
|
1146
|
+
(!conversation.startsWith(NEW_CHAT) || retiredChat(conversation, list)));
|
|
441
1147
|
for (const conversation of named) {
|
|
442
|
-
if (!replicated.has(conversation)
|
|
1148
|
+
if (!replicated.has(conversation) &&
|
|
1149
|
+
missed(conversation, reads.get(conversation), entries.get(conversation))) {
|
|
443
1150
|
behind.add(conversation);
|
|
1151
|
+
}
|
|
444
1152
|
}
|
|
445
1153
|
replicated = named;
|
|
446
1154
|
handReplicated();
|
|
@@ -448,45 +1156,131 @@ export function createConversationStore(deps) {
|
|
|
448
1156
|
await remove(leaving);
|
|
449
1157
|
},
|
|
450
1158
|
async catchUp(progress) {
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
1159
|
+
if (deps.full()) {
|
|
1160
|
+
if (behind.size > 0)
|
|
1161
|
+
progress.complete = false;
|
|
1162
|
+
return;
|
|
1163
|
+
}
|
|
1164
|
+
const owed = (conversation) => behind.has(conversation) && replicated.has(conversation);
|
|
1165
|
+
const read = async (conversations) => {
|
|
1166
|
+
if (conversations.length === 0 || deps.closed())
|
|
1167
|
+
return;
|
|
1168
|
+
const failed = await caughtUp(conversations);
|
|
454
1169
|
if (failed?.kind === 'transport-uncertainty')
|
|
455
1170
|
progress.reachable = false;
|
|
456
|
-
}
|
|
1171
|
+
};
|
|
1172
|
+
const asking = [...behind].filter(owed).sort();
|
|
1173
|
+
const awaited = asking.filter((conversation) => catching.has(conversation));
|
|
1174
|
+
await Promise.all([
|
|
1175
|
+
read(asking.filter((conversation) => !catching.has(conversation))),
|
|
1176
|
+
Promise.all(awaited.map((conversation) => catching.get(conversation))).then(() => read(awaited.filter((conversation) => owed(conversation) && !catching.has(conversation)))),
|
|
1177
|
+
]);
|
|
457
1178
|
if (behind.size > 0)
|
|
458
1179
|
progress.complete = false;
|
|
459
1180
|
},
|
|
1181
|
+
async resend(progress) {
|
|
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;
|
|
1189
|
+
if (deps.closed())
|
|
1190
|
+
return;
|
|
1191
|
+
tried.add(submission.operation);
|
|
1192
|
+
const result = await dispatched(conversation, submission);
|
|
1193
|
+
if (result.kind === 'revoked-authorization')
|
|
1194
|
+
return;
|
|
1195
|
+
if (result.kind === 'transport-uncertainty') {
|
|
1196
|
+
progress.reachable = false;
|
|
1197
|
+
return;
|
|
1198
|
+
}
|
|
1199
|
+
if (result.kind !== 'action' && result.retryable) {
|
|
1200
|
+
progress.complete = false;
|
|
1201
|
+
return;
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
}
|
|
1205
|
+
},
|
|
460
1206
|
current() {
|
|
461
1207
|
return behind.size === 0;
|
|
462
1208
|
},
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
1209
|
+
known() {
|
|
1210
|
+
return listKnown;
|
|
1211
|
+
},
|
|
1212
|
+
reopened() {
|
|
1213
|
+
relisting = null;
|
|
1214
|
+
for (const [operation, dispatch] of sending)
|
|
1215
|
+
if (!dispatch.answered)
|
|
1216
|
+
sending.delete(operation);
|
|
1217
|
+
},
|
|
1218
|
+
lost() {
|
|
1219
|
+
catching.clear();
|
|
466
1220
|
},
|
|
467
1221
|
watch(conversation, options, deliver) {
|
|
468
|
-
const
|
|
469
|
-
const key = JSON.stringify([conversation, limit ?? null]);
|
|
470
|
-
const view = views.get(key) ?? {
|
|
1222
|
+
const asked = askedOf(options);
|
|
1223
|
+
const key = JSON.stringify([conversation, asked.limit, asked.after ?? null, asked.total]);
|
|
1224
|
+
const view = views.get(key) ?? {
|
|
1225
|
+
conversation,
|
|
1226
|
+
asked,
|
|
1227
|
+
watchers: new Set(),
|
|
1228
|
+
window: null,
|
|
1229
|
+
draft: null,
|
|
1230
|
+
ended: null,
|
|
1231
|
+
marked: [],
|
|
1232
|
+
more: false,
|
|
1233
|
+
total: null,
|
|
1234
|
+
};
|
|
471
1235
|
views.set(key, view);
|
|
472
1236
|
const watcher = { notify: deliver, ready: false };
|
|
473
1237
|
view.watchers.add(watcher);
|
|
474
|
-
|
|
475
|
-
|
|
1238
|
+
const reading = new AbortController();
|
|
1239
|
+
void untilAnswered(() => deps.durably(async (local) => {
|
|
1240
|
+
if (view.window !== null)
|
|
1241
|
+
return;
|
|
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);
|
|
1245
|
+
view.window = messages;
|
|
1246
|
+
view.marked = await local.jobMarkers(conversation);
|
|
1247
|
+
view.more = more;
|
|
1248
|
+
view.total = asked.total ? await local.messageCount(conversation) : null;
|
|
1249
|
+
}), reading.signal)
|
|
476
1250
|
.then(() => {
|
|
477
1251
|
if (deps.closed() || !view.watchers.has(watcher))
|
|
478
1252
|
return;
|
|
479
1253
|
watcher.ready = true;
|
|
480
|
-
|
|
1254
|
+
const window = view.window ?? [];
|
|
1255
|
+
watcher.notify(viewOf(conversation, window, sharedFrame(view, window), view.draft, view.ended, view.marked));
|
|
481
1256
|
})
|
|
482
1257
|
.catch(() => { });
|
|
483
1258
|
return () => {
|
|
1259
|
+
reading.abort();
|
|
484
1260
|
if (!view.watchers.delete(watcher))
|
|
485
1261
|
return;
|
|
486
1262
|
if (view.watchers.size === 0)
|
|
487
1263
|
views.delete(key);
|
|
488
1264
|
};
|
|
489
1265
|
},
|
|
1266
|
+
watchList(deliver) {
|
|
1267
|
+
unlisted.add(deliver);
|
|
1268
|
+
learned();
|
|
1269
|
+
return () => {
|
|
1270
|
+
unlisted.delete(deliver);
|
|
1271
|
+
listWatchers.delete(deliver);
|
|
1272
|
+
};
|
|
1273
|
+
},
|
|
1274
|
+
watchAgents(deliver) {
|
|
1275
|
+
unnamed.add(deliver);
|
|
1276
|
+
learned();
|
|
1277
|
+
readAgents();
|
|
1278
|
+
return () => {
|
|
1279
|
+
unnamed.delete(deliver);
|
|
1280
|
+
agentWatchers.delete(deliver);
|
|
1281
|
+
};
|
|
1282
|
+
},
|
|
1283
|
+
learned,
|
|
490
1284
|
changed(set) {
|
|
491
1285
|
for (const conversation of set.conversations)
|
|
492
1286
|
emit(conversation);
|
|
@@ -494,64 +1288,49 @@ export function createConversationStore(deps) {
|
|
|
494
1288
|
receive(event) {
|
|
495
1289
|
if (deps.closed())
|
|
496
1290
|
return;
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
if (event.kind === 'working-note') {
|
|
514
|
-
live(conversation, {
|
|
515
|
-
workingNote: event.note === null ? null : { noteId: event.note.noteId, text: event.note.text },
|
|
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 });
|
|
1297
|
+
}
|
|
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 }),
|
|
516
1307
|
});
|
|
517
1308
|
return;
|
|
518
1309
|
}
|
|
519
|
-
if (event.kind === '
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
}
|
|
523
|
-
if (event.kind === 'process') {
|
|
524
|
-
live(conversation, { process: { running: event.running } });
|
|
1310
|
+
if (event.kind === 'listed') {
|
|
1311
|
+
relisting?.set(event.entry.conversation, { ...event.entry });
|
|
1312
|
+
listChanged({ replacing: false, set: [event.entry], gone: null });
|
|
525
1313
|
return;
|
|
526
1314
|
}
|
|
527
|
-
if (event.kind === '
|
|
528
|
-
|
|
1315
|
+
if (event.kind === 'unlisted') {
|
|
1316
|
+
relisting?.delete(event.conversation);
|
|
1317
|
+
listChanged({ replacing: false, set: [], gone: event.conversation });
|
|
529
1318
|
return;
|
|
530
1319
|
}
|
|
531
|
-
|
|
532
|
-
live(conversation, { options: structuredClone(event.options) });
|
|
533
|
-
return;
|
|
534
|
-
}
|
|
535
|
-
const arriving = acceptedMessage(event.message);
|
|
536
|
-
if (arriving === null)
|
|
537
|
-
return;
|
|
538
|
-
if (replicated.has(conversation)) {
|
|
539
|
-
void keep(conversation, arriving).catch(() => { });
|
|
540
|
-
return;
|
|
541
|
-
}
|
|
542
|
-
if (sealTurns(conversation, new Set([arriving.turn])))
|
|
543
|
-
emit(conversation);
|
|
544
|
-
},
|
|
545
|
-
messages() {
|
|
546
|
-
return [...messages].flatMap(([conversation, held]) => held.map(({ identity, text }) => ({ conversation, identity, text })));
|
|
547
|
-
},
|
|
548
|
-
message(conversation, identity) {
|
|
549
|
-
const held = messages.get(conversation)?.find((message) => message.identity === identity);
|
|
550
|
-
return held === undefined ? null : { conversation, identity, text: held.text };
|
|
1320
|
+
heard(event);
|
|
551
1321
|
},
|
|
552
1322
|
close() {
|
|
1323
|
+
closing.abort();
|
|
1324
|
+
if (rowTimer !== null)
|
|
1325
|
+
clearTimeout(rowTimer);
|
|
1326
|
+
rowTimer = null;
|
|
553
1327
|
views.clear();
|
|
554
1328
|
lives.clear();
|
|
1329
|
+
unsent.clear();
|
|
1330
|
+
listWatchers.clear();
|
|
1331
|
+
unlisted.clear();
|
|
1332
|
+
agentWatchers.clear();
|
|
1333
|
+
unnamed.clear();
|
|
555
1334
|
},
|
|
556
1335
|
};
|
|
557
1336
|
}
|