Files
beaver_project/app-instance/frontend/components/AuthGuard.tsx
steven_li cdfc222c9f feat: 添加swarms团队编排功能并优化agent委派系统
- 引入AgentTeamOrchestrator支持多agent协同任务执行
- 增加第三方swarms库依赖并配置git协议替换以改善包管理
- 扩展DelegationManager支持团队任务调度和进度跟踪
- 实现中文bigram分词算法提升中文任务检索准确性
- 调整A2AClient和DelegationManager超时时间从30秒增至600秒
- 优化AgentRunResult状态判断逻辑增加有意义摘要检测
- 修改Dockerfile配置npm仓库镜像地址和git协议映射
- 更新CLI命令行接口支持网关端口配置传递
- 调整提供者超时配置机制增强请求稳定性
- 移除过时的support_group字段简化agent描述符结构
- 增强错误处理和进度事件报告机制改进用户体验
2026-04-14 14:34:23 +08:00

104 lines
2.6 KiB
TypeScript

'use client';
import { useEffect } from 'react';
import { usePathname, useRouter, useSearchParams } from 'next/navigation';
import { buildAuthPortalUrl } from '@/lib/auth-portal';
import { clearTokens, getMe, isLoggedIn } from '@/lib/api';
import { pickAppText } from '@/lib/i18n/core';
import { useAppI18n } from '@/lib/i18n/provider';
import { useChatStore } from '@/lib/store';
export default function AuthGuard({
children,
minHeightClassName = 'min-h-[calc(100vh-3.5rem)]',
}: {
children: React.ReactNode;
minHeightClassName?: string;
}) {
const { locale } = useAppI18n();
const router = useRouter();
const pathname = usePathname();
const searchParams = useSearchParams();
const user = useChatStore((s) => s.user);
const setUser = useChatStore((s) => s.setUser);
const setIsAuthLoading = useChatStore((s) => s.setIsAuthLoading);
const isAuthLoading = useChatStore((s) => s.isAuthLoading);
useEffect(() => {
let cancelled = false;
const init = async () => {
if (!isLoggedIn()) {
setUser(null);
if (!cancelled) {
setIsAuthLoading(false);
}
return;
}
if (useChatStore.getState().user) {
if (!cancelled) {
setIsAuthLoading(false);
}
return;
}
setIsAuthLoading(true);
try {
const me = await getMe();
if (cancelled) return;
setUser(me);
} catch {
clearTokens();
if (cancelled) return;
setUser(null);
} finally {
if (!cancelled) {
setIsAuthLoading(false);
}
}
};
init();
return () => {
cancelled = true;
};
}, [setIsAuthLoading, setUser]);
useEffect(() => {
if (isAuthLoading) {
return;
}
const isPublicRoute = pathname === '/login' || pathname === '/register';
const loggedIn = isLoggedIn();
if (!loggedIn && !isPublicRoute) {
const search = searchParams?.toString();
const nextPath = search ? `${pathname}?${search}` : pathname;
window.location.replace(buildAuthPortalUrl('/login', nextPath));
return;
}
if (loggedIn && user && isPublicRoute) {
router.replace('/');
}
}, [isAuthLoading, pathname, router, searchParams, user]);
if (isAuthLoading) {
return (
<div className={`flex ${minHeightClassName} items-center justify-center`}>
<div className="text-muted-foreground">{pickAppText(locale, '加载中...', 'Loading...')}</div>
</div>
);
}
const isPublicRoute = pathname === '/login' || pathname === '/register';
if (!isPublicRoute && (!isLoggedIn() || !user)) {
return null;
}
return <>{children}</>;
}