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
|
||||
|
||||
@@ -10,6 +10,7 @@ import { createWebSocket, getMessages, type Message } from '../api/client';
|
||||
interface UseChatOptions {
|
||||
sessionId: string;
|
||||
onError?: (error: Error) => void;
|
||||
onSessionNotFound?: () => void;
|
||||
}
|
||||
|
||||
interface ChatState {
|
||||
@@ -19,7 +20,7 @@ interface ChatState {
|
||||
streamingContent: string;
|
||||
}
|
||||
|
||||
export function useChat({ sessionId, onError }: UseChatOptions) {
|
||||
export function useChat({ sessionId, onError, onSessionNotFound }: UseChatOptions) {
|
||||
const [state, setState] = useState<ChatState>({
|
||||
messages: [],
|
||||
isConnected: false,
|
||||
@@ -29,6 +30,16 @@ export function useChat({ sessionId, onError }: UseChatOptions) {
|
||||
|
||||
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 () => {
|
||||
@@ -36,28 +47,50 @@ export function useChat({ sessionId, onError }: UseChatOptions) {
|
||||
const { data } = await getMessages(sessionId);
|
||||
setState((prev) => ({ ...prev, messages: data }));
|
||||
} catch (error) {
|
||||
onError?.(error instanceof Error ? error : new Error('Failed to load messages'));
|
||||
// 会话不存在(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, onError]);
|
||||
}, [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 }));
|
||||
// 自动重连
|
||||
reconnectTimeoutRef.current = setTimeout(connect, 3000);
|
||||
// 主动关闭时不重连
|
||||
if (isClosingRef.current) {
|
||||
isClosingRef.current = false;
|
||||
return;
|
||||
}
|
||||
// 限制重连次数
|
||||
if (reconnectAttemptsRef.current < maxReconnectAttempts) {
|
||||
reconnectAttemptsRef.current++;
|
||||
reconnectTimeoutRef.current = setTimeout(connect, 3000);
|
||||
}
|
||||
};
|
||||
|
||||
ws.onerror = () => {
|
||||
onError?.(new Error('WebSocket connection error'));
|
||||
// 主动关闭时不报错
|
||||
if (isClosingRef.current) return;
|
||||
onErrorRef.current?.(new Error('WebSocket connection error'));
|
||||
};
|
||||
|
||||
ws.onmessage = (event) => {
|
||||
@@ -98,7 +131,7 @@ export function useChat({ sessionId, onError }: UseChatOptions) {
|
||||
break;
|
||||
|
||||
case 'error':
|
||||
onError?.(new Error(message.payload?.message || 'Unknown error'));
|
||||
onErrorRef.current?.(new Error(message.payload?.message || 'Unknown error'));
|
||||
setState((prev) => ({ ...prev, isLoading: false, streamingContent: '' }));
|
||||
break;
|
||||
}
|
||||
@@ -108,13 +141,13 @@ export function useChat({ sessionId, onError }: UseChatOptions) {
|
||||
};
|
||||
|
||||
wsRef.current = ws;
|
||||
}, [sessionId, onError]);
|
||||
}, [sessionId]);
|
||||
|
||||
// 发送消息
|
||||
const sendMessage = useCallback(
|
||||
(content: string) => {
|
||||
if (!wsRef.current || wsRef.current.readyState !== WebSocket.OPEN) {
|
||||
onError?.(new Error('WebSocket not connected'));
|
||||
onErrorRef.current?.(new Error('WebSocket not connected'));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -128,7 +161,7 @@ export function useChat({ sessionId, onError }: UseChatOptions) {
|
||||
})
|
||||
);
|
||||
},
|
||||
[sessionId, onError]
|
||||
[sessionId]
|
||||
);
|
||||
|
||||
// 取消处理
|
||||
@@ -147,12 +180,33 @@ export function useChat({ sessionId, onError }: UseChatOptions) {
|
||||
|
||||
// 初始化
|
||||
useEffect(() => {
|
||||
// 重置状态
|
||||
isClosingRef.current = false;
|
||||
setState({
|
||||
messages: [],
|
||||
isConnected: false,
|
||||
isLoading: false,
|
||||
streamingContent: '',
|
||||
});
|
||||
reconnectAttemptsRef.current = 0;
|
||||
|
||||
loadMessages();
|
||||
connect();
|
||||
|
||||
return () => {
|
||||
clearTimeout(reconnectTimeoutRef.current);
|
||||
wsRef.current?.close();
|
||||
// 标记为主动关闭,避免触发错误回调和重连
|
||||
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]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user