fix: AI Tool Calling run_command 输出同步到工作空间终端面板
- handleRunCommand 改用 execWorkspaceCommand 替代 Node exec() - 执行完成后通过 workspace:toolOutput IPC 事件推送命令和输出 - 渲染进程监听 onToolOutput,在活跃终端显示 AI 命令及结果 - 清理 tool-handlers.ts 中不再需要的 child_process 导入
This commit is contained in:
@@ -171,6 +171,12 @@ export async function initWorkspacePanel(): Promise<void> {
|
||||
handleProcessExit(data.id, data.code);
|
||||
});
|
||||
|
||||
// 监听 AI Tool Calling 命令输出,同步到终端面板
|
||||
bridge.workspace.onToolOutput((data) => {
|
||||
logDebug('工作空间收到工具输出', `command: ${data.command.slice(0, 50)} | exitCode: ${data.exitCode}`);
|
||||
appendToolOutput(data.command, data.stdout, data.stderr, data.exitCode);
|
||||
});
|
||||
|
||||
// 绑定 UI 事件
|
||||
bindEvents();
|
||||
|
||||
@@ -293,6 +299,40 @@ function executeCommand(command: string): void {
|
||||
bridge.workspace.exec({ id: session.id, command, cwd });
|
||||
}
|
||||
|
||||
/** AI Tool Calling 命令输出 → 追加到活跃终端 */
|
||||
function appendToolOutput(command: string, stdout: string, stderr: string, exitCode: number | null): void {
|
||||
const session = getActiveSession();
|
||||
if (!session) return;
|
||||
|
||||
// 显示执行的命令
|
||||
session.lines.push({ type: 'system', text: `$ ${command} (AI)` });
|
||||
|
||||
// 显示 stdout
|
||||
if (stdout) {
|
||||
for (const line of stdout.split('\n')) {
|
||||
if (line) session.lines.push({ type: 'stdout', text: line });
|
||||
}
|
||||
}
|
||||
|
||||
// 显示 stderr
|
||||
if (stderr) {
|
||||
for (const line of stderr.split('\n')) {
|
||||
if (line) session.lines.push({ type: 'stderr', text: line });
|
||||
}
|
||||
}
|
||||
|
||||
// 显示退出状态
|
||||
const exitMsg = exitCode === 0 ? '✓ AI 命令执行成功' : `✗ AI 命令退出 (code: ${exitCode})`;
|
||||
session.lines.push({ type: 'system', text: exitMsg });
|
||||
|
||||
// 限制最大行数
|
||||
if (session.lines.length > 3000) {
|
||||
session.lines = session.lines.slice(-2000);
|
||||
}
|
||||
|
||||
renderTerminal();
|
||||
}
|
||||
|
||||
function appendTerminalOutput(sessionId: string, type: 'stdout' | 'stderr', data: string): void {
|
||||
const session = terminalSessions.find(s => s.id === sessionId);
|
||||
if (!session) {
|
||||
|
||||
Vendored
+2
@@ -241,6 +241,8 @@ export interface MetonaDesktopAPI {
|
||||
onExit: (callback: (data: { id: string; code: number | null }) => void) => void;
|
||||
/** 无超时工具命令执行,用于 AI Tool Calling 集成 */
|
||||
execTool: (command: string, cwd?: string) => Promise<{ success: boolean; stdout: string; stderr: string; exitCode: number | null; duration: number; userTerminated: boolean; truncated: boolean }>;
|
||||
/** AI Tool Calling 命令输出 → 同步到工作空间终端 */
|
||||
onToolOutput: (callback: (data: { command: string; stdout: string; stderr: string; exitCode: number | null }) => void) => void;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user