Open History to see the last commit
1import {
2 CLIENT_GUARDED_RPC_SCOPES,
3 type DeviceListInput,
4 clientRpcRequiredScopes,
5 containsSecretReference,
6 authScopeRequiredResponse,
7 AssetCreateUrlInput,
8 AuthAccessReadScope,
9 ServerSettingsPatch,
10 ProviderInstanceMutation,
11 requiredScopesForServerSettingsPatch,
12 requiredScopesForProjectMutation,
13 ProjectMutation,
14 AuthSettingsWriteScope,
15 AuthProvidersManageScope,
16 AuthEnvironmentMaintainScope,
17 AuthFilesystemReadScope,
18 AuthFilesystemWriteScope,
19 AuthDiagnosticsReadScope,
20 AuthOrchestrationOperateScope,
21 AuthOrchestrationReadScope,
22 AuthPreviewOperateScope,
23 AuthRelayReadScope,
24 AuthRelayWriteScope,
25 AuthTerminalOperateScope,
26 ORCHESTRATION_V2_WS_METHODS,
27 type OrchestrationV2Command,
28 type OrchestrationV2ThreadLaunchInput,
29 AuthTerminalReadScope,
30 type AuthEnvironmentScope,
31 EnvironmentAuthorizationError,
32 RpcScopeAuthorization,
33 WS_METHODS,
34 WsRpcGroup,
35} from "@t3tools/contracts";
36import * as Effect from "effect/Effect";
37import * as Schema from "effect/Schema";
38import * as Layer from "effect/Layer";
39import type * as RpcGroup from "effect/rpc/RpcGroup";
40import { isProviderSignOutPrompt } from "../orchestration-v2/CommandPolicy.ts";
41
42type WsRpcMethod = RpcGroup.Rpcs<typeof WsRpcGroup>["_tag"];
43
44export const RPC_REQUIRED_SCOPES = {
45 ...CLIENT_GUARDED_RPC_SCOPES,
46 [ORCHESTRATION_V2_WS_METHODS.dispatchCommand]: AuthOrchestrationOperateScope,
47 [ORCHESTRATION_V2_WS_METHODS.getWorkflowScript]: AuthOrchestrationReadScope,
48 [ORCHESTRATION_V2_WS_METHODS.getNativeChildWork]: AuthOrchestrationReadScope,
49 [ORCHESTRATION_V2_WS_METHODS.stopWorkflow]: AuthOrchestrationOperateScope,
50 [ORCHESTRATION_V2_WS_METHODS.forceStopThread]: AuthOrchestrationOperateScope,
51 [ORCHESTRATION_V2_WS_METHODS.getTurnDiff]: AuthOrchestrationReadScope,
52 [ORCHESTRATION_V2_WS_METHODS.getFullThreadDiff]: AuthOrchestrationReadScope,
53 [ORCHESTRATION_V2_WS_METHODS.searchThreads]: AuthOrchestrationReadScope,
54 [ORCHESTRATION_V2_WS_METHODS.searchThread]: AuthOrchestrationReadScope,
55 [ORCHESTRATION_V2_WS_METHODS.searchThreadStream]: AuthOrchestrationReadScope,
56 [ORCHESTRATION_V2_WS_METHODS.getArchivedShellSnapshot]: AuthOrchestrationReadScope,
57 [ORCHESTRATION_V2_WS_METHODS.getThreadFamily]: AuthOrchestrationReadScope,
58 [ORCHESTRATION_V2_WS_METHODS.getThreadTitles]: AuthOrchestrationReadScope,
59 [ORCHESTRATION_V2_WS_METHODS.getThreadLinkMetadata]: AuthOrchestrationReadScope,
60 [ORCHESTRATION_V2_WS_METHODS.getWorktreeThreadIds]: AuthOrchestrationReadScope,
61 [ORCHESTRATION_V2_WS_METHODS.getOlderSettledThreads]: AuthOrchestrationReadScope,
62 [ORCHESTRATION_V2_WS_METHODS.getThreadProjection]: AuthOrchestrationReadScope,
63 [ORCHESTRATION_V2_WS_METHODS.getTurnItem]: AuthOrchestrationReadScope,
64 [ORCHESTRATION_V2_WS_METHODS.launchThread]: AuthOrchestrationOperateScope,
65 [ORCHESTRATION_V2_WS_METHODS.subscribeShell]: AuthOrchestrationReadScope,
66 [ORCHESTRATION_V2_WS_METHODS.subscribeThread]: AuthOrchestrationReadScope,
67 [ORCHESTRATION_V2_WS_METHODS.subscribeCommandOutput]: AuthOrchestrationReadScope,
68 [ORCHESTRATION_V2_WS_METHODS.subscribePromptSuggestion]: AuthOrchestrationReadScope,
69 [WS_METHODS.projectsMutate]: AuthOrchestrationOperateScope,
70 [WS_METHODS.serverProbe]: AuthOrchestrationReadScope,
71 [WS_METHODS.serverGetConfig]: AuthOrchestrationReadScope,
72 [WS_METHODS.serverGetNpmRelease]: AuthOrchestrationReadScope,
73 [WS_METHODS.serverRefreshProviders]: AuthOrchestrationReadScope,
74 [WS_METHODS.serverUpdateProvider]: AuthProvidersManageScope,
75 [WS_METHODS.providerAuthStart]: AuthProvidersManageScope,
76 [WS_METHODS.providerConsumeResetCredit]: AuthProvidersManageScope,
77 [WS_METHODS.providerAuthComplete]: AuthProvidersManageScope,
78 [WS_METHODS.providerMcpSignInStart]: AuthProvidersManageScope,
79 [WS_METHODS.providerMcpSignInComplete]: AuthProvidersManageScope,
80 [WS_METHODS.chatGptReconnectProfile]: AuthProvidersManageScope,
81 [WS_METHODS.chatGptImportProfile]: AuthProvidersManageScope,
82 [WS_METHODS.chatGptHandoffSubscribe]: AuthProvidersManageScope,
83 [WS_METHODS.providerAuthRespond]: AuthProvidersManageScope,
84 [WS_METHODS.providerAuthCancel]: AuthProvidersManageScope,
85 [WS_METHODS.providerAuthRemoveMethod]: AuthProvidersManageScope,
86 [WS_METHODS.providerAuthLogout]: AuthProvidersManageScope,
87 [WS_METHODS.providerAuthSubscribe]: AuthProvidersManageScope,
88 [WS_METHODS.providerInstallStart]: AuthProvidersManageScope,
89 [WS_METHODS.providerInstallCancel]: AuthProvidersManageScope,
90 [WS_METHODS.providerInstallSubscribe]: AuthOrchestrationReadScope,
91 [WS_METHODS.providerInstallRemove]: AuthProvidersManageScope,
92 [WS_METHODS.serverUpdateServer]: AuthEnvironmentMaintainScope,
93 [WS_METHODS.serverUpdateServerWithProgress]: AuthEnvironmentMaintainScope,
94 [WS_METHODS.serverCommitDesktopUpdate]: AuthEnvironmentMaintainScope,
95 [WS_METHODS.serverUpsertKeybinding]: AuthSettingsWriteScope,
96 [WS_METHODS.serverRemoveKeybinding]: AuthSettingsWriteScope,
97 [WS_METHODS.serverGetStorageCleanupReport]: AuthOrchestrationReadScope,
98 [WS_METHODS.serverGetDatabaseStorage]: AuthOrchestrationReadScope,
99 [WS_METHODS.serverGetSettings]: AuthOrchestrationReadScope,
100 [WS_METHODS.peerLinksList]: AuthAccessReadScope,
101 // Names only, so a client that reads threads can open a linked environment's.
102 [WS_METHODS.peerLinksNames]: AuthOrchestrationReadScope,
103 // A linked environment's thread is read as this one's own are.
104 [WS_METHODS.peerLinksSubscribeThread]: AuthOrchestrationReadScope,
105 [WS_METHODS.peerLinksThreadHistoryPage]: AuthOrchestrationReadScope,
106 [WS_METHODS.peerLinksThreadItem]: AuthOrchestrationReadScope,
107 [WS_METHODS.serverUpdateSettings]: AuthSettingsWriteScope,
108 [WS_METHODS.serverSearchAcpRegistry]: AuthOrchestrationReadScope,
109 [WS_METHODS.serverPrepareAcpRegistryAgent]: AuthProvidersManageScope,
110 [WS_METHODS.serverUninstallAcpRegistryManagedBinary]: AuthProvidersManageScope,
111 [WS_METHODS.serverAcceptAcpRegistryUrlAuth]: AuthProvidersManageScope,
112 [WS_METHODS.serverListAcpRegistrySessions]: AuthOrchestrationReadScope,
113 [WS_METHODS.serverImportAcpRegistrySession]: AuthOrchestrationOperateScope,
114 [WS_METHODS.serverDeleteAcpRegistrySession]: AuthOrchestrationOperateScope,
115 [WS_METHODS.serverListAcpRegistryProviders]: AuthOrchestrationReadScope,
116 [WS_METHODS.serverSetAcpRegistryProvider]: AuthProvidersManageScope,
117 [WS_METHODS.serverDisableAcpRegistryProvider]: AuthProvidersManageScope,
118 [WS_METHODS.serverLogoutAcpRegistry]: AuthProvidersManageScope,
119 [WS_METHODS.serverDiscoverSourceControl]: AuthOrchestrationReadScope,
120 [WS_METHODS.serverGetTraceDiagnostics]: AuthDiagnosticsReadScope,
121 [WS_METHODS.serverGetProcessDiagnostics]: AuthDiagnosticsReadScope,
122 // Thread load balancing reads host load, so it needs thread scope rather than diagnostics scope.
123 [WS_METHODS.serverGetHostResources]: AuthOrchestrationReadScope,
124 [WS_METHODS.serverGetProcessResourceHistory]: AuthDiagnosticsReadScope,
125 [WS_METHODS.serverGetResourceTelemetryHistory]: AuthDiagnosticsReadScope,
126 [WS_METHODS.serverRetryResourceTelemetry]: AuthDiagnosticsReadScope,
127 [WS_METHODS.serverGetUsageSummary]: AuthDiagnosticsReadScope,
128 [WS_METHODS.serverRefreshUsageRates]: AuthDiagnosticsReadScope,
129 [WS_METHODS.serverSignalProcess]: AuthEnvironmentMaintainScope,
130 [WS_METHODS.serverReportClientActivity]: AuthOrchestrationReadScope,
131 [WS_METHODS.scheduledTasksSubscribe]: AuthOrchestrationReadScope,
132 [WS_METHODS.workflowsSubscribe]: AuthOrchestrationReadScope,
133 [WS_METHODS.workflowsCounts]: AuthOrchestrationReadScope,
134 [WS_METHODS.workflowsGet]: AuthOrchestrationReadScope,
135 [WS_METHODS.workflowsGetDefinition]: AuthFilesystemReadScope,
136 [WS_METHODS.secretsAnswerRequest]: AuthOrchestrationOperateScope,
137 [WS_METHODS.agentFeedbackAnswerRequest]: AuthOrchestrationOperateScope,
138 // Delivery logs hold request bodies, so they need the same scope as the URL.
139 [WS_METHODS.scheduledTasksListWebhookDeliveries]: AuthOrchestrationOperateScope,
140 [WS_METHODS.scheduledTasksGetWebhookDelivery]: AuthOrchestrationOperateScope,
141 [WS_METHODS.cloudGetRelayClientStatus]: AuthRelayReadScope,
142 [WS_METHODS.cloudInstallRelayClient]: AuthRelayWriteScope,
143 [WS_METHODS.pullRequestsList]: AuthOrchestrationReadScope,
144 [WS_METHODS.pullRequestsListStats]: AuthOrchestrationReadScope,
145 [WS_METHODS.pullRequestsFilterOptions]: AuthOrchestrationReadScope,
146 [WS_METHODS.pullRequestsSummary]: AuthOrchestrationReadScope,
147 [WS_METHODS.pullRequestsRouting]: AuthOrchestrationReadScope,
148 [WS_METHODS.pullRequestsRoutingIdentity]: AuthOrchestrationReadScope,
149 [WS_METHODS.pullRequestsStack]: AuthOrchestrationReadScope,
150 [WS_METHODS.pullRequestsLinkedThreads]: AuthOrchestrationReadScope,
151 [WS_METHODS.pullRequestsDetail]: AuthOrchestrationReadScope,
152 [WS_METHODS.issuesList]: AuthOrchestrationReadScope,
153 [WS_METHODS.issuesDetail]: AuthOrchestrationReadScope,
154 [WS_METHODS.issuesComments]: AuthOrchestrationReadScope,
155 [WS_METHODS.issuesTemplates]: AuthOrchestrationReadScope,
156 [WS_METHODS.issuesCandidates]: AuthOrchestrationReadScope,
157 [WS_METHODS.issuesLinkedThreads]: AuthOrchestrationReadScope,
158 [WS_METHODS.issueTrackersList]: AuthOrchestrationReadScope,
159 [WS_METHODS.issueTrackersConnect]: AuthSettingsWriteScope,
160 [WS_METHODS.issueTrackersBindProject]: AuthSettingsWriteScope,
161 [WS_METHODS.pullRequestsPreview]: AuthOrchestrationReadScope,
162 [WS_METHODS.pullRequestsChecks]: AuthOrchestrationReadScope,
163 [WS_METHODS.pullRequestsActivity]: AuthOrchestrationReadScope,
164 [WS_METHODS.pullRequestsReviewGuide]: AuthOrchestrationReadScope,
165 [WS_METHODS.pullRequestsThreadComments]: AuthOrchestrationReadScope,
166 [WS_METHODS.pullRequestsDiffFileContents]: AuthOrchestrationReadScope,
167 [WS_METHODS.pullRequestsFilesViewed]: AuthOrchestrationReadScope,
168 [WS_METHODS.pullRequestsChangeBreakdown]: AuthOrchestrationReadScope,
169 [WS_METHODS.pullRequestsInvalidate]: AuthOrchestrationReadScope,
170 [WS_METHODS.pullRequestsReportState]: AuthOrchestrationReadScope,
171 [WS_METHODS.pullRequestsSubscribeRefreshes]: AuthOrchestrationReadScope,
172 [WS_METHODS.pullRequestsSubscribeLinkedThreadRefreshes]: AuthOrchestrationReadScope,
173 [WS_METHODS.pullRequestsReviewerCandidates]: AuthOrchestrationReadScope,
174 [WS_METHODS.pullRequestsLabelCandidates]: AuthOrchestrationReadScope,
175 [WS_METHODS.sourceControlLookupRepository]: AuthOrchestrationReadScope,
176 [WS_METHODS.sourceControlAppAccess]: AuthOrchestrationReadScope,
177 [WS_METHODS.sourceControlStarred]: AuthOrchestrationReadScope,
178 [WS_METHODS.subscribeProjectClones]: AuthOrchestrationReadScope,
179 [WS_METHODS.projectsListEntries]: AuthFilesystemReadScope,
180 [WS_METHODS.projectsListIgnoredFiles]: AuthFilesystemReadScope,
181 [WS_METHODS.projectsReadFile]: AuthFilesystemReadScope,
182 [WS_METHODS.projectsSearchContents]: AuthFilesystemReadScope,
183 [WS_METHODS.projectsSearchEntries]: AuthFilesystemReadScope,
184 [WS_METHODS.projectsWriteFile]: AuthFilesystemWriteScope,
185 [WS_METHODS.projectsEnsureScratch]: AuthOrchestrationOperateScope,
186 [WS_METHODS.projectsCreateNew]: AuthOrchestrationOperateScope,
187 [WS_METHODS.shellOpenInEditor]: AuthOrchestrationOperateScope,
188 [WS_METHODS.filesystemBrowse]: AuthFilesystemReadScope,
189 [WS_METHODS.workspaceListFolders]: AuthFilesystemReadScope,
190 [WS_METHODS.workspaceScanFolders]: AuthFilesystemReadScope,
191 [WS_METHODS.workspaceTaskFolder]: AuthFilesystemReadScope,
192 [WS_METHODS.filesystemGetMetadata]: AuthFilesystemReadScope,
193 [WS_METHODS.agentSessionsScan]: AuthOrchestrationReadScope,
194 [WS_METHODS.agentSessionsImport]: AuthOrchestrationOperateScope,
195 [WS_METHODS.assetsCreateUrl]: AuthOrchestrationReadScope,
196 [WS_METHODS.assetsPersistChatAttachments]: AuthOrchestrationOperateScope,
197 [WS_METHODS.attachmentsCreateUploadUrl]: AuthOrchestrationOperateScope,
198 [WS_METHODS.attachmentsDelete]: AuthOrchestrationOperateScope,
199 [WS_METHODS.providerUploadFeedback]: AuthOrchestrationOperateScope,
200 [WS_METHODS.mcpAppsCallTool]: AuthOrchestrationOperateScope,
201 [WS_METHODS.mcpAppsToolInfo]: AuthOrchestrationReadScope,
202 [WS_METHODS.mcpAppsReadResource]: AuthOrchestrationReadScope,
203 [WS_METHODS.mcpAppsUpdateModelContext]: AuthOrchestrationOperateScope,
204 [WS_METHODS.subscribeVcsStatus]: AuthOrchestrationReadScope,
205 [WS_METHODS.subscribeVcsBranch]: AuthOrchestrationReadScope,
206 [WS_METHODS.vcsRefreshBranch]: AuthOrchestrationReadScope,
207 [WS_METHODS.subscribeWorktreeSetup]: AuthOrchestrationReadScope,
208 [WS_METHODS.worktreeSetupCancel]: AuthOrchestrationOperateScope,
209 [WS_METHODS.subscribeResourceTelemetry]: AuthDiagnosticsReadScope,
210 [WS_METHODS.vcsRefreshStatus]: AuthOrchestrationReadScope,
211 [WS_METHODS.gitResolvePullRequest]: AuthOrchestrationReadScope,
212 [WS_METHODS.vcsListRefs]: AuthOrchestrationReadScope,
213 [WS_METHODS.reviewGetBranchCommits]: AuthFilesystemReadScope,
214 [WS_METHODS.reviewGetDiffPreview]: AuthFilesystemReadScope,
215 [WS_METHODS.reviewGetDiffFileContents]: AuthFilesystemReadScope,
216 [WS_METHODS.terminalOpen]: AuthTerminalOperateScope,
217 [WS_METHODS.terminalAttach]: AuthTerminalOperateScope,
218 [WS_METHODS.terminalObserve]: AuthTerminalReadScope,
219 [WS_METHODS.terminalWrite]: AuthTerminalOperateScope,
220 [WS_METHODS.terminalResize]: AuthTerminalOperateScope,
221 [WS_METHODS.terminalClear]: AuthTerminalOperateScope,
222 [WS_METHODS.terminalClose]: AuthTerminalOperateScope,
223 [WS_METHODS.subscribeTerminalMetadata]: AuthTerminalReadScope,
224 [WS_METHODS.previewOpen]: AuthPreviewOperateScope,
225 [WS_METHODS.previewResize]: AuthPreviewOperateScope,
226 [WS_METHODS.previewAdjust]: AuthPreviewOperateScope,
227 [WS_METHODS.previewClose]: AuthPreviewOperateScope,
228 [WS_METHODS.previewList]: AuthOrchestrationReadScope,
229 [WS_METHODS.previewClearProfile]: AuthPreviewOperateScope,
230 [WS_METHODS.previewReportProfiles]: AuthPreviewOperateScope,
231 [WS_METHODS.previewReportStatus]: AuthPreviewOperateScope,
232 [WS_METHODS.previewStopLocalServer]: AuthTerminalOperateScope,
233 [WS_METHODS.subscribePreviewEvents]: AuthOrchestrationReadScope,
234 [WS_METHODS.subscribeDiscoveredLocalServers]: AuthOrchestrationReadScope,
235 [WS_METHODS.deviceConfigure]: AuthSettingsWriteScope,
236 [WS_METHODS.deviceTestHost]: AuthSettingsWriteScope,
237 [WS_METHODS.deviceEasAccount]: AuthSettingsWriteScope,
238 [WS_METHODS.deviceSimlockIdentity]: AuthSettingsWriteScope,
239 [WS_METHODS.deviceList]: AuthOrchestrationReadScope,
240 [WS_METHODS.deviceOpen]: AuthOrchestrationOperateScope,
241 [WS_METHODS.deviceClose]: AuthOrchestrationOperateScope,
242 [WS_METHODS.deviceShutdown]: AuthOrchestrationOperateScope,
243 [WS_METHODS.deviceDetail]: AuthOrchestrationReadScope,
244 [WS_METHODS.deviceAction]: AuthOrchestrationOperateScope,
245 [WS_METHODS.subscribeDeviceState]: AuthOrchestrationReadScope,
246 [WS_METHODS.subscribeServerConfig]: AuthOrchestrationReadScope,
247 [WS_METHODS.subscribeServerLifecycle]: AuthOrchestrationReadScope,
248 [WS_METHODS.subscribeAuthAccess]: AuthAccessReadScope,
249} as const satisfies Readonly<Record<WsRpcMethod, AuthEnvironmentScope>>;
250
251export function requiredScopeForRpcMethod(method: string): AuthEnvironmentScope {
252 if (!Object.hasOwn(RPC_REQUIRED_SCOPES, method)) {
253 throw new Error(`RPC method ${method} has no declared authorization scope.`);
254 }
255 const requiredScope = RPC_REQUIRED_SCOPES[method as WsRpcMethod];
256 if (requiredScope === undefined) {
257 throw new Error(`RPC method ${method} has no declared authorization scope.`);
258 }
259 return requiredScope;
260}
261
262export const rpcAuthorizationError = (requiredScope: AuthEnvironmentScope) =>
263 new EnvironmentAuthorizationError({
264 message: `The authenticated token is missing required scope: ${requiredScope}.`,
265 ...authScopeRequiredResponse(requiredScope),
266 });
267
268const SettingsUpdate = Schema.Struct({
269 patch: ServerSettingsPatch,
270 providerInstanceMutation: Schema.optionalKey(ProviderInstanceMutation),
271});
272// Middleware receives the payload already decoded, so it is checked against the decoded shape.
273const decodeSettingsUpdate = Schema.decodeUnknownSync(Schema.toType(SettingsUpdate));
274
275const requiredScopesForSettingsUpdate = (payload: unknown) => {
276 const input = decodeSettingsUpdate(payload);
277 const scopes = requiredScopesForServerSettingsPatch(input.patch);
278 if (input.providerInstanceMutation === undefined) return scopes;
279 return Object.values(input.patch).every((value) => value === undefined)
280 ? [AuthProvidersManageScope]
281 : [...new Set([...scopes, AuthProvidersManageScope])];
282};
283
284const decodeProjectMutation = Schema.decodeUnknownSync(ProjectMutation);
285const decodeAssetCreateUrlInput = Schema.decodeUnknownSync(AssetCreateUrlInput);
286
287/**
288 * A queued edit that omits `attachments` keeps the queued ones, which a payload cannot show;
289 * ThreadMessageIntake.dispatchCommand decides that case.
290 */
291const sendsProviderSignOutPrompt = (method: string, payload: unknown): boolean => {
292 if (method === ORCHESTRATION_V2_WS_METHODS.launchThread) {
293 const message = (payload as OrchestrationV2ThreadLaunchInput).initialMessage;
294 return message !== undefined && isProviderSignOutPrompt(message);
295 }
296 if (method !== ORCHESTRATION_V2_WS_METHODS.dispatchCommand) return false;
297 const command = payload as OrchestrationV2Command;
298 switch (command.type) {
299 case "message.dispatch":
300 return isProviderSignOutPrompt(command);
301 case "queued-run.edit":
302 return (
303 command.attachments !== undefined &&
304 isProviderSignOutPrompt({ text: command.text, attachments: command.attachments })
305 );
306 case "delegated_task.request":
307 return isProviderSignOutPrompt({ text: command.task, attachments: [] });
308 default:
309 return false;
310 }
311};
312
313const requiredScopesForRpcCall = (
314 method: string,
315 payload: unknown,
316): ReadonlyArray<AuthEnvironmentScope> => {
317 if (method === WS_METHODS.serverRetryResourceTelemetry) {
318 return [AuthEnvironmentMaintainScope, AuthDiagnosticsReadScope];
319 }
320 if (method === WS_METHODS.assetsCreateUrl) {
321 const { resource } = decodeAssetCreateUrlInput(payload);
322 return [
323 resource._tag === "workspace-file" ||
324 resource._tag === "media-file" ||
325 resource._tag === "draft-workspace-file" ||
326 (resource._tag === "native-app-icon" && resource.app._tag === "app-path")
327 ? AuthFilesystemReadScope
328 : AuthOrchestrationReadScope,
329 ];
330 }
331 if (method === WS_METHODS.serverUpdateSettings) return requiredScopesForSettingsUpdate(payload);
332 // These send the credential they are given to a URL the caller names, as a saved setting would.
333 if (
334 (method === WS_METHODS.deviceSimlockIdentity ||
335 method === WS_METHODS.deviceEasAccount ||
336 method === WS_METHODS.serverListTextGeneratorModels) &&
337 containsSecretReference(payload)
338 ) {
339 return [AuthSettingsWriteScope, AuthProvidersManageScope];
340 }
341 if (method === WS_METHODS.projectsMutate) {
342 return requiredScopesForProjectMutation(decodeProjectMutation(payload));
343 }
344 const guarded = clientRpcRequiredScopes(method, payload);
345 const scopes = guarded.length > 0 ? guarded : [requiredScopeForRpcMethod(method)];
346 return sendsProviderSignOutPrompt(method, payload)
347 ? [...scopes, AuthProvidersManageScope]
348 : scopes;
349};
350
351/**
352 * `onAuthorized` runs once after an authorized request succeeds; for a stream
353 * RPC, that is when the stream completes. Every RPC passes through here, so it
354 * is the one place to observe requests without wrapping each handler.
355 */
356export const layer = (
357 scopes: ReadonlyArray<AuthEnvironmentScope>,
358 onAuthorized?: (method: string, payload: unknown) => Effect.Effect<void>,
359) =>
360 Layer.succeed(RpcScopeAuthorization)((effect, { rpc, payload }) => {
361 const requiredScopes = requiredScopesForRpcCall(rpc._tag, payload);
362 const requiredScope = requiredScopes.find((scope) => !scopes.includes(scope));
363 if (requiredScope !== undefined) return Effect.fail(rpcAuthorizationError(requiredScope));
364 return onAuthorized === undefined
365 ? effect
366 : Effect.tap(effect, () => onAuthorized(rpc._tag, payload));
367 });
368
369/** Retrying can install or restart tools even though ordinary listing is readable. */
370export const requiredScopeForDeviceList = (input: DeviceListInput): AuthEnvironmentScope =>
371 input.retryHostId || input.updateTool
372 ? AuthOrchestrationOperateScope
373 : AuthOrchestrationReadScope;