fix(zai): cap endpoint probe timeouts

This commit is contained in:
Peter Steinberger
2026-05-29 16:28:26 -04:00
parent 57aec8c565
commit 26ea53cc68
2 changed files with 27 additions and 2 deletions

View File

@@ -1,6 +1,8 @@
import { describe, expect, it } from "vitest";
import { afterEach, describe, expect, it, vi } from "vitest";
import { detectZaiEndpoint } from "./detect.js";
const MAX_TIMER_TIMEOUT_MS = 2_147_000_000;
type FetchResponse = { status: number; body?: unknown };
function makeFetch(map: Record<string, FetchResponse>) {
@@ -19,6 +21,10 @@ function makeFetch(map: Record<string, FetchResponse>) {
}
describe("detectZaiEndpoint", () => {
afterEach(() => {
vi.restoreAllMocks();
});
it("resolves preferred/fallback endpoints and null when probes fail", async () => {
const scenarios: Array<{
endpoint?: "global" | "cn" | "coding-global" | "coding-cn";
@@ -114,4 +120,22 @@ describe("detectZaiEndpoint", () => {
}
}
});
it("caps oversized probe timeouts before scheduling", async () => {
const timeoutSpy = vi
.spyOn(globalThis, "setTimeout")
.mockImplementation((() => 1) as typeof setTimeout);
vi.spyOn(globalThis, "clearTimeout").mockImplementation(() => undefined);
const fetchFn = makeFetch({
"https://api.z.ai/api/paas/v4/chat/completions::glm-5.1": { status: 200 },
});
await detectZaiEndpoint({
apiKey: "sk-test", // pragma: allowlist secret
fetchFn,
timeoutMs: MAX_TIMER_TIMEOUT_MS + 1_000_000,
});
expect(timeoutSpy).toHaveBeenCalledWith(expect.any(Function), MAX_TIMER_TIMEOUT_MS);
});
});

View File

@@ -1,3 +1,4 @@
import { resolveTimerTimeoutMs } from "openclaw/plugin-sdk/number-runtime";
import {
ZAI_CN_BASE_URL,
ZAI_CODING_CN_BASE_URL,
@@ -113,7 +114,7 @@ export async function detectZaiEndpoint(params: {
return null;
}
const timeoutMs = params.timeoutMs ?? 5_000;
const timeoutMs = resolveTimerTimeoutMs(params.timeoutMs, 5_000);
const probeCandidates = (() => {
const general = [
{