mirror of
https://github.com/musistudio/claude-code-router.git
synced 2026-02-18 14:40:49 +08:00
move llms to core package
This commit is contained in:
57
packages/core/src/utils/request.ts
Normal file
57
packages/core/src/utils/request.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user