feat(chat): native React chat page with session selector and streaming

Replace the iframe-based Control UI embed with a native React
implementation that communicates directly with the Gateway via
gateway:rpc IPC calls and chat event streaming.

New components:
- ChatToolbar: session selector dropdown, refresh button, thinking toggle
- ChatMessage: message bubbles with markdown (react-markdown + GFM),
  collapsible thinking blocks, tool use cards, image attachments
- ChatInput: textarea with Enter to send, Shift+Enter for new line
- message-utils: extractText/extractThinking/extractImages/extractToolUse
  ported from OpenClaw's message-extract.ts

Rewritten chat store with:
- Session management (sessions.list, switchSession)
- Proper chat.history loading with raw message preservation
- chat.send with idempotencyKey and run tracking
- Streaming via handleChatEvent (delta/final/error/aborted)
- Thinking toggle (show/hide reasoning blocks)
This commit is contained in:
Haze
2026-02-06 04:49:01 +08:00
Unverified
parent bdb734120f
commit 3468d1bdf4
6 changed files with 937 additions and 266 deletions

View File

@@ -0,0 +1,85 @@
/**
* Chat Input Component
* Textarea with send button. Enter to send, Shift+Enter for new line.
*/
import { useState, useRef, useEffect, useCallback } from 'react';
import { Send, Square } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Textarea } from '@/components/ui/textarea';
interface ChatInputProps {
onSend: (text: string) => void;
disabled?: boolean;
sending?: boolean;
}
export function ChatInput({ onSend, disabled = false, sending = false }: ChatInputProps) {
const [input, setInput] = useState('');
const textareaRef = useRef<HTMLTextAreaElement>(null);
// Auto-resize textarea
useEffect(() => {
if (textareaRef.current) {
textareaRef.current.style.height = 'auto';
textareaRef.current.style.height = `${Math.min(textareaRef.current.scrollHeight, 200)}px`;
}
}, [input]);
const handleSend = useCallback(() => {
const trimmed = input.trim();
if (!trimmed || disabled || sending) return;
onSend(trimmed);
setInput('');
// Reset textarea height
if (textareaRef.current) {
textareaRef.current.style.height = 'auto';
}
}, [input, disabled, sending, onSend]);
const handleKeyDown = useCallback(
(e: React.KeyboardEvent) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
handleSend();
}
},
[handleSend],
);
return (
<div className="border-t bg-background p-4">
<div className="flex items-end gap-2 max-w-4xl mx-auto">
<div className="flex-1 relative">
<Textarea
ref={textareaRef}
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyDown={handleKeyDown}
placeholder={disabled ? 'Gateway not connected...' : 'Message (Enter to send, Shift+Enter for new line)'}
disabled={disabled}
className="min-h-[44px] max-h-[200px] resize-none pr-4"
rows={1}
/>
</div>
<Button
onClick={handleSend}
disabled={!input.trim() || disabled}
size="icon"
className="shrink-0 h-10 w-10"
variant={sending ? 'destructive' : 'default'}
>
{sending ? (
<Square className="h-4 w-4" />
) : (
<Send className="h-4 w-4" />
)}
</Button>
</div>
<p className="text-xs text-muted-foreground mt-1.5 text-center">
Enter to send, Shift+Enter for new line
</p>
</div>
);
}