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.1 KB
Raw Blame
import { useCallback } from 'react'
import type { StructuredAgentSessionState } from '../../../src/shared/structured-agent-session-reducer'
import type { RpcClient } from '../transport/rpc-client'
import {
  requestStructuredAgentSessionMutation,
  type StructuredAgentSessionMutationResult
} from './mobile-structured-agent-session-rpc'

export type MobileStructuredAgentMutate = <TValue>(
  method: string,
  fingerprintMethod: string,
  fields: Record<string, unknown>,
  shouldReportError?: () => boolean
) => Promise<StructuredAgentSessionMutationResult<TValue>>

export function useMobileStructuredAgentMutate(args: {
  client: RpcClient | null
  sessionId: string | null
  enabled: boolean
  stateRef: { readonly current: StructuredAgentSessionState }
  onSendError: (message: string) => void
  agentName?: string
}): MobileStructuredAgentMutate {
  const { agentName, client, enabled, onSendError, sessionId, stateRef } = args
  return useCallback(
    async <TValue>(
      method: string,
      fingerprintMethod: string,
      fields: Record<string, unknown>,
      shouldReportError?: () => boolean
    ): Promise<StructuredAgentSessionMutationResult<TValue>> => {
      const current = stateRef.current
      if (!client || !sessionId || !enabled || current.fence === null) {
        return { status: 'rejected' }
      }
      const targetFence = current.fence
      const result = await requestStructuredAgentSessionMutation<TValue>({
        client,
        method,
        fingerprintMethod,
        sessionId,
        expectedRuntimeFence: targetFence,
        fields,
        ...(agentName ? { agentName } : {})
      })
      if (result.status === 'accepted') {
        return {
          status: 'accepted',
          value: result.value,
          sameFence: stateRef.current.fence === targetFence
        }
      }
      if (result.status === 'unknown') {
        return result
      }
      if (shouldReportError?.() !== false) {
        onSendError(result.message)
      }
      return { status: 'rejected' }
    },
    [agentName, client, enabled, onSendError, sessionId, stateRef]
  )
}