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.

3.4 KB
Raw Blame
import { z } from 'zod'

export const PROFILE_STATE_RECOVERY_FLAG = '--profile-state-recovery'
export const PROFILE_STATE_RECOVERY_RESULT_PREFIX = '[profile-state-recovery] '
/** Desktop variant: applies a startup-dialog choice, then relaunches Orca normally. */
export const PROFILE_STATE_DESKTOP_RECOVERY_FLAG = '--profile-state-desktop-recovery'

const positiveInteger = z.number().int().positive().max(Number.MAX_SAFE_INTEGER)
const repairSectionSchema = z
  .object({ domain: z.string(), backupId: z.string().min(1).nullable() })
  .strict()
const selectorSchema = z.discriminatedUnion('kind', [
  z.object({ kind: z.literal('json'), revision: positiveInteger }).strict(),
  z.object({ kind: z.literal('current-json') }).strict(),
  z.object({ kind: z.literal('current-sqlite') }).strict(),
  z.object({ kind: z.literal('latest-json') }).strict(),
  z.object({ kind: z.literal('sqlite'), backupId: z.string().min(1) }).strict(),
  // Sections: the plan the user approved; omitted, the CLI repairs whatever it finds.
  z
    .object({ kind: z.literal('repair'), sections: z.array(repairSectionSchema).optional() })
    .strict()
])

export const profileStateRecoveryRequestSchema = z
  .object({
    userDataPath: z.string().min(1),
    selector: selectorSchema,
    profileId: z
      .string()
      .regex(/^[A-Za-z0-9][A-Za-z0-9_-]{0,127}$/)
      .optional()
  })
  .strict()

const exportsSchema = z.object({
  profileId: z.string(),
  dataFile: z.string(),
  databaseFile: z.string(),
  exportPaths: z.array(z.string()).readonly(),
  backups: z
    .array(z.object({ id: z.string(), path: z.string(), createdAtMs: positiveInteger }))
    .readonly()
})
const rollbackSchema = exportsSchema.extend({
  // Canonical JSON edited outside SQLite has no database revision.
  revision: positiveInteger.nullable(),
  quarantineDirectory: z.string(),
  removedDatabaseFiles: z.array(z.string()).readonly(),
  storage: z.enum(['json', 'sqlite']),
  restoredPath: z.string(),
  backupId: z.string().optional(),
  repairedSections: z
    .array(repairSectionSchema.extend({ backupCreatedAtMs: positiveInteger.optional() }))
    .readonly()
    .optional()
})

export const profileStateRecoveryResponseSchema = z.discriminatedUnion('ok', [
  z.object({ ok: z.literal(true), result: rollbackSchema }),
  z.object({
    ok: z.literal(false),
    code: z.enum(['invalid_argument', 'runtime_error']),
    message: z.string()
  })
])

export type ProfileStateRecoverySelector = z.infer<typeof selectorSchema>
export type ProfileStateRecoveryRepairSection = z.infer<typeof repairSectionSchema>
export type ProfileStateRecoveryRequest = z.infer<typeof profileStateRecoveryRequestSchema>
export type ProfileStateRecoveryResponse = z.infer<typeof profileStateRecoveryResponseSchema>
export type ProfileStateExportsResult = z.infer<typeof exportsSchema>
export type ProfileStateRollbackResult = z.infer<typeof rollbackSchema>

export class ProfileStateRecoveryCommandError extends Error {
  constructor(
    readonly code: 'invalid_argument' | 'runtime_error',
    message: string
  ) {
    super(message)
    this.name = 'ProfileStateRecoveryCommandError'
  }
}

export function isProfileStateRecoveryCommandError(
  error: unknown
): error is Error & { code: 'invalid_argument' | 'runtime_error' } {
  return (
    error instanceof Error &&
    'code' in error &&
    (error.code === 'invalid_argument' || error.code === 'runtime_error')
  )
}