@agentshouse/mdruntime-adapter 0.1.0-c9f1e531ad7c8 → 0.1.0-ca3a44982cbe7
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 +181 -92
- package/README.md +3 -3
- package/dist/actions.d.ts +43 -2
- package/dist/actions.d.ts.map +1 -1
- package/dist/actions.js +141 -123
- package/dist/adapter.d.ts.map +1 -1
- package/dist/adapter.js +231 -156
- package/dist/discovery.d.ts +14 -27
- package/dist/discovery.d.ts.map +1 -1
- package/dist/discovery.js +56 -102
- 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 +89 -84
- package/dist/stream.d.ts +9 -4
- package/dist/stream.d.ts.map +1 -1
- package/dist/stream.js +115 -77
- package/dist/transport.d.ts +50 -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 -12
- package/dist/links.d.ts.map +0 -1
- package/dist/links.js +0 -66
package/dist/actions.js
CHANGED
|
@@ -1,50 +1,80 @@
|
|
|
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 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]));
|
|
56
|
+
export function houseConversationAgents(locator) {
|
|
57
|
+
return { locator, type: 'house-agent', identity: 'agent', name: 'name', retired: 'retired' };
|
|
58
|
+
}
|
|
22
59
|
export function declaredActions(routes) {
|
|
23
|
-
const mapped = new Map([...CONVERSATION_ROUTES].map(([name, route]) => [route.slice('/door/'.length), name]));
|
|
24
60
|
return routes.map((route) => ({
|
|
25
|
-
name:
|
|
61
|
+
name: APP_NAMES.get(route) ?? route,
|
|
26
62
|
inputShape: {},
|
|
27
63
|
resultKind: 'house',
|
|
28
64
|
}));
|
|
29
65
|
}
|
|
30
66
|
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);
|
|
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));
|
|
39
72
|
const route = routeOf(request.name);
|
|
40
73
|
if (route === null)
|
|
41
74
|
return Promise.resolve(malformed());
|
|
42
75
|
return carry(transport, route, request.name, request.input);
|
|
43
76
|
}
|
|
44
77
|
function routeOf(name) {
|
|
45
|
-
const mapped = CONVERSATION_ROUTES.get(name);
|
|
46
|
-
if (mapped !== undefined)
|
|
47
|
-
return mapped;
|
|
48
78
|
const parts = name.split('/');
|
|
49
79
|
if (parts.some((part) => part === '.' || part === '..'))
|
|
50
80
|
return null;
|
|
@@ -66,51 +96,78 @@ function operationId(input) {
|
|
|
66
96
|
return text(input, 'operation_id') ?? changeBatchIdentity();
|
|
67
97
|
}
|
|
68
98
|
function malformed() {
|
|
69
|
-
return { kind: 'rejected', code: 'invalid_request', fields: {} };
|
|
99
|
+
return { kind: 'rejected', code: 'invalid_request', fields: {}, retryable: false };
|
|
70
100
|
}
|
|
71
|
-
function
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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 ? outcome : { ...outcome, result: { ...held, conversation } };
|
|
90
126
|
}
|
|
91
127
|
function withTurn(body) {
|
|
92
128
|
const held = record(body);
|
|
93
129
|
const turn = text(held, 'message_id');
|
|
94
|
-
return turn === undefined
|
|
130
|
+
return turn === undefined
|
|
131
|
+
? body
|
|
132
|
+
: { ...held, turn };
|
|
95
133
|
}
|
|
96
134
|
export function actionOutcome(answer, name) {
|
|
97
135
|
if (answer.status === 401)
|
|
98
136
|
return { kind: 'revoked-authorization' };
|
|
99
137
|
const refusal = doorRefusal(answer.body);
|
|
100
|
-
if (refusal !== null)
|
|
101
|
-
return { kind: 'rejected',
|
|
102
|
-
}
|
|
138
|
+
if (refusal !== null)
|
|
139
|
+
return { kind: 'rejected', ...refusal };
|
|
103
140
|
if (answer.status === 404)
|
|
104
|
-
return
|
|
141
|
+
return unimplemented(name);
|
|
105
142
|
if (answer.status === 0 || answer.status >= 400)
|
|
106
143
|
return { kind: 'transport-uncertainty' };
|
|
107
|
-
if (name === 'conversation.send')
|
|
108
|
-
return { kind: 'action', result: withTurn(answer.body) };
|
|
109
144
|
return { kind: 'action', result: answer.body };
|
|
110
145
|
}
|
|
146
|
+
function referredRecord(view, roomRef) {
|
|
147
|
+
const held = record(view);
|
|
148
|
+
if (roomRef === undefined || typeof held.record !== 'string' || !Array.isArray(held.attachments))
|
|
149
|
+
return view;
|
|
150
|
+
const referrer = { room_ref: roomRef, record: held.record };
|
|
151
|
+
return {
|
|
152
|
+
...held,
|
|
153
|
+
attachments: held.attachments.map((attachment) => ({ ...record(attachment), referrer })),
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
async function recordsAction(transport, route, input, name) {
|
|
157
|
+
const outcome = actionOutcome(await transport.door(route, input), name);
|
|
158
|
+
if (outcome.kind !== 'action')
|
|
159
|
+
return outcome;
|
|
160
|
+
const roomRef = text(input, 'room_ref');
|
|
161
|
+
const read = record(outcome.result);
|
|
162
|
+
return {
|
|
163
|
+
...outcome,
|
|
164
|
+
result: Array.isArray(read.records)
|
|
165
|
+
? { ...read, records: read.records.map((view) => referredRecord(view, roomRef)) }
|
|
166
|
+
: referredRecord(outcome.result, roomRef),
|
|
167
|
+
};
|
|
168
|
+
}
|
|
111
169
|
async function carry(transport, route, name, input) {
|
|
112
|
-
|
|
113
|
-
return actionOutcome(answer, name);
|
|
170
|
+
return actionOutcome(await transport.door(route, input), name);
|
|
114
171
|
}
|
|
115
172
|
function sendFile(entry) {
|
|
116
173
|
const file = record(entry);
|
|
@@ -180,12 +237,12 @@ function uploadGrant(entry) {
|
|
|
180
237
|
return null;
|
|
181
238
|
return { method: 'POST', operation: upload.operation, url: upload.url };
|
|
182
239
|
}
|
|
183
|
-
async function
|
|
184
|
-
const
|
|
185
|
-
if (!Array.isArray(
|
|
240
|
+
async function uploadFiles(transport, input, result, name) {
|
|
241
|
+
const sent = record(result).files;
|
|
242
|
+
if (!Array.isArray(sent) || sent.length === 0)
|
|
186
243
|
return null;
|
|
187
244
|
const files = Array.isArray(record(input).files) ? record(input).files : [];
|
|
188
|
-
for (const [index, entry] of
|
|
245
|
+
for (const [index, entry] of sent.entries()) {
|
|
189
246
|
const grant = uploadGrant(entry);
|
|
190
247
|
if (grant === 'absent')
|
|
191
248
|
continue;
|
|
@@ -193,81 +250,35 @@ async function uploadOriginals(transport, input, result) {
|
|
|
193
250
|
return malformed();
|
|
194
251
|
const bytes = index < files.length ? await fileBytes(files[index]) : null;
|
|
195
252
|
if (bytes === null) {
|
|
196
|
-
return {
|
|
253
|
+
return {
|
|
254
|
+
kind: 'rejected',
|
|
255
|
+
code: 'conversation_file_descriptor_required',
|
|
256
|
+
fields: {},
|
|
257
|
+
retryable: false,
|
|
258
|
+
};
|
|
197
259
|
}
|
|
198
260
|
const failed = delivered(await transport.transfer({
|
|
199
|
-
act: '
|
|
200
|
-
method: 'POST',
|
|
261
|
+
act: 'conversation_file_upload',
|
|
201
262
|
url: grant.url,
|
|
202
|
-
answer: 'json',
|
|
203
263
|
body: bytes,
|
|
204
264
|
operation: grant.operation,
|
|
205
|
-
}),
|
|
265
|
+
}), name);
|
|
206
266
|
if (failed !== null)
|
|
207
267
|
return failed;
|
|
208
268
|
}
|
|
209
269
|
return null;
|
|
210
270
|
}
|
|
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) {
|
|
271
|
+
async function sendAction(transport, route, input, name) {
|
|
260
272
|
const body = sendBody(input);
|
|
261
273
|
if (body === null)
|
|
262
274
|
return malformed();
|
|
263
|
-
const
|
|
264
|
-
const outcome = actionOutcome(answer, 'conversation.send');
|
|
275
|
+
const outcome = actionOutcome(await transport.door(route, body), name);
|
|
265
276
|
if (outcome.kind !== 'action')
|
|
266
277
|
return outcome;
|
|
267
|
-
const uploaded = await
|
|
278
|
+
const uploaded = await uploadFiles(transport, input, outcome.result, name);
|
|
268
279
|
if (uploaded !== null)
|
|
269
280
|
return uploaded;
|
|
270
|
-
return outcome;
|
|
281
|
+
return { ...outcome, result: withTurn(outcome.result) };
|
|
271
282
|
}
|
|
272
283
|
function historyCursor(value) {
|
|
273
284
|
const acceptedAt = text(value, 'accepted_at');
|
|
@@ -291,12 +302,19 @@ export function acceptedMessage(entry) {
|
|
|
291
302
|
const cursor = historyCursor(entry);
|
|
292
303
|
if (cursor === null)
|
|
293
304
|
return null;
|
|
305
|
+
const message = structuredClone(record(entry));
|
|
306
|
+
const referrer = { message: cursor.message_id };
|
|
294
307
|
return {
|
|
295
308
|
identity: cursor.message_id,
|
|
296
309
|
turn: text(entry, 'turn_id') ?? cursor.message_id,
|
|
297
310
|
position: position(cursor),
|
|
298
311
|
text: text(entry, 'text') ?? '',
|
|
299
|
-
message:
|
|
312
|
+
message: Array.isArray(message.files)
|
|
313
|
+
? {
|
|
314
|
+
...message,
|
|
315
|
+
files: message.files.map((file) => ({ ...record(file), referrer })),
|
|
316
|
+
}
|
|
317
|
+
: message,
|
|
300
318
|
};
|
|
301
319
|
}
|
|
302
320
|
function historyPage(entry) {
|
|
@@ -333,17 +351,17 @@ function historyAsks(input) {
|
|
|
333
351
|
}
|
|
334
352
|
return asks;
|
|
335
353
|
}
|
|
336
|
-
async function historyAction(transport, input) {
|
|
354
|
+
async function historyAction(transport, route, input, name) {
|
|
337
355
|
const asks = historyAsks(input);
|
|
338
356
|
if (asks === null)
|
|
339
357
|
return malformed();
|
|
340
358
|
const histories = [];
|
|
341
359
|
for (let at = 0; at < asks.length; at += HISTORY_CONVERSATIONS) {
|
|
342
|
-
const answer = await transport.door(
|
|
360
|
+
const answer = await transport.door(route, {
|
|
343
361
|
conversations: asks.slice(at, at + HISTORY_CONVERSATIONS),
|
|
344
362
|
limit: HISTORY_PAGE,
|
|
345
363
|
});
|
|
346
|
-
const outcome = actionOutcome(answer,
|
|
364
|
+
const outcome = actionOutcome(answer, name);
|
|
347
365
|
if (outcome.kind !== 'action')
|
|
348
366
|
return outcome;
|
|
349
367
|
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"}
|