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.

3.3 KB
Raw Blame
import {
  aggregateAsyncQuestions,
  scanPendingAsks
} from '../../../../../src/shared/native-chat/input/native-chat-ask'
import { useMemo } from 'react'
import type { AgentStatusEntry } from '../../../../../src/shared/agent-status-types'
import type { AskPrompt } from '../../../../../src/shared/native-chat/input/native-chat-ask-types'
import {
  resolveNativeChatInteractivePromptCard,
  type ChatApproval
} from '../../../../../src/shared/native-chat-interactive-prompt'
import { nativeChatPromptWaitStartedAt } from '../../../../../src/shared/native-chat-prompt-dismiss-key'
import type { NativeChatMessage } from '../../../../../src/shared/native-chat-types'
import {
  useMobileStructuredAsyncQuestion,
  type MobileStructuredAskSession
} from '../../use-mobile-structured-async-question'

export type MobileNativeChatPrompts = {
  /** A terminal chat's approval; a structured chat's journal prompts are its own cards. */
  permission: ChatApproval | null
  ask: AskPrompt | null
  /** The host wait's start for a status-backed card; null for a transcript-only one. */
  waitStartedAt: number | null
  /** Set in a structured chat, whose ask is answered by a send rather than a terminal write. */
  structuredAnswer: ReturnType<typeof useMobileStructuredAsyncQuestion>['answer']
}

export function useMobileNativeChatPrompts(args: {
  enabled: boolean
  /** A structured chat reads only its async question here; its journal prompts are its own cards. */
  structured?: MobileStructuredAskSession | null
  status: AgentStatusEntry | null | undefined
  messages: readonly NativeChatMessage[]
  // Unsettled/retained reconnect history may contain answered asks and cannot supply current prompt cards.
  transcriptLoading: boolean
  asyncSourceScope: string
  hiddenAsyncIds: ReadonlySet<string>
}): MobileNativeChatPrompts {
  const { enabled, structured = null, status, messages, transcriptLoading } = args
  const scan = useMemo(
    () => (!transcriptLoading ? scanPendingAsks(messages) : null),
    [messages, transcriptLoading]
  )
  const pendingAsk = useMemo(
    () =>
      aggregateAsyncQuestions(scan?.contributors ?? [], args.asyncSourceScope, args.hiddenAsyncIds),
    [scan, args.asyncSourceScope, args.hiddenAsyncIds]
  )
  const transcriptAsk = scan?.blocking ?? pendingAsk
  const interactivePrompt = status?.interactivePrompt
  const toolName = status?.toolName
  const agent = status?.agentType
  const card = useMemo(
    () =>
      resolveNativeChatInteractivePromptCard({
        interactivePrompt,
        toolName,
        agent,
        messages,
        transcriptAsk,
        transcriptSettled: !transcriptLoading
      }),
    [interactivePrompt, toolName, agent, messages, transcriptLoading, transcriptAsk]
  )
  const structuredAsk = useMobileStructuredAsyncQuestion({
    structured,
    pendingAsk,
    transcriptLoading
  })
  if (structured) {
    return {
      permission: null,
      ask: enabled ? structuredAsk.ask : null,
      waitStartedAt: null,
      structuredAnswer: structuredAsk.answer
    }
  }
  // An approval needs no resolved chat; an ask waits for one.
  const shown = enabled || card?.kind === 'approval' ? card : null
  return {
    structuredAnswer: null,
    permission: shown?.kind === 'approval' ? shown.approval : null,
    ask: shown?.kind === 'question' ? shown.prompt : null,
    waitStartedAt: nativeChatPromptWaitStartedAt(shown, status)
  }
}