chore: initial commit for Pretor v0.1.0-alpha
正式发布 Pretor 平台的首个 alpha 版本。本项目旨在构建一个基于分布式架构的多智能体协同工作流水线。 核心功能实现: 1. 建立基于 BaseIndividual 的动态插件加载机制。 2. 实现三类核心 worker_individual 子个体。 3. 集成 Ray 框架支持分布式集群调度。 4. 基于 PostgreSQL 的全量持久化存储方案。 5. 提供完整的 FastAPI 后端与 React 前端交互界面。
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import type { ClusterNode } from '../types';
|
||||
|
||||
export function useClusterState() {
|
||||
const [nodes, setNodes] = useState<ClusterNode[]>([]);
|
||||
const [isConnected, setIsConnected] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
let ws: WebSocket | null = null;
|
||||
let reconnectTimeout: ReturnType<typeof setTimeout>;
|
||||
let retryCount = 0;
|
||||
const maxRetryCount = 10;
|
||||
const baseDelay = 1000;
|
||||
|
||||
const connect = () => {
|
||||
// Determine WS URL based on API base URL or window location
|
||||
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
||||
const host = window.location.host;
|
||||
|
||||
const wsBase = import.meta.env.VITE_API_BASE_URL
|
||||
? import.meta.env.VITE_API_BASE_URL.replace(/^http/, 'ws')
|
||||
: `${protocol}//${host}`;
|
||||
|
||||
ws = new WebSocket(`${wsBase}/api/v1/cluster/ws/state`);
|
||||
|
||||
ws.onopen = () => {
|
||||
setIsConnected(true);
|
||||
retryCount = 0; // Reset retry count on successful connection
|
||||
};
|
||||
|
||||
ws.onmessage = (event) => {
|
||||
try {
|
||||
const data = JSON.parse(event.data);
|
||||
if (Array.isArray(data)) {
|
||||
setNodes(data);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Error parsing cluster state websocket message", e);
|
||||
}
|
||||
};
|
||||
|
||||
ws.onclose = () => {
|
||||
setIsConnected(false);
|
||||
if (retryCount < maxRetryCount) {
|
||||
const delay = baseDelay * Math.pow(2, retryCount);
|
||||
retryCount++;
|
||||
console.log(`WebSocket closed. Reconnecting in ${delay}ms... (Attempt ${retryCount})`);
|
||||
reconnectTimeout = setTimeout(connect, delay);
|
||||
} else {
|
||||
console.error("Max WebSocket reconnect attempts reached.");
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
connect();
|
||||
|
||||
return () => {
|
||||
clearTimeout(reconnectTimeout);
|
||||
if (ws) {
|
||||
ws.close();
|
||||
}
|
||||
};
|
||||
}, []);
|
||||
|
||||
return { nodes, isConnected };
|
||||
}
|
||||
Reference in New Issue
Block a user