@agentshouse/mdruntime-adapter 0.1.0-cc63dd043d0e2 → 0.1.0-cc710a8825617

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 (51) hide show
  1. package/AGENTS.md +270 -183
  2. package/README.md +3 -3
  3. package/dist/actions.d.ts +63 -2
  4. package/dist/actions.d.ts.map +1 -1
  5. package/dist/actions.js +154 -138
  6. package/dist/adapter.d.ts +1 -0
  7. package/dist/adapter.d.ts.map +1 -1
  8. package/dist/adapter.js +325 -166
  9. package/dist/discovery.d.ts +6 -12
  10. package/dist/discovery.d.ts.map +1 -1
  11. package/dist/discovery.js +32 -36
  12. package/dist/gather.d.ts +1 -0
  13. package/dist/gather.d.ts.map +1 -1
  14. package/dist/gather.js +11 -6
  15. package/dist/handed.d.ts +15 -0
  16. package/dist/handed.d.ts.map +1 -0
  17. package/dist/handed.js +38 -0
  18. package/dist/index.d.ts +2 -2
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +1 -1
  21. package/dist/model.d.ts +7 -18
  22. package/dist/model.d.ts.map +1 -1
  23. package/dist/model.js +60 -39
  24. package/dist/read.d.ts +35 -7
  25. package/dist/read.d.ts.map +1 -1
  26. package/dist/read.js +164 -86
  27. package/dist/referrer.d.ts +7 -0
  28. package/dist/referrer.d.ts.map +1 -0
  29. package/dist/referrer.js +35 -0
  30. package/dist/refusal.d.ts +5 -9
  31. package/dist/refusal.d.ts.map +1 -1
  32. package/dist/refusal.js +31 -17
  33. package/dist/site.d.ts +1 -1
  34. package/dist/site.d.ts.map +1 -1
  35. package/dist/site.js +191 -107
  36. package/dist/stream.d.ts +10 -4
  37. package/dist/stream.d.ts.map +1 -1
  38. package/dist/stream.js +391 -47
  39. package/dist/transport.d.ts +79 -11
  40. package/dist/transport.d.ts.map +1 -1
  41. package/dist/transport.js +3 -1
  42. package/dist/version.d.ts +2 -0
  43. package/dist/version.d.ts.map +1 -1
  44. package/dist/version.js +10 -1
  45. package/dist/write.d.ts +2 -1
  46. package/dist/write.d.ts.map +1 -1
  47. package/dist/write.js +96 -14
  48. package/package.json +3 -4
  49. package/dist/links.d.ts +0 -12
  50. package/dist/links.d.ts.map +0 -1
  51. package/dist/links.js +0 -66
package/dist/actions.d.ts CHANGED
@@ -1,8 +1,69 @@
1
- import { type AcceptedMessage, type ActionDeclaration, type ActionRequest, type ActionResult, type ConversationCards } from '@agentshouse/mdruntime';
1
+ import { type AcceptedMessage, type ActionDeclaration, type ActionRequest, type ActionResult, type ConversationAgents, type ConversationHistories, type ConversationHistoryRequest, type FileDeclaration } from '@agentshouse/mdruntime';
2
2
  import type { HouseDoorAnswer, HouseTransport } from './transport.js';
3
- export declare const HOUSE_CONVERSATION_CARDS: ConversationCards;
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;
4
64
  export declare function declaredActions(routes: readonly string[]): readonly ActionDeclaration[];
5
65
  export declare function houseAction(transport: HouseTransport, request: ActionRequest): Promise<ActionResult>;
6
66
  export declare function actionOutcome(answer: HouseDoorAnswer, name: string): ActionResult;
7
67
  export declare function acceptedMessage(entry: unknown): AcceptedMessage | null;
68
+ export {};
8
69
  //# 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,iBAAiB,EAGvB,MAAM,wBAAwB,CAAC;AAGhC,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAkBtE,eAAO,MAAM,wBAAwB,EAAE,iBAKtC,CAAC;AAEF,wBAAgB,eAAe,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,GAAG,SAAS,iBAAiB,EAAE,CASvF;AAED,wBAAgB,WAAW,CACzB,SAAS,EAAE,cAAc,EACzB,OAAO,EAAE,aAAa,GACrB,OAAO,CAAC,YAAY,CAAC,CAQvB;AA4DD,wBAAgB,aAAa,CAAC,MAAM,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM,GAAG,YAAY,CAWjF;AAuLD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M,iBAAiB,EAAE,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC,CAAC,CAKhF,CAAC;AAMJ,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,MAAM,GAAG,kBAAkB,CAE3E;AAED,wBAAgB,eAAe,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,GAAG,SAAS,iBAAiB,EAAE,CAMvF;AAED,wBAAgB,WAAW,CACzB,SAAS,EAAE,cAAc,EACzB,OAAO,EAAE,aAAa,GACrB,OAAO,CAAC,YAAY,CAAC,CAQvB;AA6ED,wBAAgB,aAAa,CAAC,MAAM,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM,GAAG,YAAY,CAOjF;AAuKD,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,eAAe,GAAG,IAAI,CAiBtE"}
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
- 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_CARDS = {
17
- locator: PRIVATE_LOCATOR,
18
- type: 'house-conversation',
19
- identity: 'conversation',
20
- agent: 'agent',
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',
21
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: mapped.get(route) ?? route,
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 === '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);
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,86 @@ 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 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,
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 },
87
130
  };
88
- }
89
- return input;
90
131
  }
91
132
  function withTurn(body) {
92
133
  const held = record(body);
93
134
  const turn = text(held, 'message_id');
94
- return turn === undefined ? body : { ...held, turn };
135
+ return turn === undefined
136
+ ? body
137
+ : { ...held, turn };
95
138
  }
96
139
  export function actionOutcome(answer, name) {
97
140
  if (answer.status === 401)
98
141
  return { kind: 'revoked-authorization' };
99
142
  const refusal = doorRefusal(answer.body);
100
- if (refusal !== null) {
101
- return { kind: 'rejected', code: refusal.code, fields: refusal.fields };
102
- }
143
+ if (refusal !== null)
144
+ return { kind: 'rejected', ...refusal };
103
145
  if (answer.status === 404)
104
- return { kind: 'rejected', code: 'action-unimplemented', fields: { name } };
146
+ return unimplemented(name);
105
147
  if (answer.status === 0 || answer.status >= 400)
106
148
  return { kind: 'transport-uncertainty' };
107
- if (name === 'conversation.send')
108
- return { kind: 'action', result: withTurn(answer.body) };
109
149
  return { kind: 'action', result: answer.body };
110
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
+ }
111
177
  async function carry(transport, route, name, input) {
112
- const answer = await transport.door(route, bodyOf(name, input));
113
- return actionOutcome(answer, name);
178
+ return actionOutcome(await transport.door(route, input), name);
114
179
  }
115
180
  function sendFile(entry) {
116
181
  const file = record(entry);
@@ -149,12 +214,13 @@ function sendFiles(input) {
149
214
  }
150
215
  function sendBody(input) {
151
216
  const files = sendFiles(input);
152
- if (files === null)
217
+ const operation = text(input, 'operation_id');
218
+ if (files === null || operation === undefined)
153
219
  return null;
154
220
  const textValue = text(input, 'text');
155
221
  return {
156
222
  conversation_id: text(input, 'conversation'),
157
- operation_id: operationId(input),
223
+ operation_id: operation,
158
224
  ...(textValue !== undefined && textValue.length > 0 ? { text: textValue } : {}),
159
225
  ...(files.length === 0 ? {} : { files }),
160
226
  };
@@ -167,9 +233,6 @@ async function fileBytes(file) {
167
233
  return null;
168
234
  return new Uint8Array(await blob.arrayBuffer());
169
235
  }
170
- function delivered(answer, name) {
171
- return answer.status === 200 ? null : actionOutcome(answer, name);
172
- }
173
236
  function uploadGrant(entry) {
174
237
  if (!('upload' in record(entry)))
175
238
  return 'absent';
@@ -180,94 +243,40 @@ function uploadGrant(entry) {
180
243
  return null;
181
244
  return { method: 'POST', operation: upload.operation, url: upload.url };
182
245
  }
183
- async function uploadOriginals(transport, input, result) {
184
- const originals = record(result).originals;
185
- if (!Array.isArray(originals) || originals.length === 0)
186
- return null;
246
+ async function uploadFiles(transport, input, result) {
247
+ const sent = record(result).files;
248
+ if (!Array.isArray(sent))
249
+ return true;
187
250
  const files = Array.isArray(record(input).files) ? record(input).files : [];
188
- for (const [index, entry] of originals.entries()) {
251
+ for (const [index, entry] of sent.entries()) {
189
252
  const grant = uploadGrant(entry);
190
253
  if (grant === 'absent')
191
254
  continue;
192
- if (grant === null)
193
- return malformed();
194
255
  const bytes = index < files.length ? await fileBytes(files[index]) : null;
195
- if (bytes === null) {
196
- return { kind: 'rejected', code: 'conversation_original_descriptor_required', fields: {} };
197
- }
198
- const failed = delivered(await transport.transfer({
199
- act: 'conversation_original_upload',
200
- method: 'POST',
256
+ if (grant === null || bytes === null)
257
+ return false;
258
+ const answer = await transport.transfer({
259
+ act: 'conversation_file_upload',
201
260
  url: grant.url,
202
- answer: 'json',
203
261
  body: bytes,
204
262
  operation: grant.operation,
205
- }), 'conversation.send');
206
- if (failed !== null)
207
- return failed;
263
+ });
264
+ if (answer.status !== 200)
265
+ return false;
208
266
  }
209
- return null;
267
+ return true;
210
268
  }
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) {
269
+ async function sendAction(transport, route, input, name) {
260
270
  const body = sendBody(input);
261
271
  if (body === null)
262
272
  return malformed();
263
- const answer = await transport.door('/door/conversations/send', body);
264
- const outcome = actionOutcome(answer, 'conversation.send');
273
+ const outcome = actionOutcome(await transport.door(route, body), name);
265
274
  if (outcome.kind !== 'action')
266
275
  return outcome;
267
- const uploaded = await uploadOriginals(transport, input, outcome.result);
268
- if (uploaded !== null)
269
- return uploaded;
270
- return outcome;
276
+ if (!(await uploadFiles(transport, input, outcome.result))) {
277
+ return { kind: 'transport-uncertainty' };
278
+ }
279
+ return { ...outcome, result: withTurn(outcome.result) };
271
280
  }
272
281
  function historyCursor(value) {
273
282
  const acceptedAt = text(value, 'accepted_at');
@@ -291,12 +300,19 @@ export function acceptedMessage(entry) {
291
300
  const cursor = historyCursor(entry);
292
301
  if (cursor === null)
293
302
  return null;
303
+ const message = structuredClone(record(entry));
304
+ const referrer = { message: cursor.message_id };
294
305
  return {
295
306
  identity: cursor.message_id,
296
307
  turn: text(entry, 'turn_id') ?? cursor.message_id,
297
308
  position: position(cursor),
298
309
  text: text(entry, 'text') ?? '',
299
- message: structuredClone(entry),
310
+ message: Array.isArray(message.files)
311
+ ? {
312
+ ...message,
313
+ files: message.files.map((file) => ({ ...record(file), referrer })),
314
+ }
315
+ : message,
300
316
  };
301
317
  }
302
318
  function historyPage(entry) {
@@ -333,17 +349,17 @@ function historyAsks(input) {
333
349
  }
334
350
  return asks;
335
351
  }
336
- async function historyAction(transport, input) {
352
+ async function historyAction(transport, route, input, name) {
337
353
  const asks = historyAsks(input);
338
354
  if (asks === null)
339
355
  return malformed();
340
356
  const histories = [];
341
357
  for (let at = 0; at < asks.length; at += HISTORY_CONVERSATIONS) {
342
- const answer = await transport.door('/door/conversations/history', {
358
+ const answer = await transport.door(route, {
343
359
  conversations: asks.slice(at, at + HISTORY_CONVERSATIONS),
344
360
  limit: HISTORY_PAGE,
345
361
  });
346
- const outcome = actionOutcome(answer, 'conversation.history');
362
+ const outcome = actionOutcome(answer, name);
347
363
  if (outcome.kind !== 'action')
348
364
  return outcome;
349
365
  const answered = record(outcome.result).histories;
package/dist/adapter.d.ts CHANGED
@@ -5,6 +5,7 @@ export type AgentsHouseAdapterOptions = {
5
5
  authorize(): Promise<Authorization>;
6
6
  } | {
7
7
  site: HouseSiteTransport;
8
+ snapshot?: boolean;
8
9
  };
9
10
  export declare function createAgentsHouseAdapter(options: AgentsHouseAdapterOptions): Adapter;
10
11
  //# sourceMappingURL=adapter.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,OAAO,EACP,aAAa,EAad,MAAM,wBAAwB,CAAC;AAyChC,OAAO,EAQL,KAAK,kBAAkB,EACvB,KAAK,cAAc,EACpB,MAAM,gBAAgB,CAAC;AAuBxB,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;AAiCjC,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,yBAAyB,GAAG,OAAO,CA2ZpF"}
1
+ {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAKV,OAAO,EACP,aAAa,EAiBd,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,CA0hBpF"}