File size: 4,258 Bytes
fb042fa
 
472d0dd
db165bb
fb042fa
 
472d0dd
fb042fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
472d0dd
 
 
 
fb042fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
472d0dd
 
 
 
 
 
 
 
 
 
 
 
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
import type { DebugConfig, DebugRunDetail, DebugRunSummary, DebugSession, DebugUnitSpec } from './types'

export const DEBUG_BASE_URL = import.meta.env.VITE_API_BASE_URL ||
  (import.meta.env.PROD ? '' : 'http://localhost:8000') // I guess I'd better follow what api.ts is doing

async function debugFetch<T>(path: string, init?: RequestInit): Promise<T> {
  const res = await fetch(`${DEBUG_BASE_URL}${path}`, {
    credentials: 'include',
    headers: {
      'Content-Type': 'application/json',
      ...(init?.headers || {}),
    },
    ...init,
  })
  const text = await res.text().catch(() => '')
  const parse = () => {
    try { return text ? JSON.parse(text) : {} } catch { return text }
  }
  if (!res.ok) {
    const parsed = parse() as any
    const detail = typeof parsed === 'string' ? parsed : parsed?.detail || text
    throw new Error(`HTTP ${res.status} ${res.statusText}${detail ? `: ${detail}` : ''}`)
  }
  return parse() as T
}

export async function fetchOpenApiSpec(): Promise<any> {
  return debugFetch('/openapi.json', { method: 'GET' })
}

export function getDebugConfig(): Promise<DebugConfig> {
  return debugFetch<DebugConfig>('/internal/debug/config', { method: 'GET' })
}

export async function debugLogin(token: string): Promise<{ ok: boolean; session: DebugSession }> {
  return debugFetch('/internal/debug/login', {
    method: 'POST',
    body: JSON.stringify({ token }),
  })
}

export async function debugLogout(): Promise<{ ok: boolean }> {
  return debugFetch('/internal/debug/logout', { method: 'POST', body: '{}' })
}

export async function getDebugSession(): Promise<{ ok: boolean; session: DebugSession }> {
  return debugFetch('/internal/debug/session', { method: 'GET' })
}

export async function listDebugRuns(): Promise<{ runs: DebugRunSummary[] }> {
  return debugFetch('/internal/debug/behavior-tests', { method: 'GET' })
}

export async function startBehaviorDebugRun(payload: {
  query: string
  user_id?: string
  conversation_id?: string
  use_online_agent?: boolean
  auto_confirm?: boolean
  confirm_message?: string
  max_wait_seconds?: number
  poll_interval_ms?: number
}): Promise<{ ok: boolean; run_id: string; status: string }> {
  return debugFetch('/internal/debug/behavior-tests', {
    method: 'POST',
    body: JSON.stringify(payload),
  })
}

export async function trackBehaviorDebugTask(payload: {
  task_id: string
  user_id?: string
  conversation_id?: string
  max_wait_seconds?: number
  poll_interval_ms?: number
}): Promise<{ ok: boolean; run_id: string; status: string }> {
  return debugFetch('/internal/debug/behavior-tests/track', {
    method: 'POST',
    body: JSON.stringify(payload),
  })
}

export async function getBehaviorDebugRun(runId: string): Promise<DebugRunDetail> {
  return debugFetch(`/internal/debug/behavior-tests/${runId}`, { method: 'GET' })
}

export async function explainBehaviorDebugRun(runId: string): Promise<{ ok: boolean; mode: string; explanation: any }> {
  return debugFetch(`/internal/debug/behavior-tests/${runId}/explain`, {
    method: 'POST',
    body: JSON.stringify({ mode: 'nl_explain' }),
  })
}

export async function listDebugUnits(): Promise<{ units: DebugUnitSpec[] }> {
  return debugFetch('/internal/debug/unit-tests/units', { method: 'GET' })
}

export async function generateDebugUnitInput(unitName: string, mode: 'schema' | 'sample' | 'llm'): Promise<any> {
  return debugFetch('/internal/debug/unit-tests/generate-input', {
    method: 'POST',
    body: JSON.stringify({ unit_name: unitName, mode }),
  })
}

export async function runDebugUnit(payload: {
  unit_name: string
  input_mode: 'manual' | 'sample' | 'schema' | 'llm'
  input_data?: Record<string, any>
  use_llm_generation?: boolean
}): Promise<any> {
  return debugFetch('/internal/debug/unit-tests/run', {
    method: 'POST',
    body: JSON.stringify(payload),
  })
}

export async function generateDebugApiPlaygroundInput(payload: {
  mode: 'schema' | 'llm'
  schema: Record<string, any>
  method?: string
  path?: string
  summary?: string
}): Promise<{ ok: boolean; mode: string; input_data: any; validation_errors: string[] }> {
  return debugFetch('/internal/debug/api-playground/generate-input', {
    method: 'POST',
    body: JSON.stringify(payload),
  })
}