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.0 KB
Raw Blame
import AsyncStorage from '@react-native-async-storage/async-storage'
import { StoredHostProfileSchema, type HostProfile, type StoredHostProfile } from './types'
import { classifyLegacyHostName } from './host-name-identity'

const STORAGE_KEY = 'orca:hosts'

export async function loadStoredHostProfiles(): Promise<StoredHostProfile[] | null> {
  return parseStoredHostProfiles(await AsyncStorage.getItem(STORAGE_KEY))
}

export async function readStoredHostProfilesForMutation(): Promise<StoredHostProfile[]> {
  try {
    const parsed = await loadStoredHostProfiles()
    if (parsed) {
      return parsed
    }
  } catch {
    // Normalize storage and payload failures for fail-closed mutations.
  }
  throw new Error('host list storage unreadable')
}

export function writeStoredHostProfiles(hosts: readonly StoredHostProfile[]): Promise<void> {
  return AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(hosts))
}

export function toStoredHostProfile(host: HostProfile): StoredHostProfile {
  const {
    id,
    name,
    personalName,
    lastKnownMachineName,
    lastKnownHostPlatform,
    endpoint,
    publicKeyB64,
    lastConnected
  } = host
  return {
    id,
    name,
    ...(personalName !== undefined ? { personalName } : {}),
    ...(lastKnownMachineName !== undefined ? { lastKnownMachineName } : {}),
    ...(lastKnownHostPlatform !== undefined ? { lastKnownHostPlatform } : {}),
    endpoint,
    publicKeyB64,
    lastConnected
  }
}

function parseStoredHostProfiles(raw: string | null): StoredHostProfile[] | null {
  if (!raw) {
    return []
  }
  try {
    const parsed = JSON.parse(raw) as unknown
    if (!Array.isArray(parsed)) {
      return null
    }
    return parsed.flatMap((item) => {
      // Why: pre-v0.0.3 records embedded secrets; users re-pair instead of migrating them.
      if (item && typeof item === 'object' && 'deviceToken' in item) {
        return []
      }
      const result = StoredHostProfileSchema.safeParse(item)
      return result.success ? [classifyLegacyHostName(result.data)] : []
    })
  } catch {
    return null
  }
}