agl 43.0.5 → 43.2.0
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/changelog.md +30 -0
- package/dist_serve/bundle.js +5996 -5280
- package/dist_serve/bundle.js.map +2 -2
- package/dist_serve/bundle.js.third-party-notices.json +30 -30
- package/dist_serve/bundle.js.third-party-notices.txt +30 -30
- package/dist_serve/third-party-notices.md +1 -1
- package/dist_ts/00_commitinfo_data.js +1 -1
- package/dist_ts/aglapi/aglapisurface.document.js +805 -10
- package/dist_ts/aglapi/interfaces.d.ts +13 -0
- package/dist_ts/aglapi/interfaces.js +4 -1
- package/dist_ts/classes.authmodels.js +17 -4
- package/dist_ts/classes.authstore.js +16 -2
- package/dist_ts/classes.claudeharness.d.ts +14 -5
- package/dist_ts/classes.claudeharness.js +62 -2
- package/dist_ts/classes.cli.d.ts +4 -2
- package/dist_ts/classes.cli.js +6 -3
- package/dist_ts/classes.codexclientadapter.d.ts +7 -5
- package/dist_ts/classes.codexclientadapter.js +38 -3
- package/dist_ts/classes.controller.d.ts +19 -2
- package/dist_ts/classes.controller.js +234 -27
- package/dist_ts/classes.controllermanagement.d.ts +14 -4
- package/dist_ts/classes.controllermanagement.js +20 -2
- package/dist_ts/classes.gatewayclient.d.ts +115 -0
- package/dist_ts/classes.gatewayclient.js +336 -0
- package/dist_ts/classes.gatewayclientstore.d.ts +97 -0
- package/dist_ts/classes.gatewayclientstore.js +261 -0
- package/dist_ts/classes.inferencegateway.d.ts +8 -5
- package/dist_ts/classes.inferencegateway.js +62 -19
- package/dist_ts/classes.inferencerouter.d.ts +9 -0
- package/dist_ts/classes.inferencerouter.js +11 -1
- package/dist_ts/classes.inferenceservice.d.ts +9 -0
- package/dist_ts/classes.inferenceservice.js +27 -1
- package/dist_ts/classes.inferencespend.d.ts +9 -3
- package/dist_ts/classes.inferencespend.js +44 -16
- package/dist_ts/classes.mcpclient.d.ts +9 -4
- package/dist_ts/classes.mcpclient.js +24 -2
- package/dist_ts/classes.mcpcontrollerhost.d.ts +3 -1
- package/dist_ts/classes.mcpcontrollerhost.js +13 -1
- package/dist_ts/classes.mcptools.d.ts +3 -3
- package/dist_ts/classes.mcptools.js +42 -4
- package/dist_ts/classes.mcpvalidation.js +7 -1
- package/dist_ts/classes.opencodeclient.js +3 -1
- package/dist_ts/classes.proxycontrol.d.ts +33 -11
- package/dist_ts/classes.proxycontrol.js +12 -3
- package/dist_ts/classes.proxycontrolclient.d.ts +23 -5
- package/dist_ts/classes.proxycontrolclient.js +42 -6
- package/dist_ts/classes.proxydaemon.d.ts +8 -6
- package/dist_ts/classes.proxydaemon.js +42 -21
- package/dist_ts/classes.proxyleaseholder.d.ts +12 -4
- package/dist_ts/classes.proxyleaseholder.js +39 -9
- package/dist_ts/classes.proxymonitor.d.ts +7 -0
- package/dist_ts/classes.proxymonitor.js +13 -1
- package/dist_ts/classes.proxystore.d.ts +4 -3
- package/dist_ts/classes.proxystore.js +17 -7
- package/dist_ts/classes.sessionfeed.d.ts +1 -1
- package/dist_ts/classes.trustedproxysecrets.d.ts +2 -0
- package/dist_ts/classes.trustedproxysecrets.js +5 -1
- package/dist_ts/classes.validation.d.ts +7 -0
- package/dist_ts/classes.validation.js +49 -2
- package/dist_ts/driver/functions.drivertools.js +2 -2
- package/dist_ts/fleet/classes.codexworkers.d.ts +4 -11
- package/dist_ts/fleet/classes.codexworkers.js +6 -3
- package/dist_ts/fleet/classes.workers.d.ts +25 -13
- package/dist_ts/fleet/classes.workers.js +105 -9
- package/dist_ts/fleet/functions.workervalidation.d.ts +26 -1
- package/dist_ts/fleet/functions.workervalidation.js +40 -11
- package/dist_ts/fleet/interfaces.workers.d.ts +17 -11
- package/dist_ts/fleet/interfaces.workers.js +10 -1
- package/dist_ts/functions.confinedrelay.d.ts +21 -0
- package/dist_ts/functions.confinedrelay.js +65 -0
- package/dist_ts/functions.controllermanagementsurface.d.ts +4 -0
- package/dist_ts/functions.controllermanagementsurface.js +15 -1
- package/dist_ts/functions.deviceapi.d.ts +4 -2
- package/dist_ts/functions.deviceapi.js +20 -2
- package/dist_ts/functions.elicitation.d.ts +49 -0
- package/dist_ts/functions.elicitation.js +418 -0
- package/dist_ts/functions.gatewaycli.d.ts +24 -0
- package/dist_ts/functions.gatewaycli.js +395 -0
- package/dist_ts/functions.gatewayroute.d.ts +135 -0
- package/dist_ts/functions.gatewayroute.js +366 -0
- package/dist_ts/functions.inferencecli.d.ts +3 -3
- package/dist_ts/functions.inferencecli.js +62 -10
- package/dist_ts/functions.inferencecliarguments.d.ts +9 -2
- package/dist_ts/functions.inferencecliarguments.js +59 -10
- package/dist_ts/functions.inferencelaunch.d.ts +35 -7
- package/dist_ts/functions.inferencelaunch.js +143 -16
- package/dist_ts/functions.mcpfleetworkertools.d.ts +63 -70
- package/dist_ts/functions.mcpfleetworkertools.js +32 -4
- package/dist_ts/functions.proxycapacity.d.ts +50 -0
- package/dist_ts/functions.proxycapacity.js +142 -0
- package/dist_ts/functions.proxycapacitycli.d.ts +30 -0
- package/dist_ts/functions.proxycapacitycli.js +65 -0
- package/dist_ts/functions.proxycontract.d.ts +21 -4
- package/dist_ts/functions.proxycontract.js +12 -5
- package/dist_ts/functions.proxyharnessconfig.d.ts +7 -0
- package/dist_ts/functions.proxyharnessconfig.js +11 -1
- package/dist_ts/functions.remotecli.d.ts +30 -3
- package/dist_ts/functions.remotecli.js +44 -18
- package/dist_ts/interfaces.auth.d.ts +25 -4
- package/dist_ts/interfaces.auth.js +8 -1
- package/dist_ts/plugins.d.ts +2 -0
- package/dist_ts/plugins.js +4 -1
- package/dist_ts_authswitch/authority-placement.js +3 -17
- package/dist_ts_authswitch/authority-usagelimit.d.ts +29 -0
- package/dist_ts_authswitch/authority-usagelimit.js +66 -2
- package/dist_ts_authswitch/authority-usagewindows.d.ts +6 -0
- package/dist_ts_authswitch/authority-usagewindows.js +15 -1
- package/dist_ts_interfaces/devices.d.ts +133 -3
- package/dist_ts_interfaces/devices.js +3 -2
- package/dist_ts_interfaces/elicitations.d.ts +125 -0
- package/dist_ts_interfaces/elicitations.js +7 -0
- package/dist_ts_interfaces/fleetworkers.d.ts +119 -5
- package/dist_ts_interfaces/fleetworkers.js +14 -2
- package/dist_ts_interfaces/gateway.d.ts +151 -0
- package/dist_ts_interfaces/gateway.js +66 -0
- package/dist_ts_interfaces/index.d.ts +3 -0
- package/dist_ts_interfaces/index.js +4 -1
- package/dist_ts_interfaces/interfaces.d.ts +3 -1
- package/dist_ts_interfaces/interfaces.js +2 -2
- package/dist_ts_interfaces/management.d.ts +4 -4
- package/dist_ts_interfaces/management.js +3 -2
- package/dist_ts_interfaces/mcprequests.d.ts +18 -2
- package/dist_ts_interfaces/mcprequests.js +3 -1
- package/dist_ts_interfaces/nativesessions.d.ts +14 -0
- package/dist_ts_interfaces/proxycapacity.d.ts +99 -0
- package/dist_ts_interfaces/proxycapacity.js +74 -0
- package/dist_ts_interfaces/requests.d.ts +3 -3
- package/dist_ts_interfaces/requests.fleetworkers.js +2 -2
- package/dist_ts_interfaces/requests.js +1 -1
- package/dist_ts_interfaces/requests.promptreply.d.ts +18 -0
- package/dist_ts_interfaces/sessionfeed.d.ts +3 -2
- package/dist_ts_interfaces/sessionfeed.js +1 -1
- package/package.json +5 -4
- package/readme.md +267 -26
- package/third-party-notices.md +1 -1
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/aglapi/aglapisurface.document.ts +804 -9
- package/ts/aglapi/interfaces.ts +3 -0
- package/ts/classes.authmodels.ts +22 -3
- package/ts/classes.authstore.ts +15 -1
- package/ts/classes.claudeharness.ts +80 -9
- package/ts/classes.cli.ts +7 -3
- package/ts/classes.codexclientadapter.ts +41 -5
- package/ts/classes.controller.ts +276 -32
- package/ts/classes.controllermanagement.ts +40 -7
- package/ts/classes.gatewayclient.ts +401 -0
- package/ts/classes.gatewayclientstore.ts +272 -0
- package/ts/classes.inferencegateway.ts +69 -23
- package/ts/classes.inferencerouter.ts +12 -0
- package/ts/classes.inferenceservice.ts +27 -0
- package/ts/classes.inferencespend.ts +46 -13
- package/ts/classes.mcpclient.ts +26 -4
- package/ts/classes.mcpcontrollerhost.ts +17 -0
- package/ts/classes.mcptools.ts +47 -5
- package/ts/classes.mcpvalidation.ts +5 -0
- package/ts/classes.opencodeclient.ts +2 -0
- package/ts/classes.proxycontrol.ts +43 -13
- package/ts/classes.proxycontrolclient.ts +39 -6
- package/ts/classes.proxydaemon.ts +57 -27
- package/ts/classes.proxyleaseholder.ts +43 -11
- package/ts/classes.proxymonitor.ts +12 -0
- package/ts/classes.proxystore.ts +21 -8
- package/ts/classes.sessionfeed.ts +1 -1
- package/ts/classes.trustedproxysecrets.ts +5 -0
- package/ts/classes.validation.ts +52 -0
- package/ts/driver/functions.drivertools.ts +1 -1
- package/ts/fleet/classes.codexworkers.ts +8 -6
- package/ts/fleet/classes.workers.ts +101 -17
- package/ts/fleet/functions.workervalidation.ts +35 -9
- package/ts/fleet/interfaces.workers.ts +11 -4
- package/ts/functions.confinedrelay.ts +65 -0
- package/ts/functions.controllermanagementsurface.ts +15 -0
- package/ts/functions.deviceapi.ts +24 -1
- package/ts/functions.elicitation.ts +443 -0
- package/ts/functions.gatewaycli.ts +436 -0
- package/ts/functions.gatewayroute.ts +429 -0
- package/ts/functions.inferencecli.ts +62 -13
- package/ts/functions.inferencecliarguments.ts +56 -9
- package/ts/functions.inferencelaunch.ts +143 -16
- package/ts/functions.mcpfleetworkertools.ts +34 -4
- package/ts/functions.proxycapacity.ts +191 -0
- package/ts/functions.proxycapacitycli.ts +104 -0
- package/ts/functions.proxycontract.ts +17 -4
- package/ts/functions.proxyharnessconfig.ts +11 -0
- package/ts/functions.remotecli.ts +56 -20
- package/ts/interfaces.auth.ts +29 -4
- package/ts/plugins.ts +5 -0
- package/ts_authswitch/authority-placement.ts +2 -15
- package/ts_authswitch/authority-usagelimit.ts +84 -1
- package/ts_authswitch/authority-usagewindows.ts +13 -0
- package/ts_interfaces/devices.schema.json +476 -2
- package/ts_interfaces/devices.ts +146 -2
- package/ts_interfaces/elicitations.ts +148 -0
- package/ts_interfaces/fleetworkers.ts +96 -4
- package/ts_interfaces/gateway.ts +242 -0
- package/ts_interfaces/index.ts +3 -0
- package/ts_interfaces/interfaces.ts +3 -1
- package/ts_interfaces/management.ts +6 -4
- package/ts_interfaces/mcprequests.ts +19 -0
- package/ts_interfaces/nativesessions.ts +16 -0
- package/ts_interfaces/proxycapacity.ts +162 -0
- package/ts_interfaces/requests.fleetworkers.ts +1 -1
- package/ts_interfaces/requests.promptreply.ts +22 -0
- package/ts_interfaces/requests.ts +4 -0
- package/ts_interfaces/sessionfeed.ts +3 -2
- package/ts_web/00_commitinfo_data.ts +1 -1
- package/ts_web/classes.browserpresentation.ts +13 -5
- package/ts_web/classes.elicitationcards.ts +106 -0
- package/ts_web/classes.nativesessionstate.ts +38 -1
- package/ts_web/elements.accounts.ts +40 -113
- package/ts_web/elements.configurationpane.ts +24 -37
- package/ts_web/elements.devices.ts +4 -17
- package/ts_web/elements.fleetworker.ts +27 -2
- package/ts_web/elements.flexlimitstrip.ts +21 -70
- package/ts_web/elements.harnesscontrollerapp.ts +159 -186
- package/ts_web/functions.sessionchat.ts +5 -0
|
@@ -5,6 +5,7 @@ import type {
|
|
|
5
5
|
IControllerQuestion,
|
|
6
6
|
IControllerRuntimeId,
|
|
7
7
|
IControllerSessionFeedRow,
|
|
8
|
+
TControllerElicitation,
|
|
8
9
|
TControllerEventType,
|
|
9
10
|
TControllerSessionFeedDelta,
|
|
10
11
|
} from '../ts_interfaces/index.js';
|
|
@@ -18,16 +19,18 @@ import {
|
|
|
18
19
|
type IDeviceSessionRow,
|
|
19
20
|
type IReq_DeviceAuthChallenge,
|
|
20
21
|
type IReq_DeviceAuthProve,
|
|
22
|
+
type IReq_DeviceElicitationReply,
|
|
21
23
|
type IReq_DevicePair,
|
|
22
24
|
type IReq_DevicePermissionReply,
|
|
23
25
|
type IReq_DeviceQuestionReply,
|
|
24
26
|
type IReq_DeviceSessionRead,
|
|
25
27
|
type IReq_DeviceSessionSend,
|
|
26
28
|
type TDeviceEvent,
|
|
29
|
+
type TDeviceElicitation,
|
|
27
30
|
type TDeviceMessagePart,
|
|
28
31
|
} from '../ts_interfaces/devices.js';
|
|
29
32
|
import { isControllerSessionFeedRowId } from '../ts_interfaces/sessionfeed.js';
|
|
30
|
-
import { assertKeys, assertObject, assertQuestionAnswers } from './classes.validation.js';
|
|
33
|
+
import { assertElicitationReply, assertKeys, assertObject, assertQuestionAnswers } from './classes.validation.js';
|
|
31
34
|
import { sessionFeedRowId } from './classes.sessionfeed.js';
|
|
32
35
|
|
|
33
36
|
/*
|
|
@@ -127,6 +130,16 @@ export const deviceQuestions = (questionsArg: readonly IControllerQuestion[]): I
|
|
|
127
130
|
})),
|
|
128
131
|
}));
|
|
129
132
|
|
|
133
|
+
export const deviceElicitations = (elicitationsArg: readonly TControllerElicitation[]): TDeviceElicitation[] => elicitationsArg
|
|
134
|
+
.map((elicitationArg): TDeviceElicitation => {
|
|
135
|
+
const base = { id: elicitationArg.id.nativeId, serverName: elicitationArg.serverName, message: elicitationArg.message, createdAt: elicitationArg.createdAt };
|
|
136
|
+
switch (elicitationArg.mode) {
|
|
137
|
+
case 'form': return { mode: 'form', ...base, fields: structuredClone(elicitationArg.schema.fields) };
|
|
138
|
+
case 'url': return { mode: 'url', ...base, url: elicitationArg.url };
|
|
139
|
+
case 'unsupported': return { mode: 'unsupported', ...base, reason: elicitationArg.reason };
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
|
|
130
143
|
/**
|
|
131
144
|
* The row id a device names a conversation by, when the feed lists it as something a device opens; events about any
|
|
132
145
|
* other session (a driver, a child, a terminal's agent, one the feed has not listed yet) reach no device.
|
|
@@ -279,6 +292,16 @@ export const assertDeviceQuestionReplyRequest = (valueArg: unknown): IReq_Device
|
|
|
279
292
|
};
|
|
280
293
|
};
|
|
281
294
|
|
|
295
|
+
export const assertDeviceElicitationReplyRequest = (valueArg: unknown): IReq_DeviceElicitationReply['request'] => {
|
|
296
|
+
const value = assertObject(valueArg, 'device elicitation reply request');
|
|
297
|
+
assertKeys(value, ['sessionId', 'elicitationId', 'reply']);
|
|
298
|
+
return {
|
|
299
|
+
sessionId: assertRowId(value.sessionId),
|
|
300
|
+
elicitationId: assertText(value.elicitationId, 'elicitationId', 512),
|
|
301
|
+
reply: assertElicitationReply(value.reply),
|
|
302
|
+
};
|
|
303
|
+
};
|
|
304
|
+
|
|
282
305
|
export const assertDevicePairingCancelRequest = (valueArg: unknown): { pairingId: string } => {
|
|
283
306
|
const value = assertObject(valueArg, 'pairing cancel request');
|
|
284
307
|
assertKeys(value, ['pairingId']);
|
|
@@ -0,0 +1,443 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
IControllerElicitationOption, IControllerElicitationSchema, IControllerElicitationUnsupported, IControllerRuntimeId,
|
|
3
|
+
TControllerElicitation, TControllerElicitationContent, TControllerElicitationField, TControllerElicitationReply,
|
|
4
|
+
TControllerElicitationTextFormat, TControllerElicitationValue,
|
|
5
|
+
} from '../ts_interfaces/index.js';
|
|
6
|
+
import {
|
|
7
|
+
controllerElicitationMaxContentBytes, controllerElicitationMaxFields, controllerElicitationMaxOptions,
|
|
8
|
+
} from '../ts_interfaces/index.js';
|
|
9
|
+
import { boundToolText } from './functions.livetoolbounding.js';
|
|
10
|
+
|
|
11
|
+
/*
|
|
12
|
+
* MCP elicitation as AGL holds it (`TControllerElicitation`): what a harness hands over is read into a card here, by the
|
|
13
|
+
* one normaliser `controllerElicitation`, and the owner's answer is checked against the card here before the harness
|
|
14
|
+
* sends it. Every text this module writes names
|
|
15
|
+
* fields and rules only, never a value the owner entered, so a refusal can travel to the browser, an audit record or a
|
|
16
|
+
* log without carrying what the owner typed.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** A refused answer: `not_found` when the request is no longer pending, `invalid_request` when it does not fit. */
|
|
20
|
+
export class ControllerElicitationReplyError extends Error {
|
|
21
|
+
constructor(messageArg: string, public readonly code: 'invalid_request' | 'not_found') {
|
|
22
|
+
super(messageArg);
|
|
23
|
+
this.name = 'ControllerElicitationReplyError';
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** What the harness sends back to the MCP server, once the answer fits the card. */
|
|
28
|
+
export interface IControllerElicitationAnswer {
|
|
29
|
+
action: 'accept' | 'decline' | 'cancel';
|
|
30
|
+
content?: TControllerElicitationContent;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const maxServerNameBytes = 512;
|
|
34
|
+
const maxMessageBytes = 16 * 1024;
|
|
35
|
+
const maxUrlBytes = 8 * 1024;
|
|
36
|
+
const maxFieldNameBytes = 256;
|
|
37
|
+
const maxTitleBytes = 512;
|
|
38
|
+
const maxDescriptionBytes = 4 * 1024;
|
|
39
|
+
const maxOptionValueBytes = 1024;
|
|
40
|
+
const maxOptionLabelBytes = 1024;
|
|
41
|
+
/** A card's shown texts together, as for permission and question cards. */
|
|
42
|
+
const maxControllerElicitationCardBytes = 64 * 1024;
|
|
43
|
+
|
|
44
|
+
const textFormats: readonly TControllerElicitationTextFormat[] = ['email', 'uri', 'date', 'date-time'];
|
|
45
|
+
|
|
46
|
+
const record = (valueArg: unknown): Record<string, unknown> | undefined => (
|
|
47
|
+
typeof valueArg === 'object' && valueArg !== null && !Array.isArray(valueArg) ? valueArg as Record<string, unknown> : undefined
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
/** A text shown on the card, cut to its bound with a closing ellipsis. */
|
|
51
|
+
const controllerElicitationText = (valueArg: string, maxBytesArg: number): string => {
|
|
52
|
+
const bounded = boundToolText(valueArg, maxBytesArg);
|
|
53
|
+
return bounded.truncated ? `${bounded.text}…` : bounded.text;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/** How a field is named in a reason: its name as the server wrote it, quoted and bounded. */
|
|
57
|
+
const fieldName = (nameArg: string): string => JSON.stringify(boundToolText(nameArg, 128).text);
|
|
58
|
+
|
|
59
|
+
const nonNegativeInteger = (valueArg: unknown): valueArg is number => Number.isSafeInteger(valueArg) && (valueArg as number) >= 0;
|
|
60
|
+
|
|
61
|
+
// ---------------------------------------------------------------------------------------------- reading a request
|
|
62
|
+
|
|
63
|
+
/** A URL request's address, shown as text; an address AGL cannot show whole makes the request unsupported. */
|
|
64
|
+
const readUrl = (urlArg: unknown): { url: string } | { unsupported: string } => {
|
|
65
|
+
if (typeof urlArg !== 'string' || urlArg.length === 0) return { unsupported: 'The server asks to visit an address it did not name.' };
|
|
66
|
+
if (Buffer.byteLength(urlArg, 'utf8') > maxUrlBytes) return { unsupported: 'The server asks to visit an address too long for AGL to show.' };
|
|
67
|
+
return { url: urlArg };
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
type TFieldRead = { field: TControllerElicitationField } | { unsupported: string };
|
|
71
|
+
|
|
72
|
+
const malformed = (nameArg: string): { unsupported: string } => ({ unsupported: `The form's field ${fieldName(nameArg)} is malformed.` });
|
|
73
|
+
|
|
74
|
+
/** Reads the choices a field offers: MCP's `enum` (with legacy `enumNames` labels) or `oneOf`/`anyOf` of `{ const, title }`. */
|
|
75
|
+
const readOptions = (
|
|
76
|
+
nameArg: string,
|
|
77
|
+
enumArg: unknown,
|
|
78
|
+
enumNamesArg: unknown,
|
|
79
|
+
titledArg: unknown,
|
|
80
|
+
): IControllerElicitationOption[] | { unsupported: string } => {
|
|
81
|
+
let options: IControllerElicitationOption[];
|
|
82
|
+
if (enumArg !== undefined) {
|
|
83
|
+
if (titledArg !== undefined || !Array.isArray(enumArg) || enumArg.some((entry) => typeof entry !== 'string')) return malformed(nameArg);
|
|
84
|
+
const labels = enumNamesArg;
|
|
85
|
+
if (labels !== undefined && (!Array.isArray(labels) || labels.length !== enumArg.length || labels.some((entry) => typeof entry !== 'string'))) {
|
|
86
|
+
return malformed(nameArg);
|
|
87
|
+
}
|
|
88
|
+
options = (enumArg as string[]).map((value, index) => ({ value, label: Array.isArray(labels) ? labels[index] as string : value }));
|
|
89
|
+
} else {
|
|
90
|
+
if (!Array.isArray(titledArg)) return malformed(nameArg);
|
|
91
|
+
const entries = titledArg.map(record);
|
|
92
|
+
if (entries.some((entry) => typeof entry?.const !== 'string' || typeof entry.title !== 'string')) return malformed(nameArg);
|
|
93
|
+
for (const entry of entries) {
|
|
94
|
+
const unchecked = uncheckedKeyword(entry!, 'option');
|
|
95
|
+
if (unchecked !== undefined) return { unsupported: `A choice of the form's field ${fieldName(nameArg)} uses the rule "${unchecked}", which AGL cannot check.` };
|
|
96
|
+
}
|
|
97
|
+
options = entries.map((entry) => ({ value: entry!.const as string, label: entry!.title as string }));
|
|
98
|
+
}
|
|
99
|
+
if (options.length === 0) return { unsupported: `The form's field ${fieldName(nameArg)} offers no choice.` };
|
|
100
|
+
if (options.length > controllerElicitationMaxOptions) return { unsupported: `The form's field ${fieldName(nameArg)} offers more choices than AGL shows.` };
|
|
101
|
+
if (options.some((option) => Buffer.byteLength(option.value, 'utf8') > maxOptionValueBytes)) {
|
|
102
|
+
return { unsupported: `A choice of the form's field ${fieldName(nameArg)} is too long for AGL to send.` };
|
|
103
|
+
}
|
|
104
|
+
if (new Set(options.map((option) => option.value)).size !== options.length) return malformed(nameArg);
|
|
105
|
+
return options.map((option) => ({ value: option.value, label: controllerElicitationText(option.label, maxOptionLabelBytes) }));
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Where a schema object sits in a form, for the keywords AGL checks there: a field of each kind (`number` for number
|
|
110
|
+
* and integer alike), the `items` of a list of choices, an option of `oneOf` or `anyOf`, and the form itself.
|
|
111
|
+
*/
|
|
112
|
+
type TElicitationFieldPlace = 'text' | 'choice' | 'number' | 'boolean' | 'choices';
|
|
113
|
+
type TElicitationSchemaPlace = TElicitationFieldPlace | 'items' | 'option' | 'form';
|
|
114
|
+
|
|
115
|
+
/** The JSON Schema keywords (drafts 7 to 2020-12) a validator enforces; every other keyword is an annotation. */
|
|
116
|
+
const constraintKeywords: ReadonlySet<string> = new Set([
|
|
117
|
+
'type', 'const', 'enum', 'multipleOf', 'minimum', 'maximum', 'exclusiveMinimum', 'exclusiveMaximum', 'minLength', 'maxLength',
|
|
118
|
+
'pattern', 'format', 'minItems', 'maxItems', 'uniqueItems', 'contains', 'minContains', 'maxContains', 'items',
|
|
119
|
+
'prefixItems', 'additionalItems', 'unevaluatedItems', 'properties', 'patternProperties', 'additionalProperties',
|
|
120
|
+
'propertyNames', 'unevaluatedProperties', 'required', 'minProperties', 'maxProperties', 'dependentRequired',
|
|
121
|
+
'dependentSchemas', 'dependencies', 'allOf', 'anyOf', 'oneOf', 'not', 'if', 'then', 'else', '$ref', '$dynamicRef',
|
|
122
|
+
'$recursiveRef',
|
|
123
|
+
]);
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The keywords AGL reads into a card and checks an answer against, for each place. `uniqueItems` holds either way for
|
|
127
|
+
* a list of choices, as an answer never names a choice twice; the form's `additionalProperties` holds either way, as
|
|
128
|
+
* an answer never carries a field the form does not define.
|
|
129
|
+
*/
|
|
130
|
+
const mappedKeywords: Record<TElicitationSchemaPlace, ReadonlySet<string>> = {
|
|
131
|
+
text: new Set(['type', 'format', 'minLength', 'maxLength']),
|
|
132
|
+
choice: new Set(['type', 'enum', 'enumNames', 'oneOf']),
|
|
133
|
+
number: new Set(['type', 'minimum', 'maximum']),
|
|
134
|
+
boolean: new Set(['type']),
|
|
135
|
+
choices: new Set(['type', 'items', 'minItems', 'maxItems', 'uniqueItems']),
|
|
136
|
+
items: new Set(['type', 'enum', 'anyOf']),
|
|
137
|
+
option: new Set(['const']),
|
|
138
|
+
form: new Set(['type', 'properties', 'required', 'additionalProperties']),
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/** The first keyword of `schemaArg` a validator enforces and AGL does not check at `placeArg`. */
|
|
142
|
+
const uncheckedKeyword = (schemaArg: Record<string, unknown>, placeArg: TElicitationSchemaPlace): string | undefined => (
|
|
143
|
+
Object.keys(schemaArg).find((keyword) => constraintKeywords.has(keyword) && !mappedKeywords[placeArg].has(keyword))
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
/** The place of a field by the kind of value it takes; `undefined` for a kind AGL cannot show. */
|
|
147
|
+
const fieldPlace = (propertyArg: Record<string, unknown>): TElicitationFieldPlace | undefined => {
|
|
148
|
+
switch (propertyArg.type) {
|
|
149
|
+
case 'string': return propertyArg.enum !== undefined || propertyArg.oneOf !== undefined ? 'choice' : 'text';
|
|
150
|
+
case 'number': case 'integer': return 'number';
|
|
151
|
+
case 'boolean': return 'boolean';
|
|
152
|
+
case 'array': return 'choices';
|
|
153
|
+
default: return undefined;
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
const readField = (nameArg: string, valueArg: unknown, requiredArg: boolean): TFieldRead => {
|
|
158
|
+
if (nameArg.length === 0 || Buffer.byteLength(nameArg, 'utf8') > maxFieldNameBytes) return { unsupported: 'A field of the form has a name AGL cannot send.' };
|
|
159
|
+
const property = record(valueArg);
|
|
160
|
+
if (!property) return malformed(nameArg);
|
|
161
|
+
if ((property.title !== undefined && typeof property.title !== 'string')
|
|
162
|
+
|| (property.description !== undefined && typeof property.description !== 'string')) return malformed(nameArg);
|
|
163
|
+
const place = fieldPlace(property);
|
|
164
|
+
if (place === undefined) {
|
|
165
|
+
return { unsupported: property.type === 'object'
|
|
166
|
+
? `The form's field ${fieldName(nameArg)} asks for a nested object, which AGL cannot show.`
|
|
167
|
+
: `The form's field ${fieldName(nameArg)} asks for a kind of value AGL cannot show.` };
|
|
168
|
+
}
|
|
169
|
+
const unchecked = uncheckedKeyword(property, place);
|
|
170
|
+
if (unchecked !== undefined) return { unsupported: `The form's field ${fieldName(nameArg)} uses the rule "${unchecked}", which AGL cannot check.` };
|
|
171
|
+
const base = {
|
|
172
|
+
name: nameArg,
|
|
173
|
+
...(typeof property.title === 'string' ? { title: controllerElicitationText(property.title, maxTitleBytes) } : {}),
|
|
174
|
+
...(typeof property.description === 'string' ? { description: controllerElicitationText(property.description, maxDescriptionBytes) } : {}),
|
|
175
|
+
required: requiredArg,
|
|
176
|
+
};
|
|
177
|
+
let field: TControllerElicitationField;
|
|
178
|
+
switch (place) {
|
|
179
|
+
case 'choice': {
|
|
180
|
+
const options = readOptions(nameArg, property.enum, property.enumNames, property.oneOf);
|
|
181
|
+
if (!Array.isArray(options)) return options;
|
|
182
|
+
field = { ...base, kind: 'choice', options, ...(property.default === undefined ? {} : { default: property.default as string }) };
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
case 'text': {
|
|
186
|
+
const { format, minLength, maxLength } = property;
|
|
187
|
+
if (format !== undefined && !textFormats.includes(format as TControllerElicitationTextFormat)) {
|
|
188
|
+
return { unsupported: `The form's field ${fieldName(nameArg)} asks for a format AGL cannot check.` };
|
|
189
|
+
}
|
|
190
|
+
if ((minLength !== undefined && !nonNegativeInteger(minLength)) || (maxLength !== undefined && !nonNegativeInteger(maxLength))
|
|
191
|
+
|| (minLength !== undefined && maxLength !== undefined && (minLength as number) > (maxLength as number))) return malformed(nameArg);
|
|
192
|
+
field = {
|
|
193
|
+
...base, kind: 'text',
|
|
194
|
+
...(format === undefined ? {} : { format: format as TControllerElicitationTextFormat }),
|
|
195
|
+
...(minLength === undefined ? {} : { minLength: minLength as number }),
|
|
196
|
+
...(maxLength === undefined ? {} : { maxLength: maxLength as number }),
|
|
197
|
+
...(property.default === undefined ? {} : { default: property.default as string }),
|
|
198
|
+
};
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
case 'number': {
|
|
202
|
+
const { minimum, maximum } = property;
|
|
203
|
+
const finite = (entryArg: unknown) => typeof entryArg === 'number' && Number.isFinite(entryArg);
|
|
204
|
+
if ((minimum !== undefined && !finite(minimum)) || (maximum !== undefined && !finite(maximum))
|
|
205
|
+
|| (minimum !== undefined && maximum !== undefined && (minimum as number) > (maximum as number))) return malformed(nameArg);
|
|
206
|
+
field = {
|
|
207
|
+
...base, kind: 'number', integer: property.type === 'integer',
|
|
208
|
+
...(minimum === undefined ? {} : { minimum: minimum as number }),
|
|
209
|
+
...(maximum === undefined ? {} : { maximum: maximum as number }),
|
|
210
|
+
...(property.default === undefined ? {} : { default: property.default as number }),
|
|
211
|
+
};
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
case 'boolean':
|
|
215
|
+
field = { ...base, kind: 'boolean', ...(property.default === undefined ? {} : { default: property.default as boolean }) };
|
|
216
|
+
break;
|
|
217
|
+
case 'choices': {
|
|
218
|
+
const items = record(property.items);
|
|
219
|
+
if (!items) return { unsupported: `The form's field ${fieldName(nameArg)} asks for a list AGL cannot show.` };
|
|
220
|
+
const uncheckedItem = uncheckedKeyword(items, 'items');
|
|
221
|
+
if (uncheckedItem !== undefined) {
|
|
222
|
+
return { unsupported: `The choices of the form's field ${fieldName(nameArg)} use the rule "${uncheckedItem}", which AGL cannot check.` };
|
|
223
|
+
}
|
|
224
|
+
if ((items.enum === undefined && items.anyOf === undefined) || (items.type !== undefined && items.type !== 'string')) {
|
|
225
|
+
return { unsupported: `The form's field ${fieldName(nameArg)} asks for a list AGL cannot show.` };
|
|
226
|
+
}
|
|
227
|
+
const options = readOptions(nameArg, items.enum, undefined, items.anyOf);
|
|
228
|
+
if (!Array.isArray(options)) return options;
|
|
229
|
+
const { minItems, maxItems, uniqueItems } = property;
|
|
230
|
+
if ((minItems !== undefined && !nonNegativeInteger(minItems)) || (maxItems !== undefined && !nonNegativeInteger(maxItems))
|
|
231
|
+
|| (minItems !== undefined && maxItems !== undefined && (minItems as number) > (maxItems as number))
|
|
232
|
+
|| (uniqueItems !== undefined && typeof uniqueItems !== 'boolean')) return malformed(nameArg);
|
|
233
|
+
field = {
|
|
234
|
+
...base, kind: 'choices', options,
|
|
235
|
+
...(minItems === undefined ? {} : { minItems: minItems as number }),
|
|
236
|
+
...(maxItems === undefined ? {} : { maxItems: maxItems as number }),
|
|
237
|
+
...(property.default === undefined ? {} : { default: property.default as string[] }),
|
|
238
|
+
};
|
|
239
|
+
break;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
// A default is a value the owner could send unchanged, so it must pass the field's own rules.
|
|
243
|
+
if (field.default !== undefined && controllerElicitationValueProblem(field, field.default) !== undefined) {
|
|
244
|
+
return { unsupported: `The form's field ${fieldName(nameArg)} has a default its own rules refuse.` };
|
|
245
|
+
}
|
|
246
|
+
return { field };
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Reads a form request's schema (MCP `requestedSchema`) into the fields AGL collects, or names why AGL cannot collect
|
|
251
|
+
* it: anything outside MCP's flat primitive subset. Annotations are left out; a keyword a validator enforces that AGL
|
|
252
|
+
* cannot check (`constraintKeywords` beyond `mappedKeywords`, in a field, its `items` or the form) makes the form
|
|
253
|
+
* unsupported, so the owner never answers a form whose rules AGL did not check.
|
|
254
|
+
*/
|
|
255
|
+
export const controllerElicitationForm = (schemaArg: unknown): { schema: IControllerElicitationSchema } | { unsupported: string } => {
|
|
256
|
+
const schema = record(schemaArg);
|
|
257
|
+
const properties = record(schema?.properties);
|
|
258
|
+
if (!schema || schema.type !== 'object' || !properties) return { unsupported: 'The server sent no form AGL can read.' };
|
|
259
|
+
const unchecked = uncheckedKeyword(schema, 'form');
|
|
260
|
+
if (unchecked !== undefined) return { unsupported: `The form uses the rule "${unchecked}", which AGL cannot check.` };
|
|
261
|
+
if (schema.required !== undefined && (!Array.isArray(schema.required) || schema.required.some((entry) => typeof entry !== 'string'))) {
|
|
262
|
+
return { unsupported: 'The server sent no form AGL can read.' };
|
|
263
|
+
}
|
|
264
|
+
const required = new Set(schema.required as string[] | undefined);
|
|
265
|
+
const entries = Object.entries(properties);
|
|
266
|
+
if (entries.length > controllerElicitationMaxFields) return { unsupported: 'The form has more fields than AGL shows.' };
|
|
267
|
+
if ([...required].some((name) => !Object.hasOwn(properties, name))) return { unsupported: 'The form requires a field it does not define.' };
|
|
268
|
+
const fields: TControllerElicitationField[] = [];
|
|
269
|
+
for (const [name, value] of entries) {
|
|
270
|
+
const read = readField(name, value, required.has(name));
|
|
271
|
+
if ('unsupported' in read) return read;
|
|
272
|
+
fields.push(read.field);
|
|
273
|
+
}
|
|
274
|
+
return { schema: { fields } };
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
/** What a harness hands over of an MCP elicitation, as it arrived: the normaliser trusts none of it. */
|
|
278
|
+
export interface IControllerElicitationRequest {
|
|
279
|
+
id: IControllerRuntimeId;
|
|
280
|
+
sessionId: IControllerRuntimeId;
|
|
281
|
+
serverName: unknown;
|
|
282
|
+
message: unknown;
|
|
283
|
+
/** MCP's `mode`; absent is a form, as MCP defines it. */
|
|
284
|
+
mode: unknown;
|
|
285
|
+
url?: unknown;
|
|
286
|
+
requestedSchema?: unknown;
|
|
287
|
+
createdAt: number;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** A mode is named in a reason only while it is a short token, so a reason never carries arbitrary text. */
|
|
291
|
+
const modeReason = (modeArg: unknown): string => (
|
|
292
|
+
typeof modeArg === 'string' && /^[A-Za-z0-9][A-Za-z0-9._/-]{0,63}$/u.test(modeArg)
|
|
293
|
+
? `The request is of the kind ${JSON.stringify(modeArg)}, which AGL cannot show.`
|
|
294
|
+
: 'The request is of a kind AGL cannot show.'
|
|
295
|
+
);
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* The one reading of an MCP elicitation into its card, for every harness: a form of MCP's flat primitive subset, a
|
|
299
|
+
* URL, or, for anything else (another mode, a nested or otherwise unknown field, a rule AGL cannot check, a malformed
|
|
300
|
+
* form, a card larger than AGL shows), an unsupported card with the reason. A field AGL cannot collect is never left out of a form.
|
|
301
|
+
*/
|
|
302
|
+
export const controllerElicitation = (requestArg: IControllerElicitationRequest): TControllerElicitation => {
|
|
303
|
+
const base = {
|
|
304
|
+
id: requestArg.id,
|
|
305
|
+
sessionId: requestArg.sessionId,
|
|
306
|
+
serverName: controllerElicitationText(typeof requestArg.serverName === 'string' ? requestArg.serverName : '', maxServerNameBytes),
|
|
307
|
+
message: controllerElicitationText(typeof requestArg.message === 'string' ? requestArg.message : '', maxMessageBytes),
|
|
308
|
+
createdAt: requestArg.createdAt,
|
|
309
|
+
};
|
|
310
|
+
const unsupported = (reasonArg: string): IControllerElicitationUnsupported => ({ ...base, mode: 'unsupported', reason: reasonArg });
|
|
311
|
+
const mode = requestArg.mode === undefined ? 'form' : requestArg.mode;
|
|
312
|
+
let card: TControllerElicitation;
|
|
313
|
+
if (mode === 'form') {
|
|
314
|
+
const read = controllerElicitationForm(requestArg.requestedSchema);
|
|
315
|
+
card = 'schema' in read ? { ...base, mode: 'form', schema: read.schema } : unsupported(read.unsupported);
|
|
316
|
+
} else if (mode === 'url') {
|
|
317
|
+
const read = readUrl(requestArg.url);
|
|
318
|
+
card = 'url' in read ? { ...base, mode: 'url', url: read.url } : unsupported(read.unsupported);
|
|
319
|
+
} else {
|
|
320
|
+
card = unsupported(modeReason(mode));
|
|
321
|
+
}
|
|
322
|
+
// A card fits the same display budget as a permission or question card; its bounded texts always do.
|
|
323
|
+
return Buffer.byteLength(JSON.stringify(card), 'utf8') <= maxControllerElicitationCardBytes
|
|
324
|
+
? card
|
|
325
|
+
: unsupported('The form is larger than AGL shows.');
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
// ----------------------------------------------------------------------------------------------- checking an answer
|
|
329
|
+
|
|
330
|
+
const codePoints = (valueArg: string): number => [...valueArg].length;
|
|
331
|
+
|
|
332
|
+
const validDate = (yearArg: string, monthArg: string, dayArg: string): boolean => {
|
|
333
|
+
const year = Number(yearArg), month = Number(monthArg), day = Number(dayArg);
|
|
334
|
+
if (month < 1 || month > 12 || day < 1) return false;
|
|
335
|
+
return day <= new Date(Date.UTC(year, month, 0)).getUTCDate();
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
const formatValid: Record<TControllerElicitationTextFormat, (valueArg: string) => boolean> = {
|
|
339
|
+
// An address with one @ between a local part and a domain, neither with white space (RFC 5321 in practice).
|
|
340
|
+
email: (valueArg) => /^[^\s@]+@[^\s@]+$/u.test(valueArg),
|
|
341
|
+
// An absolute URI: a scheme, then what URL parsing accepts.
|
|
342
|
+
uri: (valueArg) => /^[A-Za-z][A-Za-z0-9+.-]*:\S*$/u.test(valueArg) && URL.canParse(valueArg),
|
|
343
|
+
// RFC 3339 full-date.
|
|
344
|
+
date: (valueArg) => {
|
|
345
|
+
const match = /^(\d{4})-(\d{2})-(\d{2})$/u.exec(valueArg);
|
|
346
|
+
return match !== null && validDate(match[1]!, match[2]!, match[3]!);
|
|
347
|
+
},
|
|
348
|
+
// RFC 3339 date-time.
|
|
349
|
+
'date-time': (valueArg) => {
|
|
350
|
+
const match = /^(\d{4})-(\d{2})-(\d{2})[Tt](\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(?:[Zz]|[+-](\d{2}):(\d{2}))$/u.exec(valueArg);
|
|
351
|
+
if (!match || !validDate(match[1]!, match[2]!, match[3]!)) return false;
|
|
352
|
+
const [hour, minute, second] = [Number(match[4]), Number(match[5]), Number(match[6])];
|
|
353
|
+
if (hour > 23 || minute > 59 || second > 60) return false;
|
|
354
|
+
return match[7] === undefined || (Number(match[7]) <= 23 && Number(match[8]) <= 59);
|
|
355
|
+
},
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
const formatNames: Record<TControllerElicitationTextFormat, string> = {
|
|
359
|
+
email: 'an email address', uri: 'an absolute URI', date: 'a date (YYYY-MM-DD)', 'date-time': 'a date and time (RFC 3339)',
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
/** Why `valueArg` does not fit the field, in words that name the field and its rule, never the value; `undefined` when it fits. */
|
|
363
|
+
export const controllerElicitationValueProblem = (fieldArg: TControllerElicitationField, valueArg: unknown): string | undefined => {
|
|
364
|
+
const name = fieldName(fieldArg.name);
|
|
365
|
+
switch (fieldArg.kind) {
|
|
366
|
+
case 'text': {
|
|
367
|
+
if (typeof valueArg !== 'string') return `The form's field ${name} takes text.`;
|
|
368
|
+
const length = codePoints(valueArg);
|
|
369
|
+
if (fieldArg.minLength !== undefined && length < fieldArg.minLength) return `The form's field ${name} takes at least ${fieldArg.minLength} characters.`;
|
|
370
|
+
if (fieldArg.maxLength !== undefined && length > fieldArg.maxLength) return `The form's field ${name} takes at most ${fieldArg.maxLength} characters.`;
|
|
371
|
+
if (fieldArg.format !== undefined && !formatValid[fieldArg.format](valueArg)) return `The form's field ${name} takes ${formatNames[fieldArg.format]}.`;
|
|
372
|
+
return undefined;
|
|
373
|
+
}
|
|
374
|
+
case 'number': {
|
|
375
|
+
if (typeof valueArg !== 'number' || !Number.isFinite(valueArg)) return `The form's field ${name} takes a number.`;
|
|
376
|
+
if (fieldArg.integer && !Number.isInteger(valueArg)) return `The form's field ${name} takes a whole number.`;
|
|
377
|
+
if (fieldArg.minimum !== undefined && valueArg < fieldArg.minimum) return `The form's field ${name} takes a number of at least ${fieldArg.minimum}.`;
|
|
378
|
+
if (fieldArg.maximum !== undefined && valueArg > fieldArg.maximum) return `The form's field ${name} takes a number of at most ${fieldArg.maximum}.`;
|
|
379
|
+
return undefined;
|
|
380
|
+
}
|
|
381
|
+
case 'boolean':
|
|
382
|
+
return typeof valueArg === 'boolean' ? undefined : `The form's field ${name} takes yes or no.`;
|
|
383
|
+
case 'choice':
|
|
384
|
+
return typeof valueArg === 'string' && fieldArg.options.some((option) => option.value === valueArg)
|
|
385
|
+
? undefined
|
|
386
|
+
: `The form's field ${name} takes one of its choices.`;
|
|
387
|
+
case 'choices': {
|
|
388
|
+
if (!Array.isArray(valueArg) || valueArg.some((entry) => typeof entry !== 'string' || !fieldArg.options.some((option) => option.value === entry))) {
|
|
389
|
+
return `The form's field ${name} takes some of its choices.`;
|
|
390
|
+
}
|
|
391
|
+
if (new Set(valueArg).size !== valueArg.length) return `The form's field ${name} takes each choice at most once.`;
|
|
392
|
+
if (fieldArg.minItems !== undefined && valueArg.length < fieldArg.minItems) return `The form's field ${name} takes at least ${fieldArg.minItems} choices.`;
|
|
393
|
+
if (fieldArg.maxItems !== undefined && valueArg.length > fieldArg.maxItems) return `The form's field ${name} takes at most ${fieldArg.maxItems} choices.`;
|
|
394
|
+
return undefined;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
/** Checks a form's answer against its fields: every field it carries fits, and every required field is there. */
|
|
400
|
+
export const controllerElicitationContent = (schemaArg: IControllerElicitationSchema, contentArg: unknown): TControllerElicitationContent => {
|
|
401
|
+
const content = record(contentArg);
|
|
402
|
+
if (!content) throw new ControllerElicitationReplyError('The answer to a form is an object of field values.', 'invalid_request');
|
|
403
|
+
if (Buffer.byteLength(JSON.stringify(content), 'utf8') > controllerElicitationMaxContentBytes) {
|
|
404
|
+
throw new ControllerElicitationReplyError('The answer is larger than AGL sends.', 'invalid_request');
|
|
405
|
+
}
|
|
406
|
+
const fields = new Map(schemaArg.fields.map((field) => [field.name, field]));
|
|
407
|
+
// The unknown name itself is the owner's input, so it is not repeated.
|
|
408
|
+
if (Object.keys(content).some((key) => !fields.has(key))) throw new ControllerElicitationReplyError('The answer carries a field the form does not define.', 'invalid_request');
|
|
409
|
+
const entries: Array<[string, TControllerElicitationValue]> = [];
|
|
410
|
+
for (const field of schemaArg.fields) {
|
|
411
|
+
if (!Object.hasOwn(content, field.name)) {
|
|
412
|
+
if (field.required) throw new ControllerElicitationReplyError(`The form's field ${fieldName(field.name)} needs a value.`, 'invalid_request');
|
|
413
|
+
continue;
|
|
414
|
+
}
|
|
415
|
+
const value = content[field.name];
|
|
416
|
+
const problem = controllerElicitationValueProblem(field, value);
|
|
417
|
+
if (problem !== undefined) throw new ControllerElicitationReplyError(problem, 'invalid_request');
|
|
418
|
+
entries.push([field.name, Array.isArray(value) ? [...value as string[]] : value as TControllerElicitationValue]);
|
|
419
|
+
}
|
|
420
|
+
// `fromEntries` defines each name as a property of its own, also a name such as `__proto__`.
|
|
421
|
+
return Object.fromEntries(entries);
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* The answer the harness sends for the owner's reply to `elicitationArg`. Decline and cancel always pass; accept
|
|
426
|
+
* passes only for a request AGL collects: a form with its content checked against its fields, a URL without content.
|
|
427
|
+
*/
|
|
428
|
+
export const controllerElicitationAnswer = (
|
|
429
|
+
elicitationArg: TControllerElicitation,
|
|
430
|
+
replyArg: TControllerElicitationReply,
|
|
431
|
+
): IControllerElicitationAnswer => {
|
|
432
|
+
if (replyArg.action === 'decline' || replyArg.action === 'cancel') return { action: replyArg.action };
|
|
433
|
+
switch (elicitationArg.mode) {
|
|
434
|
+
case 'unsupported':
|
|
435
|
+
throw new ControllerElicitationReplyError('AGL cannot collect what this request asks for: decline or cancel it.', 'invalid_request');
|
|
436
|
+
case 'url':
|
|
437
|
+
if (replyArg.content !== undefined) throw new ControllerElicitationReplyError('A visit to an address is accepted without content.', 'invalid_request');
|
|
438
|
+
return { action: 'accept' };
|
|
439
|
+
case 'form':
|
|
440
|
+
if (replyArg.content === undefined) throw new ControllerElicitationReplyError('An accepted form carries its content.', 'invalid_request');
|
|
441
|
+
return { action: 'accept', content: controllerElicitationContent(elicitationArg.schema, replyArg.content) };
|
|
442
|
+
}
|
|
443
|
+
};
|