Files
ecom-dmb/apps/agent/domain/ports.py
T
2026-09-18 17:34:38 +08:00

82 lines
2.2 KiB
Python

"""领域端口(依赖倒置)。"""
from __future__ import annotations
from abc import ABC, abstractmethod
from typing import Any, AsyncIterator
from domain.models import ChatMessage, LlmRuntimeConfig, Session, 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 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,
memory_lines: list[str] | None = None,
tools: list[Any] | None = None,
) -> AsyncIterator[str]: ...