refactor: 命令执行走工作空间终端,AI 不再直接执行
流程改为:AI 提出命令 → 用户确认 → 工作空间终端执行(实时显示)→ 结果反馈 AI → AI 回复 - handleRunCommand 直接 spawn 子进程,stdout/stderr 实时通过 cmd:output 推送 - 进程结束后通过 cmd:done 通知渲染进程 - 停止按钮通过 cmd:kill 终止进程 - 清理 workspace.ts 中不再需要的 execWorkspaceCommand 工具集成代码 - 去掉 workspace:execTool/killTool,改为 cmd:output/cmd:done/cmd:kill
This commit is contained in:
@@ -20,9 +20,6 @@ export const DEFAULT_WORKSPACE_DIR = path.join(app.getPath('userData'), 'workspa
|
||||
/** 活跃进程 Map */
|
||||
const activeProcesses = new Map<string, ChildProcess>();
|
||||
|
||||
/** 当前 AI 工具命令进程 ID(用于用户手动终止) */
|
||||
let _currentToolProcessId: string | null = null;
|
||||
|
||||
/** 确保工作空间目录存在 */
|
||||
export function ensureWorkspaceDir(): void {
|
||||
if (!fs.existsSync(DEFAULT_WORKSPACE_DIR)) {
|
||||
@@ -257,7 +254,6 @@ export function execWorkspaceCommand(
|
||||
});
|
||||
|
||||
activeProcesses.set(cmdId, proc);
|
||||
_currentToolProcessId = cmdId;
|
||||
|
||||
proc.stdout.on('data', (data: Buffer) => {
|
||||
const str = data.toString();
|
||||
@@ -291,7 +287,6 @@ export function execWorkspaceCommand(
|
||||
|
||||
proc.on('close', (code) => {
|
||||
activeProcesses.delete(cmdId);
|
||||
if (_currentToolProcessId === cmdId) _currentToolProcessId = null;
|
||||
const duration = Date.now() - startTime;
|
||||
sendLog(
|
||||
code === 0 ? 'success' : 'warn',
|
||||
@@ -311,7 +306,6 @@ export function execWorkspaceCommand(
|
||||
|
||||
proc.on('error', (err) => {
|
||||
activeProcesses.delete(cmdId);
|
||||
if (_currentToolProcessId === cmdId) _currentToolProcessId = null;
|
||||
const duration = Date.now() - startTime;
|
||||
sendLog('error', `🔧 execWorkspaceCommand 失败`, `${cmdId} | ${err.message}`);
|
||||
resolve({
|
||||
@@ -347,14 +341,3 @@ export function execWorkspaceCommand(
|
||||
export function terminateWorkspaceCommand(commandId: string): boolean {
|
||||
return killProcess(commandId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 终止当前正在运行的 AI 工具命令
|
||||
* @returns 是否成功终止
|
||||
*/
|
||||
export function terminateCurrentToolCommand(): boolean {
|
||||
if (!_currentToolProcessId) return false;
|
||||
const id = _currentToolProcessId;
|
||||
_currentToolProcessId = null;
|
||||
return killProcess(id);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user