@agentshouse/mdruntime-adapter 0.1.0-alpha.8 → 0.1.0-c055ce6f0fdd0

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.
Files changed (61) hide show
  1. package/AGENTS.md +186 -34
  2. package/README.md +32 -0
  3. package/dist/actions.d.ts +5 -13
  4. package/dist/actions.d.ts.map +1 -1
  5. package/dist/actions.js +241 -334
  6. package/dist/adapter.d.ts.map +1 -1
  7. package/dist/adapter.js +256 -216
  8. package/dist/digest.js +1 -5
  9. package/dist/discovery.d.ts +23 -33
  10. package/dist/discovery.d.ts.map +1 -1
  11. package/dist/discovery.js +143 -230
  12. package/dist/gather.d.ts +3 -0
  13. package/dist/gather.d.ts.map +1 -0
  14. package/dist/gather.js +40 -0
  15. package/dist/index.d.ts +2 -2
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +1 -3
  18. package/dist/links.d.ts +20 -0
  19. package/dist/links.d.ts.map +1 -0
  20. package/dist/links.js +104 -0
  21. package/dist/map.d.ts +2 -2
  22. package/dist/map.d.ts.map +1 -1
  23. package/dist/map.js +6 -7
  24. package/dist/model.d.ts +7 -18
  25. package/dist/model.d.ts.map +1 -1
  26. package/dist/model.js +48 -59
  27. package/dist/read.d.ts +31 -26
  28. package/dist/read.d.ts.map +1 -1
  29. package/dist/read.js +205 -199
  30. package/dist/refusal.d.ts +3 -1
  31. package/dist/refusal.d.ts.map +1 -1
  32. package/dist/refusal.js +27 -62
  33. package/dist/site.d.ts.map +1 -1
  34. package/dist/site.js +103 -151
  35. package/dist/stream.d.ts +16 -6
  36. package/dist/stream.d.ts.map +1 -1
  37. package/dist/stream.js +238 -214
  38. package/dist/transport.d.ts +69 -30
  39. package/dist/transport.d.ts.map +1 -1
  40. package/dist/transport.js +2 -3
  41. package/dist/version.d.ts +2 -0
  42. package/dist/version.d.ts.map +1 -1
  43. package/dist/version.js +32 -18
  44. package/dist/write.d.ts +19 -8
  45. package/dist/write.d.ts.map +1 -1
  46. package/dist/write.js +135 -208
  47. package/package.json +4 -12
  48. package/dist/actions.js.map +0 -1
  49. package/dist/adapter.js.map +0 -1
  50. package/dist/digest.js.map +0 -1
  51. package/dist/discovery.js.map +0 -1
  52. package/dist/index.js.map +0 -1
  53. package/dist/map.js.map +0 -1
  54. package/dist/model.js.map +0 -1
  55. package/dist/read.js.map +0 -1
  56. package/dist/refusal.js.map +0 -1
  57. package/dist/site.js.map +0 -1
  58. package/dist/stream.js.map +0 -1
  59. package/dist/transport.js.map +0 -1
  60. package/dist/version.js.map +0 -1
  61. package/dist/write.js.map +0 -1
package/dist/stream.js CHANGED
@@ -1,298 +1,322 @@
1
- import { actionOutcome, HISTORY_PAGE, readConversationHistory } from './actions.js';
2
- import { selects } from './read.js';
3
- import { PRIVATE_AUTHORITY } from './transport.js';
1
+ import { acceptedMessage } from './actions.js';
2
+ import { headChange } from './read.js';
4
3
  import { roomVersion } from './version.js';
5
- const HELD_POSITIONS_MAX = 256;
4
+ const HELD_FRAMES_MAX = 256;
5
+ function turnOperation(frame) {
6
+ const { type: _type, conversation_id: _conversation, turn_id: _turn, ...operation } = frame;
7
+ return structuredClone(operation);
8
+ }
9
+ function listedConversation(listed) {
10
+ return {
11
+ conversation: listed.conversation_id,
12
+ agent: listed.agent_id,
13
+ name: listed.name,
14
+ created: listed.created_at,
15
+ finished: listed.finished_at,
16
+ outcome: listed.outcome,
17
+ };
18
+ }
6
19
  function position(value) {
7
- if (!/^(?:0|[1-9][0-9]*)$/.test(value)) return null;
20
+ if (!/^(?:0|[1-9][0-9]*)$/.test(value))
21
+ return null;
8
22
  return BigInt(value);
9
23
  }
10
- function messageTurn(message) {
11
- if (typeof message !== 'object' || message === null) return null;
12
- const turn = message.message_id;
13
- return typeof turn === 'string' ? turn : null;
14
- }
15
24
  export function houseNotifications(transport, mode) {
16
25
  const stream = transport.stream;
17
26
  const lines = new Map();
18
27
  const locators = new Map();
19
- const authorities = new Map();
20
28
  const observers = new Set();
21
29
  const conversationObservers = new Set();
22
- const subscribedConversations = new Set();
23
- let selected = [];
24
- let viewed = [];
25
- let listening = false;
26
- function subscribe() {
27
- const named = [];
28
- const rooms = new Set();
29
- for (const locator of selected){
30
- const authority = authorities.get(locator);
31
- if (authority === undefined || authority === PRIVATE_AUTHORITY) continue;
32
- rooms.add(authority);
33
- }
34
- for (const room of rooms)named.push({
35
- kind: 'room',
36
- room
37
- });
38
- if (authorities.has(PRIVATE_AUTHORITY)) named.push({
39
- kind: 'private'
40
- });
41
- for (const conversation of viewed){
42
- named.push({
43
- kind: 'conversation',
44
- conversation
45
- });
46
- }
47
- stream.subscribe(named);
30
+ const modeObservers = new Set();
31
+ let replicated = [];
32
+ const live = new Set();
33
+ let opening = null;
34
+ const runtime = crypto.randomUUID();
35
+ function emit(event) {
36
+ for (const observer of observers)
37
+ observer(event);
48
38
  }
49
39
  function emitConversation(event) {
50
- if (!viewed.includes(event.conversation)) return;
51
- for (const observer of conversationObservers)observer(event);
40
+ if (event.kind === 'turn' ||
41
+ event.kind === 'questions' ||
42
+ event.kind === 'process' ||
43
+ event.kind === 'commands' ||
44
+ event.kind === 'options') {
45
+ live.add(event.conversation);
46
+ }
47
+ for (const observer of conversationObservers)
48
+ observer(event);
52
49
  }
53
- async function catchUp(conversation, history) {
54
- const viewedAnswer = await transport.door('/door/conversations/view', {
55
- conversation_id: conversation
56
- });
57
- const summary = actionOutcome(viewedAnswer, 'conversations/view');
58
- if (summary.kind === 'action' && typeof summary.result === 'object' && summary.result !== null) {
50
+ function reopened(next) {
51
+ opening = next;
52
+ const gapped = new Set([...replicated, ...live]);
53
+ live.clear();
54
+ for (const conversation of gapped)
55
+ emitConversation({ kind: 'gap', conversation });
56
+ emit({ kind: 'opened' });
57
+ }
58
+ function converse(frame) {
59
+ const conversation = frame.conversation_id;
60
+ if (frame.type === 'turn') {
59
61
  emitConversation({
60
- kind: 'session-summary',
62
+ kind: 'turn',
61
63
  conversation,
62
- summary: structuredClone('session' in summary.result ? summary.result.session : null)
64
+ turn: frame.turn_id,
65
+ operation: turnOperation(frame),
63
66
  });
67
+ return;
68
+ }
69
+ if (frame.type === 'process') {
70
+ emitConversation({ kind: 'process', conversation, running: frame.running });
71
+ if (frame.running)
72
+ return;
73
+ emitConversation({ kind: 'commands', conversation, commands: [] });
74
+ emitConversation({ kind: 'options', conversation, options: [] });
75
+ return;
64
76
  }
65
- if (!history) return;
66
- const heldHistory = await readConversationHistory(transport, conversation);
67
- if ('outcome' in heldHistory) return;
68
- const newest = heldHistory.messages.slice(-HISTORY_PAGE);
69
- for (const entry of newest){
70
- const turn = messageTurn(entry);
71
- if (turn === null) return;
77
+ if (frame.type === 'commands') {
72
78
  emitConversation({
73
- kind: 'accepted-message',
79
+ kind: 'commands',
74
80
  conversation,
75
- message: {
76
- turn,
77
- message: structuredClone(entry)
78
- }
81
+ commands: structuredClone(frame.commands),
79
82
  });
83
+ return;
80
84
  }
85
+ if (frame.type === 'options') {
86
+ emitConversation({ kind: 'options', conversation, options: structuredClone(frame.options) });
87
+ return;
88
+ }
89
+ if (frame.type === 'interactions') {
90
+ emitConversation({
91
+ kind: 'questions',
92
+ conversation,
93
+ questions: structuredClone(frame.interactions),
94
+ });
95
+ return;
96
+ }
97
+ const message = acceptedMessage(frame.message);
98
+ if (message === null)
99
+ return;
100
+ emitConversation({ kind: 'accepted-message', conversation, message });
81
101
  }
82
102
  function announce(authority, at) {
83
103
  const version = roomVersion(at);
84
- for (const locator of locators.get(authority) ?? []){
85
- for (const observer of observers)observer({
86
- locator,
87
- roomVersion: version
88
- });
104
+ for (const locator of locators.get(authority) ?? []) {
105
+ emit({ kind: 'room-version', locator, roomVersion: version });
89
106
  }
90
107
  }
91
108
  function interrupt(authority) {
92
109
  const line = lines.get(authority);
93
- if (line !== undefined) lines.set(authority, {
94
- logEpoch: line.logEpoch,
95
- frames: []
96
- });
97
- }
98
- function resubscribed(authority, position, logEpoch) {
99
- if (position === undefined || logEpoch === undefined) {
100
- interrupt(authority);
101
- return;
102
- }
103
- const held = lines.get(authority)?.logEpoch;
104
- lines.set(authority, {
105
- logEpoch,
106
- frames: []
107
- });
108
- if (held !== logEpoch) announce(authority, {
109
- position,
110
- logEpoch
111
- });
110
+ if (line !== undefined)
111
+ lines.set(authority, { ...line, frames: [] });
112
112
  }
113
113
  function receive(frame) {
114
- if (frame.type === 'draft') {
115
- emitConversation({
116
- kind: 'growing-text',
117
- conversation: frame.conversation_id,
118
- turn: frame.turn_id,
119
- text: frame.text
120
- });
114
+ if (frame.type === 'signal') {
115
+ const mode = frame.service_mode;
116
+ if (mode !== 'open' && mode !== 'write-halted')
117
+ return;
118
+ for (const observer of modeObservers)
119
+ observer(mode);
121
120
  return;
122
121
  }
123
- if (frame.type === 'message') {
124
- const turn = messageTurn(frame.message);
125
- if (turn === null) return;
126
- emitConversation({
127
- kind: 'accepted-message',
128
- conversation: frame.conversation_id,
129
- message: {
130
- turn,
131
- message: structuredClone(frame.message)
132
- }
122
+ if (frame.type === 'opening') {
123
+ emit({
124
+ kind: 'opening',
125
+ expired: frame.expired.flatMap((authority) => locators.get(authority) ?? []),
133
126
  });
134
127
  return;
135
128
  }
136
- if (frame.type === 'session') {
137
- emitConversation({
138
- kind: 'session-summary',
139
- conversation: frame.conversation_id,
140
- summary: structuredClone(frame.summary)
141
- });
129
+ if (frame.type === 'conversations') {
130
+ emitConversation({ kind: 'list', entries: frame.conversations.map(listedConversation) });
142
131
  return;
143
132
  }
144
- if (frame.type === 'subscribed' && viewed.includes(frame.authority)) {
145
- const reconnect = subscribedConversations.has(frame.authority);
146
- subscribedConversations.add(frame.authority);
147
- void catchUp(frame.authority, reconnect);
133
+ if (frame.type === 'conversation') {
134
+ emitConversation({ kind: 'listed', entry: listedConversation(frame.conversation) });
148
135
  return;
149
136
  }
150
- if (frame.type === 'subscribed') {
151
- resubscribed(frame.authority, frame.position, frame.log_epoch);
137
+ if (frame.type === 'conversation_deleted') {
138
+ emitConversation({ kind: 'unlisted', conversation: frame.conversation_id });
152
139
  return;
153
140
  }
154
- if (frame.type !== 'entries') {
155
- interrupt(frame.authority);
141
+ if (frame.type === 'turn' ||
142
+ frame.type === 'process' ||
143
+ frame.type === 'commands' ||
144
+ frame.type === 'options' ||
145
+ frame.type === 'interactions' ||
146
+ frame.type === 'message') {
147
+ converse(frame);
156
148
  return;
157
149
  }
150
+ if (frame.type !== 'entries')
151
+ return;
158
152
  const at = position(frame.position);
159
- if (at === null) {
153
+ const after = position(frame.after);
154
+ if (at === null || after === null || after >= at) {
160
155
  interrupt(frame.authority);
161
156
  return;
162
157
  }
163
158
  const line = lines.get(frame.authority);
164
159
  if (line?.logEpoch === frame.log_epoch) {
160
+ if (line.held !== null && at <= line.held)
161
+ return;
165
162
  const last = line.frames.at(-1);
166
- const continued = last !== undefined && last.position + 1n === at;
167
- const next = continued ? [
168
- ...line.frames,
169
- {
170
- position: at,
171
- entries: frame.entries
172
- }
173
- ] : [
174
- {
175
- position: at,
176
- entries: frame.entries
177
- }
178
- ];
179
- lines.set(frame.authority, {
180
- logEpoch: line.logEpoch,
181
- frames: next.length > HELD_POSITIONS_MAX ? [] : next
182
- });
183
- } else {
163
+ if (last !== undefined && at <= last.position)
164
+ return;
165
+ const held = { after, position: at, entries: frame.entries };
166
+ const next = last !== undefined && after <= last.position ? [...line.frames, held] : [held];
167
+ lines.set(frame.authority, { ...line, frames: next.length > HELD_FRAMES_MAX ? [] : next });
168
+ }
169
+ else {
184
170
  interrupt(frame.authority);
185
171
  }
186
- announce(frame.authority, {
187
- position: frame.position,
188
- logEpoch: frame.log_epoch
189
- });
172
+ announce(frame.authority, { position: frame.position, logEpoch: frame.log_epoch });
190
173
  }
174
+ stream.observe(receive);
175
+ stream.opened(reopened);
176
+ stream.closed((revoked) => {
177
+ opening = null;
178
+ emit({ kind: 'closed', revoked });
179
+ });
191
180
  return {
181
+ runtime,
182
+ serviceMode: {
183
+ observe(notify) {
184
+ modeObservers.add(notify);
185
+ return () => {
186
+ modeObservers.delete(notify);
187
+ };
188
+ },
189
+ },
192
190
  conversations: {
193
- view (next) {
194
- const open = new Set(next);
195
- for (const conversation of subscribedConversations){
196
- if (!open.has(conversation)) subscribedConversations.delete(conversation);
197
- }
198
- viewed = [
199
- ...next
200
- ];
201
- subscribe();
191
+ replicate(next) {
192
+ replicated = [...next];
202
193
  },
203
- observe (notify) {
194
+ observe(notify) {
204
195
  conversationObservers.add(notify);
205
- if (!listening) {
206
- listening = true;
207
- stream.observe(receive);
208
- }
209
- return ()=>{
196
+ return () => {
210
197
  conversationObservers.delete(notify);
211
198
  };
199
+ },
200
+ },
201
+ observe(notify) {
202
+ observers.add(notify);
203
+ if (opening !== null) {
204
+ queueMicrotask(() => {
205
+ if (observers.has(notify))
206
+ notify({ kind: 'opened' });
207
+ });
212
208
  }
209
+ return () => {
210
+ observers.delete(notify);
211
+ };
212
+ },
213
+ send(rooms) {
214
+ for (const room of rooms) {
215
+ const line = lines.get(room.authority);
216
+ const held = position(room.position);
217
+ if (line === undefined || held === null || line.logEpoch !== room.log_epoch)
218
+ continue;
219
+ lines.set(room.authority, { ...line, frames: [], held });
220
+ }
221
+ const current = opening;
222
+ opening = null;
223
+ current?.hold(rooms, runtime);
213
224
  },
214
- track (entries) {
225
+ track(entries) {
215
226
  locators.clear();
216
- authorities.clear();
217
- for (const entry of entries){
218
- locators.set(entry.authority, [
219
- ...locators.get(entry.authority) ?? [],
220
- entry.locator
221
- ]);
222
- authorities.set(entry.locator, entry.authority);
227
+ for (const entry of entries) {
228
+ locators.set(entry.authority, [...(locators.get(entry.authority) ?? []), entry.locator]);
223
229
  if (lines.get(entry.authority)?.logEpoch !== entry.logEpoch) {
224
- lines.set(entry.authority, {
225
- logEpoch: entry.logEpoch,
226
- frames: []
227
- });
230
+ lines.set(entry.authority, { logEpoch: entry.logEpoch, frames: [], held: null });
228
231
  }
229
232
  }
230
- for (const authority of lines.keys()){
231
- if (!locators.has(authority)) lines.delete(authority);
233
+ for (const authority of lines.keys()) {
234
+ if (!locators.has(authority))
235
+ lines.delete(authority);
232
236
  }
233
237
  },
234
- select (named) {
235
- selected = [
236
- ...named
237
- ];
238
- subscribe();
239
- },
240
- observe (notify) {
241
- observers.add(notify);
242
- if (!listening) {
243
- listening = true;
244
- stream.observe(receive);
245
- }
246
- return ()=>{
247
- observers.delete(notify);
248
- };
238
+ reach(authority, at) {
239
+ const line = lines.get(authority);
240
+ const held = position(at.position);
241
+ if (line === undefined || held === null || line.logEpoch !== at.logEpoch)
242
+ return;
243
+ if (line.held !== null && held <= line.held)
244
+ return;
245
+ lines.set(authority, {
246
+ ...line,
247
+ frames: line.frames.filter((frame) => frame.position > held),
248
+ held,
249
+ });
249
250
  },
250
- advance (tracked, from) {
251
+ advance(tracked, from, holds) {
251
252
  const line = lines.get(tracked.authority);
252
253
  const start = position(from.position);
253
- if (line === undefined || start === null || line.logEpoch !== from.logEpoch) return null;
254
- const at = line.frames.findIndex((frame)=>frame.position === start + 1n);
255
- if (at === -1) return null;
256
- const pending = line.frames.slice(at);
254
+ if (line === undefined || start === null || line.logEpoch !== from.logEpoch)
255
+ return null;
256
+ const pending = line.frames.filter((frame) => frame.position > start);
257
+ const first = pending[0];
258
+ if (first === undefined || first.after > start)
259
+ return null;
257
260
  const documents = {};
258
261
  const revisions = {};
262
+ const changes = {};
259
263
  const removals = [];
260
- for (const frame of pending){
261
- for (const entry of frame.entries){
262
- if (!selects(tracked, entry.path)) continue;
264
+ const applied = [];
265
+ const schemas = new Map();
266
+ const forget = (path) => {
267
+ delete documents[path];
268
+ delete revisions[path];
269
+ delete changes[path];
270
+ const removed = removals.indexOf(path);
271
+ if (removed !== -1)
272
+ removals.splice(removed, 1);
273
+ };
274
+ for (const frame of pending) {
275
+ for (const entry of frame.entries) {
276
+ if (entry.kind === 'edited' &&
277
+ entry.revision !== undefined &&
278
+ holds?.(entry.path, entry.revision) === true) {
279
+ forget(entry.path);
280
+ continue;
281
+ }
282
+ applied.push(entry);
283
+ if (tracked.schemas.includes(entry.path))
284
+ schemas.set(entry.path, entry);
263
285
  if (entry.kind === 'removed') {
264
- delete documents[entry.path];
265
- delete revisions[entry.path];
266
- if (!removals.includes(entry.path)) removals.push(entry.path);
286
+ forget(entry.path);
287
+ removals.push(entry.path);
267
288
  continue;
268
289
  }
269
- if (entry.text === undefined) return null;
270
- if (mode === 'credential' && entry.revision === undefined) return null;
290
+ if (entry.text === undefined)
291
+ return null;
292
+ if (mode === 'credential' && entry.revision === undefined)
293
+ return null;
294
+ const head = headChange(entry);
295
+ if (head === null)
296
+ return null;
297
+ forget(entry.path);
271
298
  documents[entry.path] = entry.text;
272
- if (entry.revision !== undefined) revisions[entry.path] = entry.revision;
273
- const removed = removals.indexOf(entry.path);
274
- if (removed !== -1) removals.splice(removed, 1);
299
+ if (entry.revision !== undefined)
300
+ revisions[entry.path] = entry.revision;
301
+ if (head !== undefined)
302
+ changes[entry.path] = head;
275
303
  }
276
304
  }
277
305
  const last = pending.at(-1);
278
- if (last === undefined) return null;
279
- lines.set(tracked.authority, {
280
- logEpoch: line.logEpoch,
281
- frames: pending
282
- });
306
+ lines.set(tracked.authority, { ...line, frames: pending, held: last.position });
283
307
  return {
284
- kind: 'net-advancement',
285
- roomVersion: roomVersion({
286
- position: last.position.toString(),
287
- logEpoch: line.logEpoch
288
- }),
289
- documents,
290
- revisions,
291
- binaryTargets: [],
292
- removals: removals.sort()
308
+ page: {
309
+ kind: 'net-advancement',
310
+ roomVersion: roomVersion({ position: last.position.toString(), logEpoch: line.logEpoch }),
311
+ documents,
312
+ revisions,
313
+ changes,
314
+ binaryTargets: [],
315
+ removals: removals.sort(),
316
+ },
317
+ entries: applied,
318
+ schemas,
293
319
  };
294
- }
320
+ },
295
321
  };
296
322
  }
297
-
298
- //# sourceMappingURL=stream.js.map