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 as resolveMessage } from "~/i18n/messages"; |
| 3 | import { ProviderInstanceIcon } from "../chat/ProviderInstanceIcon"; |
| 4 | |
| 5 | import { Radio as RadioPrimitive } from "@base-ui/react/radio"; |
| 6 | import { CheckIcon, ChevronDownIcon } from "lucide-react"; |
| 7 | import { useState } from "react"; |
| 8 | import { msg, T, useGT, useMessages, Var } from "gt-react"; |
| 9 | import type { MessageResolver } from "../../i18n/messages"; |
| 10 | import { |
| 11 | deriveAvailableInstanceId, |
| 12 | deriveInstanceId, |
| 13 | } from "@t3tools/client-runtime/provider-instance-ids"; |
| 14 | import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; |
| 15 | import type { ProviderClientDefinition } from "@t3tools/provider-core/client"; |
| 16 | import { |
| 17 | type AcpRegistrySearchAgent, |
| 18 | AuthProvidersManageScope, |
| 19 | ProviderInstanceId, |
| 20 | ProviderDriverKind, |
| 21 | type EnvironmentId, |
| 22 | type ProviderInstanceConfig, |
| 23 | type ProviderInstanceEnvironmentVariable, |
| 24 | isManagedDriverAllowed, |
| 25 | } from "@t3tools/contracts"; |
| 26 | |
| 27 | import { |
| 28 | useEnvironmentSettings, |
| 29 | usePersistEnvironmentProviderInstanceMutation, |
| 30 | } from "../../hooks/useSettings"; |
| 31 | |
| 32 | import { cn } from "../../lib/utils"; |
| 33 | import { normalizeProviderAccentColor } from "../../providerInstances"; |
| 34 | import { managedInstanceLocks, useManagedPolicy } from "../../state/managedPolicy"; |
| 35 | import { readEnvironmentScope, useEnvironmentScope } from "../../state/session"; |
| 36 | import { Button } from "../ui/button"; |
| 37 | import { ChatGptConnectionButton } from "./ChatGptConnectionButton"; |
| 38 | import { Dialog } from "../ui/dialog"; |
| 39 | import { Badge } from "../ui/badge"; |
| 40 | import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "../ui/collapsible"; |
| 41 | import { Input } from "../ui/input"; |
| 42 | import { RadioGroup } from "../ui/radio-group"; |
| 43 | import { toastManager } from "../ui/toast"; |
| 44 | import { providerClients } from "./providerDriverMeta"; |
| 45 | import { ProviderAccentColorPicker } from "./ProviderAccentColorPicker"; |
| 46 | import { SettingsGroup } from "./SettingsGroup"; |
| 47 | import { SettingsRow } from "./settingsLayout"; |
| 48 | import { ProviderSettingsForm, deriveProviderSettingsFields } from "./ProviderSettingsForm"; |
| 49 | import { WizardPanel, WizardPopup, WizardHeader, WizardFooter } from "../ui/wizard"; |
| 50 | import { |
| 51 | ADD_PROVIDER_WIZARD_STEPS, |
| 52 | ACP_REGISTRY_WIZARD_STEPS, |
| 53 | LOCAL_ACP_WIZARD_STEPS, |
| 54 | resolveAcpRegistryWizardNavigation, |
| 55 | resolveWizardNavigation, |
| 56 | updateProviderIdentityDraft, |
| 57 | type ProviderIdentityDraft, |
| 58 | type WizardNavigation, |
| 59 | } from "./AddProviderInstanceDialog.logic"; |
| 60 | import { AddProviderInstanceWizardSteps } from "./AddProviderInstanceWizardSteps"; |
| 61 | import { AcpRegistrySearchStep, useAcpRegistrySearch } from "./AcpRegistrySearchStep"; |
| 62 | import { ProviderWizardAuthenticationStep } from "./ProviderWizardAuthenticationStep"; |
| 63 | import { resolveOfficialAcpRegistryIconUrl } from "./AcpRegistryIcon"; |
| 64 | import { AddManagedCodexAccountDialog } from "./CodexSetupSection"; |
| 65 | import { MultipleAccountsNote } from "./MultipleAccountsNote"; |
| 66 | import { ProviderEnvironmentSection } from "./ProviderInstanceCard"; |
| 67 | import { useSecretSources } from "./SecretSourceInput"; |
| 68 | |
| 69 | const INSTANCE_ID_PATTERN = /^[a-zA-Z][a-zA-Z0-9_-]*$/; |
| 70 | const DEFAULT_DRIVER_KIND = ProviderDriverKind.make("codex"); |
| 71 | const ACP_REGISTRY_DRIVER_KIND = ProviderDriverKind.make("acpRegistry"); |
| 72 | const DEFAULT_DRIVER_OPTION = providerClients.definitions[0]!; |
| 73 | const DRIVER_OPTIONS = providerClients.definitions.filter( |
| 74 | (option) => option.driverKind !== ACP_REGISTRY_DRIVER_KIND, |
| 75 | ); |
| 76 | const FEATURED_DRIVER_OPTIONS = DRIVER_OPTIONS.filter((option) => option.featured !== false); |
| 77 | const MORE_DRIVER_OPTIONS = DRIVER_OPTIONS.filter((option) => option.featured === false); |
| 78 | const EMPTY_CONFIG_DRAFT: Record<string, unknown> = {}; |
| 79 | function validateInstanceId( |
| 80 | id: string, |
| 81 | existing: ReadonlySet<string>, |
| 82 | resolve: MessageResolver, |
| 83 | ): string | null { |
| 84 | if (id.length === 0) return resolve(msg("Instance ID is required.")); |
| 85 | if (id.length > 64) return resolve(msg("Instance ID must be 64 characters or fewer.")); |
| 86 | if (!INSTANCE_ID_PATTERN.test(id)) { |
| 87 | return resolve( |
| 88 | msg("Instance ID must start with a letter and use only letters, digits, '-', or '_'."), |
| 89 | ); |
| 90 | } |
| 91 | if (existing.has(id)) return resolve(msg("An instance named ''{id}'' already exists.", { id })); |
| 92 | return null; |
| 93 | } |
| 94 | |
| 95 | function ProviderOptionGrid({ |
| 96 | options, |
| 97 | value, |
| 98 | disabled, |
| 99 | onValueChange, |
| 100 | ...labelProps |
| 101 | }: { |
| 102 | readonly options: ReadonlyArray<ProviderClientDefinition>; |
| 103 | readonly value: ProviderDriverKind; |
| 104 | readonly disabled: boolean; |
| 105 | readonly onValueChange: (value: string) => void; |
| 106 | readonly "aria-label"?: string; |
| 107 | readonly "aria-labelledby"?: string; |
| 108 | }) { |
| 109 | return ( |
| 110 | <RadioGroup |
| 111 | {...labelProps} |
| 112 | disabled={disabled} |
| 113 | value={value} |
| 114 | onValueChange={onValueChange} |
| 115 | className="grid grid-cols-1 sm:grid-cols-2" |
| 116 | > |
| 117 | {options.map((option) => ( |
| 118 | <RadioPrimitive.Root |
| 119 | key={option.driverKind} |
| 120 | value={option.driverKind} |
| 121 | className="relative flex cursor-pointer items-center gap-3 rounded-lg bg-card px-3 py-3 text-left text-muted-foreground outline-none ring-1 ring-black/5 hover:bg-muted focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring data-checked:bg-primary/8 data-checked:text-foreground data-checked:ring-2 data-checked:ring-inset data-checked:ring-primary data-checked:hover:bg-primary/8 dark:bg-white/3 dark:ring-white/5 dark:hover:bg-white/5 dark:data-checked:bg-primary/15 dark:data-checked:ring-primary dark:data-checked:hover:bg-primary/15" |
| 122 | > |
| 123 | <ProviderInstanceIcon |
| 124 | driverKind={option.driverKind} |
| 125 | displayName={option.label} |
| 126 | iconClassName="size-4" |
| 127 | /> |
| 128 | <span className="min-w-0 flex-1 truncate text-sm font-medium text-foreground"> |
| 129 | {option.label} |
| 130 | </span> |
| 131 | <RadioPrimitive.Indicator |
| 132 | className="grid size-5 shrink-0 place-items-center rounded-full bg-primary text-primary-foreground" |
| 133 | aria-hidden |
| 134 | > |
| 135 | <CheckIcon className="size-3.5 shrink-0" /> |
| 136 | </RadioPrimitive.Indicator> |
| 137 | {option.badgeLabel ? ( |
| 138 | <Badge variant="warning" size="sm"> |
| 139 | {option.badgeLabel} |
| 140 | </Badge> |
| 141 | ) : null} |
| 142 | </RadioPrimitive.Root> |
| 143 | ))} |
| 144 | </RadioGroup> |
| 145 | ); |
| 146 | } |
| 147 | |
| 148 | interface AddProviderInstanceDialogProps { |
| 149 | readonly open: boolean; |
| 150 | readonly environmentId: EnvironmentId; |
| 151 | readonly environmentLabel: string; |
| 152 | readonly onOpenChange: (open: boolean) => void; |
| 153 | readonly onCreated?: (instanceId: ProviderInstanceId, driver: ProviderDriverKind) => void; |
| 154 | /** Opens with "Show more" expanded, so the ACP Registry search is in view. */ |
| 155 | readonly showMoreProviders?: boolean; |
| 156 | } |
| 157 | |
| 158 | export function AddProviderInstanceDialog({ |
| 159 | open, |
| 160 | environmentId, |
| 161 | environmentLabel, |
| 162 | onOpenChange, |
| 163 | onCreated, |
| 164 | showMoreProviders: initialShowMoreProviders = false, |
| 165 | }: AddProviderInstanceDialogProps) { |
| 166 | const gt = useGT(); |
| 167 | const m = useMessages(); |
| 168 | const settings = useEnvironmentSettings(environmentId); |
| 169 | const secretSources = useSecretSources(environmentId); |
| 170 | const persistProviderInstance = usePersistEnvironmentProviderInstanceMutation(environmentId); |
| 171 | const canManageProviders = useEnvironmentScope(environmentId, AuthProvidersManageScope); |
| 172 | |
| 173 | const [wizardStep, setWizardStep] = useState(0); |
| 174 | const [addingChatGptAccount, setAddingChatGptAccount] = useState(false); |
| 175 | // Drivers the administrator's policy leaves out are not offered at all. |
| 176 | const managedPolicy = useManagedPolicy(environmentId); |
| 177 | const featuredDriverOptions = FEATURED_DRIVER_OPTIONS.filter((option) => |
| 178 | isManagedDriverAllowed(managedPolicy, option.driverKind), |
| 179 | ); |
| 180 | const moreDriverOptions = MORE_DRIVER_OPTIONS.filter((option) => |
| 181 | isManagedDriverAllowed(managedPolicy, option.driverKind), |
| 182 | ); |
| 183 | const acpRegistryAllowed = isManagedDriverAllowed(managedPolicy, ACP_REGISTRY_DRIVER_KIND); |
| 184 | const [driver, setDriver] = useState<ProviderDriverKind>( |
| 185 | () => |
| 186 | (featuredDriverOptions[0] ?? moreDriverOptions[0])?.driverKind ?? |
| 187 | (acpRegistryAllowed ? ACP_REGISTRY_DRIVER_KIND : DEFAULT_DRIVER_KIND), |
| 188 | ); |
| 189 | const [identityByDriver, setIdentityByDriver] = useState<Record<string, ProviderIdentityDraft>>( |
| 190 | {}, |
| 191 | ); |
| 192 | const [selectedAcp, setSelectedAcp] = useState<AcpRegistrySearchAgent | null>(null); |
| 193 | const [localEnvironment, setLocalEnvironment] = useState< |
| 194 | ReadonlyArray<ProviderInstanceEnvironmentVariable> |
| 195 | >([]); |
| 196 | const [isManualAcpConfiguration, setIsManualAcpConfiguration] = useState(false); |
| 197 | const [showMoreProviders, setShowMoreProviders] = useState(initialShowMoreProviders); |
| 198 | const [configByDriver, setConfigByDriver] = useState<Record<string, Record<string, unknown>>>({}); |
| 199 | const [hasAttemptedSubmit, setHasAttemptedSubmit] = useState(false); |
| 200 | const [isSaving, setIsSaving] = useState(false); |
| 201 | const [createdInstanceId, setCreatedInstanceId] = useState<ProviderInstanceId | null>(null); |
| 202 | |
| 203 | // Reserve Codex/Claude default ids because they run before configuration; other unconfigured slots are |
| 204 | // available. |
| 205 | const existingIds = new Set([ |
| 206 | "codex", |
| 207 | "claudeAgent", |
| 208 | ...Object.keys(settings.providerInstances ?? {}), |
| 209 | ]); |
| 210 | |
| 211 | const driverOption = providerClients.get(driver) ?? DEFAULT_DRIVER_OPTION; |
| 212 | const isAcpRegistry = driver === ACP_REGISTRY_DRIVER_KIND; |
| 213 | const showRegistrySearch = |
| 214 | !createdInstanceId && |
| 215 | !addingChatGptAccount && |
| 216 | wizardStep === 0 && |
| 217 | !(isAcpRegistry && isManualAcpConfiguration); |
| 218 | // Searches while Show more is closed, so the fold opens on results. |
| 219 | const registry = useAcpRegistrySearch(environmentId, showRegistrySearch); |
| 220 | const isPreparingRegistryAgent = registry.preparingId !== null; |
| 221 | const defaultIdentity: ProviderIdentityDraft = { |
| 222 | label: driverOption.label, |
| 223 | accentColor: "", |
| 224 | instanceIdOverride: null, |
| 225 | }; |
| 226 | const { label, accentColor, instanceIdOverride } = identityByDriver[driver] ?? defaultIdentity; |
| 227 | const instanceId = |
| 228 | instanceIdOverride ?? |
| 229 | deriveAvailableInstanceId( |
| 230 | (candidateLabel) => { |
| 231 | if (!candidateLabel.trim() || candidateLabel === driverOption.label) { |
| 232 | return isAcpRegistry ? `${driver}_custom` : driver; |
| 233 | } |
| 234 | return deriveInstanceId(driver, candidateLabel); |
| 235 | }, |
| 236 | label, |
| 237 | existingIds, |
| 238 | ); |
| 239 | const driverSettingsFields = deriveProviderSettingsFields(driverOption, undefined, m); |
| 240 | const instanceIdError = validateInstanceId(instanceId, existingIds, m); |
| 241 | const showInstanceIdError = hasAttemptedSubmit && instanceIdError !== null; |
| 242 | const identityStep = 1; |
| 243 | const previewLabel = label.trim() || gt("{provider} Workspace", { provider: driverOption.label }); |
| 244 | |
| 245 | const configDraft = configByDriver[driver] ?? EMPTY_CONFIG_DRAFT; |
| 246 | const isLocalAcp = isAcpRegistry && isManualAcpConfiguration && configDraft.source === "local"; |
| 247 | const localCommandPath = |
| 248 | typeof configDraft.commandPath === "string" ? configDraft.commandPath.trim() : ""; |
| 249 | const manualAgentId = typeof configDraft.agentId === "string" ? configDraft.agentId.trim() : ""; |
| 250 | const acpSelectionError = isLocalAcp |
| 251 | ? localCommandPath.length > 0 |
| 252 | ? null |
| 253 | : gt("Executable is required.") |
| 254 | : selectedAcp !== null || (isManualAcpConfiguration && manualAgentId.length > 0) |
| 255 | ? null |
| 256 | : gt("Select an ACP or configure one manually."); |
| 257 | const wizardStepSummaries = isAcpRegistry |
| 258 | ? ([ |
| 259 | isLocalAcp ? gt("Local ACP command") : (selectedAcp?.name ?? (manualAgentId || null)), |
| 260 | previewLabel, |
| 261 | null, |
| 262 | ] as const) |
| 263 | : ([driverOption.label, previewLabel, null] as const); |
| 264 | const setConfigDraft = (config: Record<string, unknown> | undefined) => { |
| 265 | setConfigByDriver((existing) => { |
| 266 | const next = { ...existing }; |
| 267 | if (config === undefined || Object.keys(config).length === 0) { |
| 268 | delete next[driver]; |
| 269 | } else { |
| 270 | next[driver] = config; |
| 271 | } |
| 272 | return next; |
| 273 | }); |
| 274 | }; |
| 275 | const selectDriver = (value: string) => { |
| 276 | setDriver(ProviderDriverKind.make(value)); |
| 277 | setIsManualAcpConfiguration(false); |
| 278 | setHasAttemptedSubmit(false); |
| 279 | }; |
| 280 | const setIdentityDraft = (update: Partial<ProviderIdentityDraft>) => { |
| 281 | setIdentityByDriver((existing) => ({ |
| 282 | ...existing, |
| 283 | [driver]: { ...(existing[driver] ?? defaultIdentity), ...update }, |
| 284 | })); |
| 285 | }; |
| 286 | |
| 287 | const applyWizardNavigation = (navigation: WizardNavigation) => { |
| 288 | if (isSaving || isPreparingRegistryAgent || createdInstanceId) return; |
| 289 | if (navigation.kind === "blocked") { |
| 290 | setHasAttemptedSubmit(true); |
| 291 | } |
| 292 | if (isAcpRegistry && !isLocalAcp && navigation.kind === "navigate" && navigation.step === 2) { |
| 293 | void handleSave(); |
| 294 | return; |
| 295 | } |
| 296 | setWizardStep(navigation.step); |
| 297 | }; |
| 298 | |
| 299 | const navigateToStep = (requestedStep: number) => { |
| 300 | applyWizardNavigation( |
| 301 | isAcpRegistry |
| 302 | ? isLocalAcp |
| 303 | ? resolveWizardNavigation(wizardStep, requestedStep, LOCAL_ACP_WIZARD_STEPS.length, { |
| 304 | instanceIdError, |
| 305 | prerequisite: { step: 0, error: acpSelectionError }, |
| 306 | }) |
| 307 | : resolveAcpRegistryWizardNavigation(wizardStep, requestedStep, { |
| 308 | instanceIdError, |
| 309 | selectionError: acpSelectionError, |
| 310 | }) |
| 311 | : resolveWizardNavigation(wizardStep, requestedStep, ADD_PROVIDER_WIZARD_STEPS.length, { |
| 312 | instanceIdError, |
| 313 | }), |
| 314 | ); |
| 315 | }; |
| 316 | |
| 317 | const handleAcpPrepared = (agent: AcpRegistrySearchAgent) => { |
| 318 | setDriver(ACP_REGISTRY_DRIVER_KIND); |
| 319 | const nextLabel = agent.name; |
| 320 | const registryIconUrl = resolveOfficialAcpRegistryIconUrl(agent.icon); |
| 321 | const nextInstanceId = deriveAvailableInstanceId( |
| 322 | (candidateLabel) => deriveInstanceId(ACP_REGISTRY_DRIVER_KIND, candidateLabel), |
| 323 | nextLabel, |
| 324 | existingIds, |
| 325 | ); |
| 326 | setSelectedAcp(agent); |
| 327 | setIsManualAcpConfiguration(false); |
| 328 | setIdentityByDriver((existing) => |
| 329 | updateProviderIdentityDraft(existing, ACP_REGISTRY_DRIVER_KIND, { |
| 330 | label: nextLabel, |
| 331 | instanceIdOverride: nextInstanceId, |
| 332 | }), |
| 333 | ); |
| 334 | setConfigByDriver((existing) => ({ |
| 335 | ...existing, |
| 336 | [ACP_REGISTRY_DRIVER_KIND]: { |
| 337 | agentId: agent.id, |
| 338 | distribution: "auto", |
| 339 | ...(registryIconUrl ? { registryIconUrl } : {}), |
| 340 | }, |
| 341 | })); |
| 342 | setHasAttemptedSubmit(false); |
| 343 | setWizardStep(1); |
| 344 | }; |
| 345 | |
| 346 | const handleManualAcpConfiguration = () => { |
| 347 | setDriver(ACP_REGISTRY_DRIVER_KIND); |
| 348 | setSelectedAcp(null); |
| 349 | setIsManualAcpConfiguration(true); |
| 350 | setConfigByDriver((existing) => { |
| 351 | const next = { ...existing }; |
| 352 | delete next[ACP_REGISTRY_DRIVER_KIND]; |
| 353 | return next; |
| 354 | }); |
| 355 | setIdentityByDriver((existing) => |
| 356 | updateProviderIdentityDraft(existing, ACP_REGISTRY_DRIVER_KIND, { |
| 357 | label: "", |
| 358 | instanceIdOverride: null, |
| 359 | }), |
| 360 | ); |
| 361 | setHasAttemptedSubmit(false); |
| 362 | }; |
| 363 | |
| 364 | const handleLocalAcpConfiguration = () => { |
| 365 | setDriver(ACP_REGISTRY_DRIVER_KIND); |
| 366 | setSelectedAcp(null); |
| 367 | setIsManualAcpConfiguration(true); |
| 368 | setConfigByDriver((existing) => ({ |
| 369 | ...existing, |
| 370 | [ACP_REGISTRY_DRIVER_KIND]: { source: "local", commandArgs: [] }, |
| 371 | })); |
| 372 | setIdentityByDriver((existing) => |
| 373 | updateProviderIdentityDraft(existing, ACP_REGISTRY_DRIVER_KIND, { |
| 374 | label: gt("Local ACP"), |
| 375 | instanceIdOverride: null, |
| 376 | }), |
| 377 | ); |
| 378 | setLocalEnvironment([]); |
| 379 | setHasAttemptedSubmit(false); |
| 380 | }; |
| 381 | |
| 382 | // A policy that owns this driver's launch settings also rules out ChatGPT's own runtime. |
| 383 | const driverLocks = managedInstanceLocks(managedPolicy, driver, m); |
| 384 | |
| 385 | const handleSave = async () => { |
| 386 | if (!readEnvironmentScope(environmentId, AuthProvidersManageScope)) return; |
| 387 | if (isSaving || createdInstanceId) return; |
| 388 | setHasAttemptedSubmit(true); |
| 389 | if (instanceIdError !== null || (isAcpRegistry && acpSelectionError !== null)) return; |
| 390 | |
| 391 | const config = |
| 392 | driver === "codex" |
| 393 | ? { ...configByDriver[driver], setupMode: "existing" } |
| 394 | : (configByDriver[driver] ?? {}); |
| 395 | const hasConfig = Object.keys(config).length > 0; |
| 396 | const normalizedAccentColor = normalizeProviderAccentColor(accentColor); |
| 397 | |
| 398 | const nextInstance: ProviderInstanceConfig = { |
| 399 | driver, |
| 400 | enabled: true, |
| 401 | ...(label.trim().length > 0 ? { displayName: label.trim() } : {}), |
| 402 | ...(normalizedAccentColor ? { accentColor: normalizedAccentColor } : {}), |
| 403 | ...(hasConfig ? { config } : {}), |
| 404 | ...(isLocalAcp && localEnvironment.length > 0 ? { environment: localEnvironment } : {}), |
| 405 | }; |
| 406 | const brandedId = ProviderInstanceId.make(instanceId); |
| 407 | setIsSaving(true); |
| 408 | const result = await persistProviderInstance({ |
| 409 | operation: "create", |
| 410 | instanceId: brandedId, |
| 411 | instance: nextInstance, |
| 412 | }); |
| 413 | if (result._tag === "Failure") { |
| 414 | const error = squashAtomCommandFailure(result); |
| 415 | setIsSaving(false); |
| 416 | toastManager.add({ |
| 417 | type: "error", |
| 418 | title: gt("Could not add provider instance"), |
| 419 | description: |
| 420 | error instanceof Error |
| 421 | ? (thrownErrorMessage(error, resolveMessage) ?? error.message) |
| 422 | : gt("The settings update failed."), |
| 423 | }); |
| 424 | return; |
| 425 | } |
| 426 | onCreated?.(brandedId, driver); |
| 427 | if (isAcpRegistry && !isLocalAcp) { |
| 428 | setCreatedInstanceId(brandedId); |
| 429 | setIsSaving(false); |
| 430 | setWizardStep(2); |
| 431 | return; |
| 432 | } |
| 433 | toastManager.add({ |
| 434 | type: "success", |
| 435 | title: gt("Provider instance added"), |
| 436 | description: gt("{provider} instance ''{instanceId}'' was added.", { |
| 437 | provider: driverOption.label, |
| 438 | instanceId, |
| 439 | }), |
| 440 | }); |
| 441 | onOpenChange(false); |
| 442 | }; |
| 443 | |
| 444 | if (addingChatGptAccount) { |
| 445 | return ( |
| 446 | <AddManagedCodexAccountDialog |
| 447 | environmentId={environmentId} |
| 448 | onClose={() => onOpenChange(false)} |
| 449 | onAccountCreated={(instanceId) => onCreated?.(instanceId, ProviderDriverKind.make("codex"))} |
| 450 | /> |
| 451 | ); |
| 452 | } |
| 453 | |
| 454 | return ( |
| 455 | <Dialog open={open} onOpenChange={onOpenChange}> |
| 456 | <WizardPopup size="wide"> |
| 457 | <WizardHeader |
| 458 | title={gt("Add provider")} |
| 459 | description={ |
| 460 | <T> |
| 461 | Add an account or configure a provider on <Var>{environmentLabel}</Var>. |
| 462 | </T> |
| 463 | } |
| 464 | > |
| 465 | {isAcpRegistry ? ( |
| 466 | <AddProviderInstanceWizardSteps |
| 467 | currentStep={wizardStep} |
| 468 | summaries={wizardStepSummaries} |
| 469 | instanceIdError={instanceIdError} |
| 470 | steps={isLocalAcp ? LOCAL_ACP_WIZARD_STEPS : ACP_REGISTRY_WIZARD_STEPS} |
| 471 | disabled={isSaving || isPreparingRegistryAgent || createdInstanceId !== null} |
| 472 | identityStep={1} |
| 473 | prerequisite={{ step: 0, error: acpSelectionError }} |
| 474 | onNavigation={applyWizardNavigation} |
| 475 | /> |
| 476 | ) : ( |
| 477 | <AddProviderInstanceWizardSteps |
| 478 | currentStep={wizardStep} |
| 479 | summaries={wizardStepSummaries} |
| 480 | instanceIdError={instanceIdError} |
| 481 | disabled={isSaving || isPreparingRegistryAgent} |
| 482 | onNavigation={applyWizardNavigation} |
| 483 | /> |
| 484 | )} |
| 485 | </WizardHeader> |
| 486 | |
| 487 | {createdInstanceId ? ( |
| 488 | <ProviderWizardAuthenticationStep |
| 489 | environmentId={environmentId} |
| 490 | environmentLabel={environmentLabel} |
| 491 | instanceId={createdInstanceId} |
| 492 | onFinish={() => onOpenChange(false)} |
| 493 | /> |
| 494 | ) : ( |
| 495 | <> |
| 496 | <WizardPanel className={cn(isAcpRegistry && wizardStep === identityStep && "min-h-72")}> |
| 497 | <div className={cn("grid gap-2", wizardStep !== 0 && "hidden")}> |
| 498 | <div id="add-instance-driver-label" className="text-sm font-medium text-foreground"> |
| 499 | {gt("Provider", { $context: "wizard step" })} |
| 500 | </div> |
| 501 | <ProviderOptionGrid |
| 502 | options={featuredDriverOptions} |
| 503 | value={driver} |
| 504 | disabled={isPreparingRegistryAgent} |
| 505 | onValueChange={selectDriver} |
| 506 | aria-labelledby="add-instance-driver-label" |
| 507 | /> |
| 508 | <MultipleAccountsNote environmentId={environmentId} driver={driver} /> |
| 509 | </div> |
| 510 | |
| 511 | {wizardStep === 0 && (moreDriverOptions.length > 0 || acpRegistryAllowed) ? ( |
| 512 | <Collapsible open={showMoreProviders} onOpenChange={setShowMoreProviders}> |
| 513 | <div className="flex pt-2"> |
| 514 | <CollapsibleTrigger render={<Button variant="ghost-muted" size="xs" />}> |
| 515 | {showMoreProviders ? gt("Show less") : gt("Show more")} |
| 516 | <ChevronDownIcon |
| 517 | aria-hidden |
| 518 | className={cn("transition-transform", showMoreProviders && "rotate-180")} |
| 519 | /> |
| 520 | </CollapsibleTrigger> |
| 521 | </div> |
| 522 | {/* WizardPanel already animates its height, so the fold opens without its own. */} |
| 523 | <CollapsiblePanel animate={false}> |
| 524 | <div className="space-y-4 pt-4"> |
| 525 | <ProviderOptionGrid |
| 526 | options={moreDriverOptions} |
| 527 | value={driver} |
| 528 | disabled={isPreparingRegistryAgent} |
| 529 | onValueChange={selectDriver} |
| 530 | aria-label={gt("More providers")} |
| 531 | /> |
| 532 | {!acpRegistryAllowed ? null : ( |
| 533 | <div className="flex items-center gap-3 text-xs text-muted-foreground"> |
| 534 | <div aria-hidden className="flex-1 border-t border-border/70" /> |
| 535 | <span> |
| 536 | <T>Or choose from ACP Registry</T> |
| 537 | </span> |
| 538 | <div aria-hidden className="flex-1 border-t border-border/70" /> |
| 539 | </div> |
| 540 | )} |
| 541 | {!acpRegistryAllowed ? null : isAcpRegistry && isManualAcpConfiguration ? ( |
| 542 | <div className="grid gap-4"> |
| 543 | <div className="flex items-start justify-between gap-3"> |
| 544 | <div> |
| 545 | <h3 className="text-sm font-medium text-foreground"> |
| 546 | {isLocalAcp ? gt("Local ACP command") : gt("Enter manually")} |
| 547 | </h3> |
| 548 | <p className="mt-0.5 text-xs text-muted-foreground"> |
| 549 | {isLocalAcp |
| 550 | ? gt("Run an installed ACP executable on {environment}.", { |
| 551 | environment: environmentLabel, |
| 552 | }) |
| 553 | : gt( |
| 554 | "Enter an official registry ID and any local executable or auth override.", |
| 555 | )} |
| 556 | </p> |
| 557 | </div> |
| 558 | <Button |
| 559 | onClick={() => { |
| 560 | setIsManualAcpConfiguration(false); |
| 561 | setHasAttemptedSubmit(false); |
| 562 | }} |
| 563 | size="xs" |
| 564 | variant="ghost" |
| 565 | > |
| 566 | <T>Search registry</T> |
| 567 | </Button> |
| 568 | </div> |
| 569 | <SettingsGroup variant="plain"> |
| 570 | <ProviderSettingsForm |
| 571 | definition={driverOption} |
| 572 | value={configDraft} |
| 573 | idPrefix="add-provider-acpRegistry-manual" |
| 574 | variant="settings" |
| 575 | onChange={setConfigDraft} |
| 576 | environmentId={environmentId} |
| 577 | /> |
| 578 | {isLocalAcp ? ( |
| 579 | <ProviderEnvironmentSection |
| 580 | environment={localEnvironment} |
| 581 | onChange={setLocalEnvironment} |
| 582 | secretSources={secretSources} |
| 583 | environmentId={environmentId} |
| 584 | /> |
| 585 | ) : null} |
| 586 | </SettingsGroup> |
| 587 | {isAcpRegistry && hasAttemptedSubmit && acpSelectionError ? ( |
| 588 | <p className="text-2xs text-destructive">{acpSelectionError}</p> |
| 589 | ) : null} |
| 590 | </div> |
| 591 | ) : ( |
| 592 | <> |
| 593 | <AcpRegistrySearchStep |
| 594 | environmentId={environmentId} |
| 595 | registry={registry} |
| 596 | providerInstances={settings.providerInstances} |
| 597 | onPrepared={handleAcpPrepared} |
| 598 | onManualConfiguration={handleManualAcpConfiguration} |
| 599 | onLocalConfiguration={handleLocalAcpConfiguration} |
| 600 | /> |
| 601 | {isAcpRegistry && hasAttemptedSubmit && acpSelectionError ? ( |
| 602 | <p className="mt-2 text-2xs text-destructive">{acpSelectionError}</p> |
| 603 | ) : null} |
| 604 | </> |
| 605 | )} |
| 606 | </div> |
| 607 | </CollapsiblePanel> |
| 608 | </Collapsible> |
| 609 | ) : null} |
| 610 | |
| 611 | {isAcpRegistry && wizardStep === 1 && selectedAcp ? ( |
| 612 | <div className="mb-4 flex items-start justify-between gap-3 border-b border-border/70 pb-3"> |
| 613 | <div className="min-w-0"> |
| 614 | <p className="truncate text-sm font-medium text-foreground"> |
| 615 | {selectedAcp.name} |
| 616 | </p> |
| 617 | <p className="mt-0.5 text-xs text-muted-foreground"> |
| 618 | v{selectedAcp.version} · {selectedAcp.distribution} |
| 619 | </p> |
| 620 | </div> |
| 621 | <div className="flex shrink-0 gap-2 text-2xs"> |
| 622 | {selectedAcp.website ? ( |
| 623 | <a |
| 624 | aria-label={gt("Open documentation for {name} ({id})", { |
| 625 | name: selectedAcp.name, |
| 626 | id: selectedAcp.id, |
| 627 | })} |
| 628 | className="text-muted-foreground hover:text-foreground" |
| 629 | href={selectedAcp.website} |
| 630 | rel="noreferrer" |
| 631 | target="_blank" |
| 632 | > |
| 633 | <T>Docs</T> |
| 634 | </a> |
| 635 | ) : null} |
| 636 | {selectedAcp.repository ? ( |
| 637 | <a |
| 638 | aria-label={gt("Open source for {name} ({id})", { |
| 639 | name: selectedAcp.name, |
| 640 | id: selectedAcp.id, |
| 641 | })} |
| 642 | className="text-muted-foreground hover:text-foreground" |
| 643 | href={selectedAcp.repository} |
| 644 | rel="noreferrer" |
| 645 | target="_blank" |
| 646 | > |
| 647 | {gt("Source", { $context: "link to source code" })} |
| 648 | </a> |
| 649 | ) : null} |
| 650 | </div> |
| 651 | </div> |
| 652 | ) : null} |
| 653 | |
| 654 | <SettingsGroup |
| 655 | variant="plain" |
| 656 | className={cn(wizardStep !== identityStep && "hidden")} |
| 657 | > |
| 658 | <SettingsRow |
| 659 | title={ |
| 660 | <label htmlFor="add-provider-label"> |
| 661 | {gt("Label", { $context: "noun, display name" })} |
| 662 | </label> |
| 663 | } |
| 664 | description={ |
| 665 | <span id="add-provider-label-description"> |
| 666 | <T>Shown in the provider list.</T> |
| 667 | </span> |
| 668 | } |
| 669 | control={ |
| 670 | <Input |
| 671 | id="add-provider-label" |
| 672 | aria-describedby="add-provider-label-description" |
| 673 | size="sm" |
| 674 | className="w-full @min-[32rem]/settings-row:w-56" |
| 675 | placeholder={gt("e.g. Work")} |
| 676 | value={label} |
| 677 | onChange={(event) => setIdentityDraft({ label: event.target.value })} |
| 678 | /> |
| 679 | } |
| 680 | /> |
| 681 | <SettingsRow |
| 682 | title={ |
| 683 | <label htmlFor="add-provider-instance-id"> |
| 684 | <T>Instance ID</T> |
| 685 | </label> |
| 686 | } |
| 687 | description={ |
| 688 | <span id="add-provider-instance-id-description"> |
| 689 | <T>Letters, digits, '-', or '_'.</T> |
| 690 | </span> |
| 691 | } |
| 692 | status={ |
| 693 | showInstanceIdError ? ( |
| 694 | <span |
| 695 | id="add-provider-instance-id-error" |
| 696 | role="alert" |
| 697 | className="text-destructive" |
| 698 | > |
| 699 | {instanceIdError} |
| 700 | </span> |
| 701 | ) : undefined |
| 702 | } |
| 703 | control={ |
| 704 | <Input |
| 705 | id="add-provider-instance-id" |
| 706 | aria-describedby={ |
| 707 | showInstanceIdError |
| 708 | ? "add-provider-instance-id-description add-provider-instance-id-error" |
| 709 | : "add-provider-instance-id-description" |
| 710 | } |
| 711 | size="sm" |
| 712 | className="w-full @min-[32rem]/settings-row:w-56" |
| 713 | placeholder={`${driver}_work`} |
| 714 | value={instanceId} |
| 715 | onChange={(event) => { |
| 716 | setIdentityDraft({ instanceIdOverride: event.target.value }); |
| 717 | }} |
| 718 | aria-invalid={showInstanceIdError} |
| 719 | /> |
| 720 | } |
| 721 | /> |
| 722 | <SettingsRow |
| 723 | title={gt("Accent color")} |
| 724 | description={gt("Optional marker shown in the picker.")} |
| 725 | control={ |
| 726 | <ProviderAccentColorPicker |
| 727 | displayName={label || driverOption.label} |
| 728 | value={accentColor || undefined} |
| 729 | onCommit={(value) => setIdentityDraft({ accentColor: value })} |
| 730 | layout="inline" |
| 731 | /> |
| 732 | } |
| 733 | /> |
| 734 | </SettingsGroup> |
| 735 | |
| 736 | {!isAcpRegistry && driverSettingsFields.length > 0 ? ( |
| 737 | <SettingsGroup variant="plain" className={cn(wizardStep !== 2 && "hidden")}> |
| 738 | <ProviderSettingsForm |
| 739 | definition={driverOption} |
| 740 | value={configDraft} |
| 741 | idPrefix={`add-provider-${driver}`} |
| 742 | variant="settings" |
| 743 | onChange={setConfigDraft} |
| 744 | managed={ |
| 745 | driverLocks && { keys: driverLocks.configKeys, reason: driverLocks.reason } |
| 746 | } |
| 747 | environmentId={environmentId} |
| 748 | /> |
| 749 | </SettingsGroup> |
| 750 | ) : !isAcpRegistry && wizardStep === 2 ? ( |
| 751 | <div className="grid gap-2"> |
| 752 | <p className="text-sm text-muted-foreground"> |
| 753 | <T>This driver has no required configuration. You can add the instance now.</T> |
| 754 | </p> |
| 755 | </div> |
| 756 | ) : null} |
| 757 | </WizardPanel> |
| 758 | |
| 759 | <WizardFooter> |
| 760 | <Button |
| 761 | variant={wizardStep === 0 ? "ghost-muted" : "outline"} |
| 762 | size="sm" |
| 763 | disabled={isSaving || isPreparingRegistryAgent} |
| 764 | onClick={() => { |
| 765 | if (wizardStep === 0) { |
| 766 | onOpenChange(false); |
| 767 | return; |
| 768 | } |
| 769 | setWizardStep((step) => Math.max(0, step - 1)); |
| 770 | }} |
| 771 | > |
| 772 | {wizardStep === 0 ? gt("Cancel") : gt("Back")} |
| 773 | </Button> |
| 774 | {wizardStep === 0 && driver === "codex" ? ( |
| 775 | <> |
| 776 | <Button variant="outline" size="sm" onClick={() => navigateToStep(1)}> |
| 777 | <T>Configure manually</T> |
| 778 | </Button> |
| 779 | {driverLocks?.configKeys.has("setupMode") ? null : ( |
| 780 | <ChatGptConnectionButton |
| 781 | size="sm" |
| 782 | onClick={() => setAddingChatGptAccount(true)} |
| 783 | /> |
| 784 | )} |
| 785 | </> |
| 786 | ) : wizardStep < (isAcpRegistry ? 1 : 2) ? ( |
| 787 | <Button |
| 788 | size="sm" |
| 789 | disabled={isPreparingRegistryAgent} |
| 790 | onClick={() => navigateToStep(wizardStep + 1)} |
| 791 | > |
| 792 | <T>Next</T> |
| 793 | </Button> |
| 794 | ) : ( |
| 795 | <Button |
| 796 | size="sm" |
| 797 | disabled={isSaving || !canManageProviders} |
| 798 | onClick={() => void handleSave()} |
| 799 | > |
| 800 | {isSaving |
| 801 | ? gt("Adding...") |
| 802 | : isAcpRegistry && !isLocalAcp |
| 803 | ? gt("Continue to sign-in") |
| 804 | : gt("Add instance")} |
| 805 | </Button> |
| 806 | )} |
| 807 | </WizardFooter> |
| 808 | </> |
| 809 | )} |
| 810 | </WizardPopup> |
| 811 | </Dialog> |
| 812 | ); |
| 813 | } |