@agentshouse/mdruntime-adapter 0.1.0-c17fae3d012d0 → 0.1.0-c192ba5f83f13

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 (69) hide show
  1. package/README.md +4 -4
  2. package/dist/actions.d.ts +3 -4
  3. package/dist/actions.d.ts.map +1 -1
  4. package/dist/actions.js +10 -327
  5. package/dist/adapter.d.ts +1 -0
  6. package/dist/adapter.d.ts.map +1 -1
  7. package/dist/adapter.js +283 -246
  8. package/dist/app-reference.d.ts +2 -0
  9. package/dist/app-reference.d.ts.map +1 -0
  10. package/dist/app-reference.js +2 -0
  11. package/dist/crockford.d.ts +4 -0
  12. package/dist/crockford.d.ts.map +1 -0
  13. package/dist/crockford.js +3 -0
  14. package/dist/discovery.d.ts +18 -23
  15. package/dist/discovery.d.ts.map +1 -1
  16. package/dist/discovery.js +80 -73
  17. package/dist/history.d.ts +12 -0
  18. package/dist/history.d.ts.map +1 -0
  19. package/dist/history.js +91 -0
  20. package/dist/house-model.d.ts +1895 -0
  21. package/dist/house-model.d.ts.map +1 -0
  22. package/dist/house-model.js +1034 -0
  23. package/dist/index.d.ts +10 -1
  24. package/dist/index.d.ts.map +1 -1
  25. package/dist/index.js +9 -0
  26. package/dist/model.d.ts +10 -18
  27. package/dist/model.d.ts.map +1 -1
  28. package/dist/model.js +39 -37
  29. package/dist/part.d.ts +14 -0
  30. package/dist/part.d.ts.map +1 -0
  31. package/dist/part.js +90 -0
  32. package/dist/read.d.ts +52 -41
  33. package/dist/read.d.ts.map +1 -1
  34. package/dist/read.js +248 -237
  35. package/dist/refusal.d.ts +4 -11
  36. package/dist/refusal.d.ts.map +1 -1
  37. package/dist/refusal.js +26 -17
  38. package/dist/room-stream.d.ts +19 -0
  39. package/dist/room-stream.d.ts.map +1 -0
  40. package/dist/room-stream.js +57 -0
  41. package/dist/room-types.d.ts +7 -0
  42. package/dist/room-types.d.ts.map +1 -0
  43. package/dist/room-types.js +126 -0
  44. package/dist/site.d.ts +1 -1
  45. package/dist/site.d.ts.map +1 -1
  46. package/dist/site.js +132 -142
  47. package/dist/stream.d.ts +16 -11
  48. package/dist/stream.d.ts.map +1 -1
  49. package/dist/stream.js +250 -143
  50. package/dist/transport.d.ts +116 -22
  51. package/dist/transport.d.ts.map +1 -1
  52. package/dist/transport.js +54 -10
  53. package/dist/upload.d.ts +12 -0
  54. package/dist/upload.d.ts.map +1 -0
  55. package/dist/upload.js +63 -0
  56. package/dist/version.d.ts +2 -0
  57. package/dist/version.d.ts.map +1 -1
  58. package/dist/version.js +10 -1
  59. package/dist/write.d.ts +18 -2
  60. package/dist/write.d.ts.map +1 -1
  61. package/dist/write.js +169 -42
  62. package/package.json +9 -6
  63. package/AGENTS.md +0 -207
  64. package/dist/gather.d.ts +0 -3
  65. package/dist/gather.d.ts.map +0 -1
  66. package/dist/gather.js +0 -40
  67. package/dist/links.d.ts +0 -12
  68. package/dist/links.d.ts.map +0 -1
  69. package/dist/links.js +0 -66
package/dist/stream.js CHANGED
@@ -1,45 +1,139 @@
1
- import { acceptedMessage } from './actions.js';
2
- import { headChange } from './read.js';
3
- import { roomVersion } from './version.js';
4
- const HELD_FRAMES_MAX = 256;
1
+ import { ROW_WEIGHT, stepped, } from '@agentshouse/mdruntime';
2
+ import { acceptedMessage, cursorPosition, houseHistory } from './history.js';
3
+ import { streamedModels } from './model.js';
4
+ import { answered, answering, binaryFileOf, folded, forgotten, headChange, revised, } from './read.js';
5
+ import { houseDoorRoute, } from './transport.js';
6
+ import { DECIMAL, roomVersion } from './version.js';
5
7
  function turnOperation(frame) {
6
8
  const { type: _type, conversation_id: _conversation, turn_id: _turn, ...operation } = frame;
7
9
  return structuredClone(operation);
8
10
  }
11
+ async function houseAgents(transport) {
12
+ const answer = await transport.door(houseDoorRoute('agents/read'), {});
13
+ const agents = answer.status === 200 ? answer.body?.agents : null;
14
+ if (!Array.isArray(agents))
15
+ return null;
16
+ return agents.flatMap((one) => {
17
+ const { id, name, retired_at: retired } = (one ?? {});
18
+ return typeof id === 'string' && typeof name === 'string'
19
+ ? [{ agent: id, name, retired: retired !== null && retired !== undefined }]
20
+ : [];
21
+ });
22
+ }
23
+ function listedConversation(listed) {
24
+ return {
25
+ conversation: listed.conversation_id,
26
+ agent: listed.agent_id,
27
+ agentName: listed.agent_name,
28
+ agentRetired: listed.agent_retired,
29
+ name: listed.name,
30
+ created: listed.created_at,
31
+ finished: listed.finished_at,
32
+ outcome: listed.outcome,
33
+ waiting: listed.waiting,
34
+ newest: listed.newest === null ? null : cursorPosition(listed.newest),
35
+ };
36
+ }
9
37
  function position(value) {
10
- if (!/^(?:0|[1-9][0-9]*)$/.test(value))
38
+ if (!DECIMAL.test(value))
11
39
  return null;
12
40
  return BigInt(value);
13
41
  }
14
- export function houseNotifications(transport, mode) {
15
- const stream = transport.stream;
16
- const lines = new Map();
17
- const locators = new Map();
42
+ function* changed(tracked, entries, version, schemas) {
43
+ const answer = answering();
44
+ for (const entry of entries) {
45
+ yield ROW_WEIGHT + (entry.text?.length ?? 0);
46
+ if (entry.kind === 'folder') {
47
+ folded(answer, entry.path, entry.removed === true);
48
+ continue;
49
+ }
50
+ if (entry.path === tracked.schema)
51
+ schemas.set(entry.path, entry);
52
+ forgotten(answer, entry.path);
53
+ if (entry.kind === 'removed') {
54
+ answer.removals.add(entry.path);
55
+ continue;
56
+ }
57
+ const binary = binaryFileOf(entry);
58
+ if (binary === undefined)
59
+ answer.documents.set(entry.path, entry.text ?? '');
60
+ else if (binary !== null)
61
+ answer.binaryFiles.set(entry.path, binary);
62
+ const revision = entry.revision ?? binary?.sha256;
63
+ if (revision !== undefined)
64
+ revised(answer, entry.path, revision);
65
+ const head = headChange(entry);
66
+ if (head)
67
+ answer.changes.set(entry.path, head);
68
+ }
69
+ return { kind: 'net-advancement', roomVersion: version, ...(yield* answered(answer)) };
70
+ }
71
+ async function changePage(tracked, entries, version, mode) {
18
72
  const schemas = new Map();
73
+ const page = await stepped(changed(tracked, entries, version, schemas));
74
+ const models = await streamedModels(schemas, tracked, mode);
75
+ return models === undefined ? page : { ...page, models };
76
+ }
77
+ export function houseNotifications(transport, mode, discovering) {
78
+ const stream = transport.stream;
79
+ const staged = new Map();
80
+ let socket = null;
81
+ const tracking = new Map();
19
82
  const observers = new Set();
20
83
  const conversationObservers = new Set();
21
84
  const modeObservers = new Set();
22
85
  let replicated = [];
86
+ let listedCursor = null;
23
87
  const live = new Set();
24
- let listening = false;
25
- let catching = false;
26
- function listen() {
27
- if (listening)
28
- return;
29
- listening = true;
30
- stream.observe(receive);
88
+ let opening = null;
89
+ let announced = null;
90
+ const runtime = crypto.randomUUID();
91
+ function emit(event) {
92
+ for (const observer of observers)
93
+ observer(event);
31
94
  }
32
95
  function emitConversation(event) {
33
- if (event.kind !== 'gap' && event.kind !== 'accepted-message')
96
+ if (event.kind === 'turn' ||
97
+ event.kind === 'questions' ||
98
+ event.kind === 'process' ||
99
+ event.kind === 'commands' ||
100
+ event.kind === 'options' ||
101
+ event.kind === 'starting' ||
102
+ event.kind === 'subagents' ||
103
+ event.kind === 'link' ||
104
+ event.kind === 'deliveries') {
34
105
  live.add(event.conversation);
106
+ }
35
107
  for (const observer of conversationObservers)
36
108
  observer(event);
37
109
  }
38
- function reopened() {
110
+ function reopened(next) {
111
+ opening = next;
112
+ announced = null;
113
+ socket = next;
114
+ staged.clear();
39
115
  const gapped = new Set([...replicated, ...live]);
40
116
  live.clear();
41
117
  for (const conversation of gapped)
42
118
  emitConversation({ kind: 'gap', conversation });
119
+ emit({ kind: 'opened' });
120
+ if (mode === 'site')
121
+ announce(next, null);
122
+ }
123
+ function announce(via, frame) {
124
+ void discovering.opened(frame).then((discovery) => {
125
+ if (socket !== via)
126
+ return;
127
+ if (discovery === null) {
128
+ via.close();
129
+ return;
130
+ }
131
+ announced = { kind: 'discovered', discovery };
132
+ emit(announced);
133
+ }, () => {
134
+ if (socket === via)
135
+ via.close();
136
+ });
43
137
  }
44
138
  function converse(frame) {
45
139
  const conversation = frame.conversation_id;
@@ -58,6 +152,7 @@ export function houseNotifications(transport, mode) {
58
152
  return;
59
153
  emitConversation({ kind: 'commands', conversation, commands: [] });
60
154
  emitConversation({ kind: 'options', conversation, options: [] });
155
+ emitConversation({ kind: 'subagents', conversation, subagents: [] });
61
156
  return;
62
157
  }
63
158
  if (frame.type === 'commands') {
@@ -72,6 +167,26 @@ export function houseNotifications(transport, mode) {
72
167
  emitConversation({ kind: 'options', conversation, options: structuredClone(frame.options) });
73
168
  return;
74
169
  }
170
+ if (frame.type === 'starting') {
171
+ emitConversation({ kind: 'starting', conversation, step: frame.step });
172
+ return;
173
+ }
174
+ if (frame.type === 'subagents') {
175
+ emitConversation({
176
+ kind: 'subagents',
177
+ conversation,
178
+ subagents: structuredClone(frame.subagents),
179
+ });
180
+ return;
181
+ }
182
+ if (frame.type === 'link') {
183
+ emitConversation({ kind: 'link', conversation, connected: frame.connected });
184
+ return;
185
+ }
186
+ if (frame.type === 'deliveries') {
187
+ emitConversation({ kind: 'deliveries', conversation, unknown: [...frame.unknown] });
188
+ return;
189
+ }
75
190
  if (frame.type === 'interactions') {
76
191
  emitConversation({
77
192
  kind: 'questions',
@@ -85,22 +200,32 @@ export function houseNotifications(transport, mode) {
85
200
  return;
86
201
  emitConversation({ kind: 'accepted-message', conversation, message });
87
202
  }
88
- function announce(authority, at, modelChanged) {
89
- const version = roomVersion(at);
90
- for (const locator of locators.get(authority) ?? []) {
91
- for (const observer of observers) {
92
- observer(modelChanged
93
- ? { locator, roomVersion: version, modelChanged: true }
94
- : { locator, roomVersion: version });
203
+ function stage(frame, via) {
204
+ const held = staged.get(frame.carry);
205
+ if (frame.index !== (held?.parts ?? 0)) {
206
+ staged.delete(frame.carry);
207
+ return;
208
+ }
209
+ if (held === undefined) {
210
+ for (const [carry, other] of staged) {
211
+ if (other.authority === frame.authority)
212
+ staged.delete(carry);
95
213
  }
96
214
  }
215
+ const next = held ?? { authority: frame.authority, parts: 0, entries: [] };
216
+ next.entries.push(...frame.entries);
217
+ next.parts += 1;
218
+ staged.set(frame.carry, next);
219
+ via?.received(frame.authority, frame.carry, frame.index);
97
220
  }
98
- function interrupt(authority) {
99
- const line = lines.get(authority);
100
- if (line !== undefined)
101
- lines.set(authority, { ...line, frames: [] });
221
+ function carried(frame) {
222
+ const held = staged.get(frame.carry);
223
+ staged.delete(frame.carry);
224
+ if ((held?.parts ?? 0) !== frame.parts)
225
+ return;
226
+ publish(frame, held?.entries ?? []);
102
227
  }
103
- function receive(frame) {
228
+ function handle(frame, via) {
104
229
  if (frame.type === 'signal') {
105
230
  const mode = frame.service_mode;
106
231
  if (mode !== 'open' && mode !== 'write-halted')
@@ -109,45 +234,91 @@ export function houseNotifications(transport, mode) {
109
234
  observer(mode);
110
235
  return;
111
236
  }
237
+ if (frame.type === 'opening') {
238
+ if (via !== null && mode === 'credential')
239
+ announce(via, frame);
240
+ return;
241
+ }
242
+ if (frame.type === 'caught_up') {
243
+ emit({
244
+ kind: 'caught-up',
245
+ expired: frame.expired.flatMap((authority) => (tracking.get(authority) ?? []).map((tracked) => tracked.locator)),
246
+ });
247
+ return;
248
+ }
249
+ if (frame.type === 'authority_changed') {
250
+ const change = discovering.changed(frame);
251
+ if (change !== null)
252
+ emit(change);
253
+ return;
254
+ }
255
+ if (frame.type === 'conversations') {
256
+ emitConversation({
257
+ kind: 'list',
258
+ entries: frame.conversations.map(listedConversation),
259
+ whole: frame.whole,
260
+ more: frame.more,
261
+ cursor: frame.cursor,
262
+ });
263
+ return;
264
+ }
265
+ if (frame.type === 'conversation') {
266
+ emitConversation({ kind: 'listed', entry: listedConversation(frame.conversation) });
267
+ return;
268
+ }
269
+ if (frame.type === 'conversation_deleted') {
270
+ emitConversation({ kind: 'unlisted', conversation: frame.conversation_id });
271
+ return;
272
+ }
112
273
  if (frame.type === 'turn' ||
113
274
  frame.type === 'process' ||
114
275
  frame.type === 'commands' ||
115
276
  frame.type === 'options' ||
277
+ frame.type === 'starting' ||
278
+ frame.type === 'subagents' ||
279
+ frame.type === 'link' ||
280
+ frame.type === 'deliveries' ||
116
281
  frame.type === 'interactions' ||
117
282
  frame.type === 'message') {
118
283
  converse(frame);
119
284
  return;
120
285
  }
121
- if (frame.type !== 'entries')
122
- return;
286
+ if (frame.type === 'part')
287
+ stage(frame, via);
288
+ else if (frame.type === 'carried')
289
+ carried(frame);
290
+ }
291
+ function publish(frame, entries) {
123
292
  const at = position(frame.position);
124
293
  const after = position(frame.after);
125
- if (at === null || after === null || after >= at) {
126
- interrupt(frame.authority);
294
+ if (at === null || after === null || after >= at)
127
295
  return;
296
+ const from = roomVersion({ position: frame.after, logEpoch: frame.log_epoch });
297
+ const version = roomVersion({ position: frame.position, logEpoch: frame.log_epoch });
298
+ for (const tracked of tracking.get(frame.authority) ?? []) {
299
+ emit({
300
+ kind: 'room-change',
301
+ locator: tracked.locator,
302
+ from,
303
+ roomVersion: version,
304
+ read: () => changePage(tracked, entries, version, mode),
305
+ });
128
306
  }
129
- const line = lines.get(frame.authority);
130
- if (line?.logEpoch === frame.log_epoch) {
131
- if (line.held !== null && at <= line.held)
132
- return;
133
- const last = line.frames.at(-1);
134
- if (last !== undefined && at <= last.position)
135
- return;
136
- const held = { after, position: at, entries: frame.entries };
137
- const next = last !== undefined && after <= last.position ? [...line.frames, held] : [held];
138
- lines.set(frame.authority, { ...line, frames: next.length > HELD_FRAMES_MAX ? [] : next });
139
- }
140
- else {
141
- interrupt(frame.authority);
142
- }
143
- const addresses = schemas.get(frame.authority) ?? [];
144
- announce(frame.authority, { position: frame.position, logEpoch: frame.log_epoch }, frame.entries.some((entry) => addresses.includes(entry.path)));
145
307
  }
308
+ stream.observe((frame) => handle(frame, socket));
309
+ stream.opened(reopened);
310
+ stream.closed(({ revoked, lost }) => {
311
+ opening = null;
312
+ announced = null;
313
+ socket = null;
314
+ staged.clear();
315
+ emit({ kind: 'closed', revoked, lost });
316
+ });
146
317
  return {
318
+ runtime,
147
319
  serviceMode: {
148
320
  observe(notify) {
149
321
  modeObservers.add(notify);
150
- listen();
151
322
  return () => {
152
323
  modeObservers.delete(notify);
153
324
  };
@@ -157,114 +328,50 @@ export function houseNotifications(transport, mode) {
157
328
  replicate(next) {
158
329
  replicated = [...next];
159
330
  },
331
+ hold(cursor) {
332
+ listedCursor = cursor;
333
+ },
160
334
  observe(notify) {
161
335
  conversationObservers.add(notify);
162
- listen();
163
- if (!catching) {
164
- catching = true;
165
- stream.opened(reopened);
166
- }
167
336
  return () => {
168
337
  conversationObservers.delete(notify);
169
338
  };
170
339
  },
171
- },
172
- track(entries) {
173
- locators.clear();
174
- schemas.clear();
175
- for (const entry of entries) {
176
- locators.set(entry.authority, [...(locators.get(entry.authority) ?? []), entry.locator]);
177
- schemas.set(entry.authority, entry.schemas);
178
- if (lines.get(entry.authority)?.logEpoch !== entry.logEpoch) {
179
- lines.set(entry.authority, { logEpoch: entry.logEpoch, frames: [], held: null });
340
+ ...(mode === 'credential'
341
+ ? {
342
+ agents: () => houseAgents(transport),
343
+ history: (request) => houseHistory(transport, request),
180
344
  }
181
- }
182
- for (const authority of lines.keys()) {
183
- if (!locators.has(authority))
184
- lines.delete(authority);
185
- }
345
+ : {}),
186
346
  },
187
- select() { },
188
347
  observe(notify) {
189
348
  observers.add(notify);
190
- listen();
349
+ const replayed = opening === null ? [] : [{ kind: 'opened' }, announced];
350
+ if (replayed.length > 0) {
351
+ queueMicrotask(() => {
352
+ for (const event of replayed)
353
+ if (event !== null && observers.has(notify))
354
+ notify(event);
355
+ });
356
+ }
191
357
  return () => {
192
358
  observers.delete(notify);
193
359
  };
194
360
  },
195
- hold(authority, at) {
196
- const line = lines.get(authority);
197
- const held = position(at.position);
198
- if (line === undefined || held === null || line.logEpoch !== at.logEpoch)
199
- return;
200
- lines.set(authority, { ...line, held });
361
+ send(rooms) {
362
+ opening = null;
363
+ announced = null;
364
+ socket?.hold(rooms, runtime, listedCursor);
201
365
  },
202
- advance(tracked, from, holds) {
203
- const line = lines.get(tracked.authority);
204
- const start = position(from.position);
205
- if (line === undefined || start === null || line.logEpoch !== from.logEpoch)
206
- return null;
207
- const pending = line.frames.filter((frame) => frame.position > start);
208
- const first = pending[0];
209
- if (first === undefined || first.after > start)
210
- return null;
211
- const documents = {};
212
- const revisions = {};
213
- const changes = {};
214
- const removals = [];
215
- const applied = [];
216
- const forget = (path) => {
217
- delete documents[path];
218
- delete revisions[path];
219
- delete changes[path];
220
- const removed = removals.indexOf(path);
221
- if (removed !== -1)
222
- removals.splice(removed, 1);
223
- };
224
- for (const frame of pending) {
225
- for (const entry of frame.entries) {
226
- if (entry.kind === 'edited' &&
227
- entry.text === undefined &&
228
- entry.revision !== undefined &&
229
- holds?.(entry.path, entry.revision) === true) {
230
- forget(entry.path);
231
- continue;
232
- }
233
- applied.push(entry);
234
- if (entry.kind === 'removed') {
235
- forget(entry.path);
236
- removals.push(entry.path);
237
- continue;
238
- }
239
- if (entry.text === undefined)
240
- return null;
241
- if (mode === 'credential' && entry.revision === undefined)
242
- return null;
243
- const head = headChange(entry);
244
- if (head === null)
245
- return null;
246
- forget(entry.path);
247
- documents[entry.path] = entry.text;
248
- if (entry.revision !== undefined)
249
- revisions[entry.path] = entry.revision;
250
- if (head !== undefined)
251
- changes[entry.path] = head;
252
- }
366
+ track(entries) {
367
+ tracking.clear();
368
+ for (const entry of entries) {
369
+ tracking.set(entry.authority, [...(tracking.get(entry.authority) ?? []), entry]);
370
+ }
371
+ for (const [carry, held] of staged) {
372
+ if (!tracking.has(held.authority))
373
+ staged.delete(carry);
253
374
  }
254
- const last = pending.at(-1);
255
- lines.set(tracked.authority, { ...line, frames: pending, held: last.position });
256
- return {
257
- page: {
258
- kind: 'net-advancement',
259
- roomVersion: roomVersion({ position: last.position.toString(), logEpoch: line.logEpoch }),
260
- documents,
261
- revisions,
262
- changes,
263
- binaryTargets: [],
264
- removals: removals.sort(),
265
- },
266
- entries: applied,
267
- };
268
375
  },
269
376
  };
270
377
  }