Spaces:
Build error
Build error
File size: 4,461 Bytes
6a059d3 | 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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | "use client"
const API_URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000"
/**
* Enhanced Query Options
*/
export interface QueryOptions {
knowledgeBases?: string[] // kenya_law, kenya_news, parliament, general
model?: string // gemini-2.5-flash, gpt-4o-mini, gpt-4o, claude-3.5-sonnet
useReranking?: boolean // Enable intelligent re-ranking
useHyDE?: boolean // Enable HyDE query expansion
temperature?: number // LLM temperature (0-1)
userId?: string // User ID for profiling
}
export interface StreamCallback {
onToken?: (token: string) => void
onComplete?: (response: unknown) => void
onError?: (error: Error) => void
}
export class ApiClient {
private baseURL: string
constructor(baseURL: string = API_URL) {
this.baseURL = baseURL
}
private getHeaders(): HeadersInit {
const headers: HeadersInit = {
"Content-Type": "application/json",
}
// Add session token if available
const sessionToken = localStorage.getItem("session_token")
if (sessionToken) {
headers["X-Session-Token"] = sessionToken
}
return headers
}
private getAuthOnlyHeaders(): HeadersInit {
const headers: HeadersInit = {}
// Add session token if available (no Content-Type for FormData)
const sessionToken = localStorage.getItem("session_token")
if (sessionToken) {
headers["X-Session-Token"] = sessionToken
}
return headers
}
async get<T>(endpoint: string): Promise<T> {
const response = await fetch(`${this.baseURL}${endpoint}`, {
method: "GET",
headers: this.getHeaders(),
})
if (!response.ok) {
const error = await response.json().catch(() => ({ detail: "Request failed" }))
throw new Error(error.detail || `HTTP ${response.status}`)
}
return response.json()
}
async post<T>(endpoint: string, data?: unknown): Promise<T> {
const response = await fetch(`${this.baseURL}${endpoint}`, {
method: "POST",
headers: this.getHeaders(),
body: data ? JSON.stringify(data) : undefined,
})
if (!response.ok) {
const error = await response.json().catch(() => ({ detail: "Request failed" }))
throw new Error(error.detail || `HTTP ${response.status}`)
}
return response.json()
}
async put<T>(endpoint: string, data?: unknown): Promise<T> {
const response = await fetch(`${this.baseURL}${endpoint}`, {
method: "PUT",
headers: this.getHeaders(),
body: data ? JSON.stringify(data) : undefined,
})
if (!response.ok) {
const error = await response.json().catch(() => ({ detail: "Request failed" }))
throw new Error(error.detail || `HTTP ${response.status}`)
}
return response.json()
}
async delete<T>(endpoint: string): Promise<T> {
const response = await fetch(`${this.baseURL}${endpoint}`, {
method: "DELETE",
headers: this.getHeaders(),
})
if (!response.ok) {
const error = await response.json().catch(() => ({ detail: "Request failed" }))
throw new Error(error.detail || `HTTP ${response.status}`)
}
return response.json()
}
/**
* Upload files using FormData (multipart/form-data)
* Used for media uploads to /api/v1/media/* endpoints
*/
async uploadFormData<T>(endpoint: string, formData: FormData): Promise<T> {
const response = await fetch(`${this.baseURL}${endpoint}`, {
method: "POST",
headers: this.getAuthOnlyHeaders(), // No Content-Type - browser sets it with boundary
body: formData,
})
if (!response.ok) {
const error = await response.json().catch(() => ({ detail: "Upload failed" }))
throw new Error(error.detail || `HTTP ${response.status}`)
}
return response.json()
}
/**
* Upload a single file to the media API
*/
async uploadFile<T>(
endpoint: string,
file: File,
additionalFields?: Record<string, string>
): Promise<T> {
const formData = new FormData()
formData.append("file", file)
if (additionalFields) {
Object.entries(additionalFields).forEach(([key, value]) => {
formData.append(key, value)
})
}
return this.uploadFormData<T>(endpoint, formData)
}
}
export const apiClient = new ApiClient()
|