Spaces:
Sleeping
Sleeping
Create handles.js
Browse files- handles.js +98 -0
handles.js
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* 获取时间戳
|
3 |
+
* @returns {string} - timestamp
|
4 |
+
*/
|
5 |
+
const handleTimeStap = () => {
|
6 |
+
return new Date().getTime();
|
7 |
+
};
|
8 |
+
|
9 |
+
/**
|
10 |
+
* 生成随机字符串
|
11 |
+
* @returns {string} - nonce
|
12 |
+
*
|
13 |
+
*/
|
14 |
+
const handleBase64 = () => {
|
15 |
+
const nonce = btoa(
|
16 |
+
new Uint8Array(21).reduce(
|
17 |
+
(data) => data + String.fromCharCode(Math.floor(Math.random() * 256)),
|
18 |
+
""
|
19 |
+
)
|
20 |
+
);
|
21 |
+
return nonce.substring(0, 21);
|
22 |
+
};
|
23 |
+
|
24 |
+
/**
|
25 |
+
* 全局异常处理
|
26 |
+
* @param {Koa.ParameterizedContext} ctx
|
27 |
+
* @param {Koa.Next} next
|
28 |
+
*/
|
29 |
+
const handleErrors = async (ctx, next) => {
|
30 |
+
try {
|
31 |
+
await next();
|
32 |
+
} catch (err) {
|
33 |
+
console.error("Error:", err.message);
|
34 |
+
ctx.status
|
35 |
+
? (ctx.body = `${ctx.status} - ${ctx.message}`)
|
36 |
+
: (ctx.body = "500 - Internal Server Error");
|
37 |
+
}
|
38 |
+
};
|
39 |
+
|
40 |
+
/**
|
41 |
+
* 清除上下文消息
|
42 |
+
* @param {Object} params
|
43 |
+
* @param {String} sessionid
|
44 |
+
*/
|
45 |
+
const handleNewSession = async (params, sessionid) => {
|
46 |
+
await fetch("https://www.coze.com/api/conversation/break_message", {
|
47 |
+
method: "POST",
|
48 |
+
headers: {
|
49 |
+
"Content-Type": "application/json",
|
50 |
+
Cookie: `sessionid=${sessionid}`,
|
51 |
+
"User-Agent":
|
52 |
+
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
|
53 |
+
},
|
54 |
+
body: JSON.stringify(params),
|
55 |
+
credentials: "include",
|
56 |
+
});
|
57 |
+
await fetch("https://www.coze.com/api/conversation/create_section", {
|
58 |
+
method: "POST",
|
59 |
+
headers: {
|
60 |
+
"Content-Type": "application/json",
|
61 |
+
Cookie: `sessionid=${sessionid}`,
|
62 |
+
"User-Agent":
|
63 |
+
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
|
64 |
+
},
|
65 |
+
body: JSON.stringify({
|
66 |
+
conversation_id: params.conversation_id,
|
67 |
+
scene: params.scene,
|
68 |
+
}),
|
69 |
+
credentials: "include",
|
70 |
+
});
|
71 |
+
};
|
72 |
+
|
73 |
+
/**
|
74 |
+
* 初始化请求参数
|
75 |
+
* @param {Object} params
|
76 |
+
*/
|
77 |
+
const initParams = async (params) => {
|
78 |
+
params.bot_id = "7337496580645339154";
|
79 |
+
params.conversation_id =
|
80 |
+
"7337495393033765890_7337496580645339154_2_" + handleTimeStap();
|
81 |
+
params.local_message_id = handleBase64();
|
82 |
+
params.query = "";
|
83 |
+
params.bot_version = "1709039055567";
|
84 |
+
params.chat_history = [];
|
85 |
+
params.insert_history_message_list = [];
|
86 |
+
params.stream = true;
|
87 |
+
params.scene = 2;
|
88 |
+
params.content_type = "text";
|
89 |
+
params.extra = {};
|
90 |
+
};
|
91 |
+
|
92 |
+
export {
|
93 |
+
handleTimeStap,
|
94 |
+
handleBase64,
|
95 |
+
handleNewSession,
|
96 |
+
handleErrors,
|
97 |
+
initParams,
|
98 |
+
};
|