π€― 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.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
import type { GitHubSkillDiscovery, GitHubSkillOutcome } from '@lobechat/types';
import { useState } from 'react';
import { usePermission } from '@/hooks/usePermission';
import { normalizeAsyncError } from '@/libs/swr/normalizeError';
import { agentSkillService } from '@/services/skill';
import { useToolStore } from '@/store/tool';
export interface GithubSkillImportFailure {
/** Raw message from the server, folded behind "Show details". */
details?: string;
recovery?: 'access' | 'connect';
stage: 'find' | 'import' | 'refresh';
}
const detailsOf = (err: unknown) => (err instanceof Error ? err.message : String(err));
const importFailure = (error: unknown, stage: 'find' | 'import'): GithubSkillImportFailure => {
const { code } = normalizeAsyncError(error);
if (code === 'PRECONDITION_FAILED') return { recovery: 'connect', stage };
const details = detailsOf(error);
// Older discovery endpoints return this GitHub error as an internal error.
if (
code === 'NOT_FOUND' ||
(stage === 'find' && details === 'GitHub repository or ref not found')
)
return { recovery: 'access', stage };
return { details, stage };
};
/**
* The scan β select β import state of the GitHub import modal: one request at
* a time, and a failure kept apart from the per-skill outcomes of a batch.
*/
export const useGithubSkillImport = () => {
const refresh = useToolStore((s) => s.refreshAgentSkills);
const { allowed: canCreate } = usePermission('create_content');
const [pending, setPending] = useState<GithubSkillImportFailure['stage']>();
const loading = !!pending;
const [failure, setFailure] = useState<GithubSkillImportFailure>();
const [url, setUrlValue] = useState('');
const [query, setQuery] = useState('');
const [scan, setScan] = useState<GitHubSkillDiscovery>();
const [selected, setSelected] = useState<string[]>([]);
const [results, setResults] = useState<GitHubSkillOutcome[]>([]);
const candidates = scan?.candidates ?? [];
/** A scan belongs to the URL it was read from, so editing the URL drops it. */
const setUrl = (value: string) => {
setUrlValue(value);
setScan(undefined);
setResults([]);
setFailure(undefined);
};
const discover = async () => {
if (!canCreate || !url.trim() || loading) return;
setPending('find');
setFailure(undefined);
try {
const next = await agentSkillService.discoverGitHubSkills(url.trim());
// A server that predates a field must not take the modal down with it.
const found = next.candidates ?? [];
setScan({ ...next, candidates: found });
setQuery('');
setResults([]);
setSelected(found.length === 1 && found[0].status === 'new' ? [found[0].path] : []);
} catch (err) {
setFailure(importFailure(err, 'find'));
} finally {
setPending(undefined);
}
};
const resumeAfterConnect = () => {
if (failure?.stage === 'find') return discover();
// A batch still needs an explicit import. Keep its pinned commit and selection.
setFailure(undefined);
};
const refreshLibrary = async () => {
setPending('refresh');
setFailure(undefined);
try {
await refresh();
} catch (err) {
setFailure({ details: detailsOf(err), stage: 'refresh' });
} finally {
setPending(undefined);
}
};
const install = async (paths: string[]) => {
if (!canCreate || !scan || !paths.length || loading) return;
setPending('import');
setFailure(undefined);
try {
const batch = await agentSkillService.importGitHubSkills({
commit: scan.commit,
gitUrl: url.trim(),
scope: scan.scope,
selections: candidates
.filter((item) => paths.includes(item.path))
.map(({ path, revision }) => ({ path, revision })),
});
setResults((prev) => [...prev.filter((item) => !paths.includes(item.path)), ...batch]);
setQuery('');
} catch (err) {
setFailure(importFailure(err, 'import'));
setPending(undefined);
return;
}
await refreshLibrary();
};
return {
canCreate,
candidates,
discover,
failure,
install,
loading,
pending,
query,
refreshLibrary,
results,
resumeAfterConnect,
scan,
selected,
setQuery,
setSelected,
setUrl,
url,
};
};