The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | bootstrapRemoteBearerSession, |
| 3 | fetchRemoteSessionState, |
| 4 | RemoteEnvironmentAuthUndeclaredStatusError, |
| 5 | type RemoteEnvironmentAuthError, |
| 6 | } from "@t3tools/client-runtime/authorization"; |
| 7 | import { fetchRemoteEnvironmentDescriptor } from "@t3tools/client-runtime/environment"; |
| 8 | import { |
| 9 | EnvironmentAuthInvalidError, |
| 10 | DesktopDiscoveredSshHostSchema, |
| 11 | DesktopSshBearerBootstrapInputSchema, |
| 12 | DesktopSshBearerRequestInputSchema, |
| 13 | DesktopSshEnvironmentEnsureInputSchema, |
| 14 | DesktopSshEnvironmentEnsureResultSchema, |
| 15 | DesktopSshEnvironmentTargetSchema, |
| 16 | DesktopSshHttpBaseUrlInputSchema, |
| 17 | DesktopSshPasswordPromptCancelledType, |
| 18 | DesktopSshPasswordPromptResolutionInputSchema, |
| 19 | ExecutionEnvironmentDescriptor, |
| 20 | EnvironmentInternalError, |
| 21 | EnvironmentOperationForbiddenError, |
| 22 | EnvironmentRequestInvalidError, |
| 23 | EnvironmentScopeRequiredError, |
| 24 | AuthAccessTokenResult, |
| 25 | AuthSessionState, |
| 26 | } from "@t3tools/contracts"; |
| 27 | import { SshHttpBridgeError } from "@t3tools/ssh/errors"; |
| 28 | import { resolveLoopbackSshHttpBaseUrl } from "@t3tools/ssh/tunnel"; |
| 29 | import * as Effect from "effect/Effect"; |
| 30 | import * as Schema from "effect/Schema"; |
| 31 | |
| 32 | import * as IpcChannels from "../channels.ts"; |
| 33 | import * as DesktopIpc from "../DesktopIpc.ts"; |
| 34 | import * as DesktopSshEnvironment from "../../ssh/DesktopSshEnvironment.ts"; |
| 35 | import * as DesktopSshPasswordPrompts from "../../ssh/DesktopSshPasswordPrompts.ts"; |
| 36 | |
| 37 | const DesktopSshEnvironmentRequestOperation = Schema.Literals([ |
| 38 | "fetch-environment-descriptor", |
| 39 | "bootstrap-bearer-session", |
| 40 | "fetch-session-state", |
| 41 | ]); |
| 42 | type DesktopSshEnvironmentRequestOperation = typeof DesktopSshEnvironmentRequestOperation.Type; |
| 43 | |
| 44 | type DesktopSshEnvironmentRequestCause = RemoteEnvironmentAuthError | SshHttpBridgeError; |
| 45 | |
| 46 | const isEnvironmentAuthInvalidError = Schema.is(EnvironmentAuthInvalidError); |
| 47 | const isEnvironmentInternalError = Schema.is(EnvironmentInternalError); |
| 48 | const isEnvironmentOperationForbiddenError = Schema.is(EnvironmentOperationForbiddenError); |
| 49 | const isEnvironmentRequestInvalidError = Schema.is(EnvironmentRequestInvalidError); |
| 50 | const isEnvironmentScopeRequiredError = Schema.is(EnvironmentScopeRequiredError); |
| 51 | const isRemoteEnvironmentAuthUndeclaredStatusError = Schema.is( |
| 52 | RemoteEnvironmentAuthUndeclaredStatusError, |
| 53 | ); |
| 54 | const isSshHttpBridgeError = Schema.is(SshHttpBridgeError); |
| 55 | |
| 56 | function readSshHttpStatus(cause: DesktopSshEnvironmentRequestCause): number | null { |
| 57 | if (isRemoteEnvironmentAuthUndeclaredStatusError(cause) || isSshHttpBridgeError(cause)) { |
| 58 | return cause.status ?? null; |
| 59 | } |
| 60 | if (isEnvironmentRequestInvalidError(cause)) { |
| 61 | return 400; |
| 62 | } |
| 63 | if (isEnvironmentAuthInvalidError(cause)) { |
| 64 | return 401; |
| 65 | } |
| 66 | if (isEnvironmentScopeRequiredError(cause)) { |
| 67 | return 403; |
| 68 | } |
| 69 | if (isEnvironmentOperationForbiddenError(cause)) { |
| 70 | return 403; |
| 71 | } |
| 72 | if (isEnvironmentInternalError(cause)) { |
| 73 | return 500; |
| 74 | } |
| 75 | return null; |
| 76 | } |
| 77 | |
| 78 | export class DesktopSshEnvironmentRequestError extends Schema.TaggedError<DesktopSshEnvironmentRequestError>()( |
| 79 | "DesktopSshEnvironmentRequestError", |
| 80 | { |
| 81 | operation: DesktopSshEnvironmentRequestOperation, |
| 82 | cause: Schema.Defect(), |
| 83 | sshHttpStatus: Schema.NullOr(Schema.Number), |
| 84 | }, |
| 85 | ) { |
| 86 | // The web client matches "[ssh_http:500]" to explain version-skewed SSH pairing. |
| 87 | override get message() { |
| 88 | const prefix = this.sshHttpStatus === null ? "" : `[ssh_http:${this.sshHttpStatus}] `; |
| 89 | return `${prefix}SSH remote API request failed during ${this.operation}.`; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | const withLoopbackSshApi = |
| 94 | <A, R>( |
| 95 | operation: DesktopSshEnvironmentRequestOperation, |
| 96 | use: (httpBaseUrl: string) => Effect.Effect<A, RemoteEnvironmentAuthError, R>, |
| 97 | ) => |
| 98 | (httpBaseUrl: string): Effect.Effect<A, DesktopSshEnvironmentRequestError, R> => |
| 99 | resolveLoopbackSshHttpBaseUrl(httpBaseUrl).pipe( |
| 100 | Effect.flatMap(use), |
| 101 | Effect.mapError( |
| 102 | (cause) => |
| 103 | new DesktopSshEnvironmentRequestError({ |
| 104 | operation, |
| 105 | cause, |
| 106 | sshHttpStatus: readSshHttpStatus(cause), |
| 107 | }), |
| 108 | ), |
| 109 | ); |
| 110 | |
| 111 | export const discoverSshHosts = DesktopIpc.makeIpcMethod({ |
| 112 | channel: IpcChannels.DISCOVER_SSH_HOSTS_CHANNEL, |
| 113 | payload: Schema.Void, |
| 114 | result: Schema.Array(DesktopDiscoveredSshHostSchema), |
| 115 | handler: Effect.fn("desktop.ipc.sshEnvironment.discoverHosts")(function* () { |
| 116 | const sshEnvironment = yield* DesktopSshEnvironment.DesktopSshEnvironment; |
| 117 | return yield* sshEnvironment.discoverHosts(); |
| 118 | }), |
| 119 | }); |
| 120 | |
| 121 | export const resolveSshHost = DesktopIpc.makeIpcMethod({ |
| 122 | channel: IpcChannels.RESOLVE_SSH_HOST_CHANNEL, |
| 123 | payload: Schema.String, |
| 124 | result: DesktopSshEnvironmentTargetSchema, |
| 125 | handler: Effect.fn("desktop.ipc.sshEnvironment.resolveHost")(function* (alias) { |
| 126 | const sshEnvironment = yield* DesktopSshEnvironment.DesktopSshEnvironment; |
| 127 | return yield* sshEnvironment.resolveHost(alias); |
| 128 | }), |
| 129 | }); |
| 130 | |
| 131 | export const ensureSshEnvironment = DesktopIpc.makeIpcMethod({ |
| 132 | channel: IpcChannels.ENSURE_SSH_ENVIRONMENT_CHANNEL, |
| 133 | payload: DesktopSshEnvironmentEnsureInputSchema, |
| 134 | result: DesktopSshEnvironmentEnsureResultSchema, |
| 135 | handler: Effect.fn("desktop.ipc.sshEnvironment.ensureEnvironment")(function* ({ |
| 136 | target, |
| 137 | options, |
| 138 | }) { |
| 139 | const sshEnvironment = yield* DesktopSshEnvironment.DesktopSshEnvironment; |
| 140 | return yield* sshEnvironment.ensureEnvironment(target, options).pipe( |
| 141 | Effect.catchIf(DesktopSshEnvironment.isDesktopSshPasswordPromptCancellation, (error) => |
| 142 | Effect.succeed({ |
| 143 | type: DesktopSshPasswordPromptCancelledType, |
| 144 | message: error.message, |
| 145 | }), |
| 146 | ), |
| 147 | ); |
| 148 | }), |
| 149 | }); |
| 150 | |
| 151 | export const disconnectSshEnvironment = DesktopIpc.makeIpcMethod({ |
| 152 | channel: IpcChannels.DISCONNECT_SSH_ENVIRONMENT_CHANNEL, |
| 153 | payload: DesktopSshEnvironmentTargetSchema, |
| 154 | result: Schema.Void, |
| 155 | handler: Effect.fn("desktop.ipc.sshEnvironment.disconnectEnvironment")(function* (target) { |
| 156 | const sshEnvironment = yield* DesktopSshEnvironment.DesktopSshEnvironment; |
| 157 | yield* sshEnvironment.disconnectEnvironment(target); |
| 158 | }), |
| 159 | }); |
| 160 | |
| 161 | export const fetchSshEnvironmentDescriptor = DesktopIpc.makeIpcMethod({ |
| 162 | channel: IpcChannels.FETCH_SSH_ENVIRONMENT_DESCRIPTOR_CHANNEL, |
| 163 | payload: DesktopSshHttpBaseUrlInputSchema, |
| 164 | result: ExecutionEnvironmentDescriptor, |
| 165 | handler: Effect.fn("desktop.ipc.sshEnvironment.fetchDescriptor")(function* ({ httpBaseUrl }) { |
| 166 | return yield* withLoopbackSshApi("fetch-environment-descriptor", (resolvedHttpBaseUrl) => |
| 167 | fetchRemoteEnvironmentDescriptor({ httpBaseUrl: resolvedHttpBaseUrl }), |
| 168 | )(httpBaseUrl); |
| 169 | }), |
| 170 | }); |
| 171 | |
| 172 | export const bootstrapSshBearerSession = DesktopIpc.makeIpcMethod({ |
| 173 | channel: IpcChannels.BOOTSTRAP_SSH_BEARER_SESSION_CHANNEL, |
| 174 | payload: DesktopSshBearerBootstrapInputSchema, |
| 175 | result: AuthAccessTokenResult, |
| 176 | handler: Effect.fn("desktop.ipc.sshEnvironment.bootstrapBearerSession")(function* ({ |
| 177 | httpBaseUrl, |
| 178 | credential, |
| 179 | }) { |
| 180 | return yield* withLoopbackSshApi("bootstrap-bearer-session", (resolvedHttpBaseUrl) => |
| 181 | bootstrapRemoteBearerSession({ |
| 182 | httpBaseUrl: resolvedHttpBaseUrl, |
| 183 | credential, |
| 184 | }), |
| 185 | )(httpBaseUrl); |
| 186 | }), |
| 187 | }); |
| 188 | |
| 189 | export const fetchSshSessionState = DesktopIpc.makeIpcMethod({ |
| 190 | channel: IpcChannels.FETCH_SSH_SESSION_STATE_CHANNEL, |
| 191 | payload: DesktopSshBearerRequestInputSchema, |
| 192 | result: AuthSessionState, |
| 193 | handler: Effect.fn("desktop.ipc.sshEnvironment.fetchSessionState")(function* ({ |
| 194 | httpBaseUrl, |
| 195 | bearerToken, |
| 196 | }) { |
| 197 | return yield* withLoopbackSshApi("fetch-session-state", (resolvedHttpBaseUrl) => |
| 198 | fetchRemoteSessionState({ |
| 199 | httpBaseUrl: resolvedHttpBaseUrl, |
| 200 | bearerToken, |
| 201 | }), |
| 202 | )(httpBaseUrl); |
| 203 | }), |
| 204 | }); |
| 205 | |
| 206 | export const resolveSshPasswordPrompt = DesktopIpc.makeIpcMethod({ |
| 207 | channel: IpcChannels.RESOLVE_SSH_PASSWORD_PROMPT_CHANNEL, |
| 208 | payload: DesktopSshPasswordPromptResolutionInputSchema, |
| 209 | result: Schema.Void, |
| 210 | handler: Effect.fn("desktop.ipc.sshEnvironment.resolvePasswordPrompt")(function* ({ |
| 211 | requestId, |
| 212 | password, |
| 213 | }) { |
| 214 | const prompts = yield* DesktopSshPasswordPrompts.DesktopSshPasswordPrompts; |
| 215 | yield* prompts.resolve({ requestId, password }); |
| 216 | }), |
| 217 | }); |