117 lines
4.2 KiB
TypeScript
Raw Normal View History

2023-05-05 21:27:40 +08:00
import * as vscode from 'vscode';
2023-05-09 10:34:33 +08:00
import { MessageHandler } from './messageHandler';
2023-05-10 17:56:56 +08:00
import { regInMessage, regOutMessage } from '../util/reg_messages';
import { stopDevChatBase, sendMessageBase, deleteChatMessageBase } from './sendMessageBase';
import { UiUtilWrapper } from '../util/uiUtil';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
2023-05-10 17:56:56 +08:00
2023-05-05 21:27:40 +08:00
let _lastMessage: any = undefined;
export function createTempFile(content: string): string {
// Generate a unique file name
const fileName = path.join(os.tmpdir(), `temp_${Date.now()}.txt`);
// Write the content to the file
fs.writeFileSync(fileName, content);
return fileName;
}
export function deleteTempFiles(fileName: string): void {
// Delete the file
fs.unlinkSync(fileName);
}
regInMessage({command: 'sendMessage', text: '', parent_hash: undefined});
2023-05-10 17:56:56 +08:00
regOutMessage({ command: 'receiveMessage', text: 'xxxx', hash: 'xxx', user: 'xxx', date: 'xxx'});
regOutMessage({ command: 'receiveMessagePartial', text: 'xxxx', user: 'xxx', date: 'xxx'});
// message: { command: 'sendMessage', text: 'xxx', hash: 'xxx'}
// return message:
// { command: 'receiveMessage', text: 'xxxx', hash: 'xxx', user: 'xxx', date: 'xxx'}
// { command: 'receiveMessagePartial', text: 'xxxx', user: 'xxx', date: 'xxx'}
2023-07-24 00:11:56 +08:00
export async function sendMessage(message: any, panel: vscode.WebviewPanel|vscode.WebviewView, function_name: string|undefined = undefined): Promise<void> {
_lastMessage = [message, function_name];
// Add a new field to store the names of temporary files
let tempFiles: string[] = [];
// Handle the contextInfo field in the message
if (Array.isArray(message.contextInfo)) {
for (let context of message.contextInfo) {
if (typeof context === 'object' && context !== null && 'context' in context) {
// If the file name is not present, create a temporary file
if (!context.file) {
try {
const contextStr = JSON.stringify(context.context);
context.file = createTempFile(contextStr);
// Add the file name to the tempFiles array
tempFiles.push(context.file);
} catch (err) {
console.error('Failed to create temporary file:', err);
throw err;
}
}
// Insert the file name into the text field
message.text += ` [context|${context.file}]`;
}
}
}
// clear message.contextInfo
message.contextInfo = undefined;
const responseMessage = await sendMessageBase(message, (data: { command: string, text: string, user: string, date: string}) => {
MessageHandler.sendMessage(panel, data, false);
}, function_name);
if (responseMessage) {
MessageHandler.sendMessage(panel, responseMessage);
}
// Delete all temporary files created
for (let file of tempFiles) {
deleteTempFiles(file);
}
2023-05-05 21:27:40 +08:00
}
2023-05-23 11:16:48 +08:00
// regeneration last message again
regInMessage({command: 'regeneration'});
export async function regeneration(message: any, panel: vscode.WebviewPanel|vscode.WebviewView): Promise<void> {
// call sendMessage to send last message again
if (_lastMessage) {
2023-07-24 00:11:56 +08:00
sendMessage(_lastMessage[0], panel, _lastMessage[1]);
2023-05-23 11:16:48 +08:00
}
}
2023-05-10 17:56:56 +08:00
regInMessage({command: 'stopDevChat'});
2023-05-16 14:35:01 +08:00
export async function stopDevChat(message: any, panel: vscode.WebviewPanel|vscode.WebviewView): Promise<void> {
stopDevChatBase(message);
}
regInMessage({command: 'deleteChatMessage', hash: 'xxx'});
regOutMessage({ command: 'deletedChatMessage', hash: 'xxxx'});
export async function deleteChatMessage(message: any, panel: vscode.WebviewPanel|vscode.WebviewView): Promise<void> {
// prompt user to confirm
const confirm = await vscode.window.showWarningMessage(
`Are you sure to delete this message?`,
{ modal: true },
'Delete'
);
if (confirm !== 'Delete') {
return;
}
const deleted = await deleteChatMessageBase(message);
if (deleted) {
MessageHandler.sendMessage(panel, { command: 'deletedChatMessage', hash: message.hash });
} else {
UiUtilWrapper.showErrorMessage('Delete message failed!');
}
}
2023-05-08 12:09:52 +08:00
2023-05-05 21:27:40 +08:00