MCPcopy Create free account
hub / github.com/CommandCodeAI/BaseAI / handleSubmit

Function handleSubmit

examples/nextjs/components/pipe-run-with-memory.tsx:15–68  ·  view source on GitHub ↗
(e: React.FormEvent)

Source from the content-addressed store, hash-verified

13 const [loading, setLoading] = useState(false);
14
15 const handleSubmit = async (e: React.FormEvent) => {
16 e.preventDefault();
17 if (!prompt.trim() || loading) return;
18
19 setLoading(true);
20 setCompletion('');
21
22 try {
23 const response = await fetch('/api/langbase/pipes/run-memory', {
24 method: 'POST',
25 headers: {'Content-Type': 'application/json'},
26 // Send prompt as an LLM message.
27 body: JSON.stringify({
28 messages: [{role: 'user', content: prompt}],
29 stream: true,
30 }),
31 });
32
33 if (response.body) {
34 // Convert the stream to a stream runner.
35 const runner = getRunner(response.body);
36
37 // Method 1: Using event listeners
38 runner.on('connect', () => {
39 console.log('Stream started.\n');
40 });
41
42 runner.on('content', content => {
43 setCompletion(prev => prev + content);
44 });
45
46 runner.on('end', () => {
47 console.log('\nStream ended.');
48 });
49
50 runner.on('error', error => {
51 console.error('Error:', error);
52 });
53
54 console.dir(await runner.finalChatCompletion(), {depth: null});
55
56 // Method #2 to get all of the chunk.
57 // for await (const chunk of runner) {
58 // const content = chunk?.choices[0]?.delta?.content;
59 // content && setCompletion(prev => prev + content);
60 // }
61 }
62 } catch (error) {
63 setLoading(false);
64 console.error('Error:', error);
65 } finally {
66 setLoading(false);
67 }
68 };
69
70 return (
71 <div className="bg-neutral-200 rounded-md p-2 flex flex-col gap-2 w-full">

Callers

nothing calls this directly

Calls 2

getRunnerFunction · 0.90
logMethod · 0.80

Tested by

no test coverage detected