diff --git a/backend/app/api/chat.py b/backend/app/api/chat.py index 8a505234..e37559b0 100644 --- a/backend/app/api/chat.py +++ b/backend/app/api/chat.py @@ -193,6 +193,59 @@ def get_chat_sessions(): return jsonify(sessions) +@chat_bp.route('/sessions', methods=['DELETE']) +@require_agent_permission +def delete_all_chat_sessions(): + """ + 删除当前用户与当前 Agent 的所有历史会话 + """ + user = getattr(g, 'current_user', None) + agent = getattr(g, 'current_agent', None) + + if user is None or agent is None: + return jsonify({'error': 'Missing context'}), 500 + + chat_logs = db.session.query(ChatLog).filter_by(user_id=user.id, agent_id=agent.id).all() + chat_log_ids = [log.id for log in chat_logs] + if chat_log_ids: + from app.models import GeneralFeedback + db.session.query(Feedback).filter(Feedback.chat_log_id.in_(chat_log_ids)).delete(synchronize_session=False) + db.session.query(GeneralFeedback).filter(GeneralFeedback.chat_log_id.in_(chat_log_ids)).delete(synchronize_session=False) + db.session.query(ChatLog).filter(ChatLog.id.in_(chat_log_ids)).delete(synchronize_session=False) + db.session.commit() + + return jsonify({'status': 'ok'}) + + +@chat_bp.route('/sessions/', methods=['DELETE']) +@require_agent_permission +def delete_chat_session(conversation_id): + """ + 删除指定的会话记录 + """ + user = getattr(g, 'current_user', None) + agent = getattr(g, 'current_agent', None) + + if user is None or agent is None: + return jsonify({'error': 'Missing context'}), 500 + + chat_logs = db.session.query(ChatLog).filter_by( + user_id=user.id, + agent_id=agent.id, + conversation_id=conversation_id + ).all() + + chat_log_ids = [log.id for log in chat_logs] + if chat_log_ids: + from app.models import GeneralFeedback + db.session.query(Feedback).filter(Feedback.chat_log_id.in_(chat_log_ids)).delete(synchronize_session=False) + db.session.query(GeneralFeedback).filter(GeneralFeedback.chat_log_id.in_(chat_log_ids)).delete(synchronize_session=False) + db.session.query(ChatLog).filter(ChatLog.id.in_(chat_log_ids)).delete(synchronize_session=False) + db.session.commit() + + return jsonify({'status': 'ok'}) + + @chat_bp.route('/messages', methods=['GET']) @require_agent_permission def get_chat_messages(): diff --git a/docs/新人环境搭建与配置文档.md b/docs/新人环境搭建与配置文档.md index f48a7656..2312931d 100644 --- a/docs/新人环境搭建与配置文档.md +++ b/docs/新人环境搭建与配置文档.md @@ -1,6 +1,6 @@ -# HXOPS 新人环境搭建与配置文档(已补齐) +# HXOPS 新人环境搭建与配置文档 -> 更新时间:2026-04-25 +> 更新时间:2026-04-25 > 适用对象:开发、测试、运维、实施同学 ## 1. 项目基础信息 @@ -8,7 +8,7 @@ - 项目名称:`HXOPS / Enterprise AI Gateway` - 代码仓库:`https://gitee.com/justop/hxops.git` - 项目任务看板:`https://www.kdocs.cn/l/ct8FI9gCM5eU` -- 服务器部署默认分支:`dev` +- 服务器默认分支:`dev` - 当前本地分支(示例):`sophie` - 项目根目录:`hxops/` @@ -18,10 +18,11 @@ 2. Node.js + npm(建议 `Node.js 20 LTS`) 3. Python(建议 `3.12`) 4. PostgreSQL(建议 `>= 14`) -5. Redis(建议 `>= 6`) -6. Docker Desktop(推荐) -7. VS Code 或 Cursor(推荐插件:Python、Pylance、Vue - Official、ESLint、Prettier、Docker) -8. 可选:Postman/Apifox、DBeaver/DataGrip、企业微信客户端 +5. DBeaver/DataGrip(建议 `>= 6`) +6. 企业微信客户端 +7. VS Code(推荐插件:Python、Pylance、Vue - Official、ESLint、Prettier、Docker) +8. Codex 或 Cursor +9. 可选:Postman/Apifox、Redis、Warp ## 3. Git 分支说明与操作原则 @@ -93,6 +94,7 @@ git push origin feature/your-feature-name ### 5.1 后端(本地) 1. 创建虚拟环境并激活 + ```bash cd backend python -m venv .venv @@ -103,11 +105,13 @@ source .venv/bin/activate ``` 2. 安装依赖 + ```bash pip install -r requirements.txt ``` 3. 初始化数据库(2选1) + ```bash # 方案A:PostgreSQL(推荐) python init_db.py @@ -120,6 +124,7 @@ python init_db.py ``` 4. 启动后端 + ```bash python run.py ``` @@ -134,16 +139,17 @@ npm install npm run dev ``` -前端本地开发端口:`8881`(`frontend/vite.config.ts`)。 +前端本地开发端口:`8881`(`frontend/vite.config.ts`)。 本地开发时前端 `/api` 已代理到 `http://127.0.0.1:5002`。 -### 5.3 Docker 启动(推荐) +### 5.3 Docker 启动(可选) ```bash docker compose up -d --build ``` 当前对外端口映射(`docker-compose.yml`): + - 前端:`8890:80` - 后端:`5002:5002` @@ -154,22 +160,24 @@ docker compose up -d --build ### 6.1 登录方式 1. 账号密码登录 -2. 企业微信 OAuth 登录(WECOM) +2. 企业微信 OAuth 登录(WECOM)(暂时不支持) ### 6.2 环境入口(已补齐可确定项) -| 环境 | 前端地址 | 后端地址 | 登录方式 | 备注 | -|---|---|---|---|---| -| 本地 | `http://localhost:8881` | `http://localhost:5002` | 密码/企业微信(按配置) | | -| 测试 | `待定` | `待定` | 密码/企业微信(按配置) | | -| 生产 | `http://192.168.100.230:8881/` | | 密码/企业微信(按配置) | | -| 服务器部署(脚本) | `<待补:域名>` | `<待补:网关地址>` | 密码/企业微信 | | -| Prod | `<待补>` | `<待补>` | `<待补>` | | + +| 环境 | 前端地址 | 后端地址 | 登录方式 | 备注 | +| ------- | ------------------------------ | ----------------------- | ----------------------- | ---- | +| 本地 | `http://localhost:8881` | `http://localhost:5002` | 密码/企业微信(按配置) | | +| 测试 | `待定` | `待定` | 密码/企业微信(按配置) | | +| 生产 | `http://192.168.100.230:8881/` | | 密码/企业微信(按配置) | | +| Jenkins | `http://8.152.207.171:10240/` | `` | 密码 | | +| Casdoor | `http://192.168.100.230:8088` | | 密码 | | ### 6.3 默认测试账号(仅本地初始化脚本场景) -| 用户名 | 默认密码 | 角色 | 来源 | -|---|---|---|---| + +| 用户名 | 默认密码 | 角色 | 来源 | +| ------- | ------------ | ------- | --------------------------------------------------- | | `admin` | `Hxit@54321` | `admin` | `backend/init_local_db.py` / `backend/fix_admin.py` | 说明:生产环境禁止使用默认密码,首次上线后必须立即重置。 @@ -198,19 +206,20 @@ docker compose up -d --build ## 7. 环境变量清单(交接必填) -| 变量名 | 用途 | 示例 | -|---|---|---| -| `SECRET_KEY` | Flask 密钥 | 随机长字符串 | -| `DATABASE_URL` | 数据库连接 | `postgresql://user:pwd@host:5432/db` | -| `REDIS_URL` | Redis 连接 | `redis://:pwd@host:6379/0` | -| `DIFY_API_BASE` | Dify 地址 | `https://api.dify.ai/v1` | -| `DIFY_API_KEY` | Dify 密钥 | `sk-...` | -| `HR_WORKFLOW_IFRAME_URL` | HR 页面链接 | `https://...` | -| `HR_WORKFLOW_API_BASE` | HR API 地址 | `https://...` | -| `HR_WORKFLOW_API_KEY` | HR API 密钥 | `<待补>` | -| `INTEGRATION_API_BASE` | 集成 API 地址 | `https://...` | -| `INTEGRATION_API_KEY` | 集成 API 密钥 | `<待补>` | -| `WECOM_*` | 企业微信配置 | 见第 6.4 节 | + +| 变量名 | 用途 | 示例 | +| ------------------------ | ------------- | ------------------------------------ | +| `SECRET_KEY` | Flask 密钥 | 随机长字符串 | +| `DATABASE_URL` | 数据库连接 | `postgresql://user:pwd@host:5432/db` | +| `REDIS_URL` | Redis 连接 | `redis://:pwd@host:6379/0` | +| `DIFY_API_BASE` | Dify 地址 | `https://api.dify.ai/v1` | +| `DIFY_API_KEY` | Dify 密钥 | `sk-...` | +| `HR_WORKFLOW_IFRAME_URL` | HR 页面链接 | `https://...` | +| `HR_WORKFLOW_API_BASE` | HR API 地址 | `https://...` | +| `HR_WORKFLOW_API_KEY` | HR API 密钥 | `<待补>` | +| `INTEGRATION_API_BASE` | 集成 API 地址 | `https://...` | +| `INTEGRATION_API_KEY` | 集成 API 密钥 | `<待补>` | +| `WECOM_*` | 企业微信配置 | 见第 6.4 节 | ## 8. Codex 配置方案 @@ -250,8 +259,6 @@ docker compose up -d --build ## 10. 待补充信息区 - 服务器正式访问域名:`ai.well-trust.com:8881` -- Dev/UAT/Prod 实际入口地址:`<待补>` - 数据库与 Redis 实际地址:`postgresql://postgres:difyai123456@localhost:5432/hxops_db` - 企业微信应用 `CorpID/AgentID`: -- 新人账号申请与审批流程:`OA账号联系李振申` -- 值班群与故障升级联系人: +- 新人账号申请与审批流程:`OA账号联系李振甲` diff --git a/frontend/src/views/ChatView.vue b/frontend/src/views/ChatView.vue index 7e058fb1..7a6fdad2 100644 --- a/frontend/src/views/ChatView.vue +++ b/frontend/src/views/ChatView.vue @@ -13,7 +13,7 @@ import { ref, onMounted, computed } from 'vue'; import { useI18n } from 'vue-i18n'; import { useChat } from '../composables/useChat'; -import { Send, Bot, Loader2, ArrowLeft, Plus, Menu, Star, Copy, Edit2, RotateCw, Check } from 'lucide-vue-next'; +import { Send, Bot, Loader2, ArrowLeft, Plus, Menu, Star, Copy, Edit2, RotateCw, Check, X, Trash2 } from 'lucide-vue-next'; import { marked } from 'marked'; import { useRoute, useRouter } from 'vue-router'; import { config } from '../config'; @@ -264,7 +264,9 @@ const getSourceLinks = (resources: any[] = []): SourceFileLink[] => { if (isNoisySourceName(name)) continue; // 根据配置决定是否显示 MD 文件 if ( (isMarkdownFileName(name) || isMarkdownByUrl(url))) continue; - const key = `${name}|${url}`; + + // Deduplicate by name if it looks like a filename, else by name + base URL + const key = looksLikeFileName(name) ? name : `${name}|${url.split('?')[0]}`; if (!map.has(key)) { map.set(key, { name, url }); } @@ -289,12 +291,12 @@ const getMessageLinks = (msg: any): SourceFileLink[] => { if (isNoisySourceName(link.name)) continue; if ( (isMarkdownFileName(link.name) || isMarkdownByUrl(link.url))) continue; if (isMarkdownFileName(link.name)) { - const key = `${link.name}|${link.url}`; + const key = looksLikeFileName(link.name) ? link.name : `${link.name}|${link.url.split('?')[0]}`; if (!map.has(key)) map.set(key, link); continue; } if (isNoisySourceUrl(link.url) && !looksLikeFileName(link.name)) continue; - const key = `${link.name}|${link.url}`; + const key = looksLikeFileName(link.name) ? link.name : `${link.name}|${link.url.split('?')[0]}`; if (!map.has(key)) map.set(key, link); } }; @@ -458,6 +460,43 @@ const loadHistory = async () => { } }; +const clearAllHistory = async () => { + if (!confirm('确定要清除全部聊天历史记录吗?此操作不可恢复。')) return; + try { + const res = await fetch(`/api/chat/sessions?agent_id=${agentId}`, { + method: 'DELETE', + headers: getAuthHeaders() + }); + if (res.ok) { + history.value = []; + if (conversationId.value) { + startNewChat(); + } + } + } catch (e) { + console.error('Failed to clear history', e); + } +}; + +const deleteHistoryItem = async (id: string, event: Event) => { + event.stopPropagation(); + if (!confirm('确定要删除这条聊天记录吗?')) return; + try { + const res = await fetch(`/api/chat/sessions/${id}?agent_id=${agentId}`, { + method: 'DELETE', + headers: getAuthHeaders() + }); + if (res.ok) { + history.value = history.value.filter(h => h.conversation_id !== id); + if (conversationId.value === id) { + startNewChat(); + } + } + } catch (e) { + console.error('Failed to delete history item', e); + } +}; + const loadConversation = async (id: string) => { if (conversationId.value === id) return; @@ -581,25 +620,42 @@ const submitFeedback = async () => { {{ $t('chat.history') }} - +
+ + +
{{ $t('chat.noHistory') }}
- +
+ + +