@agentshouse/mdruntime-adapter 0.1.0-c80631bb1d363 → 0.1.0-c84b58736cfc7

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 (45) hide show
  1. package/AGENTS.md +107 -95
  2. package/dist/actions.d.ts +12 -21
  3. package/dist/actions.d.ts.map +1 -1
  4. package/dist/actions.js +82 -130
  5. package/dist/adapter.d.ts.map +1 -1
  6. package/dist/adapter.js +210 -163
  7. package/dist/discovery.d.ts +3 -2
  8. package/dist/discovery.d.ts.map +1 -1
  9. package/dist/discovery.js +24 -6
  10. package/dist/gather.d.ts +1 -1
  11. package/dist/gather.d.ts.map +1 -1
  12. package/dist/gather.js +4 -14
  13. package/dist/index.d.ts +3 -1
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +2 -0
  16. package/dist/part.d.ts +15 -0
  17. package/dist/part.d.ts.map +1 -0
  18. package/dist/part.js +85 -0
  19. package/dist/read.d.ts +52 -27
  20. package/dist/read.d.ts.map +1 -1
  21. package/dist/read.js +235 -152
  22. package/dist/room-types.d.ts +6 -0
  23. package/dist/room-types.d.ts.map +1 -0
  24. package/dist/room-types.js +66 -0
  25. package/dist/site.d.ts.map +1 -1
  26. package/dist/site.js +75 -104
  27. package/dist/stream.d.ts +5 -5
  28. package/dist/stream.d.ts.map +1 -1
  29. package/dist/stream.js +59 -37
  30. package/dist/transport.d.ts +24 -15
  31. package/dist/transport.d.ts.map +1 -1
  32. package/dist/transport.js +1 -1
  33. package/dist/upload.d.ts +12 -0
  34. package/dist/upload.d.ts.map +1 -0
  35. package/dist/upload.js +52 -0
  36. package/dist/write.d.ts +12 -1
  37. package/dist/write.d.ts.map +1 -1
  38. package/dist/write.js +92 -55
  39. package/package.json +2 -2
  40. package/dist/handed.d.ts +0 -15
  41. package/dist/handed.d.ts.map +0 -1
  42. package/dist/handed.js +0 -38
  43. package/dist/referrer.d.ts +0 -7
  44. package/dist/referrer.d.ts.map +0 -1
  45. package/dist/referrer.js +0 -35
package/dist/actions.js CHANGED
@@ -1,7 +1,9 @@
1
1
  import { changeBatchIdentity, } from '@agentshouse/mdruntime';
2
2
  import { doorRefusal } from './refusal.js';
3
+ import { finishedUpload, houseUpload } from './upload.js';
3
4
  const HISTORY_PAGE = 100;
4
5
  const HISTORY_CONVERSATIONS = 64;
6
+ const TRIGGER_ROOMS = 100;
5
7
  const OCTET_STREAM = 'application/octet-stream';
6
8
  function mapped(door, input = (given) => given) {
7
9
  return {
@@ -11,16 +13,18 @@ function mapped(door, input = (given) => given) {
11
13
  },
12
14
  };
13
15
  }
14
- function carried(door, carry, batched) {
16
+ function carried(door, carry, batched, room) {
15
17
  return {
16
18
  door,
17
19
  ...(batched === undefined ? {} : { batched }),
20
+ ...(room === undefined ? {} : { room }),
18
21
  carry: (transport, input, name) => carry(transport, `/door/${door}`, input, name),
19
22
  };
20
23
  }
21
24
  const APP_ACTS = {
22
25
  'access.read': mapped('rooms/access/read'),
23
26
  'conversation.answer': mapped('conversations/interaction/answer', answerBody),
27
+ 'conversation.attach': carried('conversations/attach', attachAction, undefined, 'room'),
24
28
  'conversation.create': carried('conversations/create', createAction),
25
29
  'conversation.delete': mapped('conversations/delete', controlBody),
26
30
  'conversation.history': carried('conversations/history', historyAction, ['conversations']),
@@ -28,22 +32,26 @@ const APP_ACTS = {
28
32
  'conversation.kill': mapped('conversations/kill', controlBody),
29
33
  'conversation.markDone': mapped('conversations/done'),
30
34
  'conversation.rename': mapped('conversations/rename'),
35
+ 'conversation.retry': mapped('conversations/retry'),
31
36
  'conversation.send': carried('conversations/send', sendAction),
32
37
  'conversation.setOption': mapped('conversations/option'),
33
38
  'feature.list': mapped('apps/features/read'),
34
39
  'holder.list': mapped('apps/holders'),
35
40
  'holder.ring': mapped('apps/ring'),
41
+ 'inboxChat.assign': mapped('chats/assign'),
42
+ 'inboxChat.return': mapped('chats/return'),
43
+ 'inboxChat.takeControl': mapped('chats/take-control'),
36
44
  'member.list': mapped('rooms/members'),
37
45
  'message.send': mapped('apps/messages/send'),
38
46
  'processing.read': mapped('rooms/settings/processing/read'),
39
- 'record.list': carried('records/read', recordsAction),
47
+ 'record.list': mapped('records/read'),
40
48
  'relay.read': mapped('apps/relay'),
41
49
  'routine.list': mapped('routines/access'),
42
50
  'routineAccess.list': mapped('routines/granted'),
43
51
  'routineAllowance.list': mapped('routines/allowance'),
44
52
  'routineRequest.list': mapped('routines/requests'),
45
53
  'storage.read': mapped('storage/inventory'),
46
- 'trigger.list': mapped('triggers/read'),
54
+ 'trigger.list': carried('triggers/read', triggersAction, ['room_refs'], 'room_refs'),
47
55
  'triggerOccurrence.list': mapped('triggers/fired'),
48
56
  };
49
57
  export const HOUSE_APP_ACTS = Object.fromEntries(Object.entries(APP_ACTS).map(([name, act]) => [name, act.door]));
@@ -53,15 +61,15 @@ export const HOUSE_SITE_ACTS = {
53
61
  };
54
62
  export const HOUSE_APP_BATCHES = Object.fromEntries(Object.entries(APP_ACTS).flatMap(([name, act]) => act.batched === undefined ? [] : [[name, act.batched]]));
55
63
  const APP_NAMES = new Map(Object.entries(APP_ACTS).map(([name, act]) => [act.door, name]));
64
+ const APP_ROOMS = new Map(Object.entries(APP_ACTS).flatMap(([name, act]) => act.room === undefined ? [] : [[name, act.room]]));
56
65
  export function houseConversationAgents(locator) {
57
66
  return { locator, type: 'house-agent', identity: 'agent', name: 'name', retired: 'retired' };
58
67
  }
59
68
  export function declaredActions(routes) {
60
- return routes.map((route) => ({
61
- name: APP_NAMES.get(route) ?? route,
62
- inputShape: {},
63
- resultKind: 'house',
64
- }));
69
+ return routes.map((route) => {
70
+ const name = APP_NAMES.get(route) ?? route;
71
+ return { name, inputShape: {}, resultKind: 'house', room: APP_ROOMS.get(name) ?? 'room_ref' };
72
+ });
65
73
  }
66
74
  export function houseAction(transport, request) {
67
75
  if (Object.hasOwn(APP_ACTS, request.name)) {
@@ -102,7 +110,12 @@ function unimplemented(name) {
102
110
  return { kind: 'rejected', code: 'action-unimplemented', fields: { name }, retryable: false };
103
111
  }
104
112
  function createBody(input) {
105
- return { agent_id: text(input, 'agent_id'), operation_id: operationId(input) };
113
+ const replaces = text(input, 'replaces');
114
+ return {
115
+ agent_id: text(input, 'agent_id'),
116
+ operation_id: operationId(input),
117
+ ...(replaces === undefined ? {} : { replaces }),
118
+ };
106
119
  }
107
120
  function controlBody(input) {
108
121
  return { conversation_id: text(input, 'conversation'), operation_id: operationId(input) };
@@ -150,135 +163,65 @@ export function actionOutcome(answer, name) {
150
163
  return { kind: 'transport-uncertainty' };
151
164
  return { kind: 'action', result: answer.body };
152
165
  }
153
- function referredRecord(view, roomRef) {
154
- const held = record(view);
155
- if (roomRef === undefined || typeof held.record !== 'string' || !Array.isArray(held.attachments))
156
- return view;
157
- const referrer = {
158
- room_ref: roomRef,
159
- record: held.record,
160
- };
161
- return {
162
- ...held,
163
- attachments: held.attachments.map((attachment) => ({ ...record(attachment), referrer })),
164
- };
165
- }
166
- async function recordsAction(transport, route, input, name) {
167
- const outcome = actionOutcome(await transport.door(route, input), name);
168
- if (outcome.kind !== 'action')
169
- return outcome;
170
- const roomRef = text(input, 'room_ref');
171
- const read = record(outcome.result);
172
- return {
173
- ...outcome,
174
- result: Array.isArray(read.records)
175
- ? { ...read, records: read.records.map((view) => referredRecord(view, roomRef)) }
176
- : referredRecord(outcome.result, roomRef),
177
- };
178
- }
179
166
  async function carry(transport, route, name, input) {
180
167
  return actionOutcome(await transport.door(route, input), name);
181
168
  }
182
- function sendFile(entry) {
183
- const file = record(entry);
184
- const name = text(file, 'name');
185
- const bytes = file.byteLength;
186
- const sha256 = text(file, 'contentHash');
187
- if (name === undefined || typeof bytes !== 'number' || sha256 === undefined)
188
- return null;
189
- const mediaType = text(file, 'mediaType');
190
- const width = file.width;
191
- const height = file.height;
192
- return {
193
- name,
194
- media_type: mediaType !== undefined && mediaType.length > 0 ? mediaType : OCTET_STREAM,
195
- bytes,
196
- sha256,
197
- ...(typeof width === 'number' && typeof height === 'number'
198
- ? { dimensions: { width, height } }
199
- : {}),
200
- };
201
- }
202
- function sendFiles(input) {
203
- const files = record(input).files;
204
- if (files === undefined)
205
- return [];
206
- if (!Array.isArray(files))
207
- return null;
208
- const declared = [];
209
- for (const entry of files) {
210
- const file = sendFile(entry);
211
- if (file === null)
212
- return null;
213
- declared.push(file);
214
- }
215
- return declared;
216
- }
217
169
  function sendBody(input) {
218
- const files = sendFiles(input);
219
170
  const operation = text(input, 'operation_id');
220
- if (files === null || operation === undefined)
171
+ const said = text(input, 'text');
172
+ if (operation === undefined || said === undefined)
221
173
  return null;
222
- const textValue = text(input, 'text');
223
- return {
224
- conversation_id: text(input, 'conversation'),
225
- operation_id: operation,
226
- ...(textValue !== undefined && textValue.length > 0 ? { text: textValue } : {}),
227
- ...(files.length === 0 ? {} : { files }),
228
- };
174
+ return { conversation_id: text(input, 'conversation'), operation_id: operation, text: said };
229
175
  }
230
- async function fileBytes(file) {
231
- const blob = record(file).blob;
232
- if (blob === undefined || typeof blob !== 'object' || blob === null)
233
- return null;
234
- if (!('arrayBuffer' in blob) || typeof blob.arrayBuffer !== 'function')
235
- return null;
236
- return new Uint8Array(await blob.arrayBuffer());
176
+ async function sendAction(transport, route, input, name) {
177
+ const body = sendBody(input);
178
+ if (body === null)
179
+ return malformed();
180
+ const outcome = actionOutcome(await transport.door(route, body), name);
181
+ if (outcome.kind !== 'action')
182
+ return outcome;
183
+ return { ...outcome, result: withTurn(outcome.result) };
237
184
  }
238
- function uploadGrant(entry) {
239
- if (!('upload' in record(entry)))
240
- return 'absent';
241
- const upload = record(record(entry).upload);
242
- if (upload.method !== 'POST')
185
+ function attachBody(input) {
186
+ const room = text(input, 'room');
187
+ const name = text(input, 'name');
188
+ const sha256 = text(input, 'contentHash');
189
+ const bytes = record(input).byteLength;
190
+ if (room === undefined || name === undefined || sha256 === undefined)
243
191
  return null;
244
- if (typeof upload.operation !== 'string' || typeof upload.url !== 'string')
192
+ if (typeof bytes !== 'number')
245
193
  return null;
246
- return { method: 'POST', operation: upload.operation, url: upload.url };
247
- }
248
- async function uploadFiles(transport, input, result) {
249
- const sent = record(result).files;
250
- if (!Array.isArray(sent))
251
- return true;
252
- const files = Array.isArray(record(input).files) ? record(input).files : [];
253
- for (const [index, entry] of sent.entries()) {
254
- const grant = uploadGrant(entry);
255
- if (grant === 'absent')
256
- continue;
257
- const bytes = index < files.length ? await fileBytes(files[index]) : null;
258
- if (grant === null || bytes === null)
259
- return false;
260
- const answer = await transport.transfer({
261
- act: 'conversation_file_upload',
262
- url: grant.url,
263
- body: bytes,
264
- operation: grant.operation,
265
- });
266
- if (answer.status !== 200)
267
- return false;
268
- }
269
- return true;
194
+ const mediaType = text(input, 'mediaType');
195
+ return {
196
+ room_ref: room,
197
+ name,
198
+ bytes,
199
+ sha256,
200
+ media_type: mediaType !== undefined && mediaType.length > 0 ? mediaType : OCTET_STREAM,
201
+ };
270
202
  }
271
- async function sendAction(transport, route, input, name) {
272
- const body = sendBody(input);
273
- if (body === null)
203
+ async function attachAction(transport, route, input, name) {
204
+ const body = attachBody(input);
205
+ const blob = record(input).blob;
206
+ if (body === null || !(blob instanceof Blob))
274
207
  return malformed();
275
208
  const outcome = actionOutcome(await transport.door(route, body), name);
276
209
  if (outcome.kind !== 'action')
277
210
  return outcome;
278
- if (!(await uploadFiles(transport, input, outcome.result))) {
211
+ const upload = houseUpload(record(outcome.result).upload);
212
+ if (upload === null)
279
213
  return { kind: 'transport-uncertainty' };
280
- }
281
- return { ...outcome, result: withTurn(outcome.result) };
214
+ const finished = await finishedUpload(transport, upload, blob);
215
+ if (finished === null)
216
+ return { kind: 'transport-uncertainty' };
217
+ const settled = actionOutcome(finished, name);
218
+ if (settled.kind !== 'action')
219
+ return settled;
220
+ const path = text(settled.result, 'path');
221
+ if (path === undefined)
222
+ return { kind: 'transport-uncertainty' };
223
+ const result = { path };
224
+ return { kind: 'action', result };
282
225
  }
283
226
  function historyCursor(value) {
284
227
  const acceptedAt = text(value, 'accepted_at');
@@ -302,19 +245,12 @@ export function acceptedMessage(entry) {
302
245
  const cursor = historyCursor(entry);
303
246
  if (cursor === null)
304
247
  return null;
305
- const message = structuredClone(record(entry));
306
- const referrer = { message: cursor.message_id };
307
248
  return {
308
249
  identity: cursor.message_id,
309
250
  turn: text(entry, 'turn_id') ?? cursor.message_id,
310
251
  position: position(cursor),
311
252
  text: text(entry, 'text') ?? '',
312
- message: Array.isArray(message.files)
313
- ? {
314
- ...message,
315
- files: message.files.map((file) => ({ ...record(file), referrer })),
316
- }
317
- : message,
253
+ message: structuredClone(record(entry)),
318
254
  };
319
255
  }
320
256
  function historyPage(entry) {
@@ -351,6 +287,22 @@ function historyAsks(input) {
351
287
  }
352
288
  return asks;
353
289
  }
290
+ async function triggersAction(transport, route, input, name) {
291
+ const asked = record(input).room_refs;
292
+ if (!Array.isArray(asked) || asked.length === 0)
293
+ return malformed();
294
+ const rooms = [];
295
+ for (let at = 0; at < asked.length; at += TRIGGER_ROOMS) {
296
+ const outcome = actionOutcome(await transport.door(route, { room_refs: asked.slice(at, at + TRIGGER_ROOMS) }), name);
297
+ if (outcome.kind !== 'action')
298
+ return outcome;
299
+ const answered = record(outcome.result).rooms;
300
+ if (!Array.isArray(answered))
301
+ return { kind: 'transport-uncertainty' };
302
+ rooms.push(...answered);
303
+ }
304
+ return { kind: 'action', result: { rooms } };
305
+ }
354
306
  async function historyAction(transport, route, input, name) {
355
307
  const asks = historyAsks(input);
356
308
  if (asks === null)
@@ -1 +1 @@
1
- {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAKV,OAAO,EACP,aAAa,EAmBd,MAAM,wBAAwB,CAAC;AA4ChC,OAAO,EASL,KAAK,kBAAkB,EACvB,KAAK,cAAc,EACpB,MAAM,gBAAgB,CAAC;AAyBxB,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;AAsErD,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,yBAAyB,GAAG,OAAO,CAyjBpF"}
1
+ {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAIV,OAAO,EACP,aAAa,EAkBd,MAAM,wBAAwB,CAAC;AA0ChC,OAAO,EASL,KAAK,kBAAkB,EACvB,KAAK,cAAc,EACpB,MAAM,gBAAgB,CAAC;AA0BxB,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;AA8ErD,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,yBAAyB,GAAG,OAAO,CAioBpF"}