@agentshouse/mdruntime-adapter 0.1.0-cc39535833e8f → 0.1.0-cc3d05e27ae63

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