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.

1.4 KB
Raw Blame
import type { AgentSessionSubscribeEvent } from '../../../src/shared/agent-session-wire'
import type { StructuredAgentSessionLiveSubscribe } from '../../../src/shared/structured-agent-session-live-read'
import type { RpcClient } from '../transport/rpc-client'

function isSubscribeEvent(value: unknown): value is AgentSessionSubscribeEvent {
  if (typeof value !== 'object' || value === null || !('type' in value)) {
    return false
  }
  const type = value.type
  return type === 'snapshot' || type === 'batch' || type === 'reset' || type === 'end'
}

// Subscribe after the hold settles even on refusal: older hosts can start on the next send, so their transcript must remain visible.
export function subscribeTranscriptAfterHold(
  client: RpcClient,
  held: Promise<unknown>,
  isEnded: () => boolean
): StructuredAgentSessionLiveSubscribe {
  return async (params, onEvent, onError) => {
    await held.catch(() => undefined)
    if (isEnded()) {
      return { unsubscribe: () => {} }
    }
    const close = client.subscribe('agentSession.subscribe', params, (raw) => {
      if (typeof raw === 'object' && raw !== null && 'type' in raw && raw.type === 'error') {
        // A stream's error frame carries the host's error, refusal and all, under `error`.
        onError('error' in raw && raw.error !== undefined ? raw.error : raw)
        return
      }
      if (isSubscribeEvent(raw)) {
        onEvent(raw)
      }
    })
    return { unsubscribe: close }
  }
}