@agentshouse/mdruntime-adapter 0.1.0-cd59fd8945dcb → 0.1.0-cd5da4f128434

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 (74) hide show
  1. package/README.md +3 -3
  2. package/dist/actions.d.ts +6 -44
  3. package/dist/actions.d.ts.map +1 -1
  4. package/dist/actions.js +26 -308
  5. package/dist/adapter.d.ts +1 -1
  6. package/dist/adapter.d.ts.map +1 -1
  7. package/dist/adapter.js +296 -243
  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 +289 -0
  14. package/dist/digest.d.ts +0 -1
  15. package/dist/discovery.d.ts +24 -17
  16. package/dist/discovery.d.ts.map +1 -1
  17. package/dist/discovery.js +97 -56
  18. package/dist/history.d.ts +6 -0
  19. package/dist/history.d.ts.map +1 -0
  20. package/dist/history.js +85 -0
  21. package/dist/index.d.ts +9 -3
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +8 -1
  24. package/dist/map.d.ts +0 -1
  25. package/dist/model.d.ts +5 -6
  26. package/dist/model.d.ts.map +1 -1
  27. package/dist/model.js +32 -43
  28. package/dist/read.d.ts +61 -46
  29. package/dist/read.d.ts.map +1 -1
  30. package/dist/read.js +179 -245
  31. package/dist/refusal.d.ts +3 -5
  32. package/dist/refusal.d.ts.map +1 -1
  33. package/dist/refusal.js +17 -11
  34. package/dist/search.d.ts +4 -0
  35. package/dist/search.d.ts.map +1 -0
  36. package/dist/search.js +47 -0
  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 +1 -2
  44. package/dist/site.d.ts.map +1 -1
  45. package/dist/site.js +116 -106
  46. package/dist/space-schema.d.ts +6 -0
  47. package/dist/space-schema.d.ts.map +1 -0
  48. package/dist/space-schema.js +11 -0
  49. package/dist/stream.d.ts +14 -14
  50. package/dist/stream.d.ts.map +1 -1
  51. package/dist/stream.js +288 -153
  52. package/dist/transport.d.ts +99 -26
  53. package/dist/transport.d.ts.map +1 -1
  54. package/dist/transport.js +55 -9
  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 +13 -0
  59. package/dist/upload.d.ts.map +1 -0
  60. package/dist/upload.js +65 -0
  61. package/dist/version.d.ts +1 -1
  62. package/dist/version.d.ts.map +1 -1
  63. package/dist/version.js +1 -1
  64. package/dist/write.d.ts +19 -12
  65. package/dist/write.d.ts.map +1 -1
  66. package/dist/write.js +153 -70
  67. package/package.json +9 -5
  68. package/AGENTS.md +0 -290
  69. package/dist/gather.d.ts +0 -4
  70. package/dist/gather.d.ts.map +0 -1
  71. package/dist/gather.js +0 -45
  72. package/dist/links.d.ts +0 -20
  73. package/dist/links.d.ts.map +0 -1
  74. package/dist/links.js +0 -104
package/dist/write.js CHANGED
@@ -1,8 +1,10 @@
1
- import { headChange } from './read.js';
1
+ import { departure, partsChanges, ROW_WEIGHT, } from '@agentshouse/mdruntime';
2
+ import { answered, answering, binaryFileOf, headChange, revised } from './read.js';
2
3
  import { sha256Hex, textBytes } from './digest.js';
3
4
  import { nativePath, roomLocalPath } from './map.js';
4
- import { classifyDoor, doorRefusal, scopedRefusal } from './refusal.js';
5
+ import { answeredFailure, classifyDoor, doorRefusal, resetOf, scopedRefusal } from './refusal.js';
5
6
  import { readPosition, roomVersion } from './version.js';
7
+ import { unusable } from './uncertain.js';
6
8
  function priorField(batch, path, name) {
7
9
  return batch.preconditions.find((precondition) => precondition.kind === 'field' && precondition.path === path && precondition.name === name);
8
10
  }
@@ -27,7 +29,7 @@ function created(change, path) {
27
29
  const { type, ...fields } = change.fields;
28
30
  if (typeof type !== 'string' || type.length === 0)
29
31
  return null;
30
- return { op: 'create_typed', path, type, fields, sections: change.sections };
32
+ return { op: 'create_typed', path, type, fields };
31
33
  }
32
34
  function operationOf(batch, change, handle) {
33
35
  const path = nativePath(change.path, handle);
@@ -73,6 +75,25 @@ function operationOf(batch, change, handle) {
73
75
  const operation = created(change, path);
74
76
  return operation === null ? { rule: 'type-missing' } : { operation };
75
77
  }
78
+ if (change.kind === 'binary') {
79
+ const written = {
80
+ op: 'upload',
81
+ path,
82
+ local_id: change.localId,
83
+ bytes: change.byteLength,
84
+ sha256: change.sha256,
85
+ media_type: change.mediaType,
86
+ };
87
+ const from = departure(batch.changes, batch.changes.indexOf(change));
88
+ if (from === change.path && priorAbsence(batch, change.path) !== undefined)
89
+ return { operation: written };
90
+ const replaced = priorExistence(batch, from);
91
+ if (replaced === undefined)
92
+ return { rule: 'precondition-missing' };
93
+ if (!('base' in replaced))
94
+ return { rule: 'presence-base-unrepresentable' };
95
+ return { operation: { ...written, base: replaced.base } };
96
+ }
76
97
  const existence = priorExistence(batch, change.path);
77
98
  if (existence === undefined)
78
99
  return { rule: 'precondition-missing' };
@@ -100,16 +121,36 @@ export function houseChanges(batch, handle) {
100
121
  }
101
122
  return { kind: 'changes', changes };
102
123
  }
103
- export function operationHead(batch) {
124
+ export function houseParts(saved, handle) {
125
+ return houseChanges({ id: '', baseRoomVersion: '', ...partsChanges(saved) }, handle);
126
+ }
127
+ export function houseMove(batch, source, destination) {
128
+ const changes = [];
129
+ for (const { path, to } of batch.between?.moves ?? []) {
130
+ const existence = priorExistence(batch, path);
131
+ if (existence === undefined)
132
+ return { kind: 'unmapped', path, rule: 'precondition-missing' };
133
+ if (!('base' in existence)) {
134
+ return { kind: 'unmapped', path, rule: 'presence-base-unrepresentable' };
135
+ }
136
+ changes.push({
137
+ op: 'rename',
138
+ path: nativePath(path, source),
139
+ to: nativePath(to, destination),
140
+ base: existence.base,
141
+ });
142
+ }
143
+ return { kind: 'changes', changes };
144
+ }
145
+ export function operationHead(batch, room) {
104
146
  const writer = batch.writtenBy;
105
- return writer === undefined
106
- ? { operation_id: batch.id }
107
- : { operation_id: batch.id, app_ref: writer.app, release: writer.release };
147
+ const head = { operation_id: batch.id, room_ref: room };
148
+ return writer === undefined ? head : { ...head, app_ref: writer.app, release: writer.release };
108
149
  }
109
150
  export function batchPaths(changes) {
110
151
  return [
111
152
  ...new Set(changes.flatMap((change) => change.op === 'rename' ? [String(change.path), String(change.to)] : [String(change.path)])),
112
- ].sort();
153
+ ];
113
154
  }
114
155
  export function deltaBody(changes) {
115
156
  return JSON.stringify({ changes });
@@ -124,20 +165,26 @@ export function uploadable(inline, bytes, bounds) {
124
165
  export function deltaDigest(delta) {
125
166
  return sha256Hex(delta);
126
167
  }
127
- function material(value, handle) {
168
+ function* material(value, handle, within = () => true) {
128
169
  if (!Array.isArray(value))
129
170
  return null;
130
171
  const held = [];
131
172
  for (const entry of value) {
173
+ yield ROW_WEIGHT;
132
174
  if (typeof entry !== 'object' || entry === null)
133
175
  return null;
134
176
  const file = entry;
135
177
  if (typeof file.path !== 'string')
136
178
  return null;
179
+ if (!within(file.path))
180
+ continue;
137
181
  if (file.content !== null && typeof file.content !== 'string')
138
182
  return null;
139
183
  if (file.revision !== null && file.revision !== undefined && typeof file.revision !== 'string')
140
184
  return null;
185
+ const binary = binaryFileOf(file);
186
+ if (binary === null)
187
+ return null;
141
188
  const head = headChange(file);
142
189
  if (head === null)
143
190
  return null;
@@ -152,32 +199,75 @@ function material(value, handle) {
152
199
  path,
153
200
  content: file.content,
154
201
  revision: typeof file.revision === 'string' ? file.revision : null,
202
+ ...(binary === undefined ? {} : { binary }),
155
203
  ...(head === undefined ? {} : { head }),
156
204
  });
157
205
  }
158
206
  return held;
159
207
  }
160
- function affectedOf(held) {
208
+ function* affectedOf(held) {
161
209
  const affected = {};
162
210
  for (const entry of held) {
163
211
  if (entry.content !== null)
164
212
  affected[entry.path] = entry.content;
213
+ yield ROW_WEIGHT;
165
214
  }
166
215
  return affected;
167
216
  }
168
- export function mutationOutcome(status, body, locator, handle) {
217
+ function* acceptedOf(at, contents) {
218
+ const answer = answering();
219
+ for (const entry of contents) {
220
+ yield ROW_WEIGHT + (entry.content?.length ?? 0);
221
+ if (entry.revision === null) {
222
+ answer.removals.add(entry.path);
223
+ continue;
224
+ }
225
+ if (entry.content !== null)
226
+ answer.documents.set(entry.path, entry.content);
227
+ if (entry.binary !== undefined)
228
+ answer.binaryFiles.set(entry.path, entry.binary);
229
+ revised(answer, entry.path, entry.revision);
230
+ if (entry.head !== undefined)
231
+ answer.changes.set(entry.path, entry.head);
232
+ }
233
+ const unbinaried = answer.binaryFiles.size === 0;
234
+ const { binaryFiles, facts: _facts, ...room } = yield* answered(answer);
235
+ return { roomVersion: roomVersion(at), ...room, ...(unbinaried ? {} : { binaryFiles }) };
236
+ }
237
+ function* movedOutcome(held, move) {
238
+ const authorities = Array.isArray(held.authorities) ? held.authorities : [];
239
+ const at = (authority) => {
240
+ const entry = authorities.find((one) => typeof one === 'object' &&
241
+ one !== null &&
242
+ one.authority === authority);
243
+ return entry === undefined ? null : readPosition(entry);
244
+ };
245
+ const left = at(move.source.authority);
246
+ const arrived = at(move.destination.authority);
247
+ if (left === null || arrived === null)
248
+ return unusable();
249
+ const under = (handle) => (path) => path.startsWith(`/rooms/${handle}/`);
250
+ const leaving = yield* material(held.contents, move.source.handle, under(move.source.handle));
251
+ const arriving = yield* material(held.contents, move.destination.handle, under(move.destination.handle));
252
+ if (leaving === null || arriving === null)
253
+ return unusable();
254
+ return {
255
+ kind: 'accepted',
256
+ ...(yield* acceptedOf(left, leaving)),
257
+ destination: yield* acceptedOf(arrived, arriving),
258
+ };
259
+ }
260
+ export function* mutationOutcome(status, body, locator, handle, move) {
169
261
  const refusal = doorRefusal(body);
170
262
  if (refusal !== null && refusal.code === 'service_mode_write_halted') {
171
263
  return { kind: 'write-halted' };
172
264
  }
173
- if (refusal !== null && refusal.code === 'expired')
174
- return { kind: 'expired' };
175
265
  if (refusal !== null && refusal.code === 'edit_conflict') {
176
266
  const { current, ...fields } = refusal.fields;
177
- const held = material(current, handle) ?? [];
267
+ const held = (yield* material(current, handle)) ?? [];
178
268
  return {
179
269
  kind: 'conflicted',
180
- affected: affectedOf(held),
270
+ affected: yield* affectedOf(held),
181
271
  code: refusal.code,
182
272
  fields,
183
273
  retryable: refusal.retryable,
@@ -187,10 +277,8 @@ export function mutationOutcome(status, body, locator, handle) {
187
277
  return scopedRefusal(locator, 'undisclosed', refusal);
188
278
  }
189
279
  const classified = classifyDoor(status, body, locator);
190
- const reset = refusal?.fields.retry_at;
191
- if (refusal?.retryable === true &&
192
- typeof reset === 'string' &&
193
- classified?.kind === 'transport-uncertainty') {
280
+ const reset = resetOf(refusal);
281
+ if (refusal !== null && reset !== undefined && classified?.kind === 'transport-uncertainty') {
194
282
  return {
195
283
  kind: 'retryable-refusal',
196
284
  code: refusal.code,
@@ -201,8 +289,9 @@ export function mutationOutcome(status, body, locator, handle) {
201
289
  }
202
290
  if (classified !== null && classified.kind !== 'scoped-refusal')
203
291
  return classified;
204
- if (classified !== null && status === 403)
292
+ if (classified !== null && status === 403 && refusal?.code === 'operation_denied') {
205
293
  return classified;
294
+ }
206
295
  if (refusal !== null && status >= 400) {
207
296
  return {
208
297
  kind: 'rejected',
@@ -212,39 +301,20 @@ export function mutationOutcome(status, body, locator, handle) {
212
301
  retryable: refusal.retryable,
213
302
  };
214
303
  }
215
- if (status !== 200 || typeof body !== 'object' || body === null) {
216
- return { kind: 'transport-uncertainty' };
217
- }
304
+ if (status !== 200)
305
+ return answeredFailure(status, body);
306
+ if (typeof body !== 'object' || body === null)
307
+ return unusable();
218
308
  const held = body;
309
+ if (move !== undefined)
310
+ return yield* movedOutcome(held, move);
219
311
  const at = readPosition(held);
220
312
  if (at === null)
221
- return { kind: 'transport-uncertainty' };
222
- const contents = material(held.contents, handle);
313
+ return unusable();
314
+ const contents = yield* material(held.contents, handle);
223
315
  if (contents === null)
224
- return { kind: 'transport-uncertainty' };
225
- const documents = {};
226
- const revisions = {};
227
- const changes = {};
228
- const removals = [];
229
- for (const entry of contents) {
230
- if (entry.content === null || entry.revision === null) {
231
- removals.push(entry.path);
232
- continue;
233
- }
234
- documents[entry.path] = entry.content;
235
- revisions[entry.path] = entry.revision;
236
- if (entry.head !== undefined)
237
- changes[entry.path] = entry.head;
238
- }
239
- return {
240
- kind: 'accepted',
241
- roomVersion: roomVersion(at),
242
- documents,
243
- revisions,
244
- changes,
245
- removals: removals.sort(),
246
- originals: [],
247
- };
316
+ return unusable();
317
+ return { kind: 'accepted', ...(yield* acceptedOf(at, contents)) };
248
318
  }
249
319
  export function rejectedMapping(mapping) {
250
320
  return {
@@ -264,13 +334,26 @@ export function revisionBody(input) {
264
334
  }
265
335
  function revisionAuthor(entry) {
266
336
  const user = entry.author_user;
337
+ const userName = entry.author_user_name;
267
338
  const site = entry.author_site;
268
- const name = entry.author_site_name;
269
- if (typeof user === 'string' && site === undefined && name === undefined) {
270
- return { kind: 'user', id: user };
339
+ const siteName = entry.author_site_name;
340
+ const inbox = entry.author_inbox;
341
+ const inboxName = entry.author_inbox_name;
342
+ const app = entry.author_app;
343
+ const appName = entry.author_app_name;
344
+ const named = [site, siteName, inbox, inboxName, app, appName].filter((held) => held !== undefined);
345
+ if (typeof user === 'string' && typeof userName === 'string' && named.length === 0)
346
+ return { kind: 'user', id: user, name: userName };
347
+ if (user !== null || userName !== undefined || named.length !== 2)
348
+ return null;
349
+ if (typeof site === 'string' && typeof siteName === 'string') {
350
+ return { kind: 'site', id: site, name: siteName };
351
+ }
352
+ if (typeof inbox === 'string' && typeof inboxName === 'string') {
353
+ return { kind: 'inbox', id: inbox, name: inboxName };
271
354
  }
272
- if (user === null && typeof site === 'string' && typeof name === 'string') {
273
- return { kind: 'site', id: site, name };
355
+ if (typeof app === 'string' && typeof appName === 'string') {
356
+ return { kind: 'app', id: app, name: appName };
274
357
  }
275
358
  return null;
276
359
  }
@@ -279,33 +362,25 @@ export function historyOutcome(status, body, locator, request) {
279
362
  if (classified !== null)
280
363
  return classified;
281
364
  if (status !== 200 || typeof body !== 'object' || body === null) {
282
- return { kind: 'transport-uncertainty' };
365
+ return unusable();
283
366
  }
284
367
  const held = body;
285
368
  if (!Array.isArray(held.revisions))
286
- return { kind: 'transport-uncertainty' };
369
+ return unusable();
287
370
  const entries = [];
288
371
  for (const entry of held.revisions) {
289
372
  if (typeof entry !== 'object' || entry === null)
290
- return { kind: 'transport-uncertainty' };
373
+ return unusable();
291
374
  entries.push(entry);
292
375
  }
293
376
  if (request.revision !== undefined) {
294
377
  const named = entries[0];
295
378
  if (named === undefined)
296
379
  return { kind: 'no-history' };
297
- if (named.unavailable === true) {
298
- return {
299
- kind: 'scoped-refusal',
300
- locator,
301
- scope: 'content',
302
- code: 'retained_history_unavailable',
303
- fields: { path: request.path, revision: request.revision },
304
- retryable: false,
305
- };
306
- }
380
+ if (typeof named.media_type === 'string')
381
+ return { kind: 'no-history' };
307
382
  if (typeof named.content !== 'string')
308
- return { kind: 'transport-uncertainty' };
383
+ return unusable();
309
384
  return { kind: 'revision', content: named.content };
310
385
  }
311
386
  if (entries.length === 0)
@@ -316,9 +391,17 @@ export function historyOutcome(status, body, locator, request) {
316
391
  if (typeof entry.revision !== 'string' ||
317
392
  author === null ||
318
393
  typeof entry.created_at !== 'string') {
319
- return { kind: 'transport-uncertainty' };
394
+ return unusable();
320
395
  }
321
- revisions.push({ revision: entry.revision, author, time: entry.created_at });
396
+ const binaryFile = binaryFileOf(entry);
397
+ if (binaryFile === null)
398
+ return unusable();
399
+ revisions.push({
400
+ revision: entry.revision,
401
+ author,
402
+ time: entry.created_at,
403
+ ...(binaryFile === undefined ? {} : { binaryFile }),
404
+ });
322
405
  }
323
406
  const next = held.next_before;
324
407
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentshouse/mdruntime-adapter",
3
- "version": "0.1.0-cd59fd8945dcb",
3
+ "version": "0.1.0-cd5da4f128434",
4
4
  "description": "Agents House Adapter for the MD Runtime",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -9,7 +9,7 @@
9
9
  "access": "public"
10
10
  },
11
11
  "engines": {
12
- "node": ">=24.21.0"
12
+ "node": ">=24.22.1"
13
13
  },
14
14
  "exports": {
15
15
  ".": {
@@ -20,12 +20,16 @@
20
20
  },
21
21
  "files": [
22
22
  "dist",
23
- "AGENTS.md",
24
23
  "README.md",
25
24
  "LICENSE"
26
25
  ],
27
26
  "dependencies": {
28
- "@agentshouse/mdmodel": "0.1.0-cc9fa0498a7f7",
29
- "@agentshouse/mdruntime": "0.1.0-cd3486bd400ed"
27
+ "@agentshouse/mdmodel": "0.1.0-c8ef006e419a0",
28
+ "@agentshouse/mdruntime": "0.1.0-c3816fa791358"
29
+ },
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "git+https://github.com/agentshouse/core.git",
33
+ "directory": "mdruntime/packages/mdruntime-adapter"
30
34
  }
31
35
  }