"""领域端口(依赖倒置)。""" from __future__ import annotations from abc import ABC, abstractmethod from typing import Any, AsyncIterator, Protocol from domain.models import ( ChatMessage, LlmProtocolCapability, LlmRuntimeConfig, Session, StreamEvent, UserMemory, ) class SessionRepository(ABC): @abstractmethod async def create(self, session: Session) -> Session: ... @abstractmethod async def get(self, company_id: int, user_id: str, thread_id: str) -> Session | None: ... @abstractmethod async def list_active( self, company_id: int, user_id: str, limit: int = 50, *, scene: str | None = None, ) -> list[Session]: ... @abstractmethod async def find_active_by_scene_ref( self, company_id: int, user_id: str, *, scene: str, scene_ref: str, ) -> Session | None: ... @abstractmethod async def update(self, session: Session) -> Session: ... class MessageRepository(ABC): @abstractmethod async def append(self, message: ChatMessage) -> ChatMessage: ... @abstractmethod async def list_recent( self, company_id: int, user_id: str, thread_id: str, limit: int ) -> list[ChatMessage]: ... @abstractmethod async def next_seq(self, company_id: int, user_id: str, thread_id: str) -> int: ... @abstractmethod async def soft_delete_by_thread( self, company_id: int, user_id: str, thread_id: str ) -> int: """软删某会话下全部消息,返回影响条数。""" ... class MemoryRepository(ABC): @abstractmethod async def list_active( self, company_id: int, user_id: str, limit: int ) -> list[UserMemory]: ... @abstractmethod async def count_active(self, company_id: int, user_id: str) -> int: ... @abstractmethod async def search( self, company_id: int, user_id: str, query: str, limit: int ) -> list[UserMemory]: ... @abstractmethod async def get_by_ids( self, company_id: int, user_id: str, memory_ids: list[str] ) -> list[UserMemory]: ... @abstractmethod async def upsert(self, memory: UserMemory) -> UserMemory: ... @abstractmethod async def soft_delete(self, company_id: int, user_id: str, memory_id: str) -> bool: ... class LlmRuntimeCache(ABC): @abstractmethod async def get( self, company_id: int, provider_id: int | None = None, model: str | None = None, ) -> LlmRuntimeConfig | None: ... @abstractmethod async def set( self, company_id: int, config: LlmRuntimeConfig, ttl_seconds: int, ) -> None: ... @abstractmethod async def delete(self, company_id: int) -> None: ... class LlmRuntimeGateway(ABC): """从 Server 拉取运行时模型配置(MySQL 真相源)。""" @abstractmethod async def fetch( self, company_id: int, provider_id: int | None = None, model: str | None = None, ) -> LlmRuntimeConfig | None: ... class LlmProtocolCapabilityCache(ABC): """缓存某提供商(或 base_url)是否支持 Responses / store。""" @abstractmethod async def get( self, *, provider_id: int | None, base_url: str ) -> LlmProtocolCapability | None: ... @abstractmethod async def set( self, *, provider_id: int | None, base_url: str, capability: LlmProtocolCapability, ttl_seconds: int, ) -> None: ... class ClientToolTurn(Protocol): """一轮聊天里前端工具的回执等待:先 arm 再推 SSE,再 wait_result。""" def arm(self, tool_call_id: str) -> None: ... async def wait_result(self, tool_call_id: str) -> str: ... class ChatStreamer(ABC): """流式生成助手回复。""" @abstractmethod async def stream_reply( self, *, llm: LlmRuntimeConfig, history: list[ChatMessage], user_text: str, system_prompt: str, tools: list[Any] | None = None, max_tool_result_chars: int = 4000, previous_response_id: str | None = None, client_tool_turn: ClientToolTurn | None = None, ) -> AsyncIterator[StreamEvent]: ...