feat(chat): 透传培训agent账号角色到dify
This commit is contained in:
1 parent
fd024c1c32
commit
9ea49d7060
5 files changed
+114
-4
No files matched your search
@@ -1338,6 +1338,7 @@ def chat_completions():
|
||||
username=getattr(user, 'username', None),
|
||||
employee_id=getattr(user, 'employee_id', None),
|
||||
real_name=getattr(user, 'real_name', None),
|
||||
role=getattr(user, 'role', None),
|
||||
)
|
||||
|
||||
from app.services.dify import DifyService
|
||||
@@ -1458,6 +1459,7 @@ def chat_completions():
|
||||
session_id=session.session_id,
|
||||
request_id=request_id,
|
||||
trigger_source="frontend_chat",
|
||||
user_role=user_proxy.role,
|
||||
adapter_hints=_build_chat_adapter_hints(
|
||||
shipping_task_id=shipping_task_id,
|
||||
shipping_batch_no=shipping_batch_no,
|
||||
@@ -1489,6 +1491,7 @@ def chat_completions():
|
||||
session_id=session.session_id,
|
||||
request_id=request_id,
|
||||
trigger_source="frontend_chat",
|
||||
user_role=user_proxy.role,
|
||||
adapter_hints=_build_chat_adapter_hints(
|
||||
shipping_task_id=shipping_task_id,
|
||||
shipping_batch_no=shipping_batch_no,
|
||||
|
||||
@@ -4,7 +4,8 @@ backend/app/services/agent_runtime_context/dify_inputs.py
|
||||
用途:提供聊天入口构建 Dify inputs 的统一分发入口。
|
||||
依赖:generic adapter、file_check_tools.dify_inputs、shipping_notice.dify_inputs。
|
||||
逻辑:chat.py 在调用 DifyService.stream_chat 前只调用本模块;本模块按 Agent 类型分发到
|
||||
shipping_notice / file_check / generic adapter,平台层只暴露会话级公共参数,领域提示由 adapter_hints 透传。
|
||||
shipping_notice / file_check / generic adapter,平台层默认暴露会话级公共参数;员工培训 Agent
|
||||
额外注入角色字段,方便 Dify 按后台账号角色切换学员/HR 问答分支。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -23,6 +24,74 @@ from app.services.shipping_notice.dify_inputs import (
|
||||
)
|
||||
|
||||
|
||||
TRAINING_AGENT_NAME_PATTERN = ("培训", "考试", "入职")
|
||||
|
||||
|
||||
def _agent_extra_config(agent) -> dict[str, Any]:
|
||||
"""读取 Agent.extra_config;非字典时回退为空,避免脏配置导致培训识别异常。"""
|
||||
extra_config = getattr(agent, "extra_config", None)
|
||||
return extra_config if isinstance(extra_config, dict) else {}
|
||||
|
||||
|
||||
def is_training_chat_agent(agent) -> bool:
|
||||
"""
|
||||
判断当前 Agent 是否启用培训工作区语义。
|
||||
|
||||
核心逻辑:后端与前端共用同一组培训识别口径,优先读取 extra_config 显式开关,
|
||||
未配置时再回退到 Agent 名称关键字,避免普通 Agent 被误注入培训专用字段。
|
||||
|
||||
Args:
|
||||
agent: 当前对话 Agent 记录或代理对象。
|
||||
|
||||
Returns:
|
||||
bool: 命中培训 Agent 返回 True,否则返回 False。
|
||||
"""
|
||||
extra_config = _agent_extra_config(agent)
|
||||
if extra_config.get("training_workspace_enabled") in (False, "false"):
|
||||
return False
|
||||
explicit_flags = (
|
||||
extra_config.get("training_workspace_enabled"),
|
||||
extra_config.get("training_mode"),
|
||||
extra_config.get("training_agent"),
|
||||
)
|
||||
if any(value in (True, "true") for value in explicit_flags):
|
||||
return True
|
||||
name = str(getattr(agent, "name", None) or "").strip()
|
||||
return any(keyword in name for keyword in TRAINING_AGENT_NAME_PATTERN)
|
||||
|
||||
|
||||
def _append_training_role_input(
|
||||
base_inputs: dict[str, Any],
|
||||
*,
|
||||
agent,
|
||||
user_role: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""
|
||||
为培训 Agent 追加 Dify 顶层 role 输入。
|
||||
|
||||
【AI 触发时机】:当员工培训 Agent 需要让 Dify 直接根据后台账号角色区分学员/HR/管理员话术时调用;
|
||||
普通 Agent 不应新增该字段,避免污染现有 Start 变量契约。
|
||||
|
||||
核心逻辑:仅当命中培训 Agent 且当前用户角色非空时,基于现有 inputs 复制后追加 `role`;
|
||||
这样既保留 agent_runtime_context 协议,也满足培训画布直接读取顶层变量的需求。
|
||||
|
||||
Args:
|
||||
base_inputs: 已构建好的 Dify inputs。
|
||||
agent: 当前对话 Agent。
|
||||
user_role: 后端鉴权后的真实账号角色,来源于 users.role。
|
||||
|
||||
Returns:
|
||||
dict[str, Any]: 若符合条件则包含 `role`,否则原样返回。
|
||||
"""
|
||||
normalized_role = str(user_role or "").strip()
|
||||
if not base_inputs or not normalized_role or not is_training_chat_agent(agent):
|
||||
return base_inputs
|
||||
return {
|
||||
**base_inputs,
|
||||
"role": normalized_role,
|
||||
}
|
||||
|
||||
|
||||
def build_chat_dify_inputs(
|
||||
agent,
|
||||
*,
|
||||
@@ -31,12 +100,14 @@ def build_chat_dify_inputs(
|
||||
trigger_source: str | None = "frontend_chat",
|
||||
locale: str | None = None,
|
||||
adapter_hints: dict[str, Any] | None = None,
|
||||
user_role: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""
|
||||
统一构建聊天 Dify inputs。
|
||||
|
||||
核心逻辑:优先命中 shipping_notice 专用 adapter;其次复用 file_check 既有 task_id 注入;
|
||||
其余 Agent 一律走 generic adapter,让所有 Dify Chatflow 至少拿到 platform/session 快照。
|
||||
员工培训 Agent 会在此基础上额外追加顶层 `role`,让 Dify 直接消费后台账号角色。
|
||||
业务域专用提示不出现在平台签名中,而是由 adapter_hints 交给对应 adapter 自行消费。
|
||||
|
||||
Args:
|
||||
@@ -46,6 +117,7 @@ def build_chat_dify_inputs(
|
||||
trigger_source: 触发来源,默认 frontend_chat。
|
||||
locale: 用户语言。
|
||||
adapter_hints: 领域 adapter 可选读取的提示字典;平台层不感知其内部业务字段。
|
||||
user_role: 当前登录账号在管理后台配置的角色;仅培训 Agent 追加到顶层 `role`。
|
||||
|
||||
Returns:
|
||||
Dify inputs 字典;session_id 为空时返回空 dict。
|
||||
@@ -71,10 +143,15 @@ def build_chat_dify_inputs(
|
||||
if agent_declares_file_check_progress(agent) or is_file_check_chat_agent(agent):
|
||||
return build_file_check_dify_inputs(agent, required=True)
|
||||
|
||||
return build_generic_dify_inputs(
|
||||
generic_inputs = build_generic_dify_inputs(
|
||||
session_id=normalized_session_id,
|
||||
agent=agent,
|
||||
request_id=request_id,
|
||||
trigger_source=trigger_source,
|
||||
locale=locale,
|
||||
)
|
||||
return _append_training_role_input(
|
||||
generic_inputs,
|
||||
agent=agent,
|
||||
user_role=user_role,
|
||||
)
|
||||
@@ -145,6 +145,36 @@ def test_build_chat_dify_inputs_falls_back_to_generic_adapter(monkeypatch):
|
||||
}
|
||||
|
||||
|
||||
def test_build_chat_dify_inputs_appends_role_for_training_agent(monkeypatch):
|
||||
"""培训 Agent 对接 Dify 时应额外透传后台账号角色,普通 generic 输入保持不变。"""
|
||||
agent = SimpleNamespace(
|
||||
name="员工培训 Agent",
|
||||
extra_config={"training_workspace_enabled": True},
|
||||
)
|
||||
|
||||
monkeypatch.setattr(dify_inputs_module, "is_shipping_notice_chat_agent", lambda _: False)
|
||||
monkeypatch.setattr(dify_inputs_module, "agent_declares_file_check_progress", lambda _: False)
|
||||
monkeypatch.setattr(dify_inputs_module, "is_file_check_chat_agent", lambda _: False)
|
||||
monkeypatch.setattr(
|
||||
dify_inputs_module,
|
||||
"build_generic_dify_inputs",
|
||||
lambda **kwargs: {
|
||||
"agent_runtime_context": '{"agent_code":"training_agent"}',
|
||||
},
|
||||
)
|
||||
|
||||
inputs = build_chat_dify_inputs(
|
||||
agent,
|
||||
session_id="conv-training-001",
|
||||
user_role="hr",
|
||||
)
|
||||
|
||||
assert inputs == {
|
||||
"agent_runtime_context": '{"agent_code":"training_agent"}',
|
||||
"role": "hr",
|
||||
}
|
||||
|
||||
|
||||
def test_build_chat_dify_inputs_ignores_invalid_shipping_hints(monkeypatch):
|
||||
"""shipping hints 非字典时应安全回退为空,避免平台入口依赖脏业务结构。"""
|
||||
agent = SimpleNamespace(name="货代发货通知 Agent", extra_config={})
|
||||
|
||||
+1
-1
@@ -31,7 +31,7 @@
|
||||
| 管理端培训模式配置 | 已实现 | Agent 管理表单支持显式开启培训工作区;平台应用说明页支持上传并标记“员工培训视频 / 员工培训试题 PDF”,并可直接把试题 PDF 解析导入题库表单;扫描件 PDF 在 OCR 依赖齐备时也可回退识别 |
|
||||
| 首页培训推荐入口 | 前端已实现 | 当 Agent 开启培训模式后,工作台首页会在应用列表上方展示培训强入口卡片 |
|
||||
| 培训记录后端落库 | 已实现 | 已新增培训记录模型、迁移与用户/管理端 API,考试进度、结果、统计与导出均可走后端数据源 |
|
||||
| 培训主链推荐架构 | 已文档化 | 推荐采用“受控考试主链 + Dify 辅助问答/ChatBI”的混合方案,见 [docs/新人入职培训Agent_推荐落地方案.md](新人入职培训Agent_推荐落地方案.md) |
|
||||
| 培训主链推荐架构 | 已文档化 | 推荐采用“受控考试主链 + Dify 辅助问答/ChatBI”的混合方案;培训 Agent 对接 Dify 时会额外透传后台账号 `role`,便于区分学员/HR 问答分支,见 [docs/新人入职培训Agent_推荐落地方案.md](新人入职培训Agent_推荐落地方案.md) |
|
||||
| 培训业务闭环整改清单 | 已文档化 | 见 [docs/新人入职培训Agent_闭环整改清单.md](新人入职培训Agent_闭环整改清单.md) |
|
||||
|
||||
## 1. 功能总览
|
||||
|
||||
+1
-1
@@ -921,7 +921,7 @@ const update = () => { isMobile.value = window.innerHeight > window.innerWidth;
|
||||
“新人入职培训 Agent”不建议走纯 Dify 考试编排主链,推荐采用:
|
||||
|
||||
- **主链**:Chat 页面内嵌培训工作区 + 前端受控状态机 + 后端真实记录落库
|
||||
- **辅助层**:Dify 承担培训 FAQ、知识点解释、HR 自然语言统计问答(ChatBI)
|
||||
- **辅助层**:Dify 承担培训 FAQ、知识点解释、HR 自然语言统计问答(ChatBI);员工培训 Agent 对接 Dify 时额外透传后台账号 `role`,供画布区分学员/HR/管理员提示词或分支
|
||||
|
||||
该方案更符合培训考试场景对“流程刚性、成绩可信、状态可恢复、结果可审计”的要求。
|
||||
|
||||
|
||||
Reference in new issue
Block a user