diff --git a/packages/webui/components/chatbot-sidebar.tsx b/packages/webui/components/chatbot-sidebar.tsx index 5f7f7857..652ca178 100644 --- a/packages/webui/components/chatbot-sidebar.tsx +++ b/packages/webui/components/chatbot-sidebar.tsx @@ -13,7 +13,7 @@ import { useChatbotStore } from '@/ui/stores/chatbot' import { useTeamContextStore } from '@/ui/stores/team-context' import { useDroppable } from '@dnd-kit/react' import type { ChatMessage } from '@shumai/dtos' -import { useQuery } from '@tanstack/react-query' +import { useQuery, useQueryClient } from '@tanstack/react-query' import { ArrowLeft, ArrowUp, @@ -26,7 +26,7 @@ import { Trash2, Wrench, } from 'lucide-react' -import { useEffect, useRef, useState } from 'react' +import { useCallback, useEffect, useRef, useState } from 'react' import Markdown from 'react-markdown' import { formatTimeAgo } from '../lib/time' @@ -171,9 +171,20 @@ export function ChatbotSidebar({ projectId, contextAssetId }: ChatbotSidebarProp } }, [isHistoryMode, fetchHistorySessions, teamId]) + const queryClient = useQueryClient() + + const handleAssetMutation = useCallback(() => { + queryClient.invalidateQueries({ queryKey: ['search'] }) + queryClient.invalidateQueries({ queryKey: ['folders'] }) + queryClient.invalidateQueries({ queryKey: ['files'] }) + queryClient.invalidateQueries({ queryKey: ['file'] }) + queryClient.invalidateQueries({ queryKey: ['version_stacks'] }) + queryClient.invalidateQueries({ queryKey: ['projects'] }) + }, [queryClient]) + const handleSend = () => { if (!inputText.trim() || isStreaming || !teamId) return - sendMessage(teamId, inputText, projectId, contextAssetId) + sendMessage(teamId, inputText, projectId, contextAssetId, handleAssetMutation) setInputText('') } diff --git a/packages/webui/stores/chatbot.test.ts b/packages/webui/stores/chatbot.test.ts index 7c6ff49f..60998290 100644 --- a/packages/webui/stores/chatbot.test.ts +++ b/packages/webui/stores/chatbot.test.ts @@ -259,4 +259,44 @@ describe('useChatbotStore', () => { expect(finalState.messages[0].id).toBe('context-display-ulid') expect(finalState.messages[1].id).toBe('user-msg-ulid') }) + + it('should invoke onAssetMutation callback when asset creation tool result arrives', async () => { + const mockEvents = [ + 'data: {"type":"session","sessionId":"sess-123"}\n\n', + 'data: {"type":"entry","entry":{"id":"user-msg-ulid","role":"user","content":[{"type":"text","text":"create a file"}],"timestamp":123}}\n\n', + 'data: {"type":"entry","entry":{"id":"tool-res-ulid","role":"toolResult","toolName":"create_file","isError":false,"content":"File created","timestamp":124}}\n\n', + 'data: {"type":"done","status":"completed"}\n\n', + ] + + let eventIdx = 0 + const mockReader = { + read: vi.fn(async () => { + if (eventIdx < mockEvents.length) { + const value = new TextEncoder().encode(mockEvents[eventIdx++]) + return { done: false, value } + } + return { done: true, value: undefined } + }), + } + + const mockResponse = { + ok: true, + headers: { + get: vi.fn(() => 'sess-123'), + }, + body: { + getReader: () => mockReader, + }, + } + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + vi.mocked(client.api.teams[':teamId'].chat.$post).mockResolvedValue(mockResponse as any) + + const onAssetMutation = vi.fn() + await useChatbotStore + .getState() + .sendMessage('team-123', 'create a file', 'proj-123', undefined, onAssetMutation) + + expect(onAssetMutation).toHaveBeenCalledTimes(1) + }) }) diff --git a/packages/webui/stores/chatbot.ts b/packages/webui/stores/chatbot.ts index 12815c83..f31a698f 100644 --- a/packages/webui/stores/chatbot.ts +++ b/packages/webui/stores/chatbot.ts @@ -42,6 +42,7 @@ interface ChatbotState { text: string, projectId: string, contextAssetId?: string, + onAssetMutation?: () => void, ) => Promise abortActiveSession: (teamId: string) => Promise } @@ -151,7 +152,7 @@ export const useChatbotStore = create((set) => ({ }) }, - sendMessage: async (teamId, text, projectId, contextAssetId) => { + sendMessage: async (teamId, text, projectId, contextAssetId, onAssetMutation) => { const state = useChatbotStore.getState() if (state.isStreaming) return @@ -251,6 +252,16 @@ export const useChatbotStore = create((set) => ({ } } else if (data.type === 'entry') { const entry = data.entry as ChatMessage + const entryObj = entry as unknown as Record + if ( + entryObj.role === 'toolResult' && + typeof entryObj.toolName === 'string' && + ['create_file', 'create_folder', 'create_version'].includes(entryObj.toolName) && + !entryObj.isError + ) { + onAssetMutation?.() + } + set((s) => { const isExisting = s.messages.some((m) => m.id === entry.id) // Replace temp/optimistic message when the real counterpart streams back