Files
hermes-agent/hermes_cli/web_routers/uploads.py
Bear Huddleston b5eb5ee7a8 feat(webapp): carry the browser-hosted Desktop's server pieces from #93508
Carried from BearHuddleston's #93508 onto main. The Webapp relay and the
profile-incarnation fence stay out: carried code follows main's
named-profile rule and refuses a missing or deleted profile home.

- Webapp routes: one-use launch tickets, window handoff, and gateway
  WebSocket tickets minted for the signed-in browser
- Browser surface policy and the SPA document policy
- A host terminal for the browser that survives reconnects
- Staged browser uploads and file tickets; file.attach takes a staged upload
- hermes sessions adopt
- Webapp bundle build helpers and the renderer freshness check
- Regenerated gateway contracts

Co-authored-by: emozilla <emozilla@nousresearch.com>
2026-10-25 03:22:14 -07:00

56 lines
1.8 KiB
Python

"""Authenticated browser-to-host attachment staging for Hermes Webapp."""
from __future__ import annotations
import asyncio
import os
from fastapi import APIRouter, File, HTTPException, UploadFile
from hermes_constants import WEBAPP_ATTACHMENT_MAX_BYTES
from hermes_cli.staged_uploads import (
publish_staged_upload,
resolve_upload_home,
staged_upload_descriptor,
)
router = APIRouter()
_MAX_UPLOAD_BYTES = WEBAPP_ATTACHMENT_MAX_BYTES
@router.post("/api/chat/file-upload")
async def upload_chat_file(
file: UploadFile = File(...),
profile: str | None = None,
):
"""Stage one user-selected browser file under the active Hermes profile.
The client never chooses a server path. A unique 0600 file under
``$HERMES_HOME/uploads`` is returned for the existing ``file.attach`` flow.
"""
profile_home = await asyncio.to_thread(resolve_upload_home, profile)
try:
# FastAPI has already spooled the complete upload outside the profile.
# Check the actual bytes, then publish that spool into the resolved home.
total = await asyncio.to_thread(file.file.seek, 0, os.SEEK_END)
if total > _MAX_UPLOAD_BYTES:
cap_mib = _MAX_UPLOAD_BYTES // (1024 * 1024)
raise HTTPException(
status_code=413,
detail=f"File is too large; cap is {cap_mib} MiB",
)
target = await asyncio.to_thread(
publish_staged_upload,
file.file,
profile_home,
file.filename or "attachment",
)
finally:
await file.close()
result = {"ok": True, "path": str(target), "size": total}
if descriptor := await asyncio.to_thread(staged_upload_descriptor, target, profile_home):
result["staged_upload"] = descriptor
return result