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

Function handleSubmit

examples/nextjs/components/pipe-stream.tsx:13–66  ·  view source on GitHub ↗
(e: React.FormEvent)

Source from the content-addressed store, hash-verified

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