@agentshouse/mdruntime-adapter 0.1.0-c7c29ea9ca063 → 0.1.0-c7ccafe31c5f3

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 (80) hide show
  1. package/README.md +3 -3
  2. package/dist/actions.d.ts +8 -67
  3. package/dist/actions.d.ts.map +1 -1
  4. package/dist/actions.js +53 -342
  5. package/dist/adapter.d.ts +0 -1
  6. package/dist/adapter.d.ts.map +1 -1
  7. package/dist/adapter.js +290 -406
  8. package/dist/authored-path.d.ts +66 -0
  9. package/dist/authored-path.d.ts.map +1 -0
  10. package/dist/authored-path.js +166 -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 +45 -19
  16. package/dist/discovery.d.ts.map +1 -1
  17. package/dist/discovery.js +252 -67
  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 +10 -3
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +9 -1
  24. package/dist/map.d.ts +0 -1
  25. package/dist/model.d.ts +6 -6
  26. package/dist/model.d.ts.map +1 -1
  27. package/dist/model.js +31 -48
  28. package/dist/read.d.ts +54 -72
  29. package/dist/read.d.ts.map +1 -1
  30. package/dist/read.js +195 -322
  31. package/dist/refusal.d.ts +6 -5
  32. package/dist/refusal.d.ts.map +1 -1
  33. package/dist/refusal.js +27 -11
  34. package/dist/renamed.d.ts +10 -0
  35. package/dist/renamed.d.ts.map +1 -0
  36. package/dist/renamed.js +29 -0
  37. package/dist/room-query.d.ts +4 -0
  38. package/dist/room-query.d.ts.map +1 -0
  39. package/dist/room-query.js +63 -0
  40. package/dist/search.d.ts +4 -0
  41. package/dist/search.d.ts.map +1 -0
  42. package/dist/search.js +47 -0
  43. package/dist/session-content/check.d.ts +17 -0
  44. package/dist/session-content/index.d.ts +6 -0
  45. package/dist/session-content/parts.d.ts +30 -0
  46. package/dist/session-content/state.d.ts +59 -0
  47. package/dist/session-content/turn.d.ts +137 -0
  48. package/dist/site.d.ts +0 -1
  49. package/dist/site.d.ts.map +1 -1
  50. package/dist/site.js +135 -207
  51. package/dist/space-schema.d.ts +6 -0
  52. package/dist/space-schema.d.ts.map +1 -0
  53. package/dist/space-schema.js +11 -0
  54. package/dist/stream.d.ts +15 -16
  55. package/dist/stream.d.ts.map +1 -1
  56. package/dist/stream.js +262 -423
  57. package/dist/transport.d.ts +109 -51
  58. package/dist/transport.d.ts.map +1 -1
  59. package/dist/transport.js +90 -11
  60. package/dist/uncertain.d.ts +5 -0
  61. package/dist/uncertain.d.ts.map +1 -0
  62. package/dist/uncertain.js +11 -0
  63. package/dist/upload.d.ts +17 -0
  64. package/dist/upload.d.ts.map +1 -0
  65. package/dist/upload.js +106 -0
  66. package/dist/version.d.ts +0 -1
  67. package/dist/write.d.ts +16 -3
  68. package/dist/write.d.ts.map +1 -1
  69. package/dist/write.js +125 -96
  70. package/package.json +10 -5
  71. package/AGENTS.md +0 -302
  72. package/dist/gather.d.ts +0 -4
  73. package/dist/gather.d.ts.map +0 -1
  74. package/dist/gather.js +0 -45
  75. package/dist/handed.d.ts +0 -15
  76. package/dist/handed.d.ts.map +0 -1
  77. package/dist/handed.js +0 -38
  78. package/dist/referrer.d.ts +0 -7
  79. package/dist/referrer.d.ts.map +0 -1
  80. package/dist/referrer.js +0 -35
package/dist/write.js CHANGED
@@ -1,9 +1,10 @@
1
- import { departure, } from '@agentshouse/mdruntime';
2
- import { binaryFileOf, headChange } from './read.js';
1
+ import { departure, partsChanges, ROW_WEIGHT, } from '@agentshouse/mdruntime';
2
+ import { answered, answering, attachmentOf, headChange, revised } from './read.js';
3
3
  import { sha256Hex, textBytes } from './digest.js';
4
4
  import { nativePath, roomLocalPath } from './map.js';
5
- import { classifyDoor, doorRefusal, scopedRefusal } from './refusal.js';
5
+ import { answeredFailure, classifyDoor, doorRefusal, resetOf, scopedRefusal } from './refusal.js';
6
6
  import { readPosition, roomVersion } from './version.js';
7
+ import { unusable } from './uncertain.js';
7
8
  function priorField(batch, path, name) {
8
9
  return batch.preconditions.find((precondition) => precondition.kind === 'field' && precondition.path === path && precondition.name === name);
9
10
  }
@@ -28,7 +29,7 @@ function created(change, path) {
28
29
  const { type, ...fields } = change.fields;
29
30
  if (typeof type !== 'string' || type.length === 0)
30
31
  return null;
31
- return { op: 'create_typed', path, type, fields, sections: change.sections };
32
+ return { op: 'create_typed', path, type, fields };
32
33
  }
33
34
  function operationOf(batch, change, handle) {
34
35
  const path = nativePath(change.path, handle);
@@ -120,16 +121,36 @@ export function houseChanges(batch, handle) {
120
121
  }
121
122
  return { kind: 'changes', changes };
122
123
  }
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
+ }
123
145
  export function operationHead(batch) {
124
146
  const writer = batch.writtenBy;
125
- return writer === undefined
126
- ? { operation_id: batch.id }
127
- : { operation_id: batch.id, app_ref: writer.app, release: writer.release };
147
+ const head = { operation_id: batch.id };
148
+ return writer === undefined ? head : { ...head, app_ref: writer.app, release: writer.release };
128
149
  }
129
150
  export function batchPaths(changes) {
130
151
  return [
131
152
  ...new Set(changes.flatMap((change) => change.op === 'rename' ? [String(change.path), String(change.to)] : [String(change.path)])),
132
- ].sort();
153
+ ];
133
154
  }
134
155
  export function deltaBody(changes) {
135
156
  return JSON.stringify({ changes });
@@ -144,21 +165,24 @@ export function uploadable(inline, bytes, bounds) {
144
165
  export function deltaDigest(delta) {
145
166
  return sha256Hex(delta);
146
167
  }
147
- function material(value, handle) {
168
+ function* material(value, handle, within = () => true) {
148
169
  if (!Array.isArray(value))
149
170
  return null;
150
171
  const held = [];
151
172
  for (const entry of value) {
173
+ yield ROW_WEIGHT;
152
174
  if (typeof entry !== 'object' || entry === null)
153
175
  return null;
154
176
  const file = entry;
155
177
  if (typeof file.path !== 'string')
156
178
  return null;
179
+ if (!within(file.path))
180
+ continue;
157
181
  if (file.content !== null && typeof file.content !== 'string')
158
182
  return null;
159
183
  if (file.revision !== null && file.revision !== undefined && typeof file.revision !== 'string')
160
184
  return null;
161
- const binary = binaryFileOf(file);
185
+ const binary = attachmentOf(file);
162
186
  if (binary === null)
163
187
  return null;
164
188
  const head = headChange(file);
@@ -181,30 +205,69 @@ function material(value, handle) {
181
205
  }
182
206
  return held;
183
207
  }
184
- function affectedOf(held) {
208
+ function* affectedOf(held) {
185
209
  const affected = {};
186
210
  for (const entry of held) {
187
211
  if (entry.content !== null)
188
212
  affected[entry.path] = entry.content;
213
+ yield ROW_WEIGHT;
189
214
  }
190
215
  return affected;
191
216
  }
192
- 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.attachments.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.attachments.size === 0;
234
+ const { attachments, facts: _facts, ...room } = yield* answered(answer);
235
+ return { roomVersion: roomVersion(at), ...room, ...(unbinaried ? {} : { attachments }) };
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) {
193
261
  const refusal = doorRefusal(body);
194
262
  if (refusal !== null && refusal.code === 'service_mode_write_halted') {
195
263
  return { kind: 'write-halted' };
196
264
  }
197
- if (refusal !== null && refusal.code === 'expired') {
198
- return refusal.fields.restored === true
199
- ? { kind: 'expired', restored: true }
200
- : { kind: 'expired' };
201
- }
202
265
  if (refusal !== null && refusal.code === 'edit_conflict') {
203
266
  const { current, ...fields } = refusal.fields;
204
- const held = material(current, handle) ?? [];
267
+ const held = (yield* material(current, handle)) ?? [];
205
268
  return {
206
269
  kind: 'conflicted',
207
- affected: affectedOf(held),
270
+ affected: yield* affectedOf(held),
208
271
  code: refusal.code,
209
272
  fields,
210
273
  retryable: refusal.retryable,
@@ -214,10 +277,8 @@ export function mutationOutcome(status, body, locator, handle) {
214
277
  return scopedRefusal(locator, 'undisclosed', refusal);
215
278
  }
216
279
  const classified = classifyDoor(status, body, locator);
217
- const reset = refusal?.fields.retry_at;
218
- if (refusal?.retryable === true &&
219
- typeof reset === 'string' &&
220
- classified?.kind === 'transport-uncertainty') {
280
+ const reset = resetOf(refusal);
281
+ if (refusal !== null && reset !== undefined && classified?.kind === 'transport-uncertainty') {
221
282
  return {
222
283
  kind: 'retryable-refusal',
223
284
  code: refusal.code,
@@ -228,8 +289,9 @@ export function mutationOutcome(status, body, locator, handle) {
228
289
  }
229
290
  if (classified !== null && classified.kind !== 'scoped-refusal')
230
291
  return classified;
231
- if (classified !== null && status === 403)
292
+ if (classified !== null && status === 403 && refusal?.code === 'operation_denied') {
232
293
  return classified;
294
+ }
233
295
  if (refusal !== null && status >= 400) {
234
296
  return {
235
297
  kind: 'rejected',
@@ -239,43 +301,20 @@ export function mutationOutcome(status, body, locator, handle) {
239
301
  retryable: refusal.retryable,
240
302
  };
241
303
  }
242
- if (status !== 200 || typeof body !== 'object' || body === null) {
243
- return { kind: 'transport-uncertainty' };
244
- }
304
+ if (status !== 200)
305
+ return answeredFailure(status, body);
306
+ if (typeof body !== 'object' || body === null)
307
+ return unusable();
245
308
  const held = body;
309
+ if (move !== undefined)
310
+ return yield* movedOutcome(held, move);
246
311
  const at = readPosition(held);
247
312
  if (at === null)
248
- return { kind: 'transport-uncertainty' };
249
- const contents = material(held.contents, handle);
313
+ return unusable();
314
+ const contents = yield* material(held.contents, handle);
250
315
  if (contents === null)
251
- return { kind: 'transport-uncertainty' };
252
- const documents = {};
253
- const revisions = {};
254
- const changes = {};
255
- const binaryFiles = {};
256
- const removals = [];
257
- for (const entry of contents) {
258
- if (entry.revision === null) {
259
- removals.push(entry.path);
260
- continue;
261
- }
262
- if (entry.content !== null)
263
- documents[entry.path] = entry.content;
264
- if (entry.binary !== undefined)
265
- binaryFiles[entry.path] = entry.binary;
266
- revisions[entry.path] = entry.revision;
267
- if (entry.head !== undefined)
268
- changes[entry.path] = entry.head;
269
- }
270
- return {
271
- kind: 'accepted',
272
- roomVersion: roomVersion(at),
273
- documents,
274
- revisions,
275
- changes,
276
- removals: removals.sort(),
277
- ...(Object.keys(binaryFiles).length === 0 ? {} : { binaryFiles }),
278
- };
316
+ return unusable();
317
+ return { kind: 'accepted', ...(yield* acceptedOf(at, contents)) };
279
318
  }
280
319
  export function rejectedMapping(mapping) {
281
320
  return {
@@ -295,61 +334,51 @@ export function revisionBody(input) {
295
334
  }
296
335
  function revisionAuthor(entry) {
297
336
  const user = entry.author_user;
337
+ const userName = entry.author_user_name;
298
338
  const site = entry.author_site;
299
- const name = entry.author_site_name;
300
- if (typeof user === 'string' && site === undefined && name === undefined) {
301
- 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 };
302
354
  }
303
- if (user === null && typeof site === 'string' && typeof name === 'string') {
304
- return { kind: 'site', id: site, name };
355
+ if (typeof app === 'string' && typeof appName === 'string') {
356
+ return { kind: 'app', id: app, name: appName };
305
357
  }
306
358
  return null;
307
359
  }
308
- function historyBinary(entry) {
309
- if (entry.file === undefined)
310
- return undefined;
311
- const { file, bytes, media_type: mediaType, sha256 } = entry;
312
- if (typeof file !== 'string' ||
313
- typeof bytes !== 'number' ||
314
- typeof mediaType !== 'string' ||
315
- typeof sha256 !== 'string')
316
- return null;
317
- return { fileRef: file, byteLength: bytes, mediaType, sha256 };
318
- }
319
360
  export function historyOutcome(status, body, locator, request) {
320
361
  const classified = classifyDoor(status, body, locator);
321
362
  if (classified !== null)
322
363
  return classified;
323
364
  if (status !== 200 || typeof body !== 'object' || body === null) {
324
- return { kind: 'transport-uncertainty' };
365
+ return unusable();
325
366
  }
326
367
  const held = body;
327
368
  if (!Array.isArray(held.revisions))
328
- return { kind: 'transport-uncertainty' };
369
+ return unusable();
329
370
  const entries = [];
330
371
  for (const entry of held.revisions) {
331
372
  if (typeof entry !== 'object' || entry === null)
332
- return { kind: 'transport-uncertainty' };
373
+ return unusable();
333
374
  entries.push(entry);
334
375
  }
335
- if (request.revision !== undefined) {
336
- const named = entries[0];
376
+ const named = entries[0];
377
+ if (request.revision !== undefined && typeof named?.media_type !== 'string') {
337
378
  if (named === undefined)
338
379
  return { kind: 'no-history' };
339
- if (named.unavailable === true) {
340
- return {
341
- kind: 'scoped-refusal',
342
- locator,
343
- scope: 'content',
344
- code: 'retained_history_unavailable',
345
- fields: { path: request.path, revision: request.revision },
346
- retryable: false,
347
- };
348
- }
349
- if (typeof named.file === 'string')
350
- return { kind: 'no-history' };
351
380
  if (typeof named.content !== 'string')
352
- return { kind: 'transport-uncertainty' };
381
+ return unusable();
353
382
  return { kind: 'revision', content: named.content };
354
383
  }
355
384
  if (entries.length === 0)
@@ -360,16 +389,16 @@ export function historyOutcome(status, body, locator, request) {
360
389
  if (typeof entry.revision !== 'string' ||
361
390
  author === null ||
362
391
  typeof entry.created_at !== 'string') {
363
- return { kind: 'transport-uncertainty' };
392
+ return unusable();
364
393
  }
365
- const binaryFile = historyBinary(entry);
366
- if (binaryFile === null)
367
- return { kind: 'transport-uncertainty' };
394
+ const attachment = attachmentOf(entry);
395
+ if (attachment === null)
396
+ return unusable();
368
397
  revisions.push({
369
398
  revision: entry.revision,
370
399
  author,
371
400
  time: entry.created_at,
372
- ...(binaryFile === undefined ? {} : { binaryFile }),
401
+ ...(attachment === undefined ? {} : { attachment }),
373
402
  });
374
403
  }
375
404
  const next = held.next_before;
package/package.json CHANGED
@@ -1,6 +1,5 @@
1
1
  {
2
2
  "name": "@agentshouse/mdruntime-adapter",
3
- "version": "0.1.0-c7c29ea9ca063",
4
3
  "description": "Agents House Adapter for the MD Runtime",
5
4
  "license": "MIT",
6
5
  "type": "module",
@@ -9,7 +8,7 @@
9
8
  "access": "public"
10
9
  },
11
10
  "engines": {
12
- "node": ">=24.21.0"
11
+ "node": ">=24.22.1"
13
12
  },
14
13
  "exports": {
15
14
  ".": {
@@ -20,11 +19,17 @@
20
19
  },
21
20
  "files": [
22
21
  "dist",
23
- "AGENTS.md",
24
22
  "README.md",
25
23
  "LICENSE"
26
24
  ],
27
25
  "dependencies": {
28
- "@agentshouse/mdruntime": "0.1.0-c454894e67f74"
29
- }
26
+ "@agentshouse/mdmodel": "0.1.0-c3ebba90039b4",
27
+ "@agentshouse/mdruntime": "0.1.0-c8f16d1da438a"
28
+ },
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "git+https://github.com/agentshouse/core.git",
32
+ "directory": "mdruntime/packages/mdruntime-adapter"
33
+ },
34
+ "version": "0.1.0-c7ccafe31c5f3"
30
35
  }