2023-05-05 21:27:40 +08:00
|
|
|
|
|
|
|
import * as vscode from 'vscode';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
|
|
|
import DevChat from '../toolwrapper/devchat';
|
|
|
|
import CommandManager from '../command/commandManager';
|
2023-05-09 10:34:33 +08:00
|
|
|
import { logger } from '../util/logger';
|
|
|
|
import { MessageHandler } from './messageHandler';
|
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[] = [];
|
|
|
|
const workspaceDir = vscode.workspace.workspaceFolders?.[0].uri.fsPath;
|
|
|
|
if (workspaceDir) {
|
|
|
|
const chatInstructionsPath = path.join(workspaceDir, '.chat', 'instructions', 'default');
|
|
|
|
try {
|
|
|
|
// 读取 chatInstructionsPath 目录下的所有文件和目录
|
|
|
|
const files = fs.readdirSync(chatInstructionsPath);
|
|
|
|
// 过滤出文件,忽略目录
|
|
|
|
for (const file of files) {
|
|
|
|
const filePath = path.join(chatInstructionsPath, file);
|
|
|
|
const fileStats = fs.statSync(filePath);
|
|
|
|
if (fileStats.isFile()) {
|
|
|
|
instructionFiles.push(filePath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2023-05-09 10:34:33 +08:00
|
|
|
logger.channel()?.error(`Error reading instruction files: ${error}`);
|
|
|
|
logger.channel()?.show();
|
2023-05-05 21:27:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return instructionFiles;
|
|
|
|
}
|
|
|
|
|
2023-05-08 16:54:47 +08:00
|
|
|
|
2023-05-05 21:27:40 +08:00
|
|
|
let lastPromptHash: string | undefined;
|
|
|
|
|
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 devChat = new DevChat();
|
|
|
|
|
|
|
|
const newText2 = await CommandManager.getInstance().processText(message.text);
|
|
|
|
const parsedMessage = parseMessage(newText2);
|
|
|
|
const chatOptions: any = lastPromptHash ? { parent: lastPromptHash } : {};
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
let partialData = "";
|
|
|
|
const onData = (partialResponse: string) => {
|
|
|
|
partialData += partialResponse;
|
2023-05-09 10:34:33 +08:00
|
|
|
MessageHandler.sendMessage(panel, { command: 'receiveMessagePartial', text: partialData }, false);
|
2023-05-05 21:27:40 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const chatResponse = await devChat.chat(parsedMessage.text, chatOptions, onData);
|
2023-05-08 16:54:47 +08:00
|
|
|
if (chatResponse && typeof chatResponse === 'object' && !Array.isArray(chatResponse) && !(chatResponse instanceof String)) {
|
|
|
|
// 检查 "prompt-hash" 是否在 chatResponse 中
|
|
|
|
if ('prompt-hash' in chatResponse) {
|
|
|
|
// 检查 chatResponse['prompt-hash'] 是否不为空
|
|
|
|
if (chatResponse['prompt-hash']) {
|
|
|
|
// 如果 "prompt-hash" 在 chatResponse 中且不为空,则更新 lastPromptHash 的值
|
|
|
|
lastPromptHash = chatResponse['prompt-hash'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-05 21:27:40 +08:00
|
|
|
const response = chatResponse.response;
|
2023-05-09 10:34:33 +08:00
|
|
|
MessageHandler.sendMessage(panel, { command: 'receiveMessage', text: response });
|
2023-05-05 21:27:40 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-08 12:09:52 +08:00
|
|
|
|
2023-05-05 21:27:40 +08:00
|
|
|
|