import { GoogleGenAI } from "@google/genai";
import * as fs from "node:fs";

const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

const script =
  "Joe: Hey Jane, ready for the demo?\n" +
  "Jane: Absolutely. Lets make it punchy and clear.";

const response = await ai.models.generateContent({
  model: "gemini-2.5-flash-preview-tts",
  contents: [{ parts: [{ text: script }] }],
  config: {
    responseModalities: ["AUDIO"],
    speechConfig: {
      multiSpeakerVoiceConfig: {
        speakerVoiceConfigs: [
          {
            speaker: "Joe",
            voiceConfig: {
              prebuiltVoiceConfig: { voiceName: "Charon" },
            },
          },
          {
            speaker: "Jane",
            voiceConfig: {
              prebuiltVoiceConfig: { voiceName: "Kore" },
            },
          },
        ],
      },
    },
  },
});

const data =
  response.candidates?.[0]?.content?.parts?.[0]?.inlineData?.data ?? "";
if (!data) {
  throw new Error("No audio returned");
}

fs.writeFileSync("dialogue.pcm", Buffer.from(data, "base64"));
console.log("Saved dialogue.pcm (24kHz, 16-bit, mono)");
