feat: 工作空间与 AI Tool Calling 深度集成

核心变更:
- 新增 execWorkspaceCommand() 无超时进程执行(workspace.ts)
- 新增 workspace:execTool IPC 通道(无超时,5MB 输出上限)
- run_command 工具改用 workspace IPC,移除 30s 超时限制
- run_command 默认启用(需用户确认),AI 上下文注入工作空间目录
- Agent Loop 工具执行:run_command 无超时,其他工具保持 30s
- Agent 系统提示词新增工作空间规则(userTerminated/truncated 反馈)

UI 修复:
- Header z-index 提升至 50,Model Bar 提升至 40,Input Area 提升至 30
- 工作空间面板 top 调整为 90px(header + model-bar),不再遮挡菜单
- Input Area 增加 position: relative 确保 z-index 生效

其他:
- 新增 docs/DEVELOPMENT.md 开发规范文档
- 设置面板 run_command 开关文案优化,默认值改为 true
This commit is contained in:
thzxx
2026-04-07 22:24:58 +08:00
parent 4a85879b20
commit d9e9416d35
10 changed files with 492 additions and 25 deletions
+7 -1
View File
@@ -22,7 +22,7 @@ import {
handleRunCommand
} from './tool-handlers.js';
import { getAllowedDirs, getBlockedDirs, setAllowedDirs } from './tool-security.js';
import { startProcess, killProcess, getWorkspaceDir, setWorkspaceDir, listWorkspaceDir } from './workspace.js';
import { startProcess, killProcess, getWorkspaceDir, setWorkspaceDir, listWorkspaceDir, execWorkspaceCommand } from './workspace.js';
export function setupIPC(): void {
ipcMain.handle('dialog:openFile', async (_, options?: { filters?: Array<{ name: string; extensions: string[] }> }) => {
@@ -171,4 +171,10 @@ export function setupIPC(): void {
sendLog('info', `🖥️ workspace:kill`, `ID: ${id}`);
killProcess(id);
});
// 无超时工具命令执行(AI Tool Calling 集成)
ipcMain.handle('workspace:execTool', async (_, command: string, cwd?: string) => {
sendLog('info', `🔧 workspace:execTool`, `${command.slice(0, 200)} | cwd: ${cwd || '默认'}`);
return execWorkspaceCommand(command, cwd);
});
}
+3 -1
View File
@@ -58,6 +58,8 @@ contextBridge.exposeInMainWorld('metonaDesktop', {
},
onExit: (callback: (data: { id: string; code: number | null }) => void) => {
ipcRenderer.on('workspace:exit', (_: unknown, data: { id: string; code: number | null }) => callback(data));
}
},
/** 无超时命令执行,用于 AI Tool Calling */
execTool: (command: string, cwd?: string) => ipcRenderer.invoke('workspace:execTool', command, cwd)
}
});
+134
View File
@@ -200,3 +200,137 @@ export function isProcessAlive(id: string): boolean {
export function getActiveProcessCount(): number {
return activeProcesses.size;
}
/**
* 执行命令并收集完整输出(无超时限制)
* 用于 AI Tool Calling 集成,通过 IPC invoke/handle 调用
* 返回完整的 stdout + stderr + exitCode
*/
export function execWorkspaceCommand(
command: string,
cwd?: string
): Promise<{ success: boolean; stdout: string; stderr: string; exitCode: number | null; duration: number; userTerminated: boolean; truncated: boolean }> {
return new Promise((resolve) => {
const workDir = cwd ? path.resolve(cwd) : _workspaceDir;
const cmdId = `_tool_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
// 安全检查
const cmdCheck = checkCommandAllowed(command);
if (!cmdCheck.ok) {
sendLog('warn', `🔧 工具命令被拦截`, `${command.slice(0, 100)} | ${cmdCheck.reason}`);
resolve({ success: false, stdout: '', stderr: cmdCheck.reason!, exitCode: 1, duration: 0, userTerminated: false, truncated: false });
return;
}
const dirCheck = checkPathAllowed(workDir, 'read');
if (!dirCheck.ok) {
sendLog('warn', `🔧 工具目录被拦截`, `${workDir} | ${dirCheck.reason}`);
resolve({ success: false, stdout: '', stderr: dirCheck.reason!, exitCode: 1, duration: 0, userTerminated: false, truncated: false });
return;
}
if (!fs.existsSync(workDir)) {
resolve({ success: false, stdout: '', stderr: `工作目录不存在: ${workDir}`, exitCode: 1, duration: 0, userTerminated: false, truncated: false });
return;
}
sendLog('info', `🔧 execWorkspaceCommand`, `${command.slice(0, 200)} | cwd: ${workDir}`);
const startTime = Date.now();
const MAX_OUTPUT = 5 * 1024 * 1024; // 5MB 输出上限
let stdoutBuf = '';
let stderrBuf = '';
let truncated = false;
try {
const isWin = process.platform === 'win32';
const shell = isWin ? 'cmd.exe' : 'bash';
const shellArgs = isWin ? ['/c', command] : ['-c', command];
const proc = spawn(shell, shellArgs, {
cwd: workDir,
env: { ...process.env, ...(isWin ? {} : { TERM: 'xterm-256color' }) },
stdio: ['pipe', 'pipe', 'pipe']
});
activeProcesses.set(cmdId, proc);
proc.stdout.on('data', (data: Buffer) => {
if (stdoutBuf.length < MAX_OUTPUT) {
stdoutBuf += data.toString();
if (stdoutBuf.length > MAX_OUTPUT) {
stdoutBuf = stdoutBuf.slice(0, MAX_OUTPUT);
truncated = true;
}
} else {
truncated = true;
}
});
proc.stderr.on('data', (data: Buffer) => {
if (stderrBuf.length < MAX_OUTPUT) {
stderrBuf += data.toString();
if (stderrBuf.length > MAX_OUTPUT) {
stderrBuf = stderrBuf.slice(0, MAX_OUTPUT);
truncated = true;
}
} else {
truncated = true;
}
});
proc.on('close', (code) => {
activeProcesses.delete(cmdId);
const duration = Date.now() - startTime;
sendLog(
code === 0 ? 'success' : 'warn',
`🔧 execWorkspaceCommand 完成`,
`code: ${code} | ${duration}ms | stdout: ${stdoutBuf.length}B | stderr: ${stderrBuf.length}B`
);
resolve({
success: code === 0,
stdout: stdoutBuf,
stderr: stderrBuf,
exitCode: code,
duration,
userTerminated: false,
truncated
});
});
proc.on('error', (err) => {
activeProcesses.delete(cmdId);
const duration = Date.now() - startTime;
sendLog('error', `🔧 execWorkspaceCommand 失败`, `${cmdId} | ${err.message}`);
resolve({
success: false,
stdout: stdoutBuf,
stderr: stderrBuf + (stderrBuf ? '\n' : '') + `进程启动失败: ${err.message}`,
exitCode: 1,
duration,
userTerminated: false,
truncated
});
});
} catch (err) {
const duration = Date.now() - startTime;
sendLog('error', `🔧 execWorkspaceCommand 异常`, (err as Error).message);
resolve({
success: false,
stdout: stdoutBuf,
stderr: (err as Error).message,
exitCode: 1,
duration,
userTerminated: false,
truncated: false
});
}
});
}
/**
* 终止 execWorkspaceCommand 启动的进程
* @returns 是否成功终止
*/
export function terminateWorkspaceCommand(commandId: string): boolean {
return killProcess(commandId);
}
+3 -2
View File
@@ -150,9 +150,10 @@ export function initSettingsModal(): void {
if (db) await db.saveSetting('runCommandEnabled', toggleRunCommand.checked);
setToolEnabled('run_command', toggleRunCommand.checked);
if (toggleRunCommand.checked) {
showToast('⚠️ 命令执行已开启,请谨慎使用', 'warning');
logWarn('命令执行已开启(高风险)');
showToast('命令执行已开启(每次执行需确认)', 'info');
logInfo('命令执行已开启');
} else {
showToast('命令执行已关闭', 'info');
logInfo('命令执行已关闭');
}
});
+3 -3
View File
@@ -191,7 +191,7 @@ async function init(): Promise<void> {
// ── 防御性重置:确保首次启动不会遗留脏状态 ──
state.set('toolCallingEnabled', false);
state.set('runCommandEnabled', false);
state.set('runCommandEnabled', true); // 默认开启(需确认)
state.set('memoryEnabled', true);
state.set('thinkEnabled', false);
@@ -245,10 +245,10 @@ async function init(): Promise<void> {
// ── Tool Calling 设置 ──
const toolCallingEnabled = await db.getSetting('toolCallingEnabled', false);
const runCommandEnabled = await db.getSetting('runCommandEnabled', false);
const runCommandEnabled = await db.getSetting('runCommandEnabled', true); // 默认开启(需确认)
state.set('toolCallingEnabled', toolCallingEnabled);
state.set('runCommandEnabled', runCommandEnabled);
if (runCommandEnabled) setToolEnabled('run_command', true);
if (!runCommandEnabled) setToolEnabled('run_command', false); // 仅在用户明确关闭时禁用
const toggleTC = document.querySelector<HTMLInputElement>('#toggleToolCalling');
if (toggleTC) toggleTC.checked = toolCallingEnabled;
const toggleRC = document.querySelector<HTMLInputElement>('#toggleRunCommand');
+29 -10
View File
@@ -13,6 +13,7 @@ import {
import { searchMemories, buildMemoryContext, markMemoryUsed, isMemoryEnabled } from './memory-manager.js';
import { showToast } from '../components/toast.js';
import { logInfo, logWarn, logError, logToolStart, logToolResult, logThink, logStream, logAgentLoop, logModelResponse } from './log-service.js';
import { getWorkspaceDirPath } from '../components/workspace-panel.js';
import type {
OllamaMessage,
OllamaStreamChunk,
@@ -31,7 +32,9 @@ const TOOL_USAGE_GUIDE = `【工具使用规则】
2. 工具调用结果中的 path 字段是权威的文件路径,优先使用它。
3. 如果对话中从未出现过相关路径,应先用 search_files 或 list_directory 定位,不要凭空猜测路径。
4. 对同一文件的连续操作(读取→修改、读取→删除),路径必须一致。
5. 当用户要求执行命令时,使用 run_command 工具执行。执行完成后,将 stdout/stderr 的结果告知用户。对于需要实时查看进度的长时间运行命令(如 npm install、ollama pull),建议用户在工作空间面板中手动执行。`;
5. 当用户要求执行命令时,使用 run_command 工具执行。run_command 通过工作空间进程管理执行,无超时限制,支持长时间运行的命令。执行完成后,将 stdout/stderr 的结果告知用户。
6. 如果 run_command 返回 userTerminated=true,说明用户手动终止了命令,请据此告知用户。
7. 如果返回 truncated=true,说明命令输出超过 5MB 被截断,应告知用户完整输出可在工作空间面板查看。`;
export interface AgentCallbacks {
onThinking: (text: string) => void;
@@ -83,6 +86,15 @@ export async function runAgentLoop(
}
}
// 注入工作空间上下文
const workspaceDir = getWorkspaceDirPath();
if (workspaceDir) {
systemPromptParts.push(`【工作空间】
当前工作空间目录: ${workspaceDir}
你可以使用 run_command 工具在此目录下执行命令,所有命令通过工作空间进程管理执行,无超时限制。
文件操作工具(read_file、write_file 等)的相对路径基于此目录解析。`);
}
if (systemPromptParts.length > 0) {
messages.push({ role: 'system', content: systemPromptParts.join('\n\n') + '\n\n' + TOOL_USAGE_GUIDE });
} else {
@@ -287,16 +299,23 @@ export async function runAgentLoop(
}
try {
// 工具执行超时保护
// 工具执行run_command 无超时,其他工具 30 秒超时)
logInfo(`执行工具: ${call.function.name}`, JSON.stringify(call.function.arguments));
const result = await Promise.race([
executeTool(call.function.name, call.function.arguments).catch(err =>
({ success: false, error: err?.message || String(err) }) as ToolResult
),
new Promise<ToolResult>((_, reject) =>
setTimeout(() => reject(new Error('工具执行超时(30秒)')), TOOL_EXEC_TIMEOUT)
)
]);
let result: ToolResult;
if (call.function.name === 'run_command') {
// run_command 通过 workspace IPC 执行,无超时限制
result = await executeTool(call.function.name, call.function.arguments);
} else {
// 其他工具保持 30 秒超时保护
result = await Promise.race([
executeTool(call.function.name, call.function.arguments).catch(err =>
({ success: false, error: err?.message || String(err) }) as ToolResult
),
new Promise<ToolResult>((_, reject) =>
setTimeout(() => reject(new Error('工具执行超时(30秒)')), TOOL_EXEC_TIMEOUT)
)
]);
}
const record: ToolCallRecord = {
name: call.function.name,
arguments: call.function.arguments,
+27 -5
View File
@@ -108,14 +108,13 @@ export const TOOL_DEFINITIONS: ToolDefinition[] = [
type: 'function',
function: {
name: 'run_command',
description: 'Execute a shell command. DANGEROUS: disabled by default, must be enabled in settings.',
description: 'Execute a shell command via workspace. No timeout limit — runs until the process exits. Requires user confirmation. Output is streamed in real-time through the workspace process.',
parameters: {
type: 'object',
required: ['command'],
properties: {
command: { type: 'string', description: 'Shell command to execute.' },
cwd: { type: 'string', description: 'Working directory.' },
timeout: { type: 'integer', description: 'Timeout ms. Max 120000.' }
cwd: { type: 'string', description: 'Working directory. Defaults to workspace directory.' }
}
}
}
@@ -130,7 +129,8 @@ export function needsConfirmation(toolName: string): boolean {
let enabledTools: Set<string> = new Set([
'read_file', 'list_directory', 'search_files',
'write_file', 'create_directory', 'delete_file'
'write_file', 'create_directory', 'delete_file',
'run_command' // 默认启用,需要用户确认
]);
export function setToolEnabled(toolName: string, enabled: boolean): void {
@@ -160,7 +160,29 @@ export async function executeTool(toolName: string, args: Record<string, unknown
try {
logToolStart(toolName, JSON.stringify(args));
// IPC 调用 + 25 秒硬超时(比 agent-engine 的 30 秒先触发)
// run_command 走 workspace 无超时 IPC,其他工具走标准 IPC
if (toolName === 'run_command') {
const command = args.command as string;
const cwd = args.cwd as string | undefined;
if (!command) {
return { success: false, error: '缺少 command 参数' };
}
logInfo(`Workspace 命令执行: ${command.slice(0, 100)}`);
const result = await bridge.workspace.execTool(command, cwd);
logToolResult('run_command', result.success, result.success ? undefined : result.stderr?.slice(0, 200));
return {
success: result.success,
stdout: result.stdout,
stderr: result.stderr,
exitCode: result.exitCode,
duration: result.duration,
userTerminated: result.userTerminated,
truncated: result.truncated
};
}
// 其他工具:IPC 调用 + 25 秒硬超时
const result = await Promise.race([
bridge.tool.execute(toolName, args),
new Promise<ToolResult>((_, reject) =>
+7 -3
View File
@@ -199,7 +199,7 @@ html, body {
-webkit-backdrop-filter: saturate(180%) blur(20px);
border-bottom: 1px solid var(--border-subtle);
flex-shrink: 0;
z-index: 10;
z-index: 50;
}
.header-left {
@@ -392,6 +392,8 @@ html, body {
background: var(--bg-layer-alt);
border-bottom: 1px solid var(--border-subtle);
flex-shrink: 0;
z-index: 40;
position: relative;
}
.model-bar-icon {
@@ -1046,7 +1048,8 @@ html, body {
backdrop-filter: saturate(180%) blur(20px);
-webkit-backdrop-filter: saturate(180%) blur(20px);
border-top: 1px solid var(--border-subtle);
z-index: 20;
z-index: 30;
position: relative;
pointer-events: auto;
}
@@ -2664,12 +2667,13 @@ html, body {
.workspace-panel {
position: fixed;
top: 0;
top: 90px; /* header(44px) + model-bar(46px) */
right: 0;
bottom: 0;
width: 420px;
background: var(--bg-solid, #202020);
border-left: 1px solid var(--border-default, rgba(255,255,255,0.08));
border-top: 1px solid var(--border-default, rgba(255,255,255,0.08));
display: flex;
flex-direction: column;
z-index: 10;
+2
View File
@@ -239,6 +239,8 @@ export interface MetonaDesktopAPI {
kill: (id: string) => void;
onOutput: (callback: (data: { id: string; type: 'stdout' | 'stderr'; data: string }) => void) => void;
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 }>;
};
}