feat(branch-statement-button): 增加前端显示时在检索失败分支语句结尾处添加一个按钮。按钮名为【知识库内容清单】
This commit is contained in:
1 parent
6c47d0639c
commit
e421d2607d
18 files changed
+344
-4
No files matched your search
@@ -12,7 +12,7 @@ from flask import Blueprint
|
||||
chat_bp = Blueprint('chat', __name__)
|
||||
|
||||
# 子模块导入时在同一 Blueprint 上注册原有 URL。
|
||||
from . import assets, completions, directory, feedback, previews, sessions, uploads # noqa: E402, F401
|
||||
from . import assets, capability_list, completions, directory, feedback, previews, sessions, uploads # noqa: E402, F401
|
||||
from .shared import ( # noqa: E402, F401
|
||||
CHAT_USER_ATTACHMENT_LIMIT,
|
||||
CHAT_USER_ATTACHMENT_MAX_SIZE,
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
"""
|
||||
api/chat/capability_list.py
|
||||
|
||||
用途:聊天端「知识库内容清单」查询接口,作为知识库未命中兜底回复按钮的数据源。
|
||||
依赖:chat.shared(公共上下文与异常)、chat_bp、services.chat.capability_list。
|
||||
逻辑:GET /api/chat/capability-list?agent_id=...,经 require_agent_permission 鉴权后
|
||||
返回常用问题中该应用「能力清单条目」的标准答案;未配置时抛 404 业务错误。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from .shared import * # noqa: F403
|
||||
from . import chat_bp
|
||||
from app.services.chat.capability_list import get_agent_capability_list
|
||||
|
||||
|
||||
@chat_bp.route('/capability-list', methods=['GET'])
|
||||
@require_agent_permission
|
||||
def get_capability_list():
|
||||
"""
|
||||
获取当前应用的知识库内容清单。
|
||||
用途:AI 回复命中知识库未命中兜底语句时,前端【知识库内容清单】按钮点击后调用。
|
||||
核心逻辑:
|
||||
1. require_agent_permission 从 query 读取 agent_id 并校验用户对该应用的访问权限;
|
||||
2. 委托 services.chat.capability_list 返回「能力清单条目」标准答案;
|
||||
3. 未配置时抛 code=CHAT.CAPABILITY_LIST_NOT_FOUND(404),前端按错误码本地化提示。
|
||||
参数约束:agent_id 必须来自当前会话路由(当前聊天应用),禁止跨应用探测他人条目。
|
||||
"""
|
||||
agent = getattr(g, 'current_agent', None)
|
||||
if agent is None:
|
||||
raise AppError(code='SYSTEM.INTERNAL_ERROR', message='Missing agent context', http_status=500)
|
||||
return jsonify(get_agent_capability_list(agent))
|
||||
@@ -0,0 +1,68 @@
|
||||
"""
|
||||
services/chat/capability_list.py
|
||||
|
||||
用途:查询指定智能体在常用问题(QuickQuestion)中的「能力清单」条目标准答案。
|
||||
依赖:models.QuickQuestion、core.errors.AppError、SQLAlchemy db。
|
||||
逻辑:Dify 知识库未命中兜底回复会在前端展示【知识库内容清单】按钮;按钮点击后由
|
||||
api/chat/capability_list.py 调用本模块,按 agent_id 过滤全局条目并返回标准答案。
|
||||
管理端维护约定:条目标题形如「<应用名>·能力清单条目」,因此以标题含「能力清单」
|
||||
作为识别关键字,应用归属仍以 agent_id 外键为准(禁止按标题前缀解析应用名)。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from app import db
|
||||
from app.core.errors import AppError
|
||||
from app.models import QuickQuestion
|
||||
|
||||
# 能力清单条目的标题识别关键字(管理端维护约定,见模块 docstring)。
|
||||
CAPABILITY_LIST_TITLE_KEYWORD = '能力清单'
|
||||
|
||||
|
||||
def get_agent_capability_list(agent: Any) -> dict[str, Any]:
|
||||
"""
|
||||
查询智能体的知识库内容清单条目。
|
||||
|
||||
用途:聊天端兜底回复【知识库内容清单】按钮的数据来源。
|
||||
核心逻辑:
|
||||
1. 仅匹配全局条目(user_id 为空)且归属当前 agent_id;
|
||||
2. 标题须含「能力清单」关键字,且标准答案非空;
|
||||
3. 多条命中时按 sort_order、id 取第一条,保证返回稳定。
|
||||
Args:
|
||||
agent: 已通过 require_agent_permission 鉴权的 Agent 模型实例,禁止传 None。
|
||||
Returns:
|
||||
含 id/agent_id/title/question/answer 的条目字典,answer 为标准答案原文。
|
||||
Raises:
|
||||
AppError: code=CHAT.CAPABILITY_LIST_NOT_FOUND,表示该应用尚未配置能力清单条目。
|
||||
"""
|
||||
agent_id = getattr(agent, 'id', None)
|
||||
if not agent_id:
|
||||
raise AppError(code='SYSTEM.INTERNAL_ERROR', message='Missing agent id', http_status=500)
|
||||
|
||||
entry = (
|
||||
db.session.query(QuickQuestion)
|
||||
.filter(
|
||||
QuickQuestion.agent_id == agent_id,
|
||||
QuickQuestion.user_id.is_(None),
|
||||
QuickQuestion.title.contains(CAPABILITY_LIST_TITLE_KEYWORD),
|
||||
QuickQuestion.answer.isnot(None),
|
||||
QuickQuestion.answer != '',
|
||||
)
|
||||
.order_by(QuickQuestion.sort_order.asc(), QuickQuestion.id.asc())
|
||||
.first()
|
||||
)
|
||||
if entry is None:
|
||||
raise AppError(
|
||||
code='CHAT.CAPABILITY_LIST_NOT_FOUND',
|
||||
message='capability list entry is not configured for this agent',
|
||||
http_status=404,
|
||||
)
|
||||
|
||||
return {
|
||||
'id': entry.id,
|
||||
'agent_id': entry.agent_id,
|
||||
'title': entry.title,
|
||||
'question': entry.question,
|
||||
'answer': entry.answer or '',
|
||||
}
|
||||
Reference in new issue
Block a user