From 8d515dc309e0e4cd4160cae01cdfa9a84c7110d6 Mon Sep 17 00:00:00 2001 From: bymyself Date: Sun, 2 Mar 2025 13:37:15 -0700 Subject: [PATCH] Use index.json to load workflow templates (#2803) --- browser_tests/helpers/templates.ts | 13 +++++++++---- browser_tests/templates.spec.ts | 6 ++++-- .../templates/TemplateWorkflowsContent.vue | 14 +++++++++----- src/scripts/api.ts | 9 +++++++++ src/stores/workflowTemplatesStore.ts | 7 +++---- 5 files changed, 34 insertions(+), 15 deletions(-) diff --git a/browser_tests/helpers/templates.ts b/browser_tests/helpers/templates.ts index 0aa59c3ee..d659e125a 100644 --- a/browser_tests/helpers/templates.ts +++ b/browser_tests/helpers/templates.ts @@ -1,8 +1,10 @@ import { Locator, Page } from '@playwright/test' import path from 'path' -import { CORE_TEMPLATES } from '../../src/constants/coreTemplates' -import { TemplateInfo } from '../../src/types/workflowTemplateTypes' +import { + TemplateInfo, + WorkflowTemplates +} from '../../src/types/workflowTemplateTypes' export class ComfyTemplates { readonly content: Locator @@ -18,8 +20,11 @@ export class ComfyTemplates { .click() } - getAllTemplates(): TemplateInfo[] { - return CORE_TEMPLATES.flatMap((category) => category.templates) + async getAllTemplates(): Promise { + const templates: WorkflowTemplates[] = await this.page.evaluate(() => + window['app'].api.getCoreWorkflowTemplates() + ) + return templates.flatMap((t) => t.templates) } getTemplatePath(filename: string): string { diff --git a/browser_tests/templates.spec.ts b/browser_tests/templates.spec.ts index d5a46b0fc..ef68e897e 100644 --- a/browser_tests/templates.spec.ts +++ b/browser_tests/templates.spec.ts @@ -12,7 +12,8 @@ test.describe('Templates', () => { test('should have a JSON workflow file for each template', async ({ comfyPage }) => { - for (const template of comfyPage.templates.getAllTemplates()) { + const templates = await comfyPage.templates.getAllTemplates() + for (const template of templates) { const workflowPath = comfyPage.templates.getTemplatePath( `${template.name}.json` ) @@ -26,7 +27,8 @@ test.describe('Templates', () => { test('should have all required thumbnail media for each template', async ({ comfyPage }) => { - for (const template of comfyPage.templates.getAllTemplates()) { + const templates = await comfyPage.templates.getAllTemplates() + for (const template of templates) { const { name, mediaSubtype, thumbnailVariant } = template const baseMedia = `${name}-1.${mediaSubtype}` const basePath = comfyPage.templates.getTemplatePath(baseMedia) diff --git a/src/components/templates/TemplateWorkflowsContent.vue b/src/components/templates/TemplateWorkflowsContent.vue index a81c9c822..953624aad 100644 --- a/src/components/templates/TemplateWorkflowsContent.vue +++ b/src/components/templates/TemplateWorkflowsContent.vue @@ -20,7 +20,7 @@ class="absolute translate-x-0 top-0 left-0 h-full w-80 shadow-md z-5 transition-transform duration-300 ease-in-out" > -
+

{{ selectedTab.title }}

@@ -96,9 +96,13 @@ const { isReady } = useAsyncState( null ) -const selectedTab = ref( - workflowTemplatesStore.defaultTemplate -) +const selectedTab = ref() +const selectFirstTab = () => { + const firstTab = workflowTemplatesStore.groupedTemplates[0].modules[0] + handleTabSelection(firstTab) +} +watch(isReady, selectFirstTab, { once: true }) + const workflowLoading = ref(null) const tabs = computed(() => workflowTemplatesStore.groupedTemplates) diff --git a/src/scripts/api.ts b/src/scripts/api.ts index acb2e1b5d..6f098aef4 100644 --- a/src/scripts/api.ts +++ b/src/scripts/api.ts @@ -29,6 +29,7 @@ import { type ComfyNodeDef, validateComfyNodeDef } from '@/schemas/nodeDefSchema' +import { WorkflowTemplates } from '@/types/workflowTemplateTypes' interface QueuePromptRequestBody { client_id: string @@ -414,6 +415,14 @@ export class ComfyApi extends EventTarget { return await res.json() } + /** + * Gets the index of core workflow templates. + */ + async getCoreWorkflowTemplates(): Promise { + const res = await axios.get('/templates/index.json') + return res.status === 200 ? res.data : [] + } + /** * Gets a list of embedding names */ diff --git a/src/stores/workflowTemplatesStore.ts b/src/stores/workflowTemplatesStore.ts index 074533a3c..f888077e0 100644 --- a/src/stores/workflowTemplatesStore.ts +++ b/src/stores/workflowTemplatesStore.ts @@ -2,7 +2,6 @@ import { groupBy } from 'lodash' import { defineStore } from 'pinia' import { computed, ref, shallowRef } from 'vue' -import { CORE_TEMPLATES } from '@/constants/coreTemplates' import { api } from '@/scripts/api' import type { TemplateGroup, @@ -13,12 +12,12 @@ export const useWorkflowTemplatesStore = defineStore( 'workflowTemplates', () => { const customTemplates = shallowRef<{ [moduleName: string]: string[] }>({}) + const coreTemplates = shallowRef([]) const isLoaded = ref(false) - const defaultTemplate: WorkflowTemplates = CORE_TEMPLATES[0] const groupedTemplates = computed(() => { const allTemplates = [ - ...CORE_TEMPLATES, + ...coreTemplates.value, ...Object.entries(customTemplates.value).map( ([moduleName, templates]) => ({ moduleName, @@ -44,6 +43,7 @@ export const useWorkflowTemplatesStore = defineStore( try { if (!isLoaded.value) { customTemplates.value = await api.getWorkflowTemplates() + coreTemplates.value = await api.getCoreWorkflowTemplates() isLoaded.value = true } } catch (error) { @@ -53,7 +53,6 @@ export const useWorkflowTemplatesStore = defineStore( return { groupedTemplates, - defaultTemplate, isLoaded, loadWorkflowTemplates }