Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Uses assistant name instead of id
Browse files Browse the repository at this point in the history
amandasavluchinske committed Jun 17, 2024
1 parent b0421be commit 967be0f
Showing 3 changed files with 56 additions and 4 deletions.
11 changes: 7 additions & 4 deletions example/assets/js/components/Chat/Chat.tsx
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ import Markdown from "react-markdown";
import {
ThreadMessagesSchemaOut,
ThreadSchema,
useAssistantList,
useAssistant,
useMessageList,
useThreadList,
} from "django-ai-assistant-client";
@@ -132,6 +132,8 @@ export function Chat({ assistantId }: { assistantId: string }) {
loadingDeleteMessage,
} = useMessageList({ threadId: activeThread?.id });

const { fetchAssistant, assistant } = useAssistant({ assistantId });

const loadingMessages =
loadingFetchMessages || loadingCreateMessage || loadingDeleteMessage;
const isThreadSelected = Boolean(activeThread);
@@ -153,10 +155,11 @@ export function Chat({ assistantId }: { assistantId: string }) {
[scrollViewport]
);

// Load threads when component mounts:
// Load threads and assistant details when component mounts:
useEffect(() => {
fetchThreads();
}, [fetchThreads]);
fetchAssistant();
}, [fetchThreads, fetchAssistant]);

// Load messages when threadId changes:
useEffect(() => {
@@ -192,7 +195,7 @@ export function Chat({ assistantId }: { assistantId: string }) {
<Container className={classes.chatContainer}>
<Stack className={classes.chat}>
<Title mt="md" order={2}>
Chat: {assistantId}
Chat: {assistant?.name || "Loading…"}
</Title>
<ScrollArea
pos="relative"
1 change: 1 addition & 0 deletions frontend/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./useAssistantList";
export * from "./useAssistant";
export * from "./useMessageList";
export * from "./useThreadList";
48 changes: 48 additions & 0 deletions frontend/src/hooks/useAssistant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { useCallback } from "react";
import { useState } from "react";
import {
AssistantSchema,
djangoAiAssistantViewsGetAssistant,
} from "../client";

/**
* React hook to manage the Assistants.
*/
export function useAssistant({ assistantId }: {
assistantId: string;
}) {
const [assistant, setAssistant] = useState<AssistantSchema | null>(null);
const [loadingFetchAssistant, setLoadingFetchAssistant] =
useState<boolean>(false);

/**
* Fetches a list of AI assistants.
*
* @returns A promise that resolves with the fetched list of AI assistants.
*/
const fetchAssistant = useCallback(async (): Promise<AssistantSchema> => {
try {
setLoadingFetchAssistant(true);
const fetchedAssistant = await djangoAiAssistantViewsGetAssistant({ assistantId });
setAssistant(fetchedAssistant);
return fetchedAssistant;
} finally {
setLoadingFetchAssistant(false);
}
}, [assistantId]);

return {
/**
* Function to fetch an AI assistant from the server.
*/
fetchAssistant,
/**
* Fetched AI assistant.
*/
assistant,
/**
* Loading state of the fetch operation.
*/
loadingFetchAssistant,
};
}

0 comments on commit 967be0f

Please sign in to comment.