commit
cfe6a4fa50
@ -1,6 +1,7 @@
|
||||
|
||||
import * as vscode from 'vscode';
|
||||
import * as path from 'path';
|
||||
import * as fs from 'fs';
|
||||
import { createTempSubdirectory, getLanguageIdByFileName } from '../util/commonUtil';
|
||||
|
||||
export async function handleFileSelected(fileSelected: string) {
|
||||
@ -12,8 +13,7 @@ export async function handleFileSelected(fileSelected: string) {
|
||||
const tempFile = path.join(tempDir, fileName);
|
||||
|
||||
// load content in fileSelected
|
||||
const fileContent = await vscode.workspace.fs.readFile(vscode.Uri.file(fileSelected));
|
||||
|
||||
const fileContent = fs.readFileSync(fileSelected, 'utf-8');
|
||||
// get the language from fileSelected
|
||||
const languageId = await getLanguageIdByFileName(fileSelected);
|
||||
|
||||
|
@ -3,12 +3,15 @@ import * as vscode from 'vscode';
|
||||
import { handleCodeSelected } from '../context/contextCodeSelected';
|
||||
import { handleFileSelected } from '../context/contextFileSelected';
|
||||
import { MessageHandler } from '../handler/messageHandler';
|
||||
import { regInMessage, regOutMessage } from '../util/reg_messages';
|
||||
|
||||
regOutMessage({command: 'appendContext', context: ''});
|
||||
export async function sendFileSelectMessage(panel: vscode.WebviewPanel, filePath: string): Promise<void> {
|
||||
const codeContext = await handleFileSelected(filePath);
|
||||
MessageHandler.sendMessage(panel, { command: 'appendContext', context: codeContext });
|
||||
}
|
||||
|
||||
regOutMessage({command: 'appendContext', context: ''});
|
||||
export async function sendCodeSelectMessage(panel: vscode.WebviewPanel, filePath: string, codeBlock: string): Promise<void> {
|
||||
const codeContext = await handleCodeSelected(filePath, codeBlock);
|
||||
MessageHandler.sendMessage(panel, { command: 'appendContext', context: codeContext });
|
||||
|
@ -1,6 +1,6 @@
|
||||
import * as vscode from 'vscode';
|
||||
|
||||
import { createChatDirectoryAndCopyInstructionsSync } from './init/chatConfig';
|
||||
|
||||
import {
|
||||
registerOpenChatPanelCommand,
|
||||
registerAddContextCommand,
|
||||
@ -16,9 +16,6 @@ function activate(context: vscode.ExtensionContext) {
|
||||
ExtensionContextHolder.context = context;
|
||||
logger.init(context);
|
||||
|
||||
// 创建 .chat 目录并复制 workflows
|
||||
createChatDirectoryAndCopyInstructionsSync(context.extensionUri);
|
||||
|
||||
registerOpenChatPanelCommand(context);
|
||||
registerAddContextCommand(context);
|
||||
registerAskForCodeCommand(context);
|
||||
|
@ -2,7 +2,11 @@ import * as vscode from 'vscode';
|
||||
import ChatContextManager from '../context/contextManager';
|
||||
|
||||
import { MessageHandler } from './messageHandler';
|
||||
import { regInMessage, regOutMessage } from '../util/reg_messages';
|
||||
|
||||
|
||||
regInMessage({command: 'addContext', selected: ''});
|
||||
regOutMessage({command: 'appendContext', context: ''});
|
||||
export async function addConext(message: any, panel: vscode.WebviewPanel): Promise<void> {
|
||||
const contextStr = await ChatContextManager.getInstance().processText(message.selected);
|
||||
MessageHandler.sendMessage(panel, { command: 'appendContext', context: contextStr });
|
||||
|
@ -1,7 +1,11 @@
|
||||
import * as vscode from 'vscode';
|
||||
import { handleRefCommand } from '../context/contextRef';
|
||||
import { MessageHandler } from './messageHandler';
|
||||
import { regInMessage, regOutMessage } from '../util/reg_messages';
|
||||
|
||||
|
||||
regInMessage({command: 'addRefCommandContext', refCommand: ''});
|
||||
regOutMessage({command: 'appendContext', context: ''});
|
||||
// message: { command: 'addRefCommandContext', refCommand: string }
|
||||
// User input: /ref ls . then "ls ." will be passed to refCommand
|
||||
export async function addRefCommandContext(message: any, panel: vscode.WebviewPanel): Promise<void> {
|
||||
|
@ -1,4 +1,5 @@
|
||||
import * as vscode from 'vscode';
|
||||
import { regInMessage, regOutMessage } from '../util/reg_messages';
|
||||
|
||||
|
||||
export async function applyCode(text: string) {
|
||||
@ -24,6 +25,7 @@ export async function applyCode(text: string) {
|
||||
}
|
||||
|
||||
|
||||
regInMessage({command: 'code_apply', content: ''});
|
||||
export async function codeApply(message: any, panel: vscode.WebviewPanel): Promise<void> {
|
||||
await applyCode(message.content);
|
||||
return;
|
||||
|
@ -1,4 +1,5 @@
|
||||
import * as vscode from 'vscode';
|
||||
import { regInMessage, regOutMessage } from '../util/reg_messages';
|
||||
|
||||
|
||||
async function replaceFileContent(uri: vscode.Uri, newContent: string) {
|
||||
@ -58,6 +59,7 @@ export async function applyCodeFile(text: string, fileName: string): Promise<voi
|
||||
});
|
||||
}
|
||||
|
||||
regInMessage({command: 'code_file_apply', content: '', fileName: ''});
|
||||
export async function codeFileApply(message: any, panel: vscode.WebviewPanel): Promise<void> {
|
||||
await applyCodeFile(message.content, message.fileName);
|
||||
return;
|
||||
|
@ -1,7 +1,11 @@
|
||||
import * as vscode from 'vscode';
|
||||
import * as fs from 'fs';
|
||||
import { MessageHandler } from './messageHandler';
|
||||
import { regInMessage, regOutMessage } from '../util/reg_messages';
|
||||
|
||||
|
||||
regInMessage({command: 'contextDetail', file: ''});
|
||||
regOutMessage({command: 'contextDetailResponse', file: '', result: ''});
|
||||
// message: { command: 'contextDetail', file: string }
|
||||
// read detail context information from file
|
||||
// return json string
|
||||
|
@ -1,7 +1,11 @@
|
||||
import * as vscode from 'vscode';
|
||||
import CommandManager from '../command/commandManager';
|
||||
import { MessageHandler } from './messageHandler';
|
||||
import { regInMessage, regOutMessage } from '../util/reg_messages';
|
||||
|
||||
|
||||
regInMessage({command: 'convertCommand', text: ''});
|
||||
regOutMessage({command: 'convertCommand', result: ''});
|
||||
export async function convertCommand(message: any, panel: vscode.WebviewPanel): Promise<void> {
|
||||
const newText = await CommandManager.getInstance().processText(message.text);
|
||||
MessageHandler.sendMessage(panel, { command: 'convertCommand', result: newText });
|
||||
|
@ -1,7 +1,9 @@
|
||||
import * as vscode from 'vscode';
|
||||
import DtmWrapper from '../toolwrapper/dtm';
|
||||
import { regInMessage, regOutMessage } from '../util/reg_messages';
|
||||
|
||||
|
||||
regInMessage({command: 'doCommit', content: ''});
|
||||
export async function doCommit(message: any, panel: vscode.WebviewPanel): Promise<void> {
|
||||
const dtmWrapper = new DtmWrapper();
|
||||
|
||||
|
@ -2,12 +2,16 @@ import * as vscode from 'vscode';
|
||||
import DevChat, { LogOptions, LogEntry } from '../toolwrapper/devchat';
|
||||
import { MessageHandler } from './messageHandler';
|
||||
import messageHistory from '../util/messageHistory';
|
||||
import { regInMessage, regOutMessage } from '../util/reg_messages';
|
||||
|
||||
|
||||
interface LoadHistoryMessages {
|
||||
command: string;
|
||||
entries: Array<LogEntry>;
|
||||
}
|
||||
|
||||
regInMessage({command: 'historyMessages', options: { skip: 0, maxCount: 0 }});
|
||||
regOutMessage({command: 'loadHistoryMessages', entries: [{hash: '',user: '',date: '',request: '',response: '',context: [{content: '',role: ''}]}]});
|
||||
export async function historyMessages(message: any, panel: vscode.WebviewPanel): Promise<void> {
|
||||
const devChat = new DevChat();
|
||||
|
||||
|
12
src/handler/listMessages.ts
Normal file
12
src/handler/listMessages.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import * as vscode from 'vscode';
|
||||
import { getInMessages, getOutMessages } from '../util/reg_messages';
|
||||
import { MessageHandler } from './messageHandler';
|
||||
|
||||
export async function listAllMessages(message: any, panel: vscode.WebviewPanel): Promise<void> {
|
||||
const inMessages = getInMessages()
|
||||
const outMessages = getOutMessages()
|
||||
|
||||
MessageHandler.sendMessage(panel, { command: 'InMessage', result: inMessages });
|
||||
MessageHandler.sendMessage(panel, { command: 'OutMessage', result: outMessages });
|
||||
return;
|
||||
}
|
@ -12,6 +12,7 @@ import { showDiff } from './showDiff';
|
||||
import { addConext } from './addContext';
|
||||
import { addRefCommandContext } from './addRefCommandContext';
|
||||
import { contextDetail } from './contextDetail';
|
||||
import { listAllMessages } from './listMessages';
|
||||
|
||||
|
||||
// 根据用户选择的context菜单,添加对应的context文件
|
||||
@ -61,4 +62,6 @@ messageHandler.registerHandler('addRefCommandContext', addRefCommandContext);
|
||||
// 应答消息: { command: 'contextDetailResponse', 'file':<context file>, result: <context file content> }
|
||||
// <context file content>是一个JSON字符串
|
||||
messageHandler.registerHandler('contextDetail', contextDetail);
|
||||
// Debug handler
|
||||
messageHandler.registerHandler('listAllMessages', listAllMessages);
|
||||
|
||||
|
@ -18,6 +18,19 @@ export class MessageHandler {
|
||||
}
|
||||
|
||||
async handleMessage(message: any, panel: vscode.WebviewPanel): Promise<void> {
|
||||
let isNeedSendResponse = false;
|
||||
if (message.command === 'sendMessage') {
|
||||
try {
|
||||
const messageText = message.text;
|
||||
const messageObject = JSON.parse(messageText);
|
||||
if (messageObject && messageObject.user && messageObject.user == 'merico-devchat') {
|
||||
message = messageObject;
|
||||
isNeedSendResponse = true;
|
||||
}
|
||||
} catch (e) {
|
||||
}
|
||||
}
|
||||
|
||||
const handler = this.handlers[message.command];
|
||||
if (handler) {
|
||||
logger.channel()?.info(`Handling command "${message.command}"`);
|
||||
@ -27,6 +40,10 @@ export class MessageHandler {
|
||||
logger.channel()?.error(`No handler found for command "${message.command}"`);
|
||||
logger.channel()?.show();
|
||||
}
|
||||
|
||||
if (isNeedSendResponse) {
|
||||
MessageHandler.sendMessage(panel, { command: 'receiveMessage', text: 'finish', hash: '', user: '', date: 1, isError: false });
|
||||
}
|
||||
}
|
||||
|
||||
public static sendMessage(panel: vscode.WebviewPanel, message: object, log: boolean = true): void {
|
||||
|
@ -1,7 +1,11 @@
|
||||
import * as vscode from 'vscode';
|
||||
import CommandManager from '../command/commandManager';
|
||||
import { MessageHandler } from './messageHandler';
|
||||
import { regInMessage, regOutMessage } from '../util/reg_messages';
|
||||
|
||||
|
||||
regInMessage({command: 'regCommandList'});
|
||||
regOutMessage({command: 'regCommandList', result: [{name: '', pattern: '', description: ''}]});
|
||||
export async function regCommandList(message: any, panel: vscode.WebviewPanel): Promise<void> {
|
||||
const commandList = CommandManager.getInstance().getCommandList();
|
||||
MessageHandler.sendMessage(panel, { command: 'regCommandList', result: commandList });
|
||||
|
@ -1,7 +1,12 @@
|
||||
import * as vscode from 'vscode';
|
||||
import ChatContextManager from '../context/contextManager';
|
||||
import { MessageHandler } from './messageHandler';
|
||||
import { regInMessage, regOutMessage } from '../util/reg_messages';
|
||||
|
||||
|
||||
|
||||
regInMessage({command: 'regContextList'});
|
||||
regOutMessage({command: 'regContextList', result: [{name: '', description: ''}]});
|
||||
export async function regContextList(message: any, panel: vscode.WebviewPanel): Promise<void> {
|
||||
const contextList = ChatContextManager.getInstance().getContextList();
|
||||
MessageHandler.sendMessage(panel, { command: 'regContextList', result: contextList });
|
||||
|
@ -8,6 +8,8 @@ import { logger } from '../util/logger';
|
||||
import { MessageHandler } from './messageHandler';
|
||||
import messageHistory from '../util/messageHistory';
|
||||
import CustomCommands from '../command/customCommand';
|
||||
import { regInMessage, regOutMessage } from '../util/reg_messages';
|
||||
|
||||
|
||||
|
||||
// Add this function to messageHandler.ts
|
||||
@ -66,6 +68,9 @@ function getInstructionFiles(): string[] {
|
||||
const devChat = new DevChat();
|
||||
|
||||
|
||||
regInMessage({command: 'sendMessage', text: '', hash: undefined});
|
||||
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'}
|
||||
@ -116,6 +121,7 @@ export async function sendMessage(message: any, panel: vscode.WebviewPanel): Pro
|
||||
return;
|
||||
}
|
||||
|
||||
regInMessage({command: 'stopDevChat'});
|
||||
export async function stopDevChat(message: any, panel: vscode.WebviewPanel): Promise<void> {
|
||||
logger.channel()?.info(`Stopping devchat`);
|
||||
devChat.stop();
|
||||
|
@ -1,6 +1,8 @@
|
||||
import * as vscode from 'vscode';
|
||||
import * as path from 'path';
|
||||
import { createTempSubdirectory } from '../util/commonUtil';
|
||||
import { regInMessage, regOutMessage } from '../util/reg_messages';
|
||||
|
||||
|
||||
|
||||
export async function diffView(code: string) {
|
||||
@ -47,11 +49,13 @@ export async function diffView(code: string) {
|
||||
}
|
||||
|
||||
|
||||
regInMessage({command: 'show_diff', content: ''});
|
||||
export async function showDiff(message: any, panel: vscode.WebviewPanel): Promise<void> {
|
||||
diffView(message.content);
|
||||
return;
|
||||
}
|
||||
|
||||
regInMessage({command: 'block_apply', content: ''});
|
||||
export async function blockApply(message: any, panel: vscode.WebviewPanel): Promise<void> {
|
||||
diffView(message.content);
|
||||
return;
|
||||
|
@ -9,6 +9,7 @@ import WebviewManager from './webviewManager';
|
||||
import messageHistory from '../util/messageHistory';
|
||||
import CustomCommands from '../command/customCommand';
|
||||
import CommandManager from '../command/commandManager';
|
||||
import { createChatDirectoryAndCopyInstructionsSync } from '../init/chatConfig';
|
||||
|
||||
export default class ChatPanel {
|
||||
private static _instance: ChatPanel | undefined;
|
||||
@ -17,6 +18,9 @@ export default class ChatPanel {
|
||||
private _disposables: vscode.Disposable[] = [];
|
||||
|
||||
public static createOrShow(extensionUri: vscode.Uri) {
|
||||
// 创建 .chat 目录并复制 workflows
|
||||
createChatDirectoryAndCopyInstructionsSync(extensionUri);
|
||||
|
||||
const workspaceDir = vscode.workspace.workspaceFolders?.[0].uri.fsPath;
|
||||
if (workspaceDir) {
|
||||
const workflowsDir = path.join(workspaceDir!, '.chat', 'workflows');
|
||||
|
19
src/util/reg_messages.ts
Normal file
19
src/util/reg_messages.ts
Normal file
@ -0,0 +1,19 @@
|
||||
|
||||
let inMessages: object[] = [];
|
||||
let outMessages: object[] = [];
|
||||
|
||||
export function regInMessage(message: object) {
|
||||
inMessages.push(message);
|
||||
}
|
||||
|
||||
export function regOutMessage(message: object) {
|
||||
outMessages.push(message);
|
||||
}
|
||||
|
||||
export function getInMessages(): object[] {
|
||||
return inMessages;
|
||||
}
|
||||
|
||||
export function getOutMessages(): object[] {
|
||||
return outMessages;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user