Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🔥 chore: drop useSkillConnection hook (moved into cloud feature) (#14308)

YuTengjing28c2e9002a36
diff --git a/locales/zh-CN/taskTemplate.json b/locales/zh-CN/taskTemplate.json
index 3bcee1962b4..b66b1c6d210 100644
--- a/locales/zh-CN/taskTemplate.json
+++ b/locales/zh-CN/taskTemplate.json
@@ -1,5 +1,7 @@
{
"action.connect.button": "连接 {{provider}}",
+ "action.connect.error": "连接失败,请稍后再试",
+ "action.connect.popupBlocked": "连接弹窗被浏览器拦截,请允许弹窗后重试",
"action.create.error": "创建任务失败,请稍后再试",
"action.create.success": "定时任务已创建,可在 Lobe AI 中查看",
"action.createButton": "添加为定时任务",
diff --git a/src/hooks/useSkillConnection.test.ts b/src/hooks/useSkillConnection.test.ts
deleted file mode 100644
index a2ea31477ee..00000000000
--- a/src/hooks/useSkillConnection.test.ts
+++ /dev/null
@@ -1,79 +0,0 @@
-// @vitest-environment happy-dom
-import type { TaskTemplateSkillRequirement } from '@lobechat/const';
-import { describe, expect, it } from 'vitest';
-
-import { findNextUnconnectedSpec, getProviderMeta } from './useSkillConnection';
-
-describe('getProviderMeta', () => {
- it('resolves lobehub source via LOBEHUB_SKILL_PROVIDERS', () => {
- const meta = getProviderMeta({ provider: 'github', source: 'lobehub' });
- expect(meta).toMatchObject({ label: 'GitHub', provider: 'github', source: 'lobehub' });
- expect(meta?.icon).toBeDefined();
- });
-
- it('resolves klavis source via KLAVIS_SERVER_TYPES', () => {
- const meta = getProviderMeta({ provider: 'notion', source: 'klavis' });
- expect(meta).toMatchObject({ label: 'Notion', provider: 'notion', source: 'klavis' });
- expect(meta?.icon).toBeDefined();
- });
-
- it('returns undefined for unknown provider', () => {
- expect(getProviderMeta({ provider: 'nonexistent-x', source: 'lobehub' })).toBeUndefined();
- expect(getProviderMeta({ provider: 'nonexistent-x', source: 'klavis' })).toBeUndefined();
- });
-
- it('does not cross namespaces (lobehub id under klavis source returns undefined)', () => {
- // 'github' is a lobehub provider id, not a klavis identifier.
- expect(getProviderMeta({ provider: 'github', source: 'klavis' })).toBeUndefined();
- });
-});
-
-describe('findNextUnconnectedSpec', () => {
- const allConnected = () => true;
- const noneConnected = () => false;
-
- it('returns undefined when specs is undefined or empty', () => {
- expect(findNextUnconnectedSpec(undefined, noneConnected)).toBeUndefined();
- expect(findNextUnconnectedSpec([], noneConnected)).toBeUndefined();
- });
-
- it('returns undefined when all specs are connected', () => {
- const specs: TaskTemplateSkillRequirement[] = [
- { provider: 'github', source: 'lobehub' },
- { provider: 'notion', source: 'klavis' },
- ];
- expect(findNextUnconnectedSpec(specs, allConnected)).toBeUndefined();
- });
-
- it('returns the first spec when none are connected', () => {
- const specs: TaskTemplateSkillRequirement[] = [
- { provider: 'github', source: 'lobehub' },
- { provider: 'notion', source: 'klavis' },
- ];
- const result = findNextUnconnectedSpec(specs, noneConnected);
- expect(result?.provider).toBe('github');
- expect(result?.label).toBe('GitHub');
- });
-
- it('skips already-connected specs and returns the next missing one in order', () => {
- const specs: TaskTemplateSkillRequirement[] = [
- { provider: 'github', source: 'lobehub' },
- { provider: 'linear', source: 'lobehub' },
- { provider: 'notion', source: 'klavis' },
- ];
- const isConnected = (s: TaskTemplateSkillRequirement) =>
- s.provider === 'github' || s.provider === 'linear';
- const result = findNextUnconnectedSpec(specs, isConnected);
- expect(result?.provider).toBe('notion');
- expect(result?.source).toBe('klavis');
- });
-
- it('skips specs with unknown providers (no meta) and continues searching', () => {
- const specs: TaskTemplateSkillRequirement[] = [
- { provider: 'nonexistent-x', source: 'lobehub' },
- { provider: 'notion', source: 'klavis' },
- ];
- const result = findNextUnconnectedSpec(specs, noneConnected);
- expect(result?.provider).toBe('notion');
- });
-});
diff --git a/src/hooks/useSkillConnection.ts b/src/hooks/useSkillConnection.ts
deleted file mode 100644
index 6399be68fc4..00000000000
--- a/src/hooks/useSkillConnection.ts
+++ /dev/null
@@ -1,296 +0,0 @@
-'use client';
-
-import type {
- LobehubSkillProviderType,
- TaskTemplateSkillRequirement,
- TaskTemplateSkillSource,
-} from '@lobechat/const';
-import {
- getKlavisServerByServerIdentifier,
- getLobehubSkillProviderById,
- KLAVIS_SERVER_TYPES,
-} from '@lobechat/const';
-import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
-
-import { LOBEHUB_SKILL_AUTH_SUCCESS_MESSAGE } from '@/const/skillConnection';
-import { useToolStore } from '@/store/tool';
-import { klavisStoreSelectors } from '@/store/tool/slices/klavisStore/selectors';
-import { KlavisServerStatus } from '@/store/tool/slices/klavisStore/types';
-import { lobehubSkillStoreSelectors } from '@/store/tool/slices/lobehubSkillStore/selectors';
-import { LobehubSkillStatus } from '@/store/tool/slices/lobehubSkillStore/types';
-import { useUserStore } from '@/store/user';
-
-const POLL_INTERVAL_MS = 1000;
-const POLL_TIMEOUT_MS = 15_000;
-/** Hard cap on how long the OAuth popup-monitor keeps polling — protects against
- * users opening the popup, switching away, and never closing it. */
-const OAUTH_OVERALL_TIMEOUT_MS = 5 * 60 * 1000;
-
-export interface SkillProviderMeta {
- icon: LobehubSkillProviderType['icon'];
- label: string;
- provider: string;
- source: TaskTemplateSkillSource;
-}
-
-type ConnectTarget = Pick<SkillProviderMeta, 'provider' | 'source'>;
-
-export interface UseSkillConnectionResult {
- connect: () => Promise<void>;
- isAllConnected: boolean;
- isConnecting: boolean;
- /** True when there is at least one spec and at least one of them is not yet connected. */
- needsConnect: boolean;
- /** First spec in input order whose connection is missing. undefined when all connected or specs is empty. */
- nextUnconnected: SkillProviderMeta | undefined;
-}
-
-export const getProviderMeta = (
- spec: TaskTemplateSkillRequirement,
-): SkillProviderMeta | undefined => {
- if (spec.source === 'lobehub') {
- const p = getLobehubSkillProviderById(spec.provider);
- if (!p) return undefined;
- return { icon: p.icon, label: p.label, provider: spec.provider, source: 'lobehub' };
- }
- const p = getKlavisServerByServerIdentifier(spec.provider);
- if (!p) return undefined;
- return { icon: p.icon, label: p.label, provider: spec.provider, source: 'klavis' };
-};
-
-export const findNextUnconnectedSpec = (
- specs: TaskTemplateSkillRequirement[] | undefined,
- isConnected: (spec: TaskTemplateSkillRequirement) => boolean,
-): SkillProviderMeta | undefined => {
- if (!specs || specs.length === 0) return undefined;
- for (const spec of specs) {
- if (isConnected(spec)) continue;
- const meta = getProviderMeta(spec);
- if (!meta) continue;
- return meta;
- }
- return undefined;
-};
-
-export const useSkillConnection = (
- specs: TaskTemplateSkillRequirement[] | undefined,
-): UseSkillConnectionResult => {
- const getLobehubAuth = useToolStore((s) => s.getLobehubSkillAuthorizeUrl);
- const checkLobehubStatus = useToolStore((s) => s.checkLobehubSkillStatus);
- const createKlavisServer = useToolStore((s) => s.createKlavisServer);
- const refreshKlavisServerTools = useToolStore((s) => s.refreshKlavisServerTools);
-
- const lobehubServers = useToolStore(lobehubSkillStoreSelectors.getServers);
- const klavisServers = useToolStore(klavisStoreSelectors.getServers);
-
- const isConnectedFor = useCallback(
- (spec: TaskTemplateSkillRequirement): boolean => {
- if (spec.source === 'lobehub') {
- return lobehubServers.some(
- (s) => s.identifier === spec.provider && s.status === LobehubSkillStatus.CONNECTED,
- );
- }
- return klavisServers.some(
- (s) => s.identifier === spec.provider && s.status === KlavisServerStatus.CONNECTED,
- );
- },
- [lobehubServers, klavisServers],
- );
-
- const nextUnconnected = useMemo(
- () => findNextUnconnectedSpec(specs, isConnectedFor),
- [specs, isConnectedFor],
- );
-
- const hasSpecs = (specs?.length ?? 0) > 0;
- const isAllConnected = hasSpecs && !nextUnconnected;
- const needsConnect = hasSpecs && !!nextUnconnected;
-
- const [isConnecting, setIsConnecting] = useState(false);
- const [isWaitingAuth, setIsWaitingAuth] = useState(false);
-
- const oauthWindowRef = useRef<Window | null>(null);
- const windowCheckIntervalRef = useRef<ReturnType<typeof setInterval> | null>(null);
- const windowCheckTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
- const pollIntervalRef = useRef<ReturnType<typeof setInterval> | null>(null);
- const pollTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
- // Sync lock against double-click — useState guard would only flip after re-render.
- const isConnectingRef = useRef(false);
-
- const cleanup = useCallback(() => {
- if (windowCheckIntervalRef.current) {
- clearInterval(windowCheckIntervalRef.current);
- windowCheckIntervalRef.current = null;
- }
- if (windowCheckTimeoutRef.current) {
- clearTimeout(windowCheckTimeoutRef.current);
- windowCheckTimeoutRef.current = null;
- }
- if (pollIntervalRef.current) {
- clearInterval(pollIntervalRef.current);
- pollIntervalRef.current = null;
- }
- if (pollTimeoutRef.current) {
- clearTimeout(pollTimeoutRef.current);
- pollTimeoutRef.current = null;
- }
- oauthWindowRef.current = null;
- setIsWaitingAuth(false);
- }, []);
-
- useEffect(() => () => cleanup(), [cleanup]);
-
- useEffect(() => {
- if (isWaitingAuth && !nextUnconnected) cleanup();
- }, [isWaitingAuth, nextUnconnected, cleanup]);
-
- const startFallbackPolling = useCallback(
- (target: ConnectTarget) => {
- if (pollIntervalRef.current) return;
-
- pollIntervalRef.current = setInterval(async () => {
- try {
- if (target.source === 'lobehub') {
- await checkLobehubStatus(target.provider);
- } else {
- await refreshKlavisServerTools(target.provider);
- }
- } catch {
- // Polling failure is expected until auth completes — suppress noise.
- }
- }, POLL_INTERVAL_MS);
-
- pollTimeoutRef.current = setTimeout(() => {
- if (pollIntervalRef.current) {
- clearInterval(pollIntervalRef.current);
- pollIntervalRef.current = null;
- }
- setIsWaitingAuth(false);
- }, POLL_TIMEOUT_MS);
- },
- [checkLobehubStatus, refreshKlavisServerTools],
- );
-
- const startWindowMonitor = useCallback(
- (oauthWindow: Window, target: ConnectTarget) => {
- const stopMonitor = () => {
- if (windowCheckIntervalRef.current) {
- clearInterval(windowCheckIntervalRef.current);
- windowCheckIntervalRef.current = null;
- }
- if (windowCheckTimeoutRef.current) {
- clearTimeout(windowCheckTimeoutRef.current);
- windowCheckTimeoutRef.current = null;
- }
- };
-
- windowCheckIntervalRef.current = setInterval(() => {
- try {
- if (oauthWindow.closed) {
- stopMonitor();
- oauthWindowRef.current = null;
- startFallbackPolling(target);
- }
- } catch {
- // COOP can block window.closed access — fall back to polling.
- stopMonitor();
- startFallbackPolling(target);
- }
- }, 500);
-
- windowCheckTimeoutRef.current = setTimeout(() => {
- stopMonitor();
- oauthWindowRef.current = null;
- setIsWaitingAuth(false);
- }, OAUTH_OVERALL_TIMEOUT_MS);
- },
- [startFallbackPolling],
- );
-
- const openOAuthWindow = useCallback(
- (url: string, target: ConnectTarget) => {
- cleanup();
- setIsWaitingAuth(true);
-
- const oauthWindow = window.open(url, '_blank', 'width=600,height=700');
- if (oauthWindow) {
- oauthWindowRef.current = oauthWindow;
- startWindowMonitor(oauthWindow, target);
- } else {
- startFallbackPolling(target);
- }
- },
- [cleanup, startWindowMonitor, startFallbackPolling],
- );
-
- // Only LobeHub Skill OAuth signals completion via postMessage; Klavis relies on polling.
- useEffect(() => {
- const handler = (event: MessageEvent) => {
- if (event.origin !== window.location.origin) return;
- // Reject same-origin iframes / other tabs forging the success event.
- if (event.source !== oauthWindowRef.current) return;
- if (event.data?.type !== LOBEHUB_SKILL_AUTH_SUCCESS_MESSAGE) return;
- const provider = event.data?.provider;
- if (!provider) return;
- cleanup();
- void checkLobehubStatus(provider);
- };
- window.addEventListener('message', handler);
- return () => window.removeEventListener('message', handler);
- }, [checkLobehubStatus, cleanup]);
-
- const connect = useCallback(async () => {
- if (isConnectingRef.current || isWaitingAuth) return;
- const next = nextUnconnected;
- if (!next) return;
-
- isConnectingRef.current = true;
- setIsConnecting(true);
- try {
- if (next.source === 'lobehub') {
- const redirectUri = `${window.location.origin}/oauth/callback/success?provider=${encodeURIComponent(
- next.provider,
- )}`;
- const { authorizeUrl } = await getLobehubAuth(next.provider, { redirectUri });
- openOAuthWindow(authorizeUrl, next);
- return;
- }
-
- const userId = useUserStore.getState().user?.id;
- if (!userId) return;
- const klavisType = KLAVIS_SERVER_TYPES.find((t) => t.identifier === next.provider);
- if (!klavisType) return;
- const newServer = await createKlavisServer({
- identifier: next.provider,
- serverName: klavisType.serverName,
- userId,
- });
- if (!newServer) return;
- if (newServer.isAuthenticated) {
- await refreshKlavisServerTools(newServer.identifier);
- } else if (newServer.oauthUrl) {
- openOAuthWindow(newServer.oauthUrl, next);
- }
- } catch (error) {
- console.error('[useSkillConnection] Failed to connect:', error);
- } finally {
- isConnectingRef.current = false;
- setIsConnecting(false);
- }
- }, [
- nextUnconnected,
- isWaitingAuth,
- getLobehubAuth,
- createKlavisServer,
- refreshKlavisServerTools,
- openOAuthWindow,
- ]);
-
- return {
- connect,
- isAllConnected,
- isConnecting: isConnecting || isWaitingAuth,
- needsConnect,
- nextUnconnected,
- };
-};
diff --git a/src/locales/default/taskTemplate.ts b/src/locales/default/taskTemplate.ts
index 0f69435454b..75d11edded2 100644
--- a/src/locales/default/taskTemplate.ts
+++ b/src/locales/default/taskTemplate.ts
@@ -1,5 +1,8 @@
export default {
'action.connect.button': 'Connect {{provider}}',
+ 'action.connect.error': 'Connection failed, please try again.',
+ 'action.connect.popupBlocked':
+ 'Connection popup blocked. Allow popups in your browser to continue.',
'action.create.error': 'Failed to create task. Please try again.',
'action.create.success': 'Scheduled task added. Find it in Lobe AI.',
'action.createButton': 'Add as scheduled task',