@agentshouse/mdruntime-adapter 0.1.0-c4b7577bc405d → 0.1.0-c4c8594e5b8d9

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