@agentshouse/mdruntime-adapter 0.1.0-ca69607040f50 → 0.1.0-ca7fab027bb19

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.
Files changed (70) hide show
  1. package/README.md +2 -2
  2. package/dist/actions.d.ts +4 -66
  3. package/dist/actions.d.ts.map +1 -1
  4. package/dist/actions.js +7 -338
  5. package/dist/adapter.d.ts.map +1 -1
  6. package/dist/adapter.js +228 -364
  7. package/dist/app-reference.d.ts +2 -0
  8. package/dist/app-reference.d.ts.map +1 -0
  9. package/dist/app-reference.js +2 -0
  10. package/dist/chunks.d.ts +38 -0
  11. package/dist/chunks.d.ts.map +1 -0
  12. package/dist/chunks.js +247 -0
  13. package/dist/crockford.d.ts +4 -0
  14. package/dist/crockford.d.ts.map +1 -0
  15. package/dist/crockford.js +3 -0
  16. package/dist/discovery.d.ts +23 -15
  17. package/dist/discovery.d.ts.map +1 -1
  18. package/dist/discovery.js +92 -55
  19. package/dist/history.d.ts +12 -0
  20. package/dist/history.d.ts.map +1 -0
  21. package/dist/history.js +92 -0
  22. package/dist/house-model.d.ts +672 -0
  23. package/dist/house-model.d.ts.map +1 -0
  24. package/dist/house-model.js +377 -0
  25. package/dist/index.d.ts +11 -2
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/index.js +10 -1
  28. package/dist/model.d.ts +5 -4
  29. package/dist/model.d.ts.map +1 -1
  30. package/dist/model.js +34 -41
  31. package/dist/read.d.ts +57 -64
  32. package/dist/read.d.ts.map +1 -1
  33. package/dist/read.js +154 -318
  34. package/dist/refusal.d.ts +0 -3
  35. package/dist/refusal.d.ts.map +1 -1
  36. package/dist/refusal.js +0 -5
  37. package/dist/room-types.d.ts +5 -0
  38. package/dist/room-types.d.ts.map +1 -0
  39. package/dist/room-types.js +120 -0
  40. package/dist/search.d.ts +5 -0
  41. package/dist/search.d.ts.map +1 -0
  42. package/dist/search.js +47 -0
  43. package/dist/site.d.ts.map +1 -1
  44. package/dist/site.js +81 -200
  45. package/dist/space-schema.d.ts +7 -0
  46. package/dist/space-schema.d.ts.map +1 -0
  47. package/dist/space-schema.js +18 -0
  48. package/dist/stream.d.ts +16 -15
  49. package/dist/stream.d.ts.map +1 -1
  50. package/dist/stream.js +254 -422
  51. package/dist/transport.d.ts +64 -43
  52. package/dist/transport.d.ts.map +1 -1
  53. package/dist/transport.js +54 -11
  54. package/dist/upload.d.ts +12 -0
  55. package/dist/upload.d.ts.map +1 -0
  56. package/dist/upload.js +63 -0
  57. package/dist/write.d.ts +18 -2
  58. package/dist/write.d.ts.map +1 -1
  59. package/dist/write.js +105 -75
  60. package/package.json +8 -4
  61. package/AGENTS.md +0 -308
  62. package/dist/gather.d.ts +0 -4
  63. package/dist/gather.d.ts.map +0 -1
  64. package/dist/gather.js +0 -45
  65. package/dist/handed.d.ts +0 -15
  66. package/dist/handed.d.ts.map +0 -1
  67. package/dist/handed.js +0 -38
  68. package/dist/referrer.d.ts +0 -7
  69. package/dist/referrer.d.ts.map +0 -1
  70. package/dist/referrer.js +0 -35
package/README.md CHANGED
@@ -24,8 +24,8 @@ host.attach(port, 'my-app');
24
24
  ```
25
25
 
26
26
  App code never imports this package; it reaches the Runtime only through the
27
- client its host hands it. The rules an App's agent follows are in the
28
- package's `AGENTS.md`.
27
+ client its host hands it. The rules an App's agent follows are the `apps/` pages
28
+ of House's Guide, which House's `guide/open` opens.
29
29
 
30
30
  ## License
31
31
 
package/dist/actions.d.ts CHANGED
@@ -1,69 +1,7 @@
1
- import { type AcceptedMessage, type ActionDeclaration, type ActionRequest, type ActionResult, type ConversationAgents, type ConversationHistories, type ConversationHistoryRequest, type FileDeclaration } from '@agentshouse/mdruntime';
1
+ import type { ActionDeclaration, ActionRequest, ActionResult } from '@agentshouse/mdruntime';
2
2
  import type { HouseDoorAnswer, HouseTransport } from './transport.js';
3
- type Operated = {
4
- readonly operation_id?: string;
5
- };
6
- type ConversationControl = Operated & {
7
- readonly conversation: string;
8
- };
9
- type SentFile = FileDeclaration & {
10
- readonly blob: Blob;
11
- };
12
- export type AppInputs = {
13
- readonly 'conversation.answer': ConversationControl & {
14
- readonly answer: {
15
- readonly interaction_id: string;
16
- readonly response: unknown;
17
- };
18
- };
19
- readonly 'conversation.create': Operated & {
20
- readonly agent_id: string;
21
- };
22
- readonly 'conversation.delete': ConversationControl;
23
- readonly 'conversation.history': ConversationHistoryRequest;
24
- readonly 'conversation.interrupt': ConversationControl;
25
- readonly 'conversation.kill': ConversationControl;
26
- readonly 'conversation.send': ConversationControl & {
27
- readonly operation_id: string;
28
- readonly text?: string;
29
- readonly files?: readonly SentFile[];
30
- };
31
- };
32
- export type AppAnswers = {
33
- readonly 'conversation.history': ConversationHistories;
34
- };
35
- export type AppAnswerAdditions = {
36
- readonly 'conversation.create': {
37
- readonly conversation: string;
38
- };
39
- readonly 'conversation.send': {
40
- readonly turn: string;
41
- };
42
- readonly 'record.list': {
43
- readonly attachments: readonly {
44
- readonly referrer: {
45
- readonly room_ref: string;
46
- readonly record: string;
47
- };
48
- }[];
49
- readonly records: readonly {
50
- readonly attachments: readonly {
51
- readonly referrer: {
52
- readonly room_ref: string;
53
- readonly record: string;
54
- };
55
- }[];
56
- }[];
57
- };
58
- };
59
- export declare const HOUSE_APP_ACTS: Readonly<Record<string, string>>;
60
- export declare const SITE_SUBMIT = "submit";
61
- export declare const HOUSE_SITE_ACTS: Readonly<Record<string, string>>;
62
- export declare const HOUSE_APP_BATCHES: Readonly<Partial<Record<string, readonly string[]>>>;
63
- export declare function houseConversationAgents(locator: string): ConversationAgents;
3
+ export type HouseActDoor = Pick<HouseTransport, 'door'>;
64
4
  export declare function declaredActions(routes: readonly string[]): readonly ActionDeclaration[];
65
- export declare function houseAction(transport: HouseTransport, request: ActionRequest): Promise<ActionResult>;
66
- export declare function actionOutcome(answer: HouseDoorAnswer, name: string): ActionResult;
67
- export declare function acceptedMessage(entry: unknown): AcceptedMessage | null;
68
- export {};
5
+ export declare function houseAction(transport: HouseActDoor, request: ActionRequest): Promise<ActionResult>;
6
+ export declare function actionOutcome(answer: HouseDoorAnswer, name: string, input: unknown): ActionResult;
69
7
  //# sourceMappingURL=actions.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"actions.d.ts","sourceRoot":"","sources":["../src/actions.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,eAAe,EACpB,KAAK,iBAAiB,EACtB,KAAK,aAAa,EAClB,KAAK,YAAY,EACjB,KAAK,kBAAkB,EACvB,KAAK,qBAAqB,EAE1B,KAAK,0BAA0B,EAC/B,KAAK,eAAe,EACrB,MAAM,wBAAwB,CAAC;AAEhC,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAQtE,KAAK,QAAQ,GAAG;IAAE,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAEnD,KAAK,mBAAmB,GAAG,QAAQ,GAAG;IAAE,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAA;CAAE,CAAC;AAExE,KAAK,QAAQ,GAAG,eAAe,GAAG;IAAE,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAA;CAAE,CAAC;AAE1D,MAAM,MAAM,SAAS,GAAG;IACtB,QAAQ,CAAC,qBAAqB,EAAE,mBAAmB,GAAG;QACpD,QAAQ,CAAC,MAAM,EAAE;YAAE,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;YAAC,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAA;SAAE,CAAC;KAClF,CAAC;IACF,QAAQ,CAAC,qBAAqB,EAAE,QAAQ,GAAG;QAAE,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAC;IACzE,QAAQ,CAAC,qBAAqB,EAAE,mBAAmB,CAAC;IACpD,QAAQ,CAAC,sBAAsB,EAAE,0BAA0B,CAAC;IAC5D,QAAQ,CAAC,wBAAwB,EAAE,mBAAmB,CAAC;IACvD,QAAQ,CAAC,mBAAm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8 @@
1
- import { changeBatchIdentity, } from '@agentshouse/mdruntime';
2
1
  import { doorRefusal } from './refusal.js';
3
- const HISTORY_PAGE = 100;
4
- const HISTORY_CONVERSATIONS = 64;
5
- const OCTET_STREAM = 'application/octet-stream';
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]));
56
- export function houseConversationAgents(locator) {
57
- return { locator, type: 'house-agent', identity: 'agent', name: 'name', retired: 'retired' };
58
- }
59
2
  export function declaredActions(routes) {
60
- return routes.map((route) => ({
61
- name: APP_NAMES.get(route) ?? route,
62
- inputShape: {},
63
- resultKind: 'house',
64
- }));
3
+ return routes.map((name) => ({ name, inputShape: {}, resultKind: 'house', room: 'room_ref' }));
65
4
  }
66
5
  export function houseAction(transport, request) {
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));
72
6
  const route = routeOf(request.name);
73
7
  if (route === null)
74
8
  return Promise.resolve(malformed());
@@ -85,63 +19,22 @@ function routeOf(name) {
85
19
  return null;
86
20
  }
87
21
  }
88
- function record(value) {
89
- return typeof value === 'object' && value !== null ? value : {};
90
- }
91
- function text(value, name) {
92
- const held = record(value)[name];
93
- return typeof held === 'string' ? held : undefined;
94
- }
95
- function operationId(input) {
96
- return text(input, 'operation_id') ?? changeBatchIdentity();
97
- }
98
22
  function malformed() {
99
23
  return { kind: 'rejected', code: 'invalid_request', fields: {}, retryable: false };
100
24
  }
101
25
  function unimplemented(name) {
102
26
  return { kind: 'rejected', code: 'action-unimplemented', fields: { name }, retryable: false };
103
27
  }
104
- function createBody(input) {
105
- return { agent_id: text(input, 'agent_id'), operation_id: operationId(input) };
28
+ function numbered(input) {
29
+ return typeof input?.operation_id === 'string';
106
30
  }
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 },
130
- };
131
- }
132
- function withTurn(body) {
133
- const held = record(body);
134
- const turn = text(held, 'message_id');
135
- return turn === undefined
136
- ? body
137
- : { ...held, turn };
138
- }
139
- export function actionOutcome(answer, name) {
31
+ export function actionOutcome(answer, name, input) {
140
32
  if (answer.status === 401)
141
33
  return { kind: 'revoked-authorization' };
142
34
  const refusal = doorRefusal(answer.body);
143
- if (refusal?.code === 'operation_contended')
35
+ if (refusal?.code === 'operation_contended' && numbered(input)) {
144
36
  return { kind: 'transport-uncertainty' };
37
+ }
145
38
  if (refusal !== null)
146
39
  return { kind: 'rejected', ...refusal };
147
40
  if (answer.status === 404)
@@ -150,230 +43,6 @@ export function actionOutcome(answer, name) {
150
43
  return { kind: 'transport-uncertainty' };
151
44
  return { kind: 'action', result: answer.body };
152
45
  }
153
- function referredRecord(view, roomRef) {
154
- const held = record(view);
155
- if (roomRef === undefined || typeof held.record !== 'string' || !Array.isArray(held.attachments))
156
- return view;
157
- const referrer = {
158
- room_ref: roomRef,
159
- record: held.record,
160
- };
161
- return {
162
- ...held,
163
- attachments: held.attachments.map((attachment) => ({ ...record(attachment), referrer })),
164
- };
165
- }
166
- async function recordsAction(transport, route, input, name) {
167
- const outcome = actionOutcome(await transport.door(route, input), name);
168
- if (outcome.kind !== 'action')
169
- return outcome;
170
- const roomRef = text(input, 'room_ref');
171
- const read = record(outcome.result);
172
- return {
173
- ...outcome,
174
- result: Array.isArray(read.records)
175
- ? { ...read, records: read.records.map((view) => referredRecord(view, roomRef)) }
176
- : referredRecord(outcome.result, roomRef),
177
- };
178
- }
179
46
  async function carry(transport, route, name, input) {
180
- return actionOutcome(await transport.door(route, input), name);
181
- }
182
- function sendFile(entry) {
183
- const file = record(entry);
184
- const name = text(file, 'name');
185
- const bytes = file.byteLength;
186
- const sha256 = text(file, 'contentHash');
187
- if (name === undefined || typeof bytes !== 'number' || sha256 === undefined)
188
- return null;
189
- const mediaType = text(file, 'mediaType');
190
- const width = file.width;
191
- const height = file.height;
192
- return {
193
- name,
194
- media_type: mediaType !== undefined && mediaType.length > 0 ? mediaType : OCTET_STREAM,
195
- bytes,
196
- sha256,
197
- ...(typeof width === 'number' && typeof height === 'number'
198
- ? { dimensions: { width, height } }
199
- : {}),
200
- };
201
- }
202
- function sendFiles(input) {
203
- const files = record(input).files;
204
- if (files === undefined)
205
- return [];
206
- if (!Array.isArray(files))
207
- return null;
208
- const declared = [];
209
- for (const entry of files) {
210
- const file = sendFile(entry);
211
- if (file === null)
212
- return null;
213
- declared.push(file);
214
- }
215
- return declared;
216
- }
217
- function sendBody(input) {
218
- const files = sendFiles(input);
219
- const operation = text(input, 'operation_id');
220
- if (files === null || operation === undefined)
221
- return null;
222
- const textValue = text(input, 'text');
223
- return {
224
- conversation_id: text(input, 'conversation'),
225
- operation_id: operation,
226
- ...(textValue !== undefined && textValue.length > 0 ? { text: textValue } : {}),
227
- ...(files.length === 0 ? {} : { files }),
228
- };
229
- }
230
- async function fileBytes(file) {
231
- const blob = record(file).blob;
232
- if (blob === undefined || typeof blob !== 'object' || blob === null)
233
- return null;
234
- if (!('arrayBuffer' in blob) || typeof blob.arrayBuffer !== 'function')
235
- return null;
236
- return new Uint8Array(await blob.arrayBuffer());
237
- }
238
- function uploadGrant(entry) {
239
- if (!('upload' in record(entry)))
240
- return 'absent';
241
- const upload = record(record(entry).upload);
242
- if (upload.method !== 'POST')
243
- return null;
244
- if (typeof upload.operation !== 'string' || typeof upload.url !== 'string')
245
- return null;
246
- return { method: 'POST', operation: upload.operation, url: upload.url };
247
- }
248
- async function uploadFiles(transport, input, result) {
249
- const sent = record(result).files;
250
- if (!Array.isArray(sent))
251
- return true;
252
- const files = Array.isArray(record(input).files) ? record(input).files : [];
253
- for (const [index, entry] of sent.entries()) {
254
- const grant = uploadGrant(entry);
255
- if (grant === 'absent')
256
- continue;
257
- const bytes = index < files.length ? await fileBytes(files[index]) : null;
258
- if (grant === null || bytes === null)
259
- return false;
260
- const answer = await transport.transfer({
261
- act: 'conversation_file_upload',
262
- url: grant.url,
263
- body: bytes,
264
- operation: grant.operation,
265
- });
266
- if (answer.status !== 200)
267
- return false;
268
- }
269
- return true;
270
- }
271
- async function sendAction(transport, route, input, name) {
272
- const body = sendBody(input);
273
- if (body === null)
274
- return malformed();
275
- const outcome = actionOutcome(await transport.door(route, body), name);
276
- if (outcome.kind !== 'action')
277
- return outcome;
278
- if (!(await uploadFiles(transport, input, outcome.result))) {
279
- return { kind: 'transport-uncertainty' };
280
- }
281
- return { ...outcome, result: withTurn(outcome.result) };
282
- }
283
- function historyCursor(value) {
284
- const acceptedAt = text(value, 'accepted_at');
285
- const messageId = text(value, 'message_id');
286
- if (acceptedAt === undefined || messageId === undefined)
287
- return null;
288
- return { accepted_at: acceptedAt, message_id: messageId };
289
- }
290
- function position(cursor) {
291
- return JSON.stringify({ accepted_at: cursor.accepted_at, message_id: cursor.message_id });
292
- }
293
- function positionCursor(value) {
294
- try {
295
- return historyCursor(JSON.parse(value));
296
- }
297
- catch {
298
- return null;
299
- }
300
- }
301
- export function acceptedMessage(entry) {
302
- const cursor = historyCursor(entry);
303
- if (cursor === null)
304
- return null;
305
- const message = structuredClone(record(entry));
306
- const referrer = { message: cursor.message_id };
307
- return {
308
- identity: cursor.message_id,
309
- turn: text(entry, 'turn_id') ?? cursor.message_id,
310
- position: position(cursor),
311
- text: text(entry, 'text') ?? '',
312
- message: Array.isArray(message.files)
313
- ? {
314
- ...message,
315
- files: message.files.map((file) => ({ ...record(file), referrer })),
316
- }
317
- : message,
318
- };
319
- }
320
- function historyPage(entry) {
321
- const held = record(entry);
322
- const conversation = text(held, 'conversation_id');
323
- if (conversation === undefined || !Array.isArray(held.messages))
324
- return null;
325
- const messages = [];
326
- for (const one of held.messages) {
327
- const message = acceptedMessage(one);
328
- if (message === null)
329
- return null;
330
- messages.push(message);
331
- }
332
- const next = held.next === null || held.next === undefined ? undefined : historyCursor(held.next);
333
- if (next === null)
334
- return null;
335
- return { conversation, messages, ...(next === undefined ? {} : { next: position(next) }) };
336
- }
337
- function historyAsks(input) {
338
- const asked = record(input).conversations;
339
- if (!Array.isArray(asked) || asked.length === 0)
340
- return null;
341
- const asks = [];
342
- for (const entry of asked) {
343
- const conversationId = text(entry, 'conversation');
344
- if (conversationId === undefined)
345
- return null;
346
- const afterValue = text(entry, 'after');
347
- const after = afterValue === undefined ? undefined : positionCursor(afterValue);
348
- if (after === null)
349
- return null;
350
- asks.push({ conversation_id: conversationId, ...(after === undefined ? {} : { after }) });
351
- }
352
- return asks;
353
- }
354
- async function historyAction(transport, route, input, name) {
355
- const asks = historyAsks(input);
356
- if (asks === null)
357
- return malformed();
358
- const histories = [];
359
- for (let at = 0; at < asks.length; at += HISTORY_CONVERSATIONS) {
360
- const answer = await transport.door(route, {
361
- conversations: asks.slice(at, at + HISTORY_CONVERSATIONS),
362
- limit: HISTORY_PAGE,
363
- });
364
- const outcome = actionOutcome(answer, name);
365
- if (outcome.kind !== 'action')
366
- return outcome;
367
- const answered = record(outcome.result).histories;
368
- if (!Array.isArray(answered))
369
- return { kind: 'transport-uncertainty' };
370
- for (const entry of answered) {
371
- const page = historyPage(entry);
372
- if (page === null)
373
- return { kind: 'transport-uncertainty' };
374
- histories.push(page);
375
- }
376
- }
377
- const result = { histories };
378
- return { kind: 'action', result };
47
+ return actionOutcome(await transport.door(route, input), name, input);
379
48
  }
@@ -1 +1 @@
1
- {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAKV,OAAO,EACP,aAAa,EAkBd,MAAM,wBAAwB,CAAC;AA4ChC,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,CAAC;IAAC,QAAQ,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC;AAsErD,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,yBAAyB,GAAG,OAAO,CAyiBpF"}
1
+ {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAIV,OAAO,EAIP,aAAa,EAed,MAAM,wBAAwB,CAAC;AAyBhC,OAAO,EAGL,KAAK,kBAAkB,EACvB,KAAK,cAAc,EACpB,MAAM,gBAAgB,CAAC;AAoBxB,MAAM,MAAM,yBAAyB,GACjC;IACE,SAAS,EAAE,cAAc,CAAC;IAC1B,SAAS,IAAI,OAAO,CAAC,aAAa,CAAC,CAAC;CACrC,GACD;IAAE,IAAI,EAAE,kBAAkB,CAAC;IAAC,QAAQ,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC;AAoBrD,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,yBAAyB,GAAG,OAAO,CAgbpF"}