From 3de57fd1763530b6dfd4d627345f623c781df919 Mon Sep 17 00:00:00 2001 From: yiling Date: Sun, 12 Jul 2026 15:18:31 +0800 Subject: [PATCH] feat(webui): refactor chatbot message rendering to type-safe nested switch --- packages/webui/components/chatbot-sidebar.tsx | 287 ++++++++++++------ .../webui/components/file-system-manager.tsx | 8 +- 2 files changed, 207 insertions(+), 88 deletions(-) diff --git a/packages/webui/components/chatbot-sidebar.tsx b/packages/webui/components/chatbot-sidebar.tsx index 7a78c01e..f48b18b7 100644 --- a/packages/webui/components/chatbot-sidebar.tsx +++ b/packages/webui/components/chatbot-sidebar.tsx @@ -4,7 +4,18 @@ import { m } from '@/ui/paraglide/messages.js' import { useChatbotStore } from '@/ui/stores/chatbot' import { useDroppable } from '@dnd-kit/react' import type { ChatMessage } from '@shumai/dtos' -import { ArrowLeft, ArrowUp, Bot, History, Loader2, Plus, Trash2 } from 'lucide-react' +import { + ArrowLeft, + ArrowUp, + Bot, + Brain, + ChevronDown, + History, + Loader2, + Plus, + Trash2, + Wrench, +} from 'lucide-react' import { useEffect, useRef, useState } from 'react' import Markdown from 'react-markdown' import { formatTimeAgo } from '../lib/time' @@ -91,98 +102,200 @@ export function ChatbotSidebar({ projectId, contextAssetId }: ChatbotSidebarProp } const renderMessage = (msg: ChatMessage) => { - if ( - msg.role === 'custom' && - (msg as { customType?: string }).customType === 'context_display_info' - ) { - const details = msg.details as - | { assets?: Array<{ id: string; name: string; type: string }> } - | undefined - const assets = details?.assets || [] - if (assets.length === 0) return null - return ( -
-
{m.assets_added_to_context()}
- -
- ) - } - - 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)} + switch (msg.role as string) { + case 'user': { + const msgObj = msg as unknown as Record + return ( +
+
+ {getMessageText(msgObj.content)} +
-
- ) - } + ) + } - if (isAssistant) { - return ( -
-
- tag - pre: ({ node, ...props }) => ( -
-                ),
-              }}
+      case 'assistant': {
+        const msgObj = msg as unknown as Record
+        const content = msgObj.content as unknown[]
+        if (!content) return null
+
+        const renderedBlocks: React.ReactNode[] = []
+
+        content.forEach((block: unknown, idx: number) => {
+          if (!block || typeof block !== 'object') return
+
+          const b = block as unknown as Record
+
+          switch (b.type) {
+            case 'text': {
+              const text = b.text
+              if (typeof text !== 'string' || !text.trim()) return
+              renderedBlocks.push(
+                
+ ( +
+                      ),
+                    }}
+                  >
+                    {preprocessMarkdown(text)}
+                  
+                
, + ) + break + } + case 'thinking': { + const thinking = b.thinking + if (typeof thinking !== 'string' || !thinking.trim()) return + renderedBlocks.push( +
+ + + {m.role_thinking() || 'Thinking'} + + +
+ {preprocessMarkdown(thinking)} +
+
, + ) + break + } + case 'toolCall': { + const toolName = String(b.name || b.toolName || m.unknown()) + const toolArgs = b.arguments || b.args + renderedBlocks.push( +
+
+ + {m.calling_tool()} + + {toolName} + +
+ {!!toolArgs && ( +
+                      {JSON.stringify(toolArgs, null, 2)}
+                    
+ )} +
, + ) + break + } + case 'image': { + const mimeType = String(b.mimeType || 'image/png') + const data = String(b.data || '') + renderedBlocks.push( +
+ {data ? ( + Attached asset + ) : ( +
+ {m.image_object()} +
+ )} +
, + ) + break + } + default: { + const stringified = JSON.stringify(block, null, 2) + if (stringified === '{}') return + renderedBlocks.push( +
+                  {stringified}
+                
, + ) + } + } + }) + + if (renderedBlocks.length === 0) return null + + return ( +
+
{renderedBlocks}
+
+ {m.created_by_agent()} +
+
+ ) + } + + case 'toolResult': { + return ( +
+ + {m.tool_result_with_name({ name: (msg as { toolName?: string }).toolName || 'tool' })} + +
+ ) + } + + case 'thinking_level_change': + case 'custom': { + const msgObj = msg as unknown as Record + if (msgObj.customType === 'context_display_info') { + const details = msgObj.details as + | { assets?: Array<{ id: string; name: string; type: string }> } + | undefined + const assets = details?.assets || [] + if (assets.length === 0) return null + return ( +
- {preprocessMarkdown(getMessageText(msg.content))} - +
{m.assets_added_to_context()}
+
    + {assets.map((asset) => ( +
  • + {asset.name} ({asset.type}) +
  • + ))} +
+
+ ) + } + + const systemContent = msgObj.content || '' + return ( +
+ {getMessageText(systemContent)}
-
- {m.created_by_agent()} -
-
- ) - } + ) + } - if (isTool) { - return ( -
- - {m.tool_result_with_name({ name: (msg as { toolName?: string }).toolName || 'tool' })} - -
- ) + default: + return null } - - if (isSystem) { - const systemContent = 'content' in msg ? msg.content : '' - return ( -
- {getMessageText(systemContent)} -
- ) - } - - return null } return ( diff --git a/packages/webui/components/file-system-manager.tsx b/packages/webui/components/file-system-manager.tsx index fcb5febd..c075975a 100644 --- a/packages/webui/components/file-system-manager.tsx +++ b/packages/webui/components/file-system-manager.tsx @@ -1,7 +1,13 @@ import { client } from '@/ui/api/client' import { usePermissions } from '@/ui/hooks/use-permissions' import { m } from '@/ui/paraglide/messages.js' -import type { AssetInfo, AssetInfoPaginatedList, CollectionInfo, SearchCondition, SearchSort } from '@shumai/dtos' +import type { + AssetInfo, + AssetInfoPaginatedList, + CollectionInfo, + SearchCondition, + SearchSort, +} from '@shumai/dtos' import { type FieldInfo as MetadataFieldInfo } from '@shumai/dtos' import { useMutation } from '@tanstack/react-query' import { InferRequestType, InferResponseType } from 'hono/client'