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.

7.0 KB
Raw Blame
import { resolveSessionSearchLimit, SESSION_SEARCH_LIMIT_MAX } from './ai-vault-search-limit'
import { z } from 'zod'
import {
  AI_VAULT_AGENTS,
  AI_VAULT_SCOPE_PATHS_MAX_COUNT,
  AI_VAULT_SEARCH_SORTS
} from './ai-vault-types'
import { AiVaultSearchScopeIdentitySchema } from './ai-vault-search-scope'

// Hosts compare `since` as text against UTC `toISOString()` timestamps, which keep
// milliseconds only; a dropped sub-millisecond remainder rounds up so no earlier session gets in.
function sinceAsStoredInstant(since: string): string {
  const subMillisecond = /\.\d{3}(\d+)/.exec(since)?.[1] ?? ''
  const cutoffMs = Date.parse(since) + (/[1-9]/.test(subMillisecond) ? 1 : 0)
  return new Date(cutoffMs).toISOString()
}

export const AiVaultSearchFiltersSchema = z.object({
  agents: z.array(z.enum(AI_VAULT_AGENTS)).optional(),
  scopePaths: z.array(z.string().min(1).max(4096)).max(AI_VAULT_SCOPE_PATHS_MAX_COUNT).optional(),
  since: z.string().datetime({ offset: true }).transform(sinceAsStoredInstant).optional(),
  sort: z.enum(AI_VAULT_SEARCH_SORTS).optional()
})

// Strip unknown fields so legacy tier/refresh are accepted without affecting the query.
export const AiVaultSearchRequestSchema = z
  .object({
    query: z.string(),
    scope: z.enum(['conversation', 'all']).optional(),
    freshness: z.enum(['indexed', 'wait-until-current']).optional(),
    limit: z.number().optional().transform(resolveSessionSearchLimit),
    cursor: z.string().optional(),
    filters: AiVaultSearchFiltersSchema.optional(),
    supportedAgents: z.array(z.string()).optional(),
    supportsQoderHistory: z.boolean().optional(),
    supportsJcodeHistory: z.boolean().optional(),
    /** Scope by identity, resolved into paths by whichever host answers. */
    within: AiVaultSearchScopeIdentitySchema.optional(),
    debug: z.boolean().optional()
  })
  // Refuse intersecting scopes instead of guessing and widening; neither scope still means all sessions.
  .refine(
    (request) => request.within === undefined || (request.filters?.scopePaths ?? []).length === 0,
    { message: 'A search carries either a scope identity or explicit scope paths, not both' }
  )

export const AiVaultSearchSourceSchema = z.object({
  presence: z.enum(['present', 'unverifiable', 'missing']),
  filePath: z.string().optional(),
  codexHome: z.string().optional()
})
export const AiVaultSearchEvidenceSchema = z.object({
  snippet: z.string(),
  role: z.enum(['user', 'assistant', 'tool', 'system', 'unknown']),
  timestamp: z.string().nullable()
})
// Older hosts may omit attribution; the desktop stamps remote answers.
const executionHostIdSchema = z.string().min(1)

export const AiVaultSearchHitSchema = z
  .object({
    agent: z.enum(AI_VAULT_AGENTS),
    executionHostId: executionHostIdSchema.optional(),
    sessionId: z.string(),
    title: z.string(),
    cwd: z.string().nullable(),
    branch: z.string().nullable(),
    updatedAt: z.string().nullable(),
    messageCount: z.number().int().nonnegative(),
    score: z.number(),
    source: AiVaultSearchSourceSchema,
    evidence: AiVaultSearchEvidenceSchema.nullable(),
    resumeCommand: z.string().optional(),
    /** The native chat owning this transcript, from the indexing host; older hosts omit it. */
    structuredSession: z
      .object({ sessionId: z.string().min(1).max(512), workspaceId: z.string().min(1).max(512) })
      .optional()
  })
  .refine((hit) => hit.source.presence === 'present' || hit.resumeCommand === undefined, {
    message: 'Only present sources may have a resume command'
  })
export const AiVaultSearchPageSchema = z.object({
  cursor: z.string().nullable(),
  hasMore: z.boolean()
})
export const AiVaultSearchTruncationSchema = z.object({
  candidates: z.boolean(),
  snippets: z.number().int().nonnegative(),
  query: z.boolean(),
  freshness: z.boolean()
})
/** Desktop-only additive per-host results for all-computers merges; hosts never publish this and older readers drop it. */
export const AiVaultSearchHostOutcomeSchema = z.object({
  executionHostId: executionHostIdSchema,
  outcome: z.enum([
    'searched',
    'stale',
    'disabled',
    'not-ready',
    'no-service',
    'unreachable',
    // This host does not know the workspace or project the scope named.
    'scope-unknown',
    'unsupported-agent'
  ])
})
const routeSchema = z.enum(['phrase', 'and', 'or', 'typo+phrase', 'typo+and', 'typo+or'])
export const AiVaultSearchPlannerReportSchema = z.object({
  route: routeSchema,
  repairedTerms: z.array(z.string()).optional(),
  scope: z.enum(['conversation', 'all'])
})
export const AiVaultSearchDebugSchema = z.object({
  route: routeSchema,
  repairedTerms: z.array(z.string()).optional(),
  plannerReport: AiVaultSearchPlannerReportSchema
})
export const AiVaultSearchResponseSchema = z.discriminatedUnion('kind', [
  z.object({
    kind: z.literal('results'),
    hits: z.array(AiVaultSearchHitSchema).max(SESSION_SEARCH_LIMIT_MAX),
    page: AiVaultSearchPageSchema,
    generation: z.number().int().nonnegative(),
    truncated: AiVaultSearchTruncationSchema,
    durationMs: z.number().nonnegative(),
    debug: AiVaultSearchDebugSchema.optional(),
    hosts: z.array(AiVaultSearchHostOutcomeSchema).optional()
  }),
  z.object({
    kind: z.literal('stale-cursor'),
    generation: z.number().int().nonnegative(),
    expectedGeneration: z.number().int().nonnegative().optional()
  }),
  z.object({ kind: z.literal('malformed-cursor') }),
  z.object({
    kind: z.literal('unavailable'),
    // scope-unknown answers only within requests, which older clients cannot send, so they never receive an unknown reason.
    reason: z.enum(['disabled', 'not-ready', 'no-service', 'scope-unknown', 'unsupported-agent'])
  })
])
export const AiVaultSearchStatusRequestSchema = z.object({})
/** Consent flip for one host's index. Answered with that host's status after the change is applied. */
export const AiVaultSetSearchEnabledParamsSchema = z.object({ enabled: z.boolean() })
export const AiVaultSearchStatusSchema = z.object({
  // Strings keep a future host's larger catalog readable by this client.
  supportedAgents: z.array(z.string()).optional(),
  supportsQoderHistory: z.boolean().optional(),
  supportsJcodeHistory: z.boolean().optional(),
  enabled: z.boolean(),
  phase: z.enum(['idle', 'indexing', 'current', 'degraded', 'closed']),
  filesIndexed: z.number().int().nonnegative(),
  filesDue: z.number().int().nonnegative(),
  filesFailed: z.number().int().nonnegative(),
  // Optional: a host that predates this field degrades to a session count only.
  messagesIndexed: z.number().int().nonnegative().optional(),
  // `root` is a host path, withheld over the relay; the array length is the count.
  degradedRoots: z.array(z.object({ root: z.string().optional(), reason: z.string() })),
  lastReconcileAt: z.number().nullable(),
  lastSweepCompletedAt: z.number().nullable(),
  // Older hosts omit breakdowns; missing means no breakdown, never zero counts.
  sessionsByAgent: z.record(z.string(), z.number().int().nonnegative()).optional(),
  generation: z.number().int().nonnegative()
})