mirror of
https://github.com/open-webui/computer.git
synced 2026-11-29 16:35:22 -05:00
refac
This commit is contained in:
1 parent
14cfd98a67
commit
89ff737dfd
21 files changed
+1875
-90
No files matched your search
@@ -0,0 +1,189 @@
|
||||
"""Configuration model helpers for coding agents."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from copy import deepcopy
|
||||
from typing import Any
|
||||
|
||||
from fastapi import HTTPException
|
||||
|
||||
from cptr.models import Config
|
||||
|
||||
CONFIG_KEY_AGENT_PROFILES = "agents.profiles"
|
||||
|
||||
DEFAULT_AGENT_PROFILES: list[dict[str, Any]] = [
|
||||
{
|
||||
"id": "codex",
|
||||
"agent": "codex",
|
||||
"name": "Codex",
|
||||
"mode": "auto",
|
||||
"command": "codex",
|
||||
"home": None,
|
||||
"models": ["gpt-5.4"],
|
||||
"default_model": "gpt-5.4",
|
||||
"approval_mode": "auto",
|
||||
"sandbox_mode": "workspace-write",
|
||||
},
|
||||
{
|
||||
"id": "claude-code",
|
||||
"agent": "claude_code",
|
||||
"name": "Claude Code",
|
||||
"mode": "auto",
|
||||
"command": "claude",
|
||||
"home": None,
|
||||
"models": ["claude-sonnet-4-6"],
|
||||
"default_model": "claude-sonnet-4-6",
|
||||
"permission_mode": "default",
|
||||
"launch_args": "",
|
||||
},
|
||||
]
|
||||
|
||||
_PROFILE_ID_RE = re.compile(r"^[a-z][a-z0-9_-]{0,63}$")
|
||||
_VALID_AGENTS = {"codex", "claude_code"}
|
||||
_VALID_MODES = {"auto", "enabled", "disabled"}
|
||||
_VALID_CODEX_APPROVAL = {"ask", "auto", "full"}
|
||||
_VALID_CODEX_SANDBOX = {"read-only", "workspace-write", "danger-full-access"}
|
||||
_VALID_CLAUDE_PERMISSION = {"default", "accept_edits", "bypass_permissions"}
|
||||
|
||||
|
||||
def default_agent_profiles() -> list[dict[str, Any]]:
|
||||
return deepcopy(DEFAULT_AGENT_PROFILES)
|
||||
|
||||
|
||||
def normalize_agent_profile(raw: dict[str, Any]) -> dict[str, Any]:
|
||||
profile = dict(raw)
|
||||
profile_id = str(profile.get("id") or "").strip()
|
||||
if not _PROFILE_ID_RE.fullmatch(profile_id):
|
||||
raise HTTPException(400, "agent profile id must be a slug up to 64 characters")
|
||||
|
||||
agent = str(profile.get("agent") or "").strip()
|
||||
if agent not in _VALID_AGENTS:
|
||||
raise HTTPException(400, "agent must be codex or claude_code")
|
||||
fallback_model = "gpt-5.4" if agent == "codex" else "claude-sonnet-4-6"
|
||||
|
||||
mode = str(profile.get("mode") or "auto").strip()
|
||||
if mode not in _VALID_MODES:
|
||||
raise HTTPException(400, "agent mode must be auto, enabled, or disabled")
|
||||
|
||||
command = str(profile.get("command") or "").strip()
|
||||
if not command:
|
||||
command = "codex" if agent == "codex" else "claude"
|
||||
|
||||
name = str(profile.get("name") or "").strip() or (
|
||||
"Codex" if agent == "codex" else "Claude Code"
|
||||
)
|
||||
home = profile.get("home")
|
||||
if isinstance(home, str):
|
||||
home = home.strip() or None
|
||||
elif home is not None:
|
||||
raise HTTPException(400, "agent home must be a string or null")
|
||||
|
||||
models = profile.get("models")
|
||||
if not isinstance(models, list):
|
||||
models = ["default"]
|
||||
normalized_models = []
|
||||
for model in models:
|
||||
if isinstance(model, str) and model.strip():
|
||||
model_id = model.strip()
|
||||
if model_id == "default":
|
||||
continue
|
||||
if "/" in model_id:
|
||||
raise HTTPException(400, "agent model ids cannot contain '/'")
|
||||
normalized_models.append(model_id)
|
||||
if not normalized_models:
|
||||
normalized_models = [fallback_model]
|
||||
|
||||
default_model = str(profile.get("default_model") or normalized_models[0]).strip()
|
||||
if default_model == "default":
|
||||
default_model = fallback_model
|
||||
if default_model not in normalized_models:
|
||||
raise HTTPException(400, "agent default_model must be present in models")
|
||||
|
||||
normalized: dict[str, Any] = {
|
||||
"id": profile_id,
|
||||
"agent": agent,
|
||||
"name": name,
|
||||
"mode": mode,
|
||||
"command": command,
|
||||
"home": home,
|
||||
"models": normalized_models,
|
||||
"default_model": default_model,
|
||||
}
|
||||
|
||||
if agent == "codex":
|
||||
approval_mode = str(profile.get("approval_mode") or "auto").strip()
|
||||
sandbox_mode = str(profile.get("sandbox_mode") or "workspace-write").strip()
|
||||
if approval_mode not in _VALID_CODEX_APPROVAL:
|
||||
raise HTTPException(400, "codex approval_mode must be ask, auto, or full")
|
||||
if sandbox_mode not in _VALID_CODEX_SANDBOX:
|
||||
raise HTTPException(
|
||||
400, "codex sandbox_mode must be read-only, workspace-write, or danger-full-access"
|
||||
)
|
||||
normalized.update(
|
||||
{
|
||||
"approval_mode": approval_mode,
|
||||
"sandbox_mode": sandbox_mode,
|
||||
}
|
||||
)
|
||||
else:
|
||||
permission_mode = str(profile.get("permission_mode") or "default").strip()
|
||||
if permission_mode not in _VALID_CLAUDE_PERMISSION:
|
||||
raise HTTPException(
|
||||
400, "claude permission_mode must be default, accept_edits, or bypass_permissions"
|
||||
)
|
||||
normalized.update(
|
||||
{
|
||||
"permission_mode": permission_mode,
|
||||
"launch_args": str(profile.get("launch_args") or "").strip(),
|
||||
}
|
||||
)
|
||||
|
||||
return normalized
|
||||
|
||||
|
||||
def normalize_agent_profiles(raw_profiles: Any) -> list[dict[str, Any]]:
|
||||
if raw_profiles is None:
|
||||
raw_profiles = []
|
||||
if not isinstance(raw_profiles, list):
|
||||
raise HTTPException(400, "agents.profiles must be a list")
|
||||
|
||||
profiles = []
|
||||
for profile in raw_profiles:
|
||||
if not isinstance(profile, dict):
|
||||
raise HTTPException(400, "agent profiles must be objects")
|
||||
profiles.append(normalize_agent_profile(profile))
|
||||
|
||||
seen: set[str] = set()
|
||||
for profile in profiles:
|
||||
profile_id = profile["id"]
|
||||
if profile_id in seen:
|
||||
raise HTTPException(400, f"duplicate agent profile id: {profile_id}")
|
||||
seen.add(profile_id)
|
||||
return profiles
|
||||
|
||||
|
||||
async def get_raw_agent_profiles() -> Any:
|
||||
return await Config.get(CONFIG_KEY_AGENT_PROFILES)
|
||||
|
||||
|
||||
async def save_agent_profiles(profiles: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
||||
normalized = normalize_agent_profiles(profiles)
|
||||
await Config.upsert({CONFIG_KEY_AGENT_PROFILES: normalized})
|
||||
return normalized
|
||||
|
||||
|
||||
def model_id_for_profile(profile: dict[str, Any], model: str) -> str:
|
||||
return f"agent:{profile['id']}/{model}"
|
||||
|
||||
|
||||
def parse_agent_model_id(model_id: str) -> tuple[str, str] | None:
|
||||
if not model_id.startswith("agent:"):
|
||||
return None
|
||||
rest = model_id[len("agent:") :]
|
||||
if "/" not in rest:
|
||||
return None
|
||||
profile_id, model = rest.split("/", 1)
|
||||
if not profile_id or not model:
|
||||
return None
|
||||
return profile_id, model
|
||||
Reference in new issue
Block a user