feat(agent): 增强 Agent 配置,新增结构化字段与聊天功能支持

This commit is contained in:
hugo committed 2026-07-07 11:10:32 +08:00
1 parent ac4262cd75
commit c8ce77199e
183 files changed
+11690 -2300

No files matched your search

@@ -0,0 +1,75 @@
"""
backend/app/services/chat/executors/__init__.py
用途:RUNTIME_PROVIDER_REGISTRY 与聊天 Executor 解析入口。
依赖:dify_chat、hybrid、builtin executors;agent_registry.resolver。
逻辑:chat.py completions 默认 Dify 路径调用 stream_chat_via_executor。
"""
from __future__ import annotations
from typing import Iterator
from app.core.errors import ValidationError
from app.services.agent_registry.handlers.base import ChatStreamContext
from app.services.agent_registry.resolver import resolve_runtime_provider
from app.services.chat.executors.base import ChatExecutor
from app.services.chat.executors.builtin import BuiltinChatExecutor
from app.services.chat.executors.dify_chat import DifyChatExecutor
from app.services.chat.executors.dify_workflow import DifyWorkflowExecutor
from app.services.chat.executors.hybrid import HybridChatExecutor
_dify_chat = DifyChatExecutor()
_dify_workflow = DifyWorkflowExecutor()
_builtin = BuiltinChatExecutor()
_hybrid = HybridChatExecutor()
RUNTIME_PROVIDER_REGISTRY: dict[str, ChatExecutor] = {
'dify': _dify_chat,
'builtin': _builtin,
'hybrid': _hybrid,
}
def resolve_chat_executor(agent) -> ChatExecutor:
"""
按 agents.runtime_provider 解析 Executor。
核心逻辑:dify 时按 dify_app_type 区分 workflow/chatflow;iframe 禁止走 completions。
Raises:
ValidationError: runtime_provider=iframe。
"""
provider = resolve_runtime_provider(agent)
if provider == 'iframe':
raise ValidationError('Embedded iframe agents do not support chat completions')
if provider == 'dify':
app_type = str(getattr(agent, 'dify_app_type', None) or 'chatflow').strip().lower()
return _dify_workflow if app_type == 'workflow' else _dify_chat
executor = RUNTIME_PROVIDER_REGISTRY.get(provider)
if executor is None:
raise ValidationError(f'Unsupported runtime_provider: {provider}')
return executor
def stream_chat_via_executor(ctx: ChatStreamContext) -> Iterator[str]:
"""
经 RUNTIME_PROVIDER_REGISTRY 执行聊天上游 SSE 流。
Args:
ctx: ChatStreamContext。
Returns:
上游 SSE 行迭代器。
"""
executor = resolve_chat_executor(ctx.agent)
return executor.stream(ctx)
__all__ = [
'RUNTIME_PROVIDER_REGISTRY',
'resolve_chat_executor',
'stream_chat_via_executor',
]
@@ -0,0 +1,49 @@
"""
backend/app/services/chat/executors/base.py
用途:聊天 Executor 基类与 Dify 流构建辅助。
依赖:agent_runtime_context.dify_inputs、agent_registry.handlers。
逻辑:各 runtime_provider Executor 实现 stream() 返回上游 SSE 行迭代器。
"""
from __future__ import annotations
from abc import ABC, abstractmethod
from typing import Any, Iterator
from app.services.agent_registry.handlers.base import ChatStreamContext
class ChatExecutor(ABC):
"""聊天运行时 Executor 基类。"""
provider_key: str = ''
@abstractmethod
def stream(self, ctx: ChatStreamContext) -> Iterator[str]:
"""
产出上游 SSE 行(`data: {...}\\n\\n`)。
Args:
ctx: 聊天流上下文。
"""
def build_dify_inputs_for_context(ctx: ChatStreamContext) -> dict[str, Any]:
"""调用 Context Adapter Registry 构建 Dify inputs。"""
from app.services.agent_runtime_context.dify_inputs import build_chat_dify_inputs
return build_chat_dify_inputs(
ctx.agent,
session_id=str(getattr(ctx.session, 'session_id', '') or ''),
request_id=ctx.request_id,
trigger_source='frontend_chat',
adapter_hints=ctx.adapter_hints,
)
__all__ = [
'ChatExecutor',
'ChatStreamContext',
'build_dify_inputs_for_context',
]
@@ -0,0 +1,31 @@
"""
backend/app/services/chat/executors/builtin.py
用途:runtime_provider=builtin 的聊天 Executor。
依赖:agent_registry.resolver.resolve_handler。
逻辑:必须存在 Handler;由 Handler 驱动内置流(v1 未实现完整 builtin 流时显式报错)。
"""
from __future__ import annotations
from typing import Iterator
from app.core.errors import AppError
from app.services.agent_registry.resolver import resolve_handler
from app.services.chat.executors.base import ChatExecutor, ChatStreamContext
class BuiltinChatExecutor(ChatExecutor):
"""纯内置运行时 Executor。"""
provider_key = 'builtin'
def stream(self, ctx: ChatStreamContext) -> Iterator[str]:
handler = resolve_handler(ctx.agent)
ctx = handler.prepare_stream(ctx)
raise AppError(
code='AGENT.BUILTIN_NOT_IMPLEMENTED',
message='runtime_provider=builtin 的聊天流尚未实现,请使用 hybrid 或 dify',
http_status=501,
details={'handler_key': handler.handler_key},
)
@@ -0,0 +1,43 @@
"""
backend/app/services/chat/executors/dify_chat.py
用途:runtime_provider=dify 的 Chatflow 聊天 Executor。
依赖:DifyService.stream_chat、executors.base、agent_registry.resolver。
逻辑:Handler.prepare_stream → build_chat_dify_inputs → DifyService.stream_chat。
"""
from __future__ import annotations
from typing import Iterator
from app.services.agent_registry.resolver import resolve_handler_for_agent_code
from app.services.chat.executors.base import (
ChatExecutor,
ChatStreamContext,
build_dify_inputs_for_context,
)
from app.services.dify import DifyService
class DifyChatExecutor(ChatExecutor):
"""Dify Chatflow 流式对话 Executor。"""
provider_key = 'dify'
def stream(self, ctx: ChatStreamContext) -> Iterator[str]:
handler = resolve_handler_for_agent_code(ctx.agent)
if handler is not None:
ctx = handler.prepare_stream(ctx)
dify_inputs = build_dify_inputs_for_context(ctx)
yield from DifyService.stream_chat(
ctx.query,
int(getattr(ctx.user, 'id', 0) or 0),
ctx.agent,
ctx.external_conversation_id,
request_id=ctx.request_id,
session_id=str(getattr(ctx.session, 'session_id', '') or ''),
hxops_turn_id=ctx.hxops_turn_id,
files=ctx.normalized_files,
inputs=dify_inputs,
display_query=ctx.display_query,
)
@@ -0,0 +1,17 @@
"""
backend/app/services/chat/executors/dify_workflow.py
用途:runtime_provider=dify 且 dify_app_type=workflow 的 Executor。
依赖:DifyChatExecutor(当前聊天 completions 仍走 chat-messages SSE)。
逻辑:v1 与 Chatflow 共用 stream_chat;后续可切换 run_workflow_streaming 适配器。
"""
from __future__ import annotations
from app.services.chat.executors.dify_chat import DifyChatExecutor
class DifyWorkflowExecutor(DifyChatExecutor):
"""Dify Workflow 类型 Agent 的聊天 Executor(暂与 Chatflow 同路径)。"""
provider_key = 'dify_workflow'
@@ -0,0 +1,29 @@
"""
backend/app/services/chat/executors/hybrid.py
用途:runtime_provider=hybrid 的聊天 Executor(Handler 前置 + Dify 流)。
依赖:resolve_handler、DifyChatExecutor。
逻辑:resolve_handler 校验 → handler.prepare_stream → DifyChatExecutor.stream。
"""
from __future__ import annotations
from typing import Iterator
from app.services.agent_registry.resolver import resolve_handler
from app.services.chat.executors.base import ChatExecutor, ChatStreamContext
from app.services.chat.executors.dify_chat import DifyChatExecutor
class HybridChatExecutor(ChatExecutor):
"""Hybrid:业务 Handler 前置 + Dify Chatflow 流。"""
provider_key = 'hybrid'
def __init__(self) -> None:
self._dify = DifyChatExecutor()
def stream(self, ctx: ChatStreamContext) -> Iterator[str]:
handler = resolve_handler(ctx.agent)
ctx = handler.prepare_stream(ctx)
yield from self._dify.stream(ctx)