@agentshouse/mdruntime 0.1.0-c8f3163f32f93 → 0.1.0-c91a238f9703e
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 +281 -275
- package/README.md +8 -8
- package/dist/acquisition.d.ts +14 -0
- package/dist/acquisition.d.ts.map +1 -0
- package/dist/acquisition.js +24 -0
- package/dist/adapter.d.ts +32 -8
- package/dist/adapter.d.ts.map +1 -1
- package/dist/bootstrap.d.ts +2 -1
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +4 -0
- package/dist/change-set.d.ts +2 -0
- package/dist/change-set.d.ts.map +1 -1
- package/dist/change-set.js +36 -10
- package/dist/chat/earlier.d.ts +10 -0
- package/dist/chat/earlier.d.ts.map +1 -0
- package/dist/chat/earlier.js +54 -0
- package/dist/chat/kit.d.ts +2 -1
- package/dist/chat/kit.d.ts.map +1 -1
- package/dist/chat/kit.js +3 -3
- package/dist/chat/list.d.ts +2 -1
- package/dist/chat/list.d.ts.map +1 -1
- package/dist/chat/list.js +42 -5
- package/dist/chat/message.d.ts.map +1 -1
- package/dist/chat/message.js +2 -2
- package/dist/chat/messages.d.ts +1 -0
- package/dist/chat/messages.d.ts.map +1 -1
- package/dist/chat/messages.js +4 -0
- package/dist/chat/provider.d.ts +1 -1
- package/dist/chat/provider.d.ts.map +1 -1
- package/dist/chat/provider.js +37 -29
- package/dist/chat/question.d.ts +1 -1
- package/dist/chat/question.d.ts.map +1 -1
- package/dist/chat/question.js +7 -18
- package/dist/chat/ready.d.ts +1 -0
- package/dist/chat/ready.d.ts.map +1 -1
- package/dist/chat/ready.js +2 -2
- package/dist/chat/style.js +1 -1
- package/dist/client.d.ts +16 -8
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +18 -5
- package/dist/collection.d.ts +17 -3
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +230 -19
- package/dist/connection.d.ts +32 -9
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +1326 -255
- package/dist/conversation.d.ts +16 -5
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +323 -119
- package/dist/durability.d.ts +7 -1
- package/dist/durability.d.ts.map +1 -1
- package/dist/entry.d.ts +2 -2
- package/dist/entry.d.ts.map +1 -1
- package/dist/entry.js +3 -3
- package/dist/host.d.ts +16 -2
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +90 -8
- package/dist/identity.d.ts +2 -0
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +6 -0
- package/dist/index.d.ts +10 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/memory.d.ts +8 -2
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +30 -7
- package/dist/model.d.ts +4 -2
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +10 -1
- package/dist/observation.d.ts +7 -2
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +29 -12
- package/dist/outcome.d.ts +8 -2
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +3 -0
- package/dist/overlay.d.ts +2 -0
- package/dist/overlay.d.ts.map +1 -1
- package/dist/overlay.js +50 -55
- package/dist/query.d.ts +56 -4
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +36 -7
- package/dist/reach.d.ts +6 -3
- package/dist/reach.d.ts.map +1 -1
- package/dist/reach.js +29 -4
- package/dist/react.d.ts +8 -6
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +7 -17
- package/dist/read-side.d.ts +13 -6
- package/dist/read-side.d.ts.map +1 -1
- package/dist/read-side.js +419 -92
- package/dist/readiness.d.ts +53 -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 +98 -0
- package/dist/recovery.d.ts +25 -0
- package/dist/recovery.d.ts.map +1 -0
- package/dist/recovery.js +197 -0
- package/dist/refresh.d.ts +4 -4
- package/dist/refresh.d.ts.map +1 -1
- package/dist/refresh.js +17 -10
- package/dist/replica.d.ts +6 -2
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +76 -23
- package/dist/room-draft.d.ts +13 -1
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +2 -2
- package/dist/rows.d.ts +6 -13
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +10 -13
- package/dist/save.d.ts +6 -0
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +53 -14
- package/dist/search.d.ts +49 -6
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +192 -47
- 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 +11 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +368 -0
- package/dist/status.d.ts +4 -0
- package/dist/status.d.ts.map +1 -1
- package/dist/store.d.ts +36 -6
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +193 -55
- package/dist/turn.d.ts +7 -0
- package/dist/turn.d.ts.map +1 -0
- package/dist/turn.js +45 -0
- package/dist/windows.d.ts +26 -0
- package/dist/windows.d.ts.map +1 -0
- package/dist/windows.js +93 -0
- package/dist/wire.d.ts +152 -32
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +52 -31
- package/package.json +2 -2
package/dist/conversation.js
CHANGED
|
@@ -3,6 +3,7 @@ import { runtimeError } from './failure.js';
|
|
|
3
3
|
import { declaredFile } from './file.js';
|
|
4
4
|
import { changeBatchIdentity, chatDraftRevision } from './identity.js';
|
|
5
5
|
import { OFFLINE } from './outcome.js';
|
|
6
|
+
import { capped, cursorOf, decodedCursor, FROM_START, messageOrder, precedes } from './windows.js';
|
|
6
7
|
const QUIET = {
|
|
7
8
|
turn: null,
|
|
8
9
|
questions: [],
|
|
@@ -22,6 +23,17 @@ export function unheldView(conversation) {
|
|
|
22
23
|
chatDraft: null,
|
|
23
24
|
};
|
|
24
25
|
}
|
|
26
|
+
export const SESSION_STARTED = 'Session started';
|
|
27
|
+
const NEW_CHAT = 'new:';
|
|
28
|
+
export function newChat(agent) {
|
|
29
|
+
return `${NEW_CHAT}${agent}`;
|
|
30
|
+
}
|
|
31
|
+
function creationOf(input) {
|
|
32
|
+
if (typeof input !== 'object' || input === null)
|
|
33
|
+
return null;
|
|
34
|
+
const { agent_id: agent, operation_id: operation } = input;
|
|
35
|
+
return typeof agent === 'string' && typeof operation === 'string' ? { agent, operation } : null;
|
|
36
|
+
}
|
|
25
37
|
function startedTurn(turn, operation) {
|
|
26
38
|
if (operation.op === 'begin')
|
|
27
39
|
return { turn, started: operation.at, parts: [], context: null };
|
|
@@ -71,18 +83,28 @@ function appliedTurn(held, operation) {
|
|
|
71
83
|
parts: held.parts.map((kept, at) => (at === operation.index ? changed : kept)),
|
|
72
84
|
};
|
|
73
85
|
}
|
|
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) {
|
|
86
|
+
function windowOf(held, arriving, limit) {
|
|
82
87
|
const byIdentity = new Map(held.map((message) => [message.identity, message]));
|
|
83
88
|
for (const message of arriving)
|
|
84
89
|
byIdentity.set(message.identity, message);
|
|
85
|
-
|
|
90
|
+
const next = [...byIdentity.values()].sort(messageOrder);
|
|
91
|
+
const start = Math.max(0, next.length - limit);
|
|
92
|
+
return { messages: next.slice(start), dropped: start > 0 };
|
|
93
|
+
}
|
|
94
|
+
function frameOf(asked, messages, more, total) {
|
|
95
|
+
const oldest = messages[0];
|
|
96
|
+
const next = !more
|
|
97
|
+
? undefined
|
|
98
|
+
: oldest === undefined
|
|
99
|
+
? (asked.after ?? FROM_START)
|
|
100
|
+
: cursorOf([oldest.position, oldest.identity]);
|
|
101
|
+
return {
|
|
102
|
+
...(next === undefined ? {} : { next }),
|
|
103
|
+
...(total === null ? {} : { total }),
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function newestOf(arriving, from) {
|
|
107
|
+
return arriving.reduce((top, message) => (top === null || message.position > top ? message.position : top), from);
|
|
86
108
|
}
|
|
87
109
|
function acceptedMessage(value) {
|
|
88
110
|
if (typeof value !== 'object' || value === null)
|
|
@@ -141,52 +163,77 @@ function historyPages(result) {
|
|
|
141
163
|
}
|
|
142
164
|
return pages;
|
|
143
165
|
}
|
|
144
|
-
function
|
|
166
|
+
function answered(result, field) {
|
|
145
167
|
if (result.kind !== 'action')
|
|
146
168
|
return null;
|
|
147
169
|
const answer = result.result;
|
|
148
170
|
if (typeof answer !== 'object' || answer === null)
|
|
149
171
|
return null;
|
|
150
|
-
const
|
|
151
|
-
return typeof
|
|
172
|
+
const value = answer[field];
|
|
173
|
+
return typeof value === 'string' ? value : null;
|
|
174
|
+
}
|
|
175
|
+
function beforeOf(after) {
|
|
176
|
+
if (after === undefined)
|
|
177
|
+
return null;
|
|
178
|
+
const decoded = typeof after === 'string' ? decodedCursor(after) : null;
|
|
179
|
+
if (decoded?.length === 0)
|
|
180
|
+
return null;
|
|
181
|
+
const [position, identity] = decoded ?? [];
|
|
182
|
+
if (decoded?.length !== 2 || typeof position !== 'string' || typeof identity !== 'string') {
|
|
183
|
+
throw runtimeError('invalid-after', `conversation after must be the next an earlier view returned, got ${String(after)}`);
|
|
184
|
+
}
|
|
185
|
+
return [position, identity];
|
|
152
186
|
}
|
|
153
|
-
function
|
|
154
|
-
const { limit } = options;
|
|
155
|
-
if (limit
|
|
156
|
-
return undefined;
|
|
157
|
-
if (!Number.isInteger(limit) || limit < 0) {
|
|
187
|
+
function askedOf(options) {
|
|
188
|
+
const { limit, after, total } = options;
|
|
189
|
+
if (limit !== undefined && (!Number.isInteger(limit) || limit < 0)) {
|
|
158
190
|
throw runtimeError('invalid-limit', `conversation limit must be a non-negative integer, got ${String(limit)}`);
|
|
159
191
|
}
|
|
160
|
-
return limit;
|
|
192
|
+
return { limit: capped(limit), after, before: beforeOf(after), total: total === true };
|
|
161
193
|
}
|
|
194
|
+
const EXPIRED = { kind: 'rejected', code: 'expired', fields: {}, retryable: false };
|
|
162
195
|
function unwritten(draft) {
|
|
163
196
|
return draft.text === '' && draft.files.length === 0;
|
|
164
197
|
}
|
|
165
198
|
function pending(draft) {
|
|
166
|
-
return
|
|
199
|
+
return {
|
|
200
|
+
...(draft.submitted === undefined ? {} : { submitted: draft.submitted }),
|
|
201
|
+
...(draft.moves === undefined ? {} : { moves: draft.moves }),
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
function empty(draft) {
|
|
205
|
+
return unwritten(draft) && draft.submitted === undefined && draft.moves === undefined;
|
|
167
206
|
}
|
|
168
207
|
function withSubmitted(draft, submitted) {
|
|
169
208
|
const { submitted: _held, ...unsent } = draft;
|
|
170
209
|
return submitted.length === 0 ? unsent : { ...unsent, submitted };
|
|
171
210
|
}
|
|
211
|
+
function prepended(draft, said) {
|
|
212
|
+
return {
|
|
213
|
+
text: [said.text, draft.text].filter((one) => one !== '').join('\n'),
|
|
214
|
+
files: [...said.files, ...draft.files],
|
|
215
|
+
};
|
|
216
|
+
}
|
|
172
217
|
function returned(draft, submission, others) {
|
|
173
218
|
return withSubmitted({
|
|
174
219
|
conversation: draft.conversation,
|
|
175
|
-
|
|
176
|
-
files: [...submission.files, ...draft.files],
|
|
220
|
+
...prepended(draft, submission),
|
|
177
221
|
revision: chatDraftRevision(),
|
|
178
222
|
}, others);
|
|
179
223
|
}
|
|
180
224
|
export function createConversationStore(deps) {
|
|
181
|
-
const
|
|
225
|
+
const stored = new Set();
|
|
182
226
|
const reads = new Map();
|
|
227
|
+
const partials = new Set();
|
|
183
228
|
const drafts = new Map();
|
|
184
|
-
const sending = new
|
|
229
|
+
const sending = new Map();
|
|
185
230
|
const behind = new Set();
|
|
231
|
+
const catching = new Map();
|
|
186
232
|
const gaps = new Map();
|
|
187
233
|
const lives = new Map();
|
|
188
234
|
const views = new Map();
|
|
189
235
|
const listing = new Map();
|
|
236
|
+
const creations = new Set();
|
|
190
237
|
const listWatchers = new Set();
|
|
191
238
|
let listKnown = false;
|
|
192
239
|
const agentWatchers = new Set();
|
|
@@ -198,22 +245,18 @@ export function createConversationStore(deps) {
|
|
|
198
245
|
function liveOf(conversation) {
|
|
199
246
|
return lives.get(conversation) ?? QUIET;
|
|
200
247
|
}
|
|
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)) {
|
|
248
|
+
function viewOf(conversation, held, frame) {
|
|
212
249
|
const draft = drafts.get(conversation);
|
|
213
250
|
const facts = liveOf(conversation);
|
|
214
251
|
return {
|
|
215
252
|
conversation,
|
|
216
|
-
messages:
|
|
253
|
+
messages: held.map(({ identity, turn, text, message }) => ({
|
|
254
|
+
identity,
|
|
255
|
+
turn,
|
|
256
|
+
text,
|
|
257
|
+
message,
|
|
258
|
+
})),
|
|
259
|
+
...frame,
|
|
217
260
|
turn: structuredClone(facts.turn),
|
|
218
261
|
questions: structuredClone(facts.questions),
|
|
219
262
|
process: facts.process === null ? null : { ...facts.process },
|
|
@@ -224,23 +267,48 @@ export function createConversationStore(deps) {
|
|
|
224
267
|
: { text: draft.text, files: [...draft.files], revision: draft.revision },
|
|
225
268
|
};
|
|
226
269
|
}
|
|
227
|
-
function emit(conversation
|
|
270
|
+
function emit(conversation) {
|
|
228
271
|
if (deps.closed())
|
|
229
272
|
return;
|
|
230
273
|
for (const view of views.values()) {
|
|
231
|
-
if (view.conversation !== conversation)
|
|
274
|
+
if (view.conversation !== conversation || view.window === null)
|
|
232
275
|
continue;
|
|
233
|
-
if (messagesChanged)
|
|
234
|
-
view.messages = null;
|
|
235
276
|
const ready = [...view.watchers].filter((watcher) => watcher.ready);
|
|
236
277
|
if (ready.length === 0)
|
|
237
278
|
continue;
|
|
238
|
-
|
|
239
|
-
const value = viewOf(conversation, view.limit, view.messages);
|
|
279
|
+
const value = viewOf(conversation, view.window, sharedFrame(view, view.window));
|
|
240
280
|
for (const watcher of ready)
|
|
241
281
|
watcher.notify(value);
|
|
242
282
|
}
|
|
243
283
|
}
|
|
284
|
+
function sharedFrame(view, window) {
|
|
285
|
+
return frameOf(view.asked, window, view.more, view.total);
|
|
286
|
+
}
|
|
287
|
+
async function slide(local, conversation, arriving) {
|
|
288
|
+
let count;
|
|
289
|
+
for (const view of views.values()) {
|
|
290
|
+
if (view.conversation !== conversation || view.window === null)
|
|
291
|
+
continue;
|
|
292
|
+
if (arriving === null) {
|
|
293
|
+
view.window = [];
|
|
294
|
+
view.more = false;
|
|
295
|
+
if (view.total !== null)
|
|
296
|
+
view.total = 0;
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
const joining = arriving.filter((message) => precedes(message, view.asked.before));
|
|
300
|
+
const next = windowOf(view.window, joining, view.asked.limit);
|
|
301
|
+
view.window = next.messages;
|
|
302
|
+
view.more ||= next.dropped;
|
|
303
|
+
if (view.total !== null && arriving.length > 0) {
|
|
304
|
+
count ??= await local.messageCount(conversation);
|
|
305
|
+
view.total = count;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
function changesOf(conversation, arriving) {
|
|
310
|
+
return arriving.map(({ identity, text }) => ({ conversation, identity, text }));
|
|
311
|
+
}
|
|
244
312
|
function stateOf(conversation) {
|
|
245
313
|
const facts = liveOf(conversation);
|
|
246
314
|
if (facts.questions.length > 0)
|
|
@@ -297,7 +365,10 @@ export function createConversationStore(deps) {
|
|
|
297
365
|
const declaration = deps.agents();
|
|
298
366
|
if (declaration === undefined)
|
|
299
367
|
return;
|
|
300
|
-
const
|
|
368
|
+
const held = await deps.documents(declaration);
|
|
369
|
+
if ('pending' in held)
|
|
370
|
+
return;
|
|
371
|
+
const { documents } = held;
|
|
301
372
|
const names = new Map();
|
|
302
373
|
const retired = new Set();
|
|
303
374
|
for (const document of documents) {
|
|
@@ -320,6 +391,12 @@ export function createConversationStore(deps) {
|
|
|
320
391
|
if (named)
|
|
321
392
|
emitList();
|
|
322
393
|
emitAgents();
|
|
394
|
+
const ended = [...drafts.keys()].filter(retiredChat);
|
|
395
|
+
if (ended.length > 0)
|
|
396
|
+
await remove(ended);
|
|
397
|
+
}
|
|
398
|
+
function retiredChat(conversation) {
|
|
399
|
+
return (conversation.startsWith(NEW_CHAT) && retiredAgents.has(conversation.slice(NEW_CHAT.length)));
|
|
323
400
|
}
|
|
324
401
|
function agentsReady() {
|
|
325
402
|
agentsRead ??= readAgents().catch(() => {
|
|
@@ -329,7 +406,7 @@ export function createConversationStore(deps) {
|
|
|
329
406
|
}
|
|
330
407
|
function live(conversation, change) {
|
|
331
408
|
setLive(conversation, { ...liveOf(conversation), ...change });
|
|
332
|
-
emit(conversation
|
|
409
|
+
emit(conversation);
|
|
333
410
|
}
|
|
334
411
|
function relayed(conversation, turn, operation) {
|
|
335
412
|
const held = liveOf(conversation).turn;
|
|
@@ -357,21 +434,25 @@ export function createConversationStore(deps) {
|
|
|
357
434
|
}
|
|
358
435
|
async function remove(leaving) {
|
|
359
436
|
await deps.durably(async (local) => {
|
|
360
|
-
await local.dropConversations(leaving);
|
|
361
437
|
const removed = [];
|
|
362
|
-
const changed = [];
|
|
363
438
|
for (const conversation of leaving) {
|
|
364
|
-
for (const
|
|
365
|
-
removed.push({ conversation, identity:
|
|
439
|
+
for (const identity of await local.messageIdentities(conversation)) {
|
|
440
|
+
removed.push({ conversation, identity, text: null });
|
|
366
441
|
}
|
|
367
|
-
|
|
442
|
+
}
|
|
443
|
+
await local.dropConversations(leaving);
|
|
444
|
+
const changed = [];
|
|
445
|
+
for (const conversation of leaving) {
|
|
446
|
+
if (stored.has(conversation) || drafts.has(conversation) || lives.has(conversation)) {
|
|
368
447
|
changed.push(conversation);
|
|
369
448
|
}
|
|
370
|
-
|
|
449
|
+
stored.delete(conversation);
|
|
371
450
|
reads.delete(conversation);
|
|
451
|
+
partials.delete(conversation);
|
|
372
452
|
drafts.delete(conversation);
|
|
373
453
|
behind.delete(conversation);
|
|
374
454
|
setLive(conversation, null);
|
|
455
|
+
await slide(local, conversation, null);
|
|
375
456
|
}
|
|
376
457
|
if (changed.length > 0)
|
|
377
458
|
deps.publish(conversationChangeSet(removed, changed));
|
|
@@ -393,23 +474,21 @@ export function createConversationStore(deps) {
|
|
|
393
474
|
await local.keepMessages({
|
|
394
475
|
conversation,
|
|
395
476
|
messages: [message],
|
|
396
|
-
...(advances ? { read: message.position } : {}),
|
|
477
|
+
...(advances ? { read: { at: message.position, partial: false } } : {}),
|
|
397
478
|
});
|
|
398
|
-
|
|
479
|
+
stored.add(conversation);
|
|
399
480
|
if (advances)
|
|
400
481
|
reads.set(conversation, message.position);
|
|
401
482
|
sealTurns(conversation, new Set([message.turn]));
|
|
402
|
-
|
|
483
|
+
await slide(local, conversation, [message]);
|
|
484
|
+
deps.publish(conversationChangeSet(changesOf(conversation, [message]), [conversation]));
|
|
403
485
|
});
|
|
404
486
|
}
|
|
405
|
-
async function readHistories(conversations) {
|
|
487
|
+
async function readHistories(conversations, ended) {
|
|
406
488
|
const generations = new Map(conversations.map((one) => [one, gaps.get(one) ?? 0]));
|
|
407
|
-
const starts = new Map(conversations.map((one) => [one, reads.get(one)]));
|
|
408
|
-
const arriving = new Map();
|
|
409
|
-
const complete = new Set();
|
|
410
489
|
let asking = conversations.map((conversation) => ({
|
|
411
490
|
conversation,
|
|
412
|
-
after:
|
|
491
|
+
after: reads.get(conversation) ?? undefined,
|
|
413
492
|
}));
|
|
414
493
|
while (asking.length > 0) {
|
|
415
494
|
const result = await deps.call('conversation.history', {
|
|
@@ -421,43 +500,64 @@ export function createConversationStore(deps) {
|
|
|
421
500
|
const pages = historyPages(result);
|
|
422
501
|
if (pages === null)
|
|
423
502
|
return result;
|
|
424
|
-
const asked = new
|
|
503
|
+
const asked = new Map(asking.map(({ conversation, after }) => [conversation, after]));
|
|
425
504
|
const following = [];
|
|
426
505
|
for (const page of pages) {
|
|
427
506
|
if (!asked.has(page.conversation))
|
|
428
507
|
continue;
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
...page.messages,
|
|
432
|
-
]);
|
|
433
|
-
if (page.next === undefined)
|
|
434
|
-
complete.add(page.conversation);
|
|
435
|
-
else
|
|
508
|
+
await keepPage(page.conversation, page.messages, asked.get(page.conversation) ?? null, page.next, generations.get(page.conversation) ?? 0);
|
|
509
|
+
if (page.next !== undefined) {
|
|
436
510
|
following.push({ conversation: page.conversation, after: page.next });
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
for (const { conversation } of asking) {
|
|
514
|
+
if (!following.some((next) => next.conversation === conversation))
|
|
515
|
+
ended(conversation);
|
|
437
516
|
}
|
|
438
517
|
asking = following;
|
|
439
518
|
}
|
|
440
|
-
for (const [conversation, held] of arriving) {
|
|
441
|
-
if (!complete.has(conversation))
|
|
442
|
-
continue;
|
|
443
|
-
await keepHistory(conversation, held, starts.get(conversation), generations.get(conversation) ?? 0);
|
|
444
|
-
}
|
|
445
519
|
return null;
|
|
446
520
|
}
|
|
447
|
-
function
|
|
521
|
+
function caughtUp(conversations) {
|
|
522
|
+
const ends = new Map();
|
|
523
|
+
for (const conversation of conversations) {
|
|
524
|
+
const held = new Promise((end) => ends.set(conversation, end));
|
|
525
|
+
catching.set(conversation, held);
|
|
526
|
+
void held.then(() => {
|
|
527
|
+
if (catching.get(conversation) === held)
|
|
528
|
+
catching.delete(conversation);
|
|
529
|
+
});
|
|
530
|
+
}
|
|
531
|
+
const ended = (conversation) => ends.get(conversation)?.();
|
|
532
|
+
const read = readHistories(conversations, ended);
|
|
533
|
+
const endAll = () => {
|
|
534
|
+
for (const conversation of conversations)
|
|
535
|
+
ended(conversation);
|
|
536
|
+
};
|
|
537
|
+
read.then(endAll, endAll);
|
|
538
|
+
return read;
|
|
539
|
+
}
|
|
540
|
+
function keepPage(conversation, arriving, from, next, generation) {
|
|
448
541
|
return deps.durably(async (local) => {
|
|
449
542
|
if (!replicated.has(conversation))
|
|
450
543
|
return;
|
|
451
|
-
const
|
|
544
|
+
const whole = next === undefined;
|
|
545
|
+
const cursor = next ?? newestOf(arriving, from);
|
|
546
|
+
const partial = !whole && (partials.has(conversation) || !reads.has(conversation));
|
|
452
547
|
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,
|
|
548
|
+
await local.keepMessages({ conversation, messages: arriving, read: { at: cursor, partial } });
|
|
549
|
+
stored.add(conversation);
|
|
550
|
+
reads.set(conversation, cursor);
|
|
551
|
+
if (partial)
|
|
552
|
+
partials.add(conversation);
|
|
553
|
+
else
|
|
554
|
+
partials.delete(conversation);
|
|
456
555
|
sealTurns(conversation, new Set(arriving.map((message) => message.turn)));
|
|
457
|
-
if ((gaps.get(conversation) ?? 0) === generation)
|
|
556
|
+
if (whole && (gaps.get(conversation) ?? 0) === generation)
|
|
458
557
|
behind.delete(conversation);
|
|
459
|
-
|
|
460
|
-
|
|
558
|
+
await slide(local, conversation, arriving);
|
|
559
|
+
if (arriving.length > 0 || sealing || whole) {
|
|
560
|
+
deps.publish(conversationChangeSet(changesOf(conversation, arriving), [conversation]));
|
|
461
561
|
}
|
|
462
562
|
});
|
|
463
563
|
}
|
|
@@ -470,7 +570,7 @@ export function createConversationStore(deps) {
|
|
|
470
570
|
if (held === undefined)
|
|
471
571
|
return;
|
|
472
572
|
setLive(conversation, { ...QUIET, turn: held.turn });
|
|
473
|
-
emit(conversation
|
|
573
|
+
emit(conversation);
|
|
474
574
|
return;
|
|
475
575
|
}
|
|
476
576
|
if (event.kind === 'turn') {
|
|
@@ -504,12 +604,12 @@ export function createConversationStore(deps) {
|
|
|
504
604
|
emit(conversation);
|
|
505
605
|
}
|
|
506
606
|
async function kept(local, conversation, following) {
|
|
507
|
-
if (following === null || (
|
|
607
|
+
if (following === null || empty(following)) {
|
|
508
608
|
await local.dropChatDraft(conversation);
|
|
509
609
|
drafts.delete(conversation);
|
|
510
610
|
}
|
|
511
611
|
else {
|
|
512
|
-
await local.
|
|
612
|
+
await local.keepChatDrafts([following]);
|
|
513
613
|
drafts.set(conversation, following);
|
|
514
614
|
}
|
|
515
615
|
}
|
|
@@ -522,9 +622,32 @@ export function createConversationStore(deps) {
|
|
|
522
622
|
await kept(local, conversation, withSubmitted(held, left));
|
|
523
623
|
return true;
|
|
524
624
|
}
|
|
625
|
+
function moved(agent, operation, to) {
|
|
626
|
+
const from = newChat(agent);
|
|
627
|
+
return deps.durably(async (local) => {
|
|
628
|
+
const held = drafts.get(from);
|
|
629
|
+
const earlier = (held?.moves ?? []).filter((one) => !deps.expired(one));
|
|
630
|
+
if (earlier.includes(operation))
|
|
631
|
+
return;
|
|
632
|
+
const blank = {
|
|
633
|
+
conversation: from,
|
|
634
|
+
text: '',
|
|
635
|
+
files: [],
|
|
636
|
+
revision: chatDraftRevision(),
|
|
637
|
+
};
|
|
638
|
+
const { moves: _answered, ...unsent } = held ?? blank;
|
|
639
|
+
const taken = drafts.has(to) || empty(unsent) ? [] : [{ ...unsent, conversation: to }];
|
|
640
|
+
const left = { ...(taken.length === 0 ? unsent : blank), moves: [...earlier, operation] };
|
|
641
|
+
await local.keepChatDrafts([left, ...taken]);
|
|
642
|
+
drafts.set(from, left);
|
|
643
|
+
for (const one of taken)
|
|
644
|
+
drafts.set(to, one);
|
|
645
|
+
deps.publish(conversationChangeSet([], [from, to]));
|
|
646
|
+
});
|
|
647
|
+
}
|
|
525
648
|
function withDraft(conversation, next) {
|
|
526
649
|
return deps.durably(async (local) => {
|
|
527
|
-
const following = next(drafts.get(conversation));
|
|
650
|
+
const following = await next(drafts.get(conversation), local);
|
|
528
651
|
if (following === undefined)
|
|
529
652
|
return;
|
|
530
653
|
await kept(local, conversation, following);
|
|
@@ -533,8 +656,10 @@ export function createConversationStore(deps) {
|
|
|
533
656
|
}
|
|
534
657
|
async function submitted(conversation, operation, draft) {
|
|
535
658
|
let submission;
|
|
659
|
+
let earlier = false;
|
|
536
660
|
await withDraft(conversation, (current) => {
|
|
537
661
|
submission = current?.submitted?.find((one) => one.operation === operation);
|
|
662
|
+
earlier = submission !== undefined;
|
|
538
663
|
if (submission !== undefined)
|
|
539
664
|
return undefined;
|
|
540
665
|
const said = draft ?? current;
|
|
@@ -552,10 +677,19 @@ export function createConversationStore(deps) {
|
|
|
552
677
|
submitted: [...(current?.submitted ?? []), submission],
|
|
553
678
|
};
|
|
554
679
|
});
|
|
555
|
-
return submission;
|
|
680
|
+
return { submission: submission, earlier };
|
|
681
|
+
}
|
|
682
|
+
function forgotten(conversation, submission) {
|
|
683
|
+
return withDraft(conversation, (current) => {
|
|
684
|
+
const held = current?.submitted ?? [];
|
|
685
|
+
if (current === undefined || !held.includes(submission))
|
|
686
|
+
return undefined;
|
|
687
|
+
return withSubmitted(current, held.filter((one) => one !== submission));
|
|
688
|
+
});
|
|
556
689
|
}
|
|
557
690
|
async function dispatched(conversation, submission) {
|
|
558
|
-
|
|
691
|
+
const dispatch = {};
|
|
692
|
+
sending.set(submission.operation, dispatch);
|
|
559
693
|
try {
|
|
560
694
|
const files = await Promise.all(submission.files.map(async (file) => ({ ...(await declaredFile(file)), blob: file.blob })));
|
|
561
695
|
const result = await invoke('conversation.send', conversation, {
|
|
@@ -565,17 +699,19 @@ export function createConversationStore(deps) {
|
|
|
565
699
|
});
|
|
566
700
|
if (result.kind === 'revoked-authorization')
|
|
567
701
|
return result;
|
|
568
|
-
if (result.kind !== 'action' && result.retryable)
|
|
702
|
+
if (result.kind !== 'action' && result.retryable) {
|
|
703
|
+
deps.unanswered();
|
|
569
704
|
return result;
|
|
570
|
-
|
|
571
|
-
|
|
705
|
+
}
|
|
706
|
+
const turn = answered(result, 'turn');
|
|
707
|
+
await withDraft(conversation, async (current, local) => {
|
|
572
708
|
const held = current?.submitted?.find((one) => one.operation === submission.operation);
|
|
573
709
|
if (current === undefined || held === undefined || held.turn !== null)
|
|
574
710
|
return undefined;
|
|
575
711
|
const others = current.submitted.filter((one) => one !== held);
|
|
576
712
|
if (result.kind !== 'action')
|
|
577
713
|
return returned(current, held, others);
|
|
578
|
-
const arrived =
|
|
714
|
+
const arrived = turn !== null && (await local.turnArrived(conversation, turn));
|
|
579
715
|
return withSubmitted(current, turn === null || arrived
|
|
580
716
|
? others
|
|
581
717
|
: current.submitted.map((one) => (one === held ? { ...held, turn } : one)));
|
|
@@ -583,7 +719,8 @@ export function createConversationStore(deps) {
|
|
|
583
719
|
return result;
|
|
584
720
|
}
|
|
585
721
|
finally {
|
|
586
|
-
sending.
|
|
722
|
+
if (sending.get(submission.operation) === dispatch)
|
|
723
|
+
sending.delete(submission.operation);
|
|
587
724
|
}
|
|
588
725
|
}
|
|
589
726
|
function invoke(name, conversation, extra = {}) {
|
|
@@ -595,15 +732,32 @@ export function createConversationStore(deps) {
|
|
|
595
732
|
return entries();
|
|
596
733
|
},
|
|
597
734
|
async read(conversation, options = {}) {
|
|
598
|
-
const
|
|
599
|
-
return deps.durably(() =>
|
|
735
|
+
const asked = askedOf(options);
|
|
736
|
+
return deps.durably(async (local) => {
|
|
737
|
+
const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before);
|
|
738
|
+
const total = asked.total ? await local.messageCount(conversation) : null;
|
|
739
|
+
return structuredClone(viewOf(conversation, messages, frameOf(asked, messages, more, total)));
|
|
740
|
+
});
|
|
600
741
|
},
|
|
601
|
-
create(input) {
|
|
602
|
-
|
|
742
|
+
async create(input) {
|
|
743
|
+
const result = await deps.call('conversation.create', input);
|
|
744
|
+
const conversation = answered(result, 'conversation');
|
|
745
|
+
if (conversation === null)
|
|
746
|
+
return result;
|
|
747
|
+
if (!listing.has(conversation))
|
|
748
|
+
creations.add(conversation);
|
|
749
|
+
const creation = creationOf(input);
|
|
750
|
+
if (creation !== null)
|
|
751
|
+
await moved(creation.agent, creation.operation, conversation);
|
|
752
|
+
return result;
|
|
603
753
|
},
|
|
604
754
|
async send(conversation, operation = changeBatchIdentity(), draft) {
|
|
605
755
|
if (deps.online()) {
|
|
606
|
-
|
|
756
|
+
const { submission, earlier } = await submitted(conversation, operation, draft);
|
|
757
|
+
if (!earlier || !deps.expired(operation))
|
|
758
|
+
return dispatched(conversation, submission);
|
|
759
|
+
await forgotten(conversation, submission);
|
|
760
|
+
return EXPIRED;
|
|
607
761
|
}
|
|
608
762
|
if (draft !== undefined) {
|
|
609
763
|
await withDraft(conversation, (current) => current?.submitted?.some((one) => one.operation === operation) === true
|
|
@@ -644,7 +798,17 @@ export function createConversationStore(deps) {
|
|
|
644
798
|
text: draft.text,
|
|
645
799
|
files: [...draft.files],
|
|
646
800
|
revision,
|
|
647
|
-
...(current
|
|
801
|
+
...(current === undefined ? {} : pending(current)),
|
|
802
|
+
}));
|
|
803
|
+
},
|
|
804
|
+
prepend(conversation, draft) {
|
|
805
|
+
return withDraft(conversation, (current) => ({
|
|
806
|
+
conversation,
|
|
807
|
+
...(current === undefined
|
|
808
|
+
? { text: draft.text, files: [...draft.files] }
|
|
809
|
+
: prepended(current, draft)),
|
|
810
|
+
revision: chatDraftRevision(),
|
|
811
|
+
...(current === undefined ? {} : pending(current)),
|
|
648
812
|
}));
|
|
649
813
|
},
|
|
650
814
|
discard(conversation) {
|
|
@@ -662,18 +826,19 @@ export function createConversationStore(deps) {
|
|
|
662
826
|
return {
|
|
663
827
|
surface,
|
|
664
828
|
chatDraft,
|
|
665
|
-
hydrate(held,
|
|
666
|
-
|
|
829
|
+
hydrate(held, kept) {
|
|
830
|
+
stored.clear();
|
|
667
831
|
reads.clear();
|
|
668
|
-
|
|
669
|
-
view.messages = null;
|
|
832
|
+
partials.clear();
|
|
670
833
|
for (const entry of held) {
|
|
671
|
-
|
|
834
|
+
stored.add(entry.conversation);
|
|
672
835
|
if (entry.read !== undefined)
|
|
673
836
|
reads.set(entry.conversation, entry.read);
|
|
837
|
+
if (entry.partial)
|
|
838
|
+
partials.add(entry.conversation);
|
|
674
839
|
}
|
|
675
840
|
drafts.clear();
|
|
676
|
-
for (const record of
|
|
841
|
+
for (const record of kept)
|
|
677
842
|
drafts.set(record.conversation, record);
|
|
678
843
|
},
|
|
679
844
|
async reconcile() {
|
|
@@ -681,8 +846,10 @@ export function createConversationStore(deps) {
|
|
|
681
846
|
return;
|
|
682
847
|
const named = new Set(listing.keys());
|
|
683
848
|
const leaving = [
|
|
684
|
-
...new Set([...replicated, ...
|
|
685
|
-
].filter((conversation) => !named.has(conversation)
|
|
849
|
+
...new Set([...replicated, ...stored, ...reads.keys(), ...drafts.keys()]),
|
|
850
|
+
].filter((conversation) => !named.has(conversation) &&
|
|
851
|
+
!creations.has(conversation) &&
|
|
852
|
+
(!conversation.startsWith(NEW_CHAT) || retiredChat(conversation)));
|
|
686
853
|
for (const conversation of named) {
|
|
687
854
|
if (!replicated.has(conversation))
|
|
688
855
|
behind.add(conversation);
|
|
@@ -693,12 +860,20 @@ export function createConversationStore(deps) {
|
|
|
693
860
|
await remove(leaving);
|
|
694
861
|
},
|
|
695
862
|
async catchUp(progress) {
|
|
696
|
-
const
|
|
697
|
-
|
|
698
|
-
|
|
863
|
+
const owed = (conversation) => behind.has(conversation) && replicated.has(conversation);
|
|
864
|
+
const read = async (conversations) => {
|
|
865
|
+
if (conversations.length === 0 || deps.closed())
|
|
866
|
+
return;
|
|
867
|
+
const failed = await caughtUp(conversations);
|
|
699
868
|
if (failed?.kind === 'transport-uncertainty')
|
|
700
869
|
progress.reachable = false;
|
|
701
|
-
}
|
|
870
|
+
};
|
|
871
|
+
const asking = [...behind].filter(owed).sort();
|
|
872
|
+
const awaited = asking.filter((conversation) => catching.has(conversation));
|
|
873
|
+
await Promise.all([
|
|
874
|
+
read(asking.filter((conversation) => !catching.has(conversation))),
|
|
875
|
+
Promise.all(awaited.map((conversation) => catching.get(conversation))).then(() => read(awaited.filter((conversation) => owed(conversation) && !catching.has(conversation)))),
|
|
876
|
+
]);
|
|
702
877
|
if (behind.size > 0)
|
|
703
878
|
progress.complete = false;
|
|
704
879
|
},
|
|
@@ -709,6 +884,10 @@ export function createConversationStore(deps) {
|
|
|
709
884
|
continue;
|
|
710
885
|
if (deps.closed())
|
|
711
886
|
return;
|
|
887
|
+
if (deps.expired(submission.operation)) {
|
|
888
|
+
await forgotten(conversation, submission);
|
|
889
|
+
continue;
|
|
890
|
+
}
|
|
712
891
|
const result = await dispatched(conversation, submission);
|
|
713
892
|
if (result.kind === 'revoked-authorization')
|
|
714
893
|
return;
|
|
@@ -716,35 +895,58 @@ export function createConversationStore(deps) {
|
|
|
716
895
|
progress.reachable = false;
|
|
717
896
|
return;
|
|
718
897
|
}
|
|
898
|
+
if (result.kind !== 'action' && result.retryable) {
|
|
899
|
+
progress.complete = false;
|
|
900
|
+
return;
|
|
901
|
+
}
|
|
719
902
|
}
|
|
720
903
|
}
|
|
721
904
|
},
|
|
722
905
|
current() {
|
|
723
906
|
return behind.size === 0;
|
|
724
907
|
},
|
|
908
|
+
known() {
|
|
909
|
+
return listKnown;
|
|
910
|
+
},
|
|
725
911
|
reconnected() {
|
|
726
912
|
for (const conversation of replicated)
|
|
727
913
|
gap(conversation);
|
|
728
914
|
},
|
|
915
|
+
reopened() {
|
|
916
|
+
sending.clear();
|
|
917
|
+
},
|
|
918
|
+
lost() {
|
|
919
|
+
catching.clear();
|
|
920
|
+
},
|
|
729
921
|
watch(conversation, options, deliver) {
|
|
730
|
-
const
|
|
731
|
-
const key = JSON.stringify([conversation, limit ?? null]);
|
|
922
|
+
const asked = askedOf(options);
|
|
923
|
+
const key = JSON.stringify([conversation, asked.limit, asked.after ?? null, asked.total]);
|
|
732
924
|
const view = views.get(key) ?? {
|
|
733
925
|
conversation,
|
|
734
|
-
|
|
926
|
+
asked,
|
|
735
927
|
watchers: new Set(),
|
|
736
|
-
|
|
928
|
+
window: null,
|
|
929
|
+
more: false,
|
|
930
|
+
total: null,
|
|
737
931
|
};
|
|
738
932
|
views.set(key, view);
|
|
739
933
|
const watcher = { notify: deliver, ready: false };
|
|
740
934
|
view.watchers.add(watcher);
|
|
741
935
|
void deps
|
|
742
|
-
.durably(() =>
|
|
936
|
+
.durably(async (local) => {
|
|
937
|
+
if (view.window !== null)
|
|
938
|
+
return;
|
|
939
|
+
const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before);
|
|
940
|
+
view.window = messages;
|
|
941
|
+
view.more = more;
|
|
942
|
+
view.total = asked.total ? await local.messageCount(conversation) : null;
|
|
943
|
+
})
|
|
743
944
|
.then(() => {
|
|
744
945
|
if (deps.closed() || !view.watchers.has(watcher))
|
|
745
946
|
return;
|
|
746
947
|
watcher.ready = true;
|
|
747
|
-
|
|
948
|
+
const window = view.window ?? [];
|
|
949
|
+
watcher.notify(viewOf(conversation, window, sharedFrame(view, window)));
|
|
748
950
|
})
|
|
749
951
|
.catch(() => { });
|
|
750
952
|
return () => {
|
|
@@ -791,12 +993,15 @@ export function createConversationStore(deps) {
|
|
|
791
993
|
if (event.kind === 'list' || event.kind === 'listed' || event.kind === 'unlisted') {
|
|
792
994
|
if (event.kind === 'list') {
|
|
793
995
|
listing.clear();
|
|
794
|
-
for (const entry of event.entries)
|
|
996
|
+
for (const entry of event.entries) {
|
|
795
997
|
listing.set(entry.conversation, { ...entry });
|
|
998
|
+
creations.delete(entry.conversation);
|
|
999
|
+
}
|
|
796
1000
|
listKnown = true;
|
|
797
1001
|
}
|
|
798
1002
|
else if (event.kind === 'listed') {
|
|
799
1003
|
listing.set(event.entry.conversation, { ...event.entry });
|
|
1004
|
+
creations.delete(event.entry.conversation);
|
|
800
1005
|
}
|
|
801
1006
|
else {
|
|
802
1007
|
listing.delete(event.conversation);
|
|
@@ -810,12 +1015,11 @@ export function createConversationStore(deps) {
|
|
|
810
1015
|
heard(event);
|
|
811
1016
|
return false;
|
|
812
1017
|
},
|
|
813
|
-
|
|
814
|
-
return [...
|
|
1018
|
+
searchable() {
|
|
1019
|
+
return [...(listKnown ? listing.keys() : stored)].sort();
|
|
815
1020
|
},
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
return held === undefined ? null : { conversation, identity, text: held.text };
|
|
1021
|
+
acquired(conversation) {
|
|
1022
|
+
return reads.has(conversation) && !partials.has(conversation);
|
|
819
1023
|
},
|
|
820
1024
|
close() {
|
|
821
1025
|
views.clear();
|