feat: 完善 Server 层并添加 CLI 和 Web 前端
Server 层增强: - 添加 Agent 适配层,支持动态加载 core 模块 - 实现 Token 认证机制,支持本地/远程模式 - WebSocket 集成 Agent 实时对话 CLI 模块 (packages/cli): - serve 命令启动 HTTP Server - attach 命令连接远程 Server - API Client 封装 Web 前端 (packages/web): - React 18 + Vite + Tailwind CSS - 会话管理侧边栏 - WebSocket 实时聊天界面 - 流式消息显示
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
/**
|
||||
* 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}`);
|
||||
}
|
||||
Reference in New Issue
Block a user