Skip to content

Enterprise agent platform for building, deploying, and operating AI teammates.

4.0 KB
Raw Blame
import { apiRequest } from "@/lib/api-wrapper"

export interface BackgroundJobResponse {
  id: string
  user_id: number
  job_type: string
  queue: string
  status: string
  progress?: Record<string, unknown> | null
  result?: Record<string, unknown> | null
  error_message?: string | null
  celery_task_id?: string | null
  attempts: number
  max_attempts: number
  started_at?: string | null
  finished_at?: string | null
  created_at?: string | null
  updated_at?: string | null
}

interface BackgroundJobCapabilities {
  kb_ingest_mode?: string
}

export function isBackgroundJobResponse(value: unknown): value is BackgroundJobResponse {
  if (!value || typeof value !== "object") return false
  const candidate = value as Record<string, unknown>
  return (
    typeof candidate.id === "string" &&
    typeof candidate.job_type === "string" &&
    typeof candidate.queue === "string" &&
    typeof candidate.status === "string"
  )
}

export async function shouldUseBackgroundJobs(apiUrl: string): Promise<boolean> {
  try {
    const response = await apiRequest(`${apiUrl}/api/jobs/capabilities`)
    if (!response.ok) return false
    const data = await response.json() as BackgroundJobCapabilities
    return data.kb_ingest_mode === "celery"
  } catch {
    return false
  }
}

export function isBackgroundJobTerminal(job: BackgroundJobResponse): boolean {
  return ["succeeded", "failed", "cancelled"].includes(job.status)
}

export function getBackgroundJobProgressPercent(job: BackgroundJobResponse): number | null {
  if (job.status === "succeeded") return 100
  const completed = job.progress?.completed
  const total = job.progress?.total
  if (typeof completed === "number" && typeof total === "number" && total > 0) {
    return Math.max(0, Math.min(100, (completed / total) * 100))
  }
  return null
}

export function getBackgroundJobProgressMessage(job: BackgroundJobResponse): string | null {
  const message = job.progress?.message
  if (typeof message === "string" && message.trim()) return message
  return job.error_message || null
}

export function getBackgroundJobResult(job: BackgroundJobResponse): Record<string, unknown> | null {
  return job.result && typeof job.result === "object" ? job.result : null
}

export function getBackgroundJobFailureMessage(
  job: BackgroundJobResponse,
  fallbackMessage: string
): string {
  const resultMessage = job.result?.message
  if (typeof resultMessage === "string" && resultMessage.trim()) {
    return resultMessage
  }
  return job.error_message || fallbackMessage
}

// An ingest polls for minutes while the worker runs independently of this loop, so a
// gap the proxy can close on its own must not fail the job. The budget is wall-clock
// rather than a poll count because a failing poll takes anywhere from no time at all
// to the api-wrapper's 15s auth-refresh timeout. It bounds when the streak may end,
// not when the rejection lands: a single failure that outlasts the window is only
// seen once it returns.
const TRANSIENT_POLL_FAILURE_WINDOW_MS = 10_000

export async function waitForBackgroundJob(
  apiUrl: string,
  initialJob: BackgroundJobResponse,
  onUpdate?: (job: BackgroundJobResponse) => void
): Promise<BackgroundJobResponse> {
  let job = initialJob
  let failureWindowExpiresAt: number | null = null
  onUpdate?.(job)

  while (!isBackgroundJobTerminal(job)) {
    await new Promise(resolve => window.setTimeout(resolve, 1000))
    let data: unknown
    try {
      const response = await apiRequest(`${apiUrl}/api/jobs/${job.id}`)
      if (!response.ok) {
        throw new Error(`Failed to fetch background job ${job.id}`)
      }
      data = await response.json()
    } catch (error) {
      const now = Date.now()
      if (failureWindowExpiresAt === null) {
        failureWindowExpiresAt = now + TRANSIENT_POLL_FAILURE_WINDOW_MS
      } else if (now >= failureWindowExpiresAt) {
        throw error
      }
      continue
    }
    if (!isBackgroundJobResponse(data)) {
      throw new Error(`Invalid background job response for ${job.id}`)
    }
    failureWindowExpiresAt = null
    job = data
    onUpdate?.(job)
  }

  return job
}