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, 04:45 UTC
| 1 | import { MarkedText, markedTextEnglish, translatableMessage } from "../errors/translatable.ts"; |
| 2 | import { msg } from "gt-i18n"; |
| 3 | import { |
| 4 | EnvironmentHttpApi, |
| 5 | EnvironmentHttpCommonError, |
| 6 | type EnvironmentAuthInvalidError, |
| 7 | type EnvironmentConnectApprovalPendingError, |
| 8 | type EnvironmentInternalError, |
| 9 | type EnvironmentOperationForbiddenError, |
| 10 | type EnvironmentRequestInvalidError, |
| 11 | type EnvironmentResourceNotFoundError, |
| 12 | type EnvironmentScopeRequiredError, |
| 13 | } from "@t3tools/contracts"; |
| 14 | import * as HttpObservability from "@t3tools/shared/httpObservability"; |
| 15 | import * as Duration from "effect/Duration"; |
| 16 | import * as Effect from "effect/Effect"; |
| 17 | import * as Layer from "effect/Layer"; |
| 18 | import * as Option from "effect/Option"; |
| 19 | import * as Schema from "effect/Schema"; |
| 20 | import { FetchHttpClient, HttpClient, HttpClientError } from "effect/http"; |
| 21 | import * as HttpApiClient from "effect/http-api/HttpApiClient"; |
| 22 | |
| 23 | const isEnvironmentHttpCommonError = Schema.is(EnvironmentHttpCommonError); |
| 24 | |
| 25 | export class RemoteEnvironmentAuthFetchError extends Schema.TaggedError<RemoteEnvironmentAuthFetchError>()( |
| 26 | "RemoteEnvironmentAuthFetchError", |
| 27 | { |
| 28 | message: Schema.String, |
| 29 | i18nMessage: Schema.optionalKey(MarkedText), |
| 30 | cause: Schema.Defect(), |
| 31 | }, |
| 32 | ) {} |
| 33 | |
| 34 | class RemoteEnvironmentAuthInvalidJsonError extends Schema.TaggedError<RemoteEnvironmentAuthInvalidJsonError>()( |
| 35 | "RemoteEnvironmentAuthInvalidJsonError", |
| 36 | { |
| 37 | message: Schema.String, |
| 38 | i18nMessage: Schema.optionalKey(MarkedText), |
| 39 | cause: Schema.Defect(), |
| 40 | }, |
| 41 | ) {} |
| 42 | |
| 43 | export class RemoteEnvironmentAuthUndeclaredStatusError extends Schema.TaggedError<RemoteEnvironmentAuthUndeclaredStatusError>()( |
| 44 | "RemoteEnvironmentAuthUndeclaredStatusError", |
| 45 | { |
| 46 | status: Schema.Number, |
| 47 | requestUrl: Schema.String, |
| 48 | }, |
| 49 | ) { |
| 50 | override get message(): string { |
| 51 | return markedTextEnglish(this.i18nMessage); |
| 52 | } |
| 53 | |
| 54 | get i18nMessage(): MarkedText { |
| 55 | return [ |
| 56 | msg("Remote environment endpoint {url} returned undeclared status {status}.", { |
| 57 | url: this.requestUrl, |
| 58 | status: String(this.status), |
| 59 | }), |
| 60 | ]; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | export class RemoteEnvironmentAuthTimeoutError extends Schema.TaggedError<RemoteEnvironmentAuthTimeoutError>()( |
| 65 | "RemoteEnvironmentAuthTimeoutError", |
| 66 | { |
| 67 | requestUrl: Schema.String, |
| 68 | timeoutMs: Schema.Number, |
| 69 | }, |
| 70 | ) { |
| 71 | override get message(): string { |
| 72 | return markedTextEnglish(this.i18nMessage); |
| 73 | } |
| 74 | |
| 75 | get i18nMessage(): MarkedText { |
| 76 | return [ |
| 77 | msg("Remote environment endpoint {url} timed out after {timeoutMs}ms.", { |
| 78 | url: this.requestUrl, |
| 79 | timeoutMs: String(this.timeoutMs), |
| 80 | }), |
| 81 | ]; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | const isRemoteEnvironmentAuthTimeoutError = Schema.is(RemoteEnvironmentAuthTimeoutError); |
| 86 | |
| 87 | export type RemoteEnvironmentRequestError = |
| 88 | | EnvironmentRequestInvalidError |
| 89 | | EnvironmentAuthInvalidError |
| 90 | | EnvironmentScopeRequiredError |
| 91 | | EnvironmentOperationForbiddenError |
| 92 | | EnvironmentResourceNotFoundError |
| 93 | | EnvironmentInternalError |
| 94 | | EnvironmentConnectApprovalPendingError |
| 95 | | RemoteEnvironmentAuthFetchError |
| 96 | | RemoteEnvironmentAuthInvalidJsonError |
| 97 | | RemoteEnvironmentAuthUndeclaredStatusError |
| 98 | | RemoteEnvironmentAuthTimeoutError; |
| 99 | |
| 100 | export const layerRemoteHttpClient = ( |
| 101 | fetchFn: typeof globalThis.fetch, |
| 102 | ): Layer.Layer<HttpClient.HttpClient> => |
| 103 | Layer.merge( |
| 104 | FetchHttpClient.layer.pipe(Layer.provide(Layer.succeed(FetchHttpClient.Fetch, fetchFn))), |
| 105 | HttpObservability.layer, |
| 106 | ); |
| 107 | |
| 108 | const remoteApiBaseUrl = (httpBaseUrl: string): string => { |
| 109 | const url = new URL(httpBaseUrl); |
| 110 | url.pathname = "/"; |
| 111 | url.search = ""; |
| 112 | url.hash = ""; |
| 113 | return url.toString(); |
| 114 | }; |
| 115 | |
| 116 | export const makeEnvironmentHttpApiClient = (httpBaseUrl: string) => |
| 117 | HttpApiClient.make(EnvironmentHttpApi, { |
| 118 | baseUrl: remoteApiBaseUrl(httpBaseUrl), |
| 119 | }); |
| 120 | |
| 121 | export const makeEnvironmentHttpApiGroupClient = < |
| 122 | Group extends keyof typeof EnvironmentHttpApi.groups, |
| 123 | >( |
| 124 | httpBaseUrl: string, |
| 125 | group: Group, |
| 126 | ) => |
| 127 | Effect.flatMap(HttpClient.HttpClient, (httpClient) => |
| 128 | HttpApiClient.group(EnvironmentHttpApi, { |
| 129 | httpClient, |
| 130 | group, |
| 131 | baseUrl: remoteApiBaseUrl(httpBaseUrl), |
| 132 | }), |
| 133 | ); |
| 134 | |
| 135 | export const makeEnvironmentHttpApiUrlBuilder = (httpBaseUrl: string) => |
| 136 | HttpApiClient.urlBuilder(EnvironmentHttpApi, { |
| 137 | baseUrl: remoteApiBaseUrl(httpBaseUrl), |
| 138 | }); |
| 139 | |
| 140 | const failRemoteRequest = ( |
| 141 | requestUrl: string, |
| 142 | cause: unknown, |
| 143 | ): Effect.Effect<never, RemoteEnvironmentRequestError> => { |
| 144 | if (isRemoteEnvironmentAuthTimeoutError(cause)) { |
| 145 | return Effect.fail(cause); |
| 146 | } |
| 147 | if (isEnvironmentHttpCommonError(cause)) { |
| 148 | return Effect.fail(cause); |
| 149 | } |
| 150 | if (Schema.isSchemaError(cause)) { |
| 151 | return Effect.fail( |
| 152 | new RemoteEnvironmentAuthInvalidJsonError({ |
| 153 | ...translatableMessage( |
| 154 | msg("Remote environment endpoint returned an invalid response from {url}.", { |
| 155 | url: requestUrl, |
| 156 | }), |
| 157 | ), |
| 158 | cause, |
| 159 | }), |
| 160 | ); |
| 161 | } |
| 162 | if (HttpClientError.isHttpClientError(cause) && cause.response !== undefined) { |
| 163 | const response = cause.response; |
| 164 | if (response.status < 200 || response.status >= 300) { |
| 165 | return Effect.fail( |
| 166 | new RemoteEnvironmentAuthUndeclaredStatusError({ requestUrl, status: response.status }), |
| 167 | ); |
| 168 | } |
| 169 | return Effect.fail( |
| 170 | new RemoteEnvironmentAuthInvalidJsonError({ |
| 171 | ...translatableMessage( |
| 172 | msg("Remote environment endpoint returned an invalid response from {url}.", { |
| 173 | url: requestUrl, |
| 174 | }), |
| 175 | ), |
| 176 | cause, |
| 177 | }), |
| 178 | ); |
| 179 | } |
| 180 | return Effect.fail( |
| 181 | new RemoteEnvironmentAuthFetchError({ |
| 182 | ...translatableMessage( |
| 183 | msg("Failed to fetch remote environment endpoint {url} ({cause}).", { |
| 184 | url: requestUrl, |
| 185 | cause: String(cause), |
| 186 | }), |
| 187 | ), |
| 188 | cause, |
| 189 | }), |
| 190 | ); |
| 191 | }; |
| 192 | |
| 193 | export const executeEnvironmentHttpRequest = <A, E, R>( |
| 194 | requestUrl: string, |
| 195 | timeoutMs: number, |
| 196 | request: Effect.Effect<A, E, R>, |
| 197 | ): Effect.Effect<A, RemoteEnvironmentRequestError, R> => |
| 198 | request.pipe( |
| 199 | Effect.timeoutOption(Duration.millis(timeoutMs)), |
| 200 | Effect.flatMap( |
| 201 | Option.match({ |
| 202 | onNone: () => Effect.fail(new RemoteEnvironmentAuthTimeoutError({ requestUrl, timeoutMs })), |
| 203 | onSome: Effect.succeed, |
| 204 | }), |
| 205 | ), |
| 206 | Effect.catch((cause) => failRemoteRequest(requestUrl, cause)), |
| 207 | ); |