import { useState } from "react"; import { Button } from "@/components/ui/button"; import { OnboardingCardField, OnboardingLoginCodeRow, ProviderApiKeyCard, ProviderSubscriptionCard, } from "@/components/AdapterLoginChrome"; import { AI_PROVIDERS, aiMethodLabel, type AiAuthMethod, type AiProvider, } from "./model"; /** Redacted view of the existing login lifecycle, supplied by the host. */ export type AiAuthState = | { phase: "idle" | "starting" | "submitting" | "connected" | "cancelled" } | { phase: "waiting"; authorizationUrl: string; code?: string } | { phase: "error" | "expired" | "unsupported"; message: string }; export interface AiConnectionAuthProps { provider: AiProvider; method: AiAuthMethod; state: AiAuthState; onStart: () => void; onSubmit: (value: string) => void; onCancel: () => void; onDone: () => void; } /** No provider calls or polling here: live hosts keep the existing login controllers. */ export function AiConnectionAuth(props: AiConnectionAuthProps) { // Remount private input state when the provider, method, or attempt changes phase. return ( ); } function AuthAttempt({ provider, method, state, onStart, onSubmit, onCancel, onDone, }: AiConnectionAuthProps) { const [value, setValue] = useState(""); const info = AI_PROVIDERS[provider]; const busy = state.phase === "starting" || state.phase === "submitting"; const unsupported = state.phase === "unsupported" || (method === "subscription" && !info.subscriptionName); const submit = () => { if (!value.trim() || busy) return; const submitted = value.trim(); setValue(""); onSubmit(submitted); }; return (

Connect {info.name}

{aiMethodLabel(provider, method)}

{state.phase === "connected" ? ( <>

Connected. This account is saved in Connections and can be reused.

) : ( <> {unsupported ? (

{state.phase === "unsupported" ? state.message : "This provider does not offer a subscription connection."}

) : ( <> {(state.phase === "error" || state.phase === "expired") && (

{state.message}

)} {state.phase === "cancelled" && (

Sign-in cancelled. No connection was created.

)} {method === "api_key" ? ( ) : busy ? ( ) : state.phase === "waiting" ? ( {provider === "anthropic" ? ( ) : ( )} ) : (

Sign in with your {info.subscriptionName}.

)} )}
{!unsupported && (method === "api_key" ? ( ) : state.phase === "waiting" ? ( provider === "anthropic" ? ( ) : ( Waiting for sign-in… ) ) : ( ))}
)}
); }