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.0 KB
Raw Blame
import type WebSocket from 'ws'
import { logSharedControlSocketClose } from './remote-runtime-shared-control-diagnostics-log'
import { closeSharedControlSocketState } from './remote-runtime-shared-control-state'
import { finishCloseAfterReadySubscriptions } from '../requests/remote-runtime-shared-control-subscriptions'
import type {
  SharedControlConnectionState,
  SharedControlLogicalSubscription,
  SharedControlPendingRequest,
  SharedControlReadyWaiter,
  SharedControlSocketClose
} from './remote-runtime-shared-control-types'

export const LOCAL_CLOSE: SharedControlSocketClose = { cause: 'local', errorCode: null }

export function closeSharedControlSocket(args: {
  /** Records the close cause; runs only when a socket existed, like the log below. */
  reportClose: () => void
  environmentId?: string
  state: SharedControlConnectionState
  ws: WebSocket | null
  socketCleanup: (() => void) | null
  pendingRequests: Map<string, SharedControlPendingRequest<unknown>>
  subscriptions: Map<string, SharedControlLogicalSubscription<unknown>>
  readyWaiters: SharedControlReadyWaiter[]
  lastClose: { code: number; reason: string } | null
  error?: Error
  preserveReadyWaitersAndPendingRequests?: boolean
  clearReadyStableTimer: () => void
}): void {
  if (args.ws || args.socketCleanup) {
    args.reportClose()
    logSharedControlSocketClose({
      environmentId: args.environmentId ?? 'unknown',
      state: args.state,
      pendingRequests: args.pendingRequests,
      subscriptions: args.subscriptions,
      lastClose: args.lastClose,
      error: args.error
    })
  }
  args.clearReadyStableTimer()
  finishCloseAfterReadySubscriptions(args.subscriptions)
  closeSharedControlSocketState({
    readyWaiters: args.readyWaiters,
    pendingRequests: args.pendingRequests,
    subscriptions: args.subscriptions,
    socketCleanup: args.socketCleanup,
    ws: args.ws,
    error: args.error,
    preserveReadyWaitersAndPendingRequests: args.preserveReadyWaitersAndPendingRequests
  })
}