-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(anthropic): opt-in Claude OAuth account pool (#294) #578
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
gui/src/components/provider-workspace/AnthropicAccountPoolSettings.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| /** | ||
| * Opt-in Anthropic OAuth account pool controls (#294). | ||
| * Experimental — shows a strong warning because the feature is not battle-tested. | ||
| */ | ||
| import { useCallback, useEffect, useState } from "react"; | ||
| import { useT } from "../../i18n/shared"; | ||
|
|
||
| type PoolState = { | ||
| enabled: boolean; | ||
| threshold: number; | ||
| }; | ||
|
|
||
| export default function AnthropicAccountPoolSettings({ | ||
| apiBase, | ||
| accountCount, | ||
| }: { | ||
| apiBase: string; | ||
| accountCount: number; | ||
| }) { | ||
| const t = useT(); | ||
| const [state, setState] = useState<PoolState | null>(null); | ||
| const [draft, setDraft] = useState("80"); | ||
| const [saving, setSaving] = useState(false); | ||
| const [error, setError] = useState<string | null>(null); | ||
| const [loadError, setLoadError] = useState(false); | ||
|
|
||
| useEffect(() => { | ||
| let cancelled = false; | ||
| const ac = new AbortController(); | ||
| void (async () => { | ||
| try { | ||
| const res = await fetch(`${apiBase}/api/oauth/accounts/pool?provider=anthropic`, { | ||
| signal: ac.signal, | ||
| }); | ||
| if (!res.ok) throw new Error("load"); | ||
| const json = await res.json() as { enabled?: boolean; autoSwitchThreshold?: number }; | ||
| if (cancelled) return; | ||
| const nextEnabled = json.enabled === true; | ||
| const nextThreshold = typeof json.autoSwitchThreshold === "number" ? json.autoSwitchThreshold : 80; | ||
| setState({ enabled: nextEnabled, threshold: nextThreshold }); | ||
| setDraft(String(nextThreshold)); | ||
| setLoadError(false); | ||
| } catch { | ||
| if (cancelled || ac.signal.aborted) return; | ||
| setLoadError(true); | ||
| } | ||
| })(); | ||
| return () => { | ||
| cancelled = true; | ||
| ac.abort(); | ||
| }; | ||
| }, [apiBase]); | ||
|
|
||
| const save = useCallback(async (nextEnabled: boolean, nextThreshold: number) => { | ||
| setSaving(true); | ||
| setError(null); | ||
| try { | ||
| const res = await fetch(`${apiBase}/api/oauth/accounts/pool`, { | ||
| method: "PUT", | ||
| headers: { "content-type": "application/json" }, | ||
| body: JSON.stringify({ | ||
| provider: "anthropic", | ||
| enabled: nextEnabled, | ||
| autoSwitchThreshold: nextThreshold, | ||
| }), | ||
| }); | ||
| if (!res.ok) throw new Error("save"); | ||
| setState({ enabled: nextEnabled, threshold: nextThreshold }); | ||
| setDraft(String(nextThreshold)); | ||
| } catch { | ||
| setError(t("anthropicPool.saveFailed")); | ||
| } finally { | ||
| setSaving(false); | ||
| } | ||
| }, [apiBase, t]); | ||
|
|
||
| const enabled = state?.enabled === true; | ||
| const threshold = state?.threshold ?? 80; | ||
| const loading = state === null && !loadError; | ||
| // Always allow turning the pool off; only block enabling when fewer than 2 accounts. | ||
| const toggleDisabled = loading || saving || loadError || (!enabled && accountCount < 2); | ||
|
|
||
| return ( | ||
| <div className="card" style={{ marginTop: 12 }} aria-busy={loading || saving}> | ||
| <div className="card-row" style={{ alignItems: "flex-start", gap: 12 }}> | ||
| <div style={{ flex: 1 }}> | ||
| <strong>{t("anthropicPool.title")}</strong> | ||
| <div className="card-sub" style={{ marginTop: 4 }}> | ||
| {loadError | ||
| ? t("anthropicPool.loadFailed") | ||
| : loading | ||
| ? t("common.loading") | ||
| : enabled | ||
| ? t("anthropicPool.enabledDesc", { threshold }) | ||
| : t("anthropicPool.disabledDesc")} | ||
| </div> | ||
| </div> | ||
| <label className="toggle" style={{ display: "inline-flex", alignItems: "center", gap: 8 }}> | ||
| <input | ||
| type="checkbox" | ||
| checked={enabled} | ||
| disabled={toggleDisabled} | ||
| onChange={(event) => { | ||
| const next = event.target.checked; | ||
| void save(next, threshold); | ||
| }} | ||
| /> | ||
| <span>{enabled ? t("anthropicPool.on") : t("anthropicPool.off")}</span> | ||
| </label> | ||
| </div> | ||
|
|
||
| <div | ||
| role="alert" | ||
| className="card-sub" | ||
| style={{ | ||
| marginTop: 10, | ||
| padding: "8px 10px", | ||
| border: "1px solid var(--border, #c9a227)", | ||
| borderRadius: 6, | ||
| background: "color-mix(in srgb, var(--warn, #c9a227) 12%, transparent)", | ||
| }} | ||
| > | ||
| {t("anthropicPool.experimentalWarning")} | ||
| </div> | ||
|
|
||
| {accountCount < 2 && ( | ||
| <div className="card-sub" style={{ marginTop: 8 }}>{t("anthropicPool.needTwoAccounts")}</div> | ||
| )} | ||
|
|
||
| {enabled && ( | ||
| <label className="field" style={{ display: "block", marginTop: 12 }}> | ||
| <span className="field-label">{t("anthropicPool.threshold")}</span> | ||
| <input | ||
| className="input mono" | ||
| type="number" | ||
| min={0} | ||
| max={100} | ||
| step={1} | ||
| value={draft} | ||
| disabled={saving} | ||
| aria-label={t("anthropicPool.thresholdAria")} | ||
| onChange={(event) => setDraft(event.target.value)} | ||
| onBlur={() => { | ||
| const parsed = Number(draft); | ||
| if (!Number.isInteger(parsed) || parsed < 0 || parsed > 100) { | ||
| setDraft(String(threshold)); | ||
| setError(t("anthropicPool.thresholdInvalid")); | ||
| return; | ||
| } | ||
| if (parsed !== threshold) void save(true, parsed); | ||
| }} | ||
| /> | ||
| <div className="card-sub" style={{ marginTop: 4 }}>{t("anthropicPool.thresholdHelp")}</div> | ||
| </label> | ||
| )} | ||
|
|
||
| {error && ( | ||
| <div role="alert" className="card-sub" style={{ marginTop: 8, color: "var(--danger, #c44)" }}> | ||
| {error} | ||
| </div> | ||
| )} | ||
| </div> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.