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 { useCallback, useMemo, useState } from 'react'
import { structuredAgentSessionShownSnapshot } from '../../../src/shared/structured-agent-session-option-view'
import type { StructuredAgentSessionOptionState } from '../../../src/shared/structured-agent-session-options'
import type {
  SessionOptionDescriptor,
  SessionOptionsSurface,
  SessionOptionValue
} from '../../../src/shared/native-chat/input/native-chat-session-options'

export function useMobileStructuredOptionSurface(args: {
  demandModelCatalog: () => void
  lockSnapshot: (snapshot: SessionOptionDescriptor[]) => SessionOptionDescriptor[]
  optionSnapshot: SessionOptionDescriptor[]
  setStructuredOption: (id: string, value: SessionOptionValue) => Promise<boolean>
  /** The latest option state, which a settled pick's snapshot is read from. */
  readOptionState: () => StructuredAgentSessionOptionState
}) {
  const { optionSnapshot, readOptionState, setStructuredOption, demandModelCatalog } = args
  const [optionPickerRequest, setOptionPickerRequest] = useState<{
    id: string
    sequence: number
  } | null>(null)

  const invokeStructuredOption = useCallback(
    async (id: string) => {
      if (!optionSnapshot.some((entry) => entry.id === id)) {
        return false
      }
      setOptionPickerRequest((current) => ({ id, sequence: (current?.sequence ?? 0) + 1 }))
      return true
    },
    [optionSnapshot]
  )

  const setOption = useCallback(
    async (id: string, value: SessionOptionValue) => {
      await setStructuredOption(id, value)
      return { snapshot: args.lockSnapshot(structuredAgentSessionShownSnapshot(readOptionState())) }
    },
    [args.lockSnapshot, readOptionState, setStructuredOption]
  )

  const optionSurface = useMemo<SessionOptionsSurface>(
    () => ({
      getSnapshot: () => optionSnapshot,
      demandModelCatalog,
      setOption,
      invokeAction: async () => ({ snapshot: optionSnapshot }),
      subscribe: () => () => {}
    }),
    [optionSnapshot, setOption, demandModelCatalog]
  )

  return { optionPickerRequest, invokeStructuredOption, optionSurface }
}