Files
claude-code-router/packages/core/src/utils/request.ts
2025-12-28 22:42:07 +08:00

58 lines
1.5 KiB
TypeScript

import { ProxyAgent } from "undici";
import { UnifiedChatRequest } from "../types/llm";
export function sendUnifiedRequest(
url: URL | string,
request: UnifiedChatRequest,
config: any,
context: any,
logger?: any
): Promise<Response> {
const headers = new Headers({
"Content-Type": "application/json",
});
if (config.headers) {
Object.entries(config.headers).forEach(([key, value]) => {
if (value) {
headers.set(key, value as string);
}
});
}
let combinedSignal: AbortSignal;
const timeoutSignal = AbortSignal.timeout(config.TIMEOUT ?? 60 * 1000 * 60);
if (config.signal) {
const controller = new AbortController();
const abortHandler = () => controller.abort();
config.signal.addEventListener("abort", abortHandler);
timeoutSignal.addEventListener("abort", abortHandler);
combinedSignal = controller.signal;
} else {
combinedSignal = timeoutSignal;
}
const fetchOptions: RequestInit = {
method: "POST",
headers: headers,
body: JSON.stringify(request),
signal: combinedSignal,
};
if (config.httpsProxy) {
(fetchOptions as any).dispatcher = new ProxyAgent(
new URL(config.httpsProxy).toString()
);
}
logger?.debug(
{
reqId: context.req.id,
request: fetchOptions,
headers: Object.fromEntries(headers.entries()),
requestUrl: typeof url === "string" ? url : url.toString(),
useProxy: config.httpsProxy,
},
"final request"
);
return fetch(typeof url === "string" ? url : url.toString(), fetchOptions);
}