Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions server/agent-api/layout-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,17 @@ export class LayoutStore {
return undefined
}

findPaneByTerminalId(terminalId: string): { tabId: string; paneId: string } | undefined {
if (!this.snapshot) return undefined
for (const tab of this.snapshot.tabs) {
const root = this.snapshot.layouts?.[tab.id]
const leaves = this.collectLeaves(root, [])
const match = leaves.find((leaf) => leaf.content?.terminalId === terminalId)
if (match) return { tabId: tab.id, paneId: match.id }
}
return undefined
}

getPaneSnapshot(paneId: string): PaneSnapshot | undefined {
if (!this.snapshot) return undefined
for (const tab of this.snapshot.tabs) {
Expand Down
64 changes: 64 additions & 0 deletions server/agent-api/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ export function createAgentApiRouter({
}) {
const router = Router()

const broadcastReplayableUiCommand = (command: { command: string; payload?: any }) => {
if (typeof wsHandler?.broadcastUiCommandWithReplay === 'function') {
wsHandler.broadcastUiCommandWithReplay(command)
return
}
wsHandler?.broadcastUiCommand?.(command)
}

const resolvePaneTarget = (raw: string) => {
if (layoutStore.resolveTarget) {
const resolved = layoutStore.resolveTarget(raw)
Expand Down Expand Up @@ -338,6 +346,62 @@ export function createAgentApiRouter({
res.json(ok({ tabs, activeTabId }))
})

router.post('/terminals/:id/open', (req, res) => {
const terminalId = typeof req.params.id === 'string' ? req.params.id.trim() : ''
if (!terminalId) return res.status(400).json(fail('terminal id required'))
const term = registry.get?.(terminalId)
if (!term) return res.status(404).json(fail('terminal not found'))

const existing = layoutStore.findPaneByTerminalId?.(terminalId)
if (existing?.tabId && existing?.paneId) {
const result = layoutStore.selectPane?.(existing.tabId, existing.paneId) || existing
const paneId = result?.paneId || existing.paneId
const tabId = result?.tabId || existing.tabId
if (tabId && paneId) {
broadcastReplayableUiCommand({
command: 'tab.select',
payload: { id: tabId },
})
broadcastReplayableUiCommand({
command: 'pane.select',
payload: { tabId, paneId },
})
}
return res.json(ok({ tabId, paneId, terminalId, reused: true }, result?.message || 'terminal selected'))
}

if (!layoutStore.createTab || !layoutStore.attachPaneContent) {
return res.status(503).json(fail('layout store does not support terminal attach'))
}

const title = parseRequiredName(req.body?.name) || term.title || terminalId
const { tabId, paneId } = layoutStore.createTab({ title })
const paneContent = {
kind: 'terminal',
terminalId,
status: term.status || 'running',
mode: term.mode || 'shell',
initialCwd: term.cwd,
resumeSessionId: term.resumeSessionId,
}
layoutStore.attachPaneContent(tabId, paneId, paneContent)
broadcastReplayableUiCommand({
command: 'tab.create',
payload: {
id: tabId,
title,
mode: term.mode || 'shell',
terminalId,
initialCwd: term.cwd,
resumeSessionId: term.resumeSessionId,
paneId,
paneContent,
status: term.status || 'running',
},
})
res.json(ok({ tabId, paneId, terminalId, reused: false }, 'terminal opened'))
})

router.get('/panes', (req, res) => {
const tabId = req.query.tabId as string | undefined
const panes = layoutStore.listPanes?.(tabId) || []
Expand Down
Loading