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.5 KB
Raw Blame
import { installProgramStatusQueryResponder } from '../../../../src/shared/program-status/query-responder'
import { enqueueWriteBoundary } from './write-queue'
import { notify } from './host-notify'
import type {
  TerminalDocumentDisposable,
  TerminalDocumentScope,
  TerminalDocumentTerminal
} from './document-scope'

export type QueryReplyTerminal = {
  attachCustomKeyEventHandler: (handler: () => boolean) => void
  textarea?: {
    readOnly: boolean
    tabIndex: number
    setAttribute: (name: string, value: string) => void
  }
  onData: (listener: (data: string) => void) => TerminalDocumentDisposable
}

export function resetTerminalDataReplyAuthority(scope: TerminalDocumentScope) {
  scope.terminalDataRepliesEnabled = false
}

export function resumeTerminalDataReplyAuthority(scope: TerminalDocumentScope) {
  scope.terminalDataRepliesEnabled = true
}

export function forwardTerminalDataReply(scope: TerminalDocumentScope, data: string) {
  if (scope.terminalDataRepliesEnabled) {
    notify(scope, { type: 'terminal-data', bytes: data })
  }
}

export function enqueueTerminalDataReplyBoundary(scope: TerminalDocumentScope, gen: number) {
  enqueueWriteBoundary(scope, function () {
    if (gen === scope.terminalGeneration) {
      scope.terminalDataRepliesEnabled = true
    }
  })
}

export function attachTerminalQueryReplyBridge(
  scope: TerminalDocumentScope,
  term: QueryReplyTerminal,
  gen: number
) {
  // Parser replies need stdin enabled; keep xterm’s textarea inert because native controls own keyboard input.
  try {
    term.attachCustomKeyEventHandler(function () {
      return false
    })
    if (term.textarea) {
      term.textarea.readOnly = true
      term.textarea.tabIndex = -1
      term.textarea.setAttribute('inputmode', 'none')
    }
  } catch {}
  try {
    scope.termObserverDisposables.push(
      term.onData(function (data) {
        forwardTerminalDataReply(scope, data)
      })
    )
  } catch {}
  // Live queries may queue behind replay; grant authority at the boundary so only fresh queries receive replies.
  enqueueTerminalDataReplyBoundary(scope, gen)
}

// Answer OSC 7501 like desktop's capability replies; native drops it for hosts that cannot read reports.
export function attachProgramStatusQueryReplies(
  scope: TerminalDocumentScope,
  term: TerminalDocumentTerminal
) {
  if (term.parser) {
    installProgramStatusQueryResponder({
      parser: term.parser,
      emitReply: (reply) => forwardTerminalDataReply(scope, reply)
    })
  }
}