fix(desktop): 修复桌面端 API 连接问题
- API 客户端使用完整后端 URL (localhost:3000) - 添加 tauri-plugin-http 支持外部 HTTP 请求 - 配置 CSP 允许连接 localhost - 同步 useChat hook 修复 WebSocket 错误处理
This commit is contained in:
@@ -35,7 +35,8 @@ export interface HealthStatus {
|
||||
};
|
||||
}
|
||||
|
||||
const API_BASE = '/api';
|
||||
// Tauri 应用需要完整的后端 URL
|
||||
const API_BASE = 'http://localhost:3000/api';
|
||||
|
||||
async function request<T>(method: string, path: string, body?: unknown): Promise<T> {
|
||||
const response = await fetch(`${API_BASE}${path}`, {
|
||||
@@ -56,7 +57,11 @@ async function request<T>(method: string, path: string, body?: unknown): Promise
|
||||
|
||||
// Health
|
||||
export async function getHealth(): Promise<HealthStatus> {
|
||||
return request('GET', '/../health');
|
||||
const response = await fetch('http://localhost:3000/health');
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}`);
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// Sessions
|
||||
@@ -90,9 +95,8 @@ export async function sendMessage(
|
||||
|
||||
// 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}`);
|
||||
// Tauri 应用直接连接后端
|
||||
return new WebSocket(`ws://localhost:3000/api/ws/${sessionId}`);
|
||||
}
|
||||
|
||||
// Files
|
||||
|
||||
Reference in New Issue
Block a user