()
| 6 | import React, { useCallback } from 'react'; |
| 7 | |
| 8 | const ChatAdvanced: React.FC = () => { |
| 9 | const handleResponse = useCallback( |
| 10 | (message: Message) => { |
| 11 | console.log( |
| 12 | 'Received response:', |
| 13 | message.content?.slice(0, 50) + '...', |
| 14 | ); |
| 15 | }, |
| 16 | [], |
| 17 | ); |
| 18 | |
| 19 | const handleFinish = useCallback( |
| 20 | (messages: Message[]) => { |
| 21 | console.log( |
| 22 | `Conversation finished. Total messages: ${messages.length}`, |
| 23 | ); |
| 24 | }, |
| 25 | [], |
| 26 | ); |
| 27 | |
| 28 | const handleError = useCallback((error: Error) => { |
| 29 | console.error('An error occurred:', error); |
| 30 | }, []); |
| 31 | |
| 32 | const { |
| 33 | messages, |
| 34 | input, |
| 35 | handleInputChange, |
| 36 | handleSubmit, |
| 37 | isLoading, |
| 38 | error, |
| 39 | regenerate, |
| 40 | stop, |
| 41 | setMessages, |
| 42 | threadId, |
| 43 | sendMessage, |
| 44 | } = usePipe({ |
| 45 | stream: true, |
| 46 | apiRoute: '/api/langbase/pipes/run-stream', |
| 47 | onResponse: handleResponse, |
| 48 | onFinish: handleFinish, |
| 49 | onError: handleError, |
| 50 | // initialMessages: [ |
| 51 | // {role: 'assistant', content: 'Hello! How can I help you?'}, |
| 52 | // {role: 'user', content: 'Who is an AI engineer?'}, |
| 53 | // ], // You can set initial messages here if needed |
| 54 | // You can set a threadId here if needed to change the thread manually, |
| 55 | // otherwise it will be generated automatically persistented in the browser. |
| 56 | // threadId: '', |
| 57 | }); |
| 58 | |
| 59 | const handleClearChat = () => { |
| 60 | setMessages([]); |
| 61 | }; |
| 62 | |
| 63 | const handleRegenerateWithOptions = () => { |
| 64 | regenerate({ |
| 65 | headers: { 'Custom-Header': 'Regenerate' }, |
nothing calls this directly
no test coverage detected