Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean, memoized assistant ordering #2655

Merged
merged 4 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions web/src/app/chat/ChatPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
useContext,
useEffect,
useLayoutEffect,
useMemo,
useRef,
useState,
} from "react";
Expand Down Expand Up @@ -157,14 +158,17 @@ export function ChatPage({
// Useful for determining which session has been loaded (i.e. still on `new, empty session` or `previous session`)
const loadedIdSessionRef = useRef<number | null>(existingChatSessionId);

// Assistants
const { visibleAssistants, hiddenAssistants: _ } = classifyAssistants(
user,
availableAssistants
);
const finalAssistants = user
? orderAssistantsForUser(visibleAssistants, user)
: visibleAssistants;
// Assistants in order
const { finalAssistants } = useMemo(() => {
const { visibleAssistants, hiddenAssistants: _ } = classifyAssistants(
user,
availableAssistants
);
const finalAssistants = user
? orderAssistantsForUser(visibleAssistants, user)
: visibleAssistants;
return { finalAssistants };
}, [user, availableAssistants]);

const existingChatSessionAssistantId = selectedChatSession?.persona_id;
const [selectedAssistant, setSelectedAssistant] = useState<
Expand Down Expand Up @@ -208,7 +212,7 @@ export function ChatPage({
};

const llmOverrideManager = useLlmOverride(
user?.preferences.default_model,
user?.preferences.default_model ?? null,
selectedChatSession,
defaultTemperature
);
Expand Down Expand Up @@ -2409,6 +2413,7 @@ export function ChatPage({
handleFileUpload={handleImageUpload}
textAreaRef={textAreaRef}
chatSessionId={chatSessionIdRef.current!}
refreshUser={refreshUser}
/>

{enterpriseSettings &&
Expand Down
3 changes: 3 additions & 0 deletions web/src/app/chat/input/ChatInputBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export function ChatInputBar({
alternativeAssistant,
chatSessionId,
inputPrompts,
refreshUser,
}: {
showConfigureAPIKey: () => void;
openModelSettings: () => void;
Expand All @@ -86,6 +87,7 @@ export function ChatInputBar({
handleFileUpload: (files: File[]) => void;
textAreaRef: React.RefObject<HTMLTextAreaElement>;
chatSessionId?: number;
refreshUser: () => void;
}) {
useEffect(() => {
const textarea = textAreaRef.current;
Expand Down Expand Up @@ -532,6 +534,7 @@ export function ChatInputBar({
setSelectedAssistant(assistant);
close();
}}
refreshUser={refreshUser}
/>
)}
flexPriority="shrink"
Expand Down
29 changes: 14 additions & 15 deletions web/src/app/chat/modal/configuration/AssistantsTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -43,23 +44,21 @@ 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();
}
}

Expand Down
1 change: 0 additions & 1 deletion web/src/components/user/UserProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export function UserProvider({ children }: { children: React.ReactNode }) {
}, []);

const refreshUser = async () => {
setIsLoadingUser(true);
await fetchUser();
};

Expand Down
Loading