+
${title}
+ ${isTodoWrite ? '' : `
${this.escapeHtml(ChatUtils.formatJson(args))}
`}
+ ${details}
+
+ `;
// 如果是 TodoWrite,渲染任务列表
if (isTodoWrite) {
+ const todoListContainer = step.querySelector('.step-todo-list');
const todoList = this.renderTodoList(args);
- if (todoList) {
- div.appendChild(todoList);
+ if (todoList && todoListContainer) {
+ todoListContainer.appendChild(todoList);
}
- } else {
- // 普通工具调用,渲染参数
- const details = document.createElement('div');
- details.className = 'tool-call-details';
- details.textContent = '参数: ' + ChatUtils.formatJson(args);
- div.appendChild(details);
}
- this.elements.messagesContainer.appendChild(div);
+ stepsList.appendChild(step);
+ lucide.createIcons();
this.scrollToBottom();
- return div;
+ this.updateProcessStatus('calling-tool');
},
/**
- * 渲染工具响应
+ * 添加工具响应步骤
*/
- renderToolResult(name, result, options = {}) {
- const { isTodoWrite = false } = options;
+ addToolResponseStep(toolName, response, isTodoWrite = false) {
+ if (!this.activeProcessContainer) return;
+
+ const { stepsList } = this.activeProcessContainer;
+
+ const step = document.createElement('div');
+ step.className = 'process-step tool-response';
if (isTodoWrite) {
- return this.renderTodoProgress(result);
+ // 更新任务列表为进度视图
+ step.className += ' todo-progress-step';
+ const todoProgress = this.createTodoProgressElement(response);
+ step.appendChild(todoProgress);
+ } else {
+ let displayResponse = typeof response === 'string' ? response : ChatUtils.formatJson(response);
+ displayResponse = this.truncateText(displayResponse, 200);
+
+ step.innerHTML = `
+
+
工具返回: ${toolName}
+
${this.escapeHtml(displayResponse)}
+
+ `;
}
- const div = document.createElement('div');
- div.className = 'tool-result animate-in';
-
- const header = document.createElement('div');
- header.className = 'tool-result-header';
- header.innerHTML = `📄 工具返回: ${name}`;
-
- const content = document.createElement('div');
- content.className = 'tool-result-content';
- content.textContent = typeof result === 'string' ? result : ChatUtils.formatJson(result);
-
- div.appendChild(header);
- div.appendChild(content);
- this.elements.messagesContainer.appendChild(div);
+ stepsList.appendChild(step);
+ lucide.createIcons();
this.scrollToBottom();
- return div;
+ this.updateProcessStatus('got-result');
+ },
+
+ /**
+ * 设置最终回复内容
+ */
+ setFinalResponse(content) {
+ if (!this.activeProcessContainer) {
+ this.createAssistantMessageContainer();
+ }
+
+ const { finalResponse, detailsHeader } = this.activeProcessContainer;
+
+ finalResponse.className = 'final-response';
+ finalResponse.innerHTML = this.renderMarkdown(content);
+ this.addCodeCopyButtons(finalResponse);
+
+ // 更新头部状态为完成
+ const statusText = detailsHeader.querySelector('.status-text');
+ const statusIcon = detailsHeader.querySelector('.process-status i');
+ if (statusText) statusText.textContent = '已完成';
+ if (statusIcon) {
+ statusIcon.setAttribute('data-lucide', 'check-circle');
+ statusIcon.classList.remove('spinning');
+ }
+
+ lucide.createIcons();
+ this.scrollToBottom();
+
+ // 重置活动容器
+ this.activeProcessContainer = null;
+ },
+
+ /**
+ * 更新过程状态
+ */
+ updateProcessStatus(status) {
+ if (!this.activeProcessContainer) return;
+
+ const { detailsHeader } = this.activeProcessContainer;
+ const statusText = detailsHeader.querySelector('.status-text');
+
+ const statusMap = {
+ 'thinking': '思考中...',
+ 'calling-tool': '调用工具...',
+ 'got-result': '处理结果...'
+ };
+
+ if (statusText && statusMap[status]) {
+ statusText.textContent = statusMap[status];
+ }
+ },
+
+ // ==================== 旧接口兼容 ====================
+
+ /**
+ * 渲染 AI 消息(兼容旧接口)
+ */
+ renderAssistantMessage(content, options = {}) {
+ if (options.isLoading) {
+ this.createAssistantMessageContainer();
+ return this.activeProcessContainer.group;
+ }
+
+ this.setFinalResponse(content);
+ return this.activeProcessContainer?.group;
+ },
+
+ /**
+ * 渲染工具调用(兼容旧接口,转发到新方法)
+ */
+ renderToolCall(name, args, options = {}) {
+ this.addToolCallStep(name, args, options.isTodoWrite);
+ },
+
+ /**
+ * 渲染工具响应(兼容旧接口,转发到新方法)
+ */
+ renderToolResult(name, result, options = {}) {
+ this.addToolResponseStep(name, result, options.isTodoWrite);
},
// ==================== TodoWrite 渲染 ====================
@@ -193,10 +426,23 @@ const ChatUI = {
if (!todos || todos.length === 0) return null;
const listDiv = document.createElement('div');
- listDiv.className = 'todo-list';
+ listDiv.className = 'todo-list-compact';
todos.forEach(todo => {
- const item = this.createTodoItem(todo);
+ const item = document.createElement('div');
+ item.className = `todo-item-compact ${todo.status}`;
+
+ const icons = {
+ pending: '