diff --git a/frontend/src/app/new-onboarding/components/onboarding-content.tsx b/frontend/src/app/new-onboarding/components/onboarding-content.tsx index 23508f0c..9c462389 100644 --- a/frontend/src/app/new-onboarding/components/onboarding-content.tsx +++ b/frontend/src/app/new-onboarding/components/onboarding-content.tsx @@ -1,9 +1,12 @@ "use client"; -import { Loader2, User } from "lucide-react"; import { useState } from "react"; +import { AssistantMessage } from "@/app/chat/components/assistant-message"; +import { UserMessage } from "@/app/chat/components/user-message"; import Nudges from "@/app/chat/nudges"; +import type { Message } from "@/app/chat/types"; import OnboardingCard from "@/app/onboarding/components/onboarding-card"; +import { useChatStreaming } from "@/hooks/useChatStreaming"; import { OnboardingStep } from "./onboarding-step"; export function OnboardingContent({ @@ -15,59 +18,42 @@ export function OnboardingContent({ }) { const [responseId, setResponseId] = useState(null); const [selectedNudge, setSelectedNudge] = useState(""); - const [assistantResponse, setAssistantResponse] = useState(""); - const [isLoadingResponse, setIsLoadingResponse] = useState(false); + const [assistantMessage, setAssistantMessage] = useState( + null, + ); + + const { streamingMessage, isLoading, sendMessage } = useChatStreaming({ + onComplete: (message, newResponseId) => { + setAssistantMessage(message); + if (newResponseId) { + setResponseId(newResponseId); + } + }, + onError: (error) => { + console.error("Chat error:", error); + setAssistantMessage({ + role: "assistant", + content: + "Sorry, I couldn't connect to the chat service. Please try again.", + timestamp: new Date(), + }); + }, + }); const NUDGES = ["What is OpenRAG?"]; const handleNudgeClick = async (nudge: string) => { setSelectedNudge(nudge); - setIsLoadingResponse(true); - - try { - const requestBody: { - prompt: string; - stream?: boolean; - previous_response_id?: string; - } = { - prompt: nudge, - stream: false, - }; - - if (responseId) { - requestBody.previous_response_id = responseId; - } - - const response = await fetch("/api/chat", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(requestBody), - }); - - const result = await response.json(); - - if (response.ok) { - setAssistantResponse(result.response); - if (result.response_id) { - setResponseId(result.response_id); - } - } else { - setAssistantResponse( - "Sorry, I encountered an error. Please try again.", - ); - } - } catch (error) { - console.error("Chat error:", error); - setAssistantResponse( - "Sorry, I couldn't connect to the chat service. Please try again.", - ); - } finally { - setIsLoadingResponse(false); - } + setAssistantMessage(null); + await sendMessage({ + prompt: nudge, + previousResponseId: responseId || undefined, + }); }; + // Determine which message to show (streaming takes precedence) + const displayMessage = streamingMessage || assistantMessage; + return (
= 1} isCompleted={currentStep > 1 || !!selectedNudge} - text="Excellent, let’s move on to learning the basics." + text="Excellent, let's move on to learning the basics." >
- +
- = 1 && !!selectedNudge} - isCompleted={currentStep > 1} - text={selectedNudge} - icon={ -
- -
- } - > -
+ {/* User message - show when nudge is selected */} + {currentStep >= 1 && !!selectedNudge && ( +
1 ? "opacity-50" : ""}> + +
+ )} - = 1 && !!selectedNudge} - isCompleted={currentStep > 1} - text={isLoadingResponse ? "Thinking..." : assistantResponse} - isMarkdown={!isLoadingResponse && !!assistantResponse} - > - {isLoadingResponse ? ( -
- -
- ) : ( - - )} -
+ {/* Assistant message - show streaming or final message */} + {currentStep >= 1 && !!selectedNudge && (displayMessage || isLoading) && ( +
1 ? "opacity-50" : ""}> + {}} + isStreaming={!!streamingMessage} + /> + {!isLoading && displayMessage && currentStep === 1 && ( +
+ +
+ )} +
+ )} = 2} @@ -130,6 +120,7 @@ export function OnboardingContent({ Choose and connect your preferred AI model provider.