import { useEffect, useState } from 'react'; import { CONFIG } from '../config'; import { getPromptGeneratePodcastScript } from '../utils/prompts'; import { getSSEStreamAsync } from '../utils/utils'; interface SplitContent { thought: string; codeBlock: string; } const getFromTo = (content: string, from: string, to: string): string => { const firstSplit = content.split(from, 2); if (firstSplit[1] !== undefined) { const secondSplit = firstSplit[1].split(to, 1); return secondSplit[0]; } else { return ''; } }; const splitContent = (content: string): SplitContent => { return { thought: getFromTo(content, '', '').trim(), codeBlock: getFromTo(content, '```yaml', '```').trim(), }; }; export const ScriptMaker = ({ setScript, setBusy, busy, }: { setScript: (script: string) => void; setBusy: (busy: boolean) => void; busy: boolean; }) => { const [input, setInput] = useState(''); const [note, setNote] = useState(''); const [thought, setThought] = useState(''); const [isGenerating, setIsGenerating] = useState(false); useEffect(() => { setBusy(isGenerating); }, [isGenerating]); const generate = async () => { setIsGenerating(true); setThought(''); try { let responseContent = ''; const fetchResponse = await fetch(CONFIG.llmEndpoint, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ messages: [ { role: 'user', content: getPromptGeneratePodcastScript(input, note), }, ], temperature: 0.3, stream: true, }), }); if (fetchResponse.status !== 200) { const body = await fetchResponse.json(); throw new Error(body?.error?.message || 'Unknown error'); } const chunks = getSSEStreamAsync(fetchResponse); for await (const chunk of chunks) { // const stop = chunk.stop; if (chunk.error) { throw new Error(chunk.error?.message || 'Unknown error'); } const addedContent = chunk.choices[0].delta.content; responseContent += addedContent; const { thought, codeBlock } = splitContent(responseContent); setThought(thought); if (codeBlock.length > 0) { setScript(codeBlock); } } } catch (error) { console.error(error); alert('Failed to generate the script. Please try again.'); } setIsGenerating(false); }; return ( Step 1: Input information setInput(e.target.value)} disabled={isGenerating || busy} > setNote(e.target.value)} disabled={isGenerating || busy} > {thought.length > 0 && ( <> Thought process: > )} {isGenerating ? ( <> Generating... > ) : ( 'Generate script' )} ); };
Thought process: