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 * as ExpoCrypto from 'expo-crypto'
import type { ConnectionLogSink, ForegroundNudgeReason, HostProfile } from './types'
import type { MobileRelayEndpoint } from '../../../src/shared/mobile-relay-credential-contract'
import { connect } from './rpc-client'
import { MobileEndpointSupervisor } from './mobile-endpoint-supervisor'
import { connectMobileRelayRpcSession } from './mobile-relay-rpc-session'
import { resolveMobileRelayEndpoint } from './mobile-relay-resume-director'
import {
readMobileRelayCredentialBundle,
writeMobileRelayCredentialBundle
} from './mobile-relay-credential-bundle'
import { setRelayRouting } from './host-store'
import { upgradeDirectMobileRelay } from './mobile-relay-direct-upgrade'
import { directPathForEndpoint } from './mobile-direct-endpoint-probe'
import { MobileRelayDirectUpgradeController } from './mobile-relay-direct-upgrade-controller'
import { defaultCancelTimer, defaultScheduleTimer } from './timer-scheduler'
import type { StableLogicalRpcClient } from './stable-logical-rpc-client'
type EndpointLifecycle = {
setForeground(foreground: boolean): void
nudge(reason: ForegroundNudgeReason): void
stop(): void
}
type EndpointOwner = EndpointLifecycle & {
start(): Promise<void>
}
export function startMobileEndpointLifecycle(
logical: StableLogicalRpcClient,
initialHost: HostProfile,
onLog: ConnectionLogSink
): EndpointLifecycle {
let stopped = false
let foreground = true
let owner: EndpointOwner
const startSupervisor = async (relay: MobileRelayEndpoint): Promise<void> => {
if (stopped) {
return
}
const supervisor = createSupervisor(logical, initialHost, relay, onLog)
owner.stop()
owner = supervisor
supervisor.setForeground(foreground)
await supervisor.start()
}
if (initialHost.relay) {
owner = createSupervisor(logical, initialHost, initialHost.relay, onLog)
void owner.start()
} else {
owner = new MobileRelayDirectUpgradeController(logical, initialHost, {
upgrade: (client, host) =>
upgradeDirectMobileRelay({
client,
host,
dependencies: { randomBytes: ExpoCrypto.getRandomBytes }
}),
onUpgraded: ({ relay }) => startSupervisor(relay)
})
void owner.start()
}
return {
setForeground(next) {
foreground = next
owner.setForeground(next)
},
nudge(reason) {
// Why: a focus nudge can precede the AppState listener; keep the closure in
// sync or a later supervisor swap would start with a stale background flag.
if (reason !== 'network-change') {
foreground = true
}
owner.nudge(reason)
},
stop() {
stopped = true
owner.stop()
}
}
}
function createSupervisor(
logical: StableLogicalRpcClient,
host: HostProfile,
relay: MobileRelayEndpoint,
onLog: ConnectionLogSink
): MobileEndpointSupervisor {
return new MobileEndpointSupervisor(logical, host.id, relay, {
openDirect: () => connect(host.endpoint, host.deviceToken, host.publicKeyB64, { onLog }),
directPath: directPathForEndpoint(host.endpoint),
openRelay: (relay, credential, confirmReqId, onHostCloseReason) =>
connectMobileRelayRpcSession({
relay,
resumeToken: credential.token,
resumeCredentialVersion: credential.version,
resumeConfirmReqId: confirmReqId,
deviceToken: host.deviceToken,
desktopPublicKeyB64: host.publicKeyB64,
onHostCloseReason,
onLog
}),
resolveRelay: resolveMobileRelayEndpoint,
readBundle: readMobileRelayCredentialBundle,
writeBundle: writeMobileRelayCredentialBundle,
setRelayRouting,
onLog,
now: Date.now,
randomBytes: ExpoCrypto.getRandomBytes,
setTimer: defaultScheduleTimer,
clearTimer: defaultCancelTimer
})
}