From d19200e9ef2f9b4e8067108792b279cd4555e883 Mon Sep 17 00:00:00 2001 From: yiling Date: Sat, 11 Jul 2026 10:49:40 +0800 Subject: [PATCH] feat(chatbot): implement chatbot sidebar, message view, history mode and sse stream --- packages/api/src/chat.ts | 10 + packages/webui/components/chatbot-sidebar.tsx | 342 +++++++++++++++--- .../webui/components/file-system-manager.tsx | 2 +- packages/webui/messages/en.json | 9 +- packages/webui/messages/zh.json | 9 +- packages/webui/paraglide/messages/_index.d.ts | 6 + packages/webui/paraglide/messages/_index.js | 8 +- .../messages/delete_session_confirm.d.ts | 16 + .../messages/delete_session_confirm.js | 29 ++ .../webui/paraglide/messages/history.d.ts | 16 + packages/webui/paraglide/messages/history.js | 29 ++ .../webui/paraglide/messages/new_chat.d.ts | 16 + packages/webui/paraglide/messages/new_chat.js | 29 ++ .../messages/no_history_sessions.d.ts | 16 + .../paraglide/messages/no_history_sessions.js | 29 ++ packages/webui/paraglide/messages/send.d.ts | 16 + packages/webui/paraglide/messages/send.js | 29 ++ .../paraglide/messages/shumai_agent.d.ts | 16 + .../webui/paraglide/messages/shumai_agent.js | 29 ++ .../$projectId/files/$fileId.lazy.tsx | 2 +- packages/webui/stores/chatbot.ts | 208 +++++++++++ 21 files changed, 809 insertions(+), 57 deletions(-) create mode 100644 packages/webui/paraglide/messages/delete_session_confirm.d.ts create mode 100644 packages/webui/paraglide/messages/delete_session_confirm.js create mode 100644 packages/webui/paraglide/messages/history.d.ts create mode 100644 packages/webui/paraglide/messages/history.js create mode 100644 packages/webui/paraglide/messages/new_chat.d.ts create mode 100644 packages/webui/paraglide/messages/new_chat.js create mode 100644 packages/webui/paraglide/messages/no_history_sessions.d.ts create mode 100644 packages/webui/paraglide/messages/no_history_sessions.js create mode 100644 packages/webui/paraglide/messages/send.d.ts create mode 100644 packages/webui/paraglide/messages/send.js create mode 100644 packages/webui/paraglide/messages/shumai_agent.d.ts create mode 100644 packages/webui/paraglide/messages/shumai_agent.js diff --git a/packages/api/src/chat.ts b/packages/api/src/chat.ts index 8f806bce..dd264867 100644 --- a/packages/api/src/chat.ts +++ b/packages/api/src/chat.ts @@ -36,7 +36,17 @@ const route = new Hono<{ Variables: { user: User } }>() const { sessionId, taskId } = await chatService.startOrContinueChat(user, req) + c.header('x-session-id', sessionId) + return streamSSE(c, async (stream) => { + // Send session ID as the first event + await stream.writeSSE({ + data: JSON.stringify({ + type: 'session', + sessionId, + }), + }) + let lastEntryId = '' const timeoutMs = 5 * 60 * 1000 // 5 minutes timeout const startTime = Date.now() diff --git a/packages/webui/components/chatbot-sidebar.tsx b/packages/webui/components/chatbot-sidebar.tsx index baaaebc5..1ea59014 100644 --- a/packages/webui/components/chatbot-sidebar.tsx +++ b/packages/webui/components/chatbot-sidebar.tsx @@ -1,11 +1,37 @@ +import { useEffect, useRef, useState } from 'react' import { useDroppable } from '@dnd-kit/react' import { useChatbotStore } from '@/ui/stores/chatbot' -import { File, Folder, X, MessageSquare } from 'lucide-react' +import { Bot, Send, History, Plus, Trash2, Loader2, ArrowLeft } from 'lucide-react' import { cn } from '@/ui/lib/utils' import { m } from '@/ui/paraglide/messages.js' +import { ScrollArea } from '@/ui/components/ui/scroll-area' +import Markdown from 'react-markdown' +import type { ChatMessage } from '@shumai/dtos' -export function ChatbotSidebar() { - const { chatAssets, removeAsset, clearAssets } = useChatbotStore() +interface ChatbotSidebarProps { + projectId: string + contextAssetId?: string +} + +export function ChatbotSidebar({ projectId, contextAssetId }: ChatbotSidebarProps) { + const { + isHistoryMode, + setIsHistoryMode, + currentSessionId, + chatAssets, + removeAsset, + messages, + historySessions, + isStreaming, + fetchHistorySessions, + loadSession, + deleteSession, + startNewSession, + sendMessage, + } = useChatbotStore() + + const [inputText, setInputText] = useState('') + const messagesEndRef = useRef(null) const { ref: setDroppableRef, isDropTarget: isOver } = useDroppable({ id: 'chatbot-sidebar', @@ -14,70 +40,282 @@ export function ChatbotSidebar() { }, }) + // Auto-scroll messages list to bottom + useEffect(() => { + if (!isHistoryMode) { + messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }) + } + }, [messages, isStreaming, isHistoryMode]) + + // Fetch history when entering history mode + useEffect(() => { + if (isHistoryMode) { + fetchHistorySessions() + } + }, [isHistoryMode, fetchHistorySessions]) + + const handleSend = () => { + if (!inputText.trim() || isStreaming) return + sendMessage(inputText, projectId, contextAssetId) + setInputText('') + } + + const handleKeyDown = (e: React.KeyboardEvent) => { + if (e.key === 'Enter' && !e.shiftKey) { + e.preventDefault() + handleSend() + } + } + + const getMessageText = (content: unknown): string => { + if (typeof content === 'string') return content + if (Array.isArray(content)) { + return content + .map((c) => { + if (typeof c === 'string') return c + if (c && typeof c === 'object' && 'type' in c && c.type === 'text' && 'text' in c) { + return String((c as { text: unknown }).text) + } + return '' + }) + .join('') + } + return '' + } + + const preprocessMarkdown = (text: string): string => { + return text.replace(/<@([a-zA-Z0-9_-]+)>/g, '@$1').replace(/^\[([^\]]+)\]:/gm, '\\[$1\\]:') + } + + const renderMessage = (msg: ChatMessage) => { + const isUser = msg.role === 'user' + const isAssistant = msg.role === 'assistant' + const isTool = msg.role === 'toolResult' + const isSystem = (msg.role as string) === 'thinking_level_change' || msg.role === 'custom' + + if (isUser) { + return ( +
+
+ {getMessageText(msg.content)} +
+
+ ) + } + + if (isAssistant) { + return ( +
+
+ {preprocessMarkdown(getMessageText(msg.content))} +
+
+ {m.created_by_agent()} +
+
+ ) + } + + if (isTool) { + return ( +
+ + {m.tool_result_with_name({ name: (msg as { toolName?: string }).toolName || 'tool' })} + +
+ ) + } + + if (isSystem) { + const systemContent = 'content' in msg ? msg.content : '' + return ( +
+ {getMessageText(systemContent)} +
+ ) + } + + return null + } + return (
+ {/* Header section */}
-
- - {m.chatbot_context()} -
- {chatAssets.length > 0 && ( - + {isHistoryMode ? ( + <> +
+ + {m.history()} +
+ + + ) : ( + <> +
+ + {m.shumai_agent()} +
+
+ + +
+ )}
-
- {chatAssets.length === 0 ? ( -
- -

{m.chatbot_drag_drop_hint()}

-
- ) : ( -
-

- {m.context_assets_count({ count: chatAssets.length })} -

-
- {chatAssets.map((asset) => ( -
-
- {asset.type === 'folder' ? ( - - ) : ( - + {/* Main body view */} + {isHistoryMode ? ( + + {historySessions.length === 0 ? ( +
+ +

{m.no_history_sessions()}

+
+ ) : ( +
+ {historySessions.map((sess) => { + const isActive = sess.id === currentSessionId + return ( +
{asset.name} - - {asset.type} - -
- -
- ))} +
loadSession(sess.id)} className="flex-1 min-w-0 pr-6"> +
+ {m.shumai_agent()} +
+
+ {new Date(sess.createdAt).toLocaleString()} +
+
+ +
+ ) + })} +
+ )} + + ) : ( + <> + {/* Active Chat view */} + + {messages.length === 0 ? ( +
+ +

{m.chatbot_drag_drop_hint()}

+
+ ) : ( +
+ {messages.map(renderMessage)} + {isStreaming && + messages.length > 0 && + messages[messages.length - 1].role === 'user' && ( +
+ + Thinking... +
+ )} +
+
+ )} + + + {/* Context assets above input area */} +
+ {chatAssets.length > 0 && ( +
+ {chatAssets.map((asset) => ( +
+
+ {asset.name} + + {asset.type} + +
+ +
+ ))} +
+ )} + + {/* Input area */} +
+