Skip to content

LLM Wiki is a cross-platform desktop application that turns your documents into an organized, interlinked knowledge base — automatically. Instead of traditional RAG (retrieve-and-answer from scratch every time), the LLM incrementally builds and maintains a persistent wiki from your sources。

TypeScript

feat: add chat UI with streaming message display

nash_su5db82ac8ffc9
diff --git a/src/components/chat/chat-input.tsx b/src/components/chat/chat-input.tsx
new file mode 100644
index 00000000..12e13da6
--- /dev/null
+++ b/src/components/chat/chat-input.tsx
@@ -0,0 +1,79 @@
+import { useRef, useState, useCallback } from "react"
+import { Send, Square } from "lucide-react"
+import { Button } from "@/components/ui/button"
+
+interface ChatInputProps {
+ onSend: (text: string) => void
+ onStop: () => void
+ isStreaming: boolean
+ placeholder?: string
+}
+
+export function ChatInput({ onSend, onStop, isStreaming, placeholder }: ChatInputProps) {
+ const [value, setValue] = useState("")
+ const textareaRef = useRef<HTMLTextAreaElement>(null)
+
+ const handleInput = useCallback((e: React.ChangeEvent<HTMLTextAreaElement>) => {
+ setValue(e.target.value)
+ const ta = e.target
+ ta.style.height = "auto"
+ ta.style.height = `${Math.min(ta.scrollHeight, 120)}px`
+ }, [])
+
+ const handleSend = useCallback(() => {
+ const trimmed = value.trim()
+ if (!trimmed || isStreaming) return
+ onSend(trimmed)
+ setValue("")
+ if (textareaRef.current) {
+ textareaRef.current.style.height = "auto"
+ }
+ }, [value, isStreaming, onSend])
+
+ const handleKeyDown = useCallback(
+ (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
+ if (e.key === "Enter" && !e.shiftKey) {
+ e.preventDefault()
+ handleSend()
+ }
+ },
+ [handleSend],
+ )
+
+ return (
+ <div className="flex items-end gap-2 border-t p-3">
+ <textarea
+ ref={textareaRef}
+ value={value}
+ onChange={handleInput}
+ onKeyDown={handleKeyDown}
+ placeholder={placeholder ?? "Type a message... (Enter to send, Shift+Enter for newline)"}
+ disabled={isStreaming}
+ rows={1}
+ className="flex-1 resize-none rounded-md border bg-background px-3 py-2 text-sm placeholder:text-muted-foreground focus:outline-none focus:ring-1 focus:ring-ring disabled:cursor-not-allowed disabled:opacity-50"
+ style={{ maxHeight: "120px", overflowY: "auto" }}
+ />
+ {isStreaming ? (
+ <Button
+ variant="destructive"
+ size="icon"
+ onClick={onStop}
+ className="shrink-0"
+ title="Stop generation"
+ >
+ <Square className="h-4 w-4" />
+ </Button>
+ ) : (
+ <Button
+ size="icon"
+ onClick={handleSend}
+ disabled={!value.trim()}
+ className="shrink-0"
+ title="Send message"
+ >
+ <Send className="h-4 w-4" />
+ </Button>
+ )}
+ </div>
+ )
+}
diff --git a/src/components/chat/chat-message.tsx b/src/components/chat/chat-message.tsx
new file mode 100644
index 00000000..54053eaa
--- /dev/null
+++ b/src/components/chat/chat-message.tsx
@@ -0,0 +1,51 @@
+import { Bot, User } from "lucide-react"
+import type { DisplayMessage } from "@/stores/chat-store"
+
+interface ChatMessageProps {
+ message: DisplayMessage
+}
+
+export function ChatMessage({ message }: ChatMessageProps) {
+ const isUser = message.role === "user"
+
+ return (
+ <div className={`flex gap-2 ${isUser ? "flex-row-reverse" : "flex-row"}`}>
+ <div
+ className={`flex h-7 w-7 shrink-0 items-center justify-center rounded-full ${
+ isUser ? "bg-primary text-primary-foreground" : "bg-muted text-muted-foreground"
+ }`}
+ >
+ {isUser ? <User className="h-4 w-4" /> : <Bot className="h-4 w-4" />}
+ </div>
+ <div
+ className={`max-w-[80%] rounded-lg px-3 py-2 text-sm ${
+ isUser
+ ? "bg-primary text-primary-foreground"
+ : "bg-muted text-foreground"
+ }`}
+ >
+ <p className="whitespace-pre-wrap break-words">{message.content}</p>
+ </div>
+ </div>
+ )
+}
+
+interface StreamingMessageProps {
+ content: string
+}
+
+export function StreamingMessage({ content }: StreamingMessageProps) {
+ return (
+ <div className="flex gap-2 flex-row">
+ <div className="flex h-7 w-7 shrink-0 items-center justify-center rounded-full bg-muted text-muted-foreground">
+ <Bot className="h-4 w-4" />
+ </div>
+ <div className="max-w-[80%] rounded-lg px-3 py-2 text-sm bg-muted text-foreground">
+ <p className="whitespace-pre-wrap break-words">
+ {content}
+ <span className="animate-pulse">▊</span>
+ </p>
+ </div>
+ </div>
+ )
+}
diff --git a/src/components/chat/chat-panel.tsx b/src/components/chat/chat-panel.tsx
new file mode 100644
index 00000000..0a5ec412
--- /dev/null
+++ b/src/components/chat/chat-panel.tsx
@@ -0,0 +1,132 @@
+import { useRef, useEffect, useCallback } from "react"
+import { BookOpen } from "lucide-react"
+import { ScrollArea } from "@/components/ui/scroll-area"
+import { Button } from "@/components/ui/button"
+import { ChatMessage, StreamingMessage } from "./chat-message"
+import { ChatInput } from "./chat-input"
+import { useChatStore, chatMessagesToLLM } from "@/stores/chat-store"
+import { useWikiStore } from "@/stores/wiki-store"
+import { streamChat } from "@/lib/llm-client"
+import { executeIngestWrites } from "@/lib/ingest"
+import { listDirectory } from "@/commands/fs"
+
+export function ChatPanel() {
+ const messages = useChatStore((s) => s.messages)
+ const isStreaming = useChatStore((s) => s.isStreaming)
+ const streamingContent = useChatStore((s) => s.streamingContent)
+ const mode = useChatStore((s) => s.mode)
+ const addMessage = useChatStore((s) => s.addMessage)
+ const setStreaming = useChatStore((s) => s.setStreaming)
+ const appendStreamToken = useChatStore((s) => s.appendStreamToken)
+ const finalizeStream = useChatStore((s) => s.finalizeStream)
+
+ const project = useWikiStore((s) => s.project)
+ const llmConfig = useWikiStore((s) => s.llmConfig)
+ const setFileTree = useWikiStore((s) => s.setFileTree)
+
+ const abortRef = useRef<AbortController | null>(null)
+ const bottomRef = useRef<HTMLDivElement>(null)
+
+ useEffect(() => {
+ bottomRef.current?.scrollIntoView({ behavior: "smooth" })
+ }, [messages, streamingContent])
+
+ const handleSend = useCallback(
+ async (text: string) => {
+ addMessage("user", text)
+ setStreaming(true)
+
+ const allMessages = useChatStore.getState().messages
+ const llmMessages = chatMessagesToLLM(allMessages)
+
+ const controller = new AbortController()
+ abortRef.current = controller
+
+ let accumulated = ""
+
+ await streamChat(
+ llmConfig,
+ llmMessages,
+ {
+ onToken: (token) => {
+ accumulated += token
+ appendStreamToken(token)
+ },
+ onDone: () => {
+ finalizeStream(accumulated)
+ abortRef.current = null
+ },
+ onError: (err) => {
+ finalizeStream(`Error: ${err.message}`)
+ abortRef.current = null
+ },
+ },
+ controller.signal,
+ )
+ },
+ [llmConfig, addMessage, setStreaming, appendStreamToken, finalizeStream],
+ )
+
+ const handleStop = useCallback(() => {
+ abortRef.current?.abort()
+ abortRef.current = null
+ }, [])
+
+ const handleWriteToWiki = useCallback(async () => {
+ if (!project) return
+ try {
+ await executeIngestWrites(project.path, llmConfig, undefined, undefined)
+ // Refresh the file tree after writes
+ try {
+ const tree = await listDirectory(`${project.path}/wiki`)
+ setFileTree(tree)
+ } catch {
+ // ignore tree refresh failure
+ }
+ } catch (err) {
+ console.error("Failed to write to wiki:", err)
+ }
+ }, [project, llmConfig, setFileTree])
+
+ const hasAssistantMessages = messages.some((m) => m.role === "assistant")
+ const showWriteButton = mode === "ingest" && !isStreaming && hasAssistantMessages
+
+ return (
+ <div className="flex h-full flex-col">
+ <ScrollArea className="flex-1 px-3 py-2">
+ <div className="flex flex-col gap-3">
+ {messages.map((msg) => (
+ <ChatMessage key={msg.id} message={msg} />
+ ))}
+ {isStreaming && <StreamingMessage content={streamingContent} />}
+ <div ref={bottomRef} />
+ </div>
+ </ScrollArea>
+
+ {showWriteButton && (
+ <div className="border-t px-3 py-2">
+ <Button
+ variant="outline"
+ size="sm"
+ onClick={handleWriteToWiki}
+ className="w-full gap-2"
+ >
+ <BookOpen className="h-4 w-4" />
+ Write to Wiki
+ </Button>
+ </div>
+ )}
+
+ <ChatInput
+ onSend={handleSend}
+ onStop={handleStop}
+ isStreaming={isStreaming}
+ placeholder={
+ mode === "ingest"
+ ? "Discuss the source or ask follow-up questions..."
+ : "Type a message..."
+ }
+ />
+ </div>
+ )
+}
diff --git a/src/components/layout/chat-bar.tsx b/src/components/layout/chat-bar.tsx
index 94280b3f..ea948af7 100644
--- a/src/components/layout/chat-bar.tsx
+++ b/src/components/layout/chat-bar.tsx
@@ -1,5 +1,6 @@
import { MessageSquare, ChevronUp, ChevronDown } from "lucide-react"
import { useWikiStore } from "@/stores/wiki-store"
+import { ChatPanel } from "@/components/chat/chat-panel"
export function ChatBar() {
const chatExpanded = useWikiStore((s) => s.chatExpanded)
@@ -32,18 +33,8 @@ export function ChatBar() {
</span>
<ChevronDown className="h-4 w-4" />
</button>
- <div className="flex-1 overflow-auto p-4">
- <p className="text-sm text-muted-foreground">
- Chat will be available in Phase 2 (LLM Integration).
- </p>
- </div>
- <div className="border-t p-3">
- <input
- type="text"
- placeholder="Type a message..."
- disabled
- className="w-full rounded-md border bg-muted/50 px-3 py-2 text-sm"
- />
+ <div className="flex-1 overflow-hidden">
+ <ChatPanel />
</div>
</div>
)