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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | 1x 1x 1x 1x 1x 1x 1x 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 { RequestSchema, RequestType } from "./request";
import { requestHeaders } from "@/requestHeaders";
import { proxyAgentPool } from "@/lib/proxyAgentPool";
import { NoThrow } from "@/utils/NoThrow";
import { ResponseMetadataSchema } from "./response";
// TODO: handle errors properly
export async function textToDialogue(input: RequestType) {
const inputValidationResult = await RequestSchema.safeDecodeAsync(input);
Iif (!inputValidationResult.success) {
return NoThrow.error(inputValidationResult.error);
}
const validatedInput = inputValidationResult.data;
const url = `${ELEVEN_LABS_INTERNAL_API_BASE_URL}/text-to-dialogue`;
let response: Response;
const queryParams = new URLSearchParams();
queryParams.append("output_format", validatedInput.outputFormat);
try {
response = await undiciFetch(`${url}?${queryParams.toString()}`, {
method: "POST",
body: JSON.stringify({
inputs: validatedInput.inputs.map((input) => ({ text: input.text, voice_id: input.voiceID })),
model_id: validatedInput.modelID,
settings: {
stability: validatedInput.settings.stability,
},
}),
headers: {
...requestHeaders,
"content-type": "application/json",
"cache-control": "no-cache",
authorization: `Bearer ${validatedInput.bearerToken}`,
},
dispatcher: proxyAgentPool.get(validatedInput.proxyURL),
});
// console.log("response status:", response.status);
// const headers = Array.from(response.headers.entries());
// console.log("headers:", headers);
Iif (response.status !== 200) {
NoThrow.error(new Error("Response status !== 200"));
}
} 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"));
}
try {
const buffer = await response.arrayBuffer();
const headers = response.headers;
const metadataResult = await ResponseMetadataSchema.safeParseAsync({
generationInfo: headers.get("generation-info"),
historyItemID: headers.get("history-item-id"),
cost: headers.get("character-cost"),
requestID: headers.get("request-id"),
regenerationCount: headers.get("regeneration-count"),
});
Iif (!metadataResult.success) {
return NoThrow.error(metadataResult.error);
}
return NoThrow.success({
...metadataResult.data,
buffer,
});
} catch (error) {
return NoThrow.error(new Error("Unknown error"));
}
}
|