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.
This commit is contained in:
bobo.yang 2023-07-24 00:11:56 +08:00
parent dbdf67d19f
commit 31fea27cf6
2 changed files with 59 additions and 1 deletions

View File

@ -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

View File

@ -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<CommandResult> {
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}`};
}
}
};