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 { |
| 2 | clerkFrontendApiHostnameFromPublishableKey, |
| 3 | relayClerkTokenOptions, |
| 4 | } from "@t3tools/shared/relayAuth"; |
| 5 | import { normalizeSecureRelayUrl } from "@t3tools/shared/relayUrl"; |
| 6 | import * as Schema from "effect/Schema"; |
| 7 | |
| 8 | export class CloudPublicConfigMissingError extends Schema.TaggedError<CloudPublicConfigMissingError>()( |
| 9 | "CloudPublicConfigMissingError", |
| 10 | { |
| 11 | key: Schema.Literal("T3CODE_CLERK_JWT_TEMPLATE"), |
| 12 | }, |
| 13 | ) { |
| 14 | override get message(): string { |
| 15 | return `${this.key} is not configured.`; |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | export interface CloudPublicConfig { |
| 20 | readonly clerkPublishableKey: string | null; |
| 21 | readonly clerkJwtTemplate: string | null; |
| 22 | readonly relayUrl: string | null; |
| 23 | readonly relayTracing: { |
| 24 | readonly tracesUrl: string | null; |
| 25 | readonly tracesDataset: string | null; |
| 26 | readonly tracesToken: string | null; |
| 27 | }; |
| 28 | } |
| 29 | |
| 30 | export function trimNonEmpty(value: string | undefined): string | null { |
| 31 | return value?.trim() || null; |
| 32 | } |
| 33 | |
| 34 | function normalizeSecureUrl(value: string): string | null { |
| 35 | try { |
| 36 | const url = new URL(value); |
| 37 | return url.protocol === "https:" ? url.toString() : null; |
| 38 | } catch { |
| 39 | return null; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | export function resolveCloudPublicConfig(): CloudPublicConfig { |
| 44 | return { |
| 45 | clerkPublishableKey: trimNonEmpty( |
| 46 | import.meta.env.VITE_CLERK_PUBLISHABLE_KEY as string | undefined, |
| 47 | ), |
| 48 | clerkJwtTemplate: trimNonEmpty(import.meta.env.VITE_CLERK_JWT_TEMPLATE as string | undefined), |
| 49 | relayUrl: normalizeSecureRelayUrl( |
| 50 | (import.meta.env.VITE_T3CODE_RELAY_URL as string | undefined) ?? "", |
| 51 | ), |
| 52 | relayTracing: { |
| 53 | tracesUrl: normalizeSecureUrl( |
| 54 | (import.meta.env.VITE_RELAY_OTLP_TRACES_URL as string | undefined) ?? "", |
| 55 | ), |
| 56 | tracesDataset: trimNonEmpty( |
| 57 | import.meta.env.VITE_RELAY_OTLP_TRACES_DATASET as string | undefined, |
| 58 | ), |
| 59 | tracesToken: trimNonEmpty(import.meta.env.VITE_RELAY_OTLP_TRACES_TOKEN as string | undefined), |
| 60 | }, |
| 61 | }; |
| 62 | } |
| 63 | |
| 64 | let desktopRelayTelemetry: boolean | null = null; |
| 65 | |
| 66 | /** Whether relay spans may be exported now; the desktop host may not know yet at startup. */ |
| 67 | export function relayTelemetryEnabled(): boolean | null { |
| 68 | if (typeof window === "undefined" || !window.desktopBridge) return true; |
| 69 | desktopRelayTelemetry ??= window.desktopBridge.getRelayTelemetryEnabled(); |
| 70 | return desktopRelayTelemetry; |
| 71 | } |
| 72 | |
| 73 | export function resolveRelayTracingConfig() { |
| 74 | // The relay exporter is created during module initialization, so this must be synchronous. |
| 75 | if (relayTelemetryEnabled() === false) { |
| 76 | return null; |
| 77 | } |
| 78 | const { relayTracing } = resolveCloudPublicConfig(); |
| 79 | return relayTracing.tracesUrl && relayTracing.tracesDataset && relayTracing.tracesToken |
| 80 | ? { |
| 81 | tracesUrl: relayTracing.tracesUrl, |
| 82 | tracesDataset: relayTracing.tracesDataset, |
| 83 | tracesToken: relayTracing.tracesToken, |
| 84 | } |
| 85 | : null; |
| 86 | } |
| 87 | |
| 88 | export function hasCloudPublicConfig(): boolean { |
| 89 | const config = resolveCloudPublicConfig(); |
| 90 | if (!config.clerkPublishableKey || !config.clerkJwtTemplate || !config.relayUrl) { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | // Electron has its own auth runtime; development keys also support private browser origins. |
| 95 | if ( |
| 96 | typeof window === "undefined" || |
| 97 | window.desktopBridge !== undefined || |
| 98 | !config.clerkPublishableKey.startsWith("pk_live_") |
| 99 | ) { |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | try { |
| 104 | // Custom Frontend API hosts use clerk.<production domain>. |
| 105 | const domain = clerkFrontendApiHostnameFromPublishableKey(config.clerkPublishableKey).replace( |
| 106 | /^clerk\./u, |
| 107 | "", |
| 108 | ); |
| 109 | const { hostname, protocol, port } = window.location; |
| 110 | return ( |
| 111 | protocol === "https:" && |
| 112 | port === "" && |
| 113 | (hostname === domain || hostname.endsWith(`.${domain}`)) |
| 114 | ); |
| 115 | } catch { |
| 116 | return false; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | export function resolveRelayClerkTokenOptions() { |
| 121 | const { clerkJwtTemplate } = resolveCloudPublicConfig(); |
| 122 | if (!clerkJwtTemplate) { |
| 123 | throw new CloudPublicConfigMissingError({ key: "T3CODE_CLERK_JWT_TEMPLATE" }); |
| 124 | } |
| 125 | return relayClerkTokenOptions(clerkJwtTemplate); |
| 126 | } |