Build production-grade chat interfaces with real-time messaging, state management, and accessibility compliance. This skill should be used when users ask to create chat UIs, messaging components, conversational interfaces, or real-time communication frontends. Triggers on: "chat interface", "chat UI", "messaging component", "conversational UI", "real-time chat", "chat widget", "build chat", "chatbox", "chat component", "live messaging".
Design and implement production-grade chat interfaces with real-time messaging, robust state management, and WCAG-compliant accessibility.
fastapi-backend)Gather context to ensure successful implementation:
| Source | Gather |
|--------|--------|
| Codebase | Existing UI framework, component library, styling approach |
| Conversation | User's specific chat requirements and constraints |
| Skill References | Domain patterns from references/ (transport, state, a11y) |
| User Guidelines | Project conventions, team standards |
Ensure all required context is gathered before implementing. Only ask user for THEIR specific requirements (domain expertise is in this skill).
| Question | Default | |----------|---------| | Framework | React (most reference patterns available) | | Transport | WebSocket (best real-time UX) | | Message types | Text + markdown | | Theming | CSS custom properties with light/dark | | Persistence | None (stateless) | | Auth | Token-based (header) | | Typing indicators | Not included |
ChatProvider (context/state)
|
+-- ConnectionManager (transport layer)
| +-- WebSocketAdapter | SSEAdapter | PollingAdapter
|
+-- ChatWindow (layout container)
+-- MessageList (virtualized, accessible)
| +-- MessageBubble (user | assistant | system)
| +-- TypingIndicator
+-- InputBar
| +-- TextInput (auto-resize, keyboard shortcuts)
| +-- SendButton
| +-- AttachmentButton (optional)
+-- StatusBar (connection state, errors)
The generated chat interface artifact includes:
| Artifact | Format | Contents | |----------|--------|----------| | ChatProvider | Framework context/store | State management, transport hookup, message dispatch | | MessageList | Virtualized list component | Accessible, auto-scroll, role-based styling | | InputBar | Form component | Auto-resize textarea, Enter to send, Shift+Enter newline | | ConnectionManager | Transport adapter | WebSocket/SSE/polling with reconnect logic | | Styles | CSS custom properties | Theme-ready, dark mode, responsive breakpoints | | Types | TypeScript interfaces | ChatMessage, ChatEvent, ConnectionStatus |
Use scripts/scaffold_chat.py to generate the initial file structure.
Use templates from assets/templates/ as starting points.
| Protocol | Use When | Latency | Complexity | |----------|----------|---------|------------| | WebSocket | Bidirectional real-time (multiplayer, live chat) | ~10ms | Medium | | SSE + POST | Server-push with client POST for sends | ~50ms | Low | | Polling | Legacy browsers, simple use cases | ~1-5s | Lowest |
class ChatWebSocket {
private ws: WebSocket | null = null;
private reconnectAttempts = 0;
private maxReconnectDelay = 30000;
connect(url: string): void {
this.ws = new WebSocket(url);
this.ws.onopen = () => { this.reconnectAttempts = 0; };
this.ws.onclose = () => { this.reconnectWithBackoff(); };
this.ws.onmessage = (e) => { this.handleMessage(JSON.parse(e.data)); };
}
private reconnectWithBackoff(): void {
const delay = Math.min(1000 * 2 ** this.reconnectAttempts, this.maxReconnectDelay);
this.reconnectAttempts++;
setTimeout(() => this.connect(this.url), delay);
}
}
const eventSource = new EventSource('/api/chat/stream');
eventSource.onmessage = (e) => handleMessage(JSON.parse(e.data));
async function sendMessage(content: string) {
await fetch('/api/chat/send', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ content, session_id: sessionId }),
});
}
interface ChatMessage {
id: string; // UUID
role: 'user' | 'assistant' | 'system';
content: string;
content_type: 'text' | 'markdown' | 'code' | 'image';
timestamp: string; // ISO 8601
status: 'sending' | 'sent' | 'delivered' | 'error';
metadata?: {
citations?: Citation[];
attachments?: Attachment[];
tokens_used?: number;
};
}
interface ChatEvent {
type: 'message' | 'typing' | 'status' | 'error' | 'connection';
payload: ChatMessage | TypingState | ConnectionState | ErrorPayload;
}
DISCONNECTED --> CONNECTING --> CONNECTED --> IDLE
| | |
v v v
ERROR SENDING --> STREAMING --> IDLE
| | |
v v v
RECONNECTING ERROR ERROR --> IDLE
States and transitions: see references/state-machine.md.
<button> not <div onclick>, <ul> for message listsaria-live="polite" regioninnerHTML for user content (XSS risk + inaccessible)innerHTML / dangerouslySetInnerHTML with user content| Pattern | When | How |
|---------|------|-----|
| Virtualized list | >100 messages visible | react-window / @tanstack/virtual |
| Debounced typing | Typing indicators | 300ms debounce on keystroke events |
| Message batching | High-throughput chat | Batch renders per animation frame |
| Optimistic updates | Send UX | Show message immediately, reconcile on ACK |
| Connection pooling | Multiple chat rooms | Share single WS connection, multiplex |
| Script | Purpose | Usage |
|--------|---------|-------|
| scripts/scaffold_chat.py | Generate initial chat file structure | python scripts/scaffold_chat.py --framework react --transport websocket --output ./src/chat |
| File | When to Read |
|------|--------------|
| references/transport-patterns.md | Implementing WebSocket, SSE, or polling layer |
| references/state-machine.md | Designing connection/message/session state flows |
| references/component-patterns.md | Building React/Vue/Svelte components |
| references/accessibility-guide.md | Ensuring WCAG 2.2 AA compliance |
| Resource | URL | Use For | |----------|-----|---------| | WCAG 2.2 | https://www.w3.org/TR/WCAG22/ | Accessibility compliance | | WAI-ARIA Practices | https://www.w3.org/WAI/ARIA/apg/ | ARIA role patterns | | WebSocket API | https://developer.mozilla.org/en-US/docs/Web/API/WebSocket | WS implementation | | SSE API | https://developer.mozilla.org/en-US/docs/Web/API/EventSource | SSE implementation | | OWASP XSS Prevention | https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html | Security |
For patterns not covered here, fetch from official docs above.
npx skills add assadsharif/building-chat-interface下载完整 Skill 目录,包含 SKILL.md 及所有相关文件
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer