Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.
TypeScript adeagent-ideai-agentsautomationbotclaude-codecodexghosttygrok-bothermes-agentmobile-appmuseopenclawopencodeorchestrationpiruntimeterminalworktreesyc-backed
import { ORCA_LAYOUT_OWNER_IS_MAIN } from '../layout-owner-switch'
import { insertTabIdIntoOrder } from './tab-order'
import { collectLayoutLeafIdsInOrder } from './terminal-pane-tree'
import type {
LayoutApplyResult,
LayoutCommandResult,
LayoutContext,
LayoutEffects,
LayoutRefusalCode
} from './workspace-layout-command-types'
import {
paneKeyOf,
type LayoutLeaf,
type LayoutTab,
type LayoutTerminalTab,
type WorkspaceLayout,
type WorkspaceLayoutModel
} from './workspace-layout-model'
import { workspaceRootGroupId } from './workspace-layout-minted-ids'
import type { PaneOutcomeChanges } from './workspace-layout-pane-outcomes'
import { withWorkspace } from './workspace-layout-removal'
export type Applied = LayoutApplyResult<WorkspaceLayoutModel>
export const refuse = (code: LayoutRefusalCode): Applied => ({ ok: false, code })
export function applied(
model: WorkspaceLayoutModel,
result: LayoutCommandResult = {},
effects: Partial<LayoutEffects> = {},
outcomes?: PaneOutcomeChanges
): Applied {
return {
ok: true,
model,
result,
effects: { stopPtyIds: effects.stopPtyIds ?? [], startPaneKeys: effects.startPaneKeys ?? [] },
...(outcomes ? { outcomes } : {})
}
}
/** Create a missing workspace entry using its worktree ID unless runtime already supplied the key. */
export function workspaceOrEmpty(model: WorkspaceLayoutModel, key: string): WorkspaceLayout {
return model.workspaces[key] ?? { worktreeId: key, tabs: [], groups: [] }
}
export function findTab(workspace: WorkspaceLayout, tabId: string): LayoutTab | undefined {
return workspace.tabs.find((tab) => tab.id === tabId)
}
export function findTerminal(
workspace: WorkspaceLayout,
tabId: string
): LayoutTerminalTab | undefined {
const tab = findTab(workspace, tabId)
return tab?.kind === 'terminal' ? tab : undefined
}
export function replaceTab(workspace: WorkspaceLayout, tab: LayoutTab): WorkspaceLayout {
return { ...workspace, tabs: workspace.tabs.map((entry) => (entry.id === tab.id ? tab : entry)) }
}
export function updateTab(
model: WorkspaceLayoutModel,
key: string,
tab: LayoutTab,
result: LayoutCommandResult = {}
): Applied {
return applied(withWorkspace(model, key, replaceTab(model.workspaces[key]!, tab)), result)
}
export function leafIdsOf(tab: LayoutTerminalTab): string[] {
return collectLayoutLeafIdsInOrder(tab.panes.root)
}
export function boundPtyIds(workspace: WorkspaceLayout, tab: LayoutTab): string[] {
return tab.kind === 'terminal'
? leafIdsOf(tab).flatMap((leafId) => workspace.leaves?.[leafId]?.ptyId ?? [])
: []
}
export function setLeaf(
workspace: WorkspaceLayout,
leafId: string,
leaf: LayoutLeaf
): WorkspaceLayout {
const leaves = { ...workspace.leaves, [leafId]: leaf }
if (Object.keys(leaf).length === 0) {
delete leaves[leafId]
}
return { ...workspace, leaves }
}
export function locatePane(
workspace: WorkspaceLayout,
paneKey: string
): { tab: LayoutTerminalTab; leafId: string } | null {
for (const tab of workspace.tabs) {
if (tab.kind !== 'terminal') {
continue
}
const leafId = leafIdsOf(tab).find(
(candidate) => paneKeyOf(tab.entityId, candidate) === paneKey
)
if (leafId) {
return { tab, leafId }
}
}
return null
}
export type TabPlacement = { groupId?: string; afterTabId?: string; index?: number }
/** In owner builds, reuse the sender’s empty group or the window/Loader root ID; otherwise mint an ID. */
function firstGroupId(key: string, placement: TabPlacement, context: LayoutContext): string {
if (ORCA_LAYOUT_OWNER_IS_MAIN) {
return placement.groupId ?? workspaceRootGroupId(key)
}
return context.mintId()
}
/** Place in named group, anchor group or first group (create when absent); preserve pinned-before-unpinned order. */
export function placeNewTab(
workspace: WorkspaceLayout,
key: string,
tab: LayoutTab,
placement: TabPlacement,
context: LayoutContext
): WorkspaceLayout {
const tabs = [...workspace.tabs, tab]
const anchorGroup = placement.afterTabId
? workspace.groups.find((group) => group.tabOrder.includes(placement.afterTabId!))
: undefined
let group =
workspace.groups.find((entry) => entry.id === placement.groupId) ??
anchorGroup ??
workspace.groups[0]
const groups = [...workspace.groups]
if (!group) {
group = { id: firstGroupId(key, placement, context), tabOrder: [] }
groups.push(group)
}
let tabOrder: string[]
if (placement.index !== undefined) {
tabOrder = group.tabOrder.filter((id) => id !== tab.id)
tabOrder.splice(Math.max(0, Math.min(placement.index, tabOrder.length)), 0, tab.id)
} else {
const anchor = anchorGroup === group ? placement.afterTabId : undefined
tabOrder = insertTabIdIntoOrder(group.tabOrder, tabs, tab.id, tab.isPinned === true, anchor)
}
const placed = { ...group, tabOrder }
return {
...workspace,
tabs,
groups: groups.map((entry) => (entry.id === placed.id ? placed : entry))
}
}