Files
DeepTutor/web/components/settings/CodexOAuthCard.tsx
Bingxi Zhao (Frank) 47d05809ea release: v1.5.5
Maintenance release on top of v1.5.4, with two new ways to bring a model.

- OpenAI Codex is a first-party OAuth provider (#690): browser sign-in
  against your own ChatGPT plan replaces the API-key fields, credentials
  stay in <user-root>/private/openai-codex/ with owner-only permissions,
  and the managed profile is owner-bound so it is never handed out through
  grants or made active over an already-configured LLM.
- Eden AI joins as the 35th LLM binding (#671), an OpenAI-compatible
  gateway addressed as <provider>/<model>.
- Knowledge bases answer from a real document inventory instead of
  guessing from retrieval hits: a per-KB inventory rides the system prompt
  and a new kb_files tool enumerates on demand with glob/substring
  filters, mounted under rag's gate and deniable per partner.
- The rag tool cites the chunks, entities, and reports retrieval actually
  returned (#694) rather than an echo of its own query; the local LightRAG
  pipeline still surfaces nothing to cite.
- GraphRAG indexing runs on a worker thread with its own asyncio loop
  (#695), so UVICORN_LOOP=asyncio is no longer needed, and two config
  faults that broke the first run are fixed (#699).
- Assorted: unique optimistic message ids (#698, a v1.5.4 regression that
  dropped the assistant reply from the visible thread), partner-chat
  manual scrolling respected (#704), claude-opus-5 recognized as
  effort-based (#703), Kimi models omit temperature outright, and
  deeptutor start keeps relaying logs on legacy Windows code pages (#702).
- Typing: narrow the loopback callback server to asyncio.Server and gate
  the msvcrt lock path on sys.platform so it type-checks off Windows.

Release notes: assets/releases/ver1-5-5.md
2026-07-26 23:19:09 +08:00

248 lines
7.3 KiB
TypeScript

"use client";
import { useCallback, useEffect, useRef, useState } from "react";
import { ExternalLink, RefreshCw, ShieldCheck, Unplug } from "lucide-react";
import { useTranslation } from "react-i18next";
import Button from "@/components/ui/Button";
import {
cancelCodexLogin,
CodexOAuthApiError,
codexErrorMessageKey,
codexStatusMessageKey,
getCodexStatus,
logoutCodex,
refreshCodexModels,
shouldPollCodexStatus,
startCodexLogin,
type CodexOAuthStatus,
} from "@/lib/codex-oauth";
import { useSettings } from "./SettingsContext";
export function CodexOAuthCard() {
const { t } = useTranslation();
const { reloadSettings, hasUnsavedChanges, setToast } = useSettings();
const [status, setStatus] = useState<CodexOAuthStatus | null>(null);
const [pending, setPending] = useState(false);
const [errorKey, setErrorKey] = useState<string | null>(null);
const [pollTick, setPollTick] = useState(0);
const reloadedOperation = useRef<string | null>(null);
const loadStatus = useCallback(async () => {
try {
const next = await getCodexStatus();
setStatus(next);
setErrorKey(null);
return next;
} catch (error) {
setErrorKey(
codexErrorMessageKey(
error instanceof CodexOAuthApiError ? error.code : null,
),
);
return null;
}
}, []);
useEffect(() => {
void loadStatus();
}, [loadStatus]);
useEffect(() => {
if (!status || !shouldPollCodexStatus(status)) return;
// pollTick, not the status object, is what schedules the next poll: a failed
// request leaves `status` identical, and keying the timer off it alone would
// strand the card on "waiting" forever after one dropped response.
const timer = window.setTimeout(() => {
void loadStatus();
setPollTick((tick) => tick + 1);
}, 1_000);
return () => window.clearTimeout(timer);
}, [loadStatus, status, pollTick]);
// Reloading replaces the whole catalog draft, so this must never run behind
// the operator's back while they have unsaved edits open on another provider.
const syncCatalog = useCallback(async () => {
if (hasUnsavedChanges) {
setToast(t("codex.oauth.reloadDeferred"));
return;
}
await reloadSettings();
}, [hasUnsavedChanges, reloadSettings, setToast, t]);
useEffect(() => {
if (
status?.operation_state !== "completed" ||
!status.operation_id ||
reloadedOperation.current === status.operation_id
) {
return;
}
reloadedOperation.current = status.operation_id;
void syncCatalog();
}, [syncCatalog, status]);
const signIn = async () => {
const authWindow = window.open("about:blank", "_blank", "popup");
if (authWindow) authWindow.opener = null;
setPending(true);
setErrorKey(null);
try {
const started = await startCodexLogin();
if (authWindow) {
authWindow.location.replace(started.authorize_url);
} else {
window.location.assign(started.authorize_url);
}
setStatus(await getCodexStatus());
} catch (error) {
authWindow?.close();
setErrorKey(
codexErrorMessageKey(
error instanceof CodexOAuthApiError ? error.code : null,
),
);
} finally {
setPending(false);
}
};
const cancel = async () => {
setPending(true);
try {
setStatus(await cancelCodexLogin());
} catch (error) {
setErrorKey(
codexErrorMessageKey(
error instanceof CodexOAuthApiError ? error.code : null,
),
);
} finally {
setPending(false);
}
};
const refresh = async () => {
setPending(true);
try {
setStatus(await refreshCodexModels());
await syncCatalog();
setErrorKey(null);
} catch (error) {
setErrorKey(
codexErrorMessageKey(
error instanceof CodexOAuthApiError ? error.code : null,
),
);
} finally {
setPending(false);
}
};
const logout = async () => {
setPending(true);
try {
setStatus(await logoutCodex());
await syncCatalog();
setErrorKey(null);
} catch (error) {
setErrorKey(
codexErrorMessageKey(
error instanceof CodexOAuthApiError ? error.code : null,
),
);
} finally {
setPending(false);
}
};
const polling = Boolean(status && shouldPollCodexStatus(status));
const connected = status?.connection === "connected";
const messageKey =
errorKey || (status ? codexStatusMessageKey(status) : null);
return (
<section className="rounded-xl border border-[var(--border)] bg-[var(--muted)]/30 p-4">
<div className="flex items-start gap-3">
<ShieldCheck className="mt-0.5 h-5 w-5 text-[var(--primary)]" />
<div className="min-w-0 flex-1">
<p className="text-sm font-medium">{t("codex.oauth.title")}</p>
<p className="mt-1 text-xs text-[var(--muted-foreground)]">
{t("codex.oauth.isolated")}
</p>
<p className="mt-1 text-xs text-[var(--muted-foreground)]">
{t("codex.oauth.ownerBound")}
</p>
{!connected && (
<p className="mt-1 text-xs text-[var(--muted-foreground)]">
{t("codex.oauth.localOnly")}
</p>
)}
<p className="mt-1 text-xs text-amber-600 dark:text-amber-400">
{t("codex.oauth.experimental")}
</p>
{messageKey && (
<p className="mt-3 text-sm text-[var(--foreground)]">
{t(messageKey)}
</p>
)}
{connected && status?.model_count !== undefined && (
<p className="mt-1 text-xs text-[var(--muted-foreground)]">
{t("codex.oauth.modelCount", { count: status.model_count })}
</p>
)}
<div className="mt-4 flex flex-wrap gap-2">
{!connected && !polling && (
<Button
type="button"
size="sm"
loading={pending}
onClick={() => void signIn()}
icon={<ExternalLink className="h-4 w-4" />}
>
{t("codex.oauth.signIn")}
</Button>
)}
{polling && (
<Button
type="button"
size="sm"
variant="secondary"
disabled={pending}
onClick={() => void cancel()}
>
{t("codex.oauth.cancel")}
</Button>
)}
{connected && (
<>
<Button
type="button"
size="sm"
variant="secondary"
loading={pending}
onClick={() => void refresh()}
icon={<RefreshCw className="h-4 w-4" />}
>
{t("codex.oauth.refresh")}
</Button>
<Button
type="button"
size="sm"
variant="danger"
disabled={pending}
onClick={() => void logout()}
icon={<Unplug className="h-4 w-4" />}
>
{t("codex.oauth.logout")}
</Button>
</>
)}
</div>
</div>
</div>
</section>
);
}