Spaces:
Sleeping

File size: 3,125 Bytes
9c20549
 
d39c308
9c20549
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
628ad6e
 
 
 
 
 
003a245
d39c308
ca39cf3
d838024
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Thin wrappers over the HTTP API. Errors carry the server's own reason.

import type { InboxItem, IntakeEval, LedgerScores, ModelInfo, Onboarding, PolicyRule, Quote, RepriceResult, Run, Simulation } from './types'

export class ApiError extends Error {
  status: number
  constructor(status: number, message: string) {
    super(message)
    this.status = status
  }
}

async function call<T>(path: string, init?: RequestInit): Promise<T> {
  const response = await fetch(path, {
    ...init,
    headers: { 'content-type': 'application/json', ...(init?.headers ?? {}) },
  })
  if (!response.ok) {
    let message = `HTTP ${response.status}`
    try {
      const body = await response.json()
      if (typeof body.detail === 'string') message = body.detail
      else if (Array.isArray(body.detail)) message = body.detail.map((d: { msg: string }) => d.msg).join('; ')
    } catch {
      // not JSON
    }
    throw new ApiError(response.status, message)
  }
  return (await response.json()) as T
}

export const getInfo = () => call<{ provider: string; policy_version: string; sample_sender: string }>('/api/info')
export const getInbox = () => call<InboxItem[]>('/api/inbox')
export const getQuote = (id: string) => call<Quote>(`/api/quotes/${id}`)
export const getRuns = () => call<Run[]>('/api/runs')
export const getModel = () => call<ModelInfo>('/api/model')

export const postRfq = (sender: string, subject: string, body: string) =>
  call<{ run_id: string }>('/api/rfq', { method: 'POST', body: JSON.stringify({ sender, subject, body }) })

export const decide = (
  id: string,
  decision: 'approved' | 'rejected',
  role: 'estimator' | 'senior',
  expectedVersion: number,
  reason = '',
) =>
  call<{ position: number }>(`/api/quotes/${id}/decide`, {
    method: 'POST',
    body: JSON.stringify({ decision, role, reason, expected_version: expectedVersion }),
  })

export const reprice = (copperPct: number) =>
  call<RepriceResult>('/api/reprice', { method: 'POST', body: JSON.stringify({ copper_pct: copperPct }) })
export const getPolicy = () => call<{ version: string; rules: PolicyRule[] }>('/api/policy')
export const simulatePolicy = (ruleId: string, value: number) =>
  call<Simulation>('/api/policy/simulate', { method: 'POST', body: JSON.stringify({ rule_id: ruleId, value }) })
export const getSources = () => call<Onboarding>('/api/sources')
export const getLedger = () => call<LedgerScores>('/api/foundation-ledger')
export const getIntakeEval = () => call<IntakeEval>('/api/intake-eval')
export const getClock = () =>
  call<{ business_date: string; followups: { drafted: number; sent: number; rejected: number } }>('/api/clock')
export const advanceClock = (days: number) =>
  call<{ business_date: string; actions: { kind: string; quote_id: string; reason: string }[] }>('/api/clock/advance', {
    method: 'POST',
    body: JSON.stringify({ days }),
  })
export const decideFollowup = (id: string, decision: 'approved' | 'rejected', role: 'estimator' | 'senior') =>
  call<{ position: number }>('/api/quotes/' + id + '/followup', { method: 'POST', body: JSON.stringify({ decision, role }) })