Skip to content

Build your personal knowledge base with Trilium Notes

fix(llm): advance provider probe chain on non-JSON 2xx responses

raman325ad40278584e4
diff --git a/apps/server/src/services/llm/providers/local.spec.ts b/apps/server/src/services/llm/providers/local.spec.ts
index cf4f5a7af9..ae4f8d70e7 100644
--- a/apps/server/src/services/llm/providers/local.spec.ts
+++ b/apps/server/src/services/llm/providers/local.spec.ts
@@ -50,6 +50,17 @@ function status(code: number) {
return { ok: false, status: code } as Response;
}
+/** A 200 HTML page, as an auth proxy or captive portal serves for unknown paths. */
+function html() {
+ return {
+ ok: true,
+ status: 200,
+ json: async () => {
+ throw new SyntaxError(`Unexpected token '<', "<!doctype "... is not valid JSON`);
+ }
+ } as Response;
+}
+
/** Route each probe URL to a response, so the fallback chain can be driven precisely. */
function routes(map: Record<string, Response>) {
return (url: string) => {
@@ -282,6 +293,19 @@ describe("LocalProvider", () => {
expect(fetchMock.mock.calls.every(([url]) => !String(url).includes("/api/v0/models"))).toBe(true);
});
+ it("advances past an auth proxy's HTML login page on a native probe", async () => {
+ // A forward-auth proxy that exempts /v1/* but not /api/tags answers
+ // the probe with a 200 HTML login page; the chain must advance to
+ // /v1/models rather than choke on the non-JSON body.
+ fetchMock.mockImplementation(routes({
+ "/api/tags": html(),
+ "/api/v0/models": html(),
+ "/v1/models": openAiModels(["m1"])
+ }));
+ const models = await new LocalProvider("openai-compatible", "", "http://box:8080").listModels();
+ expect(models.map(m => m.id)).toEqual(["m1"]);
+ });
+
it("treats a foreign native payload as 'not that runtime' for the generic card", async () => {
// Something answers /api/tags with unrelated JSON; the generic card
// keeps probing rather than failing on a guess it made itself.
diff --git a/apps/server/src/services/llm/providers/local.ts b/apps/server/src/services/llm/providers/local.ts
index a23ad03593..a4544b2136 100644
--- a/apps/server/src/services/llm/providers/local.ts
+++ b/apps/server/src/services/llm/providers/local.ts
@@ -185,10 +185,11 @@ export class LocalProvider extends BaseProvider {
/**
* GET a JSON document from a listing endpoint.
*
- * Returns undefined when the endpoint isn't served here (404/405), which is
- * how the probe chain advances. Everything 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.
+ * Returns undefined when the endpoint isn't served here (404/405, or a 2xx
+ * whose body isn't JSON), which is how the probe chain advances. Everything
+ * 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.
*/
private async probeJson(url: string): Promise<unknown | undefined> {
let response: Response;
@@ -209,7 +210,14 @@ export class LocalProvider extends BaseProvider {
}
throw new Error(`HTTP ${response.status} from ${url}`);
}
- return await response.json();
+ try {
+ return await response.json();
+ } catch {
+ // An auth proxy or captive portal can answer a probe with a 200 HTML
+ // login page (after a redirect fetch follows transparently). A body
+ // that isn't JSON means the endpoint isn't served here.
+ return undefined;
+ }
}
/**