Skip to content

The agent that grows with you

3.1 KB
Raw Blame
import { setTerminalTakeover } from '@/app/right-sidebar/store'
import { isLayoutNode, type LayoutNode } from '@/components/pane-shell/tree/model'
import { applyLayoutPreset, LAYOUTS_AREA } from '@/components/pane-shell/tree/presets'
import { revealTreePane } from '@/components/pane-shell/tree/store'
import { registry } from '@/contrib/registry'

import { setFileBrowserOpen, setSidebarOpen } from './layout'
import { openReview } from './review'

// Explicit-request pane reveals, keyed to the backend `focus_pane` tool. Each
// entry drives the pane's own reveal path (some are toggle-bound) so a revealed
// pane matches a user-driven open. files/review are workspace-gated — a no-op
// without a project cwd, which is the honest behavior.
const PANE_REVEALERS: Record<string, () => void> = {
  chat: () => revealTreePane('workspace'),
  files: () => setFileBrowserOpen(true),
  review: () => openReview(),
  sessions: () => setSidebarOpen(true),
  terminal: () => setTerminalTakeover(true)
}

// The store setters above are same-value no-ops: `$open` already reads true
// while the pane sits in a zone the user minimized from its chevron, so the
// listener never fires and the tool reported success over an invisible pane
// (#106009). An explicit reveal also un-minimizes and fronts the tree pane.
const TREE_PANE_OF: Record<string, string> = {
  files: 'files',
  review: 'review',
  sessions: 'sessions',
  terminal: 'terminal'
}

/** Reveal a desktop pane by name. Returns false for an unknown pane. */
export function revealDesktopPane(pane: string): boolean {
  const reveal = PANE_REVEALERS[pane]

  if (!reveal) {
    return false
  }

  reveal()

  const treePane = TREE_PANE_OF[pane]

  if (treePane) {
    revealTreePane(treePane)
  }

  return true
}

// Elite is the terminal deck with the tools its card names open. The preset itself rests them, so the
// same deck picked in Settings stays quiet; a pick from the layout card or set_theme shows the difference.
const PICK_OPENS = new Map<string, readonly string[]>([['terminal-deck', ['terminal', 'files']]])

/** The panes a layout pick opens beyond its preset's resting set. */
export const layoutPickOpens = (preset: string): readonly string[] => PICK_OPENS.get(preset) ?? []

/** Open the panes `layoutPickOpens` names for `preset`. */
export function openLayoutPickPanes(preset: string): void {
  for (const pane of layoutPickOpens(preset)) {
    revealDesktopPane(pane)
  }
}

/** Apply a layout preset by id, resolved against the layouts contribution
 *  registry — the SAME list the layout picker renders, so core presets
 *  (default/focus/terminal-deck/quad), plugin presets, and user-saved presets
 *  are all addressable by the backend `set_theme` tool. Returns false for
 *  an unknown id. */
export function applyDesktopLayoutPreset(preset: string): boolean {
  if (!preset) {
    return false
  }

  const entry = registry
    .getArea(LAYOUTS_AREA)
    .find(candidate => candidate.id === preset && isLayoutNode(candidate.data))

  if (!entry) {
    return false
  }

  applyLayoutPreset(entry.id, entry.data as LayoutNode)

  return true
}