@agentshouse/mdruntime-adapter 0.1.0-c6ddfd288ac81 → 0.1.0-c7d6f34caf6de
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/AGENTS.md +164 -56
- package/README.md +3 -3
- package/dist/actions.d.ts +41 -2
- package/dist/actions.d.ts.map +1 -1
- package/dist/actions.js +137 -123
- package/dist/adapter.d.ts.map +1 -1
- package/dist/adapter.js +231 -154
- package/dist/discovery.d.ts +14 -27
- package/dist/discovery.d.ts.map +1 -1
- package/dist/discovery.js +52 -101
- package/dist/gather.d.ts +1 -0
- package/dist/gather.d.ts.map +1 -1
- package/dist/gather.js +11 -6
- package/dist/handed.d.ts +15 -0
- package/dist/handed.d.ts.map +1 -0
- package/dist/handed.js +38 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/model.d.ts +8 -18
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +63 -39
- package/dist/read.d.ts +16 -11
- package/dist/read.d.ts.map +1 -1
- package/dist/read.js +80 -106
- package/dist/referrer.d.ts +7 -0
- package/dist/referrer.d.ts.map +1 -0
- package/dist/referrer.js +35 -0
- package/dist/refusal.d.ts +5 -7
- package/dist/refusal.d.ts.map +1 -1
- package/dist/refusal.js +31 -32
- package/dist/site.d.ts.map +1 -1
- package/dist/site.js +86 -79
- package/dist/stream.d.ts +11 -4
- package/dist/stream.d.ts.map +1 -1
- package/dist/stream.js +155 -102
- package/dist/transport.d.ts +51 -20
- package/dist/transport.d.ts.map +1 -1
- package/dist/transport.js +3 -2
- package/dist/version.d.ts +1 -0
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +9 -0
- package/dist/write.d.ts +4 -5
- package/dist/write.d.ts.map +1 -1
- package/dist/write.js +98 -25
- package/package.json +4 -4
- package/dist/links.d.ts +0 -11
- package/dist/links.d.ts.map +0 -1
- package/dist/links.js +0 -51
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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:
|
|
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
|
|
32
|
-
return
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
|
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',
|
|
102
|
-
}
|
|
134
|
+
if (refusal !== null)
|
|
135
|
+
return { kind: 'rejected', ...refusal };
|
|
103
136
|
if (answer.status === 404)
|
|
104
|
-
return
|
|
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
|
-
|
|
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
|
|
184
|
-
const
|
|
185
|
-
if (!Array.isArray(
|
|
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
|
|
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 {
|
|
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: '
|
|
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
|
-
}),
|
|
261
|
+
}), name);
|
|
206
262
|
if (failed !== null)
|
|
207
263
|
return failed;
|
|
208
264
|
}
|
|
209
265
|
return null;
|
|
210
266
|
}
|
|
211
|
-
async function
|
|
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
|
|
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
|
|
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:
|
|
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(
|
|
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,
|
|
360
|
+
const outcome = actionOutcome(answer, name);
|
|
347
361
|
if (outcome.kind !== 'action')
|
|
348
362
|
return outcome;
|
|
349
363
|
const answered = record(outcome.result).histories;
|
package/dist/adapter.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,OAAO,EACP,aAAa,
|
|
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"}
|