Merge branch 'dev' into hugo-shipping-notice
# Conflicts: # backend/app/api/chat.py # backend/app/services/agent_runtime_context/dify_inputs.py # backend/app/services/maintenance_chat_service.py
This commit is contained in:
commit
fc694a8f43
55 files changed
+2107
-192
No files matched your search
@@ -17,6 +17,77 @@ from app.services.agent_runtime_context.builder import (
|
||||
)
|
||||
from app.services.agent_runtime_context.registry import register_context_adapter
|
||||
|
||||
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 _agent_handler_config(agent) -> dict[str, Any]:
|
||||
"""读取 Agent.handler_config;非字典时回退为空。"""
|
||||
handler_config = getattr(agent, 'handler_config', None)
|
||||
return handler_config if isinstance(handler_config, dict) else {}
|
||||
|
||||
|
||||
def is_training_chat_agent(agent) -> bool:
|
||||
"""
|
||||
判断当前 Agent 是否启用培训工作区语义。
|
||||
|
||||
核心逻辑:优先 handler_config.content_profile=training 与 extra_config 显式开关;
|
||||
未配置时再回退 Agent 名称关键字,避免普通 Agent 被误注入培训专用字段。
|
||||
|
||||
Args:
|
||||
agent: 当前对话 Agent 记录或代理对象。
|
||||
|
||||
Returns:
|
||||
命中培训 Agent 返回 True,否则返回 False。
|
||||
"""
|
||||
if _agent_handler_config(agent).get('content_profile') == 'training':
|
||||
return True
|
||||
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 输入。
|
||||
|
||||
核心逻辑:仅当命中培训 Agent 且当前用户角色非空时,基于现有 inputs 复制后追加 `role`。
|
||||
|
||||
Args:
|
||||
base_inputs: 已构建好的 Dify inputs。
|
||||
agent: 当前对话 Agent。
|
||||
user_role: 后端鉴权后的真实账号角色,来源于 users.role。
|
||||
|
||||
Returns:
|
||||
若符合条件则包含 `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 resolve_agent_code(agent) -> str:
|
||||
"""
|
||||
@@ -123,13 +194,15 @@ def build_generic_context_adapter(
|
||||
request_id: str | None = None,
|
||||
trigger_source: str | None = 'frontend_chat',
|
||||
locale: str | None = None,
|
||||
user_role: str | None = None,
|
||||
**kwargs: Any,
|
||||
) -> dict[str, str]:
|
||||
) -> dict[str, Any]:
|
||||
"""通用 Context Adapter 入口,供 CONTEXT_ADAPTER_REGISTRY 分发。"""
|
||||
return build_generic_dify_inputs(
|
||||
inputs = build_generic_dify_inputs(
|
||||
session_id=session_id,
|
||||
agent=agent,
|
||||
request_id=request_id,
|
||||
trigger_source=trigger_source,
|
||||
locale=locale,
|
||||
)
|
||||
return _append_training_role_input(inputs, agent=agent, user_role=user_role)
|
||||
Reference in new issue
Block a user