|
| 1 | +'use client'; |
| 2 | +import { Button } from '@/components/ui/button'; |
| 3 | +import { Input } from '@/components/ui/input'; |
| 4 | +import { Message } from 'langbase'; |
| 5 | +import { usePipe } from 'langbase/react'; |
| 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: '/langbase/pipe/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' }, |
| 66 | + body: { customOption: 'regenerateValue' }, |
| 67 | + }); |
| 68 | + }; |
| 69 | + |
| 70 | + const handleCustomMessage = () => { |
| 71 | + sendMessage('This is a custom message', { |
| 72 | + data: { context: 'custom context' }, |
| 73 | + allowEmptySubmit: false, |
| 74 | + }); |
| 75 | + }; |
| 76 | + |
| 77 | + return ( |
| 78 | + <div className="flex flex-col h-[80vh] max-w-2xl mx-auto p-4 w-full"> |
| 79 | + <div className="mb-4"> |
| 80 | + <strong>Thread ID:</strong> {threadId || 'Not available'} |
| 81 | + </div> |
| 82 | + <div className="flex-1 overflow-y-auto mb-4 space-y-4 pb-32 w-full"> |
| 83 | + {messages.map((m, index) => ( |
| 84 | + <div |
| 85 | + key={index} |
| 86 | + className={`p-2 rounded ${m.role === 'user' ? 'bg-indigo-200' : 'bg-gray-100' |
| 87 | + }`} |
| 88 | + > |
| 89 | + <strong>{m.role === 'user' ? 'You: ' : 'AI: '}</strong> |
| 90 | + {m.content} |
| 91 | + </div> |
| 92 | + ))} |
| 93 | + </div> |
| 94 | + |
| 95 | + <div className="fixed bottom-0 left-0 right-0 bg-white p-4 max-w-2xl mx-auto"> |
| 96 | + {isLoading && ( |
| 97 | + <div className="text-center text-gray-500 mb-8"> |
| 98 | + AI is thinking... |
| 99 | + </div> |
| 100 | + )} |
| 101 | + |
| 102 | + {error && ( |
| 103 | + <div className="text-center text-red-500 mb-2"> |
| 104 | + Error: {error.message} |
| 105 | + </div> |
| 106 | + )} |
| 107 | + |
| 108 | + <div className="flex space-x-2 mb-2"> |
| 109 | + <Button |
| 110 | + onClick={handleRegenerateWithOptions} |
| 111 | + disabled={isLoading || messages.length === 0} |
| 112 | + size="sm" |
| 113 | + > |
| 114 | + Regenerate |
| 115 | + </Button> |
| 116 | + <Button onClick={stop} disabled={!isLoading} size="sm"> |
| 117 | + Stop |
| 118 | + </Button> |
| 119 | + <Button |
| 120 | + onClick={handleClearChat} |
| 121 | + variant="outline" |
| 122 | + size="sm" |
| 123 | + > |
| 124 | + Clear Chat |
| 125 | + </Button> |
| 126 | + <Button |
| 127 | + onClick={handleCustomMessage} |
| 128 | + variant="outline" |
| 129 | + size="sm" |
| 130 | + > |
| 131 | + Send Custom |
| 132 | + </Button> |
| 133 | + </div> |
| 134 | + |
| 135 | + <form |
| 136 | + onSubmit={e => handleSubmit(e, { allowEmptySubmit: true })} |
| 137 | + className="flex space-x-2" |
| 138 | + > |
| 139 | + <Input |
| 140 | + className="flex-1" |
| 141 | + value={input} |
| 142 | + onChange={handleInputChange} |
| 143 | + placeholder="Type your message..." |
| 144 | + disabled={isLoading} |
| 145 | + autoFocus |
| 146 | + /> |
| 147 | + <Button type="submit">Send</Button> |
| 148 | + </form> |
| 149 | + </div> |
| 150 | + </div> |
| 151 | + ); |
| 152 | +}; |
| 153 | + |
| 154 | +export default ChatAdvanced; |
0 commit comments