feat: narrow polled servers to the current session's provider

This commit is contained in:
Troed Sångberg
2026-08-21 13:38:27 +02:00
parent cad446da2b
commit ce2f7d5baf
2 changed files with 82 additions and 1 deletions
+25
View File
@@ -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<string, unknown>
if (provider.id !== providerID) continue
const opts = provider.options
if (opts && typeof opts === "object") {
const baseURL =
(opts as Record<string, unknown>).baseURL || (opts as Record<string, unknown>).base_url
if (typeof baseURL === "string") {
const normalized = stripBaseUrlPath(baseURL)
if (candidates.includes(normalized)) return [normalized]
}
}
break
}
return candidates
}
+57 -1
View File
@@ -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"])
})