Buckets:

download
raw
41.9 kB
""" R CLI API Server - FastAPI daemon for R Agent Runtime. Usage: r serve # Start API server r serve --port 8080 # Custom port r serve --host 0.0.0.0 # Listen on all interfaces """ from __future__ import annotations import asyncio import os import time import uuid from contextlib import asynccontextmanager from datetime import datetime from typing import AsyncGenerator, Optional from fastapi import Depends, FastAPI, HTTPException, Request, status from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import JSONResponse, StreamingResponse from fastapi.staticfiles import StaticFiles from pydantic import BaseModel from r_cli import __version__ from r_cli.agent_os import AgentOS from r_cli.api.audit import ( AuditAction, AuditSeverity, audit_log, get_audit_logger, ) from r_cli.api.auth import ( ACCESS_TOKEN_EXPIRE_MINUTES, AuthResult, Token, authenticate_user, create_access_token, get_current_auth, get_storage, require_auth, require_scopes, ) from r_cli.api.models import ( AgentOSStatus, AgentTaskSummary, CapabilityDomainSummary, ChatChoice, ChatMessage, ChatRequest, ChatResponse, ChatStreamChoice, ChatStreamDelta, ChatStreamResponse, ChatUsage, ControlCenterResponse, HealthStatus, InstalledAgentSummary, LLMStatus, MemoryOverview, SecurityOverview, SkillInfo, SkillsResponse, StatusResponse, ToolCallRequest, ToolCallResponse, ToolInfo, ToolParameter, ) from r_cli.api.permissions import ( PermissionChecker, Scope, check_skill_permission, ) from r_cli.api.rate_limit import RateLimitMiddleware from r_cli.core.agent import Agent from r_cli.core.config import Config from r_cli.core.permissions import PermissionDeniedError, PermissionManager # Global state _agent: Optional[Agent] = None _start_time: float = 0 _config: Optional[Config] = None # Auth mode: "none", "optional", "required" AUTH_MODE = os.getenv("R_AUTH_MODE", "optional") CAPABILITY_DOMAINS = { "Knowledge & Docs": { "icon": "📚", "skills": { "pdf", "pdftools", "markdown", "latex", "template", "resume", "ocr", "rag", "text", }, }, "Code & Data": { "icon": "💻", "skills": { "code", "sql", "json", "yaml", "csv", "schema", "diff", "regex", "semver", "faker", }, }, "Automation & OS": { "icon": "⚙️", "skills": { "fs", "archive", "git", "system", "docker", "ssh", "cron", "env", "metrics", "logs", }, }, "Web & Network": { "icon": "🌐", "skills": { "web", "http", "network", "url", "ip", "rss", "sitemap", "openapi", "weather", "currency", }, }, "Media & Voice": { "icon": "🎙️", "skills": { "voice", "realtime_voice", "image", "imagegen", "screenshot", "audio", "video", "qr", "barcode", "color", }, }, "Communication": { "icon": "📨", "skills": { "email", "calendar", "ical", "vcard", "social", "autoresponder", }, }, "Agent Systems": { "icon": "🧠", "skills": { "multiagent", "distributed_ai", "p2p", "agimemory", "agimemory_pg", "plugin", "manifest", "msoffice", }, }, } def get_agent() -> Agent: """Get or create the global agent instance.""" if _agent is None: raise HTTPException(status_code=503, detail="Agent not initialized") return _agent def _build_status_response() -> StatusResponse: """Build the shared runtime status response.""" agent = get_agent() uptime = time.time() - _start_time llm_connected = agent.check_connection() llm_status = LLMStatus( connected=llm_connected, backend=_config.llm.provider if _config else None, model=_config.llm.model if _config else None, base_url=_config.llm.base_url if _config else None, ) health_status = HealthStatus.HEALTHY if llm_connected else HealthStatus.DEGRADED return StatusResponse( status=health_status, version=__version__, uptime_seconds=uptime, llm=llm_status, skills_loaded=len(agent.skills), timestamp=datetime.now(), ) def _resolve_capability_domain(skill_name: str) -> str: """Map a raw skill name to a product-level capability domain.""" for domain_name, metadata in CAPABILITY_DOMAINS.items(): if skill_name in metadata["skills"]: return domain_name return "Utilities" @asynccontextmanager async def lifespan(app: FastAPI): """Application lifespan manager.""" global _agent, _start_time, _config # Startup _start_time = time.time() _config = Config.load() # Create agent with all skills _agent = Agent(_config) _agent.load_skills() # Log server start audit_log( AuditAction.SERVER_STARTED, details={"version": __version__, "skills": len(_agent.skills)}, ) yield # Log server stop audit_log(AuditAction.SERVER_STOPPED) # Shutdown _agent = None def create_app() -> FastAPI: """Create and configure the FastAPI application.""" app = FastAPI( title="R CLI API", description="REST API for R Agent Runtime - Your Local AI Operating System", version=__version__, lifespan=lifespan, docs_url="/docs", redoc_url="/redoc", ) # CORS middleware app.add_middleware( CORSMiddleware, allow_origins=["*"], # Configure as needed allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Rate limiting middleware app.add_middleware(RateLimitMiddleware) # Register routes register_routes(app) # Mount static files for Web UI static_dir = os.path.join(os.path.dirname(__file__), "static") if os.path.exists(static_dir): app.mount("/ui", StaticFiles(directory=static_dir, html=True), name="static") return app # ============================================================================ # Auth Models # ============================================================================ class LoginRequest(BaseModel): """Login request.""" username: str password: str class CreateUserRequest(BaseModel): """Create user request.""" username: str password: str scopes: list[str] = ["read", "chat"] class CreateAPIKeyRequest(BaseModel): """Create API key request.""" name: str scopes: list[str] = ["read", "chat"] expires_in_days: Optional[int] = None class APIKeyResponse(BaseModel): """API key creation response (includes raw key only once).""" key: str # Raw key - only shown once key_id: str name: str scopes: list[str] expires_at: Optional[datetime] = None class APIKeyInfo(BaseModel): """API key info (without raw key).""" key_id: str name: str scopes: list[str] created_at: datetime last_used: Optional[datetime] = None expires_at: Optional[datetime] = None is_active: bool # ============================================================================ # Routes # ============================================================================ def register_routes(app: FastAPI) -> None: """Register all API routes.""" # ======================================================================== # Health & Status (Public) # ======================================================================== @app.get("/", tags=["Health"]) async def root(): """Root endpoint - basic health check.""" return { "message": "R CLI API", "version": __version__, "docs": "/docs", "ui": "/ui", "auth_mode": AUTH_MODE, } @app.get("/health", tags=["Health"]) async def health(): """Simple health check.""" return {"status": "ok"} @app.get("/v1/status", response_model=StatusResponse, tags=["Status"]) async def get_status(auth: AuthResult = Depends(get_current_auth)): """Get detailed server status.""" return _build_status_response() @app.get("/v1/control-center", response_model=ControlCenterResponse, tags=["Status"]) async def get_control_center(auth: AuthResult = Depends(get_current_auth)): """Get a product-level overview for the web Control Center.""" agent = get_agent() status_response = _build_status_response() checker = PermissionChecker(auth.scopes) if auth.authenticated else None visible_skills = [] for name, skill in agent.skills.items(): if checker and not checker.can_use_skill(name): continue visible_skills.append((name, skill)) runtime = AgentOS(_config or Config.load()) runtime_status = runtime.status() installed_agents = runtime.list_agents() domains: dict[str, dict[str, object]] = {} for domain_name, metadata in CAPABILITY_DOMAINS.items(): domains[domain_name] = { "name": domain_name, "icon": metadata["icon"], "skills": 0, "tools": 0, "highlights": [], } for skill_name, skill in visible_skills: domain_name = _resolve_capability_domain(skill_name) if domain_name not in domains: domains[domain_name] = { "name": domain_name, "icon": "🛠️", "skills": 0, "tools": 0, "highlights": [], } domain = domains[domain_name] domain["skills"] = int(domain["skills"]) + 1 domain["tools"] = int(domain["tools"]) + len(skill.get_tools()) highlights = list(domain["highlights"]) if len(highlights) < 4: highlights.append(skill_name) domain["highlights"] = highlights capability_domains = [ CapabilityDomainSummary(**domain) for domain in domains.values() if int(domain["skills"]) > 0 ] capability_domains.sort(key=lambda item: item.skills, reverse=True) return ControlCenterResponse( status=status_response, agent_os=AgentOSStatus( database=runtime_status["database"], agents=runtime_status["agents"], events=runtime_status["events"], tasks=AgentTaskSummary(**runtime_status["tasks"]), ), installed_agents=[ InstalledAgentSummary( name=item["name"], description=item["description"], kind=item["kind"], task_count=item["task_count"], completed=item["completed"], skills=len(item.get("skills") or []), network_access=bool(item.get("network_access", False)), ) for item in installed_agents[:8] ], capability_domains=capability_domains, memory=MemoryOverview( provider=_config.memory.provider if _config else "local", continuous=(_config.memory.provider == "gbrain") if _config else False, ), security=SecurityOverview( mode=_config.security.mode if _config else "ask", local_only=_config.security.local_only if _config else True, network_access=_config.security.network_access if _config else False, audit_enabled=_config.security.audit_enabled if _config else True, filesystem_roots_enforced=( _config.security.enforce_filesystem_roots if _config else False ), ), ) # ======================================================================== # Authentication # ======================================================================== @app.post("/auth/login", response_model=Token, tags=["Auth"]) async def login(request: LoginRequest, req: Request): """Login with username and password to get JWT token.""" user = authenticate_user(request.username, request.password) if not user: audit_log( AuditAction.AUTH_FAILED, username=request.username, client_ip=req.client.host if req.client else None, success=False, severity=AuditSeverity.WARNING, ) raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid username or password", headers={"WWW-Authenticate": "Bearer"}, ) # Create token access_token = create_access_token( data={ "sub": user.user_id, "username": user.username, "scopes": user.scopes, } ) audit_log( AuditAction.AUTH_LOGIN, user_id=user.user_id, username=user.username, client_ip=req.client.host if req.client else None, ) return Token( access_token=access_token, token_type="bearer", expires_in=ACCESS_TOKEN_EXPIRE_MINUTES * 60, ) @app.get("/auth/me", tags=["Auth"]) async def get_current_user(auth: AuthResult = Depends(require_auth)): """Get current authenticated user info.""" return { "user_id": auth.user_id, "username": auth.username, "auth_type": auth.auth_type, "scopes": auth.scopes, } @app.post("/auth/users", tags=["Auth"]) async def create_user( request: CreateUserRequest, auth: AuthResult = Depends(require_scopes(Scope.ADMIN)), ): """Create a new user (admin only).""" storage = get_storage() try: user = storage.create_user( username=request.username, password=request.password, scopes=request.scopes, ) audit_log( AuditAction.USER_CREATED, user_id=auth.user_id, username=auth.username, resource=request.username, ) return { "user_id": user.user_id, "username": user.username, "scopes": user.scopes, } except ValueError as e: raise HTTPException(status_code=400, detail=str(e)) @app.post("/auth/api-keys", response_model=APIKeyResponse, tags=["Auth"]) async def create_api_key( request: CreateAPIKeyRequest, auth: AuthResult = Depends(require_auth), ): """Create a new API key.""" storage = get_storage() # Users can only create keys with their own scopes or subset allowed_scopes = set(auth.scopes) if Scope.ADMIN not in allowed_scopes: requested_scopes = set(request.scopes) if not requested_scopes.issubset(allowed_scopes): raise HTTPException( status_code=403, detail="Cannot create API key with scopes you don't have", ) raw_key, api_key = storage.create_api_key( name=request.name, scopes=request.scopes, expires_in_days=request.expires_in_days, ) audit_log( AuditAction.API_KEY_CREATED, user_id=auth.user_id, username=auth.username, resource=api_key.key_id, details={"name": request.name, "scopes": request.scopes}, ) return APIKeyResponse( key=raw_key, key_id=api_key.key_id, name=api_key.name, scopes=api_key.scopes, expires_at=api_key.expires_at, ) @app.get("/auth/api-keys", response_model=list[APIKeyInfo], tags=["Auth"]) async def list_api_keys(auth: AuthResult = Depends(require_auth)): """List all API keys (admin) or own keys.""" storage = get_storage() keys = storage.list_api_keys() return [ APIKeyInfo( key_id=k.key_id, name=k.name, scopes=k.scopes, created_at=k.created_at, last_used=k.last_used, expires_at=k.expires_at, is_active=k.is_active, ) for k in keys ] @app.delete("/auth/api-keys/{key_id}", tags=["Auth"]) async def revoke_api_key( key_id: str, auth: AuthResult = Depends(require_auth), ): """Revoke an API key.""" storage = get_storage() if storage.revoke_api_key(key_id): audit_log( AuditAction.API_KEY_REVOKED, user_id=auth.user_id, username=auth.username, resource=key_id, ) return {"status": "revoked", "key_id": key_id} raise HTTPException(status_code=404, detail="API key not found") # ======================================================================== # Chat Completions # ======================================================================== @app.post("/v1/chat", tags=["Chat"]) async def chat_completions( request: ChatRequest, req: Request, auth: AuthResult = Depends(get_current_auth), ): """ Chat completion endpoint. Compatible with OpenAI's chat completions API. Supports streaming responses. """ # Check auth if required if AUTH_MODE == "required" and not auth.authenticated: raise HTTPException( status_code=401, detail="Authentication required", ) # Check chat permission if auth.authenticated: checker = PermissionChecker(auth.scopes) if not checker.can_chat(streaming=request.stream): raise HTTPException( status_code=403, detail="Missing chat permission", ) agent = get_agent() start_time = time.time() # Build conversation messages = [{"role": m.role, "content": m.content} for m in request.messages] # Get the last user message user_message = None for msg in reversed(messages): if msg["role"] == "user": user_message = msg["content"] break if not user_message: raise HTTPException(status_code=400, detail="No user message found") # Log the request audit_log( AuditAction.CHAT_REQUEST, user_id=auth.user_id if auth.authenticated else None, username=auth.username if auth.authenticated else None, auth_type=auth.auth_type if auth.authenticated else None, client_ip=req.client.host if req.client else None, details={"stream": request.stream, "message_length": len(user_message)}, ) # Generate response ID response_id = f"chatcmpl-{uuid.uuid4().hex[:12]}" created = int(time.time()) model = request.model or (_config.llm.model if _config else "unknown") if request.stream: return StreamingResponse( stream_chat_response( agent, user_message, response_id, created, model, auth, start_time ), media_type="text/event-stream", ) else: # Non-streaming response response_text = await asyncio.to_thread(agent.run, user_message) duration_ms = (time.time() - start_time) * 1000 audit_log( AuditAction.CHAT_RESPONSE, user_id=auth.user_id if auth.authenticated else None, username=auth.username if auth.authenticated else None, duration_ms=duration_ms, details={"response_length": len(response_text)}, ) return ChatResponse( id=response_id, created=created, model=model, choices=[ ChatChoice( index=0, message=ChatMessage(role="assistant", content=response_text), finish_reason="stop", ) ], usage=ChatUsage( prompt_tokens=len(user_message.split()), completion_tokens=len(response_text.split()), total_tokens=len(user_message.split()) + len(response_text.split()), ), ) async def stream_chat_response( agent: Agent, user_message: str, response_id: str, created: int, model: str, auth: AuthResult, start_time: float, ) -> AsyncGenerator[str, None]: """Generate streaming chat response.""" # Send role first initial_chunk = ChatStreamResponse( id=response_id, created=created, model=model, choices=[ ChatStreamChoice( index=0, delta=ChatStreamDelta(role="assistant"), ) ], ) yield f"data: {initial_chunk.model_dump_json()}\n\n" # Run sync generator in thread loop = asyncio.get_event_loop() queue: asyncio.Queue = asyncio.Queue() async def producer(): def run(): try: for chunk in agent.run_stream(user_message): asyncio.run_coroutine_threadsafe(queue.put(chunk), loop) asyncio.run_coroutine_threadsafe(queue.put(None), loop) except Exception as e: asyncio.run_coroutine_threadsafe(queue.put(e), loop) await asyncio.to_thread(run) # Start producer producer_task = asyncio.create_task(producer()) # Consume chunks total_content = "" while True: item = await queue.get() if item is None: break if isinstance(item, Exception): raise item total_content += item chunk_response = ChatStreamResponse( id=response_id, created=created, model=model, choices=[ ChatStreamChoice( index=0, delta=ChatStreamDelta(content=item), ) ], ) yield f"data: {chunk_response.model_dump_json()}\n\n" # Send finish final_chunk = ChatStreamResponse( id=response_id, created=created, model=model, choices=[ ChatStreamChoice( index=0, delta=ChatStreamDelta(), finish_reason="stop", ) ], ) yield f"data: {final_chunk.model_dump_json()}\n\n" yield "data: [DONE]\n\n" # Log completion duration_ms = (time.time() - start_time) * 1000 audit_log( AuditAction.CHAT_RESPONSE, user_id=auth.user_id if auth.authenticated else None, username=auth.username if auth.authenticated else None, duration_ms=duration_ms, details={"response_length": len(total_content), "stream": True}, ) await producer_task # ======================================================================== # Voice (Realtime Voice Chat) # ======================================================================== @app.post("/v1/voice/speak", tags=["Voice"]) async def voice_speak( request: Request, text: str, voice: str = "F3", speed: float = 1.05, # Supertonic default auth: AuthResult = Depends(get_current_auth), ): """ Convert text to speech using Supertonic TTS. Returns WAV audio. """ try: import io import soundfile as sf from r_cli.skills.realtime_voice_skill import RealtimeVoiceSkill skill = RealtimeVoiceSkill({}) if not skill._supertonic_available: raise HTTPException(status_code=503, detail="Supertonic TTS not available") # Get TTS engine tts, style = skill._get_tts(voice) # Synthesize audio, duration = tts.synthesize( text, style, speed=speed ) # Uses default total_steps=5 for quality audio = audio.squeeze() # Convert to WAV bytes buffer = io.BytesIO() sf.write(buffer, audio, 24000, format="WAV") buffer.seek(0) from fastapi.responses import Response return Response( content=buffer.read(), media_type="audio/wav", headers={ "X-Audio-Duration": str(len(audio) / 24000), "X-Voice": voice, }, ) except Exception as e: raise HTTPException(status_code=500, detail=str(e)) @app.post("/v1/voice/transcribe", tags=["Voice"]) async def voice_transcribe( request: Request, auth: AuthResult = Depends(get_current_auth), ): """ Transcribe audio to text using Whisper STT. Accepts WAV audio in request body. """ try: import io import soundfile as sf from r_cli.skills.realtime_voice_skill import RealtimeVoiceSkill skill = RealtimeVoiceSkill({}) if not skill._whisper_available: raise HTTPException(status_code=503, detail="Whisper STT not available") # Read audio from request body audio_bytes = await request.body() if not audio_bytes: raise HTTPException(status_code=400, detail="No audio data received") # Convert bytes to numpy array buffer = io.BytesIO(audio_bytes) try: audio, sr = sf.read(buffer) except Exception: raise HTTPException(status_code=400, detail="Invalid audio format. Send WAV audio.") # Resample to 16kHz if needed (Whisper expects 16kHz) if sr != 16000: import librosa audio = librosa.resample(audio, orig_sr=sr, target_sr=16000) # Ensure mono if len(audio.shape) > 1: audio = audio.mean(axis=1) # Transcribe result = skill._transcribe_audio(audio, language="en", model_size="base") import json return json.loads(result) except HTTPException: raise except Exception as e: raise HTTPException(status_code=500, detail=str(e)) @app.post("/v1/voice/chat", tags=["Voice"]) async def voice_chat( request: Request, voice: str = "F3", auth: AuthResult = Depends(get_current_auth), ): """ Complete voice chat: transcribe audio -> LLM -> TTS response. Accepts WAV audio, returns WAV audio response. """ try: import io import json import soundfile as sf from r_cli.skills.realtime_voice_skill import RealtimeVoiceSkill skill = RealtimeVoiceSkill({}) # 1. Transcribe incoming audio audio_bytes = await request.body() if not audio_bytes: raise HTTPException(status_code=400, detail="No audio data received") buffer = io.BytesIO(audio_bytes) try: audio, sr = sf.read(buffer) except Exception: raise HTTPException(status_code=400, detail="Invalid audio format") if sr != 16000: import librosa audio = librosa.resample(audio, orig_sr=sr, target_sr=16000) if len(audio.shape) > 1: audio = audio.mean(axis=1) # Transcribe transcription = json.loads(skill._transcribe_audio(audio, "en", "base")) user_text = transcription.get("text", "").strip() if not user_text: raise HTTPException(status_code=400, detail="Could not transcribe audio") # 2. Get LLM response agent = get_agent() response_text = await asyncio.to_thread(agent.run, user_text) # 3. Generate TTS response tts, style = skill._get_tts(voice) response_audio, _ = tts.synthesize( response_text, style ) # Uses defaults for natural voice response_audio = response_audio.squeeze() # Convert to WAV output_buffer = io.BytesIO() sf.write(output_buffer, response_audio, 24000, format="WAV") output_buffer.seek(0) from fastapi.responses import Response return Response( content=output_buffer.read(), media_type="audio/wav", headers={ "X-User-Text": user_text[:100], "X-Response-Text": response_text[:100], "X-Audio-Duration": str(len(response_audio) / 24000), }, ) except HTTPException: raise except Exception as e: raise HTTPException(status_code=500, detail=str(e)) @app.get("/v1/voice/status", tags=["Voice"]) async def voice_status(auth: AuthResult = Depends(get_current_auth)): """Check voice system status.""" try: import json from r_cli.skills.realtime_voice_skill import RealtimeVoiceSkill skill = RealtimeVoiceSkill({}) return json.loads(skill.voice_status()) except Exception as e: return {"error": str(e), "ready": False} # ======================================================================== # Skills # ======================================================================== @app.get("/v1/skills", response_model=SkillsResponse, tags=["Skills"]) async def list_skills(auth: AuthResult = Depends(get_current_auth)): """List all available skills and their tools.""" agent = get_agent() # Filter skills based on permissions if authenticated checker = None if auth.authenticated: checker = PermissionChecker(auth.scopes) skills_list = [] for name, skill in agent.skills.items(): # Check permission if checker and not checker.can_use_skill(name): continue tools = [] for tool in skill.get_tools(): params = [] if tool.parameters and "properties" in tool.parameters: required = tool.parameters.get("required", []) for param_name, param_info in tool.parameters["properties"].items(): params.append( ToolParameter( name=param_name, type=param_info.get("type", "string"), description=param_info.get("description", ""), required=param_name in required, default=param_info.get("default"), ) ) tools.append( ToolInfo( name=tool.name, description=tool.description, parameters=params, ) ) skills_list.append( SkillInfo( name=name, description=skill.description, tools=tools, category=_resolve_capability_domain(name), ) ) return SkillsResponse(total=len(skills_list), skills=skills_list) @app.get("/v1/skills/{skill_name}", response_model=SkillInfo, tags=["Skills"]) async def get_skill(skill_name: str, auth: AuthResult = Depends(get_current_auth)): """Get details about a specific skill.""" agent = get_agent() if skill_name not in agent.skills: raise HTTPException(status_code=404, detail=f"Skill not found: {skill_name}") # Check permission if auth.authenticated: checker = PermissionChecker(auth.scopes) if not checker.can_use_skill(skill_name): raise HTTPException( status_code=403, detail=f"No permission for skill: {skill_name}", ) skill = agent.skills[skill_name] tools = [] for tool in skill.get_tools(): params = [] if tool.parameters and "properties" in tool.parameters: required = tool.parameters.get("required", []) for param_name, param_info in tool.parameters["properties"].items(): params.append( ToolParameter( name=param_name, type=param_info.get("type", "string"), description=param_info.get("description", ""), required=param_name in required, default=param_info.get("default"), ) ) tools.append( ToolInfo( name=tool.name, description=tool.description, parameters=params, ) ) return SkillInfo( name=skill_name, description=skill.description, tools=tools, category=_resolve_capability_domain(skill_name), ) @app.post("/v1/skills/call", response_model=ToolCallResponse, tags=["Skills"]) async def call_tool( request: ToolCallRequest, req: Request, auth: AuthResult = Depends(get_current_auth), ): """ Call a specific tool directly. This bypasses the LLM and calls the tool handler directly. """ # Check auth if required if AUTH_MODE == "required" and not auth.authenticated: raise HTTPException(status_code=401, detail="Authentication required") agent = get_agent() start_time = time.time() # Check skill permission if auth.authenticated: allowed, reason = check_skill_permission(request.skill, auth.scopes) if not allowed: audit_log( AuditAction.SKILL_DENIED, user_id=auth.user_id, username=auth.username, resource=request.skill, details={"tool": request.tool, "reason": reason}, success=False, severity=AuditSeverity.WARNING, ) return ToolCallResponse( success=False, error=reason, execution_time_ms=(time.time() - start_time) * 1000, ) if request.skill not in agent.skills: return ToolCallResponse( success=False, error=f"Skill not found: {request.skill}", execution_time_ms=(time.time() - start_time) * 1000, ) skill = agent.skills[request.skill] # Find the tool target_tool = None for tool in skill.get_tools(): if tool.name == request.tool: target_tool = tool break if not target_tool: return ToolCallResponse( success=False, error=f"Tool not found: {request.tool} in skill {request.skill}", execution_time_ms=(time.time() - start_time) * 1000, ) # Log skill call audit_log( AuditAction.SKILL_CALLED, user_id=auth.user_id if auth.authenticated else None, username=auth.username if auth.authenticated else None, client_ip=req.client.host if req.client else None, resource=request.skill, resource_id=request.tool, details={"arguments": list(request.arguments.keys())}, ) try: # API scopes authorize the request; local explicit deny rules still win. permission_manager = PermissionManager( agent.config, auto_approve=auth.authenticated, source="api", ) result = await asyncio.to_thread( permission_manager.execute, request.skill, request.tool, target_tool.handler, request.arguments, ) duration_ms = (time.time() - start_time) * 1000 audit_log( AuditAction.SKILL_COMPLETED, user_id=auth.user_id if auth.authenticated else None, username=auth.username if auth.authenticated else None, resource=request.skill, resource_id=request.tool, duration_ms=duration_ms, ) return ToolCallResponse( success=True, result=result, execution_time_ms=duration_ms, ) except PermissionDeniedError as e: duration_ms = (time.time() - start_time) * 1000 return ToolCallResponse( success=False, error=str(e), execution_time_ms=duration_ms, ) except Exception as e: duration_ms = (time.time() - start_time) * 1000 audit_log( AuditAction.SKILL_ERROR, user_id=auth.user_id if auth.authenticated else None, username=auth.username if auth.authenticated else None, resource=request.skill, resource_id=request.tool, success=False, error_message=str(e), duration_ms=duration_ms, severity=AuditSeverity.ERROR, ) return ToolCallResponse( success=False, error=str(e), execution_time_ms=duration_ms, ) # ======================================================================== # Audit Logs (Admin) # ======================================================================== @app.get("/v1/audit/logs", tags=["Admin"]) async def get_audit_logs( limit: int = 100, auth: AuthResult = Depends(require_scopes(Scope.ADMIN)), ): """Get recent audit logs (admin only).""" logger = get_audit_logger() events = logger.get_recent_events(limit=limit) return { "total": len(events), "events": [e.model_dump() for e in events], } # ======================================================================== # Distributed AI Routes # ======================================================================== try: from r_cli.api.distributed_routes import router as distributed_router app.include_router(distributed_router) except ImportError: pass # Distributed AI module not available # ======================================================================== # Error Handlers # ======================================================================== @app.exception_handler(HTTPException) async def http_exception_handler(request: Request, exc: HTTPException): return JSONResponse( status_code=exc.status_code, content={ "error": { "code": f"HTTP_{exc.status_code}", "message": exc.detail, } }, ) def run_server( host: str = "127.0.0.1", port: int = 8765, reload: bool = False, workers: int = 1, ) -> None: """Run the API server.""" import uvicorn uvicorn.run( "r_cli.api.server:create_app", factory=True, host=host, port=port, reload=reload, workers=workers, log_level="info", ) if __name__ == "__main__": run_server()
"""
R CLI API Server - FastAPI daemon for R Agent Runtime.
Usage:
r serve # Start API server
r serve --port 8080 # Custom port
r serve --host 0.0.0.0 # Listen on all interfaces
"""
from __future__ import annotations
import asyncio
import os
import time
import uuid
from contextlib import asynccontextmanager
from datetime import datetime
from typing import AsyncGenerator, Optional
from fastapi import Depends, FastAPI, HTTPException, Request, status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse, StreamingResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
from r_cli import __version__
from r_cli.agent_os import AgentOS
from r_cli.api.audit import (
AuditAction,
AuditSeverity,
audit_log,
get_audit_logger,
)
from r_cli.api.auth import (
ACCESS_TOKEN_EXPIRE_MINUTES,
AuthResult,
Token,
authenticate_user,
create_access_token,
get_current_auth,
get_storage,
require_auth,
require_scopes,
)
from r_cli.api.models import (
AgentOSStatus,
AgentTaskSummary,
CapabilityDomainSummary,
ChatChoice,
ChatMessage,
ChatRequest,
ChatResponse,
ChatStreamChoice,
ChatStreamDelta,
ChatStreamResponse,
ChatUsage,
ControlCenterResponse,
HealthStatus,
InstalledAgentSummary,
LLMStatus,
MemoryOverview,
SecurityOverview,
SkillInfo,
SkillsResponse,
StatusResponse,
ToolCallRequest,
ToolCallResponse,
ToolInfo,
ToolParameter,
)
from r_cli.api.permissions import (
PermissionChecker,
Scope,
check_skill_permission,
)
from r_cli.api.rate_limit import RateLimitMiddleware
from r_cli.core.agent import Agent
from r_cli.core.config import Config
from r_cli.core.permissions import PermissionDeniedError, PermissionManager
# Global state
_agent: Optional[Agent] = None
_start_time: float = 0
_config: Optional[Config] = None
# Auth mode: "none", "optional", "required"
AUTH_MODE = os.getenv("R_AUTH_MODE", "optional")
CAPABILITY_DOMAINS = {
"Knowledge & Docs": {
"icon": "📚",
"skills": {
"pdf",
"pdftools",
"markdown",
"latex",
"template",
"resume",
"ocr",
"rag",
"text",

Xet Storage Details

Size:
41.9 kB
·
Xet hash:
fdc2fb2da6ddac44f34243675a13436b11a1982e71f725e320a3617a4000998c

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.