"""领域端口(依赖倒置)。""" from __future__ import annotations from abc import ABC, abstractmethod from typing import Any, AsyncIterator from domain.models import ChatMessage, 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) -> list[Session]: ... @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: ... 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) -> 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) -> LlmRuntimeConfig | None: ... class ChatStreamer(ABC): """流式生成助手回复。""" @abstractmethod async def stream_reply( self, *, llm: LlmRuntimeConfig, history: list[ChatMessage], user_text: str, tools: list[Any] | None = None, memory_count: int = 0, memory_just_saved: str | None = None, ) -> AsyncIterator[StreamEvent]: ...