import { useState } from 'react' import type { PromptStructure } from './StructuredPromptEditor' interface Props { /** Current prompt text. Empty = generate mode, non-empty = complete mode. */ prompt: string /** Called with the Maestro result — the generated or completed prompt. */ onResult: (result: string, mode: 'generate' | 'complete') => void /** Called when Maestro encounters an error. */ onError: (message: string) => void /** Optional CSS class for the button container. */ className?: string /** * If true, send structured=true and return structured fields. * The onStructuredResult callback will be called instead of onResult. */ structured?: boolean /** Called with structured fields when structured mode returns. */ onStructuredResult?: (fields: PromptStructure, mode: 'generate' | 'complete') => void } function BouncingNote() { return ( ) } export default function MaestroButton({ prompt, onResult, onError, className = '', structured = false, onStructuredResult, }: Props) { const [isMaestroing, setIsMaestroing] = useState(false) const handleMaestro = async () => { setIsMaestroing(true) try { const res = await fetch('/api/text-gen/maestro', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ hint: prompt.trim(), structured, }), }) if (!res.ok) { try { const errorData = await res.json() throw new Error(errorData.detail || 'Maestro generation failed') } catch (parseErr) { if (parseErr instanceof Error && parseErr.message !== 'Maestro generation failed') throw parseErr throw new Error('Maestro generation failed') } } const data = await res.json() if (structured && data.fields && onStructuredResult) { onStructuredResult(data.fields as PromptStructure, data.mode) } else { onResult(data.prompt, data.mode) } } catch (err) { onError(err instanceof Error ? err.message : 'Maestro generation failed') } finally { setIsMaestroing(false) } } const mode = prompt.trim() ? 'complete' : 'generate' const label = mode === 'generate' ? '♪ Maestro Mode' : '♪ Keep Composing' return ( ) }