forked from XiaoMo/ChatGPT-Next-Web
Merge pull request #557 from Yidadaa/Yidadaa-patch-1
Update requests.ts
This commit is contained in:
commit
f7624f29b1
@ -6,6 +6,7 @@ async function makeRequest(req: NextRequest) {
|
|||||||
const api = await requestOpenai(req);
|
const api = await requestOpenai(req);
|
||||||
const res = new NextResponse(api.body);
|
const res = new NextResponse(api.body);
|
||||||
res.headers.set("Content-Type", "application/json");
|
res.headers.set("Content-Type", "application/json");
|
||||||
|
res.headers.set("Cache-Control", "no-cache");
|
||||||
return res;
|
return res;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("[OpenAI] ", req.body, e);
|
console.error("[OpenAI] ", req.body, e);
|
||||||
@ -16,7 +17,7 @@ async function makeRequest(req: NextRequest) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
status: 500,
|
status: 500,
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ const makeRequestParam = (
|
|||||||
options?: {
|
options?: {
|
||||||
filterBot?: boolean;
|
filterBot?: boolean;
|
||||||
stream?: boolean;
|
stream?: boolean;
|
||||||
},
|
}
|
||||||
): ChatRequest => {
|
): ChatRequest => {
|
||||||
let sendMessages = messages.map((v) => ({
|
let sendMessages = messages.map((v) => ({
|
||||||
role: v.role,
|
role: v.role,
|
||||||
@ -46,11 +46,10 @@ function getHeaders() {
|
|||||||
|
|
||||||
export function requestOpenaiClient(path: string) {
|
export function requestOpenaiClient(path: string) {
|
||||||
return (body: any, method = "POST") =>
|
return (body: any, method = "POST") =>
|
||||||
fetch("/api/openai", {
|
fetch("/api/openai?_vercel_no_cache=1", {
|
||||||
method,
|
method,
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"Cache-Control": "no-cache",
|
|
||||||
path,
|
path,
|
||||||
...getHeaders(),
|
...getHeaders(),
|
||||||
},
|
},
|
||||||
@ -77,7 +76,7 @@ export async function requestUsage() {
|
|||||||
.getDate()
|
.getDate()
|
||||||
.toString()
|
.toString()
|
||||||
.padStart(2, "0")}`;
|
.padStart(2, "0")}`;
|
||||||
const ONE_DAY = 24 * 60 * 60 * 1000;
|
const ONE_DAY = 2 * 24 * 60 * 60 * 1000;
|
||||||
const now = new Date(Date.now() + ONE_DAY);
|
const now = new Date(Date.now() + ONE_DAY);
|
||||||
const startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);
|
const startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);
|
||||||
const startDate = formatDate(startOfMonth);
|
const startDate = formatDate(startOfMonth);
|
||||||
@ -85,7 +84,7 @@ export async function requestUsage() {
|
|||||||
|
|
||||||
const [used, subs] = await Promise.all([
|
const [used, subs] = await Promise.all([
|
||||||
requestOpenaiClient(
|
requestOpenaiClient(
|
||||||
`dashboard/billing/usage?start_date=${startDate}&end_date=${endDate}`,
|
`dashboard/billing/usage?start_date=${startDate}&end_date=${endDate}`
|
||||||
)(null, "GET"),
|
)(null, "GET"),
|
||||||
requestOpenaiClient("dashboard/billing/subscription")(null, "GET"),
|
requestOpenaiClient("dashboard/billing/subscription")(null, "GET"),
|
||||||
]);
|
]);
|
||||||
@ -125,7 +124,7 @@ export async function requestChatStream(
|
|||||||
onMessage: (message: string, done: boolean) => void;
|
onMessage: (message: string, done: boolean) => void;
|
||||||
onError: (error: Error, statusCode?: number) => void;
|
onError: (error: Error, statusCode?: number) => void;
|
||||||
onController?: (controller: AbortController) => void;
|
onController?: (controller: AbortController) => void;
|
||||||
},
|
}
|
||||||
) {
|
) {
|
||||||
const req = makeRequestParam(messages, {
|
const req = makeRequestParam(messages, {
|
||||||
stream: true,
|
stream: true,
|
||||||
@ -214,7 +213,7 @@ export const ControllerPool = {
|
|||||||
addController(
|
addController(
|
||||||
sessionIndex: number,
|
sessionIndex: number,
|
||||||
messageId: number,
|
messageId: number,
|
||||||
controller: AbortController,
|
controller: AbortController
|
||||||
) {
|
) {
|
||||||
const key = this.key(sessionIndex, messageId);
|
const key = this.key(sessionIndex, messageId);
|
||||||
this.controllers[key] = controller;
|
this.controllers[key] = controller;
|
||||||
|
Loading…
Reference in New Issue
Block a user