Files
DeepTutor/deeptutor/services/config/capabilities_settings.py
wojiecihuo 651a616b02 fix(visualize): raise the budget to 32k, cap it to the model, and retry without thinking (#3019)
* fix(book): retry an interactive page with thinking off when it runs out of room

deepseek-flash thinks by default and can spend the whole 16000-token
visualize budget on reasoning, so a book interactive block came back with
no code. When the page is empty or cut off before </html>, ask once more
with thinking turned off (thinking_off_effort_for) before failing. If that
still runs out of room, the error names the budget sent to the model and
the settings that change it.

The shipped default stays at 16000: the budget is shared by every visualize
caller, and 32768 breaks models with a lower output cap (#3018).

Refs #3011

* fix(llm): raise the visualize budget to 32k and cap each request to the model

A thinking model can spend 16k tokens before it writes any code, so the
visualize default is now 32768. Sent unchanged that would be rejected by
models whose output stops lower (gpt-4o and gpt-4o-mini at 16384) and could
overflow a small local context window, so every request now carries
min(budget, the model's known output limit, half the configured context
window). The cap lives in get_token_limit_kwargs, so it applies to every
caller; BaseAgent and the agentic loop pass the configured window.
2026-11-27 22:26:59 +08:00

527 lines
21 KiB
Python

"""Read/write the per-capability tunables surfaced by the Settings UI.
This is the source of truth for the ``/api/capabilities/settings``
endpoint. It bridges two on-disk files:
* ``data/user/settings/agents.yaml`` — per-capability LLM params
(``temperature``, stage ``max_tokens``). Owned by
:func:`get_chat_params` / :func:`get_agent_params` in
:mod:`deeptutor.services.config.loader`.
* ``data/user/settings/main.yaml`` — per-capability runtime knobs that
aren't LLM params (research's ``researching.*`` and question's
``exploring.*`` subtrees).
The schema we expose to the UI is a single dict so the frontend can
render one form. Saving splits the payload back into the right files.
We deliberately do not include capabilities whose pipelines do not
actually read the corresponding YAML keys today — surfacing knobs that
don't do anything would be misleading. As we lift more hardcoded
constants into settings, capabilities can be added here.
"""
from __future__ import annotations
import copy
from pathlib import Path
from typing import Any
import yaml
from deeptutor.services.config.loader import (
DEFAULT_CHAT_PARAMS,
PROJECT_ROOT,
get_runtime_settings_dir,
)
from deeptutor.utils.config_manager import ConfigManager
# ── Schema definition ────────────────────────────────────────────────────
# The keys here drive both the GET response shape and the PUT validation.
# Each capability lists its (file, sub-path) reads so we know how to
# round-trip values without disturbing unrelated YAML keys.
_AGENTS_YAML_CAPABILITY_SECTIONS: dict[str, tuple[str, ...]] = {
"solve": ("capabilities", "solve"),
"research": ("capabilities", "research"),
"question": ("capabilities", "question"),
"visualize": ("capabilities", "visualize"),
"co_writer": ("capabilities", "co_writer"),
"vision_solver": ("plugins", "vision_solver"),
"math_animator": ("plugins", "math_animator"),
}
_SIMPLE_LLM_DEFAULTS: dict[str, dict[str, Any]] = {
"solve": {"temperature": 0.3, "max_tokens": 8192},
"research": {"temperature": 0.5, "max_tokens": 16834},
"question": {"temperature": 0.7, "max_tokens": 4096},
# The visualize loop's budget is configurable because a reasoning model can
# spend most of it thinking (#1546). 32k leaves room for that; each request
# is still capped to the model's output limit (services/llm/output_limits).
"visualize": {"temperature": 0.15, "max_tokens": 32768},
"co_writer": {"temperature": 0.6, "max_tokens": 4096},
"vision_solver": {"temperature": 0.3, "max_tokens": 12000},
"math_animator": {"temperature": 0.2, "max_tokens": 16834},
}
# main.yaml subtrees that capabilities read at runtime (besides LLM params).
_MAIN_YAML_RUNTIME_DEFAULTS: dict[str, dict[str, Any]] = {
"solve": {
# Total LLM-round budget for one solve turn (plan + tool + finish all
# count as rounds in the flat agent loop). Higher than chat's default
# (each plan step costs several rounds) but kept moderate so a churning
# turn finishes naturally instead of running long enough to hit an LLM
# timeout — raise it in settings if you want deeper solving.
"max_rounds": 12,
"max_replans": 2,
},
"research": {
"researching": {
"note_agent_mode": "auto",
# Must match ``ResearchPipeline``'s own defaults. This table is the
# fallback used when a settings payload is written, so a user who
# merely opens research settings and saves would otherwise persist
# 60/3 and permanently shadow the pipeline's 240/0 — the same shape
# of bug as #1316, where a layer below the config silently won.
"tool_timeout": 240,
"tool_max_retries": 0,
"paper_search_years_limit": 5,
},
},
"question": {
"exploring": {
"max_iterations": 7,
"tool_summarizer": {
"enabled": True,
"max_tokens": 1024,
},
},
},
"math_animator": {
# Runtime fallback for one code-repair call in the render-fix loop. It is
# deliberately omitted from the Settings DTO until explicitly configured,
# because the page PUTs that DTO back verbatim; injecting the default here
# would persist 180 on an unrelated save and shadow the loop's own default.
"repair_timeout": 180,
},
}
# ── Helpers ──────────────────────────────────────────────────────────────
def _agents_yaml_path() -> Path:
return get_runtime_settings_dir(PROJECT_ROOT) / "agents.yaml"
def _read_agents_yaml() -> dict[str, Any]:
path = _agents_yaml_path()
if not path.exists():
return {}
with open(path, encoding="utf-8") as f:
return yaml.safe_load(f) or {}
def _write_agents_yaml(data: dict[str, Any]) -> None:
path = _agents_yaml_path()
path.parent.mkdir(parents=True, exist_ok=True)
with open(path, "w", encoding="utf-8") as f:
yaml.safe_dump(data, f, default_flow_style=False, allow_unicode=True, sort_keys=False)
def _get_at(d: dict[str, Any], path: tuple[str, ...]) -> dict[str, Any]:
"""Walk a nested dict by path, returning {} if any segment is missing."""
node: Any = d
for key in path:
if not isinstance(node, dict):
return {}
node = node.get(key, {})
return node if isinstance(node, dict) else {}
def _set_at(d: dict[str, Any], path: tuple[str, ...], value: dict[str, Any]) -> None:
"""Insert ``value`` at ``path`` in ``d``, creating intermediate dicts."""
node = d
for key in path[:-1]:
nxt = node.get(key)
if not isinstance(nxt, dict):
nxt = {}
node[key] = nxt
node = nxt
node[path[-1]] = value
def _deep_merge(into: dict[str, Any], src: dict[str, Any]) -> dict[str, Any]:
"""Merge ``src`` into ``into`` recursively (keys in src win)."""
for key, value in src.items():
if isinstance(value, dict) and isinstance(into.get(key), dict):
_deep_merge(into[key], value)
else:
into[key] = value
return into
def _coerce_float(raw: Any, default: float, *, lo: float = 0.0, hi: float = 2.0) -> float:
try:
value = float(raw)
except (TypeError, ValueError):
return default
return max(lo, min(hi, value))
def _coerce_int(raw: Any, default: int, *, lo: int = 1, hi: int = 200_000) -> int:
try:
value = int(raw)
except (TypeError, ValueError):
return default
return max(lo, min(hi, value))
def _coerce_bool(raw: Any, default: bool) -> bool:
if isinstance(raw, bool):
return raw
if isinstance(raw, str):
if raw.lower() in {"true", "1", "yes", "on"}:
return True
if raw.lower() in {"false", "0", "no", "off"}:
return False
return default
# ── Schema build / read ──────────────────────────────────────────────────
# Only the chat sub-sections actually read by ``AgenticChatPipeline.__init__``.
_CHAT_STAGES_IN_USE: tuple[str, ...] = (
"exploring",
"responding",
)
# Targeting-era chat keys no longer read by the pipeline; dropped on write.
_CHAT_LEGACY_KEYS: tuple[str, ...] = (
"max_iterations",
"max_explore_rounds",
"max_act_rounds",
"max_tool_steps",
"targeting",
"explore",
"act",
)
def _build_chat_block(agents_cfg: dict[str, Any]) -> dict[str, Any]:
"""Read agents.yaml.capabilities.chat into the UI schema with defaults."""
chat_cfg: dict[str, Any] = _get_at(agents_cfg, ("capabilities", "chat"))
# Deep-copy the shipped defaults: ``_deep_merge`` mutates its first argument
# in place, so seeding from the module-level ``DEFAULT_CHAT_PARAMS`` would
# alias (and then overwrite) its nested stage dicts with the user's values.
merged: dict[str, Any] = copy.deepcopy(DEFAULT_CHAT_PARAMS)
_deep_merge(merged, chat_cfg)
return {
"temperature": _coerce_float(merged.get("temperature"), DEFAULT_CHAT_PARAMS["temperature"]),
"max_rounds": _coerce_int(
merged.get("max_rounds"), DEFAULT_CHAT_PARAMS["max_rounds"], lo=1, hi=50
),
"stage_budgets": {
stage: _coerce_int(
(merged.get(stage) or {}).get("max_tokens"),
DEFAULT_CHAT_PARAMS[stage]["max_tokens"],
lo=1,
hi=200_000,
)
for stage in _CHAT_STAGES_IN_USE
},
}
def _build_simple_llm_block(agents_cfg: dict[str, Any], capability: str) -> dict[str, Any]:
defaults = _SIMPLE_LLM_DEFAULTS[capability]
section = _get_at(agents_cfg, _AGENTS_YAML_CAPABILITY_SECTIONS[capability])
return {
"temperature": _coerce_float(section.get("temperature"), defaults["temperature"]),
"max_tokens": _coerce_int(section.get("max_tokens"), defaults["max_tokens"]),
}
def _build_main_runtime_block(main_cfg: dict[str, Any], capability: str) -> dict[str, Any]:
defaults = _MAIN_YAML_RUNTIME_DEFAULTS.get(capability)
if defaults is None:
return {}
if capability == "solve":
solve_cfg = _get_at(main_cfg, ("capabilities", "solve"))
# The pre-flat-loop ``max_iterations_per_step`` key was inert, so a stale
# value is intentionally ignored — only the new ``max_rounds`` counts,
# otherwise everyone would silently inherit the old (too-low) number.
return {
"max_rounds": _coerce_int(
solve_cfg.get("max_rounds"),
defaults["max_rounds"],
lo=1,
hi=50,
),
"max_replans": _coerce_int(
solve_cfg.get("max_replans"),
defaults["max_replans"],
lo=0,
hi=10,
),
}
if capability == "research":
researching_cfg = _get_at(main_cfg, ("capabilities", "research", "researching"))
d = defaults["researching"]
return {
"researching": {
"note_agent_mode": str(
researching_cfg.get("note_agent_mode") or d["note_agent_mode"]
),
"tool_timeout": _coerce_int(
researching_cfg.get("tool_timeout"), d["tool_timeout"], lo=1, hi=600
),
"tool_max_retries": _coerce_int(
researching_cfg.get("tool_max_retries"), d["tool_max_retries"], lo=0, hi=10
),
"paper_search_years_limit": _coerce_int(
researching_cfg.get("paper_search_years_limit"),
d["paper_search_years_limit"],
lo=1,
hi=50,
),
},
}
if capability == "question":
exploring_cfg = _get_at(main_cfg, ("capabilities", "question", "exploring"))
d = defaults["exploring"]
summarizer_cfg = (
exploring_cfg.get("tool_summarizer")
if isinstance(exploring_cfg.get("tool_summarizer"), dict)
else {}
)
return {
"exploring": {
"max_iterations": _coerce_int(
exploring_cfg.get("max_iterations"), d["max_iterations"], lo=1, hi=50
),
"tool_summarizer": {
"enabled": _coerce_bool(
summarizer_cfg.get("enabled"), d["tool_summarizer"]["enabled"]
),
"max_tokens": _coerce_int(
summarizer_cfg.get("max_tokens"), d["tool_summarizer"]["max_tokens"]
),
},
},
}
if capability == "math_animator":
animator_cfg = _get_at(main_cfg, ("capabilities", "math_animator"))
return {
"repair_timeout": _coerce_int(
animator_cfg.get("repair_timeout"),
defaults["repair_timeout"],
lo=1,
hi=3600,
),
}
return {}
def capabilities_settings_dict() -> dict[str, Any]:
"""Return the full schema as a JSON-safe dict (defaults merged in)."""
agents_cfg = _read_agents_yaml()
main_cfg = ConfigManager().load_config()
result: dict[str, Any] = {"chat": _build_chat_block(agents_cfg)}
for cap in _AGENTS_YAML_CAPABILITY_SECTIONS:
block = _build_simple_llm_block(agents_cfg, cap)
block.update(_build_main_runtime_block(main_cfg, cap))
if cap == "math_animator":
animator_cfg = _get_at(main_cfg, ("capabilities", "math_animator"))
if not isinstance(animator_cfg, dict) or "repair_timeout" not in animator_cfg:
block.pop("repair_timeout", None)
result[cap] = block
return result
# ── Write path ───────────────────────────────────────────────────────────
def _apply_chat_into_agents_yaml(agents_cfg: dict[str, Any], block: dict[str, Any]) -> None:
current = _get_at(agents_cfg, ("capabilities", "chat"))
new_chat: dict[str, Any] = dict(current) if isinstance(current, dict) else {}
new_chat.pop("answer_now", None)
for legacy_key in _CHAT_LEGACY_KEYS:
new_chat.pop(legacy_key, None)
if "temperature" in block:
new_chat["temperature"] = _coerce_float(
block.get("temperature"), DEFAULT_CHAT_PARAMS["temperature"]
)
if "max_rounds" in block:
new_chat["max_rounds"] = _coerce_int(
block.get("max_rounds"), DEFAULT_CHAT_PARAMS["max_rounds"], lo=1, hi=50
)
stage_budgets = block.get("stage_budgets") or {}
if isinstance(stage_budgets, dict):
for stage, default_sub in DEFAULT_CHAT_PARAMS.items():
if not isinstance(default_sub, dict):
continue
if stage in stage_budgets:
existing = new_chat.get(stage) if isinstance(new_chat.get(stage), dict) else {}
existing = dict(existing)
existing["max_tokens"] = _coerce_int(
stage_budgets[stage], default_sub["max_tokens"], lo=1, hi=200_000
)
new_chat[stage] = existing
_set_at(agents_cfg, ("capabilities", "chat"), new_chat)
def _apply_simple_llm_into_agents_yaml(
agents_cfg: dict[str, Any], capability: str, block: dict[str, Any]
) -> None:
defaults = _SIMPLE_LLM_DEFAULTS[capability]
section_path = _AGENTS_YAML_CAPABILITY_SECTIONS[capability]
current = _get_at(agents_cfg, section_path)
new_section: dict[str, Any] = dict(current) if isinstance(current, dict) else {}
if "temperature" in block:
new_section["temperature"] = _coerce_float(
block.get("temperature"), defaults["temperature"]
)
if "max_tokens" in block:
new_section["max_tokens"] = _coerce_int(block.get("max_tokens"), defaults["max_tokens"])
_set_at(agents_cfg, section_path, new_section)
def _apply_main_runtime(
main_payload: dict[str, Any], capability: str, block: dict[str, Any]
) -> None:
defaults = _MAIN_YAML_RUNTIME_DEFAULTS.get(capability)
if defaults is None:
return
if capability == "solve":
solve_section: dict[str, Any] = {}
if "max_rounds" in block:
solve_section["max_rounds"] = _coerce_int(
block.get("max_rounds"),
defaults["max_rounds"],
lo=1,
hi=50,
)
if "max_replans" in block:
solve_section["max_replans"] = _coerce_int(
block.get("max_replans"),
defaults["max_replans"],
lo=0,
hi=10,
)
if solve_section:
main_payload.setdefault("capabilities", {})["solve"] = solve_section
if capability == "research" and isinstance(block.get("researching"), dict):
d = defaults["researching"]
r = block["researching"]
main_payload.setdefault("capabilities", {}).setdefault("research", {})["researching"] = {
"note_agent_mode": str(r.get("note_agent_mode") or d["note_agent_mode"]),
"tool_timeout": _coerce_int(r.get("tool_timeout"), d["tool_timeout"], lo=1, hi=600),
"tool_max_retries": _coerce_int(
r.get("tool_max_retries"), d["tool_max_retries"], lo=0, hi=10
),
"paper_search_years_limit": _coerce_int(
r.get("paper_search_years_limit"), d["paper_search_years_limit"], lo=1, hi=50
),
}
if capability == "question" and isinstance(block.get("exploring"), dict):
d = defaults["exploring"]
e = block["exploring"]
sm = e.get("tool_summarizer") if isinstance(e.get("tool_summarizer"), dict) else {}
main_payload.setdefault("capabilities", {}).setdefault("question", {})["exploring"] = {
"max_iterations": _coerce_int(
e.get("max_iterations"), d["max_iterations"], lo=1, hi=50
),
"tool_summarizer": {
"enabled": _coerce_bool(sm.get("enabled"), d["tool_summarizer"]["enabled"]),
"max_tokens": _coerce_int(sm.get("max_tokens"), d["tool_summarizer"]["max_tokens"]),
},
}
if capability == "math_animator" and "repair_timeout" in block:
main_payload.setdefault("capabilities", {})["math_animator"] = {
"repair_timeout": _coerce_int(
block.get("repair_timeout"),
defaults["repair_timeout"],
lo=1,
hi=3600,
),
}
def save_capabilities_settings(payload: dict[str, Any]) -> dict[str, Any]:
"""Merge ``payload`` into both YAML files and return the new state.
Unknown keys are dropped; values are coerced + clamped via the helpers
above so the YAML cannot pick up junk.
"""
agents_cfg = _read_agents_yaml()
main_payload: dict[str, Any] = {}
if isinstance(payload.get("chat"), dict):
_apply_chat_into_agents_yaml(agents_cfg, payload["chat"])
for cap in _AGENTS_YAML_CAPABILITY_SECTIONS:
block = payload.get(cap)
if not isinstance(block, dict):
continue
_apply_simple_llm_into_agents_yaml(agents_cfg, cap, block)
_apply_main_runtime(main_payload, cap, block)
_write_agents_yaml(agents_cfg)
if main_payload:
ConfigManager().save_config(main_payload)
return capabilities_settings_dict()
def get_visualize_params() -> dict[str, Any]:
"""Runtime visualize params, read through the same coerce path as the UI.
The budget used to be a literal in the capability, so ``agents.yaml`` and
the settings page could not move it and a reasoning model that spent most
of the budget on chain-of-thought truncated the payload with no recourse
(#1546).
"""
return _build_simple_llm_block(_read_agents_yaml(), "visualize")
def get_solve_params() -> dict[str, Any]:
"""Runtime solve params, read through the same coerce path as the UI.
Combines the two storage locations the solve settings page writes to:
``temperature`` / ``max_tokens`` (agents.yaml) and ``max_rounds`` /
``max_replans`` (main config). This is the single source the deep-solve
capability forwards into the chat agent loop, so the settings page actually
drives the loop instead of being inert.
"""
agents_cfg = _read_agents_yaml()
main_cfg = ConfigManager().load_config()
llm = _build_simple_llm_block(agents_cfg, "solve")
runtime = _build_main_runtime_block(main_cfg, "solve")
return {**llm, **runtime}
def get_math_animator_params() -> dict[str, Any]:
"""Runtime math-animator knobs, read through the same coerce path as the UI.
Today this is the code-repair overall deadline (#2767): ``run_render`` used to
build ``CodeRetryManager`` with a hardcoded 180s timeout that no setting could
move, so a long reasoning model repairing a book animation was cancelled
mid-edit with no recourse. The pipeline reads this once per render, so an
in-flight render keeps the value it started with. The agents read their LLM
params (``temperature`` / ``max_tokens``) through their own loader seam, so
this getter returns only the runtime knob the render loop reads here.
"""
main_cfg = ConfigManager().load_config()
return _build_main_runtime_block(main_cfg, "math_animator")
__all__ = [
"capabilities_settings_dict",
"get_math_animator_params",
"get_solve_params",
"get_visualize_params",
"save_capabilities_settings",
]