@agentshouse/mdruntime-adapter 0.1.0-c674b14001ced → 0.1.0-c67edc489e9f6

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 (55) hide show
  1. package/README.md +1 -1
  2. package/dist/actions.d.ts +3 -1
  3. package/dist/actions.d.ts.map +1 -1
  4. package/dist/actions.js +22 -6
  5. package/dist/adapter.d.ts.map +1 -1
  6. package/dist/adapter.js +73 -100
  7. package/dist/chunks.d.ts +38 -0
  8. package/dist/chunks.d.ts.map +1 -0
  9. package/dist/chunks.js +247 -0
  10. package/dist/discovery.d.ts +10 -3
  11. package/dist/discovery.d.ts.map +1 -1
  12. package/dist/discovery.js +34 -8
  13. package/dist/history.d.ts.map +1 -1
  14. package/dist/history.js +3 -2
  15. package/dist/house-model.d.ts +86 -359
  16. package/dist/house-model.d.ts.map +1 -1
  17. package/dist/house-model.js +81 -206
  18. package/dist/index.d.ts +4 -3
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +3 -2
  21. package/dist/model.d.ts +5 -7
  22. package/dist/model.d.ts.map +1 -1
  23. package/dist/model.js +30 -18
  24. package/dist/read.d.ts +19 -13
  25. package/dist/read.d.ts.map +1 -1
  26. package/dist/read.js +46 -168
  27. package/dist/refusal.d.ts +1 -0
  28. package/dist/refusal.d.ts.map +1 -1
  29. package/dist/refusal.js +4 -0
  30. package/dist/room-types.d.ts +0 -2
  31. package/dist/room-types.d.ts.map +1 -1
  32. package/dist/room-types.js +6 -11
  33. package/dist/search.d.ts +5 -0
  34. package/dist/search.d.ts.map +1 -0
  35. package/dist/search.js +47 -0
  36. package/dist/site.d.ts.map +1 -1
  37. package/dist/site.js +33 -41
  38. package/dist/space-schema.d.ts +7 -0
  39. package/dist/space-schema.d.ts.map +1 -0
  40. package/dist/space-schema.js +18 -0
  41. package/dist/stream.d.ts +6 -4
  42. package/dist/stream.d.ts.map +1 -1
  43. package/dist/stream.js +92 -24
  44. package/dist/transport.d.ts +11 -12
  45. package/dist/transport.d.ts.map +1 -1
  46. package/dist/transport.js +12 -12
  47. package/dist/write.d.ts.map +1 -1
  48. package/dist/write.js +4 -11
  49. package/package.json +2 -2
  50. package/dist/part.d.ts +0 -14
  51. package/dist/part.d.ts.map +0 -1
  52. package/dist/part.js +0 -90
  53. package/dist/room-stream.d.ts +0 -19
  54. package/dist/room-stream.d.ts.map +0 -1
  55. package/dist/room-stream.js +0 -57
package/README.md CHANGED
@@ -25,7 +25,7 @@ host.attach(port, 'my-app');
25
25
 
26
26
  App code never imports this package; it reaches the Runtime only through the
27
27
  client its host hands it. The rules an App's agent follows are the `apps/` pages
28
- of House's Guide, which House's `open_guide` opens.
28
+ of House's Guide, which House's `guide/open` opens.
29
29
 
30
30
  ## License
31
31
 
package/dist/actions.d.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  import type { ActionDeclaration, ActionRequest, ActionResult } from '@agentshouse/mdruntime';
2
2
  import type { HouseDoorAnswer, HouseTransport } from './transport.js';
3
3
  export type HouseActDoor = Pick<HouseTransport, 'door'>;
4
+ export declare const HOUSE_UNKEPT_READS: readonly string[];
5
+ export declare const HOUSE_LEADING_READS: readonly string[];
4
6
  export declare function declaredActions(routes: readonly string[]): readonly ActionDeclaration[];
5
7
  export declare function houseAction(transport: HouseActDoor, request: ActionRequest): Promise<ActionResult>;
6
- export declare function actionOutcome(answer: HouseDoorAnswer, name: string): ActionResult;
8
+ export declare function actionOutcome(answer: HouseDoorAnswer, name: string, input: unknown): ActionResult;
7
9
  //# sourceMappingURL=actions.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"actions.d.ts","sourceRoot":"","sources":["../src/actions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAE7F,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAEtE,MAAM,MAAM,YAAY,GAAG,IAAI,CAAC,cAAc,EAAE,MAAM,CAAC,CAAC;AAExD,wBAAgB,eAAe,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,GAAG,SAAS,iBAAiB,EAAE,CAEvF;AAED,wBAAgB,WAAW,CACzB,SAAS,EAAE,YAAY,EACvB,OAAO,EAAE,aAAa,GACrB,OAAO,CAAC,YAAY,CAAC,CAIvB;AAoBD,wBAAgB,aAAa,CAAC,MAAM,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM,GAAG,YAAY,CAQjF"}
1
+ {"version":3,"file":"actions.d.ts","sourceRoot":"","sources":["../src/actions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAE7F,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAEtE,MAAM,MAAM,YAAY,GAAG,IAAI,CAAC,cAAc,EAAE,MAAM,CAAC,CAAC;AAExD,eAAO,MAAM,kBAAkB,EAAE,SAAS,MAAM,EAG/C,CAAC;AAEF,eAAO,MAAM,mBAAmB,EAAE,SAAS,MAAM,EAKhD,CAAC;AAEF,wBAAgB,eAAe,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,GAAG,SAAS,iBAAiB,EAAE,CAEvF;AAED,wBAAgB,WAAW,CACzB,SAAS,EAAE,YAAY,EACvB,OAAO,EAAE,aAAa,GACrB,OAAO,CAAC,YAAY,CAAC,CAIvB;AAwBD,wBAAgB,aAAa,CAAC,MAAM,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,YAAY,CAajG"}
package/dist/actions.js CHANGED
@@ -1,4 +1,14 @@
1
- import { doorRefusal } from './refusal.js';
1
+ import { doorRefusal, resetOf } from './refusal.js';
2
+ export const HOUSE_UNKEPT_READS = [
3
+ 'account/security/read',
4
+ 'clients/last-use/list',
5
+ ];
6
+ export const HOUSE_LEADING_READS = [
7
+ 'rooms/list',
8
+ 'apps/list',
9
+ 'sites/list',
10
+ 'rooms/members/list',
11
+ ];
2
12
  export function declaredActions(routes) {
3
13
  return routes.map((name) => ({ name, inputShape: {}, resultKind: 'house', room: 'room_ref' }));
4
14
  }
@@ -25,14 +35,20 @@ function malformed() {
25
35
  function unimplemented(name) {
26
36
  return { kind: 'rejected', code: 'action-unimplemented', fields: { name }, retryable: false };
27
37
  }
28
- export function actionOutcome(answer, name) {
38
+ function numbered(input) {
39
+ return typeof input?.operation_id === 'string';
40
+ }
41
+ export function actionOutcome(answer, name, input) {
29
42
  if (answer.status === 401)
30
43
  return { kind: 'revoked-authorization' };
31
44
  const refusal = doorRefusal(answer.body);
32
- if (refusal?.code === 'operation_contended')
45
+ if (refusal?.code === 'operation_contended' && numbered(input)) {
33
46
  return { kind: 'transport-uncertainty' };
34
- if (refusal !== null)
35
- return { kind: 'rejected', ...refusal };
47
+ }
48
+ if (refusal !== null) {
49
+ const reset = resetOf(refusal);
50
+ return { kind: 'rejected', ...refusal, ...(reset === undefined ? {} : { reset }) };
51
+ }
36
52
  if (answer.status === 404)
37
53
  return unimplemented(name);
38
54
  if (answer.status === 0 || answer.status >= 400)
@@ -40,5 +56,5 @@ export function actionOutcome(answer, name) {
40
56
  return { kind: 'action', result: answer.body };
41
57
  }
42
58
  async function carry(transport, route, name, input) {
43
- return actionOutcome(await transport.door(route, input), name);
59
+ return actionOutcome(await transport.door(route, input), name, input);
44
60
  }
@@ -1 +1 @@
1
- {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAIV,OAAO,EAIP,aAAa,EAcd,MAAM,wBAAwB,CAAC;AA+BhC,OAAO,EAGL,KAAK,kBAAkB,EACvB,KAAK,cAAc,EACpB,MAAM,gBAAgB,CAAC;AAmBxB,MAAM,MAAM,yBAAyB,GACjC;IACE,SAAS,EAAE,cAAc,CAAC;IAC1B,SAAS,IAAI,OAAO,CAAC,aAAa,CAAC,CAAC;CACrC,GACD;IAAE,IAAI,EAAE,kBAAkB,CAAC;IAAC,QAAQ,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC;AA+DrD,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,yBAAyB,GAAG,OAAO,CAwbpF"}
1
+ {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAIV,OAAO,EAIP,aAAa,EAed,MAAM,wBAAwB,CAAC;AAyBhC,OAAO,EAGL,KAAK,kBAAkB,EACvB,KAAK,cAAc,EACpB,MAAM,gBAAgB,CAAC;AAoBxB,MAAM,MAAM,yBAAyB,GACjC;IACE,SAAS,EAAE,cAAc,CAAC;IAC1B,SAAS,IAAI,OAAO,CAAC,aAAa,CAAC,CAAC;CACrC,GACD;IAAE,IAAI,EAAE,kBAAkB,CAAC;IAAC,QAAQ,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC;AAoBrD,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,yBAAyB,GAAG,OAAO,CAkbpF"}
package/dist/adapter.js CHANGED
@@ -1,17 +1,19 @@
1
1
  import { stepped } from '@agentshouse/mdruntime';
2
2
  import { capabilities, discoveredRoom, mapDiscovery, parseAuthorityChange, parseDiscovery, trackedFrom, } from './discovery.js';
3
3
  import { createSiteAdapter } from './site.js';
4
- import { authorityRoot, bootstrapping, parseFileAddress, parseGrant, } from './read.js';
5
- import { roomStreams } from './room-stream.js';
6
- import { sha256Hex } from './digest.js';
4
+ import { parseFileAddress, parseGrant } from './read.js';
5
+ import { chunkedHead, chunkLister, chunkPart } from './chunks.js';
6
+ import { spaceSchemas } from './space-schema.js';
7
7
  import { finishedUpload, houseUpload } from './upload.js';
8
- import { houseAction } from './actions.js';
8
+ import { houseAction, HOUSE_LEADING_READS, HOUSE_UNKEPT_READS } from './actions.js';
9
+ import { houseSearch } from './search.js';
9
10
  import { classifyDoor, doorRefusal } from './refusal.js';
10
11
  import { houseNotifications, } from './stream.js';
11
12
  import { houseDoorRoute, } from './transport.js';
12
13
  import { batchPaths, deltaBody, deltaBytes, deltaDigest, historyOutcome, houseChanges, houseMove, mutationOutcome, operationHead, rejectedMapping, revisionBody, uploadable, } from './write.js';
14
+ import { HOUSE_TYPED_PATHS } from './house-model.js';
13
15
  import { HOUSE_NAMEABLE_ROOM_TYPES, HOUSE_ROOM_TYPES } from './room-types.js';
14
- import { followsVersion, logPosition, presented, reachedVersion, roomVersion } from './version.js';
16
+ import { followsVersion, reachedVersion, roomVersion } from './version.js';
15
17
  const UNCERTAIN = { kind: 'transport-uncertainty' };
16
18
  const UNCERTAIN_READ = { kind: 'transport-uncertainty' };
17
19
  const OCTET_STREAM = 'application/octet-stream';
@@ -19,43 +21,20 @@ const EXPIRED_ADDRESS = 403;
19
21
  function lapsedGrant(outcome) {
20
22
  return outcome.kind === 'rejected' && outcome.code === 'upload_unavailable';
21
23
  }
22
- async function heldFiles(authority, held, bound) {
23
- const listed = await held.files(null, bound + 1);
24
- const files = listed.slice(0, bound);
25
- const through = listed.length > bound ? (files.at(-1)?.path ?? null) : null;
26
- return files.length === 0 ? null : { room: authority, through, files };
27
- }
28
- async function reused(scope, held) {
29
- const sent = new Set(scope.contents.map((entry) => entry.path));
30
- const contents = [...scope.contents];
31
- const asked = scope.files.filter((file) => file.binary === undefined && file.outline === undefined && !sent.has(file.path));
32
- if (asked.length === 0)
33
- return scope;
34
- if (held === undefined)
35
- return null;
36
- const kept = await held.contents(asked.map(({ path }) => path));
37
- for (const file of asked) {
38
- const found = kept.get(file.path);
39
- if (found === undefined ||
40
- found.revision !== file.revision ||
41
- (await sha256Hex(found.content)) !== file.sha256) {
42
- return null;
43
- }
44
- contents.push({ path: file.path, content: found.content });
45
- }
46
- return { ...scope, contents };
47
- }
48
24
  export function createAgentsHouseAdapter(options) {
49
25
  if ('site' in options)
50
26
  return createSiteAdapter(options.site, options.snapshot === true);
51
27
  const transport = options.transport;
28
+ const spaces = spaceSchemas();
52
29
  const notifications = houseNotifications(transport, 'credential', {
53
30
  opened: async (frame) => (frame === null ? null : opened(frame)),
54
31
  changed,
55
- });
32
+ expired: (authorities) => lister.askAll(authorities.flatMap((authority) => [...tracked.values()].filter((entry) => entry.authority === authority))),
33
+ }, spaces);
56
34
  const tracked = new Map();
57
- const rooms = roomStreams('credential');
58
- const unreused = new Set();
35
+ const lister = chunkLister(transport, () => declared().max_prefixes);
36
+ const chunked = new Map();
37
+ const renewals = new WeakMap();
59
38
  let bounds;
60
39
  function declared() {
61
40
  if (bounds === undefined)
@@ -68,6 +47,7 @@ export function createAgentsHouseAdapter(options) {
68
47
  return null;
69
48
  bounds = document.bounds;
70
49
  tracked.clear();
50
+ lister.forget();
71
51
  for (const entry of trackedFrom(document))
72
52
  tracked.set(entry.locator, entry);
73
53
  notifications.track([...tracked.values()]);
@@ -92,65 +72,52 @@ export function createAgentsHouseAdapter(options) {
92
72
  member: change.member,
93
73
  };
94
74
  }
95
- async function streamedPage(entry, token, held) {
96
- const part = await rooms.part(entry, token);
97
- if ('kind' in part)
98
- return part;
99
- const scope = await reused(part.scope, held);
100
- if (scope === null) {
101
- unreused.add(entry.locator);
102
- return UNCERTAIN_READ;
103
- }
104
- return {
105
- ...(await stepped(bootstrapping(scope))),
106
- ...(part.models === undefined ? {} : { models: part.models }),
107
- ...(part.pageToken === undefined ? {} : { pageToken: part.pageToken }),
108
- bytes: part.bytes,
109
- };
75
+ async function listedRoom(entry) {
76
+ const listed = await lister.list(entry);
77
+ if ('kind' in listed)
78
+ return listed;
79
+ const head = await chunkedHead(listed, entry, spaces);
80
+ if (head.kind === 'bootstrap')
81
+ chunked.set(entry.locator, listed);
82
+ return head;
110
83
  }
111
- async function namedDocument(entry, path, signal) {
112
- const answered = await transport.streamed(houseDoorRoute('bootstrap'), { room_ref: entry.authority, prefixes: [`${authorityRoot(entry.handle)}/${path}`] }, signal ?? new AbortController().signal);
113
- if (answered.room === null)
114
- return classifyDoor(answered.status, answered.body, entry.locator) ?? UNCERTAIN_READ;
115
- const opened = await rooms.head(entry, answered.room, undefined, signal);
116
- if (opened.kind !== 'bootstrap')
117
- return opened;
118
- const files = [];
119
- const contents = [];
120
- let last = null;
121
- for (let token = opened.pageToken; token !== undefined;) {
122
- const part = await rooms.part(entry, token);
123
- if ('kind' in part)
124
- return part;
125
- files.push(...part.scope.files);
126
- contents.push(...part.scope.contents);
127
- last = part.scope;
128
- token = part.pageToken;
129
- }
130
- if (last === null)
131
- return UNCERTAIN_READ;
132
- return stepped(bootstrapping({ ...last, files, contents }));
84
+ async function chunkRead(entry, id) {
85
+ const room = chunked.get(entry.locator);
86
+ const address = room?.chunks.get(id);
87
+ if (room === undefined || address === undefined)
88
+ return null;
89
+ const read = await transport.download(address.url);
90
+ if (read.status !== EXPIRED_ADDRESS)
91
+ return { room, read };
92
+ const renewed = await renewal(entry, room);
93
+ if ('kind' in renewed || !renewed.chunks.has(id))
94
+ return null;
95
+ chunked.set(entry.locator, renewed);
96
+ return { room: renewed, read: await transport.download(renewed.chunks.get(id).url) };
133
97
  }
134
- async function openedRoom(entry, request) {
135
- const reusing = unreused.delete(entry.locator) ? undefined : request.held;
136
- const files = reusing === undefined
137
- ? null
138
- : await heldFiles(entry.authority, reusing, declared().max_held_files);
139
- const answered = await transport.streamed(houseDoorRoute('bootstrap'), {
140
- room_ref: entry.authority,
141
- prefixes: [authorityRoot(entry.handle)],
142
- ...(files === null ? {} : { held: [files] }),
143
- }, request.signal ?? new AbortController().signal);
144
- if (answered.room === null)
145
- return classifyDoor(answered.status, answered.body, entry.locator) ?? UNCERTAIN_READ;
146
- return rooms.head(entry, answered.room, request.arriving, request.signal);
98
+ function renewal(entry, stale) {
99
+ const held = renewals.get(stale);
100
+ if (held !== undefined)
101
+ return held;
102
+ const renewed = lister.renew(entry, [...chunked.keys()].flatMap((locator) => tracked.get(locator) ?? []));
103
+ renewals.set(stale, renewed);
104
+ return renewed;
147
105
  }
148
- function held(rooms) {
149
- notifications.send(rooms.flatMap((room) => {
150
- const entry = tracked.get(room.locator);
151
- const at = logPosition(room.roomVersion);
152
- return entry === undefined || at === null ? [] : [presented(entry.authority, at)];
153
- }));
106
+ function hold(rooms) {
107
+ for (const room of rooms) {
108
+ const listed = chunked.get(room.locator);
109
+ if (listed !== undefined &&
110
+ room.roomVersion !== null &&
111
+ reachedVersion(room.roomVersion, roomVersion(listed), listed.logEpoch))
112
+ chunked.delete(room.locator);
113
+ }
114
+ notifications.send(rooms);
115
+ }
116
+ async function chunkPage(entry, id, arriving) {
117
+ const fetched = await chunkRead(entry, id);
118
+ if (fetched === null || fetched.read.status !== 200)
119
+ return UNCERTAIN_READ;
120
+ return chunkPart(fetched.read.body, fetched.room, id, arriving);
154
121
  }
155
122
  function authorityFor(locator) {
156
123
  return tracked.get(locator);
@@ -175,7 +142,7 @@ export function createAgentsHouseAdapter(options) {
175
142
  if (handed !== undefined && handed.sha256 === sha256) {
176
143
  return { body: { ...head, paths, upload: handed, runtime: notifications.runtime } };
177
144
  }
178
- const minted = await transport.door(houseDoorRoute('batch-upload'), {
145
+ const minted = await transport.door(houseDoorRoute('files/batch-upload'), {
179
146
  ...head,
180
147
  paths,
181
148
  bytes,
@@ -228,11 +195,13 @@ export function createAgentsHouseAdapter(options) {
228
195
  : { body: inline };
229
196
  if ('outcome' in prepared)
230
197
  return prepared.outcome;
231
- const answer = await transport.door(houseDoorRoute('batch'), prepared.body);
198
+ const answer = await transport.door(houseDoorRoute('files/batch'), prepared.body);
232
199
  const stagedFiles = await staged(answer, request);
233
200
  if (stagedFiles === false)
234
201
  return UNCERTAIN;
235
- const settled = stagedFiles === null ? answer : await transport.door(houseDoorRoute('batch'), prepared.body);
202
+ const settled = stagedFiles === null
203
+ ? answer
204
+ : await transport.door(houseDoorRoute('files/batch'), prepared.body);
236
205
  if (stagedFiles === true && (await staged(settled, request)) !== null)
237
206
  return UNCERTAIN;
238
207
  return stepped(mutationOutcome(settled.status, settled.body, entry.locator, entry.handle, into === undefined
@@ -300,7 +269,7 @@ export function createAgentsHouseAdapter(options) {
300
269
  const entry = authorityFor(request.locator);
301
270
  if (entry === undefined)
302
271
  return null;
303
- return () => transport.door(houseDoorRoute('files/get'), {
272
+ return () => transport.door(houseDoorRoute('files/read'), {
304
273
  room_ref: entry.authority,
305
274
  path: request.path,
306
275
  revision: request.revision,
@@ -309,6 +278,9 @@ export function createAgentsHouseAdapter(options) {
309
278
  return {
310
279
  roomTypes: HOUSE_ROOM_TYPES,
311
280
  nameableRoomTypes: HOUSE_NAMEABLE_ROOM_TYPES,
281
+ typedPaths: HOUSE_TYPED_PATHS,
282
+ unkeptReads: HOUSE_UNKEPT_READS,
283
+ leadingReads: HOUSE_LEADING_READS,
312
284
  async authorize() {
313
285
  return options.authorize();
314
286
  },
@@ -316,11 +288,9 @@ export function createAgentsHouseAdapter(options) {
316
288
  const entry = authorityFor(request.locator);
317
289
  if (entry === undefined)
318
290
  return { kind: 'room-not-readable', locator: request.locator };
319
- if (request.path !== undefined)
320
- return namedDocument(entry, request.path, request.signal);
321
291
  return request.pageToken === undefined
322
- ? openedRoom(entry, request)
323
- : streamedPage(entry, request.pageToken, request.held);
292
+ ? listedRoom(entry)
293
+ : chunkPage(entry, request.pageToken, request.arriving);
324
294
  },
325
295
  async mutate(_authorization, request, keep) {
326
296
  const entry = authorityFor(request.locator);
@@ -351,7 +321,7 @@ export function createAgentsHouseAdapter(options) {
351
321
  const entry = authorityFor(request.locator);
352
322
  if (entry === undefined)
353
323
  return { kind: 'room-not-readable', locator: request.locator };
354
- const answer = await transport.door(houseDoorRoute('revisions'), revisionBody({
324
+ const answer = await transport.door(houseDoorRoute('files/revisions/list'), revisionBody({
355
325
  path: request.path,
356
326
  handle: entry.handle,
357
327
  ...(request.revision === undefined ? {} : { revision: request.revision }),
@@ -412,8 +382,11 @@ export function createAgentsHouseAdapter(options) {
412
382
  async action(_authorization, request) {
413
383
  return withAt(await houseAction(transport, request));
414
384
  },
385
+ search(_authorization, request) {
386
+ return houseSearch(transport, [...tracked.values()], request);
387
+ },
415
388
  roomStream: {
416
- hold: held,
389
+ hold,
417
390
  observe: notifications.observe,
418
391
  },
419
392
  conversations: notifications.conversations,
@@ -0,0 +1,38 @@
1
+ import { type AdapterFailure, type BootstrapPart, type BootstrapResult } from '@agentshouse/mdruntime';
2
+ import type { TrackedAuthority } from './discovery.js';
3
+ import { type HouseModels, type HouseScope } from './read.js';
4
+ import type { SpaceSchemas } from './space-schema.js';
5
+ import { type HouseDoor, type HouseMode } from './transport.js';
6
+ import { type LogPosition } from './version.js';
7
+ export type ChunkAddress = {
8
+ readonly url: string;
9
+ readonly bytes: number;
10
+ };
11
+ export type ChunkedRoom = LogPosition & {
12
+ readonly anchor: string;
13
+ readonly mode: HouseMode;
14
+ readonly chunks: ReadonlyMap<string, ChunkAddress>;
15
+ readonly parts: readonly BootstrapPart[];
16
+ readonly names: ReadonlyMap<string, string>;
17
+ readonly models: HouseModels;
18
+ };
19
+ export type Listed = ChunkedRoom | {
20
+ readonly kind: 'not-ready';
21
+ } | AdapterFailure;
22
+ type Answered = {
23
+ readonly status: number;
24
+ readonly body: unknown;
25
+ };
26
+ export declare function partId(logEpoch: string, hash: string): string;
27
+ export declare function siteChunkList(answer: Answered, locator: string): Listed;
28
+ export declare function chunkedHead(listed: ChunkedRoom, entry: TrackedAuthority, spaces: SpaceSchemas): Promise<BootstrapResult>;
29
+ export declare function chunkPart(body: ReadableStream<Uint8Array> | null, room: ChunkedRoom, id: string, arriving: ((bytes: number) => void) | undefined): Promise<BootstrapResult>;
30
+ export declare function chunkLister(transport: HouseDoor, most: () => number): {
31
+ askAll: (entries: readonly TrackedAuthority[]) => void;
32
+ list: (entry: TrackedAuthority) => Promise<Listed>;
33
+ renew(entry: TrackedAuthority, installing: readonly TrackedAuthority[]): Promise<Listed>;
34
+ forget(): void;
35
+ };
36
+ export declare function chunkScope(body: ReadableStream<Uint8Array>, room: ChunkedRoom, arriving: ((bytes: number) => void) | undefined): Promise<HouseScope | null>;
37
+ export {};
38
+ //# sourceMappingURL=chunks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chunks.d.ts","sourceRoot":"","sources":["../src/chunks.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,cAAc,EACnB,KAAK,aAAa,EAClB,KAAK,eAAe,EACrB,MAAM,wBAAwB,CAAC;AAChC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAEvD,OAAO,EAML,KAAK,WAAW,EAChB,KAAK,UAAU,EAChB,MAAM,WAAW,CAAC;AAEnB,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAAkB,KAAK,SAAS,EAAE,KAAK,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAChF,OAAO,EAA6B,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AAE3E,MAAM,MAAM,YAAY,GAAG;IAAE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AAE5E,MAAM,MAAM,WAAW,GAAG,WAAW,GAAG;IACtC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IACzB,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;IACnD,QAAQ,CAAC,KAAK,EAAE,SAAS,aAAa,EAAE,CAAC;IACzC,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC5C,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC;CAC9B,CAAC;AAEF,MAAM,MAAM,MAAM,GAAG,WAAW,GAAG;IAAE,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAA;CAAE,GAAG,cAAc,CAAC;AAEnF,KAAK,QAAQ,GAAG;IAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAA;CAAE,CAAC;AAoCpE,wBAAgB,MAAM,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAE7D;AA4BD,wBAAgB,aAAa,CAAC,MAAM,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CAIvE;AAED,wBAAsB,WAAW,CAC/B,MAAM,EAAE,WAAW,EACnB,KAAK,EAAE,gBAAgB,EACvB,MAAM,EAAE,YAAY,GACnB,OAAO,CAAC,eAAe,CAAC,CAuB1B;AAED,wBAAsB,SAAS,CAC7B,IAAI,EAAE,cAAc,CAAC,UAAU,CAAC,GAAG,IAAI,EACvC,IAAI,EAAE,WAAW,EACjB,EAAE,EAAE,MAAM,EACV,QAAQ,EAAE,CAAC,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC,GAAG,SAAS,GAC9C,OAAO,CAAC,eAAe,CAAC,CAM1B;AAgBD,wBAAgB,WAAW,CAAC,SAAS,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,MAAM;sBAwCzC,SAAS,gBAAgB,EAAE;kBAU/B,gBAAgB,KAAG,OAAO,CAAC,MAAM,CAAC;iBASxC,gBAAgB,cAAc,SAAS,gBAAgB,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC;;EAc3F;AAuCD,wBAAsB,UAAU,CAC9B,IAAI,EAAE,cAAc,CAAC,UAAU,CAAC,EAChC,IAAI,EAAE,WAAW,EACjB,QAAQ,EAAE,CAAC,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC,GAAG,SAAS,GAC9C,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC,CAgC5B"}
package/dist/chunks.js ADDED
@@ -0,0 +1,247 @@
1
+ import { paced, ROW_WEIGHT, stepped, } from '@agentshouse/mdruntime';
2
+ import { manifestedModels } from './model.js';
3
+ import { authorityRoot, bootstrapping, parseFile, } from './read.js';
4
+ import { classifyDoor } from './refusal.js';
5
+ import { houseDoorRoute } from './transport.js';
6
+ import { readPosition, roomVersion } from './version.js';
7
+ function authorKey(author) {
8
+ for (const kind of ['user', 'site', 'inbox']) {
9
+ if (typeof author[kind] === 'string')
10
+ return `${kind}:${author[kind]}`;
11
+ }
12
+ const { app, release, function: named } = author;
13
+ return typeof app === 'string' && typeof release === 'string' && typeof named === 'string'
14
+ ? `app:${app}:${release}:${named}`
15
+ : null;
16
+ }
17
+ function namesOf(authors) {
18
+ if (!Array.isArray(authors))
19
+ return null;
20
+ const names = new Map();
21
+ for (const author of authors) {
22
+ const key = typeof author === 'object' && author !== null ? authorKey(author) : null;
23
+ if (key === null || typeof author.name !== 'string')
24
+ return null;
25
+ names.set(key, author.name);
26
+ }
27
+ return names;
28
+ }
29
+ function modelsOf(models, mode) {
30
+ if (!Array.isArray(models))
31
+ return null;
32
+ const files = [];
33
+ const contents = [];
34
+ for (const entry of models) {
35
+ const file = parseFile(entry, mode);
36
+ if (file === null || typeof entry.content !== 'string')
37
+ return null;
38
+ files.push(file);
39
+ contents.push({ path: file.path, content: entry.content });
40
+ }
41
+ return { files, contents };
42
+ }
43
+ export function partId(logEpoch, hash) {
44
+ return `${logEpoch}:${hash}`;
45
+ }
46
+ function chunkedRoom(room, mode) {
47
+ const at = readPosition(room);
48
+ const names = mode === 'site' ? new Map() : namesOf(room.authors);
49
+ const models = modelsOf(room.models, mode);
50
+ if (at === null || names === null || models === null || !Array.isArray(room.chunks))
51
+ return null;
52
+ const chunks = new Map();
53
+ const parts = [];
54
+ for (const chunk of room.chunks) {
55
+ const { hash, bytes, download } = chunk;
56
+ if (typeof hash !== 'string' || typeof bytes !== 'number' || typeof download !== 'string')
57
+ return null;
58
+ const id = partId(at.logEpoch, hash);
59
+ chunks.set(id, { url: download, bytes });
60
+ parts.push({ id, bytes });
61
+ }
62
+ return { anchor: room.room_ref, ...at, mode, chunks, parts, names, models };
63
+ }
64
+ function listedRoom(room, mode) {
65
+ if (typeof room !== 'object' || room === null)
66
+ return null;
67
+ const held = room;
68
+ if (typeof held.room_ref !== 'string')
69
+ return null;
70
+ if (held.ready === false)
71
+ return { kind: 'not-ready' };
72
+ return held.ready === true ? chunkedRoom(held, mode) : null;
73
+ }
74
+ export function siteChunkList(answer, locator) {
75
+ const failure = classifyDoor(answer.status, answer.body, locator);
76
+ if (failure !== null)
77
+ return failure;
78
+ return listedRoom(answer.body, 'site') ?? { kind: 'transport-uncertainty' };
79
+ }
80
+ export async function chunkedHead(listed, entry, spaces) {
81
+ if (listed.anchor !== entry.authority)
82
+ return { kind: 'transport-uncertainty' };
83
+ const models = await manifestedModels({
84
+ anchor: listed.anchor,
85
+ position: listed.position,
86
+ logEpoch: listed.logEpoch,
87
+ models: listed.models,
88
+ }, entry, spaces);
89
+ return {
90
+ kind: 'bootstrap',
91
+ roomVersion: roomVersion(listed),
92
+ documents: {},
93
+ facts: {},
94
+ revisions: {},
95
+ changes: {},
96
+ binaryFiles: {},
97
+ parts: listed.parts,
98
+ ...(models === undefined ? {} : { models }),
99
+ };
100
+ }
101
+ export async function chunkPart(body, room, id, arriving) {
102
+ const address = room.chunks.get(id);
103
+ if (body === null || address === undefined)
104
+ return { kind: 'transport-uncertainty' };
105
+ const scope = await chunkScope(body, room, arriving).catch(() => null);
106
+ if (scope === null)
107
+ return { kind: 'transport-uncertainty' };
108
+ return { ...(await stepped(bootstrapping(scope))), bytes: address.bytes };
109
+ }
110
+ function listedRooms(answer) {
111
+ const rooms = answer.body?.rooms;
112
+ if (!Array.isArray(rooms))
113
+ return null;
114
+ const listed = new Map();
115
+ for (const room of rooms) {
116
+ const held = listedRoom(room, 'credential');
117
+ if (held === null)
118
+ return null;
119
+ listed.set(room.room_ref, held);
120
+ }
121
+ return listed;
122
+ }
123
+ export function chunkLister(transport, most) {
124
+ const asked = new Map();
125
+ function answered(entries, whole) {
126
+ const prefixes = whole ? ['/'] : entries.map((entry) => authorityRoot(entry.handle));
127
+ return transport.door(houseDoorRoute('room-chunks/list'), { prefixes }).then((answer) => {
128
+ const failure = classifyDoor(answer.status, answer.body, '');
129
+ if (failure !== null)
130
+ return failed(entries, failure);
131
+ const listed = listedRooms(answer);
132
+ return listed ?? failed(entries, { kind: 'transport-uncertainty' });
133
+ }, () => failed(entries, { kind: 'transport-uncertainty' }));
134
+ }
135
+ function failed(entries, failure) {
136
+ return new Map(entries.map((entry) => [
137
+ entry.authority,
138
+ failure.kind === 'room-not-readable' || failure.kind === 'scoped-refusal'
139
+ ? { ...failure, locator: entry.locator }
140
+ : failure,
141
+ ]));
142
+ }
143
+ function settled(entry, lists) {
144
+ return lists.then((listed) => listed.get(entry.authority) ?? { kind: 'room-not-readable', locator: entry.locator });
145
+ }
146
+ function askAll(entries) {
147
+ if (entries.length === 0)
148
+ return;
149
+ const lists = answered(entries, entries.length > most());
150
+ for (const entry of entries) {
151
+ const listed = settled(entry, lists);
152
+ listed.catch(() => { });
153
+ asked.set(entry.authority, { entry, listed });
154
+ }
155
+ }
156
+ function list(entry) {
157
+ const held = asked.get(entry.authority);
158
+ asked.delete(entry.authority);
159
+ return held?.listed ?? settled(entry, answered([entry], false));
160
+ }
161
+ return {
162
+ askAll,
163
+ list,
164
+ renew(entry, installing) {
165
+ if (!asked.has(entry.authority)) {
166
+ const waiting = [...asked.values()].map((held) => held.entry);
167
+ const due = new Map([entry, ...installing, ...waiting].map((held) => [held.authority, held]));
168
+ askAll([...due.values()]);
169
+ }
170
+ return list(entry);
171
+ },
172
+ forget() {
173
+ asked.clear();
174
+ },
175
+ };
176
+ }
177
+ function folderLine(entry) {
178
+ return typeof entry.folder === 'string' && Object.keys(entry).length === 1 ? entry.folder : null;
179
+ }
180
+ function chunkEntry(line, room, read) {
181
+ let entry;
182
+ try {
183
+ entry = JSON.parse(line);
184
+ }
185
+ catch {
186
+ return false;
187
+ }
188
+ if (typeof entry !== 'object' || entry === null)
189
+ return false;
190
+ const folder = folderLine(entry);
191
+ if (folder !== null) {
192
+ read.folders.push(folder);
193
+ return true;
194
+ }
195
+ const author = entry.author;
196
+ let named = entry;
197
+ if (author !== undefined) {
198
+ const key = typeof author === 'object' && author !== null ? authorKey(author) : null;
199
+ const name = key === null ? undefined : room.names.get(key);
200
+ if (name === undefined)
201
+ return false;
202
+ named = { ...entry, author: { ...author, name } };
203
+ }
204
+ const file = parseFile(named, room.mode);
205
+ if (file === null)
206
+ return false;
207
+ read.files.push(file);
208
+ if (typeof entry.content === 'string')
209
+ read.contents.push({ path: file.path, content: entry.content });
210
+ return true;
211
+ }
212
+ export async function chunkScope(body, room, arriving) {
213
+ const read = {
214
+ files: [],
215
+ contents: [],
216
+ folders: [],
217
+ };
218
+ const reader = body.getReader();
219
+ const decoder = new TextDecoder();
220
+ const pace = paced();
221
+ const unfinished = [];
222
+ let bytes = 0;
223
+ for (let chunk = await reader.read(); chunk.done !== true; chunk = await reader.read()) {
224
+ bytes += chunk.value.length;
225
+ arriving?.(bytes);
226
+ const text = decoder.decode(chunk.value, { stream: true });
227
+ let start = 0;
228
+ for (let end = text.indexOf('\n'); end !== -1; end = text.indexOf('\n', start)) {
229
+ const piece = text.slice(start, end);
230
+ const line = unfinished.length === 0 ? piece : unfinished.splice(0).join('') + piece;
231
+ start = end + 1;
232
+ if (line.length > 0 && !chunkEntry(line, room, read)) {
233
+ await reader.cancel();
234
+ return null;
235
+ }
236
+ const due = pace(line.length + ROW_WEIGHT);
237
+ if (due instanceof Promise)
238
+ await due;
239
+ }
240
+ if (start < text.length)
241
+ unfinished.push(text.slice(start));
242
+ }
243
+ const rest = unfinished.join('') + decoder.decode();
244
+ if (rest.length > 0 && !chunkEntry(rest, room, read))
245
+ return null;
246
+ return { anchor: room.anchor, position: room.position, logEpoch: room.logEpoch, ...read };
247
+ }