+36
![github-actions[bot]](/assets/img/avatar_default.png)








Julius Marminge
maria-rcks
Bilal Bakr
shivam
Vitalii Yehorov
Jake Leventhal
Claude Opus 5
Alex Southwell
github-actions[bot]
Nicholas Wasmiller
PB
Exotic
Yash Singh
Devin
Theo Browne
Gabriel De Andrade
Dara Adedeji
scratchyone
Dominic Roy
chukfinley
Primož Ajdišek
benthecarman
NaveDanan
aaditagrawal
Aditya Garud
Nick Anisimov
MacKinley Smith
Yordis Prieto
t3-code[bot]
AKolenda
Guillermo Casanova
Otavio Salvador
Shirish Pothi
Ishaan Kothari
Bob Fowler
Anton Bezdenezhnykh
ValeraZSD
Ephraim
Ryan Ilano
Alex
maco
Cursor
Tristan Knight
PR Batch Tester
oliver
kamkm
de34391427
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Bilal Bakr <[email protected]> Co-authored-by: shivam <[email protected]> Co-authored-by: Vitalii Yehorov <[email protected]> Co-authored-by: Jake Leventhal <[email protected]> Co-authored-by: Claude Opus 5 (1M context) <[email protected]> Co-authored-by: Alex Southwell <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Nicholas Wasmiller <[email protected]> Co-authored-by: PB <[email protected]> Co-authored-by: Exotic <[email protected]> Co-authored-by: Yash Singh <[email protected]> Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Theo Browne <[email protected]> Co-authored-by: Gabriel De Andrade <[email protected]> Co-authored-by: Dara Adedeji <[email protected]> Co-authored-by: scratchyone <[email protected]> Co-authored-by: Dominic Roy <[email protected]> Co-authored-by: chukfinley <[email protected]> Co-authored-by: Primož Ajdišek <[email protected]> Co-authored-by: benthecarman <[email protected]> Co-authored-by: NaveDanan <[email protected]> Co-authored-by: aaditagrawal <[email protected]> Co-authored-by: Aditya Garud <[email protected]> Co-authored-by: Nick Anisimov <[email protected]> Co-authored-by: MacKinley Smith <[email protected]> Co-authored-by: Yordis Prieto <[email protected]> Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com> Co-authored-by: AKolenda <[email protected]> Co-authored-by: Guillermo Casanova <[email protected]> Co-authored-by: Otavio Salvador <[email protected]> Co-authored-by: Shirish Pothi <[email protected]> Co-authored-by: Ishaan Kothari <[email protected]> Co-authored-by: Bob Fowler <[email protected]> Co-authored-by: Anton Bezdenezhnykh <[email protected]> Co-authored-by: ValeraZSD <[email protected]> Co-authored-by: Ephraim <[email protected]> Co-authored-by: Ryan Ilano <[email protected]> Co-authored-by: Alex <[email protected]> Co-authored-by: maco <[email protected]> Co-authored-by: Cursor <[email protected]> Co-authored-by: Tristan Knight <[email protected]> Co-authored-by: PR Batch Tester <[email protected]> Co-authored-by: oliver <[email protected]> Co-authored-by: kamkm <[email protected]> Signed-off-by: Yordis Prieto <[email protected]>
476 lines
18 KiB
TypeScript
476 lines
18 KiB
TypeScript
import {
|
|
isAtomCommandInterrupted,
|
|
squashAtomCommandFailure,
|
|
type AtomCommandResult,
|
|
} from "@t3tools/client-runtime/state/runtime";
|
|
import type {
|
|
EnvironmentId,
|
|
ProviderAuthRespondInput,
|
|
ProviderAuthResponse,
|
|
ProviderInstanceId,
|
|
ServerProvider,
|
|
} from "@t3tools/contracts";
|
|
import { lazy, Suspense, useRef, useState } from "react";
|
|
import { CopyIcon } from "lucide-react";
|
|
|
|
import { writeTextToClipboard } from "../../hooks/useCopyToClipboard";
|
|
import { ensureLocalApi } from "../../localApi";
|
|
import { useEnvironmentQuery } from "../../state/query";
|
|
import { serverEnvironment } from "../../state/server";
|
|
import { useAtomCommand } from "../../state/use-atom-command";
|
|
import { Button } from "../ui/button";
|
|
import { Input } from "../ui/input";
|
|
import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select";
|
|
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
|
|
import { SettingsRow } from "./settingsLayout";
|
|
import { RedactedSensitiveText } from "./RedactedSensitiveText";
|
|
|
|
const ProviderAuthTerminal = lazy(() => import("./ProviderAuthTerminal"));
|
|
|
|
/** All actions target the provider's environment, even when the browser is on another device. */
|
|
export function ProviderAuthenticationSection({
|
|
environmentId,
|
|
environmentLabel,
|
|
instanceId,
|
|
provider,
|
|
readOnly,
|
|
}: {
|
|
readonly environmentId: EnvironmentId;
|
|
readonly environmentLabel: string;
|
|
readonly instanceId: ProviderInstanceId;
|
|
readonly provider: ServerProvider;
|
|
readonly readOnly: boolean;
|
|
}) {
|
|
const target = { environmentId, input: { instanceId } };
|
|
const query = useEnvironmentQuery(serverEnvironment.providerAuthState(target));
|
|
const commands = { reportFailure: false, reportDefect: false };
|
|
const start = useAtomCommand(serverEnvironment.startProviderAuth, commands);
|
|
const respond = useAtomCommand(serverEnvironment.respondProviderAuth, commands);
|
|
const complete = useAtomCommand(serverEnvironment.completeProviderAuth, commands);
|
|
const cancel = useAtomCommand(serverEnvironment.cancelProviderAuth, commands);
|
|
const logout = useAtomCommand(serverEnvironment.logoutProviderAuth, commands);
|
|
const [methodId, setMethodId] = useState("");
|
|
const [draft, setDraft] = useState({ id: "", values: {} as Record<string, string> });
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [pending, setPending] = useState(false);
|
|
const pendingRef = useRef(false);
|
|
const terminalQueue = useRef<ProviderAuthRespondInput[]>([]);
|
|
const terminalSending = useRef(false);
|
|
const auth = query.data;
|
|
const interaction = auth?.interaction;
|
|
const active =
|
|
auth?.phase === "starting" || auth?.phase === "waiting" || auth?.phase === "verifying";
|
|
const signedIn =
|
|
provider.auth.status === "authenticated" ||
|
|
(provider.auth.status === "unknown" && auth?.phase === "succeeded");
|
|
const isDiscovering =
|
|
provider.driver === "acpRegistry" &&
|
|
!active &&
|
|
!signedIn &&
|
|
!query.error &&
|
|
auth?.methods === undefined;
|
|
const needsExternalSetup =
|
|
!active &&
|
|
!signedIn &&
|
|
(provider.setup?.canAuthenticate === false ||
|
|
(provider.driver === "acpRegistry" && auth?.methods?.length === 0));
|
|
const accountDescription = active
|
|
? auth?.phase === "starting"
|
|
? "Starting sign-in…"
|
|
: auth?.phase === "verifying"
|
|
? "Checking your account…"
|
|
: interaction?.type === "terminal"
|
|
? "Complete sign-in in the terminal below."
|
|
: interaction?.type === "credentials"
|
|
? "Enter your credentials below."
|
|
: "Finish signing in in your browser."
|
|
: signedIn
|
|
? "Signed in."
|
|
: isDiscovering
|
|
? "Discovering sign-in methods…"
|
|
: needsExternalSetup
|
|
? "No in-app sign-in advertised. Follow the provider's docs to finish setup."
|
|
: `Sign in on ${environmentLabel}.`;
|
|
const statusMessage = auth?.phase === "failed" ? auth.message : null;
|
|
const disabled = readOnly || pending || query.error !== null || isDiscovering;
|
|
const draftId = `${auth?.flowId ?? ""}:${interaction?.id ?? ""}`;
|
|
const values = draft.id === draftId ? draft.values : {};
|
|
const url =
|
|
interaction?.type === "browser" || interaction?.type === "deviceCode"
|
|
? interaction.url
|
|
: auth?.authorizationUrl;
|
|
|
|
async function run(command: () => Promise<AtomCommandResult<unknown, unknown>>) {
|
|
if (pendingRef.current) return false;
|
|
pendingRef.current = true;
|
|
setPending(true);
|
|
setError(null);
|
|
let succeeded = false;
|
|
try {
|
|
const result = await command();
|
|
if (result._tag === "Success") succeeded = true;
|
|
else if (!isAtomCommandInterrupted(result)) {
|
|
const failure = squashAtomCommandFailure(result);
|
|
setError(
|
|
failure instanceof Error ? failure.message : "Provider sign-in failed. Try again.",
|
|
);
|
|
}
|
|
} catch {
|
|
setError("Provider sign-in failed. Try again.");
|
|
}
|
|
pendingRef.current = false;
|
|
setPending(false);
|
|
return succeeded;
|
|
}
|
|
|
|
async function send(response: ProviderAuthResponse) {
|
|
if (!auth?.flowId || !interaction) return false;
|
|
return run(() =>
|
|
respond({
|
|
environmentId,
|
|
input: { instanceId, flowId: auth.flowId!, interactionId: interaction.id, response },
|
|
}),
|
|
);
|
|
}
|
|
|
|
async function openBrowser() {
|
|
if (!url || readOnly) return;
|
|
const requiresConsent = interaction?.type === "browser" && interaction.requiresConsent;
|
|
// Browsers block tabs opened after an await, so the web build reserves one
|
|
// while the environment records consent.
|
|
const pending = requiresConsent && !window.desktopBridge ? window.open("", "_blank") : null;
|
|
if (pending) pending.opener = null;
|
|
try {
|
|
// Consent is checked on the environment before opening a provider URL locally.
|
|
if (requiresConsent && !(await send({ type: "browser", action: "accept" }))) {
|
|
pending?.close();
|
|
return;
|
|
}
|
|
if (pending) pending.location.href = url;
|
|
else await ensureLocalApi().shell.openExternal(url);
|
|
setError(null);
|
|
} catch {
|
|
pending?.close();
|
|
setError("Could not open the sign-in page. Copy the link and open it in your browser.");
|
|
}
|
|
}
|
|
|
|
function updateDraft(name: string, value: string) {
|
|
setDraft({ id: draftId, values: { ...values, [name]: value } });
|
|
}
|
|
|
|
return (
|
|
<SettingsRow
|
|
title="Account"
|
|
description={
|
|
signedIn && !active && provider.auth.email?.trim() ? (
|
|
<span>
|
|
Signed in as{" "}
|
|
<RedactedSensitiveText
|
|
key={provider.auth.email}
|
|
value={provider.auth.email}
|
|
ariaLabel="Toggle account email visibility"
|
|
revealTooltip="Click to reveal email"
|
|
hideTooltip="Click to hide email"
|
|
className="max-w-full truncate"
|
|
/>
|
|
</span>
|
|
) : (
|
|
<span role="status">{accountDescription}</span>
|
|
)
|
|
}
|
|
status={
|
|
statusMessage ? (
|
|
<p role="status" className="[overflow-wrap:anywhere]">
|
|
{statusMessage}
|
|
</p>
|
|
) : undefined
|
|
}
|
|
control={
|
|
<div className="flex min-w-0 flex-wrap items-center gap-1.5">
|
|
{!active && (auth?.methods?.length ?? 0) > 1 ? (
|
|
<Select
|
|
value={auth?.methods?.some((method) => method.id === methodId) ? methodId : ""}
|
|
disabled={disabled}
|
|
onValueChange={(value) => setMethodId(value ?? "")}
|
|
>
|
|
<SelectTrigger size="sm" aria-label="Sign-in method" className="w-44">
|
|
<SelectValue>
|
|
{auth?.methods?.find((method) => method.id === methodId)?.name ??
|
|
"Provider default"}
|
|
</SelectValue>
|
|
</SelectTrigger>
|
|
<SelectPopup>
|
|
<SelectItem value="">Provider default</SelectItem>
|
|
{auth?.methods?.map((method) => (
|
|
<SelectItem key={method.id} value={method.id}>
|
|
{method.name}
|
|
</SelectItem>
|
|
))}
|
|
</SelectPopup>
|
|
</Select>
|
|
) : null}
|
|
{url ? (
|
|
<>
|
|
<Button
|
|
size="sm"
|
|
variant="outline"
|
|
disabled={disabled}
|
|
onClick={() => void openBrowser()}
|
|
>
|
|
Open browser
|
|
</Button>
|
|
<Tooltip>
|
|
<TooltipTrigger
|
|
render={
|
|
<Button
|
|
aria-label="Copy sign-in link"
|
|
size="icon-sm"
|
|
variant="ghost-muted"
|
|
disabled={disabled}
|
|
onClick={() => {
|
|
// Copy inside the click so the clipboard keeps the user
|
|
// activation; the link only works once consent is recorded.
|
|
const copied = writeTextToClipboard(url, "Provider sign-in link");
|
|
void (async () => {
|
|
await copied;
|
|
if (interaction?.type === "browser" && interaction.requiresConsent)
|
|
await send({ type: "browser", action: "accept" });
|
|
})().catch(() => setError("Could not copy the sign-in link."));
|
|
}}
|
|
>
|
|
<CopyIcon />
|
|
</Button>
|
|
}
|
|
/>
|
|
<TooltipPopup>Copy sign-in link</TooltipPopup>
|
|
</Tooltip>
|
|
</>
|
|
) : null}
|
|
<>
|
|
{needsExternalSetup && provider.setup?.documentationUrl ? (
|
|
<Button
|
|
size="sm"
|
|
variant="outline"
|
|
render={
|
|
<a href={provider.setup.documentationUrl} target="_blank" rel="noreferrer" />
|
|
}
|
|
>
|
|
Open docs
|
|
</Button>
|
|
) : active && auth?.flowId ? (
|
|
<Button
|
|
size="sm"
|
|
variant="ghost-muted"
|
|
aria-label="Cancel sign-in"
|
|
disabled={disabled}
|
|
onClick={() =>
|
|
void run(() =>
|
|
cancel({ environmentId, input: { instanceId, flowId: auth.flowId! } }),
|
|
)
|
|
}
|
|
>
|
|
Cancel
|
|
</Button>
|
|
) : !active && !needsExternalSetup && provider.setup?.canAuthenticate !== false ? (
|
|
<Button
|
|
size="sm"
|
|
variant="outline"
|
|
disabled={disabled || !provider.enabled || !provider.installed || !auth}
|
|
onClick={() =>
|
|
void run(() =>
|
|
start({
|
|
environmentId,
|
|
input: {
|
|
instanceId,
|
|
...(methodId && auth?.methods?.some((method) => method.id === methodId)
|
|
? { methodId }
|
|
: {}),
|
|
},
|
|
}),
|
|
)
|
|
}
|
|
>
|
|
{signedIn
|
|
? "Change account"
|
|
: auth?.phase === "failed" || auth?.phase === "cancelled"
|
|
? "Retry sign-in"
|
|
: "Sign in"}
|
|
</Button>
|
|
) : null}
|
|
{!active && signedIn && (provider.auth.canLogout ?? provider.setup?.canAuthenticate) ? (
|
|
<Button
|
|
size="sm"
|
|
variant="ghost"
|
|
disabled={disabled || !auth}
|
|
onClick={() => {
|
|
void ensureLocalApi()
|
|
.dialogs.confirm(
|
|
`Sign out of ${provider.displayName ?? provider.driver} on ${environmentLabel}? This stops running threads that share this sign-in. Thread history is kept.`,
|
|
)
|
|
.then((confirmed) => {
|
|
if (confirmed) void run(() => logout(target));
|
|
});
|
|
}}
|
|
>
|
|
Sign out
|
|
</Button>
|
|
) : null}
|
|
</>
|
|
</div>
|
|
}
|
|
>
|
|
{interaction?.type === "terminal" ||
|
|
interaction?.type === "credentials" ||
|
|
interaction?.type === "deviceCode" ||
|
|
(url && (interaction?.type === "browser" ? interaction.acceptsCallback : !interaction)) ||
|
|
error ||
|
|
query.error ? (
|
|
<>
|
|
{interaction?.type === "deviceCode" ? (
|
|
<p className="py-2 text-sm text-muted-foreground">
|
|
Enter code{" "}
|
|
<code className="select-all font-mono text-foreground">{interaction.userCode}</code>{" "}
|
|
in your browser.
|
|
</p>
|
|
) : null}
|
|
{interaction?.type === "terminal" ? (
|
|
<div className="py-2">
|
|
<Suspense
|
|
fallback={
|
|
<p className="text-xs text-muted-foreground">Loading sign-in terminal…</p>
|
|
}
|
|
>
|
|
<ProviderAuthTerminal
|
|
key={`${auth?.flowId}:${interaction.id}`}
|
|
output={interaction.output}
|
|
outputOffset={interaction.outputOffset}
|
|
onResponse={(response) => {
|
|
if (readOnly || !auth?.flowId) return;
|
|
for (
|
|
let offset = 0;
|
|
offset < Math.max(1, response.data.length);
|
|
offset += 4_096
|
|
) {
|
|
terminalQueue.current.push({
|
|
instanceId,
|
|
flowId: auth.flowId,
|
|
interactionId: interaction.id,
|
|
response: {
|
|
...response,
|
|
data: response.data.slice(offset, offset + 4_096),
|
|
},
|
|
});
|
|
}
|
|
if (terminalSending.current) return;
|
|
terminalSending.current = true;
|
|
void (async () => {
|
|
while (terminalQueue.current.length > 0) {
|
|
const input = terminalQueue.current.shift()!;
|
|
const result = await respond({ environmentId, input });
|
|
if (result._tag !== "Success") {
|
|
terminalQueue.current = [];
|
|
if (!isAtomCommandInterrupted(result))
|
|
setError("The provider sign-in terminal is no longer available.");
|
|
break;
|
|
}
|
|
}
|
|
})()
|
|
.catch(() => {
|
|
terminalQueue.current = [];
|
|
setError("Could not send input to the provider sign-in terminal.");
|
|
})
|
|
.finally(() => {
|
|
terminalSending.current = false;
|
|
});
|
|
}}
|
|
/>
|
|
</Suspense>
|
|
</div>
|
|
) : null}
|
|
{interaction?.type === "credentials" ? (
|
|
<form
|
|
className="grid gap-2 py-2 text-sm leading-normal"
|
|
onSubmit={(event) => {
|
|
event.preventDefault();
|
|
void send({ type: "credentials", values }).then((sent) => {
|
|
if (sent) setDraft({ id: "", values: {} });
|
|
});
|
|
}}
|
|
>
|
|
{interaction.fields.map((field) => (
|
|
<label key={field.name} className="grid gap-1">
|
|
{field.label}
|
|
<Input
|
|
size="sm"
|
|
type={field.secret ? "password" : "text"}
|
|
autoComplete="off"
|
|
value={values[field.name] ?? ""}
|
|
disabled={disabled}
|
|
maxLength={16_384}
|
|
onChange={(event) => updateDraft(field.name, event.target.value)}
|
|
/>
|
|
</label>
|
|
))}
|
|
<Button
|
|
type="submit"
|
|
size="sm"
|
|
variant="outline"
|
|
className="w-fit"
|
|
disabled={disabled}
|
|
>
|
|
Connect
|
|
</Button>
|
|
</form>
|
|
) : null}
|
|
{url && (interaction?.type === "browser" ? interaction.acceptsCallback : !interaction) ? (
|
|
<form
|
|
className="grid gap-2 py-2 text-sm leading-normal"
|
|
onSubmit={(event) => {
|
|
event.preventDefault();
|
|
if (!auth?.flowId || !values.callback?.trim()) return;
|
|
void run(() =>
|
|
complete({
|
|
environmentId,
|
|
input: { instanceId, flowId: auth.flowId!, callbackUrl: values.callback! },
|
|
}),
|
|
).then((sent) => {
|
|
if (sent) setDraft({ id: "", values: {} });
|
|
});
|
|
}}
|
|
>
|
|
<label className="grid gap-1">
|
|
If the final localhost page does not load, paste its full URL here.
|
|
<Input
|
|
size="sm"
|
|
id={`provider-callback-${instanceId}`}
|
|
type="url"
|
|
autoComplete="off"
|
|
value={values.callback ?? ""}
|
|
disabled={disabled}
|
|
maxLength={16_384}
|
|
onChange={(event) => updateDraft("callback", event.target.value)}
|
|
/>
|
|
</label>
|
|
<Button
|
|
type="submit"
|
|
size="sm"
|
|
variant="outline"
|
|
className="w-fit"
|
|
disabled={disabled || !values.callback?.trim()}
|
|
>
|
|
Continue
|
|
</Button>
|
|
</form>
|
|
) : null}
|
|
{error || query.error ? (
|
|
<p role="alert" className="py-2 text-xs text-destructive">
|
|
{error ?? query.error}
|
|
</p>
|
|
) : null}
|
|
</>
|
|
) : null}
|
|
</SettingsRow>
|
|
);
|
|
}
|