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.5 KB
Raw Blame
import type { AgentSessionModelCatalogResult } from './agent-session-wire'

// Coalesce picker reads per chat across mounts/effects: sent remote requests cannot be withdrawn.
// Continue bounded reads while interested joiners remain.

/** Null when the read failed or timed out. */
export type HostModelListingJoiner = (catalog: AgentSessionModelCatalogResult | null) => void

type ListingWait = {
  joiners: Set<HostModelListingJoiner>
  continuation: ReturnType<typeof setTimeout> | null
}
type HostModelListingTransport = { close: () => void }
const waits = new Map<string, ListingWait>()
const ownerWaits = new WeakMap<HostModelListingTransport, Map<string, ListingWait>>()

function listingWaits(owner?: HostModelListingTransport): Map<string, ListingWait> {
  if (!owner) {
    return waits
  }
  let owned = ownerWaits.get(owner)
  if (!owned) {
    owned = new Map()
    ownerWaits.set(owner, owned)
  }
  return owned
}

/** Joins the chat's waiting read, started by `read` only when none is in flight. Returns `leave`. */
export function joinHostModelListingWait(
  key: string,
  read: () => Promise<AgentSessionModelCatalogResult>,
  onSettled: HostModelListingJoiner,
  owner?: HostModelListingTransport
): () => void {
  const waits = listingWaits(owner)
  let wait = waits.get(key)
  if (!wait) {
    const started: ListingWait = { joiners: new Set(), continuation: null }
    wait = started
    waits.set(key, started)
    const settle = (catalog: AgentSessionModelCatalogResult | null): void => {
      try {
        for (const joiner of started.joiners) {
          joiner(catalog)
        }
      } finally {
        if (catalog?.listingInProgress && started.joiners.size > 0) {
          // A fast host reply must not turn a queued listing into a request spin.
          started.continuation = setTimeout(run, 1_000)
        } else {
          waits.delete(key)
        }
      }
    }
    const run = (): void => {
      started.continuation = null
      new Promise<AgentSessionModelCatalogResult>((resolve) => resolve(read())).then(settle, () =>
        settle(null)
      )
    }
    run()
  }
  wait.joiners.add(onSettled)
  const joined = wait
  return () => {
    joined.joiners.delete(onSettled)
    if (joined.joiners.size === 0 && joined.continuation !== null) {
      clearTimeout(joined.continuation)
      waits.delete(key)
    }
  }
}

export function isHostModelListingWaitInFlight(
  key: string,
  owner?: HostModelListingTransport
): boolean {
  return listingWaits(owner).has(key)
}