From 2f5e1858bb941f1e84bfd5bee6e492eec59e8c43 Mon Sep 17 00:00:00 2001 From: backryun Date: Tue, 28 Jul 2026 12:45:25 +0900 Subject: [PATCH] fix(types): normalize Kie transcription results (#8824) --- open-sse/handlers/audioTranscription.ts | 20 +++-- .../kie-audio-transcription-result.test.ts | 77 +++++++++++++++++++ 2 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 tests/unit/kie-audio-transcription-result.test.ts diff --git a/open-sse/handlers/audioTranscription.ts b/open-sse/handlers/audioTranscription.ts index f119860cada..aea16a03771 100644 --- a/open-sse/handlers/audioTranscription.ts +++ b/open-sse/handlers/audioTranscription.ts @@ -24,6 +24,7 @@ import { buildAuthHeaders } from "../config/registryUtils.ts"; import { kieExecutor } from "../executors/kie.ts"; import { vertexTranscribe } from "../executors/vertexMedia.ts"; import { errorResponse } from "../utils/error.ts"; +import { isJsonObject } from "../utils/kieTask.ts"; import { handleOpenRouterTranscription } from "./openrouterTranscription.ts"; type TranscriptionCredentials = { @@ -393,6 +394,18 @@ async function handleHuggingFaceTranscription(providerConfig, file, modelId, tok /** * Handle Kie.ai transcription */ +function normalizeKieTranscriptionText(recordData: unknown): string { + const record = isJsonObject(recordData) ? recordData : {}; + const data = isJsonObject(record.data) ? record.data : {}; + const response = isJsonObject(data.response) ? data.response : {}; + + for (const value of [response.text, data.resultText, data.text, record.text]) { + if (typeof value === "string") return value; + } + + return ""; +} + async function handleKieAudioTranscription(providerConfig, file, modelId, token) { const baseUrl = providerConfig.baseUrl.replace(/\/$/, ""); const fileBuffer = await file.arrayBuffer(); @@ -456,12 +469,7 @@ async function pollKieTranscriptionResult(baseUrl, modelId, taskId, token) { }); if (state === "success") { - const text = - data?.data?.response?.text || - data?.data?.resultText || - data?.data?.text || - data?.text || - ""; + const text = normalizeKieTranscriptionText(data); return Response.json({ text }, { headers: { ...CORS_HEADERS } }); } } catch (err: unknown) { diff --git a/tests/unit/kie-audio-transcription-result.test.ts b/tests/unit/kie-audio-transcription-result.test.ts new file mode 100644 index 00000000000..5880f2c6b05 --- /dev/null +++ b/tests/unit/kie-audio-transcription-result.test.ts @@ -0,0 +1,77 @@ +import test from "node:test"; +import assert from "node:assert/strict"; + +const { handleAudioTranscription } = await import("../../open-sse/handlers/audioTranscription.ts"); + +function buildFormData(): FormData { + const formData = new FormData(); + formData.append("model", "kie/elevenlabs/speech-to-text"); + formData.append("file", new File([Buffer.from("audio")], "clip.wav", { type: "audio/wav" })); + return formData; +} + +test("Kie transcription returns text from every supported result envelope", async () => { + const originalFetch = globalThis.fetch; + const cases = [ + { + name: "nested response", + record: { data: { status: "SUCCESS", response: { text: "response text" } } }, + expected: "response text", + }, + { + name: "resultText fallback", + record: { + data: { + status: "SUCCESS", + response: { text: { malformed: true } }, + resultText: "result text", + }, + }, + expected: "result text", + }, + { + name: "nested text fallback", + record: { data: { status: "SUCCESS", text: "nested text" } }, + expected: "nested text", + }, + { + name: "top-level text fallback", + record: { data: { status: "SUCCESS" }, text: "top-level text" }, + expected: "top-level text", + }, + ]; + + try { + for (const testCase of cases) { + const calls: string[] = []; + globalThis.fetch = async (url, options = {}) => { + const requestUrl = String(url); + calls.push(requestUrl); + + if (requestUrl === "https://api.kie.ai/api/v1/jobs/createTask") { + const payload = JSON.parse(String(options.body)); + assert.equal(payload.model, "elevenlabs/speech-to-text", testCase.name); + return Response.json({ data: { taskId: "task-1" } }); + } + + assert.equal( + requestUrl, + "https://api.kie.ai/api/v1/jobs/recordInfo?taskId=task-1", + testCase.name + ); + return Response.json(testCase.record); + }; + + const response = await handleAudioTranscription({ + formData: buildFormData(), + credentials: { apiKey: "kie-key" }, + }); + + assert.equal(response.status, 200, testCase.name); + assert.deepEqual(await response.json(), { text: testCase.expected }, testCase.name); + assert.equal(calls.length, 2, testCase.name); + } + } finally { + globalThis.fetch = originalFetch; + } +});