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.6 KB
Raw Blame
import type { MobileRelayEndpoint } from '../../../src/shared/mobile-relay-credential-contract'
import type { HostListSnapshot } from './host-list-load-sharing'
import type { HostCatalogEntry, StoredHostProfile } from './types'

export async function joinHostCatalogCredentials(args: {
  storedHosts: readonly StoredHostProfile[]
  relays: ReadonlyMap<string, MobileRelayEndpoint>
  tokenCache: Map<string, string>
  readToken: (hostId: string) => Promise<string | null>
  getRevision: () => number
}): Promise<HostListSnapshot> {
  const catalog: HostCatalogEntry[] = []
  const profiles: HostListSnapshot['profiles'] = []
  for (const stored of args.storedHosts) {
    let token = args.tokenCache.get(stored.id)
    let credentialStatus: HostCatalogEntry['credentialStatus'] = 'ready'
    if (!token) {
      const readRevision = args.getRevision()
      let fetched: string | null
      try {
        fetched = await args.readToken(stored.id)
      } catch {
        credentialStatus = 'temporarily-unavailable'
        fetched = null
      }
      if (!fetched && credentialStatus === 'ready') {
        credentialStatus = 'missing'
      }
      token = fetched ?? undefined
      if (token && readRevision === args.getRevision()) {
        args.tokenCache.set(stored.id, token)
      }
    }
    const relay = args.relays.get(stored.id)
    const base = { ...stored, ...(relay ? { relay } : {}) }
    const profile = token ? { ...base, deviceToken: token } : null
    catalog.push({ ...base, credentialStatus, profile })
    if (profile) {
      profiles.push(profile)
    }
  }
  return { catalog, profiles }
}