Skip to content

OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.

Python

Fix docs refresh and filter payload normalization.

Olfa Maslah3d0b9c882df0
diff --git a/frontend/app/api/mutations/useRefreshOpenragDocs.ts b/frontend/app/api/mutations/useRefreshOpenragDocs.ts
index 14b6289a2..12cb0b04a 100644
--- a/frontend/app/api/mutations/useRefreshOpenragDocs.ts
+++ b/frontend/app/api/mutations/useRefreshOpenragDocs.ts
@@ -17,7 +17,9 @@ const refreshOpenragDocs = async (): Promise<RefreshOpenRAGDocsResponse> => {
if (!response.ok) {
const error = await response.json();
- throw new Error(error.detail || error.error || "Failed to refresh OpenRAG docs");
+ throw new Error(
+ error.detail || error.error || "Failed to refresh OpenRAG docs",
+ );
}
return response.json();
diff --git a/frontend/app/api/queries/useGetSearchQuery.ts b/frontend/app/api/queries/useGetSearchQuery.ts
index 4d7db437d..e0b96f719 100644
--- a/frontend/app/api/queries/useGetSearchQuery.ts
+++ b/frontend/app/api/queries/useGetSearchQuery.ts
@@ -5,6 +5,7 @@ import {
} from "@tanstack/react-query";
import type { ParsedQueryData } from "@/contexts/knowledge-filter-context";
import { SEARCH_CONSTANTS } from "@/lib/constants";
+import { buildSearchPayloadFilters } from "@/lib/filter-normalization";
export interface SearchPayload {
query: string;
@@ -89,40 +90,8 @@ export const useGetSearchQuery = (
scoreThreshold: queryData?.scoreThreshold || 0,
};
if (queryData?.filters) {
- const filters = queryData.filters;
-
- // Only include filters if they're not wildcards (not "*")
- const hasSpecificFilters =
- !filters.data_sources.includes("*") ||
- !filters.document_types.includes("*") ||
- !filters.owners.includes("*") ||
- (filters.connector_types && !filters.connector_types.includes("*"));
-
- if (hasSpecificFilters) {
- const processedFilters: SearchPayload["filters"] = {};
-
- // Only add filter arrays that don't contain wildcards
- if (!filters.data_sources.includes("*")) {
- processedFilters.data_sources = filters.data_sources;
- }
- if (!filters.document_types.includes("*")) {
- processedFilters.document_types = filters.document_types;
- }
- if (!filters.owners.includes("*")) {
- processedFilters.owners = filters.owners;
- }
- if (
- filters.connector_types &&
- !filters.connector_types.includes("*")
- ) {
- processedFilters.connector_types = filters.connector_types;
- }
-
- // Only add filters object if it has any actual filters
- if (Object.keys(processedFilters).length > 0) {
- searchPayload.filters = processedFilters;
- }
- }
+ searchPayload.filters =
+ buildSearchPayloadFilters(queryData.filters) ?? undefined;
}
const response = await fetch(`/api/search`, {
diff --git a/frontend/app/chat/_types/types.ts b/frontend/app/chat/_types/types.ts
index c00ce97d6..cc9e2b0e0 100644
--- a/frontend/app/chat/_types/types.ts
+++ b/frontend/app/chat/_types/types.ts
@@ -62,7 +62,12 @@ export interface RequestBody {
prompt: string;
stream?: boolean;
previous_response_id?: string;
- filters?: SelectedFilters;
+ filters?: {
+ data_sources?: string[];
+ document_types?: string[];
+ owners?: string[];
+ connector_types?: string[];
+ };
filter_id?: string;
limit?: number;
scoreThreshold?: number;
diff --git a/frontend/app/chat/page.tsx b/frontend/app/chat/page.tsx
index 71aeee4d8..482d17594 100644
--- a/frontend/app/chat/page.tsx
+++ b/frontend/app/chat/page.tsx
@@ -9,6 +9,7 @@ import { type EndpointType, useChat } from "@/contexts/chat-context";
import { useTask } from "@/contexts/task-context";
import { useChatStreaming } from "@/hooks/useChatStreaming";
import { FILE_CONFIRMATION, FILES_REGEX } from "@/lib/constants";
+import { buildSearchPayloadFilters } from "@/lib/filter-normalization";
import { useLoadingStore } from "@/stores/loadingStore";
import { useGetConversationsQuery } from "../api/queries/useGetConversationsQuery";
import { useGetNudgesQuery } from "../api/queries/useGetNudgesQuery";
@@ -18,1282 +19,1209 @@ import { ChatInput, type ChatInputHandle } from "./_components/chat-input";
import Nudges from "./_components/nudges";
import { UserMessage } from "./_components/user-message";
import type {
- FunctionCall,
- KnowledgeFilterData,
- Message,
- RequestBody,
- SelectedFilters,
- ToolCallResult,
+ FunctionCall,
+ KnowledgeFilterData,
+ Message,
+ RequestBody,
+ ToolCallResult,
} from "./_types/types";
function ChatPage() {
- const isDebugMode = process.env.NEXT_PUBLIC_OPENRAG_DEBUG === "true";
- const {
- endpoint,
- setEndpoint,
- currentConversationId,
- conversationData,
- setCurrentConversationId,
- addConversationDoc,
- forkFromResponse,
- refreshConversations,
- refreshConversationsSilent,
- refreshTrigger,
- previousResponseIds,
- setPreviousResponseIds,
- placeholderConversation,
- conversationFilter,
- setConversationFilter,
- } = useChat();
- const [messages, setMessages] = useState<Message[]>([
- {
- role: "assistant",
- content: "How can I assist?",
- timestamp: new Date(),
- },
- ]);
- const [input, setInput] = useState("");
- const { loading, setLoading } = useLoadingStore();
- const { setChatError } = useChat();
- const [asyncMode, setAsyncMode] = useState(true);
- const [expandedFunctionCalls, setExpandedFunctionCalls] = useState<
- Set<string>
- >(new Set());
- // previousResponseIds now comes from useChat context
- const [isUploading, setIsUploading] = useState(false);
- const [isFilterHighlighted, setIsFilterHighlighted] = useState(false);
- const [isUserInteracting, setIsUserInteracting] = useState(false);
- const [isForkingInProgress, setIsForkingInProgress] = useState(false);
- const [uploadedFile, setUploadedFile] = useState<File | null>(null);
- const [waitingTooLong, setWaitingTooLong] = useState(false);
-
- const chatInputRef = useRef<ChatInputHandle>(null);
-
- const { scrollToBottom } = useStickToBottomContext();
-
- const lastLoadedConversationRef = useRef<string | null>(null);
- const { addTask } = useTask();
-
- // Check if chat history is loading
- const { isLoading: isConversationsLoading } = useGetConversationsQuery(
- endpoint,
- refreshTrigger,
- );
-
- // Use conversation-specific filter instead of global filter
- const selectedFilter = conversationFilter;
-
- // Parse the conversation filter data
- const parsedFilterData = useMemo(() => {
- if (!selectedFilter?.query_data) return null;
- try {
- return JSON.parse(selectedFilter.query_data);
- } catch (error) {
- console.error("Error parsing filter data:", error);
- return null;
- }
- }, [selectedFilter]);
-
- // Use the chat streaming hook
- const apiEndpoint = endpoint === "chat" ? "/api/chat" : "/api/langflow";
- const {
- streamingMessage,
- sendMessage: sendStreamingMessage,
- abortStream,
- isLoading: isStreamLoading,
- } = useChatStreaming({
- endpoint: apiEndpoint,
- onComplete: (message, responseId) => {
- setMessages((prev) => [...prev, message]);
- setLoading(false);
- setWaitingTooLong(false);
- if (responseId) {
- cancelNudges();
- setPreviousResponseIds((prev) => ({
- ...prev,
- [endpoint]: responseId,
- }));
-
- if (!currentConversationId) {
- setCurrentConversationId(responseId);
- refreshConversations(true);
- } else {
- refreshConversationsSilent();
- }
-
- // Save filter association for this response
- if (conversationFilter && typeof window !== "undefined") {
- const newKey = `conversation_filter_${responseId}`;
- localStorage.setItem(newKey, conversationFilter.id);
- console.log(
- "[CHAT] Saved filter association:",
- newKey,
- "=",
- conversationFilter.id,
- );
- }
- }
- },
- onError: (error) => {
- console.error("Streaming error:", error);
- setLoading(false);
- setWaitingTooLong(false);
- // Set chat error flag to trigger test_completion=true on health checks
- setChatError(true);
- const errorMessage: Message = {
- role: "assistant",
- content:
- "Sorry, I couldn't connect to the chat service. Please try again.",
- timestamp: new Date(),
- };
- setMessages((prev) => [...prev, errorMessage]);
- },
- });
-
- // Show warning if waiting too long (20 seconds)
- useEffect(() => {
- let timeoutId: NodeJS.Timeout | null = null;
-
- if (isStreamLoading && !streamingMessage) {
- timeoutId = setTimeout(() => {
- setWaitingTooLong(true);
- }, 20000); // 20 seconds
- } else {
- setWaitingTooLong(false);
- }
-
- return () => {
- if (timeoutId) clearTimeout(timeoutId);
- };
- }, [isStreamLoading, streamingMessage]);
-
- const handleEndpointChange = (newEndpoint: EndpointType) => {
- setEndpoint(newEndpoint);
- // Clear the conversation when switching endpoints to avoid response ID conflicts
- setMessages([]);
- setPreviousResponseIds({ chat: null, langflow: null });
- };
-
- const handleFileUpload = async (file: File) => {
- console.log("handleFileUpload called with file:", file.name);
-
- if (isUploading) return;
-
- setIsUploading(true);
- setLoading(true);
-
- try {
- const formData = new FormData();
- formData.append("file", file);
- formData.append("endpoint", endpoint);
-
- // Add previous_response_id if we have one for this endpoint
- const currentResponseId = previousResponseIds[endpoint];
- if (currentResponseId) {
- formData.append("previous_response_id", currentResponseId);
- }
-
- const response = await fetch("/api/upload_context", {
- method: "POST",
- body: formData,
- });
-
- console.log("Upload response status:", response.status);
-
- if (!response.ok) {
- const errorText = await response.text();
- console.error(
- "Upload failed with status:",
- response.status,
- "Response:",
- errorText,
- );
- throw new Error("Failed to process document");
- }
-
- const result = await response.json();
- console.log("Upload result:", result);
-
- if (!response.ok) {
- // Set chat error flag if upload fails
- setChatError(true);
- }
-
- if (response.status === 201) {
- // New flow: Got task ID, start tracking with centralized system
- const taskId = result.task_id || result.id;
-
- if (!taskId) {
- console.error("No task ID in 201 response:", result);
- throw new Error("No task ID received from server");
- }
-
- // Add task to centralized tracking
- addTask(taskId);
-
- return null;
- } else if (response.ok) {
- // Original flow: Direct response
-
- const uploadMessage: Message = {
- role: "user",
- content: `I'm uploading a document called "${result.filename}". Here is its content:`,
- timestamp: new Date(),
- };
-
- const confirmationMessage: Message = {
- role: "assistant",
- content: `Confirmed`,
- timestamp: new Date(),
- };
-
- setMessages((prev) => [...prev, uploadMessage, confirmationMessage]);
-
- // Add file to conversation docs
- if (result.filename) {
- addConversationDoc(result.filename);
- }
-
- // Update the response ID for this endpoint
- if (result.response_id) {
- setPreviousResponseIds((prev) => ({
- ...prev,
- [endpoint]: result.response_id,
- }));
-
- // If this is a new conversation (no currentConversationId), set it now
- if (!currentConversationId) {
- setCurrentConversationId(result.response_id);
- refreshConversations(true);
- } else {
- // For existing conversations, do a silent refresh to keep backend in sync
- refreshConversationsSilent();
- }
-
- return result.response_id;
- }
- } else {
- throw new Error(`Upload failed: ${response.status}`);
- }
- } catch (error) {
- console.error("Upload failed:", error);
- // Set chat error flag to trigger test_completion=true on health checks
- setChatError(true);
- const errorMessage: Message = {
- role: "assistant",
- content: `❌ Failed to process document. Please try again.`,
- timestamp: new Date(),
- };
- setMessages((prev) => [...prev.slice(0, -1), errorMessage]);
- } finally {
- setIsUploading(false);
- setLoading(false);
- }
- };
-
- const handleFilePickerClick = () => {
- chatInputRef.current?.clickFileInput();
- };
-
- const handleFilterSelect = (filter: KnowledgeFilterData | null) => {
- // Update conversation-specific filter
- setConversationFilter(filter);
- setIsFilterHighlighted(false);
- };
-
- // Auto-focus the input on component mount
- useEffect(() => {
- chatInputRef.current?.focusInput();
- }, []);
-
- // Explicitly handle external new conversation trigger
- useEffect(() => {
- const handleNewConversation = () => {
- // Abort any in-flight streaming so it doesn't bleed into new chat
- abortStream();
- // Reset chat UI even if context state was already 'new'
- setMessages([
- {
- role: "assistant",
- content: "How can I assist?",
- timestamp: new Date(),
- },
- ]);
- setInput("");
- setExpandedFunctionCalls(new Set());
- setIsFilterHighlighted(false);
- setLoading(false);
- lastLoadedConversationRef.current = null;
-
- // Focus input after a short delay to ensure rendering is complete
- setTimeout(() => {
- chatInputRef.current?.focusInput();
- }, 100);
- };
-
- const handleFocusInput = () => {
- chatInputRef.current?.focusInput();
- };
-
- window.addEventListener("newConversation", handleNewConversation);
- window.addEventListener("focusInput", handleFocusInput);
- return () => {
- window.removeEventListener("newConversation", handleNewConversation);
- window.removeEventListener("focusInput", handleFocusInput);
- };
- }, [abortStream, setLoading]);
-
- // Load conversation only when user explicitly selects a conversation
- useEffect(() => {
- // Only load conversation data when:
- // 1. conversationData exists AND
- // 2. It's different from the last loaded conversation AND
- // 3. User is not in the middle of an interaction
- if (
- conversationData?.messages &&
- lastLoadedConversationRef.current !== conversationData.response_id &&
- !isUserInteracting &&
- !isForkingInProgress
- ) {
- console.log(
- "Loading conversation with",
- conversationData.messages.length,
- "messages",
- );
- // Convert backend message format to frontend Message interface
- const convertedMessages: Message[] = conversationData.messages.map(
- (msg: {
- role: string;
- content: string;
- timestamp?: string;
- response_id?: string;
- chunks?: Array<{
- item?: {
- type?: string;
- tool_name?: string;
- id?: string;
- inputs?: unknown;
- results?: unknown;
- status?: string;
- };
- delta?: {
- tool_calls?: Array<{
- id?: string;
- function?: { name?: string; arguments?: string };
- type?: string;
- }>;
- };
- type?: string;
- result?: unknown;
- output?: unknown;
- response?: unknown;
- }>;
- response_data?: unknown;
- }) => {
- const message: Message = {
- role: msg.role as "user" | "assistant",
- content: msg.content,
- timestamp: new Date(msg.timestamp || new Date()),
- };
-
- // Extract function calls from chunks or response_data
- if (msg.role === "assistant" && (msg.chunks || msg.response_data)) {
- const functionCalls: FunctionCall[] = [];
- console.log("Processing assistant message for function calls:", {
- hasChunks: !!msg.chunks,
- chunksLength: msg.chunks?.length,
- hasResponseData: !!msg.response_data,
- });
-
- // Process chunks (streaming data)
- if (msg.chunks && Array.isArray(msg.chunks)) {
- for (const chunk of msg.chunks) {
- // Handle Langflow format: chunks[].item.tool_call
- if (chunk.item && chunk.item.type === "tool_call") {
- const toolCall = chunk.item;
- console.log("Found Langflow tool call:", toolCall);
- functionCalls.push({
- id: toolCall.id || "",
- name: toolCall.tool_name || "unknown",
- arguments:
- (toolCall.inputs as Record<string, unknown>) || {},
- argumentsString: JSON.stringify(toolCall.inputs || {}),
- result: toolCall.results as
- | Record<string, unknown>
- | ToolCallResult[],
- status:
- (toolCall.status as "pending" | "completed" | "error") ||
- "completed",
- type: "tool_call",
- });
- }
- // Handle OpenAI format: chunks[].delta.tool_calls
- else if (chunk.delta?.tool_calls) {
- for (const toolCall of chunk.delta.tool_calls) {
- if (toolCall.function) {
- functionCalls.push({
- id: toolCall.id || "",
- name: toolCall.function.name || "unknown",
- arguments: toolCall.function.arguments
- ? JSON.parse(toolCall.function.arguments)
- : {},
- argumentsString: toolCall.function.arguments || "",
- status: "completed",
- type: toolCall.type || "function",
- });
- }
- }
- }
- // Process tool call results from chunks
- if (
- chunk.type === "response.tool_call.result" ||
- chunk.type === "tool_call_result"
- ) {
- const lastCall = functionCalls[functionCalls.length - 1];
- if (lastCall) {
- lastCall.result =
- (chunk.result as
- | Record<string, unknown>
- | ToolCallResult[]) ||
- (chunk as Record<string, unknown>);
- lastCall.status = "completed";
- }
- }
- }
- }
-
- // Process response_data (non-streaming data)
- if (msg.response_data && typeof msg.response_data === "object") {
- // Look for tool_calls in various places in the response data
- const responseData =
- typeof msg.response_data === "string"
- ? JSON.parse(msg.response_data)
- : msg.response_data;
-
- if (
- responseData.tool_calls &&
- Array.isArray(responseData.tool_calls)
- ) {
- for (const toolCall of responseData.tool_calls) {
- functionCalls.push({
- id: toolCall.id,
- name: toolCall.function?.name || toolCall.name,
- arguments:
- toolCall.function?.arguments || toolCall.arguments,
- argumentsString:
- typeof (
- toolCall.function?.arguments || toolCall.arguments
- ) === "string"
- ? toolCall.function?.arguments || toolCall.arguments
- : JSON.stringify(
- toolCall.function?.arguments || toolCall.arguments,
- ),
- result: toolCall.result,
- status: "completed",
- type: toolCall.type || "function",
- });
- }
- }
- }
-
- if (functionCalls.length > 0) {
- console.log("Setting functionCalls on message:", functionCalls);
- message.functionCalls = functionCalls;
- } else {
- console.log("No function calls found in message");
- }
-
- // Extract usage data from response_data
- if (msg.response_data && typeof msg.response_data === "object") {
- const responseData =
- typeof msg.response_data === "string"
- ? JSON.parse(msg.response_data)
- : msg.response_data;
- if (responseData.usage) {
- message.usage = responseData.usage;
- }
- }
- }
-
- return message;
- },
- );
-
- setMessages(convertedMessages);
- lastLoadedConversationRef.current = conversationData.response_id;
-
- // Set the previous response ID for this conversation
- setPreviousResponseIds((prev) => ({
- ...prev,
- [conversationData.endpoint]: conversationData.response_id,
- }));
-
- // Focus input when loading a conversation
- setTimeout(() => {
- chatInputRef.current?.focusInput();
- }, 100);
- } else if (!conversationData) {
- // No conversation selected (new conversation)
- lastLoadedConversationRef.current = null;
- }
- }, [
- conversationData,
- isUserInteracting,
- isForkingInProgress,
- setPreviousResponseIds,
- ]);
-
- // Handle new conversation creation - only reset messages when placeholderConversation is set
- useEffect(() => {
- if (placeholderConversation && currentConversationId === null) {
- console.log("Starting new conversation");
- setMessages([
- {
- role: "assistant",
- content: "How can I assist?",
- timestamp: new Date(),
- },
- ]);
- lastLoadedConversationRef.current = null;
-
- // Focus input when starting a new conversation
- setTimeout(() => {
- chatInputRef.current?.focusInput();
- }, 100);
- }
- }, [placeholderConversation, currentConversationId]);
-
- // Listen for file upload events from navigation
- useEffect(() => {
- const handleFileUploadStart = (event: CustomEvent) => {
- const { filename } = event.detail;
- console.log("Chat page received file upload start event:", filename);
-
- setLoading(true);
- setIsUploading(true);
- setUploadedFile(null); // Clear previous file
- };
-
- const handleFileUploaded = (event: CustomEvent) => {
- const { result } = event.detail;
- console.log("Chat page received file upload event:", result);
-
- setUploadedFile(null); // Clear file after upload
-
- // Update the response ID for this endpoint
- if (result.response_id) {
- setPreviousResponseIds((prev) => ({
- ...prev,
- [endpoint]: result.response_id,
- }));
- }
- };
-
- const handleFileUploadComplete = () => {
- console.log("Chat page received file upload complete event");
- setLoading(false);
- setIsUploading(false);
- };
-
- const handleFileUploadError = (event: CustomEvent) => {
- const { filename, error } = event.detail;
- console.log(
- "Chat page received file upload error event:",
- filename,
- error,
- );
-
- // Replace the last message with error message
- const errorMessage: Message = {
- role: "assistant",
- content: `❌ Upload failed for **${filename}**: ${error}`,
- timestamp: new Date(),
- };
- setMessages((prev) => [...prev.slice(0, -1), errorMessage]);
- setUploadedFile(null); // Clear file on error
- };
-
- window.addEventListener(
- "fileUploadStart",
- handleFileUploadStart as EventListener,
- );
- window.addEventListener(
- "fileUploaded",
- handleFileUploaded as EventListener,
- );
- window.addEventListener(
- "fileUploadComplete",
- handleFileUploadComplete as EventListener,
- );
- window.addEventListener(
- "fileUploadError",
- handleFileUploadError as EventListener,
- );
-
- return () => {
- window.removeEventListener(
- "fileUploadStart",
- handleFileUploadStart as EventListener,
- );
- window.removeEventListener(
- "fileUploaded",
- handleFileUploaded as EventListener,
- );
- window.removeEventListener(
- "fileUploadComplete",
- handleFileUploadComplete as EventListener,
- );
- window.removeEventListener(
- "fileUploadError",
- handleFileUploadError as EventListener,
- );
- };
- }, [endpoint, setPreviousResponseIds, setLoading]);
-
- // Get settings to check onboarding completion
- const { data: settings } = useGetSettingsQuery();
-
- // Check if onboarding is complete (current_step >= 4 means complete)
- const TOTAL_ONBOARDING_STEPS = 4;
- const isOnboardingComplete =
- settings?.onboarding?.current_step !== undefined &&
- settings.onboarding.current_step >= TOTAL_ONBOARDING_STEPS;
-
- // Prepare filters for nudges (same as chat)
- const processedFiltersForNudges = parsedFilterData?.filters
- ? (() => {
- const filters = parsedFilterData.filters;
- const processed: SelectedFilters = {
- data_sources: [],
- document_types: [],
- owners: [],
- connector_types: [],
- };
- processed.data_sources = filters.data_sources.includes("*")
- ? []
- : filters.data_sources;
- processed.document_types = filters.document_types.includes("*")
- ? []
- : filters.document_types;
- processed.owners = filters.owners.includes("*") ? [] : filters.owners;
- processed.connector_types =
- filters.connector_types?.includes("*")
- ? []
- : (filters.connector_types ?? []);
-
- const hasFilters =
- processed.data_sources.length > 0 ||
- processed.document_types.length > 0 ||
- processed.owners.length > 0 ||
- processed.connector_types.length > 0;
- return hasFilters ? processed : undefined;
- })()
- : undefined;
-
- const { data: nudges = [], cancel: cancelNudges } = useGetNudgesQuery(
- {
- chatId: previousResponseIds[endpoint],
- filters: processedFiltersForNudges,
- limit: parsedFilterData?.limit ?? 3,
- scoreThreshold: parsedFilterData?.scoreThreshold ?? 0,
- },
- {
- enabled: isOnboardingComplete && !isConversationsLoading, // Only fetch nudges after onboarding is complete AND chat history is not loading
- },
- );
-
- const handleSSEStream = async (
- userMessage: Message,
- previousResponseId?: string,
- ) => {
- // Prepare filters
- const processedFilters = parsedFilterData?.filters
- ? (() => {
- const filters = parsedFilterData.filters;
- const processed: SelectedFilters = {
- data_sources: [],
- document_types: [],
- owners: [],
- connector_types: [],
- };
- processed.data_sources = filters.data_sources.includes("*")
- ? []
- : filters.data_sources;
- processed.document_types = filters.document_types.includes("*")
- ? []
- : filters.document_types;
- processed.owners = filters.owners.includes("*") ? [] : filters.owners;
- processed.connector_types =
- filters.connector_types?.includes("*")
- ? []
- : (filters.connector_types ?? []);
-
- const hasFilters =
- processed.data_sources.length > 0 ||
- processed.document_types.length > 0 ||
- processed.owners.length > 0 ||
- processed.connector_types.length > 0;
- return hasFilters ? processed : undefined;
- })()
- : undefined;
-
- // Use passed previousResponseId if available, otherwise fall back to state
- const responseIdToUse = previousResponseId || previousResponseIds[endpoint];
-
- console.log("[CHAT] Sending streaming message:", {
- conversationFilter: conversationFilter?.id,
- currentConversationId,
- responseIdToUse,
- });
-
- // Use the hook to send the message
- await sendStreamingMessage({
- prompt: userMessage.content,
- previousResponseId: responseIdToUse || undefined,
- filters: processedFilters,
- filter_id: conversationFilter?.id, // ✅ Add filter_id for this conversation
- limit: parsedFilterData?.limit ?? 10,
- scoreThreshold: parsedFilterData?.scoreThreshold ?? 0,
- });
- scrollToBottom({
- animation: "smooth",
- duration: 1000,
- });
- };
-
- const handleSendMessage = async (
- inputMessage: string,
- previousResponseId?: string,
- ) => {
- if (!inputMessage.trim() || loading) return;
-
- const userMessage: Message = {
- role: "user",
- content: inputMessage.trim(),
- timestamp: new Date(),
- };
-
- if (messages.length === 1) {
- setMessages([userMessage]);
- } else {
- setMessages((prev) => [...prev, userMessage]);
- }
- setInput("");
- setLoading(true);
- setIsFilterHighlighted(false);
-
- scrollToBottom({
- animation: "smooth",
- duration: 1000,
- });
-
- if (asyncMode) {
- await handleSSEStream(userMessage, previousResponseId);
- } else {
- // Original non-streaming logic
- try {
- const apiEndpoint = endpoint === "chat" ? "/api/chat" : "/api/langflow";
-
- const requestBody: RequestBody = {
- prompt: userMessage.content,
- ...(parsedFilterData?.filters &&
- (() => {
- const filters = parsedFilterData.filters;
- const processed: SelectedFilters = {
- data_sources: [],
- document_types: [],
- owners: [],
- connector_types: [],
- };
- // Only copy non-wildcard arrays
- processed.data_sources = filters.data_sources.includes("*")
- ? []
- : filters.data_sources;
- processed.document_types = filters.document_types.includes("*")
- ? []
- : filters.document_types;
- processed.owners = filters.owners.includes("*")
- ? []
- : filters.owners;
- processed.connector_types =
- filters.connector_types?.includes("*")
- ? []
- : (filters.connector_types ?? []);
-
- // Only include filters if any array has values
- const hasFilters =
- processed.data_sources.length > 0 ||
- processed.document_types.length > 0 ||
- processed.owners.length > 0 ||
- processed.connector_types.length > 0;
- return hasFilters ? { filters: processed } : {};
- })()),
- limit: parsedFilterData?.limit ?? 10,
- scoreThreshold: parsedFilterData?.scoreThreshold ?? 0,
- };
-
- // Add previous_response_id if we have one for this endpoint
- const currentResponseId = previousResponseIds[endpoint];
- if (currentResponseId) {
- requestBody.previous_response_id = currentResponseId;
- }
-
- // Add filter_id if a filter is selected for this conversation
- if (conversationFilter) {
- requestBody.filter_id = conversationFilter.id;
- }
-
- // Debug logging
- console.log("[DEBUG] Sending message with:", {
- previous_response_id: requestBody.previous_response_id,
- filter_id: requestBody.filter_id,
- currentConversationId,
- previousResponseIds,
- });
-
- const response = await fetch(apiEndpoint, {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify(requestBody),
- });
-
- const result = await response.json();
-
- if (response.ok) {
- const assistantMessage: Message = {
- role: "assistant",
- content: result.response,
- timestamp: new Date(),
- usage: result.usage,
- };
- setMessages((prev) => [...prev, assistantMessage]);
- if (result.response_id) {
- cancelNudges();
- }
-
- // Store the response ID if present for this endpoint
- if (result.response_id) {
- console.log(
- "[DEBUG] Received response_id:",
- result.response_id,
- "currentConversationId:",
- currentConversationId,
- );
-
- setPreviousResponseIds((prev) => ({
- ...prev,
- [endpoint]: result.response_id,
- }));
-
- // If this is a new conversation (no currentConversationId), set it now
- if (!currentConversationId) {
- console.log(
- "[DEBUG] Setting currentConversationId to:",
- result.response_id,
- );
- setCurrentConversationId(result.response_id);
- refreshConversations(true);
- } else {
- console.log(
- "[DEBUG] Existing conversation, doing silent refresh",
- );
- // For existing conversations, do a silent refresh to keep backend in sync
- refreshConversationsSilent();
- }
-
- // Carry forward the filter association to the new response_id
- if (conversationFilter && typeof window !== "undefined") {
- const newKey = `conversation_filter_${result.response_id}`;
- localStorage.setItem(newKey, conversationFilter.id);
- console.log(
- "[DEBUG] Saved filter association:",
- newKey,
- "=",
- conversationFilter.id,
- );
- }
- }
- } else {
- console.error("Chat failed:", result.error);
- // Set chat error flag to trigger test_completion=true on health checks
- setChatError(true);
- const errorMessage: Message = {
- role: "assistant",
- content: "Sorry, I encountered an error. Please try again.",
- timestamp: new Date(),
- };
- setMessages((prev) => [...prev, errorMessage]);
- }
- } catch (error) {
- console.error("Chat error:", error);
- // Set chat error flag to trigger test_completion=true on health checks
- setChatError(true);
- const errorMessage: Message = {
- role: "assistant",
- content:
- "Sorry, I couldn't connect to the chat service. Please try again.",
- timestamp: new Date(),
- };
- setMessages((prev) => [...prev, errorMessage]);
- }
- }
-
- setLoading(false);
- };
-
- const handleSubmit = async (e: React.FormEvent) => {
- e.preventDefault();
-
- // Check if there's an uploaded file and upload it first
- let uploadedResponseId: string | null = null;
- if (uploadedFile) {
- // Upload the file first
- const responseId = await handleFileUpload(uploadedFile);
- // Clear the file after upload
- setUploadedFile(null);
-
- // If the upload resulted in a new conversation, store the response ID
- if (responseId) {
- uploadedResponseId = responseId;
- setPreviousResponseIds((prev) => ({
- ...prev,
- [endpoint]: responseId,
- }));
- }
- }
-
- // Only send message if there's input text
- if (input.trim() || uploadedFile) {
- // Pass the responseId from upload (if any) to handleSendMessage
- handleSendMessage(
- !input.trim() ? FILE_CONFIRMATION : input,
- uploadedResponseId || undefined,
- );
- }
- };
-
- const toggleFunctionCall = (functionCallId: string) => {
- setExpandedFunctionCalls((prev) => {
- const newSet = new Set(prev);
- if (newSet.has(functionCallId)) {
- newSet.delete(functionCallId);
- } else {
- newSet.add(functionCallId);
- }
- return newSet;
- });
- };
-
- const handleForkConversation = (
- messageIndex: number,
- event?: React.MouseEvent,
- ) => {
- // Prevent any default behavior and stop event propagation
- if (event) {
- event.preventDefault();
- event.stopPropagation();
- }
-
- // Set interaction state to prevent auto-scroll interference
- setIsUserInteracting(true);
- setIsForkingInProgress(true);
-
- console.log("Fork conversation called for message index:", messageIndex);
-
- // Get messages up to and including the selected assistant message
- const messagesToKeep = messages.slice(0, messageIndex + 1);
-
- // The selected message should be an assistant message (since fork button is only on assistant messages)
- const forkedMessage = messages[messageIndex];
- if (forkedMessage.role !== "assistant") {
- console.error("Fork button should only be on assistant messages");
- setIsUserInteracting(false);
- setIsForkingInProgress(false);
- return;
- }
-
- // For forking, we want to continue from the response_id of the assistant message we're forking from
- // Since we don't store individual response_ids per message yet, we'll use the current conversation's response_id
- // This means we're continuing the conversation thread from that point
- const responseIdToForkFrom =
- currentConversationId || previousResponseIds[endpoint];
-
- // Create a new conversation by properly forking
- setMessages(messagesToKeep);
-
- // Use the chat context's fork method which handles creating a new conversation properly
- if (forkFromResponse) {
- forkFromResponse(responseIdToForkFrom || "");
- } else {
- // Fallback to manual approach
- setCurrentConversationId(null); // This creates a new conversation thread
-
- // Set the response_id we want to continue from as the previous response ID
- // This tells the backend to continue the conversation from this point
- setPreviousResponseIds((prev) => ({
- ...prev,
- [endpoint]: responseIdToForkFrom,
- }));
- }
-
- console.log("Forked conversation with", messagesToKeep.length, "messages");
-
- // Reset interaction state after a longer delay to ensure all effects complete
- setTimeout(() => {
- setIsUserInteracting(false);
- setIsForkingInProgress(false);
- console.log("Fork interaction complete, re-enabling auto effects");
- }, 500);
-
- // The original conversation remains unchanged in the sidebar
- // This new forked conversation will get its own response_id when the user sends the next message
- };
-
- const handleSuggestionClick = (suggestion: string) => {
- handleSendMessage(suggestion);
- };
-
- return (
- <>
- {/* Debug header - only show in debug mode */}
- {isDebugMode && (
- <div className="flex items-center justify-between p-6">
- <div className="flex items-center gap-2"></div>
- <div className="flex items-center gap-4">
- {/* Async Mode Toggle */}
- <div className="flex items-center gap-2 bg-muted/50 rounded-lg p-1">
- <Button
- variant={!asyncMode ? "default" : "ghost"}
- size="sm"
- onClick={() => setAsyncMode(false)}
- className="h-7 text-xs"
- >
- Streaming Off
- </Button>
- <Button
- variant={asyncMode ? "default" : "ghost"}
- size="sm"
- onClick={() => setAsyncMode(true)}
- className="h-7 text-xs"
- >
- <Zap className="h-3 w-3 mr-1" />
- Streaming On
- </Button>
- </div>
- {/* Endpoint Toggle */}
- <div className="flex items-center gap-2 bg-muted/50 rounded-lg p-1">
- <Button
- variant={endpoint === "chat" ? "default" : "ghost"}
- size="sm"
- onClick={() => handleEndpointChange("chat")}
- className="h-7 text-xs"
- >
- Chat
- </Button>
- <Button
- variant={endpoint === "langflow" ? "default" : "ghost"}
- size="sm"
- onClick={() => handleEndpointChange("langflow")}
- className="h-7 text-xs"
- >
- Langflow
- </Button>
- </div>
- </div>
- </div>
- )}
-
- <StickToBottom.Content className="flex flex-col min-h-full overflow-x-hidden p-6">
- <div className="flex flex-col place-self-center space-y-6 max-w-[960px] w-full mx-auto">
- {messages.length === 0 && !streamingMessage ? (
- <div className="flex items-center justify-center h-full text-muted-foreground">
- <div className="text-center">
- {isUploading ? (
- <>
- <Loader2 className="h-12 w-12 mx-auto mb-4 animate-spin" />
- <p>Processing your document...</p>
- <p className="text-sm mt-2">This may take a few moments</p>
- </>
- ) : null}
- </div>
- </div>
- ) : (
- <>
- {messages.map((message, index) =>
- message.role === "user"
- ? (messages[index]?.content.match(FILES_REGEX)?.[0] ??
- null) === null && (
- <div
- key={`${
- message.role
- }-${index}-${message.timestamp?.getTime()}`}
- className="space-y-6 group"
- >
- <UserMessage
- animate={
- message.source
- ? message.source !== "langflow"
- : false
- }
- content={
- index >= 2 &&
- (messages[index - 2]?.content.match(
- FILES_REGEX,
- )?.[0] ??
- undefined) &&
- message.content === FILE_CONFIRMATION
- ? undefined
- : message.content
- }
- files={
- index >= 2
- ? (messages[index - 2]?.content.match(
- FILES_REGEX,
- )?.[0] ?? undefined)
- : undefined
- }
- />
- </div>
- )
- : message.role === "assistant" &&
- (index < 1 ||
- (messages[index - 1]?.content.match(FILES_REGEX)?.[0] ??
- null) === null) && (
- <div
- key={`${
- message.role
- }-${index}-${message.timestamp?.getTime()}`}
- className="space-y-6 group"
- >
- <AssistantMessage
- content={message.content}
- functionCalls={message.functionCalls}
- messageIndex={index}
- expandedFunctionCalls={expandedFunctionCalls}
- onToggle={toggleFunctionCall}
- showForkButton={endpoint === "chat"}
- onFork={(e) => handleForkConversation(index, e)}
- animate={false}
- isInactive={index < messages.length - 1}
- isInitialGreeting={
- index === 0 &&
- messages.length === 1 &&
- message.content === "How can I assist?"
- }
- usage={message.usage}
- />
- </div>
- ),
- )}
-
- {/* Streaming Message Display */}
- {streamingMessage && (
- <AssistantMessage
- content={streamingMessage.content}
- functionCalls={streamingMessage.functionCalls}
- messageIndex={messages.length}
- expandedFunctionCalls={expandedFunctionCalls}
- onToggle={toggleFunctionCall}
- delay={0.4}
- isStreaming
- isCompleted={false}
- />
- )}
-
- {/* Waiting too long indicator */}
- {waitingTooLong && !streamingMessage && loading && (
- <div className="pl-10 space-y-2">
- <div className="flex items-center gap-2 text-sm text-muted-foreground">
- <Loader2 className="h-4 w-4 animate-spin" />
- <span>The server is taking longer than expected...</span>
- </div>
- <p className="text-xs text-muted-foreground">
- This may be due to high server load. The request will
- timeout after 60 seconds.
- </p>
- </div>
- )}
- </>
- )}
- {!streamingMessage && (
- <div className="pl-10">
- <Nudges
- nudges={loading ? [] : (nudges as string[])}
- handleSuggestionClick={handleSuggestionClick}
- />
- </div>
- )}
- </div>
- </StickToBottom.Content>
- <div className="p-6 pt-0 max-w-[960px] mx-auto w-full">
- {/* Input Area - Fixed at bottom */}
- <ChatInput
- ref={chatInputRef}
- input={input}
- loading={loading}
- isUploading={isUploading}
- selectedFilter={selectedFilter}
- parsedFilterData={parsedFilterData}
- uploadedFile={uploadedFile}
- onSubmit={handleSubmit}
- onChange={setInput}
- onKeyDown={(e) => {
- // Handle backspace for filter clearing
- if (
- e.key === "Backspace" &&
- selectedFilter &&
- input.trim() === ""
- ) {
- e.preventDefault();
- if (isFilterHighlighted) {
- // Second backspace - remove the filter
- setConversationFilter(null);
- setIsFilterHighlighted(false);
- } else {
- // First backspace - highlight the filter
- setIsFilterHighlighted(true);
- }
- return;
- }
-
- // Handle Enter key for form submission
- if (e.key === "Enter" && !e.shiftKey) {
- e.preventDefault();
- if (input.trim() && !loading) {
- // Trigger form submission by finding the form and calling submit
- const form = e.currentTarget.closest("form");
- if (form) {
- form.requestSubmit();
- }
- }
- }
- }}
- onFilterSelect={handleFilterSelect}
- onFilePickerClick={handleFilePickerClick}
- onFileSelected={setUploadedFile}
- setSelectedFilter={setConversationFilter}
- setIsFilterHighlighted={setIsFilterHighlighted}
- />
- </div>
- </>
- );
+ const isDebugMode = process.env.NEXT_PUBLIC_OPENRAG_DEBUG === "true";
+ const {
+ endpoint,
+ setEndpoint,
+ currentConversationId,
+ conversationData,
+ setCurrentConversationId,
+ addConversationDoc,
+ forkFromResponse,
+ refreshConversations,
+ refreshConversationsSilent,
+ refreshTrigger,
+ previousResponseIds,
+ setPreviousResponseIds,
+ placeholderConversation,
+ conversationFilter,
+ setConversationFilter,
+ } = useChat();
+ const [messages, setMessages] = useState<Message[]>([
+ {
+ role: "assistant",
+ content: "How can I assist?",
+ timestamp: new Date(),
+ },
+ ]);
+ const [input, setInput] = useState("");
+ const { loading, setLoading } = useLoadingStore();
+ const { setChatError } = useChat();
+ const [asyncMode, setAsyncMode] = useState(true);
+ const [expandedFunctionCalls, setExpandedFunctionCalls] = useState<
+ Set<string>
+ >(new Set());
+ // previousResponseIds now comes from useChat context
+ const [isUploading, setIsUploading] = useState(false);
+ const [isFilterHighlighted, setIsFilterHighlighted] = useState(false);
+ const [isUserInteracting, setIsUserInteracting] = useState(false);
+ const [isForkingInProgress, setIsForkingInProgress] = useState(false);
+ const [uploadedFile, setUploadedFile] = useState<File | null>(null);
+ const [waitingTooLong, setWaitingTooLong] = useState(false);
+
+ const chatInputRef = useRef<ChatInputHandle>(null);
+
+ const { scrollToBottom } = useStickToBottomContext();
+
+ const lastLoadedConversationRef = useRef<string | null>(null);
+ const { addTask } = useTask();
+
+ // Check if chat history is loading
+ const { isLoading: isConversationsLoading } = useGetConversationsQuery(
+ endpoint,
+ refreshTrigger,
+ );
+
+ // Use conversation-specific filter instead of global filter
+ const selectedFilter = conversationFilter;
+
+ // Parse the conversation filter data
+ const parsedFilterData = useMemo(() => {
+ if (!selectedFilter?.query_data) return null;
+ try {
+ return JSON.parse(selectedFilter.query_data);
+ } catch (error) {
+ console.error("Error parsing filter data:", error);
+ return null;
+ }
+ }, [selectedFilter]);
+
+ // Use the chat streaming hook
+ const apiEndpoint = endpoint === "chat" ? "/api/chat" : "/api/langflow";
+ const {
+ streamingMessage,
+ sendMessage: sendStreamingMessage,
+ abortStream,
+ isLoading: isStreamLoading,
+ } = useChatStreaming({
+ endpoint: apiEndpoint,
+ onComplete: (message, responseId) => {
+ setMessages((prev) => [...prev, message]);
+ setLoading(false);
+ setWaitingTooLong(false);
+ if (responseId) {
+ cancelNudges();
+ setPreviousResponseIds((prev) => ({
+ ...prev,
+ [endpoint]: responseId,
+ }));
+
+ if (!currentConversationId) {
+ setCurrentConversationId(responseId);
+ refreshConversations(true);
+ } else {
+ refreshConversationsSilent();
+ }
+
+ // Save filter association for this response
+ if (conversationFilter && typeof window !== "undefined") {
+ const newKey = `conversation_filter_${responseId}`;
+ localStorage.setItem(newKey, conversationFilter.id);
+ console.log(
+ "[CHAT] Saved filter association:",
+ newKey,
+ "=",
+ conversationFilter.id,
+ );
+ }
+ }
+ },
+ onError: (error) => {
+ console.error("Streaming error:", error);
+ setLoading(false);
+ setWaitingTooLong(false);
+ // Set chat error flag to trigger test_completion=true on health checks
+ setChatError(true);
+ const errorMessage: Message = {
+ role: "assistant",
+ content:
+ "Sorry, I couldn't connect to the chat service. Please try again.",
+ timestamp: new Date(),
+ };
+ setMessages((prev) => [...prev, errorMessage]);
+ },
+ });
+
+ // Show warning if waiting too long (20 seconds)
+ useEffect(() => {
+ let timeoutId: NodeJS.Timeout | null = null;
+
+ if (isStreamLoading && !streamingMessage) {
+ timeoutId = setTimeout(() => {
+ setWaitingTooLong(true);
+ }, 20000); // 20 seconds
+ } else {
+ setWaitingTooLong(false);
+ }
+
+ return () => {
+ if (timeoutId) clearTimeout(timeoutId);
+ };
+ }, [isStreamLoading, streamingMessage]);
+
+ const handleEndpointChange = (newEndpoint: EndpointType) => {
+ setEndpoint(newEndpoint);
+ // Clear the conversation when switching endpoints to avoid response ID conflicts
+ setMessages([]);
+ setPreviousResponseIds({ chat: null, langflow: null });
+ };
+
+ const handleFileUpload = async (file: File) => {
+ console.log("handleFileUpload called with file:", file.name);
+
+ if (isUploading) return;
+
+ setIsUploading(true);
+ setLoading(true);
+
+ try {
+ const formData = new FormData();
+ formData.append("file", file);
+ formData.append("endpoint", endpoint);
+
+ // Add previous_response_id if we have one for this endpoint
+ const currentResponseId = previousResponseIds[endpoint];
+ if (currentResponseId) {
+ formData.append("previous_response_id", currentResponseId);
+ }
+
+ const response = await fetch("/api/upload_context", {
+ method: "POST",
+ body: formData,
+ });
+
+ console.log("Upload response status:", response.status);
+
+ if (!response.ok) {
+ const errorText = await response.text();
+ console.error(
+ "Upload failed with status:",
+ response.status,
+ "Response:",
+ errorText,
+ );
+ throw new Error("Failed to process document");
+ }
+
+ const result = await response.json();
+ console.log("Upload result:", result);
+
+ if (!response.ok) {
+ // Set chat error flag if upload fails
+ setChatError(true);
+ }
+
+ if (response.status === 201) {
+ // New flow: Got task ID, start tracking with centralized system
+ const taskId = result.task_id || result.id;
+
+ if (!taskId) {
+ console.error("No task ID in 201 response:", result);
+ throw new Error("No task ID received from server");
+ }
+
+ // Add task to centralized tracking
+ addTask(taskId);
+
+ return null;
+ } else if (response.ok) {
+ // Original flow: Direct response
+
+ const uploadMessage: Message = {
+ role: "user",
+ content: `I'm uploading a document called "${result.filename}". Here is its content:`,
+ timestamp: new Date(),
+ };
+
+ const confirmationMessage: Message = {
+ role: "assistant",
+ content: `Confirmed`,
+ timestamp: new Date(),
+ };
+
+ setMessages((prev) => [...prev, uploadMessage, confirmationMessage]);
+
+ // Add file to conversation docs
+ if (result.filename) {
+ addConversationDoc(result.filename);
+ }
+
+ // Update the response ID for this endpoint
+ if (result.response_id) {
+ setPreviousResponseIds((prev) => ({
+ ...prev,
+ [endpoint]: result.response_id,
+ }));
+
+ // If this is a new conversation (no currentConversationId), set it now
+ if (!currentConversationId) {
+ setCurrentConversationId(result.response_id);
+ refreshConversations(true);
+ } else {
+ // For existing conversations, do a silent refresh to keep backend in sync
+ refreshConversationsSilent();
+ }
+
+ return result.response_id;
+ }
+ } else {
+ throw new Error(`Upload failed: ${response.status}`);
+ }
+ } catch (error) {
+ console.error("Upload failed:", error);
+ // Set chat error flag to trigger test_completion=true on health checks
+ setChatError(true);
+ const errorMessage: Message = {
+ role: "assistant",
+ content: `❌ Failed to process document. Please try again.`,
+ timestamp: new Date(),
+ };
+ setMessages((prev) => [...prev.slice(0, -1), errorMessage]);
+ } finally {
+ setIsUploading(false);
+ setLoading(false);
+ }
+ };
+
+ const handleFilePickerClick = () => {
+ chatInputRef.current?.clickFileInput();
+ };
+
+ const handleFilterSelect = (filter: KnowledgeFilterData | null) => {
+ // Update conversation-specific filter
+ setConversationFilter(filter);
+ setIsFilterHighlighted(false);
+ };
+
+ // Auto-focus the input on component mount
+ useEffect(() => {
+ chatInputRef.current?.focusInput();
+ }, []);
+
+ // Explicitly handle external new conversation trigger
+ useEffect(() => {
+ const handleNewConversation = () => {
+ // Abort any in-flight streaming so it doesn't bleed into new chat
+ abortStream();
+ // Reset chat UI even if context state was already 'new'
+ setMessages([
+ {
+ role: "assistant",
+ content: "How can I assist?",
+ timestamp: new Date(),
+ },
+ ]);
+ setInput("");
+ setExpandedFunctionCalls(new Set());
+ setIsFilterHighlighted(false);
+ setLoading(false);
+ lastLoadedConversationRef.current = null;
+
+ // Focus input after a short delay to ensure rendering is complete
+ setTimeout(() => {
+ chatInputRef.current?.focusInput();
+ }, 100);
+ };
+
+ const handleFocusInput = () => {
+ chatInputRef.current?.focusInput();
+ };
+
+ window.addEventListener("newConversation", handleNewConversation);
+ window.addEventListener("focusInput", handleFocusInput);
+ return () => {
+ window.removeEventListener("newConversation", handleNewConversation);
+ window.removeEventListener("focusInput", handleFocusInput);
+ };
+ }, [abortStream, setLoading]);
+
+ // Load conversation only when user explicitly selects a conversation
+ useEffect(() => {
+ // Only load conversation data when:
+ // 1. conversationData exists AND
+ // 2. It's different from the last loaded conversation AND
+ // 3. User is not in the middle of an interaction
+ if (
+ conversationData?.messages &&
+ lastLoadedConversationRef.current !== conversationData.response_id &&
+ !isUserInteracting &&
+ !isForkingInProgress
+ ) {
+ console.log(
+ "Loading conversation with",
+ conversationData.messages.length,
+ "messages",
+ );
+ // Convert backend message format to frontend Message interface
+ const convertedMessages: Message[] = conversationData.messages.map(
+ (msg: {
+ role: string;
+ content: string;
+ timestamp?: string;
+ response_id?: string;
+ chunks?: Array<{
+ item?: {
+ type?: string;
+ tool_name?: string;
+ id?: string;
+ inputs?: unknown;
+ results?: unknown;
+ status?: string;
+ };
+ delta?: {
+ tool_calls?: Array<{
+ id?: string;
+ function?: { name?: string; arguments?: string };
+ type?: string;
+ }>;
+ };
+ type?: string;
+ result?: unknown;
+ output?: unknown;
+ response?: unknown;
+ }>;
+ response_data?: unknown;
+ }) => {
+ const message: Message = {
+ role: msg.role as "user" | "assistant",
+ content: msg.content,
+ timestamp: new Date(msg.timestamp || new Date()),
+ };
+
+ // Extract function calls from chunks or response_data
+ if (msg.role === "assistant" && (msg.chunks || msg.response_data)) {
+ const functionCalls: FunctionCall[] = [];
+ console.log("Processing assistant message for function calls:", {
+ hasChunks: !!msg.chunks,
+ chunksLength: msg.chunks?.length,
+ hasResponseData: !!msg.response_data,
+ });
+
+ // Process chunks (streaming data)
+ if (msg.chunks && Array.isArray(msg.chunks)) {
+ for (const chunk of msg.chunks) {
+ // Handle Langflow format: chunks[].item.tool_call
+ if (chunk.item && chunk.item.type === "tool_call") {
+ const toolCall = chunk.item;
+ console.log("Found Langflow tool call:", toolCall);
+ functionCalls.push({
+ id: toolCall.id || "",
+ name: toolCall.tool_name || "unknown",
+ arguments:
+ (toolCall.inputs as Record<string, unknown>) || {},
+ argumentsString: JSON.stringify(toolCall.inputs || {}),
+ result: toolCall.results as
+ | Record<string, unknown>
+ | ToolCallResult[],
+ status:
+ (toolCall.status as "pending" | "completed" | "error") ||
+ "completed",
+ type: "tool_call",
+ });
+ }
+ // Handle OpenAI format: chunks[].delta.tool_calls
+ else if (chunk.delta?.tool_calls) {
+ for (const toolCall of chunk.delta.tool_calls) {
+ if (toolCall.function) {
+ functionCalls.push({
+ id: toolCall.id || "",
+ name: toolCall.function.name || "unknown",
+ arguments: toolCall.function.arguments
+ ? JSON.parse(toolCall.function.arguments)
+ : {},
+ argumentsString: toolCall.function.arguments || "",
+ status: "completed",
+ type: toolCall.type || "function",
+ });
+ }
+ }
+ }
+ // Process tool call results from chunks
+ if (
+ chunk.type === "response.tool_call.result" ||
+ chunk.type === "tool_call_result"
+ ) {
+ const lastCall = functionCalls[functionCalls.length - 1];
+ if (lastCall) {
+ lastCall.result =
+ (chunk.result as
+ | Record<string, unknown>
+ | ToolCallResult[]) ||
+ (chunk as Record<string, unknown>);
+ lastCall.status = "completed";
+ }
+ }
+ }
+ }
+
+ // Process response_data (non-streaming data)
+ if (msg.response_data && typeof msg.response_data === "object") {
+ // Look for tool_calls in various places in the response data
+ const responseData =
+ typeof msg.response_data === "string"
+ ? JSON.parse(msg.response_data)
+ : msg.response_data;
+
+ if (
+ responseData.tool_calls &&
+ Array.isArray(responseData.tool_calls)
+ ) {
+ for (const toolCall of responseData.tool_calls) {
+ functionCalls.push({
+ id: toolCall.id,
+ name: toolCall.function?.name || toolCall.name,
+ arguments:
+ toolCall.function?.arguments || toolCall.arguments,
+ argumentsString:
+ typeof (
+ toolCall.function?.arguments || toolCall.arguments
+ ) === "string"
+ ? toolCall.function?.arguments || toolCall.arguments
+ : JSON.stringify(
+ toolCall.function?.arguments || toolCall.arguments,
+ ),
+ result: toolCall.result,
+ status: "completed",
+ type: toolCall.type || "function",
+ });
+ }
+ }
+ }
+
+ if (functionCalls.length > 0) {
+ console.log("Setting functionCalls on message:", functionCalls);
+ message.functionCalls = functionCalls;
+ } else {
+ console.log("No function calls found in message");
+ }
+
+ // Extract usage data from response_data
+ if (msg.response_data && typeof msg.response_data === "object") {
+ const responseData =
+ typeof msg.response_data === "string"
+ ? JSON.parse(msg.response_data)
+ : msg.response_data;
+ if (responseData.usage) {
+ message.usage = responseData.usage;
+ }
+ }
+ }
+
+ return message;
+ },
+ );
+
+ setMessages(convertedMessages);
+ lastLoadedConversationRef.current = conversationData.response_id;
+
+ // Set the previous response ID for this conversation
+ setPreviousResponseIds((prev) => ({
+ ...prev,
+ [conversationData.endpoint]: conversationData.response_id,
+ }));
+
+ // Focus input when loading a conversation
+ setTimeout(() => {
+ chatInputRef.current?.focusInput();
+ }, 100);
+ } else if (!conversationData) {
+ // No conversation selected (new conversation)
+ lastLoadedConversationRef.current = null;
+ }
+ }, [
+ conversationData,
+ isUserInteracting,
+ isForkingInProgress,
+ setPreviousResponseIds,
+ ]);
+
+ // Handle new conversation creation - only reset messages when placeholderConversation is set
+ useEffect(() => {
+ if (placeholderConversation && currentConversationId === null) {
+ console.log("Starting new conversation");
+ setMessages([
+ {
+ role: "assistant",
+ content: "How can I assist?",
+ timestamp: new Date(),
+ },
+ ]);
+ lastLoadedConversationRef.current = null;
+
+ // Focus input when starting a new conversation
+ setTimeout(() => {
+ chatInputRef.current?.focusInput();
+ }, 100);
+ }
+ }, [placeholderConversation, currentConversationId]);
+
+ // Listen for file upload events from navigation
+ useEffect(() => {
+ const handleFileUploadStart = (event: CustomEvent) => {
+ const { filename } = event.detail;
+ console.log("Chat page received file upload start event:", filename);
+
+ setLoading(true);
+ setIsUploading(true);
+ setUploadedFile(null); // Clear previous file
+ };
+
+ const handleFileUploaded = (event: CustomEvent) => {
+ const { result } = event.detail;
+ console.log("Chat page received file upload event:", result);
+
+ setUploadedFile(null); // Clear file after upload
+
+ // Update the response ID for this endpoint
+ if (result.response_id) {
+ setPreviousResponseIds((prev) => ({
+ ...prev,
+ [endpoint]: result.response_id,
+ }));
+ }
+ };
+
+ const handleFileUploadComplete = () => {
+ console.log("Chat page received file upload complete event");
+ setLoading(false);
+ setIsUploading(false);
+ };
+
+ const handleFileUploadError = (event: CustomEvent) => {
+ const { filename, error } = event.detail;
+ console.log(
+ "Chat page received file upload error event:",
+ filename,
+ error,
+ );
+
+ // Replace the last message with error message
+ const errorMessage: Message = {
+ role: "assistant",
+ content: `❌ Upload failed for **${filename}**: ${error}`,
+ timestamp: new Date(),
+ };
+ setMessages((prev) => [...prev.slice(0, -1), errorMessage]);
+ setUploadedFile(null); // Clear file on error
+ };
+
+ window.addEventListener(
+ "fileUploadStart",
+ handleFileUploadStart as EventListener,
+ );
+ window.addEventListener(
+ "fileUploaded",
+ handleFileUploaded as EventListener,
+ );
+ window.addEventListener(
+ "fileUploadComplete",
+ handleFileUploadComplete as EventListener,
+ );
+ window.addEventListener(
+ "fileUploadError",
+ handleFileUploadError as EventListener,
+ );
+
+ return () => {
+ window.removeEventListener(
+ "fileUploadStart",
+ handleFileUploadStart as EventListener,
+ );
+ window.removeEventListener(
+ "fileUploaded",
+ handleFileUploaded as EventListener,
+ );
+ window.removeEventListener(
+ "fileUploadComplete",
+ handleFileUploadComplete as EventListener,
+ );
+ window.removeEventListener(
+ "fileUploadError",
+ handleFileUploadError as EventListener,
+ );
+ };
+ }, [endpoint, setPreviousResponseIds, setLoading]);
+
+ // Get settings to check onboarding completion
+ const { data: settings } = useGetSettingsQuery();
+
+ // Check if onboarding is complete (current_step >= 4 means complete)
+ const TOTAL_ONBOARDING_STEPS = 4;
+ const isOnboardingComplete =
+ settings?.onboarding?.current_step !== undefined &&
+ settings.onboarding.current_step >= TOTAL_ONBOARDING_STEPS;
+
+ // Prepare filters for nudges (same as chat)
+ const processedFiltersForNudges = parsedFilterData?.filters
+ ? (() => {
+ return buildSearchPayloadFilters(parsedFilterData.filters);
+ })()
+ : undefined;
+
+ const { data: nudges = [], cancel: cancelNudges } = useGetNudgesQuery(
+ {
+ chatId: previousResponseIds[endpoint],
+ filters: processedFiltersForNudges,
+ limit: parsedFilterData?.limit ?? 3,
+ scoreThreshold: parsedFilterData?.scoreThreshold ?? 0,
+ },
+ {
+ enabled: isOnboardingComplete && !isConversationsLoading, // Only fetch nudges after onboarding is complete AND chat history is not loading
+ },
+ );
+
+ const handleSSEStream = async (
+ userMessage: Message,
+ previousResponseId?: string,
+ ) => {
+ // Prepare filters
+ const processedFilters = parsedFilterData?.filters
+ ? (() => {
+ return buildSearchPayloadFilters(parsedFilterData.filters);
+ })()
+ : undefined;
+
+ // Use passed previousResponseId if available, otherwise fall back to state
+ const responseIdToUse = previousResponseId || previousResponseIds[endpoint];
+
+ console.log("[CHAT] Sending streaming message:", {
+ conversationFilter: conversationFilter?.id,
+ currentConversationId,
+ responseIdToUse,
+ });
+
+ // Use the hook to send the message
+ await sendStreamingMessage({
+ prompt: userMessage.content,
+ previousResponseId: responseIdToUse || undefined,
+ filters: processedFilters,
+ filter_id: conversationFilter?.id, // ✅ Add filter_id for this conversation
+ limit: parsedFilterData?.limit ?? 10,
+ scoreThreshold: parsedFilterData?.scoreThreshold ?? 0,
+ });
+ scrollToBottom({
+ animation: "smooth",
+ duration: 1000,
+ });
+ };
+
+ const handleSendMessage = async (
+ inputMessage: string,
+ previousResponseId?: string,
+ ) => {
+ if (!inputMessage.trim() || loading) return;
+
+ const userMessage: Message = {
+ role: "user",
+ content: inputMessage.trim(),
+ timestamp: new Date(),
+ };
+
+ if (messages.length === 1) {
+ setMessages([userMessage]);
+ } else {
+ setMessages((prev) => [...prev, userMessage]);
+ }
+ setInput("");
+ setLoading(true);
+ setIsFilterHighlighted(false);
+
+ scrollToBottom({
+ animation: "smooth",
+ duration: 1000,
+ });
+
+ if (asyncMode) {
+ await handleSSEStream(userMessage, previousResponseId);
+ } else {
+ // Original non-streaming logic
+ try {
+ const apiEndpoint = endpoint === "chat" ? "/api/chat" : "/api/langflow";
+
+ const requestBody: RequestBody = {
+ prompt: userMessage.content,
+ ...(parsedFilterData?.filters
+ ? (() => {
+ const processedFilters = buildSearchPayloadFilters(
+ parsedFilterData.filters,
+ );
+ return processedFilters ? { filters: processedFilters } : {};
+ })()
+ : {}),
+ limit: parsedFilterData?.limit ?? 10,
+ scoreThreshold: parsedFilterData?.scoreThreshold ?? 0,
+ };
+
+ // Add previous_response_id if we have one for this endpoint
+ const currentResponseId = previousResponseIds[endpoint];
+ if (currentResponseId) {
+ requestBody.previous_response_id = currentResponseId;
+ }
+
+ // Add filter_id if a filter is selected for this conversation
+ if (conversationFilter) {
+ requestBody.filter_id = conversationFilter.id;
+ }
+
+ // Debug logging
+ console.log("[DEBUG] Sending message with:", {
+ previous_response_id: requestBody.previous_response_id,
+ filter_id: requestBody.filter_id,
+ currentConversationId,
+ previousResponseIds,
+ });
+
+ const response = await fetch(apiEndpoint, {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify(requestBody),
+ });
+
+ const result = await response.json();
+
+ if (response.ok) {
+ const assistantMessage: Message = {
+ role: "assistant",
+ content: result.response,
+ timestamp: new Date(),
+ usage: result.usage,
+ };
+ setMessages((prev) => [...prev, assistantMessage]);
+ if (result.response_id) {
+ cancelNudges();
+ }
+
+ // Store the response ID if present for this endpoint
+ if (result.response_id) {
+ console.log(
+ "[DEBUG] Received response_id:",
+ result.response_id,
+ "currentConversationId:",
+ currentConversationId,
+ );
+
+ setPreviousResponseIds((prev) => ({
+ ...prev,
+ [endpoint]: result.response_id,
+ }));
+
+ // If this is a new conversation (no currentConversationId), set it now
+ if (!currentConversationId) {
+ console.log(
+ "[DEBUG] Setting currentConversationId to:",
+ result.response_id,
+ );
+ setCurrentConversationId(result.response_id);
+ refreshConversations(true);
+ } else {
+ console.log(
+ "[DEBUG] Existing conversation, doing silent refresh",
+ );
+ // For existing conversations, do a silent refresh to keep backend in sync
+ refreshConversationsSilent();
+ }
+
+ // Carry forward the filter association to the new response_id
+ if (conversationFilter && typeof window !== "undefined") {
+ const newKey = `conversation_filter_${result.response_id}`;
+ localStorage.setItem(newKey, conversationFilter.id);
+ console.log(
+ "[DEBUG] Saved filter association:",
+ newKey,
+ "=",
+ conversationFilter.id,
+ );
+ }
+ }
+ } else {
+ console.error("Chat failed:", result.error);
+ // Set chat error flag to trigger test_completion=true on health checks
+ setChatError(true);
+ const errorMessage: Message = {
+ role: "assistant",
+ content: "Sorry, I encountered an error. Please try again.",
+ timestamp: new Date(),
+ };
+ setMessages((prev) => [...prev, errorMessage]);
+ }
+ } catch (error) {
+ console.error("Chat error:", error);
+ // Set chat error flag to trigger test_completion=true on health checks
+ setChatError(true);
+ const errorMessage: Message = {
+ role: "assistant",
+ content:
+ "Sorry, I couldn't connect to the chat service. Please try again.",
+ timestamp: new Date(),
+ };
+ setMessages((prev) => [...prev, errorMessage]);
+ }
+ }
+
+ setLoading(false);
+ };
+
+ const handleSubmit = async (e: React.FormEvent) => {
+ e.preventDefault();
+
+ // Check if there's an uploaded file and upload it first
+ let uploadedResponseId: string | null = null;
+ if (uploadedFile) {
+ // Upload the file first
+ const responseId = await handleFileUpload(uploadedFile);
+ // Clear the file after upload
+ setUploadedFile(null);
+
+ // If the upload resulted in a new conversation, store the response ID
+ if (responseId) {
+ uploadedResponseId = responseId;
+ setPreviousResponseIds((prev) => ({
+ ...prev,
+ [endpoint]: responseId,
+ }));
+ }
+ }
+
+ // Only send message if there's input text
+ if (input.trim() || uploadedFile) {
+ // Pass the responseId from upload (if any) to handleSendMessage
+ handleSendMessage(
+ !input.trim() ? FILE_CONFIRMATION : input,
+ uploadedResponseId || undefined,
+ );
+ }
+ };
+
+ const toggleFunctionCall = (functionCallId: string) => {
+ setExpandedFunctionCalls((prev) => {
+ const newSet = new Set(prev);
+ if (newSet.has(functionCallId)) {
+ newSet.delete(functionCallId);
+ } else {
+ newSet.add(functionCallId);
+ }
+ return newSet;
+ });
+ };
+
+ const handleForkConversation = (
+ messageIndex: number,
+ event?: React.MouseEvent,
+ ) => {
+ // Prevent any default behavior and stop event propagation
+ if (event) {
+ event.preventDefault();
+ event.stopPropagation();
+ }
+
+ // Set interaction state to prevent auto-scroll interference
+ setIsUserInteracting(true);
+ setIsForkingInProgress(true);
+
+ console.log("Fork conversation called for message index:", messageIndex);
+
+ // Get messages up to and including the selected assistant message
+ const messagesToKeep = messages.slice(0, messageIndex + 1);
+
+ // The selected message should be an assistant message (since fork button is only on assistant messages)
+ const forkedMessage = messages[messageIndex];
+ if (forkedMessage.role !== "assistant") {
+ console.error("Fork button should only be on assistant messages");
+ setIsUserInteracting(false);
+ setIsForkingInProgress(false);
+ return;
+ }
+
+ // For forking, we want to continue from the response_id of the assistant message we're forking from
+ // Since we don't store individual response_ids per message yet, we'll use the current conversation's response_id
+ // This means we're continuing the conversation thread from that point
+ const responseIdToForkFrom =
+ currentConversationId || previousResponseIds[endpoint];
+
+ // Create a new conversation by properly forking
+ setMessages(messagesToKeep);
+
+ // Use the chat context's fork method which handles creating a new conversation properly
+ if (forkFromResponse) {
+ forkFromResponse(responseIdToForkFrom || "");
+ } else {
+ // Fallback to manual approach
+ setCurrentConversationId(null); // This creates a new conversation thread
+
+ // Set the response_id we want to continue from as the previous response ID
+ // This tells the backend to continue the conversation from this point
+ setPreviousResponseIds((prev) => ({
+ ...prev,
+ [endpoint]: responseIdToForkFrom,
+ }));
+ }
+
+ console.log("Forked conversation with", messagesToKeep.length, "messages");
+
+ // Reset interaction state after a longer delay to ensure all effects complete
+ setTimeout(() => {
+ setIsUserInteracting(false);
+ setIsForkingInProgress(false);
+ console.log("Fork interaction complete, re-enabling auto effects");
+ }, 500);
+
+ // The original conversation remains unchanged in the sidebar
+ // This new forked conversation will get its own response_id when the user sends the next message
+ };
+
+ const handleSuggestionClick = (suggestion: string) => {
+ handleSendMessage(suggestion);
+ };
+
+ return (
+ <>
+ {/* Debug header - only show in debug mode */}
+ {isDebugMode && (
+ <div className="flex items-center justify-between p-6">
+ <div className="flex items-center gap-2"></div>
+ <div className="flex items-center gap-4">
+ {/* Async Mode Toggle */}
+ <div className="flex items-center gap-2 bg-muted/50 rounded-lg p-1">
+ <Button
+ variant={!asyncMode ? "default" : "ghost"}
+ size="sm"
+ onClick={() => setAsyncMode(false)}
+ className="h-7 text-xs"
+ >
+ Streaming Off
+ </Button>
+ <Button
+ variant={asyncMode ? "default" : "ghost"}
+ size="sm"
+ onClick={() => setAsyncMode(true)}
+ className="h-7 text-xs"
+ >
+ <Zap className="h-3 w-3 mr-1" />
+ Streaming On
+ </Button>
+ </div>
+ {/* Endpoint Toggle */}
+ <div className="flex items-center gap-2 bg-muted/50 rounded-lg p-1">
+ <Button
+ variant={endpoint === "chat" ? "default" : "ghost"}
+ size="sm"
+ onClick={() => handleEndpointChange("chat")}
+ className="h-7 text-xs"
+ >
+ Chat
+ </Button>
+ <Button
+ variant={endpoint === "langflow" ? "default" : "ghost"}
+ size="sm"
+ onClick={() => handleEndpointChange("langflow")}
+ className="h-7 text-xs"
+ >
+ Langflow
+ </Button>
+ </div>
+ </div>
+ </div>
+ )}
+
+ <StickToBottom.Content className="flex flex-col min-h-full overflow-x-hidden p-6">
+ <div className="flex flex-col place-self-center space-y-6 max-w-[960px] w-full mx-auto">
+ {messages.length === 0 && !streamingMessage ? (
+ <div className="flex items-center justify-center h-full text-muted-foreground">
+ <div className="text-center">
+ {isUploading ? (
+ <>
+ <Loader2 className="h-12 w-12 mx-auto mb-4 animate-spin" />
+ <p>Processing your document...</p>
+ <p className="text-sm mt-2">This may take a few moments</p>
+ </>
+ ) : null}
+ </div>
+ </div>
+ ) : (
+ <>
+ {messages.map((message, index) =>
+ message.role === "user"
+ ? (messages[index]?.content.match(FILES_REGEX)?.[0] ??
+ null) === null && (
+ <div
+ key={`${
+ message.role
+ }-${index}-${message.timestamp?.getTime()}`}
+ className="space-y-6 group"
+ >
+ <UserMessage
+ animate={
+ message.source
+ ? message.source !== "langflow"
+ : false
+ }
+ content={
+ index >= 2 &&
+ (messages[index - 2]?.content.match(
+ FILES_REGEX,
+ )?.[0] ??
+ undefined) &&
+ message.content === FILE_CONFIRMATION
+ ? undefined
+ : message.content
+ }
+ files={
+ index >= 2
+ ? (messages[index - 2]?.content.match(
+ FILES_REGEX,
+ )?.[0] ?? undefined)
+ : undefined
+ }
+ />
+ </div>
+ )
+ : message.role === "assistant" &&
+ (index < 1 ||
+ (messages[index - 1]?.content.match(FILES_REGEX)?.[0] ??
+ null) === null) && (
+ <div
+ key={`${
+ message.role
+ }-${index}-${message.timestamp?.getTime()}`}
+ className="space-y-6 group"
+ >
+ <AssistantMessage
+ content={message.content}
+ functionCalls={message.functionCalls}
+ messageIndex={index}
+ expandedFunctionCalls={expandedFunctionCalls}
+ onToggle={toggleFunctionCall}
+ showForkButton={endpoint === "chat"}
+ onFork={(e) => handleForkConversation(index, e)}
+ animate={false}
+ isInactive={index < messages.length - 1}
+ isInitialGreeting={
+ index === 0 &&
+ messages.length === 1 &&
+ message.content === "How can I assist?"
+ }
+ usage={message.usage}
+ />
+ </div>
+ ),
+ )}
+
+ {/* Streaming Message Display */}
+ {streamingMessage && (
+ <AssistantMessage
+ content={streamingMessage.content}
+ functionCalls={streamingMessage.functionCalls}
+ messageIndex={messages.length}
+ expandedFunctionCalls={expandedFunctionCalls}
+ onToggle={toggleFunctionCall}
+ delay={0.4}
+ isStreaming
+ isCompleted={false}
+ />
+ )}
+
+ {/* Waiting too long indicator */}
+ {waitingTooLong && !streamingMessage && loading && (
+ <div className="pl-10 space-y-2">
+ <div className="flex items-center gap-2 text-sm text-muted-foreground">
+ <Loader2 className="h-4 w-4 animate-spin" />
+ <span>The server is taking longer than expected...</span>
+ </div>
+ <p className="text-xs text-muted-foreground">
+ This may be due to high server load. The request will
+ timeout after 60 seconds.
+ </p>
+ </div>
+ )}
+ </>
+ )}
+ {!streamingMessage && (
+ <div className="pl-10">
+ <Nudges
+ nudges={loading ? [] : (nudges as string[])}
+ handleSuggestionClick={handleSuggestionClick}
+ />
+ </div>
+ )}
+ </div>
+ </StickToBottom.Content>
+ <div className="p-6 pt-0 max-w-[960px] mx-auto w-full">
+ {/* Input Area - Fixed at bottom */}
+ <ChatInput
+ ref={chatInputRef}
+ input={input}
+ loading={loading}
+ isUploading={isUploading}
+ selectedFilter={selectedFilter}
+ parsedFilterData={parsedFilterData}
+ uploadedFile={uploadedFile}
+ onSubmit={handleSubmit}
+ onChange={setInput}
+ onKeyDown={(e) => {
+ // Handle backspace for filter clearing
+ if (
+ e.key === "Backspace" &&
+ selectedFilter &&
+ input.trim() === ""
+ ) {
+ e.preventDefault();
+ if (isFilterHighlighted) {
+ // Second backspace - remove the filter
+ setConversationFilter(null);
+ setIsFilterHighlighted(false);
+ } else {
+ // First backspace - highlight the filter
+ setIsFilterHighlighted(true);
+ }
+ return;
+ }
+
+ // Handle Enter key for form submission
+ if (e.key === "Enter" && !e.shiftKey) {
+ e.preventDefault();
+ if (input.trim() && !loading) {
+ // Trigger form submission by finding the form and calling submit
+ const form = e.currentTarget.closest("form");
+ if (form) {
+ form.requestSubmit();
+ }
+ }
+ }
+ }}
+ onFilterSelect={handleFilterSelect}
+ onFilePickerClick={handleFilePickerClick}
+ onFileSelected={setUploadedFile}
+ setSelectedFilter={setConversationFilter}
+ setIsFilterHighlighted={setIsFilterHighlighted}
+ />
+ </div>
+ </>
+ );
}
export default function ProtectedChatPage() {
- return (
- <ProtectedRoute>
- <div className="flex w-full h-full overflow-hidden">
- <StickToBottom
- className="flex h-full flex-1 flex-col"
- resize="smooth"
- initial="instant"
- mass={1}
- >
- <ChatPage />
- </StickToBottom>
- </div>
- </ProtectedRoute>
- );
+ return (
+ <ProtectedRoute>
+ <div className="flex w-full h-full overflow-hidden">
+ <StickToBottom
+ className="flex h-full flex-1 flex-col"
+ resize="smooth"
+ initial="instant"
+ mass={1}
+ >
+ <ChatPage />
+ </StickToBottom>
+ </div>
+ </ProtectedRoute>
+ );
}
diff --git a/frontend/app/onboarding/_components/onboarding-content.tsx b/frontend/app/onboarding/_components/onboarding-content.tsx
index 4c791f5b9..c2b85a317 100644
--- a/frontend/app/onboarding/_components/onboarding-content.tsx
+++ b/frontend/app/onboarding/_components/onboarding-content.tsx
@@ -12,15 +12,16 @@ import type { Message, SelectedFilters } 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 { buildSearchPayloadFilters } from "@/lib/filter-normalization";
import { OnboardingStep } from "./onboarding-step";
import OnboardingUpload from "./onboarding-upload";
// Filters for OpenRAG documentation
const OPENRAG_DOCS_FILTERS: SelectedFilters = {
- data_sources: ["*"],
- document_types: ["*"],
- owners: ["*"],
+ data_sources: [],
+ document_types: [],
+ owners: [],
connector_types: ["system_default"],
};
@@ -179,7 +180,9 @@ export function OnboardingContent({
previousResponseId: responseId || undefined,
// Send both filter_id and filters (selections)
filter_id: filterToUse?.id,
- filters: openragDocsFilterId ? OPENRAG_DOCS_FILTERS : undefined,
+ filters: openragDocsFilterId
+ ? buildSearchPayloadFilters(OPENRAG_DOCS_FILTERS)
+ : undefined,
});
}, 1500);
};
diff --git a/frontend/hooks/useChatStreaming.ts b/frontend/hooks/useChatStreaming.ts
index 13144e910..1a6acf1ae 100644
--- a/frontend/hooks/useChatStreaming.ts
+++ b/frontend/hooks/useChatStreaming.ts
@@ -2,10 +2,11 @@ import { useRef, useState } from "react";
import type {
FunctionCall,
Message,
- SelectedFilters,
TokenUsage,
} from "@/app/chat/_types/types";
import { useChat } from "@/contexts/chat-context";
+import type { FilterInput } from "@/lib/filter-normalization";
+import { buildSearchPayloadFilters } from "@/lib/filter-normalization";
interface UseChatStreamingOptions {
endpoint?: string;
@@ -16,7 +17,7 @@ interface UseChatStreamingOptions {
interface SendMessageOptions {
prompt: string;
previousResponseId?: string;
- filters?: SelectedFilters;
+ filters?: FilterInput;
filter_id?: string;
limit?: number;
scoreThreshold?: number;
@@ -78,7 +79,7 @@ export function useChatStreaming({
prompt: string;
stream: boolean;
previous_response_id?: string;
- filters?: SelectedFilters;
+ filters?: FilterInput;
filter_id?: string;
limit?: number;
scoreThreshold?: number;
@@ -94,7 +95,10 @@ export function useChatStreaming({
}
if (filters) {
- requestBody.filters = filters;
+ const payloadFilters = buildSearchPayloadFilters(filters);
+ if (payloadFilters) {
+ requestBody.filters = payloadFilters;
+ }
}
if (filter_id) {
diff --git a/frontend/lib/filter-normalization.ts b/frontend/lib/filter-normalization.ts
new file mode 100644
index 000000000..7b1aaaaff
--- /dev/null
+++ b/frontend/lib/filter-normalization.ts
@@ -0,0 +1,53 @@
+export interface FilterInput {
+ data_sources?: string[];
+ document_types?: string[];
+ owners?: string[];
+ connector_types?: string[];
+}
+
+export interface NormalizedSelectedFilters {
+ data_sources: string[];
+ document_types: string[];
+ owners: string[];
+ connector_types: string[];
+}
+
+function normalizeFilterDimension(values?: string[]): string[] {
+ if (!values || values.includes("*")) {
+ return [];
+ }
+ return values;
+}
+
+export function normalizeSelectedFilters(
+ filters?: FilterInput,
+): NormalizedSelectedFilters {
+ return {
+ data_sources: normalizeFilterDimension(filters?.data_sources),
+ document_types: normalizeFilterDimension(filters?.document_types),
+ owners: normalizeFilterDimension(filters?.owners),
+ connector_types: normalizeFilterDimension(filters?.connector_types),
+ };
+}
+
+export function buildSearchPayloadFilters(
+ filters?: FilterInput,
+): FilterInput | undefined {
+ const normalized = normalizeSelectedFilters(filters);
+ const payloadFilters: FilterInput = {};
+
+ if (normalized.data_sources.length > 0) {
+ payloadFilters.data_sources = normalized.data_sources;
+ }
+ if (normalized.document_types.length > 0) {
+ payloadFilters.document_types = normalized.document_types;
+ }
+ if (normalized.owners.length > 0) {
+ payloadFilters.owners = normalized.owners;
+ }
+ if (normalized.connector_types.length > 0) {
+ payloadFilters.connector_types = normalized.connector_types;
+ }
+
+ return Object.keys(payloadFilters).length > 0 ? payloadFilters : undefined;
+}
diff --git a/src/main.py b/src/main.py
index d1bd9626b..137104722 100644
--- a/src/main.py
+++ b/src/main.py
@@ -42,7 +42,14 @@ from api import (
from api.connector_router import ConnectorRouter
from services.api_key_service import APIKeyService
from api import keys as api_keys
-from api.v1 import chat as v1_chat, search as v1_search, documents as v1_documents, settings as v1_settings, models as v1_models, knowledge_filters as v1_knowledge_filters
+from api.v1 import (
+ chat as v1_chat,
+ search as v1_search,
+ documents as v1_documents,
+ settings as v1_settings,
+ models as v1_models,
+ knowledge_filters as v1_knowledge_filters,
+)
# Configuration and setup
from config.settings import (
@@ -87,14 +94,23 @@ logger = get_logger(__name__)
# Files to exclude from startup ingestion
EXCLUDED_INGESTION_FILES = {"warmup_ocr.pdf"}
+
async def wait_for_opensearch():
"""Wait for OpenSearch to be ready, delegating to the shared utility."""
- from utils.opensearch_utils import wait_for_opensearch as _wait_for_opensearch, OpenSearchNotReadyError
+ from utils.opensearch_utils import (
+ wait_for_opensearch as _wait_for_opensearch,
+ OpenSearchNotReadyError,
+ )
+
try:
await _wait_for_opensearch(clients.opensearch)
- await TelemetryClient.send_event(Category.OPENSEARCH_SETUP, MessageId.ORB_OS_CONN_ESTABLISHED)
+ await TelemetryClient.send_event(
+ Category.OPENSEARCH_SETUP, MessageId.ORB_OS_CONN_ESTABLISHED
+ )
except OpenSearchNotReadyError:
- await TelemetryClient.send_event(Category.OPENSEARCH_SETUP, MessageId.ORB_OS_TIMEOUT)
+ await TelemetryClient.send_event(
+ Category.OPENSEARCH_SETUP, MessageId.ORB_OS_TIMEOUT
+ )
raise
@@ -139,7 +155,9 @@ async def _ensure_opensearch_index():
"dimension"
],
)
- await TelemetryClient.send_event(Category.OPENSEARCH_INDEX, MessageId.ORB_OS_INDEX_CREATED)
+ await TelemetryClient.send_event(
+ Category.OPENSEARCH_INDEX, MessageId.ORB_OS_INDEX_CREATED
+ )
except Exception as e:
logger.error(
@@ -147,7 +165,9 @@ async def _ensure_opensearch_index():
error=str(e),
index_name=get_index_name(),
)
- await TelemetryClient.send_event(Category.OPENSEARCH_INDEX, MessageId.ORB_OS_INDEX_CREATE_FAIL)
+ await TelemetryClient.send_event(
+ Category.OPENSEARCH_INDEX, MessageId.ORB_OS_INDEX_CREATE_FAIL
+ )
# Don't raise the exception to avoid breaking the initialization
# The service can still function, document operations might fail later
@@ -168,7 +188,7 @@ async def init_index():
dynamic_index_body = await create_dynamic_index_body(
embedding_model,
provider=embedding_provider,
- endpoint=getattr(embedding_provider_config, "endpoint", None)
+ endpoint=getattr(embedding_provider_config, "endpoint", None),
)
# Create documents index
@@ -182,14 +202,18 @@ async def init_index():
index_name=index_name,
embedding_model=embedding_model,
)
- await TelemetryClient.send_event(Category.OPENSEARCH_INDEX, MessageId.ORB_OS_INDEX_CREATED)
+ await TelemetryClient.send_event(
+ Category.OPENSEARCH_INDEX, MessageId.ORB_OS_INDEX_CREATED
+ )
else:
logger.info(
"Index already exists, skipping creation",
index_name=index_name,
embedding_model=embedding_model,
)
- await TelemetryClient.send_event(Category.OPENSEARCH_INDEX, MessageId.ORB_OS_INDEX_EXISTS)
+ await TelemetryClient.send_event(
+ Category.OPENSEARCH_INDEX, MessageId.ORB_OS_INDEX_EXISTS
+ )
# Create knowledge filters index
knowledge_filter_index_name = "knowledge_filters"
@@ -210,14 +234,19 @@ async def init_index():
}
}
- if not await clients.opensearch.indices.exists(index=knowledge_filter_index_name):
+ if not await clients.opensearch.indices.exists(
+ index=knowledge_filter_index_name
+ ):
await clients.opensearch.indices.create(
index=knowledge_filter_index_name, body=knowledge_filter_index_body
)
logger.info(
- "Created knowledge filters index", index_name=knowledge_filter_index_name
+ "Created knowledge filters index",
+ index_name=knowledge_filter_index_name,
+ )
+ await TelemetryClient.send_event(
+ Category.OPENSEARCH_INDEX, MessageId.ORB_OS_KF_INDEX_CREATED
)
- await TelemetryClient.send_event(Category.OPENSEARCH_INDEX, MessageId.ORB_OS_KF_INDEX_CREATED)
else:
logger.info(
"Knowledge filters index already exists, skipping creation",
@@ -229,9 +258,7 @@ async def init_index():
await clients.opensearch.indices.create(
index=API_KEYS_INDEX_NAME, body=API_KEYS_INDEX_BODY
)
- logger.info(
- "Created API keys index", index_name=API_KEYS_INDEX_NAME
- )
+ logger.info("Created API keys index", index_name=API_KEYS_INDEX_NAME)
else:
logger.info(
"API keys index already exists, skipping creation",
@@ -244,11 +271,13 @@ async def init_index():
except Exception as e:
error_msg = str(e).lower()
if "disk usage exceeded" in error_msg or "flood-stage watermark" in error_msg:
- logger.error("OpenSearch disk usage exceeded flood-stage watermark. Index creation failed.")
- raise Exception(
- "OpenSearch disk space is full (flood-stage watermark exceeded). "
- "Please free up disk space on your Docker volume or host machine to continue."
- ) from e
+ logger.error(
+ "OpenSearch disk usage exceeded flood-stage watermark. Index creation failed."
+ )
+ raise Exception(
+ "OpenSearch disk space is full (flood-stage watermark exceeded). "
+ "Please free up disk space on your Docker volume or host machine to continue."
+ ) from e
raise e
@@ -301,7 +330,9 @@ def generate_jwt_keys():
logger.info("Generated RSA keys for JWT signing")
except subprocess.CalledProcessError as e:
logger.error("Failed to generate RSA keys", error=str(e))
- TelemetryClient.send_event_sync(Category.SERVICE_INITIALIZATION, MessageId.ORB_SVC_JWT_KEY_FAIL)
+ TelemetryClient.send_event_sync(
+ Category.SERVICE_INITIALIZATION, MessageId.ORB_SVC_JWT_KEY_FAIL
+ )
raise
else:
# Ensure correct permissions on existing keys
@@ -348,7 +379,9 @@ async def ingest_default_documents_when_ready(
disable_langflow_ingest=DISABLE_INGEST_WITH_LANGFLOW,
ingest_source=DEFAULT_DOCS_INGEST_SOURCE,
)
- await TelemetryClient.send_event(Category.DOCUMENT_INGESTION, MessageId.ORB_DOC_DEFAULT_START)
+ await TelemetryClient.send_event(
+ Category.DOCUMENT_INGESTION, MessageId.ORB_DOC_DEFAULT_START
+ )
use_url_ingest = _should_use_url_default_docs_ingest()
if (
DEFAULT_DOCS_INGEST_SOURCE == "url"
@@ -365,12 +398,16 @@ async def ingest_default_documents_when_ready(
docs_url=DEFAULT_DOCS_URL,
crawl_depth=DEFAULT_DOCS_CRAWL_DEPTH,
)
- await TelemetryClient.send_event(Category.DOCUMENT_INGESTION, MessageId.ORB_DOC_DEFAULT_COMPLETE)
+ await TelemetryClient.send_event(
+ Category.DOCUMENT_INGESTION, MessageId.ORB_DOC_DEFAULT_COMPLETE
+ )
return
base_dir = _get_documents_dir()
if not os.path.isdir(base_dir):
- raise FileNotFoundError(f"Default documents directory not found: {base_dir}")
+ raise FileNotFoundError(
+ f"Default documents directory not found: {base_dir}"
+ )
# Collect files recursively, excluding warmup files
file_paths = [
@@ -392,11 +429,15 @@ async def ingest_default_documents_when_ready(
langflow_file_service, session_manager, task_service, file_paths
)
- await TelemetryClient.send_event(Category.DOCUMENT_INGESTION, MessageId.ORB_DOC_DEFAULT_COMPLETE)
+ await TelemetryClient.send_event(
+ Category.DOCUMENT_INGESTION, MessageId.ORB_DOC_DEFAULT_COMPLETE
+ )
except Exception as e:
logger.error("Default documents ingestion failed", error=str(e))
- await TelemetryClient.send_event(Category.DOCUMENT_INGESTION, MessageId.ORB_DOC_DEFAULT_FAILED)
+ await TelemetryClient.send_event(
+ Category.DOCUMENT_INGESTION, MessageId.ORB_DOC_DEFAULT_FAILED
+ )
raise
@@ -465,7 +506,9 @@ async def _ingest_default_documents_langflow(
)
-async def _ingest_default_documents_url(session_manager, docs_url: str, crawl_depth: int):
+async def _ingest_default_documents_url(
+ session_manager, docs_url: str, crawl_depth: int
+):
"""Ingest default docs by crawling a URL with the Langflow URL ingest flow."""
if not LANGFLOW_URL_INGEST_FLOW_ID:
raise ValueError("LANGFLOW_URL_INGEST_FLOW_ID is not configured")
@@ -479,7 +522,9 @@ async def _ingest_default_documents_url(session_manager, docs_url: str, crawl_de
anonymous_user = AnonymousUser()
effective_jwt = None
if session_manager:
- session_manager.get_user_opensearch_client(anonymous_user.user_id, effective_jwt)
+ session_manager.get_user_opensearch_client(
+ anonymous_user.user_id, effective_jwt
+ )
if hasattr(session_manager, "_anonymous_jwt"):
effective_jwt = session_manager._anonymous_jwt
@@ -502,7 +547,9 @@ async def _ingest_default_documents_url(session_manager, docs_url: str, crawl_de
"X-Langflow-Global-Var-OWNER_NAME": str(anonymous_user.name),
"X-Langflow-Global-Var-OWNER_EMAIL": str(anonymous_user.email),
"X-Langflow-Global-Var-CONNECTOR_TYPE": "system_default",
- "X-Langflow-Global-Var-SELECTED_EMBEDDING_MODEL": str(config.knowledge.embedding_model),
+ "X-Langflow-Global-Var-SELECTED_EMBEDDING_MODEL": str(
+ config.knowledge.embedding_model
+ ),
}
add_provider_credentials_to_headers(headers, config)
@@ -539,7 +586,9 @@ async def _delete_existing_default_docs(session_manager):
anonymous_user = AnonymousUser()
effective_jwt = None
if session_manager:
- session_manager.get_user_opensearch_client(anonymous_user.user_id, effective_jwt)
+ session_manager.get_user_opensearch_client(
+ anonymous_user.user_id, effective_jwt
+ )
if hasattr(session_manager, "_anonymous_jwt"):
effective_jwt = session_manager._anonymous_jwt
@@ -605,8 +654,8 @@ async def _reingest_default_docs_on_upgrade_if_needed(
if _should_use_url_default_docs_ingest():
# Refresh signature metadata after upgrade reingestion so startup
# signature checks don't trigger an immediate duplicate ingest.
- config.onboarding.openrag_docs_remote_signature = await _get_remote_docs_signature(
- DEFAULT_DOCS_URL
+ config.onboarding.openrag_docs_remote_signature = (
+ await _get_remote_docs_signature(DEFAULT_DOCS_URL)
)
else:
config.onboarding.openrag_docs_remote_signature = None
@@ -637,8 +686,13 @@ async def _get_remote_docs_signature(docs_url: str):
etag = (head_response.headers.get("etag") or "").strip()
last_modified = (head_response.headers.get("last-modified") or "").strip()
- if etag or last_modified:
- return f"etag={etag}|last_modified={last_modified}"
+ if etag:
+ # Prefer ETag when available: it is typically the strongest
+ # cache validator and stays stable if extra cache headers
+ # appear/disappear without content changes.
+ return f"etag={etag}"
+ if last_modified:
+ return f"last_modified={last_modified}"
# HEAD has no body. If cache headers are missing, fetch the page body.
get_response = await client.get(docs_url)
@@ -721,7 +775,9 @@ async def refresh_default_openrag_docs(
return False
previous_signature = config.onboarding.openrag_docs_remote_signature
- should_refresh = force or (signature is not None and signature != previous_signature)
+ should_refresh = force or (
+ signature is not None and signature != previous_signature
+ )
if not should_refresh:
logger.info(
"OpenRAG docs refresh skipped: remote signature unchanged",
@@ -781,6 +837,7 @@ async def refresh_default_openrag_docs(
)
raise
+
async def health_check(request: Request):
"""Simple liveness probe: Indicates that the OpenRAG Backend service is online and running."""
return JSONResponse({"status": "ok"}, status_code=200)
@@ -805,9 +862,8 @@ async def opensearch_health_ready(request):
status_code=503,
)
-async def _ingest_default_documents_openrag(
- document_service, task_service, file_paths
-):
+
+async def _ingest_default_documents_openrag(document_service, task_service, file_paths):
"""Ingest default documents using traditional OpenRAG processor."""
logger.info(
"Using traditional OpenRAG ingestion for default documents",
@@ -826,9 +882,7 @@ async def _ingest_default_documents_openrag(
is_sample_data=True, # Mark as sample data
)
- task_id = await task_service.create_custom_task(
- "anonymous", file_paths, processor
- )
+ task_id = await task_service.create_custom_task("anonymous", file_paths, processor)
logger.info(
"Started traditional OpenRAG ingestion task",
task_id=task_id,
@@ -869,26 +923,41 @@ async def _update_mcp_servers_with_provider_credentials(services):
# Add anonymous user details
anonymous_user = AnonymousUser()
global_vars["OWNER"] = anonymous_user.user_id # "anonymous"
- global_vars["OWNER_NAME"] = f'"{anonymous_user.name}"' # "Anonymous User" (quoted for spaces)
+ global_vars["OWNER_NAME"] = (
+ f'"{anonymous_user.name}"' # "Anonymous User" (quoted for spaces)
+ )
global_vars["OWNER_EMAIL"] = anonymous_user.email # "anonymous@localhost"
- logger.info("Added anonymous JWT and user details to MCP servers for no-auth mode")
+ logger.info(
+ "Added anonymous JWT and user details to MCP servers for no-auth mode"
+ )
if global_vars:
- result = await auth_service.langflow_mcp_service.update_mcp_servers_with_global_vars(global_vars)
- logger.info("Updated MCP servers with provider credentials at startup", **result)
+ result = await auth_service.langflow_mcp_service.update_mcp_servers_with_global_vars(
+ global_vars
+ )
+ logger.info(
+ "Updated MCP servers with provider credentials at startup", **result
+ )
else:
- logger.debug("No provider credentials configured, skipping MCP server update")
+ logger.debug(
+ "No provider credentials configured, skipping MCP server update"
+ )
except Exception as e:
- logger.warning("Failed to update MCP servers with provider credentials at startup", error=str(e))
+ logger.warning(
+ "Failed to update MCP servers with provider credentials at startup",
+ error=str(e),
+ )
# Don't fail startup if MCP update fails
async def startup_tasks(services):
"""Startup tasks"""
logger.info("Starting startup tasks")
- await TelemetryClient.send_event(Category.APPLICATION_STARTUP, MessageId.ORB_APP_START_INIT)
+ await TelemetryClient.send_event(
+ Category.APPLICATION_STARTUP, MessageId.ORB_APP_START_INIT
+ )
# Only initialize basic OpenSearch connection, not the index
# Index will be created after onboarding when we know the embedding model
await wait_for_opensearch()
@@ -966,24 +1035,37 @@ async def startup_tasks(services):
logger.info(
f"Detected reset flows: {', '.join(reset_flows)}. Reapplying all settings."
)
- await TelemetryClient.send_event(Category.FLOW_OPERATIONS, MessageId.ORB_FLOW_RESET_DETECTED)
+ await TelemetryClient.send_event(
+ Category.FLOW_OPERATIONS, MessageId.ORB_FLOW_RESET_DETECTED
+ )
from api.settings import reapply_all_settings
+
await reapply_all_settings(session_manager=services["session_manager"])
- logger.info("Successfully reapplied settings after detecting flow resets")
- await TelemetryClient.send_event(Category.FLOW_OPERATIONS, MessageId.ORB_FLOW_SETTINGS_REAPPLIED)
+ logger.info(
+ "Successfully reapplied settings after detecting flow resets"
+ )
+ await TelemetryClient.send_event(
+ Category.FLOW_OPERATIONS, MessageId.ORB_FLOW_SETTINGS_REAPPLIED
+ )
else:
- logger.info("No flows detected as reset, skipping settings reapplication")
+ logger.info(
+ "No flows detected as reset, skipping settings reapplication"
+ )
else:
logger.debug("Configuration not yet edited, skipping flow reset check")
except Exception as e:
logger.error(f"Failed to check flows reset or reapply settings: {str(e)}")
- await TelemetryClient.send_event(Category.FLOW_OPERATIONS, MessageId.ORB_FLOW_RESET_CHECK_FAIL)
+ await TelemetryClient.send_event(
+ Category.FLOW_OPERATIONS, MessageId.ORB_FLOW_RESET_CHECK_FAIL
+ )
# Don't fail startup if this check fails
async def initialize_services():
"""Initialize all services and their dependencies"""
- await TelemetryClient.send_event(Category.SERVICE_INITIALIZATION, MessageId.ORB_SVC_INIT_START)
+ await TelemetryClient.send_event(
+ Category.SERVICE_INITIALIZATION, MessageId.ORB_SVC_INIT_START
+ )
# Generate JWT keys if they don't exist
generate_jwt_keys()
@@ -992,7 +1074,9 @@ async def initialize_services():
await clients.initialize()
except Exception as e:
logger.error("Failed to initialize clients", error=str(e))
- await TelemetryClient.send_event(Category.SERVICE_INITIALIZATION, MessageId.ORB_SVC_OS_CLIENT_FAIL)
+ await TelemetryClient.send_event(
+ Category.SERVICE_INITIALIZATION, MessageId.ORB_SVC_OS_CLIENT_FAIL
+ )
raise
# Initialize session manager
@@ -1050,11 +1134,15 @@ async def initialize_services():
logger.warning(
"Failed to load persisted connections on startup", error=str(e)
)
- await TelemetryClient.send_event(Category.CONNECTOR_OPERATIONS, MessageId.ORB_CONN_LOAD_FAILED)
+ await TelemetryClient.send_event(
+ Category.CONNECTOR_OPERATIONS, MessageId.ORB_CONN_LOAD_FAILED
+ )
else:
logger.info("[CONNECTORS] Skipping connection loading in no-auth mode")
- await TelemetryClient.send_event(Category.SERVICE_INITIALIZATION, MessageId.ORB_SVC_INIT_SUCCESS)
+ await TelemetryClient.send_event(
+ Category.SERVICE_INITIALIZATION, MessageId.ORB_SVC_INIT_SUCCESS
+ )
langflow_file_service = LangflowFileService()
@@ -1089,152 +1177,449 @@ async def create_app():
# Register route handlers — auth and service injection done via FastAPI Depends() in each handler
# Langflow Files endpoints
- app.add_api_route("/langflow/files/upload", langflow_files.upload_user_file, methods=["POST"], tags=["internal"])
- app.add_api_route("/langflow/ingest", langflow_files.run_ingestion, methods=["POST"], tags=["internal"])
- app.add_api_route("/langflow/files", langflow_files.delete_user_files, methods=["DELETE"], tags=["internal"])
- app.add_api_route("/langflow/upload_ingest", langflow_files.upload_and_ingest_user_file, methods=["POST"], tags=["internal"])
+ app.add_api_route(
+ "/langflow/files/upload",
+ langflow_files.upload_user_file,
+ methods=["POST"],
+ tags=["internal"],
+ )
+ app.add_api_route(
+ "/langflow/ingest",
+ langflow_files.run_ingestion,
+ methods=["POST"],
+ tags=["internal"],
+ )
+ app.add_api_route(
+ "/langflow/files",
+ langflow_files.delete_user_files,
+ methods=["DELETE"],
+ tags=["internal"],
+ )
+ app.add_api_route(
+ "/langflow/upload_ingest",
+ langflow_files.upload_and_ingest_user_file,
+ methods=["POST"],
+ tags=["internal"],
+ )
# Upload endpoints
- app.add_api_route("/upload_context", upload.upload_context, methods=["POST"], tags=["internal"])
- app.add_api_route("/upload_path", upload.upload_path, methods=["POST"], tags=["internal"])
- app.add_api_route("/upload_options", upload.upload_options, methods=["GET"], tags=["internal"])
- app.add_api_route("/upload_bucket", upload.upload_bucket, methods=["POST"], tags=["internal"])
+ app.add_api_route(
+ "/upload_context", upload.upload_context, methods=["POST"], tags=["internal"]
+ )
+ app.add_api_route(
+ "/upload_path", upload.upload_path, methods=["POST"], tags=["internal"]
+ )
+ app.add_api_route(
+ "/upload_options", upload.upload_options, methods=["GET"], tags=["internal"]
+ )
+ app.add_api_route(
+ "/upload_bucket", upload.upload_bucket, methods=["POST"], tags=["internal"]
+ )
# Task endpoints
- app.add_api_route("/tasks/{task_id}", tasks.task_status, methods=["GET"], tags=["internal"])
+ app.add_api_route(
+ "/tasks/{task_id}", tasks.task_status, methods=["GET"], tags=["internal"]
+ )
app.add_api_route("/tasks", tasks.all_tasks, methods=["GET"], tags=["internal"])
- app.add_api_route("/tasks/{task_id}/cancel", tasks.cancel_task, methods=["POST"], tags=["internal"])
+ app.add_api_route(
+ "/tasks/{task_id}/cancel",
+ tasks.cancel_task,
+ methods=["POST"],
+ tags=["internal"],
+ )
# Search endpoint
app.add_api_route("/search", search.search, methods=["POST"], tags=["internal"])
# Knowledge Filter endpoints
- app.add_api_route("/knowledge-filter", knowledge_filter.create_knowledge_filter, methods=["POST"], tags=["internal"])
- app.add_api_route("/knowledge-filter/search", knowledge_filter.search_knowledge_filters, methods=["POST"], tags=["internal"])
- app.add_api_route("/knowledge-filter/{filter_id}", knowledge_filter.get_knowledge_filter, methods=["GET"], tags=["internal"])
- app.add_api_route("/knowledge-filter/{filter_id}", knowledge_filter.update_knowledge_filter, methods=["PUT"], tags=["internal"])
- app.add_api_route("/knowledge-filter/{filter_id}", knowledge_filter.delete_knowledge_filter, methods=["DELETE"], tags=["internal"])
+ app.add_api_route(
+ "/knowledge-filter",
+ knowledge_filter.create_knowledge_filter,
+ methods=["POST"],
+ tags=["internal"],
+ )
+ app.add_api_route(
+ "/knowledge-filter/search",
+ knowledge_filter.search_knowledge_filters,
+ methods=["POST"],
+ tags=["internal"],
+ )
+ app.add_api_route(
+ "/knowledge-filter/{filter_id}",
+ knowledge_filter.get_knowledge_filter,
+ methods=["GET"],
+ tags=["internal"],
+ )
+ app.add_api_route(
+ "/knowledge-filter/{filter_id}",
+ knowledge_filter.update_knowledge_filter,
+ methods=["PUT"],
+ tags=["internal"],
+ )
+ app.add_api_route(
+ "/knowledge-filter/{filter_id}",
+ knowledge_filter.delete_knowledge_filter,
+ methods=["DELETE"],
+ tags=["internal"],
+ )
# Knowledge Filter Subscription endpoints
- app.add_api_route("/knowledge-filter/{filter_id}/subscribe", knowledge_filter.subscribe_to_knowledge_filter, methods=["POST"], tags=["internal"])
- app.add_api_route("/knowledge-filter/{filter_id}/subscriptions", knowledge_filter.list_knowledge_filter_subscriptions, methods=["GET"], tags=["internal"])
- app.add_api_route("/knowledge-filter/{filter_id}/subscribe/{subscription_id}", knowledge_filter.cancel_knowledge_filter_subscription, methods=["DELETE"], tags=["internal"])
+ app.add_api_route(
+ "/knowledge-filter/{filter_id}/subscribe",
+ knowledge_filter.subscribe_to_knowledge_filter,
+ methods=["POST"],
+ tags=["internal"],
+ )
+ app.add_api_route(
+ "/knowledge-filter/{filter_id}/subscriptions",
+ knowledge_filter.list_knowledge_filter_subscriptions,
+ methods=["GET"],
+ tags=["internal"],
+ )
+ app.add_api_route(
+ "/knowledge-filter/{filter_id}/subscribe/{subscription_id}",
+ knowledge_filter.cancel_knowledge_filter_subscription,
+ methods=["DELETE"],
+ tags=["internal"],
+ )
# Knowledge Filter Webhook endpoint (no auth required - called by OpenSearch)
- app.add_api_route("/knowledge-filter/{filter_id}/webhook/{subscription_id}", knowledge_filter.knowledge_filter_webhook, methods=["POST"], tags=["internal"])
+ app.add_api_route(
+ "/knowledge-filter/{filter_id}/webhook/{subscription_id}",
+ knowledge_filter.knowledge_filter_webhook,
+ methods=["POST"],
+ tags=["internal"],
+ )
# Chat endpoints
app.add_api_route("/chat", chat.chat_endpoint, methods=["POST"], tags=["internal"])
- app.add_api_route("/langflow", chat.langflow_endpoint, methods=["POST"], tags=["internal"])
+ app.add_api_route(
+ "/langflow", chat.langflow_endpoint, methods=["POST"], tags=["internal"]
+ )
# Chat history endpoints
- app.add_api_route("/chat/history", chat.chat_history_endpoint, methods=["GET"], tags=["internal"])
- app.add_api_route("/langflow/history", chat.langflow_history_endpoint, methods=["GET"], tags=["internal"])
+ app.add_api_route(
+ "/chat/history", chat.chat_history_endpoint, methods=["GET"], tags=["internal"]
+ )
+ app.add_api_route(
+ "/langflow/history",
+ chat.langflow_history_endpoint,
+ methods=["GET"],
+ tags=["internal"],
+ )
# Session deletion endpoint
- app.add_api_route("/sessions/{session_id}", chat.delete_session_endpoint, methods=["DELETE"], tags=["internal"])
+ app.add_api_route(
+ "/sessions/{session_id}",
+ chat.delete_session_endpoint,
+ methods=["DELETE"],
+ tags=["internal"],
+ )
# Authentication endpoints
app.add_api_route("/auth/init", auth.auth_init, methods=["POST"], tags=["internal"])
- app.add_api_route("/auth/callback", auth.auth_callback, methods=["POST"], tags=["internal"])
+ app.add_api_route(
+ "/auth/callback", auth.auth_callback, methods=["POST"], tags=["internal"]
+ )
app.add_api_route("/auth/me", auth.auth_me, methods=["GET"], tags=["internal"])
- app.add_api_route("/auth/logout", auth.auth_logout, methods=["POST"], tags=["internal"])
+ app.add_api_route(
+ "/auth/logout", auth.auth_logout, methods=["POST"], tags=["internal"]
+ )
# Connector endpoints
- app.add_api_route("/connectors", connectors.list_connectors, methods=["GET"], tags=["internal"])
- app.add_api_route("/connectors/{connector_type}/sync", connectors.connector_sync, methods=["POST"], tags=["internal"])
- app.add_api_route("/connectors/sync-all", connectors.sync_all_connectors, methods=["POST"], tags=["internal"])
- app.add_api_route("/connectors/{connector_type}/status", connectors.connector_status, methods=["GET"], tags=["internal"])
- app.add_api_route("/connectors/{connector_type}/token", connectors.connector_token, methods=["GET"], tags=["internal"])
- app.add_api_route("/connectors/{connector_type}/disconnect", connectors.connector_disconnect, methods=["DELETE"], tags=["internal"])
- app.add_api_route("/connectors/{connector_type}/webhook", connectors.connector_webhook, methods=["POST", "GET"], tags=["internal"])
+ app.add_api_route(
+ "/connectors", connectors.list_connectors, methods=["GET"], tags=["internal"]
+ )
+ app.add_api_route(
+ "/connectors/{connector_type}/sync",
+ connectors.connector_sync,
+ methods=["POST"],
+ tags=["internal"],
+ )
+ app.add_api_route(
+ "/connectors/sync-all",
+ connectors.sync_all_connectors,
+ methods=["POST"],
+ tags=["internal"],
+ )
+ app.add_api_route(
+ "/connectors/{connector_type}/status",
+ connectors.connector_status,
+ methods=["GET"],
+ tags=["internal"],
+ )
+ app.add_api_route(
+ "/connectors/{connector_type}/token",
+ connectors.connector_token,
+ methods=["GET"],
+ tags=["internal"],
+ )
+ app.add_api_route(
+ "/connectors/{connector_type}/disconnect",
+ connectors.connector_disconnect,
+ methods=["DELETE"],
+ tags=["internal"],
+ )
+ app.add_api_route(
+ "/connectors/{connector_type}/webhook",
+ connectors.connector_webhook,
+ methods=["POST", "GET"],
+ tags=["internal"],
+ )
# Document endpoints
- app.add_api_route("/documents/check-filename", documents.check_filename_exists, methods=["GET"], tags=["internal"])
- app.add_api_route("/documents/delete-by-filename", documents.delete_documents_by_filename, methods=["POST"], tags=["internal"])
+ app.add_api_route(
+ "/documents/check-filename",
+ documents.check_filename_exists,
+ methods=["GET"],
+ tags=["internal"],
+ )
+ app.add_api_route(
+ "/documents/delete-by-filename",
+ documents.delete_documents_by_filename,
+ methods=["POST"],
+ tags=["internal"],
+ )
# OIDC endpoints
- app.add_api_route("/.well-known/openid-configuration", oidc.oidc_discovery, methods=["GET"], tags=["internal"])
- app.add_api_route("/auth/jwks", oidc.jwks_endpoint, methods=["GET"], tags=["internal"])
- app.add_api_route("/auth/introspect", oidc.token_introspection, methods=["POST"], tags=["internal"])
+ app.add_api_route(
+ "/.well-known/openid-configuration",
+ oidc.oidc_discovery,
+ methods=["GET"],
+ tags=["internal"],
+ )
+ app.add_api_route(
+ "/auth/jwks", oidc.jwks_endpoint, methods=["GET"], tags=["internal"]
+ )
+ app.add_api_route(
+ "/auth/introspect",
+ oidc.token_introspection,
+ methods=["POST"],
+ tags=["internal"],
+ )
# Settings endpoints
- app.add_api_route("/settings", settings.get_settings, methods=["GET"], tags=["internal"])
- app.add_api_route("/settings", settings.update_settings, methods=["POST"], tags=["internal"])
- app.add_api_route("/onboarding/state", settings.update_onboarding_state, methods=["POST"], tags=["internal"])
- app.add_api_route("/openrag-docs/refresh", settings.refresh_openrag_docs, methods=["POST"], tags=["internal"])
+ app.add_api_route(
+ "/settings", settings.get_settings, methods=["GET"], tags=["internal"]
+ )
+ app.add_api_route(
+ "/settings", settings.update_settings, methods=["POST"], tags=["internal"]
+ )
+ app.add_api_route(
+ "/onboarding/state",
+ settings.update_onboarding_state,
+ methods=["POST"],
+ tags=["internal"],
+ )
+ app.add_api_route(
+ "/openrag-docs/refresh",
+ settings.refresh_openrag_docs,
+ methods=["POST"],
+ tags=["internal"],
+ )
# Provider health check endpoint
- app.add_api_route("/provider/health", provider_health.check_provider_health, methods=["GET"], tags=["internal"])
+ app.add_api_route(
+ "/provider/health",
+ provider_health.check_provider_health,
+ methods=["GET"],
+ tags=["internal"],
+ )
# Health check endpoints
app.add_api_route("/health", health_check, methods=["GET"], tags=["internal"])
- app.add_api_route("/search/health", opensearch_health_ready, methods=["GET"], tags=["internal"])
+ app.add_api_route(
+ "/search/health", opensearch_health_ready, methods=["GET"], tags=["internal"]
+ )
# Models endpoints
- app.add_api_route("/models/openai", models.get_openai_models, methods=["POST"], tags=["internal"])
- app.add_api_route("/models/anthropic", models.get_anthropic_models, methods=["POST"], tags=["internal"])
- app.add_api_route("/models/ollama", models.get_ollama_models, methods=["GET"], tags=["internal"])
- app.add_api_route("/models/ibm", models.get_ibm_models, methods=["POST"], tags=["internal"])
+ app.add_api_route(
+ "/models/openai", models.get_openai_models, methods=["POST"], tags=["internal"]
+ )
+ app.add_api_route(
+ "/models/anthropic",
+ models.get_anthropic_models,
+ methods=["POST"],
+ tags=["internal"],
+ )
+ app.add_api_route(
+ "/models/ollama", models.get_ollama_models, methods=["GET"], tags=["internal"]
+ )
+ app.add_api_route(
+ "/models/ibm", models.get_ibm_models, methods=["POST"], tags=["internal"]
+ )
# Onboarding endpoints
- app.add_api_route("/onboarding", settings.onboarding, methods=["POST"], tags=["internal"])
- app.add_api_route("/onboarding/rollback", settings.rollback_onboarding, methods=["POST"], tags=["internal"])
+ app.add_api_route(
+ "/onboarding", settings.onboarding, methods=["POST"], tags=["internal"]
+ )
+ app.add_api_route(
+ "/onboarding/rollback",
+ settings.rollback_onboarding,
+ methods=["POST"],
+ tags=["internal"],
+ )
# Docling preset update endpoint
- app.add_api_route("/settings/docling-preset", settings.update_docling_preset, methods=["PATCH"], tags=["internal"])
+ app.add_api_route(
+ "/settings/docling-preset",
+ settings.update_docling_preset,
+ methods=["PATCH"],
+ tags=["internal"],
+ )
# Nudges endpoints
- app.add_api_route("/nudges", nudges.nudges_from_kb_endpoint, methods=["POST"], tags=["internal"])
- app.add_api_route("/nudges/{chat_id}", nudges.nudges_from_chat_id_endpoint, methods=["POST"], tags=["internal"])
+ app.add_api_route(
+ "/nudges", nudges.nudges_from_kb_endpoint, methods=["POST"], tags=["internal"]
+ )
+ app.add_api_route(
+ "/nudges/{chat_id}",
+ nudges.nudges_from_chat_id_endpoint,
+ methods=["POST"],
+ tags=["internal"],
+ )
# Flow reset endpoint
- app.add_api_route("/reset-flow/{flow_type}", flows.reset_flow_endpoint, methods=["POST"], tags=["internal"])
+ app.add_api_route(
+ "/reset-flow/{flow_type}",
+ flows.reset_flow_endpoint,
+ methods=["POST"],
+ tags=["internal"],
+ )
# Router upload ingest endpoint
- app.add_api_route("/router/upload_ingest", router.upload_ingest_router, methods=["POST"], tags=["internal"])
+ app.add_api_route(
+ "/router/upload_ingest",
+ router.upload_ingest_router,
+ methods=["POST"],
+ tags=["internal"],
+ )
# Docling service proxy
- app.add_api_route("/docling/health", docling.health, methods=["GET"], tags=["internal"])
+ app.add_api_route(
+ "/docling/health", docling.health, methods=["GET"], tags=["internal"]
+ )
# ===== API Key Management Endpoints (JWT auth for UI) =====
- app.add_api_route("/keys", api_keys.list_keys_endpoint, methods=["GET"], tags=["internal"])
- app.add_api_route("/keys", api_keys.create_key_endpoint, methods=["POST"], tags=["internal"])
- app.add_api_route("/keys/{key_id}", api_keys.revoke_key_endpoint, methods=["DELETE"], tags=["internal"])
+ app.add_api_route(
+ "/keys", api_keys.list_keys_endpoint, methods=["GET"], tags=["internal"]
+ )
+ app.add_api_route(
+ "/keys", api_keys.create_key_endpoint, methods=["POST"], tags=["internal"]
+ )
+ app.add_api_route(
+ "/keys/{key_id}",
+ api_keys.revoke_key_endpoint,
+ methods=["DELETE"],
+ tags=["internal"],
+ )
# ===== Public API v1 Endpoints (API Key auth) =====
# Chat endpoints
- app.add_api_route("/v1/chat", v1_chat.chat_create_endpoint, methods=["POST"], tags=["public"])
- app.add_api_route("/v1/chat", v1_chat.chat_list_endpoint, methods=["GET"], tags=["public"])
- app.add_api_route("/v1/chat/{chat_id}", v1_chat.chat_get_endpoint, methods=["GET"], tags=["public"])
- app.add_api_route("/v1/chat/{chat_id}", v1_chat.chat_delete_endpoint, methods=["DELETE"], tags=["public"])
+ app.add_api_route(
+ "/v1/chat", v1_chat.chat_create_endpoint, methods=["POST"], tags=["public"]
+ )
+ app.add_api_route(
+ "/v1/chat", v1_chat.chat_list_endpoint, methods=["GET"], tags=["public"]
+ )
+ app.add_api_route(
+ "/v1/chat/{chat_id}",
+ v1_chat.chat_get_endpoint,
+ methods=["GET"],
+ tags=["public"],
+ )
+ app.add_api_route(
+ "/v1/chat/{chat_id}",
+ v1_chat.chat_delete_endpoint,
+ methods=["DELETE"],
+ tags=["public"],
+ )
# Search endpoint
- app.add_api_route("/v1/search", v1_search.search_endpoint, methods=["POST"], tags=["public"])
+ app.add_api_route(
+ "/v1/search", v1_search.search_endpoint, methods=["POST"], tags=["public"]
+ )
# Documents endpoints
- app.add_api_route("/v1/documents/ingest", v1_documents.ingest_endpoint, methods=["POST"], tags=["public"])
- app.add_api_route("/v1/tasks/{task_id}", v1_documents.task_status_endpoint, methods=["GET"], tags=["public"])
- app.add_api_route("/v1/documents", v1_documents.delete_document_endpoint, methods=["DELETE"], tags=["public"])
+ app.add_api_route(
+ "/v1/documents/ingest",
+ v1_documents.ingest_endpoint,
+ methods=["POST"],
+ tags=["public"],
+ )
+ app.add_api_route(
+ "/v1/tasks/{task_id}",
+ v1_documents.task_status_endpoint,
+ methods=["GET"],
+ tags=["public"],
+ )
+ app.add_api_route(
+ "/v1/documents",
+ v1_documents.delete_document_endpoint,
+ methods=["DELETE"],
+ tags=["public"],
+ )
# Settings endpoints
- app.add_api_route("/v1/settings", v1_settings.get_settings_endpoint, methods=["GET"], tags=["public"])
- app.add_api_route("/v1/settings", v1_settings.update_settings_endpoint, methods=["POST"], tags=["public"])
+ app.add_api_route(
+ "/v1/settings",
+ v1_settings.get_settings_endpoint,
+ methods=["GET"],
+ tags=["public"],
+ )
+ app.add_api_route(
+ "/v1/settings",
+ v1_settings.update_settings_endpoint,
+ methods=["POST"],
+ tags=["public"],
+ )
# Models endpoint
- app.add_api_route("/v1/models/{provider}", v1_models.list_models_endpoint, methods=["GET"], tags=["public"])
+ app.add_api_route(
+ "/v1/models/{provider}",
+ v1_models.list_models_endpoint,
+ methods=["GET"],
+ tags=["public"],
+ )
# Knowledge filters endpoints
- app.add_api_route("/v1/knowledge-filters", v1_knowledge_filters.create_endpoint, methods=["POST"], tags=["public"])
- app.add_api_route("/v1/knowledge-filters/search", v1_knowledge_filters.search_endpoint, methods=["POST"], tags=["public"])
- app.add_api_route("/v1/knowledge-filters/{filter_id}", v1_knowledge_filters.get_endpoint, methods=["GET"], tags=["public"])
- app.add_api_route("/v1/knowledge-filters/{filter_id}", v1_knowledge_filters.update_endpoint, methods=["PUT"], tags=["public"])
- app.add_api_route("/v1/knowledge-filters/{filter_id}", v1_knowledge_filters.delete_endpoint, methods=["DELETE"], tags=["public"])
+ app.add_api_route(
+ "/v1/knowledge-filters",
+ v1_knowledge_filters.create_endpoint,
+ methods=["POST"],
+ tags=["public"],
+ )
+ app.add_api_route(
+ "/v1/knowledge-filters/search",
+ v1_knowledge_filters.search_endpoint,
+ methods=["POST"],
+ tags=["public"],
+ )
+ app.add_api_route(
+ "/v1/knowledge-filters/{filter_id}",
+ v1_knowledge_filters.get_endpoint,
+ methods=["GET"],
+ tags=["public"],
+ )
+ app.add_api_route(
+ "/v1/knowledge-filters/{filter_id}",
+ v1_knowledge_filters.update_endpoint,
+ methods=["PUT"],
+ tags=["public"],
+ )
+ app.add_api_route(
+ "/v1/knowledge-filters/{filter_id}",
+ v1_knowledge_filters.delete_endpoint,
+ methods=["DELETE"],
+ tags=["public"],
+ )
# Add startup event handler
@app.on_event("startup")
async def startup_event():
- await TelemetryClient.send_event(Category.APPLICATION_STARTUP, MessageId.ORB_APP_STARTED)
+ await TelemetryClient.send_event(
+ Category.APPLICATION_STARTUP, MessageId.ORB_APP_STARTED
+ )
# Start index initialization in background to avoid blocking OIDC endpoints
t1 = asyncio.create_task(startup_tasks(services))
app.state.background_tasks.add(t1)
@@ -1253,13 +1638,17 @@ async def create_app():
# Check if onboarding has been completed
config = get_openrag_config()
if not config.edited:
- logger.debug("Onboarding not completed yet, skipping periodic backup")
+ logger.debug(
+ "Onboarding not completed yet, skipping periodic backup"
+ )
continue
flows_service = services.get("flows_service")
if flows_service:
logger.info("Running periodic flow backup")
- backup_results = await flows_service.backup_all_flows(only_if_changed=True)
+ backup_results = await flows_service.backup_all_flows(
+ only_if_changed=True
+ )
if backup_results["backed_up"]:
logger.info(
"Periodic backup completed",
@@ -1285,7 +1674,9 @@ async def create_app():
# Add shutdown event handler
@app.on_event("shutdown")
async def shutdown_event():
- await TelemetryClient.send_event(Category.APPLICATION_SHUTDOWN, MessageId.ORB_APP_SHUTDOWN)
+ await TelemetryClient.send_event(
+ Category.APPLICATION_SHUTDOWN, MessageId.ORB_APP_SHUTDOWN
+ )
await cleanup_subscriptions_proper(services)
# Cleanup task service (cancels background tasks and process pool)
await services["task_service"].shutdown()
@@ -1293,6 +1684,7 @@ async def create_app():
await clients.cleanup()
# Cleanup telemetry client
from utils.telemetry.client import cleanup_telemetry_client
+
await cleanup_telemetry_client()
return app