Upload index.js
Browse files50 unmodified lines51525354555636 unmodified lines9394959697989910046 unmodified lines147148149150151152236 unmodified lines38939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245350 unmodified lines'baggage': 'sentry-public_key=b311e0f2690c81f25e2c4cf6d4f7ce1c'};class Utils {static async extractGrokHeaders() {try {36 unmodified linesif (headers) {console.log("获取认证信息成功");CONFIG.API.SIGNATURE_COOKIE = { cookie: `x-anonuserid=${headers["x-anonuserid"]}; x-challenge=${headers["x-challenge"]}; x-signature=${headers["x-signature"]}` };fs.writeFileSync(path.resolve(process.cwd(), 'signature.json'), JSON.stringify(CONFIG.API.SIGNATURE_COOKIE));//保存认证信息return CONFIG.API.SIGNATURE_COOKIE;}retryCount++;if (retryCount >= CONFIG.RETRY.MAX_ATTEMPTS) {46 unmodified lines}});}}236 unmodified linesreturn res.status(400).json({ error: '该模型不支持流式' });}const makeRequest = async () => {if (!CONFIG.API.SIGNATURE_COOKIE) {await Utils.get_signature();CONFIG.API.SIGNATURE_COOKIE = JSON.parse(fs.readFileSync(path.resolve(process.cwd(), 'signature.json')));}const grokClient = new GrokApiClient(req.body.model);const requestPayload = await grokClient.prepareChatRequest(req.body);//创建新对话const newMessageReq = await fetch(`${CONFIG.API.BASE_URL}/api/rpc`, {method: 'POST',headers: {...DEFAULT_HEADERS,...CONFIG.API.SIGNATURE_COOKIE},body: JSON.stringify({rpc: "createConversation",req: {temporary: false}})});if (!newMessageReq.ok) {throw new Error(`上游服务请求失败! status: ${newMessageReq.status}`);}// 获取响应文本const responseText = await newMessageReq.json();const conversationId = responseText.conversationId;console.log("会话ID:conversationId", conversationId);if (!conversationId) {throw new Error(`创建会话失败! status: ${newMessageReq.status}`);}//发送对话const response = await fetch(`${CONFIG.API.BASE_URL}/api/conversations/${conversationId}/responses`, {method: 'POST',headers: {"accept": "text/event-stream","baggage": "sentry-public_key=b311e0f2690c81f25e2c4cf6d4f7ce1c","content-type": "text/plain;charset=UTF-8","Connection": "keep-alive",...CONFIG.API.SIGNATURE_COOKIE},body: JSON.stringify(requestPayload)});if (!response.ok) {throw new Error(`上游服务请求失败! status: ${response.status}`);}if (req.body.stream) {await handleStreamResponse(response, req.body.model, res);} else {await handleNormalResponse(response, req.body.model, res);}}try {await makeRequest();} catch (error) {await Utils.handleError(error, res, makeRequest);}});50 unmodified lines51525354555657585936 unmodified lines9697989910010110210310410510610710846 unmodified lines155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226236 unmodified lines46346446546646746846947047147250 unmodified lines'baggage': 'sentry-public_key=b311e0f2690c81f25e2c4cf6d4f7ce1c'};// 定义签名文件路径为 /tmp 目录const SIGNATURE_FILE_PATH = '/tmp/signature.json';class Utils {static async extractGrokHeaders() {try {36 unmodified linesif (headers) {console.log("获取认证信息成功");CONFIG.API.SIGNATURE_COOKIE = { cookie: `x-anonuserid=${headers["x-anonuserid"]}; x-challenge=${headers["x-challenge"]}; x-signature=${headers["x-signature"]}` };try {fs.writeFileSync(SIGNATURE_FILE_PATH, JSON.stringify(CONFIG.API.SIGNATURE_COOKIE));//保存认证信息return CONFIG.API.SIGNATURE_COOKIE;} catch (error) {console.error('写入签名文件失败:', error);return CONFIG.API.SIGNATURE_COOKIE;}}retryCount++;if (retryCount >= CONFIG.RETRY.MAX_ATTEMPTS) {46 unmodified lines}});}static async makeRequest(req) {try {if (!CONFIG.API.SIGNATURE_COOKIE) {await Utils.get_signature();try {CONFIG.API.SIGNATURE_COOKIE = JSON.parse(fs.readFileSync(SIGNATURE_FILE_PATH));} catch (error) {console.error('读取签名文件失败:', error);await Utils.get_signature(); // 如果读取失败,重新获取签名}}const grokClient = new GrokApiClient(req.body.model);const requestPayload = await grokClient.prepareChatRequest(req.body);//创建新对话const newMessageReq = await fetch(`${CONFIG.API.BASE_URL}/api/rpc`, {method: 'POST',headers: {...DEFAULT_HEADERS,...CONFIG.API.SIGNATURE_COOKIE},body: JSON.stringify({rpc: "createConversation",req: {temporary: false}})});if (!newMessageReq.ok) {throw new Error(`上游服务请求失败! status: ${newMessageReq.status}`);}// 获取响应文本const responseText = await newMessageReq.json();const conversationId = responseText.conversationId;console.log("会话ID:conversationId", conversationId);if (!conversationId) {throw new Error(`创建会话失败! status: ${newMessageReq.status}`);}//发送对话const response = await fetch(`${CONFIG.API.BASE_URL}/api/conversations/${conversationId}/responses`, {method: 'POST',headers: {"accept": "text/event-stream","baggage": "sentry-public_key=b311e0f2690c81f25e2c4cf6d4f7ce1c","content-type": "text/plain;charset=UTF-8","Connection": "keep-alive",...CONFIG.API.SIGNATURE_COOKIE},body: JSON.stringify(requestPayload)});if (!response.ok) {throw new Error(`上游服务请求失败! status: ${response.status}`);}if (req.body.stream) {await handleStreamResponse(response, req.body.model, res);} else {await handleNormalResponse(response, req.body.model, res);}} catch (error) {throw error;}}}236 unmodified linesreturn res.status(400).json({ error: '该模型不支持流式' });}try {await Utils.makeRequest(req);} catch (error) {await Utils.handleError(error, res);}});
index.js+78-59