diff --git a/.gitignore b/.gitignore index 6512ebb..5c084ed 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ yarn.lock dist.zip dist plugin.zip +.env* +!.env.example diff --git a/changelogs.md b/changelogs.md index a7aa172..b4fd6d9 100644 --- a/changelogs.md +++ b/changelogs.md @@ -1,3 +1,10 @@ +# Unreleased + +- Fixed: custom endpoints can run without an API key, with key entry and removal available in Provider access. +- Fixed: editing an endpoint reuses its saved key for model discovery only when the base URL is unchanged. +- Fixed: reusing a saved key for model discovery no longer overwrites later key changes; changing the endpoint URL reports when its old key is removed. +- Fixed: Copilot and other sign-in errors show the provider's explanation, with Sign in available for access failures during a run. + # 0.5.0 - Added: Retry and Sign in on failed runs. diff --git a/readme.md b/readme.md index 50076f8..ec1eb4d 100644 --- a/readme.md +++ b/readme.md @@ -114,10 +114,10 @@ Add credentials under **Provider access**. Keys and tokens are kept in Acode's p Anthropic is API-key only. Anthropic doesn't allow third-party apps to use Claude subscription sign-in, so Claude Pro / Max login was removed. If you used it before, add a Console API key. -**Custom models and local servers** +**Custom models and endpoints** - In the model picker, paste any model id the provider accepts (e.g. `anthropic/claude-sonnet-4.6` on OpenRouter) to use a model missing from the catalog. -- **Provider access → Local endpoint** adds an OpenAI-compatible server such as llama.cpp, vLLM, LM Studio or Ollama. Set the base URL (e.g. `http://192.168.1.10:8080/v1`), list model ids or fetch them from `/models`, and mark whether they accept images or reasoning. Up to 20 endpoints. +- **Provider access → Add custom endpoint…** adds a local or hosted OpenAI-compatible server, including llama.cpp, vLLM, LM Studio or Ollama. Set the base URL (e.g. `http://192.168.1.10:8080/v1`), provide an API key only if required, and list model ids or fetch them from `/models`. Up to 20 endpoints. Provider access also lets you replace or remove a saved key. - `/scoped-models` chooses which models this agent offers. ## Tools diff --git a/src/app/agentController.ts b/src/app/agentController.ts index 3d9e752..13a2e28 100644 --- a/src/app/agentController.ts +++ b/src/app/agentController.ts @@ -34,7 +34,7 @@ import { AgentSession, type RevertResult } from "../session/agentSession"; import { pickGlobalSkillsFolder } from "../session/workspaceResources"; import { finalizeCustomEndpoint, - isLocalUrl, + normalizeOpenAIBaseUrl, probeOpenAIModels, upsertCustomEndpoint, type CustomEndpoint, @@ -554,11 +554,15 @@ export class AgentController { return this.providers.descriptors(); } - async saveCustomEndpoint(draft: CustomEndpointDraft, apiKey?: string): Promise { + async saveCustomEndpoint( + draft: CustomEndpointDraft, + apiKey?: string, + ): Promise<{ endpoint: CustomEndpoint; removedApiKey: boolean }> { const existing = this.settings.value.customEndpoints; const endpoint = finalizeCustomEndpoint(draft, existing); let models = endpoint.models; - const key = apiKey?.trim(); + const replacementKey = apiKey?.trim(); + const key = await this.#customEndpointApiKey(endpoint.baseUrl, replacementKey, draft.id); if (endpoint.fetchModels) { try { const discovered = await probeOpenAIModels(endpoint.baseUrl, key); @@ -572,13 +576,45 @@ export class AgentController { const saved = { ...endpoint, models }; const customModels = { ...this.settings.value.customModels }; delete customModels[saved.id]; + let removedApiKey = false; + if (draft.id && existing.find((item) => item.id === draft.id)?.baseUrl !== saved.baseUrl) { + // Clear the old credential before publishing a URL that must never receive it. + await this.credentials.delete(saved.id, undefined, async (credential) => { + removedApiKey = Boolean( + credential?.type === "api_key" && credential.key && !replacementKey, + ); + }); + } this.settings.update({ customEndpoints: upsertCustomEndpoint(existing, saved), customModels, }); - if (key) await this.credentials.setApiKey(saved.id, key); + // A reused probe key may have been removed or replaced while /models was pending. + if (replacementKey) await this.credentials.setApiKey(saved.id, replacementKey); await this.selectProvider(saved.id); - return saved; + return { endpoint: saved, removedApiKey }; + } + + async probeCustomEndpointModels( + baseUrl: string, + apiKey?: string, + endpointId?: string, + ): Promise { + const key = await this.#customEndpointApiKey(baseUrl, apiKey, endpointId); + return probeOpenAIModels(baseUrl, key); + } + + async #customEndpointApiKey( + baseUrl: string, + apiKey?: string, + endpointId?: string, + ): Promise { + if (apiKey?.trim()) return apiKey.trim(); + const endpoint = this.settings.value.customEndpoints.find((item) => item.id === endpointId); + // A saved key belongs to this exact endpoint, never a new destination typed into the form. + if (!endpoint || endpoint.baseUrl !== normalizeOpenAIBaseUrl(baseUrl)) return undefined; + const credential = await this.credentials.read(endpoint.id); + return credential?.type === "api_key" ? credential.key : undefined; } async removeCustomEndpoint(id: string): Promise { @@ -608,9 +644,11 @@ export class AgentController { ): Promise { const endpoint = this.providers.customEndpoint(providerId); if (!endpoint) throw new Error("That provider is not a custom OpenAI-compatible endpoint."); - const credential = await this.credentials.read(providerId); - const key = credential?.type === "api_key" ? credential.key : undefined; - const discovered = await probeOpenAIModels(endpoint.baseUrl, key); + const discovered = await this.probeCustomEndpointModels( + endpoint.baseUrl, + undefined, + providerId, + ); const models = [...new Set([...endpoint.models, ...discovered])].slice(0, 50); this.settings.update({ customEndpoints: upsertCustomEndpoint(this.settings.value.customEndpoints, { @@ -857,12 +895,10 @@ export class AgentController { } async hasCredential(providerId: ProviderId): Promise { - if (await this.credentials.read(providerId)) return true; - const endpoint = this.providers.customEndpoint(providerId); + const credential = await this.credentials.read(providerId); + // Presence only: the provider validates the key, OAuth grant, or environment when used. return Boolean( - endpoint && - isLocalUrl(endpoint.baseUrl) && - (await this.providers.models.checkAuth(providerId)), + credential?.type === "oauth" || credential?.key || Object.keys(credential?.env ?? {}).length, ); } diff --git a/src/providers/customEndpoints.ts b/src/providers/customEndpoints.ts index a82c5d5..57396bc 100644 --- a/src/providers/customEndpoints.ts +++ b/src/providers/customEndpoints.ts @@ -84,26 +84,6 @@ export function isLoopbackUrl(baseUrl: string): boolean { } } -export function isLocalUrl(baseUrl: string): boolean { - try { - const url = new URL(baseUrl); - const host = url.hostname.replace(/^\[|\]$/g, "").toLowerCase(); - if (host === "localhost" || host === "127.0.0.1" || host === "::1") return true; - if (host.endsWith(".local")) return true; - if (/^10\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(host)) return true; - if (/^192\.168\.\d{1,3}\.\d{1,3}$/.test(host)) return true; - if (/^169\.254\.\d{1,3}\.\d{1,3}$/.test(host)) return true; - const match = /^172\.(\d{1,3})\.\d{1,3}\.\d{1,3}$/.exec(host); - if (match) { - const second = Number(match[1]); - return second >= 16 && second <= 31; - } - return false; - } catch { - return false; - } -} - export function normalizeOpenAIBaseUrl(value: string): string | undefined { const trimmed = value.trim(); if (!trimmed || trimmed.length > 500) return undefined; @@ -285,7 +265,7 @@ export async function probeOpenAIModels( if (!url) throw new Error("Enter a valid OpenAI-compatible base URL first."); const headers: Record = { Accept: "application/json" }; const key = apiKey?.trim(); - if (key && key !== "none") headers.Authorization = `Bearer ${key}`; + if (key) headers.Authorization = `Bearer ${key}`; const timeout = new AbortController(); const timer = setTimeout(() => timeout.abort(), FETCH_TIMEOUT_MS); const abort = () => timeout.abort(); @@ -352,14 +332,13 @@ function customEndpointAuth(endpoint: CustomEndpoint): ApiKeyAuth { const key = await interaction.prompt({ type: "secret", message: `Enter ${name}`, - placeholder: isLocalUrl(endpoint.baseUrl) ? "optional for local servers" : "sk-…", }); return { type: "api_key", key }; }, resolve: async ({ credential }) => { if (credential?.key) return { auth: { apiKey: credential.key }, source: "stored credential" }; - if (isLocalUrl(endpoint.baseUrl)) return { auth: { apiKey: "none" }, source: "keyless" }; - return undefined; + // The SDK requires a nonempty key, but keyless endpoints must receive no bearer header. + return { auth: { apiKey: "none", headers: { Authorization: null } }, source: "keyless" }; }, }; } diff --git a/src/providers/portableOAuth.ts b/src/providers/portableOAuth.ts index 6b5e0c5..4159470 100644 --- a/src/providers/portableOAuth.ts +++ b/src/providers/portableOAuth.ts @@ -765,12 +765,28 @@ async function requestJson( init: RequestInit, ): Promise<{ ok: boolean; status: number; body: Json; url: string }> { const response = await nativeFetch(url, init); - const body = (await response.json().catch(() => ({}))) as Json; + const raw: unknown = await response.json().catch(() => undefined); + const body = raw && typeof raw === "object" && !Array.isArray(raw) ? (raw as Json) : {}; return { ok: response.ok, status: response.status, body, url }; } function requestError(url: string, status: number, body: Json): string { - return `${new URL(url).hostname} authentication failed (HTTP ${status})${optionalString(body, "error_description") ? `: ${optionalString(body, "error_description")}` : ""}`; + const copilotError = + body.error_details && typeof body.error_details === "object" + ? (body.error_details as Json) + : undefined; + const nestedError = + body.error && typeof body.error === "object" ? (body.error as Json) : undefined; + const detail = + optionalString(body, "error_description") ?? + (copilotError && optionalString(copilotError, "message")) ?? + optionalString(body, "message") ?? + (nestedError && optionalString(nestedError, "message")) ?? + optionalString(body, "error"); + const reason = + status === 401 ? "authentication failed" : status === 403 ? "access denied" : "request failed"; + const summary = detail?.replace(/\s+/g, " ").trim().slice(0, 160); + return `${new URL(url).hostname} (HTTP ${status}): ${summary || reason}`; } function requiredString(body: Json, field: string): string { diff --git a/src/providers/providerRegistry.ts b/src/providers/providerRegistry.ts index 15954e8..b51aa57 100644 --- a/src/providers/providerRegistry.ts +++ b/src/providers/providerRegistry.ts @@ -232,7 +232,7 @@ export function providerDescriptors( id: endpoint.id, name: endpoint.name, hint: endpoint.baseUrl, - keyPlaceholder: "sk-… or leave blank for local", + keyPlaceholder: "Optional if authentication is not required", apiKey: true, })), ]; diff --git a/src/session/runFailure.ts b/src/session/runFailure.ts index 88e829a..ce14547 100644 --- a/src/session/runFailure.ts +++ b/src/session/runFailure.ts @@ -41,8 +41,10 @@ const RETRYABLE = new Set([ "turn_limit", "faulted", ]); +// A 403 alone can mean a plan, region, or policy restriction; require auth context. const SIGN_IN_ERROR = /oauth|refresh token|sign[ -]?in|log[ -]?in|unauthori[sz]ed|\b401\b|invalid (api )?key|authentication failed/i; +const RATE_LIMIT_ERROR = /\b429\b|rate[ -]?limit|too many requests/i; export function failureNotice( failure: LiveState["runFailure"], @@ -58,6 +60,9 @@ export function failureNotice( title: TITLES[failure.reason] ?? "Request failed", message, retryable: RETRYABLE.has(failure.reason), - signIn: failure.reason === "model_error" && SIGN_IN_ERROR.test(message), + signIn: + failure.reason === "model_error" && + SIGN_IN_ERROR.test(message) && + !RATE_LIMIT_ERROR.test(message), }; } diff --git a/src/ui/App.tsx b/src/ui/App.tsx index c8a9e4a..ba0e425 100644 --- a/src/ui/App.tsx +++ b/src/ui/App.tsx @@ -26,12 +26,7 @@ import type { PublicAgentState, WorkspaceInfo, } from "../core/types"; -import { - isLoopbackUrl, - isLocalUrl, - probeOpenAIModels, - type CustomEndpoint, -} from "../providers/customEndpoints"; +import { isLoopbackUrl, type CustomEndpoint } from "../providers/customEndpoints"; import { providerDescriptors } from "../providers/providerRegistry"; import { thinkingLevelsFor } from "../providers/thinkingLevels"; import { backActionId, useBackAction } from "./actionStack"; @@ -665,20 +660,23 @@ export function App({ controller, onActiveChatChange, inbox }: Props) {
- {connected === false && ( -
- -
- Connect to {providerName} - - {provider?.subscriptionLabel ? "Sign in" : "Add an API key"} to start chatting. - + {connected === false && + !state.settings.customEndpoints.some( + (endpoint) => endpoint.id === state.settings.providerId, + ) && ( +
+ +
+ Connect to {providerName} + + {provider?.subscriptionLabel ? "Sign in" : "Add an API key"} to start chatting. + +
+
- -
- )} + )} {state.questionnaire && ( -

{endpointView.endpoint ? "Edit endpoint" : "Local endpoint"}

+

{endpointView.endpoint ? "Edit endpoint" : "Custom endpoint"}

{ setProviderId(saved.id); setEndpointView(null); - onToast(`${saved.name} saved`); }} onRemoved={() => { setEndpointView(null); @@ -1209,10 +1212,14 @@ function SettingsSheet({ {!signingIn && (

{connected === undefined - ? "Checking connection…" - : connected - ? `${provider.name} is connected` - : `Connect to ${provider.name}`} + ? "Checking credentials…" + : selectedEndpoint + ? connected + ? "API key saved" + : "No API key configured" + : connected + ? `${provider.name} is connected` + : `Connect to ${provider.name}`}

)} {providerId === "chatgpt" && !signingIn && (chatgptAccount || connected) && ( @@ -1228,7 +1235,7 @@ function SettingsSheet({
)} {provider.apiKey && - connected === false && + (connected === false || Boolean(selectedEndpoint)) && !signingIn && (!provider.subscriptionLabel || showKey) && ( <> @@ -1382,7 +1389,13 @@ function SettingsSheet({ aria-live="polite" onClick={() => void changeCredential("disconnect")} > - {disconnecting ? "Disconnecting…" : "Disconnect"} + {selectedEndpoint + ? disconnecting + ? "Removing…" + : "Remove API key" + : disconnecting + ? "Disconnecting…" + : "Disconnect"} )} {providerId === "chatgpt" && ( @@ -1899,7 +1912,7 @@ function ConfigSheet({ -

{editing && editing !== "new" ? "Edit endpoint" : "Local endpoint"}

+

{editing && editing !== "new" ? "Edit endpoint" : "Custom endpoint"}

{ setBusy("save"); setError(""); @@ -2158,7 +2170,14 @@ function EndpointForm({ }, apiKey, ) - .then(onSaved) + .then(({ endpoint: saved, removedApiKey }) => { + onSaved(saved); + showToast( + removedApiKey + ? `${saved.name} saved. Previous API key removed because the base URL changed.` + : `${saved.name} saved`, + ); + }) .catch((caught) => { setError(caught instanceof Error ? caught.message : String(caught)); }) @@ -2167,7 +2186,8 @@ function EndpointForm({ const probe = () => { setBusy("probe"); setError(""); - void probeOpenAIModels(baseUrl, apiKey) + void controller + .probeCustomEndpointModels(baseUrl, apiKey, endpoint?.id) .then((ids) => { setModels((current) => [ @@ -2227,7 +2247,7 @@ function EndpointForm({ />

- Point at the OpenAI-compatible `/v1` URL on your computer. Replace `192.168.1.10` with that + Enter your provider's OpenAI-compatible base URL. For a server on your computer, use that machine's LAN IP.

{loopback && ( @@ -2236,18 +2256,24 @@ function EndpointForm({

)} + {endpoint && ( +

+ Changing the base URL clears the saved key unless you enter a replacement. Remove a saved + key in Provider access to use this endpoint without authentication. +

+ )}