From ce2f7d5bafd3475acf5c9d0a697f0d249781b7a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Troed=20S=C3=A5ngberg?= Date: Fri, 21 Aug 2026 13:38:27 +0200 Subject: [PATCH] feat: narrow polled servers to the current session's provider --- src/stats.ts | 25 +++++++++++++++++++++ test-backoff.ts | 58 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/src/stats.ts b/src/stats.ts index 0dee631..25620ff 100644 --- a/src/stats.ts +++ b/src/stats.ts @@ -276,3 +276,28 @@ export function resolveServerUrls(options: unknown, providers: unknown, config: const legacy = extractProviderUrls(config) return legacy.length > 0 ? legacy : ["http://localhost:8080"] } + +export function selectSessionUrls( + candidates: string[], + providers: unknown, + providerID: string | undefined, +): string[] { + if (!providerID || providerID.trim() === "" || candidates.length === 0) return candidates + if (!Array.isArray(providers)) return candidates + for (const entry of providers) { + if (!entry || typeof entry !== "object") continue + const provider = entry as Record + if (provider.id !== providerID) continue + const opts = provider.options + if (opts && typeof opts === "object") { + const baseURL = + (opts as Record).baseURL || (opts as Record).base_url + if (typeof baseURL === "string") { + const normalized = stripBaseUrlPath(baseURL) + if (candidates.includes(normalized)) return [normalized] + } + } + break + } + return candidates +} diff --git a/test-backoff.ts b/test-backoff.ts index ea26da1..95860ac 100644 --- a/test-backoff.ts +++ b/test-backoff.ts @@ -1,6 +1,6 @@ import { test } from "node:test" import assert from "node:assert/strict" -import { backoffDelayMs, resolveServerUrls } from "./src/stats.ts" +import { backoffDelayMs, resolveServerUrls, selectSessionUrls } from "./src/stats.ts" test("first failure retries after the base delay", () => { assert.equal(backoffDelayMs(1), 1000) @@ -151,3 +151,59 @@ test("ollama in id excludes even when name mentions llama", () => { const mixed = [{ id: "ollama-bridge", name: "My llama server", options: { baseURL: "http://localhost:11434" } }] assert.deepEqual(resolveServerUrls(undefined, mixed, {}), ["http://localhost:8080"]) }) + +test("selectSessionUrls narrows to the matching provider's baseURL", () => { + const candidates = ["http://a:1", "http://b:2"] + const providers = [ + { id: "vision", name: "Vision", options: { baseURL: "http://c:3" } }, + { id: "llamacpp-a", name: "Llama A", options: { baseURL: "http://b:2/v1" } }, + ] + assert.deepEqual(selectSessionUrls(candidates, providers, "llamacpp-a"), ["http://b:2"]) +}) + +test("selectSessionUrls matches base_url variant", () => { + const candidates = ["http://localhost:9090"] + const providers = [{ id: "llamacpp-b", name: "L", options: { base_url: "http://localhost:9090" } }] + assert.deepEqual(selectSessionUrls(candidates, providers, "llamacpp-b"), ["http://localhost:9090"]) +}) + +test("selectSessionUrls normalizes /v1 before comparing", () => { + const candidates = ["http://x:9"] + const providers = [{ id: "p", name: "P", options: { baseURL: "http://x:9/v1" } }] + assert.deepEqual(selectSessionUrls(candidates, providers, "p"), ["http://x:9"]) +}) + +test("selectSessionUrls returns candidates unchanged when provider not found", () => { + const candidates = ["http://a:1"] + assert.deepEqual(selectSessionUrls(candidates, [], "nope"), ["http://a:1"]) +}) + +test("selectSessionUrls returns candidates unchanged when matched provider URL is not a candidate", () => { + const candidates = ["http://a:1"] + const providers = [{ id: "ollama", name: "Ollama", options: { baseURL: "http://d:4" } }] + assert.deepEqual(selectSessionUrls(candidates, providers, "ollama"), ["http://a:1"]) +}) + +test("selectSessionUrls returns candidates unchanged for missing or empty providerID", () => { + assert.deepEqual(selectSessionUrls(["http://a:1"], [], undefined), ["http://a:1"]) + assert.deepEqual(selectSessionUrls(["http://a:1"], [], ""), ["http://a:1"]) +}) + +test("selectSessionUrls returns empty candidates unchanged", () => { + assert.deepEqual(selectSessionUrls([], [], "p"), []) +}) + +test("selectSessionUrls tolerates non-array and malformed providers", () => { + const c = ["http://a:1"] + assert.deepEqual(selectSessionUrls(c, undefined, "p"), c) + assert.deepEqual(selectSessionUrls(c, [null, 42, {}, { id: "p" }], "p"), c) +}) + +test("selectSessionUrls first match wins for duplicate ids", () => { + const candidates = ["http://a:1", "http://b:2"] + const providers = [ + { id: "dup", name: "first", options: { baseURL: "http://b:2/v1" } }, + { id: "dup", name: "second", options: { baseURL: "http://a:1" } }, + ] + assert.deepEqual(selectSessionUrls(candidates, providers, "dup"), ["http://b:2"]) +})