File size: 3,070 Bytes
3cbcbf0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87b8fba
3cbcbf0
 
 
 
87b8fba
3cbcbf0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const { v4: uuidv4 } = require('uuid');
const zlib = require('zlib');
const $root = require('./message.js');

const regex = /<\|BEGIN_SYSTEM\|>.*?<\|END_SYSTEM\|>.*?<\|BEGIN_USER\|>.*?<\|END_USER\|>/s;

async function stringToHex(messages, modelName) {
  const formattedMessages = messages.map((msg) => ({
    ...msg,
    role: msg.role === 'user' ? 1 : 2,
    message_id: uuidv4(),
  }));

  const message = {
    messages: formattedMessages,
    instructions: {
      instruction: 'Always respond in 中文',
    },
    projectPath: '/path/to/project',
    model: {
      name: modelName,
      empty: '',
    },
    requestId: uuidv4(),
    summary: '',
    conversationId: uuidv4(),
  };
  const errMsg = $root.ChatMessage.verify(message);
  if (errMsg) throw Error(errMsg);

  const messageInstance = $root.ChatMessage.create(message);

  const buffer = $root.ChatMessage.encode(messageInstance).finish();
  const hexString = (buffer.length.toString(16).padStart(10, '0') + buffer.toString('hex')).toUpperCase();

  return Buffer.from(hexString, 'hex');
}

async function chunkToUtf8String(chunk) {
  try {
    let hex = Buffer.from(chunk).toString('hex');
    console.log("debug [42] :", hex)
    
    let offset = 0;
    let results = [];

    while (offset < hex.length) {
      if (offset + 10 > hex.length) break;

      const dataLength = parseInt(hex.slice(offset, offset + 10), 16);
      offset += 10;

      if (offset + dataLength * 2 > hex.length) break;

      const messageHex = hex.slice(offset, offset + dataLength * 2);
      offset += dataLength * 2;

      console.log("debug [57] :", messageHex)
      const messageBuffer = Buffer.from(messageHex, 'hex');
      const message = $root.ResMessage.decode(messageBuffer);
      results.push(message.msg);
    }

    if (results.length == 0) {
      console.log("debug [63] :", chunk)
      return gunzip(chunk);
    }
    return results.join('');
  } catch (err) {
    console.log("debug [68] :", chunk)
    return gunzip(chunk);
  }
}

function gunzip(chunk) {
  return new Promise((resolve, reject) => {
    zlib.gunzip(chunk.slice(5), (err, decompressed) => {
      if (err) {
        resolve('');
      } else {
        const text = decompressed.toString('utf-8');
        // 这里只是尝试解析错误数据,如果是包含了全量的返回结果直接忽略
        if (regex.test(text)) {
          resolve('');
        } else {
          resolve(text);
        }
      }
    });
  });
}

function getRandomIDPro({ size, dictType, customDict }) {
  let random = '';
  if (!customDict) {
    switch (dictType) {
      case 'alphabet':
        customDict = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        break;
      case 'max':
        customDict = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-';
        break;
      default:
        customDict = '0123456789';
    }
  }
  for (; size--; ) random += customDict[(Math.random() * customDict.length) | 0];
  return random;
}

module.exports = {
  stringToHex,
  chunkToUtf8String,
  getRandomIDPro,
};