diff --git a/package.json b/package.json index 22b92d6..bb815b9 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "devchat", "displayName": "DevChat", "description": "Write prompts, not code", - "version": "0.0.47", + "version": "0.0.49", "icon": "assets/devchat.png", "publisher": "merico", "engines": { @@ -306,6 +306,7 @@ } }, "scripts": { + "vscode:uninstall": "node ./dist/uninstall", "vscode:prepublish": "npm run package", "compile": "webpack", "watch": "webpack --watch", diff --git a/src/context/contextManager.ts b/src/context/contextManager.ts index 2421396..e11ceb5 100644 --- a/src/context/contextManager.ts +++ b/src/context/contextManager.ts @@ -1,3 +1,10 @@ +import * as path from 'path'; +import { logger } from '../util/logger'; + +import { createTempSubdirectory } from '../util/commonUtil'; +import CustomContexts from './customContext'; + + export interface ChatContext { name: string; description: string; @@ -21,8 +28,31 @@ export interface ChatContext { registerContext(context: ChatContext): void { this.contexts.push(context); } + + public async loadCustomContexts(workflowsDir: string): Promise { + const customContexts = CustomContexts.getInstance(); + customContexts.parseContexts(workflowsDir); + + for (const customContext of customContexts.getContexts()) { + this.registerContext({ + name: customContext.name, + description: customContext.description, + handler: async () => { + const tempDir = await createTempSubdirectory('devchat/context'); + + const outputFile = path.join(tempDir, 'context.txt'); + const commandResult = await customContexts.handleCommand(customContext.name, outputFile); + + logger.channel()?.info(`${customContext.command.join(' ')} exit code:`, commandResult!.exitCode); + logger.channel()?.debug(`stdout:`, commandResult!.stdout); + logger.channel()?.debug(`stderr:`, commandResult!.stderr); + return `[context|${outputFile}]`; + }, + }); + } + } - getContextList(): ChatContext[] { + getContextList(): ChatContext[] { return this.contexts; } @@ -35,7 +65,7 @@ export interface ChatContext { } return ''; - } + } } export default ChatContextManager; diff --git a/src/handler/loadHandlers.ts b/src/handler/loadHandlers.ts index c7913f8..d372865 100644 --- a/src/handler/loadHandlers.ts +++ b/src/handler/loadHandlers.ts @@ -13,6 +13,8 @@ import { addConext } from './addContext'; import { addRefCommandContext } from './addRefCommandContext'; import { contextDetail } from './contextDetail'; import { listAllMessages } from './listMessages'; +import { regActionList } from './regActionList'; +import { applyAction } from './applyAction'; // According to the context menu selected by the user, add the corresponding context file @@ -65,3 +67,9 @@ messageHandler.registerHandler('listAllMessages', listAllMessages); // Regeneration // The response is the same as sendMessage messageHandler.registerHandler('regeneration', regeneration); +// Register the action list +// Response: { command: 'regActionList', result: } +messageHandler.registerHandler('regActionList', regActionList); +// Apply action for code block +// Response: none +messageHandler.registerHandler('applyAction', applyAction); diff --git a/src/handler/sendMessageBase.ts b/src/handler/sendMessageBase.ts index 90fca3d..ee335b7 100644 --- a/src/handler/sendMessageBase.ts +++ b/src/handler/sendMessageBase.ts @@ -51,7 +51,7 @@ export function getInstructionFiles(): string[] { for (const command of customCommands) { if (command.default) { for (const instruction of command.instructions) { - instructionFiles.push(`./.chat/workflows/${command.name}/${instruction}`); + instructionFiles.push(`${instruction}`); } } } @@ -73,7 +73,7 @@ export async function parseMessageAndSetOptions(message: any, chatOptions: any): } chatOptions.header = getInstructionFiles(); - if (parsedMessage.instruction.length > 0) { + if (message.text === newText2) { chatOptions.header = parsedMessage.instruction; } diff --git a/src/panel/devchatView.ts b/src/panel/devchatView.ts index 5ce44da..8d92d8b 100644 --- a/src/panel/devchatView.ts +++ b/src/panel/devchatView.ts @@ -9,6 +9,8 @@ import ExtensionContextHolder from '../util/extensionContext'; import CustomCommands from '../command/customCommand'; import { TopicManager } from '../topic/topicManager'; import { UiUtilWrapper } from '../util/uiUtil'; +import ChatContextManager from '../context/contextManager'; +import ActionManager from '../action/actionManager'; export class DevChatViewProvider implements vscode.WebviewViewProvider { @@ -32,6 +34,8 @@ export class DevChatViewProvider implements vscode.WebviewViewProvider { if (workspaceDir) { const workflowsDir = path.join(workspaceDir!, '.chat', 'workflows'); CustomCommands.getInstance().parseCommands(workflowsDir); + ChatContextManager.getInstance().loadCustomContexts(workflowsDir); + ActionManager.getInstance().loadCustomActions(workflowsDir); } this._view = webviewView; diff --git a/src/util/commonUtil.ts b/src/util/commonUtil.ts index d6285a3..e2a5d39 100644 --- a/src/util/commonUtil.ts +++ b/src/util/commonUtil.ts @@ -24,7 +24,7 @@ export function createTempSubdirectory(subdir: string): string { return targetDir; } -interface CommandResult { +export interface CommandResult { exitCode: number | null; stdout: string; stderr: string; @@ -40,6 +40,7 @@ export class CommandRun { public async spawnAsync(command: string, args: string[], options: object, onData: ((data: string) => void) | undefined, onError: ((data: string) => void) | undefined, onOutputFile: ((command: string, stdout: string, stderr: string) => string) | undefined, outputFile: string | undefined): Promise { return new Promise((resolve, reject) => { + logger.channel()?.info(`Running command: ${command} ${args.join(' ')}`); this.childProcess = spawn(command, args, options); let stdout = ''; @@ -108,7 +109,7 @@ export class CommandRun { export async function runCommandAndWriteOutput( command: string, args: string[], - outputFile: string + outputFile: string | undefined ): Promise { const run = new CommandRun(); const options = { @@ -143,6 +144,28 @@ export async function runCommandStringAndWriteOutput( return run.spawnAsync(command, args, options, undefined, undefined, onOutputFile, outputFile); } +export async function runCommandStringArrayAndWriteOutput( + commandStringList: string[], + outputFile: string +): Promise { + const run = new CommandRun(); + const options = { + cwd: UiUtilWrapper.workspaceFoldersFirstPath() || '.' + }; + + const commandString = commandStringList[0]; + const args: string[] = commandStringList.slice(1); + const onOutputFile = (command: string, stdout: string, stderr: string): string => { + const data = { + command: commandString, + content: stdout, + }; + return JSON.stringify(data); + }; + + return run.spawnAsync(commandString, args, options, undefined, undefined, onOutputFile, outputFile); +} + export async function getLanguageIdByFileName(fileName: string): Promise { try { const languageId = await UiUtilWrapper.languageId(fileName);