2023-05-31 16:10:53 +08:00
|
|
|
import DevChat, { ChatResponse } from '../toolwrapper/devchat';
|
|
|
|
import CommandManager from '../command/commandManager';
|
|
|
|
import { logger } from '../util/logger';
|
|
|
|
import messageHistory from '../util/messageHistory';
|
|
|
|
import { TopicManager } from '../topic/topicManager';
|
|
|
|
import CustomCommands from '../command/customCommand';
|
|
|
|
|
|
|
|
|
|
|
|
// Add this function to messageHandler.ts
|
2023-05-31 16:10:53 +08:00
|
|
|
export function parseMessage(message: string): { context: string[]; instruction: string[]; reference: string[]; text: string } {
|
2023-05-31 16:10:53 +08:00
|
|
|
const contextRegex = /\[context\|(.*?)\]/g;
|
|
|
|
const instructionRegex = /\[instruction\|(.*?)\]/g;
|
|
|
|
const referenceRegex = /\[reference\|(.*?)\]/g;
|
|
|
|
|
|
|
|
const contextPaths = [];
|
|
|
|
const instructionPaths = [];
|
|
|
|
const referencePaths = [];
|
|
|
|
|
|
|
|
let match;
|
|
|
|
|
|
|
|
// 提取 context
|
|
|
|
while ((match = contextRegex.exec(message)) !== null) {
|
|
|
|
contextPaths.push(match[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 提取 instruction
|
|
|
|
while ((match = instructionRegex.exec(message)) !== null) {
|
|
|
|
instructionPaths.push(match[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 提取 reference
|
|
|
|
while ((match = referenceRegex.exec(message)) !== null) {
|
|
|
|
referencePaths.push(match[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 移除标签,保留纯文本
|
|
|
|
const text = message
|
|
|
|
.replace(contextRegex, '')
|
|
|
|
.replace(instructionRegex, '')
|
|
|
|
.replace(referenceRegex, '')
|
|
|
|
.trim();
|
|
|
|
|
|
|
|
return { context: contextPaths, instruction: instructionPaths, reference: referencePaths, text };
|
|
|
|
}
|
|
|
|
|
2023-05-31 16:10:53 +08:00
|
|
|
export function getInstructionFiles(): string[] {
|
2023-05-31 16:10:53 +08:00
|
|
|
const instructionFiles: string[] = [];
|
|
|
|
|
|
|
|
const customCommands = CustomCommands.getInstance().getCommands();
|
|
|
|
// visit customCommands, get default command
|
|
|
|
for (const command of customCommands) {
|
|
|
|
if (command.default) {
|
|
|
|
for (const instruction of command.instructions) {
|
2023-06-02 09:58:44 +08:00
|
|
|
instructionFiles.push(`${instruction}`);
|
2023-05-31 16:10:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return instructionFiles;
|
|
|
|
}
|
|
|
|
|
|
|
|
const devChat = new DevChat();
|
|
|
|
let userStop = false;
|
|
|
|
|
|
|
|
|
|
|
|
// 将解析消息的部分提取到一个单独的函数中
|
2023-05-31 16:10:53 +08:00
|
|
|
export async function parseMessageAndSetOptions(message: any, chatOptions: any): Promise<{ context: string[]; instruction: string[]; reference: string[]; text: string }> {
|
2023-05-31 16:10:53 +08:00
|
|
|
const newText2 = await CommandManager.getInstance().processText(message.text);
|
|
|
|
const parsedMessage = parseMessage(newText2);
|
|
|
|
|
|
|
|
if (parsedMessage.context.length > 0) {
|
|
|
|
chatOptions.context = parsedMessage.context;
|
|
|
|
}
|
|
|
|
|
|
|
|
chatOptions.header = getInstructionFiles();
|
2023-06-02 09:58:44 +08:00
|
|
|
if (message.text === newText2) {
|
2023-05-31 16:10:53 +08:00
|
|
|
chatOptions.header = parsedMessage.instruction;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parsedMessage.reference.length > 0) {
|
|
|
|
chatOptions.reference = parsedMessage.reference;
|
|
|
|
}
|
|
|
|
return parsedMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 将处理父哈希的部分提取到一个单独的函数中
|
2023-05-31 16:10:53 +08:00
|
|
|
export function getParentHash(message: any): string|undefined {
|
2023-05-31 16:10:53 +08:00
|
|
|
let parentHash = undefined;
|
|
|
|
logger.channel()?.info(`request message hash: ${message.hash}`);
|
|
|
|
if (message.hash) {
|
|
|
|
const hmessage = messageHistory.find(message.hash);
|
2023-05-31 16:10:53 +08:00
|
|
|
parentHash = hmessage ? hmessage.parentHash : undefined;
|
2023-05-31 16:10:53 +08:00
|
|
|
} else {
|
|
|
|
const hmessage = messageHistory.findLast();
|
|
|
|
parentHash = hmessage ? hmessage.hash : undefined;
|
|
|
|
}
|
|
|
|
logger.channel()?.info(`parent hash: ${parentHash}`);
|
|
|
|
return parentHash;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function handleTopic(parentHash:string, message: any, chatResponse: ChatResponse) {
|
|
|
|
if (!chatResponse.isError) {
|
|
|
|
messageHistory.add({ request: message.text, text: chatResponse.response, parentHash, hash: chatResponse['prompt-hash'], user: chatResponse.user, date: chatResponse.date });
|
|
|
|
|
|
|
|
let topicId = TopicManager.getInstance().currentTopicId;
|
|
|
|
if (!topicId) {
|
|
|
|
// create new topic
|
|
|
|
const topic = TopicManager.getInstance().createTopic();
|
|
|
|
topicId = topic.topicId;
|
|
|
|
}
|
|
|
|
|
|
|
|
TopicManager.getInstance().updateTopic(topicId!, chatResponse['prompt-hash'], Number(chatResponse.date), message.text, chatResponse.response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function handlerResponseText(partialDataText: string, chatResponse: ChatResponse) : Promise<string|undefined> {
|
|
|
|
let responseText = chatResponse.response.replace(/```\ncommitmsg/g, "```commitmsg");
|
|
|
|
if (userStop) {
|
|
|
|
userStop = false;
|
2023-05-31 16:10:53 +08:00
|
|
|
if (responseText == '' && chatResponse.isError) {
|
2023-05-31 16:10:53 +08:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
2023-06-05 09:17:58 +08:00
|
|
|
|
2023-05-31 16:10:53 +08:00
|
|
|
return responseText;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 重构后的sendMessage函数
|
|
|
|
export async function sendMessageBase(message: any, handlePartialData: (data: { command: string, text: string, user: string, date: string}) => void): Promise<{ command: string, text: string, hash: string, user: string, date: string, isError: boolean }|undefined> {
|
|
|
|
const chatOptions: any = {};
|
|
|
|
const parsedMessage = await parseMessageAndSetOptions(message, chatOptions);
|
|
|
|
|
|
|
|
const parentHash = getParentHash(message);
|
|
|
|
if (parentHash) {
|
|
|
|
chatOptions.parent = parentHash;
|
|
|
|
}
|
|
|
|
|
|
|
|
let partialDataText = '';
|
|
|
|
const onData = (partialResponse: ChatResponse) => {
|
|
|
|
partialDataText = partialResponse.response.replace(/```\ncommitmsg/g, "```commitmsg");
|
|
|
|
handlePartialData({ command: 'receiveMessagePartial', text: partialDataText!, user: partialResponse.user, date: partialResponse.date });
|
|
|
|
};
|
|
|
|
|
|
|
|
const chatResponse = await devChat.chat(parsedMessage.text, chatOptions, onData);
|
|
|
|
await handleTopic(parentHash!, message, chatResponse);
|
|
|
|
const responseText = await handlerResponseText(partialDataText, chatResponse);
|
|
|
|
if (responseText === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return { command: 'receiveMessage', text: responseText, hash: chatResponse['prompt-hash'], user: chatResponse.user, date: chatResponse.date, isError: chatResponse.isError };
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function stopDevChatBase(message: any): Promise<void> {
|
|
|
|
logger.channel()?.info(`Stopping devchat`);
|
|
|
|
userStop = true;
|
|
|
|
devChat.stop();
|
|
|
|
}
|