1109 lines
37 KiB
Svelte
1109 lines
37 KiB
Svelte
<script lang="ts">
|
||
import { onMount } from "svelte";
|
||
import { storageGet, storageSet } from "$lib/storage";
|
||
import { isWorkflowRunnable, workflowState } from "$lib/workflowState";
|
||
import FamilyBadge from "$lib/components/manage/FamilyBadge.svelte";
|
||
import ManagementPage from "$lib/components/manage/ManagementPage.svelte";
|
||
import { page } from "$app/state";
|
||
import { createMutation, useQueryClient } from "@tanstack/svelte-query";
|
||
import Button from "$lib/components/ui/Button.svelte";
|
||
import ConfirmDialog from "$lib/components/ui/ConfirmDialog.svelte";
|
||
import Icon from "$lib/components/ui/Icon.svelte";
|
||
import Switch from "$lib/components/ui/Switch.svelte";
|
||
import WorkflowImportDrawer from "$lib/components/WorkflowImportDrawer.svelte";
|
||
import WorkflowTestDialog from "$lib/components/WorkflowTestDialog.svelte";
|
||
import WorkflowBatchImportWizard from "$lib/components/workflows/WorkflowBatchImportWizard.svelte";
|
||
import WorkflowMetaDialog from "$lib/components/workflows/WorkflowMetaDialog.svelte";
|
||
import {
|
||
apiErrorMessage,
|
||
workflows,
|
||
type ComfyEndpoint,
|
||
type Workflow,
|
||
type WorkflowMeta,
|
||
type WorkflowOutputMapEntry,
|
||
type WorkflowUsage,
|
||
} from "$lib/api";
|
||
import { WORKFLOW_USAGES } from "$lib/comfy/types";
|
||
import { t } from "$lib/i18n.svelte";
|
||
import { toast } from "$lib/toast";
|
||
|
||
/**
|
||
* 工作流库 (HEL-682 / S4):以 view=meta 轻量元数据驱动的管理列表——
|
||
* 搜索 / 分类·模型·媒介筛选 / 收藏 / 最近使用 / 启停 / 去重 / 批量标注,
|
||
* 全程不下载每条的完整工作流 JSON;完整数据只在试运行、输出映射等
|
||
* 操作时按需单条加载。试运行复用 WorkflowTestDialog。
|
||
*/
|
||
interface Props {
|
||
rows: WorkflowMeta[];
|
||
endpoints: ComfyEndpoint[];
|
||
loading?: boolean;
|
||
error?: boolean;
|
||
onRetry?: () => void;
|
||
}
|
||
|
||
let {
|
||
rows,
|
||
endpoints,
|
||
loading = false,
|
||
error = false,
|
||
onRetry,
|
||
}: Props = $props();
|
||
|
||
// A pending workflow on the creative desk leads to its actual mapping/test row.
|
||
let deepLinkDone = false;
|
||
$effect(() => {
|
||
if (deepLinkDone || !rows.length) return;
|
||
const id = Number(page.url.searchParams.get("wf"));
|
||
const row = rows.find((r) => r.id === id);
|
||
deepLinkDone = true;
|
||
if (!row) return;
|
||
activeId = row.id;
|
||
if (rowNeedsOutputMap(row)) void toggleWfMapEdit(row);
|
||
else void ensureFull(row.id);
|
||
});
|
||
|
||
const client = useQueryClient();
|
||
let pins = $state<number[]>([]);
|
||
onMount(() => {
|
||
try {
|
||
const saved = JSON.parse(
|
||
storageGet("calliope.reference.workflowPins") ?? "[]",
|
||
);
|
||
pins = Array.isArray(saved)
|
||
? saved.filter((id): id is number => typeof id === "number")
|
||
: [];
|
||
} catch {
|
||
pins = [];
|
||
}
|
||
});
|
||
function pin(id: number) {
|
||
pins = pins.includes(id)
|
||
? pins.filter((value) => value !== id)
|
||
: [...pins, id];
|
||
storageSet("calliope.reference.workflowPins", JSON.stringify(pins));
|
||
}
|
||
function inputNodeLabel(wf: Workflow, id: string, label: string): string {
|
||
const [nodeId, field] = id.split("@");
|
||
const node = wf.workflow_json[nodeId];
|
||
const name = typeof node?.class_type === "string" ? node.class_type : label;
|
||
return nodeId + " · " + name + (field ? "." + field : "");
|
||
}
|
||
|
||
function invalidateWorkflows() {
|
||
return Promise.all([
|
||
client.invalidateQueries({ queryKey: ["workflows"] }),
|
||
client.invalidateQueries({ queryKey: ["workflows-meta"] }),
|
||
]);
|
||
}
|
||
|
||
// ------------------------------------------------------------------
|
||
// Category label (S2 stores the key; the label never leaks dir names)
|
||
// ------------------------------------------------------------------
|
||
const CATEGORY_LABELS: Record<string, string> = {
|
||
basic: "wfCat.basic",
|
||
audio_tts: "wfCat.audioTts",
|
||
music: "wfCat.music",
|
||
image: "wfCat.image",
|
||
video: "wfCat.video",
|
||
digital_human: "wfCat.digitalHuman",
|
||
motion_transfer: "wfCat.motionTransfer",
|
||
};
|
||
function categoryLabel(key: string | null | undefined): string {
|
||
if (!key) return "";
|
||
const k = CATEGORY_LABELS[key];
|
||
return k ? t(k) : key;
|
||
}
|
||
|
||
// ------------------------------------------------------------------
|
||
// Filters / search / sort
|
||
// ------------------------------------------------------------------
|
||
let search = $state("");
|
||
let kindFilter = $state<"all" | "image" | "video">("all");
|
||
let statusFilter = $state<
|
||
"all" | "pending" | "enabled" | "disabled" | "problem"
|
||
>("all");
|
||
let favOnly = $state(false);
|
||
let catFilter = $state("all");
|
||
let familyFilter = $state("all");
|
||
let mediaFilter = $state("all");
|
||
let sortBy = $state<"recent" | "name">("recent");
|
||
let visibleCount = $state(60);
|
||
let searchInput = $state<HTMLInputElement | null>(null);
|
||
|
||
const categories = $derived(
|
||
[
|
||
...new Set(rows.map((r) => r.category).filter((c): c is string => !!c)),
|
||
].sort(),
|
||
);
|
||
const families = $derived(
|
||
[
|
||
...new Set(
|
||
rows.map((r) => r.model_family).filter((f): f is string => !!f),
|
||
),
|
||
].sort(),
|
||
);
|
||
|
||
const filtered = $derived.by(() => {
|
||
const q = search.trim().toLowerCase();
|
||
let list = rows.filter((r) => {
|
||
if (kindFilter !== "all" && r.kind !== kindFilter) return false;
|
||
if (favOnly && !r.favorite) return false;
|
||
if (catFilter !== "all" && (r.category ?? "") !== catFilter) return false;
|
||
if (familyFilter !== "all" && (r.model_family ?? "") !== familyFilter)
|
||
return false;
|
||
if (mediaFilter !== "all") {
|
||
const io = r.media_io ?? { in: [], out: [] };
|
||
if (!io.in.includes(mediaFilter) && !io.out.includes(mediaFilter))
|
||
return false;
|
||
}
|
||
if (statusFilter === "pending" && !r.needs_mapping) return false;
|
||
if (statusFilter === "enabled" && !isWorkflowRunnable(r)) return false;
|
||
if (statusFilter === "disabled" && r.is_enabled) return false;
|
||
if (
|
||
statusFilter === "problem" &&
|
||
!wfRecheckResult[r.id]?.findings.some((f) => f.level === "error")
|
||
)
|
||
return false;
|
||
if (q) {
|
||
const hay = [
|
||
r.name,
|
||
r.description ?? "",
|
||
(r.tags ?? []).join(" "),
|
||
r.model_family ?? "",
|
||
categoryLabel(r.category),
|
||
r.source_path ?? "",
|
||
]
|
||
.join(" ")
|
||
.toLowerCase();
|
||
if (!hay.includes(q)) return false;
|
||
}
|
||
return true;
|
||
});
|
||
if (sortBy === "recent")
|
||
list = [...list].sort((a, b) =>
|
||
(b.last_used_at ?? "").localeCompare(a.last_used_at ?? ""),
|
||
);
|
||
if (sortBy === "name")
|
||
list = [...list].sort((a, b) => a.name.localeCompare(b.name, "zh"));
|
||
return list;
|
||
});
|
||
|
||
const visible = $derived(filtered.slice(0, visibleCount));
|
||
|
||
$effect(() => {
|
||
// any filter change collapses the incremental list back to page one
|
||
void search;
|
||
void kindFilter;
|
||
void statusFilter;
|
||
void favOnly;
|
||
void catFilter;
|
||
void familyFilter;
|
||
void mediaFilter;
|
||
void sortBy;
|
||
visibleCount = 60;
|
||
});
|
||
|
||
function resetFilters() {
|
||
search = "";
|
||
kindFilter = "all";
|
||
statusFilter = "all";
|
||
favOnly = false;
|
||
catFilter = "all";
|
||
familyFilter = "all";
|
||
mediaFilter = "all";
|
||
}
|
||
|
||
const hasActiveFilter = $derived(
|
||
search.trim() !== "" ||
|
||
kindFilter !== "all" ||
|
||
statusFilter !== "all" ||
|
||
favOnly ||
|
||
catFilter !== "all" ||
|
||
familyFilter !== "all" ||
|
||
mediaFilter !== "all",
|
||
);
|
||
|
||
// ------------------------------------------------------------------
|
||
// Selection + batch actions
|
||
// ------------------------------------------------------------------
|
||
let selected = $state<Set<number>>(new Set());
|
||
let bulkMode = $state(false);
|
||
|
||
const allVisibleSelected = $derived(
|
||
visible.length > 0 && visible.every((r) => selected.has(r.id)),
|
||
);
|
||
|
||
function toggleRow(id: number, on: boolean) {
|
||
const next = new Set(selected);
|
||
if (on) next.add(id);
|
||
else next.delete(id);
|
||
selected = next;
|
||
}
|
||
|
||
function toggleSelectVisible() {
|
||
const next = new Set(selected);
|
||
if (allVisibleSelected) {
|
||
for (const r of visible) next.delete(r.id);
|
||
} else {
|
||
for (const r of visible) next.add(r.id);
|
||
}
|
||
selected = next;
|
||
}
|
||
|
||
let batchPending = $state(false);
|
||
let metaDialogOpen = $state(false);
|
||
let metaTargets = $state<WorkflowMeta[]>([]);
|
||
|
||
async function batchSetEnabled(enabled: boolean) {
|
||
batchPending = true;
|
||
let done = 0;
|
||
let failed = 0;
|
||
try {
|
||
for (const row of rows) {
|
||
if (!selected.has(row.id)) continue;
|
||
if (row.is_enabled === enabled) {
|
||
done++;
|
||
continue;
|
||
}
|
||
try {
|
||
const updated = await workflows.update(row.id, {
|
||
is_enabled: enabled,
|
||
});
|
||
fullCache[row.id] = updated;
|
||
done++;
|
||
} catch {
|
||
failed++;
|
||
}
|
||
}
|
||
} finally {
|
||
batchPending = false;
|
||
}
|
||
invalidateWorkflows();
|
||
if (failed === 0) toast.success(t("wl.batchDone", { n: done }));
|
||
else toast.error(t("wl.batchPartial", { n: done, m: failed }));
|
||
}
|
||
|
||
// ------------------------------------------------------------------
|
||
// Single-row actions (favorite / enable / usage / bind / delete / meta)
|
||
// ------------------------------------------------------------------
|
||
const favMutation = createMutation({
|
||
mutationFn: ({ id, fav }: { id: number; fav: boolean }) =>
|
||
workflows.update(id, { favorite: fav }),
|
||
onSuccess: () => invalidateWorkflows(),
|
||
onError: (err) => toast.error(apiErrorMessage(err)),
|
||
});
|
||
|
||
const wfConfirmMutation = createMutation({
|
||
mutationFn: ({ id, usage }: { id: number; usage: WorkflowUsage }) =>
|
||
workflows.update(id, { usage, usage_source: "user" }),
|
||
onSuccess: (wf) => {
|
||
invalidateWorkflows();
|
||
toast.success(t("conn.wfConfirmed", { usage: usageLabel(wf.usage) }));
|
||
},
|
||
onError: (err) => toast.error(apiErrorMessage(err)),
|
||
});
|
||
|
||
const wfBindMutation = createMutation({
|
||
mutationFn: ({
|
||
id,
|
||
endpointId,
|
||
}: {
|
||
id: number;
|
||
endpointId: string | null;
|
||
}) => workflows.update(id, { endpoint_id: endpointId }),
|
||
onSuccess: () => invalidateWorkflows(),
|
||
});
|
||
|
||
const wfDeleteMutation = createMutation({
|
||
mutationFn: (id: number) => workflows.delete(id),
|
||
onSuccess: (_res, id) => {
|
||
invalidateWorkflows();
|
||
const next = new Set(selected);
|
||
next.delete(id);
|
||
selected = next;
|
||
toast.success(t("conn.wfDeleted"));
|
||
},
|
||
onError: (err) => toast.error(apiErrorMessage(err)),
|
||
});
|
||
|
||
function usageLabel(u: WorkflowUsage | null | undefined): string {
|
||
return u ? t(`usage.${u}`) : t("usage.pending");
|
||
}
|
||
|
||
function openMetaDialog(targetRows: WorkflowMeta[]) {
|
||
metaTargets = targetRows;
|
||
metaDialogOpen = true;
|
||
}
|
||
|
||
// ------------------------------------------------------------------
|
||
// Lazy full-row loads — the ONLY place full workflow JSON is fetched
|
||
// ------------------------------------------------------------------
|
||
const fullCache = $state<Record<number, Workflow>>({});
|
||
const fullPending = $state<Record<number, boolean>>({});
|
||
|
||
const fullRequests: Partial<Record<number, Promise<Workflow | null>>> = {};
|
||
async function ensureFull(
|
||
id: number,
|
||
refresh = false,
|
||
): Promise<Workflow | null> {
|
||
if (refresh) {
|
||
await fullRequests[id];
|
||
delete fullCache[id];
|
||
}
|
||
if (fullCache[id]) return fullCache[id];
|
||
if (fullRequests[id]) return fullRequests[id];
|
||
fullPending[id] = true;
|
||
const request = (async () => {
|
||
try {
|
||
const wf = await workflows.get(id);
|
||
fullCache[id] = wf;
|
||
return wf;
|
||
} catch (err) {
|
||
toast.error(apiErrorMessage(err));
|
||
return null;
|
||
} finally {
|
||
delete fullPending[id];
|
||
delete fullRequests[id];
|
||
}
|
||
})();
|
||
fullRequests[id] = request;
|
||
return request;
|
||
}
|
||
|
||
// ------------------------------------------------------------------
|
||
// Test run (reuses WorkflowTestDialog) + L2 recheck
|
||
// ------------------------------------------------------------------
|
||
let testOpen = $state(false);
|
||
let testWf = $state<Workflow | null>(null);
|
||
let openingTestId = $state<number | null>(null);
|
||
// HEL-685 键盘焦点:openTest 会在异步加载期间禁用触发按钮,焦点先掉回
|
||
// body,抽屉关闭时就还原不到触发键。这里记下触发元素,关闭后兜底还原。
|
||
let testTriggerEl: HTMLElement | null = null;
|
||
|
||
async function openTest(row: WorkflowMeta) {
|
||
openingTestId = row.id;
|
||
const wf = await ensureFull(row.id, true);
|
||
openingTestId = null;
|
||
if (wf) {
|
||
testWf = wf;
|
||
testOpen = true;
|
||
}
|
||
}
|
||
|
||
$effect(() => {
|
||
if (!testOpen && testTriggerEl) {
|
||
const el = testTriggerEl;
|
||
testTriggerEl = null;
|
||
if (document.activeElement === document.body && el.isConnected)
|
||
el.focus();
|
||
}
|
||
});
|
||
|
||
let wfRecheckId = $state<number | null>(null);
|
||
type ConnectionCheck = Awaited<ReturnType<typeof workflows.check>>;
|
||
const wfRecheckResult = $state<Record<number, ConnectionCheck>>({});
|
||
|
||
async function recheckRow(row: WorkflowMeta) {
|
||
wfRecheckId = row.id;
|
||
try {
|
||
const res = await workflows.check(row.id, { refresh: true });
|
||
wfRecheckResult[row.id] = res;
|
||
} catch (err) {
|
||
toast.error(apiErrorMessage(err));
|
||
} finally {
|
||
wfRecheckId = null;
|
||
}
|
||
}
|
||
|
||
function recheckLine(
|
||
row: WorkflowMeta,
|
||
): { tone: "ok" | "bad" | "warn"; text: string } | null {
|
||
const check = wfRecheckResult[row.id];
|
||
if (!check) return null;
|
||
if (!check.reachable)
|
||
return { tone: "warn", text: t("conn.wfCheckUnreachable") };
|
||
if (check.ok) return { tone: "ok", text: t("conn.wfCheckOk") };
|
||
return {
|
||
tone: "bad",
|
||
text: t("conn.wfCheckProblems", { n: check.findings.length }),
|
||
};
|
||
}
|
||
|
||
function lastTestLine(wf: Workflow | undefined): string | null {
|
||
const lt = wf?.last_test;
|
||
if (!lt) return null;
|
||
if (lt.status === "done" && lt.output_count > 0) {
|
||
return t("conn.wfTestPassed", {
|
||
n: lt.output_count,
|
||
time: formatDbTime(lt.completed_at ?? lt.created_at),
|
||
});
|
||
}
|
||
if (lt.status === "pending" || lt.status === "running") {
|
||
return t("conn.wfTestRunning", { time: formatDbTime(lt.created_at) });
|
||
}
|
||
return t("conn.wfTestFailed", {
|
||
time: formatDbTime(lt.completed_at ?? lt.created_at),
|
||
});
|
||
}
|
||
|
||
function formatDbTime(at: string | null): string {
|
||
if (!at) return "—";
|
||
const ts = Date.parse(at.replace(" ", "T") + "Z");
|
||
const d = new Date(Number.isFinite(ts) ? ts : at);
|
||
if (Number.isNaN(d.getTime())) return "—";
|
||
const p = (n: number) => String(n).padStart(2, "0");
|
||
return `${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}`;
|
||
}
|
||
|
||
// ------------------------------------------------------------------
|
||
// Output-node designation (HEL-638) — kept, on the lazy full row
|
||
// ------------------------------------------------------------------
|
||
let wfMapEditId = $state<number | null>(null);
|
||
let wfMapPicked = $state<Record<string, boolean>>({});
|
||
let wfMapKind = $state<Record<string, "image" | "video">>({});
|
||
|
||
function rowNeedsOutputMap(row: WorkflowMeta): boolean {
|
||
return row.output_map_source == null;
|
||
}
|
||
|
||
async function toggleWfMapEdit(row: WorkflowMeta) {
|
||
if (wfMapEditId === row.id) {
|
||
wfMapEditId = null;
|
||
return;
|
||
}
|
||
const wf = await ensureFull(row.id);
|
||
if (!wf) return;
|
||
const picked: Record<string, boolean> = {};
|
||
const kinds: Record<string, "image" | "video"> = {};
|
||
const mapped = new Map(
|
||
(wf.output_map ?? []).map((e) => [e.nodeId, e.kind]),
|
||
);
|
||
for (const c of (wf.output_schema ?? []).filter(
|
||
(o) => o.evidence === "unknown_node",
|
||
)) {
|
||
const known = mapped.get(c.nodeId);
|
||
if (known) picked[c.nodeId] = true;
|
||
kinds[c.nodeId] = known ?? c.suggested_kind ?? "image";
|
||
}
|
||
wfMapPicked = picked;
|
||
wfMapKind = kinds;
|
||
wfMapEditId = row.id;
|
||
}
|
||
|
||
const wfMapMutation = createMutation({
|
||
mutationFn: ({
|
||
id,
|
||
outputMap,
|
||
}: {
|
||
id: number;
|
||
outputMap: WorkflowOutputMapEntry[];
|
||
}) =>
|
||
workflows.update(
|
||
id,
|
||
outputMap.length
|
||
? { output_map: outputMap, output_map_source: "user" }
|
||
: { output_map: [] },
|
||
),
|
||
onSuccess: (wf) => {
|
||
invalidateWorkflows();
|
||
if (wf) fullCache[wf.id] = wf;
|
||
wfMapEditId = null;
|
||
const nodes = (wf.output_map ?? []).map((e) => e.nodeId).join("、");
|
||
toast.success(
|
||
wf.output_map?.length
|
||
? t("conn.wfOutputApplied", { nodes })
|
||
: t("conn.wfOutputCleared"),
|
||
);
|
||
},
|
||
onError: (err) => toast.error(apiErrorMessage(err)),
|
||
});
|
||
|
||
function submitWfMap(row: WorkflowMeta) {
|
||
const wf = fullCache[row.id];
|
||
if (!wf || $wfMapMutation.isPending) return;
|
||
const outputMap = (wf.output_schema ?? [])
|
||
.filter((o) => o.evidence === "unknown_node")
|
||
.filter((c) => wfMapPicked[c.nodeId])
|
||
.map((c) => ({
|
||
nodeId: c.nodeId,
|
||
kind: wfMapKind[c.nodeId] ?? c.suggested_kind ?? "image",
|
||
label: c.label,
|
||
...(c.class_type ? { class_type: c.class_type } : {}),
|
||
}));
|
||
$wfMapMutation.mutate({ id: row.id, outputMap });
|
||
}
|
||
|
||
// ------------------------------------------------------------------
|
||
// Import wizards + dedupe
|
||
// ------------------------------------------------------------------
|
||
let batchOpen = $state(false);
|
||
let singleOpen = $state(false);
|
||
|
||
let dedupeOpen = $state(false);
|
||
const dedupeMutation = createMutation({
|
||
mutationFn: () => workflows.dedupe(),
|
||
onSuccess: (res) => {
|
||
invalidateWorkflows();
|
||
toast.success(
|
||
res.removed > 0
|
||
? t("wl.dedupeDone", { n: res.removed })
|
||
: t("wl.dedupeNone"),
|
||
);
|
||
},
|
||
onError: (err) => toast.error(apiErrorMessage(err)),
|
||
onSettled: () => (dedupeOpen = false),
|
||
});
|
||
|
||
const anyOverlay = $derived(
|
||
batchOpen || singleOpen || testOpen || metaDialogOpen,
|
||
);
|
||
|
||
function onWindowKeydown(e: KeyboardEvent) {
|
||
if (e.key !== "/" || e.ctrlKey || e.metaKey || e.altKey || e.isComposing)
|
||
return;
|
||
const el = e.target as HTMLElement | null;
|
||
if (
|
||
el &&
|
||
(el.tagName === "INPUT" ||
|
||
el.tagName === "TEXTAREA" ||
|
||
el.tagName === "SELECT" ||
|
||
el.isContentEditable)
|
||
) {
|
||
return;
|
||
}
|
||
if (
|
||
anyOverlay ||
|
||
e.defaultPrevented ||
|
||
document.querySelector('[aria-modal="true"]')
|
||
)
|
||
return;
|
||
e.preventDefault();
|
||
searchInput?.focus();
|
||
}
|
||
|
||
function mediaTitle(kind: string): string {
|
||
if (kind === "video") return t("wl.mediaVideo");
|
||
if (kind === "audio") return t("wl.mediaAudio");
|
||
return t("wl.mediaImage");
|
||
}
|
||
|
||
let activeId = $state<number | null>(null);
|
||
const activeRow = $derived(rows.find((r) => r.id === activeId) ?? null);
|
||
$effect(() => {
|
||
if (
|
||
rows.length &&
|
||
(activeId == null || !rows.some((r) => r.id === activeId))
|
||
) {
|
||
activeId = rows[0].id;
|
||
void ensureFull(rows[0].id);
|
||
}
|
||
});
|
||
function pickRow(row: WorkflowMeta) {
|
||
activeId = row.id;
|
||
void ensureFull(row.id);
|
||
}
|
||
async function setEnabled(row: WorkflowMeta, enabled: boolean) {
|
||
try {
|
||
const updated = await workflows.update(row.id, { is_enabled: enabled });
|
||
fullCache[row.id] = updated;
|
||
await invalidateWorkflows();
|
||
} catch (err) {
|
||
toast.error(apiErrorMessage(err));
|
||
}
|
||
}
|
||
const favCount = $derived(rows.filter((r) => r.favorite).length);
|
||
</script>
|
||
|
||
<svelte:window onkeydown={onWindowKeydown} />
|
||
<ManagementPage active="workflows" title="工作流库">
|
||
{#snippet tools()}<span class="mg-dim"
|
||
>已导入 {rows.length} 个 · 已配置可运行 {rows.filter(isWorkflowRunnable)
|
||
.length} 个</span
|
||
>{/snippet}
|
||
{#snippet actions()}<Button size="sm" onclick={() => (singleOpen = true)}
|
||
><Icon name="upload-manage" size={14} />导入工作流</Button
|
||
>{/snippet}
|
||
<div class="mg-layout">
|
||
<aside class="mg-side" aria-label="工作流筛选">
|
||
<div class="mg-label">用途</div>
|
||
<button
|
||
class="mg-filter"
|
||
class:on={kindFilter === "all" && catFilter === "all"}
|
||
onclick={() => {
|
||
kindFilter = "all";
|
||
catFilter = "all";
|
||
}}>全部<small>{rows.length}</small></button
|
||
>
|
||
{#each [["image", "图片生成"], ["video", "视频生成"]] as [value, label]}
|
||
<button
|
||
class="mg-filter"
|
||
class:on={kindFilter === value}
|
||
onclick={() => {
|
||
kindFilter = value as "image" | "video";
|
||
catFilter = "all";
|
||
}}
|
||
>{label}<small>{rows.filter((r) => r.kind === value).length}</small
|
||
></button
|
||
>
|
||
{/each}
|
||
{#each categories.filter((c) => c !== "image" && c !== "video") as c}<button
|
||
class="mg-filter"
|
||
class:on={catFilter === c}
|
||
onclick={() => {
|
||
catFilter = c;
|
||
kindFilter = "all";
|
||
}}
|
||
>{categoryLabel(c)}<small
|
||
>{rows.filter((r) => r.category === c).length}</small
|
||
></button
|
||
>{/each}
|
||
<div class="mg-label">模型族</div>
|
||
<button
|
||
class="mg-filter"
|
||
class:on={familyFilter === "all"}
|
||
onclick={() => (familyFilter = "all")}
|
||
>全部模型<small>{rows.length}</small></button
|
||
>
|
||
{#each families as f}<button
|
||
class="mg-filter"
|
||
class:on={familyFilter === f}
|
||
onclick={() => (familyFilter = f)}
|
||
>{f}<small>{rows.filter((r) => r.model_family === f).length}</small
|
||
></button
|
||
>{/each}
|
||
<div class="mg-label">状态</div>
|
||
<button
|
||
class="mg-filter"
|
||
class:on={statusFilter === "enabled"}
|
||
onclick={() => (statusFilter = "enabled")}
|
||
>可运行<small>{rows.filter(isWorkflowRunnable).length}</small></button
|
||
>
|
||
<button
|
||
class="mg-filter"
|
||
class:on={statusFilter === "pending"}
|
||
onclick={() => (statusFilter = "pending")}
|
||
>待配置<small>{rows.filter((r) => r.needs_mapping).length}</small
|
||
></button
|
||
>
|
||
<button
|
||
class="mg-filter"
|
||
class:on={statusFilter === "disabled"}
|
||
onclick={() => (statusFilter = "disabled")}
|
||
>已停用<small>{rows.filter((r) => !r.is_enabled).length}</small></button
|
||
>
|
||
<button
|
||
class="mg-filter"
|
||
class:on={favOnly}
|
||
onclick={() => (favOnly = !favOnly)}
|
||
>收藏<small>{favCount}</small></button
|
||
>
|
||
<div class="mg-label">目录与导入</div>
|
||
<button class="mg-filter" onclick={() => (batchOpen = true)}
|
||
><Icon name="folder" size={13} />扫描 / 转换 / 批量导入</button
|
||
>
|
||
<button class="mg-filter" onclick={() => (dedupeOpen = true)}
|
||
>检查重复工作流</button
|
||
>
|
||
<select class="a-select" aria-label="素材类型" bind:value={mediaFilter}
|
||
><option value="all">全部输入输出媒介</option><option value="image"
|
||
>图片</option
|
||
><option value="video">视频</option><option value="audio">音频</option
|
||
></select
|
||
>
|
||
<select class="a-select" aria-label="排序" bind:value={sortBy}
|
||
><option value="recent">最近使用</option><option value="name"
|
||
>名称排序</option
|
||
></select
|
||
>
|
||
{#if hasActiveFilter}<Button
|
||
variant="ghost"
|
||
size="sm"
|
||
onclick={resetFilters}>清除筛选</Button
|
||
>{/if}
|
||
</aside>
|
||
<div class="mg-main">
|
||
<div class="mg-tools">
|
||
<label class="mg-search workflow-search"
|
||
><Icon name="search-manage" size={14} /><input
|
||
bind:this={searchInput}
|
||
bind:value={search}
|
||
placeholder="搜索名称、文件名、模型…"
|
||
aria-label="搜索名称、文件名、模型…"
|
||
/></label
|
||
>
|
||
<div class="mg-seg workflow-state" aria-label="工作流状态">
|
||
{#each [["all", "全部"], ["enabled", "可运行"], ["pending", "待配置"], ["problem", "有问题"]] as [value, label]}<button
|
||
class:on={statusFilter === value}
|
||
aria-pressed={statusFilter === value}
|
||
onclick={() => (statusFilter = value as typeof statusFilter)}
|
||
>{label}</button
|
||
>{/each}
|
||
</div>
|
||
<span class="mg-grow"></span><Button
|
||
variant="ghost"
|
||
size="sm"
|
||
onclick={() => {
|
||
if (selected.size)
|
||
openMetaDialog(rows.filter((r) => selected.has(r.id)));
|
||
else bulkMode = !bulkMode;
|
||
}}>批量标注</Button
|
||
>
|
||
</div>
|
||
{#if bulkMode}<div class="mg-tools">
|
||
<span class="mg-dim">已选 {selected.size} 项</span><Button
|
||
size="sm"
|
||
disabled={batchPending || !selected.size}
|
||
onclick={() => batchSetEnabled(true)}>启用</Button
|
||
><Button
|
||
size="sm"
|
||
disabled={batchPending || !selected.size}
|
||
onclick={() => batchSetEnabled(false)}>停用</Button
|
||
><Button
|
||
size="sm"
|
||
variant="ghost"
|
||
onclick={() => {
|
||
selected = new Set();
|
||
bulkMode = false;
|
||
}}>取消选择</Button
|
||
>
|
||
</div>{/if}
|
||
<div class="mg-table">
|
||
<table>
|
||
<colgroup
|
||
><col style="width:36%" /><col style="width:9%" /><col
|
||
style="width:13%"
|
||
/><col style="width:20%" /><col style="width:12%" /><col
|
||
style="width:10%"
|
||
/></colgroup
|
||
><thead
|
||
><tr
|
||
><th
|
||
>{#if bulkMode}<input
|
||
aria-label="全选当前列表"
|
||
type="checkbox"
|
||
checked={allVisibleSelected}
|
||
onchange={toggleSelectVisible}
|
||
/>{/if} 名称</th
|
||
><th>用途</th><th>模型族</th><th>需要的素材</th><th>状态</th><th
|
||
></th></tr
|
||
></thead
|
||
><tbody>
|
||
{#each visible as row (row.id)}{@const state = workflowState(row)}
|
||
<tr class:on={activeId === row.id}>
|
||
<td
|
||
><div class="mg-row">
|
||
{#if bulkMode}<input
|
||
type="checkbox"
|
||
aria-label={"选择 " + row.name}
|
||
checked={selected.has(row.id)}
|
||
onchange={(e) =>
|
||
toggleRow(row.id, e.currentTarget.checked)}
|
||
/>{/if}<button
|
||
class="mg-click"
|
||
onclick={() => pickRow(row)}
|
||
title={row.name}
|
||
><FamilyBadge
|
||
model={row.model_family}
|
||
muted={!isWorkflowRunnable(row)}
|
||
/><span class="mg-name"
|
||
><b>{row.name}</b><small
|
||
>{row.source_path
|
||
?.replaceAll("\\", "/")
|
||
.split("/")
|
||
.pop() ?? "来源未知"}</small
|
||
></span
|
||
></button
|
||
>
|
||
</div></td
|
||
>
|
||
<td title={usageLabel(row.usage)}>{usageLabel(row.usage)}</td
|
||
><td class="mg-muted" title={row.model_family ?? ""}
|
||
>{row.model_family ?? "—"}</td
|
||
><td title={row.media_io.in.map(mediaTitle).join("、")}
|
||
>{row.media_io.in.length
|
||
? row.media_io.in.map(mediaTitle).join(" · ")
|
||
: "仅提示词"}</td
|
||
><td><span class="mg-tag {state.tone}">{state.label}</span></td>
|
||
<td
|
||
><button
|
||
class="mg-pin"
|
||
aria-label={"在创作台置顶 " + row.name}
|
||
aria-pressed={pins.includes(row.id)}
|
||
onclick={() => pin(row.id)}
|
||
><Icon name="pin" size={14} /></button
|
||
></td
|
||
>
|
||
</tr>
|
||
{/each}
|
||
</tbody>
|
||
</table>
|
||
{#if loading}<p class="mg-empty">正在读取工作流…</p>{:else if error}<p
|
||
class="mg-empty"
|
||
role="alert"
|
||
>
|
||
工作流加载失败<Button size="sm" onclick={onRetry}>重新加载</Button>
|
||
</p>{:else if !visible.length}<p class="mg-empty">
|
||
{rows.length
|
||
? "没有匹配的工作流"
|
||
: "尚未导入工作流;扫描目录不会自动启用。"}
|
||
</p>{/if}
|
||
{#if filtered.length > visibleCount}<Button
|
||
variant="ghost"
|
||
onclick={() => (visibleCount += 60)}
|
||
>再显示 {Math.min(60, filtered.length - visibleCount)} 项</Button
|
||
>{/if}
|
||
</div>
|
||
</div>
|
||
<aside class="mg-detail" aria-label="工作流详情">
|
||
{#if activeRow}{@const row = activeRow}{@const full =
|
||
fullCache[row.id]}{@const state = workflowState(row)}
|
||
<div class="mg-row">
|
||
<FamilyBadge model={row.model_family} large />
|
||
<h2>{row.name}</h2>
|
||
</div>
|
||
<div
|
||
class="mg-mono workflow-file"
|
||
title={row.source_path ?? "来源未知"}
|
||
>
|
||
{row.source_path?.replaceAll("\\", "/").split("/").pop() ??
|
||
"来源未知"}
|
||
</div>
|
||
<div class="mg-row">
|
||
<span class="mg-tag {state.tone}">{state.label}</span><span
|
||
class="mg-tag"
|
||
>{endpoints.find((e) => e.id === row.endpoint_id)?.name ??
|
||
"跟随默认连接"}</span
|
||
>
|
||
</div>
|
||
<div class="mg-field">
|
||
<h3>显示名称与用途</h3>
|
||
<div class="mg-search">
|
||
<input
|
||
readonly
|
||
aria-label="显示名称(打开编辑)"
|
||
aria-haspopup="dialog"
|
||
value={row.name}
|
||
onclick={() => openMetaDialog([row])}
|
||
onkeydown={(e) => {
|
||
if (e.key === "Enter") {
|
||
e.preventDefault();
|
||
openMetaDialog([row]);
|
||
}
|
||
}}
|
||
/>
|
||
</div>
|
||
<div class="mg-options">
|
||
<select
|
||
class="mg-option on purpose-select"
|
||
aria-label="用途"
|
||
value={row.usage ?? ""}
|
||
onchange={(e) => {
|
||
if (e.currentTarget.value)
|
||
$wfConfirmMutation.mutate({
|
||
id: row.id,
|
||
usage: e.currentTarget.value as WorkflowUsage,
|
||
});
|
||
}}
|
||
><option value="">待确认</option
|
||
>{#each WORKFLOW_USAGES as u}<option value={u}
|
||
>{usageLabel(u)}</option
|
||
>{/each}</select
|
||
><Button
|
||
size="sm"
|
||
variant="ghost"
|
||
onclick={() => openMetaDialog([row])}>+ 标签</Button
|
||
><Button
|
||
size="sm"
|
||
variant="ghost"
|
||
loading={$favMutation.isPending}
|
||
onclick={() =>
|
||
$favMutation.mutate({ id: row.id, fav: !row.favorite })}
|
||
>{row.favorite ? "★ 已收藏" : "☆ 收藏"}</Button
|
||
>
|
||
</div>
|
||
</div>
|
||
<div class="mg-field">
|
||
<h3>输入映射 <span class="mg-dim">· 节点 → 创作台控件</span></h3>
|
||
{#if fullPending[row.id]}<p class="mg-dim">
|
||
正在读取输入…
|
||
</p>{:else if full}{#each full.input_schema as input}<div
|
||
class="mg-map"
|
||
>
|
||
<span class="a"
|
||
>{inputNodeLabel(full, input.nodeId, input.label)}</span
|
||
><Icon name="arrow-right" size={14} /><span class="b"
|
||
>{input.label}{input.required ? "(必填)" : ""}</span
|
||
>
|
||
</div>{/each}{:else}<Button
|
||
size="sm"
|
||
onclick={() => ensureFull(row.id)}>重新读取</Button
|
||
>{/if}
|
||
</div>
|
||
<div class="mg-field">
|
||
<h3>连接与运行</h3>
|
||
<select
|
||
class="a-select"
|
||
aria-label="绑定连接"
|
||
value={row.endpoint_id ?? ""}
|
||
onchange={(e) =>
|
||
$wfBindMutation.mutate({
|
||
id: row.id,
|
||
endpointId: e.currentTarget.value || null,
|
||
})}
|
||
><option value="">跟随默认连接</option
|
||
>{#each endpoints as ep}<option value={ep.id}>{ep.name}</option
|
||
>{/each}</select
|
||
><Switch
|
||
checked={row.is_enabled}
|
||
label="启用工作流"
|
||
disabled={row.needs_mapping || !row.usage}
|
||
onchange={(value) => setEnabled(row, value)}
|
||
/><Button
|
||
size="sm"
|
||
loading={wfRecheckId === row.id}
|
||
onclick={() => recheckRow(row)}>检查节点与模型</Button
|
||
>{#if recheckLine(row)}<p class="mg-dim">
|
||
{recheckLine(row)?.text}
|
||
</p>{/if}
|
||
<p class="mg-dim">最近使用:{formatDbTime(row.last_used_at)}</p>
|
||
<p class="mg-dim">{lastTestLine(full) ?? "打开试运行查看最近记录"}</p>
|
||
</div>
|
||
<div class="mg-row">
|
||
{#if isWorkflowRunnable(row)}<a
|
||
class="a-btn secondary sm"
|
||
href={"/" + row.kind + "?wf=" + row.id}>在创作台打开</a
|
||
>{/if}<Button
|
||
size="sm"
|
||
loading={openingTestId === row.id}
|
||
onclick={(e) => {
|
||
testTriggerEl = e.currentTarget as HTMLElement;
|
||
void openTest(row);
|
||
}}>试运行</Button
|
||
><Button size="sm" onclick={() => toggleWfMapEdit(row)}
|
||
>输出映射</Button
|
||
><Button
|
||
variant="danger"
|
||
size="sm"
|
||
onclick={() => $wfDeleteMutation.mutate(row.id)}>删除</Button
|
||
>
|
||
</div>
|
||
{#if wfMapEditId === row.id && full}
|
||
<div class="wf-map-editor">
|
||
<p class="wf-map-hint">{t("conn.wfOutputHint")}</p>
|
||
<div class="wf-slot-rows">
|
||
{#each (full.output_schema ?? []).filter((o) => o.evidence === "unknown_node") as c (c.nodeId)}
|
||
<div class="wf-slot-row" class:off={!wfMapPicked[c.nodeId]}>
|
||
<label class="wf-out-check">
|
||
<input
|
||
type="checkbox"
|
||
checked={wfMapPicked[c.nodeId] ?? false}
|
||
onchange={(e) =>
|
||
(wfMapPicked = {
|
||
...wfMapPicked,
|
||
[c.nodeId]: e.currentTarget.checked,
|
||
})}
|
||
/>
|
||
<span class="wf-slot-id mono">{c.nodeId}</span>
|
||
<span class="wf-slot-name" title={c.class_type}
|
||
>{c.label}</span
|
||
>
|
||
</label>
|
||
<select
|
||
class="a-select"
|
||
aria-label={t("conn.wfOutputKindAria")}
|
||
disabled={!wfMapPicked[c.nodeId]}
|
||
value={wfMapKind[c.nodeId] ?? c.suggested_kind ?? "image"}
|
||
onchange={(e) =>
|
||
(wfMapKind = {
|
||
...wfMapKind,
|
||
[c.nodeId]: e.currentTarget.value as "image" | "video",
|
||
})}
|
||
>
|
||
<option value="image">{t("conn.wfKindImage")}</option>
|
||
<option value="video">{t("conn.wfKindVideo")}</option>
|
||
</select>
|
||
</div>
|
||
{/each}
|
||
</div>
|
||
<div class="form-actions">
|
||
<Button
|
||
variant="secondary"
|
||
size="sm"
|
||
onclick={() => (wfMapEditId = null)}
|
||
>
|
||
{t("conn.wfMapCancel")}
|
||
</Button>
|
||
<Button
|
||
variant="primary"
|
||
size="sm"
|
||
disabled={!Object.values(wfMapPicked).some(Boolean)}
|
||
loading={$wfMapMutation.isPending}
|
||
onclick={() => submitWfMap(row)}
|
||
>
|
||
{t("conn.wfOutputApply")}
|
||
</Button>
|
||
</div>
|
||
</div>
|
||
{/if}
|
||
{:else}<div class="mg-empty">选择工作流查看输入映射与连接状态</div>{/if}
|
||
</aside>
|
||
</div>
|
||
</ManagementPage>
|
||
<WorkflowBatchImportWizard
|
||
bind:open={batchOpen}
|
||
{endpoints}
|
||
onimported={() => invalidateWorkflows()}
|
||
/>
|
||
<WorkflowImportDrawer
|
||
bind:open={singleOpen}
|
||
{endpoints}
|
||
onchanged={() => invalidateWorkflows()}
|
||
/>
|
||
<WorkflowTestDialog
|
||
bind:open={testOpen}
|
||
workflow={testWf}
|
||
{endpoints}
|
||
onchanged={() => {
|
||
invalidateWorkflows();
|
||
if (testWf) {
|
||
delete fullCache[testWf.id];
|
||
void ensureFull(testWf.id);
|
||
}
|
||
}}
|
||
/>
|
||
<WorkflowMetaDialog
|
||
bind:open={metaDialogOpen}
|
||
targets={metaTargets}
|
||
{categories}
|
||
{families}
|
||
/>
|
||
|
||
<ConfirmDialog
|
||
bind:open={dedupeOpen}
|
||
title={t("wl.dedupeTitle")}
|
||
message={t("wl.dedupeConfirm")}
|
||
confirmLabel={t("wl.dedupe")}
|
||
danger
|
||
onconfirm={() => $dedupeMutation.mutate()}
|
||
/>
|
||
|
||
<style>
|
||
.workflow-search {
|
||
width: 320px;
|
||
flex: 0 1 320px;
|
||
}
|
||
.workflow-state {
|
||
width: 260px;
|
||
}
|
||
.purpose-select {
|
||
appearance: none;
|
||
text-align: center;
|
||
width: auto;
|
||
}
|
||
.workflow-file {
|
||
white-space: nowrap;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
}
|
||
</style>
|