@agentshouse/mdruntime-adapter 0.1.0-alpha.9 → 0.1.0-c023d3564cb7c

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 (69) hide show
  1. package/AGENTS.md +312 -74
  2. package/README.md +32 -0
  3. package/dist/actions.d.ts +57 -13
  4. package/dist/actions.d.ts.map +1 -1
  5. package/dist/actions.js +274 -397
  6. package/dist/adapter.d.ts +1 -0
  7. package/dist/adapter.d.ts.map +1 -1
  8. package/dist/adapter.js +525 -216
  9. package/dist/digest.js +1 -5
  10. package/dist/discovery.d.ts +27 -36
  11. package/dist/discovery.d.ts.map +1 -1
  12. package/dist/discovery.js +173 -228
  13. package/dist/gather.d.ts +4 -0
  14. package/dist/gather.d.ts.map +1 -0
  15. package/dist/gather.js +35 -0
  16. package/dist/index.d.ts +4 -2
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +3 -3
  19. package/dist/map.d.ts +2 -2
  20. package/dist/map.d.ts.map +1 -1
  21. package/dist/map.js +6 -7
  22. package/dist/model.d.ts +7 -18
  23. package/dist/model.d.ts.map +1 -1
  24. package/dist/model.js +60 -57
  25. package/dist/part.d.ts +15 -0
  26. package/dist/part.d.ts.map +1 -0
  27. package/dist/part.js +85 -0
  28. package/dist/read.d.ts +93 -35
  29. package/dist/read.d.ts.map +1 -1
  30. package/dist/read.js +415 -245
  31. package/dist/refusal.d.ts +5 -7
  32. package/dist/refusal.d.ts.map +1 -1
  33. package/dist/refusal.js +47 -68
  34. package/dist/room-types.d.ts +6 -0
  35. package/dist/room-types.d.ts.map +1 -0
  36. package/dist/room-types.js +66 -0
  37. package/dist/site.d.ts +1 -1
  38. package/dist/site.d.ts.map +1 -1
  39. package/dist/site.js +219 -161
  40. package/dist/stream.d.ts +18 -6
  41. package/dist/stream.d.ts.map +1 -1
  42. package/dist/stream.js +556 -214
  43. package/dist/transport.d.ts +131 -38
  44. package/dist/transport.d.ts.map +1 -1
  45. package/dist/transport.js +4 -3
  46. package/dist/upload.d.ts +12 -0
  47. package/dist/upload.d.ts.map +1 -0
  48. package/dist/upload.js +52 -0
  49. package/dist/version.d.ts +3 -0
  50. package/dist/version.d.ts.map +1 -1
  51. package/dist/version.js +32 -18
  52. package/dist/write.d.ts +18 -8
  53. package/dist/write.d.ts.map +1 -1
  54. package/dist/write.js +259 -228
  55. package/package.json +4 -13
  56. package/dist/actions.js.map +0 -1
  57. package/dist/adapter.js.map +0 -1
  58. package/dist/digest.js.map +0 -1
  59. package/dist/discovery.js.map +0 -1
  60. package/dist/index.js.map +0 -1
  61. package/dist/map.js.map +0 -1
  62. package/dist/model.js.map +0 -1
  63. package/dist/read.js.map +0 -1
  64. package/dist/refusal.js.map +0 -1
  65. package/dist/site.js.map +0 -1
  66. package/dist/stream.js.map +0 -1
  67. package/dist/transport.js.map +0 -1
  68. package/dist/version.js.map +0 -1
  69. package/dist/write.js.map +0 -1
package/dist/actions.js CHANGED
@@ -1,121 +1,97 @@
1
- import { VERSION } from '@agentshouse/mdmodel';
2
- import { changeBatchIdentity } from '@agentshouse/mdruntime';
1
+ import { changeBatchIdentity, } from '@agentshouse/mdruntime';
3
2
  import { doorRefusal } from './refusal.js';
4
- import { PRIVATE_AUTHORITY } from './transport.js';
5
- export const CONVERSATION_ACTIONS = [
6
- 'conversation.create',
7
- 'conversation.send',
8
- 'conversation.answer',
9
- 'conversation.interrupt',
10
- 'conversation.kill',
11
- 'conversation.cancel',
12
- 'conversation.delete',
13
- 'conversation.history'
14
- ];
15
- export const HOUSE_ACTS = [
16
- 'conversations/interaction/read',
17
- 'conversations/originals/read',
18
- 'apps/read',
19
- 'apps/customize',
20
- 'rooms/read',
21
- 'rooms/access/read',
22
- 'rooms/access/invitations',
23
- 'rooms/access/entries',
24
- 'rooms/access/invite',
25
- 'rooms/access/invitation-cancel',
26
- 'rooms/access/revoke',
27
- 'rooms/access/leave',
28
- 'rooms/roles/define',
29
- 'rooms/roles/remove',
30
- 'rooms/roles/assign',
31
- 'rooms/roles/unassign',
32
- 'rooms/settings/processing/read',
33
- 'rooms/settings/processing/write',
34
- 'profiles/create',
35
- 'profiles/read',
36
- 'profiles/edit',
37
- 'profiles/duplicate',
38
- 'profiles/delegate',
39
- 'agents/create',
40
- 'agents/read',
41
- 'routines/requests',
42
- 'routines/access',
43
- 'routines/owned',
44
- 'routines/granted',
45
- 'routines/grant',
46
- 'routines/send',
47
- 'routines/cancel',
48
- 'routines/start',
49
- 'routines/schedules',
50
- 'routines/schedules/create',
51
- 'routines/schedules/enabled',
52
- 'account/security/read',
53
- 'storage/inventory',
54
- 'storage/usage',
55
- 'usage/statement',
56
- 'storage/blob-store',
57
- 'storage/blob-store/select',
58
- 'storage/connections',
59
- 'storage/archive',
60
- 'storage/archive/status',
61
- 'environments/read',
62
- 'environments/create',
63
- 'environments/start',
64
- 'environments/stop',
65
- 'environments/delete',
66
- 'environments/renewals/read',
67
- 'environments/work/read',
68
- 'nodes/read'
69
- ];
70
- export const HOUSE_PROVIDER_ACTIONS = [
71
- ...CONVERSATION_ACTIONS,
72
- ...HOUSE_ACTS
73
- ];
74
- const DECLARED = new Set(HOUSE_PROVIDER_ACTIONS);
75
- export const HISTORY_PAGE = 100;
3
+ import { finishedUpload, houseUpload } from './upload.js';
4
+ const HISTORY_PAGE = 100;
5
+ const HISTORY_CONVERSATIONS = 64;
6
+ const TRIGGER_ROOMS = 100;
76
7
  const OCTET_STREAM = 'application/octet-stream';
77
- const CONVERSATION_ROUTES = {
78
- 'conversation.create': '/door/conversations/create',
79
- 'conversation.send': '/door/conversations/send',
80
- 'conversation.answer': '/door/conversations/interaction/answer',
81
- 'conversation.interrupt': '/door/conversations/interrupt',
82
- 'conversation.kill': '/door/conversations/kill',
83
- 'conversation.cancel': '/door/conversations/cancel',
84
- 'conversation.delete': '/door/conversations/delete',
85
- 'conversation.history': '/door/conversations/history',
86
- 'conversations/interaction/read': '/door/conversations/interaction/read',
87
- 'conversations/originals/read': '/door/conversations/originals/read'
8
+ function mapped(door, input = (given) => given) {
9
+ return {
10
+ door,
11
+ async carry(transport, given, name) {
12
+ return actionOutcome(await transport.door(`/door/${door}`, input(given)), name);
13
+ },
14
+ };
15
+ }
16
+ function carried(door, carry, batched, room) {
17
+ return {
18
+ door,
19
+ ...(batched === undefined ? {} : { batched }),
20
+ ...(room === undefined ? {} : { room }),
21
+ carry: (transport, input, name) => carry(transport, `/door/${door}`, input, name),
22
+ };
23
+ }
24
+ const APP_ACTS = {
25
+ 'access.read': mapped('rooms/access/read'),
26
+ 'conversation.answer': mapped('conversations/interaction/answer', answerBody),
27
+ 'conversation.attach': carried('conversations/attach', attachAction, undefined, 'room'),
28
+ 'conversation.create': carried('conversations/create', createAction),
29
+ 'conversation.delete': mapped('conversations/delete', controlBody),
30
+ 'conversation.history': carried('conversations/history', historyAction, ['conversations']),
31
+ 'conversation.interrupt': mapped('conversations/interrupt', controlBody),
32
+ 'conversation.kill': mapped('conversations/kill', controlBody),
33
+ 'conversation.markDone': mapped('conversations/done'),
34
+ 'conversation.rename': mapped('conversations/rename'),
35
+ 'conversation.retry': mapped('conversations/retry'),
36
+ 'conversation.send': carried('conversations/send', sendAction),
37
+ 'conversation.setOption': mapped('conversations/option'),
38
+ 'feature.list': mapped('apps/features/read'),
39
+ 'holder.list': mapped('apps/holders'),
40
+ 'holder.ring': mapped('apps/ring'),
41
+ 'inboxChat.assign': mapped('chats/assign'),
42
+ 'inboxChat.return': mapped('chats/return'),
43
+ 'inboxChat.takeControl': mapped('chats/take-control'),
44
+ 'member.list': mapped('rooms/members'),
45
+ 'message.send': mapped('apps/messages/send'),
46
+ 'processing.read': mapped('rooms/settings/processing/read'),
47
+ 'record.list': mapped('records/read'),
48
+ 'relay.read': mapped('apps/relay'),
49
+ 'routine.list': mapped('routines/access'),
50
+ 'routineAccess.list': mapped('routines/granted'),
51
+ 'routineAllowance.list': mapped('routines/allowance'),
52
+ 'routineRequest.list': mapped('routines/requests'),
53
+ 'storage.read': mapped('storage/inventory'),
54
+ 'trigger.list': carried('triggers/read', triggersAction, ['room_refs'], 'room_refs'),
55
+ 'triggerOccurrence.list': mapped('triggers/fired'),
88
56
  };
89
- export const HOUSE_CONVERSATION_CARDS = {
90
- locator: PRIVATE_AUTHORITY,
91
- type: 'conversation',
92
- identity: 'conversation',
93
- agent: 'agent'
57
+ export const HOUSE_APP_ACTS = Object.fromEntries(Object.entries(APP_ACTS).map(([name, act]) => [name, act.door]));
58
+ export const SITE_SUBMIT = 'submit';
59
+ export const HOUSE_SITE_ACTS = {
60
+ [SITE_SUBMIT]: 'site/submission',
94
61
  };
95
- export function declaredActions() {
96
- return HOUSE_PROVIDER_ACTIONS.map((name)=>({
97
- name,
98
- inputShape: {},
99
- resultKind: 'house'
100
- }));
62
+ export const HOUSE_APP_BATCHES = Object.fromEntries(Object.entries(APP_ACTS).flatMap(([name, act]) => act.batched === undefined ? [] : [[name, act.batched]]));
63
+ const APP_NAMES = new Map(Object.entries(APP_ACTS).map(([name, act]) => [act.door, name]));
64
+ const APP_ROOMS = new Map(Object.entries(APP_ACTS).flatMap(([name, act]) => act.room === undefined ? [] : [[name, act.room]]));
65
+ export function houseConversationAgents(locator) {
66
+ return { locator, type: 'house-agent', identity: 'agent', name: 'name', retired: 'retired' };
67
+ }
68
+ export function declaredActions(routes) {
69
+ return routes.map((route) => {
70
+ const name = APP_NAMES.get(route) ?? route;
71
+ return { name, inputShape: {}, resultKind: 'house', room: APP_ROOMS.get(name) ?? 'room_ref' };
72
+ });
101
73
  }
102
74
  export function houseAction(transport, request) {
103
- if (!DECLARED.has(request.name)) {
104
- return Promise.resolve({
105
- kind: 'rejected',
106
- code: 'action-unimplemented',
107
- fields: {
108
- name: request.name,
109
- mdModel: VERSION
110
- }
111
- });
75
+ if (Object.hasOwn(APP_ACTS, request.name)) {
76
+ return APP_ACTS[request.name].carry(transport, request.input, request.name);
112
77
  }
113
- if (request.name === 'conversation.send') return sendAction(transport, request.input);
114
- if (request.name === 'conversation.history') return historyAction(transport, request.input);
115
- return carry(transport, request.name, request.input);
78
+ if (APP_NAMES.has(request.name))
79
+ return Promise.resolve(unimplemented(request.name));
80
+ const route = routeOf(request.name);
81
+ if (route === null)
82
+ return Promise.resolve(malformed());
83
+ return carry(transport, route, request.name, request.input);
116
84
  }
117
85
  function routeOf(name) {
118
- return CONVERSATION_ROUTES[name] ?? `/door/${name}`;
86
+ const parts = name.split('/');
87
+ if (parts.some((part) => part === '.' || part === '..'))
88
+ return null;
89
+ try {
90
+ return `/door/${parts.map(encodeURIComponent).join('/')}`;
91
+ }
92
+ catch {
93
+ return null;
94
+ }
119
95
  }
120
96
  function record(value) {
121
97
  return typeof value === 'object' && value !== null ? value : {};
@@ -128,327 +104,228 @@ function operationId(input) {
128
104
  return text(input, 'operation_id') ?? changeBatchIdentity();
129
105
  }
130
106
  function malformed() {
107
+ return { kind: 'rejected', code: 'invalid_request', fields: {}, retryable: false };
108
+ }
109
+ function unimplemented(name) {
110
+ return { kind: 'rejected', code: 'action-unimplemented', fields: { name }, retryable: false };
111
+ }
112
+ function createBody(input) {
113
+ const replaces = text(input, 'replaces');
131
114
  return {
132
- kind: 'rejected',
133
- code: 'invalid_request',
134
- fields: {}
115
+ agent_id: text(input, 'agent_id'),
116
+ operation_id: operationId(input),
117
+ ...(replaces === undefined ? {} : { replaces }),
135
118
  };
136
119
  }
137
- function bodyOf(name, input) {
138
- if (name === 'conversation.create') {
139
- return {
140
- agent_id: text(input, 'agent_id'),
141
- operation_id: operationId(input)
142
- };
143
- }
144
- if (name === 'conversation.interrupt' || name === 'conversation.kill' || name === 'conversation.delete') {
145
- return {
146
- conversation_id: text(input, 'conversation'),
147
- operation_id: operationId(input)
148
- };
149
- }
150
- if (name === 'conversation.cancel') {
151
- return {
152
- conversation_id: text(input, 'conversation'),
153
- message_id: text(input, 'turn'),
154
- operation_id: operationId(input)
155
- };
156
- }
157
- if (name === 'conversation.answer') {
158
- const nested = record(record(input).answer);
159
- return {
160
- conversation_id: text(input, 'conversation'),
161
- operation_id: operationId(input),
162
- interaction_id: text(nested, 'interaction_id'),
163
- revision: nested.revision,
164
- response: nested.response
120
+ function controlBody(input) {
121
+ return { conversation_id: text(input, 'conversation'), operation_id: operationId(input) };
122
+ }
123
+ function answerBody(input) {
124
+ const nested = record(record(input).answer);
125
+ return {
126
+ conversation_id: text(input, 'conversation'),
127
+ operation_id: operationId(input),
128
+ interaction_id: text(nested, 'interaction_id'),
129
+ response: nested.response,
130
+ };
131
+ }
132
+ async function createAction(transport, route, input, name) {
133
+ const outcome = actionOutcome(await transport.door(route, createBody(input)), name);
134
+ if (outcome.kind !== 'action')
135
+ return outcome;
136
+ const held = record(outcome.result);
137
+ const conversation = text(held, 'conversation_id');
138
+ return conversation === undefined
139
+ ? outcome
140
+ : {
141
+ ...outcome,
142
+ result: { ...held, conversation },
165
143
  };
166
- }
167
- return input;
168
144
  }
169
145
  function withTurn(body) {
170
146
  const held = record(body);
171
147
  const turn = text(held, 'message_id');
172
- return turn === undefined ? body : {
173
- ...held,
174
- turn
175
- };
148
+ return turn === undefined
149
+ ? body
150
+ : { ...held, turn };
176
151
  }
177
152
  export function actionOutcome(answer, name) {
178
- if (answer.status === 401) return {
179
- kind: 'revoked-authorization'
180
- };
153
+ if (answer.status === 401)
154
+ return { kind: 'revoked-authorization' };
181
155
  const refusal = doorRefusal(answer.body);
182
- if (refusal !== null) {
183
- return {
184
- kind: 'rejected',
185
- code: refusal.code,
186
- fields: refusal.fields
187
- };
188
- }
189
- if (answer.status === 0 || answer.status >= 400) return {
190
- kind: 'transport-uncertainty'
191
- };
192
- if (name === 'conversation.send') return {
193
- kind: 'action',
194
- result: withTurn(answer.body)
195
- };
196
- return {
197
- kind: 'action',
198
- result: answer.body
199
- };
156
+ if (refusal?.code === 'operation_contended')
157
+ return { kind: 'transport-uncertainty' };
158
+ if (refusal !== null)
159
+ return { kind: 'rejected', ...refusal };
160
+ if (answer.status === 404)
161
+ return unimplemented(name);
162
+ if (answer.status === 0 || answer.status >= 400)
163
+ return { kind: 'transport-uncertainty' };
164
+ return { kind: 'action', result: answer.body };
165
+ }
166
+ async function carry(transport, route, name, input) {
167
+ return actionOutcome(await transport.door(route, input), name);
168
+ }
169
+ function sendBody(input) {
170
+ const operation = text(input, 'operation_id');
171
+ const said = text(input, 'text');
172
+ if (operation === undefined || said === undefined)
173
+ return null;
174
+ return { conversation_id: text(input, 'conversation'), operation_id: operation, text: said };
200
175
  }
201
- async function carry(transport, name, input) {
202
- const answer = await transport.door(routeOf(name), bodyOf(name, input));
203
- return actionOutcome(answer, name);
176
+ async function sendAction(transport, route, input, name) {
177
+ const body = sendBody(input);
178
+ if (body === null)
179
+ return malformed();
180
+ const outcome = actionOutcome(await transport.door(route, body), name);
181
+ if (outcome.kind !== 'action')
182
+ return outcome;
183
+ return { ...outcome, result: withTurn(outcome.result) };
204
184
  }
205
- function sendFile(entry) {
206
- const file = record(entry);
207
- const name = text(file, 'name');
208
- const bytes = file.byteLength;
209
- const sha256 = text(file, 'contentHash');
210
- if (name === undefined || typeof bytes !== 'number' || sha256 === undefined) return null;
211
- const mediaType = text(file, 'mediaType');
212
- const width = file.width;
213
- const height = file.height;
185
+ function attachBody(input) {
186
+ const room = text(input, 'room');
187
+ const name = text(input, 'name');
188
+ const sha256 = text(input, 'contentHash');
189
+ const bytes = record(input).byteLength;
190
+ if (room === undefined || name === undefined || sha256 === undefined)
191
+ return null;
192
+ if (typeof bytes !== 'number')
193
+ return null;
194
+ const mediaType = text(input, 'mediaType');
214
195
  return {
196
+ room_ref: room,
215
197
  name,
216
- media_type: mediaType !== undefined && mediaType.length > 0 ? mediaType : OCTET_STREAM,
217
198
  bytes,
218
199
  sha256,
219
- ...typeof width === 'number' && typeof height === 'number' ? {
220
- dimensions: {
221
- width,
222
- height
223
- }
224
- } : {}
225
- };
226
- }
227
- function sendFiles(input) {
228
- const files = record(input).files;
229
- if (files === undefined) return [];
230
- if (!Array.isArray(files)) return null;
231
- const declared = [];
232
- for (const entry of files){
233
- const file = sendFile(entry);
234
- if (file === null) return null;
235
- declared.push(file);
236
- }
237
- return declared;
238
- }
239
- function sendBody(input) {
240
- const files = sendFiles(input);
241
- if (files === null) return null;
242
- const textValue = text(input, 'text');
243
- return {
244
- conversation_id: text(input, 'conversation'),
245
- operation_id: operationId(input),
246
- ...textValue !== undefined && textValue.length > 0 ? {
247
- text: textValue
248
- } : {},
249
- ...files.length === 0 ? {} : {
250
- files
251
- }
200
+ media_type: mediaType !== undefined && mediaType.length > 0 ? mediaType : OCTET_STREAM,
252
201
  };
253
202
  }
254
- async function fileBytes(file) {
255
- const blob = record(file).blob;
256
- if (blob === undefined || typeof blob !== 'object' || blob === null) return null;
257
- if (!('arrayBuffer' in blob) || typeof blob.arrayBuffer !== 'function') return null;
258
- return new Uint8Array(await blob.arrayBuffer());
259
- }
260
- function uploadGrant(entry) {
261
- if (!('upload' in record(entry))) return 'absent';
262
- const upload = record(record(entry).upload);
263
- if (upload.method !== 'POST') return null;
264
- if (typeof upload.operation !== 'string' || typeof upload.url !== 'string') return null;
265
- return {
266
- method: 'POST',
267
- operation: upload.operation,
268
- url: upload.url
269
- };
203
+ async function attachAction(transport, route, input, name) {
204
+ const body = attachBody(input);
205
+ const blob = record(input).blob;
206
+ if (body === null || !(blob instanceof Blob))
207
+ return malformed();
208
+ const outcome = actionOutcome(await transport.door(route, body), name);
209
+ if (outcome.kind !== 'action')
210
+ return outcome;
211
+ const upload = houseUpload(record(outcome.result).upload);
212
+ if (upload === null)
213
+ return { kind: 'transport-uncertainty' };
214
+ const finished = await finishedUpload(transport, upload, blob);
215
+ if (finished === null)
216
+ return { kind: 'transport-uncertainty' };
217
+ const settled = actionOutcome(finished, name);
218
+ if (settled.kind !== 'action')
219
+ return settled;
220
+ const path = text(settled.result, 'path');
221
+ if (path === undefined)
222
+ return { kind: 'transport-uncertainty' };
223
+ const result = { path };
224
+ return { kind: 'action', result };
270
225
  }
271
- async function uploadOriginals(transport, input, result) {
272
- const originals = record(result).originals;
273
- if (!Array.isArray(originals) || originals.length === 0) return null;
274
- const files = Array.isArray(record(input).files) ? record(input).files : [];
275
- for (const [index, entry] of originals.entries()){
276
- const grant = uploadGrant(entry);
277
- if (grant === 'absent') continue;
278
- if (grant === null) return malformed();
279
- const bytes = index < files.length ? await fileBytes(files[index]) : null;
280
- if (bytes === null) {
281
- return {
282
- kind: 'rejected',
283
- code: 'conversation_original_descriptor_required',
284
- fields: {}
285
- };
286
- }
287
- const delivered = await transport.transfer({
288
- act: 'conversation_original_upload',
289
- method: 'POST',
290
- url: grant.url,
291
- body: bytes,
292
- operation: grant.operation
293
- });
294
- if (delivered.status === 200) continue;
295
- return actionOutcome(delivered, 'conversation.send');
296
- }
297
- return null;
226
+ function historyCursor(value) {
227
+ const acceptedAt = text(value, 'accepted_at');
228
+ const messageId = text(value, 'message_id');
229
+ if (acceptedAt === undefined || messageId === undefined)
230
+ return null;
231
+ return { accepted_at: acceptedAt, message_id: messageId };
298
232
  }
299
- async function sendAction(transport, input) {
300
- const body = sendBody(input);
301
- if (body === null) return malformed();
302
- const answer = await transport.door('/door/conversations/send', body);
303
- const outcome = actionOutcome(answer, 'conversation.send');
304
- if (outcome.kind !== 'action') return outcome;
305
- const uploaded = await uploadOriginals(transport, input, outcome.result);
306
- if (uploaded !== null) return uploaded;
307
- return outcome;
233
+ function position(cursor) {
234
+ return JSON.stringify({ accepted_at: cursor.accepted_at, message_id: cursor.message_id });
308
235
  }
309
- function historyCursor(value) {
310
- let parsed;
236
+ function positionCursor(value) {
311
237
  try {
312
- parsed = JSON.parse(value);
313
- } catch {
238
+ return historyCursor(JSON.parse(value));
239
+ }
240
+ catch {
314
241
  return null;
315
242
  }
316
- const acceptedAt = text(parsed, 'accepted_at');
317
- const messageId = text(parsed, 'message_id');
318
- if (acceptedAt === undefined || messageId === undefined) return null;
319
- return {
320
- accepted_at: acceptedAt,
321
- message_id: messageId
322
- };
323
243
  }
324
- function turned(entry) {
325
- const turn = text(entry, 'message_id');
326
- if (turn === undefined) return null;
244
+ export function acceptedMessage(entry) {
245
+ const cursor = historyCursor(entry);
246
+ if (cursor === null)
247
+ return null;
327
248
  return {
328
- turn,
329
- message: structuredClone(entry)
249
+ identity: cursor.message_id,
250
+ turn: text(entry, 'turn_id') ?? cursor.message_id,
251
+ position: position(cursor),
252
+ text: text(entry, 'text') ?? '',
253
+ message: structuredClone(record(entry)),
330
254
  };
331
255
  }
332
- function olderCursor(entry) {
333
- const acceptedAt = text(entry.message, 'accepted_at');
334
- const messageId = text(entry.message, 'message_id');
335
- if (acceptedAt === undefined || messageId === undefined) return null;
336
- return JSON.stringify({
337
- accepted_at: acceptedAt,
338
- message_id: messageId
339
- });
340
- }
341
- export async function readConversationHistory(transport, conversationId) {
256
+ function historyPage(entry) {
257
+ const held = record(entry);
258
+ const conversation = text(held, 'conversation_id');
259
+ if (conversation === undefined || !Array.isArray(held.messages))
260
+ return null;
342
261
  const messages = [];
343
- let after;
344
- for(;;){
345
- const answer = await transport.door('/door/conversations/history', {
346
- conversation_id: conversationId,
347
- ...after === undefined ? {} : {
348
- after
349
- },
350
- limit: HISTORY_PAGE
351
- });
352
- const outcome = actionOutcome(answer, 'conversation.history');
353
- if (outcome.kind !== 'action') return {
354
- outcome
355
- };
356
- const held = record(outcome.result);
357
- if (!Array.isArray(held.messages)) return {
358
- outcome: {
359
- kind: 'transport-uncertainty'
360
- }
361
- };
362
- for (const entry of held.messages){
363
- if (text(entry, 'message_id') === undefined) {
364
- return {
365
- outcome: {
366
- kind: 'transport-uncertainty'
367
- }
368
- };
369
- }
370
- messages.push(entry);
371
- }
372
- if (held.next === null || held.next === undefined) return {
373
- messages
374
- };
375
- const next = record(held.next);
376
- const acceptedAt = text(next, 'accepted_at');
377
- const messageId = text(next, 'message_id');
378
- if (acceptedAt === undefined || messageId === undefined) {
379
- return {
380
- outcome: {
381
- kind: 'transport-uncertainty'
382
- }
383
- };
384
- }
385
- after = {
386
- accepted_at: acceptedAt,
387
- message_id: messageId
388
- };
262
+ for (const one of held.messages) {
263
+ const message = acceptedMessage(one);
264
+ if (message === null)
265
+ return null;
266
+ messages.push(message);
389
267
  }
268
+ const next = held.next === null || held.next === undefined ? undefined : historyCursor(held.next);
269
+ if (next === null)
270
+ return null;
271
+ return { conversation, messages, ...(next === undefined ? {} : { next: position(next) }) };
390
272
  }
391
- function historyPage(conversationId, messages, until) {
392
- const from = Math.max(0, until - HISTORY_PAGE);
393
- const page = messages.slice(from, until);
394
- const first = page[0];
395
- const cursor = from === 0 || first === undefined ? null : olderCursor(first);
396
- if (from > 0 && cursor === null) return {
397
- kind: 'transport-uncertainty'
398
- };
399
- return {
400
- kind: 'action',
401
- result: {
402
- conversation_id: conversationId,
403
- messages: page,
404
- ...cursor === null ? {} : {
405
- cursor
406
- }
407
- }
408
- };
409
- }
410
- async function historyAction(transport, input) {
411
- const conversationId = text(input, 'conversation');
412
- if (conversationId === undefined) return malformed();
413
- const cursorValue = text(input, 'cursor');
414
- let olderThan;
415
- if (cursorValue !== undefined) {
416
- const parsed = historyCursor(cursorValue);
417
- if (parsed === null) {
418
- return {
419
- kind: 'rejected',
420
- code: 'unknown-cursor',
421
- fields: {
422
- cursor: cursorValue
423
- }
424
- };
425
- }
426
- olderThan = parsed;
427
- }
428
- const held = await readConversationHistory(transport, conversationId);
429
- if ('outcome' in held) return held.outcome;
430
- const messages = [];
431
- for (const entry of held.messages){
432
- const mapped = turned(entry);
433
- if (mapped === null) return {
434
- kind: 'transport-uncertainty'
435
- };
436
- messages.push(mapped);
273
+ function historyAsks(input) {
274
+ const asked = record(input).conversations;
275
+ if (!Array.isArray(asked) || asked.length === 0)
276
+ return null;
277
+ const asks = [];
278
+ for (const entry of asked) {
279
+ const conversationId = text(entry, 'conversation');
280
+ if (conversationId === undefined)
281
+ return null;
282
+ const afterValue = text(entry, 'after');
283
+ const after = afterValue === undefined ? undefined : positionCursor(afterValue);
284
+ if (after === null)
285
+ return null;
286
+ asks.push({ conversation_id: conversationId, ...(after === undefined ? {} : { after }) });
437
287
  }
438
- if (olderThan === undefined) {
439
- return historyPage(conversationId, messages, messages.length);
288
+ return asks;
289
+ }
290
+ async function triggersAction(transport, route, input, name) {
291
+ const asked = record(input).room_refs;
292
+ if (!Array.isArray(asked) || asked.length === 0)
293
+ return malformed();
294
+ const rooms = [];
295
+ for (let at = 0; at < asked.length; at += TRIGGER_ROOMS) {
296
+ const outcome = actionOutcome(await transport.door(route, { room_refs: asked.slice(at, at + TRIGGER_ROOMS) }), name);
297
+ if (outcome.kind !== 'action')
298
+ return outcome;
299
+ const answered = record(outcome.result).rooms;
300
+ if (!Array.isArray(answered))
301
+ return { kind: 'transport-uncertainty' };
302
+ rooms.push(...answered);
440
303
  }
441
- const at = messages.findIndex((entry)=>text(entry.message, 'accepted_at') === olderThan.accepted_at && text(entry.message, 'message_id') === olderThan.message_id);
442
- if (at === -1) {
443
- return {
444
- kind: 'rejected',
445
- code: 'unknown-cursor',
446
- fields: {
447
- cursor: cursorValue
448
- }
449
- };
304
+ return { kind: 'action', result: { rooms } };
305
+ }
306
+ async function historyAction(transport, route, input, name) {
307
+ const asks = historyAsks(input);
308
+ if (asks === null)
309
+ return malformed();
310
+ const histories = [];
311
+ for (let at = 0; at < asks.length; at += HISTORY_CONVERSATIONS) {
312
+ const answer = await transport.door(route, {
313
+ conversations: asks.slice(at, at + HISTORY_CONVERSATIONS),
314
+ limit: HISTORY_PAGE,
315
+ });
316
+ const outcome = actionOutcome(answer, name);
317
+ if (outcome.kind !== 'action')
318
+ return outcome;
319
+ const answered = record(outcome.result).histories;
320
+ if (!Array.isArray(answered))
321
+ return { kind: 'transport-uncertainty' };
322
+ for (const entry of answered) {
323
+ const page = historyPage(entry);
324
+ if (page === null)
325
+ return { kind: 'transport-uncertainty' };
326
+ histories.push(page);
327
+ }
450
328
  }
451
- return historyPage(conversationId, messages, at);
329
+ const result = { histories };
330
+ return { kind: 'action', result };
452
331
  }
453
-
454
- //# sourceMappingURL=actions.js.map