fix(ai-provider): match JSON Content-Type case-insensitively (#303)

This commit is contained in:
ANIRUDDHA ADAK
2026-09-12 11:37:16 +08:00
committed by GitHub
parent d529a56f98
commit 0c6fa8fb35
2 changed files with 22 additions and 1 deletions
+1 -1
View File
@@ -109,7 +109,7 @@ export function sseErrorText(error: unknown, fallback: string): string {
*/ */
export async function jsonBodyInsteadOfSse(response: Response): Promise<string | null> { export async function jsonBodyInsteadOfSse(response: Response): Promise<string | null> {
const contentType = response.headers.get('content-type') ?? '' const contentType = response.headers.get('content-type') ?? ''
return contentType.includes('application/json') ? await response.text() : null return contentType.toLowerCase().includes('application/json') ? await response.text() : null
} }
/** /**
+21
View File
@@ -1,6 +1,7 @@
import { afterEach, describe, expect, it, vi } from 'vitest' import { afterEach, describe, expect, it, vi } from 'vitest'
import type { AgentToolCall } from '@genoffice/agent-core' import type { AgentToolCall } from '@genoffice/agent-core'
import { AiCreditsError, sseLines, streamForProvider } from '../src/stream' import { AiCreditsError, sseLines, streamForProvider } from '../src/stream'
import { jsonBodyInsteadOfSse } from '../src/protocols/shared'
import { jsonResponse, okResponse, sseStream } from './test-utils' import { jsonResponse, okResponse, sseStream } from './test-utils'
afterEach(() => { afterEach(() => {
@@ -1146,3 +1147,23 @@ describe('streamForProvider: a connection dropped mid tool arguments is not an e
expect(toolCalls.map((c) => [c.name, c.input])).toEqual([['ping', { a: 1 }]]) expect(toolCalls.map((c) => [c.name, c.input])).toEqual([['ping', { a: 1 }]])
}) })
}) })
describe('jsonBodyInsteadOfSse', () => {
it('detects JSON bodies regardless of Content-Type casing', async () => {
const payload = JSON.stringify({ choices: [] })
for (const contentType of [
'application/json',
'Application/JSON',
'APPLICATION/JSON; charset=utf-8',
'Application/Json; charset=utf-8',
]) {
const res = new Response(payload, { status: 200, headers: { 'content-type': contentType } })
await expect(jsonBodyInsteadOfSse(res)).resolves.toBe(payload)
}
const sse = new Response('data: hi\n', {
status: 200,
headers: { 'content-type': 'text/event-stream' },
})
await expect(jsonBodyInsteadOfSse(sse)).resolves.toBeNull()
})
})