@agentshouse/mdruntime-adapter 0.1.0-c472401ff1b6a → 0.1.0-c4b2019a24549

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/actions.js CHANGED
@@ -1,50 +1,76 @@
1
1
  import { changeBatchIdentity, } from '@agentshouse/mdruntime';
2
2
  import { doorRefusal } from './refusal.js';
3
- import { PRIVATE_LOCATOR } from './transport.js';
4
3
  const HISTORY_PAGE = 100;
5
4
  const HISTORY_CONVERSATIONS = 64;
6
5
  const OCTET_STREAM = 'application/octet-stream';
7
- const CONVERSATION_ROUTES = new Map([
8
- ['conversation.create', '/door/conversations/create'],
9
- ['conversation.send', '/door/conversations/send'],
10
- ['conversation.answer', '/door/conversations/interaction/answer'],
11
- ['conversation.interrupt', '/door/conversations/interrupt'],
12
- ['conversation.kill', '/door/conversations/kill'],
13
- ['conversation.delete', '/door/conversations/delete'],
14
- ['conversation.history', '/door/conversations/history'],
15
- ]);
16
- export const HOUSE_CONVERSATION_AGENTS = {
17
- locator: PRIVATE_LOCATOR,
18
- type: 'house-agent',
19
- identity: 'agent',
20
- name: 'name',
6
+ function mapped(door, input = (given) => given) {
7
+ return {
8
+ door,
9
+ async carry(transport, given, name) {
10
+ return actionOutcome(await transport.door(`/door/${door}`, input(given)), name);
11
+ },
12
+ };
13
+ }
14
+ function carried(door, carry, batched) {
15
+ return {
16
+ door,
17
+ ...(batched === undefined ? {} : { batched }),
18
+ carry: (transport, input, name) => carry(transport, `/door/${door}`, input, name),
19
+ };
20
+ }
21
+ const APP_ACTS = {
22
+ 'access.read': mapped('rooms/access/read'),
23
+ 'conversation.answer': mapped('conversations/interaction/answer', answerBody),
24
+ 'conversation.create': carried('conversations/create', createAction),
25
+ 'conversation.delete': mapped('conversations/delete', controlBody),
26
+ 'conversation.history': carried('conversations/history', historyAction, ['conversations']),
27
+ 'conversation.interrupt': mapped('conversations/interrupt', controlBody),
28
+ 'conversation.kill': mapped('conversations/kill', controlBody),
29
+ 'conversation.markDone': mapped('conversations/done'),
30
+ 'conversation.rename': mapped('conversations/rename'),
31
+ 'conversation.send': carried('conversations/send', sendAction),
32
+ 'conversation.setOption': mapped('conversations/option'),
33
+ 'feature.list': mapped('apps/features/read'),
34
+ 'holder.list': mapped('apps/holders'),
35
+ 'holder.ring': mapped('apps/ring'),
36
+ 'member.list': mapped('rooms/members'),
37
+ 'message.send': mapped('apps/messages/send'),
38
+ 'processing.read': mapped('rooms/settings/processing/read'),
39
+ 'record.list': carried('records/read', recordsAction),
40
+ 'relay.read': mapped('apps/relay'),
41
+ 'routine.list': mapped('routines/access'),
42
+ 'routineAccess.list': mapped('routines/granted'),
43
+ 'routineAllowance.list': mapped('routines/allowance'),
44
+ 'routineRequest.list': mapped('routines/requests'),
45
+ 'storage.read': mapped('storage/inventory'),
46
+ 'trigger.list': mapped('triggers/read'),
47
+ 'triggerOccurrence.list': mapped('triggers/fired'),
21
48
  };
49
+ export const HOUSE_APP_ACTS = Object.fromEntries(Object.entries(APP_ACTS).map(([name, act]) => [name, act.door]));
50
+ export const HOUSE_APP_BATCHES = Object.fromEntries(Object.entries(APP_ACTS).flatMap(([name, act]) => act.batched === undefined ? [] : [[name, act.batched]]));
51
+ const APP_NAMES = new Map(Object.entries(APP_ACTS).map(([name, act]) => [act.door, name]));
52
+ export function houseConversationAgents(locator) {
53
+ return { locator, type: 'house-agent', identity: 'agent', name: 'name', retired: 'retired' };
54
+ }
22
55
  export function declaredActions(routes) {
23
- const mapped = new Map([...CONVERSATION_ROUTES].map(([name, route]) => [route.slice('/door/'.length), name]));
24
56
  return routes.map((route) => ({
25
- name: mapped.get(route) ?? route,
57
+ name: APP_NAMES.get(route) ?? route,
26
58
  inputShape: {},
27
59
  resultKind: 'house',
28
60
  }));
29
61
  }
30
62
  export function houseAction(transport, request) {
31
- if (request.name === 'conversation.send')
32
- return sendAction(transport, request.input);
33
- if (request.name === 'conversation.history')
34
- return historyAction(transport, request.input);
35
- if (request.name === 'attachments/upload')
36
- return attachAction(transport, request.input);
37
- if (request.name === 'attachments/get')
38
- return openAction(transport, request.input);
63
+ if (Object.hasOwn(APP_ACTS, request.name)) {
64
+ return APP_ACTS[request.name].carry(transport, request.input, request.name);
65
+ }
66
+ if (APP_NAMES.has(request.name))
67
+ return Promise.resolve(unimplemented(request.name));
39
68
  const route = routeOf(request.name);
40
69
  if (route === null)
41
70
  return Promise.resolve(malformed());
42
71
  return carry(transport, route, request.name, request.input);
43
72
  }
44
73
  function routeOf(name) {
45
- const mapped = CONVERSATION_ROUTES.get(name);
46
- if (mapped !== undefined)
47
- return mapped;
48
74
  const parts = name.split('/');
49
75
  if (parts.some((part) => part === '.' || part === '..'))
50
76
  return null;
@@ -66,51 +92,78 @@ function operationId(input) {
66
92
  return text(input, 'operation_id') ?? changeBatchIdentity();
67
93
  }
68
94
  function malformed() {
69
- return { kind: 'rejected', code: 'invalid_request', fields: {} };
95
+ return { kind: 'rejected', code: 'invalid_request', fields: {}, retryable: false };
70
96
  }
71
- function bodyOf(name, input) {
72
- if (name === 'conversation.create') {
73
- return { agent_id: text(input, 'agent_id'), operation_id: operationId(input) };
74
- }
75
- if (name === 'conversation.interrupt' ||
76
- name === 'conversation.kill' ||
77
- name === 'conversation.delete') {
78
- return { conversation_id: text(input, 'conversation'), operation_id: operationId(input) };
79
- }
80
- if (name === 'conversation.answer') {
81
- const nested = record(record(input).answer);
82
- return {
83
- conversation_id: text(input, 'conversation'),
84
- operation_id: operationId(input),
85
- interaction_id: text(nested, 'interaction_id'),
86
- response: nested.response,
87
- };
88
- }
89
- return input;
97
+ function unimplemented(name) {
98
+ return { kind: 'rejected', code: 'action-unimplemented', fields: { name }, retryable: false };
99
+ }
100
+ function createBody(input) {
101
+ return { agent_id: text(input, 'agent_id'), operation_id: operationId(input) };
102
+ }
103
+ function controlBody(input) {
104
+ return { conversation_id: text(input, 'conversation'), operation_id: operationId(input) };
105
+ }
106
+ function answerBody(input) {
107
+ const nested = record(record(input).answer);
108
+ return {
109
+ conversation_id: text(input, 'conversation'),
110
+ operation_id: operationId(input),
111
+ interaction_id: text(nested, 'interaction_id'),
112
+ response: nested.response,
113
+ };
114
+ }
115
+ async function createAction(transport, route, input, name) {
116
+ const outcome = actionOutcome(await transport.door(route, createBody(input)), name);
117
+ if (outcome.kind !== 'action')
118
+ return outcome;
119
+ const held = record(outcome.result);
120
+ const conversation = text(held, 'conversation_id');
121
+ return conversation === undefined ? outcome : { ...outcome, result: { ...held, conversation } };
90
122
  }
91
123
  function withTurn(body) {
92
124
  const held = record(body);
93
125
  const turn = text(held, 'message_id');
94
- return turn === undefined ? body : { ...held, turn };
126
+ return turn === undefined
127
+ ? body
128
+ : { ...held, turn };
95
129
  }
96
130
  export function actionOutcome(answer, name) {
97
131
  if (answer.status === 401)
98
132
  return { kind: 'revoked-authorization' };
99
133
  const refusal = doorRefusal(answer.body);
100
- if (refusal !== null) {
101
- return { kind: 'rejected', code: refusal.code, fields: refusal.fields };
102
- }
134
+ if (refusal !== null)
135
+ return { kind: 'rejected', ...refusal };
103
136
  if (answer.status === 404)
104
- return { kind: 'rejected', code: 'action-unimplemented', fields: { name } };
137
+ return unimplemented(name);
105
138
  if (answer.status === 0 || answer.status >= 400)
106
139
  return { kind: 'transport-uncertainty' };
107
- if (name === 'conversation.send')
108
- return { kind: 'action', result: withTurn(answer.body) };
109
140
  return { kind: 'action', result: answer.body };
110
141
  }
142
+ function referredRecord(view, roomRef) {
143
+ const held = record(view);
144
+ if (roomRef === undefined || typeof held.record !== 'string' || !Array.isArray(held.attachments))
145
+ return view;
146
+ const referrer = { room_ref: roomRef, record: held.record };
147
+ return {
148
+ ...held,
149
+ attachments: held.attachments.map((attachment) => ({ ...record(attachment), referrer })),
150
+ };
151
+ }
152
+ async function recordsAction(transport, route, input, name) {
153
+ const outcome = actionOutcome(await transport.door(route, input), name);
154
+ if (outcome.kind !== 'action')
155
+ return outcome;
156
+ const roomRef = text(input, 'room_ref');
157
+ const read = record(outcome.result);
158
+ return {
159
+ ...outcome,
160
+ result: Array.isArray(read.records)
161
+ ? { ...read, records: read.records.map((view) => referredRecord(view, roomRef)) }
162
+ : referredRecord(outcome.result, roomRef),
163
+ };
164
+ }
111
165
  async function carry(transport, route, name, input) {
112
- const answer = await transport.door(route, bodyOf(name, input));
113
- return actionOutcome(answer, name);
166
+ return actionOutcome(await transport.door(route, input), name);
114
167
  }
115
168
  function sendFile(entry) {
116
169
  const file = record(entry);
@@ -180,12 +233,12 @@ function uploadGrant(entry) {
180
233
  return null;
181
234
  return { method: 'POST', operation: upload.operation, url: upload.url };
182
235
  }
183
- async function uploadOriginals(transport, input, result) {
184
- const originals = record(result).originals;
185
- if (!Array.isArray(originals) || originals.length === 0)
236
+ async function uploadFiles(transport, input, result, name) {
237
+ const sent = record(result).files;
238
+ if (!Array.isArray(sent) || sent.length === 0)
186
239
  return null;
187
240
  const files = Array.isArray(record(input).files) ? record(input).files : [];
188
- for (const [index, entry] of originals.entries()) {
241
+ for (const [index, entry] of sent.entries()) {
189
242
  const grant = uploadGrant(entry);
190
243
  if (grant === 'absent')
191
244
  continue;
@@ -193,81 +246,35 @@ async function uploadOriginals(transport, input, result) {
193
246
  return malformed();
194
247
  const bytes = index < files.length ? await fileBytes(files[index]) : null;
195
248
  if (bytes === null) {
196
- return { kind: 'rejected', code: 'conversation_original_descriptor_required', fields: {} };
249
+ return {
250
+ kind: 'rejected',
251
+ code: 'conversation_file_descriptor_required',
252
+ fields: {},
253
+ retryable: false,
254
+ };
197
255
  }
198
256
  const failed = delivered(await transport.transfer({
199
- act: 'conversation_original_upload',
200
- method: 'POST',
257
+ act: 'conversation_file_upload',
201
258
  url: grant.url,
202
- answer: 'json',
203
259
  body: bytes,
204
260
  operation: grant.operation,
205
- }), 'conversation.send');
261
+ }), name);
206
262
  if (failed !== null)
207
263
  return failed;
208
264
  }
209
265
  return null;
210
266
  }
211
- async function attachAction(transport, input) {
212
- const { blob, ...descriptor } = record(input);
213
- const bytes = await fileBytes({ blob });
214
- if (bytes === null)
215
- return malformed();
216
- const answer = await transport.door('/door/attachments/upload', descriptor);
217
- const outcome = actionOutcome(answer, 'attachments/upload');
218
- if (outcome.kind !== 'action')
219
- return outcome;
220
- const grant = uploadGrant(outcome.result);
221
- if (grant === 'absent')
222
- return outcome;
223
- if (grant === null)
224
- return malformed();
225
- const stored = await transport.transfer({
226
- act: 'original_upload',
227
- method: 'POST',
228
- url: grant.url,
229
- answer: 'json',
230
- body: bytes,
231
- operation: grant.operation,
232
- });
233
- return delivered(stored, 'attachments/upload') ?? { kind: 'action', result: stored.body };
234
- }
235
- async function openAction(transport, input) {
236
- const answer = await transport.door('/door/attachments/get', input);
237
- const outcome = actionOutcome(answer, 'attachments/get');
238
- if (outcome.kind !== 'action')
239
- return outcome;
240
- const { download, ...opened } = record(outcome.result);
241
- if (download === undefined)
242
- return outcome;
243
- const grant = record(download);
244
- if (grant.method !== 'GET' || typeof grant.url !== 'string')
245
- return malformed();
246
- const read = await transport.transfer({
247
- act: 'original_download',
248
- method: 'GET',
249
- url: grant.url,
250
- answer: 'bytes',
251
- });
252
- const failed = delivered(read, 'attachments/get');
253
- if (failed !== null)
254
- return failed;
255
- if (!(read.body instanceof Blob))
256
- return { kind: 'transport-uncertainty' };
257
- return { kind: 'action', result: { ...opened, blob: read.body } };
258
- }
259
- async function sendAction(transport, input) {
267
+ async function sendAction(transport, route, input, name) {
260
268
  const body = sendBody(input);
261
269
  if (body === null)
262
270
  return malformed();
263
- const answer = await transport.door('/door/conversations/send', body);
264
- const outcome = actionOutcome(answer, 'conversation.send');
271
+ const outcome = actionOutcome(await transport.door(route, body), name);
265
272
  if (outcome.kind !== 'action')
266
273
  return outcome;
267
- const uploaded = await uploadOriginals(transport, input, outcome.result);
274
+ const uploaded = await uploadFiles(transport, input, outcome.result, name);
268
275
  if (uploaded !== null)
269
276
  return uploaded;
270
- return outcome;
277
+ return { ...outcome, result: withTurn(outcome.result) };
271
278
  }
272
279
  function historyCursor(value) {
273
280
  const acceptedAt = text(value, 'accepted_at');
@@ -291,12 +298,19 @@ export function acceptedMessage(entry) {
291
298
  const cursor = historyCursor(entry);
292
299
  if (cursor === null)
293
300
  return null;
301
+ const message = structuredClone(record(entry));
302
+ const referrer = { message: cursor.message_id };
294
303
  return {
295
304
  identity: cursor.message_id,
296
305
  turn: text(entry, 'turn_id') ?? cursor.message_id,
297
306
  position: position(cursor),
298
307
  text: text(entry, 'text') ?? '',
299
- message: structuredClone(entry),
308
+ message: Array.isArray(message.files)
309
+ ? {
310
+ ...message,
311
+ files: message.files.map((file) => ({ ...record(file), referrer })),
312
+ }
313
+ : message,
300
314
  };
301
315
  }
302
316
  function historyPage(entry) {
@@ -333,17 +347,17 @@ function historyAsks(input) {
333
347
  }
334
348
  return asks;
335
349
  }
336
- async function historyAction(transport, input) {
350
+ async function historyAction(transport, route, input, name) {
337
351
  const asks = historyAsks(input);
338
352
  if (asks === null)
339
353
  return malformed();
340
354
  const histories = [];
341
355
  for (let at = 0; at < asks.length; at += HISTORY_CONVERSATIONS) {
342
- const answer = await transport.door('/door/conversations/history', {
356
+ const answer = await transport.door(route, {
343
357
  conversations: asks.slice(at, at + HISTORY_CONVERSATIONS),
344
358
  limit: HISTORY_PAGE,
345
359
  });
346
- const outcome = actionOutcome(answer, 'conversation.history');
360
+ const outcome = actionOutcome(answer, name);
347
361
  if (outcome.kind !== 'action')
348
362
  return outcome;
349
363
  const answered = record(outcome.result).histories;
@@ -1 +1 @@
1
- {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,OAAO,EACP,aAAa,EAWd,MAAM,wBAAwB,CAAC;AAqChC,OAAO,EAQL,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,CAAA;CAAE,CAAC;AAsBjC,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,yBAAyB,GAAG,OAAO,CA0WpF"}
1
+ {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,OAAO,EACP,aAAa,EAgBd,MAAM,wBAAwB,CAAC;AAuChC,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,CAAA;CAAE,CAAC;AAwBjC,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,yBAAyB,GAAG,OAAO,CA8cpF"}