chore: 清除所有 console.log,主进程日志接入执行日志面板

This commit is contained in:
thzxx
2026-04-07 19:49:30 +08:00
parent d524339634
commit 34a6b9fd0b
5 changed files with 26 additions and 6 deletions
+9 -4
View File
@@ -7,6 +7,11 @@ import * as fs from 'fs';
import * as path from 'path';
import { mainWindow } from './main.js';
import { showNotification } from './utils.js';
/** 发送日志到渲染进程日志面板 */
function sendLog(level: 'info' | 'success' | 'warn' | 'error' | 'debug', message: string, detail?: string): void {
mainWindow?.webContents.send('main:log', { level, message, detail });
}
import {
handleReadFile,
handleWriteFile,
@@ -84,7 +89,7 @@ export function setupIPC(): void {
// ── Tool Calling IPC ──
ipcMain.handle('tool:execute', async (_, toolName: string, args: Record<string, unknown>) => {
try {
console.log('[IPC] tool:execute', toolName, JSON.stringify(args || {}).slice(0, 200));
sendLog('debug', `🔧 tool:execute ${toolName}`, JSON.stringify(args || {}).slice(0, 200));
switch (toolName) {
case 'read_file': return await handleReadFile(args as { path: string; encoding?: string; start_line?: number; end_line?: number });
case 'write_file': return await handleWriteFile(args as { path: string; content: string; encoding?: string });
@@ -96,7 +101,7 @@ export function setupIPC(): void {
default: return { success: false, error: `未知工具: ${toolName}` };
}
} catch (err) {
console.error('[IPC] tool:execute 异常:', toolName, err);
sendLog('error', ` tool:execute 异常: ${toolName}`, String(err));
return { success: false, error: (err as Error).message || '工具执行异常' };
}
});
@@ -139,7 +144,7 @@ export function setupIPC(): void {
// 启动命令(流式输出,通过 workspace:output 事件推送)
ipcMain.on('workspace:exec', (event, { id, command, cwd }: { id: string; command: string; cwd?: string }) => {
console.log('[IPC] workspace:exec', id, command.slice(0, 200));
sendLog('info', `🖥️ workspace:exec`, `ID: ${id} | ${command.slice(0, 200)}`);
const result = startProcess(
id,
@@ -163,7 +168,7 @@ export function setupIPC(): void {
// 停止命令
ipcMain.on('workspace:kill', (_, id: string) => {
console.log('[IPC] workspace:kill', id);
sendLog('info', `🖥️ workspace:kill`, `ID: ${id}`);
killProcess(id);
});
}
+3
View File
@@ -39,6 +39,9 @@ contextBridge.exposeInMainWorld('metonaDesktop', {
removeAllListeners: (channel: string) => {
ipcRenderer.removeAllListeners(channel);
},
onMainLog: (callback: (data: { level: string; message: string; detail?: string }) => void) => {
ipcRenderer.on('main:log', (_: unknown, data: { level: string; message: string; detail?: string }) => callback(data));
},
workspace: {
getDir: () => ipcRenderer.invoke('workspace:getDir'),
setDir: (dir: string) => ipcRenderer.invoke('workspace:setDir', dir),
+7 -1
View File
@@ -7,8 +7,14 @@ import { spawn, ChildProcess } from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import { app } from 'electron';
import { mainWindow } from './main.js';
import { checkPathAllowed, checkCommandAllowed } from './tool-security.js';
/** 发送日志到渲染进程日志面板 */
function sendLog(level: 'info' | 'success' | 'warn' | 'error' | 'debug', message: string, detail?: string): void {
mainWindow?.webContents.send('main:log', { level, message, detail });
}
export const DEFAULT_WORKSPACE_DIR = path.join(app.getPath('userData'), 'workspace');
/** 活跃进程 Map */
@@ -18,7 +24,7 @@ const activeProcesses = new Map<string, ChildProcess>();
export function ensureWorkspaceDir(): void {
if (!fs.existsSync(DEFAULT_WORKSPACE_DIR)) {
fs.mkdirSync(DEFAULT_WORKSPACE_DIR, { recursive: true });
console.log('[Workspace] 创建默认工作空间目录:', DEFAULT_WORKSPACE_DIR);
sendLog('info', `📁 创建默认工作空间目录`, DEFAULT_WORKSPACE_DIR);
}
}
+6 -1
View File
@@ -26,7 +26,7 @@ import { setToolEnabled } from './services/tool-registry.js';
import { initToolConfirmModal } from './components/tool-confirm-modal.js';
import { initMemoryPanel } from './components/memory-panel.js';
import { initWorkspacePanel } from './components/workspace-panel.js';
import { initLogPanel } from './services/log-service.js';
import { initLogPanel, addLog } from './services/log-service.js';
import { logInfo, logSuccess, logError, logDebug, logInit, logWarn } from './services/log-service.js';
import type { ChatSession } from './types.js';
@@ -78,6 +78,11 @@ function setupDesktopIntegration(): void {
}
}) as EventListener);
// 主进程日志转发到日志面板
bridge.onMainLog((data: { level: string; message: string; detail?: string }) => {
addLog(data.level as any, data.message, data.detail);
});
// 退出时释放显存
bridge.onAppQuit(() => {
const api = state.get<OllamaAPI | null>(KEYS.API);
+1
View File
@@ -229,6 +229,7 @@ export interface MetonaDesktopAPI {
onTrayAction: (callback: (action: string) => void) => void;
onAppQuit: (callback: () => void) => void;
removeAllListeners: (channel: string) => void;
onMainLog: (callback: (data: { level: string; message: string; detail?: string }) => void) => void;
workspace: {
getDir: () => Promise<{ dir: string; defaultDir: string }>;
setDir: (dir: string) => Promise<{ success: boolean; dir?: string; error?: string }>;