diff --git a/src/handler/messageHandler.ts b/src/handler/messageHandler.ts index b73ca05..a4ddb2e 100644 --- a/src/handler/messageHandler.ts +++ b/src/handler/messageHandler.ts @@ -8,7 +8,10 @@ import { logger } from '../util/logger'; import { isWaitForApiKey } from './historyMessagesBase'; import { onApiKey } from './historyMessages'; import { ApiKeyManager } from '../util/apiKey'; +import { regeneration, sendMessage as sendMessageX } from './sendMessage'; +import { codeFileApply } from './codeFileApply'; +let autox = false; export class MessageHandler { private handlers: { [command: string]: (message: any, panel: vscode.WebviewPanel|vscode.WebviewView) => Promise } = {}; @@ -42,6 +45,14 @@ export class MessageHandler { } } + autox = false; + if (message.command === 'sendMessage') { + // if "/autox" in message.text, then flag global variable autox to true + if (message.text.indexOf('/autox') !== -1) { + autox = true; + } + } + const handler = this.handlers[message.command]; if (handler) { logger.channel()?.info(`Handling the command "${message.command}"`); @@ -57,11 +68,54 @@ export class MessageHandler { } } - public static sendMessage(panel: vscode.WebviewPanel|vscode.WebviewView, message: object, log: boolean = true): void { + public static sendMessage(panel: vscode.WebviewPanel|vscode.WebviewView, message : any, log: boolean = true): void { if (log) { logger.channel()?.info(`Sending message: "${JSON.stringify(message)}"`); } + panel.webview.postMessage(message); + + if (message.command === 'receiveMessage' && autox) { + // if message.isError is true, then regenerate message + if (message.isError) { + regeneration({}, panel); + } else { + // if message.text is ```command\n {xxx} ``` then get xxx + const messageText = message.text; + // if messageText match "```command\n ... ```", then parse block content + const reg = /```command\n([\s\S]*)```/; + const match = messageText.match(reg); + if (match) { + const command = match[1]; + try { + const commandObject = JSON.parse(command); + if (!(commandObject && commandObject.name)) { + logger.channel()?.error(`${command} is not a valid command`); + logger.channel()?.show(); + return ; + } + + if (commandObject.name === "fail_task" || commandObject.name === "finish_task") { + logger.channel()?.info(`Task has finished.`); + logger.channel()?.show(); + return ; + } + + codeFileApply({"content": command, "fileName": ""}, panel); + + } catch (e) { + logger.channel()?.error(`parse ${command} error: ${e}`); + logger.channel()?.show(); + + MessageHandler.sendMessage(panel, { "command": "systemMessage", "text": "continue. 并且确认你在围绕最初的任务在执行相关步骤。" }); + sendMessageX({command: 'sendMessage', text: "continue"}, panel); + } + } else { + MessageHandler.sendMessage(panel, { "command": "systemMessage", "text": "continue. 并且确认你在围绕最初的任务在执行相关步骤。" }); + sendMessageX({command: 'sendMessage', text: "continue"}, panel); + } + } + } } }