Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ yarn.lock
dist.zip
dist
plugin.zip
.env*
!.env.example
7 changes: 7 additions & 0 deletions changelogs.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
62 changes: 49 additions & 13 deletions src/app/agentController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { AgentSession, type RevertResult } from "../session/agentSession";
import { pickGlobalSkillsFolder } from "../session/workspaceResources";
import {
finalizeCustomEndpoint,
isLocalUrl,
normalizeOpenAIBaseUrl,
probeOpenAIModels,
upsertCustomEndpoint,
type CustomEndpoint,
Expand Down Expand Up @@ -554,11 +554,15 @@ export class AgentController {
return this.providers.descriptors();
}

async saveCustomEndpoint(draft: CustomEndpointDraft, apiKey?: string): Promise<CustomEndpoint> {
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);
Expand All @@ -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<string[]> {
const key = await this.#customEndpointApiKey(baseUrl, apiKey, endpointId);
return probeOpenAIModels(baseUrl, key);
}

async #customEndpointApiKey(
baseUrl: string,
apiKey?: string,
endpointId?: string,
): Promise<string | undefined> {
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<void> {
Expand Down Expand Up @@ -608,9 +644,11 @@ export class AgentController {
): Promise<string[]> {
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, {
Expand Down Expand Up @@ -857,12 +895,10 @@ export class AgentController {
}

async hasCredential(providerId: ProviderId): Promise<boolean> {
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,
);
}

Expand Down
27 changes: 3 additions & 24 deletions src/providers/customEndpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -285,7 +265,7 @@ export async function probeOpenAIModels(
if (!url) throw new Error("Enter a valid OpenAI-compatible base URL first.");
const headers: Record<string, string> = { 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();
Expand Down Expand Up @@ -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" };
},
};
}
Expand Down
20 changes: 18 additions & 2 deletions src/providers/portableOAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Comment thread
bajrangCoder marked this conversation as resolved.
Resolved
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 {
Expand Down
2 changes: 1 addition & 1 deletion src/providers/providerRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})),
];
Expand Down
7 changes: 6 additions & 1 deletion src/session/runFailure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -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),
};
}
Loading
Loading