{"path":"lib/sdk/src/oracle.ts","size":2130,"locked":false,"binary":false,"truncated":false,"content":"import {\n  SubmitRequest,\n  SubmitResponse,\n  OracleHealthResponse,\n  OracleJobResponse,\n} from \"./types.js\";\n\n// ─── ORACLE CLIENT ────────────────────────────────────────────────────────────\n\nexport class OracleClient {\n  private readonly baseUrl: string;\n\n  constructor(baseUrl: string) {\n    this.baseUrl = baseUrl.replace(/\\/$/, \"\");\n  }\n\n  private async request<T>(path: string, init?: RequestInit): Promise<T> {\n    const url = `${this.baseUrl}${path}`;\n    const res = await fetch(url, {\n      headers: { \"Content-Type\": \"application/json\" },\n      ...init,\n    });\n\n    const text = await res.text();\n\n    let body: T & { error?: string };\n    try {\n      body = JSON.parse(text) as T & { error?: string };\n    } catch {\n      throw new OracleError(\n        !res.ok\n          ? `HTTP ${res.status}: ${text.slice(0, 200)}`\n          : `Unexpected non-JSON response from oracle: ${text.slice(0, 200)}`,\n        res.status,\n      );\n    }\n\n    if (!res.ok) {\n      const message = body.error ?? `HTTP ${res.status}`;\n      throw new OracleError(message, res.status);\n    }\n\n    return body;\n  }\n\n  async health(): Promise<OracleHealthResponse> {\n    return this.request<OracleHealthResponse>(\"/health\");\n  }\n\n  async getJob(jobId: string): Promise<OracleJobResponse> {\n    return this.request<OracleJobResponse>(`/jobs/${encodeURIComponent(jobId)}`);\n  }\n\n  async submitDeliverable(payload: SubmitRequest): Promise<SubmitResponse> {\n    return this.request<SubmitResponse>(\"/submit\", {\n      method:  \"POST\",\n      body:    JSON.stringify(payload),\n    });\n  }\n}\n\n// ─── ORACLE ERROR ─────────────────────────────────────────────────────────────\n\nexport class OracleError extends Error {\n  readonly statusCode: number;\n\n  constructor(message: string, statusCode: number) {\n    super(message);\n    this.name       = \"OracleError\";\n    this.statusCode = statusCode;\n  }\n}\n"}