๐ Built using [Crystal](https://github.com/stravu/crystal) Co-Authored-By: Crystal <crystal@stravu.com>
5.0 KiB
5.0 KiB
Tool Panel System Implementation Guidelines
โ ๏ธ IMPORTANT: The tool panel system implements a flexible, extensible architecture for managing multiple tool instances per session.
Architecture Overview
The tool panel system consists of several key components:
- Panel Manager (
main/src/services/panelManager.ts): Central coordinator for panel lifecycle management - Terminal Panel Manager (
main/src/services/terminalPanelManager.ts): Specialized handler for terminal panel processes - Panel Event Bus (
main/src/services/panelEventBus.ts): Event communication system between panels - Panel Store (
frontend/src/stores/panelStore.ts): Frontend state management with Zustand - Panel Components: React components for rendering different panel types
Key Implementation Principles
- Lazy Initialization: Panels are created in the database immediately but background processes (like terminal PTY) only start when the panel is first viewed
- State Persistence: All panel state including terminal scrollback, working directories, and configurations persist across application restarts
- Memory Efficiency: Inactive panels suspend rendering but maintain background processes
- Event-Driven Updates: Uses IPC events to synchronize state between main and renderer processes
- Extensible Design: Architecture supports future panel types beyond terminals
Panel Lifecycle
- Creation: User clicks "Add Tool" โ Panel entry created in database โ Added to UI
- First View: User clicks panel tab โ Background process initializes โ XTerm.js mounts
- Switching: User switches panels โ Previous panel unmounts XTerm โ New panel mounts
- Background Operation: Processes continue running even when panel is not visible
- Deletion: Panel closed โ Process terminated โ Database entry removed โ UI updated
Terminal Panel Specifics
- Each terminal panel spawns an independent PTY process using node-pty
- Terminal state (scrollback, history, dimensions) persists in
tool_panels.stateas JSON - XTerm.js instances mount/unmount based on panel visibility to save memory
- Working directories are maintained independently per panel
- Command history and environment variables can be preserved across restarts
Database Schema
tool_panelstable stores panel configuration and statesessions.active_panel_idtracks the currently active panel per session- Foreign key constraints ensure panels are cleaned up when sessions are deleted
Event System
- Terminal panels emit
terminal:command_executed,terminal:exit, andfiles:changedevents - Event bus routes events to subscribed panels (planned for future panel types)
- Events support future inter-panel communication (e.g., diff panels reacting to file changes)
Implemented Panel Types
The tool panel system currently supports 8 panel types:
- Terminal (
terminal) - Multiple PTY shells per session
- Independent processes with command history and scrollback
- File operation detection triggers
files:changedevents
- Claude (
claude) - Multiple Claude Code instances
- Managed by
claudePanelManager.ts - Permission mode: approve/ignore
- Automatic context tracking with token usage display
- Codex (
codex) - Multiple Codex CLI instances
- Managed by
codexPanelManager.ts - Configurable model provider and approval policy
- Sandbox mode and web search support
- Diff (
diff) - Git diff viewer (singleton, permanent)
- View modes: split/unified
- Whitespace and context line configuration
- Auto-refreshes on file changes
- Editor (
editor) - In-app file editor
- Syntax highlighting with Monaco
- File tree with expanded directory tracking
- Cursor position and scroll persistence
- Logs (
logs) - Script execution panel (singleton)
- Process management with PID tracking
- Output buffer with error/warning counts
- Start/end time and exit code tracking
- Dashboard (
dashboard) - Project overview (singleton, permanent, projects-only)
- Session health monitoring
- Filter by status: all/stale/changes/pr
- Cached data persistence
- Setup Tasks (
setup-tasks) - Project setup checklist (singleton, permanent, projects-only)
- Task completion tracking
- Dismissible tasks
- Last check timestamp
Panel Capabilities
Each panel type has specific capabilities defined in PANEL_CAPABILITIES:
- requiresProcess: Whether panel needs background process (terminal, claude, codex, logs)
- singleton: Only one instance per session (diff, logs, dashboard, setup-tasks)
- permanent: Cannot be closed by user (diff, dashboard, setup-tasks)
- canAppearInProjects: Available in project view (all except diff)
- canAppearInWorktrees: Available in worktree sessions (all except dashboard, setup-tasks)
- canEmit: Events this panel can produce
- canConsume: Events this panel listens to
Example: Diff panels are singleton + permanent + worktree-only, consuming files:changed and terminal:command_executed events to auto-refresh.