# Conflicts: # apps/desktop/electron/backend-dial-claim.test.ts # apps/desktop/electron/gateway-file-download-transport.test.ts # apps/desktop/electron/pool-limits.test.ts # apps/desktop/electron/pool-spawn-coordinator.test.ts # apps/desktop/electron/quit-guard.test.ts # apps/desktop/src/app/chat/composer/controls.test.tsx # apps/desktop/src/components/notifications.test.tsx # apps/desktop/src/plugins/hermes-bots/group-chat-view.inline-code.test.tsx # apps/desktop/src/sdk/profile-routing.test.ts # apps/desktop/src/store/gateway-spawn-priority.test.ts # apps/desktop/src/store/profile.test.ts # gateway/run.py # gateway/run_turn.py # gateway/status.py # hermes_cli/dashboard_auth/ws_tickets.py # hermes_cli/web_routers/sessions.py # tests/agent/test_secret_scope_tier1_migration.py # tests/cron/test_bot_chat_delivery_child_env.py # tests/cron/test_bot_chat_warning_replay.py # tests/cron/test_codex_execution_paths.py # tests/cron/test_cron_bot_chat_delivery.py # tests/cron/test_cron_empty_payload.py # tests/cron/test_cron_mcp_toolset_empty_block.py # tests/cron/test_cron_memory_contract.py # tests/cron/test_cron_no_agent.py # tests/cron/test_cron_request_overrides.py # tests/cron/test_cron_workdir.py # tests/cron/test_run_one_job.py # tests/cron/test_scheduler.py # tests/cron/test_sessiondb_init_hang.py # tests/desktop/test_bots_chat_live_append.py # tests/gateway/test_api_server_runs_extraction.py # tests/gateway/test_approval_prompt_redaction.py # tests/gateway/test_auto_reset_clean_context.py # tests/gateway/test_auto_reset_evicts_cached_agent.py # tests/gateway/test_compression_deferred_soft_result.py # tests/gateway/test_compression_session_id_persistence.py # tests/gateway/test_control_socket_windows_live.py # tests/gateway/test_fallback_chain_reload.py # tests/gateway/test_model_switch_after_auto_reset.py # tests/gateway/test_session_hygiene.py # tests/gateway/test_warning_wiring_conservation.py # tests/hermes_cli/test_backup.py # tests/hermes_cli/test_chat_q_exit_clear.py # tests/hermes_cli/test_cli_preloaded_skills.py # tests/hermes_cli/test_cron.py # tests/hermes_cli/test_ensure_gateway_service.py # tests/hermes_cli/test_kanban_gateway_restart_handoff.py # tests/hermes_cli/test_observational_sessiondb_modes.py # tests/hermes_cli/test_profiles_sidebar_cache.py # tests/hermes_cli/test_sessions_export_md_cli.py # tests/hermes_cli/test_single_query_session_finalize.py # tests/hermes_cli/test_web_server.py # tests/hermes_state/test_hermes_state.py # tests/tools/test_bot_live_owner_delivery.py # tests/tools/test_bot_mode_dm.py # tests/tools/test_bot_retry_policy.py # tests/tools/test_bot_turn_lock.py # tests/tui_gateway/test_bot_live_owner_delivery.py # tests/tui_gateway/test_compute_host_turn_protocol.py # tui_gateway/server.py # ui-tui/src/__tests__/createSlashHandler.test.ts # ui-tui/src/__tests__/submissionCore.test.ts # ui-tui/src/app/useSessionLifecycle.ts
78 lines
3.4 KiB
Python
78 lines
3.4 KiB
Python
"""Chat workspace picker: which host directories a NEW dashboard chat can start in.
|
|
|
|
The dashboard's ``/chat`` tab used to spawn every fresh TUI in the dashboard process's own
|
|
launch directory, so from a phone or browser there was no way to say "work in ~/code/foo".
|
|
This lists the same projects and discovered repositories the Desktop sidebar shows (the
|
|
per-profile ``projects.db`` plus session-derived and scanned git roots) so the SPA can offer
|
|
a picker, and ``/api/pty?cwd=`` (``chat_ws``) honours the choice.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from pathlib import Path
|
|
from typing import Optional
|
|
|
|
from fastapi import APIRouter, HTTPException
|
|
|
|
from hermes_cli.web_deps import late
|
|
from hermes_cli.web_routers._common import http_failure, scoped_to_thread
|
|
|
|
router = APIRouter()
|
|
|
|
_open_session_db_for_profile = late("_open_session_db_for_profile", "hermes_cli.web_server_sessions")
|
|
|
|
|
|
def resolve_chat_cwd(raw: Optional[str]) -> Optional[str]:
|
|
"""Validate a ``?cwd=`` for a fresh chat: an existing host directory, or None when unset.
|
|
|
|
A dead or relative path fails closed (400) instead of silently falling back to the launch
|
|
dir — the user picked a workspace and would otherwise get a session in the wrong place.
|
|
"""
|
|
text = (raw or "").strip()
|
|
if not text:
|
|
return None
|
|
resolved = os.path.abspath(os.path.expanduser(text))
|
|
if not os.path.isdir(resolved):
|
|
raise HTTPException(status_code=400, detail=f"Working directory does not exist: {text}")
|
|
return resolved
|
|
|
|
|
|
def _collect_workspaces(profile: Optional[str], scan: bool) -> dict:
|
|
# The dashboard hosts the in-process gateway (``web_server`` imports ``tui_gateway.server``
|
|
# at startup), so the sidebar's repo-discovery helpers are already bound there.
|
|
import tui_gateway.server as gateway
|
|
from hermes_cli import projects_db as pdb
|
|
|
|
# Browsing never bootstraps the owner's session store: a profile whose gateway has not
|
|
# yet initialized state.db simply contributes no session-derived roots (``db=None``).
|
|
try:
|
|
db = _open_session_db_for_profile(profile, read_only=True)
|
|
except HTTPException as exc:
|
|
if exc.status_code != 503:
|
|
raise
|
|
db = None
|
|
try:
|
|
with pdb.connect_closing() as conn:
|
|
policy = gateway._repo_discovery_policy()
|
|
if scan and policy["enabled"]:
|
|
gateway._scan_discovered_repos_remote(conn, policy)
|
|
projects = [p.to_dict() for p in pdb.list_projects(conn)]
|
|
repos = gateway._discover_repos_payload(
|
|
db, conn=conn, backfill=False, include_cached=policy["enabled"])
|
|
finally:
|
|
if db is not None:
|
|
db.close()
|
|
default_cwd = gateway._completion_cwd({"profile": profile} if profile else {})
|
|
return {
|
|
"projects": projects, "repos": repos, "default_cwd": default_cwd,
|
|
"home": str(Path.home()), "scan_enabled": bool(policy["enabled"])}
|
|
|
|
|
|
@router.get("/api/chat/workspaces")
|
|
async def get_chat_workspaces(profile: Optional[str] = None, scan: bool = False):
|
|
"""Projects + discovered repos a fresh chat may start in; ``scan=1`` rescans the
|
|
configured discovery roots on the host first (headless installs have no Desktop to do it)."""
|
|
with http_failure("GET /api/chat/workspaces failed", 500, "Failed to list workspaces"):
|
|
return await scoped_to_thread(profile, lambda: _collect_workspaces(profile, scan))
|