feat: 主界面支持伪流式接口,实时展示工具调用步骤
- 添加 streamMessageWithTools 函数支持伪流式接口 - 实时展示工具调用、工具响应、AI 思考过程 - 更新欢迎消息,说明新功能特性 - 保留原有 sendMessageWithTools 函数用于兼容 功能特性: - 🔧 工具调用实时可见 - ✅ 工具响应实时展示 - 💬 AI 中间思考过程可见 - 📊 完整执行流程可视化 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
efc330ae3d
commit
5270f2a55e
1 file changed
+108
-7
@@ -57,10 +57,10 @@ chatForm.addEventListener('submit', async function(e) {
|
||||
// 根据切换状态选择接口
|
||||
const useTools = toolToggle.checked;
|
||||
if (useTools) {
|
||||
// 使用工具调用(非流式)
|
||||
await sendMessageWithTools(message);
|
||||
// 使用伪流式接口(实时展示工具调用步骤)
|
||||
await streamMessageWithTools(message);
|
||||
} else {
|
||||
// 使用流式响应
|
||||
// 使用传统流式响应
|
||||
await streamMessage(message);
|
||||
}
|
||||
});
|
||||
@@ -148,7 +148,99 @@ async function streamMessage(message) {
|
||||
}
|
||||
}
|
||||
|
||||
// 非流式发送消息(支持工具调用)
|
||||
// 伪流式发送消息(支持工具调用的实时展示)
|
||||
async function streamMessageWithTools(message) {
|
||||
updateConnectionStatus(true);
|
||||
|
||||
try {
|
||||
const response = await fetch(`/api/chat/stream-with-tools?conversationId=${conversationId}`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ message })
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
let buffer = '';
|
||||
let assistantMessageElement = null;
|
||||
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
|
||||
if (done) break;
|
||||
|
||||
buffer += decoder.decode(value, { stream: true });
|
||||
const lines = buffer.split('\n\n');
|
||||
buffer = lines.pop(); // 保留不完整的行
|
||||
|
||||
for (const line of lines) {
|
||||
if (!line.trim()) continue;
|
||||
|
||||
const eventMatch = line.match(/event: (\w+)/);
|
||||
const dataMatch = line.match(/data: (.+)/);
|
||||
|
||||
if (eventMatch && dataMatch) {
|
||||
const eventType = eventMatch[1];
|
||||
const data = JSON.parse(dataMatch[1]);
|
||||
|
||||
// 根据事件类型处理
|
||||
switch(eventType) {
|
||||
case 'user_message':
|
||||
// 用户消息已经显示,跳过
|
||||
break;
|
||||
|
||||
case 'tool_call':
|
||||
// 显示工具调用
|
||||
appendToolCall(data.tool_name, data.arguments);
|
||||
break;
|
||||
|
||||
case 'tool_response':
|
||||
// 显示工具响应
|
||||
appendToolResult(data.tool_name, data.response);
|
||||
break;
|
||||
|
||||
case 'assistant_message':
|
||||
// 显示 AI 中间响应
|
||||
if (!assistantMessageElement) {
|
||||
assistantMessageElement = createAssistantMessageWithoutLoading();
|
||||
}
|
||||
updateAssistantMessage(assistantMessageElement, data.content);
|
||||
break;
|
||||
|
||||
case 'final_response':
|
||||
// 显示最终响应
|
||||
if (!assistantMessageElement) {
|
||||
assistantMessageElement = createAssistantMessageWithoutLoading();
|
||||
}
|
||||
updateAssistantMessage(assistantMessageElement, data.content);
|
||||
break;
|
||||
|
||||
case 'error':
|
||||
// 显示错误
|
||||
showError(data.message);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
scrollToBottom();
|
||||
|
||||
} catch (error) {
|
||||
console.error('伪流式响应错误:', error);
|
||||
showError('连接错误: ' + error.message);
|
||||
} finally {
|
||||
updateConnectionStatus(false);
|
||||
}
|
||||
}
|
||||
|
||||
// 非流式发送消息(支持工具调用)- 已废弃,保留用于兼容
|
||||
async function sendMessageWithTools(message) {
|
||||
updateConnectionStatus(true);
|
||||
|
||||
@@ -475,16 +567,25 @@ window.addEventListener('load', () => {
|
||||
|
||||
我可以帮助你完成各种任务,包括:
|
||||
|
||||
- 💬 **普通对话**(关闭"启用工具调用"使用流式响应)
|
||||
- 💬 **普通对话**(关闭"启用工具调用"使用传统流式响应)
|
||||
- 🔧 **执行 Shell 命令**
|
||||
- 📁 **读写文件**
|
||||
- 🎯 **使用自定义 Skills**
|
||||
- 📋 **任务管理**(TodoWrite)
|
||||
|
||||
### 🌟 新功能:伪流式接口
|
||||
|
||||
开启"启用工具调用"后,你可以实时看到:
|
||||
- 🔧 **工具调用**:AI 调用了哪些工具
|
||||
- ✅ **工具响应**:工具返回了什么结果
|
||||
- 💬 **AI 思考**:AI 的中间思考过程
|
||||
- 📊 **执行步骤**:完整的执行流程可视化
|
||||
|
||||
### 快速开始:
|
||||
|
||||
1. **开启工具调用**:点击右上角的"启用工具调用"开关
|
||||
2. **发送指令**:例如"列出当前目录的文件"
|
||||
3. **查看执行过程**:工具调用和结果会实时显示
|
||||
2. **发送指令**:例如"请帮我为某某公司执行完整的定价流程"
|
||||
3. **实时观察**:工具调用和执行步骤会实时展示
|
||||
|
||||
现在就开始吧!🚀`;
|
||||
|
||||
|
||||
Reference in new issue
Block a user