Files
aiagent/backend/app/services/execution_budget.py
renjianbo 0608161c82 feat: 完善企业场景多线路由与执行稳定性
补齐平台模板与场景 DSL、预算控制、执行看板和企业场景脚本,增强 Windows 启动/迁移与前端代理和聊天会话记忆,修复执行创建阶段 500 与异步链路排障体验。

Made-with: Cursor
2026-04-09 21:58:53 +08:00

46 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
执行预算:全局默认 + Agent.budget_config 合并,供 WorkflowEngine 与任务入口使用。
"""
from typing import Dict, Optional
from sqlalchemy.orm import Session
from app.core.config import settings
from app.models.agent import Agent
from app.models.execution import Execution
def merge_budget_for_execution(db: Session, execution: Optional[Execution]) -> Dict[str, int]:
"""
返回合并后的整数预算(至少为 1
keys: max_steps, max_llm_invocations, max_tool_calls
"""
out: Dict[str, int] = {
"max_steps": max(1, int(getattr(settings, "WORKFLOW_MAX_STEPS_PER_RUN", 2000) or 2000)),
"max_llm_invocations": max(
1, int(getattr(settings, "WORKFLOW_MAX_LLM_INVOCATIONS_PER_RUN", 200) or 200)
),
"max_tool_calls": max(
1, int(getattr(settings, "WORKFLOW_MAX_TOOL_CALLS_PER_RUN", 500) or 500)
),
}
if not execution or not execution.agent_id:
return out
ag = db.query(Agent).filter(Agent.id == execution.agent_id).first()
if not ag or not isinstance(ag.budget_config, dict):
return out
bc = ag.budget_config
mapping = {
"max_steps": "max_steps",
"max_llm_invocations": "max_llm_invocations",
"max_tool_calls": "max_tool_calls",
}
for json_key, out_key in mapping.items():
if json_key not in bc or bc[json_key] is None:
continue
try:
out[out_key] = max(1, int(bc[json_key]))
except (TypeError, ValueError):
continue
return out