259 lines
6.9 KiB
JavaScript
259 lines
6.9 KiB
JavaScript
/**
|
||||
|
|
* 主程序入口
|
|||
|
|
* 整合所有模块,处理事件绑定和业务流程
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
const ChatApp = {
|
|||
|
|
conversationId: null,
|
|||
|
|
isProcessing: false,
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 初始化应用
|
|||
|
|
*/
|
|||
|
|
init() {
|
|||
|
|
// 初始化 DOM 元素
|
|||
|
|
ChatUI.init();
|
|||
|
|
|
|||
|
|
// 初始化会话 ID
|
|||
|
|
this.conversationId = ChatUtils.getOrCreateConversationId();
|
|||
|
|
ChatAPI.init(this.conversationId);
|
|||
|
|
|
|||
|
|
// 绑定事件
|
|||
|
|
this.bindEvents();
|
|||
|
|
|
|||
|
|
// 显示欢迎消息
|
|||
|
|
this.showWelcomeMessage();
|
|||
|
|
|
|||
|
|
console.log('🚀 ChatApp 初始化完成');
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 绑定所有事件
|
|||
|
|
*/
|
|||
|
|
bindEvents() {
|
|||
|
|
const ui = ChatUI.elements;
|
|||
|
|
|
|||
|
|
// 表单提交
|
|||
|
|
ui.chatForm.addEventListener('submit', (e) => this.handleSubmit(e));
|
|||
|
|
|
|||
|
|
// 输入框键盘事件
|
|||
|
|
ui.userInput.addEventListener('keydown', (e) => this.handleKeyDown(e));
|
|||
|
|
|
|||
|
|
// 输入框自动调整高度
|
|||
|
|
ui.userInput.addEventListener('input', () => ChatUI.autoResizeInput());
|
|||
|
|
|
|||
|
|
// 清空对话
|
|||
|
|
ui.clearBtn.addEventListener('click', () => this.handleClear());
|
|||
|
|
|
|||
|
|
// 页面卸载前保存会话 ID
|
|||
|
|
window.addEventListener('beforeunload', () => {
|
|||
|
|
localStorage.setItem('conversationId', this.conversationId);
|
|||
|
|
});
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 处理表单提交
|
|||
|
|
*/
|
|||
|
|
async handleSubmit(e) {
|
|||
|
|
e.preventDefault();
|
|||
|
|
|
|||
|
|
const message = ChatUI.elements.userInput.value.trim();
|
|||
|
|
if (!message || this.isProcessing) return;
|
|||
|
|
|
|||
|
|
// 设置处理状态
|
|||
|
|
this.isProcessing = true;
|
|||
|
|
ChatUI.setInputState({ disabled: true, value: '' });
|
|||
|
|
|
|||
|
|
// 显示用户消息
|
|||
|
|
ChatUI.renderUserMessage(message);
|
|||
|
|
|
|||
|
|
// 根据模式选择处理方式
|
|||
|
|
const useTools = ChatUI.isToolEnabled();
|
|||
|
|
if (useTools) {
|
|||
|
|
await this.handleStreamWithTools(message);
|
|||
|
|
} else {
|
|||
|
|
await this.handleStream(message);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 恢复输入状态
|
|||
|
|
this.isProcessing = false;
|
|||
|
|
ChatUI.setInputState({ disabled: false });
|
|||
|
|
ChatUI.elements.userInput.focus();
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 处理键盘事件
|
|||
|
|
*/
|
|||
|
|
handleKeyDown(e) {
|
|||
|
|
if (e.key === 'Enter' && !e.shiftKey) {
|
|||
|
|
e.preventDefault();
|
|||
|
|
ChatUI.elements.chatForm.dispatchEvent(new Event('submit'));
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 处理传统流式响应
|
|||
|
|
*/
|
|||
|
|
async handleStream(message) {
|
|||
|
|
ChatUI.updateConnectionStatus(true);
|
|||
|
|
|
|||
|
|
// 创建加载消息
|
|||
|
|
const messageEl = ChatUI.renderAssistantMessage('', { isLoading: true });
|
|||
|
|
let content = '';
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
for await (const event of ChatAPI.streamMessage(message)) {
|
|||
|
|
if (event.type === 'text') {
|
|||
|
|
content += event.content;
|
|||
|
|
ChatUI.updateMessage(messageEl, content);
|
|||
|
|
} else if (event.type === 'error') {
|
|||
|
|
ChatUI.showError(event.message);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('流式响应错误:', error);
|
|||
|
|
ChatUI.showError('连接错误: ' + error.message);
|
|||
|
|
messageEl.remove();
|
|||
|
|
} finally {
|
|||
|
|
ChatUI.updateConnectionStatus(false);
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 处理伪流式响应(支持工具调用)
|
|||
|
|
*/
|
|||
|
|
async handleStreamWithTools(message) {
|
|||
|
|
ChatUI.updateConnectionStatus(true);
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
for await (const event of ChatAPI.streamMessageWithTools(message)) {
|
|||
|
|
await this.handleStreamEvent(event);
|
|||
|
|
}
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('伪流式响应错误:', error);
|
|||
|
|
ChatUI.showError('连接错误: ' + error.message);
|
|||
|
|
} finally {
|
|||
|
|
ChatUI.updateConnectionStatus(false);
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 处理流式事件
|
|||
|
|
*/
|
|||
|
|
async handleStreamEvent(event) {
|
|||
|
|
const { type, data } = event;
|
|||
|
|
|
|||
|
|
switch (type) {
|
|||
|
|
case 'user_message':
|
|||
|
|
// 用户消息已显示,跳过
|
|||
|
|
break;
|
|||
|
|
|
|||
|
|
case 'tool_call':
|
|||
|
|
this.handleToolCall(data);
|
|||
|
|
break;
|
|||
|
|
|
|||
|
|
case 'tool_response':
|
|||
|
|
this.handleToolResponse(data);
|
|||
|
|
break;
|
|||
|
|
|
|||
|
|
case 'assistant_message':
|
|||
|
|
ChatUI.renderAssistantMessage(data.content);
|
|||
|
|
break;
|
|||
|
|
|
|||
|
|
case 'final_response':
|
|||
|
|
ChatUI.renderAssistantMessage(data.content);
|
|||
|
|
break;
|
|||
|
|
|
|||
|
|
case 'error':
|
|||
|
|
ChatUI.showError(data.message);
|
|||
|
|
break;
|
|||
|
|
|
|||
|
|
default:
|
|||
|
|
console.log('未知事件类型:', type, data);
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 处理工具调用
|
|||
|
|
*/
|
|||
|
|
handleToolCall(data) {
|
|||
|
|
const { tool_name, arguments: args } = data;
|
|||
|
|
const isTodoWrite = tool_name === 'TodoWrite';
|
|||
|
|
|
|||
|
|
ChatUI.renderToolCall(tool_name, args, { isTodoWrite });
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 处理工具响应
|
|||
|
|
*/
|
|||
|
|
handleToolResponse(data) {
|
|||
|
|
const { tool_name, response } = data;
|
|||
|
|
const isTodoWrite = tool_name === 'TodoWrite';
|
|||
|
|
|
|||
|
|
ChatUI.renderToolResult(tool_name, response, { isTodoWrite });
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 处理清空对话
|
|||
|
|
*/
|
|||
|
|
async handleClear() {
|
|||
|
|
if (!confirm('确定要清空对话吗?')) return;
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
await ChatAPI.clearConversation();
|
|||
|
|
|
|||
|
|
// 生成新会话 ID
|
|||
|
|
this.conversationId = ChatAPI.regenerateConversationId();
|
|||
|
|
|
|||
|
|
// 清空界面
|
|||
|
|
ChatUI.clearMessages();
|
|||
|
|
|
|||
|
|
// 显示重置消息
|
|||
|
|
ChatUI.renderAssistantMessage('对话已清空,我们可以重新开始!');
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('清空对话错误:', error);
|
|||
|
|
ChatUI.showError('清空对话失败: ' + error.message);
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 显示欢迎消息
|
|||
|
|
*/
|
|||
|
|
showWelcomeMessage() {
|
|||
|
|
const welcomeMessage = `## 👋 欢迎使用 Spring AI Skills Chat
|
|||
|
|
|
|||
|
|
我可以帮助你完成各种任务,包括:
|
|||
|
|
|
|||
|
|
- 💬 **普通对话**(关闭"启用工具调用"使用传统流式响应)
|
|||
|
|
- 🔧 **执行 Shell 命令**
|
|||
|
|
- 📁 **读写文件**
|
|||
|
|
- 🎯 **使用自定义 Skills**
|
|||
|
|
- 📋 **任务管理**(TodoWrite)
|
|||
|
|
|
|||
|
|
### 🌟 新功能:伪流式接口
|
|||
|
|
|
|||
|
|
开启"启用工具调用"后,你可以实时看到:
|
|||
|
|
- 🔧 **工具调用**:AI 调用了哪些工具
|
|||
|
|
- ✅ **工具响应**:工具返回了什么结果
|
|||
|
|
- 💬 **AI 思考**:AI 的中间思考过程
|
|||
|
|
- 📊 **执行步骤**:完整的执行流程可视化
|
|||
|
|
|
|||
|
|
### 快速开始:
|
|||
|
|
|
|||
|
|
1. **开启工具调用**:点击右上角的"启用工具调用"开关
|
|||
|
|
2. **发送指令**:例如"请帮我为某某公司执行完整的定价流程"
|
|||
|
|
3. **实时观察**:工具调用和执行步骤会实时展示
|
|||
|
|
|
|||
|
|
现在就开始吧!🚀`;
|
|||
|
|
|
|||
|
|
ChatUI.renderAssistantMessage(welcomeMessage);
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// 页面加载完成后初始化
|
|||
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|||
|
|
ChatApp.init();
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// 导出(用于测试)
|
|||
|
|
window.ChatApp = ChatApp;
|