(body)
| 181 | * @returns {Promise<Response>} - Raw fetch response (already checked for !ok) |
| 182 | */ |
| 183 | export async function postChatCompletion(body) { |
| 184 | const cleanedBody = {}; |
| 185 | for (const [key, value] of Object.entries(body)) { |
| 186 | if (value !== undefined && value !== null) { |
| 187 | cleanedBody[key] = value; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | const response = await fetchWithAuth( |
| 192 | `${API_BASE_URL}/v1/chat/completions`, |
| 193 | { |
| 194 | method: "POST", |
| 195 | headers: { "Content-Type": "application/json" }, |
| 196 | body: JSON.stringify(cleanedBody), |
| 197 | timeoutMs: 30000, |
| 198 | }, |
| 199 | ); |
| 200 | |
| 201 | if (!response.ok) { |
| 202 | const errorText = await response.text().catch(() => "Unknown error"); |
| 203 | if (response.status === 429) { |
| 204 | throw new Error("Rate limited. Please wait before retrying."); |
| 205 | } |
| 206 | throw new Error(parseApiError(response.status, errorText)); |
| 207 | } |
| 208 | |
| 209 | return response; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * @param {string} url - URL to fetch |
no test coverage detected