use file to unlock ctx
This commit is contained in:
@@ -1,9 +1,13 @@
|
|||||||
const express = require("express");
|
const express = require("express");
|
||||||
const https = require("https");
|
const FormData = require("form-data");
|
||||||
const { v4: uuidv4 } = require("uuid");
|
const { v4: uuidv4 } = require("uuid");
|
||||||
const app = express();
|
const app = express();
|
||||||
|
const axios = require("axios");
|
||||||
const port = 8080;
|
const port = 8080;
|
||||||
const YOUCOM_COOKIE = process.env.YOUCOM_COOKIE;
|
|
||||||
|
(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) => {
|
app.post("/v1/messages", (req, res) => {
|
||||||
req.rawBody = "";
|
req.rawBody = "";
|
||||||
@@ -13,7 +17,7 @@ app.post("/v1/messages", (req, res) => {
|
|||||||
req.rawBody += chunk;
|
req.rawBody += chunk;
|
||||||
});
|
});
|
||||||
|
|
||||||
req.on("end", function () {
|
req.on("end", async () => {
|
||||||
res.setHeader("Content-Type", "text/event-stream;charset=utf-8");
|
res.setHeader("Content-Type", "text/event-stream;charset=utf-8");
|
||||||
try {
|
try {
|
||||||
let jsonBody = JSON.parse(req.rawBody);
|
let jsonBody = JSON.parse(req.rawBody);
|
||||||
@@ -41,7 +45,7 @@ app.post("/v1/messages", (req, res) => {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
} else if (jsonBody.stream == true) {
|
} else if (jsonBody.stream == true) {
|
||||||
// squash messages
|
// 计算用户消息长度
|
||||||
let userMessage = [{ question: "", answer: "" }];
|
let userMessage = [{ question: "", answer: "" }];
|
||||||
let userQuery = "";
|
let userQuery = "";
|
||||||
let lastUpdate = true;
|
let lastUpdate = true;
|
||||||
@@ -67,11 +71,23 @@ app.post("/v1/messages", (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
userQuery = userMessage[userMessage.length - 1].question;
|
userQuery = userMessage[userMessage.length - 1].question;
|
||||||
// if (userMessage[userMessage.length - 1].answer == "") {
|
if (userMessage[userMessage.length - 1].answer == "") {
|
||||||
// userMessage.pop();
|
userMessage.pop();
|
||||||
// }
|
}
|
||||||
console.log(userMessage);
|
console.log(userMessage);
|
||||||
|
|
||||||
|
// user message to plaintext
|
||||||
|
let previousMessages = jsonBody.messages
|
||||||
|
.map((msg) => {
|
||||||
|
return `${msg.role}: ${msg.content}`;
|
||||||
|
})
|
||||||
|
.join("\n\n");
|
||||||
|
|
||||||
|
// 只保留最后一条用户消息
|
||||||
|
if(userMessage.length > 1) {
|
||||||
|
userMessage = userMessage.slice(-1);
|
||||||
|
}
|
||||||
|
|
||||||
let msgid = uuidv4();
|
let msgid = uuidv4();
|
||||||
|
|
||||||
// send message start
|
// send message start
|
||||||
@@ -92,6 +108,25 @@ app.post("/v1/messages", (req, res) => {
|
|||||||
);
|
);
|
||||||
res.write(createEvent("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }));
|
res.write(createEvent("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }));
|
||||||
res.write(createEvent("ping", { type: "ping" }));
|
res.write(createEvent("ping", { type: "ping" }));
|
||||||
|
|
||||||
|
// 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();
|
||||||
|
messageBuffer = Buffer.from(previousMessages, "utf8");
|
||||||
|
form_data.append("file", messageBuffer, { filename: "Previous_Conversation.txt", contentType: "text/plain" });
|
||||||
|
let 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");
|
||||||
|
|
||||||
// proxy response
|
// proxy response
|
||||||
let youcom_params = new URLSearchParams();
|
let youcom_params = new URLSearchParams();
|
||||||
youcom_params.append("page", "0");
|
youcom_params.append("page", "0");
|
||||||
@@ -104,85 +139,87 @@ app.post("/v1/messages", (req, res) => {
|
|||||||
youcom_params.append("conversationTurnId", msgid);
|
youcom_params.append("conversationTurnId", msgid);
|
||||||
youcom_params.append("selectedAIModel", "claude_3_opus");
|
youcom_params.append("selectedAIModel", "claude_3_opus");
|
||||||
youcom_params.append("selectedChatMode", "custom");
|
youcom_params.append("selectedChatMode", "custom");
|
||||||
youcom_params.append("pastChatLength", userMessage.length - 1);
|
youcom_params.append("pastChatLength", "0");
|
||||||
youcom_params.append("queryTraceId", msgid);
|
youcom_params.append("queryTraceId", msgid);
|
||||||
youcom_params.append("use_personalization_extraction", "false");
|
youcom_params.append("use_personalization_extraction", "false");
|
||||||
youcom_params.append("domain", "youchat");
|
youcom_params.append("domain", "youchat");
|
||||||
youcom_params.append("responseFilter", "");
|
youcom_params.append("responseFilter", "");
|
||||||
youcom_params.append("mkt", "zh-CN");
|
youcom_params.append("mkt", "zh-CN");
|
||||||
|
youcom_params.append(
|
||||||
|
"userFiles",
|
||||||
|
JSON.stringify([
|
||||||
|
{
|
||||||
|
user_filename: "Previous_Conversation.txt",
|
||||||
|
filename: uploadedFile,
|
||||||
|
size: messageBuffer.length,
|
||||||
|
},
|
||||||
|
])
|
||||||
|
);
|
||||||
youcom_params.append("chat", JSON.stringify(userMessage));
|
youcom_params.append("chat", JSON.stringify(userMessage));
|
||||||
|
|
||||||
var proxyReq = https.request(
|
var proxyReq = await axios({
|
||||||
{
|
method: "GET",
|
||||||
hostname: "you.com",
|
url: "https://you.com/api/streamingSearch?" + youcom_params.toString(),
|
||||||
port: 443,
|
headers: {
|
||||||
method: "GET",
|
accept: "text/event-stream",
|
||||||
path: "/api/streamingSearch?" + youcom_params.toString(),
|
|
||||||
headers: {
|
|
||||||
cookie: YOUCOM_COOKIE,
|
|
||||||
"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",
|
|
||||||
accept: "text/event-stream",
|
|
||||||
"content-type": "application/x-www-form-urlencoded",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
(proxyRes) => {
|
responseType: "stream",
|
||||||
let cachedLine = "";
|
}).catch((e) => {
|
||||||
proxyRes.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 {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
proxyRes.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();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
);
|
|
||||||
proxyReq.on("error", (e) => {
|
|
||||||
throw e;
|
throw e;
|
||||||
});
|
});
|
||||||
//proxyReq.write(youcom_params.toString());
|
|
||||||
proxyReq.end();
|
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 {
|
} else {
|
||||||
throw new Error("Invalid request");
|
throw new Error("Invalid request");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,9 @@
|
|||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"axios": "^1.6.8",
|
||||||
"express": "^4.19.2",
|
"express": "^4.19.2",
|
||||||
|
"form-data": "^4.0.0",
|
||||||
"uuid": "^9.0.1"
|
"uuid": "^9.0.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user