Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions packages/llms/src/OpenAIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,14 @@ export class OpenAIClient implements LLMClient {

// 3. Handle HTTP errors
if (!response.ok) {
const errorData = await response.json().catch()
let errorData: unknown
const contentType = response.headers.get('content-type')
if (contentType?.includes('application/json')) {
errorData = await response.json().catch(() => undefined)
} else {
const text = await response.text().catch(() => '')
errorData = { error: { message: `Non-JSON response (${contentType || 'unknown content-type'}): ${text.slice(0, 200)}` } }
}
const errorMessage =
(errorData as { error?: { message?: string } }).error?.message || response.statusText

Expand Down Expand Up @@ -100,7 +107,25 @@ export class OpenAIClient implements LLMClient {
}

// 4. Parse and validate response
const data = await response.json()
let data: any
const responseContentType = response.headers.get('content-type')
if (!responseContentType?.includes('application/json')) {
const text = await response.text().catch(() => '')
throw new InvokeError(
InvokeErrorType.UNKNOWN,
`Expected JSON response but received ${responseContentType || 'unknown content-type'}. Body starts with: ${text.slice(0, 200)}`,
undefined
)
}
try {
data = await response.json()
} catch (error) {
throw new InvokeError(
InvokeErrorType.UNKNOWN,
'Failed to parse API response as JSON',
error
)
}

const choice = data.choices?.[0]
if (!choice) {
Expand Down
8 changes: 7 additions & 1 deletion packages/website/src/hooks/useGitHubStars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ export function useGitHubStars() {
useEffect(() => {
if (cached !== null) return
fetch(STATS_URL)
.then((r) => r.json())
.then((r) => {
const contentType = r.headers.get('content-type')
if (!r.ok || !contentType?.includes('application/json')) {
throw new Error(`Non-JSON response: ${r.status} ${contentType}`)
}
return r.json()
})
.then((data) => {
cached = data.stargazers_count ?? null
setStars(cached)
Expand Down
Loading