The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { ORCHESTRATION_V2_WS_METHODS, WS_METHODS, type WsRpcGroup } from "@t3tools/contracts"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import * as Option from "effect/Option"; |
| 5 | import * as References from "effect/References"; |
| 6 | import type * as RpcGroup from "effect/rpc/RpcGroup"; |
| 7 | import * as RpcMiddleware from "effect/rpc/RpcMiddleware"; |
| 8 | |
| 9 | import { rpcRequestDuration, rpcRequestsTotal, withMetrics } from "./Metrics.ts"; |
| 10 | |
| 11 | type WsRpcMethod = RpcGroup.Rpcs<typeof WsRpcGroup>["_tag"]; |
| 12 | |
| 13 | // Trace queries and dashboards depend on these labels, including deviations from method prefixes. |
| 14 | const RPC_AGGREGATES = { |
| 15 | [ORCHESTRATION_V2_WS_METHODS.dispatchCommand]: "orchestrationV2", |
| 16 | [ORCHESTRATION_V2_WS_METHODS.getWorkflowScript]: "orchestration", |
| 17 | [ORCHESTRATION_V2_WS_METHODS.getNativeChildWork]: "orchestration", |
| 18 | [ORCHESTRATION_V2_WS_METHODS.stopWorkflow]: "orchestration", |
| 19 | [ORCHESTRATION_V2_WS_METHODS.forceStopThread]: "orchestration", |
| 20 | [ORCHESTRATION_V2_WS_METHODS.getTurnItem]: "orchestration", |
| 21 | [ORCHESTRATION_V2_WS_METHODS.getTurnDiff]: "orchestration", |
| 22 | [ORCHESTRATION_V2_WS_METHODS.getFullThreadDiff]: "orchestration", |
| 23 | [ORCHESTRATION_V2_WS_METHODS.searchThreads]: "orchestration", |
| 24 | [ORCHESTRATION_V2_WS_METHODS.searchThread]: "orchestration", |
| 25 | [ORCHESTRATION_V2_WS_METHODS.searchThreadStream]: "orchestration", |
| 26 | [ORCHESTRATION_V2_WS_METHODS.getArchivedShellSnapshot]: "orchestration", |
| 27 | [ORCHESTRATION_V2_WS_METHODS.getThreadFamily]: "orchestration", |
| 28 | [ORCHESTRATION_V2_WS_METHODS.getThreadTitles]: "orchestration", |
| 29 | [ORCHESTRATION_V2_WS_METHODS.getThreadLinkMetadata]: "orchestration", |
| 30 | [ORCHESTRATION_V2_WS_METHODS.getWorktreeThreadIds]: "orchestration", |
| 31 | [ORCHESTRATION_V2_WS_METHODS.getOlderSettledThreads]: "orchestration", |
| 32 | [ORCHESTRATION_V2_WS_METHODS.getThreadProjection]: "orchestrationV2", |
| 33 | [ORCHESTRATION_V2_WS_METHODS.launchThread]: "orchestration", |
| 34 | [ORCHESTRATION_V2_WS_METHODS.subscribeShell]: "orchestrationV2", |
| 35 | [ORCHESTRATION_V2_WS_METHODS.subscribeThread]: "orchestrationV2", |
| 36 | [ORCHESTRATION_V2_WS_METHODS.subscribeCommandOutput]: "orchestrationV2", |
| 37 | [ORCHESTRATION_V2_WS_METHODS.subscribePromptSuggestion]: "orchestrationV2", |
| 38 | [WS_METHODS.projectsMutate]: "orchestration", |
| 39 | [WS_METHODS.serverProbe]: "server", |
| 40 | [WS_METHODS.serverGetConfig]: "server", |
| 41 | [WS_METHODS.serverGetNpmRelease]: "server", |
| 42 | [WS_METHODS.serverRefreshProviders]: "server", |
| 43 | [WS_METHODS.serverUpdateProvider]: "server", |
| 44 | [WS_METHODS.providerAuthStart]: "provider", |
| 45 | [WS_METHODS.providerConsumeResetCredit]: "provider", |
| 46 | [WS_METHODS.providerAuthComplete]: "provider", |
| 47 | [WS_METHODS.providerMcpSignInStart]: "provider", |
| 48 | [WS_METHODS.providerMcpSignInComplete]: "provider", |
| 49 | [WS_METHODS.chatGptReconnectProfile]: "provider", |
| 50 | [WS_METHODS.chatGptImportProfile]: "provider", |
| 51 | [WS_METHODS.chatGptHandoffSubscribe]: "provider", |
| 52 | [WS_METHODS.providerAuthRespond]: "provider", |
| 53 | [WS_METHODS.providerAuthCancel]: "provider", |
| 54 | [WS_METHODS.providerAuthRemoveMethod]: "provider", |
| 55 | [WS_METHODS.providerAuthLogout]: "provider", |
| 56 | [WS_METHODS.providerAuthSubscribe]: "provider", |
| 57 | [WS_METHODS.providerInstallStart]: "provider", |
| 58 | [WS_METHODS.providerInstallCancel]: "provider", |
| 59 | [WS_METHODS.providerInstallSubscribe]: "provider", |
| 60 | [WS_METHODS.providerInstallRemove]: "provider", |
| 61 | [WS_METHODS.serverUpdateServer]: "server", |
| 62 | [WS_METHODS.serverUpdateServerWithProgress]: "server", |
| 63 | [WS_METHODS.serverCommitDesktopUpdate]: "server", |
| 64 | [WS_METHODS.serverUpsertKeybinding]: "server", |
| 65 | [WS_METHODS.serverRemoveKeybinding]: "server", |
| 66 | [WS_METHODS.serverRunStorageCleanup]: "server", |
| 67 | [WS_METHODS.serverGetStorageCleanupReport]: "server", |
| 68 | [WS_METHODS.serverGetDatabaseStorage]: "server", |
| 69 | [WS_METHODS.serverReclaimDatabaseSpace]: "server", |
| 70 | [WS_METHODS.peerLinksList]: "peerLinks", |
| 71 | [WS_METHODS.peerLinksNames]: "peerLinks", |
| 72 | [WS_METHODS.peerLinksLink]: "peerLinks", |
| 73 | [WS_METHODS.peerLinksUnlink]: "peerLinks", |
| 74 | [WS_METHODS.peerLinksAnswerRequest]: "peerLinks", |
| 75 | [WS_METHODS.peerLinksSubscribeThread]: "peerLinks", |
| 76 | [WS_METHODS.peerLinksThreadHistoryPage]: "peerLinks", |
| 77 | [WS_METHODS.peerLinksThreadItem]: "peerLinks", |
| 78 | [WS_METHODS.peerLinksPairClient]: "peerLinks", |
| 79 | [WS_METHODS.serverGetSettings]: "server", |
| 80 | [WS_METHODS.serverUpdateSettings]: "server", |
| 81 | [WS_METHODS.serverSearchAcpRegistry]: "server", |
| 82 | [WS_METHODS.serverPrepareAcpRegistryAgent]: "server", |
| 83 | [WS_METHODS.serverUninstallAcpRegistryManagedBinary]: "server", |
| 84 | [WS_METHODS.serverAcceptAcpRegistryUrlAuth]: "server", |
| 85 | [WS_METHODS.serverListAcpRegistrySessions]: "server", |
| 86 | [WS_METHODS.serverImportAcpRegistrySession]: "server", |
| 87 | [WS_METHODS.serverDeleteAcpRegistrySession]: "server", |
| 88 | [WS_METHODS.serverListAcpRegistryProviders]: "server", |
| 89 | [WS_METHODS.serverSetAcpRegistryProvider]: "server", |
| 90 | [WS_METHODS.serverDisableAcpRegistryProvider]: "server", |
| 91 | [WS_METHODS.serverLogoutAcpRegistry]: "server", |
| 92 | [WS_METHODS.serverDiscoverSourceControl]: "server", |
| 93 | [WS_METHODS.serverCheckSecretSource]: "server", |
| 94 | [WS_METHODS.serverGetTraceDiagnostics]: "server", |
| 95 | [WS_METHODS.serverGetProcessDiagnostics]: "server", |
| 96 | [WS_METHODS.serverGetHostResources]: "server", |
| 97 | [WS_METHODS.serverGetProcessResourceHistory]: "server", |
| 98 | [WS_METHODS.serverGetResourceTelemetryHistory]: "server", |
| 99 | [WS_METHODS.serverRetryResourceTelemetry]: "server", |
| 100 | [WS_METHODS.serverCheckOtlpEndpoint]: "server", |
| 101 | [WS_METHODS.serverListTextGeneratorModels]: "server", |
| 102 | [WS_METHODS.serverPrepareTriage]: "server", |
| 103 | [WS_METHODS.serverGetUsageSummary]: "server", |
| 104 | [WS_METHODS.serverRefreshUsageRates]: "server", |
| 105 | [WS_METHODS.serverSignalProcess]: "server", |
| 106 | [WS_METHODS.serverReportClientActivity]: "server", |
| 107 | [WS_METHODS.scheduledTasksSubscribe]: "scheduledTasks", |
| 108 | [WS_METHODS.scheduledTasksUpsert]: "scheduledTasks", |
| 109 | [WS_METHODS.scheduledTasksSetEnabled]: "scheduledTasks", |
| 110 | [WS_METHODS.scheduledTasksDelete]: "scheduledTasks", |
| 111 | [WS_METHODS.scheduledTasksRunNow]: "scheduledTasks", |
| 112 | [WS_METHODS.scheduledTasksRotateWebhookToken]: "scheduledTasks", |
| 113 | [WS_METHODS.scheduledTasksListWebhookDeliveries]: "scheduledTasks", |
| 114 | [WS_METHODS.scheduledTasksGetWebhookDelivery]: "scheduledTasks", |
| 115 | [WS_METHODS.secretsAnswerRequest]: "secrets", |
| 116 | [WS_METHODS.guestShareCreate]: "guest-share", |
| 117 | [WS_METHODS.cloudGetRelayClientStatus]: "cloud", |
| 118 | [WS_METHODS.cloudInstallRelayClient]: "cloud", |
| 119 | [WS_METHODS.pullRequestsList]: "pull-requests", |
| 120 | [WS_METHODS.pullRequestsListStats]: "pull-requests", |
| 121 | [WS_METHODS.pullRequestsFilterOptions]: "pull-requests", |
| 122 | [WS_METHODS.pullRequestsSummary]: "pull-requests", |
| 123 | [WS_METHODS.pullRequestsRouting]: "pull-requests", |
| 124 | [WS_METHODS.pullRequestsRoutingIdentity]: "pull-requests", |
| 125 | [WS_METHODS.pullRequestsStack]: "pull-requests", |
| 126 | [WS_METHODS.pullRequestsLinkedThreads]: "pull-requests", |
| 127 | [WS_METHODS.pullRequestsDetail]: "pull-requests", |
| 128 | [WS_METHODS.pullRequestsPreview]: "pull-requests", |
| 129 | [WS_METHODS.pullRequestsChecks]: "pull-requests", |
| 130 | [WS_METHODS.pullRequestsActivity]: "pull-requests", |
| 131 | [WS_METHODS.pullRequestsReviewGuide]: "pull-requests", |
| 132 | [WS_METHODS.pullRequestsGenerateReviewGuide]: "pull-requests", |
| 133 | [WS_METHODS.pullRequestsThreadComments]: "pull-requests", |
| 134 | [WS_METHODS.pullRequestsDiffFileContents]: "pull-requests", |
| 135 | [WS_METHODS.pullRequestsFilesViewed]: "pull-requests", |
| 136 | [WS_METHODS.pullRequestsChangeBreakdown]: "pull-requests", |
| 137 | [WS_METHODS.pullRequestsRunAction]: "pull-requests", |
| 138 | [WS_METHODS.pullRequestsUpdate]: "pull-requests", |
| 139 | [WS_METHODS.pullRequestsComment]: "pull-requests", |
| 140 | [WS_METHODS.pullRequestsUpdateComment]: "pull-requests", |
| 141 | [WS_METHODS.pullRequestsSubmitReview]: "pull-requests", |
| 142 | [WS_METHODS.pullRequestsReplyToThread]: "pull-requests", |
| 143 | [WS_METHODS.pullRequestsSetThreadResolution]: "pull-requests", |
| 144 | [WS_METHODS.pullRequestsSetReaction]: "pull-requests", |
| 145 | [WS_METHODS.pullRequestsSetFilesViewed]: "pull-requests", |
| 146 | [WS_METHODS.pullRequestsInvalidate]: "pull-requests", |
| 147 | [WS_METHODS.pullRequestsReportState]: "pull-requests", |
| 148 | [WS_METHODS.pullRequestsSubscribeRefreshes]: "pull-requests", |
| 149 | [WS_METHODS.pullRequestsSubscribeLinkedThreadRefreshes]: "pull-requests", |
| 150 | [WS_METHODS.pullRequestsReviewerCandidates]: "pull-requests", |
| 151 | [WS_METHODS.pullRequestsRequestReviewers]: "pull-requests", |
| 152 | [WS_METHODS.pullRequestsLabelCandidates]: "pull-requests", |
| 153 | [WS_METHODS.pullRequestsSetLabels]: "pull-requests", |
| 154 | [WS_METHODS.issuesList]: "issues", |
| 155 | [WS_METHODS.issuesDetail]: "issues", |
| 156 | [WS_METHODS.issuesComments]: "issues", |
| 157 | [WS_METHODS.issuesTemplates]: "issues", |
| 158 | [WS_METHODS.issuesCandidates]: "issues", |
| 159 | [WS_METHODS.issuesLinkedThreads]: "issues", |
| 160 | [WS_METHODS.issuesCreate]: "issues", |
| 161 | [WS_METHODS.issuesComment]: "issues", |
| 162 | [WS_METHODS.issuesUpdate]: "issues", |
| 163 | [WS_METHODS.issuesSetState]: "issues", |
| 164 | [WS_METHODS.issuesReact]: "issues", |
| 165 | [WS_METHODS.issueTrackersList]: "issues", |
| 166 | [WS_METHODS.issueTrackersConnect]: "issues", |
| 167 | [WS_METHODS.issueTrackersBindProject]: "issues", |
| 168 | [WS_METHODS.sourceControlLookupRepository]: "source-control", |
| 169 | [WS_METHODS.sourceControlCloneRepository]: "source-control", |
| 170 | [WS_METHODS.sourceControlPublishRepository]: "source-control", |
| 171 | [WS_METHODS.sourceControlSignInStart]: "source-control", |
| 172 | [WS_METHODS.sourceControlSignInWait]: "source-control", |
| 173 | [WS_METHODS.sourceControlSignInCancel]: "source-control", |
| 174 | [WS_METHODS.sourceControlAppAccess]: "source-control", |
| 175 | [WS_METHODS.sourceControlStarred]: "source-control", |
| 176 | [WS_METHODS.sourceControlStar]: "source-control", |
| 177 | [WS_METHODS.projectCloneStart]: "source-control", |
| 178 | [WS_METHODS.projectCloneCancel]: "source-control", |
| 179 | [WS_METHODS.projectCloneRetry]: "source-control", |
| 180 | [WS_METHODS.subscribeProjectClones]: "source-control", |
| 181 | [WS_METHODS.projectsListEntries]: "workspace", |
| 182 | [WS_METHODS.projectsListIgnoredFiles]: "workspace", |
| 183 | [WS_METHODS.projectsReadFile]: "workspace", |
| 184 | [WS_METHODS.projectsSearchContents]: "workspace", |
| 185 | [WS_METHODS.projectsSearchEntries]: "workspace", |
| 186 | [WS_METHODS.projectsWriteFile]: "workspace", |
| 187 | [WS_METHODS.projectsEnsureScratch]: "orchestration", |
| 188 | [WS_METHODS.projectsCreateNew]: "orchestration", |
| 189 | [WS_METHODS.projectsCreateFromThread]: "orchestration", |
| 190 | [WS_METHODS.shellOpenInEditor]: "workspace", |
| 191 | [WS_METHODS.filesystemBrowse]: "workspace", |
| 192 | [WS_METHODS.filesystemGetMetadata]: "workspace", |
| 193 | [WS_METHODS.agentSessionsScan]: "workspace", |
| 194 | [WS_METHODS.agentSessionsImport]: "workspace", |
| 195 | [WS_METHODS.assetsCreateUrl]: "workspace", |
| 196 | [WS_METHODS.assetsPersistChatAttachments]: "orchestration", |
| 197 | [WS_METHODS.attachmentsCreateUploadUrl]: "workspace", |
| 198 | [WS_METHODS.attachmentsDelete]: "workspace", |
| 199 | [WS_METHODS.providerUploadFeedback]: "provider", |
| 200 | [WS_METHODS.mcpAppsCallTool]: "provider", |
| 201 | [WS_METHODS.mcpAppsToolInfo]: "provider", |
| 202 | [WS_METHODS.mcpAppsUpdateModelContext]: "provider", |
| 203 | [WS_METHODS.mcpAppsReadResource]: "provider", |
| 204 | [WS_METHODS.subscribeVcsStatus]: "vcs", |
| 205 | [WS_METHODS.subscribeVcsBranch]: "vcs", |
| 206 | [WS_METHODS.vcsRefreshBranch]: "vcs", |
| 207 | [WS_METHODS.subscribeWorktreeSetup]: "vcs", |
| 208 | [WS_METHODS.worktreeSetupCancel]: "vcs", |
| 209 | [WS_METHODS.subscribeResourceTelemetry]: "server", |
| 210 | [WS_METHODS.vcsRefreshStatus]: "vcs", |
| 211 | [WS_METHODS.vcsPull]: "git", |
| 212 | [WS_METHODS.gitRunStackedAction]: "vcs", |
| 213 | [WS_METHODS.gitResolvePullRequest]: "git", |
| 214 | [WS_METHODS.gitPreparePullRequestThread]: "git", |
| 215 | [WS_METHODS.vcsListRefs]: "vcs", |
| 216 | [WS_METHODS.vcsCreateWorktree]: "vcs", |
| 217 | [WS_METHODS.vcsRemoveWorktree]: "vcs", |
| 218 | [WS_METHODS.vcsCreateRef]: "vcs", |
| 219 | [WS_METHODS.vcsSwitchRef]: "vcs", |
| 220 | [WS_METHODS.vcsInit]: "vcs", |
| 221 | [WS_METHODS.reviewGetBranchCommits]: "review", |
| 222 | [WS_METHODS.reviewGetDiffPreview]: "review", |
| 223 | [WS_METHODS.reviewGetDiffFileContents]: "review", |
| 224 | [WS_METHODS.terminalOpen]: "terminal", |
| 225 | [WS_METHODS.terminalAttach]: "terminal", |
| 226 | [WS_METHODS.terminalObserve]: "terminal", |
| 227 | [WS_METHODS.terminalWrite]: "terminal", |
| 228 | [WS_METHODS.terminalResize]: "terminal", |
| 229 | [WS_METHODS.terminalClear]: "terminal", |
| 230 | [WS_METHODS.terminalClose]: "terminal", |
| 231 | [WS_METHODS.subscribeTerminalMetadata]: "terminal", |
| 232 | [WS_METHODS.previewOpen]: "preview", |
| 233 | [WS_METHODS.previewResize]: "preview", |
| 234 | [WS_METHODS.previewAdjust]: "preview", |
| 235 | [WS_METHODS.previewClose]: "preview", |
| 236 | [WS_METHODS.previewList]: "preview", |
| 237 | [WS_METHODS.previewClearProfile]: "preview", |
| 238 | [WS_METHODS.previewReportProfiles]: "preview", |
| 239 | [WS_METHODS.previewReportStatus]: "preview", |
| 240 | [WS_METHODS.subscribePreviewEvents]: "preview", |
| 241 | [WS_METHODS.subscribeDiscoveredLocalServers]: "preview", |
| 242 | [WS_METHODS.previewStopLocalServer]: "preview", |
| 243 | [WS_METHODS.deviceConfigure]: "device", |
| 244 | [WS_METHODS.deviceTestHost]: "device", |
| 245 | [WS_METHODS.deviceEasAccount]: "device", |
| 246 | [WS_METHODS.deviceSimlockIdentity]: "device", |
| 247 | [WS_METHODS.deviceList]: "device", |
| 248 | [WS_METHODS.deviceOpen]: "device", |
| 249 | [WS_METHODS.deviceClose]: "device", |
| 250 | [WS_METHODS.deviceShutdown]: "device", |
| 251 | [WS_METHODS.deviceDetail]: "device", |
| 252 | [WS_METHODS.deviceAction]: "device", |
| 253 | [WS_METHODS.subscribeDeviceState]: "device", |
| 254 | [WS_METHODS.subscribeServerConfig]: "server", |
| 255 | [WS_METHODS.subscribeServerLifecycle]: "server", |
| 256 | [WS_METHODS.subscribeAuthAccess]: "auth", |
| 257 | } as const satisfies Readonly<Record<WsRpcMethod, string>>; |
| 258 | |
| 259 | const RPC_SPAN_PREFIX = "ws.rpc"; |
| 260 | const DEFAULT_RPC_SPAN_ATTRIBUTES = { |
| 261 | "rpc.transport": "websocket", |
| 262 | "rpc.system": "effect-rpc", |
| 263 | } as const; |
| 264 | const RPC_METHODS_WITH_TRACING_DISABLED: ReadonlySet<string> = new Set([ |
| 265 | WS_METHODS.serverGetTraceDiagnostics, |
| 266 | WS_METHODS.serverGetProcessDiagnostics, |
| 267 | WS_METHODS.serverGetProcessResourceHistory, |
| 268 | WS_METHODS.serverSignalProcess, |
| 269 | ]); |
| 270 | |
| 271 | /** Add after RpcScopeAuthorization to wrap authorization and record rejected calls. */ |
| 272 | export class RpcInstrumentation extends RpcMiddleware.Service<RpcInstrumentation>()( |
| 273 | "t3/server/RpcInstrumentation", |
| 274 | ) {} |
| 275 | |
| 276 | export const rpcInstrumentationLayer = Layer.succeed(RpcInstrumentation)((effect, { rpc }) => { |
| 277 | const method = rpc._tag; |
| 278 | const measured = effect.pipe( |
| 279 | withMetrics({ counter: rpcRequestsTotal, timer: rpcRequestDuration, attributes: { method } }), |
| 280 | ); |
| 281 | |
| 282 | if (RPC_METHODS_WITH_TRACING_DISABLED.has(method)) { |
| 283 | return measured.pipe(Effect.provideService(References.TracerEnabled, false)); |
| 284 | } |
| 285 | // The ambient span is the socket's `GET /ws` request, open for the whole connection. Parenting |
| 286 | // every call to it grows one trace past the collector's per-trace limit, which then drops it. |
| 287 | return Effect.currentParentSpan.pipe( |
| 288 | Effect.option, |
| 289 | Effect.flatMap((connection) => |
| 290 | measured.pipe( |
| 291 | Effect.withSpan(`${RPC_SPAN_PREFIX}.${method}`, { |
| 292 | root: true, |
| 293 | links: Option.match(connection, { |
| 294 | onNone: () => [], |
| 295 | onSome: (span) => [{ span, attributes: {} }], |
| 296 | }), |
| 297 | attributes: { |
| 298 | ...DEFAULT_RPC_SPAN_ATTRIBUTES, |
| 299 | "rpc.method": method, |
| 300 | "rpc.aggregate": RPC_AGGREGATES[method as WsRpcMethod], |
| 301 | }, |
| 302 | }), |
| 303 | ), |
| 304 | ), |
| 305 | ); |
| 306 | }); |