feat: 优化 TodoWrite 工具的前端展示效果

新增功能:
- 特殊识别和处理 TodoWrite 工具调用和响应
- 实时展示任务进度(百分比和进度条)
- 可视化任务状态:[ ] pending, [→] in_progress, [✓] completed
- 橙色渐变主题区分 TodoWrite 和普通工具

样式优化:
- 添加 .todo-write-call 和 .todo-write-progress 样式
- 进度条动画效果
- 任务列表悬停效果
- 不同状态的任务使用不同颜色

用户体验提升:
- 清晰的进度百分比显示
- 直观的任务完成状态
- 实时更新的进度条

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
hughhuangandClaude Sonnet 4.5 committed 2026-01-30 18:09:22 +08:00
1 parent fa30b210ff
commit 98f5ec2d27
2 files changed
+201 -2

No files matched your search

+95
View File
@@ -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;
+106 -2
View File
@@ -196,13 +196,23 @@ async function streamMessageWithTools(message) {
break;
case 'tool_call':
// 显示工具调用
// 特殊处理 TodoWrite 工具调用
if (data.tool_name === 'TodoWrite') {
appendTodoWriteCall(data.arguments);
} else {
// 显示普通工具调用
appendToolCall(data.tool_name, data.arguments);
}
break;
case 'tool_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 = `<span class="todo-status">${statusIcon}</span> <span class="todo-content">${todo.content}</span>`;
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');