Skip to content

armgit / orca

Public Imported from GitHub
0

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.

8.4 KB
Raw Blame
// Real-shaped stored partitions: the desktop's own, a relay-fallback SSH host's, a remote server's.

import type { SleepingAgentSessionRecord } from '../agent-session-resume'
import { FLOATING_TERMINAL_WORKTREE_ID } from '../constants'
import {
  LOCAL_EXECUTION_HOST_ID,
  toRuntimeExecutionHostId,
  toSshExecutionHostId,
  type ExecutionHostId
} from '../execution-host'
import { toRemoteRuntimePtyId } from '../remote-runtime/remote-runtime-pty-id'
import { toAppSshPtyId } from '../ssh-pty-id'
import type { WorkspaceSessionState } from '../workspace-session-state-types'
import {
  addWorkspace,
  emptySession,
  FOLDER_KEY,
  GIT_KEY,
  leaf,
  SERVER_KEY,
  SSH_KEY
} from './workspace-layout-session.test-fixture'

export function sleepingRecord(
  worktreeId: string,
  tabId: string,
  leafId: string
): SleepingAgentSessionRecord {
  return {
    paneKey: `${tabId}:${leafId}`,
    tabId,
    worktreeId,
    agent: 'claude',
    providerSession: { key: 'session_id', id: `session-${tabId}` },
    prompt: 'fix the flaky test',
    state: 'done',
    capturedAt: 1_700_000_500_000,
    updatedAt: 1_700_000_500_000,
    origin: 'quit'
  }
}

/** Desktop partition with git and folder workspaces, including split groups. */
export function localDesktopSession(): WorkspaceSessionState {
  const session = addWorkspace(emptySession(), GIT_KEY, [
    {
      id: 'group-a',
      recent: ['tab-shell', 'tab-agent'],
      activeTabId: 'tab-agent',
      tabs: [
        {
          id: 'tab-shell',
          leaves: [
            [leaf(1), `${GIT_KEY}@@aaaa0001`],
            [leaf(2), `${GIT_KEY}@@aaaa0002`]
          ],
          split: 'horizontal',
          layout: {
            titlesByLeafId: { [leaf(2)]: 'server' },
            scrollbackRefsByLeafId: { [leaf(1)]: 'ref-1' }
          }
        },
        {
          // Its tab-bar id and terminal id differ, as for a tab adopted from another window.
          id: 'tab-agent',
          entityId: 'term-agent',
          leaves: [[leaf(3), `${GIT_KEY}@@aaaa0003`]],
          title: 'claude',
          row: {
            defaultTitle: 'Terminal 2',
            customTitle: 'Agent',
            color: '#7c3aed',
            isPinned: true,
            viewMode: 'chat',
            launchAgent: 'claude',
            generatedTitle: 'Fix flaky test',
            generation: 2
          },
          entry: {
            label: 'claude',
            customLabel: 'Agent',
            color: '#7c3aed',
            isPinned: true,
            viewMode: 'chat',
            generatedLabel: 'Fix flaky test',
            lastFocusedAt: 1_700_000_400_000
          },
          layout: { chatLeafId: leaf(3), expandedLeafId: null }
        },
        {
          id: 'editor:src/app.ts',
          entityId: '/Users/dev/orca/src/app.ts',
          kind: 'editor',
          entry: { label: 'app.ts' }
        }
      ]
    },
    {
      id: 'group-b',
      tabs: [
        { id: 'browser-1', kind: 'browser', entry: { label: 'Docs' } },
        {
          id: 'agent-session-1',
          kind: 'agent-session',
          entry: { label: 'Codex', agentSessionAgent: 'codex' }
        },
        {
          id: 'tab-unbound',
          leaves: [[leaf(4)]],
          row: { startupCwd: '/Users/dev/orca/src', shellOverride: 'zsh' }
        }
      ]
    }
  ])
  session.openFilesByWorktree = {
    [GIT_KEY]: [
      {
        filePath: '/Users/dev/orca/src/app.ts',
        relativePath: 'src/app.ts',
        worktreeId: GIT_KEY,
        language: 'typescript',
        dirtyDraftContent: 'export {}\n',
        lastKnownDiskSignature: 'sig-1'
      }
    ]
  }
  session.browserTabsByWorktree = {
    [GIT_KEY]: [
      {
        id: 'browser-1',
        worktreeId: GIT_KEY,
        label: 'Browser 1',
        sessionProfileId: null,
        activePageId: 'page-1',
        pageIds: ['page-1'],
        url: 'https://example.com/docs',
        title: 'Docs',
        loading: false,
        faviconUrl: null,
        canGoBack: true,
        canGoForward: false,
        loadError: null,
        createdAt: 1_700_000_004_000
      }
    ]
  }
  session.browserPagesByWorkspace = {
    'browser-1': [
      {
        id: 'page-1',
        workspaceId: 'browser-1',
        worktreeId: GIT_KEY,
        url: 'https://example.com/docs',
        title: 'Docs',
        loading: false,
        faviconUrl: null,
        canGoBack: true,
        canGoForward: false,
        loadError: null,
        createdAt: 1_700_000_004_000
      }
    ]
  }
  session.activeBrowserTabIdByWorktree = { [GIT_KEY]: 'browser-1' }
  session.activeFileIdByWorktree = { [GIT_KEY]: '/Users/dev/orca/src/app.ts' }
  session.activeTabTypeByWorktree = { [GIT_KEY]: 'terminal' }
  addWorkspace(session, FOLDER_KEY, [
    {
      id: 'group-folder',
      tabs: [{ id: 'tab-notes', leaves: [[leaf(5), `${FOLDER_KEY}@@bbbb0001`]] }]
    }
  ])
  return {
    ...session,
    activeRepoId: 'repo-1',
    activeWorktreeId: GIT_KEY,
    activeWorkspaceKey: `worktree:${GIT_KEY}`,
    activeTabId: 'term-agent',
    activeWorktreeIdsOnShutdown: [GIT_KEY, FOLDER_KEY],
    localOnlyScrollbackByTabId: { 'tab-shell': { [leaf(2)]: 'last screen' } },
    lastVisitedAtByWorktreeId: {
      [GIT_KEY]: 1_700_000_400_000,
      [`local|${FOLDER_KEY}`]: 1_700_000_300_000
    },
    workspaceDocHistory: [],
    sleepingAgentSessionsByPaneKey: {
      [`term-agent:${leaf(3)}`]: sleepingRecord(GIT_KEY, 'term-agent', leaf(3))
    },
    terminalPtyIncarnationsByPaneKey: {
      [`tab-shell:${leaf(1)}`]: 'inc-1',
      [`term-agent:${leaf(3)}`]: 'inc-3'
    },
    terminalTopologyRevisionByRepoId: { 'repo-1': 7 },
    defaultTerminalTabsAppliedByWorktreeId: { [GIT_KEY]: true },
    closedTerminalTabTombstonesByTabId: {
      'tab-closed': { closedAt: 1_700_000_450_000, worktreeId: GIT_KEY, reason: 'user' }
    },
    closedTerminalPanesByPaneKey: {
      'tab-closed:leaf-closed': {
        ptyId: 'pty-closed',
        incarnationId: 'inc-closed',
        worktreeId: GIT_KEY,
        closedAt: 1_700_000_450_000
      }
    },
    clientHostedBrowserCloseIntentsByEnvironment: {}
  }
}

/** A relay-fallback SSH host's partition, written by the window with remote session ids. */
export function relaySshSession(targetId: string): WorkspaceSessionState {
  const pty = (n: number) => toAppSshPtyId(targetId, `pty-${n}`)
  const session = addWorkspace(
    emptySession(),
    SSH_KEY,
    [
      {
        id: 'group-ssh',
        tabs: [
          {
            id: 'tab-ssh-1',
            leaves: [
              [leaf(6), pty(1)],
              [leaf(7), pty(2)]
            ]
          },
          { id: 'tab-ssh-2', leaves: [[leaf(8), pty(3)]] }
        ]
      }
    ],
    toSshExecutionHostId(targetId)
  )
  return {
    ...session,
    remoteSessionIdsByTabId: { 'tab-ssh-1': pty(1), 'tab-ssh-2': pty(3) },
    terminalPtyIncarnationsByPaneKey: { [`tab-ssh-1:${leaf(6)}`]: 'relay-inc-1' },
    // Today's writers keep an empty revision map once created.
    terminalTopologyRevisionByRepoId: {},
    activeConnectionIdsAtShutdown: [targetId]
  }
}

/** A remote Orca server's partition (`runtime:<env>`), mirrored by the desktop. */
export function serverRuntimeSession(environmentId: string): WorkspaceSessionState {
  return addWorkspace(
    emptySession(),
    SERVER_KEY,
    [
      {
        id: 'group-server',
        tabs: [
          { id: 'tab-server', leaves: [[leaf(9), toRemoteRuntimePtyId('handle-1', environmentId)]] }
        ]
      }
    ],
    toRuntimeExecutionHostId(environmentId)
  )
}

export type WorkspaceLayoutProfile = {
  name: string
  /** The partition the session is stored under. */
  hostId: ExecutionHostId
  build: () => WorkspaceSessionState
}

export const WORKSPACE_LAYOUT_PROFILES: readonly WorkspaceLayoutProfile[] = [
  {
    name: 'local desktop (git worktree, folder workspace, all tab kinds)',
    hostId: LOCAL_EXECUTION_HOST_ID,
    build: localDesktopSession
  },
  {
    name: 'floating terminal',
    hostId: LOCAL_EXECUTION_HOST_ID,
    build: () =>
      addWorkspace(emptySession(), FLOATING_TERMINAL_WORKTREE_ID, [
        { id: 'group-floating', tabs: [{ id: 'tab-floating', leaves: [[leaf(1), 'floating@@1']] }] }
      ])
  },
  {
    name: 'relay SSH host partition',
    hostId: toSshExecutionHostId('target-1'),
    build: () => relaySshSession('target-1')
  },
  {
    name: 'remote server partition',
    hostId: toRuntimeExecutionHostId('env-1'),
    build: () => serverRuntimeSession('env-1')
  },
  { name: 'empty profile', hostId: LOCAL_EXECUTION_HOST_ID, build: emptySession }
]