feat(cli): add terminal create, split, rename, focus, close, tui-idle wait (#734)

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Jinwoo-H <jinwoo0825@gmail.com>
This commit is contained in:
Ramzi
2026-04-22 17:51:32 -07:00
committed by GitHub
co-authored by Claude Sonnet 4.6 Jinwoo-H
parent c07db38dfe
commit 4f7b488d22
13 changed files with 1534 additions and 50 deletions
@@ -32,6 +32,12 @@ import type { PtyTransport } from './pty-transport'
import { fitAndFocusPanes, fitPanes } from './pane-helpers'
import { registerRuntimeTerminalTab, scheduleRuntimeGraphSync } from '@/runtime/sync-runtime-graph'
import { e2eConfig } from '@/lib/e2e-config'
import {
SPLIT_TERMINAL_PANE_EVENT,
CLOSE_TERMINAL_PANE_EVENT,
type SplitTerminalPaneDetail,
type CloseTerminalPaneDetail
} from '@/constants/terminal'
type UseTerminalPaneLifecycleDeps = {
tabId: string
@@ -668,7 +674,56 @@ export function useTerminalPaneLifecycle({
persistLayoutSnapshot()
scheduleRuntimeGraphSync()
// Why: CLI-driven splits go through splitPaneWithOneShotStartup so the
// startup command is delivered via the PTY connection path (which waits
// for shell readiness) instead of terminal.paste() which can lose input
// if the shell hasn't started reading stdin yet.
function onCliSplitPane(event: Event): void {
const detail = (event as CustomEvent<SplitTerminalPaneDetail>).detail
if (!detail?.tabId || detail.tabId !== tabId) {
return
}
const mgr = managerRef.current
if (!mgr) {
return
}
if (detail.command) {
splitPaneWithOneShotStartup(ptyDeps, { command: detail.command }, () =>
mgr.splitPane(detail.paneRuntimeId, detail.direction)
)
} else {
mgr.splitPane(detail.paneRuntimeId, detail.direction)
}
}
window.addEventListener(SPLIT_TERMINAL_PANE_EVENT, onCliSplitPane)
// Why: CLI-driven pane close dispatches a CustomEvent so PaneManager handles
// sibling promotion in split layouts. Falls back to closing the whole tab
// when the target pane is the only one remaining.
function onCliClosePane(event: Event): void {
const detail = (event as CustomEvent<CloseTerminalPaneDetail>).detail
if (!detail?.tabId || detail.tabId !== tabId) {
return
}
const mgr = managerRef.current
if (!mgr) {
return
}
if (mgr.getPanes().length <= 1) {
useAppStore.getState().closeTab(tabId)
} else {
mgr.closePane(detail.paneRuntimeId)
scheduleRuntimeGraphSync()
syncCanExpandState()
queueResizeAll(isActive)
persistLayoutSnapshot()
}
}
window.addEventListener(CLOSE_TERMINAL_PANE_EVENT, onCliClosePane)
return () => {
window.removeEventListener(SPLIT_TERMINAL_PANE_EVENT, onCliSplitPane)
window.removeEventListener(CLOSE_TERMINAL_PANE_EVENT, onCliClosePane)
const tabStillExists = Boolean(
useAppStore
.getState()
+14
View File
@@ -1,5 +1,7 @@
export const TOGGLE_TERMINAL_PANE_EXPAND_EVENT = 'orca-toggle-terminal-pane-expand'
export const FOCUS_TERMINAL_PANE_EVENT = 'orca-focus-terminal-pane'
export const SPLIT_TERMINAL_PANE_EVENT = 'orca-split-terminal-pane'
export const CLOSE_TERMINAL_PANE_EVENT = 'orca-close-terminal-pane'
export type ToggleTerminalPaneExpandDetail = {
tabId: string
@@ -9,3 +11,15 @@ export type FocusTerminalPaneDetail = {
tabId: string
paneId: number
}
export type SplitTerminalPaneDetail = {
tabId: string
paneRuntimeId: number
direction: 'horizontal' | 'vertical'
command?: string
}
export type CloseTerminalPaneDetail = {
tabId: string
paneRuntimeId: number
}
+216
View File
@@ -153,6 +153,13 @@ describe('useIpcEvents updater integration', () => {
onJumpToWorktreeIndex: () => () => {},
onWorktreeHistoryNavigate: () => () => {},
onActivateWorktree: () => () => {},
onCreateTerminal: () => () => {},
onRequestTerminalCreate: () => () => {},
replyTerminalCreate: () => {},
onSplitTerminal: () => () => {},
onRenameTerminal: () => () => {},
onFocusTerminal: () => () => {},
onCloseTerminal: () => () => {},
onNewBrowserTab: () => () => {},
onRequestTabCreate: () => () => {},
replyTabCreate: () => {},
@@ -323,6 +330,13 @@ describe('useIpcEvents updater integration', () => {
onJumpToWorktreeIndex: () => () => {},
onWorktreeHistoryNavigate: () => () => {},
onActivateWorktree: () => () => {},
onCreateTerminal: () => () => {},
onRequestTerminalCreate: () => () => {},
replyTerminalCreate: () => {},
onSplitTerminal: () => () => {},
onRenameTerminal: () => () => {},
onFocusTerminal: () => () => {},
onCloseTerminal: () => () => {},
onNewBrowserTab: () => () => {},
onRequestTabCreate: () => () => {},
replyTabCreate: () => {},
@@ -386,6 +400,180 @@ describe('useIpcEvents updater integration', () => {
expect(clearTabPtyId).toHaveBeenCalledWith('tab-1')
expect(clearTabPtyId).not.toHaveBeenCalledWith('tab-2')
})
it('activates the target worktree when CLI creates a terminal there', async () => {
const createTab = vi.fn(() => ({ id: 'tab-new' }))
const setActiveView = vi.fn()
const setActiveWorktree = vi.fn()
const setActiveTabType = vi.fn()
const setActiveTab = vi.fn()
const revealWorktreeInSidebar = vi.fn()
const setTabCustomTitle = vi.fn()
const queueTabStartupCommand = vi.fn()
const createTerminalListenerRef: {
current: ((data: { worktreeId: string; command?: string; title?: string }) => void) | null
} = { current: null }
vi.resetModules()
vi.unstubAllGlobals()
vi.doMock('react', async () => {
const actual = await vi.importActual<typeof ReactModule>('react')
return {
...actual,
useEffect: (effect: () => void | (() => void)) => {
effect()
}
}
})
vi.doMock('../store', () => ({
useAppStore: {
getState: () => ({
setUpdateStatus: vi.fn(),
createTab,
setActiveView,
setActiveWorktree,
setActiveTabType,
setActiveTab,
revealWorktreeInSidebar,
setTabCustomTitle,
queueTabStartupCommand,
fetchRepos: vi.fn(),
fetchWorktrees: vi.fn(),
activeModal: null,
closeModal: vi.fn(),
openModal: vi.fn(),
activeWorktreeId: 'wt-1',
activeView: 'terminal',
setActiveRepo: vi.fn(),
setIsFullScreen: vi.fn(),
updateBrowserPageState: vi.fn(),
activeTabType: 'terminal',
editorFontZoomLevel: 0,
setEditorFontZoomLevel: vi.fn(),
setRateLimitsFromPush: vi.fn(),
setSshConnectionState: vi.fn(),
setSshTargetLabels: vi.fn(),
enqueueSshCredentialRequest: vi.fn(),
removeSshCredentialRequest: vi.fn(),
clearTabPtyId: vi.fn(),
settings: { terminalFontSize: 13 }
})
}
}))
vi.doMock('@/lib/ui-zoom', () => ({
applyUIZoom: vi.fn()
}))
vi.doMock('@/lib/worktree-activation', () => ({
activateAndRevealWorktree: vi.fn(),
ensureWorktreeHasInitialTerminal: vi.fn()
}))
vi.doMock('@/components/sidebar/visible-worktrees', () => ({
getVisibleWorktreeIds: () => []
}))
vi.doMock('@/lib/editor-font-zoom', () => ({
nextEditorFontZoomLevel: vi.fn(() => 0),
computeEditorFontSize: vi.fn(() => 13)
}))
vi.doMock('@/components/settings/SettingsConstants', () => ({
zoomLevelToPercent: vi.fn(() => 100),
ZOOM_MIN: -3,
ZOOM_MAX: 3
}))
vi.doMock('@/lib/zoom-events', () => ({
dispatchZoomLevelChanged: vi.fn()
}))
vi.stubGlobal('window', {
api: {
repos: { onChanged: () => () => {} },
worktrees: { onChanged: () => () => {} },
ui: {
onOpenSettings: () => () => {},
onToggleLeftSidebar: () => () => {},
onToggleRightSidebar: () => () => {},
onToggleWorktreePalette: () => () => {},
onOpenQuickOpen: () => () => {},
onOpenNewWorkspace: () => () => {},
onJumpToWorktreeIndex: () => () => {},
onActivateWorktree: () => () => {},
onWorktreeHistoryNavigate: () => () => {},
onCreateTerminal: (
listener: (data: { worktreeId: string; command?: string; title?: string }) => void
) => {
createTerminalListenerRef.current = listener
return () => {}
},
onRequestTerminalCreate: () => () => {},
replyTerminalCreate: () => {},
onSplitTerminal: () => () => {},
onRenameTerminal: () => () => {},
onFocusTerminal: () => () => {},
onCloseTerminal: () => () => {},
onNewBrowserTab: () => () => {},
onRequestTabCreate: () => () => {},
replyTabCreate: () => {},
onRequestTabClose: () => () => {},
replyTabClose: vi.fn(),
onNewTerminalTab: () => () => {},
onCloseActiveTab: () => () => {},
onSwitchTab: () => () => {},
onToggleStatusBar: () => () => {},
onFullscreenChanged: () => () => {},
onTerminalZoom: () => () => {},
getZoomLevel: () => 0,
set: vi.fn()
},
updater: {
getStatus: () => Promise.resolve({ state: 'idle' }),
onStatus: () => () => {},
onClearDismissal: () => () => {}
},
browser: {
onGuestLoadFailed: () => () => {},
onOpenLinkInOrcaTab: () => () => {},
onNavigationUpdate: () => () => {},
onActivateView: () => () => {}
},
rateLimits: {
get: () => Promise.resolve({ limits: {}, lastUpdatedAt: Date.now() }),
onUpdate: () => () => {}
},
ssh: {
listTargets: () => Promise.resolve([]),
getState: () => Promise.resolve(null),
onStateChanged: () => () => {},
onCredentialRequest: () => () => {},
onCredentialResolved: () => () => {}
}
}
})
const { useIpcEvents } = await import('./useIpcEvents')
useIpcEvents()
await Promise.resolve()
if (typeof createTerminalListenerRef.current !== 'function') {
throw new Error('Expected create-terminal listener to be registered')
}
createTerminalListenerRef.current({
worktreeId: 'wt-2',
title: 'Runner',
command: 'opencode'
})
expect(setActiveView).toHaveBeenCalledWith('terminal')
expect(setActiveWorktree).toHaveBeenCalledWith('wt-2')
expect(createTab).toHaveBeenCalledWith('wt-2')
expect(setActiveTabType).toHaveBeenCalledWith('terminal')
expect(setActiveTab).toHaveBeenCalledWith('tab-new')
expect(revealWorktreeInSidebar).toHaveBeenCalledWith('wt-2')
expect(setTabCustomTitle).toHaveBeenCalledWith('tab-new', 'Runner')
expect(queueTabStartupCommand).toHaveBeenCalledWith('tab-new', { command: 'opencode' })
})
})
describe('useIpcEvents browser tab close routing', () => {
@@ -496,6 +684,13 @@ describe('useIpcEvents browser tab close routing', () => {
onJumpToWorktreeIndex: () => () => {},
onWorktreeHistoryNavigate: () => () => {},
onActivateWorktree: () => () => {},
onCreateTerminal: () => () => {},
onRequestTerminalCreate: () => () => {},
replyTerminalCreate: () => {},
onSplitTerminal: () => () => {},
onRenameTerminal: () => () => {},
onFocusTerminal: () => () => {},
onCloseTerminal: () => () => {},
onNewBrowserTab: () => () => {},
onRequestTabCreate: () => () => {},
replyTabCreate: () => {},
@@ -662,6 +857,13 @@ describe('useIpcEvents browser tab close routing', () => {
onJumpToWorktreeIndex: () => () => {},
onWorktreeHistoryNavigate: () => () => {},
onActivateWorktree: () => () => {},
onCreateTerminal: () => () => {},
onRequestTerminalCreate: () => () => {},
replyTerminalCreate: () => {},
onSplitTerminal: () => () => {},
onRenameTerminal: () => () => {},
onFocusTerminal: () => () => {},
onCloseTerminal: () => () => {},
onNewBrowserTab: () => () => {},
onRequestTabCreate: () => () => {},
replyTabCreate: () => {},
@@ -823,6 +1025,13 @@ describe('useIpcEvents browser tab close routing', () => {
onJumpToWorktreeIndex: () => () => {},
onWorktreeHistoryNavigate: () => () => {},
onActivateWorktree: () => () => {},
onCreateTerminal: () => () => {},
onRequestTerminalCreate: () => () => {},
replyTerminalCreate: () => {},
onSplitTerminal: () => () => {},
onRenameTerminal: () => () => {},
onFocusTerminal: () => () => {},
onCloseTerminal: () => () => {},
onNewBrowserTab: () => () => {},
onRequestTabCreate: () => () => {},
replyTabCreate: () => {},
@@ -1002,6 +1211,13 @@ describe('useIpcEvents shortcut hint clearing', () => {
},
onWorktreeHistoryNavigate: () => () => {},
onActivateWorktree: () => () => {},
onCreateTerminal: () => () => {},
onRequestTerminalCreate: () => () => {},
replyTerminalCreate: () => {},
onSplitTerminal: () => () => {},
onRenameTerminal: () => () => {},
onFocusTerminal: () => () => {},
onCloseTerminal: () => () => {},
onNewBrowserTab: () => () => {},
onRequestTabCreate: () => () => {},
replyTabCreate: () => {},
+98
View File
@@ -6,6 +6,8 @@ import {
activateAndRevealWorktree,
ensureWorktreeHasInitialTerminal
} from '@/lib/worktree-activation'
import { SPLIT_TERMINAL_PANE_EVENT, CLOSE_TERMINAL_PANE_EVENT } from '@/constants/terminal'
import type { SplitTerminalPaneDetail, CloseTerminalPaneDetail } from '@/constants/terminal'
import { getVisibleWorktreeIds } from '@/components/sidebar/visible-worktrees'
import { nextEditorFontZoomLevel, computeEditorFontSize } from '@/lib/editor-font-zoom'
import type { UpdateStatus } from '../../../shared/types'
@@ -155,6 +157,102 @@ export function useIpcEvents(): void {
})
)
unsubs.push(
window.api.ui.onCreateTerminal(({ worktreeId, command, title }) => {
const store = useAppStore.getState()
store.setActiveView('terminal')
store.setActiveWorktree(worktreeId)
const tab = store.createTab(worktreeId)
store.setActiveTabType('terminal')
store.setActiveTab(tab.id)
store.revealWorktreeInSidebar(worktreeId)
if (title) {
store.setTabCustomTitle(tab.id, title)
}
if (command) {
store.queueTabStartupCommand(tab.id, { command })
}
})
)
// Why: CLI-driven terminal creation sends a request and waits for the
// tabId reply so it can resolve a handle the caller can use immediately.
// This mirrors the browser's onRequestTabCreate/replyTabCreate pattern.
unsubs.push(
window.api.ui.onRequestTerminalCreate((data) => {
try {
const store = useAppStore.getState()
const worktreeId = data.worktreeId ?? store.activeWorktreeId
if (!worktreeId) {
window.api.ui.replyTerminalCreate({
requestId: data.requestId,
error: 'No active worktree'
})
return
}
store.setActiveView('terminal')
store.setActiveWorktree(worktreeId)
const tab = store.createTab(worktreeId)
store.setActiveTabType('terminal')
store.setActiveTab(tab.id)
store.revealWorktreeInSidebar(worktreeId)
if (data.title) {
store.setTabCustomTitle(tab.id, data.title)
}
if (data.command) {
store.queueTabStartupCommand(tab.id, { command: data.command })
}
window.api.ui.replyTerminalCreate({
requestId: data.requestId,
tabId: tab.id,
title: data.title ?? tab.title
})
} catch (err) {
window.api.ui.replyTerminalCreate({
requestId: data.requestId,
error: err instanceof Error ? err.message : 'Terminal creation failed'
})
}
})
)
unsubs.push(
window.api.ui.onSplitTerminal(({ tabId, paneRuntimeId, direction, command }) => {
const detail: SplitTerminalPaneDetail = { tabId, paneRuntimeId, direction, command }
window.dispatchEvent(new CustomEvent(SPLIT_TERMINAL_PANE_EVENT, { detail }))
})
)
unsubs.push(
window.api.ui.onRenameTerminal(({ tabId, title }) => {
useAppStore.getState().setTabCustomTitle(tabId, title)
})
)
unsubs.push(
window.api.ui.onFocusTerminal(({ tabId, worktreeId }) => {
const store = useAppStore.getState()
store.setActiveWorktree(worktreeId)
store.setActiveView('terminal')
store.setActiveTab(tabId)
store.revealWorktreeInSidebar(worktreeId)
})
)
unsubs.push(
window.api.ui.onCloseTerminal(({ tabId, paneRuntimeId }) => {
if (paneRuntimeId != null) {
// Why: when targeting a specific pane in a split layout, dispatch to the
// lifecycle hook so PaneManager.closePane() handles sibling promotion.
// The lifecycle hook falls through to closeTab() if this is the last pane.
const detail: CloseTerminalPaneDetail = { tabId, paneRuntimeId }
window.dispatchEvent(new CustomEvent(CLOSE_TERMINAL_PANE_EVENT, { detail }))
} else {
useAppStore.getState().closeTab(tabId)
}
})
)
// Hydrate initial update status then subscribe to changes
window.api.updater.getStatus().then((status) => {
useAppStore.getState().setUpdateStatus(status as UpdateStatus)