Clean, memoized assistant ordering (#2655)

* updated refresh

* memoization and so on

* nit

* build issue
This commit is contained in:
pablodanswer
2024-10-02 09:15:54 -07:00
committed by GitHub
parent 07aeea69e7
commit a30de693cb
4 changed files with 30 additions and 24 deletions

View File

@ -50,6 +50,7 @@ import {
useContext, useContext,
useEffect, useEffect,
useLayoutEffect, useLayoutEffect,
useMemo,
useRef, useRef,
useState, useState,
} from "react"; } from "react";
@ -157,7 +158,8 @@ export function ChatPage({
// Useful for determining which session has been loaded (i.e. still on `new, empty session` or `previous session`) // Useful for determining which session has been loaded (i.e. still on `new, empty session` or `previous session`)
const loadedIdSessionRef = useRef<number | null>(existingChatSessionId); const loadedIdSessionRef = useRef<number | null>(existingChatSessionId);
// Assistants // Assistants in order
const { finalAssistants } = useMemo(() => {
const { visibleAssistants, hiddenAssistants: _ } = classifyAssistants( const { visibleAssistants, hiddenAssistants: _ } = classifyAssistants(
user, user,
availableAssistants availableAssistants
@ -165,6 +167,8 @@ export function ChatPage({
const finalAssistants = user const finalAssistants = user
? orderAssistantsForUser(visibleAssistants, user) ? orderAssistantsForUser(visibleAssistants, user)
: visibleAssistants; : visibleAssistants;
return { finalAssistants };
}, [user, availableAssistants]);
const existingChatSessionAssistantId = selectedChatSession?.persona_id; const existingChatSessionAssistantId = selectedChatSession?.persona_id;
const [selectedAssistant, setSelectedAssistant] = useState< const [selectedAssistant, setSelectedAssistant] = useState<
@ -2410,6 +2414,7 @@ export function ChatPage({
handleFileUpload={handleImageUpload} handleFileUpload={handleImageUpload}
textAreaRef={textAreaRef} textAreaRef={textAreaRef}
chatSessionId={chatSessionIdRef.current!} chatSessionId={chatSessionIdRef.current!}
refreshUser={refreshUser}
/> />
{enterpriseSettings && {enterpriseSettings &&

View File

@ -63,6 +63,7 @@ export function ChatInputBar({
alternativeAssistant, alternativeAssistant,
chatSessionId, chatSessionId,
inputPrompts, inputPrompts,
refreshUser,
}: { }: {
showConfigureAPIKey: () => void; showConfigureAPIKey: () => void;
openModelSettings: () => void; openModelSettings: () => void;
@ -86,6 +87,7 @@ export function ChatInputBar({
handleFileUpload: (files: File[]) => void; handleFileUpload: (files: File[]) => void;
textAreaRef: React.RefObject<HTMLTextAreaElement>; textAreaRef: React.RefObject<HTMLTextAreaElement>;
chatSessionId?: number; chatSessionId?: number;
refreshUser: () => void;
}) { }) {
useEffect(() => { useEffect(() => {
const textarea = textAreaRef.current; const textarea = textAreaRef.current;
@ -532,6 +534,7 @@ export function ChatInputBar({
setSelectedAssistant(assistant); setSelectedAssistant(assistant);
close(); close();
}} }}
refreshUser={refreshUser}
/> />
)} )}
flexPriority="shrink" flexPriority="shrink"

View File

@ -13,25 +13,26 @@ import {
sortableKeyboardCoordinates, sortableKeyboardCoordinates,
verticalListSortingStrategy, verticalListSortingStrategy,
} from "@dnd-kit/sortable"; } from "@dnd-kit/sortable";
import { CSS } from "@dnd-kit/utilities";
import { Persona } from "@/app/admin/assistants/interfaces"; import { Persona } from "@/app/admin/assistants/interfaces";
import { LLMProviderDescriptor } from "@/app/admin/configuration/llm/interfaces"; import { LLMProviderDescriptor } from "@/app/admin/configuration/llm/interfaces";
import { getFinalLLM } from "@/lib/llm/utils"; import { getFinalLLM } from "@/lib/llm/utils";
import React, { useState } from "react"; import React, { useState } from "react";
import { updateUserAssistantList } from "@/lib/assistants/updateAssistantPreferences"; import { updateUserAssistantList } from "@/lib/assistants/updateAssistantPreferences";
import { DraggableAssistantCard } from "@/components/assistants/AssistantCards"; import { DraggableAssistantCard } from "@/components/assistants/AssistantCards";
import { orderAssistantsForUser } from "@/lib/assistants/utils"; import { useRouter } from "next/navigation";
export function AssistantsTab({ export function AssistantsTab({
selectedAssistant, selectedAssistant,
availableAssistants, availableAssistants,
llmProviders, llmProviders,
onSelect, onSelect,
refreshUser,
}: { }: {
selectedAssistant: Persona; selectedAssistant: Persona;
availableAssistants: Persona[]; availableAssistants: Persona[];
llmProviders: LLMProviderDescriptor[]; llmProviders: LLMProviderDescriptor[];
onSelect: (assistant: Persona) => void; onSelect: (assistant: Persona) => void;
refreshUser: () => void;
}) { }) {
const [_, llmName] = getFinalLLM(llmProviders, null, null); const [_, llmName] = getFinalLLM(llmProviders, null, null);
const [assistants, setAssistants] = useState(availableAssistants); const [assistants, setAssistants] = useState(availableAssistants);
@ -43,23 +44,21 @@ export function AssistantsTab({
}) })
); );
function handleDragEnd(event: DragEndEvent) { async function handleDragEnd(event: DragEndEvent) {
const { active, over } = event; const { active, over } = event;
if (over && active.id !== over.id) { if (over && active.id !== over.id) {
setAssistants((items) => { const oldIndex = assistants.findIndex(
const oldIndex = items.findIndex(
(item) => item.id.toString() === active.id (item) => item.id.toString() === active.id
); );
const newIndex = items.findIndex( const newIndex = assistants.findIndex(
(item) => item.id.toString() === over.id (item) => item.id.toString() === over.id
); );
const updatedAssistants = arrayMove(items, oldIndex, newIndex); const updatedAssistants = arrayMove(assistants, oldIndex, newIndex);
updateUserAssistantList(updatedAssistants.map((a) => a.id)); setAssistants(updatedAssistants);
await updateUserAssistantList(updatedAssistants.map((a) => a.id));
return updatedAssistants; refreshUser();
});
} }
} }

View File

@ -40,7 +40,6 @@ export function UserProvider({ children }: { children: React.ReactNode }) {
}, []); }, []);
const refreshUser = async () => { const refreshUser = async () => {
setIsLoadingUser(true);
await fetchUser(); await fetchUser();
}; };