Refactor delete method in DevChat class

- Un-commented and updated the delete method logic.
- Added logging for the devchat command execution.
- Added error handling for stderr, failure to delete prompt, and non-zero exit code.
This commit is contained in:
bobo.yang 2023-07-18 23:11:28 +08:00
parent 0ceab66eb6
commit 70be0acd16

View File

@ -236,35 +236,40 @@ class DevChat {
}
async delete(hash: string): Promise<boolean> {
const args = ["log", "--delete", hash];
const devChat = this.getDevChatPath();
const workspaceDir = UiUtilWrapper.workspaceFoldersFirstPath();
const openaiApiKey = process.env.OPENAI_API_KEY;
logger.channel()?.info(`Running devchat with arguments: ${args.join(" ")}`);
const spawnOptions = {
maxBuffer: 10 * 1024 * 1024, // Set maxBuffer to 10 MB
cwd: workspaceDir,
env: {
...process.env,
OPENAI_API_KEY: openaiApiKey,
},
};
const { exitCode: code, stdout, stderr } = await this.commandRun.spawnAsync(devChat, args, spawnOptions, undefined, undefined, undefined, undefined);
logger.channel()?.info(`Finish devchat with arguments: ${args.join(" ")}`);
if (stderr) {
logger.channel()?.error(`Error: ${stderr}`);
logger.channel()?.show();
return false;
}
if (stdout.indexOf('Failed to delete prompt') >= 0) {
logger.channel()?.error(`Failed to delete prompt: ${hash}`);
logger.channel()?.show();
return false;
}
if (code !== 0) {
logger.channel()?.error(`Exit code: ${code}`);
logger.channel()?.show();
return false;
}
return true;
// const args = ["delete", hash];
// const devChat = this.getDevChatPath();
// const workspaceDir = UiUtilWrapper.workspaceFoldersFirstPath();
// const openaiApiKey = process.env.OPENAI_API_KEY;
// logger.channel()?.info(`Running devchat with arguments: ${args.join(" ")}`);
// const spawnOptions = {
// maxBuffer: 10 * 1024 * 1024, // Set maxBuffer to 10 MB
// cwd: workspaceDir,
// env: {
// ...process.env,
// OPENAI_API_KEY: openaiApiKey,
// },
// };
// const { exitCode: code, stdout, stderr } = await this.commandRun.spawnAsync(devChat, args, spawnOptions, undefined, undefined, undefined, undefined);
// logger.channel()?.info(`Finish devchat with arguments: ${args.join(" ")}`);
// if (stderr) {
// logger.channel()?.error(`Error: ${stderr}`);
// logger.channel()?.show();
// }
// if (exitCode !== 0) {
// logger.channel()?.error(`Exit code: ${exitCode}`);
// logger.channel()?.show();
// return false;
// }
// return true;
}
async log(options: LogOptions = {}): Promise<LogEntry[]> {