Spaces:
Paused
Paused
Update app.js
Browse files
app.js
CHANGED
|
@@ -3,91 +3,75 @@ const { v4: uuidv4 } = require('uuid');
|
|
| 3 |
const zlib = require('zlib');
|
| 4 |
const protobuf = require('protobufjs');
|
| 5 |
|
| 6 |
-
//
|
| 7 |
-
const
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
messages: { rule: "repeated", type: "UserMessage", id: 2 },
|
| 15 |
-
instructions: { type: "Instructions", id: 4 },
|
| 16 |
-
projectPath: { type: "string", id: 5 },
|
| 17 |
-
model: { type: "Model", id: 7 },
|
| 18 |
-
requestId: { type: "string", id: 9 },
|
| 19 |
-
summary: { type: "string", id: 11 },
|
| 20 |
-
conversationId: { type: "string", id: 15 }
|
| 21 |
-
},
|
| 22 |
-
nested: {
|
| 23 |
-
FileContent: {
|
| 24 |
-
fields: {
|
| 25 |
-
filename: { type: "string", id: 1 },
|
| 26 |
-
content: { type: "string", id: 2 },
|
| 27 |
-
position: { type: "Position", id: 3 },
|
| 28 |
-
language: { type: "string", id: 5 },
|
| 29 |
-
range: { type: "Range", id: 6 },
|
| 30 |
-
length: { type: "int32", id: 8 },
|
| 31 |
-
type: { type: "int32", id: 9 },
|
| 32 |
-
error_code: { type: "int32", id: 11 }
|
| 33 |
-
},
|
| 34 |
-
nested: {
|
| 35 |
-
Position: {
|
| 36 |
-
fields: {
|
| 37 |
-
line: { type: "int32", id: 1 },
|
| 38 |
-
column: { type: "int32", id: 2 }
|
| 39 |
-
}
|
| 40 |
-
},
|
| 41 |
-
Range: {
|
| 42 |
-
fields: {
|
| 43 |
-
start: { type: "Position", id: 1 },
|
| 44 |
-
end: { type: "Position", id: 2 }
|
| 45 |
-
}
|
| 46 |
-
}
|
| 47 |
-
}
|
| 48 |
-
},
|
| 49 |
-
UserMessage: {
|
| 50 |
-
fields: {
|
| 51 |
-
content: { type: "string", id: 1 },
|
| 52 |
-
role: { type: "int32", id: 2 },
|
| 53 |
-
message_id: { type: "string", id: 13 }
|
| 54 |
-
}
|
| 55 |
-
},
|
| 56 |
-
Instructions: {
|
| 57 |
-
fields: {
|
| 58 |
-
instruction: { type: "string", id: 1 }
|
| 59 |
-
}
|
| 60 |
-
},
|
| 61 |
-
Model: {
|
| 62 |
-
fields: {
|
| 63 |
-
name: { type: "string", id: 1 },
|
| 64 |
-
empty: { type: "string", id: 4 }
|
| 65 |
-
}
|
| 66 |
-
}
|
| 67 |
-
}
|
| 68 |
-
},
|
| 69 |
-
ResMessage: {
|
| 70 |
-
fields: {
|
| 71 |
-
msg: { type: "string", id: 1 }
|
| 72 |
-
}
|
| 73 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
}
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
//
|
| 78 |
-
root.
|
| 79 |
|
| 80 |
// 获取消息类型
|
| 81 |
const ChatMessage = root.lookupType("ChatMessage");
|
| 82 |
const ResMessage = root.lookupType("ResMessage");
|
| 83 |
|
| 84 |
-
//
|
| 85 |
const $root = {
|
| 86 |
ChatMessage,
|
| 87 |
ResMessage
|
| 88 |
};
|
| 89 |
|
| 90 |
-
//
|
| 91 |
const regex = /<\|BEGIN_SYSTEM\|>.*?<\|END_SYSTEM\|>.*?<\|BEGIN_USER\|>.*?<\|END_USER\|>/s;
|
| 92 |
|
| 93 |
async function stringToHex(messages, modelName) {
|
|
|
|
| 3 |
const zlib = require('zlib');
|
| 4 |
const protobuf = require('protobufjs');
|
| 5 |
|
| 6 |
+
// 创建 proto 定义字符串
|
| 7 |
+
const protoDefinition = `
|
| 8 |
+
syntax = "proto3";
|
| 9 |
|
| 10 |
+
message ChatMessage {
|
| 11 |
+
message FileContent {
|
| 12 |
+
message Position {
|
| 13 |
+
int32 line = 1;
|
| 14 |
+
int32 column = 2;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
}
|
| 16 |
+
|
| 17 |
+
message Range {
|
| 18 |
+
Position start = 1;
|
| 19 |
+
Position end = 2;
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
string filename = 1;
|
| 23 |
+
string content = 2;
|
| 24 |
+
Position position = 3;
|
| 25 |
+
string language = 5;
|
| 26 |
+
Range range = 6;
|
| 27 |
+
int32 length = 8;
|
| 28 |
+
int32 type = 9;
|
| 29 |
+
int32 error_code = 11;
|
| 30 |
}
|
| 31 |
+
|
| 32 |
+
message UserMessage {
|
| 33 |
+
string content = 1;
|
| 34 |
+
int32 role = 2;
|
| 35 |
+
string message_id = 13;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
message Instructions {
|
| 39 |
+
string instruction = 1;
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
message Model {
|
| 43 |
+
string name = 1;
|
| 44 |
+
string empty = 4;
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
repeated UserMessage messages = 2;
|
| 48 |
+
Instructions instructions = 4;
|
| 49 |
+
string projectPath = 5;
|
| 50 |
+
Model model = 7;
|
| 51 |
+
string requestId = 9;
|
| 52 |
+
string summary = 11;
|
| 53 |
+
string conversationId = 15;
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
message ResMessage {
|
| 57 |
+
string msg = 1;
|
| 58 |
+
}
|
| 59 |
+
`;
|
| 60 |
|
| 61 |
+
// 使用 protobuf.parse 解析 proto 定义
|
| 62 |
+
const root = protobuf.parse(protoDefinition).root;
|
| 63 |
|
| 64 |
// 获取消息类型
|
| 65 |
const ChatMessage = root.lookupType("ChatMessage");
|
| 66 |
const ResMessage = root.lookupType("ResMessage");
|
| 67 |
|
| 68 |
+
// 创建 $root 对象
|
| 69 |
const $root = {
|
| 70 |
ChatMessage,
|
| 71 |
ResMessage
|
| 72 |
};
|
| 73 |
|
| 74 |
+
// regex 定义
|
| 75 |
const regex = /<\|BEGIN_SYSTEM\|>.*?<\|END_SYSTEM\|>.*?<\|BEGIN_USER\|>.*?<\|END_USER\|>/s;
|
| 76 |
|
| 77 |
async function stringToHex(messages, modelName) {
|