diff --git a/apps/web/src/aldo/AldoAgentConnections.tsx b/apps/web/src/aldo/AldoAgentConnections.tsx index dee5572096a1..87297aa8e6db 100644 --- a/apps/web/src/aldo/AldoAgentConnections.tsx +++ b/apps/web/src/aldo/AldoAgentConnections.tsx @@ -1,4 +1,5 @@ -import { useState } from "react"; +import { useEffect, useState } from "react"; +import type { AuthConnectorMethod } from "@t3tools/contracts"; import { AuthConnectorDialog } from "../components/settings/AuthConnectorDialog"; import { AGENT_AUTH_METHODS } from "../components/settings/authConnectorMethods"; @@ -6,7 +7,12 @@ import { SettingsRow, SettingsSection } from "../components/settings/settingsLay import { Button } from "../components/ui/button"; import { AldoBrandTile } from "./AldoBrandTile"; import { AldoPolicyRuleRow, useAldoPolicy } from "./AldoPolicy"; -import { disconnectAldoAccount } from "./accountApi"; +import { Input } from "../components/ui/input"; +import { + disconnectAldoAccount, + fetchOpenCodeProviders, + type AldoOpenCodeProvider, +} from "./accountApi"; import { useAldoAccounts } from "./useAldoAccounts"; const GO = { @@ -25,6 +31,136 @@ const GOOGLE = { returnInstruction: "After you sign in, paste the code or full address from Antigravity here.", }; +/** How many matches the provider search shows at once. */ +const SHOWN = 8; + +/** A key for one provider OpenCode knows, as the connect dialog offers it. */ +function otherProviderMethod(provider: AldoOpenCodeProvider) { + return { + // Aldo takes any provider's "-api-key"; the contract lists only T3's own. + method: `${provider.id}-api-key` as AuthConnectorMethod, + label: provider.name, + description: `Use ${provider.name} models with your own API key.`, + ...(provider.doc + ? { externalHelpUrl: provider.doc, externalHelpLabel: `${provider.name} documentation` } + : {}), + }; +} + +/** Another provider than the four OpenCode offers by name: search OpenCode's list, then connect its key. */ +function OtherOpenCodeProvider(props: { + /** The four named above and the keys already connected: not offered again. */ + readonly listed: ReadonlySet; + readonly onConnected: () => void; +}) { + const [open, setOpen] = useState(false); + const [providers, setProviders] = useState | null>(null); + const [failed, setFailed] = useState(false); + const [query, setQuery] = useState(""); + const [chosen, setChosen] = useState(null); + useEffect(() => { + if (!open || providers) return; + let cancelled = false; + fetchOpenCodeProviders() + .then((list) => !cancelled && setProviders(list)) + .catch(() => !cancelled && setFailed(true)); + return () => { + cancelled = true; + }; + }, [open, providers]); + const words = query.toLowerCase().split(/\s+/).filter(Boolean); + const matches = (providers ?? []).filter( + (p) => + !props.listed.has(`${p.id}-api-key`) && + words.every((w) => `${p.name} ${p.id}`.toLowerCase().includes(w)), + ); + return ( + + + { + setChosen(null); + setOpen(false); + setQuery(""); + props.onConnected(); + }} + /> + + ) : ( + + ) + } + > + {open && !chosen ? ( +
+ setQuery(event.currentTarget.value)} + autoFocus + /> + {failed ? ( +

+ Couldn't load the providers. Try again in a moment. +

+ ) : !providers ? ( +

Loading providers…

+ ) : matches.length === 0 ? ( +

+ {words.length + ? `No provider matches “${query}”.` + : "No other providers are available right now. Try again later."} +

+ ) : ( +
    + {matches.slice(0, SHOWN).map((provider) => ( +
  • + +
  • + ))} +
+ )} + {matches.length > SHOWN ? ( +

+ {matches.length - SHOWN} more. Type to narrow the list. +

+ ) : null} +
+ ) : null} +
+ ); +} + export function AldoAgentConnections() { const { accounts, refresh } = useAldoAccounts(); const policy = useAldoPolicy(); @@ -72,10 +208,12 @@ export function AldoAgentConnections() { ) : null} {methods .filter((method) => method.connected) - .map(({ method, kind }) => ( + .map(({ method, kind, label }) => ( option.method === method)?.label ?? "OpenCode key"} + title={ + offered.find((option) => option.method === method)?.label ?? label ?? "OpenCode key" + } status="Connected" inline control={ @@ -92,6 +230,12 @@ export function AldoAgentConnections() { } /> ))} + {accounts.opencode?.otherProviders ? ( + m.method))} + onConnected={refresh} + /> + ) : null} {accounts.antigravity ? ( ; + /** A key for another provider than the four Settings names carries its name (`label`). */ + methods: ReadonlyArray<{ method: string; kind: string; connected: boolean; label?: string }>; + /** Whether Aldo takes a key for any provider in fetchOpenCodeProviders. */ + otherProviders?: boolean; }; antigravity?: AldoAccount & { instanceId: string | null }; }; @@ -74,6 +77,20 @@ export async function connectAldoGitHost( await api(`/api/hosts/${kind}`, { method: "POST", body: JSON.stringify(values) }); } +/** A provider OpenCode knows that takes one API key (OpenCode's registry, as Aldo reads it). */ +export interface AldoOpenCodeProvider { + readonly id: string; + readonly name: string; + readonly doc: string | null; +} + +export async function fetchOpenCodeProviders(): Promise> { + const { providers } = await api<{ providers: AldoOpenCodeProvider[] }>( + "/api/connections/opencode/providers", + ); + return providers; +} + export async function disconnectAldoAccount( kind: AldoAccountKind | AldoGitHostKind | "antigravity" | `opencode:${string}`, ): Promise { diff --git a/apps/web/src/aldo/serverConfig.logic.test.ts b/apps/web/src/aldo/serverConfig.logic.test.ts index fd21895b742c..f4672b8a552d 100644 --- a/apps/web/src/aldo/serverConfig.logic.test.ts +++ b/apps/web/src/aldo/serverConfig.logic.test.ts @@ -3,6 +3,7 @@ import { describe, expect, it } from "vite-plus/test"; import { missingProviders, mergeProviderSnapshots, + withSavedBackends, providerInstances, providerKinds, withAldoTemplate, @@ -150,3 +151,28 @@ it("refreshes existing sleeping providers independently, without replacing a new expect(JSON.parse(refreshed!).config.providers).toEqual([future, claude]); expect(withAldoTemplate(refreshed, { providers: reported }, {})).toBeNull(); }); + +it("keeps the saved OpenCode backends a machine without their keys doesn't list", () => { + const model = (slug: string) => ({ slug, name: slug }); + const opencode = (slugs: string[]) => ({ + instanceId: "opencode", + driver: "opencode", + checkedAt: "2026-11-28T01:00:00Z", + models: slugs.map(model), + }); + const codex = { instanceId: "codex", driver: "codex", models: [model("gpt")] }; + const saved = [opencode(["aldo/a", "openrouter/x", "openrouter/y"]), codex]; + // A machine started before the OpenRouter key: Aldo's saved models of it stay, after the machine's own. + expect(withSavedBackends([opencode(["aldo/a", "aldo/b"]), codex], saved)).toEqual([ + opencode(["aldo/a", "aldo/b", "openrouter/x", "openrouter/y"]), + codex, + ]); + // A backend the machine lists is its own; other agents and an empty save are as reported. + expect(withSavedBackends([opencode(["openrouter/z"])], saved)).toEqual([ + opencode(["openrouter/z", "aldo/a"]), + ]); + expect(withSavedBackends([opencode(["aldo/a"]), codex], [])).toEqual([ + opencode(["aldo/a"]), + codex, + ]); +}); diff --git a/apps/web/src/aldo/serverConfig.logic.ts b/apps/web/src/aldo/serverConfig.logic.ts index 9879af2d81d6..85b60cba384e 100644 --- a/apps/web/src/aldo/serverConfig.logic.ts +++ b/apps/web/src/aldo/serverConfig.logic.ts @@ -120,3 +120,30 @@ export function mergeProviderSnapshots

( ...reported.filter((p) => !previous.some((old) => old.instanceId === p.instanceId)), ]; } + +/** An OpenCode model's backend: its slug's first segment (openrouter, opencode-go, aldo…). */ +function backendOf(model: unknown): string { + const slug = (model as { slug?: unknown } | null)?.slug; + return typeof slug === "string" && slug.includes("/") ? slug.slice(0, slug.indexOf("/")) : ""; +} + +/** + * A machine's OpenCode lists the backends of the keys it had when it started, + * so a machine started before a key was added lists fewer than the account + * has. Aldo's saved catalog keeps each backend a machine reported, and drops a + * disconnected one at once: a report gets the saved models of the backends it + * doesn't list, after its own. + */ +export function withSavedBackends< + P extends Provider & { readonly models?: ReadonlyArray }, +>(reported: ReadonlyArray

, saved: ReadonlyArray

): P[] { + return reported.map((provider) => { + if (provider.driver !== "opencode" || !Array.isArray(provider.models)) return provider; + const own = saved.find( + (p) => p.instanceId === provider.instanceId && p.driver === provider.driver, + ); + const listed = new Set(provider.models.map(backendOf)); + const kept = (own?.models ?? []).filter((model) => !listed.has(backendOf(model))); + return kept.length > 0 ? { ...provider, models: [...provider.models, ...kept] } : provider; + }); +} diff --git a/apps/web/src/aldo/serverConfig.ts b/apps/web/src/aldo/serverConfig.ts index 0086889f494b..32dbfcc67f8e 100644 --- a/apps/web/src/aldo/serverConfig.ts +++ b/apps/web/src/aldo/serverConfig.ts @@ -15,7 +15,7 @@ import { fetchAldoServerConfig, reportAldoServerConfig } from "./threadApi"; import { isAldoCloud, isAldoEnvironmentId } from "./api"; import { settingsAccounts } from "./settingsData"; import { loadAldoMachineSettings } from "./machineSettings"; -import { providerKinds, mergeProviderSnapshots } from "./serverConfig.logic"; +import { providerKinds, mergeProviderSnapshots, withSavedBackends } from "./serverConfig.logic"; type EncodedConfig = { environment: Record; @@ -34,6 +34,8 @@ const ACCOUNT_FOR_DRIVER: Record | null = null; let fetchedAt = 0; @@ -99,7 +101,10 @@ function capture(): void { const encoded = encodeServerConfig(config) as unknown as EncodedConfig; latest = { ...encoded, - providers: mergeProviderSnapshots(latest?.providers ?? [], encoded.providers), + providers: mergeProviderSnapshots( + latest?.providers ?? [], + withSavedBackends(encoded.providers, saved), + ), }; // Save when the models (or their availability) change, not on every check. const providers = JSON.stringify( @@ -129,6 +134,7 @@ async function template(): Promise { .then((config) => { if (!config) return; const reported = config as EncodedConfig; + if (Array.isArray(reported.providers)) saved = reported.providers; latest = { ...reported, providers: mergeProviderSnapshots(latest?.providers ?? [], reported.providers),