Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.
Python agent-orchestrationai-agentai-coding-assistantclaude-codecoding-agentcomputer-usedesktop-applocal-firstmcppersonal-jarvisvoice-assistantvoice-coding
"""REST API for the desktop shell's window management.
Sections never get their own window; they are tiled side by side inside the
main window (the frontend's tiling layout). The routes below open and close
the shell's TOOL windows only — the appshot editor and the Dodaios X editor.
The window itself is created by the desktop shell (pywebview) because
``window.open`` is silently dropped inside WebView2.
Endpoints:
POST /api/window/detach {view} → open (or focus) the detached window
GET /api/window/detached → registry snapshot for mount-time resync
POST /api/window/reattach {view} → close the detached window
Dispatch follows the ``app.state.desktop_app`` pattern (see
``settings_routes.py::open_external``): when no desktop shell is attached —
headless server, Docker, degraded Linux desktop — the answer is an honest
``{"ok": false, "reason": "no_desktop_shell", "fallback_url": ...}`` so the
caller can open the solo URL in a real browser tab instead. Every pywebview
call is thread-hopped off the event loop: window methods block the calling
thread while the GUI thread services them (the 2026-08-06 realtime stall), and
pywebview only materializes runtime windows from a non-MainThread thread.
Wired into the WebServer in ``server.py::_build_app`` via
from .desktop_routes import router as desktop_router
app.include_router(desktop_router)
"""
from __future__ import annotations
import asyncio
import logging
from typing import Any, Literal
from fastapi import APIRouter, Request
from pydantic import BaseModel, Field
log = logging.getLogger(__name__)
router = APIRouter(prefix="/api/window", tags=["window"])
class DetachBody(BaseModel):
view: str = Field(min_length=1, max_length=64)
class BackgroundBody(BaseModel):
enabled: bool = Field(strict=True)
@router.get("/background", operation_id="background_status")
async def window_background_status(request: Request) -> dict[str, Any]:
"""Read the explicit window-close background policy and native availability."""
desktop = getattr(request.app.state, "desktop_app", None)
getter = getattr(desktop, "get_background_status", None)
if not callable(getter):
return {
"available": False, "reason": "headless_host", "enabled": True,
"mode": "background", "window_open": False,
}
return await asyncio.to_thread(getter)
@router.post("/background", operation_id="background_set")
async def window_background_set(body: BackgroundBody, request: Request) -> dict[str, Any]:
"""Opt into background work for this session; explicit Quit still stops the app."""
desktop = getattr(request.app.state, "desktop_app", None)
setter = getattr(desktop, "set_background_mode", None)
if not callable(setter):
return await window_background_status(request)
return await asyncio.to_thread(setter, body.enabled)
def _solo_url_path(view: str) -> str:
"""Relative solo URL for a view — the SPA catch-all serves it anywhere."""
return f"/?view={view}&solo=1"
# operation_ids are the CLI command names: the dynamic layer builds one Click
# group per tag and one command per operation, so these read
# `jarvis api window detach|detached|reattach`. The paths match no dangerous
# marker — opening/listing/closing local UI windows mutates no data — so the
# normal mutating-confirmation tier applies (see dodaios/cli_ctl/safety.py).
@router.post("/detach", operation_id="detach")
async def window_detach(body: DetachBody, request: Request) -> dict[str, Any]:
"""Open ``view`` in its own detached desktop window (or focus it).
Idempotent: a second detach of the same view focuses the existing window
and reports ``already_open``. An unknown/undetachable view, a host without
a desktop shell, and a webview backend that cannot create runtime windows
(the macOS/GTK risk) all answer with ``ok: false`` and a machine-readable
reason — the frontend then falls back to opening ``fallback_url`` in a
real browser tab, which is the honest degrade on those hosts.
"""
desktop = getattr(request.app.state, "desktop_app", None)
fn = getattr(desktop, "open_detached_window", None)
if not callable(fn):
return {
"ok": False,
"reason": "no_desktop_shell",
"fallback_url": _solo_url_path(body.view),
}
try:
return await asyncio.to_thread(fn, body.view)
except Exception as exc: # noqa: BLE001
# Not swallowed: the traceback goes to the log and a machine-readable
# reason travels back to the caller (exception text stays server-side).
log.warning("window detach failed: %s", exc, exc_info=True)
return {
"ok": False,
"reason": "detach_failed",
"fallback_url": _solo_url_path(body.view),
}
@router.get("/detached", operation_id="detached")
async def window_detached(request: Request) -> dict[str, Any]:
"""Snapshot of the currently detached views.
The WS events (``DetachedViewOpened/Closed``) keep every window live; this
GET exists for the mount-time resync after a reload (preload recovery
reloads every window after a frontend rebuild). Headless hosts honestly
report an empty registry — nothing can be detached there.
"""
desktop = getattr(request.app.state, "desktop_app", None)
fn = getattr(desktop, "detached_views_snapshot", None)
if not callable(fn):
return {"ok": True, "views": []}
return {"ok": True, "views": await asyncio.to_thread(fn)}
@router.post("/reattach", operation_id="reattach")
async def window_reattach(body: DetachBody, request: Request) -> dict[str, Any]:
"""Close the detached window for ``view`` (its section returns to the app).
The pywebview ``closed`` hook — not this route — publishes
``DetachedViewClosed`` and clears the registry, so closing the window with
its own X and clicking "Bring it back" take exactly the same path.
"""
desktop = getattr(request.app.state, "desktop_app", None)
fn = getattr(desktop, "close_detached_window", None)
if not callable(fn):
return {"ok": False, "reason": "no_desktop_shell"}
try:
return await asyncio.to_thread(fn, body.view)
except Exception as exc: # noqa: BLE001
log.warning("window reattach failed: %s", exc, exc_info=True)
return {"ok": False, "reason": "reattach_failed"}
class FullscreenBody(BaseModel):
enabled: bool
class WindowCommandBody(BaseModel):
action: Literal["minimize", "maximize", "close"]
view: str | None = Field(default=None, max_length=64)
@router.get("/chrome", operation_id="chrome")
async def window_chrome(request: Request) -> dict[str, Any]:
"""Whether this window draws its own minimize, maximize and close.
A browser tab and a headless server answer ``frameless: false`` so the
page does not invent window buttons that do nothing. The desktop shell
answers from the live window.
"""
desktop = getattr(request.app.state, "desktop_app", None)
fn = getattr(desktop, "window_chrome_snapshot", None)
if not callable(fn):
return {"ok": True, "frameless": False, "platform": "headless", "controls": "none"}
try:
return await asyncio.to_thread(fn)
except Exception as exc: # noqa: BLE001
log.warning("window chrome failed: %s: %s", type(exc).__name__, exc)
return {"ok": False, "frameless": False, "controls": "none", "reason": type(exc).__name__}
@router.post("/command", operation_id="command")
async def window_command(body: WindowCommandBody, request: Request) -> dict[str, Any]:
"""Minimize, maximize or close the window the page is showing.
``view`` names a detached window. Omitted, the main window is used.
Closing follows the same path as the window's own close button.
"""
desktop = getattr(request.app.state, "desktop_app", None)
fn = getattr(desktop, "window_command", None)
if not callable(fn):
return {"ok": False, "reason": "no_desktop_shell"}
try:
return await asyncio.to_thread(fn, body.action, body.view or None)
except Exception as exc: # noqa: BLE001
log.warning("window command failed: %s", exc, exc_info=True)
return {"ok": False, "reason": "command_failed"}
class WindowZoomBody(BaseModel):
factor: float = Field(ge=0.25, le=5.0)
view: str | None = Field(default=None, max_length=64)
@router.post("/zoom", operation_id="zoom")
async def window_zoom(body: WindowZoomBody, request: Request) -> dict[str, Any]:
"""Zoom the page of the window the page is showing (1.0 = 100 %).
Real engine zoom, the way a browser's Ctrl + `+` works. A browser tab or a
headless server answers ``ok: false``; the page then leaves zoom to the
browser. ``view`` names a detached window. Omitted, the main window is used.
"""
desktop = getattr(request.app.state, "desktop_app", None)
fn = getattr(desktop, "set_window_zoom", None)
if not callable(fn):
return {"ok": False, "reason": "no_desktop_shell"}
try:
return await asyncio.to_thread(fn, body.factor, body.view or None)
except Exception as exc: # noqa: BLE001
log.warning("window zoom failed: %s", exc, exc_info=True)
return {"ok": False, "reason": "zoom_failed"}
@router.post("/fullscreen", operation_id="fullscreen")
async def window_fullscreen(body: FullscreenBody, request: Request) -> dict[str, Any]:
"""Enter or leave native desktop fullscreen."""
desktop = getattr(request.app.state, "desktop_app", None)
setter = getattr(desktop, "set_fullscreen", None)
if not callable(setter):
return {"ok": False, "reason": "no_desktop_shell"}
try:
return await asyncio.to_thread(setter, body.enabled)
except Exception:
log.exception("Could not change desktop fullscreen")
return {"ok": False, "reason": "fullscreen_failed"}