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