From 31fea27cf6ff77a127acc8fa3980816f539f3673 Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Mon, 24 Jul 2023 00:11:56 +0800 Subject: [PATCH] Add AskInputAction and update ActionManager - Created a new action class AskInputAction in askInputAction.ts. - This action prompts the user for input with a specified question. - Updated ActionManager to register the new AskInputAction. - Added error handling in the actionInstruction method of ActionManager. --- src/action/actionManager.ts | 12 ++++++++- src/action/askInputAction.ts | 48 ++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/action/askInputAction.ts diff --git a/src/action/actionManager.ts b/src/action/actionManager.ts index 31c4b6b..47b8989 100644 --- a/src/action/actionManager.ts +++ b/src/action/actionManager.ts @@ -7,6 +7,7 @@ import { logger } from '../util/logger'; import { SymbolRefAction } from './symbolRefAction'; import { SymbolDefAction } from './symbolDefAction'; +import { AskInputAction } from './askInputAction'; // extend Action @@ -56,6 +57,7 @@ export default class ActionManager { ActionManager.instance.registerAction(new CommandRunAction()); ActionManager.instance.registerAction(new SymbolRefAction()); ActionManager.instance.registerAction(new SymbolDefAction()); + ActionManager.instance.registerAction(new AskInputAction()); return ActionManager.instance; } @@ -170,7 +172,15 @@ export default class ActionManager { public actionInstruction(): string { let functionsDefList = [] for (const action of this.actions) { - functionsDefList.push(getActionInstruction(action)); + try { + if (action.name === "command_run") { + continue; + } + functionsDefList.push(getActionInstruction(action)); + } catch (error) { + logger.channel()?.error(`Failed to get action instruction: ${error}`); + logger.channel()?.show(); + } } // return as json string diff --git a/src/action/askInputAction.ts b/src/action/askInputAction.ts new file mode 100644 index 0000000..064af80 --- /dev/null +++ b/src/action/askInputAction.ts @@ -0,0 +1,48 @@ + +import { Action, CustomActions } from './customAction'; + +import { CommandResult } from '../util/commonUtil'; +import { logger } from '../util/logger'; +import { UiUtilVscode } from '../util/uiUtil_vscode'; +import { UiUtilWrapper } from '../util/uiUtil'; + + +export class AskInputAction implements Action { + name: string; + description: string; + type: string[]; + action: string; + handler: string[]; + args: { "name": string, "description": string, "type": string, "as"?: string, "from": string }[]; + + constructor() { + this.name = 'ask_input'; + this.description = 'Ask user a question to when you need the user to input something'; + this.type = ['question']; + this.action = 'ask_input'; + this.handler = []; + this.args = [ + {"name": "question", "description": "The question you asked.", "type": "string", "from": "content.content.question"}, + ]; + } + + async handlerAction(args: {[key: string]: any}): Promise { + try { + const question = args.question; + + const answer: string | undefined = await UiUtilWrapper.showInputBox({ + title: question, + placeHolder: "Please input your answer here." + }); + if (answer === undefined) { + return {exitCode: -1, stdout: '', stderr: ``}; + } else { + return {exitCode: 0, stdout: answer, stderr: ""}; + } + } catch (error) { + logger.channel()?.error(`${this.name} handle error: ${error}`); + logger.channel()?.show(); + return {exitCode: -1, stdout: '', stderr: `${this.name} handle error: ${error}`}; + } + } +}; \ No newline at end of file