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 { thrownErrorMessage } from "@t3tools/client-runtime/errors"; |
| 2 | import { m } from "~/i18n/messages"; |
| 3 | import { |
| 4 | isAtomCommandInterrupted, |
| 5 | settlePromise, |
| 6 | squashAtomCommandFailure, |
| 7 | type AtomCommandResult, |
| 8 | } from "@t3tools/client-runtime/state/runtime"; |
| 9 | import { |
| 10 | ANTIGRAVITY_AUTH_METHODS, |
| 11 | type AntigravityAuthMethod, |
| 12 | type EnvironmentId, |
| 13 | type ProviderInstanceId, |
| 14 | type ServerProvider, |
| 15 | } from "@t3tools/contracts"; |
| 16 | import { T, useGT, useMessages } from "gt-react"; |
| 17 | import { useRef, useState } from "react"; |
| 18 | import { Trash2Icon } from "lucide-react"; |
| 19 | |
| 20 | import { ensureLocalApi } from "../../localApi"; |
| 21 | import { useEnvironmentQuery } from "../../state/query"; |
| 22 | import { serverEnvironment } from "../../state/server"; |
| 23 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 24 | import { Button } from "../ui/button"; |
| 25 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 26 | import { SettingsRow } from "./settingsLayout"; |
| 27 | import { ProviderAuthenticationSection } from "./ProviderAuthenticationSection"; |
| 28 | |
| 29 | interface ProviderSetupSectionProps { |
| 30 | readonly environmentId: EnvironmentId; |
| 31 | readonly environmentLabel: string; |
| 32 | readonly instanceId: ProviderInstanceId; |
| 33 | readonly provider: ServerProvider | undefined; |
| 34 | readonly binaryPath?: string | undefined; |
| 35 | readonly authMethod?: AntigravityAuthMethod | undefined; |
| 36 | readonly enabled: boolean; |
| 37 | readonly readOnly: boolean; |
| 38 | readonly onEnable: () => void; |
| 39 | } |
| 40 | |
| 41 | export function readAntigravityAuthMethod(config: unknown): AntigravityAuthMethod { |
| 42 | const value = |
| 43 | config !== null && typeof config === "object" && "authMethod" in config |
| 44 | ? config.authMethod |
| 45 | : undefined; |
| 46 | return ( |
| 47 | ANTIGRAVITY_AUTH_METHODS.find((method) => method.value === value)?.value ?? "oauth-personal" |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | export function ProviderSetupSection(props: ProviderSetupSectionProps) { |
| 52 | const gt = useGT(); |
| 53 | return ( |
| 54 | <section |
| 55 | aria-label={gt("Antigravity setup")} |
| 56 | className="@container/setup divide-y divide-border/50 text-xs [&_[data-slot=settings-row]]:rounded-none" |
| 57 | > |
| 58 | <SettingsRow |
| 59 | className="@max-lg/setup:[&>div:first-child]:flex @max-lg/setup:[&>div:first-child]:items-stretch @max-lg/setup:[&>div:first-child]:gap-3" |
| 60 | title={gt("Environment")} |
| 61 | description={gt("Device that runs this provider.")} |
| 62 | control={ |
| 63 | <div className="flex min-w-0 flex-col gap-2 sm:items-end"> |
| 64 | <span className="text-muted-foreground [overflow-wrap:anywhere]"> |
| 65 | {props.environmentLabel} |
| 66 | </span> |
| 67 | {!props.enabled && !props.readOnly ? ( |
| 68 | <Button size="sm" variant="outline" onClick={props.onEnable}> |
| 69 | <T>Enable Antigravity</T> |
| 70 | </Button> |
| 71 | ) : null} |
| 72 | </div> |
| 73 | } |
| 74 | /> |
| 75 | {props.readOnly ? ( |
| 76 | <SettingsRow |
| 77 | title={gt("Setup unavailable")} |
| 78 | description={gt("Provider setup is read-only.")} |
| 79 | /> |
| 80 | ) : props.provider?.setup === undefined ? ( |
| 81 | <SettingsRow |
| 82 | title={gt("Update required")} |
| 83 | description={gt("Update this environment to manage Antigravity.")} |
| 84 | /> |
| 85 | ) : ( |
| 86 | <ProviderSetupActions |
| 87 | key={`${props.environmentId}:${props.instanceId}`} |
| 88 | environmentId={props.environmentId} |
| 89 | environmentLabel={props.environmentLabel} |
| 90 | instanceId={props.instanceId} |
| 91 | provider={props.provider} |
| 92 | binaryPath={props.binaryPath} |
| 93 | authMethod={props.authMethod ?? "oauth-personal"} |
| 94 | enabled={props.enabled} |
| 95 | /> |
| 96 | )} |
| 97 | </section> |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | function ProviderSetupActions({ |
| 102 | environmentId, |
| 103 | environmentLabel, |
| 104 | instanceId, |
| 105 | provider, |
| 106 | enabled, |
| 107 | binaryPath, |
| 108 | }: Pick< |
| 109 | ProviderSetupSectionProps, |
| 110 | "environmentId" | "environmentLabel" | "instanceId" | "enabled" | "binaryPath" |
| 111 | > & { |
| 112 | readonly provider: ServerProvider; |
| 113 | readonly authMethod: AntigravityAuthMethod; |
| 114 | }) { |
| 115 | const gt = useGT(); |
| 116 | const target = { environmentId, input: { instanceId } }; |
| 117 | const authQuery = useEnvironmentQuery(serverEnvironment.providerAuthState(target), useMessages()); |
| 118 | const installQuery = useEnvironmentQuery( |
| 119 | serverEnvironment.providerInstallState(target), |
| 120 | useMessages(), |
| 121 | ); |
| 122 | const auth = authQuery.data; |
| 123 | const installation = installQuery.data; |
| 124 | const commandOptions = { reportFailure: false, reportDefect: false }; |
| 125 | const startInstall = useAtomCommand(serverEnvironment.startProviderInstall, commandOptions); |
| 126 | const cancelInstall = useAtomCommand(serverEnvironment.cancelProviderInstall, commandOptions); |
| 127 | const removeInstall = useAtomCommand( |
| 128 | serverEnvironment.removeProviderInstallation, |
| 129 | commandOptions, |
| 130 | ); |
| 131 | const [pendingLabel, setPendingLabel] = useState<string | null>(null); |
| 132 | const pendingRef = useRef(false); |
| 133 | const [error, setError] = useState<string | null>(null); |
| 134 | const authActive = |
| 135 | auth?.phase === "starting" || auth?.phase === "waiting" || auth?.phase === "verifying"; |
| 136 | const installActive = |
| 137 | installation?.phase === "downloading" || |
| 138 | installation?.phase === "extracting" || |
| 139 | installation?.phase === "verifying"; |
| 140 | const usesCustomBinary = Boolean(binaryPath?.trim()); |
| 141 | const installed = |
| 142 | provider.installed || (!usesCustomBinary && installation?.installedVersion != null); |
| 143 | const queryError = authQuery.error ?? installQuery.error; |
| 144 | const actionsDisabled = pendingLabel !== null || queryError !== null; |
| 145 | const installationStatusMessage = |
| 146 | installation === null |
| 147 | ? gt("Checking the Antigravity runtime.") |
| 148 | : installation?.phase === "downloading" |
| 149 | ? installation.totalBytes === null |
| 150 | ? gt("Downloading {downloaded} MB.", { |
| 151 | downloaded: (installation.downloadedBytes / 1_000_000).toFixed(1), |
| 152 | }) |
| 153 | : gt("Downloading {downloaded} MB of {total} MB.", { |
| 154 | downloaded: (installation.downloadedBytes / 1_000_000).toFixed(1), |
| 155 | total: (installation.totalBytes / 1_000_000).toFixed(1), |
| 156 | }) |
| 157 | : installation?.phase === "extracting" |
| 158 | ? gt("Extracting Antigravity.") |
| 159 | : installation?.phase === "verifying" |
| 160 | ? gt("Checking the downloaded runtime.") |
| 161 | : installed |
| 162 | ? gt("Installed.") |
| 163 | : usesCustomBinary |
| 164 | ? enabled |
| 165 | ? gt("The configured Antigravity runtime is unavailable.") |
| 166 | : gt("The configured Antigravity runtime has not been checked.") |
| 167 | : installation?.totalBytes |
| 168 | ? gt("{size} MB download.", { |
| 169 | size: Math.ceil(installation.totalBytes / 1_000_000), |
| 170 | }) |
| 171 | : gt("Not installed."); |
| 172 | |
| 173 | async function runCommand<A, E>( |
| 174 | label: string, |
| 175 | request: () => Promise<AtomCommandResult<A, E>>, |
| 176 | ): Promise<boolean> { |
| 177 | if (pendingRef.current) return false; |
| 178 | pendingRef.current = true; |
| 179 | setPendingLabel(label); |
| 180 | setError(null); |
| 181 | const settled = await settlePromise(request); |
| 182 | pendingRef.current = false; |
| 183 | setPendingLabel(null); |
| 184 | if (settled._tag === "Failure") { |
| 185 | setError(gt("Provider setup failed. Try again.")); |
| 186 | return false; |
| 187 | } |
| 188 | const result = settled.value; |
| 189 | if (result._tag === "Failure") { |
| 190 | if (!isAtomCommandInterrupted(result)) { |
| 191 | const failure = squashAtomCommandFailure(result); |
| 192 | setError( |
| 193 | failure instanceof Error |
| 194 | ? (thrownErrorMessage(failure, m) ?? failure.message) |
| 195 | : gt("Provider setup failed."), |
| 196 | ); |
| 197 | } |
| 198 | return false; |
| 199 | } |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | async function removeRuntime() { |
| 204 | const confirmed = await ensureLocalApi().dialogs.confirm( |
| 205 | gt( |
| 206 | "Remove the downloaded Antigravity runtime from {environment}? Google sign-in and thread history are kept.", |
| 207 | { environment: environmentLabel }, |
| 208 | ), |
| 209 | ); |
| 210 | if (confirmed) { |
| 211 | await runCommand(gt("Removing runtime"), () => removeInstall(target)); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | return ( |
| 216 | <div className="divide-y divide-border/50"> |
| 217 | {/* Keep the live region first so sibling divider rules do not add a divider after the last visible |
| 218 | * row. */} |
| 219 | <p className="sr-only" role="status"> |
| 220 | {pendingLabel ? `${pendingLabel}.` : null} |
| 221 | </p> |
| 222 | <SettingsRow |
| 223 | title={gt("Runtime")} |
| 224 | className="@max-lg/setup:[&>div:first-child]:flex @max-lg/setup:[&>div:first-child]:items-stretch @max-lg/setup:[&>div:first-child]:gap-3" |
| 225 | description={gt("Install and manage Antigravity.")} |
| 226 | status={ |
| 227 | <div className="space-y-2"> |
| 228 | {usesCustomBinary ? ( |
| 229 | <p className="text-muted-foreground"> |
| 230 | <T>Uses the custom binary path below. Installation keeps that path.</T> |
| 231 | </p> |
| 232 | ) : null} |
| 233 | {!installed && !provider.setup?.canInstall ? ( |
| 234 | <p className="text-muted-foreground"> |
| 235 | <T> |
| 236 | Automatic installation unavailable. Set a binary path or use another environment. |
| 237 | </T> |
| 238 | </p> |
| 239 | ) : null} |
| 240 | </div> |
| 241 | } |
| 242 | control={ |
| 243 | <div className="flex w-full min-w-0 flex-col gap-2 @min-[32rem]/settings-row:w-56 @min-[32rem]/settings-row:text-right"> |
| 244 | <p role="status" className="min-h-4 text-muted-foreground tabular-nums"> |
| 245 | {installationStatusMessage} |
| 246 | </p> |
| 247 | <div className="h-1"> |
| 248 | {installation?.phase === "downloading" && |
| 249 | installation.totalBytes !== null && |
| 250 | installation.totalBytes > 0 ? ( |
| 251 | <progress |
| 252 | aria-label={gt("Antigravity download")} |
| 253 | // Forced-colors mode repaints the track and fill as Canvas, and |
| 254 | // appearance:auto cannot bring the native widget back: the |
| 255 | // preflight border reset marks every <progress> as author-styled, |
| 256 | // so Chromium keeps its fallback paint. Use system colors instead. |
| 257 | className="block h-1 w-full appearance-none overflow-hidden rounded-full bg-muted forced-colors:border [&::-moz-progress-bar]:rounded-full [&::-moz-progress-bar]:bg-primary forced-colors:[&::-moz-progress-bar]:bg-[Highlight] [&::-webkit-progress-bar]:rounded-full [&::-webkit-progress-bar]:bg-muted [&::-webkit-progress-value]:rounded-full [&::-webkit-progress-value]:bg-primary forced-colors:[&::-webkit-progress-value]:bg-[Highlight]" |
| 258 | value={installation.downloadedBytes} |
| 259 | max={installation.totalBytes} |
| 260 | /> |
| 261 | ) : null} |
| 262 | </div> |
| 263 | {!installActive && |
| 264 | installation?.message && |
| 265 | installation.message !== installationStatusMessage ? ( |
| 266 | <p className="text-muted-foreground [overflow-wrap:anywhere]"> |
| 267 | {installation.message} |
| 268 | </p> |
| 269 | ) : null} |
| 270 | <div className="grid min-h-7 grid-cols-[1.75rem_minmax(0,1fr)] gap-2"> |
| 271 | <div className="col-start-2 row-start-1 grid"> |
| 272 | {installActive && installation.operationId ? ( |
| 273 | <Button |
| 274 | size="sm" |
| 275 | variant="outline" |
| 276 | disabled={actionsDisabled} |
| 277 | onClick={() => { |
| 278 | const operationId = installation.operationId; |
| 279 | if (!operationId) return; |
| 280 | void runCommand(gt("Cancelling installation"), () => |
| 281 | cancelInstall({ environmentId, input: { instanceId, operationId } }), |
| 282 | ); |
| 283 | }} |
| 284 | > |
| 285 | <T>Cancel installation</T> |
| 286 | </Button> |
| 287 | ) : !installActive && provider.setup?.canInstall ? ( |
| 288 | <Button |
| 289 | size="sm" |
| 290 | variant="outline" |
| 291 | disabled={actionsDisabled || authActive} |
| 292 | onClick={() => |
| 293 | void runCommand(gt("Starting installation"), () => startInstall(target)) |
| 294 | } |
| 295 | > |
| 296 | {installation?.installedVersion |
| 297 | ? installation.version && |
| 298 | installation.version !== installation.installedVersion |
| 299 | ? gt("Update Antigravity") |
| 300 | : gt("Reinstall Antigravity") |
| 301 | : installation?.phase === "failed" || installation?.phase === "cancelled" |
| 302 | ? gt("Retry installation") |
| 303 | : installed |
| 304 | ? gt("Install managed runtime") |
| 305 | : gt("Install Antigravity")} |
| 306 | </Button> |
| 307 | ) : null} |
| 308 | </div> |
| 309 | {installation?.canRemove && !installActive ? ( |
| 310 | <Tooltip> |
| 311 | <TooltipTrigger |
| 312 | render={ |
| 313 | <Button |
| 314 | size="icon-sm" |
| 315 | variant="ghost" |
| 316 | className="col-start-1 row-start-1" |
| 317 | aria-label={gt("Remove downloaded runtime")} |
| 318 | disabled={actionsDisabled || authActive} |
| 319 | onClick={() => void removeRuntime()} |
| 320 | /> |
| 321 | } |
| 322 | > |
| 323 | <Trash2Icon className="size-3.5" /> |
| 324 | </TooltipTrigger> |
| 325 | <TooltipPopup> |
| 326 | <T>Remove downloaded runtime</T> |
| 327 | </TooltipPopup> |
| 328 | </Tooltip> |
| 329 | ) : null} |
| 330 | </div> |
| 331 | </div> |
| 332 | } |
| 333 | /> |
| 334 | |
| 335 | <ProviderAuthenticationSection |
| 336 | environmentId={environmentId} |
| 337 | environmentLabel={environmentLabel} |
| 338 | instanceId={instanceId} |
| 339 | provider={provider} |
| 340 | readOnly={false} |
| 341 | /> |
| 342 | |
| 343 | {error || queryError ? ( |
| 344 | <div className="grid gap-2 px-3 py-3 sm:px-4"> |
| 345 | <p role="alert" className="text-destructive [overflow-wrap:anywhere]"> |
| 346 | {error ?? queryError} |
| 347 | </p> |
| 348 | {queryError ? ( |
| 349 | <Button |
| 350 | size="sm" |
| 351 | variant="outline" |
| 352 | className="w-fit" |
| 353 | onClick={() => { |
| 354 | authQuery.refresh(); |
| 355 | installQuery.refresh(); |
| 356 | }} |
| 357 | > |
| 358 | <T>Retry setup status</T> |
| 359 | </Button> |
| 360 | ) : null} |
| 361 | </div> |
| 362 | ) : null} |
| 363 | </div> |
| 364 | ); |
| 365 | } |