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