agl 43.2.0 → 43.3.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 +20 -0
- package/dist_serve/bundle.js +2845 -2638
- package/dist_serve/bundle.js.map +2 -2
- package/dist_serve/bundle.js.third-party-notices.json +1 -1
- package/dist_serve/bundle.js.third-party-notices.txt +1 -1
- package/dist_ts/00_commitinfo_data.js +1 -1
- package/dist_ts/aglapi/aglapisurface.document.js +197 -17
- package/dist_ts/classes.accountsactions.d.ts +3 -1
- package/dist_ts/classes.accountsactions.js +21 -3
- package/dist_ts/classes.claudeharness.d.ts +16 -0
- package/dist_ts/classes.claudeharness.js +24 -2
- package/dist_ts/classes.codexclientadapter.d.ts +11 -0
- package/dist_ts/classes.codexclientadapter.js +14 -2
- package/dist_ts/classes.controller.d.ts +165 -11
- package/dist_ts/classes.controller.js +1095 -170
- package/dist_ts/classes.controllerdevices.d.ts +2 -0
- package/dist_ts/classes.controllerdevices.js +6 -1
- package/dist_ts/classes.inbox.d.ts +72 -0
- package/dist_ts/classes.inbox.js +231 -0
- package/dist_ts/classes.inferencerouter.d.ts +10 -0
- package/dist_ts/classes.inferencerouter.js +5 -3
- package/dist_ts/classes.inferenceservice.js +2 -2
- package/dist_ts/classes.mcpclient.d.ts +1 -1
- package/dist_ts/classes.mcpclient.js +24 -6
- package/dist_ts/classes.mcpcontrollerhost.js +3 -1
- package/dist_ts/classes.mcptools.js +13 -8
- package/dist_ts/classes.mcpvalidation.d.ts +5 -0
- package/dist_ts/classes.mcpvalidation.js +32 -2
- package/dist_ts/classes.nativesessions.d.ts +17 -4
- package/dist_ts/classes.nativesessions.js +41 -9
- package/dist_ts/classes.opencodeclient.d.ts +23 -2
- package/dist_ts/classes.opencodeclient.js +47 -3
- package/dist_ts/classes.publishedset.d.ts +38 -0
- package/dist_ts/classes.publishedset.js +50 -0
- package/dist_ts/classes.sessionfeed.d.ts +3 -3
- package/dist_ts/classes.sessionfeed.js +18 -26
- package/dist_ts/classes.systemmetricssampler.d.ts +6 -0
- package/dist_ts/classes.systemmetricssampler.js +15 -3
- package/dist_ts/classes.ticketstore.d.ts +50 -3
- package/dist_ts/classes.ticketstore.js +45 -8
- package/dist_ts/driver/classes.directreports.d.ts +14 -4
- package/dist_ts/driver/classes.directreports.js +25 -6
- package/dist_ts/driver/classes.driverservice.d.ts +44 -3
- package/dist_ts/driver/classes.driverservice.js +50 -16
- package/dist_ts/fleet/classes.claudeworkers.d.ts +21 -5
- package/dist_ts/fleet/classes.claudeworkers.js +142 -46
- package/dist_ts/fleet/classes.codexworkers.d.ts +16 -4
- package/dist_ts/fleet/classes.codexworkers.js +22 -5
- package/dist_ts/fleet/classes.workercommand.d.ts +6 -1
- package/dist_ts/fleet/classes.workercommand.js +15 -4
- package/dist_ts/fleet/classes.workers.d.ts +5 -1
- package/dist_ts/fleet/classes.workers.js +8 -2
- package/dist_ts/fleet/functions.unbounddrivers.js +2 -2
- package/dist_ts/fleet/functions.workervalidation.js +2 -2
- package/dist_ts/fleet/interfaces.workers.d.ts +21 -3
- package/dist_ts/fleet/interfaces.workers.js +1 -1
- package/dist_ts/functions.agentmessage.d.ts +3 -0
- package/dist_ts/functions.agentmessage.js +4 -1
- package/dist_ts/functions.deviceapi.d.ts +151 -4
- package/dist_ts/functions.deviceapi.js +893 -38
- package/dist_ts/functions.fleetworkercli.d.ts +1 -1
- package/dist_ts/functions.fleetworkercli.js +11 -8
- package/dist_ts/functions.inferencecli.js +3 -1
- package/dist_ts/functions.mcpfleetworkertools.js +2 -2
- package/dist_ts/functions.ticketvalidation.js +11 -2
- package/dist_ts/interfaces.auth.d.ts +1 -1
- package/dist_ts/interfaces.auth.js +9 -1
- package/dist_ts/plugins.d.ts +3 -1
- package/dist_ts/plugins.js +7 -2
- package/dist_ts_authswitch/authority-contract.d.ts +25 -0
- package/dist_ts_authswitch/authority-contract.js +1 -1
- package/dist_ts_authswitch/authority-overview.d.ts +8 -0
- package/dist_ts_authswitch/authority-overview.js +17 -6
- package/dist_ts_authswitch/authority-placement.d.ts +20 -16
- package/dist_ts_authswitch/authority-placement.js +17 -10
- package/dist_ts_authswitch/authority-renewal.d.ts +34 -0
- package/dist_ts_authswitch/authority-renewal.js +56 -0
- package/dist_ts_authswitch/classes.authoritybroker.d.ts +8 -0
- package/dist_ts_authswitch/classes.authoritybroker.js +35 -2
- package/dist_ts_authswitch/classes.authoritycli.d.ts +1 -0
- package/dist_ts_authswitch/classes.authoritycli.js +44 -3
- package/dist_ts_authswitch/classes.authorityclient.d.ts +6 -1
- package/dist_ts_authswitch/classes.authorityclient.js +12 -4
- package/dist_ts_authswitch/classes.authoritydaemon.js +8 -1
- package/dist_ts_authswitch/classes.authoritydatabase.js +6 -2
- package/dist_ts_authswitch/classes.authoritymodels.d.ts +7 -0
- package/dist_ts_authswitch/classes.authoritymodels.js +21 -5
- package/dist_ts_interfaces/devices.d.ts +1012 -5
- package/dist_ts_interfaces/devices.js +68 -2
- package/dist_ts_interfaces/fleetworkers.d.ts +5 -3
- package/dist_ts_interfaces/inbox.d.ts +234 -0
- package/dist_ts_interfaces/inbox.js +101 -0
- package/dist_ts_interfaces/index.d.ts +1 -0
- package/dist_ts_interfaces/index.js +2 -1
- package/dist_ts_interfaces/interfaces.d.ts +10 -3
- package/dist_ts_interfaces/interfaces.js +4 -2
- package/dist_ts_interfaces/mcprequests.d.ts +34 -4
- package/dist_ts_interfaces/mcprequests.js +11 -1
- package/dist_ts_interfaces/mcpworkspacerequests.d.ts +4 -1
- package/dist_ts_interfaces/mcpworkspacerequests.js +1 -1
- package/dist_ts_interfaces/nativesessions.d.ts +16 -3
- package/dist_ts_interfaces/plugins.d.ts +2 -1
- package/dist_ts_interfaces/plugins.js +4 -2
- package/dist_ts_interfaces/requests.accounts.d.ts +21 -0
- package/dist_ts_interfaces/requests.accounts.js +3 -1
- package/dist_ts_interfaces/requests.d.ts +3 -2
- package/dist_ts_interfaces/requests.js +1 -1
- package/docs/authswitch.md +20 -1
- package/package.json +3 -3
- package/readme.md +112 -26
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/aglapi/aglapisurface.document.ts +196 -16
- package/ts/classes.accountsactions.ts +30 -3
- package/ts/classes.claudeharness.ts +27 -1
- package/ts/classes.codexclientadapter.ts +14 -1
- package/ts/classes.controller.ts +1294 -246
- package/ts/classes.controllerdevices.ts +6 -0
- package/ts/classes.inbox.ts +239 -0
- package/ts/classes.inferencerouter.ts +15 -2
- package/ts/classes.inferenceservice.ts +1 -1
- package/ts/classes.mcpclient.ts +24 -3
- package/ts/classes.mcpcontrollerhost.ts +2 -0
- package/ts/classes.mcptools.ts +12 -7
- package/ts/classes.mcpvalidation.ts +27 -1
- package/ts/classes.nativesessions.ts +50 -12
- package/ts/classes.opencodeclient.ts +55 -4
- package/ts/classes.publishedset.ts +56 -0
- package/ts/classes.sessionfeed.ts +17 -20
- package/ts/classes.systemmetricssampler.ts +13 -2
- package/ts/classes.ticketstore.ts +73 -12
- package/ts/driver/classes.directreports.ts +25 -6
- package/ts/driver/classes.driverservice.ts +75 -14
- package/ts/fleet/classes.claudeworkers.ts +112 -39
- package/ts/fleet/classes.codexworkers.ts +21 -6
- package/ts/fleet/classes.workercommand.ts +14 -4
- package/ts/fleet/classes.workers.ts +9 -2
- package/ts/fleet/functions.unbounddrivers.ts +1 -1
- package/ts/fleet/functions.workervalidation.ts +1 -1
- package/ts/fleet/interfaces.workers.ts +20 -5
- package/ts/functions.agentmessage.ts +3 -0
- package/ts/functions.deviceapi.ts +1071 -34
- package/ts/functions.fleetworkercli.ts +10 -7
- package/ts/functions.inferencecli.ts +2 -0
- package/ts/functions.mcpfleetworkertools.ts +1 -1
- package/ts/functions.ticketvalidation.ts +11 -1
- package/ts/interfaces.auth.ts +8 -0
- package/ts/plugins.ts +7 -0
- package/ts_authswitch/authority-contract.ts +20 -0
- package/ts_authswitch/authority-overview.ts +28 -6
- package/ts_authswitch/authority-placement.ts +25 -17
- package/ts_authswitch/authority-renewal.ts +64 -0
- package/ts_authswitch/classes.authoritybroker.ts +34 -1
- package/ts_authswitch/classes.authoritycli.ts +49 -2
- package/ts_authswitch/classes.authorityclient.ts +14 -4
- package/ts_authswitch/classes.authoritydaemon.ts +9 -1
- package/ts_authswitch/classes.authoritydatabase.ts +5 -1
- package/ts_authswitch/classes.authoritymodels.ts +23 -3
- package/ts_authswitch/native-contracts.md +21 -0
- package/ts_authswitch/readme.md +1 -1
- package/ts_interfaces/devices.schema.json +3573 -603
- package/ts_interfaces/devices.ts +1093 -4
- package/ts_interfaces/fleetworkers.ts +5 -3
- package/ts_interfaces/inbox.ts +287 -0
- package/ts_interfaces/index.ts +1 -0
- package/ts_interfaces/interfaces.ts +9 -2
- package/ts_interfaces/mcprequests.ts +43 -4
- package/ts_interfaces/mcpworkspacerequests.ts +4 -2
- package/ts_interfaces/nativesessions.ts +17 -3
- package/ts_interfaces/plugins.ts +3 -1
- package/ts_interfaces/requests.accounts.ts +20 -0
- package/ts_interfaces/requests.ts +4 -0
- package/ts_web/00_commitinfo_data.ts +1 -1
- package/ts_web/accounts.plugins.ts +4 -2
- package/ts_web/classes.accountsreadmodel.ts +33 -0
- package/ts_web/classes.browserpresentation.ts +10 -5
- package/ts_web/classes.inboxfeed.ts +329 -0
- package/ts_web/classes.nativesessionstate.ts +9 -23
- package/ts_web/elements.accounts.ts +81 -0
- package/ts_web/elements.deployments.ts +66 -2
- package/ts_web/elements.harnesscontrollerapp.ts +107 -37
- package/ts_web/elements.inbox.ts +808 -0
- package/ts_web/functions.inbox.ts +95 -0
- package/ts_web/functions.sessioncards.ts +40 -0
- package/ts_web/plugins.ts +2 -0
|
@@ -1,42 +1,129 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import * as plugins from './plugins.js';
|
|
2
|
+
import {
|
|
3
|
+
controllerOperationFailedErrorCode,
|
|
4
|
+
proxyCapacityProviders,
|
|
5
|
+
type IControllerAuthorityProjection,
|
|
6
|
+
type IControllerDevice,
|
|
7
|
+
type IControllerDriverStatus,
|
|
8
|
+
type IControllerEvent,
|
|
9
|
+
type IControllerFleetWorkerView,
|
|
10
|
+
type IControllerGatewayStatus,
|
|
11
|
+
type IControllerInboxDeployGo,
|
|
12
|
+
type IControllerInboxSnapshot,
|
|
13
|
+
type IControllerMessageBundle,
|
|
14
|
+
type IControllerPermission,
|
|
15
|
+
type IControllerQuestion,
|
|
16
|
+
type IControllerRuntimeId,
|
|
17
|
+
type IControllerSessionFeedRow,
|
|
18
|
+
type IDirectReportView,
|
|
19
|
+
type IFleetDeployPlan,
|
|
20
|
+
type IFleetDeployStatus,
|
|
21
|
+
type IFleetDeployTargetStatus,
|
|
22
|
+
type IFleetDeployTicketPlacement,
|
|
23
|
+
type IProxyCapacityAnswer,
|
|
24
|
+
type IProxyCapacityMember,
|
|
25
|
+
type ITicket,
|
|
26
|
+
type ITicketBoard,
|
|
27
|
+
type ITicketComment,
|
|
28
|
+
type ITicketFilters,
|
|
29
|
+
type ITicketPerson,
|
|
30
|
+
type ITicketQuestion,
|
|
31
|
+
type TControllerAccountLogin,
|
|
32
|
+
type TControllerAccountsAnswer,
|
|
33
|
+
type TControllerAccountUsage,
|
|
34
|
+
type TControllerAuthorityDaemonState,
|
|
35
|
+
type TControllerElicitation,
|
|
36
|
+
type TControllerDriverErrorCode,
|
|
37
|
+
type TControllerEventType,
|
|
38
|
+
type TControllerGatewayState,
|
|
39
|
+
type TControllerInboxDelta,
|
|
40
|
+
type TControllerInboxItem,
|
|
41
|
+
type TControllerInboxWhere,
|
|
42
|
+
type TControllerSessionFeedDelta,
|
|
11
43
|
} from '../ts_interfaces/index.js';
|
|
12
44
|
import {
|
|
13
45
|
deviceLabelMaximumCharacters,
|
|
14
46
|
deviceMessageMaximumBytes,
|
|
47
|
+
deviceTicketDiscussionPage,
|
|
48
|
+
deviceTicketRefusalCodes,
|
|
49
|
+
type IDeviceApiKeyGroup,
|
|
50
|
+
type IDeviceDeployGo,
|
|
51
|
+
type IDeviceDeployPlan,
|
|
52
|
+
type IDeviceDeployTarget,
|
|
53
|
+
type IDeviceDirectReport,
|
|
54
|
+
type IDeviceDriverStatus,
|
|
55
|
+
type IDeviceHostAgl,
|
|
56
|
+
type IDeviceHostApiKeys,
|
|
57
|
+
type IDeviceHostDeploys,
|
|
58
|
+
type IDeviceHostDevice,
|
|
59
|
+
type IDeviceHostPools,
|
|
60
|
+
type IDeviceHostUnavailable,
|
|
61
|
+
type IDevicePoolMember,
|
|
62
|
+
type IDeviceInboxDelta,
|
|
15
63
|
type IDeviceMessage,
|
|
16
64
|
type IDevicePermission,
|
|
65
|
+
type IDeviceTicketQuestion,
|
|
66
|
+
type IReq_DeviceInboxList,
|
|
67
|
+
type IReq_DeviceTicketAnswer,
|
|
68
|
+
type TDeviceInboxAnswer,
|
|
69
|
+
type TDeviceInboxItem,
|
|
70
|
+
type TDeviceInboxWhere,
|
|
17
71
|
type IDeviceQuestion,
|
|
18
72
|
type IDeviceSessionFeedDelta,
|
|
19
73
|
type IDeviceSessionRow,
|
|
74
|
+
type IDeviceTicket,
|
|
75
|
+
type IDeviceTicketCard,
|
|
76
|
+
type IDeviceTicketComment,
|
|
77
|
+
type IDeviceTicketPerson,
|
|
78
|
+
type IDeviceTicketPlacement,
|
|
79
|
+
type IDeviceTicketPlans,
|
|
20
80
|
type IReq_DeviceAuthChallenge,
|
|
21
81
|
type IReq_DeviceAuthProve,
|
|
82
|
+
type IReq_DeviceBoardList,
|
|
83
|
+
type IReq_DeviceDirectReportMessage,
|
|
84
|
+
type IReq_DeviceDriverRead,
|
|
85
|
+
type IReq_DeviceDriverSend,
|
|
22
86
|
type IReq_DeviceElicitationReply,
|
|
87
|
+
type IReq_DeviceHostGet,
|
|
23
88
|
type IReq_DevicePair,
|
|
24
89
|
type IReq_DevicePermissionReply,
|
|
25
90
|
type IReq_DeviceQuestionReply,
|
|
26
91
|
type IReq_DeviceSessionRead,
|
|
27
92
|
type IReq_DeviceSessionSend,
|
|
93
|
+
type IReq_DeviceTicketAsk,
|
|
94
|
+
type IReq_DeviceTicketComment,
|
|
95
|
+
type IReq_DeviceTicketGet,
|
|
96
|
+
type IReq_DeviceTicketMove,
|
|
97
|
+
type TDeviceAccountProvider,
|
|
98
|
+
type TDeviceAssigneeFilter,
|
|
28
99
|
type TDeviceEvent,
|
|
29
100
|
type TDeviceElicitation,
|
|
101
|
+
type TDeviceGatewayState,
|
|
102
|
+
type TDeviceHostUnavailableReason,
|
|
103
|
+
type TDeviceRefusalCode,
|
|
30
104
|
type TDeviceMessagePart,
|
|
105
|
+
type TDeviceTicketColumn,
|
|
106
|
+
type TDeviceTicketRefusalCode,
|
|
31
107
|
} from '../ts_interfaces/devices.js';
|
|
32
108
|
import { isControllerSessionFeedRowId } from '../ts_interfaces/sessionfeed.js';
|
|
33
|
-
import { assertElicitationReply, assertKeys, assertObject, assertQuestionAnswers } from './classes.validation.js';
|
|
109
|
+
import { assertDirectReportMessageRequest, assertElicitationReply, assertKeys, assertObject, assertQuestionAnswers } from './classes.validation.js';
|
|
34
110
|
import { sessionFeedRowId } from './classes.sessionfeed.js';
|
|
111
|
+
import { directReportReachable, directReportRuntimeId } from './driver/classes.directreports.js';
|
|
112
|
+
import { DriverError, type DriverService, type IDriverNeedsInput } from './driver/classes.driverservice.js';
|
|
113
|
+
import { deviceRefusal } from './classes.controllerdevices.js';
|
|
114
|
+
import { controllerFailureJournal } from './classes.failurejournal.js';
|
|
115
|
+
import { DeployError } from './fleet/classes.deploymonitor.js';
|
|
116
|
+
import { AglProxyOutdatedError, AglProxyRefusedError, AglProxyUnavailableError } from './classes.proxycontrolclient.js';
|
|
117
|
+
import { TicketError, ticketBoardNarrows, ticketFilter, type ITicketBoardNarrowing, type TTicketErrorCode } from './classes.ticketstore.js';
|
|
118
|
+
import { assertTicketRequest } from './functions.ticketvalidation.js';
|
|
119
|
+
import { ticketColumns, ticketAutoLabels, type ticketErrorMessages } from '../ts_interfaces/tickets.js';
|
|
35
120
|
|
|
36
121
|
/*
|
|
37
122
|
* The device API's requests and projections: what a device may ask, checked before anything is read, and the reduced
|
|
38
123
|
* shapes of what it gets. A device sees the session feed without host paths, tickets or terminals, transcripts with
|
|
39
|
-
* tool calls reduced to name, title and status,
|
|
124
|
+
* tool calls reduced to name, title and status, the driver's status without its project or session, direct reports
|
|
125
|
+
* by name, the host's status without anything that names an account, a key or a probe's address, the shared board
|
|
126
|
+
* with people by name (the owner as `owner`) and sessions only as the feed lists them, and an allowlist of events.
|
|
40
127
|
*/
|
|
41
128
|
|
|
42
129
|
/** The controller events a device receives, each projected by `deviceEvent`; every other event never reaches one. */
|
|
@@ -48,8 +135,418 @@ export const deviceRelayedEventTypes: ReadonlySet<TControllerEventType> = new Se
|
|
|
48
135
|
'permissions.changed',
|
|
49
136
|
'session.text.updated',
|
|
50
137
|
'session.text.delta',
|
|
138
|
+
'inbox.changed',
|
|
139
|
+
'driver.changed',
|
|
140
|
+
'directreports.changed',
|
|
141
|
+
'fleet.workers.changed',
|
|
142
|
+
'tickets.changed',
|
|
143
|
+
'deployments.changed',
|
|
144
|
+
'accounts.changed',
|
|
51
145
|
]);
|
|
52
146
|
|
|
147
|
+
/** Every refusal code of the device contract, once: what a device request may be refused with as it is. */
|
|
148
|
+
const deviceRefusalCodes: ReadonlySet<string> = new Set([...Object.keys({
|
|
149
|
+
device_unknown: true,
|
|
150
|
+
device_pairing_invalid: true,
|
|
151
|
+
device_pairing_unavailable: true,
|
|
152
|
+
device_limit: true,
|
|
153
|
+
device_already_paired: true,
|
|
154
|
+
device_proof_invalid: true,
|
|
155
|
+
device_challenge_invalid: true,
|
|
156
|
+
device_authentication_required: true,
|
|
157
|
+
session_not_found: true,
|
|
158
|
+
session_read_only: true,
|
|
159
|
+
driver_absent: true,
|
|
160
|
+
direct_report_failed: true,
|
|
161
|
+
direct_report_busy: true,
|
|
162
|
+
rate_limited: true,
|
|
163
|
+
invalid_request: true,
|
|
164
|
+
controller_unavailable: true,
|
|
165
|
+
operation_limit: true,
|
|
166
|
+
} satisfies Record<Exclude<TDeviceRefusalCode, TDeviceTicketRefusalCode>, true>), ...deviceTicketRefusalCodes]);
|
|
167
|
+
|
|
168
|
+
/** The code a typed refusal carries, if any. */
|
|
169
|
+
const typedResponseErrorCode = (errorArg: unknown): unknown => (
|
|
170
|
+
errorArg instanceof plugins.typedrequest.TypedResponseError ? (errorArg.errorData as { code?: unknown } | undefined)?.code : undefined
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
/** How a driver error reaches a device: as a code of the device contract, never as the browser's own. */
|
|
174
|
+
const deviceDriverErrorRefusals: Record<TControllerDriverErrorCode, TDeviceRefusalCode> = {
|
|
175
|
+
// The Flex harness or the configuration authority is not available: a state of the controller, as for any request.
|
|
176
|
+
DRIVER_UNAVAILABLE: 'controller_unavailable',
|
|
177
|
+
// No driver project, so no driver session; a device never creates one.
|
|
178
|
+
DRIVER_PROJECT_REQUIRED: 'driver_absent',
|
|
179
|
+
// An operation the driver session never takes: a device asks none.
|
|
180
|
+
DRIVER_SESSION_PROTECTED: 'invalid_request',
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Runs a device's driver request and answers its failures in the device contract: a driver error by its device code,
|
|
185
|
+
* and a driver project or session that went away after the status named it as `driver_absent`. Every other failure is
|
|
186
|
+
* the session read's or send's own, as `device.session.read` and `device.session.send` answer it.
|
|
187
|
+
*/
|
|
188
|
+
export const serveDeviceDriver = async <T>(operationArg: () => Promise<T>): Promise<T> => {
|
|
189
|
+
try {
|
|
190
|
+
return await operationArg();
|
|
191
|
+
} catch (errorArg) {
|
|
192
|
+
if (errorArg instanceof DriverError) throw deviceRefusal(deviceDriverErrorRefusals[errorArg.code], errorArg.message);
|
|
193
|
+
const code = typedResponseErrorCode(errorArg);
|
|
194
|
+
if (code === 'project_not_found' || code === 'session_not_found') {
|
|
195
|
+
throw deviceRefusal('driver_absent', 'The driver session is gone; the owner starts the driver in the browser.');
|
|
196
|
+
}
|
|
197
|
+
throw errorArg;
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* A failure of the owner's message to a direct report, in the device contract: another Codex client's turn is
|
|
203
|
+
* `direct_report_busy` (a device reads no turn to name), the agent message rate `rate_limited` (final, as for every
|
|
204
|
+
* device request), and every other refusal outside the contract `direct_report_failed` with its text and the failure
|
|
205
|
+
* journal's reference, where AGL kept the cause. Other failures are answered as `serveDirectReport` answers them.
|
|
206
|
+
*/
|
|
207
|
+
export const deviceDirectReportMessageRefusal = (errorArg: unknown): unknown => {
|
|
208
|
+
if (!(errorArg instanceof plugins.typedrequest.TypedResponseError)) return errorArg;
|
|
209
|
+
const code = typedResponseErrorCode(errorArg);
|
|
210
|
+
if (code === 'delivery_unsupported') {
|
|
211
|
+
return deviceRefusal('direct_report_busy', 'Another Codex client runs this report\'s turn, which a device does not steer; nothing was sent. Send it again once the turn ended.');
|
|
212
|
+
}
|
|
213
|
+
if (code === 'message_rate_limited') return deviceRefusal('rate_limited', errorArg.errorText);
|
|
214
|
+
if (typeof code === 'string' && deviceRefusalCodes.has(code)) return errorArg;
|
|
215
|
+
const reference = code === controllerOperationFailedErrorCode ? (errorArg.errorData as { reference?: unknown }).reference : undefined;
|
|
216
|
+
return deviceRefusal('direct_report_failed', `${errorArg.errorText}${typeof reference === 'string' ? ` (ref ${reference})` : ''}`.slice(0, 4096));
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* How the ticket store's refusals outside `ticketErrorMessages` reach a device: a concurrent change the transaction lost
|
|
221
|
+
* wrote nothing and reads as the board having changed meanwhile; a commit whose outcome is unknown is a state of the
|
|
222
|
+
* controller (AGL-1670 gives it a code of its own, which a client never resends); a fleet event's conflict and an owner
|
|
223
|
+
* capability the controller could not verify answer no device request.
|
|
224
|
+
*/
|
|
225
|
+
const deviceTicketStoreRefusals: Readonly<Record<Exclude<TTicketErrorCode, keyof typeof ticketErrorMessages>, TDeviceRefusalCode>> = {
|
|
226
|
+
concurrent_change: 'ticket_revision_conflict',
|
|
227
|
+
outcome_unknown: 'controller_unavailable',
|
|
228
|
+
ticket_fleet_event_conflict: 'invalid_request',
|
|
229
|
+
unauthorized: 'invalid_request',
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
const deviceTicketCodes: ReadonlySet<string> = new Set(deviceTicketRefusalCodes);
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* A refusal of a device's ticket request in the device contract, whether the store's own (`TicketError`) or as the
|
|
236
|
+
* browser's ticket functions answer it: the store's codes `deviceTicketRefusalCodes` lists as they are, with the
|
|
237
|
+
* store's reason, and its other codes by `deviceTicketStoreRefusals`. Every other failure is answered as any device
|
|
238
|
+
* request's.
|
|
239
|
+
*/
|
|
240
|
+
export const deviceTicketRefusal = (errorArg: unknown): unknown => {
|
|
241
|
+
const [code, text] = errorArg instanceof TicketError ? [errorArg.code, errorArg.message]
|
|
242
|
+
: errorArg instanceof plugins.typedrequest.TypedResponseError ? [typedResponseErrorCode(errorArg), errorArg.errorText] : [undefined, ''];
|
|
243
|
+
if (typeof code !== 'string') return errorArg;
|
|
244
|
+
if (deviceTicketCodes.has(code)) {
|
|
245
|
+
return errorArg instanceof TicketError ? deviceRefusal(code as TDeviceTicketRefusalCode, text) : errorArg;
|
|
246
|
+
}
|
|
247
|
+
return Object.hasOwn(deviceTicketStoreRefusals, code)
|
|
248
|
+
? deviceRefusal(deviceTicketStoreRefusals[code as keyof typeof deviceTicketStoreRefusals], text) : errorArg;
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
/** What a device event reads of the driver as it is sent: the controller's `DriverService`. */
|
|
252
|
+
export type TDeviceDriverSource = Pick<DriverService, 'isSessionChange' | 'current' | 'needsInput'>;
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* The driver's status as a device sees it: no project, no session, no failure or limit text. The `needs input:` line
|
|
256
|
+
* only while it is what the status's `needs_input` stands for.
|
|
257
|
+
*/
|
|
258
|
+
export const deviceDriverStatus = (statusArg: IControllerDriverStatus, needsInputArg: IDriverNeedsInput | undefined): IDeviceDriverStatus => ({
|
|
259
|
+
activity: statusArg.activity,
|
|
260
|
+
unreadCount: statusArg.unreadCount,
|
|
261
|
+
...(statusArg.session?.title ? { title: statusArg.session.title } : {}),
|
|
262
|
+
...(statusArg.activity === 'needs_input' && needsInputArg !== undefined && needsInputArg.nativeId === statusArg.sessionId?.nativeId
|
|
263
|
+
? { needsInput: needsInputArg.line } : {}),
|
|
264
|
+
updatedAt: statusArg.updatedAt,
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* A direct report as a device sees it, with the fleet workers that report to it: named, never by the report's stored
|
|
269
|
+
* id (which holds the configuration authority's id); the workers without their directories or profiles.
|
|
270
|
+
*/
|
|
271
|
+
export const deviceDirectReport = (viewArg: IDirectReportView, workersArg: readonly IControllerFleetWorkerView[]): IDeviceDirectReport => {
|
|
272
|
+
const { report } = viewArg;
|
|
273
|
+
return {
|
|
274
|
+
name: report.name,
|
|
275
|
+
kind: report.session.kind,
|
|
276
|
+
harness: directReportRuntimeId(report).harnessId,
|
|
277
|
+
...(viewArg.status === undefined ? {} : { status: viewArg.status }),
|
|
278
|
+
sessionId: report.sessionId,
|
|
279
|
+
projects: [...report.projects],
|
|
280
|
+
ticketKey: report.driverTicketKey,
|
|
281
|
+
...(report.latestResult === undefined ? {} : { latestResult: { text: report.latestResult.text, at: Date.parse(report.latestResult.at) } }),
|
|
282
|
+
openQuestions: viewArg.openQuestions.map(({ ticketKey, project, question }) => ({ ticketKey, project, question: deviceTicketQuestion(question) })),
|
|
283
|
+
openQuestionCount: viewArg.openQuestionCount,
|
|
284
|
+
workers: workersArg.map(({ worker, status }) => ({
|
|
285
|
+
name: worker.name,
|
|
286
|
+
harness: worker.harness,
|
|
287
|
+
...(status === undefined ? {} : { status }),
|
|
288
|
+
...(worker.ticketKey === undefined ? {} : { ticketKey: worker.ticketKey }),
|
|
289
|
+
})),
|
|
290
|
+
messageable: directReportReachable(report),
|
|
291
|
+
};
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
/** Where the host's route on its gateway stands, in the device contract's words. */
|
|
295
|
+
const deviceGatewayStates: Readonly<Record<Exclude<TControllerGatewayState, 'not_enrolled'>, TDeviceGatewayState>> = {
|
|
296
|
+
finalize_pending: 'finalize_pending',
|
|
297
|
+
syncing: 'syncing',
|
|
298
|
+
synced: 'synced',
|
|
299
|
+
retrying: 'retrying',
|
|
300
|
+
waiting: 'waiting',
|
|
301
|
+
failed: 'failed',
|
|
302
|
+
stopped: 'stopped',
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* The AGL that runs, as a device sees it: its release, its start, its public origin, and how a device reaches it --
|
|
307
|
+
* through the gateway AGL keeps its route on, with where that route's sync stands (never the gateway's address, the
|
|
308
|
+
* client AGL is there or the route's target), or through the owner's own reverse proxy while AGL is enrolled with no
|
|
309
|
+
* gateway.
|
|
310
|
+
*/
|
|
311
|
+
export const deviceHostAgl = (aglArg: {
|
|
312
|
+
version: string;
|
|
313
|
+
startedAt: number;
|
|
314
|
+
origin: string;
|
|
315
|
+
gateway: IControllerGatewayStatus | undefined;
|
|
316
|
+
}): IDeviceHostAgl => ({
|
|
317
|
+
version: aglArg.version,
|
|
318
|
+
startedAt: aglArg.startedAt,
|
|
319
|
+
origin: aglArg.origin,
|
|
320
|
+
reach: aglArg.gateway === undefined || aglArg.gateway.state === 'not_enrolled'
|
|
321
|
+
? { kind: 'proxy' }
|
|
322
|
+
: { kind: 'gateway', state: deviceGatewayStates[aglArg.gateway.state] },
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
/** A device's own record as it reads it: no id, key or fingerprint, and nothing it could change. */
|
|
326
|
+
export const deviceHostDevice = (deviceArg: IControllerDevice): IDeviceHostDevice => ({
|
|
327
|
+
name: deviceArg.name,
|
|
328
|
+
model: deviceArg.model,
|
|
329
|
+
pairedAt: deviceArg.pairedAt,
|
|
330
|
+
...(deviceArg.lastSeenAt === undefined ? {} : { lastSeenAt: deviceArg.lastSeenAt }),
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* A planned deploy as a device sees it: its ticket, the release and the owner's go, and the window it is queued into by
|
|
335
|
+
* its times alone. Never its question or who deploys it, a session of the host.
|
|
336
|
+
*/
|
|
337
|
+
const deviceDeployPlan = (planArg: IFleetDeployPlan): IDeviceDeployPlan => ({
|
|
338
|
+
ticketKey: planArg.key,
|
|
339
|
+
title: planArg.title,
|
|
340
|
+
version: planArg.version,
|
|
341
|
+
go: planArg.go,
|
|
342
|
+
...(planArg.window === undefined ? {} : { window: { from: Date.parse(planArg.window.from), to: Date.parse(planArg.window.to) } }),
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* A deploy target as a device sees it: what it runs, whether its probe answers, the releases it does not run and its
|
|
347
|
+
* planned deploys, never the probe's address, the owner's notes on how it deploys and what a deploy interrupts, or a
|
|
348
|
+
* failed probe's text. Its planned deploys are tickets, so `tickets.changed` tells a device of them as well as
|
|
349
|
+
* `deployments.changed`.
|
|
350
|
+
*/
|
|
351
|
+
const deviceDeployTarget = (statusArg: IFleetDeployTargetStatus): IDeviceDeployTarget => {
|
|
352
|
+
const { target, running, probe, latest } = statusArg;
|
|
353
|
+
return {
|
|
354
|
+
id: target.id,
|
|
355
|
+
...(target.name === undefined ? {} : { name: target.name }),
|
|
356
|
+
...(target.environment === undefined ? {} : { environment: target.environment }),
|
|
357
|
+
repo: target.repo,
|
|
358
|
+
...(running === undefined ? {} : {
|
|
359
|
+
running: {
|
|
360
|
+
version: running.version,
|
|
361
|
+
since: Date.parse(running.since),
|
|
362
|
+
...(running.previousVersion === undefined ? {} : { previousVersion: running.previousVersion }),
|
|
363
|
+
rolledBack: running.rolledBack === true,
|
|
364
|
+
untagged: running.untagged === true,
|
|
365
|
+
},
|
|
366
|
+
}),
|
|
367
|
+
probe: probe.state,
|
|
368
|
+
...(probe.checkedAt === undefined ? {} : { probedAt: Date.parse(probe.checkedAt) }),
|
|
369
|
+
...(probe.downSince === undefined ? {} : { downSince: Date.parse(probe.downSince) }),
|
|
370
|
+
...(latest === undefined ? {} : { latest: latest.version }),
|
|
371
|
+
undeployed: statusArg.notRunning.releases.map((releaseArg) => releaseArg.version),
|
|
372
|
+
deployWhen: statusArg.deployWhen,
|
|
373
|
+
plans: statusArg.deployPlans.map(deviceDeployPlan),
|
|
374
|
+
};
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
/** The deploy monitor's status of every target, as a device sees it. */
|
|
378
|
+
const deviceHostDeploys = (statusArg: IFleetDeployStatus): IDeviceHostDeploys => ({
|
|
379
|
+
state: 'ready',
|
|
380
|
+
checkedAt: Date.parse(statusArg.checkedAt),
|
|
381
|
+
targets: statusArg.targets.map(deviceDeployTarget),
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
/** A pool's account as a device sees it: its plan, its state and the window its room is bound by, never its id. */
|
|
385
|
+
const devicePoolMember = (memberArg: IProxyCapacityMember): IDevicePoolMember => ({
|
|
386
|
+
plan: memberArg.plan,
|
|
387
|
+
state: memberArg.state,
|
|
388
|
+
...(memberArg.binding === null ? {} : {
|
|
389
|
+
window: {
|
|
390
|
+
name: memberArg.binding.name,
|
|
391
|
+
usedPercent: memberArg.binding.usedPercent,
|
|
392
|
+
estimated: memberArg.binding.estimated,
|
|
393
|
+
...(memberArg.binding.resetAt === null ? {} : { resetAt: Date.parse(memberArg.binding.resetAt) }),
|
|
394
|
+
},
|
|
395
|
+
}),
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* A pool's accounts in the order a device lists them: by plan, then the most used first (one without a window last),
|
|
400
|
+
* then by everything else it shows. The order follows only what the device sees; the proxy's own order follows its
|
|
401
|
+
* accounts, so a position there would stand in for an account.
|
|
402
|
+
*/
|
|
403
|
+
const devicePoolMemberOrder = (leftArg: IDevicePoolMember, rightArg: IDevicePoolMember): number => (
|
|
404
|
+
leftArg.plan.localeCompare(rightArg.plan, 'en')
|
|
405
|
+
|| (rightArg.window?.usedPercent ?? -1) - (leftArg.window?.usedPercent ?? -1)
|
|
406
|
+
|| leftArg.state.localeCompare(rightArg.state, 'en')
|
|
407
|
+
|| (leftArg.window?.name ?? '').localeCompare(rightArg.window?.name ?? '', 'en')
|
|
408
|
+
|| Number(leftArg.window?.estimated ?? false) - Number(rightArg.window?.estimated ?? false)
|
|
409
|
+
|| (leftArg.window?.resetAt ?? 0) - (rightArg.window?.resetAt ?? 0)
|
|
410
|
+
);
|
|
411
|
+
|
|
412
|
+
/** The inference proxy's pools as a device sees them: anonymous accounts, and times as Unix milliseconds. */
|
|
413
|
+
const deviceHostPools = (answerArg: IProxyCapacityAnswer): IDeviceHostPools => ({
|
|
414
|
+
state: 'ready',
|
|
415
|
+
pools: answerArg.pools.map((poolArg) => ({
|
|
416
|
+
provider: poolArg.provider,
|
|
417
|
+
verdict: poolArg.verdict.state === 'open'
|
|
418
|
+
? { state: 'open' }
|
|
419
|
+
: { state: 'wait', reason: poolArg.verdict.reason, ...(poolArg.verdict.until === null ? {} : { until: Date.parse(poolArg.verdict.until) }) },
|
|
420
|
+
room: poolArg.room,
|
|
421
|
+
members: poolArg.members.map(devicePoolMember).sort(devicePoolMemberOrder),
|
|
422
|
+
})),
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* The provider of an API key's login, or none for a sign-in. Every login purpose is named, so a new one is decided
|
|
427
|
+
* here rather than counted by default.
|
|
428
|
+
*/
|
|
429
|
+
const apiKeyProviders: Readonly<Record<TControllerAccountLogin['purpose'], TDeviceAccountProvider | null>> = {
|
|
430
|
+
openai_managed: null,
|
|
431
|
+
claude_host_native: null,
|
|
432
|
+
claude_container_setup: null,
|
|
433
|
+
opencode_native: null,
|
|
434
|
+
anthropic_api_key: 'anthropic',
|
|
435
|
+
openai_api_key: 'openai',
|
|
436
|
+
};
|
|
437
|
+
|
|
438
|
+
/** Why the accounts service cannot be asked, in the device contract's words. */
|
|
439
|
+
const deviceAuthorityUnavailable: Readonly<Record<Exclude<TControllerAuthorityDaemonState, 'ready'>, TDeviceHostUnavailableReason>> = {
|
|
440
|
+
absent: 'not_running',
|
|
441
|
+
connecting: 'not_running',
|
|
442
|
+
incompatible: 'outdated',
|
|
443
|
+
outdated: 'outdated',
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
/** What `device.host.get` reads besides the AGL and the device, each section from its owner. */
|
|
447
|
+
export interface IDeviceHostSources {
|
|
448
|
+
/** The deploy monitor's status of every target, as the Deployments view reads it; never a probe. */
|
|
449
|
+
deployStatus: () => Promise<IFleetDeployStatus>;
|
|
450
|
+
/** The inference proxy's pools; absent while no proxy runs here. */
|
|
451
|
+
capacity: ((signalArg: AbortSignal) => Promise<IProxyCapacityAnswer>) | undefined;
|
|
452
|
+
/** The accounts service as the controller holds it. */
|
|
453
|
+
authority: () => IControllerAuthorityProjection;
|
|
454
|
+
/** The usage the accounts service holds for one login; it reads no provider for it. */
|
|
455
|
+
usage: (accountIdArg: string, loginIdArg: string, signalArg: AbortSignal) => Promise<TControllerAccountsAnswer<TControllerAccountUsage>>;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* The host's API keys, counted per provider against their monthly spend limits by the rule the proxy places by
|
|
460
|
+
* (`authSwitchSpendPercent` is null without a limit; `authSwitchSpendExhausted` holds a key at it), from the month the
|
|
461
|
+
* accounts service holds for each key's login: never a label, an email, an account or key id, or a dollar amount. A
|
|
462
|
+
* key whose month was not read counts as `unread`.
|
|
463
|
+
*/
|
|
464
|
+
const readDeviceApiKeys = async (sourcesArg: Pick<IDeviceHostSources, 'authority' | 'usage'>, nowArg: number,
|
|
465
|
+
signalArg: AbortSignal): Promise<IDeviceHostApiKeys | IDeviceHostUnavailable> => {
|
|
466
|
+
const authority = sourcesArg.authority();
|
|
467
|
+
if (authority.state !== 'ready') return { state: 'unavailable', reason: deviceAuthorityUnavailable[authority.state] };
|
|
468
|
+
const snapshot = authority.snapshot;
|
|
469
|
+
if (snapshot === null) return { state: 'unavailable', reason: 'not_running' };
|
|
470
|
+
const keys = snapshot.accounts.flatMap((accountArg) => {
|
|
471
|
+
const provider = accountArg.removed ? undefined : snapshot.logins
|
|
472
|
+
.map((loginArg) => (loginArg.accountId === accountArg.id ? apiKeyProviders[loginArg.purpose] : null))
|
|
473
|
+
.find((providerArg) => providerArg !== null);
|
|
474
|
+
return provider ? [{ provider, accountId: accountArg.id, login: plugins.authswitchOverview.authSwitchUsageLogin(snapshot, accountArg.id) }] : [];
|
|
475
|
+
});
|
|
476
|
+
const months = await Promise.all(keys.map(async ({ provider, accountId, login }) => {
|
|
477
|
+
const answer = login === null ? undefined : await sourcesArg.usage(accountId, login.id, signalArg);
|
|
478
|
+
const reading = answer?.state === 'done' ? answer.value.reading : null;
|
|
479
|
+
return { provider, spend: reading?.kind === 'spend' ? reading.spend : null };
|
|
480
|
+
}));
|
|
481
|
+
const groups = new Map<TDeviceAccountProvider, IDeviceApiKeyGroup>();
|
|
482
|
+
for (const { provider, spend } of months) {
|
|
483
|
+
const group = groups.get(provider) ?? { provider, within: 0, over: 0, unlimited: 0, unread: 0 };
|
|
484
|
+
groups.set(provider, group);
|
|
485
|
+
if (spend === null) {
|
|
486
|
+
group.unread += 1;
|
|
487
|
+
} else if (plugins.authswitchSpend.authSwitchSpendPercent(spend) === null) {
|
|
488
|
+
group.unlimited += 1;
|
|
489
|
+
} else if (plugins.authswitchSpend.authSwitchSpendExhausted(spend, nowArg)) {
|
|
490
|
+
group.over += 1;
|
|
491
|
+
const resetAt = Date.parse(spend.resetAt);
|
|
492
|
+
group.resetAt = group.resetAt === undefined ? resetAt : Math.min(group.resetAt, resetAt);
|
|
493
|
+
} else {
|
|
494
|
+
group.within += 1;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
return { state: 'ready', groups: proxyCapacityProviders.flatMap((providerArg) => groups.get(providerArg) ?? []) };
|
|
498
|
+
};
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* Why a section's owner did not answer, when that is a state of the host rather than a fault: the configuration
|
|
502
|
+
* service the deploy monitor reads its targets from, or the inference proxy and the accounts service behind it.
|
|
503
|
+
*/
|
|
504
|
+
const deviceHostUnavailableReason = (errorArg: unknown): Exclude<TDeviceHostUnavailableReason, 'failed'> | undefined => {
|
|
505
|
+
if (errorArg instanceof DeployError) return errorArg.code === 'deploy_unavailable' ? 'not_running' : undefined;
|
|
506
|
+
if (errorArg instanceof AglProxyUnavailableError) return 'not_running';
|
|
507
|
+
if (errorArg instanceof AglProxyRefusedError) return errorArg.code === 'authority_unavailable' ? 'not_running' : undefined;
|
|
508
|
+
if (errorArg instanceof AglProxyOutdatedError) return 'outdated';
|
|
509
|
+
return undefined;
|
|
510
|
+
};
|
|
511
|
+
|
|
512
|
+
/**
|
|
513
|
+
* Reads one section of a device's answer on its own (a section of the host's status, a ticket's placement): a state of
|
|
514
|
+
* the host is `unavailable` with its reason, and every other failure `failed`, its cause kept in the failure journal
|
|
515
|
+
* and the log under the reference the device gets. A request the device's connection or its revocation aborted fails
|
|
516
|
+
* as a whole. `operationArg` names the section in the journal, `whatArg` in the log.
|
|
517
|
+
*/
|
|
518
|
+
const readDeviceSection = async <T>(operationArg: string, whatArg: string, signalArg: AbortSignal,
|
|
519
|
+
readArg: () => Promise<T | IDeviceHostUnavailable>): Promise<T | IDeviceHostUnavailable> => {
|
|
520
|
+
try {
|
|
521
|
+
return await readArg();
|
|
522
|
+
} catch (errorArg) {
|
|
523
|
+
if (signalArg.aborted) throw errorArg;
|
|
524
|
+
const reason = deviceHostUnavailableReason(errorArg);
|
|
525
|
+
if (reason !== undefined) return { state: 'unavailable', reason };
|
|
526
|
+
const reference = controllerFailureJournal.record({ operation: operationArg, cause: errorArg });
|
|
527
|
+
console.error(`Reading ${whatArg} for a device failed (ref ${reference}):`, errorArg);
|
|
528
|
+
return { state: 'unavailable', reason: 'failed', reference };
|
|
529
|
+
}
|
|
530
|
+
};
|
|
531
|
+
|
|
532
|
+
const readDeviceHostSection = <T>(sectionArg: string, signalArg: AbortSignal,
|
|
533
|
+
readArg: () => Promise<T | IDeviceHostUnavailable>): Promise<T | IDeviceHostUnavailable> => (
|
|
534
|
+
readDeviceSection(`device.host.get ${sectionArg}`, `the host's ${sectionArg}`, signalArg, readArg)
|
|
535
|
+
);
|
|
536
|
+
|
|
537
|
+
/** The host's deploy targets, account pools and API keys, read at once and each on its own. */
|
|
538
|
+
export const readDeviceHostSections = async (sourcesArg: IDeviceHostSources, nowArg: number, signalArg: AbortSignal):
|
|
539
|
+
Promise<Pick<IReq_DeviceHostGet['response'], 'deploys' | 'pools' | 'apiKeys'>> => {
|
|
540
|
+
const [deploys, pools, apiKeys] = await Promise.all([
|
|
541
|
+
readDeviceHostSection<IDeviceHostDeploys>('deploys', signalArg, async () => deviceHostDeploys(await sourcesArg.deployStatus())),
|
|
542
|
+
readDeviceHostSection<IDeviceHostPools>('pools', signalArg, async () => (
|
|
543
|
+
sourcesArg.capacity === undefined ? { state: 'unavailable', reason: 'not_running' } : deviceHostPools(await sourcesArg.capacity(signalArg))
|
|
544
|
+
)),
|
|
545
|
+
readDeviceHostSection<IDeviceHostApiKeys>('apiKeys', signalArg, () => readDeviceApiKeys(sourcesArg, nowArg, signalArg)),
|
|
546
|
+
]);
|
|
547
|
+
return { deploys, pools, apiKeys };
|
|
548
|
+
};
|
|
549
|
+
|
|
53
550
|
/** A feed row as a device sees it; none for a terminal row, which a device does not open. */
|
|
54
551
|
export const deviceSessionRow = (rowArg: IControllerSessionFeedRow): IDeviceSessionRow | undefined => (
|
|
55
552
|
rowArg.open.kind === 'terminal' ? undefined : {
|
|
@@ -140,6 +637,363 @@ export const deviceElicitations = (elicitationsArg: readonly TControllerElicitat
|
|
|
140
637
|
}
|
|
141
638
|
});
|
|
142
639
|
|
|
640
|
+
/** A ticket question as a device answers it: its options by id, and who asked and answered by name. */
|
|
641
|
+
export const deviceTicketQuestion = (questionArg: ITicketQuestion): IDeviceTicketQuestion => ({
|
|
642
|
+
id: questionArg.id,
|
|
643
|
+
question: questionArg.question,
|
|
644
|
+
options: questionArg.options.map(({ id, label, description }) => ({ id, label, ...(description === undefined ? {} : { description }) })),
|
|
645
|
+
multiSelect: questionArg.multiSelect,
|
|
646
|
+
allowCustom: questionArg.allowCustom,
|
|
647
|
+
askedBy: questionArg.askedBy.name,
|
|
648
|
+
createdAt: Date.parse(questionArg.createdAt),
|
|
649
|
+
...(questionArg.answer === undefined ? {} : {
|
|
650
|
+
answer: {
|
|
651
|
+
choices: [...questionArg.answer.choices],
|
|
652
|
+
...(questionArg.answer.text === undefined ? {} : { text: questionArg.answer.text }),
|
|
653
|
+
answeredBy: questionArg.answer.answeredBy.name,
|
|
654
|
+
answeredAt: Date.parse(questionArg.answer.answeredAt),
|
|
655
|
+
},
|
|
656
|
+
}),
|
|
657
|
+
});
|
|
658
|
+
|
|
659
|
+
/**
|
|
660
|
+
* What a device's ticket views read besides the tickets: the owner's principal id (`owner:<webauthnUserId>`), which
|
|
661
|
+
* names the owner's account and never reaches a device, and the session feed's published rows.
|
|
662
|
+
*/
|
|
663
|
+
export interface IDeviceTicketViewer {
|
|
664
|
+
ownerId: string;
|
|
665
|
+
rowOf: (rowIdArg: string) => IControllerSessionFeedRow | undefined;
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
/**
|
|
669
|
+
* A person on a ticket as a device sees it: by name and kind, the owner as `kind` `owner` (never the owner's principal
|
|
670
|
+
* id), and a session only as the session feed names it while the feed lists it for devices, so never the driver's own
|
|
671
|
+
* session or one the feed does not list. A person's id never reaches a device.
|
|
672
|
+
*/
|
|
673
|
+
export const deviceTicketPerson = (personArg: ITicketPerson, viewerArg: IDeviceTicketViewer): IDeviceTicketPerson => {
|
|
674
|
+
const row = personArg.sessionId === undefined ? undefined : viewerArg.rowOf(personArg.sessionId);
|
|
675
|
+
const kind = personArg.id === viewerArg.ownerId ? 'owner' : personArg.kind;
|
|
676
|
+
return {
|
|
677
|
+
name: personArg.name,
|
|
678
|
+
...(kind === undefined ? {} : { kind }),
|
|
679
|
+
...(row !== undefined && row.open.kind !== 'terminal' ? { sessionId: row.id } : {}),
|
|
680
|
+
};
|
|
681
|
+
};
|
|
682
|
+
|
|
683
|
+
/** The epic a ticket belongs to, when `parentArg`, the ticket its `parentId` names, is one. */
|
|
684
|
+
const deviceTicketEpic = (ticketArg: ITicket, parentArg: ITicket | undefined): { key: string; title: string } | undefined => (
|
|
685
|
+
parentArg !== undefined && parentArg.kind === 'epic' && ticketArg.parentId === parentArg.key ? { key: parentArg.key, title: parentArg.title } : undefined
|
|
686
|
+
);
|
|
687
|
+
|
|
688
|
+
/** A ticket as a device's card shows it; `parentArg` is the ticket its `parentId` names, when read. */
|
|
689
|
+
export const deviceTicketCard = (ticketArg: ITicket, parentArg: ITicket | undefined, viewerArg: IDeviceTicketViewer): IDeviceTicketCard => {
|
|
690
|
+
const epic = deviceTicketEpic(ticketArg, parentArg);
|
|
691
|
+
return {
|
|
692
|
+
key: ticketArg.key,
|
|
693
|
+
title: ticketArg.title,
|
|
694
|
+
kind: ticketArg.kind,
|
|
695
|
+
project: ticketArg.project,
|
|
696
|
+
column: ticketArg.columnId,
|
|
697
|
+
priority: ticketArg.priority,
|
|
698
|
+
...(ticketArg.assignee === undefined ? {} : { assignee: deviceTicketPerson(ticketArg.assignee, viewerArg) }),
|
|
699
|
+
labels: [...ticketArg.labels],
|
|
700
|
+
autoLabels: [...(ticketArg.autoLabels ?? [])],
|
|
701
|
+
...(epic === undefined ? {} : { epic }),
|
|
702
|
+
updatedAt: Date.parse(ticketArg.updatedAt),
|
|
703
|
+
};
|
|
704
|
+
};
|
|
705
|
+
|
|
706
|
+
/** Every link a ticket holds, field by field. */
|
|
707
|
+
const deviceTicketLink = (linkArg: ITicket['links'][number]): IDeviceTicket['links'][number] => {
|
|
708
|
+
switch (linkArg.kind) {
|
|
709
|
+
case 'branch': return { kind: 'branch', repo: linkArg.repo, branch: linkArg.branch, sha: linkArg.sha };
|
|
710
|
+
case 'commit': return { kind: 'commit', repo: linkArg.repo, sha: linkArg.sha };
|
|
711
|
+
case 'release': return { kind: 'release', version: linkArg.version, ...(linkArg.package === undefined ? {} : { package: linkArg.package }),
|
|
712
|
+
...(linkArg.repo === undefined ? {} : { repo: linkArg.repo }), ...(linkArg.sha === undefined ? {} : { sha: linkArg.sha }) };
|
|
713
|
+
case 'affects': return { kind: 'affects', repo: linkArg.repo, version: linkArg.version };
|
|
714
|
+
}
|
|
715
|
+
};
|
|
716
|
+
|
|
717
|
+
/**
|
|
718
|
+
* A ticket as a device's detail shows it: its card, description, source words, evidence and parent; never its source
|
|
719
|
+
* attachments, its plans' questions, who created or last changed it, or its permissions.
|
|
720
|
+
*/
|
|
721
|
+
export const deviceTicket = (ticketArg: ITicket, parentArg: ITicket | undefined, viewerArg: IDeviceTicketViewer): IDeviceTicket => ({
|
|
722
|
+
...deviceTicketCard(ticketArg, parentArg, viewerArg),
|
|
723
|
+
description: ticketArg.description,
|
|
724
|
+
source: { kind: ticketArg.source.kind, text: ticketArg.source.text, by: deviceTicketPerson(ticketArg.source.actor, viewerArg) },
|
|
725
|
+
links: ticketArg.links.map(deviceTicketLink),
|
|
726
|
+
...(parentArg !== undefined && ticketArg.parentId === parentArg.key ? { parent: { key: parentArg.key, title: parentArg.title } } : {}),
|
|
727
|
+
revision: ticketArg.revision,
|
|
728
|
+
createdAt: Date.parse(ticketArg.createdAt),
|
|
729
|
+
});
|
|
730
|
+
|
|
731
|
+
export const deviceTicketComment = (commentArg: ITicketComment, viewerArg: IDeviceTicketViewer): IDeviceTicketComment => ({
|
|
732
|
+
id: commentArg.id,
|
|
733
|
+
author: deviceTicketPerson(commentArg.author, viewerArg),
|
|
734
|
+
createdAt: Date.parse(commentArg.createdAt),
|
|
735
|
+
body: commentArg.body.map((partArg) => (partArg.type === 'text'
|
|
736
|
+
? { type: 'text' as const, text: partArg.text } : { type: 'reference' as const, id: partArg.id, label: partArg.label })),
|
|
737
|
+
});
|
|
738
|
+
|
|
739
|
+
/** How the board's assignee filter of a device narrows the board: the owner by the owner's principal id. */
|
|
740
|
+
const deviceAssigneeNarrowing = (filterArg: TDeviceAssigneeFilter, ownerIdArg: string): NonNullable<ITicketBoardNarrowing['assignee']> => {
|
|
741
|
+
switch (filterArg.kind) {
|
|
742
|
+
case 'owner': return { kind: 'person', id: ownerIdArg };
|
|
743
|
+
case 'unassigned': return { kind: 'unassigned' };
|
|
744
|
+
case 'session': return { kind: 'session', sessionId: filterArg.sessionId };
|
|
745
|
+
}
|
|
746
|
+
};
|
|
747
|
+
|
|
748
|
+
/**
|
|
749
|
+
* One column of the board as `device.board.list` answers it, from one read of the whole board (`board`): the tasks the
|
|
750
|
+
* store's own filters (`ticketFilter`: text and project) and the board's narrowing (`ticketBoardNarrows`: epic,
|
|
751
|
+
* assignee, AutoLabel) let through, as the browser's board shows its cards, counted per column, and the page of the
|
|
752
|
+
* requested column in the column's order; the projects and the epics a filter may choose, as the browser's board
|
|
753
|
+
* offers them (the epics in the chosen project, their own or one of their tasks', and the chosen epic).
|
|
754
|
+
*/
|
|
755
|
+
export const deviceBoardPage = (
|
|
756
|
+
boardArg: ITicketBoard, requestArg: IReq_DeviceBoardList['request'], viewerArg: IDeviceTicketViewer,
|
|
757
|
+
): IReq_DeviceBoardList['response'] => {
|
|
758
|
+
const byKey = new Map(boardArg.tickets.map((ticketArg) => [ticketArg.key, ticketArg]));
|
|
759
|
+
const matches = ticketFilter({
|
|
760
|
+
...(requestArg.text === undefined ? {} : { text: requestArg.text }),
|
|
761
|
+
...(requestArg.project === undefined ? {} : { project: requestArg.project }),
|
|
762
|
+
}, undefined);
|
|
763
|
+
const narrowing: ITicketBoardNarrowing = {
|
|
764
|
+
...(requestArg.epic === undefined ? {} : { epic: requestArg.epic }),
|
|
765
|
+
...(requestArg.assignee === undefined ? {} : { assignee: deviceAssigneeNarrowing(requestArg.assignee, viewerArg.ownerId) }),
|
|
766
|
+
...(requestArg.label === undefined ? {} : { autoLabel: requestArg.label }),
|
|
767
|
+
};
|
|
768
|
+
const shown = boardArg.tickets.filter((ticketArg) => ticketArg.kind === 'task' && matches(ticketArg) && ticketBoardNarrows(ticketArg, narrowing));
|
|
769
|
+
const counts = new Map<TDeviceTicketColumn, number>();
|
|
770
|
+
for (const ticket of shown) counts.set(ticket.columnId, (counts.get(ticket.columnId) ?? 0) + 1);
|
|
771
|
+
const column = shown.filter((ticketArg) => ticketArg.columnId === requestArg.column);
|
|
772
|
+
const offset = requestArg.offset ?? 0;
|
|
773
|
+
const project = requestArg.project;
|
|
774
|
+
const inProject = (epicArg: ITicket, projectArg: string): boolean => epicArg.project === projectArg
|
|
775
|
+
|| boardArg.tickets.some((taskArg) => taskArg.parentId === epicArg.key && taskArg.project === projectArg);
|
|
776
|
+
return {
|
|
777
|
+
revision: boardArg.revision,
|
|
778
|
+
columns: boardArg.columns.map((columnArg) => ({ id: columnArg.id, title: columnArg.title, done: columnArg.done === true,
|
|
779
|
+
count: counts.get(columnArg.id) ?? 0 })),
|
|
780
|
+
tickets: column.slice(offset, offset + (requestArg.limit ?? 50))
|
|
781
|
+
.map((ticketArg) => deviceTicketCard(ticketArg, ticketArg.parentId === undefined ? undefined : byKey.get(ticketArg.parentId), viewerArg)),
|
|
782
|
+
total: column.length,
|
|
783
|
+
projects: [...new Set(boardArg.tickets.map((ticketArg) => ticketArg.project))].sort((leftArg, rightArg) => leftArg.localeCompare(rightArg, 'en')),
|
|
784
|
+
epics: boardArg.tickets
|
|
785
|
+
.filter((ticketArg) => ticketArg.kind === 'epic' && (project === undefined || ticketArg.key === requestArg.epic || inProject(ticketArg, project)))
|
|
786
|
+
.sort((leftArg, rightArg) => leftArg.order - rightArg.order || leftArg.key.localeCompare(rightArg.key, 'en'))
|
|
787
|
+
.map(({ key, title }) => ({ key, title })),
|
|
788
|
+
};
|
|
789
|
+
};
|
|
790
|
+
|
|
791
|
+
/** A comment or a question of a ticket's discussion, in the order they were written, and the cursor that names it. */
|
|
792
|
+
interface IDeviceDiscussionEntry {
|
|
793
|
+
cursor: string;
|
|
794
|
+
at: number;
|
|
795
|
+
/** Questions before comments written in the same millisecond, each in its own order. */
|
|
796
|
+
rank: number;
|
|
797
|
+
order: string | number;
|
|
798
|
+
item: { comment: IDeviceTicketComment } | { question: IDeviceTicketQuestion };
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
const discussionOrder = (leftArg: IDeviceDiscussionEntry, rightArg: IDeviceDiscussionEntry): number => (
|
|
802
|
+
leftArg.at - rightArg.at || leftArg.rank - rightArg.rank
|
|
803
|
+
|| (leftArg.order < rightArg.order ? -1 : leftArg.order > rightArg.order ? 1 : 0)
|
|
804
|
+
);
|
|
805
|
+
|
|
806
|
+
/**
|
|
807
|
+
* One page of a ticket's discussion, its comments and questions together in the order they were written: those
|
|
808
|
+
* before the entry `before` names (`c:<comment id>` or `q:<question id>`, as a page returned it), or the newest, at
|
|
809
|
+
* most `limit` and `deviceTicketDiscussionPage.bytes` of them as JSON but at least one, each list oldest first, and the
|
|
810
|
+
* cursor of the next older page, `null` at the first entry. A `before` that names no entry is refused.
|
|
811
|
+
*/
|
|
812
|
+
export const deviceTicketDiscussion = (
|
|
813
|
+
commentsArg: readonly ITicketComment[], questionsArg: readonly ITicketQuestion[], pageArg: { before?: string; limit?: number },
|
|
814
|
+
viewerArg: IDeviceTicketViewer,
|
|
815
|
+
): Pick<IReq_DeviceTicketGet['response'], 'comments' | 'questions' | 'before'> => {
|
|
816
|
+
const entries: IDeviceDiscussionEntry[] = [
|
|
817
|
+
...commentsArg.map((commentArg) => ({ cursor: `c:${commentArg.id}`, at: Date.parse(commentArg.createdAt), rank: 1, order: commentArg.id,
|
|
818
|
+
item: { comment: deviceTicketComment(commentArg, viewerArg) } })),
|
|
819
|
+
...questionsArg.map((questionArg) => ({ cursor: `q:${questionArg.id}`, at: Date.parse(questionArg.createdAt), rank: 0,
|
|
820
|
+
order: Number(questionArg.id.slice(1)), item: { question: deviceTicketQuestion(questionArg) } })),
|
|
821
|
+
].sort(discussionOrder);
|
|
822
|
+
const end = pageArg.before === undefined ? entries.length : entries.findIndex((entryArg) => entryArg.cursor === pageArg.before);
|
|
823
|
+
if (end < 0) throw deviceRefusal('invalid_request', 'before names nothing in this ticket\'s discussion; read its newest page.');
|
|
824
|
+
const limit = pageArg.limit ?? deviceTicketDiscussionPage.default;
|
|
825
|
+
let start = end;
|
|
826
|
+
let bytes = 0;
|
|
827
|
+
while (start > 0 && end - start < limit) {
|
|
828
|
+
const size = Buffer.byteLength(JSON.stringify(entries[start - 1]!.item), 'utf8');
|
|
829
|
+
if (end > start && bytes + size > deviceTicketDiscussionPage.bytes) break;
|
|
830
|
+
bytes += size;
|
|
831
|
+
start -= 1;
|
|
832
|
+
}
|
|
833
|
+
const page = entries.slice(start, end).map((entryArg) => entryArg.item);
|
|
834
|
+
return {
|
|
835
|
+
questions: page.flatMap((itemArg) => ('question' in itemArg ? [itemArg.question] : [])),
|
|
836
|
+
comments: page.flatMap((itemArg) => ('comment' in itemArg ? [itemArg.comment] : [])),
|
|
837
|
+
before: start > 0 ? entries[start]!.cursor : null,
|
|
838
|
+
};
|
|
839
|
+
};
|
|
840
|
+
|
|
841
|
+
/** Where a ticket's work runs as a device sees it: the deploy monitor's placement, the targets by id and name. */
|
|
842
|
+
export const deviceTicketPlacement = (placementArg: IFleetDeployTicketPlacement): IDeviceTicketPlacement => ({
|
|
843
|
+
state: 'ready',
|
|
844
|
+
landed: placementArg.landed.map((landedArg) => ({ repo: landedArg.repo, sha: landedArg.sha,
|
|
845
|
+
...(landedArg.branch === undefined ? {} : { branch: landedArg.branch }), ...(landedArg.fixedIn === undefined ? {} : { fixedIn: landedArg.fixedIn.version }) })),
|
|
846
|
+
runs: placementArg.runs.map((runArg) => ({ target: runArg.targetId, name: runArg.name,
|
|
847
|
+
...(runArg.environment === undefined ? {} : { environment: runArg.environment }), repo: runArg.repo,
|
|
848
|
+
...(runArg.version === undefined ? {} : { version: runArg.version }), probe: runArg.probe, runs: runArg.runs,
|
|
849
|
+
...(runArg.shippedIn === undefined ? {} : { shippedIn: runArg.shippedIn }) })),
|
|
850
|
+
foundIn: placementArg.foundIn.map(({ repo, version }) => ({ repo, version })),
|
|
851
|
+
});
|
|
852
|
+
|
|
853
|
+
/**
|
|
854
|
+
* What a ticket plans, from the ticket itself: its release plan, its maintenance window and its deploy plan; while the
|
|
855
|
+
* deploy monitor answered (`placementArg`), where the window stands and the owner's go on it and on the deploy. Never a
|
|
856
|
+
* plan's question or who deploys it.
|
|
857
|
+
*/
|
|
858
|
+
export const deviceTicketPlans = (ticketArg: ITicket, placementArg: IFleetDeployTicketPlacement | undefined): IDeviceTicketPlans => {
|
|
859
|
+
const { releasePlan, maintenanceWindow, deployPlan } = ticketArg;
|
|
860
|
+
const observedWindow = placementArg?.maintenanceWindow;
|
|
861
|
+
const observedPlan = placementArg?.deployPlan;
|
|
862
|
+
return {
|
|
863
|
+
...(releasePlan === undefined ? {} : { release: { repo: releasePlan.repo, ...(releasePlan.version === undefined ? {} : { version: releasePlan.version }) } }),
|
|
864
|
+
...(maintenanceWindow === undefined ? {} : { window: {
|
|
865
|
+
from: Date.parse(maintenanceWindow.from), to: Date.parse(maintenanceWindow.to), targets: [...maintenanceWindow.targets],
|
|
866
|
+
...(observedWindow === undefined ? {} : { state: observedWindow.state, go: observedWindow.go }),
|
|
867
|
+
} }),
|
|
868
|
+
...(deployPlan === undefined ? {} : { deploy: {
|
|
869
|
+
target: deployPlan.target, version: deployPlan.version,
|
|
870
|
+
...(observedPlan === undefined ? {} : {
|
|
871
|
+
targetName: observedPlan.targetName, ...(observedPlan.environment === undefined ? {} : { environment: observedPlan.environment }),
|
|
872
|
+
go: observedPlan.plan.go, deployWhen: observedPlan.deployWhen,
|
|
873
|
+
...(observedPlan.plan.window === undefined ? {} : { window: { from: Date.parse(observedPlan.plan.window.from), to: Date.parse(observedPlan.plan.window.to) } }),
|
|
874
|
+
}),
|
|
875
|
+
} }),
|
|
876
|
+
};
|
|
877
|
+
};
|
|
878
|
+
|
|
879
|
+
/** A ticket's placement for `device.ticket.get`, read on its own as a section of `device.host.get` is. */
|
|
880
|
+
export const readDeviceTicketPlacement = async (readArg: () => Promise<IFleetDeployTicketPlacement>, signalArg: AbortSignal):
|
|
881
|
+
Promise<{ placement: IDeviceTicketPlacement | IDeviceHostUnavailable; observed: IFleetDeployTicketPlacement | undefined }> => {
|
|
882
|
+
let observed: IFleetDeployTicketPlacement | undefined;
|
|
883
|
+
const placement = await readDeviceSection<IDeviceTicketPlacement>('device.ticket.get placement', 'a ticket\'s placement', signalArg, async () => {
|
|
884
|
+
observed = await readArg();
|
|
885
|
+
return deviceTicketPlacement(observed);
|
|
886
|
+
});
|
|
887
|
+
return { placement, observed };
|
|
888
|
+
};
|
|
889
|
+
|
|
890
|
+
const deviceDeployGo = (planArg: IControllerInboxDeployGo): IDeviceDeployGo => ({
|
|
891
|
+
target: planArg.target,
|
|
892
|
+
...(planArg.targetName === undefined ? {} : { targetName: planArg.targetName }),
|
|
893
|
+
...(planArg.environment === undefined ? {} : { environment: planArg.environment }),
|
|
894
|
+
version: planArg.version,
|
|
895
|
+
...(planArg.runs === undefined ? {} : { runs: planArg.runs }),
|
|
896
|
+
...(planArg.deployWhen === undefined ? {} : { deployWhen: planArg.deployWhen }),
|
|
897
|
+
...(planArg.window === undefined ? {} : { window: { from: Date.parse(planArg.window.from), to: Date.parse(planArg.window.to) } }),
|
|
898
|
+
ships: planArg.ships.map(({ key, title }) => ({ key, title })),
|
|
899
|
+
shipsTotal: planArg.shipsTotal,
|
|
900
|
+
});
|
|
901
|
+
|
|
902
|
+
/**
|
|
903
|
+
* Where an Inbox item lives as a device names it, and how a device answers it: a session only by a row the feed lists
|
|
904
|
+
* for devices, never by host paths; the driver, which no row stands for, without an answer.
|
|
905
|
+
*/
|
|
906
|
+
const deviceInboxPlace = (
|
|
907
|
+
whereArg: TControllerInboxWhere,
|
|
908
|
+
answerArg: TControllerInboxItem['answer'],
|
|
909
|
+
rowOfArg: (rowIdArg: string) => IControllerSessionFeedRow | undefined,
|
|
910
|
+
): { where: TDeviceInboxWhere; answer: TDeviceInboxAnswer } => {
|
|
911
|
+
switch (whereArg.kind) {
|
|
912
|
+
case 'conversation':
|
|
913
|
+
case 'native':
|
|
914
|
+
case 'child': {
|
|
915
|
+
const row = rowOfArg(whereArg.rowId);
|
|
916
|
+
const listed = row !== undefined && row.open.kind !== 'terminal';
|
|
917
|
+
const where: TDeviceInboxWhere = {
|
|
918
|
+
kind: 'session', sessionId: whereArg.rowId, harness: whereArg.harness, name: whereArg.name,
|
|
919
|
+
...(whereArg.kind === 'child' ? { childSessionId: whereArg.childSessionId.nativeId, childName: whereArg.childName } : {}),
|
|
920
|
+
};
|
|
921
|
+
const answer: TDeviceInboxAnswer = answerArg === 'none' || !listed ? 'none' : whereArg.kind === 'child' ? 'child' : 'session';
|
|
922
|
+
return { where, answer };
|
|
923
|
+
}
|
|
924
|
+
case 'driver':
|
|
925
|
+
return { where: { kind: 'driver' }, answer: 'none' };
|
|
926
|
+
case 'ticket':
|
|
927
|
+
return {
|
|
928
|
+
where: { kind: 'ticket', ticketKey: whereArg.ticketKey, ticketTitle: whereArg.ticketTitle, project: whereArg.project },
|
|
929
|
+
answer: answerArg === 'ticket' ? 'ticket' : 'none',
|
|
930
|
+
};
|
|
931
|
+
}
|
|
932
|
+
};
|
|
933
|
+
|
|
934
|
+
/** An Inbox item as a device sees it: the same item without host paths, in the device protocol's shapes. */
|
|
935
|
+
export const deviceInboxItem = (
|
|
936
|
+
itemArg: TControllerInboxItem,
|
|
937
|
+
rowOfArg: (rowIdArg: string) => IControllerSessionFeedRow | undefined,
|
|
938
|
+
): TDeviceInboxItem => {
|
|
939
|
+
const base = {
|
|
940
|
+
id: itemArg.id, since: itemArg.since, title: itemArg.title,
|
|
941
|
+
...deviceInboxPlace(itemArg.where, itemArg.answer, rowOfArg),
|
|
942
|
+
...(itemArg.omitted === undefined ? {} : { omitted: { ...itemArg.omitted } }),
|
|
943
|
+
};
|
|
944
|
+
switch (itemArg.kind) {
|
|
945
|
+
case 'permission':
|
|
946
|
+
return { ...base, kind: 'permission', ...(itemArg.permission ? { permission: devicePermissions([itemArg.permission])[0] } : {}) };
|
|
947
|
+
case 'question':
|
|
948
|
+
return { ...base, kind: 'question', ...(itemArg.question ? { question: deviceQuestions([itemArg.question])[0] } : {}) };
|
|
949
|
+
case 'elicitation':
|
|
950
|
+
return { ...base, kind: 'elicitation', ...(itemArg.elicitation ? { elicitation: deviceElicitations([itemArg.elicitation])[0] } : {}) };
|
|
951
|
+
case 'native_wait':
|
|
952
|
+
return { ...base, kind: 'native_wait', reason: itemArg.nativeWait.reason, host: itemArg.host };
|
|
953
|
+
case 'driver':
|
|
954
|
+
return { ...base, kind: 'driver', ...(itemArg.line === undefined ? {} : { line: itemArg.line }) };
|
|
955
|
+
case 'deploy_go':
|
|
956
|
+
return { ...base, kind: 'deploy_go', plan: deviceDeployGo(itemArg.plan), ...(itemArg.question ? { question: deviceTicketQuestion(itemArg.question) } : {}) };
|
|
957
|
+
case 'window_go':
|
|
958
|
+
return {
|
|
959
|
+
...base, kind: 'window_go',
|
|
960
|
+
window: { from: Date.parse(itemArg.window.from), to: Date.parse(itemArg.window.to), targets: [...itemArg.window.targets] },
|
|
961
|
+
...(itemArg.question ? { question: deviceTicketQuestion(itemArg.question) } : {}),
|
|
962
|
+
};
|
|
963
|
+
case 'ticket_question':
|
|
964
|
+
return { ...base, kind: 'ticket_question', ...(itemArg.question ? { question: deviceTicketQuestion(itemArg.question) } : {}) };
|
|
965
|
+
}
|
|
966
|
+
};
|
|
967
|
+
|
|
968
|
+
/** The Inbox as `device.inbox.list` answers it. */
|
|
969
|
+
export const deviceInboxList = (
|
|
970
|
+
snapshotArg: IControllerInboxSnapshot,
|
|
971
|
+
rowOfArg: (rowIdArg: string) => IControllerSessionFeedRow | undefined,
|
|
972
|
+
): IReq_DeviceInboxList['response'] => ({
|
|
973
|
+
epoch: snapshotArg.epoch,
|
|
974
|
+
revision: snapshotArg.revision,
|
|
975
|
+
items: snapshotArg.items.map((itemArg) => deviceInboxItem(itemArg, rowOfArg)),
|
|
976
|
+
total: snapshotArg.total,
|
|
977
|
+
limited: snapshotArg.limited,
|
|
978
|
+
});
|
|
979
|
+
|
|
980
|
+
/** An Inbox change as a device applies it: the same epoch, revision and order. */
|
|
981
|
+
export const deviceInboxDelta = (
|
|
982
|
+
deltaArg: TControllerInboxDelta,
|
|
983
|
+
rowOfArg: (rowIdArg: string) => IControllerSessionFeedRow | undefined,
|
|
984
|
+
): IDeviceInboxDelta => 'reset' in deltaArg
|
|
985
|
+
? { epoch: deltaArg.epoch, revision: deltaArg.revision, reset: true, upserts: [], removed: [], order: [], total: 0, limited: false }
|
|
986
|
+
: {
|
|
987
|
+
epoch: deltaArg.epoch,
|
|
988
|
+
revision: deltaArg.revision,
|
|
989
|
+
reset: false,
|
|
990
|
+
upserts: deltaArg.upserts.map((itemArg) => deviceInboxItem(itemArg, rowOfArg)),
|
|
991
|
+
removed: [...deltaArg.removed],
|
|
992
|
+
order: [...deltaArg.order],
|
|
993
|
+
total: deltaArg.total,
|
|
994
|
+
limited: deltaArg.limited,
|
|
995
|
+
};
|
|
996
|
+
|
|
143
997
|
/**
|
|
144
998
|
* The row id a device names a conversation by, when the feed lists it as something a device opens; events about any
|
|
145
999
|
* other session (a driver, a child, a terminal's agent, one the feed has not listed yet) reach no device.
|
|
@@ -154,35 +1008,63 @@ const visibleRowId = (
|
|
|
154
1008
|
return row && row.open.kind !== 'terminal' ? rowId : undefined;
|
|
155
1009
|
};
|
|
156
1010
|
|
|
157
|
-
/**
|
|
1011
|
+
/**
|
|
1012
|
+
* A controller event as a device receives it: none, one, or several in the order they are sent (a ticket change is
|
|
1013
|
+
* the board's, the direct reports' and the host's). `rowOfArg` answers the feed's published rows; `driverArg` the driver as it
|
|
1014
|
+
* is now, whose own session changes reach a device as `driver.changed` (never its streamed content).
|
|
1015
|
+
*/
|
|
158
1016
|
export const deviceEvent = (
|
|
159
1017
|
eventArg: IControllerEvent,
|
|
160
1018
|
rowOfArg: (rowIdArg: string) => IControllerSessionFeedRow | undefined,
|
|
161
|
-
|
|
1019
|
+
driverArg: TDeviceDriverSource | undefined,
|
|
1020
|
+
): readonly TDeviceEvent[] => {
|
|
162
1021
|
switch (eventArg.type) {
|
|
163
1022
|
case 'sessionfeed.changed':
|
|
164
|
-
return eventArg.sessionFeed ? { type: 'sessionfeed.changed', delta: deviceFeedDelta(eventArg.sessionFeed) } :
|
|
1023
|
+
return eventArg.sessionFeed ? [{ type: 'sessionfeed.changed', delta: deviceFeedDelta(eventArg.sessionFeed) }] : [];
|
|
1024
|
+
case 'inbox.changed':
|
|
1025
|
+
return eventArg.inbox ? [{ type: 'inbox.changed', delta: deviceInboxDelta(eventArg.inbox, rowOfArg) }] : [];
|
|
165
1026
|
case 'sessions.changed':
|
|
166
|
-
return { type: 'sessions.changed' };
|
|
1027
|
+
return [{ type: 'sessions.changed' }];
|
|
1028
|
+
case 'driver.changed':
|
|
1029
|
+
return eventArg.driverStatus
|
|
1030
|
+
? [{ type: 'driver.changed', driver: deviceDriverStatus(eventArg.driverStatus, driverArg?.needsInput()) }] : [];
|
|
1031
|
+
// A report's status is its worker's, and its open questions are tickets'.
|
|
1032
|
+
case 'directreports.changed':
|
|
1033
|
+
case 'fleet.workers.changed':
|
|
1034
|
+
return [{ type: 'directreports.changed' }];
|
|
1035
|
+
// The board, which no key names; tickets also hold the reports' open questions and the deploy targets' planned deploys.
|
|
1036
|
+
case 'tickets.changed':
|
|
1037
|
+
return [
|
|
1038
|
+
{ type: 'tickets.changed', ...(eventArg.ticketRevision === undefined ? {} : { revision: eventArg.ticketRevision }) },
|
|
1039
|
+
{ type: 'directreports.changed' },
|
|
1040
|
+
{ type: 'host.changed' },
|
|
1041
|
+
];
|
|
1042
|
+
// What the host's status reads: its deploy targets and its accounts. Never what these events carry.
|
|
1043
|
+
case 'deployments.changed':
|
|
1044
|
+
case 'accounts.changed':
|
|
1045
|
+
return [{ type: 'host.changed' }];
|
|
167
1046
|
case 'session.changed':
|
|
168
1047
|
case 'session.history.changed':
|
|
169
1048
|
case 'permissions.changed': {
|
|
170
|
-
if (
|
|
1049
|
+
if (driverArg?.isSessionChange(eventArg)) {
|
|
1050
|
+
return [{ type: 'driver.changed', driver: deviceDriverStatus(driverArg.current(), driverArg.needsInput()) }];
|
|
1051
|
+
}
|
|
1052
|
+
if (!eventArg.sessionId) return eventArg.type === 'permissions.changed' ? [{ type: 'sessions.changed' }] : [];
|
|
171
1053
|
const sessionId = visibleRowId(eventArg.sessionId, rowOfArg);
|
|
172
|
-
return sessionId === undefined ?
|
|
1054
|
+
return sessionId === undefined ? [] : [{ type: 'session.changed', sessionId }];
|
|
173
1055
|
}
|
|
174
1056
|
case 'session.text.updated': {
|
|
175
1057
|
const update = eventArg.textUpdate;
|
|
176
1058
|
const sessionId = visibleRowId(update?.sessionId, rowOfArg);
|
|
177
|
-
return update && sessionId !== undefined ? {
|
|
1059
|
+
return update && sessionId !== undefined ? [{
|
|
178
1060
|
type: 'session.text.updated',
|
|
179
1061
|
update: { sessionId, messageId: update.messageId.nativeId, partId: update.partId.nativeId, text: update.text },
|
|
180
|
-
} :
|
|
1062
|
+
}] : [];
|
|
181
1063
|
}
|
|
182
1064
|
case 'session.text.delta': {
|
|
183
1065
|
const delta = eventArg.textDelta;
|
|
184
1066
|
const sessionId = visibleRowId(delta?.sessionId, rowOfArg);
|
|
185
|
-
return delta && sessionId !== undefined ? {
|
|
1067
|
+
return delta && sessionId !== undefined ? [{
|
|
186
1068
|
type: 'session.text.delta',
|
|
187
1069
|
delta: {
|
|
188
1070
|
sessionId,
|
|
@@ -192,10 +1074,10 @@ export const deviceEvent = (
|
|
|
192
1074
|
baseTextUtf8Bytes: delta.baseTextUtf8Bytes,
|
|
193
1075
|
textUtf8Bytes: delta.textUtf8Bytes,
|
|
194
1076
|
},
|
|
195
|
-
} :
|
|
1077
|
+
}] : [];
|
|
196
1078
|
}
|
|
197
1079
|
default:
|
|
198
|
-
return
|
|
1080
|
+
return [];
|
|
199
1081
|
}
|
|
200
1082
|
};
|
|
201
1083
|
|
|
@@ -249,33 +1131,71 @@ export const assertDeviceAuthProveRequest = (valueArg: unknown): IReq_DeviceAuth
|
|
|
249
1131
|
return { deviceId: assertDeviceId(value.deviceId), signature: assertText(value.signature, 'signature', 128) };
|
|
250
1132
|
};
|
|
251
1133
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
if (value.limit !== undefined && (!Number.isSafeInteger(value.limit) || (value.limit as number) < 1 || (value.limit as number) > 50)) {
|
|
1134
|
+
/** A transcript page a device asks for: the `before` cursor of a newer page, and 1 to 50 messages. */
|
|
1135
|
+
const assertDevicePage = (valueArg: Record<string, unknown>): { before?: string; limit?: number } => {
|
|
1136
|
+
if (valueArg.limit !== undefined && (!Number.isSafeInteger(valueArg.limit) || (valueArg.limit as number) < 1 || (valueArg.limit as number) > 50)) {
|
|
256
1137
|
throw new Error('limit must be an integer from 1 to 50.');
|
|
257
1138
|
}
|
|
258
1139
|
return {
|
|
259
|
-
|
|
260
|
-
...(
|
|
261
|
-
...(value.limit === undefined ? {} : { limit: value.limit as number }),
|
|
1140
|
+
...(valueArg.before === undefined ? {} : { before: assertText(valueArg.before, 'before', 4096) }),
|
|
1141
|
+
...(valueArg.limit === undefined ? {} : { limit: valueArg.limit as number }),
|
|
262
1142
|
};
|
|
263
1143
|
};
|
|
264
1144
|
|
|
1145
|
+
/** A device's message: 1 to 64 KiB of UTF-8, not blank. */
|
|
1146
|
+
const assertDeviceMessageText = (valueArg: unknown): string => {
|
|
1147
|
+
const text = assertText(valueArg, 'text', deviceMessageMaximumBytes);
|
|
1148
|
+
if (!text.trim() || text.includes('\0')) throw new Error('text must be a message that is not blank.');
|
|
1149
|
+
return text;
|
|
1150
|
+
};
|
|
1151
|
+
|
|
1152
|
+
export const assertDeviceSessionReadRequest = (valueArg: unknown): IReq_DeviceSessionRead['request'] => {
|
|
1153
|
+
const value = assertObject(valueArg, 'device session read request');
|
|
1154
|
+
assertKeys(value, ['sessionId'], ['before', 'limit']);
|
|
1155
|
+
return { sessionId: assertRowId(value.sessionId), ...assertDevicePage(value) };
|
|
1156
|
+
};
|
|
1157
|
+
|
|
265
1158
|
export const assertDeviceSessionSendRequest = (valueArg: unknown): IReq_DeviceSessionSend['request'] => {
|
|
266
1159
|
const value = assertObject(valueArg, 'device session send request');
|
|
267
1160
|
assertKeys(value, ['sessionId', 'text']);
|
|
268
|
-
const text =
|
|
269
|
-
if (!text.trim() || text.includes('\0')) throw new Error('text must be a message that is not blank.');
|
|
1161
|
+
const text = assertDeviceMessageText(value.text);
|
|
270
1162
|
return { sessionId: assertRowId(value.sessionId), text };
|
|
271
1163
|
};
|
|
272
1164
|
|
|
1165
|
+
export const assertDeviceDriverReadRequest = (valueArg: unknown): IReq_DeviceDriverRead['request'] => {
|
|
1166
|
+
const value = assertObject(valueArg, 'device driver read request');
|
|
1167
|
+
assertKeys(value, [], ['before', 'limit']);
|
|
1168
|
+
return assertDevicePage(value);
|
|
1169
|
+
};
|
|
1170
|
+
|
|
1171
|
+
export const assertDeviceDriverSendRequest = (valueArg: unknown): IReq_DeviceDriverSend['request'] => {
|
|
1172
|
+
const value = assertObject(valueArg, 'device driver send request');
|
|
1173
|
+
assertKeys(value, ['text']);
|
|
1174
|
+
return { text: assertDeviceMessageText(value.text) };
|
|
1175
|
+
};
|
|
1176
|
+
|
|
1177
|
+
/** A device's message to a direct report, in shape; the Driver view's own rules check the name and the text. */
|
|
1178
|
+
export const assertDeviceDirectReportMessageRequest = (valueArg: unknown): IReq_DeviceDirectReportMessage['request'] => {
|
|
1179
|
+
const value = assertObject(valueArg, 'device direct report message request');
|
|
1180
|
+
assertKeys(value, ['name', 'text']);
|
|
1181
|
+
const { name, text } = assertDirectReportMessageRequest({ name: value.name, text: value.text });
|
|
1182
|
+
return { name, text };
|
|
1183
|
+
};
|
|
1184
|
+
|
|
1185
|
+
/** A direct child's OpenCode session id, as the Inbox names it. */
|
|
1186
|
+
const assertChildSessionId = (valueArg: unknown): string => {
|
|
1187
|
+
const id = assertText(valueArg, 'childSessionId', 512);
|
|
1188
|
+
if (!/^[^\s\p{Cc}]+$/u.test(id)) throw new Error('childSessionId must name a session as the Inbox does.');
|
|
1189
|
+
return id;
|
|
1190
|
+
};
|
|
1191
|
+
|
|
273
1192
|
export const assertDevicePermissionReplyRequest = (valueArg: unknown): IReq_DevicePermissionReply['request'] => {
|
|
274
1193
|
const value = assertObject(valueArg, 'device permission reply request');
|
|
275
|
-
assertKeys(value, ['sessionId', 'permissionId', 'reply'], ['message']);
|
|
1194
|
+
assertKeys(value, ['sessionId', 'permissionId', 'reply'], ['message', 'childSessionId']);
|
|
276
1195
|
if (value.reply !== 'once' && value.reply !== 'reject') throw new Error('reply must be once or reject.');
|
|
277
1196
|
return {
|
|
278
1197
|
sessionId: assertRowId(value.sessionId),
|
|
1198
|
+
...(value.childSessionId === undefined ? {} : { childSessionId: assertChildSessionId(value.childSessionId) }),
|
|
279
1199
|
permissionId: assertText(value.permissionId, 'permissionId', 512),
|
|
280
1200
|
reply: value.reply,
|
|
281
1201
|
...(value.message === undefined ? {} : { message: assertText(value.message, 'message', 2048) }),
|
|
@@ -284,9 +1204,10 @@ export const assertDevicePermissionReplyRequest = (valueArg: unknown): IReq_Devi
|
|
|
284
1204
|
|
|
285
1205
|
export const assertDeviceQuestionReplyRequest = (valueArg: unknown): IReq_DeviceQuestionReply['request'] => {
|
|
286
1206
|
const value = assertObject(valueArg, 'device question reply request');
|
|
287
|
-
assertKeys(value, ['sessionId', 'questionId', 'answers']);
|
|
1207
|
+
assertKeys(value, ['sessionId', 'questionId', 'answers'], ['childSessionId']);
|
|
288
1208
|
return {
|
|
289
1209
|
sessionId: assertRowId(value.sessionId),
|
|
1210
|
+
...(value.childSessionId === undefined ? {} : { childSessionId: assertChildSessionId(value.childSessionId) }),
|
|
290
1211
|
questionId: assertText(value.questionId, 'questionId', 512),
|
|
291
1212
|
answers: assertQuestionAnswers(value.answers),
|
|
292
1213
|
};
|
|
@@ -302,6 +1223,122 @@ export const assertDeviceElicitationReplyRequest = (valueArg: unknown): IReq_Dev
|
|
|
302
1223
|
};
|
|
303
1224
|
};
|
|
304
1225
|
|
|
1226
|
+
/**
|
|
1227
|
+
* A ticket answer of a device, in shape: the ticket by key, the question, option ids and text. The ticket store's own
|
|
1228
|
+
* rules (`assertTicketRequest`) check the values as for the browser.
|
|
1229
|
+
*/
|
|
1230
|
+
export const assertDeviceTicketAnswerRequest = (valueArg: unknown): IReq_DeviceTicketAnswer['request'] => {
|
|
1231
|
+
const value = assertObject(valueArg, 'device ticket answer request');
|
|
1232
|
+
assertKeys(value, ['ticketKey', 'questionId'], ['choices', 'text']);
|
|
1233
|
+
if (value.choices !== undefined && (!Array.isArray(value.choices) || value.choices.some((choiceArg) => typeof choiceArg !== 'string'))) {
|
|
1234
|
+
throw new Error('choices must be option ids.');
|
|
1235
|
+
}
|
|
1236
|
+
if (value.text !== undefined && typeof value.text !== 'string') throw new Error('text must be text.');
|
|
1237
|
+
return {
|
|
1238
|
+
ticketKey: assertText(value.ticketKey, 'ticketKey', 64),
|
|
1239
|
+
questionId: assertText(value.questionId, 'questionId', 16),
|
|
1240
|
+
...(value.choices === undefined ? {} : { choices: [...value.choices as string[]] }),
|
|
1241
|
+
...(value.text === undefined ? {} : { text: value.text as string }),
|
|
1242
|
+
};
|
|
1243
|
+
};
|
|
1244
|
+
|
|
1245
|
+
/** A ticket's key, as the board names it (`AGL-12`). */
|
|
1246
|
+
const assertTicketKey = (valueArg: unknown, nameArg: string): string => {
|
|
1247
|
+
if (typeof valueArg !== 'string' || !/^AGL-[1-9][0-9]{0,15}$/u.test(valueArg)) throw new Error(`${nameArg} must name a ticket, such as AGL-12.`);
|
|
1248
|
+
return valueArg;
|
|
1249
|
+
};
|
|
1250
|
+
|
|
1251
|
+
const assertTicketColumn = (valueArg: unknown, nameArg: string): TDeviceTicketColumn => {
|
|
1252
|
+
if (!(ticketColumns as readonly unknown[]).includes(valueArg)) throw new Error(`${nameArg} must be a column of the board: ${ticketColumns.join(', ')}.`);
|
|
1253
|
+
return valueArg as TDeviceTicketColumn;
|
|
1254
|
+
};
|
|
1255
|
+
|
|
1256
|
+
/** An integer from `minimumArg` to `maximumArg`, or absent. */
|
|
1257
|
+
const assertBoundedInteger = (valueArg: unknown, nameArg: string, minimumArg: number, maximumArg: number): number | undefined => {
|
|
1258
|
+
if (valueArg === undefined) return undefined;
|
|
1259
|
+
if (!Number.isSafeInteger(valueArg) || (valueArg as number) < minimumArg || (valueArg as number) > maximumArg) {
|
|
1260
|
+
throw new Error(`${nameArg} must be an integer from ${minimumArg} to ${maximumArg}.`);
|
|
1261
|
+
}
|
|
1262
|
+
return valueArg as number;
|
|
1263
|
+
};
|
|
1264
|
+
|
|
1265
|
+
const assertDeviceAssigneeFilter = (valueArg: unknown): TDeviceAssigneeFilter => {
|
|
1266
|
+
const value = assertObject(valueArg, 'assignee');
|
|
1267
|
+
if (value.kind === 'owner' || value.kind === 'unassigned') {
|
|
1268
|
+
assertKeys(value, ['kind']);
|
|
1269
|
+
return { kind: value.kind };
|
|
1270
|
+
}
|
|
1271
|
+
if (value.kind !== 'session') throw new Error('assignee must be the owner, unassigned or a session.');
|
|
1272
|
+
assertKeys(value, ['kind', 'sessionId']);
|
|
1273
|
+
return { kind: 'session', sessionId: assertRowId(value.sessionId) };
|
|
1274
|
+
};
|
|
1275
|
+
|
|
1276
|
+
/**
|
|
1277
|
+
* A page of the board a device asks for: a column, the board's filters (text 1 to 200 characters, a project, an epic, an
|
|
1278
|
+
* assignee, an AutoLabel) and `limit` (1 to 100) tickets from `offset`. The store's own rules check the text and the
|
|
1279
|
+
* project as for every listing (`assertTicketRequest('board')`).
|
|
1280
|
+
*/
|
|
1281
|
+
export const assertDeviceBoardListRequest = (valueArg: unknown): IReq_DeviceBoardList['request'] => {
|
|
1282
|
+
const value = assertObject(valueArg, 'device board list request');
|
|
1283
|
+
assertKeys(value, ['column'], ['text', 'project', 'epic', 'assignee', 'label', 'offset', 'limit']);
|
|
1284
|
+
if (value.text !== undefined && (typeof value.text !== 'string' || value.text.length === 0 || [...value.text].length > 200)) {
|
|
1285
|
+
throw new Error('text must hold 1-200 characters.');
|
|
1286
|
+
}
|
|
1287
|
+
if (value.label !== undefined && !(ticketAutoLabels as readonly unknown[]).includes(value.label)) {
|
|
1288
|
+
throw new Error(`label must be an AutoLabel: ${ticketAutoLabels.join(', ')}.`);
|
|
1289
|
+
}
|
|
1290
|
+
const filters = assertTicketRequest('board', {
|
|
1291
|
+
...(value.text === undefined ? {} : { text: value.text }),
|
|
1292
|
+
...(value.project === undefined ? {} : { project: value.project }),
|
|
1293
|
+
});
|
|
1294
|
+
const offset = assertBoundedInteger(value.offset, 'offset', 0, Number.MAX_SAFE_INTEGER);
|
|
1295
|
+
const limit = assertBoundedInteger(value.limit, 'limit', 1, 100);
|
|
1296
|
+
return {
|
|
1297
|
+
column: assertTicketColumn(value.column, 'column'),
|
|
1298
|
+
...(filters.text === undefined ? {} : { text: filters.text }),
|
|
1299
|
+
...(filters.project === undefined ? {} : { project: filters.project }),
|
|
1300
|
+
...(value.epic === undefined ? {} : { epic: assertTicketKey(value.epic, 'epic') }),
|
|
1301
|
+
...(value.assignee === undefined ? {} : { assignee: assertDeviceAssigneeFilter(value.assignee) }),
|
|
1302
|
+
...(value.label === undefined ? {} : { label: value.label as NonNullable<IReq_DeviceBoardList['request']['label']> }),
|
|
1303
|
+
...(offset === undefined ? {} : { offset }),
|
|
1304
|
+
...(limit === undefined ? {} : { limit }),
|
|
1305
|
+
};
|
|
1306
|
+
};
|
|
1307
|
+
|
|
1308
|
+
/** A ticket a device opens, and the page of its discussion: the `before` of a newer page, and 1 to 50 entries. */
|
|
1309
|
+
export const assertDeviceTicketGetRequest = (valueArg: unknown): IReq_DeviceTicketGet['request'] => {
|
|
1310
|
+
const value = assertObject(valueArg, 'device ticket get request');
|
|
1311
|
+
assertKeys(value, ['ticketKey'], ['before', 'limit']);
|
|
1312
|
+
const limit = assertBoundedInteger(value.limit, 'limit', 1, deviceTicketDiscussionPage.maximum);
|
|
1313
|
+
return {
|
|
1314
|
+
ticketKey: assertTicketKey(value.ticketKey, 'ticketKey'),
|
|
1315
|
+
...(value.before === undefined ? {} : { before: assertText(value.before, 'before', 2048) }),
|
|
1316
|
+
...(limit === undefined ? {} : { limit }),
|
|
1317
|
+
};
|
|
1318
|
+
};
|
|
1319
|
+
|
|
1320
|
+
/** A device's comment: 1 to 64 KiB of UTF-8, not blank. The store's own rules check it as the browser's. */
|
|
1321
|
+
export const assertDeviceTicketCommentRequest = (valueArg: unknown): IReq_DeviceTicketComment['request'] => {
|
|
1322
|
+
const value = assertObject(valueArg, 'device ticket comment request');
|
|
1323
|
+
assertKeys(value, ['ticketKey', 'text']);
|
|
1324
|
+
return { ticketKey: assertTicketKey(value.ticketKey, 'ticketKey'), text: assertDeviceMessageText(value.text) };
|
|
1325
|
+
};
|
|
1326
|
+
|
|
1327
|
+
/** A device's question: text alone, 1 to 16 KiB of UTF-8, not blank. The store's own rules check it as the browser's. */
|
|
1328
|
+
export const assertDeviceTicketAskRequest = (valueArg: unknown): IReq_DeviceTicketAsk['request'] => {
|
|
1329
|
+
const value = assertObject(valueArg, 'device ticket ask request');
|
|
1330
|
+
assertKeys(value, ['ticketKey', 'question']);
|
|
1331
|
+
const question = assertText(value.question, 'question', 16 * 1024);
|
|
1332
|
+
if (!question.trim() || question.includes('\0')) throw new Error('question must be a question that is not blank.');
|
|
1333
|
+
return { ticketKey: assertTicketKey(value.ticketKey, 'ticketKey'), question };
|
|
1334
|
+
};
|
|
1335
|
+
|
|
1336
|
+
export const assertDeviceTicketMoveRequest = (valueArg: unknown): IReq_DeviceTicketMove['request'] => {
|
|
1337
|
+
const value = assertObject(valueArg, 'device ticket move request');
|
|
1338
|
+
assertKeys(value, ['ticketKey', 'column']);
|
|
1339
|
+
return { ticketKey: assertTicketKey(value.ticketKey, 'ticketKey'), column: assertTicketColumn(value.column, 'column') };
|
|
1340
|
+
};
|
|
1341
|
+
|
|
305
1342
|
export const assertDevicePairingCancelRequest = (valueArg: unknown): { pairingId: string } => {
|
|
306
1343
|
const value = assertObject(valueArg, 'pairing cancel request');
|
|
307
1344
|
assertKeys(value, ['pairingId']);
|