mirror of
https://github.com/OpenSquawk/OpenSquawk
synced 2026-08-06 17:53:19 +08:00
webseite sieht gut aus
This commit is contained in:
@@ -1,35 +1,62 @@
|
||||
import { readMultipartFormData, createError } from "h3";
|
||||
import { openai, LLM_MODEL, TTS_MODEL, atcReplyPrompt } from "../../utils/openai";
|
||||
import { LLM_MODEL, TTS_MODEL, atcReplyPrompt, openai } from "../../utils/openai";
|
||||
import { applyRadioEffect } from "../../utils/radio";
|
||||
import { writeFile, rm } from "node:fs/promises";
|
||||
import { writeFile, rm, readFile } from "node:fs/promises";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { toFile } from "openai/uploads"; // ⟵ NEU
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const parts = await readMultipartFormData(event);
|
||||
const mode = parts?.find(p=>p.name==="mode")?.data?.toString("utf8") || "text";
|
||||
const audio = parts?.find(p=>p.type && p.data);
|
||||
if (!audio) throw createError({ statusCode:400, statusMessage:"No audio" });
|
||||
if (!parts) throw createError({ statusCode: 400, statusMessage: "No form-data" });
|
||||
|
||||
const file = new File([audio.data], audio.filename || "audio.webm", { type: audio.type || "audio/webm" });
|
||||
const mode = (parts.find(p => p.name === "mode")?.data?.toString("utf8") || "text").toLowerCase();
|
||||
const audio = parts.find(p => p.type && p.data);
|
||||
if (!audio) throw createError({ statusCode: 400, statusMessage: "No audio file" });
|
||||
|
||||
// ✅ saubere Datei-Erzeugung
|
||||
const file = await toFile(
|
||||
audio.data,
|
||||
audio.filename || "ptt.webm",
|
||||
{ type: audio.type || "audio/webm" }
|
||||
);
|
||||
|
||||
// 1) Transkribieren
|
||||
const tr = await openai.audio.transcriptions.create({ model: "whisper-1", file });
|
||||
const pilotText = tr.text.trim();
|
||||
const pilotText = tr.text?.trim() || "";
|
||||
if (!pilotText) throw createError({ statusCode: 400, statusMessage: "Empty transcription" });
|
||||
|
||||
const resp = await openai.responses.create({ model: LLM_MODEL, input: atcReplyPrompt(pilotText) });
|
||||
// 2) ATC-Antwort
|
||||
const resp = await openai.responses.create({
|
||||
model: LLM_MODEL,
|
||||
input: atcReplyPrompt(pilotText),
|
||||
});
|
||||
const replyText = resp.output_text?.trim() || "";
|
||||
if (!replyText) throw createError({ statusCode: 500, statusMessage: "LLM empty" });
|
||||
|
||||
// 3) Optional TTS + Funk
|
||||
if (mode === "tts") {
|
||||
const clean = join(tmpdir(), `tts-${randomUUID()}.wav`);
|
||||
const radio = join(tmpdir(), `radio-${randomUUID()}.wav`);
|
||||
const tts = await openai.audio.speech.create({ model: TTS_MODEL, voice: "alloy", format: "wav", input: replyText });
|
||||
|
||||
const tts = await openai.audio.speech.create({
|
||||
model: TTS_MODEL,
|
||||
voice: "alloy",
|
||||
format: "wav",
|
||||
input: replyText,
|
||||
});
|
||||
|
||||
await writeFile(clean, Buffer.from(await tts.arrayBuffer()));
|
||||
await applyRadioEffect(clean, radio);
|
||||
const data = await import("node:fs/promises").then(fs => fs.readFile(radio));
|
||||
await rm(clean).catch(()=>{});
|
||||
|
||||
const data = await readFile(radio);
|
||||
const b64 = Buffer.from(data).toString("base64");
|
||||
await rm(radio).catch(()=>{});
|
||||
return { pilotText, replyText, audio:{ mime:"audio/wav", base64:b64 } };
|
||||
rm(clean).catch(() => {});
|
||||
rm(radio).catch(() => {});
|
||||
|
||||
return { pilotText, replyText, audio: { mime: "audio/wav", base64: b64 } };
|
||||
}
|
||||
|
||||
return { pilotText, replyText };
|
||||
});
|
||||
|
||||
@@ -1,10 +1,24 @@
|
||||
import { readMultipartFormData, createError } from "h3";
|
||||
import { openai } from "../../utils/openai";
|
||||
import { toFile } from "openai/uploads"; // ⟵ NEU
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const parts = await readMultipartFormData(event);
|
||||
const audio = parts?.find(p=>p.type && p.data);
|
||||
if (!audio) throw createError({ statusCode:400, statusMessage:"No audio" });
|
||||
const file = new File([audio.data], audio.filename || "audio.webm", { type: audio.type || "audio/webm" });
|
||||
const tr = await openai.audio.transcriptions.create({ model: "whisper-1", file });
|
||||
if (!parts) throw createError({ statusCode: 400, statusMessage: "No form-data" });
|
||||
|
||||
const audio = parts.find(p => p.type && p.data);
|
||||
if (!audio) throw createError({ statusCode: 400, statusMessage: "No audio file" });
|
||||
|
||||
const file = await toFile(
|
||||
audio.data, // Buffer
|
||||
audio.filename || "ptt.webm", // ⟵ mit Endung
|
||||
{ type: audio.type || "audio/webm" } // ⟵ korrekter MIME
|
||||
);
|
||||
|
||||
const tr = await openai.audio.transcriptions.create({
|
||||
model: "whisper-1",
|
||||
file
|
||||
});
|
||||
|
||||
return { text: tr.text };
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user