Skip to content

Build your personal knowledge base with Trilium Notes

fix(llm): don't let a blocked native probe path fail provider setup

Elian Doran37177242d20d
diff --git a/packages/trilium-core/src/services/llm/providers/local.spec.ts b/packages/trilium-core/src/services/llm/providers/local.spec.ts
index 1167ab0e19..059de724bf 100644
--- a/packages/trilium-core/src/services/llm/providers/local.spec.ts
+++ b/packages/trilium-core/src/services/llm/providers/local.spec.ts
@@ -241,14 +241,32 @@ describe("LocalProvider", () => {
);
});
- it("reports a rejected credential instead of probing on", async () => {
- fetchMock.mockImplementation(routes({ "/api/tags": status(401) }));
+ it("reports a rejected credential from the endpoint it depends on", async () => {
+ // A key the endpoint really rejects is refused by /v1/models too, so
+ // tolerating the native probes costs nothing: the verdict still comes,
+ // it just comes from the path that decides it.
+ fetchMock.mockImplementation(routes({ "/api/tags": status(401), "/v1/models": status(401) }));
await expect(new LocalProvider("ollama").listModels()).rejects.toThrow(/Authentication failed \(HTTP 401\)/);
});
- it("reports a server error instead of probing on", async () => {
- fetchMock.mockImplementation(routes({ "/api/tags": status(500) }));
- await expect(new LocalProvider("ollama").listModels()).rejects.toThrow(/HTTP 500/);
+ it("reports a server error from the endpoint it depends on", async () => {
+ fetchMock.mockImplementation(routes({ "/v1/models": status(500) }));
+ await expect(new LocalProvider("openai-compatible", "", "http://box:8080").listModels())
+ .rejects.toThrow(/HTTP 500/);
+ });
+
+ it("advances past a gateway that rejects the native probe paths", async () => {
+ // A CDN or WAF in front of a hosted OpenAI-compatible API blocks the
+ // paths outside /v1 outright — apihub.agnes-ai.com answers /api/tags
+ // with a 403 HTML block page. That is a verdict on the path, not on
+ // the credential, so the chain must still reach /v1/models. (#10996)
+ fetchMock.mockImplementation(routes({
+ "/api/tags": status(403),
+ "/api/v0/models": status(403),
+ "/v1/models": openAiModels(["gpt-4o"])
+ }));
+ const models = await new LocalProvider("openai-compatible", "sk-key", "https://apihub.example.com/v1").listModels();
+ expect(models.map(m => m.id)).toEqual(["gpt-4o"]);
});
it("reports an endpoint that serves no listing at all", async () => {
diff --git a/packages/trilium-core/src/services/llm/providers/local.ts b/packages/trilium-core/src/services/llm/providers/local.ts
index b17f756938..e6eaf7e299 100644
--- a/packages/trilium-core/src/services/llm/providers/local.ts
+++ b/packages/trilium-core/src/services/llm/providers/local.ts
@@ -141,7 +141,7 @@ export class LocalProvider extends BaseProvider {
*/
private async listNativeModels(): Promise<LocalModel[] | null> {
if (this.kind !== "lmstudio") {
- const payload = await this.probeJson(`${this.root}/api/tags`);
+ const payload = await this.probeJson(`${this.root}/api/tags`, { optional: true });
const models = payload !== undefined ? parseOllamaTags(payload) : null;
if (models) {
this.isLocalRuntime = true;
@@ -155,7 +155,7 @@ export class LocalProvider extends BaseProvider {
}
}
if (this.kind !== "ollama") {
- const payload = await this.probeJson(`${this.root}/api/v0/models`);
+ const payload = await this.probeJson(`${this.root}/api/v0/models`, { optional: true });
const models = payload !== undefined ? parseLmStudioModels(payload) : null;
if (models) {
this.isLocalRuntime = true;
@@ -192,8 +192,18 @@ export class LocalProvider extends BaseProvider {
* else throws: an unreachable host or a rejected credential is a real
* problem the add/edit-provider screen must report, not a reason to keep
* trying other URLs.
+ *
+ * `optional` marks a probe that exists only to enrich the listing — the two
+ * native endpoints. Those paths sit outside the OpenAI-compatible namespace,
+ * so anything fronting the endpoint (a CDN's WAF, an API gateway, a
+ * forward-auth proxy) routinely rejects them while `/v1/*` works perfectly:
+ * a 403 HTML block page on `/api/tags` says nothing about the credential.
+ * Any error status therefore just advances the chain, leaving only
+ * `/v1/models` — the endpoint the provider actually depends on — able to
+ * fail the operation. A genuinely rejected key still surfaces, because
+ * `/v1/models` answers it with the same status.
*/
- private async probeJson(url: string): Promise<unknown | undefined> {
+ private async probeJson(url: string, { optional = false } = {}): Promise<unknown | undefined> {
let response: Response;
try {
response = await llmFetch(url, {
@@ -201,12 +211,15 @@ export class LocalProvider extends BaseProvider {
signal: AbortSignal.timeout(MODEL_LIST_TIMEOUT_MS)
});
} catch (e) {
+ // Unlike a status, a transport failure is a fact about the host, not
+ // the path: the later probes go to that same host and would only
+ // burn another timeout each to reach the same verdict.
throw new Error(`Could not reach ${url}: ${e instanceof Error ? e.message : String(e)}`);
}
- if (response.status === 404 || response.status === 405) {
- return undefined;
- }
if (!response.ok) {
+ if (optional || response.status === 404 || response.status === 405) {
+ return undefined;
+ }
if (response.status === 401 || response.status === 403) {
throw new Error(`Authentication failed (HTTP ${response.status}) — check the API key and base URL.`);
}