"use client"; /** * useTestConnection — 封裝 Wails `TestConnection(url)` binding * * spec §6.2.1「測試連線」按鈕:發 WS handshake(不帶 token,只測 reachability)。 * * AF6 更新: * - 從 mock 改為呼叫 `agentAPI.testConnection()` * - Go 端會實際跑 WebSocket dial(5 秒 timeout) * - `test()` 永遠 resolve(不 reject)— 失敗資訊在 result.ok=false + reason * - 瀏覽器 dev 模式下走 mock */ import { useCallback, useState } from "react"; import { agentAPI } from "@/lib/agent-api"; import type { TestRelayResult } from "@/types/agent"; export interface UseTestConnectionResult { /** 發起測試;回傳結果(永遠不 reject — 失敗資訊在 result.ok=false + reason)。 */ test: (url: string) => Promise; testing: boolean; } export function useTestConnection(): UseTestConnectionResult { const [testing, setTesting] = useState(false); const test = useCallback(async (url: string): Promise => { setTesting(true); try { return await agentAPI.testConnection(url); } catch (err) { // 理論上 agentAPI.testConnection 不會 throw(Go 端回 TestResult 帶 reason); // 但萬一底層出事(例:Wails binding 不存在),包成 ok=false 讓 UI 能顯示 return { ok: false, reason: err instanceof Error ? err.message : String(err), }; } finally { setTesting(false); } }, []); return { test, testing }; }