Add pipe-watcher and manual refresh for hooks
Introduce a background pipe-watcher and per-PID operation serialization in HookManager to reliably detect and reconnect to SnowLuma pipes, reduce races, and remove the previous connect retry loop. Add new status 'connecting' and a refreshProcess API endpoint to force a probe/reconcile for a PID. Implement QqHookClient.listLivePipes() (and isClosed/probePipe updates) to enumerate live mojo pipes efficiently. Simplify client teardown logic and improve state transitions/logging. Update web UI: add a Refresh button, track inflight process ops, show connecting status and updated action messages. Update shared types to include the new 'connecting' status.
This commit is contained in:
1 parent
2e340f58a8
commit
4b02367a5f
6 files changed
+435
-138
No files matched your search
@@ -39,7 +39,11 @@ function AppInner() {
|
||||
|
||||
const [processLoadingPid, setProcessLoadingPid] = useState<number | null>(null);
|
||||
const [processUnloadingPid, setProcessUnloadingPid] = useState<number | null>(null);
|
||||
const [processRefreshingPid, setProcessRefreshingPid] = useState<number | null>(null);
|
||||
const [processActionStatus, setProcessActionStatus] = useState('');
|
||||
// Tracks PIDs with an in-flight load/unload/refresh request so the UI can
|
||||
// collapse spam-clicks instead of firing a second concurrent request.
|
||||
const inflightProcessOps = useRef(new Set<number>());
|
||||
|
||||
const [selectedUin, setSelectedUin] = useState<string | null>(null);
|
||||
const [config, setConfig] = useState<OneBotConfig | null>(null);
|
||||
@@ -224,39 +228,65 @@ function AppInner() {
|
||||
}, [fetchApi]);
|
||||
|
||||
const handleLoadProcess = useCallback(async (pid: number) => {
|
||||
if (inflightProcessOps.current.has(pid)) return;
|
||||
inflightProcessOps.current.add(pid);
|
||||
setProcessLoadingPid(pid);
|
||||
setProcessActionStatus(`正在向进程 ${pid} 加载 SnowLuma…`);
|
||||
try {
|
||||
const res = await fetchApi(`/api/processes/${pid}/load`, { method: 'POST' });
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok) throw new Error(data.error || '加载失败');
|
||||
setProcessActionStatus(`已向进程 ${pid} 注入 SnowLuma`);
|
||||
if (!res.ok) throw new Error(data.message || data.error || '加载失败');
|
||||
setProcessActionStatus(`已向进程 ${pid} 注入 SnowLuma,等待管道连接…`);
|
||||
await refreshProcesses();
|
||||
} catch (e) {
|
||||
setProcessActionStatus(`加载失败:${(e as Error).message}`);
|
||||
} finally {
|
||||
inflightProcessOps.current.delete(pid);
|
||||
setProcessLoadingPid(null);
|
||||
setTimeout(() => setProcessActionStatus(''), 4000);
|
||||
}
|
||||
}, [fetchApi, refreshProcesses]);
|
||||
|
||||
const handleUnloadProcess = useCallback(async (pid: number) => {
|
||||
if (inflightProcessOps.current.has(pid)) return;
|
||||
inflightProcessOps.current.add(pid);
|
||||
setProcessUnloadingPid(pid);
|
||||
setProcessActionStatus(`正在从进程 ${pid} 卸载…`);
|
||||
try {
|
||||
const res = await fetchApi(`/api/processes/${pid}/unload`, { method: 'POST' });
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok) throw new Error(data.error || '卸载失败');
|
||||
if (!res.ok) throw new Error(data.message || data.error || '卸载失败');
|
||||
setProcessActionStatus(`已从进程 ${pid} 卸载`);
|
||||
await refreshProcesses();
|
||||
} catch (e) {
|
||||
setProcessActionStatus(`卸载失败:${(e as Error).message}`);
|
||||
} finally {
|
||||
inflightProcessOps.current.delete(pid);
|
||||
setProcessUnloadingPid(null);
|
||||
setTimeout(() => setProcessActionStatus(''), 4000);
|
||||
}
|
||||
}, [fetchApi, refreshProcesses]);
|
||||
|
||||
const handleRefreshProcess = useCallback(async (pid: number) => {
|
||||
if (inflightProcessOps.current.has(pid)) return;
|
||||
inflightProcessOps.current.add(pid);
|
||||
setProcessRefreshingPid(pid);
|
||||
setProcessActionStatus(`正在刷新进程 ${pid} 的管道状态…`);
|
||||
try {
|
||||
const res = await fetchApi(`/api/processes/${pid}/refresh`, { method: 'POST' });
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok) throw new Error(data.message || data.error || '刷新失败');
|
||||
setProcessActionStatus(`已刷新进程 ${pid} 的管道状态`);
|
||||
await refreshProcesses();
|
||||
} catch (e) {
|
||||
setProcessActionStatus(`刷新失败:${(e as Error).message}`);
|
||||
} finally {
|
||||
inflightProcessOps.current.delete(pid);
|
||||
setProcessRefreshingPid(null);
|
||||
setTimeout(() => setProcessActionStatus(''), 4000);
|
||||
}
|
||||
}, [fetchApi, refreshProcesses]);
|
||||
|
||||
const handleSaveConfig = useCallback(async () => {
|
||||
if (!selectedUin || !config) return;
|
||||
setSaveStatus('保存中...');
|
||||
@@ -321,12 +351,14 @@ function AppInner() {
|
||||
processList={processList}
|
||||
processLoadingPid={processLoadingPid}
|
||||
processUnloadingPid={processUnloadingPid}
|
||||
processRefreshingPid={processRefreshingPid}
|
||||
processActionStatus={processActionStatus}
|
||||
systemInfo={systemInfo}
|
||||
onRefreshProcesses={refreshProcesses}
|
||||
onRefreshSystem={refreshSystem}
|
||||
onLoadProcess={handleLoadProcess}
|
||||
onUnloadProcess={handleUnloadProcess}
|
||||
onRefreshProcess={handleRefreshProcess}
|
||||
/>
|
||||
)}
|
||||
{active === 'config' && (
|
||||
|
||||
@@ -31,17 +31,20 @@ interface OverviewPageProps {
|
||||
processList: HookProcessInfo[];
|
||||
processLoadingPid: number | null;
|
||||
processUnloadingPid: number | null;
|
||||
processRefreshingPid: number | null;
|
||||
processActionStatus: string;
|
||||
systemInfo: SystemInfo | null;
|
||||
onRefreshProcesses: () => void;
|
||||
onRefreshSystem: () => void;
|
||||
onLoadProcess: (pid: number) => Promise<void> | void;
|
||||
onUnloadProcess: (pid: number) => Promise<void> | void;
|
||||
onRefreshProcess: (pid: number) => Promise<void> | void;
|
||||
}
|
||||
|
||||
const processStatusLabel: Record<HookProcessInfo['status'], string> = {
|
||||
available: '可加载',
|
||||
loading: '加载中',
|
||||
connecting: '等待连接',
|
||||
loaded: '等待登录',
|
||||
online: '已在线',
|
||||
error: '错误',
|
||||
@@ -50,8 +53,9 @@ const processStatusLabel: Record<HookProcessInfo['status'], string> = {
|
||||
|
||||
function processBadgeVariant(status: HookProcessInfo['status']) {
|
||||
if (status === 'online') return 'success' as const;
|
||||
if (status === 'error' || status === 'disconnected') return 'destructive' as const;
|
||||
if (status === 'loading' || status === 'loaded') return 'default' as const;
|
||||
if (status === 'error') return 'destructive' as const;
|
||||
if (status === 'disconnected') return 'destructive' as const;
|
||||
if (status === 'loading' || status === 'connecting' || status === 'loaded') return 'default' as const;
|
||||
return 'secondary' as const;
|
||||
}
|
||||
|
||||
@@ -99,12 +103,14 @@ export function OverviewPage({
|
||||
processList,
|
||||
processLoadingPid,
|
||||
processUnloadingPid,
|
||||
processRefreshingPid,
|
||||
processActionStatus,
|
||||
systemInfo,
|
||||
onRefreshProcesses,
|
||||
onRefreshSystem,
|
||||
onLoadProcess,
|
||||
onUnloadProcess,
|
||||
onRefreshProcess,
|
||||
}: OverviewPageProps) {
|
||||
const [confirm, setConfirm] = useState<
|
||||
| { kind: 'load' | 'unload'; pid: number; name: string }
|
||||
@@ -278,10 +284,13 @@ export function OverviewPage({
|
||||
{processList.map((proc, idx) => {
|
||||
const loading = processLoadingPid === proc.pid || proc.status === 'loading';
|
||||
const unloading = processUnloadingPid === proc.pid;
|
||||
const busy = loading || unloading;
|
||||
const refreshing = processRefreshingPid === proc.pid;
|
||||
const busy = loading || unloading || refreshing;
|
||||
const isOnline = proc.status === 'online';
|
||||
const canUnload =
|
||||
proc.injected || proc.status === 'loaded' || isOnline || proc.status === 'disconnected';
|
||||
const canUnload = proc.injected;
|
||||
// Refresh is meaningful whenever a hook may exist (so the user
|
||||
// can re-check the pipe and trigger a reconnect on demand).
|
||||
const showRefresh = proc.injected || proc.status === 'connecting' || proc.status === 'disconnected';
|
||||
return (
|
||||
<motion.div
|
||||
key={proc.pid}
|
||||
@@ -328,26 +337,44 @@ export function OverviewPage({
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<Button
|
||||
size="sm"
|
||||
variant={canUnload ? 'outline' : 'default'}
|
||||
disabled={busy}
|
||||
onClick={() =>
|
||||
setConfirm({
|
||||
kind: canUnload ? 'unload' : 'load',
|
||||
pid: proc.pid,
|
||||
name: proc.name || `PID ${proc.pid}`,
|
||||
})
|
||||
}
|
||||
className={cn(
|
||||
'shrink-0',
|
||||
canUnload && 'text-destructive hover:bg-destructive/10 hover:text-destructive'
|
||||
<div className="flex shrink-0 items-center gap-1.5">
|
||||
{showRefresh && (
|
||||
<Button
|
||||
size="icon"
|
||||
variant="ghost"
|
||||
disabled={busy}
|
||||
aria-label={`刷新进程 ${proc.pid} 管道状态`}
|
||||
title="刷新管道状态 / 重连"
|
||||
onClick={() => onRefreshProcess(proc.pid)}
|
||||
className="size-8 text-muted-foreground hover:text-foreground"
|
||||
>
|
||||
{refreshing ? (
|
||||
<Loader2 className="size-3.5 animate-spin" />
|
||||
) : (
|
||||
<RefreshCw className="size-3.5" />
|
||||
)}
|
||||
</Button>
|
||||
)}
|
||||
>
|
||||
{busy && <Loader2 className="size-3.5 animate-spin" />}
|
||||
{!busy && canUnload && <Unplug className="size-3.5" />}
|
||||
{canUnload ? (unloading ? '卸载中' : '卸载') : loading ? '加载中' : '加载'}
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
variant={canUnload ? 'outline' : 'default'}
|
||||
disabled={busy}
|
||||
onClick={() =>
|
||||
setConfirm({
|
||||
kind: canUnload ? 'unload' : 'load',
|
||||
pid: proc.pid,
|
||||
name: proc.name || `PID ${proc.pid}`,
|
||||
})
|
||||
}
|
||||
className={cn(
|
||||
canUnload && 'text-destructive hover:bg-destructive/10 hover:text-destructive'
|
||||
)}
|
||||
>
|
||||
{(loading || unloading) && <Loader2 className="size-3.5 animate-spin" />}
|
||||
{!loading && !unloading && canUnload && <Unplug className="size-3.5" />}
|
||||
{canUnload ? (unloading ? '卸载中' : '卸载') : loading ? '加载中' : '加载'}
|
||||
</Button>
|
||||
</div>
|
||||
</motion.div>
|
||||
);
|
||||
})}
|
||||
|
||||
@@ -14,7 +14,7 @@ export interface HookProcessInfo {
|
||||
connected: boolean;
|
||||
loggedIn: boolean;
|
||||
uin: string;
|
||||
status: 'available' | 'loading' | 'loaded' | 'online' | 'error' | 'disconnected';
|
||||
status: 'available' | 'loading' | 'connecting' | 'loaded' | 'online' | 'error' | 'disconnected';
|
||||
error: string;
|
||||
method: string;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user