From 1f32563c68575c4c77e6e3e6a0f7e6c7a9095c74 Mon Sep 17 00:00:00 2001 From: Benjamin Lu Date: Tue, 10 Nov 2026 19:16:06 -0800 Subject: [PATCH 1/9] feat(home): add a Home page with example workflows from the Comfy content catalog Desktop had no Home page: the sidebar started at Builds and the Comfy mark, labelled Home, also went to Builds. DESK-255 asks for workflows from the Comfy Content Catalog on a Home page. Home is a sidebar entry above Builds. It lists up to 12 example workflows read from the catalog (GET /api/v1/catalog/items?type=workflow, public, no sign-in). The catalog's desktop_starters surface decides which ones appear, the same switch that already picks Desktop's apps. No workflow carries that surface yet, so until one does Home falls back to the catalog's own order. A card opens the workflow's page on comfy.org/hub in the browser. Opening it inside a Cloud or local build is a follow-up: only two catalog workflows name a bundled template and the featured ones have no Cloud share link. Builds stays the landing page so the 46 E2E specs that start there are untouched. --- locales/en.json | 7 + locales/zh.json | 7 + src/main/catalog/workflows.test.ts | 210 ++++++++++++++++++++ src/main/catalog/workflows.ts | 173 ++++++++++++++++ src/main/host/panelView.ts | 1 + src/main/lib/ipc/index.ts | 2 + src/main/lib/ipc/registerCatalogHandlers.ts | 19 ++ src/preload/api.ts | 2 + src/renderer/panel.html | 2 +- src/renderer/src/panel/AppSidebar.test.ts | 6 +- src/renderer/src/panel/AppSidebar.vue | 5 +- src/renderer/src/panel/HomeView.test.ts | 98 +++++++++ src/renderer/src/panel/HomeView.vue | 144 ++++++++++++++ src/renderer/src/panel/PanelApp.test.ts | 39 ++++ src/renderer/src/panel/PanelApp.vue | 22 +- src/renderer/src/types/ipc.ts | 1 + src/types/ipc.ts | 11 +- 17 files changed, 737 insertions(+), 12 deletions(-) create mode 100644 src/main/catalog/workflows.test.ts create mode 100644 src/main/catalog/workflows.ts create mode 100644 src/main/lib/ipc/registerCatalogHandlers.ts create mode 100644 src/renderer/src/panel/HomeView.test.ts create mode 100644 src/renderer/src/panel/HomeView.vue diff --git a/locales/en.json b/locales/en.json index c910b8e41..5f19a7ca0 100644 --- a/locales/en.json +++ b/locales/en.json @@ -25,6 +25,13 @@ "signInTitle": "Log in to Comfy", "signInBody": "Sync your work and run it on Comfy Cloud." }, + "home": { + "title": "Let's create", + "exampleWorkflows": "Example workflows", + "openWorkflow": "Open {title} on Comfy Hub", + "workflowsUnavailable": "Example workflows couldn't be loaded. Check your connection and try again.", + "retry": "Try again" + }, "installType": { "standalone": "Standalone", "cloud": "Cloud", diff --git a/locales/zh.json b/locales/zh.json index f6246e123..46e8628cd 100644 --- a/locales/zh.json +++ b/locales/zh.json @@ -25,6 +25,13 @@ "signInTitle": "登录 Comfy", "signInBody": "同步你的作品,并在 Comfy Cloud 上运行。" }, + "home": { + "title": "开始创作", + "exampleWorkflows": "示例工作流", + "openWorkflow": "在 Comfy Hub 打开 {title}", + "workflowsUnavailable": "无法加载示例工作流。请检查网络连接后重试。", + "retry": "重试" + }, "installType": { "standalone": "独立版", "cloud": "云端", diff --git a/src/main/catalog/workflows.test.ts b/src/main/catalog/workflows.test.ts new file mode 100644 index 000000000..8d8336b1e --- /dev/null +++ b/src/main/catalog/workflows.test.ts @@ -0,0 +1,210 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' + +vi.mock('../lib/fetch', () => ({ fetchJSON: vi.fn() })) + +import { fetchJSON } from '../lib/fetch' +import { + HOME_WORKFLOW_LIMIT, + _resetCatalogWorkflowsForTest, + hubUrlForListedWorkflow, + listHomeWorkflows +} from './workflows' + +const mockedFetchJSON = vi.mocked(fetchJSON) + +function catalogItem(over: { + uid?: string + slug?: unknown + data?: Record +}): Record { + const uid = over.uid ?? 'upscale-uid' + return { + uid, + slug: 'slug' in over ? over.slug : `/hub/workflows/${uid}`, + data: { + title: 'Upscale and restore detail', + description: 'Enlarge an image and restore fine detail.', + website_media: { '$.static_thumb': `/catalog-media/${uid}/v1` }, + website_media_types: { '$.static_thumb': 'image/webp' }, + translations: { + 'zh-CN': { title: '放大图像并修复细节', description: '放大图像并修复精细细节。' } + }, + ...over.data + } + } +} + +function serveCatalog(pages: { desktop: unknown[]; everything: unknown[] }): void { + mockedFetchJSON.mockImplementation(async (url) => { + const surface = new URL(url).searchParams.get('surface') + return { items: surface === 'desktop_starters' ? pages.desktop : pages.everything } + }) +} + +beforeEach(() => { + vi.clearAllMocks() + _resetCatalogWorkflowsForTest() +}) + +describe('Home example workflows from the Comfy content catalog', () => { + it('shows the workflows the catalog marks for Desktop', async () => { + serveCatalog({ + desktop: [catalogItem({ uid: 'picked-for-desktop' })], + everything: [catalogItem({ uid: 'hub-only' })] + }) + + expect(await listHomeWorkflows('en')).toEqual([ + { + id: 'picked-for-desktop', + title: 'Upscale and restore detail', + description: 'Enlarge an image and restore fine detail.', + thumbnailUrl: 'https://comfy.org/catalog-media/picked-for-desktop/v1' + } + ]) + }) + + it("falls back to the catalog's own order while no workflow is marked for Desktop", async () => { + serveCatalog({ + desktop: [], + everything: [catalogItem({ uid: 'first' }), catalogItem({ uid: 'second' })] + }) + + const workflows = await listHomeWorkflows('en') + + expect(workflows.map((workflow) => workflow.id)).toEqual(['first', 'second']) + }) + + it('never shows more cards than the Home row holds', async () => { + serveCatalog({ + desktop: Array.from({ length: HOME_WORKFLOW_LIMIT + 5 }, (_, index) => + catalogItem({ uid: `workflow-${index}` }) + ), + everything: [] + }) + + expect(await listHomeWorkflows('en')).toHaveLength(HOME_WORKFLOW_LIMIT) + }) + + it('shows the title and description in the app language when the catalog has them', async () => { + serveCatalog({ desktop: [catalogItem({})], everything: [] }) + + const [workflow] = await listHomeWorkflows('zh') + + expect(workflow).toMatchObject({ + title: '放大图像并修复细节', + description: '放大图像并修复精细细节。' + }) + }) + + it('leaves out records that have no title or no Comfy Hub page to open', async () => { + serveCatalog({ + desktop: [ + catalogItem({ uid: 'untitled', data: { title: ' ' } }), + catalogItem({ uid: 'elsewhere', slug: 'https://example.com/hub/workflows/elsewhere' }), + catalogItem({ uid: 'not-a-workflow-page', slug: '/hub/apps/not-a-workflow-page' }), + catalogItem({ uid: 'missing-slug', slug: undefined }), + 'not-a-record', + catalogItem({ uid: 'good' }) + ], + everything: [] + }) + + const workflows = await listHomeWorkflows('en') + + expect(workflows.map((workflow) => workflow.id)).toEqual(['good']) + }) + + it('shows a card without a picture when the catalog only has a video or an off-site image', async () => { + serveCatalog({ + desktop: [ + catalogItem({ + uid: 'video-only', + data: { + website_media: { '$.thumbnail': '/catalog-media/video-only/v1' }, + website_media_types: { '$.thumbnail': 'video/mp4' } + } + }), + catalogItem({ + uid: 'off-site', + data: { + website_media: { '$.static_thumb': 'https://example.com/tracker.png' }, + website_media_types: { '$.static_thumb': 'image/png' } + } + }) + ], + everything: [] + }) + + const workflows = await listHomeWorkflows('en') + + expect(workflows.map((workflow) => workflow.thumbnailUrl)).toEqual([null, null]) + }) + + it('keeps showing the last list when the catalog stops answering', async () => { + vi.useFakeTimers() + try { + serveCatalog({ desktop: [catalogItem({ uid: 'loaded-earlier' })], everything: [] }) + await listHomeWorkflows('en') + + vi.advanceTimersByTime(60 * 60 * 1000) + mockedFetchJSON.mockRejectedValue(new Error('offline')) + + const workflows = await listHomeWorkflows('en') + + expect(workflows.map((workflow) => workflow.id)).toEqual(['loaded-earlier']) + } finally { + vi.useRealTimers() + } + }) + + it('reports a failure when the catalog has never answered', async () => { + mockedFetchJSON.mockRejectedValue(new Error('offline')) + + await expect(listHomeWorkflows('en')).rejects.toThrow('offline') + }) + + it('reports a failure when the catalog answers with something that is not a list', async () => { + mockedFetchJSON.mockResolvedValue({ code: 'invalid', message: 'invalid catalog query' }) + + await expect(listHomeWorkflows('en')).rejects.toThrow() + }) + + it('asks the catalog once while several pages wait for the same list', async () => { + serveCatalog({ desktop: [catalogItem({})], everything: [] }) + + await Promise.all([listHomeWorkflows('en'), listHomeWorkflows('en'), listHomeWorkflows('en')]) + await listHomeWorkflows('en') + + expect(mockedFetchJSON).toHaveBeenCalledTimes(1) + }) +}) + +describe('opening an example workflow', () => { + it('opens the Comfy Hub page of a workflow that Home is showing', async () => { + serveCatalog({ desktop: [catalogItem({ uid: 'shown' })], everything: [] }) + await listHomeWorkflows('en') + + expect(hubUrlForListedWorkflow('shown')).toBe('https://comfy.org/hub/workflows/shown') + }) + + it('drops query strings and fragments the catalog put on a Hub page address', async () => { + serveCatalog({ + desktop: [catalogItem({ uid: 'tagged', slug: '/hub/workflows/tagged?next=evil#frag' })], + everything: [] + }) + await listHomeWorkflows('en') + + expect(hubUrlForListedWorkflow('tagged')).toBe('https://comfy.org/hub/workflows/tagged') + }) + + it('opens nothing for an id Home is not showing', async () => { + serveCatalog({ desktop: [catalogItem({ uid: 'shown' })], everything: [] }) + await listHomeWorkflows('en') + + expect(hubUrlForListedWorkflow('https://example.com')).toBeNull() + }) + + it('opens nothing before Home has loaded the list', () => { + expect(hubUrlForListedWorkflow('shown')).toBeNull() + }) +}) diff --git a/src/main/catalog/workflows.ts b/src/main/catalog/workflows.ts new file mode 100644 index 000000000..f4f432305 --- /dev/null +++ b/src/main/catalog/workflows.ts @@ -0,0 +1,173 @@ +import { CLOUD_CONFIG } from '../cloud/config' +import { fetchJSON } from '../lib/fetch' +import type { CatalogWorkflow } from '../../types/ipc' + +export const HUB_ORIGIN = 'https://comfy.org' +export const HOME_WORKFLOW_LIMIT = 12 + +const HUB_WORKFLOW_PATH_PREFIX = '/hub/workflows/' +const CATALOG_MEDIA_PATH_PREFIX = '/catalog-media/' +const DESKTOP_SURFACE = 'desktop_starters' +const FETCH_TIMEOUT_MS = 8000 +const CACHE_TTL_MS = 10 * 60 * 1000 +const MAX_TEXT_LENGTH = 4096 +const THUMBNAIL_KEYS = ['$.static_thumb', '$.thumbnail', '$.hero'] as const +const IMAGE_TYPE_PREFIX = 'image/' + +interface ResolvedWorkflow { + card: CatalogWorkflow + hubUrl: string +} + +type JsonRecord = Record + +function asRecord(value: unknown): JsonRecord | null { + return value && typeof value === 'object' && !Array.isArray(value) ? (value as JsonRecord) : null +} + +function text(value: unknown): string | null { + if (typeof value !== 'string' || value.length > MAX_TEXT_LENGTH) return null + return value.trim() || null +} + +export function catalogWorkflowsUrl(surface: string | null): string { + const url = new URL('/api/v1/catalog/items', CLOUD_CONFIG.issuer) + url.searchParams.set('type', 'workflow') + if (surface) url.searchParams.set('surface', surface) + url.searchParams.set('limit', String(HOME_WORKFLOW_LIMIT)) + return url.toString() +} + +function translationFor(data: JsonRecord, locale: string): JsonRecord | null { + const translations = asRecord(data.translations) + if (!translations) return null + const exact = asRecord(translations[locale]) + if (exact) return exact + const language = locale.split('-')[0] + const sameLanguage = Object.keys(translations).find((key) => key.split('-')[0] === language) + return sameLanguage ? asRecord(translations[sameLanguage]) : null +} + +function hubUrlFor(slug: unknown): string | null { + if (typeof slug !== 'string') return null + let url: URL + try { + url = new URL(slug, HUB_ORIGIN) + } catch { + return null + } + if (url.origin !== HUB_ORIGIN) return null + if (!url.pathname.startsWith(HUB_WORKFLOW_PATH_PREFIX)) return null + if (url.pathname.length === HUB_WORKFLOW_PATH_PREFIX.length) return null + return `${HUB_ORIGIN}${url.pathname}` +} + +function thumbnailUrlFor(data: JsonRecord): string | null { + const media = asRecord(data.website_media) + const mediaTypes = asRecord(data.website_media_types) + if (!media) return null + for (const key of THUMBNAIL_KEYS) { + const mediaPath = media[key] + if (typeof mediaPath !== 'string' || !mediaPath.startsWith(CATALOG_MEDIA_PATH_PREFIX)) continue + const mediaType = mediaTypes?.[key] + if (typeof mediaType !== 'string' || !mediaType.startsWith(IMAGE_TYPE_PREFIX)) continue + const url = new URL(mediaPath, HUB_ORIGIN) + if (url.origin === HUB_ORIGIN) return url.toString() + } + return null +} + +export function toCatalogWorkflow(item: unknown, locale: string): ResolvedWorkflow | null { + const record = asRecord(item) + const data = asRecord(record?.data) + if (!record || !data) return null + const id = text(record.uid) + const hubUrl = hubUrlFor(record.slug) + const translation = locale.startsWith('en') ? null : translationFor(data, locale) + const title = text(translation?.title) ?? text(data.title) + if (!id || !hubUrl || !title) return null + return { + hubUrl, + card: { + id, + title, + description: text(translation?.description) ?? text(data.description) ?? '', + thumbnailUrl: thumbnailUrlFor(data) + } + } +} + +export function parseCatalogWorkflows(raw: unknown, locale: string): ResolvedWorkflow[] { + const items = asRecord(raw)?.items + if (!Array.isArray(items)) throw new Error('Catalog response has no items list') + const seen = new Set() + const workflows: ResolvedWorkflow[] = [] + for (const item of items) { + const workflow = toCatalogWorkflow(item, locale) + if (!workflow || seen.has(workflow.card.id)) continue + seen.add(workflow.card.id) + workflows.push(workflow) + } + return workflows.slice(0, HOME_WORKFLOW_LIMIT) +} + +function fetchPage(surface: string | null): Promise { + return Promise.race([ + fetchJSON(catalogWorkflowsUrl(surface), { refresh: true }), + new Promise((_resolve, reject) => { + setTimeout( + () => reject(new Error('Catalog workflows request timed out')), + FETCH_TIMEOUT_MS + ).unref?.() + }) + ]) +} + +async function fetchHomeWorkflows(locale: string): Promise { + const forDesktop = parseCatalogWorkflows(await fetchPage(DESKTOP_SURFACE), locale) + if (forDesktop.length > 0) return forDesktop + return parseCatalogWorkflows(await fetchPage(null), locale) +} + +interface CacheEntry { + locale: string + loadedAt: number + workflows: ResolvedWorkflow[] +} + +let cache: CacheEntry | null = null +let inFlight: { locale: string; request: Promise } | null = null + +function loadHomeWorkflows(locale: string): Promise { + if (cache && cache.locale === locale && Date.now() - cache.loadedAt < CACHE_TTL_MS) { + return Promise.resolve(cache.workflows) + } + if (inFlight?.locale === locale) return inFlight.request + const request = fetchHomeWorkflows(locale) + .then((workflows) => { + cache = { locale, loadedAt: Date.now(), workflows } + return workflows + }) + .catch((err: unknown) => { + if (cache?.locale === locale) return cache.workflows + throw err + }) + .finally(() => { + if (inFlight?.request === request) inFlight = null + }) + inFlight = { locale, request } + return request +} + +export async function listHomeWorkflows(locale: string): Promise { + return (await loadHomeWorkflows(locale)).map((workflow) => workflow.card) +} + +export function hubUrlForListedWorkflow(id: string): string | null { + return cache?.workflows.find((workflow) => workflow.card.id === id)?.hubUrl ?? null +} + +export function _resetCatalogWorkflowsForTest(): void { + cache = null + inFlight = null +} diff --git a/src/main/host/panelView.ts b/src/main/host/panelView.ts index 349a4dc23..13d193696 100644 --- a/src/main/host/panelView.ts +++ b/src/main/host/panelView.ts @@ -21,6 +21,7 @@ import { import type { BodyMode, ComfyPanelKey, ComfyWindowEntry } from './registry' const DASHBOARD_PAGE_PANELS: ReadonlyMap = new Map([ + ['home', 'comfy'], ['chooser', 'comfy'], ['benchmarks', 'benchmarks'], ['performance-test', 'performance-test'] diff --git a/src/main/lib/ipc/index.ts b/src/main/lib/ipc/index.ts index 70f5f2476..092fc132d 100644 --- a/src/main/lib/ipc/index.ts +++ b/src/main/lib/ipc/index.ts @@ -36,6 +36,7 @@ import { registerCrashHandlers } from './registerCrashHandlers' import { registerTelemetryHandlers } from './registerTelemetryHandlers' import { registerDevPlatformHandlers } from './registerDevPlatformHandlers' import { registerEmbeddedSessionHandlers } from './registerEmbeddedSessionHandlers' +import { registerCatalogHandlers } from './registerCatalogHandlers' import { reconcileAdoptedSettings } from '../desktopAdopt' import { finalizeComfyBuilderRecovery, @@ -279,5 +280,6 @@ export function register(callbacks: RegisterCallbacks = {}): Promise { registerTelemetryHandlers() registerDevPlatformHandlers() registerEmbeddedSessionHandlers() + registerCatalogHandlers() return startupRecovery.then(() => undefined) } diff --git a/src/main/lib/ipc/registerCatalogHandlers.ts b/src/main/lib/ipc/registerCatalogHandlers.ts new file mode 100644 index 000000000..c0442f143 --- /dev/null +++ b/src/main/lib/ipc/registerCatalogHandlers.ts @@ -0,0 +1,19 @@ +import { ipcMain, shell } from 'electron' +import { hubUrlForListedWorkflow, listHomeWorkflows } from '../../catalog/workflows' +import { getLocale } from '../i18n' + +export function registerCatalogHandlers(): void { + ipcMain.handle('catalog:list-home-workflows', () => listHomeWorkflows(getLocale())) + + ipcMain.handle('catalog:open-workflow', async (_event, id: unknown) => { + if (typeof id !== 'string') return false + const hubUrl = hubUrlForListedWorkflow(id) + if (!hubUrl) return false + try { + await shell.openExternal(hubUrl) + return true + } catch { + return false + } + }) +} diff --git a/src/preload/api.ts b/src/preload/api.ts index 5ac3d4253..f34b284fe 100644 --- a/src/preload/api.ts +++ b/src/preload/api.ts @@ -98,6 +98,8 @@ export function buildElectronApi(): ElectronApi { closeAnnouncement: () => ipcRenderer.send('comfy-window:close-announcement'), signalOverlayReady: () => ipcRenderer.send('comfy-window:overlay-ready'), navigatePanel: (page) => ipcRenderer.send('comfy-window:navigate-panel', page), + listHomeWorkflows: () => ipcRenderer.invoke('catalog:list-home-workflows'), + openCatalogWorkflow: (id) => ipcRenderer.invoke('catalog:open-workflow', id), getBuildStorageSnapshot: () => ipcRenderer.invoke('comfy-window:build-storage-snapshot'), resolveStartupRestoreReveal: (result) => ipcRenderer.send('comfy-window:startup-restore-reveal', { result }), diff --git a/src/renderer/panel.html b/src/renderer/panel.html index f20a5f3b0..ab59e0c05 100644 --- a/src/renderer/panel.html +++ b/src/renderer/panel.html @@ -5,7 +5,7 @@ Comfy Desktop — Panel