mirror of
https://github.com/leonvanzyl/autocoder.git
synced 2026-02-05 16:33:08 +00:00
Merge pull request #74 from lirielgozi/feature-conversation-history
feature: add conversation history feature to AI assistant
This commit is contained in:
@@ -120,6 +120,9 @@ export function useAssistantChat({
|
||||
ws.onmessage = (event) => {
|
||||
try {
|
||||
const data = JSON.parse(event.data) as AssistantChatServerMessage;
|
||||
if (import.meta.env.DEV) {
|
||||
console.debug('[useAssistantChat] Received WebSocket message:', data.type, data);
|
||||
}
|
||||
|
||||
switch (data.type) {
|
||||
case "text": {
|
||||
@@ -277,6 +280,9 @@ export function useAssistantChat({
|
||||
payload.conversation_id = existingConversationId;
|
||||
setConversationId(existingConversationId);
|
||||
}
|
||||
if (import.meta.env.DEV) {
|
||||
console.debug('[useAssistantChat] Sending start message:', payload);
|
||||
}
|
||||
wsRef.current.send(JSON.stringify(payload));
|
||||
} else if (wsRef.current?.readyState === WebSocket.CONNECTING) {
|
||||
checkAndSendTimeoutRef.current = window.setTimeout(checkAndSend, 100);
|
||||
@@ -336,7 +342,7 @@ export function useAssistantChat({
|
||||
|
||||
const clearMessages = useCallback(() => {
|
||||
setMessages([]);
|
||||
setConversationId(null);
|
||||
// Don't reset conversationId here - it will be set by start() when switching
|
||||
}, []);
|
||||
|
||||
return {
|
||||
|
||||
48
ui/src/hooks/useConversations.ts
Normal file
48
ui/src/hooks/useConversations.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* React Query hooks for assistant conversation management
|
||||
*/
|
||||
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import * as api from '../lib/api'
|
||||
|
||||
/**
|
||||
* List all conversations for a project
|
||||
*/
|
||||
export function useConversations(projectName: string | null) {
|
||||
return useQuery({
|
||||
queryKey: ['conversations', projectName],
|
||||
queryFn: () => api.listAssistantConversations(projectName!),
|
||||
enabled: !!projectName,
|
||||
staleTime: 30000, // Cache for 30 seconds
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single conversation with all its messages
|
||||
*/
|
||||
export function useConversation(projectName: string | null, conversationId: number | null) {
|
||||
return useQuery({
|
||||
queryKey: ['conversation', projectName, conversationId],
|
||||
queryFn: () => api.getAssistantConversation(projectName!, conversationId!),
|
||||
enabled: !!projectName && !!conversationId,
|
||||
staleTime: 30_000, // Cache for 30 seconds
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a conversation
|
||||
*/
|
||||
export function useDeleteConversation(projectName: string) {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (conversationId: number) =>
|
||||
api.deleteAssistantConversation(projectName, conversationId),
|
||||
onSuccess: (_, deletedId) => {
|
||||
// Invalidate conversations list
|
||||
queryClient.invalidateQueries({ queryKey: ['conversations', projectName] })
|
||||
// Remove the specific conversation from cache
|
||||
queryClient.removeQueries({ queryKey: ['conversation', projectName, deletedId] })
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user