feat: ship Wave 5-6 multi-surface hygiene, optimization, and bug fixes
This commit is contained in:
parent
01ef3293cf
commit
66b6b45ba9
399 changed files with 17828 additions and 11330 deletions
|
|
@ -232,3 +232,42 @@ Deno.test('auth rejection response is returned as-is and non-POST is 405', async
|
|||
assertEquals(preflight.status, 200, 'preflight status')
|
||||
await preflight.body?.cancel()
|
||||
})
|
||||
|
||||
Deno.test('malformed or oversized JSON returns 400/413 and OpenAI provider attaches AbortSignal', async () => {
|
||||
const h = harness()
|
||||
const badJson = await h.handler(new Request('http://localhost/realtime-token', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: '{broken',
|
||||
}))
|
||||
assertEquals(badJson.status, 400, 'malformed json status')
|
||||
assertEquals(await badJson.json(), { error: 'invalid_json' }, 'malformed json body')
|
||||
|
||||
const oversized = await h.handler(new Request('http://localhost/realtime-token', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ instructions: 'x'.repeat(20_000) }),
|
||||
}))
|
||||
assertEquals(oversized.status, 413, 'oversized json status')
|
||||
assertEquals(await oversized.json(), { error: 'request_too_large' }, 'oversized json body')
|
||||
|
||||
const emptyPost = await h.handler(new Request('http://localhost/realtime-token', { method: 'POST' }))
|
||||
assertEquals(emptyPost.status, 200, 'empty POST defaults to {}')
|
||||
await emptyPost.body?.cancel()
|
||||
|
||||
let capturedSignal: unknown
|
||||
const provider = createOpenAiRealtimeProvider({
|
||||
readApiKey: () => 'sk-test',
|
||||
fetchImpl: (_url, init) => {
|
||||
capturedSignal = init && 'signal' in init ? init.signal : undefined
|
||||
return Promise.resolve(new Response(JSON.stringify({ value: 'ek_ok' }), { status: 200 }))
|
||||
},
|
||||
})
|
||||
const minted = await provider.mintClientSecret('user-1', {
|
||||
type: 'realtime',
|
||||
model: 'gpt-realtime-2.1-mini',
|
||||
instructions: '',
|
||||
audio: { output: { voice: 'alloy' } },
|
||||
})
|
||||
assert(minted.ok && capturedSignal instanceof AbortSignal, 'provider request must carry AbortSignal')
|
||||
})
|
||||
|
|
|
|||
|
|
@ -14,7 +14,8 @@
|
|||
// 공급자 에러 본문(조직 ID, 마스킹된 키 힌트 등)은 절대 응답에 싣지 않는다.
|
||||
// 상태 코드만 서버 로그에 남기고 고정 코드를 반환한다.
|
||||
|
||||
import { corsHeaders, handleCorsPreflightRequest } from '../_shared/cors.ts'
|
||||
import { handleCorsPreflightRequest } from '../_shared/cors.ts'
|
||||
import { createStatusJsonResponder, JsonBodyError, readJsonBody } from '../_shared/json-response.ts'
|
||||
import {
|
||||
buildRealtimeSessionConfig,
|
||||
type RealtimeSessionConfig,
|
||||
|
|
@ -75,21 +76,15 @@ export interface RealtimeTokenDeps {
|
|||
logger: RealtimeLogger
|
||||
}
|
||||
|
||||
function json(
|
||||
status: number,
|
||||
payload: Record<string, unknown>,
|
||||
): Response {
|
||||
return new Response(JSON.stringify(payload), {
|
||||
status,
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
||||
})
|
||||
}
|
||||
const MAX_REQUEST_BYTES = 16 * 1024
|
||||
const json = createStatusJsonResponder()
|
||||
|
||||
async function readBody(req: Request): Promise<RealtimeTokenRequest> {
|
||||
const parsed: unknown = await req.json().catch(() => ({}))
|
||||
return parsed && typeof parsed === 'object' && !Array.isArray(parsed)
|
||||
? parsed as RealtimeTokenRequest
|
||||
: {}
|
||||
return await readJsonBody(req, {
|
||||
maxBytes: MAX_REQUEST_BYTES,
|
||||
allowEmptyObject: true,
|
||||
requirePlainObject: true,
|
||||
}) as RealtimeTokenRequest
|
||||
}
|
||||
|
||||
export function createRealtimeTokenHandler(deps: RealtimeTokenDeps): (req: Request) => Promise<Response> {
|
||||
|
|
@ -167,6 +162,9 @@ export function createRealtimeTokenHandler(deps: RealtimeTokenDeps): (req: Reque
|
|||
|
||||
return json(200, { ...minted.data, model: resolution.model, tier })
|
||||
} catch (err) {
|
||||
if (err instanceof JsonBodyError) {
|
||||
return json(err.status, { error: err.code })
|
||||
}
|
||||
logger.error('realtime-token failed', {
|
||||
kind: err instanceof Error ? err.name : typeof err,
|
||||
})
|
||||
|
|
|
|||
|
|
@ -11,10 +11,14 @@ export interface OpenAiRealtimeProviderOptions {
|
|||
/** 호출마다 읽는다 — 시크릿 교체가 재배포 없이 반영되도록. 빈 문자열이면 미설정. */
|
||||
readApiKey(): string
|
||||
fetchImpl?: typeof fetch
|
||||
timeoutMs?: number
|
||||
}
|
||||
|
||||
const DEFAULT_REALTIME_TIMEOUT_MS = 15_000
|
||||
|
||||
export function createOpenAiRealtimeProvider(options: OpenAiRealtimeProviderOptions): RealtimeProviderPort {
|
||||
const fetchImpl = options.fetchImpl ?? fetch
|
||||
const timeoutMs = options.timeoutMs ?? DEFAULT_REALTIME_TIMEOUT_MS
|
||||
|
||||
return {
|
||||
isConfigured(): boolean {
|
||||
|
|
@ -35,6 +39,7 @@ export function createOpenAiRealtimeProvider(options: OpenAiRealtimeProviderOpti
|
|||
'OpenAI-Safety-Identifier': userId,
|
||||
},
|
||||
body: JSON.stringify({ session }),
|
||||
signal: AbortSignal.timeout(timeoutMs),
|
||||
})
|
||||
} catch {
|
||||
return { ok: false, reason: 'network' }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue