@agentshouse/mdruntime-adapter 0.1.0-c4e5de8f794bd → 0.1.0-c4f8432079022

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.
package/dist/read.js CHANGED
@@ -1,21 +1,52 @@
1
+ import { patching, ROW_WEIGHT, stepped, } from '@agentshouse/mdruntime';
2
+ import { checkedPayload, gunzipped, parsePart } from './part.js';
1
3
  import { DECIMAL, readPosition, roomVersion } from './version.js';
2
4
  const REVISION_FILE = /^fr_([0-9a-z]+)\.[0-9a-z]+\.[0-9a-z]$/u;
3
- export function identities(revisions) {
4
- const fileIds = Object.fromEntries(Object.entries(revisions).flatMap(([path, revision]) => {
5
- const file = REVISION_FILE.exec(revision)?.[1];
6
- return file === undefined ? [] : [[path, file]];
7
- }));
8
- return Object.keys(fileIds).length === 0 ? {} : { fileIds };
5
+ function fileOf(revision) {
6
+ return REVISION_FILE.exec(revision)?.[1];
7
+ }
8
+ export function answering() {
9
+ return {
10
+ documents: new Map(),
11
+ revisions: new Map(),
12
+ fileIds: new Map(),
13
+ changes: new Map(),
14
+ binaryFiles: new Map(),
15
+ removals: new Set(),
16
+ };
17
+ }
18
+ export function revised(answer, path, revision) {
19
+ answer.revisions.set(path, revision);
20
+ const file = fileOf(revision);
21
+ if (file !== undefined)
22
+ answer.fileIds.set(path, file);
23
+ }
24
+ export function forgotten(answer, path) {
25
+ answer.documents.delete(path);
26
+ answer.revisions.delete(path);
27
+ answer.fileIds.delete(path);
28
+ answer.changes.delete(path);
29
+ answer.binaryFiles.delete(path);
30
+ answer.removals.delete(path);
31
+ }
32
+ export function* answered(answer) {
33
+ const removals = [];
34
+ for (const path of answer.removals) {
35
+ removals.push(path);
36
+ yield ROW_WEIGHT;
37
+ }
38
+ return {
39
+ documents: yield* patching({}, answer.documents),
40
+ revisions: yield* patching({}, answer.revisions),
41
+ ...(answer.fileIds.size === 0 ? {} : { fileIds: yield* patching({}, answer.fileIds) }),
42
+ changes: yield* patching({}, answer.changes),
43
+ binaryFiles: yield* patching({}, answer.binaryFiles),
44
+ removals,
45
+ };
9
46
  }
10
47
  export function authorityRoot(handle) {
11
48
  return `/rooms/${handle}`;
12
49
  }
13
- export function contentPrefixes(tracked) {
14
- return [
15
- authorityRoot(tracked.handle),
16
- ...tracked.schemas.map((schema) => schemaPath(tracked.handle, schema)),
17
- ];
18
- }
19
50
  export function schemaPath(handle, schema) {
20
51
  return `${authorityRoot(handle)}/${schema}`;
21
52
  }
@@ -28,22 +59,37 @@ export function parseGrant(body) {
28
59
  const transfer = body.transfer;
29
60
  if (typeof transfer.url !== 'string')
30
61
  return null;
31
- if (transfer.method === 'GET')
32
- return { method: 'GET', url: transfer.url };
33
62
  if (transfer.method !== 'POST' || typeof transfer.operation !== 'string')
34
63
  return null;
35
- return { method: 'POST', operation: transfer.operation, url: transfer.url };
64
+ return { operation: transfer.operation, url: transfer.url };
65
+ }
66
+ export function parseFileAddress(body) {
67
+ if (typeof body !== 'object' || body === null)
68
+ return null;
69
+ const { download, name, sha256, media_type: mediaType } = body;
70
+ if (typeof download !== 'string' ||
71
+ typeof name !== 'string' ||
72
+ typeof sha256 !== 'string' ||
73
+ typeof mediaType !== 'string') {
74
+ return null;
75
+ }
76
+ return { url: download, name, sha256, mediaType };
36
77
  }
37
78
  function changeAuthor(value) {
38
79
  if (typeof value !== 'object' || value === null)
39
80
  return null;
40
- const { user, site, name } = value;
81
+ const { user, site, inbox, name } = value;
41
82
  if (typeof name !== 'string')
42
83
  return null;
43
- if (typeof user === 'string' && site === undefined)
84
+ const named = [user, site, inbox].filter((id) => id !== undefined);
85
+ if (named.length !== 1)
86
+ return null;
87
+ if (typeof user === 'string')
44
88
  return { kind: 'user', id: user, name };
45
- if (typeof site === 'string' && user === undefined)
89
+ if (typeof site === 'string')
46
90
  return { kind: 'site', id: site, name };
91
+ if (typeof inbox === 'string')
92
+ return { kind: 'inbox', id: inbox, name };
47
93
  return null;
48
94
  }
49
95
  export function headChange(held) {
@@ -105,13 +151,13 @@ function parseFile(value, mode) {
105
151
  ...binaried(binary),
106
152
  };
107
153
  }
108
- function parseScope(value, mode) {
154
+ function parseRoomParts(value) {
109
155
  if (typeof value !== 'object' || value === null)
110
156
  return null;
111
157
  const held = value;
112
158
  if (typeof held.manifest !== 'object' || held.manifest === null)
113
159
  return null;
114
- if (!Array.isArray(held.contents))
160
+ if (!Array.isArray(held.parts))
115
161
  return null;
116
162
  const manifest = held.manifest;
117
163
  if (manifest.scope !== 'room')
@@ -121,78 +167,89 @@ function parseScope(value, mode) {
121
167
  return null;
122
168
  if (typeof manifest.room_handle !== 'string' || manifest.room_handle.length === 0)
123
169
  return null;
124
- if (!Array.isArray(manifest.files))
125
- return null;
126
- const files = [];
127
- for (const entry of manifest.files) {
128
- const file = parseFile(entry, mode);
129
- if (file === null)
170
+ const parts = [];
171
+ for (const entry of held.parts) {
172
+ const part = parsePart(entry);
173
+ if (part === null)
130
174
  return null;
131
- files.push(file);
175
+ parts.push(part);
132
176
  }
133
- const contents = [];
134
- for (const entry of held.contents) {
135
- if (typeof entry !== 'object' || entry === null)
136
- return null;
137
- const content = entry;
138
- if (typeof content.path !== 'string' || typeof content.content !== 'string')
139
- return null;
140
- contents.push({ path: content.path, content: content.content });
141
- }
142
- return { anchor: manifest.anchor, ...at, files, contents };
143
- }
144
- function parseCut(value) {
145
- if (value === undefined)
146
- return undefined;
147
- if (typeof value !== 'object' || value === null)
148
- return null;
149
- const { position, log_epoch: logEpoch } = value;
150
- if (typeof position !== 'string' || typeof logEpoch !== 'string')
151
- return null;
152
- return { position, log_epoch: logEpoch };
153
- }
154
- function parseCursor(value) {
155
- if (typeof value !== 'object' || value === null)
156
- return null;
157
- const { room, path, cut } = value;
158
- if (typeof room !== 'string' || (path !== null && typeof path !== 'string'))
159
- return null;
160
- const parsed = parseCut(cut);
161
- if (parsed === null)
162
- return null;
163
- return { room, path, ...(parsed === undefined ? {} : { cut: parsed }) };
177
+ return { anchor: manifest.anchor, ...at, parts };
164
178
  }
165
- export function parseEnvelope(body) {
179
+ export function parseParted(body) {
166
180
  if (typeof body !== 'object' || body === null)
167
181
  return null;
168
182
  if (!('scopes' in body) || !Array.isArray(body.scopes))
169
183
  return null;
170
- const scopes = [];
184
+ const rooms = [];
171
185
  for (const entry of body.scopes) {
172
- const scope = parseScope(entry, 'credential');
173
- if (scope === null)
186
+ const room = parseRoomParts(entry);
187
+ if (room === null)
174
188
  return null;
175
- scopes.push(scope);
189
+ rooms.push(room);
176
190
  }
177
191
  if (!('next' in body) || body.next === undefined)
178
- return { scopes };
179
- const next = parseCursor(body.next);
180
- return next === null ? null : { scopes, next };
192
+ return { rooms };
193
+ const next = body.next;
194
+ if (typeof next !== 'object' || next === null || typeof next.room !== 'string')
195
+ return null;
196
+ return { rooms, next: next.room };
181
197
  }
182
- export function parseSiteBootstrap(body) {
183
- return parseScope(body, 'site');
198
+ export function parseSiteParted(body) {
199
+ return parseRoomParts(body);
184
200
  }
185
- export function siteBootstrapNext(body) {
186
- if (typeof body !== 'object' || body === null || !('next' in body))
187
- return null;
188
- return typeof body.next === 'string' && body.next.length > 0 ? body.next : null;
201
+ export function emptyScope(room) {
202
+ return {
203
+ anchor: room.anchor,
204
+ position: room.position,
205
+ logEpoch: room.logEpoch,
206
+ files: [],
207
+ contents: [],
208
+ };
209
+ }
210
+ function* partLines(text, mode) {
211
+ const files = [];
212
+ const contents = [];
213
+ let start = 0;
214
+ while (start < text.length) {
215
+ const end = text.indexOf('\n', start);
216
+ if (end === -1)
217
+ return null;
218
+ let entry;
219
+ try {
220
+ entry = JSON.parse(text.slice(start, end));
221
+ }
222
+ catch {
223
+ return null;
224
+ }
225
+ start = end + 1;
226
+ if (folderLine(entry))
227
+ continue;
228
+ const file = parseFile(entry, mode);
229
+ if (file === null)
230
+ return null;
231
+ files.push(file);
232
+ const { content } = entry;
233
+ if (typeof content === 'string')
234
+ contents.push({ path: file.path, content });
235
+ else if (content !== undefined)
236
+ return null;
237
+ yield ROW_WEIGHT;
238
+ }
239
+ return { files, contents };
240
+ }
241
+ function folderLine(entry) {
242
+ if (typeof entry !== 'object' || entry === null || !('folder' in entry))
243
+ return false;
244
+ return typeof entry.folder === 'string' && Object.keys(entry).length === 1;
189
245
  }
190
- export function siteBootstrapOffset(body) {
191
- if (typeof body !== 'object' || body === null || !('offset' in body))
246
+ export async function housePart(room, body, listed, mode) {
247
+ const payload = await checkedPayload(body, listed);
248
+ if (payload === null)
192
249
  return null;
193
- return Number.isSafeInteger(body.offset) && body.offset > 0
194
- ? body.offset
195
- : null;
250
+ const text = await gunzipped(payload);
251
+ const read = text === null ? null : await stepped(partLines(text, mode));
252
+ return read === null ? { kind: 'transport-uncertainty' } : { ...emptyScope(room), ...read };
196
253
  }
197
254
  function parseChange(value, mode) {
198
255
  if (typeof value !== 'object' || value === null)
@@ -310,12 +367,21 @@ export async function wholeView(first, more) {
310
367
  changes: [...changes.values()],
311
368
  };
312
369
  }
313
- export function advancedScope(scope, view) {
314
- const files = new Map(scope.files.map((file) => [file.path, file]));
315
- const contents = new Map(scope.contents.map((entry) => [entry.path, entry.content]));
370
+ export function* advancedScope(scope, view) {
371
+ const files = new Map();
372
+ for (const file of scope.files) {
373
+ files.set(file.path, file);
374
+ yield ROW_WEIGHT;
375
+ }
376
+ const contents = new Map();
377
+ for (const entry of scope.contents) {
378
+ contents.set(entry.path, entry.content);
379
+ yield ROW_WEIGHT;
380
+ }
316
381
  for (const change of view.changes) {
317
382
  files.delete(change.path);
318
383
  contents.delete(change.path);
384
+ yield ROW_WEIGHT;
319
385
  if ('removed' in change)
320
386
  continue;
321
387
  files.set(change.path, {
@@ -328,43 +394,40 @@ export function advancedScope(scope, view) {
328
394
  if ('content' in change)
329
395
  contents.set(change.path, change.content);
330
396
  }
397
+ const listed = [];
398
+ for (const [path, content] of contents) {
399
+ listed.push({ path, content });
400
+ yield ROW_WEIGHT;
401
+ }
331
402
  return {
332
403
  ...scope,
333
404
  position: view.position,
334
405
  logEpoch: view.logEpoch,
335
406
  files: [...files.values()],
336
- contents: [...contents].map(([path, content]) => ({ path, content })),
407
+ contents: listed,
337
408
  };
338
409
  }
339
- export function matchesScope(scope, tracked) {
340
- return scope.anchor === tracked.authority;
341
- }
342
- export function bootstrapOf(scope) {
343
- const contents = new Map(scope.contents.map((entry) => [entry.path, entry.content]));
344
- const documents = {};
345
- const revisions = {};
346
- const changes = {};
347
- const binaryFiles = {};
410
+ export function* bootstrapping(scope) {
411
+ const contents = new Map();
412
+ for (const entry of scope.contents) {
413
+ contents.set(entry.path, entry.content);
414
+ yield ROW_WEIGHT;
415
+ }
416
+ const answer = answering();
348
417
  for (const file of scope.files) {
349
- if (file.revision !== undefined)
350
- revisions[file.path] = file.revision;
418
+ yield ROW_WEIGHT;
351
419
  if (file.head !== undefined)
352
- changes[file.path] = file.head;
420
+ answer.changes.set(file.path, file.head);
353
421
  const content = contents.get(file.path);
354
422
  if (file.binary !== undefined)
355
- binaryFiles[file.path] = file.binary;
423
+ answer.binaryFiles.set(file.path, file.binary);
356
424
  else if (content !== undefined)
357
- documents[file.path] = content;
425
+ answer.documents.set(file.path, content);
426
+ if (file.revision !== undefined)
427
+ revised(answer, file.path, file.revision);
358
428
  }
359
- return {
360
- kind: 'bootstrap',
361
- roomVersion: roomVersion(scope),
362
- documents,
363
- revisions,
364
- ...identities(revisions),
365
- changes,
366
- binaryFiles,
367
- };
429
+ const { removals: _removals, ...page } = yield* answered(answer);
430
+ return { kind: 'bootstrap', roomVersion: roomVersion(scope), ...page };
368
431
  }
369
432
  export function schemaOf(scope, schema) {
370
433
  const file = scope.files.find((entry) => entry.path === schema);
@@ -378,36 +441,27 @@ export function schemaOf(scope, schema) {
378
441
  export function viewOf(enumeration, tracked) {
379
442
  return enumeration.authorities.find((entry) => entry.authority === tracked.authority);
380
443
  }
381
- export function refreshFrom(view, fromRoomVersion) {
444
+ function* refreshing(view, fromRoomVersion) {
382
445
  if (roomVersion(view) === fromRoomVersion)
383
446
  return { kind: 'still-current' };
384
- const documents = {};
385
- const revisions = {};
386
- const changes = {};
387
- const binaryFiles = {};
388
- const removals = [];
447
+ const answer = answering();
389
448
  for (const change of view.changes) {
449
+ yield ROW_WEIGHT;
390
450
  if ('removed' in change) {
391
- removals.push(change.path);
451
+ answer.removals.add(change.path);
392
452
  continue;
393
453
  }
394
454
  if ('content' in change)
395
- documents[change.path] = change.content;
455
+ answer.documents.set(change.path, change.content);
396
456
  else
397
- binaryFiles[change.path] = change.binary;
398
- if (change.revision !== undefined)
399
- revisions[change.path] = change.revision;
457
+ answer.binaryFiles.set(change.path, change.binary);
400
458
  if (change.head !== undefined)
401
- changes[change.path] = change.head;
459
+ answer.changes.set(change.path, change.head);
460
+ if (change.revision !== undefined)
461
+ revised(answer, change.path, change.revision);
402
462
  }
403
- return {
404
- kind: 'net-advancement',
405
- roomVersion: roomVersion(view),
406
- documents,
407
- revisions,
408
- ...identities(revisions),
409
- changes,
410
- binaryFiles,
411
- removals: removals.sort(),
412
- };
463
+ return { kind: 'net-advancement', roomVersion: roomVersion(view), ...(yield* answered(answer)) };
464
+ }
465
+ export async function refreshFrom(view, fromRoomVersion) {
466
+ return stepped(refreshing(view, fromRoomVersion));
413
467
  }
@@ -1,7 +1,9 @@
1
+ import type { ReferrerScope } from '@agentshouse/mdruntime';
1
2
  import type { TrackedAuthority } from './discovery.js';
2
3
  export type HouseReferrer = {
3
4
  locator: string;
4
5
  referrer: Readonly<Record<string, string>>;
5
6
  };
6
7
  export declare function houseReferrer(referrer: unknown, tracked: ReadonlyMap<string, TrackedAuthority>): HouseReferrer | null;
8
+ export declare function houseReferrerScope(referrer: unknown, tracked: ReadonlyMap<string, TrackedAuthority>): ReferrerScope | null;
7
9
  //# sourceMappingURL=referrer.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"referrer.d.ts","sourceRoot":"","sources":["../src/referrer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAEvD,MAAM,MAAM,aAAa,GAAG;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAA;CAAE,CAAC;AAgB5F,wBAAgB,aAAa,CAC3B,QAAQ,EAAE,OAAO,EACjB,OAAO,EAAE,WAAW,CAAC,MAAM,EAAE,gBAAgB,CAAC,GAC7C,aAAa,GAAG,IAAI,CAqBtB"}
1
+ {"version":3,"file":"referrer.d.ts","sourceRoot":"","sources":["../src/referrer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAC5D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAEvD,MAAM,MAAM,aAAa,GAAG;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAA;CAAE,CAAC;AAgB5F,wBAAgB,aAAa,CAC3B,QAAQ,EAAE,OAAO,EACjB,OAAO,EAAE,WAAW,CAAC,MAAM,EAAE,gBAAgB,CAAC,GAC7C,aAAa,GAAG,IAAI,CAoBtB;AAED,wBAAgB,kBAAkB,CAChC,QAAQ,EAAE,OAAO,EACjB,OAAO,EAAE,WAAW,CAAC,MAAM,EAAE,gBAAgB,CAAC,GAC7C,aAAa,GAAG,IAAI,CAOtB"}
package/dist/referrer.js CHANGED
@@ -29,7 +29,13 @@ export function houseReferrer(referrer, tracked) {
29
29
  referrer: { room_ref: roomRef, record: held.record },
30
30
  };
31
31
  }
32
- if (named(held, 'message'))
33
- return { locator: '', referrer: { message: held.message } };
34
32
  return null;
35
33
  }
34
+ export function houseReferrerScope(referrer, tracked) {
35
+ const held = fields(referrer);
36
+ if (held !== null && named(held, 'room', 'path')) {
37
+ return { locator: held.room, path: held.path };
38
+ }
39
+ const found = houseReferrer(referrer, tracked);
40
+ return found === null ? null : { locator: found.locator };
41
+ }
@@ -0,0 +1,6 @@
1
+ export declare const INBOX_CHANNEL_KINDS: readonly ['telegram-bot'];
2
+ export type InboxChannelKind = (typeof INBOX_CHANNEL_KINDS)[number];
3
+ export declare const INBOX_CHAT_TYPE = "inbox-chat";
4
+ export declare const INBOX_MESSAGE_TYPE = "inbox-message";
5
+ export declare const HOUSE_ROOM_TYPES: string;
6
+ //# sourceMappingURL=room-types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"room-types.d.ts","sourceRoot":"","sources":["../src/room-types.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,mBAAmB,YAAI,cAAc,CAAU,CAAC;AAE7D,MAAM,MAAM,gBAAgB,GAAG,CAAC,OAAO,mBAAmB,CAAC,CAAC,MAAM,CAAC,CAAC;AAEpE,eAAO,MAAM,eAAe,eAAe,CAAC;AAE5C,eAAO,MAAM,kBAAkB,kBAAkB,CAAC;AAIlD,eAAO,MAAM,gBAAgB,QA6CjB,CAAC"}
@@ -0,0 +1,50 @@
1
+ export const INBOX_CHANNEL_KINDS = ['telegram-bot'];
2
+ export const INBOX_CHAT_TYPE = 'inbox-chat';
3
+ export const INBOX_MESSAGE_TYPE = 'inbox-message';
4
+ const IDENTITY = "{ type: string, pattern: '^\\S+$' }";
5
+ export const HOUSE_ROOM_TYPES = [
6
+ `${INBOX_CHAT_TYPE}:`,
7
+ ' properties:',
8
+ ' inbox: { type: string, format: uuid }',
9
+ ` channel: { enum: [${INBOX_CHANNEL_KINDS.join(', ')}] }`,
10
+ ` chat: ${IDENTITY}`,
11
+ ' customer:',
12
+ ' type: object',
13
+ ' properties:',
14
+ ' name: { type: string }',
15
+ ` username: ${IDENTITY}`,
16
+ ` phone: ${IDENTITY}`,
17
+ ' required: [inbox, channel, chat, customer]',
18
+ `${INBOX_MESSAGE_TYPE}:`,
19
+ ' properties:',
20
+ ' direction: { enum: [in, out] }',
21
+ ' sender:',
22
+ ' type: object',
23
+ ' properties:',
24
+ ' name: { type: string }',
25
+ ` username: ${IDENTITY}`,
26
+ ' sent_at: { type: string, format: date-time }',
27
+ ` channel_message: ${IDENTITY}`,
28
+ ' state: { enum: [sending, sent, failed] }',
29
+ ' reason: { type: string }',
30
+ ' reactions:',
31
+ ' type: array',
32
+ ' items:',
33
+ ' type: object',
34
+ ' properties:',
35
+ ' reaction: { type: string, minLength: 1 }',
36
+ ' sender:',
37
+ ' type: object',
38
+ ' properties:',
39
+ ` id: ${IDENTITY}`,
40
+ ' name: { type: string }',
41
+ ` username: ${IDENTITY}`,
42
+ ' count: { type: integer, minimum: 1 }',
43
+ ' required: [reaction]',
44
+ ` post: ${IDENTITY}`,
45
+ ' sent_outside: { const: true }',
46
+ ' required: [direction]',
47
+ ' if: { properties: { direction: { const: in } } }',
48
+ ' then: { required: [sent_at] }',
49
+ '',
50
+ ].join('\n');
@@ -1 +1 @@
1
- {"version":3,"file":"site.d.ts","sourceRoot":"","sources":["../src/site.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,OAAO,EAUR,MAAM,wBAAwB,CAAC;AA2BhC,OAAO,EAOL,KAAK,kBAAkB,EACxB,MAAM,gBAAgB,CAAC;AAyCxB,wBAAgB,iBAAiB,CAAC,SAAS,EAAE,kBAAkB,EAAE,QAAQ,UAAQ,GAAG,OAAO,CAwR1F"}
1
+ {"version":3,"file":"site.d.ts","sourceRoot":"","sources":["../src/site.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,OAAO,EAUR,MAAM,wBAAwB,CAAC;AA8BhC,OAAO,EAOL,KAAK,kBAAkB,EACxB,MAAM,gBAAgB,CAAC;AAuCxB,wBAAgB,iBAAiB,CAAC,SAAS,EAAE,kBAAkB,EAAE,QAAQ,UAAQ,GAAG,OAAO,CAmQ1F"}