From 98f5ec2d27530ae72153eaf9d9d6bd3ca27b141d Mon Sep 17 00:00:00 2001 From: hugh <1012760428@qq.com> Date: Fri, 30 Jan 2026 18:09:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=20TodoWrite=20?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E7=9A=84=E5=89=8D=E7=AB=AF=E5=B1=95=E7=A4=BA?= =?UTF-8?q?=E6=95=88=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增功能: - 特殊识别和处理 TodoWrite 工具调用和响应 - 实时展示任务进度(百分比和进度条) - 可视化任务状态:[ ] pending, [→] in_progress, [✓] completed - 橙色渐变主题区分 TodoWrite 和普通工具 样式优化: - 添加 .todo-write-call 和 .todo-write-progress 样式 - 进度条动画效果 - 任务列表悬停效果 - 不同状态的任务使用不同颜色 用户体验提升: - 清晰的进度百分比显示 - 直观的任务完成状态 - 实时更新的进度条 Co-Authored-By: Claude Sonnet 4.5 --- src/main/resources/static/css/style.css | 95 ++++++++++++++++++++ src/main/resources/static/js/chat.js | 112 +++++++++++++++++++++++- 2 files changed, 203 insertions(+), 4 deletions(-) diff --git a/src/main/resources/static/css/style.css b/src/main/resources/static/css/style.css index 5eb6a1f..de4d5db 100644 --- a/src/main/resources/static/css/style.css +++ b/src/main/resources/static/css/style.css @@ -277,6 +277,101 @@ body { animation: slideIn 0.3s ease-out; } +/* TodoWrite 工具调用样式 */ +.todo-write-call { + background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%); + border-radius: 12px; + padding: 16px; + margin: 12px 0; + box-shadow: 0 4px 12px rgba(245, 158, 11, 0.2); + animation: slideIn 0.3s ease-out; +} + +.todo-write-header { + font-weight: 600; + color: white; + display: flex; + align-items: center; + gap: 8px; + font-size: 15px; +} + +/* TodoWrite 进度展示样式 */ +.todo-write-progress { + background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%); + border-radius: 12px; + padding: 16px; + margin: 12px 0; + box-shadow: 0 4px 12px rgba(245, 158, 11, 0.2); + animation: slideIn 0.3s ease-out; +} + +.todo-progress-header { + font-weight: 600; + color: white; + margin-bottom: 12px; + font-size: 15px; +} + +.progress-bar-container { + background: rgba(255, 255, 255, 0.3); + border-radius: 8px; + height: 8px; + margin-bottom: 12px; + overflow: hidden; +} + +.progress-bar { + background: white; + height: 100%; + border-radius: 8px; + transition: width 0.3s ease; +} + +.todo-list { + background: rgba(255, 255, 255, 0.95); + border-radius: 8px; + padding: 12px; +} + +.todo-item { + padding: 8px 12px; + margin: 4px 0; + border-radius: 6px; + display: flex; + align-items: center; + gap: 8px; + font-size: 14px; + transition: background 0.2s; +} + +.todo-item:hover { + background: rgba(245, 158, 11, 0.1); +} + +.todo-item.todo-pending { + color: #6b7280; +} + +.todo-item.todo-in_progress { + color: #2563eb; + font-weight: 500; +} + +.todo-item.todo-completed { + color: #059669; +} + +.todo-status { + font-family: 'Courier New', monospace; + font-weight: bold; + min-width: 24px; +} + +.todo-content { + flex: 1; +} + .tool-call-header { font-weight: 600; color: white; diff --git a/src/main/resources/static/js/chat.js b/src/main/resources/static/js/chat.js index d776846..8af69ea 100644 --- a/src/main/resources/static/js/chat.js +++ b/src/main/resources/static/js/chat.js @@ -196,13 +196,23 @@ async function streamMessageWithTools(message) { break; case 'tool_call': - // 显示工具调用 - appendToolCall(data.tool_name, data.arguments); + // 特殊处理 TodoWrite 工具调用 + if (data.tool_name === 'TodoWrite') { + appendTodoWriteCall(data.arguments); + } else { + // 显示普通工具调用 + appendToolCall(data.tool_name, data.arguments); + } break; case 'tool_response': - // 显示工具响应 - appendToolResult(data.tool_name, data.response); + // 特殊处理 TodoWrite 工具响应 + if (data.tool_name === 'TodoWrite') { + appendTodoWriteProgress(data.response); + } else { + // 显示普通工具响应 + appendToolResult(data.tool_name, data.response); + } break; case 'assistant_message': @@ -314,6 +324,100 @@ function createAssistantMessageWithoutLoading() { // 显示工具调用(已废弃,直接在 sendMessageWithTools 中处理) +// 添加 TodoWrite 工具调用展示 +function appendTodoWriteCall(arguments) { + const todoCallDiv = document.createElement('div'); + todoCallDiv.className = 'todo-write-call'; + + const headerDiv = document.createElement('div'); + headerDiv.className = 'todo-write-header'; + headerDiv.innerHTML = '📋 创建任务列表'; + + todoCallDiv.appendChild(headerDiv); + messagesContainer.appendChild(todoCallDiv); + scrollToBottom(); +} + +// 添加 TodoWrite 进度展示 +function appendTodoWriteProgress(response) { + try { + // 尝试解析 JSON 响应 + let todos = []; + if (typeof response === 'string') { + // 如果响应包含 JSON,尝试提取 + const jsonMatch = response.match(/\{[\s\S]*"todos"[\s\S]*\}/); + if (jsonMatch) { + const data = JSON.parse(jsonMatch[0]); + todos = data.todos || []; + } + } else if (response.todos) { + todos = response.todos; + } + + if (todos.length === 0) { + // 如果无法解析,显示原始响应 + appendToolResult('TodoWrite', response); + return; + } + + // 计算进度 + const total = todos.length; + const completed = todos.filter(t => t.status === 'completed').length; + const inProgress = todos.filter(t => t.status === 'in_progress').length; + const percentage = total > 0 ? Math.round((completed / total) * 100) : 0; + + // 创建进度展示元素 + const progressDiv = document.createElement('div'); + progressDiv.className = 'todo-write-progress'; + + // 进度头部 + const headerDiv = document.createElement('div'); + headerDiv.className = 'todo-progress-header'; + headerDiv.innerHTML = `📊 任务进度: ${completed}/${total} 已完成 (${percentage}%)`; + + // 进度条 + const progressBarContainer = document.createElement('div'); + progressBarContainer.className = 'progress-bar-container'; + + const progressBar = document.createElement('div'); + progressBar.className = 'progress-bar'; + progressBar.style.width = `${percentage}%`; + + progressBarContainer.appendChild(progressBar); + + // 任务列表 + const todoListDiv = document.createElement('div'); + todoListDiv.className = 'todo-list'; + + todos.forEach(todo => { + const todoItem = document.createElement('div'); + todoItem.className = `todo-item todo-${todo.status}`; + + let statusIcon = '[ ]'; + if (todo.status === 'completed') { + statusIcon = '[✓]'; + } else if (todo.status === 'in_progress') { + statusIcon = '[→]'; + } + + todoItem.innerHTML = `${statusIcon} ${todo.content}`; + todoListDiv.appendChild(todoItem); + }); + + progressDiv.appendChild(headerDiv); + progressDiv.appendChild(progressBarContainer); + progressDiv.appendChild(todoListDiv); + + messagesContainer.appendChild(progressDiv); + scrollToBottom(); + + } catch (error) { + console.error('解析 TodoWrite 响应失败:', error); + // 降级到普通工具响应展示 + appendToolResult('TodoWrite', response); + } +} + // 添加工具调用展示 function appendToolCall(name, arguments) { const toolCallDiv = document.createElement('div');