From 7ca7bddba43358559004d2dcc687db2c7c7fd684 Mon Sep 17 00:00:00 2001 From: JOJO <1498581755@qq.com> Date: Tue, 18 Nov 2025 10:27:23 +0800 Subject: [PATCH] feat(web): make thinking toggle usable --- core/web_terminal.py | 5 ++++- static/app.js | 44 +++++++++++++++++++++++++++++++++++--------- static/index.html | 14 +++++++------- web_server.py | 37 ++++++++++++++++++++++++++++++++++++- 4 files changed, 82 insertions(+), 18 deletions(-) diff --git a/core/web_terminal.py b/core/web_terminal.py index 8ee8781..5b6881b 100644 --- a/core/web_terminal.py +++ b/core/web_terminal.py @@ -252,7 +252,10 @@ class WebTerminal(MainTerminal): # 构建状态信息 status = { "project_path": self.project_path, - "thinking_mode": self.thinking_mode, + "thinking_mode": { + "enabled": self.thinking_mode, + "label": self.get_thinking_mode_status() + }, "thinking_status": self.get_thinking_mode_status(), "context": { "usage_percent": context_status['usage_percent'], diff --git a/static/app.js b/static/app.js index 237c414..82205d3 100644 --- a/static/app.js +++ b/static/app.js @@ -110,8 +110,7 @@ async function bootstrapApp() { // 系统信息 projectPath: '', agentVersion: '', - thinkingMode: '未知', - nextThinkingMode: null, + thinkingMode: true, // true=思考模式, false=快速模式 // 消息相关 messages: [], @@ -593,8 +592,11 @@ async function bootstrapApp() { this.socket.on('system_ready', (data) => { this.projectPath = data.project_path || ''; this.agentVersion = data.version || this.agentVersion; - this.thinkingMode = data.thinking_mode || '未知'; - this.nextThinkingMode = null; + if (typeof data.thinking_mode === 'object') { + this.thinkingMode = !!data.thinking_mode.enabled; + } else { + this.thinkingMode = data.thinking_mode === '思考模式'; + } console.log('系统就绪:', data); // 系统就绪后立即加载对话列表 @@ -1291,6 +1293,7 @@ async function bootstrapApp() { this.toolMenuOpen = false; this.toolSettingsLoading = false; this.toolSettings = []; + this.thinkingMode = false; console.log('前端状态重置完成'); }, @@ -1324,11 +1327,9 @@ async function bootstrapApp() { this.projectPath = statusData.project_path || ''; this.agentVersion = statusData.version || this.agentVersion; if (statusData.thinking_mode) { - this.thinkingMode = statusData.thinking_mode.label || '未知'; - this.nextThinkingMode = statusData.thinking_mode.next ?? null; + this.thinkingMode = !!statusData.thinking_mode.enabled; } else { - this.thinkingMode = '未知'; - this.nextThinkingMode = null; + this.thinkingMode = false; } // 获取当前对话信息 @@ -1862,7 +1863,7 @@ async function bootstrapApp() { 'Content-Type': 'application/json' }, body: JSON.stringify({ - thinking_mode: this.thinkingMode !== '快速模式' + thinking_mode: this.thinkingMode }) }); @@ -1987,6 +1988,31 @@ async function bootstrapApp() { toggleSidebar() { this.sidebarCollapsed = !this.sidebarCollapsed; }, + + async toggleThinkingMode() { + try { + const resp = await fetch('/api/thinking-mode', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ thinking_mode: !this.thinkingMode }) + }); + const data = await resp.json(); + if (data.success && data.data) { + if (typeof data.data === 'object') { + this.thinkingMode = !!data.data.enabled; + } else { + this.thinkingMode = data.data === '思考模式'; + } + } else { + throw new Error(data.message || data.error || '切换失败'); + } + } catch (error) { + console.error('切换思考模式失败:', error); + alert(`切换思考模式失败: ${error.message}`); + } + }, formatTime(timeString) { if (!timeString) return ''; diff --git a/static/index.html b/static/index.html index 8502957..567c488 100644 --- a/static/index.html +++ b/static/index.html @@ -45,7 +45,7 @@ {{ agentVersion }}
- {{ thinkingMode }} + {{ thinkingMode ? '思考模式' : '快速模式' }} {{ isConnected ? '已连接' : '未连接' }} @@ -534,18 +534,18 @@ :disabled="streamingMessage || !isConnected"> {{ rightCollapsed ? '展开聚焦面板' : '折叠聚焦面板' }} + -
diff --git a/web_server.py b/web_server.py index ccf3b17..50a4ac9 100644 --- a/web_server.py +++ b/web_server.py @@ -69,6 +69,13 @@ stop_flags: Dict[str, Dict[str, Any]] = {} DEFAULT_PORT = 8091 +def serialize_thinking_mode(terminal: WebTerminal) -> Dict[str, Any]: + """构造思考模式状态payload,包含是否启用与显示标签。""" + return { + "enabled": bool(getattr(terminal, "thinking_mode", False)), + "label": terminal.get_thinking_mode_status() if hasattr(terminal, "get_thinking_mode_status") else ("思考模式" if getattr(terminal, "thinking_mode", False) else "快速模式") + } + def format_read_file_result(result_data: Dict) -> str: """格式化 read_file 工具的输出,便于在Web端展示。""" if not isinstance(result_data, dict): @@ -497,6 +504,34 @@ def get_status(terminal: WebTerminal, workspace: UserWorkspace, username: str): status['version'] = AGENT_VERSION return jsonify(status) +@app.route('/api/thinking-mode', methods=['POST']) +@api_login_required +@with_terminal +def update_thinking_mode(terminal: WebTerminal, workspace: UserWorkspace, username: str): + """切换思考模式""" + try: + data = request.get_json() or {} + desired_mode = bool(data.get('thinking_mode')) + terminal.thinking_mode = desired_mode + terminal.api_client.thinking_mode = desired_mode + terminal.api_client.start_new_task() + session['thinking_mode'] = desired_mode + + status = terminal.get_status() + socketio.emit('status_update', status, room=f"user_{username}") + + return jsonify({ + "success": True, + "data": status.get("thinking_mode") + }) + except Exception as exc: + print(f"[API] 切换思考模式失败: {exc}") + return jsonify({ + "success": False, + "error": str(exc), + "message": "切换思考模式时发生异常" + }), 500 + @app.route('/api/files') @api_login_required @with_terminal @@ -1053,7 +1088,7 @@ def handle_connect(): reset_system_state(terminal) emit('system_ready', { 'project_path': str(workspace.project_path), - 'thinking_mode': terminal.get_thinking_mode_status(), + 'thinking_mode': serialize_thinking_mode(terminal), 'version': AGENT_VERSION }, room=request.sid)