179 lines
5.7 KiB
TypeScript
179 lines
5.7 KiB
TypeScript
"use client";
|
|
|
|
import { AlertCircleIcon, Loader2Icon, PackageIcon } from "lucide-react";
|
|
import { useEffect, useMemo, useState } from "react";
|
|
import { toast } from "sonner";
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
} from "@/components/ui/dialog";
|
|
import { Input } from "@/components/ui/input";
|
|
import {
|
|
installSkill,
|
|
previewSkillInstall,
|
|
type PreviewSkillInstallResponse,
|
|
} from "@/core/skills/api";
|
|
|
|
export function SkillInstallDialog({
|
|
open,
|
|
onOpenChange,
|
|
threadId,
|
|
filepath,
|
|
onInstalled,
|
|
}: {
|
|
open: boolean;
|
|
onOpenChange: (open: boolean) => void;
|
|
threadId: string;
|
|
filepath: string | null;
|
|
onInstalled?: (skillName: string) => void;
|
|
}) {
|
|
const [preview, setPreview] = useState<PreviewSkillInstallResponse | null>(null);
|
|
const [installName, setInstallName] = useState("");
|
|
const [loading, setLoading] = useState(false);
|
|
const [installing, setInstalling] = useState(false);
|
|
|
|
useEffect(() => {
|
|
if (!open || !filepath) {
|
|
setPreview(null);
|
|
setInstallName("");
|
|
setLoading(false);
|
|
setInstalling(false);
|
|
return;
|
|
}
|
|
|
|
let cancelled = false;
|
|
setLoading(true);
|
|
setPreview(null);
|
|
setInstallName("");
|
|
|
|
void previewSkillInstall({ thread_id: threadId, path: filepath })
|
|
.then((result) => {
|
|
if (cancelled) return;
|
|
setPreview(result);
|
|
setInstallName(result.skill_name);
|
|
})
|
|
.catch((error) => {
|
|
if (cancelled) return;
|
|
toast.error(error instanceof Error ? error.message : "Failed to preview skill install");
|
|
onOpenChange(false);
|
|
})
|
|
.finally(() => {
|
|
if (!cancelled) setLoading(false);
|
|
});
|
|
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, [filepath, onOpenChange, open, threadId]);
|
|
|
|
const trimmedName = installName.trim();
|
|
const unchangedConflict = useMemo(() => {
|
|
if (!preview) return false;
|
|
return trimmedName === preview.skill_name && !preview.can_install_as_is;
|
|
}, [preview, trimmedName]);
|
|
|
|
async function handleInstall() {
|
|
if (!filepath || !trimmedName || installing) return;
|
|
setInstalling(true);
|
|
try {
|
|
const result = await installSkill({
|
|
thread_id: threadId,
|
|
path: filepath,
|
|
skill_name: trimmedName,
|
|
});
|
|
if (!result.success) {
|
|
toast.error(result.message || "Failed to install skill");
|
|
return;
|
|
}
|
|
toast.success(result.message);
|
|
onInstalled?.(result.skill_name);
|
|
onOpenChange(false);
|
|
} catch (error) {
|
|
toast.error(error instanceof Error ? error.message : "Failed to install skill");
|
|
} finally {
|
|
setInstalling(false);
|
|
}
|
|
}
|
|
|
|
const helperText = preview?.owned_by_other
|
|
? `该技能名已被 ${preview.owner_username || "其他用户"} 安装。你可以换个名字继续安装。`
|
|
: preview?.owned_by_me
|
|
? "你已经安装过这个技能了;如果想保留一份新副本,可以换个名字安装。"
|
|
: preview?.builtin_conflict
|
|
? "这个名字已被公共技能占用,请换个名字安装。"
|
|
: "可以直接安装,也可以先改一个更适合自己的名字。";
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
<DialogContent className="sm:max-w-md">
|
|
<DialogHeader>
|
|
<DialogTitle>安装技能</DialogTitle>
|
|
<DialogDescription>安装前会校验技能归属;如果名字已被占用,可以改名后继续安装。</DialogDescription>
|
|
</DialogHeader>
|
|
|
|
{loading ? (
|
|
<div className="text-muted-foreground flex items-center justify-center gap-2 py-8 text-sm">
|
|
<Loader2Icon className="size-4 animate-spin" />
|
|
正在检查技能安装权限…
|
|
</div>
|
|
) : preview ? (
|
|
<div className="space-y-4">
|
|
<div className="rounded-xl border bg-muted/35 p-3 text-sm">
|
|
<div className="font-medium">{preview.skill_name}</div>
|
|
{preview.description ? (
|
|
<div className="text-muted-foreground mt-1 line-clamp-3">{preview.description}</div>
|
|
) : null}
|
|
</div>
|
|
|
|
<div className="space-y-2">
|
|
<div className="text-sm font-medium">安装名称</div>
|
|
<Input
|
|
value={installName}
|
|
onChange={(e) => setInstallName(e.currentTarget.value)}
|
|
placeholder="请输入技能名称"
|
|
/>
|
|
<div className="text-muted-foreground text-xs">{helperText}</div>
|
|
</div>
|
|
|
|
{!preview.can_install_as_is && (
|
|
<div className="flex gap-2 rounded-xl border border-amber-300/60 bg-amber-50 px-3 py-2 text-sm text-amber-900">
|
|
<AlertCircleIcon className="mt-0.5 size-4 shrink-0" />
|
|
<div>{preview.message}</div>
|
|
</div>
|
|
)}
|
|
|
|
{preview.warnings.length > 0 && (
|
|
<div className="text-muted-foreground rounded-xl border px-3 py-2 text-xs">
|
|
{preview.warnings.join(" ")}
|
|
</div>
|
|
)}
|
|
</div>
|
|
) : null}
|
|
|
|
<DialogFooter>
|
|
<Button variant="outline" onClick={() => onOpenChange(false)} disabled={installing}>
|
|
取消
|
|
</Button>
|
|
<Button
|
|
onClick={() => void handleInstall()}
|
|
disabled={loading || installing || !trimmedName || unchangedConflict}
|
|
>
|
|
{installing ? (
|
|
<Loader2Icon className="size-4 animate-spin" />
|
|
) : (
|
|
<PackageIcon className="size-4" />
|
|
)}
|
|
安装
|
|
</Button>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|