@agentshouse/mdruntime-adapter 0.1.0-c0c4e0c770488 → 0.1.0-c0ccd4d323214

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 (83) hide show
  1. package/README.md +2 -2
  2. package/dist/actions.d.ts +2 -1
  3. package/dist/actions.d.ts.map +1 -1
  4. package/dist/actions.js +20 -7
  5. package/dist/adapter.d.ts +0 -1
  6. package/dist/adapter.d.ts.map +1 -1
  7. package/dist/adapter.js +166 -158
  8. package/dist/authored-path.d.ts +9 -0
  9. package/dist/authored-path.d.ts.map +1 -0
  10. package/dist/authored-path.js +34 -0
  11. package/dist/chunks.d.ts +36 -0
  12. package/dist/chunks.d.ts.map +1 -0
  13. package/dist/chunks.js +281 -0
  14. package/dist/digest.d.ts +0 -1
  15. package/dist/discovery.d.ts +20 -10
  16. package/dist/discovery.d.ts.map +1 -1
  17. package/dist/discovery.js +141 -16
  18. package/dist/history.d.ts +1 -7
  19. package/dist/history.d.ts.map +1 -1
  20. package/dist/history.js +22 -29
  21. package/dist/index.d.ts +5 -7
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +4 -5
  24. package/dist/map.d.ts +0 -1
  25. package/dist/model.d.ts +5 -10
  26. package/dist/model.d.ts.map +1 -1
  27. package/dist/model.js +19 -39
  28. package/dist/read.d.ts +10 -25
  29. package/dist/read.d.ts.map +1 -1
  30. package/dist/read.js +51 -171
  31. package/dist/refusal.d.ts +6 -2
  32. package/dist/refusal.d.ts.map +1 -1
  33. package/dist/refusal.js +28 -7
  34. package/dist/search.d.ts +0 -1
  35. package/dist/search.d.ts.map +1 -1
  36. package/dist/search.js +5 -5
  37. package/dist/session-content/bounds.d.ts +7 -0
  38. package/dist/session-content/check.d.ts +18 -0
  39. package/dist/session-content/index.d.ts +7 -0
  40. package/dist/session-content/parts.d.ts +103 -0
  41. package/dist/session-content/state.d.ts +61 -0
  42. package/dist/session-content/turn.d.ts +328 -0
  43. package/dist/site.d.ts +0 -1
  44. package/dist/site.d.ts.map +1 -1
  45. package/dist/site.js +46 -54
  46. package/dist/space-schema.d.ts +0 -1
  47. package/dist/space-schema.d.ts.map +1 -1
  48. package/dist/space-schema.js +1 -8
  49. package/dist/stream.d.ts +5 -6
  50. package/dist/stream.d.ts.map +1 -1
  51. package/dist/stream.js +147 -76
  52. package/dist/transport.d.ts +44 -25
  53. package/dist/transport.d.ts.map +1 -1
  54. package/dist/transport.js +16 -13
  55. package/dist/uncertain.d.ts +5 -0
  56. package/dist/uncertain.d.ts.map +1 -0
  57. package/dist/uncertain.js +11 -0
  58. package/dist/upload.d.ts +3 -2
  59. package/dist/upload.d.ts.map +1 -1
  60. package/dist/upload.js +8 -6
  61. package/dist/version.d.ts +0 -1
  62. package/dist/write.d.ts +0 -2
  63. package/dist/write.d.ts.map +1 -1
  64. package/dist/write.js +21 -23
  65. package/package.json +3 -2
  66. package/dist/app-reference.d.ts +0 -2
  67. package/dist/app-reference.d.ts.map +0 -1
  68. package/dist/app-reference.js +0 -2
  69. package/dist/crockford.d.ts +0 -4
  70. package/dist/crockford.d.ts.map +0 -1
  71. package/dist/crockford.js +0 -3
  72. package/dist/house-model.d.ts +0 -719
  73. package/dist/house-model.d.ts.map +0 -1
  74. package/dist/house-model.js +0 -393
  75. package/dist/part.d.ts +0 -14
  76. package/dist/part.d.ts.map +0 -1
  77. package/dist/part.js +0 -90
  78. package/dist/room-stream.d.ts +0 -20
  79. package/dist/room-stream.d.ts.map +0 -1
  80. package/dist/room-stream.js +0 -57
  81. package/dist/room-types.d.ts +0 -7
  82. package/dist/room-types.d.ts.map +0 -1
  83. package/dist/room-types.js +0 -123
package/README.md CHANGED
@@ -20,12 +20,12 @@ import { createAgentsHouseAdapter } from '@agentshouse/mdruntime-adapter';
20
20
 
21
21
  const adapter = createAgentsHouseAdapter({ transport, authorize });
22
22
  const host = serveMdRuntime({ adapter, shell });
23
- host.attach(port, 'my-app');
23
+ host.attach(port, { app: 'my-app' });
24
24
  ```
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,8 @@
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
8
  export declare function actionOutcome(answer: HouseDoorAnswer, name: string, input: unknown): ActionResult;
7
- //# 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;AAwBD,wBAAgB,aAAa,CAAC,MAAM,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,YAAY,CAUjG"}
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;AAG7F,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,EAI/C,CAAC;AAEF,eAAO,MAAM,mBAAmB,EAAE,SAAS,MAAM,EAIhD,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,15 @@
1
- import { doorRefusal } from './refusal.js';
1
+ import { answeredFailure, doorRefusal, resetOf } from './refusal.js';
2
+ import { refusedFor } from './uncertain.js';
3
+ export const HOUSE_UNKEPT_READS = [
4
+ 'account/security/read',
5
+ 'billing/read',
6
+ 'clients/last-use/list',
7
+ ];
8
+ export const HOUSE_LEADING_READS = [
9
+ 'apps/list',
10
+ 'sites/list',
11
+ 'rooms/members/list',
12
+ ];
2
13
  export function declaredActions(routes) {
3
14
  return routes.map((name) => ({ name, inputShape: {}, resultKind: 'house', room: 'room_ref' }));
4
15
  }
@@ -32,15 +43,17 @@ export function actionOutcome(answer, name, input) {
32
43
  if (answer.status === 401)
33
44
  return { kind: 'revoked-authorization' };
34
45
  const refusal = doorRefusal(answer.body);
35
- if (refusal?.code === 'operation_contended' && numbered(input)) {
36
- return { kind: 'transport-uncertainty' };
46
+ if (refusal?.code === 'operation_contended' && numbered(input))
47
+ return refusedFor(refusal);
48
+ if (refusal !== null) {
49
+ const reset = resetOf(refusal);
50
+ return { kind: 'rejected', ...refusal, ...(reset === undefined ? {} : { reset }) };
37
51
  }
38
- if (refusal !== null)
39
- return { kind: 'rejected', ...refusal };
40
52
  if (answer.status === 404)
41
53
  return unimplemented(name);
42
- if (answer.status === 0 || answer.status >= 400)
43
- return { kind: 'transport-uncertainty' };
54
+ if (answer.status === 0 || answer.status >= 400) {
55
+ return answeredFailure(answer.status, answer.body);
56
+ }
44
57
  return { kind: 'action', result: answer.body };
45
58
  }
46
59
  async function carry(transport, route, name, input) {
package/dist/adapter.d.ts CHANGED
@@ -8,4 +8,3 @@ export type AgentsHouseAdapterOptions = {
8
8
  snapshot?: boolean;
9
9
  };
10
10
  export declare function createAgentsHouseAdapter(options: AgentsHouseAdapterOptions): Adapter;
11
- //# sourceMappingURL=adapter.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAIV,OAAO,EAIP,aAAa,EAgBd,MAAM,wBAAwB,CAAC;AAiChC,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;AA6DrD,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,yBAAyB,GAAG,OAAO,CAqapF"}
1
+ {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAIV,OAAO,EAIP,aAAa,EAkBd,MAAM,wBAAwB,CAAC;AAyBhC,OAAO,EAIL,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;AAmBrD,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,yBAAyB,GAAG,OAAO,CA0cpF"}
package/dist/adapter.js CHANGED
@@ -1,65 +1,40 @@
1
+ import { HOUSE_ROOM_TYPES, HOUSE_TYPED_PATHS } from '@agentshouse/mdmodel';
1
2
  import { stepped } from '@agentshouse/mdruntime';
2
3
  import { capabilities, discoveredRoom, mapDiscovery, parseAuthorityChange, parseDiscovery, trackedFrom, } from './discovery.js';
3
4
  import { createSiteAdapter } from './site.js';
4
- import { authorityRoot, bootstrapping, parseFileAddress, parseGrant, } from './read.js';
5
- import { roomStreams } from './room-stream.js';
6
- import { spaceSchemas } from './space-schema.js';
7
- import { sha256Hex } from './digest.js';
5
+ import { parseFileAddress, parseGrant } from './read.js';
6
+ import { chunkedHead, chunkLister, chunkPart } from './chunks.js';
8
7
  import { finishedUpload, houseUpload } from './upload.js';
9
- import { houseAction } from './actions.js';
8
+ import { houseAction, HOUSE_LEADING_READS, HOUSE_UNKEPT_READS } from './actions.js';
10
9
  import { houseSearch } from './search.js';
11
- import { classifyDoor, doorRefusal } from './refusal.js';
10
+ import { answeredFailure, classifyDoor, doorRefusal, keyedFailure } from './refusal.js';
11
+ import { UNANSWERED, unusable } from './uncertain.js';
12
12
  import { houseNotifications, } from './stream.js';
13
13
  import { houseDoorRoute, } from './transport.js';
14
14
  import { batchPaths, deltaBody, deltaBytes, deltaDigest, historyOutcome, houseChanges, houseMove, mutationOutcome, operationHead, rejectedMapping, revisionBody, uploadable, } from './write.js';
15
- import { HOUSE_TYPED_PATHS } from './house-model.js';
16
- import { HOUSE_NAMEABLE_ROOM_TYPES, HOUSE_ROOM_TYPES } from './room-types.js';
17
- import { followsVersion, logPosition, presented, reachedVersion, roomVersion } from './version.js';
18
- const UNCERTAIN = { kind: 'transport-uncertainty' };
19
- const UNCERTAIN_READ = { kind: 'transport-uncertainty' };
20
- const OCTET_STREAM = 'application/octet-stream';
15
+ import { admittedPath } from './authored-path.js';
16
+ import { followsVersion, reachedVersion, roomVersion } from './version.js';
21
17
  const EXPIRED_ADDRESS = 403;
22
18
  function lapsedGrant(outcome) {
23
19
  return outcome.kind === 'rejected' && outcome.code === 'upload_unavailable';
24
20
  }
25
- async function heldFiles(authority, held, bound) {
26
- const listed = await held.files(null, bound + 1);
27
- const files = listed.slice(0, bound);
28
- const through = listed.length > bound ? (files.at(-1)?.path ?? null) : null;
29
- return files.length === 0 ? null : { room: authority, through, files };
30
- }
31
- async function reused(scope, held) {
32
- const sent = new Set(scope.contents.map((entry) => entry.path));
33
- const contents = [...scope.contents];
34
- const asked = scope.files.filter((file) => file.binary === undefined && !sent.has(file.path));
35
- if (asked.length === 0)
36
- return scope;
37
- if (held === undefined)
38
- return null;
39
- const kept = await held.contents(asked.map(({ path }) => path));
40
- for (const file of asked) {
41
- const found = kept.get(file.path);
42
- if (found === undefined ||
43
- found.revision !== file.revision ||
44
- (await sha256Hex(found.content)) !== file.sha256) {
45
- return null;
46
- }
47
- contents.push({ path: file.path, content: found.content });
48
- }
49
- return { ...scope, contents };
21
+ function unstagedRefusal(answer) {
22
+ const refusal = doorRefusal(answer.body);
23
+ return refusal?.code === 'files_unstaged' ? refusal : null;
50
24
  }
51
25
  export function createAgentsHouseAdapter(options) {
52
26
  if ('site' in options)
53
27
  return createSiteAdapter(options.site, options.snapshot === true);
54
28
  const transport = options.transport;
55
- const spaces = spaceSchemas();
56
29
  const notifications = houseNotifications(transport, 'credential', {
57
30
  opened: async (frame) => (frame === null ? null : opened(frame)),
58
31
  changed,
59
- }, spaces);
32
+ expired: (entries) => lister.askAll(entries),
33
+ });
60
34
  const tracked = new Map();
61
- const rooms = roomStreams('credential', spaces);
62
- const unreused = new Set();
35
+ const lister = chunkLister(transport, () => declared().max_prefixes, (locator) => tracked.get(locator));
36
+ const chunked = new Map();
37
+ const renewals = new WeakMap();
63
38
  let bounds;
64
39
  function declared() {
65
40
  if (bounds === undefined)
@@ -72,19 +47,23 @@ export function createAgentsHouseAdapter(options) {
72
47
  return null;
73
48
  bounds = document.bounds;
74
49
  tracked.clear();
50
+ lister.forget();
75
51
  for (const entry of trackedFrom(document))
76
52
  tracked.set(entry.locator, entry);
77
53
  notifications.track([...tracked.values()]);
78
54
  return {
79
55
  ...mapDiscovery(document, document.authorities.map((authority) => discoveredRoom(authority)), capabilities(document.actions)),
80
56
  member: document.member,
57
+ timeZone: document.timeZone,
58
+ roomList: document.roomList,
81
59
  };
82
60
  }
83
61
  function changed(frame) {
84
62
  const change = parseAuthorityChange(frame);
85
63
  if (change === null)
86
64
  return null;
87
- for (const locator of change.left)
65
+ const unheld = [...change.left, ...change.listed];
66
+ for (const locator of unheld)
88
67
  tracked.delete(locator);
89
68
  for (const entry of trackedFrom(change))
90
69
  tracked.set(entry.locator, entry);
@@ -92,46 +71,63 @@ export function createAgentsHouseAdapter(options) {
92
71
  return {
93
72
  kind: 'authority-changed',
94
73
  rooms: change.authorities.map((authority) => discoveredRoom(authority)),
95
- left: change.left,
74
+ left: unheld,
96
75
  member: change.member,
76
+ timeZone: change.timeZone,
77
+ roomList: { ...change.roomList, left: change.left },
97
78
  };
98
79
  }
99
- async function streamedPage(entry, token, held) {
100
- const part = await rooms.part(entry, token);
101
- if ('kind' in part)
102
- return part;
103
- const scope = await reused(part.scope, held);
104
- if (scope === null) {
105
- unreused.add(entry.locator);
106
- return UNCERTAIN_READ;
107
- }
108
- return {
109
- ...(await stepped(bootstrapping(scope))),
110
- ...(part.models === undefined ? {} : { models: part.models }),
111
- ...(part.pageToken === undefined ? {} : { pageToken: part.pageToken }),
112
- bytes: part.bytes,
113
- };
80
+ async function listedRoom(entry) {
81
+ const listed = await lister.list(entry);
82
+ if ('kind' in listed)
83
+ return listed;
84
+ const head = await chunkedHead(listed, entry);
85
+ if (head.kind === 'bootstrap')
86
+ chunked.set(entry.locator, listed);
87
+ return head;
114
88
  }
115
- async function openedRoom(entry, request) {
116
- const reusing = unreused.delete(entry.locator) ? undefined : request.held;
117
- const files = reusing === undefined
118
- ? null
119
- : await heldFiles(entry.authority, reusing, declared().max_held_files);
120
- const answered = await transport.streamed(houseDoorRoute('bootstrap'), {
121
- room_ref: entry.authority,
122
- prefixes: [authorityRoot(entry.handle)],
123
- ...(files === null ? {} : { held: [files] }),
124
- }, request.signal ?? new AbortController().signal);
125
- if (answered.room === null)
126
- return classifyDoor(answered.status, answered.body, entry.locator) ?? UNCERTAIN_READ;
127
- return rooms.head(entry, answered.room, request.arriving, request.signal);
89
+ async function chunkRead(entry, id) {
90
+ const room = chunked.get(entry.locator);
91
+ const address = room?.chunks.get(id);
92
+ if (room === undefined || address === undefined)
93
+ return UNANSWERED;
94
+ const read = await transport.download(address.url);
95
+ if (read.status !== EXPIRED_ADDRESS)
96
+ return { room, read };
97
+ const renewed = await renewal(entry, room);
98
+ if ('kind' in renewed)
99
+ return renewed;
100
+ const renewedAddress = renewed.chunks.get(id);
101
+ if (renewedAddress === undefined)
102
+ return unusable();
103
+ chunked.set(entry.locator, renewed);
104
+ return { room: renewed, read: await transport.download(renewedAddress.url) };
128
105
  }
129
- function held(rooms) {
130
- notifications.send(rooms.flatMap((room) => {
131
- const entry = tracked.get(room.locator);
132
- const at = logPosition(room.roomVersion);
133
- return entry === undefined || at === null ? [] : [presented(entry.authority, at)];
134
- }));
106
+ function renewal(entry, stale) {
107
+ const held = renewals.get(stale);
108
+ if (held !== undefined)
109
+ return held;
110
+ const renewed = lister.renew(entry, [...chunked.keys()].flatMap((locator) => tracked.get(locator) ?? []));
111
+ renewals.set(stale, renewed);
112
+ return renewed;
113
+ }
114
+ function hold(rooms) {
115
+ for (const room of rooms) {
116
+ const listed = chunked.get(room.locator);
117
+ if (listed !== undefined &&
118
+ room.roomVersion !== null &&
119
+ reachedVersion(room.roomVersion, roomVersion(listed), listed.logEpoch))
120
+ chunked.delete(room.locator);
121
+ }
122
+ notifications.send(rooms);
123
+ }
124
+ async function chunkPage(entry, id, arriving) {
125
+ const fetched = await chunkRead(entry, id);
126
+ if ('kind' in fetched)
127
+ return fetched;
128
+ if (fetched.read.status !== 200)
129
+ return answeredFailure(fetched.read.status, null);
130
+ return chunkPart(fetched.read.body, fetched.room, id, arriving);
135
131
  }
136
132
  function authorityFor(locator) {
137
133
  return tracked.get(locator);
@@ -156,7 +152,7 @@ export function createAgentsHouseAdapter(options) {
156
152
  if (handed !== undefined && handed.sha256 === sha256) {
157
153
  return { body: { ...head, paths, upload: handed, runtime: notifications.runtime } };
158
154
  }
159
- const minted = await transport.door(houseDoorRoute('batch-upload'), {
155
+ const minted = await transport.door(houseDoorRoute('files/batch-upload'), {
160
156
  ...head,
161
157
  paths,
162
158
  bytes,
@@ -169,38 +165,38 @@ export function createAgentsHouseAdapter(options) {
169
165
  }
170
166
  const grant = parseGrant(minted.body);
171
167
  if (grant === null)
172
- return { outcome: UNCERTAIN };
168
+ return { outcome: unusable() };
173
169
  const delivered = await transport.transfer({
174
- act: 'authored_batch_upload',
175
170
  url: grant.url,
176
171
  body: `${grant.operation}\n${delta}`,
177
172
  });
178
- if (delivered.status !== 200)
179
- return { outcome: UNCERTAIN };
173
+ if (delivered.status !== 200) {
174
+ return { outcome: answeredFailure(delivered.status, delivered.body) };
175
+ }
180
176
  const holding = parseGrant(delivered.body);
181
177
  if (holding === null)
182
- return { outcome: UNCERTAIN };
178
+ return { outcome: unusable() };
183
179
  const upload = { url: holding.url, operation: holding.operation, bytes, sha256 };
184
180
  await keep(upload);
185
181
  return { body: { ...head, paths, upload, runtime: notifications.runtime } };
186
182
  }
187
- async function staged(answer, { changeBatch: batch, file }) {
188
- const refusal = doorRefusal(answer.body);
189
- if (refusal?.code !== 'files_unstaged')
190
- return null;
191
- const uploads = refusal.fields.upload;
183
+ async function staged(uploads, { changeBatch: batch, file }) {
192
184
  if (!Array.isArray(uploads))
193
- return false;
185
+ return unusable();
194
186
  for (const named of uploads) {
195
187
  const upload = houseUpload(named);
196
188
  const change = batch.changes.find((held) => held.kind === 'binary' && held.sha256 === named.sha256);
197
189
  if (upload === null || change === undefined)
198
- return false;
190
+ return unusable();
199
191
  const finished = await finishedUpload(transport, upload, await file(change.localId));
200
- if (finished?.body?.staged !== true)
201
- return false;
192
+ if ('unfinished' in finished)
193
+ return answeredFailure(finished.unfinished, null);
194
+ if (finished.status !== 200)
195
+ return answeredFailure(finished.status, finished.body);
196
+ if (finished.body?.staged !== true)
197
+ return unusable();
202
198
  }
203
- return true;
199
+ return null;
204
200
  }
205
201
  async function submit(request, head, changes, delta, bytes, entry, handed, keep, into) {
206
202
  const inline = { ...head, changes, runtime: notifications.runtime };
@@ -209,13 +205,19 @@ export function createAgentsHouseAdapter(options) {
209
205
  : { body: inline };
210
206
  if ('outcome' in prepared)
211
207
  return prepared.outcome;
212
- const answer = await transport.door(houseDoorRoute('batch'), prepared.body);
213
- const stagedFiles = await staged(answer, request);
214
- if (stagedFiles === false)
215
- return UNCERTAIN;
216
- const settled = stagedFiles === null ? answer : await transport.door(houseDoorRoute('batch'), prepared.body);
217
- if (stagedFiles === true && (await staged(settled, request)) !== null)
218
- return UNCERTAIN;
208
+ const answer = await transport.door(houseDoorRoute('files/batch'), prepared.body);
209
+ const unstaged = unstagedRefusal(answer);
210
+ if (unstaged !== null) {
211
+ const staging = await staged(unstaged.fields.upload, request);
212
+ if (staging !== null)
213
+ return staging;
214
+ }
215
+ const settled = unstaged === null
216
+ ? answer
217
+ : await transport.door(houseDoorRoute('files/batch'), prepared.body);
218
+ if (unstaged !== null && unstagedRefusal(settled) !== null) {
219
+ return answeredFailure(settled.status, settled.body);
220
+ }
219
221
  return stepped(mutationOutcome(settled.status, settled.body, entry.locator, entry.handle, into === undefined
220
222
  ? undefined
221
223
  : {
@@ -223,28 +225,9 @@ export function createAgentsHouseAdapter(options) {
223
225
  destination: { authority: into.authority, handle: into.handle },
224
226
  }));
225
227
  }
226
- function addressed(answer, locator) {
227
- const failure = classifyDoor(answer.status, answer.body, locator);
228
- if (failure !== null) {
229
- return failure.kind === 'scoped-refusal'
230
- ? {
231
- kind: 'rejected',
232
- code: failure.code,
233
- fields: failure.fields,
234
- retryable: failure.retryable,
235
- }
236
- : failure;
237
- }
238
- const address = parseFileAddress(answer.body);
239
- return address === null ? UNCERTAIN_READ : { kind: 'address', ...address };
240
- }
241
- function linked(answer, locator) {
242
- const address = addressed(answer, locator);
243
- return address.kind === 'address'
244
- ? { kind: 'link', url: address.url, name: address.name, mediaType: address.mediaType }
245
- : address;
246
- }
247
- function refusedAttach(answer, locator) {
228
+ function refusedOn(answer, locator) {
229
+ if (locator === null)
230
+ return keyedFailure(answer.status, answer.body);
248
231
  const failure = classifyDoor(answer.status, answer.body, locator);
249
232
  if (failure === null)
250
233
  return null;
@@ -257,6 +240,48 @@ export function createAgentsHouseAdapter(options) {
257
240
  }
258
241
  : failure;
259
242
  }
243
+ function addressed(answer, locator) {
244
+ const failure = refusedOn(answer, locator);
245
+ if (failure !== null)
246
+ return failure;
247
+ const address = parseFileAddress(answer.body);
248
+ return address === null ? unusable() : { kind: 'address', ...address };
249
+ }
250
+ function linked(answer, locator) {
251
+ const address = addressed(answer, locator);
252
+ return address.kind === 'address'
253
+ ? { kind: 'link', url: address.url, name: address.name, mediaType: address.mediaType }
254
+ : address;
255
+ }
256
+ async function attached(request, into) {
257
+ const locator = into?.locator ?? null;
258
+ const declared = await transport.door(houseDoorRoute('files/attach'), {
259
+ ...(into === null ? {} : { room_ref: into.authority }),
260
+ name: request.name,
261
+ bytes: request.byteLength,
262
+ sha256: request.sha256,
263
+ media_type: request.mediaType,
264
+ });
265
+ const refused = refusedOn(declared, locator);
266
+ if (refused !== null)
267
+ return refused;
268
+ const upload = houseUpload(declared.body?.upload);
269
+ if (upload === null)
270
+ return unusable();
271
+ const finished = await finishedUpload(transport, upload, request.blob);
272
+ if ('unfinished' in finished)
273
+ return answeredFailure(finished.unfinished, null);
274
+ const unfinished = refusedOn(finished, locator);
275
+ if (unfinished !== null)
276
+ return unfinished;
277
+ const { path, revision, link } = (finished.body ?? {});
278
+ if (into === null)
279
+ return typeof link === 'string' ? { kind: 'linked', link } : unusable();
280
+ const root = `/rooms/${into.handle}/`;
281
+ return typeof path === 'string' && path.startsWith(root) && typeof revision === 'string'
282
+ ? { kind: 'attached', path: path.slice(root.length), revision }
283
+ : unusable();
284
+ }
260
285
  async function downloaded(ask, locator) {
261
286
  let address = addressed(await ask(), locator);
262
287
  if (address.kind !== 'address')
@@ -268,8 +293,10 @@ export function createAgentsHouseAdapter(options) {
268
293
  return address;
269
294
  read = await transport.download(address.url);
270
295
  }
271
- if (read.status !== 200 || read.body === null)
272
- return UNCERTAIN_READ;
296
+ if (read.status !== 200)
297
+ return answeredFailure(read.status, null);
298
+ if (read.body === null)
299
+ return unusable();
273
300
  return {
274
301
  kind: 'binary',
275
302
  body: read.body,
@@ -281,7 +308,7 @@ export function createAgentsHouseAdapter(options) {
281
308
  const entry = authorityFor(request.locator);
282
309
  if (entry === undefined)
283
310
  return null;
284
- return () => transport.door(houseDoorRoute('files/get'), {
311
+ return () => transport.door(houseDoorRoute('files/read'), {
285
312
  room_ref: entry.authority,
286
313
  path: request.path,
287
314
  revision: request.revision,
@@ -289,8 +316,10 @@ export function createAgentsHouseAdapter(options) {
289
316
  }
290
317
  return {
291
318
  roomTypes: HOUSE_ROOM_TYPES,
292
- nameableRoomTypes: HOUSE_NAMEABLE_ROOM_TYPES,
293
319
  typedPaths: HOUSE_TYPED_PATHS,
320
+ pathRule: admittedPath,
321
+ unkeptReads: HOUSE_UNKEPT_READS,
322
+ leadingReads: HOUSE_LEADING_READS,
294
323
  async authorize() {
295
324
  return options.authorize();
296
325
  },
@@ -299,8 +328,8 @@ export function createAgentsHouseAdapter(options) {
299
328
  if (entry === undefined)
300
329
  return { kind: 'room-not-readable', locator: request.locator };
301
330
  return request.pageToken === undefined
302
- ? openedRoom(entry, request)
303
- : streamedPage(entry, request.pageToken, request.held);
331
+ ? listedRoom(entry)
332
+ : chunkPage(entry, request.pageToken, request.arriving);
304
333
  },
305
334
  async mutate(_authorization, request, keep) {
306
335
  const entry = authorityFor(request.locator);
@@ -331,16 +360,13 @@ export function createAgentsHouseAdapter(options) {
331
360
  const entry = authorityFor(request.locator);
332
361
  if (entry === undefined)
333
362
  return { kind: 'room-not-readable', locator: request.locator };
334
- const answer = await transport.door(houseDoorRoute('revisions'), revisionBody({
363
+ const answer = await transport.door(houseDoorRoute('files/revisions/list'), revisionBody({
335
364
  path: request.path,
336
365
  handle: entry.handle,
337
366
  ...(request.revision === undefined ? {} : { revision: request.revision }),
338
367
  ...(request.pageToken === undefined ? {} : { pageToken: request.pageToken }),
339
368
  }));
340
- return historyOutcome(answer.status, answer.body, entry.locator, {
341
- path: request.path,
342
- ...(request.revision === undefined ? {} : { revision: request.revision }),
343
- });
369
+ return historyOutcome(answer.status, answer.body, entry.locator, request);
344
370
  },
345
371
  async binary(_authorization, request) {
346
372
  const ask = pathAddress(request);
@@ -349,39 +375,21 @@ export function createAgentsHouseAdapter(options) {
349
375
  return downloaded(ask, request.locator);
350
376
  },
351
377
  async link(_authorization, request) {
378
+ if (request.kind === 'key') {
379
+ return linked(await transport.door(houseDoorRoute('files/read'), { link: request.link }), null);
380
+ }
352
381
  const ask = pathAddress(request);
353
382
  if (ask === null)
354
383
  return { kind: 'room-not-readable', locator: request.locator };
355
384
  return linked(await ask(), request.locator);
356
385
  },
357
386
  async attach(_authorization, request) {
387
+ if (request.locator === undefined)
388
+ return attached(request, null);
358
389
  const entry = authorityFor(request.locator);
359
390
  if (entry === undefined)
360
391
  return { kind: 'room-not-readable', locator: request.locator };
361
- const declared = await transport.door(houseDoorRoute('files/attach'), {
362
- room_ref: entry.authority,
363
- name: request.name,
364
- bytes: request.byteLength,
365
- sha256: request.sha256,
366
- media_type: request.mediaType.length > 0 ? request.mediaType : OCTET_STREAM,
367
- });
368
- const refused = refusedAttach(declared, request.locator);
369
- if (refused !== null)
370
- return refused;
371
- const upload = houseUpload(declared.body?.upload);
372
- if (upload === null)
373
- return UNCERTAIN_READ;
374
- const finished = await finishedUpload(transport, upload, request.blob);
375
- if (finished === null)
376
- return UNCERTAIN_READ;
377
- const unfinished = refusedAttach(finished, request.locator);
378
- if (unfinished !== null)
379
- return unfinished;
380
- const { path, revision } = (finished.body ?? {});
381
- const root = `/rooms/${entry.handle}/`;
382
- return typeof path === 'string' && path.startsWith(root) && typeof revision === 'string'
383
- ? { kind: 'attached', path: path.slice(root.length), revision }
384
- : UNCERTAIN_READ;
392
+ return attached(request, entry);
385
393
  },
386
394
  reached(locator, version, target) {
387
395
  return reachedVersion(version, target, authorityFor(locator)?.logEpoch);
@@ -396,7 +404,7 @@ export function createAgentsHouseAdapter(options) {
396
404
  return houseSearch(transport, [...tracked.values()], request);
397
405
  },
398
406
  roomStream: {
399
- hold: held,
407
+ hold,
400
408
  observe: notifications.observe,
401
409
  },
402
410
  conversations: notifications.conversations,
@@ -0,0 +1,9 @@
1
+ export declare const GENERATED_PROVENANCE = "_provenance";
2
+ export declare const GENERATED_REVISIONS = "_revisions";
3
+ export declare const RESERVED_DERIVED = "_derived";
4
+ export declare const WHOLE_ESCAPED_NAMES: readonly string[];
5
+ export declare const AUTHORED_SEGMENT_MAX_CHARS = 255;
6
+ export declare const AUTHORED_SEGMENT: RegExp;
7
+ export declare const LOCAL_COPY_MANIFEST = ".house";
8
+ export declare const LOCAL_COPY_ROOT_NAMES: readonly string[];
9
+ export declare function admittedPath(path: string): boolean;
@@ -0,0 +1 @@
1
+ {"version":3,"file":"authored-path.d.ts","sourceRoot":"","sources":["../src/authored-path.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,oBAAoB,gBAAgB,CAAC;AAElD,eAAO,MAAM,mBAAmB,eAAe,CAAC;AAEhD,eAAO,MAAM,gBAAgB,aAAa,CAAC;AAE3C,eAAO,MAAM,mBAAmB,EAAE,SAAS,MAAM,EAMhD,CAAC;AAEF,eAAO,MAAM,0BAA0B,MAAM,CAAC;AAO9C,eAAO,MAAM,gBAAgB,QAE5B,CAAC;AAEF,eAAO,MAAM,mBAAmB,WAAW,CAAC;AAE5C,eAAO,MAAM,qBAAqB,EAAE,SAAS,MAAM,EAIlD,CAAC;AASF,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAQlD"}
@@ -0,0 +1,34 @@
1
+ import { APP_PREFERENCES_HOME, APP_REFERENCE_PATTERN } from '@agentshouse/mdmodel';
2
+ export const GENERATED_PROVENANCE = '_provenance';
3
+ export const GENERATED_REVISIONS = '_revisions';
4
+ export const RESERVED_DERIVED = '_derived';
5
+ export const WHOLE_ESCAPED_NAMES = [
6
+ '.',
7
+ '..',
8
+ GENERATED_PROVENANCE,
9
+ GENERATED_REVISIONS,
10
+ RESERVED_DERIVED,
11
+ ];
12
+ export const AUTHORED_SEGMENT_MAX_CHARS = 255;
13
+ const REFUSED_SEGMENTS = [
14
+ ...WHOLE_ESCAPED_NAMES.map((name) => name.split('.').join('[.]')),
15
+ '[.][Gg][Ii][Tt][.]*',
16
+ ].join('|');
17
+ export const AUTHORED_SEGMENT = new RegExp(`^(?!(?:${REFUSED_SEGMENTS})(?:/|$))[A-Za-z0-9._][A-Za-z0-9._-]{0,${AUTHORED_SEGMENT_MAX_CHARS - 1}}$`);
18
+ export const LOCAL_COPY_MANIFEST = '.house';
19
+ export const LOCAL_COPY_ROOT_NAMES = [
20
+ '.gitignore',
21
+ '.ignore',
22
+ LOCAL_COPY_MANIFEST,
23
+ ];
24
+ function appPreferences(path) {
25
+ return (path.endsWith('.md') &&
26
+ APP_REFERENCE_PATTERN.test(path.slice(APP_PREFERENCES_HOME.length + 1, -3)));
27
+ }
28
+ export function admittedPath(path) {
29
+ const names = path.split('/');
30
+ const [root] = names;
31
+ return (!LOCAL_COPY_ROOT_NAMES.includes(root.toLowerCase()) &&
32
+ names.every((name) => AUTHORED_SEGMENT.test(name)) &&
33
+ (!path.startsWith(`${APP_PREFERENCES_HOME}/`) || appPreferences(path)));
34
+ }