import { serve } from "https://deno.land/std/http/server.ts"; import { EdgeSpeechTTS } from "https://esm.sh/@lobehub/tts@1"; const VOICES_URL = "https://speech.platform.bing.com/consumer/speech/synthesize/readaloud/voices/list?trustedclienttoken=6A5AA1D4EAFF4E9FB37E23D68491D6F4"; async function fetchVoiceList() { const response = await fetch(VOICES_URL); const voices = await response.json(); return voices.reduce((acc: Record, voice: any) => { const { ShortName: model, ShortName: name, FriendlyName: friendlyName, Locale: locale } = voice; if (!acc[locale]) acc[locale] = []; acc[locale].push({ model, name, friendlyName, locale }); return acc; }, {}); } async function handleDemoRequest(req: Request) { const html = ` tts
-0.1
0.1

Результат

`; return new Response(html, { headers: { "Content-Type": "text/html" }, }); } function validateContentType(req: Request, expected: string) { const contentType = req.headers.get("Content-Type"); if (contentType !== expected) { console.log(`Invalid Content-Type ${contentType}, expected ${expected}`); return new Response("Bad Request", { status: 400 }); } } async function handleDebugRequest(req: Request) { const url = new URL(req.url); const voice = url.searchParams.get("voice") || ""; const model = url.searchParams.get("model") || ""; const text = url.searchParams.get("text") || ""; console.log(`Debug request with model=${model}, voice=${voice}, text=${text}`); if (!voice || !model || !text) { console.log("Missing required parameters"); return new Response("Bad Request", { status: 400 }); } return synthesizeSpeech(model, voice, text); } async function handleSynthesisRequest(req: Request) { if (req.method !== "POST") { console.log(`Invalid method ${req.method}, expected POST`); return new Response("Method Not Allowed", { status: 405 }); } const invalidContentType = validateContentType(req, "application/json"); if (invalidContentType) return invalidContentType; const { model, input, voice } = await req.json(); console.log(`Synthesis request with model=${model}, input=${input}, voice=${voice}`); return synthesizeSpeech(model, voice, input); } async function handleDemoRequest(req: Request) { const groupedVoiceList = await fetchVoiceList(); const html = ` tts
-0.1
0.1

голос

`; return new Response(html, { headers: { "Content-Type": "text/html" }, }); } serve(async (req) => { try { const url = new URL(req.url); if (url.pathname === "/") { return handleDemoRequest(req); } if (url.pathname === "/tts") { return handleDebugRequest(req); } if (url.pathname !== "/v1/audio/speech") { console.log(`Unhandled path ${url.pathname}`); return new Response("Not Found", { status: 404 }); } return handleSynthesisRequest(req); } catch (err) { console.error(`Error processing request: ${err.message}`); return new Response(`Internal Server Error\n${err.message}`, { status: 500, }); } });