-
Notifications
You must be signed in to change notification settings - Fork 450
Expand file tree
/
Copy pathonboarding-content.tsx
More file actions
307 lines (281 loc) · 10.1 KB
/
Copy pathonboarding-content.tsx
File metadata and controls
307 lines (281 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
"use client";
import { useEffect, useRef, useState } from "react";
import { StickToBottom } from "use-stick-to-bottom";
import { useUpdateOnboardingStateMutation } from "@/app/api/mutations/useUpdateOnboardingStateMutation";
import { getFilterById } from "@/app/api/queries/useGetFilterByIdQuery";
import { useGetSettingsQuery } from "@/app/api/queries/useGetSettingsQuery";
import { AssistantMessage } from "@/app/chat/_components/assistant-message";
import Nudges from "@/app/chat/_components/nudges";
import { UserMessage } from "@/app/chat/_components/user-message";
import type { Message } from "@/app/chat/_types/types";
import OnboardingCard from "@/app/onboarding/_components/onboarding-card";
import { useChat } from "@/contexts/chat-context";
import { useChatStreaming } from "@/hooks/useChatStreaming";
import { trackButton, trackLLMCall } from "@/lib/analytics";
import type { FilterInput } from "@/lib/filter-normalization";
import { buildSearchPayloadFilters } from "@/lib/filter-normalization";
import { OnboardingStep } from "./onboarding-step";
import OnboardingUpload from "./onboarding-upload";
// Filters for OpenRAG documentation
const OPENRAG_DOCS_FILTERS: FilterInput = {
data_sources: [],
document_types: [],
owners: [],
connector_types: ["openrag_docs"],
};
export function OnboardingContent({
handleStepComplete,
handleStepBack,
currentStep,
}: {
handleStepComplete: () => void;
handleStepBack: () => void;
currentStep: number;
}) {
const { setConversationFilter, setCurrentConversationId } = useChat();
const { data: settings } = useGetSettingsQuery();
const updateOnboardingMutation = useUpdateOnboardingStateMutation();
const parseFailedRef = useRef(false);
const [responseId, setResponseId] = useState<string | null>(null);
// Initialize from backend settings
const [selectedNudge, setSelectedNudge] = useState<string>(() => {
return settings?.onboarding?.selected_nudge || "";
});
const [assistantMessage, setAssistantMessage] = useState<Message | null>(
() => {
// Get from backend settings
if (settings?.onboarding?.assistant_message) {
const msg = settings.onboarding.assistant_message;
return {
role: msg.role as "user" | "assistant",
content: msg.content,
timestamp: new Date(msg.timestamp),
};
}
return null;
},
);
// Sync state when settings change
useEffect(() => {
if (settings?.onboarding?.selected_nudge) {
setSelectedNudge(settings.onboarding.selected_nudge);
}
if (settings?.onboarding?.assistant_message) {
const msg = settings.onboarding.assistant_message;
setAssistantMessage({
role: msg.role as "user" | "assistant",
content: msg.content,
timestamp: new Date(msg.timestamp),
});
}
}, [settings?.onboarding]);
// Handle parse errors by going back a step
useEffect(() => {
if (parseFailedRef.current && currentStep >= 2) {
handleStepBack();
}
}, [handleStepBack, currentStep]);
const { streamingMessage, isLoading, sendMessage } = useChatStreaming({
onComplete: async (message, newResponseId) => {
trackLLMCall({
mode: "onboarding",
model: settings?.agent?.llm_model,
inputTokens: message.usage?.input_tokens,
outputTokens: message.usage?.output_tokens,
});
setAssistantMessage(message);
// Save assistant message to backend
await updateOnboardingMutation.mutateAsync({
assistant_message: {
role: message.role,
content: message.content,
timestamp: message.timestamp.toISOString(),
},
});
if (newResponseId) {
setResponseId(newResponseId);
// Set the current conversation ID
setCurrentConversationId(newResponseId);
// Get filter ID from backend settings
const openragDocsFilterId =
settings?.onboarding?.openrag_docs_filter_id;
if (openragDocsFilterId) {
try {
// Load the filter and set it in the context with explicit responseId
// This ensures the filter is saved to localStorage with the correct conversation ID
const filter = await getFilterById(openragDocsFilterId);
if (filter) {
// Pass explicit newResponseId to ensure correct localStorage association
setConversationFilter(filter, newResponseId);
console.log(
"[ONBOARDING] Saved filter association:",
`conversation_filter_${newResponseId}`,
"=",
openragDocsFilterId,
);
}
} catch (error) {
console.error(
"Failed to associate filter with conversation:",
error,
);
}
}
}
},
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) => {
trackButton({
CTA: `Learn Basics - ${nudge}`,
elementId: "onboarding-nudge",
namespace: "onboarding",
});
setSelectedNudge(nudge);
setAssistantMessage(null);
// Save selected nudge to backend and clear assistant message
await updateOnboardingMutation.mutateAsync({
selected_nudge: nudge,
assistant_message: null,
});
setTimeout(async () => {
// Check if we have the OpenRAG docs filter ID (sample data was ingested)
const openragDocsFilterId = settings?.onboarding?.openrag_docs_filter_id;
// Load and set the OpenRAG docs filter if available
let filterToUse = null;
console.log("[ONBOARDING] openragDocsFilterId:", openragDocsFilterId);
if (openragDocsFilterId) {
try {
const filter = await getFilterById(openragDocsFilterId);
console.log("[ONBOARDING] Loaded filter:", filter);
if (filter) {
// Pass null to skip localStorage save - no conversation exists yet
setConversationFilter(filter, null);
filterToUse = filter;
}
} catch (error) {
console.error("Failed to load OpenRAG docs filter:", error);
}
}
console.log(
"[ONBOARDING] Sending message with filter_id:",
filterToUse?.id,
);
await sendMessage({
prompt: nudge,
previousResponseId: responseId || undefined,
// Send both filter_id and filters (selections)
filter_id: filterToUse?.id,
filters: openragDocsFilterId
? buildSearchPayloadFilters(OPENRAG_DOCS_FILTERS)
: undefined,
});
}, 1500);
};
// Determine which message to show (streaming takes precedence)
const displayMessage = streamingMessage || assistantMessage;
useEffect(() => {
if (currentStep === 2 && !isLoading && !!displayMessage) {
handleStepComplete();
}
}, [isLoading, displayMessage, handleStepComplete, currentStep]);
return (
<StickToBottom
className="flex h-full flex-1 flex-col [&>div]:scrollbar-hide"
resize="smooth"
initial="instant"
mass={1}
>
<StickToBottom.Content className="flex flex-col min-h-full overflow-x-hidden px-8 py-6">
<div
className="flex flex-col place-self-center w-full space-y-6"
data-testid="onboarding-content"
>
{/* Step 1 - LLM Provider */}
<OnboardingStep
isVisible={currentStep >= 0}
isCompleted={currentStep > 0}
showCompleted={true}
text="Let's get started by setting up your LLM provider."
>
<OnboardingCard
onComplete={() => {
handleStepComplete();
}}
isCompleted={currentStep > 0}
/>
</OnboardingStep>
{/* Step 2 - Embedding provider and ingestion */}
<OnboardingStep
isVisible={currentStep >= 1}
isCompleted={currentStep > 1}
showCompleted={true}
text="Now, let's set up your embedding provider."
>
<OnboardingCard
isEmbedding={true}
onComplete={() => {
handleStepComplete();
}}
isCompleted={currentStep > 1}
/>
</OnboardingStep>
{/* Step 3 */}
<OnboardingStep
isVisible={currentStep >= 2}
isCompleted={currentStep > 2 || !!selectedNudge}
text="Excellent, let's move on to learning the basics."
>
<div className="py-2">
<Nudges
onboarding
nudges={NUDGES}
handleSuggestionClick={handleNudgeClick}
/>
</div>
</OnboardingStep>
{/* User message - show when nudge is selected */}
{currentStep >= 2 && !!selectedNudge && (
<UserMessage
content={selectedNudge}
isCompleted={currentStep > 3}
/>
)}
{/* Assistant message - show streaming or final message */}
{currentStep >= 2 &&
!!selectedNudge &&
(displayMessage || isLoading) && (
<AssistantMessage
content={displayMessage?.content || ""}
functionCalls={displayMessage?.functionCalls}
messageIndex={0}
expandedFunctionCalls={new Set()}
onToggle={() => {}}
isStreaming={!!streamingMessage}
isCompleted={currentStep > 3}
showFeedback={false}
/>
)}
{/* Step 4 */}
<OnboardingStep
isVisible={currentStep >= 3 && !isLoading && !!displayMessage}
isCompleted={currentStep > 3}
text="Lastly, let's add your data."
hideIcon={true}
>
<OnboardingUpload onComplete={handleStepComplete} />
</OnboardingStep>
</div>
</StickToBottom.Content>
</StickToBottom>
);
}