feat(v3): T17-T18 模型池 + 工具智能体后端

- T17 模型池:PoolStore(local/budget/premium 条目 + architect/worker/agent 角色指派),
  /pool CRUD+连通测试+模型探测端点;build_v2_pipeline 池指派优先(测试 override 最后);
  V2Stats 新增 by_model 按 token/成本分账
- T18 智能体:OpenAI 兼容工具调用客户端(transport 可注入)+ AgentService
  (事件落盘 agent_runs/{id}/events.jsonl)+ /agent 提交/status/events/SSE stream
  + 工作区浏览/读取端点(越界 400);轮数与 token 双护栏,模型经池 agent 角色或经典回退
- 新增测试 16 项,全量 262 passed(httpx 假注入,不依赖真实模型/key)
This commit is contained in:
tzt
2026-09-01 08:51:52 +08:00
parent 6247e053c1
commit 374dc765c2
8 changed files with 1571 additions and 36 deletions
+16
View File
@@ -22,6 +22,7 @@ class V2Stats:
self._api_input_tokens = 0
self._api_output_tokens = 0
self._api_cost_usd = 0.0
self._by_model: Dict[str, Dict[str, Any]] = {}
self._recent: List[Dict[str, Any]] = []
def record(self, result) -> None:
@@ -38,6 +39,17 @@ class V2Stats:
self._api_input_tokens += getattr(result, "api_input_tokens", 0)
self._api_output_tokens += getattr(result, "api_output_tokens", 0)
self._api_cost_usd += getattr(result, "cost_est", 0.0)
# 按模型分账(token / 成本 / 次数)
model = getattr(result, "model_used", None) or "unknown"
in_tok = getattr(result, "api_input_tokens", 0)
out_tok = getattr(result, "api_output_tokens", 0)
bucket = self._by_model.setdefault(model, {
"requests": 0, "input_tokens": 0, "output_tokens": 0, "cost_est_usd": 0.0,
})
bucket["requests"] += 1
bucket["input_tokens"] += in_tok
bucket["output_tokens"] += out_tok
bucket["cost_est_usd"] = round(bucket["cost_est_usd"] + getattr(result, "cost_est", 0.0), 6)
self._recent.append({
"request_id": getattr(result, "request_id", ""),
"status": status,
@@ -69,6 +81,10 @@ class V2Stats:
"total": self._api_input_tokens + self._api_output_tokens,
"cost_est_usd": round(self._api_cost_usd, 6),
},
"by_model": {
m: {**b, "cost_est_usd": round(b["cost_est_usd"], 6)}
for m, b in self._by_model.items()
},
}