Skip to content

The agent that grows with you

34.1 KB
Raw Blame
import { useStore } from '@nanostores/react'
import { atom, computed } from 'nanostores'
import type { CSSProperties, ReactElement, PointerEvent as ReactPointerEvent } from 'react'

import { SessionDraftTitle } from '@/app/chat/session-draft-title'
import { SessionStatusDot } from '@/app/chat/session-status-dot'
import { PALETTE_AREA, type PaletteContribution, paletteToggle } from '@/app/command-palette/contrib'
import { type StatusbarItem } from '@/app/shell/statusbar-controls'
import { InlinePreviewDirective } from '@/components/assistant-ui/inline-preview-directive'
import { IdleMount } from '@/components/idle-mount'
import { $layoutEditMode, toggleLayoutEditMode } from '@/components/pane-shell/edit-mode'
import { allPaneIds } from '@/components/pane-shell/tree/model'
import { LayoutTreeRoot } from '@/components/pane-shell/tree/renderer'
import {
  $layoutTree,
  bindPaneVisibility,
  bindToolPaneCollapse,
  declareDefaultTree,
  dismissTreePane,
  hydrateContributedPanes,
  isPaneVisible,
  markCollapsePane,
  paneRootSide,
  registerLayoutResetHandler,
  registerPaneCloser,
  registerPaneOpener,
  removeTreePane,
  resetLayoutTree,
  revealTreePane,
  setStripTabHidden,
  targetZoneTabStripVisible,
  togglePaneVisible,
  toggleTargetZoneTabStrip
} from '@/components/pane-shell/tree/store'
import { $workspaceOwnerLabels, workspaceOwnerTitle } from '@/components/pane-shell/workspace-scope'
import { SidebarProvider } from '@/components/ui/sidebar'
import { discoverBundledPlugins } from '@/contrib/plugins'
import { Slot } from '@/contrib/react/slot'
import { registry } from '@/contrib/registry'
import { discoverRuntimePlugins } from '@/contrib/runtime-loader'
import { LocalizedTabTitle, translateNow } from '@/i18n'
import { NEW_SESSION_TITLE, sessionTitle as storedSessionTitle } from '@/lib/chat-runtime'
import {
  Download,
  FileText,
  LayoutDashboard,
  PanelBottom,
  PanelTop,
  SlidersHorizontal,
  Upload,
  Users,
  Zap
} from '@/lib/icons'
import { type KeybindContribution, KEYBINDS_AREA } from '@/lib/keybinds/actions'
import { TRANSCRIPT_DIRECTIVE_AREA, type TranscriptDirectiveContribution } from '@/lib/transcript-directives'
import { setYoloEnabled } from '@/lib/yolo-session'
import { $connectionsRegistry } from '@/store/connection-registry-state'
import { watchDeadSessionPrune } from '@/store/dead-session-prune'
import { $interfaceMode, $showsAdvancedChrome, setModeContext, toggleSimpleMode } from '@/store/interface-mode'
import {
  $fileBrowserOpen,
  $sidebarOpen,
  FILE_BROWSER_DEFAULT_WIDTH,
  FILE_BROWSER_MAX_WIDTH,
  FILE_BROWSER_MIN_WIDTH,
  fileBrowserSide,
  setFileBrowserOpen,
  setSidebarOpen,
  SIDEBAR_DEFAULT_WIDTH,
  SIDEBAR_MAX_WIDTH,
  sidebarSide
} from '@/store/layout'
import { $profiles } from '@/store/profile'
import { $profileRailVisible } from '@/store/profile-rail-prefs'
import { runExportProfileFlow, runImportProfileFlow } from '@/store/profile-share'
import {
  $reviewOpen,
  $reviewScopeCwd,
  $reviewScopeTarget,
  closeReview,
  openReview,
  REVIEW_PANE_ID
} from '@/store/review'
import {
  $cronSessions,
  $currentCwd,
  $messagingSessions,
  $selectedStoredSessionId,
  $sessions,
  $yoloActive,
  ownerLookupSessionRows,
  sessionMatchesStoredId
} from '@/store/session'
import { watchSessionPins } from '@/store/session-pin-sync'
import { $botChatScopes } from '@/store/session-states'
import { watchUnreadWriteGuard } from '@/store/session-unread-remote'
import { $statusbarVisible } from '@/store/statusbar-prefs'
import { isBrowserWindow, isHudWindow } from '@/store/windows'

import { BrowserPopoutShell } from '../chat/browser-popout-shell'
import type { SessionDragPayload } from '../chat/composer/inline-refs'
import { watchPreviewTiles } from '../chat/preview-tile'
import { watchRouteTiles } from '../chat/route-tile'
import { startSessionDrag } from '../chat/session-drag'
import {
  SessionTileCloseConfirm,
  stackSessionTilesIntoMain,
  startTileBackendIdentityGuard,
  startUnrestoredTileTitleBackfill,
  watchSessionTiles,
  workspaceTabActions,
  WorkspaceTabMenu
} from '../chat/session-tile'
import { AppContextMenu } from '../context-menu/app-context-menu'
import { HudShell } from '../hud/hud-shell'
import { $terminalTakeover, setTerminalTakeover } from '../right-sidebar/store'
import { terminalPaletteToggle } from '../right-sidebar/terminal/reveal-focus'
import { $workspaceIsPage, WORKSPACE_PAGE_HEADER_AREA } from '../routes'
import { Butterbar } from '../shell/butterbar'
import { TermsButterbar } from '../shell/terms-butterbar'

import { BASIC_TREE, DEFAULT_TREE, registerLayoutPresets } from './layout-presets'
import { bindLayoutSides } from './layout-sides'
import { FilesPane, LogsPane, ReviewPaneContent } from './panes'
import { PluginPreviewConsentDialog } from './plugin-preview-consent-dialog'
import { ContribWiring, WiredPane } from './wiring'
import { WorkspacePageHeaderHostContext } from './workspace-page-header'

/**
 * Stripped-down app root (bb/contrib-areas) on the layout TREE model, mounting
 * the REAL app surfaces. The title bar and status bar sit OUTSIDE the grid
 * (fixed chrome) but are fully composable: title bar renders `titleBar.left/
 * right` slots; the status bar consumes `statusBar.left/right` DATA
 * contributions (payload = StatusbarItem). Core registers its items through
 * the same calls a plugin would use.
 */

// ---------------------------------------------------------------------------
// Pane contributions. `data.placement` = semantic role for grid presets;
// `data.minWidth/maxWidth/minHeight/maxHeight` = the SAME clamps the app's
// `Pane` props declare — the layout tree sizes zones by weight (percentage)
// but a zone never shrinks/grows past its active pane's clamp.
// Headers are contextual (tree-side): a pane alone in a zone shows no
// header/tab by default; stacked panes show chips. Double-click a zone
// toggles its header either way.
// ---------------------------------------------------------------------------

// ONE render identity for the workspace pane — syncWorkspaceTitle re-registers
// the contribution (new title) and a fresh closure would remount the chat.
// The host context marks this subtree as the one whose zone paints
// WORKSPACE_PAGE_HEADER_AREA; route tiles and the HUD render outside it.
const renderWorkspacePane = () => (
  <WorkspacePageHeaderHostContext.Provider value={true}>
    <WiredPane part="chatRoutes" />
  </WorkspacePageHeaderHostContext.Provider>
)

// Boot-hidden panes mount behind display:none (instant-toggle contract) — defer
// them to idle so they're off the first-paint path, warm before reveal.
const idle = (node: ReactElement) => <IdleMount>{node}</IdleMount>
// The main tab carries the same session context menu as tile tabs (targets
// the loaded primary session; no menu on a fresh draft).
const wrapWorkspaceTab = (tab: ReactElement) => <WorkspaceTabMenu>{tab}</WorkspaceTabMenu>

/** The `@session` payload for the workspace tab — the loaded primary session,
 *  or null on a fresh draft / full-page view (nothing to link). */
const workspaceDragPayload = (): SessionDragPayload | null => {
  const selected = $selectedStoredSessionId.get()

  if (!selected || $workspaceIsPage.get()) {
    return null
  }

  const stored = ownerLookupSessionRows().find(s => sessionMatchesStoredId(s, selected))

  return { id: selected, profile: stored?.profile ?? '', title: stored ? storedSessionTitle(stored) : '' }
}

// The main tab drags like a session tile — drop it on a composer to link the
// chat, on a zone/edge to stack/split. Defers (`false`) to the generic pane
// move when there's no loaded session to carry.
const workspaceTabDrag = (event: ReactPointerEvent<HTMLElement>, onTap: () => void) => {
  const payload = workspaceDragPayload()

  if (!payload) {
    return false
  }

  startSessionDrag(payload, event, { onTap })

  return true
}

registry.registerMany([
  {
    id: 'sessions',
    area: 'panes',
    // String fallback only (menus, drag ghosts); the tab itself renders
    // `tabTitle` below so the strip follows `display.language` once it loads.
    title: translateNow('sidebar.sessions'),
    // Collapsible: leaves the grid on narrow viewports (edge overlay instead).
    // dock: where a RE-ADOPTED pane lands (healed from a stale dismissal) —
    // its default-ish spot beside main, not a random same-placement stack.
    data: {
      placement: 'left',
      collapsible: true,
      dock: { pane: 'workspace', pos: 'left' },
      revealAliases: ['chat-sidebar'],
      // Standing chrome: no close gestures at all — the tab is shown/hidden
      // (zone menu Show/Hide rows + the auto-registered ⌘K toggle below).
      hideOnly: true,
      tabTitle: () => <LocalizedTabTitle select={t => t.sidebar.sessions} />,
      tabTitleText: () => translateNow('sidebar.sessions'),
      width: `${SIDEBAR_DEFAULT_WIDTH}px`,
      minWidth: `${SIDEBAR_DEFAULT_WIDTH}px`,
      maxWidth: `${SIDEBAR_MAX_WIDTH}px`
    },
    render: () => <WiredPane part="sidebar" />
  },
  {
    id: 'workspace',
    area: 'panes',
    // Live-retitled to the loaded session by syncWorkspaceTitle below.
    title: NEW_SESSION_TITLE,
    data: {
      placement: 'main',
      minWidth: '22vw',
      tabDrag: workspaceTabDrag,
      tabActions: workspaceTabActions,
      tabWrap: wrapWorkspaceTab,
      uncloseable: true
    },
    render: renderWorkspacePane
  },
  {
    id: 'terminal',
    area: 'panes',
    // Register-time sample; the tab renders `tabTitle` (see sessions).
    title: translateNow('sidebar.terminal'),
    // height sizes the fixed track (a single-pane zone declaring a height is a
    // fixed track — the preset weight is moot): a short deck, not a third of
    // the window.
    //
    // NO minHeight: a tool panel drags all the way down to its collapsed
    // header (the sash floors it at COLLAPSED_ZONE_PX and folds the zone to
    // its rail there). A real floor left a sliver of unusable terminal.
    data: {
      placement: 'bottom',
      height: '20vh',
      maxHeight: '80vh',
      lifecycleKeepAlive: true,
      tabTitle: () => <LocalizedTabTitle select={t => t.sidebar.terminal} />,
      tabTitleText: () => translateNow('sidebar.terminal')
    },
    render: () => <WiredPane part="terminal" />
  },
  {
    id: 'files',
    area: 'panes',
    title: translateNow('sidebar.files'),
    // dock: re-adoption target after a stale dismissal (see sessions).
    data: {
      placement: 'right',
      collapsible: true,
      dock: { pane: 'workspace', pos: 'right' },
      revealAliases: ['file-browser'],
      width: FILE_BROWSER_DEFAULT_WIDTH,
      minWidth: FILE_BROWSER_MIN_WIDTH,
      maxWidth: FILE_BROWSER_MAX_WIDTH,
      tabTitle: () => <LocalizedTabTitle select={t => t.sidebar.files} />,
      tabTitleText: () => translateNow('sidebar.files')
    },
    render: () => idle(<FilesPane />)
  },
  {
    id: 'review',
    area: 'panes',
    title: translateNow('sidebar.review'),
    // The second right sidebar: hidden until ⌘G ($reviewOpen) — bound below
    // like the other chrome toggles; its zone collapses while hidden.
    data: {
      placement: 'right',
      collapsible: true,
      revealAliases: [REVIEW_PANE_ID],
      width: FILE_BROWSER_DEFAULT_WIDTH,
      minWidth: FILE_BROWSER_MIN_WIDTH,
      maxWidth: FILE_BROWSER_MAX_WIDTH,
      tabTitle: () => <LocalizedTabTitle select={t => t.sidebar.review} />,
      tabTitleText: () => translateNow('sidebar.review')
    },
    render: () => idle(<ReviewPaneContent />)
  }
])

// ---------------------------------------------------------------------------
// Chrome contributions. The title bar and status bar are fixed chrome outside
// the grid, composable through these areas. Everything real lives in the real
// components (TitlebarControls / useStatusbarItems). Sample PLUGIN
// contributions don't live here — they're their own files under `src/plugins/`,
// auto-discovered by discoverBundledPlugins() below.
// ---------------------------------------------------------------------------

registry.registerMany([
  // Titlebar center stays empty on purpose: session title lives in tabs +
  // sidebar; place/cwd lives in the sidebar project tree. Center is drag
  // chrome (plugins can still contribute to titleBar.center if needed).
  // Layout edit mode registers through the SAME declarative surfaces plugins
  // use: a rebindable keybind (collision-checked in the panel) + a ⌘K row
  // whose hotkey hint tracks the live binding.
  {
    id: 'layout.editMode',
    area: KEYBINDS_AREA,
    data: {
      id: 'layout.editMode',
      label: 'Toggle layout edit mode',
      defaults: ['mod+shift+\\'],
      run: toggleLayoutEditMode
    } satisfies KeybindContribution
  },
  paletteToggle({
    id: 'layout.editMode',
    label: 'Toggle layout edit mode',
    action: 'layout.editMode',
    icon: LayoutDashboard,
    keywords: ['layout', 'zones', 'panes', 'edit', 'rearrange'],
    get: () => $layoutEditMode.get(),
    set: enabled => $layoutEditMode.set(enabled)
  }),
  // The agent's write -> see loop: rescan <hermes home>/desktop-plugins
  // without relaunching (same-id reloads dispose the previous incarnation).
  {
    id: 'plugins.reload',
    area: PALETTE_AREA,
    data: {
      id: 'plugins.reload',
      label: 'Reload desktop plugins',
      keywords: ['plugins', 'reload', 'refresh', 'desktop'],
      run: () => void discoverRuntimePlugins()
    } satisfies PaletteContribution
  },
  // The core `::preview{file="…"}` transcript directive — the model (or a
  // skill) renders a workspace HTML file LIVE inside its own message
  // (sandboxed srcdoc iframe; falls back to the classic preview card for
  // non-HTML targets and remote gateways). Also the reference consumer for
  // the `transcript.directives` area plugins register into.
  {
    id: 'transcript.preview',
    area: TRANSCRIPT_DIRECTIVE_AREA,
    data: {
      name: 'preview',
      render: ({ attrs, streaming }) => <InlinePreviewDirective attrs={attrs} streaming={streaming} />
    } satisfies TranscriptDirectiveContribution
  },
  {
    id: 'layout.reset',
    area: PALETTE_AREA,
    data: {
      id: 'layout.reset',
      label: 'Reset layout',
      icon: LayoutDashboard,
      keywords: ['layout', 'reset', 'default', 'panes'],
      run: resetLayoutTree
    } satisfies PaletteContribution
  },
  // Hiding the bar removes the surface that would otherwise offer it back, so
  // ⌘K is the guaranteed door in (alongside the rebindable ⌘⇧S).
  paletteToggle({
    id: 'view.toggleStatusbar',
    label: 'Toggle status bar',
    action: 'view.toggleStatusbar',
    icon: PanelBottom,
    keywords: ['status bar', 'statusbar', 'bottom bar', 'hide', 'show', 'chrome'],
    get: () => $statusbarVisible.get(),
    set: enabled => $statusbarVisible.set(enabled)
  }),
  paletteToggle({
    id: 'view.toggleProfileRail',
    label: 'Toggle profile rail',
    action: 'view.toggleProfileRail',
    icon: Users,
    keywords: ['profile rail', 'profile bar', 'profile strip', 'profiles', 'sidebar', 'hide', 'show', 'chrome'],
    get: () => $profileRailVisible.get(),
    set: enabled => $profileRailVisible.set(enabled)
  }),
  // Simple hides most of the chrome that would offer the way back, so ⌘K is a
  // guaranteed door (alongside the layout editor and Settings → Appearance).
  paletteToggle({
    id: 'view.simpleMode',
    label: 'Simple mode',
    action: 'view.toggleSimpleMode',
    icon: SlidersHorizontal,
    keywords: ['simple', 'advanced', 'mode', 'interface', 'chrome', 'minimal', 'focus', 'distraction'],
    get: () => $interfaceMode.get() === 'simple',
    set: toggleSimpleMode
  }),
  paletteToggle({
    id: 'view.toggleTabStrip',
    label: 'Toggle tabs',
    action: 'view.toggleTabStrip',
    icon: PanelTop,
    keywords: ['tab strip', 'tab bar', 'tabs', 'header', 'zone', 'hide', 'show', 'chrome'],
    // On-screen truth for the zone the verbs target, not a stored flag: a zone
    // on auto has no stored value, and the row must read as "what pressing
    // this does to what I can see".
    get: () => Boolean(targetZoneTabStripVisible()),
    set: () => void toggleTargetZoneTabStrip()
  }),
  // The keybind panel's non-titlebar door (the keyboard icon is gone).
  {
    id: 'keybinds.panel',
    area: PALETTE_AREA,
    data: {
      id: 'keybinds.panel',
      label: 'Keyboard shortcuts',
      keywords: ['keybinds', 'shortcuts', 'hotkeys', 'keyboard'],
      run: () => window.dispatchEvent(new CustomEvent('hermes:open-keybinds'))
    } satisfies PaletteContribution
  },
  // Profile sharing: bundle the active profile (config, skills, theme, layout)
  // into a portable archive, or adopt someone else's. Both open native dialogs,
  // so the palette closing on select is correct.
  {
    id: 'profile.export',
    area: PALETTE_AREA,
    data: {
      id: 'profile.export',
      label: 'Export profile…',
      icon: Upload,
      keywords: ['profile', 'export', 'share', 'bundle', 'theme', 'settings', 'backup'],
      run: () => void runExportProfileFlow()
    } satisfies PaletteContribution
  },
  {
    id: 'profile.import',
    area: PALETTE_AREA,
    data: {
      id: 'profile.import',
      label: 'Import profile…',
      icon: Download,
      keywords: ['profile', 'import', 'share', 'bundle', 'archive', 'restore'],
      run: () => void runImportProfileFlow()
    } satisfies PaletteContribution
  }
])

registerLayoutPresets()

declareDefaultTree(DEFAULT_TREE, BASIC_TREE)

// Bundled plugins load AFTER core, so a same-id contribution from a plugin
// deliberately overrides the core default (last writer wins). Third-party
// runtime plugins will flow through the same discovery seam.
discoverBundledPlugins()

// Plugin panes join the tree by their `placement` hint the moment they
// register — incl. runtime plugins arriving seconds after boot. The FIRST
// pass runs as layout hydration: the reload prune→re-register cycle must not
// record split shares (#108679).
hydrateContributedPanes()

// Session + route (page) tiles: persisted splits register panes docked beside
// main. A popped-out Browser and the HUD have no layout tree — registering
// tiles there would still run, and preview-tile watching would try to dock
// into a tree this window never renders (and, in the HUD, paint a webview
// into the transparent overlay).
if (!isBrowserWindow() && !isHudWindow()) {
  watchSessionTiles()
  startUnrestoredTileTitleBackfill()
  startTileBackendIdentityGuard()
  watchRouteTiles()
  watchPreviewTiles()
}

// Mirror sidebar pins into the backend keep-flag so the auto-archive sweep
// never hides a pinned chat (and pre-existing pins migrate transparently).
watchSessionPins()

// Release unread-write guards once a list page confirms the value we wrote.
watchUnreadWriteGuard()

// Drop local pins/drafts/queued prompts whose sessions no longer exist on the
// backend — otherwise every boot re-requests the dead ids and 404s on each.
watchDeadSessionPrune()

// The main tab reads as its SESSION (the loaded title, "New session" on a
// fresh draft) — a stack of main + tiles is then just a row of session names.
// register() replaces same-id in place; the render fn is the shared constant
// above, so the pane content never remounts.
const syncWorkspaceTitle = () => {
  const selected = $selectedStoredSessionId.get()
  // Every loaded slice, not just recents: a telegram/discord/cron conversation
  // is listed ONLY in $messagingSessions / $cronSessions (recents excludes
  // those sources), so a recents-only scan missed the row and fell through to
  // the NEW_SESSION_TITLE placeholder — a loaded gateway chat titled
  // "New session" in the tab while its sidebar row read correctly.
  const stored = selected ? ownerLookupSessionRows().find(s => sessionMatchesStoredId(s, selected)) : null

  registry.register({
    id: 'workspace',
    area: 'panes',
    // The placeholder, not the draft's live name — `tabTitle` below renders
    // that. Keeping it here would re-register the pane on every keystroke.
    // A bot chat reads as its BOT: every canonical Bot Chat is stored under
    // the same name, which told two open bots apart by nothing (#99152).
    title: workspaceOwnerTitle(
      stored ? storedSessionTitle(stored) : NEW_SESSION_TITLE,
      selected ? $botChatScopes.get()[selected] : undefined
    ),
    data: {
      // The tab's status dot — the SAME primitive the sidebar row and session
      // tiles render, so the main tab never disagrees with its sidebar row. A
      // fresh draft has no session to key by, which IS its status: the dot
      // resolves to `draft` and marks the tab rather than leaving a hole.
      tabLead: () => <SessionStatusDot session={stored} storedSessionId={selected} />,
      // A draft's name lives in its composer, not in any session row, so the
      // label subscribes to it directly — typing renames the tab without
      // re-registering the pane.
      tabTitle: stored ? undefined : () => <SessionDraftTitle scope={selected} />,
      // Pages aren't tab-able: the main zone's bar stands down while one shows.
      headerVeto: $workspaceIsPage.get(),
      // Page-owned controls take the vetoed tab row. Deliberately NOT the
      // `titleBar.center` slot: that one stays mounted in the titlebar on every
      // route so plugin components (and their effects) survive navigation.
      headerContent:
        $workspaceIsPage.get() && registry.getArea(WORKSPACE_PAGE_HEADER_AREA).length
          ? () => <Slot area={WORKSPACE_PAGE_HEADER_AREA} />
          : undefined,
      placement: 'main',
      minWidth: '22vw',
      tabDrag: workspaceTabDrag,
      tabActions: workspaceTabActions,
      tabWrap: wrapWorkspaceTab,
      uncloseable: true
    },
    render: renderWorkspacePane
  })
}

$selectedStoredSessionId.listen(syncWorkspaceTitle)
$sessions.listen(syncWorkspaceTitle)
// The cron and messaging slices arrive on their OWN fetch, after a restored
// tab has already registered. Without these listens the workspace tab keeps
// whatever it resolved at register time — "New session" for a gateway chat.
$cronSessions.listen(syncWorkspaceTitle)
$messagingSessions.listen(syncWorkspaceTitle)
$botChatScopes.listen(syncWorkspaceTitle)
$workspaceOwnerLabels.listen(syncWorkspaceTitle)
$workspaceIsPage.listen(syncWorkspaceTitle)
registry.subscribeArea(WORKSPACE_PAGE_HEADER_AREA, syncWorkspaceTitle)

// Layout reset collapses every session tile into main as a tab (after the
// workspace) instead of re-scattering them — pre-placed before adoption.
registerLayoutResetHandler(stackSessionTilesIntoMain)

// ---------------------------------------------------------------------------
// Titlebar chrome toggles -> tree. The TitlebarControls buttons keep their
// store semantics ($sidebarOpen / $fileBrowserOpen / $panesFlipped); the tree
// reacts — a hidden pane's zone collapses (content stays mounted), the flip
// toggle mirrors the root row.
// ---------------------------------------------------------------------------

// HIDE-STYLE PANES (files, review, preview): the binding lives in the tree
// store — bindPaneVisibility — alongside bindToolPaneCollapse, so both are
// testable against the real function instead of a copy.

// TOOL PANELS (terminal, logs): the binding lives in the tree store —
// bindToolPaneCollapse — so the boot rule it encodes is testable against the
// real function instead of a copy. See its docblock for the semantics.

bindLayoutSides()

// Workspace-scoped surfaces: the file tree and git diff only mean something
// inside a project. A detached chat (no cwd) hides them — their zones
// collapse and the chat absorbs the width; picking a project brings them
// back. The terminal is NOT workspace-gated: unlike the old shell (where it
// rode the rail's row and vanished with it), its zone stands on its own.
const $hasWorkspace = computed($currentCwd, cwd => Boolean(cwd.trim()))

// The tree pane's own presence tracks ⌘J directly, not just the column's
// collapse — otherwise a pane revealed into that shared column would drag the
// tree along with it.
//
// Both get a CLOSER and an OPENER. The closer keeps ⌘J/⌘G truthful when the
// pane is closed from the tab menu; the opener is its mirror, so bringing the
// pane back through the tree (the toggle's reveal path, the rail, a preset)
// writes the store too. Without the opener the boolean went stale the moment
// anything but the toggle showed the pane — the divergence this whole change
// is about.
bindPaneVisibility(
  'files',
  computed([$hasWorkspace, $fileBrowserOpen], (workspace, open) => workspace && open),
  () => setFileBrowserOpen(false),
  () => setFileBrowserOpen(true)
)
// ⌘G — the review sidebar appears/disappears (and comes to the front).
bindPaneVisibility(
  'review',
  computed([$reviewOpen, $hasWorkspace], (open, workspace) => open && workspace),
  closeReview,
  () => openReview($reviewScopeCwd.get(), $reviewScopeTarget.get())
)
// ⌃` / statusbar toggle — the terminal COLLAPSES to a rail (tab stays), not
// hides; PTYs stay alive while collapsed (see PersistentTerminal). Simple has
// no terminal: where chrome is off a closed one hides, rail and all, and ⌃`
// is the door for the session.
bindToolPaneCollapse(
  'terminal',
  $terminalTakeover,
  () => setTerminalTakeover(false),
  () => setTerminalTakeover(true),
  $showsAdvancedChrome
)
// Without the statusbar, the rail is the only way to switch profiles or gateways.
$profiles.subscribe(profiles => setModeContext({ profileCount: profiles.length }))
$connectionsRegistry.subscribe(registry => setModeContext({ connectionCount: registry?.connections.length ?? 0 }))
// ⌘K door onto the same pane the keybind and statusbar pill flip.
registry.register(terminalPaletteToggle)

// Logs are ⌘K-ONLY chrome: the pane contribution EXISTS only while $logsOpen
// is on. Off (the default) keeps logs out of the registry and the tree
// entirely — no secondary tab riding the terminal strip, no preset or
// adoption path that resurrects it. Session-only on purpose (not persisted):
// a fresh boot never re-opens logs automatically. The palette toggle is the
// single door in; tab ✕ / ⌘W / the toggle itself remove it again.
const $logsOpen = atom(false)

let unregisterLogsPane: (() => void) | null = null

const syncLogsPane = (open: boolean) => {
  if (open) {
    unregisterLogsPane ??= registry.register({
      id: 'logs',
      area: 'panes',
      title: translateNow('sidebar.logs'),
      // Same tool-panel sizing rule as the terminal above — no minHeight, so
      // the sash floors it at COLLAPSED_ZONE_PX and folds the zone to its rail
      // rather than leaving a sliver. dock: its OWN zone beside the terminal —
      // never a tab in the terminal's strip.
      data: {
        placement: 'bottom',
        dock: { pane: 'terminal', pos: 'right' },
        height: '20vh',
        maxHeight: '80vh',
        tabTitle: () => <LocalizedTabTitle select={t => t.sidebar.logs} />,
        tabTitleText: () => translateNow('sidebar.logs')
      },
      render: () => idle(<LogsPane />)
    })
    // Summoning logs is explicit intent — front it (un-dismisses if a ✕ close
    // left a dismissal record behind).
    revealTreePane('logs')
  } else {
    unregisterLogsPane?.()
    unregisterLogsPane = null

    // No dismissal record — the next toggle-on must re-adopt cleanly. Also
    // sweeps 'logs' out of persisted trees from before it was summon-only.
    // Guarded: removePane rebuilds the tree even for an absent pane, and a
    // no-op boot sweep would commit (and persist) a fresh identical tree.
    const tree = $layoutTree.get()

    if (tree && allPaneIds(tree).includes('logs')) {
      removeTreePane('logs')
    }
  }
}

// Tool-panel tab semantics (✕ / ⌘W route through the store) so the palette
// toggle stays truthful either way.
markCollapsePane('logs')
registerPaneCloser('logs', () => $logsOpen.set(false))
registerPaneOpener('logs', () => $logsOpen.set(true))
syncLogsPane($logsOpen.get())
$logsOpen.listen(syncLogsPane)

registry.register(
  paletteToggle({
    id: 'logs.toggle',
    label: 'Toggle logs',
    icon: FileText,
    keywords: ['logs', 'agent log', 'tail', 'debug'],
    // On-screen, not the store's boolean. Summon-only keeps the two in step
    // while logs sits in its own zone, but the user can still drag it into the
    // terminal's strip or minimize its zone — and then `$logsOpen` reads true
    // with nothing visible, so the row would show "on" and its press would
    // spend itself re-asserting a value it already held.
    get: () => isPaneVisible('logs'),
    set: () => togglePaneVisible('logs')
  })
)

// Hide-only chrome tabs (sessions / Bots) get a ⌘K toggle each — the palette
// door onto the same show/hide the zone menu offers. Auto-registered from the
// panes area so a plugin's hideOnly pane (Bots registers at plugin load, after
// this module runs) gets its row for free; disposers keep it in step when a
// plugin unloads. Registry writes during a subscriber callback are safe (the
// registry snapshots per-area and re-notifies), and re-registering the same
// palette id replaces the row instead of stacking duplicates.
{
  const stripTabToggles = new Map<string, () => void>()

  const syncStripTabToggles = () => {
    const hideOnlyPanes = registry
      .getArea('panes')
      .filter(c => (c.data as { hideOnly?: boolean } | undefined)?.hideOnly)

    const wanted = new Set(hideOnlyPanes.map(c => c.id))

    for (const [paneId, dispose] of stripTabToggles) {
      if (!wanted.has(paneId)) {
        dispose()
        stripTabToggles.delete(paneId)
      }
    }

    for (const pane of hideOnlyPanes) {
      if (stripTabToggles.has(pane.id)) {
        continue
      }

      const title = String(pane.title ?? pane.id)

      stripTabToggles.set(
        pane.id,
        registry.register(
          paletteToggle({
            id: `strip-tab.${pane.id}`,
            label: translateNow('zones.toggleStripTab', title),
            icon: LayoutDashboard,
            keywords: [title.toLowerCase(), 'tab', 'pane', 'sidebar', 'show', 'hide'],
            // On-screen truth, same contract as the logs toggle above.
            get: () => isPaneVisible(pane.id),
            set: visible => {
              if (visible) {
                revealTreePane(pane.id)
              } else {
                setStripTabHidden(pane.id, true)
              }
            }
          })
        )
      )
    }
  }

  syncStripTabToggles()
  registry.subscribeArea('panes', syncStripTabToggles)
}

// YOLO (dangerous-command approval bypass) is a status-bar zap and a /yolo
// command; ⌘K is the third door onto the SAME store function, so a user who
// lives in the palette never has to hunt for the pill.
registry.register(
  paletteToggle({
    id: 'session.yolo',
    label: 'Toggle yolo',
    icon: Zap,
    keywords: ['yolo', 'approvals', 'auto-approve', 'bypass', 'dangerous', 'commands'],
    get: () => $yoloActive.get(),
    set: enabled => void setYoloEnabled(enabled).catch(() => undefined)
  })
)

// Sessions/files Close = collapse their SIDE (⌘B/⌘J truthful, titlebar button
// flips back) — but only while the pane actually lives in that root side
// column. Dragged next to main, a side collapse can't hide it (the collapse
// skips main-bearing children), so Close falls back to dismissal there —
// otherwise ⌘W/Close silently no-op. The sessions opener is the mirror: a
// preset that places the sidebar shows it, ⌘B truthful.
registerPaneCloser('sessions', () =>
  paneRootSide('sessions') === sidebarSide() ? setSidebarOpen(false) : dismissTreePane('sessions')
)
registerPaneOpener('sessions', () => {
  if (paneRootSide('sessions') === sidebarSide()) {
    setSidebarOpen(true)
  }
})
registerPaneCloser('files', () =>
  paneRootSide('files') === fileBrowserSide() ? setFileBrowserOpen(false) : dismissTreePane('files')
)

// ---------------------------------------------------------------------------

export function ContribController() {
  const sidebarOpen = useStore($sidebarOpen)
  const statusbarVisible = useStore($statusbarVisible)

  // HUD mode is the SAME app with its frame removed: the wiring (gateway,
  // sessions, streams, submit) mounts identically, and only the shell around
  // the chat surface differs. Branching here rather than at the window entry
  // is what keeps the HUD's composer the real composer.
  if (isHudWindow()) {
    return (
      <ContribWiring>
        <AppContextMenu />
        <HudShell />
        <PluginPreviewConsentDialog />
      </ContribWiring>
    )
  }

  if (isBrowserWindow()) {
    return (
      <ContribWiring>
        <BrowserPopoutShell />
        <PluginPreviewConsentDialog />
      </ContribWiring>
    )
  }

  return (
    <SidebarProvider
      className="h-full min-h-0 flex-col bg-background"
      onOpenChange={setSidebarOpen}
      open={sidebarOpen}
      style={{ '--sidebar-width': '100%' } as CSSProperties}
    >
      <ContribWiring>
        <AppContextMenu />
        {/* App-owned answer to a plugin's ctx.preview.executeJavaScript. Plugins
            run in every window kind, so each shell mounts it. */}
        <PluginPreviewConsentDialog />
        <div
          className="flex h-full min-h-0 w-screen flex-col bg-(--ui-bg-chrome) text-(--ui-text-primary)"
          // Window-glass hook: this div and the sidebar-wrapper above it are
          // the app shell's two full-window opaque painters; the
          // [data-hermes-glass] rules in styles.css clear them so the tint
          // painted by <body> is the only thing between the page and the
          // vibrancy material.
          data-contrib-shell=""
          style={{ '--titlebar-height': '0px' } as CSSProperties}
        >
          <LayoutTreeRoot titlebar />

          {/* "Close running tab?" — the busy/input-blocked tile close gate. */}
          <SessionTileCloseConfirm />

          {/* The REAL statusbar (model pill, command center, agents, …) with
              statusBar.left/right contributions merged in. Unmounted — not
              just hidden — while toggled off, so its 15s status poll and the
              per-turn readouts stop with it. */}
          {/* Notices registered through `registerButterbar` / `useButterbar`;
              renders nothing while none are registered. */}
          <Butterbar />
          <TermsButterbar />
          {statusbarVisible && <WiredPane part="statusbar" />}
        </div>
      </ContribWiring>
    </SidebarProvider>
  )
}

// Referenced type kept for plugin authors' reference (payload shape of
// statusBar.* contributions).
export type { StatusbarItem }