-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathindex.js
284 lines (257 loc) · 8.25 KB
/
index.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
const express = require("express");
const FormData = require("form-data");
const { v4: uuidv4 } = require("uuid");
const app = express();
const axios = require("axios");
const port = 8080;
axios.defaults.headers.common["User-Agent"] =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36";
axios.defaults.headers.common["Cookie"] = process.env.YOUCOM_COOKIE;
app.post("/v1/messages", (req, res) => {
req.rawBody = "";
req.setEncoding("utf8");
req.on("data", function (chunk) {
req.rawBody += chunk;
});
req.on("end", async () => {
res.setHeader("Content-Type", "text/event-stream;charset=utf-8");
try {
let jsonBody = JSON.parse(req.rawBody);
if (jsonBody.stream == false) {
res.send(
JSON.stringify({
id: uuidv4(),
content: [
{
text: "Please turn on streaming.",
},
{
id: "string",
name: "string",
input: {},
},
],
model: "string",
stop_reason: "end_turn",
stop_sequence: "string",
usage: {
input_tokens: 0,
output_tokens: 0,
},
})
);
} else if (jsonBody.stream == true) {
// 计算用户消息长度
let userMessage = [{ question: "", answer: "" }];
let userQuery = "";
let lastUpdate = true;
if (jsonBody.system) {
// 把系统消息加入messages的首条
jsonBody.messages.unshift({ role: "system", content: jsonBody.system });
}
jsonBody.messages.forEach((msg) => {
if (msg.role == "system" || msg.role == "user") {
if (lastUpdate) {
userMessage[userMessage.length - 1].question += msg.content + "\n";
} else if (userMessage[userMessage.length - 1].question == "") {
userMessage[userMessage.length - 1].question += msg.content + "\n";
} else {
userMessage.push({ question: msg.content + "\n", answer: "" });
}
lastUpdate = true;
} else if (msg.role == "assistant") {
if (!lastUpdate) {
userMessage[userMessage.length - 1].answer += msg.content + "\n";
} else if (userMessage[userMessage.length - 1].answer == "") {
userMessage[userMessage.length - 1].answer += msg.content + "\n";
} else {
userMessage.push({ question: "", answer: msg.content + "\n" });
}
lastUpdate = false;
}
});
userQuery = userMessage[userMessage.length - 1].question;
// 获取traceId
/*let initSearch = await axios.get("https://you.com/_next/data/f3b05a38b379a189a1db48b6665e81f064fbf1b8/en-US/search.json", {
params: {
q: userQuery.trim(),
fromSearchBar: "true",
tbm: "youchat",
chatMode: "custom"
},
headers: {
"X-Nextjs-Data":"1"
},
}).then((res) => res.data);
if(!initSearch) throw new Error("Failed to init search");
var traceId = initSearch.pageProps.initialTraceId;*/
var traceId=uuidv4();
// 试算用户消息长度
if(encodeURIComponent(JSON.stringify(userMessage)).length > 30000) {
//太长了,需要上传
// user message to plaintext
let previousMessages = jsonBody.messages
.map((msg) => {
return msg.content
})
.join("\n\n");
userQuery = userMessage[userMessage.length - 1].question;
userMessage = [];
// GET https://you.com/api/get_nonce to get nonce
let nonce = await axios("https://you.com/api/get_nonce").then((res) => res.data);
if (!nonce) throw new Error("Failed to get nonce");
// POST https://you.com/api/upload to upload user message
const form_data = new FormData();
var messageBuffer = Buffer.from(previousMessages, "utf8");
form_data.append("file", messageBuffer, { filename: "messages.txt", contentType: "text/plain" });
var uploadedFile = await axios
.post("https://you.com/api/upload", form_data, {
headers: {
...form_data.getHeaders(),
"X-Upload-Nonce": nonce,
},
})
.then((res) => res.data.filename);
if (!uploadedFile) throw new Error("Failed to upload messages");
}
let msgid = uuidv4();
// send message start
res.write(
createEvent("message_start", {
type: "message_start",
message: {
id: `${traceId}`,
type: "message",
role: "assistant",
content: [],
model: "claude-3-opus-20240229",
stop_reason: null,
stop_sequence: null,
usage: { input_tokens: 8, output_tokens: 1 },
},
})
);
res.write(createEvent("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }));
res.write(createEvent("ping", { type: "ping" }));
// proxy response
var proxyReq = await axios
.get("https://you.com/api/streamingSearch", {
params: {
page: "1",
count: "10",
safeSearch: "Off",
q: userQuery.trim(),
incognito: "true",
chatId: traceId,
traceId: `${traceId}|${msgid}|${new Date().toISOString()}`,
conversationTurnId: msgid,
selectedAIModel: "claude_3_opus",
selectedChatMode: "custom",
pastChatLength: userMessage.length,
queryTraceId: traceId,
use_personalization_extraction: "false",
domain: "youchat",
responseFilter: "WebPages,TimeZone,Computation,RelatedSearches",
mkt: "zh-CN",
userFiles: uploadedFile
? JSON.stringify([
{
user_filename: "messages.txt",
filename: uploadedFile,
size: messageBuffer.length,
},
])
: "",
chat: JSON.stringify(userMessage),
},
headers: {
accept: "text/event-stream",
referer: "https://you.com/search?q=" + encodeURIComponent(userQuery.trim()) +"&fromSearchBar=true&tbm=youchat&chatMode=custom"
},
responseType: "stream",
})
.catch((e) => {
if(e?.response?.data) {
// print data
e.response.data.on("data", (chunk) => {
console.log(chunk.toString());
}
);
}else{
throw e;
}
});
let cachedLine = "";
const stream = proxyReq.data;
stream.on("data", (chunk) => {
// try to parse eventstream chunk
chunk = chunk.toString();
if (cachedLine) {
chunk = cachedLine + chunk;
cachedLine = "";
}
if (!chunk.endsWith("\n")) {
const lines = chunk.split("\n");
cachedLine = lines.pop();
chunk = lines.join("\n");
}
try {
console.log(chunk);
if (chunk.indexOf("event: youChatToken\n") != -1) {
chunk.split("\n").forEach((line) => {
if (line.startsWith(`data: {"youChatToken"`)) {
let data = line.substring(6);
let json = JSON.parse(data);
//console.log(json);
chunkJSON = JSON.stringify({
type: "content_block_delta",
index: 0,
delta: { type: "text_delta", text: json.youChatToken },
});
res.write(createEvent("content_block_delta", chunkJSON));
}
});
}
} catch (e) {
console.log(e);
}
});
stream.on("end", () => {
// send ending
res.write(createEvent("content_block_stop", { type: "content_block_stop", index: 0 }));
res.write(
createEvent("message_delta", {
type: "message_delta",
delta: { stop_reason: "end_turn", stop_sequence: null },
usage: { output_tokens: 12 },
})
);
res.write(createEvent("message_stop", { type: "message_stop" }));
res.end();
});
} else {
throw new Error("Invalid request");
}
} catch (e) {
console.log(e);
res.write(JSON.stringify({ error: e.message }));
res.end();
return;
}
});
});
// handle other
app.use((req, res, next) => {
res.status(404).send("Not Found");
});
app.listen(port, () => {
console.log(`YouChat proxy listening on port ${port}`);
});
// eventStream util
function createEvent(event, data) {
// if data is object, stringify it
if (typeof data === "object") {
data = JSON.stringify(data);
}
return `event: ${event}\ndata: ${data}\n\n`;
}