feat: 实现会话管理和 AI 回复保存功能
## 新增功能 - 实现会话持久化存储(JSON 文件) - 支持会话列表显示、切换、创建、删除 - 自动保存用户消息和 AI 回复 - 优化 Markdown 渲染样式(标题、列表、代码块、表格等) - 添加过程数据可视化(思考步骤、工具调用、响应) ## 技术改进 - 新增 ConversationMessage、ConversationMetadata 模型 - 新增 ConversationStorageService 服务(JSON 文件存储) - 在 ChatService 中添加流式响应完成后保存 AI 回复的逻辑 - 优化删除会话逻辑:删除当前会话后切换到现有会话,而非自动创建新会话 - 优化应用初始化:优先使用现有会话,仅在没有会话时才创建新会话 ## 依赖更新 - 添加 jackson-datatype-jsr310 支持 Java 8 DateTime Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1ffd256c9d
commit
fd09a55614
11 files changed
+2697
-993
No files matched your search
@@ -10,21 +10,45 @@ const ChatApp = {
|
||||
/**
|
||||
* 初始化应用
|
||||
*/
|
||||
init() {
|
||||
async init() {
|
||||
// 初始化 DOM 元素
|
||||
ChatUI.init();
|
||||
|
||||
// 先加载会话列表
|
||||
const conversations = await ChatAPI.listConversations();
|
||||
|
||||
// 初始化会话 ID
|
||||
this.conversationId = ChatUtils.getOrCreateConversationId();
|
||||
ChatAPI.init(this.conversationId);
|
||||
const localConvId = ChatUtils.getOrCreateConversationId();
|
||||
|
||||
// 检查本地会话是否存在
|
||||
const existingConv = await ChatAPI.getConversation(localConvId);
|
||||
|
||||
if (existingConv) {
|
||||
// 本地会话存在,使用它
|
||||
this.conversationId = localConvId;
|
||||
ChatAPI.init(this.conversationId);
|
||||
} else if (conversations.length > 0) {
|
||||
// 本地会话不存在,但有其他会话,使用最近的会话
|
||||
this.conversationId = conversations[0].id;
|
||||
ChatAPI.init(this.conversationId);
|
||||
// 更新本地存储
|
||||
localStorage.setItem('conversationId', this.conversationId);
|
||||
} else {
|
||||
// 没有任何会话,创建新会话
|
||||
const newConv = await ChatAPI.createConversation('新对话');
|
||||
this.conversationId = newConv.id;
|
||||
}
|
||||
|
||||
// 绑定事件
|
||||
this.bindEvents();
|
||||
|
||||
// 显示欢迎消息
|
||||
this.showWelcomeMessage();
|
||||
// 加载会话列表(更新选中状态)
|
||||
await ChatUI.loadConversationList();
|
||||
|
||||
console.log('🚀 ChatApp 初始化完成');
|
||||
// 加载当前会话的历史消息
|
||||
await this.loadCurrentConversation();
|
||||
|
||||
console.log('ChatApp 初始化完成,当前会话:', this.conversationId);
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -45,12 +69,47 @@ const ChatApp = {
|
||||
// 清空对话
|
||||
ui.clearBtn.addEventListener('click', () => this.handleClear());
|
||||
|
||||
// 工具开关变化
|
||||
ui.toolToggle.addEventListener('change', () => {
|
||||
const enabled = ui.toolToggle.checked;
|
||||
console.log('工具调用已', enabled ? '开启' : '关闭');
|
||||
});
|
||||
|
||||
// 页面卸载前保存会话 ID
|
||||
window.addEventListener('beforeunload', () => {
|
||||
localStorage.setItem('conversationId', this.conversationId);
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 加载当前会话的历史消息
|
||||
*/
|
||||
async loadCurrentConversation() {
|
||||
try {
|
||||
const messages = await ChatAPI.getMessages(this.conversationId);
|
||||
|
||||
// 清空现有消息
|
||||
ChatUI.elements.messagesContainer.innerHTML = '';
|
||||
|
||||
// 如果有历史消息,渲染它们
|
||||
if (messages && messages.length > 0) {
|
||||
messages.forEach(msg => {
|
||||
if (msg.role === 'user') {
|
||||
ChatUI.renderUserMessage(msg.content);
|
||||
} else if (msg.role === 'assistant') {
|
||||
ChatUI.renderAssistantMessage(msg.content);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// 没有消息则显示欢迎界面
|
||||
ChatUI.clearMessages();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('加载当前会话失败:', error);
|
||||
ChatUI.clearMessages();
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 处理表单提交
|
||||
*/
|
||||
@@ -75,6 +134,9 @@ const ChatApp = {
|
||||
await this.handleStream(message);
|
||||
}
|
||||
|
||||
// 刷新会话列表(更新时间和消息数)
|
||||
await ChatUI.loadConversationList();
|
||||
|
||||
// 恢复输入状态
|
||||
this.isProcessing = false;
|
||||
ChatUI.setInputState({ disabled: false });
|
||||
@@ -97,23 +159,24 @@ const ChatApp = {
|
||||
async handleStream(message) {
|
||||
ChatUI.updateConnectionStatus(true);
|
||||
|
||||
// 创建加载消息
|
||||
const messageEl = ChatUI.renderAssistantMessage('', { isLoading: true });
|
||||
// 创建 AI 消息容器
|
||||
ChatUI.createAssistantMessageContainer();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
// 设置最终回复
|
||||
ChatUI.setFinalResponse(content);
|
||||
} catch (error) {
|
||||
console.error('流式响应错误:', error);
|
||||
ChatUI.showError('连接错误: ' + error.message);
|
||||
messageEl.remove();
|
||||
} finally {
|
||||
ChatUI.updateConnectionStatus(false);
|
||||
}
|
||||
@@ -125,6 +188,9 @@ const ChatApp = {
|
||||
async handleStreamWithTools(message) {
|
||||
ChatUI.updateConnectionStatus(true);
|
||||
|
||||
// 创建 AI 消息容器
|
||||
ChatUI.createAssistantMessageContainer();
|
||||
|
||||
try {
|
||||
for await (const event of ChatAPI.streamMessageWithTools(message)) {
|
||||
await this.handleStreamEvent(event);
|
||||
@@ -157,11 +223,13 @@ const ChatApp = {
|
||||
break;
|
||||
|
||||
case 'assistant_message':
|
||||
ChatUI.renderAssistantMessage(data.content);
|
||||
// 显示思考过程
|
||||
ChatUI.addThinkingStep(data.content);
|
||||
break;
|
||||
|
||||
case 'final_response':
|
||||
ChatUI.renderAssistantMessage(data.content);
|
||||
// 设置最终回复
|
||||
ChatUI.setFinalResponse(data.content);
|
||||
break;
|
||||
|
||||
case 'error':
|
||||
@@ -180,7 +248,7 @@ const ChatApp = {
|
||||
const { tool_name, arguments: args } = data;
|
||||
const isTodoWrite = tool_name === 'TodoWrite';
|
||||
|
||||
ChatUI.renderToolCall(tool_name, args, { isTodoWrite });
|
||||
ChatUI.addToolCallStep(tool_name, args, isTodoWrite);
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -190,7 +258,7 @@ const ChatApp = {
|
||||
const { tool_name, response } = data;
|
||||
const isTodoWrite = tool_name === 'TodoWrite';
|
||||
|
||||
ChatUI.renderToolResult(tool_name, response, { isTodoWrite });
|
||||
ChatUI.addToolResponseStep(tool_name, response, isTodoWrite);
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -208,45 +276,16 @@ const ChatApp = {
|
||||
// 清空界面
|
||||
ChatUI.clearMessages();
|
||||
|
||||
// 刷新列表
|
||||
await ChatUI.loadConversationList();
|
||||
|
||||
// 显示重置消息
|
||||
ChatUI.renderAssistantMessage('对话已清空,我们可以重新开始!');
|
||||
ChatUI.createAssistantMessageContainer();
|
||||
ChatUI.setFinalResponse('对话已清空,我们可以重新开始!');
|
||||
} 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);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user