mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 01:59:20 +08:00
128 lines
3.2 KiB
Python
128 lines
3.2 KiB
Python
"""领域端口(依赖倒置)。"""
|
|
|
|
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,
|
|
*,
|
|
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: ...
|
|
|
|
|
|
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 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,
|
|
) -> AsyncIterator[StreamEvent]: ...
|