Files
group_fqcd_jr/app/service/model_gateway.py
T
lzf_0626 6516ccb385 feat: 第二版——接口契约对齐 docs/05,修复静默故障与数据库基线
相对第一版 46fc976 的完整变更。组员迁移对照表见 docs/20。

一、对外契约对齐 docs/05(破坏性,共 4 处,组员需按 docs/20 调整)
1) 配置发布端点改为文档规定的复数资源名:submit→validations、
   approve→reviews(需 body decision)、activate→activations、
   rollback→rollbacks;第一版这 4 个动词式路径 docs/05 从未定义过。
2) 错误码由 8 个笼统码改为 15 个具体语义码(FORBIDDEN→AGENT_PERMISSION_DENIED、
   UNAUTHORIZED→AUTHENTICATION_REQUIRED、CONFLICT→RESOURCE_VERSION_CONFLICT、
   RESOURCE_NOT_FOUND→RUN_NOT_FOUND/SESSION_NOT_FOUND 等),
   输入类错误状态码 400→422。
3) POST /api/v1/agent-runs 与 GET /api/v1/agent-runs/{run_id} 统一为
   {data, meta} 信封(data 内字段名与语义未变)。
4) 错误响应体统一为 {error:{code,message,retryable,field_errors}, meta:{trace_id}},
   不再返回 FastAPI 默认的 {"detail": ...}。

二、数据库基线与约束
新增 39 张表的基线迁移(链根)与联合唯一键纠偏(4 张表、删 8 增 4,幂等收敛);
撤下 config_release 的双人复核 CHECK(应用层已允许自审,审核节点保留,
自审如实写入 reviewer_id);记忆 active key 生成列与唯一键;
activate 开始记录 supersedes_release_id 使版本链可追溯。
docs/00 基线未修改,未重命名或删除任何表与字段。

三、修复会静默出错或无报错的缺陷
- 跑完集成测试后平台会静默失去生效配置:清理只删自己创建的版本,却没有恢复被它
  顶成 superseded 的原生效版本,且审计一并删除因而完全无痕,表现为所有工具被拒
  但没有任何报错。已修清理逻辑并加恢复。
- Worker 单轮异常导致进程退出;记忆抽取调用方的“事务已开始”异常;
  召回缓存丢失 degraded 标记;连接时区未生效导致 created_at/updated_at 差 8 小时;
  .env 与 os.getenv 密钥来源分裂导致“没有可用的已批准模型端点”。
- 记忆信号识别漏判与跨键误命中;SSE 未带 Accept 的协商行为。

四、功能补齐
记忆链路 P1/P2/P3(抽取、受控词表、召回与缓存、生命周期级联及投影事件)、
fin_* 场内交易只读 ORM 层、agent_intent_config 状态流转并在运行期真正生效、
限流(Redis 固定窗口、故障一律放行)、游标校验、trace_id 中间件、
示例业务 Agent fund_query_demo 与一键端到端验证脚本,以及审计/指纹/迁移状态工具。

五、文档与验证
新增 docs/19(业务 Agent 接入实操)、docs/20(第一版迁移指南)与 docs/evidence 证据;
docs/01/02/06/08/09/17 同步实现现状。

验证结果:ruff 通过、mypy 103 文件无错、unit+contract 447 passed、
integration 29 passed、acceptance_check --production 7 PASS、
demo_agent_e2e 9/9 PASS(含失败关闭反证)。
2026-09-10 15:55:54 +08:00

265 lines
10 KiB
Python

import os
from collections.abc import Mapping
from dataclasses import dataclass
from typing import Any, Protocol
import httpx
from sqlalchemy import select
from app.core.errors import (
DependencyUnavailableError,
RecoverableAgentError,
UpstreamTimeoutError,
)
from app.infrastructure.db import SessionFactory
from app.model.configuration import ModelEndpointConfig
class ModelGateway(Protocol):
async def generate(self, *, endpoint_code: str, prompt: str, timeout_ms: int) -> str: ...
async def embed(self, *, endpoint_code: str, text: str, timeout_ms: int) -> list[float]: ...
class EndpointSettings(Protocol):
endpoint_code: str
base_url: str
model_name: str
secret_ref: str
class EnvironmentSecretResolver:
def resolve(self, secret_ref: str) -> str:
if not secret_ref.startswith("env:"):
raise RecoverableAgentError("模型密钥必须使用 env: 引用")
name = secret_ref.removeprefix("env:")
value = os.getenv(name)
if not value:
raise RecoverableAgentError("模型密钥未配置")
return value
class OpenAICompatibleGateway:
"""供应商无关的 Chat Completions Adapter;密钥只从 secret_ref 解析。"""
def __init__(
self,
endpoints: Mapping[str, EndpointSettings],
*,
secret_resolver: EnvironmentSecretResolver | None = None,
client: httpx.AsyncClient | None = None,
) -> None:
self.endpoints = endpoints
self.secret_resolver = secret_resolver or EnvironmentSecretResolver()
self.client = client
self._owns_client = client is None
async def generate(self, *, endpoint_code: str, prompt: str, timeout_ms: int) -> str:
endpoint = self.endpoints.get(endpoint_code)
if endpoint is None:
raise RecoverableAgentError("模型端点未注册")
token = self.secret_resolver.resolve(endpoint.secret_ref)
url = endpoint.base_url.rstrip("/") + "/chat/completions"
payload = {
"model": endpoint.model_name,
"messages": [{"role": "user", "content": prompt}],
"temperature": 0,
}
client = self.client or httpx.AsyncClient()
try:
response = await client.post(
url,
headers={"Authorization": f"Bearer {token}", "Content-Type": "application/json"},
json=payload,
timeout=httpx.Timeout(timeout_ms / 1000),
)
response.raise_for_status()
body: Any = response.json()
content = body.get("choices", [{}])[0].get("message", {}).get("content")
if not isinstance(content, str) or not content.strip():
raise DependencyUnavailableError("模型响应缺少文本")
return content
except httpx.TimeoutException as exc:
raise UpstreamTimeoutError("模型端点调用超时") from exc
except httpx.HTTPError as exc:
raise DependencyUnavailableError("模型端点调用失败") from exc
finally:
if self._owns_client:
await client.aclose()
async def embed(self, *, endpoint_code: str, text: str, timeout_ms: int) -> list[float]:
"""OpenAI-compatible Embeddings Adapter;密钥同样只从 secret_ref 解析。"""
endpoint = self.endpoints.get(endpoint_code)
if endpoint is None:
raise RecoverableAgentError("模型端点未注册")
token = self.secret_resolver.resolve(endpoint.secret_ref)
url = endpoint.base_url.rstrip("/") + "/embeddings"
payload = {"model": endpoint.model_name, "input": text}
client = self.client or httpx.AsyncClient()
try:
response = await client.post(
url,
headers={"Authorization": f"Bearer {token}", "Content-Type": "application/json"},
json=payload,
timeout=httpx.Timeout(timeout_ms / 1000),
)
response.raise_for_status()
body: Any = response.json()
vector = _first_embedding(body)
if vector is None:
raise DependencyUnavailableError("向量响应缺少 embedding")
return vector
except httpx.TimeoutException as exc:
raise UpstreamTimeoutError("向量端点调用超时") from exc
except httpx.HTTPError as exc:
raise DependencyUnavailableError("向量端点调用失败") from exc
finally:
if self._owns_client:
await client.aclose()
def _first_embedding(body: Any) -> list[float] | None:
"""取 OpenAI-compatible 响应的首条向量;结构不符时返回 None,由调用方失败关闭。"""
data = body.get("data") if isinstance(body, dict) else None
if not isinstance(data, list) or not data:
return None
entry = data[0]
raw = entry.get("embedding") if isinstance(entry, dict) else None
if not isinstance(raw, list) or not raw:
return None
try:
return [float(value) for value in raw]
except (TypeError, ValueError):
return None
class DatabaseModelGateway:
"""从当前数据库端点配置动态构造已批准的 OpenAI-compatible Adapter。"""
async def _endpoint(self, endpoint_code: str) -> ModelEndpointConfig:
async with SessionFactory() as session:
endpoint = await session.scalar(select(ModelEndpointConfig).where(
ModelEndpointConfig.endpoint_code == endpoint_code,
ModelEndpointConfig.status == "active",
))
if endpoint is None:
raise RecoverableAgentError("模型端点未注册或未激活")
return endpoint
async def generate(self, *, endpoint_code: str, prompt: str, timeout_ms: int) -> str:
endpoint = await self._endpoint(endpoint_code)
adapter = OpenAICompatibleGateway({endpoint.endpoint_code: endpoint})
return await adapter.generate(
endpoint_code=endpoint.endpoint_code, prompt=prompt, timeout_ms=timeout_ms
)
async def embed(self, *, endpoint_code: str, text: str, timeout_ms: int) -> list[float]:
endpoint = await self._endpoint(endpoint_code)
adapter = OpenAICompatibleGateway({endpoint.endpoint_code: endpoint})
return await adapter.embed(
endpoint_code=endpoint.endpoint_code, text=text, timeout_ms=timeout_ms
)
class DatabaseModelEndpointResolver:
"""为统一意图分类提供当前已激活模型端点快照。"""
async def resolve(self, *, agent_type: str, task_type: str) -> list[ModelEndpointConfig]:
del agent_type, task_type # 路由筛选由发布配置和 ModelRouterService 扩展。
async with SessionFactory() as session:
return list(await session.scalars(select(ModelEndpointConfig).where(
ModelEndpointConfig.status == "active"
)))
@dataclass(frozen=True)
class ModelExecution:
endpoint_code: str
text: str
attempts: int
degraded: bool = False
@dataclass(frozen=True)
class ModelEmbedding:
endpoint_code: str
vector: list[float]
attempts: int
degraded: bool = False
class ModelDispatchService:
"""Executes only router-approved endpoints and falls back in declared order."""
def __init__(self, gateway: ModelGateway) -> None:
self.gateway = gateway
async def generate(
self,
endpoints: list[Any],
prompt: str,
*,
max_attempts: int = 2,
) -> ModelExecution:
last_error: Exception | None = None
attempts = 0
for endpoint in endpoints[: max(1, max_attempts)]:
attempts += 1
try:
text = await self.gateway.generate(
endpoint_code=endpoint.endpoint_code,
prompt=prompt,
timeout_ms=endpoint.timeout_ms,
)
return ModelExecution(endpoint.endpoint_code, text, attempts, attempts > 1)
except Exception as exc:
last_error = exc
raise RecoverableAgentError("所有已批准模型端点调用失败") from last_error
async def embed(
self, endpoints: list[Any], text: str, *, max_attempts: int = 2
) -> ModelEmbedding:
"""与文本生成同族的受控降级:按声明顺序尝试,成功即返回并标记是否降级。"""
last_error: Exception | None = None
attempts = 0
for endpoint in endpoints[: max(1, max_attempts)]:
attempts += 1
try:
vector = await self.gateway.embed(
endpoint_code=endpoint.endpoint_code,
text=text,
timeout_ms=endpoint.timeout_ms,
)
return ModelEmbedding(endpoint.endpoint_code, vector, attempts, attempts > 1)
except Exception as exc:
last_error = exc
raise RecoverableAgentError("所有已批准向量端点调用失败") from last_error
class ModelGenerationService:
"""业务 Agent 的唯一模型生成入口。路由结果必须先由 ModelRouterService 给出。"""
def __init__(self, dispatch: ModelDispatchService) -> None:
self.dispatch = dispatch
async def generate(
self, endpoints: list[Any], prompt: str, *, max_attempts: int = 2
) -> ModelExecution:
if not endpoints:
raise RecoverableAgentError("没有可用的已批准模型端点")
return await self.dispatch.generate(endpoints, prompt, max_attempts=max_attempts)
class ModelEmbeddingService:
"""文本向量化入口(记忆语义召回等只读用途),与生成同样必须先经过端点解析。"""
def __init__(self, dispatch: ModelDispatchService) -> None:
self.dispatch = dispatch
async def embed(
self, endpoints: list[Any], text: str, *, max_attempts: int = 2
) -> ModelEmbedding:
if not endpoints:
raise RecoverableAgentError("没有可用的已批准向量端点")
return await self.dispatch.embed(endpoints, text, max_attempts=max_attempts)