Skip to content

Project NOMAD is an offline-first knowledge and education server. Wikipedia, thousands of books, courses, maps, and optional local AI, all running on hardware you own with no internet required.

TypeScript

fix(AI Assistant): chat suggestion performance improvements

Jake Turner9301c44d3f5c
diff --git a/admin/inertia/components/chat/index.tsx b/admin/inertia/components/chat/index.tsx
index 96f5f360..8359d2c4 100644
--- a/admin/inertia/components/chat/index.tsx
+++ b/admin/inertia/components/chat/index.tsx
@@ -55,11 +55,13 @@ export default function Chat({
const { data: chatSuggestions, isLoading: chatSuggestionsLoading } = useQuery<string[]>({
queryKey: ['chatSuggestions'],
- queryFn: async () => {
- const res = await api.getChatSuggestions()
+ queryFn: async ({ signal }) => {
+ const res = await api.getChatSuggestions(signal)
return res ?? []
},
- enabled: suggestionsEnabled,
+ enabled: suggestionsEnabled && !activeSessionId,
+ refetchOnWindowFocus: false,
+ refetchOnMount: false,
})
const deleteAllSessionsMutation = useMutation({
@@ -151,6 +153,9 @@ export default function Chat({
const handleSessionSelect = useCallback(
async (sessionId: string) => {
+ // Cancel any ongoing suggestions fetch
+ queryClient.cancelQueries({ queryKey: ['chatSuggestions'] })
+
setActiveSessionId(sessionId)
// Load messages for this session
const sessionData = await api.getChatSession(sessionId)
@@ -172,7 +177,7 @@ export default function Chat({
setSelectedModel(sessionData.model)
}
},
- [installedModels]
+ [installedModels, queryClient]
)
const handleSendMessage = useCallback(
diff --git a/admin/inertia/lib/api.ts b/admin/inertia/lib/api.ts
index 0df5ac03..b50e0e24 100644
--- a/admin/inertia/lib/api.ts
+++ b/admin/inertia/lib/api.ts
@@ -147,10 +147,11 @@ class API {
})()
}
- async getChatSuggestions() {
+ async getChatSuggestions(signal?: AbortSignal) {
return catchInternal(async () => {
const response = await this.client.get<{ suggestions: string[] }>(
- '/chat/suggestions'
+ '/chat/suggestions',
+ { signal }
)
return response.data.suggestions
})()