| 10 | const [loading, setLoading] = useState(false); |
| 11 | |
| 12 | const handleSubmit = async (e: React.FormEvent) => { |
| 13 | e.preventDefault(); |
| 14 | if (!prompt.trim() || loading) return; |
| 15 | |
| 16 | setLoading(true); |
| 17 | setCompletion(''); |
| 18 | |
| 19 | try { |
| 20 | console.log('Prompt:', prompt); |
| 21 | const response = await fetch('/api/langbase/pipes/run-memory', { |
| 22 | method: 'POST', |
| 23 | headers: { 'Content-Type': 'application/json' }, |
| 24 | body: JSON.stringify({ |
| 25 | messages: [{ role: 'user', content: prompt }], |
| 26 | stream: true, |
| 27 | }), |
| 28 | }); |
| 29 | |
| 30 | if (response.body) { |
| 31 | const reader = response.body.getReader(); |
| 32 | const decoder = new TextDecoder(); |
| 33 | let buffer = ''; |
| 34 | |
| 35 | while (true) { |
| 36 | const { done, value } = await reader.read(); |
| 37 | if (done) break; |
| 38 | |
| 39 | buffer += decoder.decode(value, { stream: true }); |
| 40 | const lines = buffer.split('\n'); |
| 41 | buffer = lines.pop() || ''; |
| 42 | |
| 43 | for (const line of lines) { |
| 44 | try { |
| 45 | const data = JSON.parse(line); |
| 46 | const content = data.choices[0]?.delta?.content; |
| 47 | if (content) { |
| 48 | setCompletion(prev => prev + content); |
| 49 | } |
| 50 | } catch (error) { |
| 51 | console.error('Error parsing JSON:', error); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } catch (error) { |
| 57 | console.error('Error:', error); |
| 58 | } finally { |
| 59 | setLoading(false); |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | return ( |
| 64 | <div className="bg-neutral-200 rounded-md p-2 flex flex-col gap-2 w-full"> |