feat(ui): 创建共享 UI 组件包

将 web 和 desktop 的重复代码抽取到 @ai-assistant/ui 包:

- 添加可配置的 API 客户端 (configureApiClient)
- 迁移共享组件: ChatMessage, ChatInput, Sidebar, FileBrowser, ConfigPanel
- 迁移共享 hook: useChat
- 添加 responsive prop 支持响应式布局
- 更新 web/desktop 依赖并删除重复代码
This commit is contained in:
2025-12-12 15:52:53 +08:00
parent 563224fa73
commit 68ab6a2016
30 changed files with 711 additions and 1388 deletions
+1
View File
@@ -10,6 +10,7 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@ai-assistant/ui": "workspace:*",
"clsx": "^2.1.0",
"lucide-react": "^0.344.0",
"react": "^18.3.1",
+16 -6
View File
@@ -5,11 +5,15 @@
*/
import { useState, useEffect, useCallback } from 'react';
import { Sidebar } from './components/Sidebar';
import {
Sidebar,
FileBrowser,
ConfigPanel,
listSessions,
createSession,
type Session,
} from '@ai-assistant/ui';
import { ChatPage } from './pages/Chat';
import { FileBrowser } from './components/FileBrowser';
import { ConfigPanel } from './components/ConfigPanel';
import { listSessions, createSession, type Session } from './api/client';
export function App() {
const [currentSessionId, setCurrentSessionId] = useState<string | null>(null);
@@ -76,6 +80,7 @@ export function App() {
currentSessionId={currentSessionId}
onSelectSession={handleSelectSession}
onCreateSession={handleCreateSession}
responsive
/>
{/* 工具栏按钮 - 移动端右移避开菜单按钮 */}
@@ -126,7 +131,12 @@ export function App() {
{/* 聊天区域 */}
<div className={`flex-1 min-w-0 ${showFileBrowser ? 'hidden md:block md:w-1/2' : 'w-full'}`}>
{currentSessionId ? (
<ChatPage key={currentSessionId} sessionId={currentSessionId} onSessionNotFound={handleSessionNotFound} />
<ChatPage
key={currentSessionId}
sessionId={currentSessionId}
onSessionNotFound={handleSessionNotFound}
responsive
/>
) : (
<div className="flex-1 flex items-center justify-center h-full">
<p className="text-gray-400">Select or create a session</p>
@@ -172,7 +182,7 @@ export function App() {
</div>
{/* 配置面板 */}
{showConfig && <ConfigPanel onClose={() => setShowConfig(false)} />}
{showConfig && <ConfigPanel onClose={() => setShowConfig(false)} responsive />}
{/* 移动端底部文件按钮 */}
<button
-181
View File
@@ -1,181 +0,0 @@
/**
* API Client for Web
*/
export interface Session {
id: string;
name?: string;
createdAt: string;
updatedAt: string;
status: string;
messageCount: number;
}
export interface Message {
id: string;
role: 'user' | 'assistant' | 'system';
content: string;
timestamp: string;
}
export interface HealthStatus {
status: string;
timestamp: string;
agent: {
coreAvailable: boolean;
};
auth: {
enabled: boolean;
tokenCount: number;
};
stats: {
sessions: number;
websocket: { connections: number };
sse: { connections: number };
};
}
const API_BASE = '/api';
async function request<T>(method: string, path: string, body?: unknown): Promise<T> {
const response = await fetch(`${API_BASE}${path}`, {
method,
headers: {
'Content-Type': 'application/json',
},
body: body ? JSON.stringify(body) : undefined,
});
if (!response.ok) {
const error = await response.json().catch(() => ({ error: response.statusText }));
throw new Error(error.error || `HTTP ${response.status}`);
}
return response.json();
}
// Health
export async function getHealth(): Promise<HealthStatus> {
return request('GET', '/../health');
}
// Sessions
export async function listSessions(): Promise<{ success: boolean; data: Session[] }> {
return request('GET', '/sessions');
}
export async function createSession(name?: string): Promise<{ success: boolean; data: Session }> {
return request('POST', '/sessions', { name });
}
export async function getSession(id: string): Promise<{ success: boolean; data: Session }> {
return request('GET', `/sessions/${id}`);
}
export async function deleteSession(id: string): Promise<{ success: boolean }> {
return request('DELETE', `/sessions/${id}`);
}
// Messages
export async function getMessages(sessionId: string): Promise<{ success: boolean; data: Message[] }> {
return request('GET', `/sessions/${sessionId}/messages`);
}
export async function sendMessage(
sessionId: string,
content: string
): Promise<{ success: boolean; data: Message }> {
return request('POST', `/sessions/${sessionId}/messages`, { content });
}
// WebSocket
export function createWebSocket(sessionId: string): WebSocket {
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const host = window.location.host;
return new WebSocket(`${protocol}//${host}/api/ws/${sessionId}`);
}
// Files
export interface FileInfo {
name: string;
path: string;
type: 'file' | 'directory';
size: number;
modified: string;
extension?: string;
}
export interface FileListResponse {
success: boolean;
data: {
path: string;
absolutePath: string;
parent: string | null;
files: FileInfo[];
};
}
export interface FileReadResponse {
success: boolean;
data: {
path: string;
name: string;
type: string;
size: number;
modified: string;
content: string;
encoding: 'utf-8' | 'base64';
};
}
export interface FileTreeNode {
name: string;
path: string;
type: 'file' | 'directory';
children?: FileTreeNode[];
}
export interface FileTreeResponse {
success: boolean;
data: {
path: string;
tree: FileTreeNode[];
};
}
export async function getWorkingDirectory(): Promise<{ success: boolean; data: { workingDirectory: string; separator: string } }> {
return request('GET', '/files');
}
export async function listFiles(path: string = '.', showHidden: boolean = false): Promise<FileListResponse> {
const params = new URLSearchParams({ path });
if (showHidden) params.set('hidden', 'true');
return request('GET', `/files/list?${params}`);
}
export async function readFile(path: string): Promise<FileReadResponse> {
return request('GET', `/files/read?path=${encodeURIComponent(path)}`);
}
export async function getFileTree(path: string = '.', depth: number = 3): Promise<FileTreeResponse> {
const params = new URLSearchParams({ path, depth: String(depth) });
return request('GET', `/files/tree?${params}`);
}
// Config
export interface ServerConfig {
model: string;
maxTokens: number;
temperature: number;
workdir: string;
allowedPaths: string[];
deniedPaths: string[];
}
export async function getConfig(): Promise<{ success: boolean; data: ServerConfig }> {
return request('GET', '/config');
}
export async function updateConfig(config: Partial<ServerConfig>): Promise<{ success: boolean; data: ServerConfig }> {
return request('PATCH', '/config', config);
}
-97
View File
@@ -1,97 +0,0 @@
/**
* Chat Input Component
*
* 响应式输入框:适配移动端键盘和触摸操作
*/
import { useState, useRef, useEffect } from 'react';
import { Send, Square } from 'lucide-react';
import clsx from 'clsx';
interface ChatInputProps {
onSend: (content: string) => void;
onCancel: () => void;
isLoading: boolean;
disabled?: boolean;
}
export function ChatInput({ onSend, onCancel, isLoading, disabled }: ChatInputProps) {
const [input, setInput] = useState('');
const textareaRef = useRef<HTMLTextAreaElement>(null);
// 自动调整高度
useEffect(() => {
const textarea = textareaRef.current;
if (textarea) {
textarea.style.height = 'auto';
// 移动端最大高度稍小
const maxHeight = window.innerWidth < 768 ? 120 : 200;
textarea.style.height = `${Math.min(textarea.scrollHeight, maxHeight)}px`;
}
}, [input]);
const handleSubmit = () => {
const trimmed = input.trim();
if (!trimmed || isLoading || disabled) return;
onSend(trimmed);
setInput('');
// 重置高度
if (textareaRef.current) {
textareaRef.current.style.height = 'auto';
}
};
const handleKeyDown = (e: React.KeyboardEvent) => {
// 移动端 Enter 直接发送,桌面端 Shift+Enter 换行
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
handleSubmit();
}
};
return (
<div className="border-t border-gray-700 p-3 md:p-4 bg-gray-900 safe-area-pb">
<div className="max-w-4xl mx-auto flex gap-2">
<div className="flex-1 relative">
<textarea
ref={textareaRef}
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyDown={handleKeyDown}
placeholder="Type a message..."
disabled={disabled}
rows={1}
className={clsx(
'w-full resize-none rounded-lg border border-gray-600 bg-gray-800',
'px-3 py-2.5 md:px-4 md:py-3',
'text-base md:text-sm', // 移动端使用 16px 防止缩放
'text-gray-100 placeholder-gray-500',
'focus:outline-none focus:ring-2 focus:ring-primary-500 focus:border-transparent',
'disabled:opacity-50 disabled:cursor-not-allowed'
)}
/>
</div>
<button
onClick={isLoading ? onCancel : handleSubmit}
disabled={!isLoading && (!input.trim() || disabled)}
className={clsx(
'px-3 py-2.5 md:px-4 md:py-3 rounded-lg flex items-center justify-center transition-colors',
'min-w-[44px] min-h-[44px]', // 最小触摸目标 44x44
isLoading
? 'bg-red-600 hover:bg-red-700 active:bg-red-800 text-white'
: 'bg-primary-600 hover:bg-primary-700 active:bg-primary-800 text-white',
'disabled:opacity-50 disabled:cursor-not-allowed'
)}
>
{isLoading ? <Square size={20} /> : <Send size={20} />}
</button>
</div>
{/* 移动端提示文字 */}
<p className="hidden md:block text-xs text-gray-500 text-center mt-2">
Press Enter to send, Shift+Enter for new line
</p>
</div>
);
}
@@ -1,80 +0,0 @@
/**
* Chat Message Component
*/
import { User, Bot } from 'lucide-react';
import clsx from 'clsx';
import type { Message } from '../api/client';
interface ChatMessageProps {
message: Message;
}
export function ChatMessage({ message }: ChatMessageProps) {
const isUser = message.role === 'user';
return (
<div
className={clsx(
'flex gap-4 p-4 rounded-lg',
isUser ? 'bg-gray-800' : 'bg-gray-850'
)}
>
<div
className={clsx(
'w-8 h-8 rounded-full flex items-center justify-center flex-shrink-0',
isUser ? 'bg-primary-600' : 'bg-green-600'
)}
>
{isUser ? <User size={18} /> : <Bot size={18} />}
</div>
<div className="flex-1 min-w-0">
<div className="text-sm text-gray-400 mb-1">
{isUser ? 'You' : 'AI Assistant'}
</div>
<div className="message-content whitespace-pre-wrap break-words">
{message.content}
</div>
</div>
</div>
);
}
interface StreamingMessageProps {
content: string;
}
export function StreamingMessage({ content }: StreamingMessageProps) {
return (
<div className="flex gap-4 p-4 rounded-lg bg-gray-850">
<div className="w-8 h-8 rounded-full flex items-center justify-center flex-shrink-0 bg-green-600">
<Bot size={18} />
</div>
<div className="flex-1 min-w-0">
<div className="text-sm text-gray-400 mb-1">AI Assistant</div>
<div className="message-content whitespace-pre-wrap break-words">
{content}
<span className="inline-block w-2 h-4 bg-gray-400 animate-pulse ml-1" />
</div>
</div>
</div>
);
}
export function TypingIndicator() {
return (
<div className="flex gap-4 p-4 rounded-lg bg-gray-850">
<div className="w-8 h-8 rounded-full flex items-center justify-center flex-shrink-0 bg-green-600">
<Bot size={18} />
</div>
<div className="flex-1">
<div className="text-sm text-gray-400 mb-1">AI Assistant</div>
<div className="typing-indicator">
<span />
<span />
<span />
</div>
</div>
</div>
);
}
-267
View File
@@ -1,267 +0,0 @@
/**
* ConfigPanel Component
*
* 配置面板:移动端全屏显示,桌面端居中弹窗
*/
import { useState, useEffect } from 'react';
import { getConfig, updateConfig, type ServerConfig } from '../api/client';
interface ConfigPanelProps {
onClose: () => void;
}
// 可用的模型列表
const AVAILABLE_MODELS = [
{ id: 'claude-sonnet-4-20250514', name: 'Claude Sonnet 4' },
{ id: 'claude-3-5-sonnet-20241022', name: 'Claude 3.5 Sonnet' },
{ id: 'claude-3-opus-20240229', name: 'Claude 3 Opus' },
{ id: 'claude-3-haiku-20240307', name: 'Claude 3 Haiku' },
];
export function ConfigPanel({ onClose }: ConfigPanelProps) {
const [config, setConfig] = useState<ServerConfig | null>(null);
const [loading, setLoading] = useState(true);
const [saving, setSaving] = useState(false);
const [error, setError] = useState<string | null>(null);
const [success, setSuccess] = useState(false);
// 表单状态
const [formData, setFormData] = useState({
model: '',
maxTokens: 8192,
temperature: 0.7,
workdir: '',
});
// 加载配置
useEffect(() => {
async function loadConfig() {
try {
const response = await getConfig();
setConfig(response.data);
setFormData({
model: response.data.model,
maxTokens: response.data.maxTokens,
temperature: response.data.temperature,
workdir: response.data.workdir,
});
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to load config');
} finally {
setLoading(false);
}
}
loadConfig();
}, []);
// 保存配置
const handleSave = async () => {
setSaving(true);
setError(null);
setSuccess(false);
try {
const response = await updateConfig(formData);
setConfig(response.data);
setSuccess(true);
setTimeout(() => setSuccess(false), 2000);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to save config');
} finally {
setSaving(false);
}
};
// 重置为默认值
const handleReset = () => {
if (config) {
setFormData({
model: config.model,
maxTokens: config.maxTokens,
temperature: config.temperature,
workdir: config.workdir,
});
}
};
if (loading) {
return (
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50">
<div className="bg-gray-800 rounded-lg p-6">
<div className="text-gray-400">Loading configuration...</div>
</div>
</div>
);
}
return (
<div className="fixed inset-0 bg-black/50 flex items-end md:items-center justify-center z-50">
{/* 移动端:从底部滑出的全屏面板;桌面端:居中弹窗 */}
<div className="bg-gray-800 w-full md:w-full md:max-w-lg md:mx-4 max-h-full md:max-h-[90vh] overflow-auto rounded-t-2xl md:rounded-lg">
{/* Header */}
<div className="sticky top-0 flex items-center justify-between px-4 md:px-6 py-4 border-b border-gray-700 bg-gray-800 z-10">
{/* 移动端拖动指示器 */}
<div className="absolute top-2 left-1/2 -translate-x-1/2 w-10 h-1 bg-gray-600 rounded-full md:hidden" />
<h2 className="text-lg font-semibold mt-2 md:mt-0">Configuration</h2>
<button
onClick={onClose}
className="p-2 hover:bg-gray-700 active:bg-gray-600 rounded-lg transition-colors min-w-[44px] min-h-[44px] flex items-center justify-center"
>
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
{/* Content */}
<div className="p-4 md:p-6 space-y-6">
{/* Error message */}
{error && (
<div className="p-3 bg-red-900/50 border border-red-700 rounded-lg text-red-300 text-sm">
{error}
</div>
)}
{/* Success message */}
{success && (
<div className="p-3 bg-green-900/50 border border-green-700 rounded-lg text-green-300 text-sm">
Configuration saved successfully!
</div>
)}
{/* Model */}
<div>
<label className="block text-sm font-medium text-gray-300 mb-2">
Model
</label>
<select
value={formData.model}
onChange={(e) => setFormData({ ...formData, model: e.target.value })}
className="w-full px-3 py-3 md:py-2 bg-gray-900 border border-gray-700 rounded-lg text-white text-base md:text-sm focus:outline-none focus:border-blue-500"
>
{AVAILABLE_MODELS.map((model) => (
<option key={model.id} value={model.id}>
{model.name}
</option>
))}
</select>
<p className="mt-1 text-xs text-gray-500">
Select the AI model to use for conversations
</p>
</div>
{/* Max Tokens */}
<div>
<label className="block text-sm font-medium text-gray-300 mb-2">
Max Tokens: {formData.maxTokens.toLocaleString()}
</label>
<input
type="range"
min="1024"
max="32768"
step="1024"
value={formData.maxTokens}
onChange={(e) => setFormData({ ...formData, maxTokens: parseInt(e.target.value) })}
className="w-full h-2 bg-gray-700 rounded-lg appearance-none cursor-pointer touch-pan-x"
/>
<div className="flex justify-between text-xs text-gray-500 mt-1">
<span>1K</span>
<span>8K</span>
<span>16K</span>
<span>32K</span>
</div>
<p className="mt-1 text-xs text-gray-500">
Maximum number of tokens in the response
</p>
</div>
{/* Temperature */}
<div>
<label className="block text-sm font-medium text-gray-300 mb-2">
Temperature: {formData.temperature.toFixed(2)}
</label>
<input
type="range"
min="0"
max="1"
step="0.05"
value={formData.temperature}
onChange={(e) => setFormData({ ...formData, temperature: parseFloat(e.target.value) })}
className="w-full h-2 bg-gray-700 rounded-lg appearance-none cursor-pointer touch-pan-x"
/>
<div className="flex justify-between text-xs text-gray-500 mt-1">
<span>Precise</span>
<span>Balanced</span>
<span>Creative</span>
</div>
<p className="mt-1 text-xs text-gray-500">
Controls randomness in responses
</p>
</div>
{/* Working Directory */}
<div>
<label className="block text-sm font-medium text-gray-300 mb-2">
Working Directory
</label>
<input
type="text"
value={formData.workdir}
onChange={(e) => setFormData({ ...formData, workdir: e.target.value })}
className="w-full px-3 py-3 md:py-2 bg-gray-900 border border-gray-700 rounded-lg text-white text-base md:text-sm focus:outline-none focus:border-blue-500 font-mono"
placeholder="/path/to/project"
/>
<p className="mt-1 text-xs text-gray-500">
Root directory for file operations
</p>
</div>
{/* Server Info (Read-only) */}
{config && (
<div className="pt-4 border-t border-gray-700">
<h3 className="text-sm font-medium text-gray-400 mb-3">Server Information</h3>
<div className="grid grid-cols-1 md:grid-cols-2 gap-3 text-sm">
<div className="flex justify-between md:block">
<span className="text-gray-500">Allowed Paths:</span>
<span className="md:ml-2 text-gray-300">
{config.allowedPaths.length || 'All'}
</span>
</div>
<div className="flex justify-between md:block">
<span className="text-gray-500">Denied Paths:</span>
<span className="md:ml-2 text-gray-300">
{config.deniedPaths.length || 'None'}
</span>
</div>
</div>
</div>
)}
</div>
{/* Footer - 移动端固定在底部 */}
<div className="sticky bottom-0 flex flex-col-reverse md:flex-row items-stretch md:items-center justify-end gap-2 md:gap-3 p-4 md:px-6 md:py-4 border-t border-gray-700 bg-gray-800 safe-area-pb">
<button
onClick={handleReset}
className="px-4 py-3 md:py-2 text-sm text-gray-300 hover:text-white active:bg-gray-700 rounded-lg transition-colors"
>
Reset
</button>
<button
onClick={onClose}
className="px-4 py-3 md:py-2 text-sm bg-gray-700 hover:bg-gray-600 active:bg-gray-500 rounded-lg transition-colors"
>
Cancel
</button>
<button
onClick={handleSave}
disabled={saving}
className="px-4 py-3 md:py-2 text-sm bg-blue-600 hover:bg-blue-500 active:bg-blue-700 rounded-lg transition-colors disabled:opacity-50 disabled:cursor-not-allowed font-medium"
>
{saving ? 'Saving...' : 'Save Changes'}
</button>
</div>
</div>
</div>
);
}
-236
View File
@@ -1,236 +0,0 @@
/**
* FileBrowser Component
*
* 文件浏览器组件
*/
import { useState, useEffect, useCallback } from 'react';
import { listFiles, readFile, type FileInfo } from '../api/client';
interface FileBrowserProps {
onFileSelect?: (path: string, content: string) => void;
className?: string;
}
// 文件图标
const FileIcon = ({ type, extension }: { type: 'file' | 'directory'; extension?: string }) => {
if (type === 'directory') {
return (
<svg className="w-4 h-4 text-yellow-400" fill="currentColor" viewBox="0 0 20 20">
<path d="M2 6a2 2 0 012-2h5l2 2h5a2 2 0 012 2v6a2 2 0 01-2 2H4a2 2 0 01-2-2V6z" />
</svg>
);
}
// 根据扩展名显示不同颜色
const colors: Record<string, string> = {
ts: 'text-blue-400',
tsx: 'text-blue-400',
js: 'text-yellow-300',
jsx: 'text-yellow-300',
json: 'text-yellow-500',
md: 'text-gray-400',
css: 'text-pink-400',
html: 'text-orange-400',
py: 'text-green-400',
go: 'text-cyan-400',
rs: 'text-orange-500',
};
const color = colors[extension || ''] || 'text-gray-400';
return (
<svg className={`w-4 h-4 ${color}`} fill="currentColor" viewBox="0 0 20 20">
<path
fillRule="evenodd"
d="M4 4a2 2 0 012-2h4.586A2 2 0 0112 2.586L15.414 6A2 2 0 0116 7.414V16a2 2 0 01-2 2H6a2 2 0 01-2-2V4z"
clipRule="evenodd"
/>
</svg>
);
};
// 格式化文件大小
const formatSize = (bytes: number): string => {
if (bytes === 0) return '0 B';
const k = 1024;
const sizes = ['B', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(1))} ${sizes[i]}`;
};
export function FileBrowser({ onFileSelect, className = '' }: FileBrowserProps) {
const [currentPath, setCurrentPath] = useState('.');
const [files, setFiles] = useState<FileInfo[]>([]);
const [parentPath, setParentPath] = useState<string | null>(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [selectedFile, setSelectedFile] = useState<string | null>(null);
const [fileContent, setFileContent] = useState<string | null>(null);
const [showHidden, setShowHidden] = useState(false);
// 加载目录内容
const loadDirectory = useCallback(async (path: string) => {
setLoading(true);
setError(null);
setSelectedFile(null);
setFileContent(null);
try {
const response = await listFiles(path, showHidden);
setFiles(response.data.files);
setCurrentPath(response.data.path);
setParentPath(response.data.parent);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to load directory');
} finally {
setLoading(false);
}
}, [showHidden]);
// 初始加载
useEffect(() => {
loadDirectory('.');
}, [loadDirectory]);
// 处理文件/目录点击
const handleItemClick = async (item: FileInfo) => {
if (item.type === 'directory') {
loadDirectory(item.path);
} else {
setSelectedFile(item.path);
try {
const response = await readFile(item.path);
if (response.data.encoding === 'utf-8') {
setFileContent(response.data.content);
onFileSelect?.(item.path, response.data.content);
} else {
setFileContent('[Binary file]');
}
} catch (err) {
setFileContent(`Error: ${err instanceof Error ? err.message : 'Failed to read file'}`);
}
}
};
// 返回上级目录
const handleGoUp = () => {
if (parentPath !== null) {
loadDirectory(parentPath);
}
};
// 刷新
const handleRefresh = () => {
loadDirectory(currentPath);
};
return (
<div className={`flex flex-col h-full bg-gray-900 ${className}`}>
{/* 工具栏 */}
<div className="flex items-center gap-2 p-2 border-b border-gray-700 bg-gray-800">
<button
onClick={handleGoUp}
disabled={parentPath === null}
className="p-1.5 rounded hover:bg-gray-700 disabled:opacity-50 disabled:cursor-not-allowed"
title="Go up"
>
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 15l7-7 7 7" />
</svg>
</button>
<button
onClick={handleRefresh}
className="p-1.5 rounded hover:bg-gray-700"
title="Refresh"
>
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"
/>
</svg>
</button>
<div className="flex-1 px-2 py-1 text-sm text-gray-400 bg-gray-900 rounded truncate">
{currentPath}
</div>
<label className="flex items-center gap-1 text-xs text-gray-400 cursor-pointer">
<input
type="checkbox"
checked={showHidden}
onChange={(e) => {
setShowHidden(e.target.checked);
loadDirectory(currentPath);
}}
className="w-3 h-3"
/>
Hidden
</label>
</div>
{/* 文件列表 */}
<div className="flex-1 overflow-auto">
{loading ? (
<div className="flex items-center justify-center h-32 text-gray-400">
Loading...
</div>
) : error ? (
<div className="flex items-center justify-center h-32 text-red-400">
{error}
</div>
) : files.length === 0 ? (
<div className="flex items-center justify-center h-32 text-gray-500">
Empty directory
</div>
) : (
<div className="divide-y divide-gray-800">
{files.map((file) => (
<div
key={file.path}
onClick={() => handleItemClick(file)}
className={`flex items-center gap-2 px-3 py-2 cursor-pointer hover:bg-gray-800 ${
selectedFile === file.path ? 'bg-gray-800 border-l-2 border-blue-500' : ''
}`}
>
<FileIcon type={file.type} extension={file.extension} />
<span className="flex-1 truncate text-sm">{file.name}</span>
{file.type === 'file' && (
<span className="text-xs text-gray-500">{formatSize(file.size)}</span>
)}
</div>
))}
</div>
)}
</div>
{/* 文件预览 */}
{selectedFile && fileContent && (
<div className="border-t border-gray-700 max-h-48 overflow-auto">
<div className="sticky top-0 flex items-center justify-between px-3 py-1 bg-gray-800 border-b border-gray-700">
<span className="text-xs text-gray-400 truncate">{selectedFile}</span>
<button
onClick={() => {
setSelectedFile(null);
setFileContent(null);
}}
className="p-1 hover:bg-gray-700 rounded"
>
<svg className="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
<pre className="p-3 text-xs text-gray-300 whitespace-pre-wrap font-mono">
{fileContent.slice(0, 5000)}
{fileContent.length > 5000 && '\n... (truncated)'}
</pre>
</div>
)}
</div>
);
}
-178
View File
@@ -1,178 +0,0 @@
/**
* Sidebar Component
*
* 响应式侧边栏:桌面端固定显示,移动端抽屉式菜单
*/
import { useState, useEffect } from 'react';
import { Plus, MessageSquare, Trash2, RefreshCw, Menu, X } from 'lucide-react';
import clsx from 'clsx';
import { listSessions, createSession, deleteSession, type Session } from '../api/client';
interface SidebarProps {
currentSessionId: string | null;
onSelectSession: (id: string) => void;
onCreateSession: (session: Session) => void;
}
export function Sidebar({ currentSessionId, onSelectSession, onCreateSession }: SidebarProps) {
const [sessions, setSessions] = useState<Session[]>([]);
const [isLoading, setIsLoading] = useState(false);
const [isOpen, setIsOpen] = useState(false);
const loadSessions = async () => {
setIsLoading(true);
try {
const { data } = await listSessions();
setSessions(data);
} catch (error) {
console.error('Failed to load sessions:', error);
} finally {
setIsLoading(false);
}
};
const handleCreate = async () => {
try {
const { data } = await createSession();
setSessions((prev) => [data, ...prev]);
onCreateSession(data);
setIsOpen(false); // 创建后关闭侧边栏
} catch (error) {
console.error('Failed to create session:', error);
}
};
const handleDelete = async (id: string, e: React.MouseEvent) => {
e.stopPropagation();
try {
await deleteSession(id);
setSessions((prev) => prev.filter((s) => s.id !== id));
if (currentSessionId === id) {
const remaining = sessions.filter((s) => s.id !== id);
if (remaining.length > 0) {
onSelectSession(remaining[0].id);
}
}
} catch (error) {
console.error('Failed to delete session:', error);
}
};
const handleSelectSession = (id: string) => {
onSelectSession(id);
setIsOpen(false); // 选择后关闭侧边栏
};
useEffect(() => {
loadSessions();
}, []);
// 点击遮罩层关闭
const handleOverlayClick = () => {
setIsOpen(false);
};
return (
<>
{/* 移动端菜单按钮 */}
<button
onClick={() => setIsOpen(true)}
className="fixed top-3 left-3 z-40 p-2 rounded-lg bg-gray-800 text-gray-300 hover:bg-gray-700 transition-colors md:hidden"
aria-label="Open menu"
>
<Menu size={20} />
</button>
{/* 遮罩层 - 仅移动端 */}
{isOpen && (
<div
className="fixed inset-0 bg-black/50 z-40 md:hidden"
onClick={handleOverlayClick}
/>
)}
{/* 侧边栏 */}
<div
className={clsx(
'fixed md:static inset-y-0 left-0 z-50',
'w-64 bg-gray-800 border-r border-gray-700 flex flex-col',
'transform transition-transform duration-300 ease-in-out',
'md:transform-none',
isOpen ? 'translate-x-0' : '-translate-x-full md:translate-x-0'
)}
>
{/* Header */}
<div className="p-4 border-b border-gray-700">
<div className="flex items-center justify-between mb-3 md:hidden">
<span className="text-lg font-semibold">Sessions</span>
<button
onClick={() => setIsOpen(false)}
className="p-1 hover:bg-gray-700 rounded transition-colors"
aria-label="Close menu"
>
<X size={20} />
</button>
</div>
<button
onClick={handleCreate}
className="w-full flex items-center justify-center gap-2 px-4 py-2 bg-primary-600 hover:bg-primary-700 rounded-lg transition-colors"
>
<Plus size={18} />
<span>New Chat</span>
</button>
</div>
{/* Session List */}
<div className="flex-1 overflow-y-auto">
{isLoading ? (
<div className="p-4 text-center text-gray-500">
<RefreshCw className="animate-spin inline-block" size={20} />
</div>
) : sessions.length === 0 ? (
<div className="p-4 text-center text-gray-500">
No conversations yet
</div>
) : (
<div className="p-2 space-y-1">
{sessions.map((session) => (
<div
key={session.id}
onClick={() => handleSelectSession(session.id)}
className={clsx(
'flex items-center gap-2 p-3 rounded-lg cursor-pointer group',
'hover:bg-gray-700 transition-colors',
'active:bg-gray-600', // 触摸反馈
currentSessionId === session.id && 'bg-gray-700'
)}
>
<MessageSquare size={18} className="text-gray-400 flex-shrink-0" />
<div className="flex-1 min-w-0">
<div className="text-sm truncate">
{session.name || `Chat ${session.id.slice(0, 8)}`}
</div>
<div className="text-xs text-gray-500">
{session.messageCount} messages
</div>
</div>
<button
onClick={(e) => handleDelete(session.id, e)}
className="opacity-0 group-hover:opacity-100 p-1 hover:bg-gray-600 rounded transition-all md:opacity-0"
aria-label="Delete session"
>
<Trash2 size={14} className="text-gray-400" />
</button>
</div>
))}
</div>
)}
</div>
{/* Footer */}
<div className="p-4 border-t border-gray-700 text-center text-xs text-gray-500">
AI Assistant v1.0
</div>
</div>
</>
);
}
-219
View File
@@ -1,219 +0,0 @@
/**
* Chat Hook
*
* 管理 WebSocket 连接和消息状态
*/
import { useState, useEffect, useCallback, useRef } from 'react';
import { createWebSocket, getMessages, type Message } from '../api/client';
interface UseChatOptions {
sessionId: string;
onError?: (error: Error) => void;
onSessionNotFound?: () => void;
}
interface ChatState {
messages: Message[];
isConnected: boolean;
isLoading: boolean;
streamingContent: string;
}
export function useChat({ sessionId, onError, onSessionNotFound }: UseChatOptions) {
const [state, setState] = useState<ChatState>({
messages: [],
isConnected: false,
isLoading: false,
streamingContent: '',
});
const wsRef = useRef<WebSocket | null>(null);
const reconnectTimeoutRef = useRef<NodeJS.Timeout>();
const reconnectAttemptsRef = useRef(0);
const maxReconnectAttempts = 5;
// 标记是否正在主动关闭连接(切换 session 时)
const isClosingRef = useRef(false);
// 用 ref 存储回调,避免依赖变化导致无限循环
const onErrorRef = useRef(onError);
const onSessionNotFoundRef = useRef(onSessionNotFound);
onErrorRef.current = onError;
onSessionNotFoundRef.current = onSessionNotFound;
// 加载历史消息
const loadMessages = useCallback(async () => {
try {
const { data } = await getMessages(sessionId);
setState((prev) => ({ ...prev, messages: data }));
} catch (error) {
// 会话不存在(404 或 "Session not found"),通知上层重新创建
const msg = error instanceof Error ? error.message : '';
if (msg.includes('404') || msg.toLowerCase().includes('not found')) {
onSessionNotFoundRef.current?.();
return;
}
onErrorRef.current?.(error instanceof Error ? error : new Error('Failed to load messages'));
}
}, [sessionId]);
// 连接 WebSocket
const connect = useCallback(() => {
// 如果正在关闭,不要连接
if (isClosingRef.current) return;
// 如果已经连接,不要重复连接
if (wsRef.current?.readyState === WebSocket.OPEN) return;
// 如果正在连接中,不要重复连接
if (wsRef.current?.readyState === WebSocket.CONNECTING) return;
const ws = createWebSocket(sessionId);
ws.onopen = () => {
reconnectAttemptsRef.current = 0; // 连接成功,重置重连次数
setState((prev) => ({ ...prev, isConnected: true }));
};
ws.onclose = () => {
setState((prev) => ({ ...prev, isConnected: false }));
// 主动关闭时不重连
if (isClosingRef.current) {
isClosingRef.current = false;
return;
}
// 限制重连次数
if (reconnectAttemptsRef.current < maxReconnectAttempts) {
reconnectAttemptsRef.current++;
reconnectTimeoutRef.current = setTimeout(connect, 3000);
}
};
ws.onerror = () => {
// 主动关闭时不报错
if (isClosingRef.current) return;
onErrorRef.current?.(new Error('WebSocket connection error'));
};
ws.onmessage = (event) => {
try {
const message = JSON.parse(event.data);
switch (message.type) {
case 'chunk':
setState((prev) => ({
...prev,
streamingContent: prev.streamingContent + (message.payload?.content || ''),
}));
break;
case 'done':
setState((prev) => {
const newMessage: Message = message.payload || {
id: Date.now().toString(),
role: 'assistant',
content: prev.streamingContent,
timestamp: new Date().toISOString(),
};
return {
...prev,
messages: [...prev.messages, newMessage],
streamingContent: '',
isLoading: false,
};
});
break;
case 'message_received':
// 用户消息已确认
setState((prev) => ({
...prev,
messages: [...prev.messages, message.payload],
}));
break;
case 'error':
onErrorRef.current?.(new Error(message.payload?.message || 'Unknown error'));
setState((prev) => ({ ...prev, isLoading: false, streamingContent: '' }));
break;
}
} catch {
// 忽略解析错误
}
};
wsRef.current = ws;
}, [sessionId]);
// 发送消息
const sendMessage = useCallback(
(content: string) => {
if (!wsRef.current || wsRef.current.readyState !== WebSocket.OPEN) {
onErrorRef.current?.(new Error('WebSocket not connected'));
return;
}
setState((prev) => ({ ...prev, isLoading: true }));
wsRef.current.send(
JSON.stringify({
type: 'message',
sessionId,
payload: { content },
})
);
},
[sessionId]
);
// 取消处理
const cancelProcessing = useCallback(() => {
if (!wsRef.current || wsRef.current.readyState !== WebSocket.OPEN) return;
wsRef.current.send(
JSON.stringify({
type: 'cancel',
sessionId,
})
);
setState((prev) => ({ ...prev, isLoading: false, streamingContent: '' }));
}, [sessionId]);
// 初始化
useEffect(() => {
// 重置状态
isClosingRef.current = false;
setState({
messages: [],
isConnected: false,
isLoading: false,
streamingContent: '',
});
reconnectAttemptsRef.current = 0;
loadMessages();
connect();
return () => {
clearTimeout(reconnectTimeoutRef.current);
// 标记为主动关闭,避免触发错误回调和重连
isClosingRef.current = true;
// 只关闭已建立的连接
if (wsRef.current) {
const ws = wsRef.current;
// 清除引用,防止后续操作
wsRef.current = null;
// 只有在 OPEN 或 CONNECTING 状态才关闭
if (ws.readyState === WebSocket.OPEN || ws.readyState === WebSocket.CONNECTING) {
ws.close();
}
}
};
}, [loadMessages, connect]);
return {
...state,
sendMessage,
cancelProcessing,
reload: loadMessages,
};
}
+1
View File
@@ -2,6 +2,7 @@ import React from 'react';
import ReactDOM from 'react-dom/client';
import { App } from './App';
import './styles/index.css';
import '@ai-assistant/ui/styles';
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
+10 -4
View File
@@ -4,16 +4,21 @@
import { useEffect, useRef } from 'react';
import { Wifi, WifiOff } from 'lucide-react';
import { useChat } from '../hooks/useChat';
import { ChatMessage, StreamingMessage, TypingIndicator } from '../components/ChatMessage';
import { ChatInput } from '../components/ChatInput';
import {
useChat,
ChatMessage,
StreamingMessage,
TypingIndicator,
ChatInput,
} from '@ai-assistant/ui';
interface ChatPageProps {
sessionId: string;
onSessionNotFound?: () => void;
responsive?: boolean;
}
export function ChatPage({ sessionId, onSessionNotFound }: ChatPageProps) {
export function ChatPage({ sessionId, onSessionNotFound, responsive = false }: ChatPageProps) {
const {
messages,
isConnected,
@@ -86,6 +91,7 @@ export function ChatPage({ sessionId, onSessionNotFound }: ChatPageProps) {
onCancel={cancelProcessing}
isLoading={isLoading}
disabled={!isConnected}
responsive={responsive}
/>
</div>
);
+5 -1
View File
@@ -1,6 +1,10 @@
/** @type {import('tailwindcss').Config} */
export default {
content: ['./index.html', './src/**/*.{js,ts,jsx,tsx}'],
content: [
'./index.html',
'./src/**/*.{js,ts,jsx,tsx}',
'../ui/src/**/*.{js,ts,jsx,tsx}',
],
darkMode: 'class',
theme: {
extend: {