Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { ELEVEN_LABS_INTERNAL_API_BASE_URL } from "../constants";
import { Response, fetch as undiciFetch } from "undici";
import { RequestType } from "./request";
import { requestHeaders } from "@/requestHeaders";
import { proxyAgentPool } from "@/lib/proxyAgentPool";
import { NoThrow } from "@/utils/NoThrow";
import { ResponseSchema } from "./response";
export async function enhanceDialogue(req: RequestType) {
const url = `${ELEVEN_LABS_INTERNAL_API_BASE_URL}/enhance-dialogue`;
let response: Response;
try {
response = await undiciFetch(url, {
method: "POST",
body: JSON.stringify({ dialogue_blocks: req.dialogueBlocks }),
headers: {
...requestHeaders,
"Content-Type": "application/json",
"Cache-Control": "no-cache",
Authorization: `Bearer ${req.bearerToken}`,
},
dispatcher: proxyAgentPool.get(req.proxyURL),
});
} catch (error) {
if (error instanceof TypeError) {
return NoThrow.error(error);
}
if (error instanceof SyntaxError) {
return NoThrow.error(error);
}
if (error instanceof DOMException) {
return NoThrow.error(error);
}
return NoThrow.error(new Error("Unknown error"));
}
let rawData: unknown;
try {
rawData = await response.json();
} catch (error) {
if (error instanceof TypeError) {
return NoThrow.error(error);
}
if (error instanceof SyntaxError) {
return NoThrow.error(error);
}
if (error instanceof DOMException) {
return NoThrow.error(error);
}
return NoThrow.error(new Error("Unknown error"));
}
console.log("rawData:", JSON.stringify(rawData, null, 4));
const validatedDataResult = await ResponseSchema.safeParseAsync(rawData);
Iif (!validatedDataResult.success) {
return NoThrow.error(validatedDataResult.error);
}
return NoThrow.success(validatedDataResult.data);
}
|