mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-17 21:51:59 +00:00
62aac187d8
MCP / 自定义工具的审批三档(tools/trust.py) - 平台看不出这两类工具做什么,以前审批关卡一律当它们安全、照跑不误。现在默认每次调用都等审批; 工具页可以逐个改成「始终允许 · 门控把关」或「始终允许」,审批卡上的「始终允许」等于前者 - 门控把关:每次调用先由门控模型审一遍工具说明、参数和节点任务;判可疑、出错、超时、答不成格式, 一律交还给人,不会变成放行。判定包进 checkpoint,审批恢复重放时不再问一遍 - agent 节点、工具节点、协作团队三处关卡都接上;团队成员停不下来等人,门控没放行的调用不执行 - 每次运行在发起时快照档位(runs.tool_trust),恢复和接着跑沿用同一份;正式运行不看档位,一律等审批; 升级前发起的运行照旧不问——它们可能正停在某个审批上,多问一次答复就会对错号 - 接口:GET /api/tools 带 trust / trust_key,PUT /api/tools/trust;设置新增门控模型;新事件 tool.gated - 界面:工具页三选一、审批卡「始终允许」、设置页门控模型、运行记录解码 tool.gated; 顺手修了 MCP 工具详情 404、工具审批被驳回却显示成放行 智能体护栏(engine/guards.py),取代固定的「最多 12 步」 - 步数只留兜底:默认 100,设置里可改;以前画布自动写进节点的 12 按「没填」处理 - 同一个工具、同样的参数第二次不再执行,把上次的结果交还给它;连续 3 步拿不到新信息就收尾 - 每个节点的令牌预算(默认 200 万)和金额预算(默认不限),都可以在设置里改成不限 - 上下文用到窗口的 60% 先压缩早期的工具结果,到 85% 就收尾;临近任何一个上限先提醒模型一次 - 下游结构化校验失败时写明是上游哪个 agent 提前收了尾;复核信号带上收尾原因, 问数据页只在真是步数用满时才给「放宽步数重跑」 - 上限在发起时快照(runs.agent_limits);升级前发起的运行一字不改走旧逻辑 检查脚本 - check-all 默认 4 道并行:量时间最多的两项互不重叠;并行时没过的最后单独重跑一遍,重跑才过的 单列成「偶发」。16 项全量从约 21 分钟降到约 7 分钟(加上一次重跑约 10 分钟);--lanes 1 回到串行 顺带修的 - 设置接口里的 limits 改为只读:库里存着旧版设置页写进去的一份(max_agent_steps: 25),从不生效, 却盖住了真实上限,问数据页照着错的数许诺 - test_kb 里嵌入模型那条测试不再真连 api.openai.com(代理慢时一条要等近 4 分钟) - check-stream 读滚动容器前先等它挂上,并行跑时不再偶发 验证(沙箱):tsc 干净,生产构建通过,pytest 1655 passed(两遍),check-all 16 项检查、3562 项全过; 新增后端测试 53 条,关键逻辑做了 22 处变异验证、全部抓到。
241 lines
8.8 KiB
Python
241 lines
8.8 KiB
Python
from __future__ import annotations
|
||
|
||
from typing import Any
|
||
|
||
# --------------------------------------------------------------------------
|
||
# 模型目录
|
||
#
|
||
# pricing 单位是「美元 / 百万 token」,用于 run 的成本估算。
|
||
# 新模型的价格会变,这份表只是开箱即用的默认值,用户可以在设置页改。
|
||
# --------------------------------------------------------------------------
|
||
|
||
ANTHROPIC_MODELS: list[dict[str, Any]] = [
|
||
{"id": "claude-opus-5", "label": "Claude Opus 5", "context": 1_000_000,
|
||
"pricing": {"input": 5.0, "output": 25.0}, "thinking": "adaptive"},
|
||
{"id": "claude-fable-5-1", "label": "Claude Fable 5.1", "context": 1_000_000,
|
||
"pricing": {"input": 10.0, "output": 50.0}, "thinking": "always"},
|
||
{"id": "claude-opus-4-8", "label": "Claude Opus 4.8", "context": 1_000_000,
|
||
"pricing": {"input": 5.0, "output": 25.0}, "thinking": "adaptive"},
|
||
{"id": "claude-sonnet-5", "label": "Claude Sonnet 5", "context": 1_000_000,
|
||
"pricing": {"input": 2.0, "output": 10.0}, "thinking": "adaptive"},
|
||
{"id": "claude-haiku-4-5", "label": "Claude Haiku 4.5", "context": 200_000,
|
||
"pricing": {"input": 1.0, "output": 5.0}, "thinking": "budget"},
|
||
]
|
||
|
||
OPENAI_MODELS: list[dict[str, Any]] = [
|
||
{"id": "gpt-5.2", "label": "GPT-5.2", "context": 400_000,
|
||
"pricing": {"input": 1.25, "output": 10.0}},
|
||
{"id": "gpt-5.2-mini", "label": "GPT-5.2 mini", "context": 400_000,
|
||
"pricing": {"input": 0.25, "output": 2.0}},
|
||
{"id": "o4", "label": "o4", "context": 200_000,
|
||
"pricing": {"input": 3.0, "output": 12.0}},
|
||
]
|
||
|
||
# 常见的 OpenAI 兼容服务,一键添加用
|
||
COMPATIBLE_PRESETS: list[dict[str, Any]] = [
|
||
{
|
||
"key": "deepseek",
|
||
"label": "DeepSeek",
|
||
"base_url": "https://api.deepseek.com/v1",
|
||
"models": [
|
||
{"id": "deepseek-chat", "label": "DeepSeek Chat", "context": 128_000,
|
||
"pricing": {"input": 0.27, "output": 1.1}},
|
||
{"id": "deepseek-reasoner", "label": "DeepSeek Reasoner", "context": 128_000,
|
||
"pricing": {"input": 0.55, "output": 2.19}},
|
||
],
|
||
},
|
||
{
|
||
"key": "moonshot",
|
||
"label": "Moonshot / Kimi",
|
||
"base_url": "https://api.moonshot.cn/v1",
|
||
"models": [
|
||
{"id": "kimi-k2-turbo-preview", "label": "Kimi K2 Turbo", "context": 256_000,
|
||
"pricing": {"input": 0.6, "output": 2.5}},
|
||
{"id": "moonshot-v1-128k", "label": "Moonshot v1 128k", "context": 128_000,
|
||
"pricing": {"input": 0.8, "output": 3.0}},
|
||
],
|
||
},
|
||
{
|
||
"key": "dashscope",
|
||
"label": "阿里云百炼 / 通义千问",
|
||
"base_url": "https://dashscope.aliyuncs.com/compatible-mode/v1",
|
||
"models": [
|
||
{"id": "qwen-max", "label": "Qwen Max", "context": 32_000,
|
||
"pricing": {"input": 0.34, "output": 1.37}},
|
||
{"id": "qwen-plus", "label": "Qwen Plus", "context": 131_000,
|
||
"pricing": {"input": 0.11, "output": 0.28}},
|
||
],
|
||
},
|
||
{
|
||
"key": "zhipu",
|
||
"label": "智谱 GLM",
|
||
"base_url": "https://open.bigmodel.cn/api/paas/v4",
|
||
"models": [
|
||
{"id": "glm-4.6", "label": "GLM-4.6", "context": 200_000,
|
||
"pricing": {"input": 0.6, "output": 2.2}},
|
||
],
|
||
},
|
||
{
|
||
"key": "siliconflow",
|
||
"label": "硅基流动",
|
||
"base_url": "https://api.siliconflow.cn/v1",
|
||
"models": [
|
||
{"id": "deepseek-ai/DeepSeek-V3", "label": "DeepSeek V3", "context": 128_000,
|
||
"pricing": {"input": 0.27, "output": 1.1}},
|
||
],
|
||
},
|
||
{
|
||
"key": "openrouter",
|
||
"label": "OpenRouter",
|
||
"base_url": "https://openrouter.ai/api/v1",
|
||
"models": [
|
||
{"id": "anthropic/claude-opus-5", "label": "Claude Opus 5", "context": 1_000_000,
|
||
"pricing": {"input": 5.0, "output": 25.0}},
|
||
],
|
||
},
|
||
{
|
||
"key": "ollama",
|
||
"label": "Ollama(本地)",
|
||
"base_url": "http://localhost:11434/v1",
|
||
"api_key_optional": True,
|
||
"models": [
|
||
{"id": "qwen3:8b", "label": "Qwen3 8B (本地)", "context": 32_000,
|
||
"pricing": {"input": 0.0, "output": 0.0}},
|
||
{"id": "llama3.2", "label": "Llama 3.2 (本地)", "context": 128_000,
|
||
"pricing": {"input": 0.0, "output": 0.0}},
|
||
],
|
||
},
|
||
]
|
||
|
||
MOCK_MODELS: list[dict[str, Any]] = [
|
||
{"id": "mock-fast", "label": "Mock(不花钱,用来试编排)", "context": 128_000,
|
||
"pricing": {"input": 0.0, "output": 0.0}},
|
||
]
|
||
|
||
PROVIDER_KINDS = [
|
||
{
|
||
"kind": "anthropic",
|
||
"label": "Anthropic",
|
||
"base_url_hint": "留空走官方;填了就是自定义网关(读 ANTHROPIC_BASE_URL)",
|
||
"models": ANTHROPIC_MODELS,
|
||
"default_model": "claude-opus-5",
|
||
},
|
||
{
|
||
"kind": "openai",
|
||
"label": "OpenAI",
|
||
"base_url_hint": "留空走官方",
|
||
"models": OPENAI_MODELS,
|
||
"default_model": "gpt-5.2",
|
||
},
|
||
{
|
||
"kind": "openai_compatible",
|
||
"label": "OpenAI 兼容(DeepSeek / Kimi / 通义 / Ollama…)",
|
||
"base_url_hint": "必填,例如 https://api.deepseek.com/v1",
|
||
"models": [],
|
||
"presets": COMPATIBLE_PRESETS,
|
||
"default_model": "",
|
||
},
|
||
{
|
||
"kind": "mock",
|
||
"label": "Mock(无需 API Key)",
|
||
"base_url_hint": "",
|
||
"models": MOCK_MODELS,
|
||
"default_model": "mock-fast",
|
||
},
|
||
]
|
||
|
||
|
||
# --------------------------------------------------------------------------
|
||
# 模型能力
|
||
# --------------------------------------------------------------------------
|
||
|
||
# Claude 4.6 及以后的模型移除了采样参数,传 temperature/top_p 会直接 400。
|
||
_NO_SAMPLING_PREFIXES = (
|
||
"claude-fable-5",
|
||
"claude-mythos-5",
|
||
"claude-opus-5",
|
||
"claude-opus-4-8",
|
||
"claude-opus-4-7",
|
||
"claude-opus-4-6",
|
||
"claude-sonnet-5",
|
||
"claude-sonnet-4-6",
|
||
)
|
||
|
||
# 支持 adaptive thinking(而不是老的 budget_tokens)的模型
|
||
_ADAPTIVE_THINKING_PREFIXES = _NO_SAMPLING_PREFIXES
|
||
|
||
# 支持 output_config.effort
|
||
_EFFORT_PREFIXES = _NO_SAMPLING_PREFIXES + ("claude-opus-4-5",)
|
||
|
||
# 交错思考(工具结果回来之后再想一段,而不是一轮开头想完就只剩执行)。
|
||
#
|
||
# 4.6 及以后走 adaptive thinking,交错是自带的,不需要也不该再挂 beta header
|
||
# ——在 Opus 4.6 上手动模式压根没有交错思考,只有 adaptive 有。上面那张
|
||
# _ADAPTIVE_THINKING_PREFIXES 覆盖的都属于这一档。
|
||
#
|
||
# 需要显式开的是 Claude 4 / 4.5 这一代。目录里没有列它们,但模型名是放行的
|
||
# (用户可以手填),所以这条路径要留着。Haiku 4.5 不支持,前缀也就不包含它。
|
||
_INTERLEAVED_BETA_PREFIXES = ("claude-opus-4", "claude-sonnet-4")
|
||
INTERLEAVED_BETA = "interleaved-thinking-2025-05-14"
|
||
|
||
|
||
def needs_interleaved_beta(model: str) -> bool:
|
||
"""这个模型要不要靠 beta header 才会在工具之间思考。"""
|
||
name = _norm(model)
|
||
if supports_adaptive_thinking(name):
|
||
return False # adaptive 自带,多挂一个 header 只会是噪音
|
||
return name.startswith(_INTERLEAVED_BETA_PREFIXES)
|
||
|
||
|
||
def _norm(model: str) -> str:
|
||
"""去掉 openrouter 之类的前缀,只看模型名本身。"""
|
||
return (model or "").split("/")[-1].strip()
|
||
|
||
|
||
def supports_sampling(model: str) -> bool:
|
||
"""这个模型还能不能传 temperature / top_p。"""
|
||
return not _norm(model).startswith(_NO_SAMPLING_PREFIXES)
|
||
|
||
|
||
def supports_adaptive_thinking(model: str) -> bool:
|
||
return _norm(model).startswith(_ADAPTIVE_THINKING_PREFIXES)
|
||
|
||
|
||
def supports_effort(model: str) -> bool:
|
||
return _norm(model).startswith(_EFFORT_PREFIXES)
|
||
|
||
|
||
def find_pricing(model: str) -> dict[str, float] | None:
|
||
target = _norm(model)
|
||
pools = [ANTHROPIC_MODELS, OPENAI_MODELS, MOCK_MODELS]
|
||
pools += [p["models"] for p in COMPATIBLE_PRESETS]
|
||
for pool in pools:
|
||
for entry in pool:
|
||
if _norm(entry["id"]) == target:
|
||
return entry.get("pricing")
|
||
return None
|
||
|
||
|
||
def find_context(model: str) -> int | None:
|
||
"""目录里登记的上下文窗口(token)。没登记的返回 None,由调用方取保守值。"""
|
||
target = _norm(model)
|
||
pools = [ANTHROPIC_MODELS, OPENAI_MODELS, MOCK_MODELS]
|
||
pools += [p["models"] for p in COMPATIBLE_PRESETS]
|
||
for pool in pools:
|
||
for entry in pool:
|
||
if _norm(entry["id"]) == target:
|
||
value = entry.get("context")
|
||
return int(value) if isinstance(value, (int, float)) and value > 0 else None
|
||
return None
|
||
|
||
|
||
def estimate_cost(model: str, input_tokens: int, output_tokens: int) -> float:
|
||
"""按目录价估算一次调用的花费(美元)。"""
|
||
pricing = find_pricing(model)
|
||
if not pricing:
|
||
return 0.0
|
||
return (
|
||
input_tokens / 1_000_000 * pricing.get("input", 0.0)
|
||
+ output_tokens / 1_000_000 * pricing.get("output", 0.0)
|
||
)
|