mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-11-16 17:09:09 +00:00
fix(types): normalize Kie transcription results (#8824)
This commit is contained in:
@@ -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) {
|
||||
|
||||
77
tests/unit/kie-audio-transcription-result.test.ts
Normal file
77
tests/unit/kie-audio-transcription-result.test.ts
Normal file
@@ -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;
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user