-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwoker.js
107 lines (93 loc) · 2.84 KB
/
woker.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const API_BASE = 'https://api.jarvis.cx';
const TOKEN_REFRESH_INTERVAL = 25 * 60 * 1000; // 25分钟,单位毫秒
export default {
async fetch(request, env) {
if (request.method === 'OPTIONS') {
return handleCORS();
}
try {
const { refreshToken, message } = await request.json();
if (!refreshToken) {
return new Response(JSON.stringify({
error: 'refreshToken is required'
}), {
status: 400,
headers: corsHeaders()
});
}
// 从KV中获取缓存的token信息
let tokenInfo = await env.TOKEN_STORE.get(refreshToken, { type: 'json' });
const now = Date.now();
// 如果没有token或者token已经接近过期(25分钟),则刷新
if (!tokenInfo || (now - tokenInfo.timestamp) >= TOKEN_REFRESH_INTERVAL) {
// 获取新的access token
const tokenResponse = await fetch(`${API_BASE}/api/v1/auth/refresh?refreshToken=${refreshToken}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
});
if (!tokenResponse.ok) {
throw new Error('Failed to refresh token');
}
const tokenData = await tokenResponse.json();
// 更新token信息到KV存储
tokenInfo = {
accessToken: tokenData.token.accessToken,
timestamp: now
};
// 存储token信息,过期时间设置为30分钟
await env.TOKEN_STORE.put(
refreshToken,
JSON.stringify(tokenInfo),
{ expirationTtl: 30 * 60 }
);
}
// 使用access token发送聊天请求
const chatResponse = await fetch(`${API_BASE}/api/v1/ai-chat/messages`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${tokenInfo.accessToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
content: message || 'hello',
metadata: {
conversation: {
messages: []
}
},
assistant: {
id: "gemini-1.5-pro-latest",
model: "dify",
name: "Gemini 1.5 Pro"
}
})
});
const chatData = await chatResponse.json();
return new Response(JSON.stringify(chatData), {
headers: corsHeaders()
});
} catch (error) {
return new Response(JSON.stringify({
error: error.message
}), {
status: 500,
headers: corsHeaders()
});
}
}
};
function corsHeaders() {
return {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
'Content-Type': 'application/json'
};
}
function handleCORS() {
return new Response(null, {
headers: corsHeaders()
});
}