@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.
- package/AGENTS.md +312 -74
- package/README.md +32 -0
- package/dist/actions.d.ts +57 -13
- package/dist/actions.d.ts.map +1 -1
- package/dist/actions.js +274 -397
- package/dist/adapter.d.ts +1 -0
- package/dist/adapter.d.ts.map +1 -1
- package/dist/adapter.js +525 -216
- package/dist/digest.js +1 -5
- package/dist/discovery.d.ts +27 -36
- package/dist/discovery.d.ts.map +1 -1
- package/dist/discovery.js +173 -228
- package/dist/gather.d.ts +4 -0
- package/dist/gather.d.ts.map +1 -0
- package/dist/gather.js +35 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -3
- package/dist/map.d.ts +2 -2
- package/dist/map.d.ts.map +1 -1
- package/dist/map.js +6 -7
- package/dist/model.d.ts +7 -18
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +60 -57
- package/dist/part.d.ts +15 -0
- package/dist/part.d.ts.map +1 -0
- package/dist/part.js +85 -0
- package/dist/read.d.ts +93 -35
- package/dist/read.d.ts.map +1 -1
- package/dist/read.js +415 -245
- package/dist/refusal.d.ts +5 -7
- package/dist/refusal.d.ts.map +1 -1
- package/dist/refusal.js +47 -68
- package/dist/room-types.d.ts +6 -0
- package/dist/room-types.d.ts.map +1 -0
- package/dist/room-types.js +66 -0
- package/dist/site.d.ts +1 -1
- package/dist/site.d.ts.map +1 -1
- package/dist/site.js +219 -161
- package/dist/stream.d.ts +18 -6
- package/dist/stream.d.ts.map +1 -1
- package/dist/stream.js +556 -214
- package/dist/transport.d.ts +131 -38
- package/dist/transport.d.ts.map +1 -1
- package/dist/transport.js +4 -3
- package/dist/upload.d.ts +12 -0
- package/dist/upload.d.ts.map +1 -0
- package/dist/upload.js +52 -0
- package/dist/version.d.ts +3 -0
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +32 -18
- package/dist/write.d.ts +18 -8
- package/dist/write.d.ts.map +1 -1
- package/dist/write.js +259 -228
- package/package.json +4 -13
- package/dist/actions.js.map +0 -1
- package/dist/adapter.js.map +0 -1
- package/dist/digest.js.map +0 -1
- package/dist/discovery.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/map.js.map +0 -1
- package/dist/model.js.map +0 -1
- package/dist/read.js.map +0 -1
- package/dist/refusal.js.map +0 -1
- package/dist/site.js.map +0 -1
- package/dist/stream.js.map +0 -1
- package/dist/transport.js.map +0 -1
- package/dist/version.js.map +0 -1
- package/dist/write.js.map +0 -1
package/dist/actions.js
CHANGED
|
@@ -1,121 +1,97 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { changeBatchIdentity } from '@agentshouse/mdruntime';
|
|
1
|
+
import { changeBatchIdentity, } from '@agentshouse/mdruntime';
|
|
3
2
|
import { doorRefusal } from './refusal.js';
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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 (
|
|
104
|
-
return
|
|
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 (
|
|
114
|
-
|
|
115
|
-
|
|
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
|
-
|
|
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
|
-
|
|
133
|
-
|
|
134
|
-
|
|
115
|
+
agent_id: text(input, 'agent_id'),
|
|
116
|
+
operation_id: operationId(input),
|
|
117
|
+
...(replaces === undefined ? {} : { replaces }),
|
|
135
118
|
};
|
|
136
119
|
}
|
|
137
|
-
function
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
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
|
|
173
|
-
|
|
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)
|
|
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
|
|
183
|
-
return {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
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
|
|
202
|
-
const
|
|
203
|
-
|
|
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
|
|
206
|
-
const
|
|
207
|
-
const name = text(
|
|
208
|
-
const
|
|
209
|
-
const
|
|
210
|
-
if (
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
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
|
-
|
|
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
|
|
255
|
-
const
|
|
256
|
-
|
|
257
|
-
if (
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
const upload =
|
|
263
|
-
if (upload
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
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
|
-
|
|
272
|
-
const
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
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
|
-
|
|
300
|
-
|
|
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
|
|
310
|
-
let parsed;
|
|
236
|
+
function positionCursor(value) {
|
|
311
237
|
try {
|
|
312
|
-
|
|
313
|
-
}
|
|
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
|
|
325
|
-
const
|
|
326
|
-
if (
|
|
244
|
+
export function acceptedMessage(entry) {
|
|
245
|
+
const cursor = historyCursor(entry);
|
|
246
|
+
if (cursor === null)
|
|
247
|
+
return null;
|
|
327
248
|
return {
|
|
328
|
-
|
|
329
|
-
|
|
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
|
|
333
|
-
const
|
|
334
|
-
const
|
|
335
|
-
if (
|
|
336
|
-
|
|
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
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
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
|
|
392
|
-
const
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
const
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
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
|
-
|
|
439
|
-
|
|
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
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
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
|
-
|
|
329
|
+
const result = { histories };
|
|
330
|
+
return { kind: 'action', result };
|
|
452
331
|
}
|
|
453
|
-
|
|
454
|
-
//# sourceMappingURL=actions.js.map
|