2023-05-05 21:27:40 +08:00
|
|
|
|
|
|
|
import * as vscode from 'vscode';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
2023-05-09 13:24:33 +08:00
|
|
|
import DevChat, { ChatResponse } from '../toolwrapper/devchat';
|
2023-05-05 21:27:40 +08:00
|
|
|
import CommandManager from '../command/commandManager';
|
2023-05-09 10:34:33 +08:00
|
|
|
import { logger } from '../util/logger';
|
|
|
|
import { MessageHandler } from './messageHandler';
|
2023-05-09 16:05:49 +08:00
|
|
|
import messageHistory from '../util/messageHistory';
|
2023-05-10 15:57:02 +08:00
|
|
|
import CustomCommands from '../command/customCommand';
|
2023-05-05 21:27:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
// Add this function to messageHandler.ts
|
|
|
|
function parseMessage(message: string): { context: string[]; instruction: string[]; reference: string[]; text: string } {
|
|
|
|
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 };
|
|
|
|
}
|
|
|
|
|
|
|
|
function getInstructionFiles(): string[] {
|
|
|
|
const instructionFiles: string[] = [];
|
2023-05-10 15:57:02 +08:00
|
|
|
|
|
|
|
const customCommands = CustomCommands.getInstance().getCommands();
|
|
|
|
// visit customCommands, get default command
|
|
|
|
for (const command of customCommands) {
|
|
|
|
if (command.default) {
|
|
|
|
for (const instruction of command.instructions) {
|
|
|
|
instructionFiles.push(`./.chat/workflows/${command.name}/${instruction}`);
|
2023-05-05 21:27:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-10 15:57:02 +08:00
|
|
|
|
2023-05-05 21:27:40 +08:00
|
|
|
return instructionFiles;
|
|
|
|
}
|
|
|
|
|
2023-05-09 14:08:18 +08:00
|
|
|
const devChat = new DevChat();
|
2023-05-05 21:27:40 +08:00
|
|
|
|
2023-05-09 16:05:49 +08:00
|
|
|
|
|
|
|
// message: { command: 'sendMessage', text: 'xxx', hash: 'xxx'}
|
2023-05-09 13:24:33 +08:00
|
|
|
// return message:
|
|
|
|
// { command: 'receiveMessage', text: 'xxxx', hash: 'xxx', user: 'xxx', date: 'xxx'}
|
|
|
|
// { command: 'receiveMessagePartial', text: 'xxxx', user: 'xxx', date: 'xxx'}
|
2023-05-08 12:09:52 +08:00
|
|
|
export async function sendMessage(message: any, panel: vscode.WebviewPanel): Promise<void> {
|
2023-05-05 21:27:40 +08:00
|
|
|
const newText2 = await CommandManager.getInstance().processText(message.text);
|
|
|
|
const parsedMessage = parseMessage(newText2);
|
2023-05-09 13:24:33 +08:00
|
|
|
const chatOptions: any = {};
|
|
|
|
|
2023-05-09 16:05:49 +08:00
|
|
|
let parent_hash = undefined;
|
|
|
|
logger.channel()?.info(`request message hash: ${message.hash}`)
|
|
|
|
if (message.hash) {
|
|
|
|
const hmessage = messageHistory.find(panel, message.hash);
|
|
|
|
parent_hash = hmessage ? message.parent_hash : undefined;
|
|
|
|
} else {
|
|
|
|
const hmessage = messageHistory.findLast(panel);
|
|
|
|
parent_hash = hmessage ? hmessage.hash : undefined;
|
|
|
|
}
|
|
|
|
if (parent_hash) {
|
|
|
|
chatOptions.parent = parent_hash;
|
2023-05-09 13:24:33 +08:00
|
|
|
}
|
2023-05-09 16:05:49 +08:00
|
|
|
logger.channel()?.info(`parent hash: ${parent_hash}`);
|
2023-05-05 21:27:40 +08:00
|
|
|
|
|
|
|
if (parsedMessage.context.length > 0) {
|
|
|
|
chatOptions.context = parsedMessage.context;
|
|
|
|
}
|
|
|
|
|
|
|
|
chatOptions.header = getInstructionFiles();
|
|
|
|
if (parsedMessage.instruction.length > 0) {
|
|
|
|
chatOptions.header = parsedMessage.instruction;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parsedMessage.reference.length > 0) {
|
|
|
|
chatOptions.reference = parsedMessage.reference;
|
|
|
|
}
|
|
|
|
|
2023-05-09 13:24:33 +08:00
|
|
|
const onData = (partialResponse: ChatResponse) => {
|
|
|
|
MessageHandler.sendMessage(panel, { command: 'receiveMessagePartial', text: partialResponse.response, user: partialResponse.user, date: partialResponse.date }, false);
|
2023-05-05 21:27:40 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const chatResponse = await devChat.chat(parsedMessage.text, chatOptions, onData);
|
2023-05-09 13:24:33 +08:00
|
|
|
|
2023-05-09 16:05:49 +08:00
|
|
|
if (!chatResponse.isError) {
|
|
|
|
messageHistory.add(panel, {request: message.text, text: parsedMessage.text, parent_hash, hash: chatResponse['prompt-hash'], user: chatResponse.user, date: chatResponse.date });
|
|
|
|
}
|
|
|
|
|
2023-05-09 13:24:33 +08:00
|
|
|
MessageHandler.sendMessage(panel, { command: 'receiveMessage', text: chatResponse.response, hash: chatResponse['prompt-hash'], user: chatResponse.user, date: chatResponse.date, isError: chatResponse.isError });
|
2023-05-05 21:27:40 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-09 14:08:18 +08:00
|
|
|
export async function stopDevChat(message: any, panel: vscode.WebviewPanel): Promise<void> {
|
|
|
|
logger.channel()?.info(`Stopping devchat`);
|
|
|
|
devChat.stop();
|
|
|
|
}
|
|
|
|
|
2023-05-08 12:09:52 +08:00
|
|
|
|
2023-05-05 21:27:40 +08:00
|
|
|
|