changed onboarding content to use default messages instead of onboarding steps, and to use the new hook to send messages
This commit is contained in:
parent
d336baba32
commit
1aac17aa3e
1 changed files with 71 additions and 79 deletions
|
|
@ -1,9 +1,12 @@
|
||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { Loader2, User } from "lucide-react";
|
|
||||||
import { useState } from "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 Nudges from "@/app/chat/nudges";
|
||||||
|
import type { Message } from "@/app/chat/types";
|
||||||
import OnboardingCard from "@/app/onboarding/components/onboarding-card";
|
import OnboardingCard from "@/app/onboarding/components/onboarding-card";
|
||||||
|
import { useChatStreaming } from "@/hooks/useChatStreaming";
|
||||||
import { OnboardingStep } from "./onboarding-step";
|
import { OnboardingStep } from "./onboarding-step";
|
||||||
|
|
||||||
export function OnboardingContent({
|
export function OnboardingContent({
|
||||||
|
|
@ -15,59 +18,42 @@ export function OnboardingContent({
|
||||||
}) {
|
}) {
|
||||||
const [responseId, setResponseId] = useState<string | null>(null);
|
const [responseId, setResponseId] = useState<string | null>(null);
|
||||||
const [selectedNudge, setSelectedNudge] = useState<string>("");
|
const [selectedNudge, setSelectedNudge] = useState<string>("");
|
||||||
const [assistantResponse, setAssistantResponse] = useState<string>("");
|
const [assistantMessage, setAssistantMessage] = useState<Message | null>(
|
||||||
const [isLoadingResponse, setIsLoadingResponse] = useState<boolean>(false);
|
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 NUDGES = ["What is OpenRAG?"];
|
||||||
|
|
||||||
const handleNudgeClick = async (nudge: string) => {
|
const handleNudgeClick = async (nudge: string) => {
|
||||||
setSelectedNudge(nudge);
|
setSelectedNudge(nudge);
|
||||||
setIsLoadingResponse(true);
|
setAssistantMessage(null);
|
||||||
|
await sendMessage({
|
||||||
try {
|
prompt: nudge,
|
||||||
const requestBody: {
|
previousResponseId: responseId || undefined,
|
||||||
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);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Determine which message to show (streaming takes precedence)
|
||||||
|
const displayMessage = streamingMessage || assistantMessage;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="space-y-6">
|
<div className="space-y-6">
|
||||||
<OnboardingStep
|
<OnboardingStep
|
||||||
|
|
@ -81,44 +67,48 @@ export function OnboardingContent({
|
||||||
<OnboardingStep
|
<OnboardingStep
|
||||||
isVisible={currentStep >= 1}
|
isVisible={currentStep >= 1}
|
||||||
isCompleted={currentStep > 1 || !!selectedNudge}
|
isCompleted={currentStep > 1 || !!selectedNudge}
|
||||||
text="Excellent, let’s move on to learning the basics."
|
text="Excellent, let's move on to learning the basics."
|
||||||
>
|
>
|
||||||
<div className="py-2">
|
<div className="py-2">
|
||||||
<Nudges onboarding nudges={NUDGES} handleSuggestionClick={handleNudgeClick} />
|
<Nudges
|
||||||
|
onboarding
|
||||||
|
nudges={NUDGES}
|
||||||
|
handleSuggestionClick={handleNudgeClick}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</OnboardingStep>
|
</OnboardingStep>
|
||||||
|
|
||||||
<OnboardingStep
|
{/* User message - show when nudge is selected */}
|
||||||
isVisible={currentStep >= 1 && !!selectedNudge}
|
{currentStep >= 1 && !!selectedNudge && (
|
||||||
isCompleted={currentStep > 1}
|
<div className={currentStep > 1 ? "opacity-50" : ""}>
|
||||||
text={selectedNudge}
|
<UserMessage content={selectedNudge} />
|
||||||
icon={
|
</div>
|
||||||
<div className="w-8 h-8 rounded-lg bg-primary/10 flex items-center justify-center flex-shrink-0 select-none">
|
)}
|
||||||
<User className="h-4 w-4 text-primary" />
|
|
||||||
</div>
|
|
||||||
}
|
|
||||||
>
|
|
||||||
</OnboardingStep>
|
|
||||||
|
|
||||||
<OnboardingStep
|
{/* Assistant message - show streaming or final message */}
|
||||||
isVisible={currentStep >= 1 && !!selectedNudge}
|
{currentStep >= 1 && !!selectedNudge && (displayMessage || isLoading) && (
|
||||||
isCompleted={currentStep > 1}
|
<div className={currentStep > 1 ? "opacity-50" : ""}>
|
||||||
text={isLoadingResponse ? "Thinking..." : assistantResponse}
|
<AssistantMessage
|
||||||
isMarkdown={!isLoadingResponse && !!assistantResponse}
|
content={displayMessage?.content || ""}
|
||||||
>
|
functionCalls={displayMessage?.functionCalls}
|
||||||
{isLoadingResponse ? (
|
messageIndex={0}
|
||||||
<div className="flex items-center gap-2">
|
expandedFunctionCalls={new Set()}
|
||||||
<Loader2 className="w-4 h-4 animate-spin text-muted-foreground" />
|
onToggle={() => {}}
|
||||||
</div>
|
isStreaming={!!streamingMessage}
|
||||||
) : (
|
/>
|
||||||
<button
|
{!isLoading && displayMessage && currentStep === 1 && (
|
||||||
onClick={handleStepComplete}
|
<div className="mt-4">
|
||||||
className="px-4 py-2 bg-primary text-primary-foreground rounded-lg hover:bg-primary/90"
|
<button
|
||||||
>
|
type="button"
|
||||||
Continue
|
onClick={handleStepComplete}
|
||||||
</button>
|
className="px-4 py-2 bg-primary text-primary-foreground rounded-lg hover:bg-primary/90"
|
||||||
)}
|
>
|
||||||
</OnboardingStep>
|
Continue
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
<OnboardingStep
|
<OnboardingStep
|
||||||
isVisible={currentStep >= 2}
|
isVisible={currentStep >= 2}
|
||||||
|
|
@ -130,6 +120,7 @@ export function OnboardingContent({
|
||||||
Choose and connect your preferred AI model provider.
|
Choose and connect your preferred AI model provider.
|
||||||
</p>
|
</p>
|
||||||
<button
|
<button
|
||||||
|
type="button"
|
||||||
onClick={handleStepComplete}
|
onClick={handleStepComplete}
|
||||||
className="px-4 py-2 bg-primary text-primary-foreground rounded-lg hover:bg-primary/90"
|
className="px-4 py-2 bg-primary text-primary-foreground rounded-lg hover:bg-primary/90"
|
||||||
>
|
>
|
||||||
|
|
@ -148,6 +139,7 @@ export function OnboardingContent({
|
||||||
Your account is ready to use. Let's start chatting!
|
Your account is ready to use. Let's start chatting!
|
||||||
</p>
|
</p>
|
||||||
<button
|
<button
|
||||||
|
type="button"
|
||||||
onClick={handleStepComplete}
|
onClick={handleStepComplete}
|
||||||
className="px-4 py-2 bg-primary text-primary-foreground rounded-lg hover:bg-primary/90"
|
className="px-4 py-2 bg-primary text-primary-foreground rounded-lg hover:bg-primary/90"
|
||||||
>
|
>
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue