feat: write API + session protocol

This commit is contained in:
Nabil Derouiche
2026-06-26 00:35:34 +01:00
parent 9e7a974538
commit 395381ae07
11 changed files with 361 additions and 21 deletions
+26 -1
View File
@@ -2,7 +2,7 @@ import json
from fastapi import APIRouter, Depends, HTTPException, Request
from app.auth import get_current_agent
from app.scopes import has_scope_access, ALL_SCOPES
from app.models import get_rule, get_all_rules, get_all_ports, add_audit_log
from app.models import get_rule, get_all_rules, get_all_ports, add_audit_log, set_rule
router = APIRouter(prefix="/api")
@@ -48,3 +48,28 @@ async def search_rules(q: str, request: Request, agent: str = Depends(get_curren
results[scope] = content
return results
@router.patch("/rules/{scope}")
async def patch_scope_rules(scope: str, request: Request, agent: str = Depends(get_current_agent)):
if not has_scope_access(agent, scope):
await log_audit(request, agent, f"{scope} (WRITE FORBIDDEN)")
raise HTTPException(status_code=403, detail="Forbidden")
body = await request.json()
if not isinstance(body, dict):
raise HTTPException(status_code=400, detail="Body must be a JSON object")
existing = await get_rule(scope) or {}
existing.update(body)
await set_rule(scope, existing)
await log_audit(request, agent, f"{scope} (WRITE)")
return {"status": "ok", "scope": scope, "updated_keys": list(body.keys())}
@router.get("/context")
async def get_agent_context(request: Request, agent: str = Depends(get_current_agent)):
await log_audit(request, agent, "context (READ ALL)")
all_rules = await get_all_rules()
result = {}
for scope in ALL_SCOPES:
if has_scope_access(agent, scope):
result[scope] = all_rules.get(scope, {})
ports = await get_all_ports()
return {"agent": agent, "scopes": result, "ports": ports}