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.

2.4 KB
Raw Blame
import type { RuntimeStatus } from './runtime-types'
import type { PublicKnownRuntimeEnvironment } from './runtime-environments'
import type { FolderWorkspace } from './folder-workspace-types'
import { folderWorkspaceToWorktree } from './folder-workspace-worktree'
import { normalizeWorkspaceCreatorProvenance } from './workspace-creator-provenance'

type RuntimeStatusEntry = { status: Pick<RuntimeStatus, 'pairedDeviceId'> | null }

export const EMPTY_PAIRED_DEVICE_IDS_BY_ENVIRONMENT: ReadonlyMap<string, string> = new Map()

/** This client's own device id on each paired server. */
export function getPairedDeviceIdsByEnvironment(
  environments: readonly Pick<PublicKnownRuntimeEnvironment, 'id' | 'pairedDeviceId'>[],
  statuses: ReadonlyMap<string, RuntimeStatusEntry>
): ReadonlyMap<string, string> {
  const result = new Map<string, string>()
  for (const environment of environments) {
    const deviceId =
      statuses.get(environment.id)?.status?.pairedDeviceId ?? environment.pairedDeviceId
    if (deviceId) {
      result.set(environment.id, deviceId)
    }
  }
  return result
}

/** Hide workspaces created by others, including the server itself. Phones attribute to the owner they act for; missing creator/unnamed pairings remain visible. */
export function isWorkspaceOwnedBySomeoneElse(
  worktree: { creatorProvenance?: unknown; runtimeOwnerEnvironmentId?: string },
  pairedDeviceIdsByEnvironment: ReadonlyMap<string, string>
): boolean {
  const creator = normalizeWorkspaceCreatorProvenance(worktree.creatorProvenance)
  if (!creator) {
    return false
  }
  const environmentId = worktree.runtimeOwnerEnvironmentId
  if (!environmentId) {
    return creator.kind !== 'host'
  }
  const pairedDeviceId = pairedDeviceIdsByEnvironment.get(environmentId)
  if (!pairedDeviceId) {
    return false
  }
  return creator.kind !== 'paired-device' || creator.deviceId !== pairedDeviceId
}

export function isFolderWorkspaceFromOtherDevice(
  workspace: FolderWorkspace,
  pairedDeviceIdsByEnvironment: ReadonlyMap<string, string>
): boolean {
  return isWorkspaceOwnedBySomeoneElse(
    folderWorkspaceToWorktree(workspace),
    pairedDeviceIdsByEnvironment
  )
}

export function filterFolderWorkspacesFromOtherDevices(
  workspaces: readonly FolderWorkspace[],
  pairedDeviceIdsByEnvironment: ReadonlyMap<string, string>
): FolderWorkspace[] {
  return workspaces.filter(
    (workspace) => !isFolderWorkspaceFromOtherDevice(workspace, pairedDeviceIdsByEnvironment)
  )
}