Repository navigation
feat(tui): /logout signs out of the provider behind the current model
#1845
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
Changes from all commits
9d2081f
4840a3a
c5fc61a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ import type { BuiltinTuiPlugin } from "@opencode-ai/tui/builtins" | |
| import { createSignal, Show } from "solid-js" | ||
| import { AltimateApi } from "@/altimate/api/client" | ||
| import { Telemetry } from "@/altimate/telemetry" | ||
| import { FreeTier } from "@/altimate/free/client" // altimate_change — /logout for Altimate Base | ||
|
|
||
| const id = "altimate:provider-credentials" | ||
|
|
||
|
|
@@ -101,6 +102,21 @@ async function logout(api: TuiPluginApi) { | |
| } | ||
| // altimate_change end | ||
|
|
||
| // altimate_change start — /logout when the current model is Altimate Base. Same steps as | ||
| // `altimate auth logout altimate-free`: clear the managed credential, then drop any legacy | ||
| // auth-store entry left by builds before the managed store. | ||
| async function logoutBase(api: TuiPluginApi) { | ||
| try { | ||
| await FreeTier.logout() | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor (edge: remote attach only). |
||
| await api.client.auth.remove({ providerID: FreeTier.PROVIDER_ID }, { throwOnError: true }) | ||
| await api.client.instance.dispose() | ||
| api.ui.toast({ variant: "success", message: "Logged out of Altimate Base" }) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor (real, cosmetic). The gateway and Base paths toast success right after |
||
| } catch (err) { | ||
| api.ui.toast({ variant: "error", message: err instanceof Error ? err.message : "Logout failed" }) | ||
| } | ||
| } | ||
| // altimate_change end | ||
|
|
||
| const tui: TuiPlugin = async (api) => { | ||
| api.keymap.registerLayer({ | ||
| commands: [ | ||
|
|
@@ -123,11 +139,22 @@ const tui: TuiPlugin = async (api) => { | |
| void logout(api) | ||
| }, | ||
| }, | ||
| { | ||
| name: "altimate.provider.logout.base", | ||
| title: "Log out of Altimate Base", | ||
| category: "Altimate", | ||
| namespace: "palette", | ||
| hidden: true, | ||
| run() { | ||
| void logoutBase(api) | ||
| }, | ||
| }, | ||
| // altimate_change end | ||
| ], | ||
| bindings: api.tuiConfig.keybinds.gather("altimate.palette", [ | ||
| "altimate.provider.connect", | ||
| "altimate.provider.logout", | ||
| "altimate.provider.logout.base", | ||
| ]), | ||
| }) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1279,6 +1279,11 @@ export namespace Provider { | |
| id: ProviderID.zod, | ||
| name: z.string(), | ||
| source: z.enum(["env", "config", "custom", "api"]), | ||
| // altimate_change start — the login saved for this provider, if any. `source` can't tell it: a | ||
| // config entry or an env key overrides it, and a subscription loads as "custom". The TUI's | ||
| // /logout reads this to name and remove the right login. | ||
| auth: z.enum(["api", "oauth", "wellknown"]).optional(), | ||
| // altimate_change end | ||
| env: z.string().array(), | ||
| key: z.string().optional(), | ||
| options: z.record(z.string(), z.any()), | ||
|
|
@@ -2028,6 +2033,16 @@ export namespace Provider { | |
| mergeProvider(providerID, partial) | ||
| } | ||
|
|
||
| // altimate_change start — record the saved login type; see `auth` on Info. Its own read keeps the | ||
| // upstream API-key loop above unchanged. OPENCODE_AUTH_CONTENT is parsed without validation, so | ||
| // only a known type is reported. | ||
| const savedLogins = await Auth.all() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SUGGESTION: Second
Hoist a single |
||
| for (const [id, provider] of Object.entries(providers)) { | ||
| const type = savedLogins[id]?.type | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: This lookup misses well-known logins because the auth store keys them by URL, while the fetched provider is identified by its remote-config provider ID. Prompt for AI agents |
||
| if (type === "api" || type === "oauth" || type === "wellknown") provider.auth = type | ||
| } | ||
| // altimate_change end | ||
|
|
||
| for (const [id, provider] of Object.entries(providers)) { | ||
| const providerID = ProviderID.make(id) | ||
| if (!isProviderAllowed(providerID)) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3088,3 +3088,68 @@ test("fromModelsDevProvider survives a catalog with a malformed entry mixed in", | |||||
| expect(database["bad-id"]).toBeUndefined() | ||||||
| }) | ||||||
| // altimate_change end | ||||||
|
|
||||||
| // altimate_change start — `auth` tells clients which login is saved; the TUI's /logout reads it | ||||||
| test("provider reports a saved API key as auth: api even when config also lists it", async () => { | ||||||
| const saved = await Auth.get("openai") | ||||||
| await Auth.set("openai", { type: "api", key: "sk-test-saved" }) | ||||||
| try { | ||||||
| await using tmp = await tmpdir({ config: { provider: { openai: {} } } }) | ||||||
| await provideProviderTestInstance({ | ||||||
| directory: tmp.path, | ||||||
| init: async () => Env.remove("OPENAI_API_KEY"), | ||||||
| fn: async () => { | ||||||
| const providers = await Provider.list() | ||||||
| expect(providers[ProviderID.openai].source).toBe("config") | ||||||
| expect(providers[ProviderID.openai].auth).toBe("api") | ||||||
| }, | ||||||
| }) | ||||||
| } finally { | ||||||
| if (saved) await Auth.set("openai", saved) | ||||||
| else await Auth.remove("openai") | ||||||
| } | ||||||
| }) | ||||||
|
|
||||||
| test("provider reports a saved sign-in as auth: oauth", async () => { | ||||||
| const saved = await Auth.get("openai") | ||||||
| await Auth.set("openai", { | ||||||
| type: "oauth", | ||||||
| refresh: "refresh-test", | ||||||
| access: "access-test", | ||||||
| expires: Date.now() + 3_600_000, | ||||||
| }) | ||||||
| try { | ||||||
| await using tmp = await tmpdir({ config: {} }) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: This test does not register the OpenAI provider: OAuth credentials do not autoload OpenAI, so Prompt for AI agents
Suggested change
|
||||||
| await provideProviderTestInstance({ | ||||||
| directory: tmp.path, | ||||||
| init: async () => Env.remove("OPENAI_API_KEY"), | ||||||
| fn: async () => { | ||||||
| const providers = await Provider.list() | ||||||
| expect(providers[ProviderID.openai].auth).toBe("oauth") | ||||||
| }, | ||||||
| }) | ||||||
| } finally { | ||||||
| if (saved) await Auth.set("openai", saved) | ||||||
| else await Auth.remove("openai") | ||||||
| } | ||||||
| }) | ||||||
|
|
||||||
| test("provider set only by an environment variable has no saved login", async () => { | ||||||
| const saved = await Auth.get("anthropic") | ||||||
| if (saved) await Auth.remove("anthropic") | ||||||
| try { | ||||||
| await using tmp = await tmpdir({ config: {} }) | ||||||
| await provideProviderTestInstance({ | ||||||
| directory: tmp.path, | ||||||
| init: async () => Env.set("ANTHROPIC_API_KEY", "test-api-key"), | ||||||
| fn: async () => { | ||||||
| const providers = await Provider.list() | ||||||
| expect(providers[ProviderID.anthropic].source).toBe("env") | ||||||
| expect(providers[ProviderID.anthropic].auth).toBeUndefined() | ||||||
| }, | ||||||
| }) | ||||||
| } finally { | ||||||
| if (saved) await Auth.set("anthropic", saved) | ||||||
| } | ||||||
| }) | ||||||
| // altimate_change end | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,6 +85,7 @@ import { FrecencyProvider } from "./component/prompt/frecency" | |
| import { PromptStashProvider } from "./component/prompt/stash" | ||
| import { DialogAlert } from "./ui/dialog-alert" | ||
| import { DialogConfirm } from "./ui/dialog-confirm" | ||
| import { logoutTarget, logoutTitle } from "./util/logout-target" // altimate_change — /logout | ||
| import { ToastProvider, useToast } from "./ui/toast" | ||
| import { isDefaultTitle } from "./util/session" | ||
| import { KVProvider, useKV } from "./context/kv" | ||
|
|
@@ -887,6 +888,87 @@ function App(props: { onSnapshot?: () => Promise<string[]>; pluginHost: TuiPlugi | |
| if (workspace?.type !== "worktree" || !workspace.directory) return | ||
| return workspace | ||
| }) | ||
| // altimate_change start — /logout: the provider behind the current model, and how to sign out of it | ||
| const logoutTargetNow = createMemo(() => logoutTarget(local.model.current(), sync.data.provider)) | ||
|
|
||
| async function logoutCurrentProvider() { | ||
| const target = logoutTargetNow() | ||
| if (!target) return | ||
| if (target.kind === "external") { | ||
| toast.show({ variant: "warning", message: target.hint, duration: 6000 }) | ||
| return | ||
| } | ||
| const confirmed = await DialogConfirm.show(dialog, "Log out", `Log out of ${target.label}?`) | ||
| if (confirmed !== true) return | ||
| const othersConnected = sync.data.provider_next.connected.some((id) => id !== target.providerID) | ||
| if (target.kind === "gateway") { | ||
| // The gateway and Base sign-outs need opencode-side modules (AltimateApi, FreeTier), so they | ||
| // run in packages/opencode/src/plugin/tui/altimate/provider-credentials.tsx. | ||
| keymap.dispatchCommand("altimate.provider.logout") | ||
| } else if (target.kind === "base") { | ||
| keymap.dispatchCommand("altimate.provider.logout.base") | ||
| } else { | ||
| try { | ||
| await sdk.client.auth.remove({ providerID: target.providerID }, { throwOnError: true }) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor (edge race, pre-existing code). A ChatGPT request that is refreshing an expired token while |
||
| await sdk.client.instance.dispose(undefined, { throwOnError: true }) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SUGGESTION: A failed
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: Handle disposal failure separately from credential removal: Prompt for AI agents |
||
| } catch (err) { | ||
| toast.show({ variant: "error", message: err instanceof Error ? err.message : "Logout failed" }) | ||
| return | ||
| } | ||
| } | ||
| // The instance reload drops the provider from the synced list; the current model then falls | ||
| // back to another connected one. Say which, or reopen /connect when nothing is left. | ||
| const remaining = () => sync.data.provider.find((item) => item.id === target.providerID) | ||
| // A saved login can sit on top of an env key or config entry: once the reload lands, the | ||
| // provider is either gone or back without `auth`, and then it is still connected that way. | ||
| const settled = await waitFor(() => { | ||
| const provider = remaining() | ||
| return !provider || (target.kind === "saved" && !provider.auth) | ||
| }) | ||
| if (!settled) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SUGGESTION: A slow or lost reload now also skips the onboarding reset for a login that was already removed
|
||
| // The gateway and Base sign-outs report their own failure; this covers a reload that is slow or lost. | ||
| toast.show({ | ||
| variant: "warning", | ||
| message: `The provider list hasn't reloaded yet. If ${target.label} is still listed, run /logout again or restart.`, | ||
| duration: 8000, | ||
| }) | ||
| return | ||
| } | ||
| const still = remaining() | ||
| if (still) { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor (real, warned). When a config entry also defines the provider (e.g. an |
||
| const after = logoutTarget({ providerID: still.id }, sync.data.provider) | ||
| toast.show({ | ||
| variant: "warning", | ||
| message: `Removed the saved login. ${after?.kind === "external" ? after.hint : `${still.name} is still connected.`}`, | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor (edge provider, pre-existing loaders). The |
||
| duration: 8000, | ||
| }) | ||
| return | ||
| } | ||
| // Only now is the sign-out known to have landed; with nothing else connected, setup starts over. | ||
| if (!othersConnected) resetSetupComplete() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Read the remaining-provider check after the sign-out settles.
Read the connected list after Suggested fix
|
||
| const next = local.model.current() | ||
| if (!next || next.providerID === target.providerID) { | ||
| dialog.replace(() => <DialogModelWelcome />) | ||
| return | ||
| } | ||
| if (target.kind === "saved") { | ||
| toast.show({ | ||
| variant: "success", | ||
| message: `Logged out of ${target.label}. Now using ${local.model.parsed().model}.`, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| async function waitFor(check: () => boolean, timeoutMs = 10_000) { | ||
| const deadline = Date.now() + timeoutMs | ||
| while (!check()) { | ||
| if (Date.now() > deadline) return false | ||
| await new Promise((resolve) => setTimeout(resolve, 100)) | ||
| } | ||
| return true | ||
| } | ||
| // altimate_change end | ||
|
|
||
| const appCommands = createMemo(() => | ||
| [ | ||
| { | ||
|
|
@@ -1120,8 +1202,7 @@ function App(props: { onSnapshot?: () => Promise<string[]>; pluginHost: TuiPlugi | |
| category: "Provider", | ||
| }, | ||
| // altimate_change end | ||
| // altimate_change start — /auth: sign in to the Altimate LLM Gateway directly; | ||
| // /logout: clear the stored gateway credential and disconnect. | ||
| // altimate_change start — /auth: sign in to the Altimate LLM Gateway directly | ||
| { | ||
| name: "altimate.auth", | ||
| title: "Sign in to Altimate LLM Gateway", | ||
|
|
@@ -1132,16 +1213,16 @@ function App(props: { onSnapshot?: () => Promise<string[]>; pluginHost: TuiPlugi | |
| }, | ||
| category: "Provider", | ||
| }, | ||
| // altimate_change end | ||
| // altimate_change start — /logout signs out of the provider behind the current model; the | ||
| // title names it, e.g. "Log out of OpenAI (ChatGPT subscription)". See util/logout-target.ts. | ||
| { | ||
| name: "altimate.logout", | ||
| title: "Sign out of Altimate LLM Gateway", | ||
| title: logoutTitle(logoutTargetNow()), | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
|
||
| hidden: !logoutTargetNow(), | ||
| slashName: "logout", | ||
| run: () => { | ||
| // altimate_change — the credential clear + instance dispose + toast happen | ||
| // opencode-side (AltimateApi is unreachable from packages/tui); see | ||
| // packages/opencode/src/plugin/tui/altimate/provider-credentials.tsx. | ||
| keymap.dispatchCommand("altimate.provider.logout") | ||
| resetSetupComplete() | ||
| void logoutCurrentProvider() | ||
| }, | ||
| category: "Provider", | ||
| }, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Base sign-out in attached mode clears the client's credential, not the connected server's.
In attach mode, the plugin runs in the client TUI process.
logoutBasecallsFreeTier.logout(), which removes the managed credential in the client's data directory. The server keeps its ownaltimate-base.json. The server loads Base only from that dedicated store, and the legacyauth.removecall does not affect Base because provider loading skips that entry (provider.ts:1947). After the server is disposed, Base remains connected there. The toast still reports "Logged out of Altimate Base".When the server uses a different data directory, the sign-out can leave a usable Base credential on the server while the user believes it was removed. The client's Base registration, which can belong to another server, is also deleted.
Route the Base sign-out to the server that owns the managed credential. Show success only after that server's credential store has been cleared. Keep the direct local call only for the local TUI path, which shares the worker's data directory.