@agentshouse/mdruntime-adapter 0.1.0-alpha.5 → 0.1.0-alpha.50
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 +157 -25
- package/README.md +32 -0
- package/dist/actions.d.ts +3 -11
- package/dist/actions.d.ts.map +1 -1
- package/dist/actions.js +201 -232
- package/dist/actions.js.map +1 -1
- package/dist/adapter.d.ts +3 -1
- package/dist/adapter.d.ts.map +1 -1
- package/dist/adapter.js +242 -173
- package/dist/adapter.js.map +1 -1
- package/dist/discovery.d.ts +39 -15
- package/dist/discovery.d.ts.map +1 -1
- package/dist/discovery.js +169 -43
- package/dist/discovery.js.map +1 -1
- package/dist/gather.d.ts +3 -0
- package/dist/gather.d.ts.map +1 -0
- package/dist/gather.js +41 -0
- package/dist/gather.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/links.d.ts +11 -0
- package/dist/links.d.ts.map +1 -0
- package/dist/links.js +57 -0
- package/dist/links.js.map +1 -0
- package/dist/map.d.ts +2 -2
- package/dist/map.d.ts.map +1 -1
- package/dist/map.js +2 -3
- package/dist/map.js.map +1 -1
- package/dist/model.d.ts +19 -0
- package/dist/model.d.ts.map +1 -0
- package/dist/model.js +64 -0
- package/dist/model.js.map +1 -0
- package/dist/read.d.ts +39 -27
- package/dist/read.d.ts.map +1 -1
- package/dist/read.js +246 -91
- package/dist/read.js.map +1 -1
- package/dist/refusal.d.ts +4 -1
- package/dist/refusal.d.ts.map +1 -1
- package/dist/refusal.js +13 -1
- package/dist/refusal.js.map +1 -1
- package/dist/site.d.ts +4 -0
- package/dist/site.d.ts.map +1 -0
- package/dist/site.js +228 -0
- package/dist/site.js.map +1 -0
- package/dist/stream.d.ts +11 -5
- package/dist/stream.d.ts.map +1 -1
- package/dist/stream.js +220 -142
- package/dist/stream.js.map +1 -1
- package/dist/transport.d.ts +57 -29
- package/dist/transport.d.ts.map +1 -1
- package/dist/transport.js +6 -1
- package/dist/transport.js.map +1 -1
- package/dist/version.d.ts +15 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +39 -0
- package/dist/version.js.map +1 -0
- package/dist/write.d.ts +9 -3
- package/dist/write.d.ts.map +1 -1
- package/dist/write.js +51 -9
- package/dist/write.js.map +1 -1
- package/package.json +4 -12
package/dist/stream.js
CHANGED
|
@@ -1,167 +1,227 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { selects } from './read.js';
|
|
3
|
-
import {
|
|
1
|
+
import { acceptedMessage } from './actions.js';
|
|
2
|
+
import { headChange, selects } from './read.js';
|
|
3
|
+
import { roomVersion } from './version.js';
|
|
4
4
|
const HELD_POSITIONS_MAX = 256;
|
|
5
5
|
function position(value) {
|
|
6
6
|
if (!/^(?:0|[1-9][0-9]*)$/.test(value)) return null;
|
|
7
7
|
return BigInt(value);
|
|
8
8
|
}
|
|
9
|
-
function
|
|
10
|
-
if (typeof message !== 'object' || message === null) return null;
|
|
11
|
-
const turn = message.message_id;
|
|
12
|
-
return typeof turn === 'string' ? turn : null;
|
|
13
|
-
}
|
|
14
|
-
export function houseNotifications(transport) {
|
|
9
|
+
export function houseNotifications(transport, mode) {
|
|
15
10
|
const stream = transport.stream;
|
|
16
|
-
const
|
|
11
|
+
const lines = new Map();
|
|
17
12
|
const locators = new Map();
|
|
18
|
-
const
|
|
13
|
+
const schemas = new Map();
|
|
19
14
|
const observers = new Set();
|
|
20
15
|
const conversationObservers = new Set();
|
|
21
|
-
const
|
|
22
|
-
let
|
|
23
|
-
|
|
16
|
+
const modeObservers = new Set();
|
|
17
|
+
let replicated = [];
|
|
18
|
+
const live = new Set();
|
|
19
|
+
const noted = new Set();
|
|
24
20
|
let listening = false;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
named.push({
|
|
31
|
-
kind: 'room',
|
|
32
|
-
room: authority
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
if (authorities.has(PRIVATE_AUTHORITY)) named.push({
|
|
36
|
-
kind: 'private'
|
|
37
|
-
});
|
|
38
|
-
for (const conversation of viewed){
|
|
39
|
-
named.push({
|
|
40
|
-
kind: 'conversation',
|
|
41
|
-
conversation
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
stream.subscribe(named);
|
|
21
|
+
let catching = false;
|
|
22
|
+
function listen() {
|
|
23
|
+
if (listening) return;
|
|
24
|
+
listening = true;
|
|
25
|
+
stream.observe(receive);
|
|
45
26
|
}
|
|
46
27
|
function emitConversation(event) {
|
|
47
|
-
if (
|
|
28
|
+
if (event.kind !== 'gap' && event.kind !== 'accepted-message') live.add(event.conversation);
|
|
48
29
|
for (const observer of conversationObservers)observer(event);
|
|
49
30
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
31
|
+
function unnote(conversation) {
|
|
32
|
+
if (!noted.delete(conversation)) return;
|
|
33
|
+
emitConversation({
|
|
34
|
+
kind: 'working-note',
|
|
35
|
+
conversation,
|
|
36
|
+
note: null
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
function reopened() {
|
|
40
|
+
const gapped = new Set([
|
|
41
|
+
...replicated,
|
|
42
|
+
...live
|
|
43
|
+
]);
|
|
44
|
+
live.clear();
|
|
45
|
+
noted.clear();
|
|
46
|
+
for (const conversation of gapped)emitConversation({
|
|
47
|
+
kind: 'gap',
|
|
48
|
+
conversation
|
|
53
49
|
});
|
|
54
|
-
|
|
55
|
-
|
|
50
|
+
}
|
|
51
|
+
function converse(frame) {
|
|
52
|
+
const conversation = frame.conversation_id;
|
|
53
|
+
if (frame.type === 'draft') {
|
|
54
|
+
unnote(conversation);
|
|
55
|
+
emitConversation({
|
|
56
|
+
kind: 'growing-text',
|
|
57
|
+
conversation,
|
|
58
|
+
turn: frame.turn_id,
|
|
59
|
+
text: frame.text
|
|
60
|
+
});
|
|
56
61
|
emitConversation({
|
|
57
|
-
kind: '
|
|
62
|
+
kind: 'plan',
|
|
58
63
|
conversation,
|
|
59
|
-
|
|
64
|
+
turn: frame.turn_id,
|
|
65
|
+
plan: structuredClone(frame.steps)
|
|
60
66
|
});
|
|
67
|
+
return;
|
|
61
68
|
}
|
|
62
|
-
if (
|
|
63
|
-
|
|
64
|
-
if ('outcome' in heldHistory) return;
|
|
65
|
-
const newest = heldHistory.messages.slice(-HISTORY_PAGE);
|
|
66
|
-
for (const entry of newest){
|
|
67
|
-
const turn = messageTurn(entry);
|
|
68
|
-
if (turn === null) return;
|
|
69
|
+
if (frame.type === 'working-note') {
|
|
70
|
+
noted.add(conversation);
|
|
69
71
|
emitConversation({
|
|
70
|
-
kind: '
|
|
72
|
+
kind: 'working-note',
|
|
71
73
|
conversation,
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
74
|
+
note: {
|
|
75
|
+
noteId: frame.note_id,
|
|
76
|
+
text: frame.text
|
|
75
77
|
}
|
|
76
78
|
});
|
|
79
|
+
return;
|
|
77
80
|
}
|
|
78
|
-
|
|
79
|
-
function receive(frame) {
|
|
80
|
-
if (frame.type === 'draft') {
|
|
81
|
+
if (frame.type === 'process') {
|
|
81
82
|
emitConversation({
|
|
82
|
-
kind: '
|
|
83
|
-
conversation
|
|
84
|
-
|
|
85
|
-
text: frame.text
|
|
83
|
+
kind: 'process',
|
|
84
|
+
conversation,
|
|
85
|
+
running: frame.running
|
|
86
86
|
});
|
|
87
|
+
if (frame.running) return;
|
|
88
|
+
emitConversation({
|
|
89
|
+
kind: 'commands',
|
|
90
|
+
conversation,
|
|
91
|
+
commands: []
|
|
92
|
+
});
|
|
93
|
+
emitConversation({
|
|
94
|
+
kind: 'options',
|
|
95
|
+
conversation,
|
|
96
|
+
options: []
|
|
97
|
+
});
|
|
98
|
+
unnote(conversation);
|
|
87
99
|
return;
|
|
88
100
|
}
|
|
89
|
-
if (frame.type === '
|
|
90
|
-
const turn = messageTurn(frame.message);
|
|
91
|
-
if (turn === null) return;
|
|
101
|
+
if (frame.type === 'commands') {
|
|
92
102
|
emitConversation({
|
|
93
|
-
kind: '
|
|
94
|
-
conversation
|
|
95
|
-
|
|
96
|
-
turn,
|
|
97
|
-
message: structuredClone(frame.message)
|
|
98
|
-
}
|
|
103
|
+
kind: 'commands',
|
|
104
|
+
conversation,
|
|
105
|
+
commands: structuredClone(frame.commands)
|
|
99
106
|
});
|
|
100
107
|
return;
|
|
101
108
|
}
|
|
102
|
-
if (frame.type === '
|
|
109
|
+
if (frame.type === 'options') {
|
|
103
110
|
emitConversation({
|
|
104
|
-
kind: '
|
|
105
|
-
conversation
|
|
106
|
-
|
|
111
|
+
kind: 'options',
|
|
112
|
+
conversation,
|
|
113
|
+
options: structuredClone(frame.options)
|
|
107
114
|
});
|
|
108
115
|
return;
|
|
109
116
|
}
|
|
110
|
-
if (frame.type === '
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
117
|
+
if (frame.type === 'interactions') {
|
|
118
|
+
emitConversation({
|
|
119
|
+
kind: 'questions',
|
|
120
|
+
conversation,
|
|
121
|
+
questions: structuredClone(frame.interactions)
|
|
122
|
+
});
|
|
114
123
|
return;
|
|
115
124
|
}
|
|
116
|
-
|
|
117
|
-
|
|
125
|
+
const message = acceptedMessage(frame.message);
|
|
126
|
+
if (message === null) return;
|
|
127
|
+
emitConversation({
|
|
128
|
+
kind: 'accepted-message',
|
|
129
|
+
conversation,
|
|
130
|
+
message
|
|
131
|
+
});
|
|
132
|
+
if (frame.message.role === 'user') return;
|
|
133
|
+
unnote(conversation);
|
|
134
|
+
}
|
|
135
|
+
function announce(authority, at, modelChanged) {
|
|
136
|
+
const version = roomVersion(at);
|
|
137
|
+
for (const locator of locators.get(authority) ?? []){
|
|
138
|
+
for (const observer of observers){
|
|
139
|
+
observer(modelChanged ? {
|
|
140
|
+
locator,
|
|
141
|
+
roomVersion: version,
|
|
142
|
+
modelChanged: true
|
|
143
|
+
} : {
|
|
144
|
+
locator,
|
|
145
|
+
roomVersion: version
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
function interrupt(authority) {
|
|
151
|
+
const line = lines.get(authority);
|
|
152
|
+
if (line !== undefined) lines.set(authority, {
|
|
153
|
+
logEpoch: line.logEpoch,
|
|
154
|
+
frames: []
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
function receive(frame) {
|
|
158
|
+
if (frame.type === 'signal') {
|
|
159
|
+
const mode = frame.service_mode;
|
|
160
|
+
if (mode !== 'open' && mode !== 'write-halted') return;
|
|
161
|
+
for (const observer of modeObservers)observer(mode);
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
if (frame.type === 'draft' || frame.type === 'working-note' || frame.type === 'process' || frame.type === 'commands' || frame.type === 'options' || frame.type === 'interactions' || frame.type === 'message') {
|
|
165
|
+
converse(frame);
|
|
118
166
|
return;
|
|
119
167
|
}
|
|
168
|
+
if (frame.type !== 'entries') return;
|
|
120
169
|
const at = position(frame.position);
|
|
121
170
|
if (at === null) {
|
|
122
|
-
|
|
171
|
+
interrupt(frame.authority);
|
|
123
172
|
return;
|
|
124
173
|
}
|
|
125
|
-
const
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
174
|
+
const line = lines.get(frame.authority);
|
|
175
|
+
if (line?.logEpoch === frame.log_epoch) {
|
|
176
|
+
const last = line.frames.at(-1);
|
|
177
|
+
const continued = last !== undefined && last.position + 1n === at;
|
|
178
|
+
const next = continued ? [
|
|
179
|
+
...line.frames,
|
|
180
|
+
{
|
|
181
|
+
position: at,
|
|
182
|
+
entries: frame.entries
|
|
183
|
+
}
|
|
184
|
+
] : [
|
|
185
|
+
{
|
|
186
|
+
position: at,
|
|
187
|
+
entries: frame.entries
|
|
188
|
+
}
|
|
189
|
+
];
|
|
190
|
+
lines.set(frame.authority, {
|
|
191
|
+
logEpoch: line.logEpoch,
|
|
192
|
+
frames: next.length > HELD_POSITIONS_MAX ? [] : next
|
|
193
|
+
});
|
|
194
|
+
} else {
|
|
195
|
+
interrupt(frame.authority);
|
|
196
|
+
}
|
|
197
|
+
const addresses = schemas.get(frame.authority) ?? [];
|
|
198
|
+
announce(frame.authority, {
|
|
199
|
+
position: frame.position,
|
|
200
|
+
logEpoch: frame.log_epoch
|
|
201
|
+
}, frame.entries.some((entry)=>addresses.includes(entry.path)));
|
|
147
202
|
}
|
|
148
203
|
return {
|
|
204
|
+
serviceMode: {
|
|
205
|
+
observe (notify) {
|
|
206
|
+
modeObservers.add(notify);
|
|
207
|
+
listen();
|
|
208
|
+
return ()=>{
|
|
209
|
+
modeObservers.delete(notify);
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
},
|
|
149
213
|
conversations: {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
for (const conversation of subscribedConversations){
|
|
153
|
-
if (!open.has(conversation)) subscribedConversations.delete(conversation);
|
|
154
|
-
}
|
|
155
|
-
viewed = [
|
|
214
|
+
replicate (next) {
|
|
215
|
+
replicated = [
|
|
156
216
|
...next
|
|
157
217
|
];
|
|
158
|
-
subscribe();
|
|
159
218
|
},
|
|
160
219
|
observe (notify) {
|
|
161
220
|
conversationObservers.add(notify);
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
221
|
+
listen();
|
|
222
|
+
if (!catching) {
|
|
223
|
+
catching = true;
|
|
224
|
+
stream.opened(reopened);
|
|
165
225
|
}
|
|
166
226
|
return ()=>{
|
|
167
227
|
conversationObservers.delete(notify);
|
|
@@ -170,40 +230,42 @@ export function houseNotifications(transport) {
|
|
|
170
230
|
},
|
|
171
231
|
track (entries) {
|
|
172
232
|
locators.clear();
|
|
173
|
-
|
|
233
|
+
schemas.clear();
|
|
174
234
|
for (const entry of entries){
|
|
175
|
-
locators.set(entry.authority,
|
|
176
|
-
|
|
235
|
+
locators.set(entry.authority, [
|
|
236
|
+
...locators.get(entry.authority) ?? [],
|
|
237
|
+
entry.locator
|
|
238
|
+
]);
|
|
239
|
+
schemas.set(entry.authority, entry.schemas);
|
|
240
|
+
if (lines.get(entry.authority)?.logEpoch !== entry.logEpoch) {
|
|
241
|
+
lines.set(entry.authority, {
|
|
242
|
+
logEpoch: entry.logEpoch,
|
|
243
|
+
frames: []
|
|
244
|
+
});
|
|
245
|
+
}
|
|
177
246
|
}
|
|
178
|
-
for (const authority of
|
|
179
|
-
if (!locators.has(authority))
|
|
247
|
+
for (const authority of lines.keys()){
|
|
248
|
+
if (!locators.has(authority)) lines.delete(authority);
|
|
180
249
|
}
|
|
181
250
|
},
|
|
182
|
-
select (
|
|
183
|
-
selected = [
|
|
184
|
-
...named
|
|
185
|
-
];
|
|
186
|
-
subscribe();
|
|
187
|
-
},
|
|
251
|
+
select () {},
|
|
188
252
|
observe (notify) {
|
|
189
253
|
observers.add(notify);
|
|
190
|
-
|
|
191
|
-
listening = true;
|
|
192
|
-
stream.observe(receive);
|
|
193
|
-
}
|
|
254
|
+
listen();
|
|
194
255
|
return ()=>{
|
|
195
256
|
observers.delete(notify);
|
|
196
257
|
};
|
|
197
258
|
},
|
|
198
|
-
advance (tracked,
|
|
199
|
-
const
|
|
200
|
-
const
|
|
201
|
-
if (
|
|
202
|
-
const at =
|
|
259
|
+
advance (tracked, from) {
|
|
260
|
+
const line = lines.get(tracked.authority);
|
|
261
|
+
const start = position(from.position);
|
|
262
|
+
if (line === undefined || start === null || line.logEpoch !== from.logEpoch) return null;
|
|
263
|
+
const at = line.frames.findIndex((frame)=>frame.position === start + 1n);
|
|
203
264
|
if (at === -1) return null;
|
|
204
|
-
const pending =
|
|
265
|
+
const pending = line.frames.slice(at);
|
|
205
266
|
const documents = {};
|
|
206
267
|
const revisions = {};
|
|
268
|
+
const changes = {};
|
|
207
269
|
const removals = [];
|
|
208
270
|
for (const frame of pending){
|
|
209
271
|
for (const entry of frame.entries){
|
|
@@ -211,26 +273,42 @@ export function houseNotifications(transport) {
|
|
|
211
273
|
if (entry.kind === 'removed') {
|
|
212
274
|
delete documents[entry.path];
|
|
213
275
|
delete revisions[entry.path];
|
|
276
|
+
delete changes[entry.path];
|
|
214
277
|
if (!removals.includes(entry.path)) removals.push(entry.path);
|
|
215
278
|
continue;
|
|
216
279
|
}
|
|
217
|
-
if (entry.text === undefined
|
|
280
|
+
if (entry.text === undefined) return null;
|
|
281
|
+
if (mode === 'credential' && entry.revision === undefined) return null;
|
|
282
|
+
const head = headChange(entry);
|
|
283
|
+
if (head === null) return null;
|
|
218
284
|
documents[entry.path] = entry.text;
|
|
219
|
-
revisions[entry.path] = entry.revision;
|
|
285
|
+
if (entry.revision !== undefined) revisions[entry.path] = entry.revision;
|
|
286
|
+
if (head === undefined) delete changes[entry.path];
|
|
287
|
+
else changes[entry.path] = head;
|
|
220
288
|
const removed = removals.indexOf(entry.path);
|
|
221
289
|
if (removed !== -1) removals.splice(removed, 1);
|
|
222
290
|
}
|
|
223
291
|
}
|
|
224
292
|
const last = pending.at(-1);
|
|
225
293
|
if (last === undefined) return null;
|
|
226
|
-
|
|
294
|
+
lines.set(tracked.authority, {
|
|
295
|
+
logEpoch: line.logEpoch,
|
|
296
|
+
frames: pending
|
|
297
|
+
});
|
|
227
298
|
return {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
299
|
+
page: {
|
|
300
|
+
kind: 'net-advancement',
|
|
301
|
+
roomVersion: roomVersion({
|
|
302
|
+
position: last.position.toString(),
|
|
303
|
+
logEpoch: line.logEpoch
|
|
304
|
+
}),
|
|
305
|
+
documents,
|
|
306
|
+
revisions,
|
|
307
|
+
changes,
|
|
308
|
+
binaryTargets: [],
|
|
309
|
+
removals: removals.sort()
|
|
310
|
+
},
|
|
311
|
+
entries: pending.flatMap((frame)=>frame.entries)
|
|
234
312
|
};
|
|
235
313
|
}
|
|
236
314
|
};
|
package/dist/stream.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/workspace/packages/mdruntime-adapter/src/stream.ts"],"sourcesContent":["import type {\n ConversationEvent,\n RefreshPage,\n RoomVersionNotification,\n SelectedRooms,\n ViewedConversations,\n} from '@agentshouse/mdruntime';\nimport { actionOutcome, HISTORY_PAGE, readConversationHistory } from './actions.js';\nimport type { TrackedAuthority } from './discovery.js';\nimport { selects } from './read.js';\nimport { PRIVATE_AUTHORITY, type HouseStreamAuthority, type HouseTransport } from './transport.js';\nimport type { HouseStreamEntry, HouseStreamFrame } from './transport.js';\n\nconst HELD_POSITIONS_MAX = 256;\n\ntype HeldFrame = {\n position: bigint;\n entries: readonly HouseStreamEntry[];\n};\n\nexport type HouseNotifications = SelectedRooms & {\n track(authorities: readonly TrackedAuthority[]): void;\n advance(tracked: TrackedAuthority, fromRoomVersion: string): RefreshPage | null;\n conversations: ViewedConversations;\n};\n\nfunction position(value: string): bigint | null {\n if (!/^(?:0|[1-9][0-9]*)$/.test(value)) return null;\n return BigInt(value);\n}\n\nfunction messageTurn(message: unknown): string | null {\n if (typeof message !== 'object' || message === null) return null;\n const turn = (message as { message_id?: unknown }).message_id;\n return typeof turn === 'string' ? turn : null;\n}\n\nexport function houseNotifications(transport: HouseTransport): HouseNotifications {\n const stream = transport.stream;\n const held = new Map<string, HeldFrame[]>();\n const locators = new Map<string, string>();\n const authorities = new Map<string, string>();\n const observers = new Set<(notification: RoomVersionNotification) => void>();\n const conversationObservers = new Set<(event: ConversationEvent) => void>();\n const subscribedConversations = new Set<string>();\n let selected: readonly string[] = [];\n let viewed: readonly string[] = [];\n let listening = false;\n\n function subscribe() {\n const named: HouseStreamAuthority[] = [];\n for (const locator of selected) {\n const authority = authorities.get(locator);\n if (authority === undefined || authority === PRIVATE_AUTHORITY) continue;\n named.push({ kind: 'room', room: authority });\n }\n if (authorities.has(PRIVATE_AUTHORITY)) named.push({ kind: 'private' });\n for (const conversation of viewed) {\n named.push({ kind: 'conversation', conversation });\n }\n stream.subscribe(named);\n }\n\n function emitConversation(event: ConversationEvent) {\n if (!viewed.includes(event.conversation)) return;\n for (const observer of conversationObservers) observer(event);\n }\n\n async function catchUp(conversation: string, history: boolean) {\n const viewedAnswer = await transport.door('/door/conversations/view', {\n conversation_id: conversation,\n });\n const summary = actionOutcome(viewedAnswer, 'conversations/view');\n if (\n summary.kind === 'action' &&\n typeof summary.result === 'object' &&\n summary.result !== null\n ) {\n emitConversation({\n kind: 'session-summary',\n conversation,\n summary: structuredClone('session' in summary.result ? summary.result.session : null),\n });\n }\n if (!history) return;\n const heldHistory = await readConversationHistory(transport, conversation);\n if ('outcome' in heldHistory) return;\n const newest = heldHistory.messages.slice(-HISTORY_PAGE);\n for (const entry of newest) {\n const turn = messageTurn(entry);\n if (turn === null) return;\n emitConversation({\n kind: 'accepted-message',\n conversation,\n message: { turn, message: structuredClone(entry) },\n });\n }\n }\n\n function receive(frame: HouseStreamFrame) {\n if (frame.type === 'draft') {\n emitConversation({\n kind: 'growing-text',\n conversation: frame.conversation_id,\n turn: frame.turn_id,\n text: frame.text,\n });\n return;\n }\n if (frame.type === 'message') {\n const turn = messageTurn(frame.message);\n if (turn === null) return;\n emitConversation({\n kind: 'accepted-message',\n conversation: frame.conversation_id,\n message: { turn, message: structuredClone(frame.message) },\n });\n return;\n }\n if (frame.type === 'session') {\n emitConversation({\n kind: 'session-summary',\n conversation: frame.conversation_id,\n summary: structuredClone(frame.summary),\n });\n return;\n }\n if (frame.type === 'subscribed' && viewed.includes(frame.authority)) {\n const reconnect = subscribedConversations.has(frame.authority);\n subscribedConversations.add(frame.authority);\n void catchUp(frame.authority, reconnect);\n return;\n }\n if (frame.type !== 'entries') {\n held.delete(frame.authority);\n return;\n }\n const at = position(frame.position);\n if (at === null) {\n held.delete(frame.authority);\n return;\n }\n const run = held.get(frame.authority) ?? [];\n const last = run.at(-1);\n const continued = last !== undefined && last.position + 1n === at;\n const next = continued\n ? [...run, { position: at, entries: frame.entries }]\n : [{ position: at, entries: frame.entries }];\n held.set(frame.authority, next.length > HELD_POSITIONS_MAX ? [] : next);\n const locator = locators.get(frame.authority);\n if (locator === undefined) return;\n for (const observer of observers) observer({ locator, roomVersion: frame.position });\n }\n\n return {\n conversations: {\n view(next) {\n const open = new Set(next);\n for (const conversation of subscribedConversations) {\n if (!open.has(conversation)) subscribedConversations.delete(conversation);\n }\n viewed = [...next];\n subscribe();\n },\n observe(notify) {\n conversationObservers.add(notify);\n if (!listening) {\n listening = true;\n stream.observe(receive);\n }\n return () => {\n conversationObservers.delete(notify);\n };\n },\n },\n track(entries) {\n locators.clear();\n authorities.clear();\n for (const entry of entries) {\n locators.set(entry.authority, entry.locator);\n authorities.set(entry.locator, entry.authority);\n }\n for (const authority of held.keys()) {\n if (!locators.has(authority)) held.delete(authority);\n }\n },\n select(named) {\n selected = [...named];\n subscribe();\n },\n observe(notify) {\n observers.add(notify);\n if (!listening) {\n listening = true;\n stream.observe(receive);\n }\n return () => {\n observers.delete(notify);\n };\n },\n advance(tracked, fromRoomVersion) {\n const run = held.get(tracked.authority);\n const from = position(fromRoomVersion);\n if (run === undefined || from === null) return null;\n const at = run.findIndex((frame) => frame.position === from + 1n);\n if (at === -1) return null;\n const pending = run.slice(at);\n const documents: Record<string, string> = {};\n const revisions: Record<string, string> = {};\n const removals: string[] = [];\n for (const frame of pending) {\n for (const entry of frame.entries) {\n if (!selects(tracked, entry.path)) continue;\n if (entry.kind === 'removed') {\n delete documents[entry.path];\n delete revisions[entry.path];\n if (!removals.includes(entry.path)) removals.push(entry.path);\n continue;\n }\n if (entry.text === undefined || entry.revision === undefined) return null;\n documents[entry.path] = entry.text;\n revisions[entry.path] = entry.revision;\n const removed = removals.indexOf(entry.path);\n if (removed !== -1) removals.splice(removed, 1);\n }\n }\n const last = pending.at(-1);\n if (last === undefined) return null;\n held.set(tracked.authority, pending);\n return {\n kind: 'net-advancement',\n roomVersion: last.position.toString(),\n documents,\n revisions,\n binaryTargets: [],\n removals: removals.sort(),\n };\n },\n };\n}\n"],"names":["actionOutcome","HISTORY_PAGE","readConversationHistory","selects","PRIVATE_AUTHORITY","HELD_POSITIONS_MAX","position","value","test","BigInt","messageTurn","message","turn","message_id","houseNotifications","transport","stream","held","Map","locators","authorities","observers","Set","conversationObservers","subscribedConversations","selected","viewed","listening","subscribe","named","locator","authority","get","undefined","push","kind","room","has","conversation","emitConversation","event","includes","observer","catchUp","history","viewedAnswer","door","conversation_id","summary","result","structuredClone","session","heldHistory","newest","messages","slice","entry","receive","frame","type","turn_id","text","reconnect","add","delete","at","run","last","continued","next","entries","set","length","roomVersion","conversations","view","open","observe","notify","track","clear","keys","select","advance","tracked","fromRoomVersion","from","findIndex","pending","documents","revisions","removals","path","revision","removed","indexOf","splice","toString","binaryTargets","sort"],"mappings":"AAOA,SAASA,aAAa,EAAEC,YAAY,EAAEC,uBAAuB,QAAQ,eAAe;AAEpF,SAASC,OAAO,QAAQ,YAAY;AACpC,SAASC,iBAAiB,QAAwD,iBAAiB;AAGnG,MAAMC,qBAAqB;AAa3B,SAASC,SAASC,KAAa;IAC7B,IAAI,CAAC,sBAAsBC,IAAI,CAACD,QAAQ,OAAO;IAC/C,OAAOE,OAAOF;AAChB;AAEA,SAASG,YAAYC,OAAgB;IACnC,IAAI,OAAOA,YAAY,YAAYA,YAAY,MAAM,OAAO;IAC5D,MAAMC,OAAO,AAACD,QAAqCE,UAAU;IAC7D,OAAO,OAAOD,SAAS,WAAWA,OAAO;AAC3C;AAEA,OAAO,SAASE,mBAAmBC,SAAyB;IAC1D,MAAMC,SAASD,UAAUC,MAAM;IAC/B,MAAMC,OAAO,IAAIC;IACjB,MAAMC,WAAW,IAAID;IACrB,MAAME,cAAc,IAAIF;IACxB,MAAMG,YAAY,IAAIC;IACtB,MAAMC,wBA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MoC,IAAI,GAAGD,SAASzD,IAAI,CAACsB,MAAMoC,IAAI;wBAC5D;oBACF;oBACA,IAAIpC,MAAMK,IAAI,KAAK5B,aAAauB,MAAMqC,QAAQ,KAAK5D,WAAW,OAAO;oBACrEwD,SAAS,CAACjC,MAAMoC,IAAI,CAAC,GAAGpC,MAAMK,IAAI;oBAClC6B,SAAS,CAAClC,MAAMoC,IAAI,CAAC,GAAGpC,MAAMqC,QAAQ;oBACtC,MAAMC,UAAUH,SAASI,OAAO,CAACvC,MAAMoC,IAAI;oBAC3C,IAAIE,YAAY,CAAC,GAAGH,SAASK,MAAM,CAACF,SAAS;gBAC/C;YACF;YACA,MAAM3B,OAAOqB,QAAQvB,EAAE,CAAC,CAAC;YACzB,IAAIE,SAASlC,WAAW,OAAO;YAC/BhB,KAAKsD,GAAG,CAACa,QAAQrD,SAAS,EAAEyD;YAC5B,OAAO;gBACLrD,MAAM;gBACNsC,aAAaN,KAAK7D,QAAQ,CAAC2F,QAAQ;gBACnCR;gBACAC;gBACAQ,eAAe,EAAE;gBACjBP,UAAUA,SAASQ,IAAI;YACzB;QACF;IACF;AACF"}
|
|
1
|
+
{"version":3,"sources":["/workspace/packages/mdruntime-adapter/src/stream.ts"],"sourcesContent":["import type {\n ConversationEvent,\n ConversationStream,\n RefreshPage,\n RoomVersionNotification,\n SelectedRooms,\n ServiceMode,\n ServiceModeSignal,\n} from '@agentshouse/mdruntime';\nimport { acceptedMessage } from './actions.js';\nimport type { TrackedAuthority } from './discovery.js';\nimport { headChange, selects, type HeadChange } from './read.js';\nimport type { HouseDoor, HouseMode, HouseStreamEntry, HouseStreamFrame } from './transport.js';\nimport { roomVersion, type LogPosition } from './version.js';\n\nconst HELD_POSITIONS_MAX = 256;\n\ntype HeldFrame = {\n position: bigint;\n entries: readonly HouseStreamEntry[];\n};\n\ntype HeldLine = {\n logEpoch: string;\n frames: readonly HeldFrame[];\n};\n\nexport type StreamedAdvancement = {\n page: RefreshPage;\n entries: readonly HouseStreamEntry[];\n};\n\nexport type HouseNotifications = SelectedRooms & {\n track(authorities: readonly TrackedAuthority[]): void;\n advance(tracked: TrackedAuthority, from: LogPosition): StreamedAdvancement | null;\n conversations: ConversationStream;\n serviceMode: ServiceModeSignal;\n};\n\nfunction position(value: string): bigint | null {\n if (!/^(?:0|[1-9][0-9]*)$/.test(value)) return null;\n return BigInt(value);\n}\n\nexport function houseNotifications(transport: HouseDoor, mode: HouseMode): HouseNotifications {\n const stream = transport.stream;\n const lines = new Map<string, HeldLine>();\n const locators = new Map<string, string[]>();\n const schemas = new Map<string, readonly string[]>();\n const observers = new Set<(notification: RoomVersionNotification) => void>();\n const conversationObservers = new Set<(event: ConversationEvent) => void>();\n const modeObservers = new Set<(mode: ServiceMode) => void>();\n let replicated: readonly string[] = [];\n const live = new Set<string>();\n const noted = new Set<string>();\n let listening = false;\n let catching = false;\n\n function listen() {\n if (listening) return;\n listening = true;\n stream.observe(receive);\n }\n\n function emitConversation(event: ConversationEvent) {\n if (event.kind !== 'gap' && event.kind !== 'accepted-message') live.add(event.conversation);\n for (const observer of conversationObservers) observer(event);\n }\n\n function unnote(conversation: string) {\n if (!noted.delete(conversation)) return;\n emitConversation({ kind: 'working-note', conversation, note: null });\n }\n\n function reopened() {\n const gapped = new Set([...replicated, ...live]);\n live.clear();\n noted.clear();\n for (const conversation of gapped) emitConversation({ kind: 'gap', conversation });\n }\n\n function converse(frame: Exclude<HouseStreamFrame, { type: 'entries' | 'signal' }>) {\n const conversation = frame.conversation_id;\n if (frame.type === 'draft') {\n unnote(conversation);\n emitConversation({\n kind: 'growing-text',\n conversation,\n turn: frame.turn_id,\n text: frame.text,\n });\n emitConversation({\n kind: 'plan',\n conversation,\n turn: frame.turn_id,\n plan: structuredClone(frame.steps),\n });\n return;\n }\n if (frame.type === 'working-note') {\n noted.add(conversation);\n emitConversation({\n kind: 'working-note',\n conversation,\n note: { noteId: frame.note_id, text: frame.text },\n });\n return;\n }\n if (frame.type === 'process') {\n emitConversation({ kind: 'process', conversation, running: frame.running });\n if (frame.running) return;\n emitConversation({ kind: 'commands', conversation, commands: [] });\n emitConversation({ kind: 'options', conversation, options: [] });\n unnote(conversation);\n return;\n }\n if (frame.type === 'commands') {\n emitConversation({\n kind: 'commands',\n conversation,\n commands: structuredClone(frame.commands),\n });\n return;\n }\n if (frame.type === 'options') {\n emitConversation({ kind: 'options', conversation, options: structuredClone(frame.options) });\n return;\n }\n if (frame.type === 'interactions') {\n emitConversation({\n kind: 'questions',\n conversation,\n questions: structuredClone(frame.interactions),\n });\n return;\n }\n const message = acceptedMessage(frame.message);\n if (message === null) return;\n emitConversation({ kind: 'accepted-message', conversation, message });\n if ((frame.message as { role?: unknown }).role === 'user') return;\n unnote(conversation);\n }\n\n function announce(authority: string, at: LogPosition, modelChanged: boolean) {\n const version = roomVersion(at);\n for (const locator of locators.get(authority) ?? []) {\n for (const observer of observers) {\n observer(\n modelChanged\n ? { locator, roomVersion: version, modelChanged: true }\n : { locator, roomVersion: version },\n );\n }\n }\n }\n\n function interrupt(authority: string) {\n const line = lines.get(authority);\n if (line !== undefined) lines.set(authority, { logEpoch: line.logEpoch, frames: [] });\n }\n\n function receive(frame: HouseStreamFrame) {\n if (frame.type === 'signal') {\n const mode = frame.service_mode;\n if (mode !== 'open' && mode !== 'write-halted') return;\n for (const observer of modeObservers) observer(mode);\n return;\n }\n if (\n frame.type === 'draft' ||\n frame.type === 'working-note' ||\n frame.type === 'process' ||\n frame.type === 'commands' ||\n frame.type === 'options' ||\n frame.type === 'interactions' ||\n frame.type === 'message'\n ) {\n converse(frame);\n return;\n }\n if (frame.type !== 'entries') return;\n const at = position(frame.position);\n if (at === null) {\n interrupt(frame.authority);\n return;\n }\n const line = lines.get(frame.authority);\n if (line?.logEpoch === frame.log_epoch) {\n const last = line.frames.at(-1);\n const continued = last !== undefined && last.position + 1n === at;\n const next = continued\n ? [...line.frames, { position: at, entries: frame.entries }]\n : [{ position: at, entries: frame.entries }];\n lines.set(frame.authority, {\n logEpoch: line.logEpoch,\n frames: next.length > HELD_POSITIONS_MAX ? [] : next,\n });\n } else {\n interrupt(frame.authority);\n }\n const addresses = schemas.get(frame.authority) ?? [];\n announce(\n frame.authority,\n { position: frame.position, logEpoch: frame.log_epoch },\n frame.entries.some((entry) => addresses.includes(entry.path)),\n );\n }\n\n return {\n serviceMode: {\n observe(notify) {\n modeObservers.add(notify);\n listen();\n return () => {\n modeObservers.delete(notify);\n };\n },\n },\n conversations: {\n replicate(next) {\n replicated = [...next];\n },\n observe(notify) {\n conversationObservers.add(notify);\n listen();\n if (!catching) {\n catching = true;\n stream.opened(reopened);\n }\n return () => {\n conversationObservers.delete(notify);\n };\n },\n },\n track(entries) {\n locators.clear();\n schemas.clear();\n for (const entry of entries) {\n locators.set(entry.authority, [...(locators.get(entry.authority) ?? []), entry.locator]);\n schemas.set(entry.authority, entry.schemas);\n if (lines.get(entry.authority)?.logEpoch !== entry.logEpoch) {\n lines.set(entry.authority, { logEpoch: entry.logEpoch, frames: [] });\n }\n }\n for (const authority of lines.keys()) {\n if (!locators.has(authority)) lines.delete(authority);\n }\n },\n select() {},\n observe(notify) {\n observers.add(notify);\n listen();\n return () => {\n observers.delete(notify);\n };\n },\n advance(tracked, from) {\n const line = lines.get(tracked.authority);\n const start = position(from.position);\n if (line === undefined || start === null || line.logEpoch !== from.logEpoch) return null;\n const at = line.frames.findIndex((frame) => frame.position === start + 1n);\n if (at === -1) return null;\n const pending = line.frames.slice(at);\n const documents: Record<string, string> = {};\n const revisions: Record<string, string> = {};\n const changes: Record<string, HeadChange> = {};\n const removals: string[] = [];\n for (const frame of pending) {\n for (const entry of frame.entries) {\n if (!selects(tracked, entry.path)) continue;\n if (entry.kind === 'removed') {\n delete documents[entry.path];\n delete revisions[entry.path];\n delete changes[entry.path];\n if (!removals.includes(entry.path)) removals.push(entry.path);\n continue;\n }\n if (entry.text === undefined) return null;\n if (mode === 'credential' && entry.revision === undefined) return null;\n const head = headChange(entry);\n if (head === null) return null;\n documents[entry.path] = entry.text;\n if (entry.revision !== undefined) revisions[entry.path] = entry.revision;\n if (head === undefined) delete changes[entry.path];\n else changes[entry.path] = head;\n const removed = removals.indexOf(entry.path);\n if (removed !== -1) removals.splice(removed, 1);\n }\n }\n const last = pending.at(-1);\n if (last === undefined) return null;\n lines.set(tracked.authority, { logEpoch: line.logEpoch, frames: pending });\n return {\n page: {\n kind: 'net-advancement',\n roomVersion: roomVersion({ position: last.position.toString(), logEpoch: line.logEpoch }),\n documents,\n revisions,\n changes,\n binaryTargets: [],\n removals: removals.sort(),\n },\n entries: pending.flatMap((frame) => frame.entries),\n };\n },\n };\n}\n"],"names":["acceptedMessage","headChange","selects","roomVersion","HELD_POSITIONS_MAX","position","value","test","BigInt","houseNotifications","transport","mode","stream","lines","Map","locators","schemas","observers","Set","conversationObservers","modeObservers","replicated","live","noted","listening","catching","listen","observe","receive","emitConversation","event","kind","add","conversation","observer","unnote","delete","note","reopened","gapped","clear","converse","frame","conversation_id","type","turn","turn_id","text","plan","structuredClone","steps","noteId","note_id","running","commands","options","questions","interactions","message","role","announce","authority","at","modelChanged","version","locator","get","interrupt","line","undefined","set","logEpoch","frames","service_mode","log_epoch","last","continued","next","entries","length","addresses","some","entry","includes","path","serviceMode","notify","conversations","replicate","opened","track","keys","has","select","advance","tracked","from","start","findIndex","pending","slice","documents","revisions","changes","removals","push","revision","head","removed","indexOf","splice","page","toString","binaryTargets","sort","flatMap"],"mappings":"AASA,SAASA,eAAe,QAAQ,eAAe;AAE/C,SAASC,UAAU,EAAEC,OAAO,QAAyB,YAAY;AAEjE,SAASC,WAAW,QAA0B,eAAe;AAE7D,MAAMC,qBAAqB;AAwB3B,SAASC,SAASC,KAAa;IAC7B,IAAI,CAAC,sBAAsBC,IAAI,CAACD,QAAQ,OAAO;IAC/C,OAAOE,OAAOF;AAChB;AAEA,OAAO,SAASG,mBAAmBC,SAAoB,EAAEC,IAAe;IACtE,MAAMC,SAASF,UAAUE,MAAM;IAC/B,MAAMC,QAAQ,I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