fix: restore check after upstream type drift

This commit is contained in:
Peter Steinberger
2026-03-16 18:43:00 -07:00
parent 683be73d54
commit 5a763ac57b
5 changed files with 17 additions and 10 deletions

View File

@@ -22,6 +22,7 @@ type OnboardEnv = {
configPath: string;
runtime: NonInteractiveRuntime;
};
type FetchLike = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
const ensureWorkspaceAndSessionsMock = vi.hoisted(() => vi.fn(async (..._args: unknown[]) => {}));
@@ -61,7 +62,7 @@ type ProviderAuthConfigSnapshot = {
};
};
function createZaiFetchMock(responses: Record<string, number>): typeof fetch {
function createZaiFetchMock(responses: Record<string, number>): FetchLike {
return vi.fn(async (input, init) => {
const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : "";
const parsedBody =
@@ -77,12 +78,12 @@ function createZaiFetchMock(responses: Record<string, number>): typeof fetch {
headers: { "content-type": "application/json" },
},
);
}) as typeof fetch;
});
}
async function withZaiProbeFetch<T>(
responses: Record<string, number>,
run: (fetchMock: typeof fetch) => Promise<T>,
run: (fetchMock: FetchLike) => Promise<T>,
): Promise<T> {
const originalVitest = process.env.VITEST;
delete process.env.VITEST;

View File

@@ -3,6 +3,7 @@ import { ProxyAgent } from "undici";
import { afterEach, describe, expect, it, vi } from "vitest";
const TEST_GAXIOS_CONSTRUCTOR_OVERRIDE = "__OPENCLAW_TEST_GAXIOS_CONSTRUCTOR__";
type FetchLike = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
describe("gaxios fetch compat", () => {
afterEach(() => {
@@ -14,14 +15,14 @@ describe("gaxios fetch compat", () => {
it("uses native fetch without defining window or importing node-fetch", async () => {
type MockRequestConfig = RequestInit & {
fetchImplementation?: typeof fetch;
fetchImplementation?: FetchLike;
responseType?: string;
url: string;
};
let MockGaxiosCtor!: new () => {
request(config: MockRequestConfig): Promise<{ data: string } & object>;
};
const fetchMock = vi.fn<typeof fetch>(async () => {
const fetchMock = vi.fn<FetchLike>(async () => {
return new Response("ok", {
headers: { "content-type": "text/plain" },
status: 200,
@@ -64,14 +65,14 @@ describe("gaxios fetch compat", () => {
it("falls back to a legacy window fetch shim when gaxios is unavailable", async () => {
const originalWindowDescriptor = Object.getOwnPropertyDescriptor(globalThis, "window");
vi.stubGlobal("fetch", vi.fn<typeof fetch>());
vi.stubGlobal("fetch", vi.fn<FetchLike>());
Reflect.deleteProperty(globalThis as object, "window");
(globalThis as Record<string, unknown>)[TEST_GAXIOS_CONSTRUCTOR_OVERRIDE] = null;
const { installGaxiosFetchCompat } = await import("./gaxios-fetch-compat.js");
try {
await expect(installGaxiosFetchCompat()).resolves.toBeUndefined();
expect((globalThis as { window?: { fetch?: typeof fetch } }).window?.fetch).toBe(fetch);
expect((globalThis as { window?: { fetch?: FetchLike } }).window?.fetch).toBe(fetch);
await expect(installGaxiosFetchCompat()).resolves.toBeUndefined();
} finally {
Reflect.deleteProperty(globalThis as object, "window");
@@ -82,7 +83,7 @@ describe("gaxios fetch compat", () => {
});
it("translates proxy agents into undici dispatchers for native fetch", async () => {
const fetchMock = vi.fn<typeof fetch>(async () => {
const fetchMock = vi.fn<FetchLike>(async () => {
return new Response("ok", {
headers: { "content-type": "text/plain" },
status: 200,

View File

@@ -7,12 +7,13 @@ import { Agent as UndiciAgent, ProxyAgent } from "undici";
type ProxyRule = RegExp | URL | string;
type TlsCert = ConnectionOptions["cert"];
type TlsKey = ConnectionOptions["key"];
type FetchLike = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
type GaxiosFetchRequestInit = RequestInit & {
agent?: unknown;
cert?: TlsCert;
dispatcher?: Dispatcher;
fetchImplementation?: typeof fetch;
fetchImplementation?: FetchLike;
key?: TlsKey;
noProxy?: ProxyRule[];
proxy?: string | URL;
@@ -240,7 +241,9 @@ function installLegacyWindowFetchShim(): void {
(globalThis as Record<string, unknown>).window = { fetch: globalThis.fetch };
}
export function createGaxiosCompatFetch(baseFetch: typeof fetch = globalThis.fetch): typeof fetch {
export function createGaxiosCompatFetch(
baseFetch: FetchLike = globalThis.fetch.bind(globalThis),
): FetchLike {
return async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
const gaxiosInit = (init ?? {}) as GaxiosFetchRequestInit;
const requestUrl =

View File

@@ -1,4 +1,5 @@
export type { OpenClawConfig } from "../config/config.js";
export type { DmPolicy } from "../config/types.js";
export type { WizardPrompter } from "../wizard/prompts.js";
export type { ChannelSetupAdapter } from "../channels/plugins/types.adapters.js";
export type { ChannelSetupDmPolicy } from "../channels/plugins/setup-wizard-types.js";

View File

@@ -2,6 +2,7 @@ export type {
ChannelAccountSnapshot,
ChannelGatewayContext,
ChannelMessageActionAdapter,
ChannelPlugin,
} from "../channels/plugins/types.js";
export type { OpenClawConfig } from "../config/config.js";
export type { PluginRuntime } from "../plugins/runtime/types.js";