feat(chat): 透传培训agent账号角色到dify
This commit is contained in:
1 parent
fd024c1c32
commit
9ea49d7060
5 files changed
+114
-4
No files matched your search
@@ -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,
|
||||
)
|
||||
Reference in new issue
Block a user