File size: 3,588 Bytes
bf8faa9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/** Typed fetch wrappers for the ROTR capability (/api/rotr/*). */

import type {
  RotrConsequenceDetail,
  RotrQueryResponse,
  RotrRegressionResult,
  RotrRunListItem,
  RotrRunSummary,
  RotrHITLReview,
  RotrStopshipPolicy,
  RotrSuite,
  RotrTrainingCandidate,
  RotrViolationDetail,
  RotrAttributionMatrix,
} from '../types/rotr';

async function handle<T>(res: Response): Promise<T> {
  if (!res.ok) {
    const body = await res.text().catch(() => '');
    throw new Error(`API ${res.status}: ${body || res.statusText}`);
  }
  return res.json() as Promise<T>;
}

const JSON_HEADERS = { 'Content-Type': 'application/json' };

function get<T>(url: string): Promise<T> {
  return fetch(url).then((res) => handle<T>(res));
}

function post<T>(url: string, body?: unknown): Promise<T> {
  return fetch(url, {
    method: 'POST',
    headers: JSON_HEADERS,
    body: body === undefined ? undefined : JSON.stringify(body),
  }).then((res) => handle<T>(res));
}

export function createRun(params: {
  n_scenarios: number;
  seed: number;
  model_version: string;
}): Promise<RotrRunSummary> {
  return post('/api/rotr/runs', params);
}

export function listRuns(): Promise<RotrRunListItem[]> {
  return get('/api/rotr/runs');
}

export function getRun(runId: string): Promise<RotrRunSummary> {
  return get(`/api/rotr/runs/${encodeURIComponent(runId)}`);
}

export function getAttributionMatrix(runId: string): Promise<RotrAttributionMatrix> {
  return get(`/api/rotr/runs/${encodeURIComponent(runId)}/attribution`);
}

export function getViolation(runId: string, violationId: string): Promise<RotrViolationDetail> {
  return get(
    `/api/rotr/runs/${encodeURIComponent(runId)}/violations/${encodeURIComponent(violationId)}`
  );
}

export function getConsequence(
  runId: string,
  violationId: string
): Promise<RotrConsequenceDetail> {
  return get(
    `/api/rotr/runs/${encodeURIComponent(runId)}/violations/${encodeURIComponent(
      violationId
    )}/consequence`
  );
}

export function structuredQuery(params: {
  run_id: string;
  text?: string;
  filters?: Record<string, string>;
}): Promise<RotrQueryResponse> {
  return post('/api/rotr/query', params);
}

export function runRegression(params: {
  baseline_run_id: string;
  candidate_run_id: string;
}): Promise<RotrRegressionResult> {
  return post('/api/rotr/regression', params);
}

export function listRegressions(): Promise<RotrRegressionResult[]> {
  return get('/api/rotr/regressions');
}

export function getHitlQueue(runId: string): Promise<RotrHITLReview[]> {
  return get(`/api/rotr/runs/${encodeURIComponent(runId)}/hitl`);
}

export function hitlAction(params: {
  run_id: string;
  review_id: string;
  action: 'VALIDATE' | 'REJECT';
  actor: string;
  notes?: string;
}): Promise<{ review: RotrHITLReview; candidate: RotrTrainingCandidate | null }> {
  return post('/api/rotr/hitl/action', params);
}

export function getSuite(): Promise<RotrSuite> {
  return get('/api/rotr/flywheel/suite');
}

export function listCandidates(): Promise<RotrTrainingCandidate[]> {
  return get('/api/rotr/flywheel/candidates');
}

export function promoteCandidate(params: {
  candidate_id: string;
  actor: string;
}): Promise<RotrTrainingCandidate> {
  return post('/api/rotr/flywheel/promote', params);
}

export function overrideCandidate(params: {
  candidate_id: string;
  actor: string;
  reason: string;
}): Promise<RotrTrainingCandidate> {
  return post('/api/rotr/flywheel/override', params);
}

export function getStopshipPolicy(): Promise<RotrStopshipPolicy> {
  return get('/api/rotr/stopship/policy');
}