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.8 KB
Raw Blame
import { z } from 'zod'
import { SidebarInputsReadResultSchema } from '../../../src/shared/sidebar-list/sidebar-inputs-feed-contract'
import type { SidebarFeedTransport } from '../../../src/shared/sidebar-feed-client/sidebar-feed-reader'
import { bindDeferredRpcOperation, defineRpcOperation } from '../transport/rpc-operation'
import { rpcResultVariant } from '../transport/rpc-operation-result-reader'
import { isMobileMethodUnavailableError } from '../transport/mobile-method-unavailable'
import type { RpcResponse } from '../transport/types'
import type { UnvalidatedRpcRequestPort } from '../transport/unvalidated-rpc-request-port'

// Only ORCA_SIDEBAR_INPUTS=main pages read this feed; method_not_found or an older allowlist’s forbidden means the page is newer than the host.

export const sidebarInputsRead = bindDeferredRpcOperation(
  defineRpcOperation({
    name: 'sidebarInputs.read-or-skip',
    method: 'sidebarInputs.read',
    acceptance: 'success-result-or-skip',
    barrier: 'after-caller-barrier',
    read: rpcResultVariant('sidebar-inputs', SidebarInputsReadResultSchema)
  })
)

export const sidebarRepoIconsRead = bindDeferredRpcOperation(
  defineRpcOperation({
    name: 'sidebarInputs.repoIcons-or-skip',
    method: 'sidebarInputs.repoIcons',
    acceptance: 'success-result-or-skip',
    barrier: 'after-caller-barrier',
    read: rpcResultVariant(
      'repo-icons',
      z.object({ icons: z.array(z.object({ id: z.string(), dataUrl: z.string().nullable() })) })
    )
  })
)

export const setCollapsedWrite = bindDeferredRpcOperation(
  defineRpcOperation({
    name: 'ui.setCollapsed-or-skip',
    method: 'ui.setCollapsed',
    acceptance: 'success-result-or-skip',
    barrier: 'after-caller-barrier',
    read: rpcResultVariant(
      'collapse-written',
      z.object({ collapsed: z.boolean(), applied: z.boolean() })
    )
  })
)

export type HostMethodReply<Value> =
  | { kind: 'answered'; value: Value }
  | { kind: 'unavailable' }
  | { kind: 'failed' }

export function readHostMethodReply<Value>(
  reply: RpcResponse,
  interpret: (reply: RpcResponse) => { accepted: true; value: Value } | { accepted: false }
): HostMethodReply<Value> {
  if (!reply.ok) {
    return isMobileMethodUnavailableError(reply.error.code, reply.error.message)
      ? { kind: 'unavailable' }
      : { kind: 'failed' }
  }
  try {
    const verdict = interpret(reply)
    return verdict.accepted ? { kind: 'answered', value: verdict.value } : { kind: 'failed' }
  } catch {
    return { kind: 'failed' }
  }
}

export function hostSidebarFeedTransport(client: UnvalidatedRpcRequestPort): SidebarFeedTransport {
  return async (params) => {
    const read = readHostMethodReply(
      await sidebarInputsRead.request(client, params),
      sidebarInputsRead.interpret
    )
    return read.kind === 'answered' ? { kind: 'answered', reply: read.value } : read
  }
}