mirror of
https://github.com/leonvanzyl/autocoder.git
synced 2026-02-05 16:33:08 +00:00
feat: add conversation history feature to AI assistant
- Add ConversationHistory dropdown component with list of past conversations - Add useConversations hook for fetching and managing conversations via React Query - Implement conversation switching with proper state management - Fix bug where reopening panel showed new greeting instead of resuming conversation - Fix bug where selecting from history caused conversation ID to revert - Add server-side history context loading for resumed conversations - Add Playwright E2E tests for conversation history feature - Add logging for debugging conversation flow Key changes: - AssistantPanel: manages conversation state with localStorage persistence - AssistantChat: header with [+] New Chat and [History] buttons - Server: skips greeting for resumed conversations, loads history context on first message - Fixed race condition in onConversationCreated callback
This commit is contained in:
@@ -120,6 +120,7 @@ export function useAssistantChat({
|
||||
ws.onmessage = (event) => {
|
||||
try {
|
||||
const data = JSON.parse(event.data) as AssistantChatServerMessage;
|
||||
console.log('[useAssistantChat] Received WebSocket message:', data.type, data);
|
||||
|
||||
switch (data.type) {
|
||||
case "text": {
|
||||
@@ -277,6 +278,7 @@ export function useAssistantChat({
|
||||
payload.conversation_id = existingConversationId;
|
||||
setConversationId(existingConversationId);
|
||||
}
|
||||
console.log('[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 +338,7 @@ export function useAssistantChat({
|
||||
|
||||
const clearMessages = useCallback(() => {
|
||||
setMessages([]);
|
||||
setConversationId(null);
|
||||
// Don't reset conversationId here - it will be set by start() when switching
|
||||
}, []);
|
||||
|
||||
return {
|
||||
|
||||
47
ui/src/hooks/useConversations.ts
Normal file
47
ui/src/hooks/useConversations.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* 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,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 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