52 lines
1.4 KiB
Python
52 lines
1.4 KiB
Python
"""
|
|
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',
|
|
user_role=str(getattr(ctx.user, 'role', '') or '').strip() or None,
|
|
adapter_hints=ctx.adapter_hints,
|
|
user_role=getattr(ctx.user, 'role', None),
|
|
)
|
|
|
|
|
|
__all__ = [
|
|
'ChatExecutor',
|
|
'ChatStreamContext',
|
|
'build_dify_inputs_for_context',
|
|
]
|