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 { notify } from './host-notify'
import type { TerminalDocumentScope } from './document-scope'
import { ESC } from './escape-introducers'
import { viewportToMouseReportCell } from './mouse-report-cell'
export function isAlternateBufferActive(scope: TerminalDocumentScope) {
try {
return !!(
scope.term &&
scope.term.buffer &&
scope.term.buffer.active &&
scope.term.buffer.active.type === 'alternate'
)
} catch {
return false
}
}
export function getMouseTrackingMode(scope: TerminalDocumentScope) {
try {
if (scope.term && scope.term.modes && typeof scope.term.modes.mouseTrackingMode === 'string') {
const mode = scope.term.modes.mouseTrackingMode
if (mode === 'x10' || mode === 'vt200' || mode === 'drag' || mode === 'any') {
return mode
}
return 'none'
}
} catch {}
if (
scope.trackedMouseTrackingMode === 'x10' ||
scope.trackedMouseTrackingMode === 'vt200' ||
scope.trackedMouseTrackingMode === 'drag' ||
scope.trackedMouseTrackingMode === 'any'
) {
return scope.trackedMouseTrackingMode
}
return 'none'
}
export function repeatSequence(sequence: string, count: number) {
let out = ''
for (let i = 0; i < count; i++) {
out += sequence
}
return out
}
export function buildArrowScrollSequence(scope: TerminalDocumentScope, lines: number) {
let prefix = '['
try {
if (scope.term && scope.term.modes && scope.term.modes.applicationCursorKeysMode) {
prefix = 'O'
}
} catch {}
return ESC + prefix + (lines < 0 ? 'A' : 'B')
}
export function buildMouseWheelSequence(
scope: TerminalDocumentScope,
lines: number,
clientX: number,
clientY: number
) {
if (!scope.mouseEncodingKnown) {
return ''
}
const cell = viewportToMouseReportCell(scope, clientX, clientY)
if (!cell) {
return ''
}
const eventCode = lines < 0 ? 64 : 65
if (scope.sgrMousePixelsMode) {
if (!isSafeSgrMouseCoordinate(cell.x) || !isSafeSgrMouseCoordinate(cell.y)) {
return ''
}
return ESC + '[<' + eventCode + ';' + cell.x + ';' + cell.y + 'M'
}
if (scope.sgrMouseMode) {
// Match xterm’s one-based encoded mouse cells.
const sgrCol = cell.col + 1
const sgrRow = cell.row + 1
if (!isSafeSgrMouseCoordinate(sgrCol) || !isSafeSgrMouseCoordinate(sgrRow)) {
return ''
}
return ESC + '[<' + eventCode + ';' + sgrCol + ';' + sgrRow + 'M'
}
// Match xterm’s one-based encoded mouse cells.
const button = eventCode + 32
const col = cell.col + 1 + 32
const row = cell.row + 1 + 32
// Legacy high bytes are unsafe through JSON/RPC; use keys on wide terminals.
if (button > 126 || col > 126 || row > 126) {
return ''
}
return (
ESC + '[M' + String.fromCharCode(button) + String.fromCharCode(col) + String.fromCharCode(row)
)
}
export function isSafeSgrMouseCoordinate(value: number) {
return Number.isInteger(value) && value >= 0 && value <= 9999
}
export function buildMouseClickInput(
scope: TerminalDocumentScope,
clientX: number,
clientY: number
) {
const mouseTrackingMode = getMouseTrackingMode(scope)
if (!isClickMouseTrackingMode(mouseTrackingMode) || !scope.mouseEncodingKnown) {
return ''
}
const cell = viewportToMouseReportCell(scope, clientX, clientY)
if (!cell) {
return ''
}
if (scope.sgrMousePixelsMode) {
// Mode 1016 uses SGR syntax with zero-based pixel coordinates.
const pixelX = cell.x
const pixelY = cell.y
if (!isSafeSgrMouseCoordinate(pixelX) || !isSafeSgrMouseCoordinate(pixelY)) {
return ''
}
const pixelPress = ESC + '[<0;' + pixelX + ';' + pixelY + 'M'
if (mouseTrackingMode === 'x10') {
return pixelPress
}
return pixelPress + ESC + '[<0;' + pixelX + ';' + pixelY + 'm'
}
if (scope.sgrMouseMode) {
// Match xterm’s one-based encoded mouse cells.
const sgrCol = cell.col + 1
const sgrRow = cell.row + 1
if (!isSafeSgrMouseCoordinate(sgrCol) || !isSafeSgrMouseCoordinate(sgrRow)) {
return ''
}
const sgrPress = ESC + '[<0;' + sgrCol + ';' + sgrRow + 'M'
if (mouseTrackingMode === 'x10') {
return sgrPress
}
return sgrPress + ESC + '[<0;' + sgrCol + ';' + sgrRow + 'm'
}
// Legacy click bytes must stay printable ASCII so wide cells cannot corrupt bridge input.
const col = cell.col + 1 + 32
const row = cell.row + 1 + 32
if (col > 126 || row > 126) {
return ''
}
const press =
ESC + '[M' + String.fromCharCode(32) + String.fromCharCode(col) + String.fromCharCode(row)
if (mouseTrackingMode === 'x10') {
return press
}
return (
press +
ESC +
'[M' +
String.fromCharCode(35) +
String.fromCharCode(col) +
String.fromCharCode(row)
)
}
export function isClickMouseTrackingMode(mode: string) {
return mode !== 'none'
}
export function isWheelMouseTrackingMode(mode: string) {
return mode !== 'none' && mode !== 'x10'
}
// Replay may omit encoding; guessed legacy reports could type escape bytes into the TUI, so scroll locally.
export function isWheelEncodingUnproven(scope: TerminalDocumentScope, mode: string) {
return isWheelMouseTrackingMode(mode) && !scope.mouseEncodingKnown
}
export function shouldRouteScrollToTerminalInput(scope: TerminalDocumentScope) {
const mode = getMouseTrackingMode(scope)
if (isWheelEncodingUnproven(scope, mode)) {
return false
}
return isWheelMouseTrackingMode(mode) || isAlternateBufferActive(scope)
}
export function buildMouseWheelScrollInput(
scope: TerminalDocumentScope,
lines: number,
clientX: number,
clientY: number
) {
const count = Math.min(Math.abs(lines), 32)
if (count === 0) {
return ''
}
const sequence = buildMouseWheelSequence(scope, lines, clientX, clientY)
if (!sequence) {
return ''
}
return repeatSequence(sequence, count)
}
export function buildTuiScrollInput(
scope: TerminalDocumentScope,
lines: number,
clientX: number,
clientY: number
) {
const count = Math.min(Math.abs(lines), 32)
if (count === 0) {
return ''
}
const mouseTrackingMode = getMouseTrackingMode(scope)
let sequence = ''
if (isWheelMouseTrackingMode(mouseTrackingMode)) {
sequence = buildMouseWheelSequence(scope, lines, clientX, clientY)
}
if (!sequence) {
sequence = buildArrowScrollSequence(scope, lines)
}
return repeatSequence(sequence, count)
}
export function routeScrollLines(
scope: TerminalDocumentScope,
lines: number,
clientX: number,
clientY: number
) {
if (!scope.term || lines === 0) {
return
}
const mouseTrackingMode = getMouseTrackingMode(scope)
const alternateBufferActive = isAlternateBufferActive(scope)
if (isWheelEncodingUnproven(scope, mouseTrackingMode)) {
scope.term.scrollLines(lines)
return
}
if (isWheelMouseTrackingMode(mouseTrackingMode)) {
// Mouse-aware TUIs receive wheel input before scrollback, including on the normal buffer.
const mouseInput = buildMouseWheelScrollInput(scope, lines, clientX, clientY)
if (mouseInput) {
notify(scope, { type: 'terminal-input', bytes: mouseInput })
return
}
// Wide-cell legacy encoding may be unsafe; send bounded arrows while the TUI owns scrolling.
const fallbackInput = buildTuiScrollInput(scope, lines, clientX, clientY)
if (fallbackInput) {
notify(scope, { type: 'terminal-input', bytes: fallbackInput })
}
return
}
if (alternateBufferActive) {
// Alternate-screen TUIs have no xterm scrollback; convert gestures into terminal input.
const input = buildTuiScrollInput(scope, lines, clientX, clientY)
if (input) {
notify(scope, { type: 'terminal-input', bytes: input })
}
return
}
scope.term.scrollLines(lines)
}