- 版本目标 → 收录版本(前后端用户可见字符串、能力描述、给模型的提示) - Agent 上游 / 上游 → 模型服务;界面上的 Agent / Agent 会话 → 智能体 / 智能体会话 - 身份歧义 / 识别有歧义 / 无法确定作品 → 没识别出作品;待识别(池) → 未识别 - 建档 → 加入媒体库;资产 → 文件;库根 → 媒体库目录;纳管 → 本工具 / 外部任务; 哨兵 → 标记文件 / 检查目录;站点快照 → 抓取;Feed / RSS 订阅 / 来源健康 → RSS - 代码标识符、API 字段名、迁移文件不改;glossary.md 同步;smoke 选择器跟随 Co-Authored-By: Claude Opus 5.5 <[email protected]>
172 lines
5.8 KiB
TypeScript
172 lines
5.8 KiB
TypeScript
import { keepPreviousData, useInfiniteQuery, useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import {
|
|
AcquisitionPlan,
|
|
cursorPage,
|
|
Dashboard,
|
|
DownloaderInstance,
|
|
type DownloadStatus,
|
|
DownloadTask,
|
|
type PlanStatus,
|
|
} from "@umt/contracts";
|
|
import { z } from "zod";
|
|
import { TTL_SECOND } from "~/lib/api.ts";
|
|
import { apiMutate, apiParsed, cleanParams } from "./parse.ts";
|
|
|
|
// 下载任务、下载计划、缓存卷(W2-C:/download-tasks、/acquisition-plans、/download-cache/cleanup、/dashboard)
|
|
|
|
const DownloadPage = cursorPage(DownloadTask);
|
|
const PlanPage = cursorPage(AcquisitionPlan);
|
|
|
|
export type DownloadFilters = { status?: DownloadStatus; instanceId?: string; managed?: "true" | "false" };
|
|
|
|
export const downloadKeys = {
|
|
all: ["downloads"] as const,
|
|
list: (f: DownloadFilters) => ["downloads", "list", f] as const,
|
|
one: (id: string) => ["downloads", "one", id] as const,
|
|
plans: (status?: PlanStatus) => ["downloads", "plans", status ?? "all"] as const,
|
|
plan: (id: string) => ["downloads", "plan", id] as const,
|
|
cache: () => ["downloads", "cache"] as const,
|
|
};
|
|
|
|
const ACTIVE: DownloadStatus[] = ["queued", "submitting", "downloading", "importing", "stalled"];
|
|
|
|
export function useDownloadTasks(f: DownloadFilters) {
|
|
return useInfiniteQuery({
|
|
queryKey: downloadKeys.list(f),
|
|
initialPageParam: undefined as string | undefined,
|
|
placeholderData: keepPreviousData,
|
|
queryFn: ({ pageParam, signal }) =>
|
|
apiParsed(DownloadPage, "/api/v1/download-tasks", {
|
|
params: cleanParams({ ...f, cursor: pageParam, limit: 50 }),
|
|
signal,
|
|
}),
|
|
getNextPageParam: (last) => last.nextCursor ?? undefined,
|
|
// 有进行中的任务时 10 秒轮询进度(SSE download.progress 到达时也会失效重取)
|
|
refetchInterval: (q) =>
|
|
q.state.data?.pages.some((p) => p.items.some((t) => ACTIVE.includes(t.status))) ? 10 * TTL_SECOND : false,
|
|
});
|
|
}
|
|
|
|
export function useDownloadTask(id: string | undefined) {
|
|
return useQuery({
|
|
queryKey: downloadKeys.one(id ?? ""),
|
|
enabled: !!id,
|
|
queryFn: ({ signal }) =>
|
|
apiParsed(DownloadTask, "/api/v1/download-tasks/:id", { pathParams: { id: id ?? "" }, signal }),
|
|
});
|
|
}
|
|
|
|
export function usePlans(status?: PlanStatus) {
|
|
return useInfiniteQuery({
|
|
queryKey: downloadKeys.plans(status),
|
|
initialPageParam: undefined as string | undefined,
|
|
placeholderData: keepPreviousData,
|
|
queryFn: ({ pageParam, signal }) =>
|
|
apiParsed(PlanPage, "/api/v1/acquisition-plans", {
|
|
params: cleanParams({ status, cursor: pageParam, limit: 50 }),
|
|
signal,
|
|
}),
|
|
getNextPageParam: (last) => last.nextCursor ?? undefined,
|
|
});
|
|
}
|
|
|
|
export function usePlan(id: string | null | undefined) {
|
|
return useQuery({
|
|
queryKey: downloadKeys.plan(id ?? ""),
|
|
enabled: !!id,
|
|
queryFn: ({ signal }) =>
|
|
apiParsed(AcquisitionPlan, "/api/v1/acquisition-plans/:id", { pathParams: { id: id ?? "" }, signal }),
|
|
});
|
|
}
|
|
|
|
/** 暂停(记为用户暂停)/ 恢复;阈值暂停在水位未回落前服务端拒绝恢复 */
|
|
export function useDownloadAction() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ id, action }: { id: string; action: "pause" | "resume" }) =>
|
|
apiMutate(`/api/v1/download-tasks/:id/${action}`, { pathParams: { id } }).then((d) => DownloadTask.parse(d)),
|
|
onSuccess: (d) => {
|
|
qc.setQueryData(downloadKeys.one(d.id), d);
|
|
qc.invalidateQueries({ queryKey: ["downloads", "list"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
/** 缓存卷用量/阈值:取首页聚合里的 cache 区块(同一统计口径) */
|
|
export function useCacheVolumes() {
|
|
return useQuery({
|
|
queryKey: downloadKeys.cache(),
|
|
queryFn: ({ signal }) => apiParsed(Dashboard, "/api/v1/dashboard", { signal }),
|
|
select: (d) => d.cache.volumes,
|
|
refetchInterval: 30 * TTL_SECOND,
|
|
});
|
|
}
|
|
|
|
/** 手动清理:服务端入队 cleanupDownloadCache(只清已验证入库且满足保留/H&R 的纳管任务),返回任务 id */
|
|
export function useCacheCleanup() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (downloaderInstanceId?: string) =>
|
|
apiMutate("/api/v1/download-cache/cleanup", {
|
|
body: downloaderInstanceId ? { downloaderInstanceId } : {},
|
|
}).then((d) => z.object({ taskId: z.string(), runId: z.string() }).parse(d)),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: downloadKeys.all }),
|
|
});
|
|
}
|
|
|
|
/** 下载器实例名(列表展示用) */
|
|
export function useDownloaderNames() {
|
|
return useQuery({
|
|
queryKey: ["downloads", "instances"],
|
|
staleTime: 5 * 60 * TTL_SECOND,
|
|
queryFn: ({ signal }) =>
|
|
apiParsed(cursorPage(DownloaderInstance), "/api/v1/downloader-instances", { params: { limit: 200 }, signal }),
|
|
select: (d) => new Map(d.items.map((i) => [i.id, i.name])),
|
|
});
|
|
}
|
|
|
|
export const DOWNLOAD_STATUS_LABEL: Record<DownloadStatus, string> = {
|
|
queued: "排队",
|
|
submitting: "提交中",
|
|
downloading: "下载中",
|
|
paused: "已暂停",
|
|
stalled: "无进展",
|
|
completed: "下载完成",
|
|
importing: "入库中",
|
|
imported: "已入库",
|
|
failed: "失败",
|
|
removed: "已移除",
|
|
missing: "下载器中缺失",
|
|
};
|
|
|
|
export const DOWNLOAD_STATUS_TONE: Record<DownloadStatus, "success" | "warning" | "destructive" | "neutral"> = {
|
|
queued: "neutral",
|
|
submitting: "neutral",
|
|
downloading: "neutral",
|
|
paused: "warning",
|
|
stalled: "warning",
|
|
completed: "success",
|
|
importing: "neutral",
|
|
imported: "success",
|
|
failed: "destructive",
|
|
removed: "neutral",
|
|
missing: "destructive",
|
|
};
|
|
|
|
export const PAUSE_REASON_LABEL = {
|
|
user: "用户暂停",
|
|
threshold: "阈值暂停",
|
|
policy: "策略暂停",
|
|
agent: "智能体暂停",
|
|
} as const;
|
|
|
|
export const PLAN_STATUS_LABEL: Record<PlanStatus, string> = {
|
|
draft: "草稿",
|
|
reserved: "已预留",
|
|
submitted: "已提交",
|
|
completed: "已完成",
|
|
cancelled: "已取消",
|
|
failed: "失败",
|
|
expired: "已过期",
|
|
};
|