From 2eca8ee438d5bf23f514ce29d1668b93c7f0abc6 Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Tue, 9 May 2023 08:52:07 +0800 Subject: [PATCH 1/2] create log channel --- src/extension.ts | 2 ++ src/util/logger.ts | 15 +++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 src/util/logger.ts diff --git a/src/extension.ts b/src/extension.ts index 49753be..ae21273 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -9,10 +9,12 @@ import { } from './contributes/commands'; import ExtensionContextHolder from './util/extensionContext'; +import { logger } from './util/logger'; function activate(context: vscode.ExtensionContext) { ExtensionContextHolder.context = context; + logger.init(context); // 创建 .chat 目录并复制 instructions createChatDirectoryAndCopyInstructionsSync(context.extensionUri); diff --git a/src/util/logger.ts b/src/util/logger.ts new file mode 100644 index 0000000..c4d17cf --- /dev/null +++ b/src/util/logger.ts @@ -0,0 +1,15 @@ +import * as vscode from 'vscode' + +export class logger { + private static _channel: vscode.OutputChannel | undefined; + public static init(context: vscode.ExtensionContext): void { + this._channel = vscode.window.createOutputChannel('DevChat'); + this.log('DevChat is active'); + } + public static log(text: string): void { + if (this._channel) { + this._channel.appendLine(text); + } + } +} + From 59d1a0eb3ebe07b4a2ca6dc6c8d66d4d5d856e03 Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Tue, 9 May 2023 10:34:33 +0800 Subject: [PATCH 2/2] add log --- src/command/commitMessageCommand.ts | 30 +------- src/context/contextCustomCommand.ts | 10 ++- src/context/contextGitDiff.ts | 14 +++- src/context/contextGitDiffCached.ts | 14 +++- src/context/contextRef.ts | 11 ++- src/contributes/util.ts | 13 ++-- src/handler/addContext.ts | 4 +- src/handler/addRefCommandContext.ts | 3 +- src/handler/contextDetail.ts | 4 +- src/handler/convertCommand.ts | 4 +- src/handler/historyMessages.ts | 4 +- src/handler/messageHandler.ts | 15 +++- src/handler/regCommandList.ts | 4 +- src/handler/regContextList.ts | 4 +- src/handler/sendMessage.ts | 11 +-- src/init/chatConfig.ts | 8 +- src/toolwrapper/devchat.ts | 13 +++- src/toolwrapper/dtm.ts | 114 ++++++++++++++-------------- src/util/logger.ts | 12 ++- 19 files changed, 156 insertions(+), 136 deletions(-) diff --git a/src/command/commitMessageCommand.ts b/src/command/commitMessageCommand.ts index d71f7ff..f1d3752 100644 --- a/src/command/commitMessageCommand.ts +++ b/src/command/commitMessageCommand.ts @@ -8,7 +8,7 @@ import * as os from 'os'; import * as path from 'path'; import { promisify } from 'util'; import { createTempSubdirectory } from '../util/commonUtil'; -import ExtensionContextHolder from '../util/extensionContext'; +import { logger } from '../util/logger'; const mkdirAsync = promisify(fs.mkdir); const execAsync = promisify(exec); @@ -19,26 +19,8 @@ async function createTempDirectory(tempDir: string): Promise { try { await mkdirAsync(tempDir, {recursive: true}); } catch (err) { - console.error(`Error creating temporary directory: ${err}`); - } -} - -async function writeDiffFile(diff_file: string) { - try { - const workspaceDir = vscode.workspace.workspaceFolders?.[0].uri.fsPath; - - const { stdout, stderr } = await execAsync('git diff --cached', { - cwd: workspaceDir - }); - if (stderr) { - console.error(`Error output from git diff --cached: ${stderr}`); - return; - } - // 将结果写入到临时文件中 - const tempFilePath = diff_file; - await writeFileAsync(tempFilePath, stdout); - } catch (err) { - console.error(`Error executing git diff --cached: ${err}`); + logger.channel()?.error(`Error creating temporary directory: ${err}`); + logger.channel()?.show(); } } @@ -49,12 +31,6 @@ export const commitMessageCommand: Command = { handler: async (userInput: string) => { const tempDir = createTempSubdirectory('devchat/context'); - // // 创建临时目录 - // const diff_file = path.join(tempDir, 'diff_output.txt'); - // await writeDiffFile(diff_file); - - // return `[context|${diff_file}] Write a commit message`; - const workspaceDir = vscode.workspace.workspaceFolders?.[0].uri.fsPath; if (workspaceDir) { const commitmessageInstruction = path.join(workspaceDir, '.chat', 'instructions', 'commit_message', 'instCommitMessage.txt'); diff --git a/src/context/contextCustomCommand.ts b/src/context/contextCustomCommand.ts index 35dc85d..54fa938 100644 --- a/src/context/contextCustomCommand.ts +++ b/src/context/contextCustomCommand.ts @@ -2,6 +2,7 @@ import * as path from 'path'; import * as vscode from 'vscode'; import { ChatContext } from './contextManager'; import { createTempSubdirectory, runCommandStringAndWriteOutput } from '../util/commonUtil'; +import { logger } from '../util/logger'; export const customCommandContext: ChatContext = { name: '', @@ -17,10 +18,13 @@ export const customCommandContext: ChatContext = { if (customCommand) { const tempDir = await createTempSubdirectory('devchat/context'); const diff_file = path.join(tempDir, 'custom.txt'); + + logger.channel()?.info(`custom command: ${customCommand}`); const result = await runCommandStringAndWriteOutput(customCommand, diff_file); - console.log(result.exitCode); - console.log(result.stdout); - console.log(result.stderr); + logger.channel()?.info(`custom command: ${customCommand} exit code:`, result.exitCode); + + logger.channel()?.debug(`custom command: ${customCommand} stdout:`, result.stdout); + logger.channel()?.debug(`custom command: ${customCommand} stderr:`, result.stderr); return `[context|${diff_file}]`; } return ''; diff --git a/src/context/contextGitDiff.ts b/src/context/contextGitDiff.ts index 600ede1..ffca81e 100644 --- a/src/context/contextGitDiff.ts +++ b/src/context/contextGitDiff.ts @@ -2,16 +2,22 @@ import * as path from 'path'; import { ChatContext } from './contextManager'; import { createTempSubdirectory, runCommandStringAndWriteOutput } from '../util/commonUtil'; +import { logger } from '../util/logger'; + + export const gitDiffContext: ChatContext = { name: 'git diff', description: 'diff for all changes', handler: async () => { const tempDir = await createTempSubdirectory('devchat/context'); const diff_file = path.join(tempDir, 'diff_all.txt'); - const result = await runCommandStringAndWriteOutput('git diff', diff_file); - console.log(result.exitCode); - console.log(result.stdout); - console.log(result.stderr); + + logger.channel()?.info(`git diff`); + const result = await runCommandStringAndWriteOutput('git diff', diff_file); + logger.channel()?.info(`git diff exit code:`, result.exitCode); + + logger.channel()?.debug(`git diff stdout:`, result.stdout); + logger.channel()?.debug(`git diff stderr:`, result.stderr); return `[context|${diff_file}]`; }, }; diff --git a/src/context/contextGitDiffCached.ts b/src/context/contextGitDiffCached.ts index 548d771..7e401bd 100644 --- a/src/context/contextGitDiffCached.ts +++ b/src/context/contextGitDiffCached.ts @@ -2,16 +2,22 @@ import * as path from 'path'; import { ChatContext } from './contextManager'; import { createTempSubdirectory, runCommandStringAndWriteOutput } from '../util/commonUtil'; +import { logger } from '../util/logger'; + + export const gitDiffCachedContext: ChatContext = { name: 'git diff cached', description: 'diff for cached changes', handler: async () => { const tempDir = await createTempSubdirectory('devchat/context'); const diff_file = path.join(tempDir, 'diff_cached.txt'); - const result = await runCommandStringAndWriteOutput('git diff --cached', diff_file); - console.log(result.exitCode); - console.log(result.stdout); - console.log(result.stderr); + + logger.channel()?.info(`git diff --cached`); + const result = await runCommandStringAndWriteOutput('git diff --cached', diff_file); + logger.channel()?.info(`git diff --cached exit code:`, result.exitCode); + + logger.channel()?.debug(`git diff --cached stdout:`, result.stdout); + logger.channel()?.debug(`git diff --cached stderr:`, result.stderr); return `[context|${diff_file}]`; }, }; diff --git a/src/context/contextRef.ts b/src/context/contextRef.ts index 941a233..efbdc11 100644 --- a/src/context/contextRef.ts +++ b/src/context/contextRef.ts @@ -2,15 +2,20 @@ import * as vscode from 'vscode'; import * as path from 'path'; import { createTempSubdirectory, runCommandStringAndWriteOutput } from '../util/commonUtil'; +import { logger } from '../util/logger'; + export async function handleRefCommand(ref_command: string) { if (ref_command) { const tempDir = await createTempSubdirectory('devchat/context'); const diff_file = path.join(tempDir, 'custom.txt'); + + logger.channel()?.info(`custom command: ${ref_command}`); const result = await runCommandStringAndWriteOutput(ref_command, diff_file); - console.log(result.exitCode); - console.log(result.stdout); - console.log(result.stderr); + logger.channel()?.info(`custom command: ${ref_command} exit code:`, result.exitCode); + + logger.channel()?.debug(`custom command: ${ref_command} stdout:`, result.stdout); + logger.channel()?.debug(`custom command: ${ref_command} stderr:`, result.stderr); return `[context|${diff_file}]`; } diff --git a/src/contributes/util.ts b/src/contributes/util.ts index 6787c52..bd7d3dc 100644 --- a/src/contributes/util.ts +++ b/src/contributes/util.ts @@ -2,13 +2,14 @@ import * as vscode from 'vscode'; import { handleCodeSelected } from '../context/contextCodeSelected'; import { handleFileSelected } from '../context/contextFileSelected'; +import { MessageHandler } from '../handler/messageHandler'; export async function sendFileSelectMessage(panel: vscode.WebviewPanel, filePath: string): Promise { const codeContext = await handleFileSelected(filePath); - panel.webview.postMessage({ command: 'appendContext', context: codeContext }); - } - - export async function sendCodeSelectMessage(panel: vscode.WebviewPanel, filePath: string, codeBlock: string): Promise { + MessageHandler.sendMessage(panel, { command: 'appendContext', context: codeContext }); +} + +export async function sendCodeSelectMessage(panel: vscode.WebviewPanel, filePath: string, codeBlock: string): Promise { const codeContext = await handleCodeSelected(filePath, codeBlock); - panel.webview.postMessage({ command: 'appendContext', context: codeContext }); - } \ No newline at end of file + MessageHandler.sendMessage(panel, { command: 'appendContext', context: codeContext }); +} \ No newline at end of file diff --git a/src/handler/addContext.ts b/src/handler/addContext.ts index b74e900..a4d3876 100644 --- a/src/handler/addContext.ts +++ b/src/handler/addContext.ts @@ -1,9 +1,11 @@ import * as vscode from 'vscode'; import ChatContextManager from '../context/contextManager'; +import { MessageHandler } from './messageHandler'; + export async function addConext(message: any, panel: vscode.WebviewPanel): Promise { const contextStr = await ChatContextManager.getInstance().processText(message.selected); - panel.webview.postMessage({ command: 'appendContext', context: contextStr }); + MessageHandler.sendMessage(panel, { command: 'appendContext', context: contextStr }); return; } diff --git a/src/handler/addRefCommandContext.ts b/src/handler/addRefCommandContext.ts index a6038ce..ccbb235 100644 --- a/src/handler/addRefCommandContext.ts +++ b/src/handler/addRefCommandContext.ts @@ -1,10 +1,11 @@ import * as vscode from 'vscode'; import { handleRefCommand } from '../context/contextRef'; +import { MessageHandler } from './messageHandler'; // message: { command: 'addRefCommandContext', refCommand: string } // User input: /ref ls . then "ls ." will be passed to refCommand export async function addRefCommandContext(message: any, panel: vscode.WebviewPanel): Promise { const contextStr = await handleRefCommand(message.refCommand); - panel.webview.postMessage({ command: 'appendContext', context: contextStr }); + MessageHandler.sendMessage(panel, { command: 'appendContext', context: contextStr }); return; } diff --git a/src/handler/contextDetail.ts b/src/handler/contextDetail.ts index a1bda68..2289e92 100644 --- a/src/handler/contextDetail.ts +++ b/src/handler/contextDetail.ts @@ -1,12 +1,12 @@ import * as vscode from 'vscode'; import * as fs from 'fs'; -import { handleRefCommand } from '../context/contextRef'; +import { MessageHandler } from './messageHandler'; // message: { command: 'contextDetail', file: string } // read detail context information from file // return json string export async function contextDetail(message: any, panel: vscode.WebviewPanel): Promise { const fileContent = fs.readFileSync(message.file, 'utf-8'); - panel.webview.postMessage({ command: 'contextDetailResponse', 'file':message.file, result: fileContent }); + MessageHandler.sendMessage(panel, { command: 'contextDetailResponse', 'file':message.file, result: fileContent }); return; } diff --git a/src/handler/convertCommand.ts b/src/handler/convertCommand.ts index 8db3103..393a177 100644 --- a/src/handler/convertCommand.ts +++ b/src/handler/convertCommand.ts @@ -1,10 +1,10 @@ import * as vscode from 'vscode'; import CommandManager from '../command/commandManager'; - +import { MessageHandler } from './messageHandler'; export async function convertCommand(message: any, panel: vscode.WebviewPanel): Promise { const newText = await CommandManager.getInstance().processText(message.text); - panel.webview.postMessage({ command: 'convertCommand', result: newText }); + MessageHandler.sendMessage(panel, { command: 'convertCommand', result: newText }); return; } diff --git a/src/handler/historyMessages.ts b/src/handler/historyMessages.ts index f07426e..b191ab8 100644 --- a/src/handler/historyMessages.ts +++ b/src/handler/historyMessages.ts @@ -1,13 +1,13 @@ import * as vscode from 'vscode'; import DevChat, { LogOptions } from '../toolwrapper/devchat'; - +import { MessageHandler } from './messageHandler'; export async function historyMessages(message: any, panel: vscode.WebviewPanel): Promise { const devChat = new DevChat(); const logOptions: LogOptions = message.options || {}; const logEntries = await devChat.log(logOptions); - panel.webview.postMessage({ command: 'loadHistoryMessages', entries: logEntries }); + MessageHandler.sendMessage(panel, { command: 'loadHistoryMessages', entries: logEntries }); return; } diff --git a/src/handler/messageHandler.ts b/src/handler/messageHandler.ts index dbb353b..45dcbd6 100644 --- a/src/handler/messageHandler.ts +++ b/src/handler/messageHandler.ts @@ -4,9 +4,10 @@ import * as vscode from 'vscode'; import '../command/loadCommands'; import '../context/loadContexts' +import { logger } from '../util/logger'; -class MessageHandler { +export class MessageHandler { private handlers: { [command: string]: (message: any, panel: vscode.WebviewPanel) => Promise } = {}; constructor() { @@ -19,14 +20,20 @@ class MessageHandler { async handleMessage(message: any, panel: vscode.WebviewPanel): Promise { const handler = this.handlers[message.command]; if (handler) { + logger.channel()?.info(`Handling command "${message.command}"`); await handler(message, panel); + logger.channel()?.info(`Handling command "${message.command}" done`); } else { - console.error(`No handler found for command "${message.command}"`); + logger.channel()?.error(`No handler found for command "${message.command}"`); + logger.channel()?.show(); } } - sendMessage(panel: vscode.WebviewPanel, command: string, data: any): void { - panel.webview.postMessage({ command, ...data }); + public static sendMessage(panel: vscode.WebviewPanel, message: object, log: boolean = true): void { + if (log) { + logger.channel()?.info(`Sending message "${JSON.stringify(message)}"`); + } + panel.webview.postMessage(message); } } diff --git a/src/handler/regCommandList.ts b/src/handler/regCommandList.ts index f24be53..5805d28 100644 --- a/src/handler/regCommandList.ts +++ b/src/handler/regCommandList.ts @@ -1,10 +1,10 @@ import * as vscode from 'vscode'; import CommandManager from '../command/commandManager'; - +import { MessageHandler } from './messageHandler'; export async function regCommandList(message: any, panel: vscode.WebviewPanel): Promise { const commandList = CommandManager.getInstance().getCommandList(); - panel.webview.postMessage({ command: 'regCommandList', result: commandList }); + MessageHandler.sendMessage(panel, { command: 'regCommandList', result: commandList }); return; } diff --git a/src/handler/regContextList.ts b/src/handler/regContextList.ts index de1865f..07124f4 100644 --- a/src/handler/regContextList.ts +++ b/src/handler/regContextList.ts @@ -1,10 +1,10 @@ import * as vscode from 'vscode'; import ChatContextManager from '../context/contextManager'; - +import { MessageHandler } from './messageHandler'; export async function regContextList(message: any, panel: vscode.WebviewPanel): Promise { const contextList = ChatContextManager.getInstance().getContextList(); - panel.webview.postMessage({ command: 'regContextList', result: contextList }); + MessageHandler.sendMessage(panel, { command: 'regContextList', result: contextList }); return; } diff --git a/src/handler/sendMessage.ts b/src/handler/sendMessage.ts index b446842..e7d5f35 100644 --- a/src/handler/sendMessage.ts +++ b/src/handler/sendMessage.ts @@ -4,6 +4,8 @@ import * as fs from 'fs'; import * as path from 'path'; import DevChat from '../toolwrapper/devchat'; import CommandManager from '../command/commandManager'; +import { logger } from '../util/logger'; +import { MessageHandler } from './messageHandler'; // Add this function to messageHandler.ts @@ -60,7 +62,8 @@ function getInstructionFiles(): string[] { } } } catch (error) { - console.error('Error reading instruction files:', error); + logger.channel()?.error(`Error reading instruction files: ${error}`); + logger.channel()?.show(); } } return instructionFiles; @@ -73,8 +76,6 @@ export async function sendMessage(message: any, panel: vscode.WebviewPanel): Pro const devChat = new DevChat(); const newText2 = await CommandManager.getInstance().processText(message.text); - panel.webview.postMessage({ command: 'convertCommand', result: newText2 }); - const parsedMessage = parseMessage(newText2); const chatOptions: any = lastPromptHash ? { parent: lastPromptHash } : {}; @@ -94,7 +95,7 @@ export async function sendMessage(message: any, panel: vscode.WebviewPanel): Pro let partialData = ""; const onData = (partialResponse: string) => { partialData += partialResponse; - panel.webview.postMessage({ command: 'receiveMessagePartial', text: partialData }); + MessageHandler.sendMessage(panel, { command: 'receiveMessagePartial', text: partialData }, false); }; const chatResponse = await devChat.chat(parsedMessage.text, chatOptions, onData); @@ -109,7 +110,7 @@ export async function sendMessage(message: any, panel: vscode.WebviewPanel): Pro } } const response = chatResponse.response; - panel.webview.postMessage({ command: 'receiveMessage', text: response }); + MessageHandler.sendMessage(panel, { command: 'receiveMessage', text: response }); return; } diff --git a/src/init/chatConfig.ts b/src/init/chatConfig.ts index fdbd111..7720e19 100644 --- a/src/init/chatConfig.ts +++ b/src/init/chatConfig.ts @@ -3,6 +3,8 @@ import * as fs from 'fs'; import * as path from 'path'; import * as ncp from 'ncp'; +import { logger } from '../util/logger'; + export function createChatDirectoryAndCopyInstructionsSync(extensionUri: vscode.Uri) { const workspaceFolders = vscode.workspace.workspaceFolders; @@ -25,10 +27,12 @@ export function createChatDirectoryAndCopyInstructionsSync(extensionUri: vscode. // 将 instructions 目录复制到 .chat 目录中 ncp.ncp(instructionsSrcPath, path.join(chatDirPath, 'instructions'), (err) => { if (err) { - console.error('Error copying instructions:', err); + logger.channel()?.error('Error copying instructions:', err); + logger.channel()?.show(); } }); } catch (error) { - console.error('Error creating .chat directory and copying instructions:', error); + logger.channel()?.error('Error creating .chat directory and copying instructions:', error); + logger.channel()?.show(); } } \ No newline at end of file diff --git a/src/toolwrapper/devchat.ts b/src/toolwrapper/devchat.ts index 1be1053..08e15db 100644 --- a/src/toolwrapper/devchat.ts +++ b/src/toolwrapper/devchat.ts @@ -7,6 +7,8 @@ import * as dotenv from 'dotenv'; import * as path from 'path'; import * as fs from 'fs'; +import { logger } from '../util/logger'; + const spawnAsync = async (command: string, args: string[], options: any, onData: (data: string) => void): Promise<{ code: number, stdout: string; stderr: string }> => { return new Promise((resolve, reject) => { const child = spawn(command, args, options); @@ -121,6 +123,7 @@ class DevChat { fs.writeFileSync(configPath, configJson); try { + logger.channel()?.info(`Running devchat with args: ${args.join(" ")}`); const { code, stdout, stderr } = await spawnAsync('devchat', args, { maxBuffer: 10 * 1024 * 1024, // Set maxBuffer to 10 MB cwd: workspaceDir, @@ -142,8 +145,7 @@ class DevChat { } const responseLines = stdout.trim().split("\n"); - console.log(responseLines) - + if (responseLines.length === 0) { return { "prompt-hash": "", @@ -215,6 +217,7 @@ class DevChat { const openaiApiKey = process.env.OPENAI_API_KEY; try { + logger.channel()?.info(`Running devchat with args: ${args.join(" ")}`); const { code, stdout, stderr } = await spawnAsync('devchat', args, { maxBuffer: 10 * 1024 * 1024, // Set maxBuffer to 10 MB cwd: workspaceDir, @@ -225,13 +228,15 @@ class DevChat { }, (partialResponse: string) => { }); if (stderr) { - console.error(stderr); + logger.channel()?.error(`Error getting log: ${stderr}`); + logger.channel()?.show(); return []; } return JSON.parse(stdout.trim()); } catch (error) { - console.error(error) + logger.channel()?.error(`Error getting log: ${error}`); + logger.channel()?.show(); return []; } } diff --git a/src/toolwrapper/dtm.ts b/src/toolwrapper/dtm.ts index ed2dad6..2b18b30 100644 --- a/src/toolwrapper/dtm.ts +++ b/src/toolwrapper/dtm.ts @@ -1,67 +1,71 @@ import { spawn } from "child_process"; import * as vscode from 'vscode'; +import { logger } from "../util/logger"; + interface DtmResponse { - status: number; - message: string; - log: string; + status: number; + message: string; + log: string; } class DtmWrapper { - private workspaceDir: string; + private workspaceDir: string; - constructor() { - this.workspaceDir = vscode.workspace.workspaceFolders?.[0].uri.fsPath || '.'; - } - - private async runCommand(command: string, args: string[]): Promise { - return new Promise((resolve, reject) => { - const child = spawn(command, args, { cwd: this.workspaceDir }); - let stdout = ''; - let stderr = ''; - - child.stdout.on('data', (data) => { - stdout += data; - }); - - child.stderr.on('data', (data) => { - stderr += data; - }); - - child.on('close', (code) => { - try { - const parsedOutput = JSON.parse(stdout.trim()); - if (code === 0) { - resolve(parsedOutput); - } else { - reject(parsedOutput); - } - } catch (error) { - // 处理 JSON 解析异常 - const errorObj = error as Error; - reject({ status: -1, message: 'JSON parse error', log: errorObj.message }); - } - }); - }); - } - - async scaffold(directoryTree: string): Promise { - return await this.runCommand('dtm', ['scaffold', directoryTree, '-o', 'json']); - } - - async patch(patchFilePath: string): Promise { - return await this.runCommand('dtm', ['patch', patchFilePath, '-o', 'json']); - } - - async commit(commitMsg: string): Promise { - try { - return await this.runCommand('dtm', ['commit', '-m', commitMsg, '-o', 'json']); - } catch (error) { - // 处理 runCommand 中的 reject 错误 - console.error('Error in commit:', error); - return error as DtmResponse; + constructor() { + this.workspaceDir = vscode.workspace.workspaceFolders?.[0].uri.fsPath || '.'; + } + + private async runCommand(command: string, args: string[]): Promise { + return new Promise((resolve, reject) => { + logger.channel()?.info(`Running command: ${command} ${args.join(' ')}`); + const child = spawn(command, args, { cwd: this.workspaceDir }); + let stdout = ''; + let stderr = ''; + + child.stdout.on('data', (data) => { + stdout += data; + }); + + child.stderr.on('data', (data) => { + stderr += data; + }); + + child.on('close', (code) => { + try { + const parsedOutput = JSON.parse(stdout.trim()); + if (code === 0) { + resolve(parsedOutput); + } else { + reject(parsedOutput); + } + } catch (error) { + // 处理 JSON 解析异常 + const errorObj = error as Error; + reject({ status: -1, message: 'JSON parse error', log: errorObj.message }); + } + }); + }); + } + + async scaffold(directoryTree: string): Promise { + return await this.runCommand('dtm', ['scaffold', directoryTree, '-o', 'json']); + } + + async patch(patchFilePath: string): Promise { + return await this.runCommand('dtm', ['patch', patchFilePath, '-o', 'json']); + } + + async commit(commitMsg: string): Promise { + try { + return await this.runCommand('dtm', ['commit', '-m', commitMsg, '-o', 'json']); + } catch (error) { + // 处理 runCommand 中的 reject 错误 + logger.channel()?.error(`Error in commit: ${error}`); + logger.channel()?.show(); + return error as DtmResponse; + } } - } } export default DtmWrapper; diff --git a/src/util/logger.ts b/src/util/logger.ts index c4d17cf..3530271 100644 --- a/src/util/logger.ts +++ b/src/util/logger.ts @@ -1,15 +1,13 @@ import * as vscode from 'vscode' export class logger { - private static _channel: vscode.OutputChannel | undefined; + private static _channel: vscode.LogOutputChannel | undefined; public static init(context: vscode.ExtensionContext): void { - this._channel = vscode.window.createOutputChannel('DevChat'); - this.log('DevChat is active'); + this._channel = vscode.window.createOutputChannel('DevChat', { log: true }); } - public static log(text: string): void { - if (this._channel) { - this._channel.appendLine(text); - } + + public static channel(): vscode.LogOutputChannel | undefined { + return this._channel; } }