From ab3e07559241d51bc073ed95ed4204b40deb4af9 Mon Sep 17 00:00:00 2001 From: pablodanswer Date: Tue, 1 Oct 2024 10:43:31 -0700 Subject: [PATCH 1/4] updated refresh --- web/src/app/chat/ChatPage.tsx | 23 +++++++++++++++---- web/src/app/chat/input/ChatInputBar.tsx | 3 +++ .../modal/configuration/AssistantsTab.tsx | 6 +++-- web/src/components/user/UserProvider.tsx | 2 +- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/web/src/app/chat/ChatPage.tsx b/web/src/app/chat/ChatPage.tsx index 0d13b8607d2..581b65ae485 100644 --- a/web/src/app/chat/ChatPage.tsx +++ b/web/src/app/chat/ChatPage.tsx @@ -50,6 +50,7 @@ import { useContext, useEffect, useLayoutEffect, + useMemo, useRef, useState, } from "react"; @@ -162,9 +163,21 @@ export function ChatPage({ user, availableAssistants ); - const finalAssistants = user - ? orderAssistantsForUser(visibleAssistants, user) - : visibleAssistants; + + const [finalAssistants, setFinalAssistants] = useState(visibleAssistants); + + useEffect(() => { + const { visibleAssistants: updatedVisibleAssistants } = classifyAssistants( + user, + availableAssistants + ); + setFinalAssistants( + user + ? orderAssistantsForUser(updatedVisibleAssistants, user) + : updatedVisibleAssistants + ); + console.log(orderAssistantsForUser(updatedVisibleAssistants, user)); + }, [user, availableAssistants]); const existingChatSessionAssistantId = selectedChatSession?.persona_id; const [selectedAssistant, setSelectedAssistant] = useState< @@ -208,7 +221,7 @@ export function ChatPage({ }; const llmOverrideManager = useLlmOverride( - user?.preferences.default_model, + user?.preferences.default_model ?? null, selectedChatSession, defaultTemperature ); @@ -1779,6 +1792,7 @@ export function ChatPage({ {settingsToggled && ( {enterpriseSettings && diff --git a/web/src/app/chat/input/ChatInputBar.tsx b/web/src/app/chat/input/ChatInputBar.tsx index 64535d82b20..cef0bc49f50 100644 --- a/web/src/app/chat/input/ChatInputBar.tsx +++ b/web/src/app/chat/input/ChatInputBar.tsx @@ -63,6 +63,7 @@ export function ChatInputBar({ alternativeAssistant, chatSessionId, inputPrompts, + refreshUser, }: { showConfigureAPIKey: () => void; openModelSettings: () => void; @@ -86,6 +87,7 @@ export function ChatInputBar({ handleFileUpload: (files: File[]) => void; textAreaRef: React.RefObject; chatSessionId?: number; + refreshUser: () => void; }) { useEffect(() => { const textarea = textAreaRef.current; @@ -532,6 +534,7 @@ export function ChatInputBar({ setSelectedAssistant(assistant); close(); }} + refreshUser={refreshUser} /> )} flexPriority="shrink" diff --git a/web/src/app/chat/modal/configuration/AssistantsTab.tsx b/web/src/app/chat/modal/configuration/AssistantsTab.tsx index dcf31138ccf..39a61940099 100644 --- a/web/src/app/chat/modal/configuration/AssistantsTab.tsx +++ b/web/src/app/chat/modal/configuration/AssistantsTab.tsx @@ -13,25 +13,26 @@ import { sortableKeyboardCoordinates, verticalListSortingStrategy, } from "@dnd-kit/sortable"; -import { CSS } from "@dnd-kit/utilities"; import { Persona } from "@/app/admin/assistants/interfaces"; import { LLMProviderDescriptor } from "@/app/admin/configuration/llm/interfaces"; import { getFinalLLM } from "@/lib/llm/utils"; import React, { useState } from "react"; import { updateUserAssistantList } from "@/lib/assistants/updateAssistantPreferences"; import { DraggableAssistantCard } from "@/components/assistants/AssistantCards"; -import { orderAssistantsForUser } from "@/lib/assistants/utils"; +import { useRouter } from "next/navigation"; export function AssistantsTab({ selectedAssistant, availableAssistants, llmProviders, onSelect, + refreshUser, }: { selectedAssistant: Persona; availableAssistants: Persona[]; llmProviders: LLMProviderDescriptor[]; onSelect: (assistant: Persona) => void; + refreshUser: () => void; }) { const [_, llmName] = getFinalLLM(llmProviders, null, null); const [assistants, setAssistants] = useState(availableAssistants); @@ -61,6 +62,7 @@ export function AssistantsTab({ return updatedAssistants; }); } + refreshUser(); } return ( diff --git a/web/src/components/user/UserProvider.tsx b/web/src/components/user/UserProvider.tsx index 67777277c27..043c492f12d 100644 --- a/web/src/components/user/UserProvider.tsx +++ b/web/src/components/user/UserProvider.tsx @@ -22,6 +22,7 @@ export function UserProvider({ children }: { children: React.ReactNode }) { const fetchUser = async () => { try { + console.log("FETCHING USER"); const user = await getCurrentUser(); setUser(user); setIsAdmin(user?.role === UserRole.ADMIN); @@ -40,7 +41,6 @@ export function UserProvider({ children }: { children: React.ReactNode }) { }, []); const refreshUser = async () => { - setIsLoadingUser(true); await fetchUser(); }; From 07eddd388ac16f1aeb17f70aa759b4b952073288 Mon Sep 17 00:00:00 2001 From: pablodanswer Date: Tue, 1 Oct 2024 16:18:41 -0700 Subject: [PATCH 2/4] memoization and so on --- web/src/app/chat/ChatPage.tsx | 23 ++++++----------- .../modal/configuration/AssistantsTab.tsx | 25 ++++++++----------- 2 files changed, 18 insertions(+), 30 deletions(-) diff --git a/web/src/app/chat/ChatPage.tsx b/web/src/app/chat/ChatPage.tsx index 581b65ae485..8f9b36b6265 100644 --- a/web/src/app/chat/ChatPage.tsx +++ b/web/src/app/chat/ChatPage.tsx @@ -158,25 +158,16 @@ export function ChatPage({ // Useful for determining which session has been loaded (i.e. still on `new, empty session` or `previous session`) const loadedIdSessionRef = useRef(existingChatSessionId); - // Assistants - const { visibleAssistants, hiddenAssistants: _ } = classifyAssistants( - user, - availableAssistants - ); - - const [finalAssistants, setFinalAssistants] = useState(visibleAssistants); - - useEffect(() => { - const { visibleAssistants: updatedVisibleAssistants } = classifyAssistants( + // Assistants in order + const { finalAssistants } = useMemo(() => { + const { visibleAssistants, hiddenAssistants: _ } = classifyAssistants( user, availableAssistants ); - setFinalAssistants( - user - ? orderAssistantsForUser(updatedVisibleAssistants, user) - : updatedVisibleAssistants - ); - console.log(orderAssistantsForUser(updatedVisibleAssistants, user)); + const finalAssistants = user + ? orderAssistantsForUser(visibleAssistants, user) + : visibleAssistants; + return { finalAssistants }; }, [user, availableAssistants]); const existingChatSessionAssistantId = selectedChatSession?.persona_id; diff --git a/web/src/app/chat/modal/configuration/AssistantsTab.tsx b/web/src/app/chat/modal/configuration/AssistantsTab.tsx index 39a61940099..1db03cd605d 100644 --- a/web/src/app/chat/modal/configuration/AssistantsTab.tsx +++ b/web/src/app/chat/modal/configuration/AssistantsTab.tsx @@ -44,25 +44,22 @@ export function AssistantsTab({ }) ); - function handleDragEnd(event: DragEndEvent) { + async function handleDragEnd(event: DragEndEvent) { const { active, over } = event; if (over && active.id !== over.id) { - setAssistants((items) => { - const oldIndex = items.findIndex( - (item) => item.id.toString() === active.id - ); - const newIndex = items.findIndex( - (item) => item.id.toString() === over.id - ); - const updatedAssistants = arrayMove(items, oldIndex, newIndex); + const oldIndex = assistants.findIndex( + (item) => item.id.toString() === active.id + ); + const newIndex = assistants.findIndex( + (item) => item.id.toString() === over.id + ); + const updatedAssistants = arrayMove(assistants, oldIndex, newIndex); - updateUserAssistantList(updatedAssistants.map((a) => a.id)); - - return updatedAssistants; - }); + setAssistants(updatedAssistants); + await updateUserAssistantList(updatedAssistants.map((a) => a.id)); + refreshUser(); } - refreshUser(); } return ( From 11b073436c3d1a3fa2a353dab693d018422f4cd1 Mon Sep 17 00:00:00 2001 From: pablodanswer Date: Tue, 1 Oct 2024 16:19:28 -0700 Subject: [PATCH 3/4] nit --- web/src/components/user/UserProvider.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/web/src/components/user/UserProvider.tsx b/web/src/components/user/UserProvider.tsx index 043c492f12d..5e7bb753520 100644 --- a/web/src/components/user/UserProvider.tsx +++ b/web/src/components/user/UserProvider.tsx @@ -22,7 +22,6 @@ export function UserProvider({ children }: { children: React.ReactNode }) { const fetchUser = async () => { try { - console.log("FETCHING USER"); const user = await getCurrentUser(); setUser(user); setIsAdmin(user?.role === UserRole.ADMIN); From e277137f725d0081e33864f0746b6bc3eaee8c51 Mon Sep 17 00:00:00 2001 From: pablodanswer Date: Tue, 1 Oct 2024 16:25:10 -0700 Subject: [PATCH 4/4] build issue --- web/src/app/chat/ChatPage.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/web/src/app/chat/ChatPage.tsx b/web/src/app/chat/ChatPage.tsx index 8f9b36b6265..de78361bef3 100644 --- a/web/src/app/chat/ChatPage.tsx +++ b/web/src/app/chat/ChatPage.tsx @@ -1783,7 +1783,6 @@ export function ChatPage({ {settingsToggled && (