@agentshouse/mdruntime-adapter 0.1.0-c13e5bc38b145 → 0.1.0-c14c569816699

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,4 +1,5 @@
1
- import { patching, ROW_WEIGHT, stepped, } from '@agentshouse/mdruntime';
1
+ import { paced, patching, ROW_WEIGHT, stepped, } from '@agentshouse/mdruntime';
2
+ import { checkedPayload, gunzipped, parsePart } from './part.js';
2
3
  import { DECIMAL, readPosition, roomVersion } from './version.js';
3
4
  const REVISION_FILE = /^fr_([0-9a-z]+)\.[0-9a-z]+\.[0-9a-z]$/u;
4
5
  function fileOf(revision) {
@@ -12,6 +13,8 @@ export function answering() {
12
13
  changes: new Map(),
13
14
  binaryFiles: new Map(),
14
15
  removals: new Set(),
16
+ folders: new Set(),
17
+ folderRemovals: new Set(),
15
18
  };
16
19
  }
17
20
  export function revised(answer, path, revision) {
@@ -28,12 +31,26 @@ export function forgotten(answer, path) {
28
31
  answer.binaryFiles.delete(path);
29
32
  answer.removals.delete(path);
30
33
  }
31
- export function* answered(answer) {
32
- const removals = [];
33
- for (const path of answer.removals) {
34
- removals.push(path);
34
+ export function folded(answer, path, removed) {
35
+ if (removed) {
36
+ answer.folders.delete(path);
37
+ answer.folderRemovals.add(path);
38
+ }
39
+ else {
40
+ answer.folderRemovals.delete(path);
41
+ answer.folders.add(path);
42
+ }
43
+ }
44
+ function* pathsOf(paths) {
45
+ const list = [];
46
+ for (const path of paths) {
47
+ list.push(path);
35
48
  yield ROW_WEIGHT;
36
49
  }
50
+ return list;
51
+ }
52
+ export function* answered(answer) {
53
+ const removals = yield* pathsOf(answer.removals);
37
54
  return {
38
55
  documents: yield* patching({}, answer.documents),
39
56
  revisions: yield* patching({}, answer.revisions),
@@ -41,6 +58,10 @@ export function* answered(answer) {
41
58
  changes: yield* patching({}, answer.changes),
42
59
  binaryFiles: yield* patching({}, answer.binaryFiles),
43
60
  removals,
61
+ ...(answer.folders.size === 0 ? {} : { folders: yield* pathsOf(answer.folders) }),
62
+ ...(answer.folderRemovals.size === 0
63
+ ? {}
64
+ : { folderRemovals: yield* pathsOf(answer.folderRemovals) }),
44
65
  };
45
66
  }
46
67
  export function authorityRoot(handle) {
@@ -58,22 +79,37 @@ export function parseGrant(body) {
58
79
  const transfer = body.transfer;
59
80
  if (typeof transfer.url !== 'string')
60
81
  return null;
61
- if (transfer.method === 'GET')
62
- return { method: 'GET', url: transfer.url };
63
82
  if (transfer.method !== 'POST' || typeof transfer.operation !== 'string')
64
83
  return null;
65
- return { method: 'POST', operation: transfer.operation, url: transfer.url };
84
+ return { operation: transfer.operation, url: transfer.url };
85
+ }
86
+ export function parseFileAddress(body) {
87
+ if (typeof body !== 'object' || body === null)
88
+ return null;
89
+ const { download, name, sha256, media_type: mediaType } = body;
90
+ if (typeof download !== 'string' ||
91
+ typeof name !== 'string' ||
92
+ typeof sha256 !== 'string' ||
93
+ typeof mediaType !== 'string') {
94
+ return null;
95
+ }
96
+ return { url: download, name, sha256, mediaType };
66
97
  }
67
98
  function changeAuthor(value) {
68
99
  if (typeof value !== 'object' || value === null)
69
100
  return null;
70
- const { user, site, name } = value;
101
+ const { user, site, inbox, name } = value;
71
102
  if (typeof name !== 'string')
72
103
  return null;
73
- if (typeof user === 'string' && site === undefined)
104
+ const named = [user, site, inbox].filter((id) => id !== undefined);
105
+ if (named.length !== 1)
106
+ return null;
107
+ if (typeof user === 'string')
74
108
  return { kind: 'user', id: user, name };
75
- if (typeof site === 'string' && user === undefined)
109
+ if (typeof site === 'string')
76
110
  return { kind: 'site', id: site, name };
111
+ if (typeof inbox === 'string')
112
+ return { kind: 'inbox', id: inbox, name };
77
113
  return null;
78
114
  }
79
115
  export function headChange(held) {
@@ -90,15 +126,12 @@ function headed(head) {
90
126
  return head === undefined ? {} : { head };
91
127
  }
92
128
  export function binaryFileOf(held) {
93
- if (held.file === undefined)
129
+ if (held.media_type === undefined)
94
130
  return undefined;
95
- const { file, bytes, media_type: mediaType, sha256 } = held;
96
- if (typeof file !== 'string' ||
97
- typeof bytes !== 'number' ||
98
- typeof mediaType !== 'string' ||
99
- typeof sha256 !== 'string')
131
+ const { bytes, media_type: mediaType, sha256 } = held;
132
+ if (typeof bytes !== 'number' || typeof mediaType !== 'string' || typeof sha256 !== 'string')
100
133
  return null;
101
- return { fileRef: file, byteLength: bytes, mediaType, sha256 };
134
+ return { byteLength: bytes, mediaType, sha256 };
102
135
  }
103
136
  function binaried(binary) {
104
137
  return binary === undefined ? {} : { binary };
@@ -135,13 +168,31 @@ function parseFile(value, mode) {
135
168
  ...binaried(binary),
136
169
  };
137
170
  }
138
- function* parseScope(value, mode) {
171
+ function parseModels(value, mode) {
172
+ if (!Array.isArray(value))
173
+ return null;
174
+ const files = [];
175
+ const contents = [];
176
+ for (const entry of value) {
177
+ const file = parseFile(entry, mode);
178
+ const { content } = entry;
179
+ if (file === null || typeof content !== 'string')
180
+ return null;
181
+ files.push(file);
182
+ contents.push({ path: file.path, content });
183
+ }
184
+ return { files, contents };
185
+ }
186
+ function parseRoomParts(value, mode) {
139
187
  if (typeof value !== 'object' || value === null)
140
188
  return null;
141
189
  const held = value;
142
190
  if (typeof held.manifest !== 'object' || held.manifest === null)
143
191
  return null;
144
- if (!Array.isArray(held.contents))
192
+ if (!Array.isArray(held.parts))
193
+ return null;
194
+ const models = parseModels(held.models, mode);
195
+ if (models === null)
145
196
  return null;
146
197
  const manifest = held.manifest;
147
198
  if (manifest.scope !== 'room')
@@ -151,79 +202,136 @@ function* parseScope(value, mode) {
151
202
  return null;
152
203
  if (typeof manifest.room_handle !== 'string' || manifest.room_handle.length === 0)
153
204
  return null;
154
- if (!Array.isArray(manifest.files))
155
- return null;
156
- const files = [];
157
- for (const entry of manifest.files) {
158
- const file = parseFile(entry, mode);
159
- if (file === null)
205
+ const parts = [];
206
+ for (const entry of held.parts) {
207
+ const part = parsePart(entry);
208
+ if (part === null)
160
209
  return null;
161
- files.push(file);
162
- yield ROW_WEIGHT;
210
+ parts.push(part);
163
211
  }
164
- const contents = [];
165
- for (const entry of held.contents) {
166
- if (typeof entry !== 'object' || entry === null)
167
- return null;
168
- const content = entry;
169
- if (typeof content.path !== 'string' || typeof content.content !== 'string')
170
- return null;
171
- contents.push({ path: content.path, content: content.content });
172
- yield ROW_WEIGHT;
173
- }
174
- return { anchor: manifest.anchor, ...at, files, contents };
212
+ return { anchor: manifest.anchor, ...at, models, parts };
175
213
  }
176
- function parseCut(value) {
177
- if (value === undefined)
178
- return undefined;
179
- if (typeof value !== 'object' || value === null)
180
- return null;
181
- const { position, log_epoch: logEpoch } = value;
182
- if (typeof position !== 'string' || typeof logEpoch !== 'string')
183
- return null;
184
- return { position, log_epoch: logEpoch };
185
- }
186
- function parseCursor(value) {
187
- if (typeof value !== 'object' || value === null)
188
- return null;
189
- const { room, path, cut } = value;
190
- if (typeof room !== 'string' || (path !== null && typeof path !== 'string'))
191
- return null;
192
- const parsed = parseCut(cut);
193
- if (parsed === null)
194
- return null;
195
- return { room, path, ...(parsed === undefined ? {} : { cut: parsed }) };
196
- }
197
- export function* parseEnvelope(body) {
214
+ export function parseParted(body) {
198
215
  if (typeof body !== 'object' || body === null)
199
216
  return null;
200
217
  if (!('scopes' in body) || !Array.isArray(body.scopes))
201
218
  return null;
202
- const scopes = [];
219
+ const rooms = [];
203
220
  for (const entry of body.scopes) {
204
- const scope = yield* parseScope(entry, 'credential');
205
- if (scope === null)
221
+ const room = parseRoomParts(entry, 'credential');
222
+ if (room === null)
206
223
  return null;
207
- scopes.push(scope);
224
+ rooms.push(room);
208
225
  }
209
226
  if (!('next' in body) || body.next === undefined)
210
- return { scopes };
211
- const next = parseCursor(body.next);
212
- return next === null ? null : { scopes, next };
227
+ return { rooms };
228
+ const next = body.next;
229
+ if (typeof next !== 'object' || next === null || typeof next.room !== 'string')
230
+ return null;
231
+ return { rooms, next: next.room };
213
232
  }
214
- export function* parseSiteBootstrap(body) {
215
- return yield* parseScope(body, 'site');
233
+ export function parseSiteParted(body) {
234
+ return parseRoomParts(body, 'site');
216
235
  }
217
- export function siteBootstrapNext(body) {
218
- if (typeof body !== 'object' || body === null || !('next' in body))
236
+ export function partInventory(room) {
237
+ return {
238
+ parts: room.parts.length,
239
+ bytes: room.parts.reduce((sum, part) => sum + part.bytes, 0),
240
+ };
241
+ }
242
+ export function manifested(room, inventory, pageToken, models) {
243
+ return {
244
+ kind: 'bootstrap',
245
+ roomVersion: roomVersion(room),
246
+ documents: {},
247
+ revisions: {},
248
+ changes: {},
249
+ binaryFiles: {},
250
+ inventory,
251
+ pageToken,
252
+ ...(models === undefined ? {} : { models }),
253
+ };
254
+ }
255
+ export function modelScope(room) {
256
+ return { ...emptyScope(room), files: room.models.files, contents: room.models.contents };
257
+ }
258
+ export function emptyScope(room) {
259
+ return {
260
+ anchor: room.anchor,
261
+ position: room.position,
262
+ logEpoch: room.logEpoch,
263
+ files: [],
264
+ contents: [],
265
+ };
266
+ }
267
+ function partLine(line, mode, read) {
268
+ let entry;
269
+ try {
270
+ entry = JSON.parse(line);
271
+ }
272
+ catch {
273
+ return false;
274
+ }
275
+ if (folderLine(entry)) {
276
+ read.folders.push(entry.folder);
277
+ return true;
278
+ }
279
+ const file = parseFile(entry, mode);
280
+ if (file === null)
281
+ return false;
282
+ read.files.push(file);
283
+ const { content } = entry;
284
+ if (typeof content === 'string')
285
+ read.contents.push({ path: file.path, content });
286
+ else if (content !== undefined)
287
+ return false;
288
+ return true;
289
+ }
290
+ async function partRead(chunks, mode) {
291
+ const read = { files: [], contents: [], folders: [] };
292
+ const pace = paced();
293
+ const unfinished = [];
294
+ for (let chunk = await chunks.read(); chunk.done !== true; chunk = await chunks.read()) {
295
+ const text = chunk.value;
296
+ let start = 0;
297
+ for (let end = text.indexOf('\n'); end !== -1; end = text.indexOf('\n', start)) {
298
+ const piece = text.slice(start, end);
299
+ const line = unfinished.length === 0 ? piece : unfinished.splice(0).join('') + piece;
300
+ if (!partLine(line, mode, read)) {
301
+ await chunks.cancel();
302
+ return null;
303
+ }
304
+ start = end + 1;
305
+ const due = pace(line.length + ROW_WEIGHT);
306
+ if (due instanceof Promise)
307
+ await due;
308
+ }
309
+ if (start < text.length)
310
+ unfinished.push(text.slice(start));
311
+ }
312
+ return unfinished.length === 0 ? read : null;
313
+ }
314
+ function folderLine(entry) {
315
+ if (typeof entry !== 'object' || entry === null || !('folder' in entry))
316
+ return false;
317
+ return typeof entry.folder === 'string' && Object.keys(entry).length === 1;
318
+ }
319
+ export async function housePart(room, frame, listed, mode) {
320
+ const payload = await checkedPayload(frame, listed);
321
+ if (payload === null)
219
322
  return null;
220
- return typeof body.next === 'string' && body.next.length > 0 ? body.next : null;
323
+ const read = await partRead(gunzipped(payload).getReader(), mode).catch(() => null);
324
+ return read === null ? { kind: 'transport-uncertainty' } : { ...emptyScope(room), ...read };
221
325
  }
222
- export function siteBootstrapOffset(body) {
223
- if (typeof body !== 'object' || body === null || !('offset' in body))
326
+ function folderChange(value) {
327
+ if (typeof value !== 'object' || value === null)
224
328
  return null;
225
- return Number.isSafeInteger(body.offset) && body.offset > 0
226
- ? body.offset
329
+ const { path, folder, removed, ...rest } = value;
330
+ return typeof path === 'string' &&
331
+ folder === true &&
332
+ (removed === undefined || removed === true) &&
333
+ Object.keys(rest).length === 0
334
+ ? { path, removed: removed === true }
227
335
  : null;
228
336
  }
229
337
  function parseChange(value, mode) {
@@ -282,7 +390,13 @@ export function parseEnumeration(body, mode) {
282
390
  if (!Array.isArray(held.changes))
283
391
  return null;
284
392
  const changes = [];
393
+ const folders = [];
285
394
  for (const change of held.changes) {
395
+ const folder = folderChange(change);
396
+ if (folder !== null) {
397
+ folders.push(folder);
398
+ continue;
399
+ }
286
400
  const mapped = parseChange(change, mode);
287
401
  if (mapped === null)
288
402
  return null;
@@ -295,6 +409,7 @@ export function parseEnumeration(body, mode) {
295
409
  authority: held.authority,
296
410
  ...at,
297
411
  changes,
412
+ ...(folders.length === 0 ? {} : { folders }),
298
413
  ...(next === undefined ? {} : { next }),
299
414
  });
300
415
  }
@@ -318,6 +433,7 @@ function advances(asked, next) {
318
433
  }
319
434
  export async function wholeView(first, more) {
320
435
  const changes = new Map(first.changes.map((change) => [change.path, change]));
436
+ const folders = new Map((first.folders ?? []).map((folder) => [folder.path, folder]));
321
437
  let view = first;
322
438
  while (view.next !== undefined) {
323
439
  const asked = view.next;
@@ -333,6 +449,10 @@ export async function wholeView(first, more) {
333
449
  changes.delete(change.path);
334
450
  changes.set(change.path, change);
335
451
  }
452
+ for (const folder of page.folders ?? []) {
453
+ folders.delete(folder.path);
454
+ folders.set(folder.path, folder);
455
+ }
336
456
  view = page;
337
457
  }
338
458
  return {
@@ -340,51 +460,9 @@ export async function wholeView(first, more) {
340
460
  position: view.position,
341
461
  logEpoch: first.logEpoch,
342
462
  changes: [...changes.values()],
463
+ ...(folders.size === 0 ? {} : { folders: [...folders.values()] }),
343
464
  };
344
465
  }
345
- export function* advancedScope(scope, view) {
346
- const files = new Map();
347
- for (const file of scope.files) {
348
- files.set(file.path, file);
349
- yield ROW_WEIGHT;
350
- }
351
- const contents = new Map();
352
- for (const entry of scope.contents) {
353
- contents.set(entry.path, entry.content);
354
- yield ROW_WEIGHT;
355
- }
356
- for (const change of view.changes) {
357
- files.delete(change.path);
358
- contents.delete(change.path);
359
- yield ROW_WEIGHT;
360
- if ('removed' in change)
361
- continue;
362
- files.set(change.path, {
363
- path: change.path,
364
- revision: change.revision,
365
- version: change.version,
366
- ...headed(change.head),
367
- ...('binary' in change ? { binary: change.binary } : {}),
368
- });
369
- if ('content' in change)
370
- contents.set(change.path, change.content);
371
- }
372
- const listed = [];
373
- for (const [path, content] of contents) {
374
- listed.push({ path, content });
375
- yield ROW_WEIGHT;
376
- }
377
- return {
378
- ...scope,
379
- position: view.position,
380
- logEpoch: view.logEpoch,
381
- files: [...files.values()],
382
- contents: listed,
383
- };
384
- }
385
- export function matchesScope(scope, tracked) {
386
- return scope.anchor === tracked.authority;
387
- }
388
466
  export function* bootstrapping(scope) {
389
467
  const contents = new Map();
390
468
  for (const entry of scope.contents) {
@@ -404,7 +482,9 @@ export function* bootstrapping(scope) {
404
482
  if (file.revision !== undefined)
405
483
  revised(answer, file.path, file.revision);
406
484
  }
407
- const { removals: _removals, ...page } = yield* answered(answer);
485
+ for (const folder of scope.folders ?? [])
486
+ folded(answer, folder, false);
487
+ const { removals: _removals, folderRemovals: _folderRemovals, ...page } = yield* answered(answer);
408
488
  return { kind: 'bootstrap', roomVersion: roomVersion(scope), ...page };
409
489
  }
410
490
  export function schemaOf(scope, schema) {
@@ -438,6 +518,10 @@ function* refreshing(view, fromRoomVersion) {
438
518
  if (change.revision !== undefined)
439
519
  revised(answer, change.path, change.revision);
440
520
  }
521
+ for (const folder of view.folders ?? []) {
522
+ yield ROW_WEIGHT;
523
+ folded(answer, folder.path, folder.removed);
524
+ }
441
525
  return { kind: 'net-advancement', roomVersion: roomVersion(view), ...(yield* answered(answer)) };
442
526
  }
443
527
  export async function refreshFrom(view, fromRoomVersion) {
@@ -0,0 +1,6 @@
1
+ export declare const INBOX_CHANNEL_KINDS: readonly ['telegram-bot', 'whatsapp', 'messenger', 'instagram', 'viber', 'tiktok'];
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,YAC9B,cAAc,EACd,UAAU,EACV,WAAW,EACX,WAAW,EACX,OAAO,EACP,QAAQ,CACA,CAAC;AAEX,MAAM,MAAM,gBAAgB,GAAG,CAAC,OAAO,mBAAmB,CAAC,CAAC,MAAM,CAAC,CAAC;AAEpE,eAAO,MAAM,eAAe,eAAe,CAAC;AAE5C,eAAO,MAAM,kBAAkB,kBAAkB,CAAC;AAalD,eAAO,MAAM,gBAAgB,QAgEjB,CAAC"}
@@ -0,0 +1,83 @@
1
+ export const INBOX_CHANNEL_KINDS = [
2
+ 'telegram-bot',
3
+ 'whatsapp',
4
+ 'messenger',
5
+ 'instagram',
6
+ 'viber',
7
+ 'tiktok',
8
+ ];
9
+ export const INBOX_CHAT_TYPE = 'inbox-chat';
10
+ export const INBOX_MESSAGE_TYPE = 'inbox-message';
11
+ const IDENTITY = "{ type: string, pattern: '^\\S+$' }";
12
+ const AGENT = [' agent: { type: string, format: uuid }', ' name: { type: string }'];
13
+ const ASSIGNEE = [
14
+ ' type: object',
15
+ ' properties:',
16
+ ' user: { type: string, format: uuid }',
17
+ ...AGENT,
18
+ ];
19
+ export const HOUSE_ROOM_TYPES = [
20
+ `${INBOX_CHAT_TYPE}:`,
21
+ ' properties:',
22
+ ' inbox: { type: string, format: uuid }',
23
+ ` channel: { enum: [${INBOX_CHANNEL_KINDS.join(', ')}] }`,
24
+ ` chat: ${IDENTITY}`,
25
+ ' customer:',
26
+ ' type: object',
27
+ ' properties:',
28
+ ' name: { type: string }',
29
+ ` username: ${IDENTITY}`,
30
+ ` phone: ${IDENTITY}`,
31
+ ' assignee:',
32
+ ...ASSIGNEE,
33
+ ' return_to:',
34
+ ' type: object',
35
+ ' properties:',
36
+ ...AGENT,
37
+ ' conversation: { type: string, format: uuid }',
38
+ ' required: [inbox, channel, chat, customer]',
39
+ `${INBOX_MESSAGE_TYPE}:`,
40
+ ' properties:',
41
+ ' direction: { enum: [in, out] }',
42
+ ' sender:',
43
+ ' type: object',
44
+ ' properties:',
45
+ ' name: { type: string }',
46
+ ` username: ${IDENTITY}`,
47
+ ' sent_at: { type: string, format: date-time }',
48
+ ` channel_message: ${IDENTITY}`,
49
+ ' state: { enum: [sending, sent, failed] }',
50
+ ' reason: { type: string }',
51
+ ' reactions:',
52
+ ' type: array',
53
+ ' items:',
54
+ ' type: object',
55
+ ' properties:',
56
+ ' reaction: { type: string, minLength: 1 }',
57
+ ' sender:',
58
+ ' type: object',
59
+ ' properties:',
60
+ ` id: ${IDENTITY}`,
61
+ ' name: { type: string }',
62
+ ` username: ${IDENTITY}`,
63
+ ' count: { type: integer, minimum: 1 }',
64
+ ' required: [reaction]',
65
+ ` post: ${IDENTITY}`,
66
+ ' assignee:',
67
+ ...ASSIGNEE,
68
+ ' sent_outside: { const: true }',
69
+ ' template:',
70
+ ' type: object',
71
+ ' properties:',
72
+ ' name: { type: string, minLength: 1 }',
73
+ ' language: { type: string, minLength: 1 }',
74
+ ' required: [name, language]',
75
+ ' values:',
76
+ ' type: object',
77
+ ' additionalProperties: { type: string }',
78
+ ' withheld: { const: true }',
79
+ ' required: [direction]',
80
+ ' if: { properties: { direction: { const: in } } }',
81
+ ' then: { required: [sent_at] }',
82
+ '',
83
+ ].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;AA4BhC,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,EAWR,MAAM,wBAAwB,CAAC;AA+BhC,OAAO,EAML,KAAK,kBAAkB,EACxB,MAAM,gBAAgB,CAAC;AAoCxB,wBAAgB,iBAAiB,CAAC,SAAS,EAAE,kBAAkB,EAAE,QAAQ,UAAQ,GAAG,OAAO,CAuP1F"}