@agentshouse/mdruntime-adapter 0.1.0-alpha.9 → 0.1.0-c09c6f263662c

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 (64) hide show
  1. package/AGENTS.md +272 -33
  2. package/README.md +32 -0
  3. package/dist/actions.d.ts +43 -13
  4. package/dist/actions.d.ts.map +1 -1
  5. package/dist/actions.js +270 -351
  6. package/dist/adapter.d.ts.map +1 -1
  7. package/dist/adapter.js +361 -221
  8. package/dist/digest.js +1 -5
  9. package/dist/discovery.d.ts +23 -34
  10. package/dist/discovery.d.ts.map +1 -1
  11. package/dist/discovery.js +154 -231
  12. package/dist/gather.d.ts +4 -0
  13. package/dist/gather.d.ts.map +1 -0
  14. package/dist/gather.js +45 -0
  15. package/dist/handed.d.ts +15 -0
  16. package/dist/handed.d.ts.map +1 -0
  17. package/dist/handed.js +38 -0
  18. package/dist/index.d.ts +2 -2
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +1 -3
  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 +8 -18
  25. package/dist/model.d.ts.map +1 -1
  26. package/dist/model.js +63 -57
  27. package/dist/read.d.ts +40 -29
  28. package/dist/read.d.ts.map +1 -1
  29. package/dist/read.js +209 -208
  30. package/dist/referrer.d.ts +7 -0
  31. package/dist/referrer.d.ts.map +1 -0
  32. package/dist/referrer.js +35 -0
  33. package/dist/refusal.d.ts +5 -7
  34. package/dist/refusal.d.ts.map +1 -1
  35. package/dist/refusal.js +47 -69
  36. package/dist/site.d.ts.map +1 -1
  37. package/dist/site.js +144 -153
  38. package/dist/stream.d.ts +17 -6
  39. package/dist/stream.d.ts.map +1 -1
  40. package/dist/stream.js +251 -215
  41. package/dist/transport.d.ts +81 -33
  42. package/dist/transport.d.ts.map +1 -1
  43. package/dist/transport.js +4 -3
  44. package/dist/version.d.ts +2 -0
  45. package/dist/version.d.ts.map +1 -1
  46. package/dist/version.js +32 -18
  47. package/dist/write.d.ts +7 -8
  48. package/dist/write.d.ts.map +1 -1
  49. package/dist/write.js +196 -206
  50. package/package.json +5 -13
  51. package/dist/actions.js.map +0 -1
  52. package/dist/adapter.js.map +0 -1
  53. package/dist/digest.js.map +0 -1
  54. package/dist/discovery.js.map +0 -1
  55. package/dist/index.js.map +0 -1
  56. package/dist/map.js.map +0 -1
  57. package/dist/model.js.map +0 -1
  58. package/dist/read.js.map +0 -1
  59. package/dist/refusal.js.map +0 -1
  60. package/dist/site.js.map +0 -1
  61. package/dist/stream.js.map +0 -1
  62. package/dist/transport.js.map +0 -1
  63. package/dist/version.js.map +0 -1
  64. package/dist/write.js.map +0 -1
package/dist/stream.js CHANGED
@@ -1,298 +1,334 @@
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 { binaryFileOf, 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);
224
+ },
225
+ reopen() {
226
+ const current = opening;
227
+ opening = null;
228
+ current?.close();
213
229
  },
214
- track (entries) {
230
+ track(entries) {
215
231
  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);
232
+ for (const entry of entries) {
233
+ locators.set(entry.authority, [...(locators.get(entry.authority) ?? []), entry.locator]);
223
234
  if (lines.get(entry.authority)?.logEpoch !== entry.logEpoch) {
224
- lines.set(entry.authority, {
225
- logEpoch: entry.logEpoch,
226
- frames: []
227
- });
235
+ lines.set(entry.authority, { logEpoch: entry.logEpoch, frames: [], held: null });
228
236
  }
229
237
  }
230
- for (const authority of lines.keys()){
231
- if (!locators.has(authority)) lines.delete(authority);
238
+ for (const authority of lines.keys()) {
239
+ if (!locators.has(authority))
240
+ lines.delete(authority);
232
241
  }
233
242
  },
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
- };
243
+ reach(authority, at) {
244
+ const line = lines.get(authority);
245
+ const held = position(at.position);
246
+ if (line === undefined || held === null || line.logEpoch !== at.logEpoch)
247
+ return;
248
+ if (line.held !== null && held <= line.held)
249
+ return;
250
+ lines.set(authority, {
251
+ ...line,
252
+ frames: line.frames.filter((frame) => frame.position > held),
253
+ held,
254
+ });
249
255
  },
250
- advance (tracked, from) {
256
+ advance(tracked, from, holds) {
251
257
  const line = lines.get(tracked.authority);
252
258
  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);
259
+ if (line === undefined || start === null || line.logEpoch !== from.logEpoch)
260
+ return null;
261
+ const pending = line.frames.filter((frame) => frame.position > start);
262
+ const first = pending[0];
263
+ if (first === undefined || first.after > start)
264
+ return null;
257
265
  const documents = {};
258
266
  const revisions = {};
267
+ const changes = {};
268
+ const binaryFiles = {};
259
269
  const removals = [];
260
- for (const frame of pending){
261
- for (const entry of frame.entries){
262
- if (!selects(tracked, entry.path)) continue;
270
+ const applied = [];
271
+ const schemas = new Map();
272
+ const forget = (path) => {
273
+ delete documents[path];
274
+ delete revisions[path];
275
+ delete changes[path];
276
+ delete binaryFiles[path];
277
+ const removed = removals.indexOf(path);
278
+ if (removed !== -1)
279
+ removals.splice(removed, 1);
280
+ };
281
+ for (const frame of pending) {
282
+ for (const entry of frame.entries) {
283
+ if (entry.kind === 'edited' &&
284
+ entry.revision !== undefined &&
285
+ holds?.(entry.path, entry.revision) === true) {
286
+ forget(entry.path);
287
+ continue;
288
+ }
289
+ applied.push(entry);
290
+ if (tracked.schemas.includes(entry.path))
291
+ schemas.set(entry.path, entry);
263
292
  if (entry.kind === 'removed') {
264
- delete documents[entry.path];
265
- delete revisions[entry.path];
266
- if (!removals.includes(entry.path)) removals.push(entry.path);
293
+ forget(entry.path);
294
+ removals.push(entry.path);
267
295
  continue;
268
296
  }
269
- if (entry.text === undefined) return null;
270
- if (mode === 'credential' && entry.revision === undefined) return null;
271
- 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);
297
+ const binary = binaryFileOf(entry);
298
+ if (binary === null || (binary === undefined && entry.text === undefined))
299
+ return null;
300
+ if (mode === 'credential' && entry.revision === undefined)
301
+ return null;
302
+ const head = headChange(entry);
303
+ if (head === null)
304
+ return null;
305
+ forget(entry.path);
306
+ if (binary === undefined)
307
+ documents[entry.path] = entry.text;
308
+ else
309
+ binaryFiles[entry.path] = binary;
310
+ const revision = entry.revision ?? binary?.sha256;
311
+ if (revision !== undefined)
312
+ revisions[entry.path] = revision;
313
+ if (head !== undefined)
314
+ changes[entry.path] = head;
275
315
  }
276
316
  }
277
317
  const last = pending.at(-1);
278
- if (last === undefined) return null;
279
- lines.set(tracked.authority, {
280
- logEpoch: line.logEpoch,
281
- frames: pending
282
- });
318
+ lines.set(tracked.authority, { ...line, frames: pending, held: last.position });
283
319
  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()
320
+ page: {
321
+ kind: 'net-advancement',
322
+ roomVersion: roomVersion({ position: last.position.toString(), logEpoch: line.logEpoch }),
323
+ documents,
324
+ revisions,
325
+ changes,
326
+ binaryFiles,
327
+ removals: removals.sort(),
328
+ },
329
+ entries: applied,
330
+ schemas,
293
331
  };
294
- }
332
+ },
295
333
  };
296
334
  }
297
-
298
- //# sourceMappingURL=stream.js.map