feat: 新增agent

This commit is contained in:
2026-09-18 17:34:38 +08:00
parent 0d9b5a21c2
commit bc73668737
64 changed files with 3293 additions and 18 deletions
View File
+105
View File
@@ -0,0 +1,105 @@
"""领域模型(无框架依赖)。"""
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime
from enum import Enum
from typing import Any
class SessionStatus(str, Enum):
ACTIVE = "active"
ARCHIVED = "archived"
DELETED = "deleted"
class MessageRole(str, Enum):
USER = "user"
ASSISTANT = "assistant"
SYSTEM = "system"
TOOL = "tool"
class MemoryKind(str, Enum):
PREFERENCE = "preference"
FACT = "fact"
ENTITY = "entity"
SUMMARY = "summary"
class MemoryStatus(str, Enum):
ACTIVE = "active"
STALE = "stale"
DELETED = "deleted"
@dataclass(frozen=True)
class Actor:
"""当前请求主体。"""
user_id: str
company_id: int
company_role: str | None = None
is_super: bool = False
def can_manage_llm(self) -> bool:
if self.is_super:
return True
role = (self.company_role or "").lower()
return role in {"owner", "admin"}
@dataclass
class Session:
thread_id: str
company_id: int
user_id: str
title: str = "新对话"
status: SessionStatus = SessionStatus.ACTIVE
summary: str | None = None
created_at: datetime = field(default_factory=datetime.utcnow)
updated_at: datetime = field(default_factory=datetime.utcnow)
last_message_at: datetime | None = None
@dataclass
class ChatMessage:
company_id: int
user_id: str
thread_id: str
seq: int
role: MessageRole
content: str
tool_name: str | None = None
tool_call_id: str | None = None
created_at: datetime = field(default_factory=datetime.utcnow)
id: str | None = None
@dataclass
class UserMemory:
"""用户级长期记忆条目。"""
company_id: int
user_id: str
kind: MemoryKind
text: str
key: str | None = None
value: dict[str, Any] = field(default_factory=dict)
source_thread_id: str | None = None
status: MemoryStatus = MemoryStatus.ACTIVE
confidence: float = 0.8
created_at: datetime = field(default_factory=datetime.utcnow)
updated_at: datetime = field(default_factory=datetime.utcnow)
id: str | None = None
@dataclass(frozen=True)
class LlmRuntimeConfig:
"""注入 LLM 客户端的运行时配置(含明文 key,仅进程内)。"""
base_url: str
api_key: str
model: str
extra_headers: dict[str, str] = field(default_factory=dict)
+81
View File
@@ -0,0 +1,81 @@
"""领域端口(依赖倒置)。"""
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]: ...