// voice_speak adapter — synthesize text to audio via OpenAI or ElevenLabs.
//
// The audio is returned as a base64 string in the tool result so the caller
// (UI / agent) can play or persist it. Setting save_to_disk=true with a
// project_id additionally writes a Disk artifact.
//
// Egress logging is performed inside lib/tts.js. The adapter never inspects
// the API key.

import { synthesizeSpeech } from '../../tts.js';

export async function voice_speak(args = {}, opts = {}) {
  const text = String(args.text || '').trim();
  if (!text) throw new Error('text is required');

  const result = await synthesizeSpeech({
    text,
    provider: args.provider,
    voice: args.voice,
    model: args.model,
    format: args.format,
    save_to_disk: Boolean(args.save_to_disk),
    project_id: args.project_id,
    agent_id: opts.agent_id ?? null,
  });

  return {
    ok: true,
    status: 'complete',
    provider: result.provider,
    voice: result.voice,
    model: result.model,
    mime_type: result.mime_type,
    extension: result.extension,
    audio_base64: result.audio_buffer.toString('base64'),
    duration_estimate_ms: result.duration_estimate_ms,
    disk_file_id: result.disk_file?.id || null,
  };
}
