2026-04-18 18:33:58 +09:00
|
|
|
import { log } from "../../../shared"
|
2026-04-28 10:46:32 +09:00
|
|
|
import { shellSingleQuote } from "../../../shared/shell-env"
|
|
|
|
|
import { isServerRunning, runTmuxCommand } from "../../../shared/tmux"
|
2026-04-18 18:33:58 +09:00
|
|
|
import { getTmuxPath } from "../../../tools/interactive-bash/tmux-path-resolver"
|
|
|
|
|
import type { TmuxSessionManager } from "../../tmux-subagent/manager"
|
2026-04-28 10:46:32 +09:00
|
|
|
import { resolveCallerTmuxSession } from "./resolve-caller-tmux-session"
|
2026-04-18 18:33:58 +09:00
|
|
|
|
2026-04-28 10:46:32 +09:00
|
|
|
type TeamLayoutMember = { name: string; sessionId: string; worktreePath?: string }
|
2026-04-18 18:33:58 +09:00
|
|
|
|
2026-04-28 10:46:32 +09:00
|
|
|
export type TeamLayoutResult = {
|
2026-04-18 18:33:58 +09:00
|
|
|
focusWindowId: string
|
2026-05-06 17:17:23 +09:00
|
|
|
gridWindowId?: string
|
2026-04-28 10:46:32 +09:00
|
|
|
focusPanesByMember: Record<string, string>
|
|
|
|
|
gridPanesByMember: Record<string, string>
|
|
|
|
|
targetSessionId: string
|
|
|
|
|
ownedSession: boolean
|
2026-04-18 18:33:58 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-28 10:46:32 +09:00
|
|
|
export type TeamLayoutCleanupTarget = {
|
|
|
|
|
ownedSession: boolean
|
|
|
|
|
targetSessionId: string
|
|
|
|
|
focusWindowId?: string
|
|
|
|
|
gridWindowId?: string
|
|
|
|
|
paneIds?: Array<string>
|
2026-04-18 18:33:58 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-28 10:46:32 +09:00
|
|
|
export function canVisualize(): boolean { return process.env.TMUX !== undefined }
|
2026-04-18 18:33:58 +09:00
|
|
|
|
2026-04-28 10:46:32 +09:00
|
|
|
function getPaneWorkingDirectory(member: TeamLayoutMember): string {
|
|
|
|
|
return member.worktreePath ?? process.cwd()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function buildAttachCommand(member: TeamLayoutMember, serverUrl: string): string {
|
|
|
|
|
return `opencode attach ${shellSingleQuote(serverUrl)} --session ${shellSingleQuote(member.sessionId)} --dir ${shellSingleQuote(getPaneWorkingDirectory(member))}`
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-06 17:17:23 +09:00
|
|
|
async function listPanesInWindow(tmuxPath: string, windowTarget: string): Promise<Array<string>> {
|
|
|
|
|
const result = await runTmuxCommand(tmuxPath, ["list-panes", "-t", windowTarget, "-F", "#{pane_id}"])
|
2026-04-28 10:46:32 +09:00
|
|
|
if (!result.success || !result.output) return []
|
|
|
|
|
return result.output.trim().split("\n").filter(Boolean)
|
|
|
|
|
}
|
2026-04-18 18:33:58 +09:00
|
|
|
|
2026-05-06 17:17:23 +09:00
|
|
|
function selectExistingTeammatePane(teammatePanes: Array<string>, callerPaneId: string): string {
|
|
|
|
|
return teammatePanes[Math.floor(teammatePanes.length / 2)] ?? teammatePanes[teammatePanes.length - 1] ?? callerPaneId
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function buildSplitArgs(callerPaneId: string, teammatePanes: Array<string>, member: TeamLayoutMember): Array<string> {
|
|
|
|
|
if (teammatePanes.length === 0) {
|
|
|
|
|
return ["split-window", "-t", callerPaneId, "-h", "-l", "70%", "-P", "-F", "#{pane_id}", "-c", getPaneWorkingDirectory(member)]
|
2026-05-04 19:06:21 +09:00
|
|
|
}
|
2026-04-28 10:46:32 +09:00
|
|
|
|
2026-05-06 17:17:23 +09:00
|
|
|
return [
|
|
|
|
|
"split-window",
|
|
|
|
|
"-t",
|
|
|
|
|
selectExistingTeammatePane(teammatePanes, callerPaneId),
|
|
|
|
|
teammatePanes.length % 2 === 1 ? "-v" : "-h",
|
|
|
|
|
"-P",
|
|
|
|
|
"-F",
|
|
|
|
|
"#{pane_id}",
|
|
|
|
|
"-c",
|
|
|
|
|
getPaneWorkingDirectory(member),
|
|
|
|
|
]
|
|
|
|
|
}
|
2026-04-28 10:46:32 +09:00
|
|
|
|
2026-05-06 17:17:23 +09:00
|
|
|
async function createTeamLayoutInCallerWindow(
|
|
|
|
|
tmuxPath: string,
|
|
|
|
|
callerPaneId: string,
|
|
|
|
|
windowTarget: string,
|
|
|
|
|
members: Array<TeamLayoutMember>,
|
|
|
|
|
serverUrl: string,
|
|
|
|
|
): Promise<{ focusWindowId: string; focusPanesByMember: Record<string, string> } | null> {
|
|
|
|
|
const panesByMember: Record<string, string> = {}
|
|
|
|
|
const existingPanes = await listPanesInWindow(tmuxPath, windowTarget)
|
|
|
|
|
let teammatePanes = existingPanes.filter((paneId) => paneId !== callerPaneId)
|
2026-04-28 10:46:32 +09:00
|
|
|
|
2026-05-04 19:06:21 +09:00
|
|
|
for (const member of members) {
|
2026-05-06 17:17:23 +09:00
|
|
|
const split = await runTmuxCommand(tmuxPath, buildSplitArgs(callerPaneId, teammatePanes, member))
|
|
|
|
|
if (!split.success || !split.output) return null
|
|
|
|
|
|
|
|
|
|
const paneId = split.output.trim()
|
|
|
|
|
teammatePanes = [...teammatePanes, paneId]
|
|
|
|
|
panesByMember[member.name] = paneId
|
2026-05-04 19:06:21 +09:00
|
|
|
await runTmuxCommand(tmuxPath, ["select-pane", "-t", paneId, "-T", member.name])
|
|
|
|
|
await runTmuxCommand(tmuxPath, ["send-keys", "-t", paneId, buildAttachCommand(member, serverUrl), "Enter"])
|
2026-04-28 10:46:32 +09:00
|
|
|
}
|
2026-05-04 19:06:21 +09:00
|
|
|
|
2026-05-06 17:17:23 +09:00
|
|
|
const layoutResult = await runTmuxCommand(tmuxPath, ["select-layout", "-t", windowTarget, "main-vertical"])
|
|
|
|
|
if (!layoutResult.success) return null
|
|
|
|
|
|
|
|
|
|
const resizeResult = await runTmuxCommand(tmuxPath, ["resize-pane", "-t", callerPaneId, "-x", "30%"])
|
|
|
|
|
if (!resizeResult.success) return null
|
|
|
|
|
|
|
|
|
|
return { focusWindowId: windowTarget, focusPanesByMember: panesByMember }
|
2026-04-18 18:33:58 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-28 10:46:32 +09:00
|
|
|
export async function createTeamLayout(teamRunId: string, members: Array<TeamLayoutMember>, tmuxMgr: TmuxSessionManager): Promise<TeamLayoutResult | null> {
|
2026-04-18 18:33:58 +09:00
|
|
|
if (!canVisualize()) {
|
|
|
|
|
log("tmux visualization unavailable, skipping")
|
|
|
|
|
return null
|
|
|
|
|
}
|
2026-04-28 10:46:32 +09:00
|
|
|
if (members.length === 0) return null
|
2026-04-18 18:33:58 +09:00
|
|
|
|
|
|
|
|
try {
|
2026-04-28 10:46:32 +09:00
|
|
|
const serverUrl = tmuxMgr.getServerUrl()
|
|
|
|
|
if (!(await isServerRunning(serverUrl))) {
|
|
|
|
|
log("opencode server not reachable, skipping team layout", { serverUrl })
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-18 18:33:58 +09:00
|
|
|
const tmuxPath = await getTmuxPath()
|
|
|
|
|
if (!tmuxPath) {
|
|
|
|
|
log("tmux visualization unavailable, skipping")
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-28 10:46:32 +09:00
|
|
|
const callerSession = await resolveCallerTmuxSession(tmuxPath)
|
2026-05-06 17:17:23 +09:00
|
|
|
if (!callerSession) {
|
|
|
|
|
log("tmux visualization requires a resolvable caller tmux pane, skipping", { teamRunId })
|
|
|
|
|
return null
|
2026-04-28 10:46:32 +09:00
|
|
|
}
|
|
|
|
|
|
2026-05-06 17:17:23 +09:00
|
|
|
const focus = await createTeamLayoutInCallerWindow(tmuxPath, callerSession.paneId, callerSession.windowTarget, members, serverUrl)
|
|
|
|
|
if (!focus) return null
|
2026-04-18 18:33:58 +09:00
|
|
|
|
|
|
|
|
return {
|
2026-05-06 17:17:23 +09:00
|
|
|
focusWindowId: focus.focusWindowId,
|
|
|
|
|
gridWindowId: undefined,
|
|
|
|
|
focusPanesByMember: focus.focusPanesByMember,
|
|
|
|
|
gridPanesByMember: {},
|
|
|
|
|
targetSessionId: callerSession.sessionId,
|
|
|
|
|
ownedSession: false,
|
2026-04-18 18:33:58 +09:00
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
log("tmux visualization unavailable, skipping", { error: String(error) })
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-28 10:46:32 +09:00
|
|
|
export async function removeTeamLayout(teamRunId: string, _tmuxMgr: TmuxSessionManager): Promise<void>
|
|
|
|
|
export async function removeTeamLayout(
|
|
|
|
|
teamRunId: string,
|
|
|
|
|
_cleanupTarget: TeamLayoutCleanupTarget | undefined,
|
|
|
|
|
_tmuxMgr: TmuxSessionManager,
|
|
|
|
|
): Promise<void>
|
|
|
|
|
export async function removeTeamLayout(
|
|
|
|
|
teamRunId: string,
|
|
|
|
|
tmuxMgrOrCleanupTarget: TmuxSessionManager | TeamLayoutCleanupTarget | undefined,
|
|
|
|
|
_tmuxMgr?: TmuxSessionManager,
|
|
|
|
|
): Promise<void> {
|
2026-04-18 18:33:58 +09:00
|
|
|
if (!canVisualize()) return
|
|
|
|
|
try {
|
|
|
|
|
const tmuxPath = await getTmuxPath()
|
|
|
|
|
if (!tmuxPath) return
|
2026-04-28 10:46:32 +09:00
|
|
|
|
|
|
|
|
const cleanupTarget = isTeamLayoutCleanupTarget(tmuxMgrOrCleanupTarget)
|
|
|
|
|
? tmuxMgrOrCleanupTarget
|
|
|
|
|
: undefined
|
|
|
|
|
|
|
|
|
|
if (cleanupTarget?.ownedSession !== false) {
|
|
|
|
|
await runTmuxCommand(tmuxPath, ["kill-session", "-t", cleanupTarget?.targetSessionId ?? `omo-team-${teamRunId}`])
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-04 19:06:21 +09:00
|
|
|
if (cleanupTarget?.paneIds && cleanupTarget.paneIds.length > 0) {
|
2026-04-28 10:46:32 +09:00
|
|
|
for (const paneId of cleanupTarget.paneIds) {
|
|
|
|
|
try {
|
|
|
|
|
await runTmuxCommand(tmuxPath, ["kill-pane", "-t", paneId])
|
|
|
|
|
} catch {
|
|
|
|
|
log("tmux team pane cleanup failed", { teamRunId, paneId })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const windowId of [cleanupTarget.focusWindowId, cleanupTarget.gridWindowId]) {
|
|
|
|
|
if (!windowId) continue
|
|
|
|
|
try {
|
|
|
|
|
await runTmuxCommand(tmuxPath, ["kill-window", "-t", windowId])
|
|
|
|
|
} catch (windowError) {
|
|
|
|
|
log("tmux team layout window cleanup failed", { teamRunId, windowId, error: String(windowError) })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
log("tmux team layout cleanup failed", { teamRunId, error: String(error) })
|
2026-04-18 18:33:58 +09:00
|
|
|
}
|
|
|
|
|
}
|
2026-04-28 10:46:32 +09:00
|
|
|
|
|
|
|
|
function isTeamLayoutCleanupTarget(value: TmuxSessionManager | TeamLayoutCleanupTarget | undefined): value is TeamLayoutCleanupTarget {
|
|
|
|
|
return value !== undefined && "ownedSession" in value && "targetSessionId" in value
|
|
|
|
|
}
|