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

Function PipeRunMemory

examples/nextjs/components/pipe-run-with-memory.tsx:8–99  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

6import {useState} from 'react';
7
8export default function PipeRunMemory() {
9 const [prompt, setPrompt] = useState(
10 'Who founded Langbase, when, and why?',
11 );
12 const [completion, setCompletion] = useState('');
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 {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected