Initial commit of integrated agent system
This commit is contained in:
23
backend/api/__init__.py
Normal file
23
backend/api/__init__.py
Normal file
@@ -0,0 +1,23 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
API Routes Package
|
||||
|
||||
Provides REST API endpoints for:
|
||||
- Agent management
|
||||
- Workspace management
|
||||
- Tool guard operations
|
||||
"""
|
||||
|
||||
from .agents import router as agents_router
|
||||
from .workspaces import router as workspaces_router
|
||||
from .guard import router as guard_router
|
||||
from .openclaw import router as openclaw_router
|
||||
from .runtime import router as runtime_router
|
||||
|
||||
__all__ = [
|
||||
"agents_router",
|
||||
"workspaces_router",
|
||||
"guard_router",
|
||||
"openclaw_router",
|
||||
"runtime_router",
|
||||
]
|
||||
709
backend/api/agents.py
Normal file
709
backend/api/agents.py
Normal file
@@ -0,0 +1,709 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Agent API Routes
|
||||
|
||||
Provides REST API endpoints for agent management within workspaces.
|
||||
"""
|
||||
import logging
|
||||
import os
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Depends, Body, UploadFile, File, Form
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from backend.agents import AgentFactory, get_registry
|
||||
from backend.agents.workspace_manager import RunWorkspaceManager
|
||||
from backend.agents.agent_workspace import load_agent_workspace_config
|
||||
from backend.agents.skills_manager import SkillsManager
|
||||
from backend.agents.toolkit_factory import load_agent_profiles
|
||||
from backend.config.bootstrap_config import get_bootstrap_config_for_run
|
||||
from backend.llm.models import get_agent_model_info
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/api/workspaces/{workspace_id}/agents", tags=["agents"])
|
||||
|
||||
|
||||
# Request/Response Models
|
||||
class CreateAgentRequest(BaseModel):
|
||||
"""Request to create a new agent."""
|
||||
agent_id: str = Field(..., description="Unique agent identifier")
|
||||
agent_type: str = Field(..., description="Type of agent (e.g., technical_analyst)")
|
||||
name: Optional[str] = Field(None, description="Display name")
|
||||
description: Optional[str] = Field(None, description="Agent description")
|
||||
clone_from: Optional[str] = Field(None, description="Agent ID to clone from")
|
||||
llm_model_config: Optional[Dict[str, Any]] = Field(None, description="LLM model configuration")
|
||||
|
||||
|
||||
class UpdateAgentRequest(BaseModel):
|
||||
"""Request to update an agent."""
|
||||
name: Optional[str] = None
|
||||
description: Optional[str] = None
|
||||
enabled_skills: Optional[List[str]] = None
|
||||
disabled_skills: Optional[List[str]] = None
|
||||
|
||||
|
||||
class InstallExternalSkillRequest(BaseModel):
|
||||
"""Request to install an external skill for one agent."""
|
||||
source: str = Field(..., description="Directory path, zip path, or http(s) zip URL")
|
||||
name: Optional[str] = Field(None, description="Optional override skill name")
|
||||
activate: bool = Field(True, description="Whether to enable skill immediately")
|
||||
|
||||
|
||||
class LocalSkillRequest(BaseModel):
|
||||
skill_name: str = Field(..., description="Local skill name")
|
||||
|
||||
|
||||
class LocalSkillContentRequest(BaseModel):
|
||||
content: str = Field(..., description="Updated SKILL.md content")
|
||||
|
||||
|
||||
class AgentResponse(BaseModel):
|
||||
"""Agent information response."""
|
||||
agent_id: str
|
||||
agent_type: str
|
||||
workspace_id: str
|
||||
config_path: str
|
||||
agent_dir: str
|
||||
status: str = "inactive"
|
||||
|
||||
|
||||
class AgentFileResponse(BaseModel):
|
||||
"""Agent file content response."""
|
||||
filename: str
|
||||
content: str
|
||||
|
||||
|
||||
class AgentProfileResponse(BaseModel):
|
||||
agent_id: str
|
||||
workspace_id: str
|
||||
profile: Dict[str, Any]
|
||||
|
||||
|
||||
class AgentSkillsResponse(BaseModel):
|
||||
agent_id: str
|
||||
workspace_id: str
|
||||
skills: List[Dict[str, Any]]
|
||||
|
||||
|
||||
class SkillDetailResponse(BaseModel):
|
||||
agent_id: str
|
||||
workspace_id: str
|
||||
skill: Dict[str, Any]
|
||||
|
||||
|
||||
# Dependencies
|
||||
def get_agent_factory():
|
||||
"""Get AgentFactory instance."""
|
||||
return AgentFactory()
|
||||
|
||||
|
||||
def get_workspace_manager():
|
||||
"""Get run-scoped workspace manager instance."""
|
||||
return RunWorkspaceManager()
|
||||
|
||||
|
||||
def get_skills_manager():
|
||||
"""Get SkillsManager instance."""
|
||||
return SkillsManager()
|
||||
|
||||
|
||||
# Routes
|
||||
@router.post("", response_model=AgentResponse)
|
||||
async def create_agent(
|
||||
workspace_id: str,
|
||||
request: CreateAgentRequest,
|
||||
factory: AgentFactory = Depends(get_agent_factory),
|
||||
registry = Depends(get_registry),
|
||||
):
|
||||
"""
|
||||
Create a new agent in a workspace.
|
||||
|
||||
Args:
|
||||
workspace_id: Workspace identifier
|
||||
request: Agent creation parameters
|
||||
|
||||
Returns:
|
||||
Created agent information
|
||||
"""
|
||||
# Check workspace exists
|
||||
if not factory.workspaces_root.exists():
|
||||
raise HTTPException(status_code=404, detail="Workspaces root not found")
|
||||
|
||||
workspace_dir = factory.workspaces_root / workspace_id
|
||||
if not workspace_dir.exists():
|
||||
raise HTTPException(status_code=404, detail=f"Workspace '{workspace_id}' not found")
|
||||
|
||||
try:
|
||||
# Create agent
|
||||
agent = factory.create_agent(
|
||||
agent_id=request.agent_id,
|
||||
agent_type=request.agent_type,
|
||||
workspace_id=workspace_id,
|
||||
clone_from=request.clone_from,
|
||||
)
|
||||
|
||||
# Register in registry
|
||||
registry.register(
|
||||
agent_id=request.agent_id,
|
||||
agent_type=request.agent_type,
|
||||
workspace_id=workspace_id,
|
||||
config_path=str(agent.config_path),
|
||||
agent_dir=str(agent.agent_dir),
|
||||
status="inactive",
|
||||
)
|
||||
|
||||
return AgentResponse(
|
||||
agent_id=agent.agent_id,
|
||||
agent_type=agent.agent_type,
|
||||
workspace_id=agent.workspace_id,
|
||||
config_path=str(agent.config_path),
|
||||
agent_dir=str(agent.agent_dir),
|
||||
status="inactive",
|
||||
)
|
||||
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
|
||||
@router.get("", response_model=List[AgentResponse])
|
||||
async def list_agents(
|
||||
workspace_id: str,
|
||||
factory: AgentFactory = Depends(get_agent_factory),
|
||||
):
|
||||
"""
|
||||
List all agents in a workspace.
|
||||
|
||||
Args:
|
||||
workspace_id: Workspace identifier
|
||||
|
||||
Returns:
|
||||
List of agents
|
||||
"""
|
||||
try:
|
||||
agents_data = factory.list_agents(workspace_id=workspace_id)
|
||||
return [
|
||||
AgentResponse(
|
||||
agent_id=agent["agent_id"],
|
||||
agent_type=agent["agent_type"],
|
||||
workspace_id=workspace_id,
|
||||
config_path=agent["config_path"],
|
||||
agent_dir=str(Path(agent["config_path"]).parent),
|
||||
status="inactive",
|
||||
)
|
||||
for agent in agents_data
|
||||
]
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=404, detail=str(e))
|
||||
|
||||
|
||||
@router.get("/{agent_id}", response_model=AgentResponse)
|
||||
async def get_agent(
|
||||
workspace_id: str,
|
||||
agent_id: str,
|
||||
registry = Depends(get_registry),
|
||||
):
|
||||
"""
|
||||
Get agent details.
|
||||
|
||||
Args:
|
||||
workspace_id: Workspace identifier
|
||||
agent_id: Agent identifier
|
||||
|
||||
Returns:
|
||||
Agent information
|
||||
"""
|
||||
agent_info = registry.get(agent_id)
|
||||
|
||||
if not agent_info or agent_info.workspace_id != workspace_id:
|
||||
raise HTTPException(status_code=404, detail=f"Agent '{agent_id}' not found")
|
||||
|
||||
return AgentResponse(
|
||||
agent_id=agent_info.agent_id,
|
||||
agent_type=agent_info.agent_type,
|
||||
workspace_id=agent_info.workspace_id,
|
||||
config_path=agent_info.config_path,
|
||||
agent_dir=agent_info.agent_dir,
|
||||
status=agent_info.status,
|
||||
)
|
||||
|
||||
|
||||
@router.get("/{agent_id}/profile", response_model=AgentProfileResponse)
|
||||
async def get_agent_profile(
|
||||
workspace_id: str,
|
||||
agent_id: str,
|
||||
skills_manager: SkillsManager = Depends(get_skills_manager),
|
||||
):
|
||||
asset_dir = skills_manager.get_agent_asset_dir(workspace_id, agent_id)
|
||||
agent_config = load_agent_workspace_config(asset_dir / "agent.yaml")
|
||||
profiles = load_agent_profiles()
|
||||
profile = profiles.get(agent_id, {})
|
||||
bootstrap = get_bootstrap_config_for_run(skills_manager.project_root, workspace_id)
|
||||
override = bootstrap.agent_override(agent_id)
|
||||
active_tool_groups = override.get("active_tool_groups", agent_config.active_tool_groups or profile.get("active_tool_groups", []))
|
||||
if not isinstance(active_tool_groups, list):
|
||||
active_tool_groups = []
|
||||
disabled_tool_groups = agent_config.disabled_tool_groups
|
||||
if disabled_tool_groups:
|
||||
disabled_set = set(disabled_tool_groups)
|
||||
active_tool_groups = [group_name for group_name in active_tool_groups if group_name not in disabled_set]
|
||||
|
||||
default_skills = profile.get("skills", [])
|
||||
if not isinstance(default_skills, list):
|
||||
default_skills = []
|
||||
resolved_skills = skills_manager.resolve_agent_skill_names(
|
||||
config_name=workspace_id,
|
||||
agent_id=agent_id,
|
||||
default_skills=default_skills,
|
||||
)
|
||||
prompt_files = agent_config.prompt_files or ["SOUL.md", "PROFILE.md", "AGENTS.md", "POLICY.md", "MEMORY.md"]
|
||||
model_name, model_provider = get_agent_model_info(agent_id)
|
||||
|
||||
return AgentProfileResponse(
|
||||
agent_id=agent_id,
|
||||
workspace_id=workspace_id,
|
||||
profile={
|
||||
"model_name": model_name,
|
||||
"model_provider": model_provider,
|
||||
"prompt_files": prompt_files,
|
||||
"default_skills": default_skills,
|
||||
"resolved_skills": resolved_skills,
|
||||
"active_tool_groups": active_tool_groups,
|
||||
"disabled_tool_groups": disabled_tool_groups,
|
||||
"enabled_skills": agent_config.enabled_skills,
|
||||
"disabled_skills": agent_config.disabled_skills,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/{agent_id}/skills", response_model=AgentSkillsResponse)
|
||||
async def get_agent_skills(
|
||||
workspace_id: str,
|
||||
agent_id: str,
|
||||
skills_manager: SkillsManager = Depends(get_skills_manager),
|
||||
):
|
||||
agent_asset_dir = skills_manager.get_agent_asset_dir(workspace_id, agent_id)
|
||||
agent_config = load_agent_workspace_config(agent_asset_dir / "agent.yaml")
|
||||
resolved_skills = set(skills_manager.resolve_agent_skill_names(config_name=workspace_id, agent_id=agent_id, default_skills=[]))
|
||||
enabled = set(agent_config.enabled_skills)
|
||||
disabled = set(agent_config.disabled_skills)
|
||||
|
||||
payload = []
|
||||
for item in skills_manager.list_agent_skill_catalog(workspace_id, agent_id):
|
||||
if item.skill_name in disabled:
|
||||
status = "disabled"
|
||||
elif item.skill_name in enabled:
|
||||
status = "enabled"
|
||||
elif item.skill_name in resolved_skills:
|
||||
status = "active"
|
||||
else:
|
||||
status = "available"
|
||||
payload.append({
|
||||
"skill_name": item.skill_name,
|
||||
"name": item.name,
|
||||
"description": item.description,
|
||||
"version": item.version,
|
||||
"source": item.source,
|
||||
"tools": item.tools,
|
||||
"status": status,
|
||||
})
|
||||
|
||||
return AgentSkillsResponse(agent_id=agent_id, workspace_id=workspace_id, skills=payload)
|
||||
|
||||
|
||||
@router.get("/{agent_id}/skills/{skill_name}", response_model=SkillDetailResponse)
|
||||
async def get_agent_skill_detail(
|
||||
workspace_id: str,
|
||||
agent_id: str,
|
||||
skill_name: str,
|
||||
skills_manager: SkillsManager = Depends(get_skills_manager),
|
||||
):
|
||||
try:
|
||||
detail = skills_manager.load_agent_skill_document(
|
||||
config_name=workspace_id,
|
||||
agent_id=agent_id,
|
||||
skill_name=skill_name,
|
||||
)
|
||||
except FileNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=f"Unknown skill: {skill_name}")
|
||||
|
||||
return SkillDetailResponse(agent_id=agent_id, workspace_id=workspace_id, skill=detail)
|
||||
|
||||
|
||||
@router.delete("/{agent_id}")
|
||||
async def delete_agent(
|
||||
workspace_id: str,
|
||||
agent_id: str,
|
||||
factory: AgentFactory = Depends(get_agent_factory),
|
||||
registry = Depends(get_registry),
|
||||
):
|
||||
"""
|
||||
Delete an agent.
|
||||
|
||||
Args:
|
||||
workspace_id: Workspace identifier
|
||||
agent_id: Agent identifier
|
||||
|
||||
Returns:
|
||||
Success message
|
||||
"""
|
||||
# Check agent exists in registry
|
||||
agent_info = registry.get(agent_id)
|
||||
if not agent_info or agent_info.workspace_id != workspace_id:
|
||||
raise HTTPException(status_code=404, detail=f"Agent '{agent_id}' not found")
|
||||
|
||||
# Delete from factory
|
||||
success = factory.delete_agent(agent_id, workspace_id)
|
||||
if not success:
|
||||
raise HTTPException(status_code=404, detail=f"Agent '{agent_id}' not found")
|
||||
|
||||
# Unregister
|
||||
registry.unregister(agent_id)
|
||||
|
||||
return {"message": f"Agent '{agent_id}' deleted successfully"}
|
||||
|
||||
|
||||
@router.patch("/{agent_id}", response_model=AgentResponse)
|
||||
async def update_agent(
|
||||
workspace_id: str,
|
||||
agent_id: str,
|
||||
request: UpdateAgentRequest,
|
||||
registry = Depends(get_registry),
|
||||
):
|
||||
"""
|
||||
Update agent configuration.
|
||||
|
||||
Args:
|
||||
workspace_id: Workspace identifier
|
||||
agent_id: Agent identifier
|
||||
request: Update parameters
|
||||
|
||||
Returns:
|
||||
Updated agent information
|
||||
"""
|
||||
agent_info = registry.get(agent_id)
|
||||
if not agent_info or agent_info.workspace_id != workspace_id:
|
||||
raise HTTPException(status_code=404, detail=f"Agent '{agent_id}' not found")
|
||||
|
||||
# Update metadata in registry
|
||||
metadata_updates = {}
|
||||
if request.name:
|
||||
metadata_updates["name"] = request.name
|
||||
if request.description:
|
||||
metadata_updates["description"] = request.description
|
||||
|
||||
if metadata_updates:
|
||||
registry.update_metadata(agent_id, metadata_updates)
|
||||
|
||||
# Update skills if provided
|
||||
if request.enabled_skills or request.disabled_skills:
|
||||
skills_manager = SkillsManager()
|
||||
skills_manager.update_agent_skill_overrides(
|
||||
config_name=workspace_id,
|
||||
agent_id=agent_id,
|
||||
enable=request.enabled_skills or [],
|
||||
disable=request.disabled_skills or [],
|
||||
)
|
||||
|
||||
# Get updated info
|
||||
agent_info = registry.get(agent_id)
|
||||
return AgentResponse(
|
||||
agent_id=agent_info.agent_id,
|
||||
agent_type=agent_info.agent_type,
|
||||
workspace_id=agent_info.workspace_id,
|
||||
config_path=agent_info.config_path,
|
||||
agent_dir=agent_info.agent_dir,
|
||||
status=agent_info.status,
|
||||
)
|
||||
|
||||
|
||||
@router.post("/{agent_id}/skills/{skill_name}/enable")
|
||||
async def enable_skill(
|
||||
workspace_id: str,
|
||||
agent_id: str,
|
||||
skill_name: str,
|
||||
registry = Depends(get_registry),
|
||||
):
|
||||
"""
|
||||
Enable a skill for an agent.
|
||||
|
||||
Args:
|
||||
workspace_id: Workspace identifier
|
||||
agent_id: Agent identifier
|
||||
skill_name: Skill name to enable
|
||||
|
||||
Returns:
|
||||
Success message
|
||||
"""
|
||||
agent_info = registry.get(agent_id)
|
||||
if not agent_info or agent_info.workspace_id != workspace_id:
|
||||
raise HTTPException(status_code=404, detail=f"Agent '{agent_id}' not found")
|
||||
|
||||
skills_manager = SkillsManager()
|
||||
result = skills_manager.update_agent_skill_overrides(
|
||||
config_name=workspace_id,
|
||||
agent_id=agent_id,
|
||||
enable=[skill_name],
|
||||
)
|
||||
|
||||
return {
|
||||
"message": f"Skill '{skill_name}' enabled for agent '{agent_id}'",
|
||||
"enabled_skills": result["enabled_skills"],
|
||||
}
|
||||
|
||||
|
||||
@router.post("/{agent_id}/skills/{skill_name}/disable")
|
||||
async def disable_skill(
|
||||
workspace_id: str,
|
||||
agent_id: str,
|
||||
skill_name: str,
|
||||
registry = Depends(get_registry),
|
||||
):
|
||||
"""
|
||||
Disable a skill for an agent.
|
||||
|
||||
Args:
|
||||
workspace_id: Workspace identifier
|
||||
agent_id: Agent identifier
|
||||
skill_name: Skill name to disable
|
||||
|
||||
Returns:
|
||||
Success message
|
||||
"""
|
||||
agent_info = registry.get(agent_id)
|
||||
if not agent_info or agent_info.workspace_id != workspace_id:
|
||||
raise HTTPException(status_code=404, detail=f"Agent '{agent_id}' not found")
|
||||
|
||||
skills_manager = SkillsManager()
|
||||
result = skills_manager.update_agent_skill_overrides(
|
||||
config_name=workspace_id,
|
||||
agent_id=agent_id,
|
||||
disable=[skill_name],
|
||||
)
|
||||
|
||||
return {
|
||||
"message": f"Skill '{skill_name}' disabled for agent '{agent_id}'",
|
||||
"disabled_skills": result["disabled_skills"],
|
||||
}
|
||||
|
||||
|
||||
@router.post("/{agent_id}/skills/install")
|
||||
async def install_external_skill(
|
||||
workspace_id: str,
|
||||
agent_id: str,
|
||||
request: InstallExternalSkillRequest,
|
||||
registry=Depends(get_registry),
|
||||
):
|
||||
"""Install an external skill into one agent's local skills."""
|
||||
agent_info = registry.get(agent_id)
|
||||
if not agent_info or agent_info.workspace_id != workspace_id:
|
||||
raise HTTPException(status_code=404, detail=f"Agent '{agent_id}' not found")
|
||||
|
||||
skills_manager = SkillsManager()
|
||||
try:
|
||||
result = skills_manager.install_external_skill_for_agent(
|
||||
config_name=workspace_id,
|
||||
agent_id=agent_id,
|
||||
source=request.source,
|
||||
skill_name=request.name,
|
||||
activate=request.activate,
|
||||
)
|
||||
except (FileNotFoundError, ValueError) as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc))
|
||||
|
||||
return {
|
||||
"message": f"Installed external skill '{result['skill_name']}' for '{agent_id}'",
|
||||
**result,
|
||||
}
|
||||
|
||||
|
||||
@router.post("/{agent_id}/skills/local")
|
||||
async def create_local_skill(
|
||||
workspace_id: str,
|
||||
agent_id: str,
|
||||
request: LocalSkillRequest,
|
||||
registry=Depends(get_registry),
|
||||
):
|
||||
agent_info = registry.get(agent_id)
|
||||
if not agent_info or agent_info.workspace_id != workspace_id:
|
||||
raise HTTPException(status_code=404, detail=f"Agent '{agent_id}' not found")
|
||||
|
||||
skills_manager = SkillsManager()
|
||||
try:
|
||||
skills_manager.create_agent_local_skill(
|
||||
config_name=workspace_id,
|
||||
agent_id=agent_id,
|
||||
skill_name=request.skill_name,
|
||||
)
|
||||
except (ValueError, FileExistsError) as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc))
|
||||
|
||||
return {"message": f"Created local skill '{request.skill_name}' for '{agent_id}'"}
|
||||
|
||||
|
||||
@router.put("/{agent_id}/skills/local/{skill_name}")
|
||||
async def update_local_skill(
|
||||
workspace_id: str,
|
||||
agent_id: str,
|
||||
skill_name: str,
|
||||
request: LocalSkillContentRequest,
|
||||
registry=Depends(get_registry),
|
||||
):
|
||||
agent_info = registry.get(agent_id)
|
||||
if not agent_info or agent_info.workspace_id != workspace_id:
|
||||
raise HTTPException(status_code=404, detail=f"Agent '{agent_id}' not found")
|
||||
|
||||
skills_manager = SkillsManager()
|
||||
try:
|
||||
skills_manager.update_agent_local_skill(
|
||||
config_name=workspace_id,
|
||||
agent_id=agent_id,
|
||||
skill_name=skill_name,
|
||||
content=request.content,
|
||||
)
|
||||
except (ValueError, FileNotFoundError) as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc))
|
||||
|
||||
return {"message": f"Updated local skill '{skill_name}' for '{agent_id}'"}
|
||||
|
||||
|
||||
@router.delete("/{agent_id}/skills/local/{skill_name}")
|
||||
async def delete_local_skill(
|
||||
workspace_id: str,
|
||||
agent_id: str,
|
||||
skill_name: str,
|
||||
registry=Depends(get_registry),
|
||||
):
|
||||
agent_info = registry.get(agent_id)
|
||||
if not agent_info or agent_info.workspace_id != workspace_id:
|
||||
raise HTTPException(status_code=404, detail=f"Agent '{agent_id}' not found")
|
||||
|
||||
skills_manager = SkillsManager()
|
||||
try:
|
||||
skills_manager.delete_agent_local_skill(
|
||||
config_name=workspace_id,
|
||||
agent_id=agent_id,
|
||||
skill_name=skill_name,
|
||||
)
|
||||
skills_manager.forget_agent_skill_overrides(
|
||||
config_name=workspace_id,
|
||||
agent_id=agent_id,
|
||||
skill_names=[skill_name],
|
||||
)
|
||||
except (ValueError, FileNotFoundError) as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc))
|
||||
|
||||
return {"message": f"Deleted local skill '{skill_name}' for '{agent_id}'"}
|
||||
|
||||
|
||||
@router.post("/{agent_id}/skills/upload")
|
||||
async def upload_external_skill(
|
||||
workspace_id: str,
|
||||
agent_id: str,
|
||||
file: UploadFile = File(...),
|
||||
name: Optional[str] = Form(None),
|
||||
activate: bool = Form(True),
|
||||
registry=Depends(get_registry),
|
||||
):
|
||||
"""Upload a zip skill package from frontend and install for one agent."""
|
||||
agent_info = registry.get(agent_id)
|
||||
if not agent_info or agent_info.workspace_id != workspace_id:
|
||||
raise HTTPException(status_code=404, detail=f"Agent '{agent_id}' not found")
|
||||
|
||||
original_name = (file.filename or "").strip()
|
||||
if not original_name.lower().endswith(".zip"):
|
||||
raise HTTPException(status_code=400, detail="Uploaded file must be a .zip archive")
|
||||
|
||||
suffix = Path(original_name).suffix or ".zip"
|
||||
temp_path: Optional[str] = None
|
||||
try:
|
||||
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp:
|
||||
temp_path = tmp.name
|
||||
content = await file.read()
|
||||
tmp.write(content)
|
||||
|
||||
skills_manager = SkillsManager()
|
||||
result = skills_manager.install_external_skill_for_agent(
|
||||
config_name=workspace_id,
|
||||
agent_id=agent_id,
|
||||
source=temp_path,
|
||||
skill_name=name,
|
||||
activate=activate,
|
||||
)
|
||||
except (FileNotFoundError, ValueError) as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc))
|
||||
finally:
|
||||
try:
|
||||
await file.close()
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to close uploaded file: {e}")
|
||||
if temp_path and os.path.exists(temp_path):
|
||||
os.remove(temp_path)
|
||||
|
||||
return {
|
||||
"message": f"Uploaded and installed external skill '{result['skill_name']}' for '{agent_id}'",
|
||||
**result,
|
||||
}
|
||||
|
||||
|
||||
@router.get("/{agent_id}/files/{filename}", response_model=AgentFileResponse)
|
||||
async def get_agent_file(
|
||||
workspace_id: str,
|
||||
agent_id: str,
|
||||
filename: str,
|
||||
workspace_manager: RunWorkspaceManager = Depends(get_workspace_manager),
|
||||
):
|
||||
"""
|
||||
Read an agent's workspace file.
|
||||
|
||||
Args:
|
||||
workspace_id: Workspace identifier
|
||||
agent_id: Agent identifier
|
||||
filename: File to read (e.g., SOUL.md, PROFILE.md)
|
||||
|
||||
Returns:
|
||||
File content
|
||||
"""
|
||||
try:
|
||||
content = workspace_manager.load_agent_file(
|
||||
config_name=workspace_id,
|
||||
agent_id=agent_id,
|
||||
filename=filename,
|
||||
)
|
||||
return AgentFileResponse(filename=filename, content=content)
|
||||
except FileNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=f"File '{filename}' not found")
|
||||
|
||||
|
||||
@router.put("/{agent_id}/files/{filename}", response_model=AgentFileResponse)
|
||||
async def update_agent_file(
|
||||
workspace_id: str,
|
||||
agent_id: str,
|
||||
filename: str,
|
||||
content: str = Body(..., media_type="text/plain"),
|
||||
workspace_manager: RunWorkspaceManager = Depends(get_workspace_manager),
|
||||
):
|
||||
"""
|
||||
Update an agent's workspace file.
|
||||
|
||||
Args:
|
||||
workspace_id: Workspace identifier
|
||||
agent_id: Agent identifier
|
||||
filename: File to update
|
||||
content: New file content
|
||||
|
||||
Returns:
|
||||
Updated file information
|
||||
"""
|
||||
try:
|
||||
workspace_manager.update_agent_file(
|
||||
config_name=workspace_id,
|
||||
agent_id=agent_id,
|
||||
filename=filename,
|
||||
content=content,
|
||||
)
|
||||
return AgentFileResponse(filename=filename, content=content)
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
257
backend/api/guard.py
Normal file
257
backend/api/guard.py
Normal file
@@ -0,0 +1,257 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Tool Guard API Routes
|
||||
|
||||
Provides REST API endpoints for tool guard operations.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Dict, List, Optional
|
||||
from datetime import datetime
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from backend.agents.base.tool_guard import (
|
||||
ApprovalRecord,
|
||||
ApprovalStatus,
|
||||
SeverityLevel,
|
||||
TOOL_GUARD_STORE,
|
||||
default_findings_for_tool,
|
||||
)
|
||||
|
||||
router = APIRouter(prefix="/api/guard", tags=["guard"])
|
||||
|
||||
|
||||
# Request/Response Models
|
||||
class ToolCallRequest(BaseModel):
|
||||
"""Tool call request."""
|
||||
tool_name: str = Field(..., description="Name of the tool")
|
||||
tool_input: Dict[str, Any] = Field(default_factory=dict, description="Tool parameters")
|
||||
agent_id: str = Field(..., description="Agent making the request")
|
||||
workspace_id: str = Field(..., description="Workspace context")
|
||||
session_id: Optional[str] = Field(None, description="Session identifier")
|
||||
|
||||
|
||||
class ApprovalRequest(BaseModel):
|
||||
"""Request to approve a tool call."""
|
||||
approval_id: str = Field(..., description="Approval request ID")
|
||||
one_time: bool = Field(True, description="Whether this is a one-time approval")
|
||||
expires_in_minutes: Optional[int] = Field(30, description="Approval expiration time")
|
||||
|
||||
|
||||
class DenyRequest(BaseModel):
|
||||
"""Request to deny a tool call."""
|
||||
approval_id: str = Field(..., description="Approval request ID")
|
||||
reason: Optional[str] = Field(None, description="Reason for denial")
|
||||
|
||||
|
||||
class ToolFinding(BaseModel):
|
||||
"""Tool guard finding."""
|
||||
severity: SeverityLevel
|
||||
message: str
|
||||
field: Optional[str] = None
|
||||
|
||||
|
||||
class ApprovalResponse(BaseModel):
|
||||
"""Tool approval response."""
|
||||
approval_id: str
|
||||
status: ApprovalStatus
|
||||
tool_name: str
|
||||
tool_input: Dict[str, Any]
|
||||
agent_id: str
|
||||
workspace_id: str
|
||||
session_id: Optional[str] = None
|
||||
findings: List[ToolFinding] = Field(default_factory=list)
|
||||
created_at: str
|
||||
resolved_at: Optional[str] = None
|
||||
resolved_by: Optional[str] = None
|
||||
|
||||
|
||||
class PendingApprovalsResponse(BaseModel):
|
||||
"""List of pending approvals."""
|
||||
approvals: List[ApprovalResponse]
|
||||
total: int
|
||||
|
||||
|
||||
STORE = TOOL_GUARD_STORE
|
||||
SAFE_TOOLS = {
|
||||
"get_price",
|
||||
"get_fundamentals",
|
||||
"get_news",
|
||||
"analyze_technical",
|
||||
}
|
||||
|
||||
|
||||
def _to_response(record: ApprovalRecord) -> ApprovalResponse:
|
||||
return ApprovalResponse(
|
||||
approval_id=record.approval_id,
|
||||
status=record.status,
|
||||
tool_name=record.tool_name,
|
||||
tool_input=record.tool_input,
|
||||
agent_id=record.agent_id,
|
||||
workspace_id=record.workspace_id,
|
||||
session_id=record.session_id,
|
||||
findings=[ToolFinding(**f.to_dict()) for f in record.findings],
|
||||
created_at=record.created_at.isoformat(),
|
||||
resolved_at=record.resolved_at.isoformat() if record.resolved_at else None,
|
||||
resolved_by=record.resolved_by,
|
||||
)
|
||||
|
||||
|
||||
# Routes
|
||||
@router.post("/check", response_model=ApprovalResponse)
|
||||
async def check_tool_call(
|
||||
request: ToolCallRequest,
|
||||
):
|
||||
"""
|
||||
Check if a tool call requires approval.
|
||||
|
||||
Args:
|
||||
request: Tool call details
|
||||
|
||||
Returns:
|
||||
Approval status - may be auto-approved, auto-denied, or pending
|
||||
"""
|
||||
record = STORE.create_pending(
|
||||
tool_name=request.tool_name,
|
||||
tool_input=request.tool_input,
|
||||
agent_id=request.agent_id,
|
||||
workspace_id=request.workspace_id,
|
||||
session_id=request.session_id,
|
||||
findings=default_findings_for_tool(request.tool_name),
|
||||
)
|
||||
|
||||
if request.tool_name in SAFE_TOOLS:
|
||||
record.status = ApprovalStatus.APPROVED
|
||||
record.resolved_at = datetime.utcnow()
|
||||
record.resolved_by = "system"
|
||||
STORE.set_status(
|
||||
record.approval_id,
|
||||
ApprovalStatus.APPROVED,
|
||||
resolved_by="system",
|
||||
notify_request=False,
|
||||
)
|
||||
|
||||
return _to_response(record)
|
||||
|
||||
|
||||
@router.post("/approve", response_model=ApprovalResponse)
|
||||
async def approve_tool_call(
|
||||
request: ApprovalRequest,
|
||||
):
|
||||
"""
|
||||
Approve a pending tool call.
|
||||
|
||||
Args:
|
||||
request: Approval parameters
|
||||
|
||||
Returns:
|
||||
Updated approval status
|
||||
"""
|
||||
record = STORE.get(request.approval_id)
|
||||
if not record:
|
||||
raise HTTPException(status_code=404, detail="Approval request not found")
|
||||
|
||||
if record.status != ApprovalStatus.PENDING:
|
||||
raise HTTPException(status_code=400, detail=f"Approval already {record.status}")
|
||||
|
||||
record.status = ApprovalStatus.APPROVED
|
||||
record.resolved_at = datetime.utcnow()
|
||||
record.resolved_by = "user"
|
||||
|
||||
return _to_response(record)
|
||||
|
||||
|
||||
@router.post("/deny", response_model=ApprovalResponse)
|
||||
async def deny_tool_call(
|
||||
request: DenyRequest,
|
||||
):
|
||||
"""
|
||||
Deny a pending tool call.
|
||||
|
||||
Args:
|
||||
request: Denial parameters
|
||||
|
||||
Returns:
|
||||
Updated approval status
|
||||
"""
|
||||
record = STORE.get(request.approval_id)
|
||||
if not record:
|
||||
raise HTTPException(status_code=404, detail="Approval request not found")
|
||||
|
||||
if record.status != ApprovalStatus.PENDING:
|
||||
raise HTTPException(status_code=400, detail=f"Approval already {record.status}")
|
||||
|
||||
record.status = ApprovalStatus.DENIED
|
||||
record.resolved_at = datetime.utcnow()
|
||||
record.resolved_by = "user"
|
||||
record.metadata["denial_reason"] = request.reason
|
||||
|
||||
return _to_response(record)
|
||||
|
||||
|
||||
@router.get("/pending", response_model=PendingApprovalsResponse)
|
||||
async def list_pending_approvals(
|
||||
workspace_id: Optional[str] = None,
|
||||
agent_id: Optional[str] = None,
|
||||
):
|
||||
"""
|
||||
List pending tool approval requests.
|
||||
|
||||
Args:
|
||||
workspace_id: Filter by workspace
|
||||
agent_id: Filter by agent
|
||||
|
||||
Returns:
|
||||
List of pending approvals
|
||||
"""
|
||||
pending = [
|
||||
_to_response(record)
|
||||
for record in STORE.list(
|
||||
status=ApprovalStatus.PENDING,
|
||||
workspace_id=workspace_id,
|
||||
agent_id=agent_id,
|
||||
)
|
||||
]
|
||||
return PendingApprovalsResponse(approvals=pending, total=len(pending))
|
||||
|
||||
|
||||
@router.get("/approvals/{approval_id}", response_model=ApprovalResponse)
|
||||
async def get_approval_status(
|
||||
approval_id: str,
|
||||
):
|
||||
"""
|
||||
Get the status of a specific approval request.
|
||||
|
||||
Args:
|
||||
approval_id: Approval request ID
|
||||
|
||||
Returns:
|
||||
Approval status
|
||||
"""
|
||||
record = STORE.get(approval_id)
|
||||
if not record:
|
||||
raise HTTPException(status_code=404, detail="Approval request not found")
|
||||
return _to_response(record)
|
||||
|
||||
|
||||
@router.delete("/approvals/{approval_id}")
|
||||
async def cancel_approval(
|
||||
approval_id: str,
|
||||
):
|
||||
"""
|
||||
Cancel/delete a pending approval request.
|
||||
|
||||
Args:
|
||||
approval_id: Approval request ID
|
||||
|
||||
Returns:
|
||||
Success message
|
||||
"""
|
||||
record = STORE.get(approval_id)
|
||||
if not record:
|
||||
raise HTTPException(status_code=404, detail="Approval request not found")
|
||||
|
||||
STORE.cancel(approval_id)
|
||||
return _to_response(record)
|
||||
839
backend/api/openclaw.py
Normal file
839
backend/api/openclaw.py
Normal file
@@ -0,0 +1,839 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Read-only OpenClaw CLI API routes — typed with Pydantic models."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from backend.services.openclaw_cli import OpenClawCliError, OpenClawCliService
|
||||
from shared.models.openclaw import OpenClawStatus
|
||||
|
||||
|
||||
router = APIRouter(prefix="/api/openclaw", tags=["openclaw"])
|
||||
|
||||
|
||||
def get_openclaw_cli_service() -> OpenClawCliService:
|
||||
"""Build the OpenClaw CLI service dependency."""
|
||||
return OpenClawCliService()
|
||||
|
||||
|
||||
def _raise_cli_http_error(exc: OpenClawCliError) -> None:
|
||||
detail = {
|
||||
"message": str(exc),
|
||||
"command": exc.command,
|
||||
"exit_code": exc.exit_code,
|
||||
"stdout": exc.stdout,
|
||||
"stderr": exc.stderr,
|
||||
}
|
||||
status_code = 503 if exc.exit_code is None else 502
|
||||
raise HTTPException(status_code=status_code, detail=detail) from exc
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Response wrappers
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class StatusResponse(BaseModel):
|
||||
status: object
|
||||
|
||||
|
||||
class SessionsResponse(BaseModel):
|
||||
sessions: list[object]
|
||||
|
||||
|
||||
class SessionDetailResponse(BaseModel):
|
||||
session: object | None
|
||||
|
||||
|
||||
class SessionHistoryResponse(BaseModel):
|
||||
session_key: str
|
||||
session_id: str | None
|
||||
events: list[object]
|
||||
history: list[object]
|
||||
raw_text: str | None
|
||||
|
||||
|
||||
class CronResponse(BaseModel):
|
||||
cron: list[object]
|
||||
jobs: list[object]
|
||||
|
||||
|
||||
class ApprovalsResponse(BaseModel):
|
||||
approvals: list[object]
|
||||
pending: list[object]
|
||||
|
||||
|
||||
class AgentsResponse(BaseModel):
|
||||
agents: list[object]
|
||||
|
||||
|
||||
class SkillsResponse(BaseModel):
|
||||
workspace_dir: str
|
||||
managed_skills_dir: str
|
||||
skills: list[object]
|
||||
|
||||
|
||||
class ModelsResponse(BaseModel):
|
||||
models: list[object]
|
||||
|
||||
|
||||
class HooksResponse(BaseModel):
|
||||
workspace_dir: str
|
||||
managed_hooks_dir: str
|
||||
hooks: list[object]
|
||||
|
||||
|
||||
class PluginsResponse(BaseModel):
|
||||
workspace_dir: str
|
||||
plugins: list[object]
|
||||
diagnostics: list[object]
|
||||
|
||||
|
||||
class SecretsAuditResponse(BaseModel):
|
||||
version: int
|
||||
status: str
|
||||
findings: list[object]
|
||||
|
||||
|
||||
class SecurityAuditResponse2(BaseModel):
|
||||
report: object | None
|
||||
secret_diagnostics: list[str]
|
||||
|
||||
|
||||
class DaemonStatusResponse(BaseModel):
|
||||
service: object | None
|
||||
port: object | None
|
||||
rpc: object | None
|
||||
health: object | None
|
||||
|
||||
|
||||
class PairingListResponse2(BaseModel):
|
||||
channel: str
|
||||
requests: list[object]
|
||||
|
||||
|
||||
class QrCodeResponse2(BaseModel):
|
||||
setup_code: str
|
||||
gateway_url: str
|
||||
auth: str
|
||||
url_source: str
|
||||
|
||||
|
||||
class UpdateStatusResponse2(BaseModel):
|
||||
update: object | None
|
||||
channel: object | None
|
||||
|
||||
|
||||
class ModelAliasesResponse(BaseModel):
|
||||
aliases: dict[str, str]
|
||||
|
||||
|
||||
class ModelFallbacksResponse(BaseModel):
|
||||
key: str
|
||||
label: str
|
||||
items: list[object]
|
||||
|
||||
|
||||
class SkillUpdateResponse(BaseModel):
|
||||
ok: bool
|
||||
slug: str
|
||||
version: str
|
||||
error: str | None
|
||||
|
||||
|
||||
class ModelsStatusResponse(BaseModel):
|
||||
configPath: str | None = None
|
||||
agentId: str | None = None
|
||||
agentDir: str | None = None
|
||||
defaultModel: str | None = None
|
||||
resolvedDefault: str | None = None
|
||||
fallbacks: list[str] = Field(default_factory=list)
|
||||
imageModel: str | None = None
|
||||
imageFallbacks: list[str] = Field(default_factory=list)
|
||||
aliases: dict[str, str] = Field(default_factory=dict)
|
||||
allowed: list[str] = Field(default_factory=list)
|
||||
auth: dict[str, Any] = Field(default_factory=dict)
|
||||
|
||||
|
||||
class ChannelsStatusResponse(BaseModel):
|
||||
reachable: bool | None = None
|
||||
channelAccounts: dict[str, Any] = Field(default_factory=dict)
|
||||
channels: list[str] = Field(default_factory=list)
|
||||
issues: list[dict[str, Any]] = Field(default_factory=list)
|
||||
|
||||
|
||||
class ChannelsListResponse(BaseModel):
|
||||
chat: dict[str, list[str]] = Field(default_factory=dict)
|
||||
auth: list[dict[str, Any]] = Field(default_factory=list)
|
||||
usage: dict[str, Any] | None = None
|
||||
|
||||
|
||||
class HookInfoResponse(BaseModel):
|
||||
name: str | None = None
|
||||
description: str | None = None
|
||||
source: str | None = None
|
||||
pluginId: str | None = None
|
||||
filePath: str | None = None
|
||||
handlerPath: str | None = None
|
||||
hookKey: str | None = None
|
||||
emoji: str | None = None
|
||||
homepage: str | None = None
|
||||
events: list[str] = Field(default_factory=list)
|
||||
enabledByConfig: bool | None = None
|
||||
loadable: bool | None = None
|
||||
requirementsSatisfied: bool | None = None
|
||||
requirements: dict[str, Any] = Field(default_factory=dict)
|
||||
error: str | None = None
|
||||
raw: str | None = None
|
||||
|
||||
|
||||
class HooksCheckResponse(BaseModel):
|
||||
workspace_dir: str = ""
|
||||
managed_hooks_dir: str = ""
|
||||
hooks: list[dict[str, Any]] = Field(default_factory=list)
|
||||
eligible: bool | None = None
|
||||
verbose: bool | None = None
|
||||
|
||||
|
||||
class PluginInspectEntry(BaseModel):
|
||||
plugin: dict[str, Any] = Field(default_factory=dict)
|
||||
shape: str | None = None
|
||||
capabilityMode: str | None = None
|
||||
capabilityCount: int = 0
|
||||
capabilities: list[dict[str, Any]] = Field(default_factory=list)
|
||||
typedHooks: list[dict[str, Any]] = Field(default_factory=list)
|
||||
customHooks: list[dict[str, Any]] = Field(default_factory=list)
|
||||
tools: list[dict[str, Any]] = Field(default_factory=list)
|
||||
commands: list[str] = Field(default_factory=list)
|
||||
cliCommands: list[str] = Field(default_factory=list)
|
||||
services: list[str] = Field(default_factory=list)
|
||||
gatewayMethods: list[str] = Field(default_factory=list)
|
||||
mcpServers: list[dict[str, Any]] = Field(default_factory=list)
|
||||
lspServers: list[dict[str, Any]] = Field(default_factory=list)
|
||||
httpRouteCount: int = 0
|
||||
bundleCapabilities: list[str] = Field(default_factory=list)
|
||||
|
||||
|
||||
class PluginsInspectResponse(BaseModel):
|
||||
inspect: list[dict[str, Any]] = Field(default_factory=list)
|
||||
|
||||
|
||||
class AgentBindingItem(BaseModel):
|
||||
agentId: str
|
||||
match: dict[str, Any]
|
||||
description: str
|
||||
|
||||
|
||||
class AgentsBindingsResponse(BaseModel):
|
||||
bindings: list[AgentBindingItem]
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Routes — use typed model methods and return Pydantic models directly
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@router.get("/status")
|
||||
async def api_openclaw_status(
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> OpenClawStatus:
|
||||
"""Read `openclaw status --json` and return a typed model."""
|
||||
try:
|
||||
return service.status_model()
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.get("/sessions")
|
||||
async def api_openclaw_sessions(
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> SessionsResponse:
|
||||
"""Read `openclaw sessions --json` and return a typed SessionsList."""
|
||||
try:
|
||||
result = service.list_sessions_model()
|
||||
return SessionsResponse(sessions=result.sessions)
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.get("/sessions/{session_key:path}/history")
|
||||
async def api_openclaw_session_history(
|
||||
session_key: str,
|
||||
limit: int = Query(20, ge=1, le=200),
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> SessionHistoryResponse:
|
||||
"""Read session history and return a typed SessionHistory."""
|
||||
try:
|
||||
result = service.get_session_history_model(session_key, limit=limit)
|
||||
return SessionHistoryResponse(
|
||||
session_key=result.session_key,
|
||||
session_id=result.session_id,
|
||||
events=result.events,
|
||||
history=result.events, # alias for compat
|
||||
raw_text=result.raw_text,
|
||||
)
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.get("/sessions/{session_key:path}")
|
||||
async def api_openclaw_session_detail(
|
||||
session_key: str,
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> SessionDetailResponse:
|
||||
"""Resolve a single session and return it as a typed model."""
|
||||
try:
|
||||
session = service.get_session_model(session_key)
|
||||
return SessionDetailResponse(session=session)
|
||||
except KeyError as exc:
|
||||
raise HTTPException(status_code=404, detail=f"session '{session_key}' not found") from exc
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.get("/cron")
|
||||
async def api_openclaw_cron(
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> CronResponse:
|
||||
"""Read `openclaw cron list --json` and return a typed CronList."""
|
||||
try:
|
||||
result = service.list_cron_jobs_model()
|
||||
return CronResponse(cron=list(result.cron), jobs=list(result.jobs))
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.get("/approvals")
|
||||
async def api_openclaw_approvals(
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> ApprovalsResponse:
|
||||
"""Read `openclaw approvals get --json` and return a typed ApprovalsList."""
|
||||
try:
|
||||
result = service.list_approvals_model()
|
||||
return ApprovalsResponse(
|
||||
approvals=list(result.approvals),
|
||||
pending=list(result.pending),
|
||||
)
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.get("/agents")
|
||||
async def api_openclaw_agents(
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> AgentsResponse:
|
||||
"""Read `openclaw agents list --json` and return a typed AgentsList."""
|
||||
try:
|
||||
result = service.list_agents_model()
|
||||
return AgentsResponse(agents=list(result.agents))
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.get("/agents/presence")
|
||||
async def api_openclaw_agents_presence(
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> dict[str, Any]:
|
||||
"""Read runtime session presence for all agents from session files."""
|
||||
result = service.agents_presence()
|
||||
return result
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Write agents routes
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class AgentAddResponse(BaseModel):
|
||||
agentId: str
|
||||
name: str
|
||||
workspace: str
|
||||
agentDir: str
|
||||
model: str | None = None
|
||||
bindings: dict[str, Any] = Field(default_factory=dict)
|
||||
|
||||
|
||||
class AgentDeleteResponse(BaseModel):
|
||||
agentId: str
|
||||
workspace: str
|
||||
agentDir: str
|
||||
sessionsDir: str
|
||||
removedBindings: list[str] = Field(default_factory=list)
|
||||
removedAllow: list[str] = Field(default_factory=list)
|
||||
|
||||
|
||||
class AgentBindResponse(BaseModel):
|
||||
agentId: str
|
||||
added: list[str] = Field(default_factory=list)
|
||||
updated: list[str] = Field(default_factory=list)
|
||||
skipped: list[str] = Field(default_factory=list)
|
||||
conflicts: list[str] = Field(default_factory=list)
|
||||
|
||||
|
||||
class AgentUnbindResponse(BaseModel):
|
||||
agentId: str
|
||||
removed: list[str] = Field(default_factory=list)
|
||||
missing: list[str] = Field(default_factory=list)
|
||||
conflicts: list[str] = Field(default_factory=list)
|
||||
|
||||
|
||||
class AgentIdentityResponse(BaseModel):
|
||||
agentId: str
|
||||
identity: dict[str, Any] = Field(default_factory=dict)
|
||||
workspace: str | None = None
|
||||
identityFile: str | None = None
|
||||
|
||||
|
||||
@router.post("/agents/add")
|
||||
async def api_openclaw_agents_add(
|
||||
name: str,
|
||||
*,
|
||||
workspace: str | None = None,
|
||||
model: str | None = None,
|
||||
agent_dir: str | None = None,
|
||||
bind: list[str] | None = None,
|
||||
non_interactive: bool = False,
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> AgentAddResponse:
|
||||
"""Run `openclaw agents add <name>` and return JSON result."""
|
||||
try:
|
||||
result = service.agents_add(
|
||||
name,
|
||||
workspace=workspace,
|
||||
model=model,
|
||||
agent_dir=agent_dir,
|
||||
bind=bind,
|
||||
non_interactive=non_interactive,
|
||||
)
|
||||
return AgentAddResponse.model_validate(result, strict=False)
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.post("/agents/delete/{id}")
|
||||
async def api_openclaw_agents_delete(
|
||||
id: str,
|
||||
force: bool = False,
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> AgentDeleteResponse:
|
||||
"""Run `openclaw agents delete <id> [--force]` and return JSON result."""
|
||||
try:
|
||||
result = service.agents_delete(id, force=force)
|
||||
return AgentDeleteResponse.model_validate(result, strict=False)
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.post("/agents/bind")
|
||||
async def api_openclaw_agents_bind(
|
||||
*,
|
||||
agent: str | None = None,
|
||||
bind: list[str] | None = None,
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> AgentBindResponse:
|
||||
"""Run `openclaw agents bind [--agent <id>] [--bind <spec>]` and return JSON result."""
|
||||
try:
|
||||
result = service.agents_bind(agent=agent, bind=bind)
|
||||
return AgentBindResponse.model_validate(result, strict=False)
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.post("/agents/unbind")
|
||||
async def api_openclaw_agents_unbind(
|
||||
*,
|
||||
agent: str | None = None,
|
||||
bind: list[str] | None = None,
|
||||
all: bool = False,
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> AgentUnbindResponse:
|
||||
"""Run `openclaw agents unbind [--agent <id>] [--bind <spec>] [--all]` and return JSON result."""
|
||||
try:
|
||||
result = service.agents_unbind(agent=agent, bind=bind, all=all)
|
||||
return AgentUnbindResponse.model_validate(result, strict=False)
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.post("/agents/set-identity")
|
||||
async def api_openclaw_agents_set_identity(
|
||||
*,
|
||||
agent: str | None = None,
|
||||
workspace: str | None = None,
|
||||
identity_file: str | None = None,
|
||||
name: str | None = None,
|
||||
emoji: str | None = None,
|
||||
theme: str | None = None,
|
||||
avatar: str | None = None,
|
||||
from_identity: bool = False,
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> AgentIdentityResponse:
|
||||
"""Run `openclaw agents set-identity` and return JSON result."""
|
||||
try:
|
||||
result = service.agents_set_identity(
|
||||
agent=agent,
|
||||
workspace=workspace,
|
||||
identity_file=identity_file,
|
||||
name=name,
|
||||
emoji=emoji,
|
||||
theme=theme,
|
||||
avatar=avatar,
|
||||
from_identity=from_identity,
|
||||
)
|
||||
return AgentIdentityResponse.model_validate(result, strict=False)
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.get("/skills")
|
||||
async def api_openclaw_skills(
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> SkillsResponse:
|
||||
"""Read `openclaw skills list --json` and return a typed SkillStatusReport."""
|
||||
try:
|
||||
result = service.list_skills_model()
|
||||
return SkillsResponse(
|
||||
workspace_dir=result.workspace_dir,
|
||||
managed_skills_dir=result.managed_skills_dir,
|
||||
skills=list(result.skills),
|
||||
)
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.get("/models")
|
||||
async def api_openclaw_models(
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> ModelsResponse:
|
||||
"""Read `openclaw models list --json` and return a typed ModelsList."""
|
||||
try:
|
||||
result = service.list_models_model()
|
||||
return ModelsResponse(models=list(result.models))
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.get("/hooks")
|
||||
async def api_openclaw_hooks(
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> HooksResponse:
|
||||
try:
|
||||
result = service.list_hooks_model()
|
||||
return HooksResponse(
|
||||
workspace_dir=result.workspace_dir,
|
||||
managed_hooks_dir=result.managed_hooks_dir,
|
||||
hooks=list(result.hooks),
|
||||
)
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.get("/plugins")
|
||||
async def api_openclaw_plugins(
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> PluginsResponse:
|
||||
try:
|
||||
result = service.list_plugins_model()
|
||||
return PluginsResponse(
|
||||
workspace_dir=result.workspace_dir,
|
||||
plugins=list(result.plugins),
|
||||
diagnostics=list(result.diagnostics),
|
||||
)
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.get("/secrets-audit")
|
||||
async def api_openclaw_secrets_audit(
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> SecretsAuditResponse:
|
||||
try:
|
||||
result = service.secrets_audit_model()
|
||||
return SecretsAuditResponse(
|
||||
version=result.version,
|
||||
status=result.status,
|
||||
findings=list(result.findings),
|
||||
)
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.get("/security-audit")
|
||||
async def api_openclaw_security_audit(
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> SecurityAuditResponse2:
|
||||
try:
|
||||
result = service.security_audit_model()
|
||||
return SecurityAuditResponse2(
|
||||
report=result.report.model_dump() if result.report else None,
|
||||
secret_diagnostics=list(result.secret_diagnostics),
|
||||
)
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.get("/daemon-status")
|
||||
async def api_openclaw_daemon_status(
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> DaemonStatusResponse:
|
||||
try:
|
||||
result = service.daemon_status_model()
|
||||
return DaemonStatusResponse(
|
||||
service=result.service.model_dump() if result.service else None,
|
||||
port=result.port.model_dump() if result.port else None,
|
||||
rpc=result.rpc.model_dump() if result.rpc else None,
|
||||
health=result.health.model_dump() if result.health else None,
|
||||
)
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.get("/pairing")
|
||||
async def api_openclaw_pairing(
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> PairingListResponse2:
|
||||
try:
|
||||
result = service.pairing_list_model()
|
||||
return PairingListResponse2(
|
||||
channel=result.channel,
|
||||
requests=list(result.requests),
|
||||
)
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.get("/qr")
|
||||
async def api_openclaw_qr(
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> QrCodeResponse2:
|
||||
try:
|
||||
result = service.qr_code_model()
|
||||
return QrCodeResponse2(
|
||||
setup_code=result.setup_code,
|
||||
gateway_url=result.gateway_url,
|
||||
auth=result.auth,
|
||||
url_source=result.url_source,
|
||||
)
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.get("/update-status")
|
||||
async def api_openclaw_update_status(
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> UpdateStatusResponse2:
|
||||
try:
|
||||
result = service.update_status_model()
|
||||
return UpdateStatusResponse2(
|
||||
update=result.update.model_dump() if result.update else None,
|
||||
channel=result.channel.model_dump() if result.channel else None,
|
||||
)
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.get("/models-aliases")
|
||||
async def api_openclaw_models_aliases(
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> ModelAliasesResponse:
|
||||
try:
|
||||
result = service.list_model_aliases_model()
|
||||
return ModelAliasesResponse(aliases=result.aliases)
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.get("/models-fallbacks")
|
||||
async def api_openclaw_models_fallbacks(
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> ModelFallbacksResponse:
|
||||
try:
|
||||
result = service.list_model_fallbacks_model()
|
||||
return ModelFallbacksResponse(
|
||||
key=result.key,
|
||||
label=result.label,
|
||||
items=list(result.items),
|
||||
)
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.get("/models-image-fallbacks")
|
||||
async def api_openclaw_models_image_fallbacks(
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> ModelFallbacksResponse:
|
||||
try:
|
||||
result = service.list_model_image_fallbacks_model()
|
||||
return ModelFallbacksResponse(
|
||||
key=result.key,
|
||||
label=result.label,
|
||||
items=list(result.items),
|
||||
)
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.get("/skill-update")
|
||||
async def api_openclaw_skill_update(
|
||||
slug: str | None = None,
|
||||
all: bool = False,
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> SkillUpdateResponse:
|
||||
try:
|
||||
result = service.skill_update_model(slug=slug, all=all)
|
||||
return SkillUpdateResponse(
|
||||
ok=result.ok,
|
||||
slug=result.slug,
|
||||
version=result.version,
|
||||
error=result.error,
|
||||
)
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.get("/models-status")
|
||||
async def api_openclaw_models_status(
|
||||
probe: bool = False,
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> ModelsStatusResponse:
|
||||
"""Read `openclaw models status --json [--probe]` and return a typed dict."""
|
||||
try:
|
||||
result = service.models_status_model(probe=probe)
|
||||
return ModelsStatusResponse.model_validate(result, strict=False)
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.get("/channels-status")
|
||||
async def api_openclaw_channels_status(
|
||||
probe: bool = False,
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> ChannelsStatusResponse:
|
||||
"""Read `openclaw channels status --json [--probe]` and return a typed dict."""
|
||||
try:
|
||||
result = service.channels_status_model(probe=probe)
|
||||
return ChannelsStatusResponse.model_validate(result, strict=False)
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.get("/channels-list")
|
||||
async def api_openclaw_channels_list(
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> ChannelsListResponse:
|
||||
"""Read `openclaw channels list --json` and return a typed dict."""
|
||||
try:
|
||||
result = service.channels_list_model()
|
||||
return ChannelsListResponse.model_validate(result, strict=False)
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.get("/hooks/info/{name}")
|
||||
async def api_openclaw_hook_info(
|
||||
name: str,
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> HookInfoResponse:
|
||||
"""Read `openclaw hooks info <name> --json` and return a typed dict."""
|
||||
try:
|
||||
result = service.hook_info_model(name)
|
||||
return HookInfoResponse.model_validate(result, strict=False)
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.get("/hooks/check")
|
||||
async def api_openclaw_hooks_check(
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> HooksCheckResponse:
|
||||
"""Read `openclaw hooks check --json` and return a typed dict."""
|
||||
try:
|
||||
result = service.hooks_check_model()
|
||||
return HooksCheckResponse.model_validate(result, strict=False)
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.get("/plugins-inspect")
|
||||
async def api_openclaw_plugins_inspect(
|
||||
plugin_id: str | None = None,
|
||||
all: bool = False,
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> PluginsInspectResponse:
|
||||
"""Read `openclaw plugins inspect --json [--all]` and return a typed dict."""
|
||||
try:
|
||||
result = service.plugins_inspect_model(plugin_id=plugin_id, all=all)
|
||||
inspect = result if isinstance(result, list) else result.get("inspect", [])
|
||||
return PluginsInspectResponse(inspect=inspect)
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
class AgentBindingItem(BaseModel):
|
||||
agentId: str
|
||||
match: dict[str, Any]
|
||||
description: str
|
||||
|
||||
|
||||
class AgentsBindingsResponse(BaseModel):
|
||||
bindings: list[AgentBindingItem]
|
||||
|
||||
|
||||
@router.get("/agents-bindings")
|
||||
async def api_openclaw_agents_bindings(
|
||||
agent: str | None = None,
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> AgentsBindingsResponse:
|
||||
"""Read `openclaw agents bindings --json [--agent <id>]` and return bindings list."""
|
||||
try:
|
||||
result = service.agents_bindings_model(agent=agent)
|
||||
bindings = result if isinstance(result, list) else []
|
||||
return AgentsBindingsResponse(bindings=bindings)
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.get("/gateway-status")
|
||||
async def api_openclaw_gateway_status(
|
||||
url: str | None = None,
|
||||
token: str | None = None,
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> dict[str, Any]:
|
||||
"""Read `openclaw gateway status --json [--url <url>] [--token <token>]`. Returns full gateway probe result."""
|
||||
try:
|
||||
result = service.gateway_status(url=url, token=token)
|
||||
return result
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
@router.get("/memory-status")
|
||||
async def api_openclaw_memory_status(
|
||||
agent: str | None = None,
|
||||
deep: bool = False,
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Read `openclaw memory status --json [--agent <id>] [--deep]`. Returns array of per-agent memory status."""
|
||||
try:
|
||||
result = service.memory_status(agent=agent, deep=deep)
|
||||
return result if isinstance(result, list) else []
|
||||
except OpenClawCliError as exc:
|
||||
_raise_cli_http_error(exc)
|
||||
|
||||
|
||||
class WorkspaceFilesResponse(BaseModel):
|
||||
workspace: str
|
||||
files: list[dict[str, Any]]
|
||||
error: str | None = None
|
||||
|
||||
|
||||
@router.get("/workspace-files")
|
||||
async def api_openclaw_workspace_files(
|
||||
workspace: str = Query(..., description="Path to the agent workspace directory"),
|
||||
service: OpenClawCliService = Depends(get_openclaw_cli_service),
|
||||
) -> WorkspaceFilesResponse:
|
||||
"""List .md files in an OpenClaw agent workspace with their content previews."""
|
||||
result = service.list_workspace_files(workspace)
|
||||
return WorkspaceFilesResponse.model_validate(result, strict=False)
|
||||
969
backend/api/runtime.py
Normal file
969
backend/api/runtime.py
Normal file
@@ -0,0 +1,969 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Runtime API routes - Control Plane for managing Gateway processes."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import signal
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
from fastapi import APIRouter, BackgroundTasks, HTTPException, Request
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from backend.runtime.agent_runtime import AgentRuntimeState
|
||||
from backend.config.bootstrap_config import (
|
||||
resolve_runtime_config,
|
||||
update_bootstrap_values_for_run,
|
||||
)
|
||||
|
||||
router = APIRouter(prefix="/api/runtime", tags=["runtime"])
|
||||
|
||||
PROJECT_ROOT = Path(__file__).resolve().parents[2]
|
||||
|
||||
|
||||
class RuntimeState:
|
||||
"""Thread-safe singleton for managing runtime state.
|
||||
|
||||
Encapsulates runtime_manager, _gateway_process, and _gateway_port
|
||||
with asyncio.Lock protection for concurrent access.
|
||||
"""
|
||||
|
||||
_instance: Optional["RuntimeState"] = None
|
||||
_lock: "threading.Lock" = __import__("threading").Lock()
|
||||
|
||||
def __new__(cls) -> "RuntimeState":
|
||||
with cls._lock:
|
||||
if cls._instance is None:
|
||||
cls._instance = super().__new__(cls)
|
||||
cls._instance._initialized = False
|
||||
return cls._instance
|
||||
|
||||
def __init__(self) -> None:
|
||||
if self._initialized:
|
||||
return
|
||||
self._runtime_manager: Optional[Any] = None
|
||||
self._gateway_process: Optional[subprocess.Popen] = None
|
||||
self._gateway_port: int = 8765
|
||||
self._state_lock = asyncio.Lock()
|
||||
self._initialized = True
|
||||
|
||||
@property
|
||||
async def lock(self) -> asyncio.Lock:
|
||||
"""Get the asyncio lock for state synchronization."""
|
||||
return self._state_lock
|
||||
|
||||
@property
|
||||
def runtime_manager(self) -> Optional[Any]:
|
||||
"""Get the runtime manager (no lock - read only)."""
|
||||
return self._runtime_manager
|
||||
|
||||
@runtime_manager.setter
|
||||
def runtime_manager(self, value: Optional[Any]) -> None:
|
||||
"""Set the runtime manager."""
|
||||
self._runtime_manager = value
|
||||
|
||||
@property
|
||||
def gateway_process(self) -> Optional[subprocess.Popen]:
|
||||
"""Get the gateway process (no lock - read only)."""
|
||||
return self._gateway_process
|
||||
|
||||
@gateway_process.setter
|
||||
def gateway_process(self, value: Optional[subprocess.Popen]) -> None:
|
||||
"""Set the gateway process."""
|
||||
self._gateway_process = value
|
||||
|
||||
@property
|
||||
def gateway_port(self) -> int:
|
||||
"""Get the gateway port."""
|
||||
return self._gateway_port
|
||||
|
||||
@gateway_port.setter
|
||||
def gateway_port(self, value: int) -> None:
|
||||
"""Set the gateway port."""
|
||||
self._gateway_port = value
|
||||
|
||||
async def set_runtime_manager(self, manager: Any) -> None:
|
||||
"""Set runtime manager with lock protection."""
|
||||
async with self._state_lock:
|
||||
self._runtime_manager = manager
|
||||
|
||||
async def get_runtime_manager(self) -> Optional[Any]:
|
||||
"""Get runtime manager with lock protection."""
|
||||
async with self._state_lock:
|
||||
return self._runtime_manager
|
||||
|
||||
async def set_gateway_process(self, process: Optional[subprocess.Popen]) -> None:
|
||||
"""Set gateway process with lock protection."""
|
||||
async with self._state_lock:
|
||||
self._gateway_process = process
|
||||
|
||||
async def get_gateway_process(self) -> Optional[subprocess.Popen]:
|
||||
"""Get gateway process with lock protection."""
|
||||
async with self._state_lock:
|
||||
return self._gateway_process
|
||||
|
||||
async def set_gateway_port(self, port: int) -> None:
|
||||
"""Set gateway port with lock protection."""
|
||||
async with self._state_lock:
|
||||
self._gateway_port = port
|
||||
|
||||
async def get_gateway_port(self) -> int:
|
||||
"""Get gateway port with lock protection."""
|
||||
async with self._state_lock:
|
||||
return self._gateway_port
|
||||
|
||||
|
||||
# Singleton instance
|
||||
_runtime_state = RuntimeState()
|
||||
|
||||
|
||||
def get_runtime_state() -> RuntimeState:
|
||||
"""Get the RuntimeState singleton instance."""
|
||||
return _runtime_state
|
||||
|
||||
|
||||
# Backward compatibility: module-level runtime_manager for external imports
|
||||
# This is set by register_runtime_manager() for backward compatibility
|
||||
runtime_manager: Optional[Any] = None
|
||||
|
||||
|
||||
class RunContextResponse(BaseModel):
|
||||
config_name: str
|
||||
run_dir: str
|
||||
bootstrap_values: Dict[str, Any]
|
||||
|
||||
|
||||
class RuntimeAgentState(BaseModel):
|
||||
agent_id: str
|
||||
status: str
|
||||
last_session: Optional[str] = None
|
||||
last_updated: str
|
||||
|
||||
|
||||
class RuntimeAgentsResponse(BaseModel):
|
||||
agents: List[RuntimeAgentState]
|
||||
|
||||
|
||||
class RuntimeEvent(BaseModel):
|
||||
timestamp: str
|
||||
event: str
|
||||
details: Dict[str, Any]
|
||||
session: Optional[str]
|
||||
|
||||
|
||||
class RuntimeEventsResponse(BaseModel):
|
||||
events: List[RuntimeEvent]
|
||||
|
||||
|
||||
class LaunchConfig(BaseModel):
|
||||
"""Configuration for launching a new trading task."""
|
||||
launch_mode: str = Field(default="fresh", description="启动形式: fresh, restore")
|
||||
restore_run_id: Optional[str] = Field(default=None, description="历史任务 run_id,用于恢复启动")
|
||||
tickers: List[str] = Field(default_factory=list, description="股票池")
|
||||
schedule_mode: str = Field(default="daily", description="调度模式: daily, interval")
|
||||
interval_minutes: int = Field(default=60, ge=1, description="间隔分钟数")
|
||||
trigger_time: str = Field(default="09:30", description="触发时间 HH:MM")
|
||||
max_comm_cycles: int = Field(default=2, ge=1, description="最大会商轮数")
|
||||
initial_cash: float = Field(default=100000.0, gt=0, description="初始资金")
|
||||
margin_requirement: float = Field(default=0.0, ge=0, description="保证金要求")
|
||||
enable_memory: bool = Field(default=False, description="是否启用长期记忆")
|
||||
mode: str = Field(default="live", description="运行模式: live, backtest")
|
||||
start_date: Optional[str] = Field(default=None, description="回测开始日期 YYYY-MM-DD")
|
||||
end_date: Optional[str] = Field(default=None, description="回测结束日期 YYYY-MM-DD")
|
||||
poll_interval: int = Field(default=10, ge=1, le=300, description="市场数据轮询间隔(秒)")
|
||||
|
||||
|
||||
class LaunchResponse(BaseModel):
|
||||
run_id: str
|
||||
status: str
|
||||
run_dir: str
|
||||
gateway_port: int
|
||||
message: str
|
||||
|
||||
|
||||
class RuntimeHistoryItem(BaseModel):
|
||||
run_id: str
|
||||
run_dir: str
|
||||
updated_at: Optional[str] = None
|
||||
total_trades: int = 0
|
||||
total_asset_value: Optional[float] = None
|
||||
bootstrap: Dict[str, Any] = Field(default_factory=dict)
|
||||
|
||||
|
||||
class RuntimeHistoryResponse(BaseModel):
|
||||
runs: List[RuntimeHistoryItem]
|
||||
|
||||
|
||||
class StopResponse(BaseModel):
|
||||
status: str
|
||||
message: str
|
||||
|
||||
|
||||
class CleanupResponse(BaseModel):
|
||||
status: str
|
||||
kept: int
|
||||
pruned_run_ids: List[str]
|
||||
|
||||
|
||||
class GatewayStatusResponse(BaseModel):
|
||||
is_running: bool
|
||||
port: int
|
||||
run_id: Optional[str] = None
|
||||
|
||||
|
||||
class RuntimeConfigResponse(BaseModel):
|
||||
run_id: str
|
||||
is_running: bool
|
||||
gateway_port: int
|
||||
bootstrap: Dict[str, Any]
|
||||
resolved: Dict[str, Any]
|
||||
|
||||
|
||||
class RuntimeLogResponse(BaseModel):
|
||||
run_id: Optional[str] = None
|
||||
is_running: bool
|
||||
log_path: Optional[str] = None
|
||||
content: str = ""
|
||||
|
||||
|
||||
class UpdateRuntimeConfigRequest(BaseModel):
|
||||
schedule_mode: Optional[str] = None
|
||||
interval_minutes: Optional[int] = Field(default=None, ge=1)
|
||||
trigger_time: Optional[str] = None
|
||||
max_comm_cycles: Optional[int] = Field(default=None, ge=1)
|
||||
initial_cash: Optional[float] = Field(default=None, gt=0)
|
||||
margin_requirement: Optional[float] = Field(default=None, ge=0)
|
||||
enable_memory: Optional[bool] = None
|
||||
|
||||
|
||||
def _generate_run_id() -> str:
|
||||
"""Generate timestamp-based run ID: YYYYMMDD_HHMMSS"""
|
||||
return datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
|
||||
|
||||
def _get_run_dir(run_id: str) -> Path:
|
||||
"""Return the run directory for a given run ID."""
|
||||
return PROJECT_ROOT / "runs" / run_id
|
||||
|
||||
|
||||
def _load_run_snapshot(run_id: str) -> Dict[str, Any]:
|
||||
"""Load a specific run snapshot by run_id."""
|
||||
snapshot_path = _get_run_dir(run_id) / "state" / "runtime_state.json"
|
||||
if not snapshot_path.exists():
|
||||
raise HTTPException(status_code=404, detail=f"Run snapshot not found: {run_id}")
|
||||
return json.loads(snapshot_path.read_text(encoding="utf-8"))
|
||||
|
||||
|
||||
def _copy_path_if_exists(src: Path, dst: Path) -> None:
|
||||
if not src.exists():
|
||||
return
|
||||
if src.is_dir():
|
||||
shutil.copytree(src, dst, dirs_exist_ok=True)
|
||||
else:
|
||||
dst.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copy2(src, dst)
|
||||
|
||||
|
||||
def _restore_run_assets(source_run_id: str, target_run_dir: Path) -> None:
|
||||
"""Seed a fresh run directory from a historical run snapshot."""
|
||||
source_run_dir = _get_run_dir(source_run_id)
|
||||
if not source_run_dir.exists():
|
||||
raise HTTPException(status_code=404, detail=f"Source run not found: {source_run_id}")
|
||||
|
||||
for relative in [
|
||||
"team_dashboard",
|
||||
"agents",
|
||||
"skills",
|
||||
"memory",
|
||||
"state/server_state.json",
|
||||
"state/runtime.db",
|
||||
"state/research.db",
|
||||
]:
|
||||
_copy_path_if_exists(source_run_dir / relative, target_run_dir / relative)
|
||||
|
||||
|
||||
def _list_runs(limit: int = 50) -> list[RuntimeHistoryItem]:
|
||||
runs_root = PROJECT_ROOT / "runs"
|
||||
if not runs_root.exists():
|
||||
return []
|
||||
|
||||
items: list[RuntimeHistoryItem] = []
|
||||
run_dirs = sorted(
|
||||
[path for path in runs_root.iterdir() if path.is_dir()],
|
||||
key=lambda path: path.stat().st_mtime,
|
||||
reverse=True,
|
||||
)
|
||||
|
||||
for run_dir in run_dirs[: max(1, int(limit))]:
|
||||
run_id = run_dir.name
|
||||
runtime_state_path = run_dir / "state" / "runtime_state.json"
|
||||
summary_path = run_dir / "team_dashboard" / "summary.json"
|
||||
|
||||
bootstrap: Dict[str, Any] = {}
|
||||
updated_at: Optional[str] = None
|
||||
total_trades = 0
|
||||
total_asset_value: Optional[float] = None
|
||||
|
||||
if runtime_state_path.exists():
|
||||
try:
|
||||
snapshot = json.loads(runtime_state_path.read_text(encoding="utf-8"))
|
||||
context = snapshot.get("context") or {}
|
||||
bootstrap = dict(context.get("bootstrap_values") or {})
|
||||
updated_at = snapshot.get("events", [{}])[-1].get("timestamp") if snapshot.get("events") else None
|
||||
except Exception:
|
||||
bootstrap = {}
|
||||
|
||||
if summary_path.exists():
|
||||
try:
|
||||
summary = json.loads(summary_path.read_text(encoding="utf-8"))
|
||||
total_trades = int(summary.get("totalTrades") or 0)
|
||||
total_asset_value = float(summary.get("totalAssetValue")) if summary.get("totalAssetValue") is not None else None
|
||||
except Exception:
|
||||
total_trades = 0
|
||||
total_asset_value = None
|
||||
|
||||
items.append(
|
||||
RuntimeHistoryItem(
|
||||
run_id=run_id,
|
||||
run_dir=str(run_dir),
|
||||
updated_at=updated_at,
|
||||
total_trades=total_trades,
|
||||
total_asset_value=total_asset_value,
|
||||
bootstrap=bootstrap,
|
||||
)
|
||||
)
|
||||
|
||||
return items
|
||||
|
||||
|
||||
def _is_timestamped_run_dir(path: Path) -> bool:
|
||||
try:
|
||||
datetime.strptime(path.name, "%Y%m%d_%H%M%S")
|
||||
return True
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
|
||||
def _prune_old_timestamped_runs(*, keep: int = 20, exclude_run_ids: Optional[set[str]] = None) -> list[str]:
|
||||
"""Prune old timestamped run directories, preserving the newest N and excluded ids."""
|
||||
exclude = exclude_run_ids or set()
|
||||
runs_root = PROJECT_ROOT / "runs"
|
||||
if not runs_root.exists():
|
||||
return []
|
||||
|
||||
candidates = sorted(
|
||||
[
|
||||
path
|
||||
for path in runs_root.iterdir()
|
||||
if path.is_dir() and _is_timestamped_run_dir(path) and path.name not in exclude
|
||||
],
|
||||
key=lambda path: path.name,
|
||||
reverse=True,
|
||||
)
|
||||
|
||||
pruned: list[str] = []
|
||||
for path in candidates[max(0, keep):]:
|
||||
shutil.rmtree(path, ignore_errors=True)
|
||||
pruned.append(path.name)
|
||||
return pruned
|
||||
|
||||
|
||||
def _find_available_port(start_port: int = 8765, max_port: int = 9000) -> int:
|
||||
"""Find an available port for Gateway."""
|
||||
import socket
|
||||
for port in range(start_port, max_port):
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||
if s.connect_ex(('localhost', port)) != 0:
|
||||
return port
|
||||
raise RuntimeError("No available port found")
|
||||
|
||||
|
||||
def _is_gateway_running() -> bool:
|
||||
"""Check if Gateway process is running.
|
||||
|
||||
Checks both the internally-managed gateway process and falls back to
|
||||
port availability (for externally-managed gateway processes).
|
||||
"""
|
||||
process = _runtime_state.gateway_process
|
||||
if process is not None and process.poll() is None:
|
||||
return True
|
||||
# Fallback: check if the gateway port is in use (for externally started gateway)
|
||||
import socket
|
||||
try:
|
||||
with socket.create_connection(("127.0.0.1", _runtime_state.gateway_port), timeout=1):
|
||||
return True
|
||||
except OSError:
|
||||
return False
|
||||
|
||||
|
||||
def _stop_gateway() -> bool:
|
||||
"""Stop the Gateway process."""
|
||||
process = _runtime_state.gateway_process
|
||||
if process is None:
|
||||
return False
|
||||
|
||||
try:
|
||||
# Try graceful shutdown first
|
||||
process.terminate()
|
||||
try:
|
||||
process.wait(timeout=5)
|
||||
except subprocess.TimeoutExpired:
|
||||
# Force kill if graceful shutdown fails
|
||||
process.kill()
|
||||
process.wait()
|
||||
except Exception as e:
|
||||
logger.warning(f"Error during gateway shutdown: {e}")
|
||||
finally:
|
||||
_runtime_state.gateway_process = None
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def _start_gateway_process(
|
||||
run_id: str,
|
||||
run_dir: Path,
|
||||
bootstrap: Dict[str, Any],
|
||||
port: int
|
||||
) -> subprocess.Popen:
|
||||
"""Start Gateway as a separate process."""
|
||||
# Prepare environment
|
||||
env = os.environ.copy()
|
||||
|
||||
# Create command arguments
|
||||
cmd = [
|
||||
sys.executable,
|
||||
"-m", "backend.gateway_server",
|
||||
"--run-id", run_id,
|
||||
"--run-dir", str(run_dir),
|
||||
"--port", str(port),
|
||||
"--bootstrap", json.dumps(bootstrap)
|
||||
]
|
||||
|
||||
log_path = run_dir / "logs" / "gateway.log"
|
||||
log_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
log_file = log_path.open("ab")
|
||||
try:
|
||||
process = subprocess.Popen(
|
||||
cmd,
|
||||
env=env,
|
||||
stdout=log_file,
|
||||
stderr=subprocess.STDOUT,
|
||||
cwd=PROJECT_ROOT
|
||||
)
|
||||
finally:
|
||||
log_file.close()
|
||||
|
||||
return process
|
||||
|
||||
|
||||
@router.get("/context", response_model=RunContextResponse)
|
||||
async def get_run_context() -> RunContextResponse:
|
||||
"""Return active runtime context, or latest persisted context when stopped."""
|
||||
snapshot = _get_active_runtime_snapshot() if _is_gateway_running() else _load_latest_runtime_snapshot()
|
||||
context = snapshot.get("context")
|
||||
if context is None:
|
||||
raise HTTPException(status_code=404, detail="Run context is not ready")
|
||||
|
||||
return RunContextResponse(
|
||||
config_name=context["config_name"],
|
||||
run_dir=context["run_dir"],
|
||||
bootstrap_values=context["bootstrap_values"],
|
||||
)
|
||||
|
||||
|
||||
@router.get("/agents", response_model=RuntimeAgentsResponse)
|
||||
async def get_runtime_agents() -> RuntimeAgentsResponse:
|
||||
"""Return agent states from the active runtime, or latest persisted run."""
|
||||
snapshot = _get_active_runtime_snapshot() if _is_gateway_running() else _load_latest_runtime_snapshot()
|
||||
agents = snapshot.get("agents", [])
|
||||
|
||||
return RuntimeAgentsResponse(
|
||||
agents=[RuntimeAgentState(**a) for a in agents]
|
||||
)
|
||||
|
||||
|
||||
@router.get("/events", response_model=RuntimeEventsResponse)
|
||||
async def get_runtime_events() -> RuntimeEventsResponse:
|
||||
"""Return events from the active runtime, or latest persisted run."""
|
||||
snapshot = _get_active_runtime_snapshot() if _is_gateway_running() else _load_latest_runtime_snapshot()
|
||||
events = snapshot.get("events", [])
|
||||
|
||||
return RuntimeEventsResponse(
|
||||
events=[RuntimeEvent(**e) for e in events]
|
||||
)
|
||||
|
||||
|
||||
@router.get("/history", response_model=RuntimeHistoryResponse)
|
||||
async def get_runtime_history(limit: int = 20) -> RuntimeHistoryResponse:
|
||||
"""List recent historical runs for restore/start selection."""
|
||||
return RuntimeHistoryResponse(runs=_list_runs(limit=limit))
|
||||
|
||||
|
||||
@router.get("/gateway/status", response_model=GatewayStatusResponse)
|
||||
async def get_gateway_status() -> GatewayStatusResponse:
|
||||
"""Get Gateway process status and port."""
|
||||
is_running = _is_gateway_running()
|
||||
run_id = None
|
||||
|
||||
if is_running:
|
||||
try:
|
||||
run_id = _get_active_runtime_context().get("config_name")
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to resolve active runtime context: {e}")
|
||||
|
||||
return GatewayStatusResponse(
|
||||
is_running=is_running,
|
||||
port=_runtime_state.gateway_port,
|
||||
run_id=run_id
|
||||
)
|
||||
|
||||
|
||||
@router.get("/gateway/port")
|
||||
async def get_gateway_port(request: Request) -> Dict[str, Any]:
|
||||
"""Get WebSocket Gateway port for frontend connection."""
|
||||
gateway_port = _runtime_state.gateway_port
|
||||
return {
|
||||
"port": gateway_port,
|
||||
"is_running": _is_gateway_running(),
|
||||
"ws_url": _build_gateway_ws_url(request, gateway_port),
|
||||
}
|
||||
|
||||
|
||||
@router.get("/logs", response_model=RuntimeLogResponse)
|
||||
async def get_runtime_logs() -> RuntimeLogResponse:
|
||||
"""Return current runtime log tail, or the latest run log if runtime is stopped."""
|
||||
try:
|
||||
context = _get_active_runtime_context() if _is_gateway_running() else _get_runtime_context_from_latest_snapshot()
|
||||
except HTTPException:
|
||||
return RuntimeLogResponse(is_running=False, content="")
|
||||
|
||||
run_id = str(context.get("config_name") or "").strip() or None
|
||||
log_path = _get_gateway_log_path_for_run(run_id) if run_id else None
|
||||
content = _read_log_tail(log_path) if log_path else ""
|
||||
|
||||
return RuntimeLogResponse(
|
||||
run_id=run_id,
|
||||
is_running=_is_gateway_running(),
|
||||
log_path=str(log_path) if log_path else None,
|
||||
content=content,
|
||||
)
|
||||
|
||||
|
||||
def _build_gateway_ws_url(request: Request, port: int) -> str:
|
||||
"""Build a proxy-safe Gateway WebSocket URL."""
|
||||
forwarded_proto = request.headers.get("x-forwarded-proto", "").split(",")[0].strip()
|
||||
scheme = forwarded_proto or request.url.scheme
|
||||
ws_scheme = "wss" if scheme == "https" else "ws"
|
||||
|
||||
forwarded_host = request.headers.get("x-forwarded-host", "").split(",")[0].strip()
|
||||
host = forwarded_host or request.url.hostname or "localhost"
|
||||
if ":" in host and not host.startswith("["):
|
||||
host = host.split(":", 1)[0]
|
||||
|
||||
return f"{ws_scheme}://{host}:{port}"
|
||||
|
||||
|
||||
def _load_latest_runtime_snapshot() -> Dict[str, Any]:
|
||||
"""Load the latest persisted runtime snapshot."""
|
||||
snapshots = sorted(
|
||||
PROJECT_ROOT.glob("runs/*/state/runtime_state.json"),
|
||||
key=lambda p: p.stat().st_mtime,
|
||||
reverse=True,
|
||||
)
|
||||
if not snapshots:
|
||||
raise HTTPException(status_code=404, detail="No runtime information available")
|
||||
return json.loads(snapshots[0].read_text(encoding="utf-8"))
|
||||
|
||||
|
||||
def _get_active_runtime_snapshot() -> Dict[str, Any]:
|
||||
"""Return the active runtime snapshot, preferring in-memory manager state."""
|
||||
if not _is_gateway_running():
|
||||
raise HTTPException(status_code=404, detail="No runtime is currently running")
|
||||
|
||||
manager = _runtime_state.runtime_manager
|
||||
if manager is not None and hasattr(manager, "build_snapshot"):
|
||||
snapshot = manager.build_snapshot()
|
||||
context = snapshot.get("context") or {}
|
||||
if context.get("config_name"):
|
||||
return snapshot
|
||||
|
||||
return _load_latest_runtime_snapshot()
|
||||
|
||||
|
||||
def _get_runtime_context_from_latest_snapshot() -> Dict[str, Any]:
|
||||
"""Return the latest persisted runtime context regardless of active process state."""
|
||||
latest = _load_latest_runtime_snapshot()
|
||||
context = latest.get("context") or {}
|
||||
if not context.get("config_name"):
|
||||
raise HTTPException(status_code=404, detail="No runtime context available")
|
||||
return context
|
||||
|
||||
|
||||
def _get_gateway_log_path_for_run(run_id: str) -> Path:
|
||||
return _get_run_dir(run_id) / "logs" / "gateway.log"
|
||||
|
||||
|
||||
def _read_log_tail(path: Path, max_chars: int = 120_000) -> str:
|
||||
if not path.exists() or not path.is_file():
|
||||
return ""
|
||||
text = path.read_text(encoding="utf-8", errors="replace")
|
||||
if len(text) <= max_chars:
|
||||
return text
|
||||
return text[-max_chars:]
|
||||
|
||||
|
||||
def _get_current_runtime_context() -> Dict[str, Any]:
|
||||
"""Return the active runtime context from the latest snapshot."""
|
||||
if not _is_gateway_running():
|
||||
raise HTTPException(status_code=404, detail="No runtime is currently running")
|
||||
snapshot = _get_active_runtime_snapshot()
|
||||
context = snapshot.get("context") or {}
|
||||
if not context.get("config_name"):
|
||||
raise HTTPException(status_code=404, detail="No runtime context available")
|
||||
return context
|
||||
|
||||
|
||||
def _get_active_runtime_context() -> Dict[str, Any]:
|
||||
"""Return the active runtime context, preferring in-memory runtime manager state."""
|
||||
return _get_current_runtime_context()
|
||||
|
||||
|
||||
def _resolve_runtime_response(run_id: str) -> RuntimeConfigResponse:
|
||||
"""Build a normalized runtime config response for the active run."""
|
||||
context = _get_current_runtime_context()
|
||||
bootstrap = dict(context.get("bootstrap_values") or {})
|
||||
resolved = resolve_runtime_config(
|
||||
project_root=PROJECT_ROOT,
|
||||
config_name=run_id,
|
||||
enable_memory=bool(bootstrap.get("enable_memory", False)),
|
||||
schedule_mode=str(bootstrap.get("schedule_mode", "daily")),
|
||||
interval_minutes=int(bootstrap.get("interval_minutes", 60) or 60),
|
||||
trigger_time=str(bootstrap.get("trigger_time", "09:30") or "09:30"),
|
||||
)
|
||||
return RuntimeConfigResponse(
|
||||
run_id=run_id,
|
||||
is_running=True,
|
||||
gateway_port=_runtime_state.gateway_port,
|
||||
bootstrap=bootstrap,
|
||||
resolved=resolved,
|
||||
)
|
||||
|
||||
|
||||
def _normalize_runtime_config_updates(
|
||||
request: UpdateRuntimeConfigRequest,
|
||||
) -> Dict[str, Any]:
|
||||
"""Validate and normalize runtime config updates."""
|
||||
updates: Dict[str, Any] = {}
|
||||
|
||||
if request.schedule_mode is not None:
|
||||
schedule_mode = str(request.schedule_mode).strip().lower()
|
||||
if schedule_mode not in {"daily", "intraday"}:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="schedule_mode must be 'daily' or 'intraday'",
|
||||
)
|
||||
updates["schedule_mode"] = schedule_mode
|
||||
|
||||
if request.interval_minutes is not None:
|
||||
updates["interval_minutes"] = int(request.interval_minutes)
|
||||
|
||||
if request.trigger_time is not None:
|
||||
trigger_time = str(request.trigger_time).strip()
|
||||
if trigger_time and trigger_time != "now":
|
||||
try:
|
||||
datetime.strptime(trigger_time, "%H:%M")
|
||||
except ValueError as exc:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="trigger_time must use HH:MM or 'now'",
|
||||
) from exc
|
||||
updates["trigger_time"] = trigger_time or "09:30"
|
||||
|
||||
if request.max_comm_cycles is not None:
|
||||
updates["max_comm_cycles"] = int(request.max_comm_cycles)
|
||||
|
||||
if request.initial_cash is not None:
|
||||
updates["initial_cash"] = float(request.initial_cash)
|
||||
|
||||
if request.margin_requirement is not None:
|
||||
updates["margin_requirement"] = float(request.margin_requirement)
|
||||
|
||||
if request.enable_memory is not None:
|
||||
updates["enable_memory"] = bool(request.enable_memory)
|
||||
|
||||
if not updates:
|
||||
raise HTTPException(status_code=400, detail="No runtime config updates provided")
|
||||
|
||||
return updates
|
||||
|
||||
|
||||
@router.post("/start", response_model=LaunchResponse)
|
||||
async def start_runtime(
|
||||
config: LaunchConfig,
|
||||
background_tasks: BackgroundTasks
|
||||
) -> LaunchResponse:
|
||||
"""Start a new trading runtime with the given configuration.
|
||||
|
||||
1. Stop existing Gateway if running
|
||||
2. Generate run ID and directory
|
||||
3. Create runtime manager
|
||||
4. Start Gateway as subprocess (Data Plane)
|
||||
5. Return Gateway port for WebSocket connection
|
||||
"""
|
||||
# Lazy import to avoid circular dependency
|
||||
from backend.runtime.manager import TradingRuntimeManager
|
||||
|
||||
# 1. Stop existing Gateway
|
||||
if _is_gateway_running():
|
||||
_stop_gateway()
|
||||
await asyncio.sleep(1) # Wait for port release
|
||||
|
||||
launch_mode = str(config.launch_mode or "fresh").strip().lower()
|
||||
if launch_mode not in {"fresh", "restore"}:
|
||||
raise HTTPException(status_code=400, detail="launch_mode must be 'fresh' or 'restore'")
|
||||
|
||||
# 2. Resolve run ID, directory, and bootstrap
|
||||
if launch_mode == "restore":
|
||||
restore_run_id = str(config.restore_run_id or "").strip()
|
||||
if not restore_run_id:
|
||||
raise HTTPException(status_code=400, detail="restore_run_id is required when launch_mode=restore")
|
||||
snapshot = _load_run_snapshot(restore_run_id)
|
||||
context = snapshot.get("context") or {}
|
||||
if not context.get("config_name"):
|
||||
raise HTTPException(status_code=404, detail=f"Run context not found: {restore_run_id}")
|
||||
run_id = restore_run_id
|
||||
run_dir = _get_run_dir(run_id)
|
||||
bootstrap = dict(context.get("bootstrap_values") or {})
|
||||
bootstrap["launch_mode"] = "restore"
|
||||
bootstrap["restore_run_id"] = restore_run_id
|
||||
else:
|
||||
run_id = _generate_run_id()
|
||||
run_dir = _get_run_dir(run_id)
|
||||
bootstrap = {
|
||||
"launch_mode": "fresh",
|
||||
"restore_run_id": None,
|
||||
"tickers": config.tickers,
|
||||
"schedule_mode": config.schedule_mode,
|
||||
"interval_minutes": config.interval_minutes,
|
||||
"trigger_time": config.trigger_time,
|
||||
"max_comm_cycles": config.max_comm_cycles,
|
||||
"initial_cash": config.initial_cash,
|
||||
"margin_requirement": config.margin_requirement,
|
||||
"enable_memory": config.enable_memory,
|
||||
"mode": config.mode,
|
||||
"start_date": config.start_date,
|
||||
"end_date": config.end_date,
|
||||
"poll_interval": config.poll_interval,
|
||||
}
|
||||
|
||||
retention_keep = max(1, int(os.getenv("RUNS_RETENTION_COUNT", "20") or "20"))
|
||||
pruned_run_ids = _prune_old_timestamped_runs(
|
||||
keep=retention_keep,
|
||||
exclude_run_ids={run_id},
|
||||
)
|
||||
if pruned_run_ids:
|
||||
logger.info("Pruned old run directories: %s", ", ".join(pruned_run_ids))
|
||||
|
||||
# 4. Create runtime manager
|
||||
manager = TradingRuntimeManager(
|
||||
config_name=run_id,
|
||||
run_dir=run_dir,
|
||||
bootstrap=bootstrap,
|
||||
)
|
||||
manager.prepare_run()
|
||||
register_runtime_manager(manager)
|
||||
|
||||
# 5. Write BOOTSTRAP.md
|
||||
_write_bootstrap_md(run_dir, bootstrap)
|
||||
|
||||
# 6. Find available port and start Gateway process
|
||||
gateway_port = _find_available_port(start_port=8765)
|
||||
_runtime_state.gateway_port = gateway_port
|
||||
|
||||
try:
|
||||
process = _start_gateway_process(
|
||||
run_id=run_id,
|
||||
run_dir=run_dir,
|
||||
bootstrap=bootstrap,
|
||||
port=gateway_port
|
||||
)
|
||||
_runtime_state.gateway_process = process
|
||||
|
||||
# Wait briefly to check if process started successfully
|
||||
await asyncio.sleep(2)
|
||||
|
||||
if not _is_gateway_running():
|
||||
_runtime_state.gateway_process = None
|
||||
log_path = _get_gateway_log_path_for_run(run_id)
|
||||
log_tail = _read_log_tail(log_path, max_chars=4000)
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail=f"Gateway failed to start: {log_tail or 'Unknown error'}"
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
_stop_gateway()
|
||||
raise HTTPException(status_code=500, detail=f"Failed to start Gateway: {str(e)}")
|
||||
|
||||
return LaunchResponse(
|
||||
run_id=run_id,
|
||||
status="started",
|
||||
run_dir=str(run_dir),
|
||||
gateway_port=gateway_port,
|
||||
message=f"Runtime started with run_id: {run_id}, Gateway on port: {gateway_port}",
|
||||
)
|
||||
|
||||
|
||||
@router.get("/config", response_model=RuntimeConfigResponse)
|
||||
async def get_runtime_config() -> RuntimeConfigResponse:
|
||||
"""Return the current runtime bootstrap and resolved settings."""
|
||||
context = _get_current_runtime_context()
|
||||
return _resolve_runtime_response(context["config_name"])
|
||||
|
||||
|
||||
@router.put("/config", response_model=RuntimeConfigResponse)
|
||||
async def update_runtime_config(
|
||||
request: UpdateRuntimeConfigRequest,
|
||||
) -> RuntimeConfigResponse:
|
||||
"""Persist selected runtime configuration updates for the active run."""
|
||||
context = _get_current_runtime_context()
|
||||
run_id = context["config_name"]
|
||||
updates = _normalize_runtime_config_updates(request)
|
||||
updated = update_bootstrap_values_for_run(PROJECT_ROOT, run_id, updates)
|
||||
|
||||
manager = _runtime_state.runtime_manager
|
||||
if manager is not None and getattr(manager, "config_name", None) == run_id:
|
||||
manager.bootstrap.update(updates)
|
||||
if getattr(manager, "context", None) is not None:
|
||||
manager.context.bootstrap_values.update(updates)
|
||||
if hasattr(manager, "_persist_snapshot"):
|
||||
manager._persist_snapshot()
|
||||
|
||||
response = _resolve_runtime_response(run_id)
|
||||
response.bootstrap = dict(updated.values)
|
||||
return response
|
||||
|
||||
|
||||
@router.post("/stop", response_model=StopResponse)
|
||||
async def stop_runtime(force: bool = True) -> StopResponse:
|
||||
"""Stop the current running runtime."""
|
||||
was_running = _is_gateway_running()
|
||||
|
||||
if not was_running:
|
||||
raise HTTPException(status_code=404, detail="No runtime is currently running")
|
||||
|
||||
# Stop Gateway process
|
||||
_stop_gateway()
|
||||
|
||||
# Unregister runtime manager
|
||||
unregister_runtime_manager()
|
||||
|
||||
return StopResponse(
|
||||
status="stopped",
|
||||
message="Runtime stopped successfully",
|
||||
)
|
||||
|
||||
|
||||
@router.post("/cleanup", response_model=CleanupResponse)
|
||||
async def cleanup_old_runs(keep: int = 20) -> CleanupResponse:
|
||||
"""Prune old timestamped run directories while preserving named runs."""
|
||||
keep_count = max(1, int(keep))
|
||||
exclude: set[str] = set()
|
||||
|
||||
if _is_gateway_running():
|
||||
try:
|
||||
active_context = _get_active_runtime_context()
|
||||
active_run_id = str(active_context.get("config_name") or "").strip()
|
||||
if active_run_id:
|
||||
exclude.add(active_run_id)
|
||||
except HTTPException:
|
||||
pass
|
||||
|
||||
pruned = _prune_old_timestamped_runs(keep=keep_count, exclude_run_ids=exclude)
|
||||
return CleanupResponse(status="ok", kept=keep_count, pruned_run_ids=pruned)
|
||||
|
||||
|
||||
@router.post("/restart")
|
||||
async def restart_runtime(
|
||||
config: LaunchConfig,
|
||||
background_tasks: BackgroundTasks
|
||||
):
|
||||
"""Restart the runtime with a new configuration."""
|
||||
# Stop current runtime
|
||||
await stop_runtime(force=True)
|
||||
|
||||
# Start new runtime
|
||||
response = await start_runtime(config, background_tasks)
|
||||
|
||||
return {
|
||||
"run_id": response.run_id,
|
||||
"status": "restarted",
|
||||
"gateway_port": response.gateway_port,
|
||||
"message": f"Runtime restarted with run_id: {response.run_id}",
|
||||
}
|
||||
|
||||
|
||||
@router.get("/current")
|
||||
async def get_current_runtime():
|
||||
"""Get information about the currently running runtime."""
|
||||
if not _is_gateway_running():
|
||||
raise HTTPException(status_code=404, detail="No runtime is currently running")
|
||||
|
||||
context = _get_active_runtime_context()
|
||||
|
||||
return {
|
||||
"run_id": context.get("config_name"),
|
||||
"run_dir": context.get("run_dir"),
|
||||
"is_running": True,
|
||||
"gateway_port": _runtime_state.gateway_port,
|
||||
"bootstrap": context.get("bootstrap_values", {}),
|
||||
}
|
||||
|
||||
|
||||
def register_runtime_manager(manager: Any) -> None:
|
||||
"""Allow other modules to expose the runtime manager to the API."""
|
||||
global runtime_manager
|
||||
runtime_manager = manager
|
||||
# Also update the RuntimeState singleton for internal consistency
|
||||
_runtime_state.runtime_manager = manager
|
||||
|
||||
|
||||
def unregister_runtime_manager() -> None:
|
||||
"""Drop the runtime manager reference."""
|
||||
global runtime_manager
|
||||
runtime_manager = None
|
||||
# Also update the RuntimeState singleton for internal consistency
|
||||
_runtime_state.runtime_manager = None
|
||||
|
||||
|
||||
def _write_bootstrap_md(run_dir: Path, bootstrap: Dict[str, Any]) -> None:
|
||||
"""Write bootstrap configuration to BOOTSTRAP.md."""
|
||||
try:
|
||||
import yaml
|
||||
except ImportError:
|
||||
yaml = None
|
||||
|
||||
bootstrap_path = run_dir / "BOOTSTRAP.md"
|
||||
bootstrap_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# Filter out None values
|
||||
values = {k: v for k, v in bootstrap.items() if v is not None}
|
||||
|
||||
if yaml:
|
||||
front_matter = yaml.safe_dump(values, allow_unicode=True, sort_keys=False)
|
||||
else:
|
||||
front_matter = json.dumps(values, ensure_ascii=False, indent=2)
|
||||
|
||||
content = f"---\n{front_matter}---\n"
|
||||
bootstrap_path.write_text(content, encoding="utf-8")
|
||||
196
backend/api/workspaces.py
Normal file
196
backend/api/workspaces.py
Normal file
@@ -0,0 +1,196 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Workspace API Routes
|
||||
|
||||
Provides REST API endpoints for workspace management.
|
||||
"""
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Depends
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from backend.agents import WorkspaceManager
|
||||
|
||||
router = APIRouter(prefix="/api/workspaces", tags=["workspaces"])
|
||||
|
||||
|
||||
# Request/Response Models
|
||||
class CreateWorkspaceRequest(BaseModel):
|
||||
"""Request to create a new workspace."""
|
||||
workspace_id: str = Field(..., description="Unique workspace identifier")
|
||||
name: Optional[str] = Field(None, description="Display name")
|
||||
description: Optional[str] = Field(None, description="Workspace description")
|
||||
metadata: Optional[Dict[str, Any]] = Field(None, description="Additional metadata")
|
||||
|
||||
|
||||
class UpdateWorkspaceRequest(BaseModel):
|
||||
"""Request to update a workspace."""
|
||||
name: Optional[str] = None
|
||||
description: Optional[str] = None
|
||||
metadata: Optional[Dict[str, Any]] = None
|
||||
|
||||
|
||||
class WorkspaceResponse(BaseModel):
|
||||
"""Workspace information response."""
|
||||
workspace_id: str
|
||||
name: str
|
||||
description: str
|
||||
created_at: Optional[str] = None
|
||||
metadata: Dict[str, Any] = Field(default_factory=dict)
|
||||
|
||||
|
||||
class WorkspaceListResponse(BaseModel):
|
||||
"""List of workspaces response."""
|
||||
workspaces: List[WorkspaceResponse]
|
||||
total: int
|
||||
|
||||
|
||||
# Dependencies
|
||||
def get_workspace_manager():
|
||||
"""Get WorkspaceManager instance."""
|
||||
return WorkspaceManager()
|
||||
|
||||
|
||||
# Routes
|
||||
@router.post("", response_model=WorkspaceResponse)
|
||||
async def create_workspace(
|
||||
request: CreateWorkspaceRequest,
|
||||
manager: WorkspaceManager = Depends(get_workspace_manager),
|
||||
):
|
||||
"""
|
||||
Create a new workspace.
|
||||
|
||||
Args:
|
||||
request: Workspace creation parameters
|
||||
|
||||
Returns:
|
||||
Created workspace information
|
||||
"""
|
||||
try:
|
||||
config = manager.create_workspace(
|
||||
workspace_id=request.workspace_id,
|
||||
name=request.name,
|
||||
description=request.description,
|
||||
metadata=request.metadata or {},
|
||||
)
|
||||
return WorkspaceResponse(
|
||||
workspace_id=config.workspace_id,
|
||||
name=config.name,
|
||||
description=config.description,
|
||||
created_at=config.created_at,
|
||||
metadata=config.metadata,
|
||||
)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
|
||||
@router.get("", response_model=WorkspaceListResponse)
|
||||
async def list_workspaces(
|
||||
manager: WorkspaceManager = Depends(get_workspace_manager),
|
||||
):
|
||||
"""
|
||||
List all workspaces.
|
||||
|
||||
Returns:
|
||||
List of workspaces
|
||||
"""
|
||||
workspaces = manager.list_workspaces()
|
||||
return WorkspaceListResponse(
|
||||
workspaces=[
|
||||
WorkspaceResponse(
|
||||
workspace_id=ws.workspace_id,
|
||||
name=ws.name,
|
||||
description=ws.description,
|
||||
created_at=ws.created_at,
|
||||
metadata=ws.metadata,
|
||||
)
|
||||
for ws in workspaces
|
||||
],
|
||||
total=len(workspaces),
|
||||
)
|
||||
|
||||
|
||||
@router.get("/{workspace_id}", response_model=WorkspaceResponse)
|
||||
async def get_workspace(
|
||||
workspace_id: str,
|
||||
manager: WorkspaceManager = Depends(get_workspace_manager),
|
||||
):
|
||||
"""
|
||||
Get workspace details.
|
||||
|
||||
Args:
|
||||
workspace_id: Workspace identifier
|
||||
|
||||
Returns:
|
||||
Workspace information
|
||||
"""
|
||||
workspace = manager.get_workspace(workspace_id)
|
||||
if not workspace:
|
||||
raise HTTPException(status_code=404, detail=f"Workspace '{workspace_id}' not found")
|
||||
|
||||
return WorkspaceResponse(
|
||||
workspace_id=workspace["workspace_id"],
|
||||
name=workspace.get("name", workspace_id),
|
||||
description=workspace.get("description", ""),
|
||||
created_at=workspace.get("created_at"),
|
||||
metadata=workspace.get("metadata", {}),
|
||||
)
|
||||
|
||||
|
||||
@router.patch("/{workspace_id}", response_model=WorkspaceResponse)
|
||||
async def update_workspace(
|
||||
workspace_id: str,
|
||||
request: UpdateWorkspaceRequest,
|
||||
manager: WorkspaceManager = Depends(get_workspace_manager),
|
||||
):
|
||||
"""
|
||||
Update workspace configuration.
|
||||
|
||||
Args:
|
||||
workspace_id: Workspace identifier
|
||||
request: Update parameters
|
||||
|
||||
Returns:
|
||||
Updated workspace information
|
||||
"""
|
||||
try:
|
||||
config = manager.update_workspace_config(
|
||||
workspace_id=workspace_id,
|
||||
name=request.name,
|
||||
description=request.description,
|
||||
metadata=request.metadata,
|
||||
)
|
||||
return WorkspaceResponse(
|
||||
workspace_id=config.workspace_id,
|
||||
name=config.name,
|
||||
description=config.description,
|
||||
created_at=config.created_at,
|
||||
metadata=config.metadata,
|
||||
)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=404, detail=str(e))
|
||||
|
||||
|
||||
@router.delete("/{workspace_id}")
|
||||
async def delete_workspace(
|
||||
workspace_id: str,
|
||||
force: bool = False,
|
||||
manager: WorkspaceManager = Depends(get_workspace_manager),
|
||||
):
|
||||
"""
|
||||
Delete a workspace.
|
||||
|
||||
Args:
|
||||
workspace_id: Workspace identifier
|
||||
force: If True, delete even if workspace has agents
|
||||
|
||||
Returns:
|
||||
Success message
|
||||
"""
|
||||
try:
|
||||
success = manager.delete_workspace(workspace_id, force=force)
|
||||
if not success:
|
||||
raise HTTPException(status_code=404, detail=f"Workspace '{workspace_id}' not found")
|
||||
return {"message": f"Workspace '{workspace_id}' deleted successfully"}
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
Reference in New Issue
Block a user