- run_dual 编排:规划者两阶段 JSON(plan/review,失败回喂重试一次再降级),
redo 时裁决意见回喂执行者,交接上限 agent.max_handoffs(默认 2)
- 交接文档 agent_runs/{id}/handoff.json(智能体版交流文本:instructions/acceptance/exchanges)
- /agent 新增 executor_pool_id;规划者==执行者条目拒绝;整体 token_cap 覆盖两级调用
- ToolLoop 增 emit_final 开关(内层循环不发终态,防前端 SSE 提前收口)
- 前端:执行者选择器 + phase/message 事件渲染(阶段徽标 + 双色消息卡)
- 测试 +3(done/redo/执行者故障),全量 277 passed
- fix(tests): test_config_get_put_reset 增加设置备份/恢复隔离,防止清掉用户真实配置
125 lines
4.6 KiB
Python
125 lines
4.6 KiB
Python
"""可调整的运行设置(SettingsStore)—— 让用户自定义内置小模型 / 大模型 / 管线。
|
||
|
||
用户可在 Web 界面"模型设置"里调整并持久化到 config/settings.json(gitignore),
|
||
重启后保留。调整会触发 v2 管线重建(gateway.api.build_v2_pipeline 重新读取)。
|
||
|
||
默认值与 config/config.yaml 的 v2 段一致;settings.json 只存用户改动覆盖项。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import json
|
||
from pathlib import Path
|
||
from typing import Any, Dict, Optional
|
||
|
||
_SETTINGS_PATH = Path(__file__).resolve().parent.parent / "config" / "settings.json"
|
||
|
||
# 可调整项(含默认值);用户改动存这里
|
||
DEFAULTS: Dict[str, Any] = {
|
||
"worker": {
|
||
"backend": "llama_server", # mock | openai/api | llama_server
|
||
"model": "models/qwen3.5-4b-q4_k_m.gguf",
|
||
"base_url": "", # openai 后端填 http://127.0.0.1:11434/v1 等
|
||
"port": 8901, # llama_server 端口
|
||
"temperature": 0.3,
|
||
"max_fix_attempts": 2,
|
||
"per_step_timeout_s": 15,
|
||
"code_timeout_s": 10,
|
||
},
|
||
"architect": {
|
||
"model": "deepseek-v4-flash",
|
||
"base_url": "https://api.deepseek.com",
|
||
"api_key": "",
|
||
},
|
||
"pipeline": {
|
||
"fast_path": True,
|
||
"rounds_cap": 6,
|
||
"api_token_cap": 8000,
|
||
"breach_policy": "architect_do",
|
||
},
|
||
"agent": {
|
||
"workspace_dir": "agent_workspace", # 智能体工作区根目录(越界即拒)
|
||
"recent_workspaces": [], # 最近打开的工作区(供快速切换)
|
||
"max_rounds": 8, # 工具循环轮数上限
|
||
"token_cap": 20000, # 单次智能体任务 token 熔断
|
||
"allow_shell": False, # 允许 run_command 执行 shell(默认关)
|
||
"shell_timeout_s": 20, # shell 命令超时
|
||
"max_handoffs": 2, # 两级模式:规划者<->执行者交接轮数上限
|
||
},
|
||
}
|
||
|
||
|
||
class SettingsStore:
|
||
"""用户可调整设置(内存 + settings.json 持久化)。"""
|
||
|
||
def __init__(self, path: Optional[Path] = None):
|
||
self._path = Path(path) if path else _SETTINGS_PATH
|
||
self._data: Dict[str, Any] = {}
|
||
self.load()
|
||
|
||
# ---------- 持久化 ----------
|
||
def load(self) -> None:
|
||
if self._path.exists():
|
||
try:
|
||
self._data = json.loads(self._path.read_text(encoding="utf-8"))
|
||
except Exception:
|
||
self._data = {}
|
||
else:
|
||
self._data = {}
|
||
|
||
def save(self) -> None:
|
||
self._path.parent.mkdir(parents=True, exist_ok=True)
|
||
self._path.write_text(
|
||
json.dumps(self._data, ensure_ascii=False, indent=2), encoding="utf-8")
|
||
|
||
# ---------- 访问 ----------
|
||
def to_dict(self) -> Dict[str, Any]:
|
||
"""返回 默认值 + 用户覆盖 合并后的完整设置。"""
|
||
merged: Dict[str, Any] = {}
|
||
for section, defaults in DEFAULTS.items():
|
||
ov = self._data.get(section, {})
|
||
merged[section] = {**defaults, **(ov if isinstance(ov, dict) else {})}
|
||
return merged
|
||
|
||
def get(self, section: str, key: str, default: Any = None) -> Any:
|
||
merged = self.to_dict()
|
||
return merged.get(section, {}).get(key, default)
|
||
|
||
def update(self, patch: Dict[str, Any]) -> Dict[str, Any]:
|
||
"""应用部分更新(可只传改动的 section/key)。返回合并后的完整设置。"""
|
||
for section, values in patch.items():
|
||
if section not in DEFAULTS or not isinstance(values, dict):
|
||
continue
|
||
cur = self._data.setdefault(section, {})
|
||
for k, v in values.items():
|
||
if k in DEFAULTS[section]:
|
||
cur[k] = _coerce(v, DEFAULTS[section][k])
|
||
self.save()
|
||
return self.to_dict()
|
||
|
||
def reset(self) -> Dict[str, Any]:
|
||
"""恢复默认。"""
|
||
self._data = {}
|
||
self.save()
|
||
return self.to_dict()
|
||
|
||
|
||
def _coerce(value: Any, template: Any) -> Any:
|
||
"""按默认值的类型把输入转成一致类型(数值容错)。"""
|
||
if isinstance(template, bool):
|
||
return bool(value)
|
||
if isinstance(template, int):
|
||
try:
|
||
return int(float(value))
|
||
except (TypeError, ValueError):
|
||
return template
|
||
if isinstance(template, float):
|
||
try:
|
||
return float(value)
|
||
except (TypeError, ValueError):
|
||
return template
|
||
return value
|
||
|
||
|
||
def load_settings(path: Optional[Path] = None) -> SettingsStore:
|
||
return SettingsStore(path)
|