Merge pull request #6 from covespace/support_prompt_template
Support commands extension
This commit is contained in:
commit
99e75d8f46
48
src/commandManager.ts
Normal file
48
src/commandManager.ts
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
export interface Command {
|
||||||
|
name: string;
|
||||||
|
pattern: string;
|
||||||
|
description: string;
|
||||||
|
handler: (userInput: string) => string;
|
||||||
|
}
|
||||||
|
|
||||||
|
class CommandManager {
|
||||||
|
private static instance: CommandManager;
|
||||||
|
private commands: Command[] = [];
|
||||||
|
|
||||||
|
private constructor() {}
|
||||||
|
|
||||||
|
public static getInstance(): CommandManager {
|
||||||
|
if (!CommandManager.instance) {
|
||||||
|
CommandManager.instance = new CommandManager();
|
||||||
|
}
|
||||||
|
|
||||||
|
return CommandManager.instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
registerCommand(command: Command): void {
|
||||||
|
this.commands.push(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
getCommandList(): Command[] {
|
||||||
|
return this.commands;
|
||||||
|
}
|
||||||
|
|
||||||
|
processText(text: string): string {
|
||||||
|
let result = text;
|
||||||
|
|
||||||
|
this.commands.forEach((commandObj) => {
|
||||||
|
const commandPattern = new RegExp(
|
||||||
|
`\\/(${commandObj.pattern.replace("{{prompt}}", "(.+?)")})`,
|
||||||
|
"g"
|
||||||
|
);
|
||||||
|
|
||||||
|
result = result.replace(commandPattern, (_, matchedUserInput) => {
|
||||||
|
return commandObj.handler(matchedUserInput);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default CommandManager;
|
10
src/exampleCommand1.ts
Normal file
10
src/exampleCommand1.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import {Command} from './commandManager';
|
||||||
|
|
||||||
|
export const exampleCommand1: Command = {
|
||||||
|
name: 'exampleCommand1',
|
||||||
|
pattern: 'example: command1 {{prompt}}',
|
||||||
|
description: '这是一个示例命令1',
|
||||||
|
handler: (userInput: string) => {
|
||||||
|
return `示例命令1处理了以下文本:${userInput}`;
|
||||||
|
},
|
||||||
|
};
|
10
src/exampleCommand2.ts
Normal file
10
src/exampleCommand2.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import {Command} from './commandManager';
|
||||||
|
|
||||||
|
export const exampleCommand2: Command = {
|
||||||
|
name: 'exampleCommand2',
|
||||||
|
pattern: 'example: command2 {{prompt}}',
|
||||||
|
description: '这是一个示例命令2',
|
||||||
|
handler: (userInput: string) => {
|
||||||
|
return `示例命令2处理了以下文本:${userInput}`;
|
||||||
|
},
|
||||||
|
};
|
9
src/loadCommands.ts
Normal file
9
src/loadCommands.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import CommandManager from './commandManager';
|
||||||
|
import { exampleCommand1 } from './exampleCommand1';
|
||||||
|
import { exampleCommand2 } from './exampleCommand2';
|
||||||
|
|
||||||
|
const commandManager = CommandManager.getInstance();
|
||||||
|
|
||||||
|
// 注册命令
|
||||||
|
commandManager.registerCommand(exampleCommand1);
|
||||||
|
commandManager.registerCommand(exampleCommand2);
|
@ -7,6 +7,10 @@ import { promisify } from 'util';
|
|||||||
import DevChat, { LogOptions } from './devchat';
|
import DevChat, { LogOptions } from './devchat';
|
||||||
import DtmWrapper from './dtm';
|
import DtmWrapper from './dtm';
|
||||||
import applyCode, {applyCodeFile} from './applyCode';
|
import applyCode, {applyCodeFile} from './applyCode';
|
||||||
|
|
||||||
|
import './loadCommands';
|
||||||
|
import CommandManager, { Command } from './commandManager';
|
||||||
|
|
||||||
import * as vscode3 from 'vscode';
|
import * as vscode3 from 'vscode';
|
||||||
|
|
||||||
const writeFileAsync = promisify(fs.writeFile);
|
const writeFileAsync = promisify(fs.writeFile);
|
||||||
@ -103,6 +107,14 @@ async function handleMessage(
|
|||||||
case 'code_file_apply':
|
case 'code_file_apply':
|
||||||
await applyCodeFile(message.content);
|
await applyCodeFile(message.content);
|
||||||
return;
|
return;
|
||||||
|
case 'regCommandList':
|
||||||
|
const commandList = CommandManager.getInstance().getCommandList();
|
||||||
|
panel.webview.postMessage({ command: 'regCommandList', result: commandList });
|
||||||
|
return;
|
||||||
|
case 'convertCommand':
|
||||||
|
const newText = CommandManager.getInstance().processText(message.text);
|
||||||
|
panel.webview.postMessage({ command: 'convertCommand', result: newText });
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,11 +8,7 @@
|
|||||||
"outDir": "./dist",
|
"outDir": "./dist",
|
||||||
"sourceMap": true,
|
"sourceMap": true,
|
||||||
"rootDir": "src",
|
"rootDir": "src",
|
||||||
"strict": true /* enable all strict type-checking options */
|
"strict": true
|
||||||
/* Additional Checks */
|
|
||||||
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
|
|
||||||
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
|
|
||||||
// "noUnusedParameters": true, /* Report errors on unused parameters. */
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user