import { getWorkerClientConfigFromEnv } from '@/lib/server/compute/worker'; import type { WorkerOperationState } from '@openreader/compute-core/api-contracts'; const WORKER_OP_REQUEST_TIMEOUT_MS = 2_500; export async function fetchWorkerOperationState( opId: string | null | undefined, ): Promise | null> { const normalized = opId?.trim(); if (!normalized) return null; let cfg: { baseUrl: string; token: string }; try { cfg = getWorkerClientConfigFromEnv(); } catch (error) { console.warn('[worker-op-state] worker client env missing/invalid', { opId: normalized, error: error instanceof Error ? error.message : String(error), }); return null; } const controller = new AbortController(); const timeout = setTimeout(() => controller.abort(), WORKER_OP_REQUEST_TIMEOUT_MS); try { const res = await fetch(`${cfg.baseUrl}/ops/${encodeURIComponent(normalized)}`, { method: 'GET', headers: { Authorization: `Bearer ${cfg.token}`, Accept: 'application/json', }, cache: 'no-store', signal: controller.signal, }); if (!res.ok) { const detail = await res.text().catch(() => ''); console.warn('[worker-op-state] worker op request failed', { opId: normalized, status: res.status, detail, }); return null; } const parsed = await res.json() as WorkerOperationState; if (!parsed || typeof parsed !== 'object' || parsed.opId !== normalized) { console.warn('[worker-op-state] worker op response invalid', { opId: normalized, }); return null; } return parsed; } catch (error) { console.warn('[worker-op-state] worker op request threw', { opId: normalized, error: error instanceof Error ? error.message : String(error), }); return null; } finally { clearTimeout(timeout); } }