mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-23 23:19:19 +08:00
feat: 重构 Agent 目录为 api/agent/infrastructure 分层并更新启动入口
This commit is contained in:
@@ -40,7 +40,7 @@ deploy/ # 部署 TUI(原 ecom-deploy),流程见 deploy/MANU
|
||||
- Python 3.11+;设计 `apps/agent/DESIGN.md`
|
||||
- `cd apps/agent && python -m venv .venv && source .venv/bin/activate && pip install -r requirements.txt`
|
||||
- 配置 `.env`(参考 `.env.example`):`JWT_SECRET` 须与 Java `jwt.secret` 一致
|
||||
- 运行:`uvicorn app.main:app --reload --port 8200`
|
||||
- 运行:`uvicorn api.main:app --reload --port 8200`
|
||||
- 租户自管 OpenAI Compatible 凭证(Mongo 加密 + Redis 缓存);聊天/会话在 Agent Mongo 库
|
||||
|
||||
## 授权(apps/license-server)
|
||||
|
||||
+23
-19
@@ -79,25 +79,25 @@
|
||||
|
||||
```text
|
||||
apps/agent/
|
||||
app/ # 接口层:HTTP / DTO / 鉴权依赖
|
||||
api/ # 接口层:HTTP / DTO / 鉴权依赖
|
||||
application/ # 应用层:用例编排(发消息、列会话、恢复上下文)
|
||||
domain/ # 领域层:Session / Message / Memory 模型与规则(无框架)
|
||||
infrastructure/ # 基础设施:Mongo、LLM、MCP client、JWT 校验
|
||||
graphs/ # LangGraph 图定义(可视为 application 的特殊编排)
|
||||
agent/ # 编排层:工具装载、运行态、LangGraph(预留)
|
||||
infrastructure/ # 基础设施:Mongo/Redis、LLM、MCP、JWT
|
||||
config/ # 配置
|
||||
```
|
||||
|
||||
依赖方向:**app → application → domain ← infrastructure**;`graphs` 只依赖 application/domain 端口,MCP/LLM 走 infrastructure 适配器。
|
||||
依赖方向:**api → application → domain ← infrastructure**;`application` 经 `agent` 装载工具;`agent` 只依赖 domain/config 与 infrastructure 适配器,不写 Mongo。
|
||||
|
||||
### 3.1 分层职责
|
||||
|
||||
| 层 | 包 | 职责 | 禁止 |
|
||||
|----|-----|------|------|
|
||||
| 接口 | `app` | 路由、请求/响应模型、依赖注入、鉴权提取 | 拼 prompt、直连 Mongo、调 MCP SDK |
|
||||
| 接口 | `api` | 路由、请求/响应模型、依赖注入、鉴权提取 | 拼 prompt、直连 Mongo、调 MCP SDK |
|
||||
| 应用 | `application` | 「用户发一条消息」「新建会话」「加载记忆」用例、事务边界(逻辑) | 框架注解泄漏到 domain |
|
||||
| 领域 | `domain` | 会话状态机、记忆条目规则、隔离键不变量 | HTTP、Motor、LangChain 类型 |
|
||||
| 基础设施 | `infrastructure` | Mongo 仓库、MCP、ChatModel、JWT JWKS/本地校验 | 业务用例 if/else 堆砌 |
|
||||
| 图 | `graphs` | LangGraph StateGraph、节点、边 | 写 Mongo 细节(经 repository 端口) |
|
||||
| 编排 | `agent` | 工具装载、Turn State、LangGraph 图(预留) | 写 Mongo 细节、HTTP 路由 |
|
||||
| 基础设施 | `infrastructure` | Mongo/Redis 仓库、MCP、ChatModel、JWT 校验 | 业务用例 if/else 堆砌 |
|
||||
|
||||
---
|
||||
|
||||
@@ -257,12 +257,13 @@ api/agent/ # 调 Agent 服务,非调 Java /api
|
||||
|
||||
```text
|
||||
apps/agent/
|
||||
pyproject.toml / requirements.txt
|
||||
requirements.txt
|
||||
README.md
|
||||
DESIGN.md # 本文
|
||||
app/
|
||||
api/
|
||||
main.py
|
||||
deps.py # 当前用户
|
||||
deps.py # 当前用户 / 用例组装
|
||||
response.py
|
||||
routers/sessions.py
|
||||
routers/chat.py
|
||||
routers/memories.py
|
||||
@@ -270,19 +271,22 @@ apps/agent/
|
||||
session_service.py
|
||||
chat_service.py
|
||||
memory_service.py
|
||||
llm_settings_service.py # 现为 LlmRuntimeService:读 Redis/Server
|
||||
llm_settings_service.py # LlmRuntimeService:读 Redis/Server
|
||||
domain/
|
||||
models.py
|
||||
ports.py
|
||||
agent/
|
||||
runtime.py # 本轮工具装载入口
|
||||
state.py # Turn State(图扩展点)
|
||||
tools/builtin.py
|
||||
graphs/ # LangGraph 预留
|
||||
infrastructure/
|
||||
mongo/
|
||||
redis_cache.py
|
||||
mcp_client.py
|
||||
server_llm_gateway.py
|
||||
jwt_auth.py
|
||||
llm_factory.py
|
||||
graphs/
|
||||
assistant_graph.py
|
||||
auth/jwt.py
|
||||
llm/factory.py
|
||||
llm/gateway.py
|
||||
mcp/client.py
|
||||
persistence/mongo/
|
||||
persistence/redis/
|
||||
config/settings.py
|
||||
```
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ python -m venv .venv
|
||||
source .venv/bin/activate
|
||||
pip install -r requirements.txt
|
||||
cp .env.example .env # JWT_SECRET / Mongo / Redis(与 Server 同库) / SERVER_BASE_URL / MCP
|
||||
uvicorn app.main:app --reload --host 0.0.0.0 --port 8200
|
||||
uvicorn api.main:app --reload --host 0.0.0.0 --port 8200
|
||||
```
|
||||
|
||||
健康检查:`GET http://localhost:8200/health`
|
||||
@@ -31,4 +31,4 @@ API 前缀:`/api/agent/...`(会话/聊天/记忆;与前端代理 `/api/age
|
||||
|
||||
## 分层
|
||||
|
||||
`app` → `application` → `domain` ← `infrastructure`
|
||||
`api` → `application` → `domain` ← `infrastructure`;编排在 `agent/`(工具装载 / 图预留)
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
"""编排层:工具装载与 LangGraph 扩展点(不写 Mongo)。"""
|
||||
@@ -0,0 +1 @@
|
||||
"""LangGraph 图定义预留。"""
|
||||
@@ -0,0 +1,20 @@
|
||||
"""Agent 编排:工具装载与(预留)图运行时;不写 Mongo。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from config.settings import Settings
|
||||
|
||||
|
||||
async def load_tools_for_turn(
|
||||
settings: Settings, *, company_id: int, user_id: str
|
||||
) -> list[Any]:
|
||||
"""本轮可用工具 = 本地 builtin + MCP(按租户头)。"""
|
||||
from agent.tools.builtin import builtin_tools
|
||||
from infrastructure.mcp.client import load_mcp_tools
|
||||
|
||||
mcp_tools = await load_mcp_tools(
|
||||
settings, company_id=company_id, user_id=user_id
|
||||
)
|
||||
return [*builtin_tools(), *mcp_tools]
|
||||
@@ -0,0 +1,18 @@
|
||||
"""Agent 运行态占位(LangGraph State 后续落此)。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any
|
||||
|
||||
|
||||
@dataclass
|
||||
class AgentTurnState:
|
||||
"""单轮对话编排状态(v1 尚未被图驱动,仅作扩展点)。"""
|
||||
|
||||
company_id: int
|
||||
user_id: str
|
||||
thread_id: str
|
||||
memory_lines: list[str] = field(default_factory=list)
|
||||
tool_names: list[str] = field(default_factory=list)
|
||||
extras: dict[str, Any] = field(default_factory=dict)
|
||||
@@ -0,0 +1,5 @@
|
||||
"""Agent 工具面:本地 builtin;MCP 传输在 infrastructure.mcp。"""
|
||||
|
||||
from agent.tools.builtin import builtin_tools
|
||||
|
||||
__all__ = ["builtin_tools"]
|
||||
@@ -0,0 +1 @@
|
||||
"""HTTP 接口层:路由、DTO、依赖注入。"""
|
||||
@@ -10,16 +10,16 @@ from application.memory_service import MemoryService
|
||||
from application.session_service import SessionService
|
||||
from config.settings import Settings, get_settings
|
||||
from domain.models import Actor
|
||||
from infrastructure.jwt_auth import parse_actor
|
||||
from infrastructure.llm_factory import OpenAICompatibleStreamer
|
||||
from infrastructure.mongo.client import get_db
|
||||
from infrastructure.mongo.repositories import (
|
||||
from infrastructure.auth.jwt import parse_actor
|
||||
from infrastructure.llm.factory import OpenAICompatibleStreamer
|
||||
from infrastructure.llm.gateway import ServerLlmRuntimeGateway
|
||||
from infrastructure.persistence.mongo.client import get_db
|
||||
from infrastructure.persistence.mongo.repositories import (
|
||||
MongoMemoryRepository,
|
||||
MongoMessageRepository,
|
||||
MongoSessionRepository,
|
||||
)
|
||||
from infrastructure.redis_cache import RedisLlmRuntimeCache, get_redis
|
||||
from infrastructure.server_llm_gateway import ServerLlmRuntimeGateway
|
||||
from infrastructure.persistence.redis.cache import RedisLlmRuntimeCache, get_redis
|
||||
|
||||
|
||||
async def current_actor(
|
||||
@@ -8,11 +8,11 @@ from fastapi import FastAPI, HTTPException, Request
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from app.response import fail
|
||||
from app.routers import chat, memories, sessions
|
||||
from api.response import fail
|
||||
from api.routers import chat, memories, sessions
|
||||
from config.settings import get_settings
|
||||
from infrastructure.mongo.client import close_mongo, ensure_indexes, get_db
|
||||
from infrastructure.redis_cache import close_redis
|
||||
from infrastructure.persistence.mongo.client import close_mongo, ensure_indexes, get_db
|
||||
from infrastructure.persistence.redis.cache import close_redis
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
@@ -11,3 +11,4 @@ def ok(data: Any = None, message: str = "操作成功") -> dict[str, Any]:
|
||||
|
||||
def fail(message: str, code: int = 1) -> dict[str, Any]:
|
||||
return {"code": code, "message": message, "data": None}
|
||||
|
||||
@@ -8,8 +8,8 @@ from fastapi import APIRouter, Depends, HTTPException
|
||||
from pydantic import BaseModel, Field
|
||||
from sse_starlette.sse import EventSourceResponse
|
||||
|
||||
from app.deps import chat_service, current_actor
|
||||
from app.response import ok
|
||||
from api.deps import chat_service, current_actor
|
||||
from api.response import ok
|
||||
from application.chat_service import ChatService
|
||||
from domain.models import Actor
|
||||
|
||||
@@ -4,8 +4,8 @@ from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
|
||||
from app.deps import current_actor, memory_service
|
||||
from app.response import ok
|
||||
from api.deps import current_actor, memory_service
|
||||
from api.response import ok
|
||||
from application.memory_service import MemoryService
|
||||
from domain.models import Actor
|
||||
|
||||
@@ -5,8 +5,8 @@ from __future__ import annotations
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from app.deps import current_actor, session_service
|
||||
from app.response import ok
|
||||
from api.deps import current_actor, session_service
|
||||
from api.response import ok
|
||||
from application.session_service import SessionService
|
||||
from domain.models import Actor
|
||||
|
||||
@@ -9,10 +9,9 @@ from application.llm_settings_service import LlmRuntimeService
|
||||
from application.memory_service import MemoryService
|
||||
from application.session_service import SessionService
|
||||
from config.settings import Settings
|
||||
from agent.runtime import load_tools_for_turn
|
||||
from domain.models import Actor, ChatMessage, MessageRole
|
||||
from domain.ports import ChatStreamer, MessageRepository, SessionRepository
|
||||
from infrastructure.builtin_tools import builtin_tools
|
||||
from infrastructure.mcp_client import load_mcp_tools
|
||||
|
||||
|
||||
class ChatService:
|
||||
@@ -56,11 +55,11 @@ class ChatService:
|
||||
actor, thread_id=thread_id, text=text
|
||||
)
|
||||
memory_lines = await self._memory.lines_for_prompt(actor)
|
||||
mcp_tools = await load_mcp_tools(
|
||||
self._settings, company_id=actor.company_id, user_id=actor.user_id
|
||||
tools = await load_tools_for_turn(
|
||||
self._settings,
|
||||
company_id=actor.company_id,
|
||||
user_id=actor.user_id,
|
||||
)
|
||||
# 本地时间工具始终挂上,不依赖 MCP 是否可用
|
||||
tools = [*builtin_tools(), *mcp_tools]
|
||||
|
||||
history = await self._messages.list_recent(
|
||||
actor.company_id,
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
"""基础设施:鉴权、LLM、MCP、持久化适配。"""
|
||||
|
||||
Reference in New Issue
Block a user