Spaces:
Running
Running
File size: 5,780 Bytes
b51f54a |
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 |
const os = require('os');
const zlib = require('zlib');
const crypto = require('crypto');
const { v4: uuidv4 } = require('uuid');
const $root = require('../proto/message.js');
function generateCursorBody(messages, modelName) {
const instruction = messages
.filter(msg => msg.role === 'system')
.map(msg => msg.content)
.join('\n')
const formattedMessages = messages
.filter(msg => msg.role !== 'system')
.map(msg => ({
content: msg.content,
role: msg.role === 'user' ? 1 : 2,
messageId: uuidv4(),
...(msg.role === 'user' ? { chatModeEnum: 1 } : {})
//...(msg.role !== 'user' ? { summaryId: uuidv4() } : {})
}));
const messageIds = formattedMessages.map(msg => {
const { role, messageId, summaryId } = msg;
return summaryId ? { role, messageId, summaryId } : { role, messageId };
});
const body = {
request:{
messages: formattedMessages,
unknown2: 1,
instruction: {
instruction: instruction
},
unknown4: 1,
model: {
name: modelName,
empty: '',
},
webTool: "",
unknown13: 1,
cursorSetting: {
name: "cursor\\aisettings",
unknown3: "",
unknown6: {
unknwon1: "",
unknown2: ""
},
unknown8: 1,
unknown9: 1
},
unknown19: 1,
//unknown22: 1,
conversationId: uuidv4(),
metadata: {
os: "win32",
arch: "x64",
version: "10.0.22631",
path: "C:\\Program Files\\PowerShell\\7\\pwsh.exe",
timestamp: new Date().toISOString(),
},
unknown27: 0,
//unknown29: "",
messageIds: messageIds,
largeContext: 0,
unknown38: 0,
chatModeEnum: 1,
unknown47: "",
unknown48: 0,
unknown49: 0,
unknown51: 0,
unknown53: 1,
chatMode: "Ask"
}
};
const errMsg = $root.StreamUnifiedChatWithToolsRequest.verify(body);
if (errMsg) throw Error(errMsg);
const instance = $root.StreamUnifiedChatWithToolsRequest.create(body);
let buffer = $root.StreamUnifiedChatWithToolsRequest.encode(instance).finish();
let magicNumber = 0x00
if (formattedMessages.length >= 3){
buffer = zlib.gzipSync(buffer)
magicNumber = 0x01
}
const finalBody = Buffer.concat([
Buffer.from([magicNumber]),
Buffer.from(buffer.length.toString(16).padStart(8, '0'), 'hex'),
buffer
])
return finalBody
}
function chunkToUtf8String(chunk) {
const results = []
const errorResults = { hasError: false, errorMessage: '' }
const buffer = Buffer.from(chunk, 'hex');
//console.log("Chunk buffer:", buffer.toString('hex'))
try {
for(let i = 0; i < buffer.length; i++){
const magicNumber = parseInt(buffer.subarray(i, i + 1).toString('hex'), 16)
const dataLength = parseInt(buffer.subarray(i + 1, i + 5).toString('hex'), 16)
const data = buffer.subarray(i + 5, i + 5 + dataLength)
//console.log("Parsed buffer:", magicNumber, dataLength, data.toString('hex'))
if (magicNumber == 0 || magicNumber == 1) {
const gunzipData = magicNumber == 0 ? data : zlib.gunzipSync(data)
const response = $root.StreamUnifiedChatWithToolsResponse.decode(gunzipData);
const thinking = response?.message?.thinking?.content
if (thinking !== undefined){
results.push(thinking);
//console.log(thinking);
}
const content = response?.message?.content
if (content !== undefined){
results.push(content)
//console.log(content)
}
}
else if (magicNumber == 2 || magicNumber == 3) {
// Json message
const gunzipData = magicNumber == 2 ? data : zlib.gunzipSync(data)
const utf8 = gunzipData.toString('utf-8')
const message = JSON.parse(utf8)
if (message != null && (typeof message !== 'object' ||
(Array.isArray(message) ? message.length > 0 : Object.keys(message).length > 0))){
//results.push(utf8)
console.error(utf8)
// 检查是否为错误消息
if (message && message.error) {
errorResults.hasError = true;
errorResults.errorMessage = utf8;
}
}
}
else {
//console.log('Unknown magic number when parsing chunk response: ' + magicNumber)
}
i += 5 + dataLength - 1
}
} catch (err) {
console.log('Error parsing chunk response:', err)
}
// 如果存在错误,返回错误对象
if (errorResults.hasError) {
return { error: errorResults.errorMessage };
}
return results.join('')
}
function generateHashed64Hex(input, salt = '') {
const hash = crypto.createHash('sha256');
hash.update(input + salt);
return hash.digest('hex');
}
function obfuscateBytes(byteArray) {
let t = 165;
for (let r = 0; r < byteArray.length; r++) {
byteArray[r] = (byteArray[r] ^ t) + (r % 256);
t = byteArray[r];
}
return byteArray;
}
function generateCursorChecksum(token) {
const machineId = generateHashed64Hex(token, 'machineId');
const macMachineId = generateHashed64Hex(token, 'macMachineId');
const timestamp = Math.floor(Date.now() / 1e6);
const byteArray = new Uint8Array([
(timestamp >> 40) & 255,
(timestamp >> 32) & 255,
(timestamp >> 24) & 255,
(timestamp >> 16) & 255,
(timestamp >> 8) & 255,
255 & timestamp,
]);
const obfuscatedBytes = obfuscateBytes(byteArray);
const encodedChecksum = Buffer.from(obfuscatedBytes).toString('base64');
return `${encodedChecksum}${machineId}/${macMachineId}`;
}
module.exports = {
generateCursorBody,
chunkToUtf8String,
generateHashed64Hex,
generateCursorChecksum
};
|