550 lines
14 KiB
Svelte
550 lines
14 KiB
Svelte
<script lang="ts">
|
|
import { tick } from 'svelte';
|
|
import { createQuery, useQueryClient } from '@tanstack/svelte-query';
|
|
import { workflows, settings, apiErrorMessage } from '$lib/api';
|
|
import type { WorkflowMeta } from '$lib/comfy/types';
|
|
import { usageLabel } from '$lib/components/generate/schema';
|
|
import InputRequirementBadge from './InputRequirementBadge.svelte';
|
|
import { toast } from '$lib/toast';
|
|
interface Props {
|
|
open: boolean;
|
|
metas: WorkflowMeta[];
|
|
pendingCount: number;
|
|
selectedId: number | null;
|
|
activeId: number | null;
|
|
endpoints: Array<{ id: string; name: string }>;
|
|
onPick: (id: number) => void;
|
|
onClose: () => void;
|
|
catalogue?: boolean;
|
|
localOnly?: boolean;
|
|
onPreview?: (id: number) => void;
|
|
}
|
|
let {
|
|
open,
|
|
metas,
|
|
pendingCount,
|
|
selectedId,
|
|
activeId = $bindable(null),
|
|
endpoints,
|
|
onPick,
|
|
onClose,
|
|
catalogue = false,
|
|
localOnly = false,
|
|
onPreview,
|
|
}: Props = $props();
|
|
const client = useQueryClient();
|
|
const modelsQuery = createQuery({
|
|
queryKey: ['settings'],
|
|
queryFn: settings.get,
|
|
});
|
|
let search = $state(''),
|
|
category = $state('all'),
|
|
family = $state('all'),
|
|
source = $state('local'),
|
|
usage = $state('all'),
|
|
favOnly = $state(false),
|
|
recentOnly = $state(false),
|
|
visibleCount = $state(48);
|
|
let searchEl: HTMLInputElement;
|
|
let listEl: HTMLDivElement;
|
|
const labels: Record<string, string> = {
|
|
basic: '基础工具',
|
|
audio_tts: '语音',
|
|
music: '音乐',
|
|
image: '图片',
|
|
video: '视频',
|
|
digital_human: '数字人',
|
|
motion_transfer: '动作',
|
|
};
|
|
const categories = $derived([
|
|
...new Set(metas.map((m) => m.category).filter((k): k is string => !!k)),
|
|
]);
|
|
const families = $derived(
|
|
[...new Set(metas.map((m) => m.model_family).filter((k): k is string => !!k))].sort(),
|
|
);
|
|
const filtered = $derived(
|
|
metas
|
|
.filter(
|
|
(m) =>
|
|
(category === 'all' || m.category === category) &&
|
|
(usage === 'all' || m.usage === usage) &&
|
|
(family === 'all' || m.model_family === family) &&
|
|
(!favOnly || m.favorite) &&
|
|
(!recentOnly || m.last_used_at) &&
|
|
(!search.trim() ||
|
|
[
|
|
m.name,
|
|
m.description,
|
|
...(m.tags ?? []),
|
|
m.model_family,
|
|
m.usage ? usageLabel(m.usage) : '',
|
|
]
|
|
.join(' ')
|
|
.toLowerCase()
|
|
.includes(search.toLowerCase().trim())),
|
|
)
|
|
.sort((a, b) =>
|
|
recentOnly
|
|
? (b.last_used_at ?? '').localeCompare(a.last_used_at ?? '')
|
|
: Number(b.favorite) - Number(a.favorite) || a.name.localeCompare(b.name, 'zh'),
|
|
),
|
|
);
|
|
const visible = $derived(filtered.slice(0, visibleCount));
|
|
const cloudModels = $derived(
|
|
($modelsQuery.data?.llm_profiles ?? []).filter(
|
|
(p) => !search || [p.name, p.model].join(' ').toLowerCase().includes(search.toLowerCase()),
|
|
),
|
|
);
|
|
$effect(() => {
|
|
void search;
|
|
void category;
|
|
void family;
|
|
void usage;
|
|
void favOnly;
|
|
void recentOnly;
|
|
visibleCount = 48;
|
|
listEl?.scrollTo({ top: 0 });
|
|
});
|
|
$effect(() => {
|
|
if (!open) return;
|
|
const idx = filtered.findIndex((m) => m.id === selectedId);
|
|
if (idx >= visibleCount) visibleCount = idx + 48;
|
|
void tick().then(() => searchEl?.focus());
|
|
});
|
|
$effect(() => {
|
|
if (!filtered.some((m) => m.id === activeId)) activeId = filtered[0]?.id ?? null;
|
|
});
|
|
export function focusSearch() {
|
|
searchEl?.focus();
|
|
}
|
|
async function favorite(m: WorkflowMeta) {
|
|
try {
|
|
await workflows.update(m.id, { favorite: !m.favorite });
|
|
await client.invalidateQueries({ queryKey: ['workflows-meta'] });
|
|
} catch (e) {
|
|
toast.error(apiErrorMessage(e));
|
|
}
|
|
}
|
|
function pick(m: WorkflowMeta) {
|
|
if (onPreview) onPreview(m.id);
|
|
else if (m.is_enabled && !m.needs_mapping && m.usage) onPick(m.id);
|
|
}
|
|
function move(delta: number) {
|
|
const i = Math.max(
|
|
0,
|
|
Math.min(filtered.length - 1, filtered.findIndex((m) => m.id === activeId) + delta),
|
|
);
|
|
activeId = filtered[i]?.id ?? null;
|
|
if (i >= visibleCount) visibleCount = i + 24;
|
|
void tick().then(() =>
|
|
listEl?.querySelector(`[data-wf-row="${activeId}"]`)?.scrollIntoView({ block: 'nearest' }),
|
|
);
|
|
}
|
|
function key(e: KeyboardEvent) {
|
|
if (e.defaultPrevented) return;
|
|
if (e.key === 'Escape' && !catalogue) {
|
|
e.preventDefault();
|
|
onClose();
|
|
} else if (source !== 'local') {
|
|
return;
|
|
} else if (e.key === 'ArrowDown') {
|
|
e.preventDefault();
|
|
move(1);
|
|
} else if (e.key === 'ArrowUp') {
|
|
e.preventDefault();
|
|
move(-1);
|
|
} else if (e.key === 'Enter' && e.target === searchEl) {
|
|
e.preventDefault();
|
|
const m = filtered.find((m) => m.id === activeId);
|
|
if (m) pick(m);
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
|
<div class="catalogue" inert={!open} onkeydown={key}>
|
|
<aside class="categories">
|
|
<p>按创作任务寻找</p>
|
|
<button
|
|
class:active={category === 'all' && !favOnly && !recentOnly}
|
|
onclick={() => {
|
|
category = 'all';
|
|
favOnly = false;
|
|
recentOnly = false;
|
|
}}><span>全部工具</span><small>{metas.length}</small></button
|
|
>
|
|
{#each categories as cat}<button
|
|
class:active={category === cat}
|
|
onclick={() => {
|
|
category = cat;
|
|
favOnly = false;
|
|
recentOnly = false;
|
|
}}
|
|
><span>{labels[cat] ?? cat}</span><small
|
|
>{metas.filter((m) => m.category === cat).length}</small
|
|
></button
|
|
>{/each}
|
|
<div class="divider"></div>
|
|
<button
|
|
class:active={favOnly}
|
|
onclick={() => {
|
|
favOnly = !favOnly;
|
|
recentOnly = false;
|
|
}}><span>☆ 我的收藏</span><small>{metas.filter((m) => m.favorite).length}</small></button
|
|
><button
|
|
class:active={recentOnly}
|
|
onclick={() => {
|
|
recentOnly = !recentOnly;
|
|
favOnly = false;
|
|
}}><span>◷ 最近使用</span></button
|
|
>
|
|
</aside>
|
|
<section class="catalogue-main">
|
|
<label class="search"
|
|
><span>⌕</span><input
|
|
type="search"
|
|
bind:this={searchEl}
|
|
bind:value={search}
|
|
placeholder="搜索工作流、模型或用途…"
|
|
aria-label="搜索工作流"
|
|
/></label
|
|
>
|
|
<div class="filters">
|
|
<button class:active={source === 'local'} onclick={() => (source = 'local')}
|
|
>本地 ComfyUI</button
|
|
>{#if !localOnly}<button class:active={source === 'cloud'} onclick={() => (source = 'cloud')}
|
|
>云端与智能体</button
|
|
>{/if}{#if source === 'local'}<select aria-label="筛选创作任务" bind:value={usage}
|
|
><option value="all">所有创作任务</option>{#each [...new Set(metas
|
|
.map((m) => m.usage)
|
|
.filter(Boolean))] as u}<option value={u!}>{usageLabel(u!)}</option>{/each}</select
|
|
><select aria-label="筛选模型家族" bind:value={family}
|
|
><option value="all">全部模型</option>{#each families as f}<option value={f}>{f}</option
|
|
>{/each}</select
|
|
>{/if}<span>{source === 'local' ? filtered.length : cloudModels.length} 个结果</span>
|
|
</div>
|
|
<div class="results" bind:this={listEl}>
|
|
{#if source === 'cloud'}
|
|
<div class="cloud-context">
|
|
<h3>让专业搭档加入创作。</h3>
|
|
<p>使用已配置模型,在智能体或画布中创作。自定义多媒体接口与技能可在设置中管理。</p>
|
|
<a href="/canvas" class="a-btn primary">打开创作画布 ↗</a>
|
|
</div>
|
|
{#each cloudModels as profile}<article class="workflow-card">
|
|
<div class="family">{profile.model}<span>已配置模型</span></div>
|
|
<h3>{profile.name}</h3>
|
|
<a href="/agents" class="tool-link">交给智能体创作 ↗</a>
|
|
</article>{:else}<p class="empty">
|
|
还没有匹配的模型。<a href="/models">配置模型 ↗</a>
|
|
</p>{/each}
|
|
{:else}
|
|
{#each visible as m (m.id)}<article
|
|
class="workflow-card"
|
|
class:selected={m.id === selectedId}
|
|
class:roving={m.id === activeId}
|
|
data-wf-row={m.id}
|
|
>
|
|
<button
|
|
class="workflow-choice"
|
|
onclick={() => pick(m)}
|
|
onfocus={() => (activeId = m.id)}
|
|
disabled={!onPreview && (!m.is_enabled || m.needs_mapping || !m.usage)}
|
|
aria-label="{onPreview ? '查看' : '使用'} {m.name}"
|
|
aria-current={m.id === selectedId ? 'true' : undefined}
|
|
>
|
|
<div class="family">
|
|
{m.model_family ?? 'COMFYUI'}<span>本地</span>
|
|
</div>
|
|
<h3 title={m.name}>{m.name}</h3>
|
|
{#if m.description}<p class="description">{m.description}</p>{/if}
|
|
<div class="card-meta">
|
|
<span>{m.usage ? usageLabel(m.usage) : '待映射'}</span><InputRequirementBadge
|
|
io={m.media_io}
|
|
compact
|
|
/>
|
|
</div>
|
|
</button>
|
|
<button
|
|
class="favorite"
|
|
class:saved={m.favorite}
|
|
aria-label="{m.favorite ? '取消收藏' : '收藏'} {m.name}"
|
|
aria-pressed={m.favorite}
|
|
onclick={() => favorite(m)}>{m.favorite ? '★' : '☆'}</button
|
|
>
|
|
{#if m.needs_mapping || !m.is_enabled || !m.usage}<a
|
|
href="/workflows?manage=1"
|
|
class="tool-link">{m.needs_mapping ? '完成映射' : '管理工作流'} ↗</a
|
|
>{/if}
|
|
</article>{:else}<p class="empty">没有找到匹配的工作流,试试更短的关键词。</p>{/each}
|
|
{#if visibleCount < filtered.length}<button
|
|
class="more a-btn secondary"
|
|
onclick={() => (visibleCount += 48)}
|
|
>再看 {Math.min(48, filtered.length - visibleCount)} 个工具</button
|
|
>{/if}
|
|
{/if}
|
|
</div>
|
|
{#if pendingCount > 0}<p class="pending">
|
|
另有 {pendingCount} 个工作流待映射。<a href="/workflows?manage=1">去整理 ↗</a>
|
|
</p>{/if}
|
|
</section>
|
|
</div>
|
|
|
|
<style>
|
|
.description {
|
|
font-size: 11px;
|
|
color: var(--text-3);
|
|
line-height: 1.5;
|
|
margin: 0 0 10px;
|
|
display: -webkit-box;
|
|
-webkit-line-clamp: 2;
|
|
line-clamp: 2;
|
|
-webkit-box-orient: vertical;
|
|
overflow: hidden;
|
|
}
|
|
@media (max-width: 1100px) {
|
|
.catalogue {
|
|
grid-template-columns: 112px minmax(0, 1fr);
|
|
}
|
|
.catalogue-main {
|
|
padding-left: 12px;
|
|
}
|
|
.results {
|
|
grid-template-columns: 1fr;
|
|
}
|
|
}
|
|
.catalogue {
|
|
display: grid;
|
|
grid-template-columns: 145px minmax(0, 1fr);
|
|
height: 100%;
|
|
min-height: 0;
|
|
}
|
|
.categories {
|
|
padding: 16px 15px 20px 0;
|
|
border-right: 1px solid var(--line);
|
|
overflow: auto;
|
|
}
|
|
.categories p {
|
|
font-size: 9px;
|
|
letter-spacing: 1px;
|
|
color: var(--text-3);
|
|
margin: 0 9px 15px;
|
|
}
|
|
.categories button {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
width: 100%;
|
|
border: 0;
|
|
background: none;
|
|
border-radius: 7px;
|
|
text-align: left;
|
|
font-size: 11px;
|
|
padding: 11px 10px;
|
|
color: var(--text-2);
|
|
cursor: pointer;
|
|
}
|
|
.categories button.active {
|
|
background: #e8eafe;
|
|
color: var(--accent);
|
|
}
|
|
.categories small {
|
|
font-size: 9px;
|
|
color: var(--text-3);
|
|
}
|
|
.divider {
|
|
border-top: 1px solid var(--line);
|
|
margin: 20px 8px 12px;
|
|
}
|
|
.catalogue-main {
|
|
padding: 16px 0 0 22px;
|
|
display: flex;
|
|
flex-direction: column;
|
|
min-height: 0;
|
|
min-width: 0;
|
|
}
|
|
.search {
|
|
display: flex;
|
|
gap: 9px;
|
|
align-items: center;
|
|
background: var(--bg);
|
|
border: 1px solid var(--line);
|
|
padding: 9px 13px;
|
|
border-radius: 8px;
|
|
}
|
|
.search input {
|
|
border: 0;
|
|
outline: none;
|
|
background: transparent;
|
|
width: 100%;
|
|
font-size: 12px;
|
|
color: var(--text);
|
|
}
|
|
.search:focus-within {
|
|
outline: 2px solid var(--accent);
|
|
outline-offset: 2px;
|
|
}
|
|
.filters {
|
|
display: flex;
|
|
gap: 7px;
|
|
align-items: center;
|
|
margin: 16px 0 21px;
|
|
flex-wrap: wrap;
|
|
}
|
|
.filters button,
|
|
.filters select {
|
|
font-size: 10px;
|
|
padding: 5px 10px;
|
|
border: 1px solid var(--line);
|
|
border-radius: 20px;
|
|
background: var(--surface);
|
|
color: var(--text-3);
|
|
cursor: pointer;
|
|
}
|
|
.filters button.active {
|
|
color: var(--surface);
|
|
background: var(--text);
|
|
border-color: var(--text);
|
|
}
|
|
.filters span {
|
|
font-size: 9px;
|
|
color: var(--text-3);
|
|
margin-left: auto;
|
|
}
|
|
.filters select {
|
|
max-width: 140px;
|
|
}
|
|
.results {
|
|
display: grid;
|
|
grid-template-columns: repeat(2, minmax(0, 1fr));
|
|
gap: 11px;
|
|
overflow: auto;
|
|
min-height: 0;
|
|
align-content: start;
|
|
padding: 2px 5px 15px 2px;
|
|
}
|
|
.workflow-card {
|
|
border: 1px solid var(--line);
|
|
border-radius: 11px;
|
|
padding: 14px;
|
|
position: relative;
|
|
background: var(--surface);
|
|
min-width: 0;
|
|
content-visibility: auto;
|
|
contain-intrinsic-size: auto 132px;
|
|
}
|
|
.workflow-card.selected {
|
|
border-color: var(--accent);
|
|
background: #f1f3ff;
|
|
}
|
|
.workflow-choice {
|
|
display: block;
|
|
text-align: left;
|
|
width: 100%;
|
|
padding: 0 0 9px;
|
|
border: 0;
|
|
background: none;
|
|
color: var(--text);
|
|
cursor: pointer;
|
|
}
|
|
.workflow-choice:disabled {
|
|
opacity: 0.65;
|
|
cursor: default;
|
|
}
|
|
.family {
|
|
font: 9px var(--mono);
|
|
letter-spacing: 0.5px;
|
|
display: flex;
|
|
justify-content: space-between;
|
|
margin-bottom: 12px;
|
|
color: var(--text-3);
|
|
}
|
|
.family span {
|
|
font: 8px var(--font);
|
|
padding: 2px 5px;
|
|
border: 1px solid var(--line);
|
|
border-radius: 4px;
|
|
}
|
|
h3 {
|
|
font-size: 11px;
|
|
line-height: 1.7;
|
|
margin: 0 0 9px;
|
|
font-weight: 550;
|
|
display: -webkit-box;
|
|
-webkit-line-clamp: 2;
|
|
line-clamp: 2;
|
|
-webkit-box-orient: vertical;
|
|
overflow: hidden;
|
|
word-break: break-word;
|
|
}
|
|
.card-meta {
|
|
display: flex;
|
|
gap: 5px;
|
|
flex-wrap: wrap;
|
|
font-size: 9px;
|
|
color: var(--text-3);
|
|
padding-right: 14px;
|
|
}
|
|
.favorite {
|
|
position: absolute;
|
|
bottom: 10px;
|
|
right: 10px;
|
|
font-size: 18px;
|
|
border: 0;
|
|
background: none;
|
|
color: #838a78;
|
|
cursor: pointer;
|
|
}
|
|
.favorite.saved {
|
|
color: var(--accent);
|
|
}
|
|
.tool-link {
|
|
font-size: 10px;
|
|
color: var(--accent);
|
|
text-decoration: none;
|
|
}
|
|
.cloud-context {
|
|
grid-column: 1/-1;
|
|
background: #e9e7df;
|
|
padding: 25px;
|
|
border-radius: 12px;
|
|
}
|
|
.cloud-context h3 {
|
|
font-size: 19px;
|
|
}
|
|
.cloud-context p {
|
|
font-size: 11px;
|
|
color: var(--text-2);
|
|
line-height: 1.8;
|
|
}
|
|
.cloud-context .a-btn {
|
|
font-size: 11px;
|
|
}
|
|
.pending {
|
|
font-size: 10px;
|
|
color: var(--text-3);
|
|
padding-top: 12px;
|
|
border-top: 1px solid var(--line);
|
|
margin: 0;
|
|
}
|
|
.empty {
|
|
grid-column: 1/-1;
|
|
padding: 45px;
|
|
text-align: center;
|
|
font-size: 12px;
|
|
color: var(--text-3);
|
|
}
|
|
.more {
|
|
grid-column: 1/-1;
|
|
justify-self: center;
|
|
font-size: 11px;
|
|
}
|
|
@media (hover: hover) {
|
|
.workflow-card:hover {
|
|
border-color: var(--accent-line);
|
|
}
|
|
.categories button:hover {
|
|
background: var(--bg-3);
|
|
}
|
|
}
|
|
</style>
|