|
| 1 | +"""V2 Settings router — agent settings managed via the web UI. |
| 2 | +
|
| 3 | +Reads/writes a flat AgentSettings shape persisted in |
| 4 | +.codeframe/config.yaml via load_environment_config / save_environment_config. |
| 5 | +
|
| 6 | +Routes: |
| 7 | + GET /api/v2/settings - Load agent settings (returns defaults if missing) |
| 8 | + PUT /api/v2/settings - Save agent settings (merges into existing config) |
| 9 | +""" |
| 10 | + |
| 11 | +import logging |
| 12 | + |
| 13 | +from fastapi import APIRouter, Depends, HTTPException, Request |
| 14 | + |
| 15 | +from codeframe.core.config import ( |
| 16 | + AgentBudgetConfig, |
| 17 | + EnvironmentConfig, |
| 18 | + load_environment_config, |
| 19 | + save_environment_config, |
| 20 | +) |
| 21 | +from codeframe.core.workspace import Workspace |
| 22 | +from codeframe.lib.rate_limiter import rate_limit_standard |
| 23 | +from codeframe.ui.dependencies import get_v2_workspace |
| 24 | +from codeframe.ui.models import ( |
| 25 | + AGENT_TYPES, |
| 26 | + AgentSettingsResponse, |
| 27 | + AgentTypeModelConfig, |
| 28 | + UpdateAgentSettingsRequest, |
| 29 | +) |
| 30 | +from codeframe.ui.response_models import ErrorCodes, api_error |
| 31 | + |
| 32 | +logger = logging.getLogger(__name__) |
| 33 | + |
| 34 | +router = APIRouter(prefix="/api/v2/settings", tags=["settings"]) |
| 35 | + |
| 36 | + |
| 37 | +def _config_to_response(config: EnvironmentConfig) -> AgentSettingsResponse: |
| 38 | + """Map an EnvironmentConfig to the flat AgentSettings response shape.""" |
| 39 | + saved_models = config.agent_type_models or {} |
| 40 | + agent_models = [ |
| 41 | + AgentTypeModelConfig( |
| 42 | + agent_type=agent_type, |
| 43 | + default_model=saved_models.get(agent_type, ""), |
| 44 | + ) |
| 45 | + for agent_type in AGENT_TYPES |
| 46 | + ] |
| 47 | + # Guard against legacy YAML where agent_budget may have been removed/nulled. |
| 48 | + budget = config.agent_budget or AgentBudgetConfig() |
| 49 | + return AgentSettingsResponse( |
| 50 | + agent_models=agent_models, |
| 51 | + max_turns=budget.max_iterations, |
| 52 | + max_cost_usd=config.max_cost_usd, |
| 53 | + ) |
| 54 | + |
| 55 | + |
| 56 | +@router.get("", response_model=AgentSettingsResponse) |
| 57 | +@rate_limit_standard() |
| 58 | +async def get_settings( |
| 59 | + request: Request, |
| 60 | + workspace: Workspace = Depends(get_v2_workspace), |
| 61 | +) -> AgentSettingsResponse: |
| 62 | + """Load agent settings for the workspace. |
| 63 | +
|
| 64 | + Returns defaults if no .codeframe/config.yaml exists. |
| 65 | + """ |
| 66 | + try: |
| 67 | + config = load_environment_config(workspace.repo_path) or EnvironmentConfig() |
| 68 | + return _config_to_response(config) |
| 69 | + except Exception as e: |
| 70 | + logger.error(f"Failed to load settings: {e}", exc_info=True) |
| 71 | + raise HTTPException( |
| 72 | + status_code=500, |
| 73 | + detail=api_error( |
| 74 | + "Failed to load settings", ErrorCodes.EXECUTION_FAILED, str(e) |
| 75 | + ), |
| 76 | + ) |
| 77 | + |
| 78 | + |
| 79 | +@router.put("", response_model=AgentSettingsResponse) |
| 80 | +@rate_limit_standard() |
| 81 | +async def update_settings( |
| 82 | + request: Request, |
| 83 | + body: UpdateAgentSettingsRequest, |
| 84 | + workspace: Workspace = Depends(get_v2_workspace), |
| 85 | +) -> AgentSettingsResponse: |
| 86 | + """Save agent settings. |
| 87 | +
|
| 88 | + Merges into existing EnvironmentConfig so unrelated fields |
| 89 | + (package_manager, test_framework, etc.) are preserved. |
| 90 | + """ |
| 91 | + try: |
| 92 | + config = load_environment_config(workspace.repo_path) or EnvironmentConfig() |
| 93 | + if config.agent_budget is None: |
| 94 | + config.agent_budget = AgentBudgetConfig() |
| 95 | + |
| 96 | + config.agent_budget.max_iterations = body.max_turns |
| 97 | + config.max_cost_usd = body.max_cost_usd |
| 98 | + # Skip empty model strings — they're equivalent to "key not present" |
| 99 | + # in _config_to_response, so persisting them just adds yaml noise. |
| 100 | + # AgentType Literal in the model already rejects unknown agent_type values. |
| 101 | + config.agent_type_models = { |
| 102 | + entry.agent_type: entry.default_model |
| 103 | + for entry in body.agent_models |
| 104 | + if entry.default_model |
| 105 | + } |
| 106 | + |
| 107 | + save_environment_config(workspace.repo_path, config) |
| 108 | + return _config_to_response(config) |
| 109 | + except Exception as e: |
| 110 | + logger.error(f"Failed to save settings: {e}", exc_info=True) |
| 111 | + raise HTTPException( |
| 112 | + status_code=500, |
| 113 | + detail=api_error( |
| 114 | + "Failed to save settings", ErrorCodes.EXECUTION_FAILED, str(e) |
| 115 | + ), |
| 116 | + ) |
0 commit comments