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.

4.2 KB
Raw Blame
// Map a host terminal to its paired client’s mirrored surface without re-deriving ids in every spec.
import type { Page } from '@stablyai/playwright-test'
import { expect } from '@stablyai/playwright-test'
import {
  HOST_TERMINAL_SURFACE_SEPARATOR,
  toWebTerminalSurfaceTabId
} from '../../../src/shared/terminal-surface-id'

export type PairedHostTerminal = {
  /** The host's own tab id, for host-side RPCs. */
  hostTabId: string
  /** The host terminal handle, for terminal.* RPCs such as closeTab. */
  terminal: string
  /** The id the paired client mirrors the host tab under. */
  webTabId: string
}

export async function callEnvironment(
  page: Page,
  environmentId: string,
  method: string,
  params: unknown
): Promise<unknown> {
  return page.evaluate(
    async ({ environmentId, method, params }) => {
      const response = await window.api.runtimeEnvironments.call({
        selector: environmentId,
        method,
        params
      })
      if (!response.ok) {
        throw new Error(`${response.error.code}: ${response.error.message}`)
      }
      return response.result
    },
    { environmentId, method, params }
  )
}

// Validate the RPC shape immediately so drift cannot surface later as an undefined surface id.
function readCreatedTerminalTab(result: unknown): { id: string; terminal: string } {
  if (
    typeof result === 'object' &&
    result !== null &&
    'tab' in result &&
    typeof result.tab === 'object' &&
    result.tab !== null &&
    'id' in result.tab &&
    typeof result.tab.id === 'string' &&
    'terminal' in result.tab &&
    typeof result.tab.terminal === 'string'
  ) {
    return { id: result.tab.id, terminal: result.tab.terminal }
  }
  throw new Error(`host session terminal was not created: ${JSON.stringify(result)}`)
}

export async function createPairedHostTerminal(
  page: Page,
  environmentId: string,
  worktreeId: string,
  command: string
): Promise<PairedHostTerminal> {
  const tab = readCreatedTerminalTab(
    await callEnvironment(page, environmentId, 'session.tabs.createTerminal', {
      worktree: `id:${worktreeId}`,
      command,
      activate: false,
      select: false,
      navigation: 'caller'
    })
  )
  // Why: the host answers with a `tabId::leafId` surface id; client tabs mirror the parent tab.
  const hostTabId = tab.id.split(HOST_TERMINAL_SURFACE_SEPARATOR)[0]
  return { hostTabId, terminal: tab.terminal, webTabId: toWebTerminalSurfaceTabId(hostTabId) }
}

export async function openPairedClientTab(
  page: Page,
  worktreeId: string,
  webTabId: string
): Promise<void> {
  await expect
    .poll(
      () =>
        page.evaluate(
          (id) => (window.__store?.getState().tabsByWorktree[id] ?? []).map((tab) => tab.id),
          worktreeId
        ),
      { timeout: 60_000, message: `client never mirrored host tab ${webTabId}` }
    )
    .toContain(webTabId)
  await page.evaluate(
    ({ webTabId, worktreeId }) => {
      const state = window.__store?.getState()
      state?.setActiveView('terminal')
      state?.setActiveWorktree(worktreeId)
      state?.setActiveTab(webTabId)
      state?.setActiveTabType('terminal', window.__store?.getState().activeWorktreeId ?? null)
    },
    { webTabId, worktreeId }
  )
  await expect
    .poll(() => page.evaluate((id) => window.__paneManagers?.has(id) ?? false, webTabId), {
      timeout: 60_000,
      message: `client pane for ${webTabId} did not mount`
    })
    .toBe(true)
}

// Read this tab’s buffer; active-tab lookup can hide failures in a different revealed tab.
export async function readPairedPaneContent(page: Page, webTabId: string): Promise<string> {
  return page.evaluate((id) => {
    const manager = window.__paneManagers?.get(id)
    const pane = manager?.getActivePane?.() ?? manager?.getPanes?.()[0] ?? null
    return pane?.serializeAddon?.serialize?.() ?? ''
  }, webTabId)
}

export async function waitForPairedPaneMarker(
  page: Page,
  webTabId: string,
  marker: string,
  budgetMs: number
): Promise<boolean> {
  const deadline = Date.now() + budgetMs
  for (;;) {
    if ((await readPairedPaneContent(page, webTabId)).includes(marker)) {
      return true
    }
    if (Date.now() >= deadline) {
      return false
    }
    await new Promise((resolve) => setTimeout(resolve, 250))
  }
}