@agentshouse/mdruntime 0.1.0-alpha.4 → 0.1.0-alpha.40
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 +191 -0
- package/README.md +44 -0
- package/dist/adapter.d.ts +174 -18
- package/dist/adapter.d.ts.map +1 -1
- package/dist/adapter.js +2 -1
- package/dist/adapter.js.map +1 -1
- package/dist/bootstrap.d.ts +7 -7
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +14 -9
- package/dist/bootstrap.js.map +1 -1
- package/dist/change-set.d.ts +33 -0
- package/dist/change-set.d.ts.map +1 -0
- package/dist/change-set.js +89 -0
- package/dist/change-set.js.map +1 -0
- package/dist/change.d.ts +10 -0
- package/dist/change.d.ts.map +1 -1
- package/dist/change.js.map +1 -1
- package/dist/client.d.ts +74 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +232 -0
- package/dist/client.js.map +1 -0
- package/dist/collection.d.ts +25 -12
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +113 -31
- package/dist/collection.js.map +1 -1
- package/dist/connection.d.ts +84 -27
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +1685 -336
- package/dist/connection.js.map +1 -1
- package/dist/conversation.d.ts +79 -0
- package/dist/conversation.d.ts.map +1 -0
- package/dist/conversation.js +593 -0
- package/dist/conversation.js.map +1 -0
- package/dist/durability.d.ts +9 -0
- package/dist/durability.d.ts.map +1 -0
- package/dist/durability.js +11 -0
- package/dist/durability.js.map +1 -0
- package/dist/file.d.ts +20 -0
- package/dist/file.d.ts.map +1 -0
- package/dist/file.js +149 -0
- package/dist/file.js.map +1 -0
- package/dist/host.d.ts +14 -0
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +211 -0
- package/dist/host.js.map +1 -0
- package/dist/identity.d.ts +3 -0
- package/dist/identity.d.ts.map +1 -0
- package/dist/identity.js +23 -0
- package/dist/identity.js.map +1 -0
- package/dist/index.d.ts +15 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/memory.d.ts +17 -2
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +201 -15
- package/dist/memory.js.map +1 -1
- package/dist/model.d.ts +55 -0
- package/dist/model.d.ts.map +1 -0
- package/dist/model.js +36 -0
- package/dist/model.js.map +1 -0
- package/dist/observation.d.ts +25 -0
- package/dist/observation.d.ts.map +1 -0
- package/dist/observation.js +92 -0
- package/dist/observation.js.map +1 -0
- package/dist/outcome.d.ts +22 -18
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +1 -11
- package/dist/outcome.js.map +1 -1
- package/dist/overlay.d.ts +2 -0
- package/dist/overlay.d.ts.map +1 -1
- package/dist/overlay.js +107 -1
- package/dist/overlay.js.map +1 -1
- package/dist/paths.d.ts +0 -1
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +0 -4
- package/dist/paths.js.map +1 -1
- package/dist/query.d.ts +209 -0
- package/dist/query.d.ts.map +1 -0
- package/dist/query.js +467 -0
- package/dist/query.js.map +1 -0
- package/dist/read-side.d.ts +14 -0
- package/dist/read-side.d.ts.map +1 -0
- package/dist/read-side.js +451 -0
- package/dist/read-side.js.map +1 -0
- package/dist/refresh.d.ts +3 -3
- package/dist/refresh.d.ts.map +1 -1
- package/dist/refresh.js +10 -7
- package/dist/refresh.js.map +1 -1
- package/dist/replica.d.ts +23 -4
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +224 -13
- package/dist/replica.js.map +1 -1
- package/dist/room-draft.d.ts +124 -0
- package/dist/room-draft.d.ts.map +1 -0
- package/dist/room-draft.js +406 -0
- package/dist/room-draft.js.map +1 -0
- package/dist/rows.d.ts +39 -0
- package/dist/rows.d.ts.map +1 -0
- package/dist/rows.js +100 -0
- package/dist/rows.js.map +1 -0
- package/dist/save.d.ts +11 -2
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +116 -128
- package/dist/save.js.map +1 -1
- package/dist/search.d.ts +61 -0
- package/dist/search.d.ts.map +1 -0
- package/dist/search.js +155 -0
- package/dist/search.js.map +1 -0
- package/dist/status.d.ts +9 -2
- package/dist/status.d.ts.map +1 -1
- package/dist/status.js +5 -4
- package/dist/status.js.map +1 -1
- package/dist/store.d.ts +89 -18
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +445 -57
- package/dist/store.js.map +1 -1
- package/dist/transfer.d.ts +5 -0
- package/dist/transfer.d.ts.map +1 -1
- package/dist/transfer.js.map +1 -1
- package/dist/wire.d.ts +64 -0
- package/dist/wire.d.ts.map +1 -0
- package/dist/wire.js +144 -0
- package/dist/wire.js.map +1 -0
- package/package.json +15 -13
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conversation.d.ts","sourceRoot":"","sources":["../src/conversation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,kBAAkB,EAClB,iBAAiB,EACjB,iBAAiB,EAEjB,mBAAmB,EACnB,kBAAkB,EAClB,WAAW,EACZ,MAAM,cAAc,CAAC;AACtB,OAAO,EAAyB,KAAK,SAAS,EAAsB,MAAM,iBAAiB,CAAC;AAC5F,OAAO,EAAgB,KAAK,aAAa,EAAE,MAAM,WAAW,CAAC;AAC7D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,KAAK,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAChE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAElF,MAAM,MAAM,iBAAiB,GAAG;IAC9B,YAAY,EAAE,MAAM,CAAC;IACrB,IAAI,EAAE,QAAQ,CAAC,aAAa,CAAC,CAAC;IAC9B,KAAK,EAAE,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AI,EAAE,SAAS,gBAAgB,EAAE,EAAE,MAAM,EAAE,SAAS,eAAe,EAAE,GAAG,IAAI,CAAC;IACrF,SAAS,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAC3B,OAAO,CAAC,QAAQ,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAChD,OAAO,IAAI,OAAO,CAAC;IACnB,WAAW,IAAI,IAAI,CAAC;IACpB,KAAK,CACH,YAAY,EAAE,MAAM,EACpB,OAAO,EAAE,mBAAmB,EAC5B,OAAO,EAAE,CAAC,IAAI,EAAE,gBAAgB,KAAK,IAAI,GACxC,MAAM,IAAI,CAAC;IACd,OAAO,CAAC,GAAG,EAAE,SAAS,GAAG,IAAI,CAAC;IAC9B,OAAO,CAAC,KAAK,EAAE,iBAAiB,GAAG,IAAI,CAAC;IACxC,QAAQ,IAAI,SAAS,eAAe,EAAE,CAAC;IACvC,OAAO,CAAC,YAAY,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,eAAe,GAAG,IAAI,CAAC;IACxE,KAAK,IAAI,IAAI,CAAC;CACf,CAAC;AAuGF,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,wBAAwB,GAAG,iBAAiB,CA6bzF"}
|
|
@@ -0,0 +1,593 @@
|
|
|
1
|
+
import { conversationChangeSet } from './change-set.js';
|
|
2
|
+
import { declaredFile } from './file.js';
|
|
3
|
+
const QUIET = {
|
|
4
|
+
growingText: null,
|
|
5
|
+
plan: null,
|
|
6
|
+
workingNote: null,
|
|
7
|
+
questions: [],
|
|
8
|
+
process: null,
|
|
9
|
+
commands: [],
|
|
10
|
+
options: []
|
|
11
|
+
};
|
|
12
|
+
function ordered(left, right) {
|
|
13
|
+
if (left.position !== right.position) return left.position < right.position ? -1 : 1;
|
|
14
|
+
if (left.identity !== right.identity) return left.identity < right.identity ? -1 : 1;
|
|
15
|
+
return 0;
|
|
16
|
+
}
|
|
17
|
+
function merged(held, arriving) {
|
|
18
|
+
const byIdentity = new Map(held.map((message)=>[
|
|
19
|
+
message.identity,
|
|
20
|
+
message
|
|
21
|
+
]));
|
|
22
|
+
for (const message of arriving)byIdentity.set(message.identity, message);
|
|
23
|
+
return [
|
|
24
|
+
...byIdentity.values()
|
|
25
|
+
].sort(ordered);
|
|
26
|
+
}
|
|
27
|
+
function acceptedMessage(value) {
|
|
28
|
+
if (typeof value !== 'object' || value === null) return null;
|
|
29
|
+
const record = value;
|
|
30
|
+
if (typeof record.identity !== 'string' || typeof record.turn !== 'string' || typeof record.position !== 'string' || typeof record.text !== 'string') {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
identity: record.identity,
|
|
35
|
+
turn: record.turn,
|
|
36
|
+
position: record.position,
|
|
37
|
+
text: record.text,
|
|
38
|
+
message: structuredClone(record.message)
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function historyPage(result) {
|
|
42
|
+
if (result.kind !== 'action') return null;
|
|
43
|
+
const answer = result.result;
|
|
44
|
+
if (typeof answer !== 'object' || answer === null) return null;
|
|
45
|
+
const record = answer;
|
|
46
|
+
if (!Array.isArray(record.messages)) return null;
|
|
47
|
+
if (record.next !== undefined && typeof record.next !== 'string') return null;
|
|
48
|
+
const messages = [];
|
|
49
|
+
for (const entry of record.messages){
|
|
50
|
+
const message = acceptedMessage(entry);
|
|
51
|
+
if (message === null) return null;
|
|
52
|
+
messages.push(message);
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
messages,
|
|
56
|
+
...record.next === undefined ? {} : {
|
|
57
|
+
next: record.next
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function turnOf(result) {
|
|
62
|
+
if (result.kind !== 'action') return null;
|
|
63
|
+
const answer = result.result;
|
|
64
|
+
if (typeof answer !== 'object' || answer === null) return null;
|
|
65
|
+
const record = answer;
|
|
66
|
+
return typeof record.turn === 'string' ? record.turn : null;
|
|
67
|
+
}
|
|
68
|
+
function limitOf(options) {
|
|
69
|
+
const { limit } = options;
|
|
70
|
+
if (limit === undefined) return undefined;
|
|
71
|
+
if (!Number.isInteger(limit) || limit < 0) {
|
|
72
|
+
throw new Error(`conversation limit must be a non-negative integer, got ${String(limit)}`);
|
|
73
|
+
}
|
|
74
|
+
return limit;
|
|
75
|
+
}
|
|
76
|
+
export function createConversationStore(deps) {
|
|
77
|
+
const messages = new Map();
|
|
78
|
+
const reads = new Map();
|
|
79
|
+
const drafts = new Map();
|
|
80
|
+
const sending = new Map();
|
|
81
|
+
const behind = new Set();
|
|
82
|
+
const gaps = new Map();
|
|
83
|
+
const lives = new Map();
|
|
84
|
+
const views = new Map();
|
|
85
|
+
let replicated = new Set();
|
|
86
|
+
let handedReplicated;
|
|
87
|
+
function liveOf(conversation) {
|
|
88
|
+
return lives.get(conversation) ?? QUIET;
|
|
89
|
+
}
|
|
90
|
+
function viewOf(conversation, limit) {
|
|
91
|
+
const held = messages.get(conversation) ?? [];
|
|
92
|
+
const shown = limit === undefined ? held : held.slice(Math.max(0, held.length - limit));
|
|
93
|
+
const draft = drafts.get(conversation);
|
|
94
|
+
const facts = liveOf(conversation);
|
|
95
|
+
return {
|
|
96
|
+
conversation,
|
|
97
|
+
messages: shown.map(({ identity, turn, text, message })=>({
|
|
98
|
+
identity,
|
|
99
|
+
turn,
|
|
100
|
+
text,
|
|
101
|
+
message: structuredClone(message)
|
|
102
|
+
})),
|
|
103
|
+
growingText: facts.growingText === null ? null : {
|
|
104
|
+
...facts.growingText
|
|
105
|
+
},
|
|
106
|
+
workingNote: facts.workingNote === null ? null : {
|
|
107
|
+
...facts.workingNote
|
|
108
|
+
},
|
|
109
|
+
plan: structuredClone(facts.plan?.plan ?? null),
|
|
110
|
+
questions: structuredClone(facts.questions),
|
|
111
|
+
process: facts.process === null ? null : {
|
|
112
|
+
...facts.process
|
|
113
|
+
},
|
|
114
|
+
commands: structuredClone(facts.commands),
|
|
115
|
+
options: structuredClone(facts.options),
|
|
116
|
+
chatDraft: draft === undefined || draft.turn !== null || sending.get(conversation)?.draft === draft ? null : {
|
|
117
|
+
text: draft.text,
|
|
118
|
+
files: [
|
|
119
|
+
...draft.files
|
|
120
|
+
]
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
function emit(conversation) {
|
|
125
|
+
if (deps.closed()) return;
|
|
126
|
+
for (const view of views.values()){
|
|
127
|
+
if (view.conversation !== conversation) continue;
|
|
128
|
+
const ready = [
|
|
129
|
+
...view.watchers
|
|
130
|
+
].filter((watcher)=>watcher.ready);
|
|
131
|
+
if (ready.length === 0) continue;
|
|
132
|
+
const value = viewOf(conversation, view.limit);
|
|
133
|
+
for (const watcher of ready)watcher.notify(value);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
function live(conversation, change) {
|
|
137
|
+
lives.set(conversation, {
|
|
138
|
+
...liveOf(conversation),
|
|
139
|
+
...change
|
|
140
|
+
});
|
|
141
|
+
emit(conversation);
|
|
142
|
+
}
|
|
143
|
+
function sealTurns(conversation, turns) {
|
|
144
|
+
const held = lives.get(conversation);
|
|
145
|
+
if (held === undefined) return false;
|
|
146
|
+
const growingText = held.growingText !== null && turns.has(held.growingText.turn) ? null : held.growingText;
|
|
147
|
+
const plan = held.plan !== null && turns.has(held.plan.turn) ? null : held.plan;
|
|
148
|
+
if (growingText === held.growingText && plan === held.plan) return false;
|
|
149
|
+
lives.set(conversation, {
|
|
150
|
+
...held,
|
|
151
|
+
growingText,
|
|
152
|
+
plan
|
|
153
|
+
});
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
function handReplicated() {
|
|
157
|
+
const stream = deps.stream();
|
|
158
|
+
if (stream === undefined) return;
|
|
159
|
+
const held = [
|
|
160
|
+
...replicated
|
|
161
|
+
].sort();
|
|
162
|
+
const named = JSON.stringify(held);
|
|
163
|
+
if (named === handedReplicated) return;
|
|
164
|
+
handedReplicated = named;
|
|
165
|
+
stream.replicate(held);
|
|
166
|
+
}
|
|
167
|
+
async function documented() {
|
|
168
|
+
const declaration = deps.cards();
|
|
169
|
+
if (declaration === undefined) return [];
|
|
170
|
+
const { documents } = await deps.documents(declaration.locator);
|
|
171
|
+
return documents.filter((document)=>typeof document.type === 'string');
|
|
172
|
+
}
|
|
173
|
+
function carded(documents) {
|
|
174
|
+
const declaration = deps.cards();
|
|
175
|
+
if (declaration === undefined) return [];
|
|
176
|
+
const cards = [];
|
|
177
|
+
for (const card of documents){
|
|
178
|
+
if (card.type !== declaration.type) continue;
|
|
179
|
+
const identity = card.fields[declaration.identity];
|
|
180
|
+
if (typeof identity === 'string') cards.push({
|
|
181
|
+
conversation: identity,
|
|
182
|
+
card
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
return cards;
|
|
186
|
+
}
|
|
187
|
+
async function remove(leaving) {
|
|
188
|
+
await deps.durably(async (local)=>{
|
|
189
|
+
await local.dropConversations(leaving);
|
|
190
|
+
const removed = [];
|
|
191
|
+
const changed = [];
|
|
192
|
+
for (const conversation of leaving){
|
|
193
|
+
for (const message of messages.get(conversation) ?? []){
|
|
194
|
+
removed.push({
|
|
195
|
+
conversation,
|
|
196
|
+
identity: message.identity
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
if (messages.has(conversation) || drafts.has(conversation) || lives.has(conversation)) {
|
|
200
|
+
changed.push(conversation);
|
|
201
|
+
}
|
|
202
|
+
messages.delete(conversation);
|
|
203
|
+
reads.delete(conversation);
|
|
204
|
+
drafts.delete(conversation);
|
|
205
|
+
behind.delete(conversation);
|
|
206
|
+
lives.delete(conversation);
|
|
207
|
+
}
|
|
208
|
+
if (changed.length > 0) deps.publish(conversationChangeSet(removed, changed));
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
function gap(conversation) {
|
|
212
|
+
gaps.set(conversation, (gaps.get(conversation) ?? 0) + 1);
|
|
213
|
+
behind.add(conversation);
|
|
214
|
+
}
|
|
215
|
+
function keep(conversation, message) {
|
|
216
|
+
return deps.durably(async (local)=>{
|
|
217
|
+
if (!replicated.has(conversation)) return;
|
|
218
|
+
const read = reads.get(conversation);
|
|
219
|
+
const advances = read !== undefined && !behind.has(conversation) && (read === null || message.position > read);
|
|
220
|
+
const sealed = drafts.get(conversation)?.turn === message.turn;
|
|
221
|
+
await local.keepMessages({
|
|
222
|
+
conversation,
|
|
223
|
+
messages: [
|
|
224
|
+
message
|
|
225
|
+
],
|
|
226
|
+
...advances ? {
|
|
227
|
+
read: message.position
|
|
228
|
+
} : {},
|
|
229
|
+
sealed
|
|
230
|
+
});
|
|
231
|
+
messages.set(conversation, merged(messages.get(conversation) ?? [], [
|
|
232
|
+
message
|
|
233
|
+
]));
|
|
234
|
+
if (advances) reads.set(conversation, message.position);
|
|
235
|
+
if (sealed) drafts.delete(conversation);
|
|
236
|
+
sealTurns(conversation, new Set([
|
|
237
|
+
message.turn
|
|
238
|
+
]));
|
|
239
|
+
deps.publish(conversationChangeSet([
|
|
240
|
+
{
|
|
241
|
+
conversation,
|
|
242
|
+
identity: message.identity
|
|
243
|
+
}
|
|
244
|
+
], [
|
|
245
|
+
conversation
|
|
246
|
+
]));
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
async function readHistory(conversation) {
|
|
250
|
+
const generation = gaps.get(conversation) ?? 0;
|
|
251
|
+
const from = reads.get(conversation);
|
|
252
|
+
const arriving = [];
|
|
253
|
+
let after = from ?? undefined;
|
|
254
|
+
for(;;){
|
|
255
|
+
const result = await deps.call('conversation.history', {
|
|
256
|
+
conversation,
|
|
257
|
+
...after === undefined ? {} : {
|
|
258
|
+
after
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
const page = historyPage(result);
|
|
262
|
+
if (page === null) return result;
|
|
263
|
+
arriving.push(...page.messages);
|
|
264
|
+
if (page.next === undefined) break;
|
|
265
|
+
after = page.next;
|
|
266
|
+
}
|
|
267
|
+
await deps.durably(async (local)=>{
|
|
268
|
+
if (!replicated.has(conversation)) return;
|
|
269
|
+
const newest = arriving.reduce((top, message)=>top === null || message.position > top ? message.position : top, from ?? null);
|
|
270
|
+
const turn = drafts.get(conversation)?.turn;
|
|
271
|
+
const sealed = arriving.some((message)=>message.turn === turn);
|
|
272
|
+
await local.keepMessages({
|
|
273
|
+
conversation,
|
|
274
|
+
messages: arriving,
|
|
275
|
+
read: newest,
|
|
276
|
+
sealed
|
|
277
|
+
});
|
|
278
|
+
messages.set(conversation, merged(messages.get(conversation) ?? [], arriving));
|
|
279
|
+
reads.set(conversation, newest);
|
|
280
|
+
if (sealed) drafts.delete(conversation);
|
|
281
|
+
sealTurns(conversation, new Set(arriving.map((message)=>message.turn)));
|
|
282
|
+
if ((gaps.get(conversation) ?? 0) === generation) behind.delete(conversation);
|
|
283
|
+
if (arriving.length > 0 || sealed) {
|
|
284
|
+
deps.publish(conversationChangeSet(arriving.map((message)=>({
|
|
285
|
+
conversation,
|
|
286
|
+
identity: message.identity
|
|
287
|
+
})), [
|
|
288
|
+
conversation
|
|
289
|
+
]));
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
return null;
|
|
293
|
+
}
|
|
294
|
+
function withDraft(conversation, next) {
|
|
295
|
+
return deps.durably(async (local)=>{
|
|
296
|
+
const following = next(drafts.get(conversation));
|
|
297
|
+
if (following === undefined) return;
|
|
298
|
+
if (following === null) {
|
|
299
|
+
await local.dropChatDraft(conversation);
|
|
300
|
+
drafts.delete(conversation);
|
|
301
|
+
} else {
|
|
302
|
+
await local.keepChatDraft(following);
|
|
303
|
+
drafts.set(conversation, following);
|
|
304
|
+
}
|
|
305
|
+
deps.publish(conversationChangeSet([], [
|
|
306
|
+
conversation
|
|
307
|
+
]));
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
function invoke(name, conversation, extra = {}) {
|
|
311
|
+
return deps.call(name, {
|
|
312
|
+
conversation,
|
|
313
|
+
...extra
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
const surface = {
|
|
317
|
+
async list () {
|
|
318
|
+
const declaration = deps.cards();
|
|
319
|
+
if (declaration === undefined) return [];
|
|
320
|
+
const documents = await documented();
|
|
321
|
+
const entries = carded(documents).map(({ conversation, card })=>({
|
|
322
|
+
conversation,
|
|
323
|
+
card,
|
|
324
|
+
agent: documents.find((document)=>document.backlinks.some((backlink)=>backlink.path === card.path && backlink.field === declaration.agent)) ?? null
|
|
325
|
+
}));
|
|
326
|
+
entries.sort((left, right)=>left.conversation < right.conversation ? -1 : 1);
|
|
327
|
+
return entries;
|
|
328
|
+
},
|
|
329
|
+
async read (conversation, options = {}) {
|
|
330
|
+
const limit = limitOf(options);
|
|
331
|
+
return deps.durably(()=>Promise.resolve(viewOf(conversation, limit)));
|
|
332
|
+
},
|
|
333
|
+
create (input) {
|
|
334
|
+
return deps.call('conversation.create', input);
|
|
335
|
+
},
|
|
336
|
+
async send (conversation) {
|
|
337
|
+
const draft = drafts.get(conversation);
|
|
338
|
+
const pending = draft === undefined ? undefined : {
|
|
339
|
+
draft
|
|
340
|
+
};
|
|
341
|
+
if (pending !== undefined) {
|
|
342
|
+
sending.set(conversation, pending);
|
|
343
|
+
deps.publish(conversationChangeSet([], [
|
|
344
|
+
conversation
|
|
345
|
+
]));
|
|
346
|
+
}
|
|
347
|
+
try {
|
|
348
|
+
const files = await Promise.all((draft?.files ?? []).map(async (file)=>({
|
|
349
|
+
...await declaredFile(file),
|
|
350
|
+
blob: file.blob
|
|
351
|
+
})));
|
|
352
|
+
const result = await invoke('conversation.send', conversation, {
|
|
353
|
+
text: draft?.text ?? '',
|
|
354
|
+
files
|
|
355
|
+
});
|
|
356
|
+
const turn = turnOf(result);
|
|
357
|
+
if (turn === null || draft === undefined) return result;
|
|
358
|
+
await withDraft(conversation, (current)=>{
|
|
359
|
+
if (current !== draft) return undefined;
|
|
360
|
+
const sealed = (messages.get(conversation) ?? []).some((message)=>message.turn === turn);
|
|
361
|
+
return sealed ? null : {
|
|
362
|
+
...draft,
|
|
363
|
+
turn
|
|
364
|
+
};
|
|
365
|
+
});
|
|
366
|
+
return result;
|
|
367
|
+
} finally{
|
|
368
|
+
if (pending !== undefined && sending.get(conversation) === pending) {
|
|
369
|
+
sending.delete(conversation);
|
|
370
|
+
if (drafts.get(conversation) === draft) {
|
|
371
|
+
deps.publish(conversationChangeSet([], [
|
|
372
|
+
conversation
|
|
373
|
+
]));
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
},
|
|
378
|
+
answer (conversation, answer) {
|
|
379
|
+
return invoke('conversation.answer', conversation, {
|
|
380
|
+
answer
|
|
381
|
+
});
|
|
382
|
+
},
|
|
383
|
+
interrupt (conversation) {
|
|
384
|
+
return invoke('conversation.interrupt', conversation);
|
|
385
|
+
},
|
|
386
|
+
kill (conversation) {
|
|
387
|
+
return invoke('conversation.kill', conversation);
|
|
388
|
+
},
|
|
389
|
+
async cancel (conversation, turn) {
|
|
390
|
+
const result = await invoke('conversation.cancel', conversation, {
|
|
391
|
+
turn
|
|
392
|
+
});
|
|
393
|
+
if (result.kind !== 'action') return result;
|
|
394
|
+
await withDraft(conversation, (current)=>current === undefined || current.turn !== turn ? undefined : {
|
|
395
|
+
...current,
|
|
396
|
+
turn: null
|
|
397
|
+
});
|
|
398
|
+
return result;
|
|
399
|
+
},
|
|
400
|
+
delete (conversation) {
|
|
401
|
+
return invoke('conversation.delete', conversation);
|
|
402
|
+
}
|
|
403
|
+
};
|
|
404
|
+
const chatDraft = {
|
|
405
|
+
write (conversation, draft) {
|
|
406
|
+
return withDraft(conversation, ()=>({
|
|
407
|
+
conversation,
|
|
408
|
+
text: draft.text,
|
|
409
|
+
files: [
|
|
410
|
+
...draft.files
|
|
411
|
+
],
|
|
412
|
+
turn: null
|
|
413
|
+
}));
|
|
414
|
+
},
|
|
415
|
+
discard (conversation) {
|
|
416
|
+
return withDraft(conversation, (current)=>current === undefined ? undefined : null);
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
return {
|
|
420
|
+
surface,
|
|
421
|
+
chatDraft,
|
|
422
|
+
hydrate (held, stored) {
|
|
423
|
+
messages.clear();
|
|
424
|
+
reads.clear();
|
|
425
|
+
for (const entry of held){
|
|
426
|
+
messages.set(entry.conversation, [
|
|
427
|
+
...entry.messages
|
|
428
|
+
].sort(ordered));
|
|
429
|
+
if (entry.read !== undefined) reads.set(entry.conversation, entry.read);
|
|
430
|
+
}
|
|
431
|
+
drafts.clear();
|
|
432
|
+
for (const record of stored)drafts.set(record.conversation, record);
|
|
433
|
+
},
|
|
434
|
+
async reconcile () {
|
|
435
|
+
const named = new Set(carded(await documented()).map((entry)=>entry.conversation));
|
|
436
|
+
const leaving = [
|
|
437
|
+
...new Set([
|
|
438
|
+
...replicated,
|
|
439
|
+
...messages.keys(),
|
|
440
|
+
...reads.keys(),
|
|
441
|
+
...drafts.keys()
|
|
442
|
+
])
|
|
443
|
+
].filter((conversation)=>!named.has(conversation));
|
|
444
|
+
for (const conversation of named){
|
|
445
|
+
if (!replicated.has(conversation)) behind.add(conversation);
|
|
446
|
+
}
|
|
447
|
+
replicated = named;
|
|
448
|
+
handReplicated();
|
|
449
|
+
if (leaving.length > 0) await remove(leaving);
|
|
450
|
+
},
|
|
451
|
+
async catchUp (progress) {
|
|
452
|
+
for (const conversation of [
|
|
453
|
+
...behind
|
|
454
|
+
].sort()){
|
|
455
|
+
if (deps.closed()) return;
|
|
456
|
+
if (!replicated.has(conversation)) continue;
|
|
457
|
+
const failed = await readHistory(conversation);
|
|
458
|
+
if (failed?.kind === 'transport-uncertainty') progress.reachable = false;
|
|
459
|
+
}
|
|
460
|
+
if (behind.size > 0) progress.complete = false;
|
|
461
|
+
},
|
|
462
|
+
current () {
|
|
463
|
+
return behind.size === 0;
|
|
464
|
+
},
|
|
465
|
+
reconnected () {
|
|
466
|
+
for (const conversation of replicated)gap(conversation);
|
|
467
|
+
},
|
|
468
|
+
watch (conversation, options, deliver) {
|
|
469
|
+
const limit = limitOf(options);
|
|
470
|
+
const key = JSON.stringify([
|
|
471
|
+
conversation,
|
|
472
|
+
limit ?? null
|
|
473
|
+
]);
|
|
474
|
+
const view = views.get(key) ?? {
|
|
475
|
+
conversation,
|
|
476
|
+
limit,
|
|
477
|
+
watchers: new Set()
|
|
478
|
+
};
|
|
479
|
+
views.set(key, view);
|
|
480
|
+
const watcher = {
|
|
481
|
+
notify: deliver,
|
|
482
|
+
ready: false
|
|
483
|
+
};
|
|
484
|
+
view.watchers.add(watcher);
|
|
485
|
+
void deps.durably(()=>Promise.resolve()).then(()=>{
|
|
486
|
+
if (deps.closed() || !view.watchers.has(watcher)) return;
|
|
487
|
+
watcher.ready = true;
|
|
488
|
+
watcher.notify(viewOf(conversation, limit));
|
|
489
|
+
}).catch(()=>{});
|
|
490
|
+
return ()=>{
|
|
491
|
+
if (!view.watchers.delete(watcher)) return;
|
|
492
|
+
if (view.watchers.size === 0) views.delete(key);
|
|
493
|
+
};
|
|
494
|
+
},
|
|
495
|
+
changed (set) {
|
|
496
|
+
for (const conversation of set.conversations)emit(conversation);
|
|
497
|
+
},
|
|
498
|
+
receive (event) {
|
|
499
|
+
if (deps.closed()) return;
|
|
500
|
+
const { conversation } = event;
|
|
501
|
+
if (event.kind === 'gap') {
|
|
502
|
+
if (replicated.has(conversation)) gap(conversation);
|
|
503
|
+
if (lives.delete(conversation)) emit(conversation);
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
if (event.kind === 'growing-text') {
|
|
507
|
+
live(conversation, {
|
|
508
|
+
growingText: {
|
|
509
|
+
turn: event.turn,
|
|
510
|
+
text: event.text
|
|
511
|
+
}
|
|
512
|
+
});
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
if (event.kind === 'plan') {
|
|
516
|
+
live(conversation, {
|
|
517
|
+
plan: {
|
|
518
|
+
turn: event.turn,
|
|
519
|
+
plan: structuredClone(event.plan)
|
|
520
|
+
}
|
|
521
|
+
});
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
524
|
+
if (event.kind === 'working-note') {
|
|
525
|
+
live(conversation, {
|
|
526
|
+
workingNote: event.note === null ? null : {
|
|
527
|
+
noteId: event.note.noteId,
|
|
528
|
+
text: event.note.text
|
|
529
|
+
}
|
|
530
|
+
});
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
if (event.kind === 'questions') {
|
|
534
|
+
live(conversation, {
|
|
535
|
+
questions: structuredClone(event.questions)
|
|
536
|
+
});
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
if (event.kind === 'process') {
|
|
540
|
+
live(conversation, {
|
|
541
|
+
process: {
|
|
542
|
+
running: event.running
|
|
543
|
+
}
|
|
544
|
+
});
|
|
545
|
+
return;
|
|
546
|
+
}
|
|
547
|
+
if (event.kind === 'commands') {
|
|
548
|
+
live(conversation, {
|
|
549
|
+
commands: structuredClone(event.commands)
|
|
550
|
+
});
|
|
551
|
+
return;
|
|
552
|
+
}
|
|
553
|
+
if (event.kind === 'options') {
|
|
554
|
+
live(conversation, {
|
|
555
|
+
options: structuredClone(event.options)
|
|
556
|
+
});
|
|
557
|
+
return;
|
|
558
|
+
}
|
|
559
|
+
const arriving = acceptedMessage(event.message);
|
|
560
|
+
if (arriving === null) return;
|
|
561
|
+
if (replicated.has(conversation)) {
|
|
562
|
+
void keep(conversation, arriving).catch(()=>{});
|
|
563
|
+
return;
|
|
564
|
+
}
|
|
565
|
+
if (sealTurns(conversation, new Set([
|
|
566
|
+
arriving.turn
|
|
567
|
+
]))) emit(conversation);
|
|
568
|
+
},
|
|
569
|
+
messages () {
|
|
570
|
+
return [
|
|
571
|
+
...messages
|
|
572
|
+
].flatMap(([conversation, held])=>held.map(({ identity, text })=>({
|
|
573
|
+
conversation,
|
|
574
|
+
identity,
|
|
575
|
+
text
|
|
576
|
+
})));
|
|
577
|
+
},
|
|
578
|
+
message (conversation, identity) {
|
|
579
|
+
const held = messages.get(conversation)?.find((message)=>message.identity === identity);
|
|
580
|
+
return held === undefined ? null : {
|
|
581
|
+
conversation,
|
|
582
|
+
identity,
|
|
583
|
+
text: held.text
|
|
584
|
+
};
|
|
585
|
+
},
|
|
586
|
+
close () {
|
|
587
|
+
views.clear();
|
|
588
|
+
lives.clear();
|
|
589
|
+
}
|
|
590
|
+
};
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
//# sourceMappingURL=conversation.js.map
|