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.

6.1 KB
Raw Blame
import { useCallback, useEffect, useRef, type MutableRefObject } from 'react'
import { translate } from '../i18n/mobile-i18n'
import type { RpcClient } from '../transport/rpc-client'
import { isRpcDeliveryUnknown } from '../transport/rpc-delivery-ambiguity'
import { isLogicalClientCutoverError } from '../transport/stable-logical-rpc-client'
import { nativeChatTerminalWrite } from './mobile-session-write-operations'
import { reportWorkerTerminalUserInput } from '../terminal/worker-terminal-takeover-report'
import { openMobileNativeChatSendBudget } from './native-chat/mobile-native-chat-send'
import { nativeChatStopSendsSecondEscape } from '../../../src/shared/native-chat-agent-support'
import { NATIVE_CHAT_QUESTION_STEP_MS } from '../../../src/shared/native-chat/input/native-chat-answer-stepping'

// Resolves false only when no Escape could have landed, so the chat may offer Stop again.
export function useMobileNativeChatStop(args: {
  client: RpcClient | null
  enabled: boolean
  handleRef: MutableRefObject<string | null>
  deviceTokenRef: MutableRefObject<string | null>
  agentRef: MutableRefObject<string | null>
  streamIdentity: string
  cancelPending: () => void
  onSendError: (message: string) => void
}): () => Promise<boolean> {
  const {
    client,
    enabled,
    handleRef,
    deviceTokenRef,
    agentRef,
    streamIdentity,
    cancelPending,
    onSendError
  } = args
  const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
  const generationRef = useRef(0)
  // Settle a cancelled second Escape so first-write failure cannot wait forever for an unsent write.
  const dropSecondEscapeRef = useRef<(() => void) | null>(null)
  const activeRouteRef = useRef({ client, enabled, streamIdentity })
  activeRouteRef.current = { client, enabled, streamIdentity }
  const cancelSecondEscape = useCallback(() => {
    if (timerRef.current) {
      clearTimeout(timerRef.current)
      timerRef.current = null
    }
    const drop = dropSecondEscapeRef.current
    dropSecondEscapeRef.current = null
    drop?.()
  }, [])
  useEffect(
    () => () => {
      generationRef.current += 1
      cancelSecondEscape()
    },
    [cancelSecondEscape, client, enabled, streamIdentity]
  )
  return useCallback(() => {
    const handle = handleRef.current
    if (!client || !handle || !enabled) {
      onSendError(
        translate(
          'session.useNativeChatStop.terminalNotReady',
          'Stop not sent (terminal not ready)'
        )
      )
      return Promise.resolve(false)
    }
    cancelPending()
    generationRef.current += 1
    const generation = generationRef.current
    cancelSecondEscape()
    const stopStreamIdentity = streamIdentity
    const deadline = openMobileNativeChatSendBudget()
    // Wait for both paced Escapes and report failure only if neither was accepted; one Stop must not invite another after either key changed prompt state.
    let pending = 1
    let sawAccepted = false
    let sawUnknown = false
    let sawRejected = false
    let settle!: (sent: boolean) => void
    const verdict = new Promise<boolean>((resolve) => {
      settle = resolve
    })
    const reportIfSettled = (): void => {
      if (pending > 0) {
        return
      }
      settle(sawAccepted || sawUnknown || !sawRejected)
      if (generationRef.current !== generation || sawAccepted || (!sawUnknown && !sawRejected)) {
        return
      }
      // Lost-ack/cutover Escape may have stopped work; report uncertainty instead of inviting a second key.
      onSendError(
        sawUnknown
          ? translate(
              'session.useNativeChatStop.stopUnconfirmed',
              'Stop unconfirmed — check chat before retrying'
            )
          : translate('session.useNativeChatStop.stopNotSent', 'Stop not sent')
      )
    }
    const sendEscape = (): void => {
      const activeRoute = activeRouteRef.current
      if (
        !activeRoute.enabled ||
        activeRoute.client !== client ||
        activeRoute.streamIdentity !== stopStreamIdentity ||
        handleRef.current !== handle
      ) {
        return
      }
      pending += 1
      const timeoutMs = deadline - Date.now()
      if (timeoutMs <= 0) {
        sawRejected = true
        pending -= 1
        reportIfSettled()
        return
      }
      void nativeChatTerminalWrite
        .request(
          client,
          {
            terminal: handle,
            text: String.fromCharCode(27),
            ...(deviceTokenRef.current
              ? { client: { id: deviceTokenRef.current, type: 'mobile' as const } }
              : {})
          },
          // Bound reconnect wait so stale Escape cannot arrive minutes later in a fresh composer.
          { timeoutMs, budgetSpansConnect: true }
        )
        .then((response) => {
          if (nativeChatTerminalWrite.interpret(response) === true) {
            sawAccepted = true
            // A deliberate Stop is human input; it takes the worker over like any other key.
            reportWorkerTerminalUserInput(client, handle)
          } else {
            sawRejected = true
          }
        })
        // Both fire-and-forget Escapes settle into one verdict, including disconnect races.
        .catch((error: unknown) => {
          if (isRpcDeliveryUnknown(error) || isLogicalClientCutoverError(error)) {
            sawUnknown = true
          } else {
            sawRejected = true
          }
        })
        .finally(() => {
          pending -= 1
          reportIfSettled()
        })
    }
    sendEscape()
    if (!nativeChatStopSendsSecondEscape(agentRef.current)) {
      pending -= 1
      reportIfSettled()
      return verdict
    }
    dropSecondEscapeRef.current = () => {
      pending -= 1
      reportIfSettled()
    }
    // Pace like desktop's answer steps so remote input cannot coalesce the two Escapes away.
    timerRef.current = setTimeout(() => {
      timerRef.current = null
      dropSecondEscapeRef.current = null
      sendEscape()
      pending -= 1
      reportIfSettled()
    }, NATIVE_CHAT_QUESTION_STEP_MS)
    return verdict
  }, [
    agentRef,
    cancelPending,
    cancelSecondEscape,
    client,
    deviceTokenRef,
    enabled,
    handleRef,
    onSendError,
    streamIdentity
  ])
}