feat: 将 Agent 接入运行时镜像挂载式部署

This commit is contained in:
2026-09-20 17:34:25 +08:00
parent 5393ad3423
commit 67c2d58c32
20 changed files with 499 additions and 40 deletions
+18
View File
@@ -0,0 +1,18 @@
# 本地开发(AGENT_ENV=dev,默认)
# 可另建 gitignore 的 .env 覆盖本文件中的密钥
JWT_SECRET=your-secret-key-here-must-be-at-least-64-bytes-long-for-hs512-algorithm-security
MONGO_URI=mongodb://ecom_dmb_agent_dev:ecom_dmb_agent_dev@192.168.3.100:27017/?authSource=ecom_dmb_agent_dev
MONGO_DB=ecom_dmb_agent_dev
# 与 Server application-dev 同 Redis DB=2
REDIS_URL=redis://:redis_qinghua@192.168.3.100:6379/2
SERVER_BASE_URL=http://localhost:8080
LLM_FALLBACK_ENABLED=true
LLM_FALLBACK_BASE_URL=https://api.deepseek.com/v1
LLM_FALLBACK_API_KEY=
LLM_FALLBACK_MODEL=deepseek-chat
CORS_ORIGINS=http://localhost:5173,http://localhost:5666
MCP_BASE_URL=http://localhost:8080/mcp
MCP_BEARER_TOKEN=dev-agent-mcp-token
MCP_TOOLS_ENABLED=true
PORT=8200
+13 -1
View File
@@ -1,7 +1,19 @@
# Agent 环境配置说明
#
# 按 AGENT_ENV 加载对应文件(与后端 Spring profile 对齐):
# AGENT_ENV=dev → .env.dev (本地默认)
# AGENT_ENV=test → .env.test
# AGENT_ENV=pre → .env.pre
# AGENT_ENV=prod → .env.prod
#
# 本地也可再建 gitignore 的 .env,会覆盖同名变量(便于放真实 Key)。
# 部署时 compose 注入 AGENT_ENV=test|pre|prod,并挂载对应 .env.{env}。
#
# Redis:与同环境 Server 共用同一 Redis DB,读缓存键 agent:llm:rt:*
JWT_SECRET=your-secret-key-here-must-be-at-least-64-bytes-long-for-hs512-algorithm-security
MONGO_URI=mongodb://ecom_dmb_agent_dev:ecom_dmb_agent_dev@192.168.3.100:27017/?authSource=ecom_dmb_agent_dev
MONGO_DB=ecom_dmb_agent_dev
# 与 Server 同 Redis DB,共享 agent:llm:rt:* 缓存
REDIS_URL=redis://:redis_qinghua@192.168.3.100:6379/2
SERVER_BASE_URL=http://localhost:8080
LLM_FALLBACK_ENABLED=true
+17
View File
@@ -0,0 +1,17 @@
# 预发环境(AGENT_ENV=pre,端口 8202)
# 与 Server application-pre 对齐:Redis DB=0、后端 8102
JWT_SECRET=your-secret-key-here-must-be-at-least-64-bytes-long-for-hs512-algorithm-security
MONGO_URI=mongodb://qinghua:mongo_qinghua@192.168.3.100:27017/?authSource=ecom_dmb_agent_pre
MONGO_DB=ecom_dmb_agent_pre
REDIS_URL=redis://:redis_qinghua@192.168.3.100:6379/0
SERVER_BASE_URL=http://192.168.3.101:8102
LLM_FALLBACK_ENABLED=false
LLM_FALLBACK_BASE_URL=
LLM_FALLBACK_API_KEY=
LLM_FALLBACK_MODEL=
CORS_ORIGINS=https://cd.newloop.top
MCP_BASE_URL=http://192.168.3.101:8102/mcp
MCP_BEARER_TOKEN=
MCP_TOOLS_ENABLED=true
PORT=8202
+17
View File
@@ -0,0 +1,17 @@
# 生产环境(AGENT_ENV=prod,端口 8203)
# 与 Server application-prod 对齐:Redis DB=0、后端 8103
JWT_SECRET=your-secret-key-here-must-be-at-least-64-bytes-long-for-hs512-algorithm-security
MONGO_URI=mongodb://qinghua:mongo_qinghua@192.168.3.100:27017/?authSource=ecom_dmb_agent
MONGO_DB=ecom_dmb_agent
REDIS_URL=redis://:redis_qinghua@192.168.3.100:6379/0
SERVER_BASE_URL=http://192.168.3.101:8103
LLM_FALLBACK_ENABLED=false
LLM_FALLBACK_BASE_URL=
LLM_FALLBACK_API_KEY=
LLM_FALLBACK_MODEL=
CORS_ORIGINS=https://cd.newloop.top
MCP_BASE_URL=http://192.168.3.101:8103/mcp
MCP_BEARER_TOKEN=
MCP_TOOLS_ENABLED=true
PORT=8203
+17
View File
@@ -0,0 +1,17 @@
# 测试环境(AGENT_ENV=test,端口 8201)
# 与 Server application-test 对齐:Redis DB=3、后端 8101
JWT_SECRET=your-secret-key-here-must-be-at-least-64-bytes-long-for-hs512-algorithm-security
MONGO_URI=mongodb://ecom_dmb_agent_test:ecom_dmb_agent_test@192.168.3.100:27017/?authSource=ecom_dmb_agent_test
MONGO_DB=ecom_dmb_agent_test
REDIS_URL=redis://:redis_qinghua@192.168.3.100:6379/3
SERVER_BASE_URL=http://192.168.3.101:8101
LLM_FALLBACK_ENABLED=false
LLM_FALLBACK_BASE_URL=
LLM_FALLBACK_API_KEY=
LLM_FALLBACK_MODEL=
CORS_ORIGINS=https://cd.newloop.top,http://localhost:5666
MCP_BASE_URL=http://192.168.3.101:8101/mcp
MCP_BEARER_TOKEN=
MCP_TOOLS_ENABLED=true
PORT=8201
+16 -2
View File
@@ -9,7 +9,7 @@ cd apps/agent
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
# 默认 AGENT_ENV=dev,加载 .env.dev;也可 cp .env.dev .env 做本地覆盖
uvicorn api.main:app --reload --host 0.0.0.0 --port 8200
```
@@ -18,13 +18,27 @@ API 前缀:`/api/agent/...`(会话/聊天/记忆;与前端代理 `/api/age
**模型 API 配置不在本服务**:浏览器走 Java `GET/PUT /api/company/agent-llm-settings`(凭证 + 多模型勾选;`POST .../probe-models` 拉目录);Agent 经 Redis `agent:llm:rt:{companyId}` 或内部接口拉**默认模型**明文注入。
## 分环境配置
与后端 `application-{profile}.yml` 对应,由 `AGENT_ENV` 选择文件:
| AGENT_ENV | 配置文件 | 端口 | Redis DB(对齐 Server) |
|-----------|----------|------|-------------------------|
| `dev`(默认) | `.env.dev` | 8200 | 2 |
| `test` | `.env.test` | 8201 | 3 |
| `pre` | `.env.pre` | 8202 | 0 |
| `prod` | `.env.prod` | 8203 | 0 |
若存在 gitignore 的 `.env`,仅在 `AGENT_ENV=dev` 时覆盖同名变量。部署时 compose 注入 `AGENT_ENV=test|pre|prod`。
## 环境变量要点
| 变量 | 说明 |
|------|------|
| `AGENT_ENV` | 选择 `.env.{env}` |
| `JWT_SECRET` | 与 Java `jwt.secret` 一致 |
| `MONGO_URI` / `MONGO_DB` | Agent 独立库(sessions / messages / memory) |
| `REDIS_URL` | **与 Server 同 DB**,读 LLM 运行时缓存 |
| `REDIS_URL` | **与同环境 Server 同 DB**,读 LLM 运行时缓存 `agent:llm:rt:*` |
| `SERVER_BASE_URL` | Java 根地址,内部拉 `/api/internal/agent/llm-runtime` |
| `MCP_BASE_URL` / `MCP_BEARER_TOKEN` | 调 Server `/mcp`,并带 `X-Agent-Company-Id` / `X-Agent-User-Id` |
| `LLM_FALLBACK_*` | 仅本地:公司未配置时的 OpenAI Compatible fallback |
+36 -4
View File
@@ -1,14 +1,46 @@
"""应用配置(环境变量)。"""
"""应用配置:按 AGENT_ENV 加载 .env.{env},环境变量优先。"""
from __future__ import annotations
import os
from functools import lru_cache
from pathlib import Path
from pydantic_settings import BaseSettings, SettingsConfigDict
# 与后端 Spring profile 对齐:dev | test | pre | prod
_AGENT_ENV = (os.getenv("AGENT_ENV") or "dev").strip().lower() or "dev"
def _resolve_env_files() -> tuple[str, ...]:
"""加载 .env.{AGENT_ENV};仅 dev 额外允许 .env 覆盖(本机密钥)。"""
files: list[str] = []
profile = Path(f".env.{_AGENT_ENV}")
if profile.is_file():
files.append(str(profile))
if _AGENT_ENV == "dev":
local = Path(".env")
if local.is_file():
files.append(str(local))
if not files:
# 兼容:尚无分环境文件时回退 .env
if Path(".env").is_file():
files.append(".env")
else:
files.append(f".env.{_AGENT_ENV}")
return tuple(files)
class Settings(BaseSettings):
model_config = SettingsConfigDict(env_file=".env", env_file_encoding="utf-8", extra="ignore")
model_config = SettingsConfigDict(
env_file=_resolve_env_files(),
env_file_encoding="utf-8",
extra="ignore",
)
app_name: str = "ecom-dmb-agent"
# 当前环境名(只读展示;实际由 AGENT_ENV 决定加载哪个文件)
agent_env: str = _AGENT_ENV
host: str = "0.0.0.0"
port: int = 8200
cors_origins: str = "http://localhost:5173,http://localhost:5666"
@@ -20,14 +52,14 @@ class Settings(BaseSettings):
mongo_uri: str = "mongodb://ecom_dmb_agent_dev:ecom_dmb_agent_dev@192.168.3.100:27017/?authSource=ecom_dmb_agent_dev"
mongo_db: str = "ecom_dmb_agent_dev"
# 须与 Server Redis 同库,才能命中 agent:llm:rt:{companyId}
# 须与同环境 Server Redis 同库,才能命中 agent:llm:rt:* 缓存
redis_url: str = "redis://:redis_qinghua@192.168.3.100:6379/2"
llm_cache_ttl_seconds: int = 600
# Java Server 根地址(内部拉 LLM 运行时)
server_base_url: str = "http://localhost:8080"
# 仅本地开发:公司未配置时的 fallback(生产勿用)
# 仅本地开发:公司未配置时的 fallback(test/pre/prod 应关闭)
llm_fallback_enabled: bool = True
llm_fallback_base_url: str = "https://api.openai.com/v1"
llm_fallback_api_key: str = ""
+14
View File
@@ -0,0 +1,14 @@
# 运行时镜像:业务代码与依赖通过 volume 挂载 /app,更新部署无需重建镜像
ARG BASE_IMAGE=docker.m.daocloud.io/library/python:3.12-slim
FROM ${BASE_IMAGE}
WORKDIR /app
RUN groupadd --system app && useradd --system --gid app --home-dir /app app
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
USER app
# 实际端口由环境 PORT 决定(test:8201 / pre:8202 / prod:8203)
EXPOSE 8201 8202 8203
ENTRYPOINT ["/entrypoint.sh"]
+5
View File
@@ -0,0 +1,5 @@
#!/bin/sh
# 使用镜像内 Python 加载挂载的源码与 .deps(避免 venv shebang 路径不可移植)
set -e
export PYTHONPATH="/app:/app/.deps${PYTHONPATH:+:$PYTHONPATH}"
exec python -m uvicorn api.main:app --host 0.0.0.0 --port "${PORT:-8201}"
+9
View File
@@ -0,0 +1,9 @@
{
"include": ["."],
"exclude": [".venv", "**/__pycache__"],
"venvPath": ".",
"venv": ".venv",
"pythonVersion": "3.12",
"pythonPlatform": "Linux",
"extraPaths": ["."]
}