The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import * as Schema from "effect/Schema"; |
| 2 | import { GitPreparePullRequestThreadInput, GitRunStackedActionInput } from "./git.ts"; |
| 3 | import { |
| 4 | AuthAccessWriteScope, |
| 5 | AuthEnvironmentMaintainScope, |
| 6 | AuthOrchestrationOperateScope, |
| 7 | AuthOrchestrationReadScope, |
| 8 | AuthPreviewOperateScope, |
| 9 | AuthProvidersManageScope, |
| 10 | AuthSettingsWriteScope, |
| 11 | AuthSourceControlWriteScope, |
| 12 | AuthTerminalOperateScope, |
| 13 | type AuthEnvironmentScope, |
| 14 | } from "./auth.ts"; |
| 15 | import { ORCHESTRATION_V2_WS_METHODS } from "./orchestrationV2.ts"; |
| 16 | import { WS_METHODS } from "./rpc.ts"; |
| 17 | |
| 18 | /** Incremental client enforcement; the server still authorizes every request. */ |
| 19 | export const CLIENT_GUARDED_RPC_SCOPES = { |
| 20 | [WS_METHODS.providerAuthRemoveMethod]: AuthProvidersManageScope, |
| 21 | // An MCP sign-in stores a credential for the agent, as an account sign-in does. |
| 22 | [WS_METHODS.providerMcpSignInStart]: AuthProvidersManageScope, |
| 23 | [WS_METHODS.providerMcpSignInComplete]: AuthProvidersManageScope, |
| 24 | [WS_METHODS.previewClose]: AuthPreviewOperateScope, |
| 25 | // Stopping a server signals its processes, as closing a terminal does. |
| 26 | [WS_METHODS.previewStopLocalServer]: AuthTerminalOperateScope, |
| 27 | [WS_METHODS.serverRunStorageCleanup]: AuthSettingsWriteScope, |
| 28 | // Restarts the server, as an update does. |
| 29 | [WS_METHODS.serverReclaimDatabaseSpace]: AuthEnvironmentMaintainScope, |
| 30 | [WS_METHODS.serverCheckOtlpEndpoint]: AuthSettingsWriteScope, |
| 31 | [WS_METHODS.serverCheckSecretSource]: AuthSettingsWriteScope, |
| 32 | // Listing models spends the stored API key, so only those who can choose a model may. |
| 33 | [WS_METHODS.serverListTextGeneratorModels]: AuthSettingsWriteScope, |
| 34 | [WS_METHODS.serverRefreshProviders]: AuthOrchestrationReadScope, |
| 35 | [WS_METHODS.pullRequestsRunAction]: AuthSourceControlWriteScope, |
| 36 | [WS_METHODS.pullRequestsUpdate]: AuthSourceControlWriteScope, |
| 37 | [WS_METHODS.pullRequestsComment]: AuthSourceControlWriteScope, |
| 38 | [WS_METHODS.pullRequestsUpdateComment]: AuthSourceControlWriteScope, |
| 39 | [WS_METHODS.pullRequestsSubmitReview]: AuthSourceControlWriteScope, |
| 40 | [WS_METHODS.pullRequestsReplyToThread]: AuthSourceControlWriteScope, |
| 41 | [WS_METHODS.pullRequestsSetThreadResolution]: AuthSourceControlWriteScope, |
| 42 | [WS_METHODS.pullRequestsSetReaction]: AuthSourceControlWriteScope, |
| 43 | [WS_METHODS.pullRequestsSetFilesViewed]: AuthSourceControlWriteScope, |
| 44 | [WS_METHODS.pullRequestsRequestReviewers]: AuthSourceControlWriteScope, |
| 45 | [WS_METHODS.pullRequestsSetLabels]: AuthSourceControlWriteScope, |
| 46 | [WS_METHODS.issuesCreate]: AuthSourceControlWriteScope, |
| 47 | [WS_METHODS.issuesComment]: AuthSourceControlWriteScope, |
| 48 | [WS_METHODS.issuesUpdate]: AuthSourceControlWriteScope, |
| 49 | [WS_METHODS.issuesSetState]: AuthSourceControlWriteScope, |
| 50 | [WS_METHODS.issuesReact]: AuthSourceControlWriteScope, |
| 51 | // A tracker credential and a project's binding are settings, saved under the same grant. |
| 52 | [WS_METHODS.issueTrackersConnect]: AuthSettingsWriteScope, |
| 53 | [WS_METHODS.issueTrackersBindProject]: AuthSettingsWriteScope, |
| 54 | // Writing a guide runs a model on this environment's providers, which reading one does not. |
| 55 | [WS_METHODS.pullRequestsGenerateReviewGuide]: AuthOrchestrationOperateScope, |
| 56 | [WS_METHODS.projectsCreateFromThread]: AuthOrchestrationOperateScope, |
| 57 | [WS_METHODS.sourceControlCloneRepository]: AuthSourceControlWriteScope, |
| 58 | [WS_METHODS.sourceControlPublishRepository]: AuthSourceControlWriteScope, |
| 59 | [WS_METHODS.sourceControlStar]: AuthSourceControlWriteScope, |
| 60 | // Signing in replaces the server's GitHub credential, as saving a token in Settings does. |
| 61 | [WS_METHODS.sourceControlSignInStart]: AuthSettingsWriteScope, |
| 62 | [WS_METHODS.sourceControlSignInWait]: AuthSettingsWriteScope, |
| 63 | [WS_METHODS.sourceControlSignInCancel]: AuthSettingsWriteScope, |
| 64 | [WS_METHODS.projectCloneStart]: AuthSourceControlWriteScope, |
| 65 | [WS_METHODS.projectCloneCancel]: AuthSourceControlWriteScope, |
| 66 | [WS_METHODS.projectCloneRetry]: AuthSourceControlWriteScope, |
| 67 | [WS_METHODS.vcsPull]: AuthSourceControlWriteScope, |
| 68 | [WS_METHODS.gitRunStackedAction]: AuthSourceControlWriteScope, |
| 69 | [WS_METHODS.gitPreparePullRequestThread]: AuthSourceControlWriteScope, |
| 70 | [WS_METHODS.vcsCreateWorktree]: AuthSourceControlWriteScope, |
| 71 | [WS_METHODS.vcsRemoveWorktree]: AuthSourceControlWriteScope, |
| 72 | [WS_METHODS.vcsCreateRef]: AuthSourceControlWriteScope, |
| 73 | [WS_METHODS.vcsSwitchRef]: AuthSourceControlWriteScope, |
| 74 | [WS_METHODS.vcsInit]: AuthSourceControlWriteScope, |
| 75 | |
| 76 | // Starts an agent session on this environment that can read its logs and database. |
| 77 | [WS_METHODS.serverPrepareTriage]: AuthOrchestrationOperateScope, |
| 78 | [WS_METHODS.scheduledTasksUpsert]: AuthOrchestrationOperateScope, |
| 79 | [WS_METHODS.scheduledTasksSetEnabled]: AuthOrchestrationOperateScope, |
| 80 | [WS_METHODS.scheduledTasksDelete]: AuthOrchestrationOperateScope, |
| 81 | [WS_METHODS.scheduledTasksRunNow]: AuthOrchestrationOperateScope, |
| 82 | [WS_METHODS.scheduledTasksRotateWebhookToken]: AuthOrchestrationOperateScope, |
| 83 | [ORCHESTRATION_V2_WS_METHODS.stopWorkflow]: AuthOrchestrationOperateScope, |
| 84 | // Force stop kills agent processes, as Stop on a terminal does. |
| 85 | [ORCHESTRATION_V2_WS_METHODS.forceStopThread]: AuthOrchestrationOperateScope, |
| 86 | // A guest link hands thread access to someone outside, like a pairing link. |
| 87 | [WS_METHODS.guestShareCreate]: AuthAccessWriteScope, |
| 88 | [WS_METHODS.peerLinksLink]: AuthAccessWriteScope, |
| 89 | [WS_METHODS.peerLinksUnlink]: AuthAccessWriteScope, |
| 90 | // A link lets every agent here reach the other environment: Settings' bar. |
| 91 | [WS_METHODS.peerLinksAnswerRequest]: AuthAccessWriteScope, |
| 92 | [WS_METHODS.peerLinksPairClient]: AuthAccessWriteScope, |
| 93 | } as const; |
| 94 | export type ClientGuardedRpcTag = keyof typeof CLIENT_GUARDED_RPC_SCOPES; |
| 95 | |
| 96 | const decodePrepareThread = Schema.decodeUnknownSync(GitPreparePullRequestThreadInput); |
| 97 | const decodeStackedAction = Schema.decodeUnknownSync(GitRunStackedActionInput); |
| 98 | |
| 99 | export function clientRpcRequiredScopes( |
| 100 | method: string, |
| 101 | input: unknown, |
| 102 | ): readonly AuthEnvironmentScope[] { |
| 103 | if (method === WS_METHODS.gitPreparePullRequestThread && input !== undefined) { |
| 104 | const payload = decodePrepareThread(input); |
| 105 | if (payload.mode === "worktree" && payload.threadId !== undefined) |
| 106 | return [AuthSourceControlWriteScope, AuthOrchestrationOperateScope]; |
| 107 | } |
| 108 | if (method === WS_METHODS.gitRunStackedAction && input !== undefined) { |
| 109 | const payload = decodeStackedAction(input); |
| 110 | if ( |
| 111 | payload.threadId !== undefined && |
| 112 | (payload.action === "create_pr" || payload.action === "commit_push_pr") |
| 113 | ) |
| 114 | return [AuthSourceControlWriteScope, AuthOrchestrationReadScope]; |
| 115 | } |
| 116 | return Object.hasOwn(CLIENT_GUARDED_RPC_SCOPES, method) |
| 117 | ? [CLIENT_GUARDED_RPC_SCOPES[method as ClientGuardedRpcTag]] |
| 118 | : []; |
| 119 | } |