mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-07 18:42:25 +00:00
fix(agents): cancel OpenRouter catalog error bodies
This commit is contained in:
@@ -105,6 +105,21 @@ describe("openrouter-model-capabilities", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("cancels failed OpenRouter catalog response bodies", async () => {
|
||||
await withOpenRouterStateDir(async () => {
|
||||
const response = new Response("temporarily unavailable", { status: 503 });
|
||||
const cancel = vi.spyOn(response.body!, "cancel").mockResolvedValue(undefined);
|
||||
const fetchSpy = vi.fn(async () => response);
|
||||
vi.stubGlobal("fetch", fetchSpy);
|
||||
|
||||
const module = await importOpenRouterModelCapabilities("failed-catalog-response");
|
||||
await module.loadOpenRouterModelCapabilities("acme/missing-model");
|
||||
|
||||
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
||||
expect(cancel).toHaveBeenCalledOnce();
|
||||
});
|
||||
});
|
||||
|
||||
it("uses endpoint-specific OpenRouter context length when top_provider reports one", async () => {
|
||||
await withOpenRouterStateDir(async () => {
|
||||
vi.stubGlobal(
|
||||
|
||||
@@ -172,6 +172,12 @@ function parseModel(model: OpenRouterApiModel): OpenRouterModelCapabilities {
|
||||
};
|
||||
}
|
||||
|
||||
async function cancelUnreadResponseBody(response: Response | undefined): Promise<void> {
|
||||
if (response && !response.bodyUsed) {
|
||||
await response.body?.cancel().catch(() => undefined);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// API fetch
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -179,10 +185,11 @@ function parseModel(model: OpenRouterApiModel): OpenRouterModelCapabilities {
|
||||
async function doFetch(): Promise<void> {
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS);
|
||||
let response: Response | undefined;
|
||||
try {
|
||||
const fetchFn = resolveProxyFetchFromEnv() ?? globalThis.fetch;
|
||||
|
||||
const response = await fetchFn(OPENROUTER_MODELS_URL, {
|
||||
response = await fetchFn(OPENROUTER_MODELS_URL, {
|
||||
signal: controller.signal,
|
||||
});
|
||||
|
||||
@@ -210,6 +217,7 @@ async function doFetch(): Promise<void> {
|
||||
log.warn(`Failed to fetch OpenRouter models: ${message}`);
|
||||
} finally {
|
||||
clearTimeout(timeout);
|
||||
await cancelUnreadResponseBody(response);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user