@agentshouse/mdruntime 0.1.0-c8f3163f32f93 → 0.1.0-c907ac1fbed44
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +289 -276
- package/README.md +16 -14
- package/dist/adapter.d.ts +76 -35
- package/dist/adapter.d.ts.map +1 -1
- package/dist/attached-model.d.ts +4 -1
- package/dist/attached-model.d.ts.map +1 -1
- package/dist/attached-model.js +13 -3
- package/dist/binary.d.ts +0 -5
- package/dist/binary.d.ts.map +1 -1
- package/dist/binary.js +1 -1
- package/dist/bootstrap.d.ts +26 -7
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +148 -78
- package/dist/chain.d.ts +11 -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 -63
- package/dist/change.d.ts +31 -1
- package/dist/change.d.ts.map +1 -1
- package/dist/change.js +10 -1
- package/dist/client.d.ts +23 -11
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +48 -8
- package/dist/collection.d.ts +7 -6
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +31 -77
- package/dist/compile.d.ts +16 -0
- package/dist/compile.d.ts.map +1 -0
- package/dist/compile.js +721 -0
- package/dist/connection.d.ts +41 -15
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +3058 -638
- package/dist/conversation.d.ts +26 -7
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +441 -161
- 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 +5 -1
- package/dist/durability.d.ts.map +1 -1
- package/dist/durability.js +5 -0
- package/dist/entry.d.ts +5 -3
- package/dist/entry.d.ts.map +1 -1
- package/dist/entry.js +18 -23
- package/dist/file.d.ts +2 -10
- package/dist/file.d.ts.map +1 -1
- package/dist/file.js +3 -120
- package/dist/fulltext.d.ts +27 -0
- package/dist/fulltext.d.ts.map +1 -0
- package/dist/fulltext.js +368 -0
- package/dist/host.d.ts +13 -2
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +120 -13
- package/dist/identity.d.ts +1 -0
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +3 -0
- package/dist/index.d.ts +15 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -0
- package/dist/live.d.ts +22 -0
- package/dist/live.d.ts.map +1 -0
- package/dist/live.js +123 -0
- package/dist/memory.d.ts +8 -2
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +43 -20
- package/dist/messages.d.ts +28 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/messages.js +90 -0
- package/dist/model.d.ts +4 -2
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +10 -1
- 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 +19 -2
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +113 -12
- 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 +34 -4
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +25 -0
- package/dist/overlay.d.ts +3 -4
- package/dist/overlay.d.ts.map +1 -1
- package/dist/overlay.js +99 -189
- package/dist/query.d.ts +60 -4
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +48 -7
- package/dist/reach.d.ts +11 -6
- package/dist/reach.d.ts.map +1 -1
- package/dist/reach.js +181 -61
- package/dist/react.d.ts +8 -6
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +7 -17
- package/dist/readiness.d.ts +64 -0
- package/dist/readiness.d.ts.map +1 -0
- package/dist/readiness.js +33 -0
- package/dist/reads.d.ts +11 -0
- package/dist/reads.d.ts.map +1 -0
- package/dist/reads.js +100 -0
- package/dist/recovery.d.ts +25 -0
- package/dist/recovery.d.ts.map +1 -0
- package/dist/recovery.js +198 -0
- package/dist/refresh.d.ts +16 -15
- package/dist/refresh.d.ts.map +1 -1
- package/dist/refresh.js +112 -44
- package/dist/replica.d.ts +57 -33
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +295 -149
- package/dist/room-draft.d.ts +17 -2
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +65 -25
- package/dist/rows.d.ts +28 -37
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +113 -72
- package/dist/save.d.ts +51 -9
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +526 -165
- package/dist/search.d.ts +39 -28
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +37 -105
- 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 +16 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +431 -0
- package/dist/status.d.ts +4 -0
- package/dist/status.d.ts.map +1 -1
- package/dist/store.d.ts +103 -78
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +336 -556
- package/dist/stored.d.ts +145 -0
- package/dist/stored.d.ts.map +1 -0
- package/dist/stored.js +567 -0
- package/dist/tables.d.ts +354 -0
- package/dist/tables.d.ts.map +1 -0
- package/dist/tables.js +624 -0
- 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 +30 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +162 -0
- package/dist/windows.d.ts +22 -0
- package/dist/windows.d.ts.map +1 -0
- package/dist/windows.js +89 -0
- package/dist/wire.d.ts +164 -32
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +55 -31
- package/dist/words.d.ts +19 -0
- package/dist/words.d.ts.map +1 -0
- package/dist/words.js +150 -0
- package/package.json +14 -15
- package/dist/chat/acts.d.ts +0 -33
- package/dist/chat/acts.d.ts.map +0 -1
- package/dist/chat/acts.js +0 -36
- package/dist/chat/context.d.ts +0 -3
- package/dist/chat/context.d.ts.map +0 -1
- package/dist/chat/context.js +0 -10
- package/dist/chat/input.d.ts +0 -9
- package/dist/chat/input.d.ts.map +0 -1
- package/dist/chat/input.js +0 -99
- package/dist/chat/kit.d.ts +0 -60
- package/dist/chat/kit.d.ts.map +0 -1
- package/dist/chat/kit.js +0 -123
- package/dist/chat/lasting.d.ts +0 -6
- package/dist/chat/lasting.d.ts.map +0 -1
- package/dist/chat/lasting.js +0 -19
- package/dist/chat/list.d.ts +0 -26
- package/dist/chat/list.d.ts.map +0 -1
- package/dist/chat/list.js +0 -62
- package/dist/chat/markdown.d.ts +0 -20
- package/dist/chat/markdown.d.ts.map +0 -1
- package/dist/chat/markdown.js +0 -188
- package/dist/chat/message.d.ts +0 -6
- package/dist/chat/message.d.ts.map +0 -1
- package/dist/chat/message.js +0 -192
- package/dist/chat/messages.d.ts +0 -21
- package/dist/chat/messages.d.ts.map +0 -1
- package/dist/chat/messages.js +0 -151
- package/dist/chat/name.d.ts +0 -6
- package/dist/chat/name.d.ts.map +0 -1
- package/dist/chat/name.js +0 -37
- package/dist/chat/opened.d.ts +0 -11
- package/dist/chat/opened.d.ts.map +0 -1
- package/dist/chat/opened.js +0 -17
- package/dist/chat/overlay.d.ts +0 -31
- package/dist/chat/overlay.d.ts.map +0 -1
- package/dist/chat/overlay.js +0 -97
- package/dist/chat/parts.d.ts +0 -57
- package/dist/chat/parts.d.ts.map +0 -1
- package/dist/chat/parts.js +0 -119
- package/dist/chat/process.d.ts +0 -20
- package/dist/chat/process.d.ts.map +0 -1
- package/dist/chat/process.js +0 -32
- package/dist/chat/provider.d.ts +0 -63
- package/dist/chat/provider.d.ts.map +0 -1
- package/dist/chat/provider.js +0 -620
- package/dist/chat/question.d.ts +0 -9
- package/dist/chat/question.d.ts.map +0 -1
- package/dist/chat/question.js +0 -80
- package/dist/chat/questions.d.ts +0 -31
- package/dist/chat/questions.d.ts.map +0 -1
- package/dist/chat/questions.js +0 -111
- package/dist/chat/read.d.ts +0 -5
- package/dist/chat/read.d.ts.map +0 -1
- package/dist/chat/read.js +0 -13
- package/dist/chat/ready.d.ts +0 -33
- package/dist/chat/ready.d.ts.map +0 -1
- package/dist/chat/ready.js +0 -91
- package/dist/chat/style.d.ts +0 -4
- package/dist/chat/style.d.ts.map +0 -1
- package/dist/chat/style.js +0 -302
- package/dist/chat.d.ts +0 -14
- package/dist/chat.d.ts.map +0 -1
- package/dist/chat.js +0 -8
- package/dist/read-side.d.ts +0 -18
- package/dist/read-side.d.ts.map +0 -1
- package/dist/read-side.js +0 -440
package/dist/conversation.js
CHANGED
|
@@ -1,15 +1,29 @@
|
|
|
1
1
|
import { conversationChangeSet } from './change-set.js';
|
|
2
2
|
import { runtimeError } from './failure.js';
|
|
3
|
+
import { refusedForSpace } from './durability.js';
|
|
3
4
|
import { declaredFile } from './file.js';
|
|
4
5
|
import { changeBatchIdentity, chatDraftRevision } from './identity.js';
|
|
5
6
|
import { OFFLINE } from './outcome.js';
|
|
7
|
+
import { capped, cursorOf, decodedCursor, FROM_START, messageOrder, precedes } from './windows.js';
|
|
6
8
|
const QUIET = {
|
|
7
9
|
turn: null,
|
|
8
10
|
questions: [],
|
|
9
11
|
process: null,
|
|
10
12
|
commands: [],
|
|
11
13
|
options: [],
|
|
14
|
+
starting: null,
|
|
15
|
+
link: null,
|
|
16
|
+
deliveryUnknown: [],
|
|
12
17
|
};
|
|
18
|
+
const FACTS = [
|
|
19
|
+
'questions',
|
|
20
|
+
'process',
|
|
21
|
+
'commands',
|
|
22
|
+
'options',
|
|
23
|
+
'starting',
|
|
24
|
+
'link',
|
|
25
|
+
'deliveryUnknown',
|
|
26
|
+
];
|
|
13
27
|
export function unheldView(conversation) {
|
|
14
28
|
return {
|
|
15
29
|
conversation,
|
|
@@ -19,9 +33,26 @@ export function unheldView(conversation) {
|
|
|
19
33
|
process: null,
|
|
20
34
|
commands: [],
|
|
21
35
|
options: [],
|
|
36
|
+
starting: null,
|
|
37
|
+
link: null,
|
|
38
|
+
deliveryUnknown: [],
|
|
22
39
|
chatDraft: null,
|
|
23
40
|
};
|
|
24
41
|
}
|
|
42
|
+
export const SESSION_STARTED = 'Session started';
|
|
43
|
+
const NEW_CHAT = 'new:';
|
|
44
|
+
export const HISTORY_CONVERSATIONS = 64;
|
|
45
|
+
export function newChat(agent) {
|
|
46
|
+
return `${NEW_CHAT}${agent}`;
|
|
47
|
+
}
|
|
48
|
+
function creationOf(input) {
|
|
49
|
+
if (typeof input !== 'object' || input === null)
|
|
50
|
+
return null;
|
|
51
|
+
const { agent_id: agent, operation_id: operation, replaces, } = input;
|
|
52
|
+
return typeof agent === 'string' && typeof operation === 'string' && replaces === undefined
|
|
53
|
+
? { agent, operation }
|
|
54
|
+
: null;
|
|
55
|
+
}
|
|
25
56
|
function startedTurn(turn, operation) {
|
|
26
57
|
if (operation.op === 'begin')
|
|
27
58
|
return { turn, started: operation.at, parts: [], context: null };
|
|
@@ -71,18 +102,28 @@ function appliedTurn(held, operation) {
|
|
|
71
102
|
parts: held.parts.map((kept, at) => (at === operation.index ? changed : kept)),
|
|
72
103
|
};
|
|
73
104
|
}
|
|
74
|
-
function
|
|
75
|
-
if (left.position !== right.position)
|
|
76
|
-
return left.position < right.position ? -1 : 1;
|
|
77
|
-
if (left.identity !== right.identity)
|
|
78
|
-
return left.identity < right.identity ? -1 : 1;
|
|
79
|
-
return 0;
|
|
80
|
-
}
|
|
81
|
-
function merged(held, arriving) {
|
|
105
|
+
function windowOf(held, arriving, limit) {
|
|
82
106
|
const byIdentity = new Map(held.map((message) => [message.identity, message]));
|
|
83
107
|
for (const message of arriving)
|
|
84
108
|
byIdentity.set(message.identity, message);
|
|
85
|
-
|
|
109
|
+
const next = [...byIdentity.values()].sort(messageOrder);
|
|
110
|
+
const start = Math.max(0, next.length - limit);
|
|
111
|
+
return { messages: next.slice(start), dropped: start > 0 };
|
|
112
|
+
}
|
|
113
|
+
function frameOf(asked, messages, more, total) {
|
|
114
|
+
const oldest = messages[0];
|
|
115
|
+
const next = !more
|
|
116
|
+
? undefined
|
|
117
|
+
: oldest === undefined
|
|
118
|
+
? (asked.after ?? FROM_START)
|
|
119
|
+
: cursorOf([oldest.position, oldest.identity]);
|
|
120
|
+
return {
|
|
121
|
+
...(next === undefined ? {} : { next }),
|
|
122
|
+
...(total === null ? {} : { total }),
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
function newestOf(arriving, from) {
|
|
126
|
+
return arriving.reduce((top, message) => (top === null || message.position > top ? message.position : top), from);
|
|
86
127
|
}
|
|
87
128
|
function acceptedMessage(value) {
|
|
88
129
|
if (typeof value !== 'object' || value === null)
|
|
@@ -141,52 +182,81 @@ function historyPages(result) {
|
|
|
141
182
|
}
|
|
142
183
|
return pages;
|
|
143
184
|
}
|
|
144
|
-
function
|
|
185
|
+
function answered(result, field) {
|
|
145
186
|
if (result.kind !== 'action')
|
|
146
187
|
return null;
|
|
147
188
|
const answer = result.result;
|
|
148
189
|
if (typeof answer !== 'object' || answer === null)
|
|
149
190
|
return null;
|
|
150
|
-
const
|
|
151
|
-
return typeof
|
|
191
|
+
const value = answer[field];
|
|
192
|
+
return typeof value === 'string' ? value : null;
|
|
193
|
+
}
|
|
194
|
+
function beforeOf(after) {
|
|
195
|
+
if (after === undefined)
|
|
196
|
+
return null;
|
|
197
|
+
const decoded = typeof after === 'string' ? decodedCursor(after) : null;
|
|
198
|
+
if (decoded?.length === 0)
|
|
199
|
+
return null;
|
|
200
|
+
const [position, identity] = decoded ?? [];
|
|
201
|
+
if (decoded?.length !== 2 || typeof position !== 'string' || typeof identity !== 'string') {
|
|
202
|
+
throw runtimeError('invalid-after', `conversation after must be the next an earlier view returned, got ${String(after)}`);
|
|
203
|
+
}
|
|
204
|
+
return [position, identity];
|
|
152
205
|
}
|
|
153
|
-
function
|
|
154
|
-
const { limit } = options;
|
|
155
|
-
if (limit
|
|
156
|
-
return undefined;
|
|
157
|
-
if (!Number.isInteger(limit) || limit < 0) {
|
|
206
|
+
function askedOf(options) {
|
|
207
|
+
const { limit, after, total } = options;
|
|
208
|
+
if (limit !== undefined && (!Number.isInteger(limit) || limit < 0)) {
|
|
158
209
|
throw runtimeError('invalid-limit', `conversation limit must be a non-negative integer, got ${String(limit)}`);
|
|
159
210
|
}
|
|
160
|
-
return limit;
|
|
211
|
+
return { limit: capped(limit), after, before: beforeOf(after), total: total === true };
|
|
161
212
|
}
|
|
213
|
+
const EXPIRED = { kind: 'rejected', code: 'expired', fields: {}, retryable: false };
|
|
214
|
+
const DURABLE_WRITE = {
|
|
215
|
+
kind: 'rejected',
|
|
216
|
+
code: 'durable-write',
|
|
217
|
+
fields: {},
|
|
218
|
+
retryable: true,
|
|
219
|
+
};
|
|
162
220
|
function unwritten(draft) {
|
|
163
|
-
return draft.text === ''
|
|
221
|
+
return draft.text === '';
|
|
164
222
|
}
|
|
165
223
|
function pending(draft) {
|
|
166
|
-
return
|
|
224
|
+
return {
|
|
225
|
+
...(draft.submitted === undefined ? {} : { submitted: draft.submitted }),
|
|
226
|
+
...(draft.moves === undefined ? {} : { moves: draft.moves }),
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
function empty(draft) {
|
|
230
|
+
return unwritten(draft) && draft.submitted === undefined && draft.moves === undefined;
|
|
167
231
|
}
|
|
168
232
|
function withSubmitted(draft, submitted) {
|
|
169
233
|
const { submitted: _held, ...unsent } = draft;
|
|
170
234
|
return submitted.length === 0 ? unsent : { ...unsent, submitted };
|
|
171
235
|
}
|
|
236
|
+
function prepended(draft, said) {
|
|
237
|
+
return { text: [said.text, draft.text].filter((one) => one !== '').join('\n') };
|
|
238
|
+
}
|
|
172
239
|
function returned(draft, submission, others) {
|
|
173
240
|
return withSubmitted({
|
|
174
241
|
conversation: draft.conversation,
|
|
175
|
-
|
|
176
|
-
files: [...submission.files, ...draft.files],
|
|
242
|
+
...prepended(draft, submission),
|
|
177
243
|
revision: chatDraftRevision(),
|
|
178
244
|
}, others);
|
|
179
245
|
}
|
|
180
246
|
export function createConversationStore(deps) {
|
|
181
|
-
const
|
|
247
|
+
const stored = new Set();
|
|
182
248
|
const reads = new Map();
|
|
249
|
+
const partials = new Set();
|
|
183
250
|
const drafts = new Map();
|
|
184
|
-
const sending = new
|
|
251
|
+
const sending = new Map();
|
|
185
252
|
const behind = new Set();
|
|
253
|
+
const catching = new Map();
|
|
186
254
|
const gaps = new Map();
|
|
187
255
|
const lives = new Map();
|
|
256
|
+
const unsent = new Map();
|
|
188
257
|
const views = new Map();
|
|
189
258
|
const listing = new Map();
|
|
259
|
+
const creations = new Set();
|
|
190
260
|
const listWatchers = new Set();
|
|
191
261
|
let listKnown = false;
|
|
192
262
|
const agentWatchers = new Set();
|
|
@@ -198,49 +268,70 @@ export function createConversationStore(deps) {
|
|
|
198
268
|
function liveOf(conversation) {
|
|
199
269
|
return lives.get(conversation) ?? QUIET;
|
|
200
270
|
}
|
|
201
|
-
function
|
|
202
|
-
const held = messages.get(conversation) ?? [];
|
|
203
|
-
const shown = limit === undefined ? held : held.slice(Math.max(0, held.length - limit));
|
|
204
|
-
return shown.map(({ identity, turn, text, message }) => ({
|
|
205
|
-
identity,
|
|
206
|
-
turn,
|
|
207
|
-
text,
|
|
208
|
-
message: structuredClone(message),
|
|
209
|
-
}));
|
|
210
|
-
}
|
|
211
|
-
function viewOf(conversation, limit, shown = shownMessages(conversation, limit)) {
|
|
271
|
+
function viewOf(conversation, held, frame) {
|
|
212
272
|
const draft = drafts.get(conversation);
|
|
213
273
|
const facts = liveOf(conversation);
|
|
214
274
|
return {
|
|
215
275
|
conversation,
|
|
216
|
-
messages:
|
|
276
|
+
messages: held.map(({ identity, turn, text, message }) => ({
|
|
277
|
+
identity,
|
|
278
|
+
turn,
|
|
279
|
+
text,
|
|
280
|
+
message,
|
|
281
|
+
})),
|
|
282
|
+
...frame,
|
|
217
283
|
turn: structuredClone(facts.turn),
|
|
218
284
|
questions: structuredClone(facts.questions),
|
|
219
285
|
process: facts.process === null ? null : { ...facts.process },
|
|
220
286
|
commands: structuredClone(facts.commands),
|
|
221
287
|
options: structuredClone(facts.options),
|
|
288
|
+
starting: facts.starting,
|
|
289
|
+
link: facts.link === null ? null : { ...facts.link },
|
|
290
|
+
deliveryUnknown: [...facts.deliveryUnknown],
|
|
222
291
|
chatDraft: draft === undefined || unwritten(draft)
|
|
223
292
|
? null
|
|
224
|
-
: { text: draft.text,
|
|
293
|
+
: { text: draft.text, revision: draft.revision },
|
|
225
294
|
};
|
|
226
295
|
}
|
|
227
|
-
function emit(conversation
|
|
296
|
+
function emit(conversation) {
|
|
228
297
|
if (deps.closed())
|
|
229
298
|
return;
|
|
230
299
|
for (const view of views.values()) {
|
|
231
|
-
if (view.conversation !== conversation)
|
|
300
|
+
if (view.conversation !== conversation || view.window === null)
|
|
232
301
|
continue;
|
|
233
|
-
if (messagesChanged)
|
|
234
|
-
view.messages = null;
|
|
235
302
|
const ready = [...view.watchers].filter((watcher) => watcher.ready);
|
|
236
303
|
if (ready.length === 0)
|
|
237
304
|
continue;
|
|
238
|
-
|
|
239
|
-
const value = viewOf(conversation, view.limit, view.messages);
|
|
305
|
+
const value = viewOf(conversation, view.window, sharedFrame(view, view.window));
|
|
240
306
|
for (const watcher of ready)
|
|
241
307
|
watcher.notify(value);
|
|
242
308
|
}
|
|
243
309
|
}
|
|
310
|
+
function sharedFrame(view, window) {
|
|
311
|
+
return frameOf(view.asked, window, view.more, view.total);
|
|
312
|
+
}
|
|
313
|
+
async function slide(local, conversation, arriving) {
|
|
314
|
+
let count;
|
|
315
|
+
for (const view of views.values()) {
|
|
316
|
+
if (view.conversation !== conversation || view.window === null)
|
|
317
|
+
continue;
|
|
318
|
+
if (arriving === null) {
|
|
319
|
+
view.window = [];
|
|
320
|
+
view.more = false;
|
|
321
|
+
if (view.total !== null)
|
|
322
|
+
view.total = 0;
|
|
323
|
+
continue;
|
|
324
|
+
}
|
|
325
|
+
const joining = arriving.filter((message) => precedes(message, view.asked.before));
|
|
326
|
+
const next = windowOf(view.window, joining, view.asked.limit);
|
|
327
|
+
view.window = next.messages;
|
|
328
|
+
view.more ||= next.dropped;
|
|
329
|
+
if (view.total !== null && arriving.length > 0) {
|
|
330
|
+
count ??= await local.messageCount(conversation);
|
|
331
|
+
view.total = count;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
244
335
|
function stateOf(conversation) {
|
|
245
336
|
const facts = liveOf(conversation);
|
|
246
337
|
if (facts.questions.length > 0)
|
|
@@ -255,6 +346,7 @@ export function createConversationStore(deps) {
|
|
|
255
346
|
created: entry.created,
|
|
256
347
|
finished: entry.finished,
|
|
257
348
|
outcome: entry.outcome,
|
|
349
|
+
waiting: entry.waiting,
|
|
258
350
|
state: stateOf(entry.conversation),
|
|
259
351
|
started: lives.get(entry.conversation)?.turn?.started ?? null,
|
|
260
352
|
}));
|
|
@@ -297,20 +389,30 @@ export function createConversationStore(deps) {
|
|
|
297
389
|
const declaration = deps.agents();
|
|
298
390
|
if (declaration === undefined)
|
|
299
391
|
return;
|
|
300
|
-
const { documents } = await deps.documents(declaration.locator);
|
|
301
392
|
const names = new Map();
|
|
302
393
|
const retired = new Set();
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
394
|
+
let covered = null;
|
|
395
|
+
for (let after;;) {
|
|
396
|
+
const read = await deps.documents(declaration, after);
|
|
397
|
+
if ('pending' in read)
|
|
398
|
+
return;
|
|
399
|
+
const coverage = JSON.stringify(read.coverage);
|
|
400
|
+
if (covered !== null && coverage !== covered)
|
|
401
|
+
return readAgents();
|
|
402
|
+
covered = coverage;
|
|
403
|
+
for (const document of read.documents) {
|
|
404
|
+
const identity = document.fields?.[declaration.identity];
|
|
405
|
+
const name = document.fields?.[declaration.name];
|
|
406
|
+
if (typeof identity !== 'string' || typeof name !== 'string')
|
|
407
|
+
continue;
|
|
408
|
+
names.set(identity, name);
|
|
409
|
+
const ended = declaration.retired === undefined ? null : document.fields?.[declaration.retired];
|
|
410
|
+
if (ended !== undefined && ended !== null)
|
|
411
|
+
retired.add(identity);
|
|
412
|
+
}
|
|
413
|
+
if (read.next === undefined)
|
|
414
|
+
break;
|
|
415
|
+
after = read.next;
|
|
314
416
|
}
|
|
315
417
|
const named = JSON.stringify([...names]) !== JSON.stringify([...agentNames]);
|
|
316
418
|
if (!named && JSON.stringify([...retired]) === JSON.stringify([...retiredAgents]))
|
|
@@ -320,6 +422,12 @@ export function createConversationStore(deps) {
|
|
|
320
422
|
if (named)
|
|
321
423
|
emitList();
|
|
322
424
|
emitAgents();
|
|
425
|
+
const ended = [...drafts.keys()].filter(retiredChat);
|
|
426
|
+
if (ended.length > 0)
|
|
427
|
+
await remove(ended);
|
|
428
|
+
}
|
|
429
|
+
function retiredChat(conversation) {
|
|
430
|
+
return (conversation.startsWith(NEW_CHAT) && retiredAgents.has(conversation.slice(NEW_CHAT.length)));
|
|
323
431
|
}
|
|
324
432
|
function agentsReady() {
|
|
325
433
|
agentsRead ??= readAgents().catch(() => {
|
|
@@ -328,8 +436,33 @@ export function createConversationStore(deps) {
|
|
|
328
436
|
return agentsRead;
|
|
329
437
|
}
|
|
330
438
|
function live(conversation, change) {
|
|
439
|
+
const owed = unsent.get(conversation);
|
|
440
|
+
for (const fact of FACTS.filter((one) => one in change))
|
|
441
|
+
owed?.delete(fact);
|
|
331
442
|
setLive(conversation, { ...liveOf(conversation), ...change });
|
|
332
|
-
emit(conversation
|
|
443
|
+
emit(conversation);
|
|
444
|
+
}
|
|
445
|
+
function openingEnded() {
|
|
446
|
+
const owed = [...unsent];
|
|
447
|
+
unsent.clear();
|
|
448
|
+
for (const [conversation, facts] of owed) {
|
|
449
|
+
const held = lives.get(conversation);
|
|
450
|
+
if (held === undefined || facts.size === 0)
|
|
451
|
+
continue;
|
|
452
|
+
setLive(conversation, {
|
|
453
|
+
turn: held.turn,
|
|
454
|
+
questions: facts.has('questions') ? QUIET.questions : held.questions,
|
|
455
|
+
process: facts.has('process') ? QUIET.process : held.process,
|
|
456
|
+
commands: facts.has('commands') ? QUIET.commands : held.commands,
|
|
457
|
+
options: facts.has('options') ? QUIET.options : held.options,
|
|
458
|
+
starting: facts.has('starting') ? QUIET.starting : held.starting,
|
|
459
|
+
link: facts.has('link') ? QUIET.link : held.link,
|
|
460
|
+
deliveryUnknown: facts.has('deliveryUnknown')
|
|
461
|
+
? QUIET.deliveryUnknown
|
|
462
|
+
: held.deliveryUnknown,
|
|
463
|
+
});
|
|
464
|
+
emit(conversation);
|
|
465
|
+
}
|
|
333
466
|
}
|
|
334
467
|
function relayed(conversation, turn, operation) {
|
|
335
468
|
const held = liveOf(conversation).turn;
|
|
@@ -358,23 +491,22 @@ export function createConversationStore(deps) {
|
|
|
358
491
|
async function remove(leaving) {
|
|
359
492
|
await deps.durably(async (local) => {
|
|
360
493
|
await local.dropConversations(leaving);
|
|
361
|
-
const removed = [];
|
|
362
494
|
const changed = [];
|
|
363
495
|
for (const conversation of leaving) {
|
|
364
|
-
|
|
365
|
-
removed.push({ conversation, identity: message.identity });
|
|
366
|
-
}
|
|
367
|
-
if (messages.has(conversation) || drafts.has(conversation) || lives.has(conversation)) {
|
|
496
|
+
if (stored.has(conversation) || drafts.has(conversation) || lives.has(conversation)) {
|
|
368
497
|
changed.push(conversation);
|
|
369
498
|
}
|
|
370
|
-
|
|
499
|
+
stored.delete(conversation);
|
|
371
500
|
reads.delete(conversation);
|
|
501
|
+
partials.delete(conversation);
|
|
372
502
|
drafts.delete(conversation);
|
|
373
503
|
behind.delete(conversation);
|
|
504
|
+
unsent.delete(conversation);
|
|
374
505
|
setLive(conversation, null);
|
|
506
|
+
await slide(local, conversation, null);
|
|
375
507
|
}
|
|
376
508
|
if (changed.length > 0)
|
|
377
|
-
deps.publish(conversationChangeSet(
|
|
509
|
+
deps.publish(conversationChangeSet(changed));
|
|
378
510
|
});
|
|
379
511
|
}
|
|
380
512
|
function gap(conversation) {
|
|
@@ -393,25 +525,24 @@ export function createConversationStore(deps) {
|
|
|
393
525
|
await local.keepMessages({
|
|
394
526
|
conversation,
|
|
395
527
|
messages: [message],
|
|
396
|
-
...(advances ? { read: message.position } : {}),
|
|
528
|
+
...(advances ? { read: { at: message.position, partial: false } } : {}),
|
|
397
529
|
});
|
|
398
|
-
|
|
530
|
+
stored.add(conversation);
|
|
399
531
|
if (advances)
|
|
400
532
|
reads.set(conversation, message.position);
|
|
401
533
|
sealTurns(conversation, new Set([message.turn]));
|
|
402
|
-
|
|
534
|
+
await slide(local, conversation, [message]);
|
|
535
|
+
deps.publish(conversationChangeSet([conversation]));
|
|
403
536
|
});
|
|
404
537
|
}
|
|
405
|
-
async function readHistories(conversations) {
|
|
538
|
+
async function readHistories(conversations, ended) {
|
|
406
539
|
const generations = new Map(conversations.map((one) => [one, gaps.get(one) ?? 0]));
|
|
407
|
-
|
|
408
|
-
const arriving = new Map();
|
|
409
|
-
const complete = new Set();
|
|
410
|
-
let asking = conversations.map((conversation) => ({
|
|
540
|
+
let waiting = conversations.map((conversation) => ({
|
|
411
541
|
conversation,
|
|
412
|
-
after:
|
|
542
|
+
after: reads.get(conversation) ?? undefined,
|
|
413
543
|
}));
|
|
414
|
-
while (
|
|
544
|
+
while (waiting.length > 0) {
|
|
545
|
+
const asking = waiting.slice(0, HISTORY_CONVERSATIONS);
|
|
415
546
|
const result = await deps.call('conversation.history', {
|
|
416
547
|
conversations: asking.map(({ conversation, after }) => ({
|
|
417
548
|
conversation,
|
|
@@ -421,43 +552,64 @@ export function createConversationStore(deps) {
|
|
|
421
552
|
const pages = historyPages(result);
|
|
422
553
|
if (pages === null)
|
|
423
554
|
return result;
|
|
424
|
-
const asked = new
|
|
555
|
+
const asked = new Map(asking.map(({ conversation, after }) => [conversation, after]));
|
|
425
556
|
const following = [];
|
|
426
557
|
for (const page of pages) {
|
|
427
558
|
if (!asked.has(page.conversation))
|
|
428
559
|
continue;
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
...page.messages,
|
|
432
|
-
]);
|
|
433
|
-
if (page.next === undefined)
|
|
434
|
-
complete.add(page.conversation);
|
|
435
|
-
else
|
|
560
|
+
await keepPage(page.conversation, page.messages, asked.get(page.conversation) ?? null, page.next, generations.get(page.conversation) ?? 0);
|
|
561
|
+
if (page.next !== undefined) {
|
|
436
562
|
following.push({ conversation: page.conversation, after: page.next });
|
|
563
|
+
}
|
|
437
564
|
}
|
|
438
|
-
asking
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
await keepHistory(conversation, held, starts.get(conversation), generations.get(conversation) ?? 0);
|
|
565
|
+
for (const { conversation } of asking) {
|
|
566
|
+
if (!following.some((next) => next.conversation === conversation))
|
|
567
|
+
ended(conversation);
|
|
568
|
+
}
|
|
569
|
+
waiting = [...waiting.slice(HISTORY_CONVERSATIONS), ...following];
|
|
444
570
|
}
|
|
445
571
|
return null;
|
|
446
572
|
}
|
|
447
|
-
function
|
|
573
|
+
function caughtUp(conversations) {
|
|
574
|
+
const ends = new Map();
|
|
575
|
+
for (const conversation of conversations) {
|
|
576
|
+
const held = new Promise((end) => ends.set(conversation, end));
|
|
577
|
+
catching.set(conversation, held);
|
|
578
|
+
void held.then(() => {
|
|
579
|
+
if (catching.get(conversation) === held)
|
|
580
|
+
catching.delete(conversation);
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
const ended = (conversation) => ends.get(conversation)?.();
|
|
584
|
+
const read = readHistories(conversations, ended);
|
|
585
|
+
const endAll = () => {
|
|
586
|
+
for (const conversation of conversations)
|
|
587
|
+
ended(conversation);
|
|
588
|
+
};
|
|
589
|
+
read.then(endAll, endAll);
|
|
590
|
+
return read;
|
|
591
|
+
}
|
|
592
|
+
function keepPage(conversation, arriving, from, next, generation) {
|
|
448
593
|
return deps.durably(async (local) => {
|
|
449
594
|
if (!replicated.has(conversation))
|
|
450
595
|
return;
|
|
451
|
-
const
|
|
596
|
+
const whole = next === undefined;
|
|
597
|
+
const cursor = next ?? newestOf(arriving, from);
|
|
598
|
+
const partial = !whole && (partials.has(conversation) || !reads.has(conversation));
|
|
452
599
|
const sealing = await sealed(local, conversation, new Set(arriving.map((message) => message.turn)));
|
|
453
|
-
await local.keepMessages({ conversation, messages: arriving, read:
|
|
454
|
-
|
|
455
|
-
reads.set(conversation,
|
|
600
|
+
await local.keepMessages({ conversation, messages: arriving, read: { at: cursor, partial } });
|
|
601
|
+
stored.add(conversation);
|
|
602
|
+
reads.set(conversation, cursor);
|
|
603
|
+
if (partial)
|
|
604
|
+
partials.add(conversation);
|
|
605
|
+
else
|
|
606
|
+
partials.delete(conversation);
|
|
456
607
|
sealTurns(conversation, new Set(arriving.map((message) => message.turn)));
|
|
457
|
-
if ((gaps.get(conversation) ?? 0) === generation)
|
|
608
|
+
if (whole && (gaps.get(conversation) ?? 0) === generation)
|
|
458
609
|
behind.delete(conversation);
|
|
459
|
-
|
|
460
|
-
|
|
610
|
+
await slide(local, conversation, arriving);
|
|
611
|
+
if (arriving.length > 0 || sealing || whole) {
|
|
612
|
+
deps.publish(conversationChangeSet([conversation]));
|
|
461
613
|
}
|
|
462
614
|
});
|
|
463
615
|
}
|
|
@@ -466,11 +618,8 @@ export function createConversationStore(deps) {
|
|
|
466
618
|
if (event.kind === 'gap') {
|
|
467
619
|
if (replicated.has(conversation))
|
|
468
620
|
gap(conversation);
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
return;
|
|
472
|
-
setLive(conversation, { ...QUIET, turn: held.turn });
|
|
473
|
-
emit(conversation, false);
|
|
621
|
+
if (lives.has(conversation))
|
|
622
|
+
unsent.set(conversation, new Set(FACTS));
|
|
474
623
|
return;
|
|
475
624
|
}
|
|
476
625
|
if (event.kind === 'turn') {
|
|
@@ -493,23 +642,42 @@ export function createConversationStore(deps) {
|
|
|
493
642
|
live(conversation, { options: structuredClone(event.options) });
|
|
494
643
|
return;
|
|
495
644
|
}
|
|
645
|
+
if (event.kind === 'starting') {
|
|
646
|
+
live(conversation, { starting: event.step });
|
|
647
|
+
return;
|
|
648
|
+
}
|
|
649
|
+
if (event.kind === 'link') {
|
|
650
|
+
live(conversation, { link: { connected: event.connected } });
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
653
|
+
if (event.kind === 'deliveries') {
|
|
654
|
+
live(conversation, { deliveryUnknown: [...event.unknown] });
|
|
655
|
+
return;
|
|
656
|
+
}
|
|
496
657
|
const arriving = acceptedMessage(event.message);
|
|
497
658
|
if (arriving === null)
|
|
498
659
|
return;
|
|
499
660
|
if (replicated.has(conversation)) {
|
|
500
|
-
|
|
661
|
+
if (deps.full()) {
|
|
662
|
+
gap(conversation);
|
|
663
|
+
return;
|
|
664
|
+
}
|
|
665
|
+
void keep(conversation, arriving).catch((error) => {
|
|
666
|
+
if (refusedForSpace(error))
|
|
667
|
+
gap(conversation);
|
|
668
|
+
});
|
|
501
669
|
return;
|
|
502
670
|
}
|
|
503
671
|
if (sealTurns(conversation, new Set([arriving.turn])))
|
|
504
672
|
emit(conversation);
|
|
505
673
|
}
|
|
506
674
|
async function kept(local, conversation, following) {
|
|
507
|
-
if (following === null || (
|
|
675
|
+
if (following === null || empty(following)) {
|
|
508
676
|
await local.dropChatDraft(conversation);
|
|
509
677
|
drafts.delete(conversation);
|
|
510
678
|
}
|
|
511
679
|
else {
|
|
512
|
-
await local.
|
|
680
|
+
await local.keepChatDrafts([following]);
|
|
513
681
|
drafts.set(conversation, following);
|
|
514
682
|
}
|
|
515
683
|
}
|
|
@@ -522,60 +690,88 @@ export function createConversationStore(deps) {
|
|
|
522
690
|
await kept(local, conversation, withSubmitted(held, left));
|
|
523
691
|
return true;
|
|
524
692
|
}
|
|
693
|
+
function moved(agent, operation, to) {
|
|
694
|
+
const from = newChat(agent);
|
|
695
|
+
return deps.durably(async (local) => {
|
|
696
|
+
const held = drafts.get(from);
|
|
697
|
+
const earlier = (held?.moves ?? []).filter((one) => !deps.expired(one));
|
|
698
|
+
if (earlier.includes(operation))
|
|
699
|
+
return;
|
|
700
|
+
const blank = {
|
|
701
|
+
conversation: from,
|
|
702
|
+
text: '',
|
|
703
|
+
revision: chatDraftRevision(),
|
|
704
|
+
};
|
|
705
|
+
const { moves: _answered, ...unsent } = held ?? blank;
|
|
706
|
+
const taken = drafts.has(to) || empty(unsent) ? [] : [{ ...unsent, conversation: to }];
|
|
707
|
+
const left = { ...(taken.length === 0 ? unsent : blank), moves: [...earlier, operation] };
|
|
708
|
+
await local.keepChatDrafts([left, ...taken]);
|
|
709
|
+
drafts.set(from, left);
|
|
710
|
+
for (const one of taken)
|
|
711
|
+
drafts.set(to, one);
|
|
712
|
+
deps.publish(conversationChangeSet([from, to]));
|
|
713
|
+
});
|
|
714
|
+
}
|
|
525
715
|
function withDraft(conversation, next) {
|
|
526
716
|
return deps.durably(async (local) => {
|
|
527
|
-
const following = next(drafts.get(conversation));
|
|
717
|
+
const following = await next(drafts.get(conversation), local);
|
|
528
718
|
if (following === undefined)
|
|
529
719
|
return;
|
|
530
720
|
await kept(local, conversation, following);
|
|
531
|
-
deps.publish(conversationChangeSet([
|
|
721
|
+
deps.publish(conversationChangeSet([conversation]));
|
|
532
722
|
});
|
|
533
723
|
}
|
|
534
724
|
async function submitted(conversation, operation, draft) {
|
|
535
725
|
let submission;
|
|
726
|
+
let earlier = false;
|
|
536
727
|
await withDraft(conversation, (current) => {
|
|
537
728
|
submission = current?.submitted?.find((one) => one.operation === operation);
|
|
729
|
+
earlier = submission !== undefined;
|
|
538
730
|
if (submission !== undefined)
|
|
539
731
|
return undefined;
|
|
540
732
|
const said = draft ?? current;
|
|
541
|
-
submission = {
|
|
542
|
-
operation,
|
|
543
|
-
text: said?.text ?? '',
|
|
544
|
-
files: [...(said?.files ?? [])],
|
|
545
|
-
turn: null,
|
|
546
|
-
};
|
|
733
|
+
submission = { operation, text: said?.text ?? '', turn: null };
|
|
547
734
|
return {
|
|
548
735
|
conversation,
|
|
549
736
|
...(draft === undefined || current === undefined
|
|
550
|
-
? { text: '',
|
|
551
|
-
: { text: current.text,
|
|
737
|
+
? { text: '', revision: chatDraftRevision() }
|
|
738
|
+
: { text: current.text, revision: current.revision }),
|
|
552
739
|
submitted: [...(current?.submitted ?? []), submission],
|
|
553
740
|
};
|
|
554
741
|
});
|
|
555
|
-
return submission;
|
|
742
|
+
return { submission: submission, earlier };
|
|
743
|
+
}
|
|
744
|
+
function forgotten(conversation, submission) {
|
|
745
|
+
return withDraft(conversation, (current) => {
|
|
746
|
+
const held = current?.submitted ?? [];
|
|
747
|
+
if (current === undefined || !held.includes(submission))
|
|
748
|
+
return undefined;
|
|
749
|
+
return withSubmitted(current, held.filter((one) => one !== submission));
|
|
750
|
+
});
|
|
556
751
|
}
|
|
557
752
|
async function dispatched(conversation, submission) {
|
|
558
|
-
|
|
753
|
+
const dispatch = {};
|
|
754
|
+
sending.set(submission.operation, dispatch);
|
|
559
755
|
try {
|
|
560
|
-
const files = await Promise.all(submission.files.map(async (file) => ({ ...(await declaredFile(file)), blob: file.blob })));
|
|
561
756
|
const result = await invoke('conversation.send', conversation, {
|
|
562
757
|
text: submission.text,
|
|
563
|
-
files,
|
|
564
758
|
operation_id: submission.operation,
|
|
565
759
|
});
|
|
566
760
|
if (result.kind === 'revoked-authorization')
|
|
567
761
|
return result;
|
|
568
|
-
if (result.kind !== 'action' && result.retryable)
|
|
762
|
+
if (result.kind !== 'action' && result.retryable) {
|
|
763
|
+
deps.unanswered();
|
|
569
764
|
return result;
|
|
570
|
-
|
|
571
|
-
|
|
765
|
+
}
|
|
766
|
+
const turn = answered(result, 'turn');
|
|
767
|
+
await withDraft(conversation, async (current, local) => {
|
|
572
768
|
const held = current?.submitted?.find((one) => one.operation === submission.operation);
|
|
573
769
|
if (current === undefined || held === undefined || held.turn !== null)
|
|
574
770
|
return undefined;
|
|
575
771
|
const others = current.submitted.filter((one) => one !== held);
|
|
576
772
|
if (result.kind !== 'action')
|
|
577
773
|
return returned(current, held, others);
|
|
578
|
-
const arrived =
|
|
774
|
+
const arrived = turn !== null && (await local.turnArrived(conversation, turn));
|
|
579
775
|
return withSubmitted(current, turn === null || arrived
|
|
580
776
|
? others
|
|
581
777
|
: current.submitted.map((one) => (one === held ? { ...held, turn } : one)));
|
|
@@ -583,7 +779,8 @@ export function createConversationStore(deps) {
|
|
|
583
779
|
return result;
|
|
584
780
|
}
|
|
585
781
|
finally {
|
|
586
|
-
sending.
|
|
782
|
+
if (sending.get(submission.operation) === dispatch)
|
|
783
|
+
sending.delete(submission.operation);
|
|
587
784
|
}
|
|
588
785
|
}
|
|
589
786
|
function invoke(name, conversation, extra = {}) {
|
|
@@ -595,23 +792,58 @@ export function createConversationStore(deps) {
|
|
|
595
792
|
return entries();
|
|
596
793
|
},
|
|
597
794
|
async read(conversation, options = {}) {
|
|
598
|
-
const
|
|
599
|
-
return deps.durably(() =>
|
|
795
|
+
const asked = askedOf(options);
|
|
796
|
+
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
|
+
return structuredClone(viewOf(conversation, messages, frameOf(asked, messages, more, total)));
|
|
800
|
+
});
|
|
600
801
|
},
|
|
601
|
-
create(input) {
|
|
602
|
-
|
|
802
|
+
async create(input) {
|
|
803
|
+
const result = await deps.call('conversation.create', input);
|
|
804
|
+
const conversation = answered(result, 'conversation');
|
|
805
|
+
if (conversation === null)
|
|
806
|
+
return result;
|
|
807
|
+
if (!listing.has(conversation))
|
|
808
|
+
creations.add(conversation);
|
|
809
|
+
const creation = creationOf(input);
|
|
810
|
+
if (creation !== null)
|
|
811
|
+
await moved(creation.agent, creation.operation, conversation);
|
|
812
|
+
return result;
|
|
603
813
|
},
|
|
604
814
|
async send(conversation, operation = changeBatchIdentity(), draft) {
|
|
815
|
+
if (deps.full())
|
|
816
|
+
return DURABLE_WRITE;
|
|
605
817
|
if (deps.online()) {
|
|
606
|
-
|
|
818
|
+
const held = await submitted(conversation, operation, draft).catch((error) => {
|
|
819
|
+
if (refusedForSpace(error))
|
|
820
|
+
return null;
|
|
821
|
+
throw error;
|
|
822
|
+
});
|
|
823
|
+
if (held === null)
|
|
824
|
+
return DURABLE_WRITE;
|
|
825
|
+
const { submission, earlier } = held;
|
|
826
|
+
if (!earlier || !deps.expired(operation))
|
|
827
|
+
return dispatched(conversation, submission);
|
|
828
|
+
await forgotten(conversation, submission);
|
|
829
|
+
return EXPIRED;
|
|
607
830
|
}
|
|
608
831
|
if (draft !== undefined) {
|
|
609
832
|
await withDraft(conversation, (current) => current?.submitted?.some((one) => one.operation === operation) === true
|
|
610
833
|
? undefined
|
|
611
|
-
: returned(current ?? { conversation, text: '',
|
|
834
|
+
: returned(current ?? { conversation, text: '', revision: '' }, { operation, ...draft, turn: null }, current?.submitted ?? []));
|
|
612
835
|
}
|
|
613
836
|
return OFFLINE;
|
|
614
837
|
},
|
|
838
|
+
async attach(room, file) {
|
|
839
|
+
if (!deps.online())
|
|
840
|
+
return OFFLINE;
|
|
841
|
+
return deps.call('conversation.attach', {
|
|
842
|
+
room,
|
|
843
|
+
...(await declaredFile(file)),
|
|
844
|
+
blob: file.blob,
|
|
845
|
+
});
|
|
846
|
+
},
|
|
615
847
|
answer(conversation, answer) {
|
|
616
848
|
return invoke('conversation.answer', conversation, { answer });
|
|
617
849
|
},
|
|
@@ -642,38 +874,40 @@ export function createConversationStore(deps) {
|
|
|
642
874
|
return withDraft(conversation, (current) => ({
|
|
643
875
|
conversation,
|
|
644
876
|
text: draft.text,
|
|
645
|
-
files: [...draft.files],
|
|
646
877
|
revision,
|
|
647
|
-
...(current
|
|
878
|
+
...(current === undefined ? {} : pending(current)),
|
|
879
|
+
}));
|
|
880
|
+
},
|
|
881
|
+
prepend(conversation, draft) {
|
|
882
|
+
return withDraft(conversation, (current) => ({
|
|
883
|
+
conversation,
|
|
884
|
+
...(current === undefined ? { text: draft.text } : prepended(current, draft)),
|
|
885
|
+
revision: chatDraftRevision(),
|
|
886
|
+
...(current === undefined ? {} : pending(current)),
|
|
648
887
|
}));
|
|
649
888
|
},
|
|
650
889
|
discard(conversation) {
|
|
651
890
|
return withDraft(conversation, (current) => current === undefined
|
|
652
891
|
? undefined
|
|
653
|
-
: {
|
|
654
|
-
conversation,
|
|
655
|
-
text: '',
|
|
656
|
-
files: [],
|
|
657
|
-
revision: chatDraftRevision(),
|
|
658
|
-
...pending(current),
|
|
659
|
-
});
|
|
892
|
+
: { conversation, text: '', revision: chatDraftRevision(), ...pending(current) });
|
|
660
893
|
},
|
|
661
894
|
};
|
|
662
895
|
return {
|
|
663
896
|
surface,
|
|
664
897
|
chatDraft,
|
|
665
|
-
hydrate(held,
|
|
666
|
-
|
|
898
|
+
hydrate(held, kept) {
|
|
899
|
+
stored.clear();
|
|
667
900
|
reads.clear();
|
|
668
|
-
|
|
669
|
-
view.messages = null;
|
|
901
|
+
partials.clear();
|
|
670
902
|
for (const entry of held) {
|
|
671
|
-
|
|
903
|
+
stored.add(entry.conversation);
|
|
672
904
|
if (entry.read !== undefined)
|
|
673
905
|
reads.set(entry.conversation, entry.read);
|
|
906
|
+
if (entry.partial)
|
|
907
|
+
partials.add(entry.conversation);
|
|
674
908
|
}
|
|
675
909
|
drafts.clear();
|
|
676
|
-
for (const record of
|
|
910
|
+
for (const record of kept)
|
|
677
911
|
drafts.set(record.conversation, record);
|
|
678
912
|
},
|
|
679
913
|
async reconcile() {
|
|
@@ -681,8 +915,10 @@ export function createConversationStore(deps) {
|
|
|
681
915
|
return;
|
|
682
916
|
const named = new Set(listing.keys());
|
|
683
917
|
const leaving = [
|
|
684
|
-
...new Set([...replicated, ...
|
|
685
|
-
].filter((conversation) => !named.has(conversation)
|
|
918
|
+
...new Set([...replicated, ...stored, ...reads.keys(), ...drafts.keys()]),
|
|
919
|
+
].filter((conversation) => !named.has(conversation) &&
|
|
920
|
+
!creations.has(conversation) &&
|
|
921
|
+
(!conversation.startsWith(NEW_CHAT) || retiredChat(conversation)));
|
|
686
922
|
for (const conversation of named) {
|
|
687
923
|
if (!replicated.has(conversation))
|
|
688
924
|
behind.add(conversation);
|
|
@@ -693,12 +929,25 @@ export function createConversationStore(deps) {
|
|
|
693
929
|
await remove(leaving);
|
|
694
930
|
},
|
|
695
931
|
async catchUp(progress) {
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
932
|
+
if (deps.full()) {
|
|
933
|
+
if (behind.size > 0)
|
|
934
|
+
progress.complete = false;
|
|
935
|
+
return;
|
|
936
|
+
}
|
|
937
|
+
const owed = (conversation) => behind.has(conversation) && replicated.has(conversation);
|
|
938
|
+
const read = async (conversations) => {
|
|
939
|
+
if (conversations.length === 0 || deps.closed())
|
|
940
|
+
return;
|
|
941
|
+
const failed = await caughtUp(conversations);
|
|
699
942
|
if (failed?.kind === 'transport-uncertainty')
|
|
700
943
|
progress.reachable = false;
|
|
701
|
-
}
|
|
944
|
+
};
|
|
945
|
+
const asking = [...behind].filter(owed).sort();
|
|
946
|
+
const awaited = asking.filter((conversation) => catching.has(conversation));
|
|
947
|
+
await Promise.all([
|
|
948
|
+
read(asking.filter((conversation) => !catching.has(conversation))),
|
|
949
|
+
Promise.all(awaited.map((conversation) => catching.get(conversation))).then(() => read(awaited.filter((conversation) => owed(conversation) && !catching.has(conversation)))),
|
|
950
|
+
]);
|
|
702
951
|
if (behind.size > 0)
|
|
703
952
|
progress.complete = false;
|
|
704
953
|
},
|
|
@@ -709,6 +958,10 @@ export function createConversationStore(deps) {
|
|
|
709
958
|
continue;
|
|
710
959
|
if (deps.closed())
|
|
711
960
|
return;
|
|
961
|
+
if (deps.expired(submission.operation)) {
|
|
962
|
+
await forgotten(conversation, submission);
|
|
963
|
+
continue;
|
|
964
|
+
}
|
|
712
965
|
const result = await dispatched(conversation, submission);
|
|
713
966
|
if (result.kind === 'revoked-authorization')
|
|
714
967
|
return;
|
|
@@ -716,35 +969,58 @@ export function createConversationStore(deps) {
|
|
|
716
969
|
progress.reachable = false;
|
|
717
970
|
return;
|
|
718
971
|
}
|
|
972
|
+
if (result.kind !== 'action' && result.retryable) {
|
|
973
|
+
progress.complete = false;
|
|
974
|
+
return;
|
|
975
|
+
}
|
|
719
976
|
}
|
|
720
977
|
}
|
|
721
978
|
},
|
|
722
979
|
current() {
|
|
723
980
|
return behind.size === 0;
|
|
724
981
|
},
|
|
982
|
+
known() {
|
|
983
|
+
return listKnown;
|
|
984
|
+
},
|
|
725
985
|
reconnected() {
|
|
726
986
|
for (const conversation of replicated)
|
|
727
987
|
gap(conversation);
|
|
728
988
|
},
|
|
989
|
+
reopened() {
|
|
990
|
+
sending.clear();
|
|
991
|
+
},
|
|
992
|
+
lost() {
|
|
993
|
+
catching.clear();
|
|
994
|
+
},
|
|
729
995
|
watch(conversation, options, deliver) {
|
|
730
|
-
const
|
|
731
|
-
const key = JSON.stringify([conversation, limit ?? null]);
|
|
996
|
+
const asked = askedOf(options);
|
|
997
|
+
const key = JSON.stringify([conversation, asked.limit, asked.after ?? null, asked.total]);
|
|
732
998
|
const view = views.get(key) ?? {
|
|
733
999
|
conversation,
|
|
734
|
-
|
|
1000
|
+
asked,
|
|
735
1001
|
watchers: new Set(),
|
|
736
|
-
|
|
1002
|
+
window: null,
|
|
1003
|
+
more: false,
|
|
1004
|
+
total: null,
|
|
737
1005
|
};
|
|
738
1006
|
views.set(key, view);
|
|
739
1007
|
const watcher = { notify: deliver, ready: false };
|
|
740
1008
|
view.watchers.add(watcher);
|
|
741
1009
|
void deps
|
|
742
|
-
.durably(() =>
|
|
1010
|
+
.durably(async (local) => {
|
|
1011
|
+
if (view.window !== null)
|
|
1012
|
+
return;
|
|
1013
|
+
const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before);
|
|
1014
|
+
view.window = messages;
|
|
1015
|
+
view.more = more;
|
|
1016
|
+
view.total = asked.total ? await local.messageCount(conversation) : null;
|
|
1017
|
+
})
|
|
743
1018
|
.then(() => {
|
|
744
1019
|
if (deps.closed() || !view.watchers.has(watcher))
|
|
745
1020
|
return;
|
|
746
1021
|
watcher.ready = true;
|
|
747
|
-
|
|
1022
|
+
const window = view.window ?? [];
|
|
1023
|
+
watcher.notify(viewOf(conversation, window, sharedFrame(view, window)));
|
|
748
1024
|
})
|
|
749
1025
|
.catch(() => { });
|
|
750
1026
|
return () => {
|
|
@@ -791,12 +1067,16 @@ export function createConversationStore(deps) {
|
|
|
791
1067
|
if (event.kind === 'list' || event.kind === 'listed' || event.kind === 'unlisted') {
|
|
792
1068
|
if (event.kind === 'list') {
|
|
793
1069
|
listing.clear();
|
|
794
|
-
for (const entry of event.entries)
|
|
1070
|
+
for (const entry of event.entries) {
|
|
795
1071
|
listing.set(entry.conversation, { ...entry });
|
|
1072
|
+
creations.delete(entry.conversation);
|
|
1073
|
+
}
|
|
796
1074
|
listKnown = true;
|
|
1075
|
+
openingEnded();
|
|
797
1076
|
}
|
|
798
1077
|
else if (event.kind === 'listed') {
|
|
799
1078
|
listing.set(event.entry.conversation, { ...event.entry });
|
|
1079
|
+
creations.delete(event.entry.conversation);
|
|
800
1080
|
}
|
|
801
1081
|
else {
|
|
802
1082
|
listing.delete(event.conversation);
|
|
@@ -810,16 +1090,16 @@ export function createConversationStore(deps) {
|
|
|
810
1090
|
heard(event);
|
|
811
1091
|
return false;
|
|
812
1092
|
},
|
|
813
|
-
|
|
814
|
-
return [...
|
|
1093
|
+
searchable() {
|
|
1094
|
+
return [...(listKnown ? listing.keys() : stored)].sort();
|
|
815
1095
|
},
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
return held === undefined ? null : { conversation, identity, text: held.text };
|
|
1096
|
+
acquired(conversation) {
|
|
1097
|
+
return reads.has(conversation) && !partials.has(conversation);
|
|
819
1098
|
},
|
|
820
1099
|
close() {
|
|
821
1100
|
views.clear();
|
|
822
1101
|
lives.clear();
|
|
1102
|
+
unsent.clear();
|
|
823
1103
|
listWatchers.clear();
|
|
824
1104
|
agentWatchers.clear();
|
|
825
1105
|
},
|