feat: register local agent project workspaces

This commit is contained in:
Hamza Ayed
2026-10-04 01:57:44 +03:00
parent d29275043a
commit 1feeb372a4
12 changed files with 806 additions and 45 deletions
+48
View File
@@ -244,6 +244,54 @@ class WorkspaceFilesRequest(BaseModel):
workspace_path: str = Field(min_length=1, max_length=2048)
def _is_loopback_request(request: Request) -> bool:
client_host = request.client.host if request.client is not None else ""
try:
return ipaddress.ip_address(client_host).is_loopback
except ValueError:
return False
@app.post("/v1/agent/projects", status_code=201)
async def register_agent_project(
request: WorkspaceFilesRequest,
http_request: Request,
user_id: str = Depends(get_authenticated_user_id),
) -> dict[str, Any]:
"""Register a project folder explicitly selected by this local desktop user."""
if not _is_loopback_request(http_request):
raise HTTPException(status_code=403, detail="تسجيل مجلدات المشاريع متاح من هذا الجهاز فقط.")
try:
root = workspace.validate_workspace_registration(request.workspace_path)
except ValueError as exc:
raise HTTPException(status_code=422, detail=str(exc)) from exc
try:
database.register_workspace_root(user_id, str(root))
except ValueError as exc:
raise HTTPException(status_code=409, detail=str(exc)) from exc
files = [path.relative_to(root).as_posix() for path in workspace.list_knowledge_files(root)]
return {"path": str(root), "name": root.name, "files": files, "limit": workspace.MAX_SCAN_FILES}
@app.delete("/v1/agent/projects")
async def unregister_agent_project(
request: WorkspaceFilesRequest,
http_request: Request,
user_id: str = Depends(get_authenticated_user_id),
) -> dict[str, Any]:
"""Revoke this user's explicit local project-folder registration."""
if not _is_loopback_request(http_request):
raise HTTPException(status_code=403, detail="إدارة تسجيل المشاريع متاحة من هذا الجهاز فقط.")
try:
root = workspace.validate_workspace_registration(
request.workspace_path, must_exist=False
)
except ValueError as exc:
raise HTTPException(status_code=422, detail=str(exc)) from exc
removed = database.unregister_workspace_root(user_id, str(root))
return {"path": str(root), "removed": removed}
class KnowledgeIndexRequest(BaseModel):
workspace_path: str = Field(min_length=1, max_length=2048)
files: list[str] = Field(min_length=1, max_length=20)