feat: 添加 Tauri 桌面应用
- 创建 packages/desktop 模块 - 实现 Tauri 2.0 + React 桌面应用 - 复用 Web 前端代码 - 添加系统托盘功能 - 实现本地文件访问命令 - 配置 Vite + Tauri 集成 - 更新 .gitignore 添加 Rust/Tauri 相关规则
This commit is contained in:
@@ -0,0 +1,181 @@
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
Reference in New Issue
Block a user