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