-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathworker.js
57 lines (50 loc) · 1.84 KB
/
worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
export default {
async fetch(request, env) {
const allowHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST, OPTIONS",
"Access-Control-Allow-Headers": "Authorization, Content-Type",
};
if (request.method === "OPTIONS") {
return new Response(null, { status: 204, headers: allowHeaders });
}
if (request.method !== "POST") {
return new Response("Method Not Allowed", { status: 405, headers: allowHeaders });
}
try {
const url = env.API_URL || "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions"; // 允许环境变量覆盖默认 API
const headers = {
"Authorization": request.headers.get("Authorization"),
"Content-Type": "application/json",
};
let body = await request.text();
let parsedBody = JSON.parse(body);
parsedBody.stream = true; // 启用流式传输
const response = await fetch(url, {
method: "POST",
headers,
body: JSON.stringify(parsedBody),
});
if (!response.ok) {
return new Response(JSON.stringify({ error: "请求失败", status: response.status }), {
status: response.status,
headers: { ...allowHeaders, "Content-Type": "application/json" },
});
}
return new Response(response.body, {
status: 200,
headers: {
...allowHeaders,
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
},
});
} catch (error) {
return new Response(JSON.stringify({ error: "服务器内部错误", details: error.toString() }), {
status: 500,
headers: { ...allowHeaders, "Content-Type": "application/json" },
});
}
},
};