initial model store (#674)

* initial model store

* refactor the 'modelstoreserviceimpl' to pinia

* pepper in some reactive

the inner ModelStore (per-folder) can't be pinia because its made of temporary instances, but it can be reactive

* use refs in metadata class

* remove 'reactive'

* remove ref too

* add simple unit tests for modelStore

* make things worse via autoformatting

* move mock impls to a function
This commit is contained in:
Alex "mcmonkey" Goodwin
2024-09-04 16:59:40 -07:00
committed by GitHub
parent 6c7fb5041d
commit 060e61f0db
4 changed files with 243 additions and 20 deletions

View File

@@ -343,6 +343,19 @@ class ComfyApi extends EventTarget {
return await res.json()
}
/**
* Gets the metadata for a model
* @param {string} folder The folder containing the model
* @param {string} model The model to get metadata for
* @returns The metadata for the model
*/
async viewMetadata(folder: string, model: string) {
const res = await this.fetchApi(
`/view_metadata/${folder}?filename=${encodeURIComponent(model)}`
)
return await res.json()
}
/**
* Tells the server to download a model from the specified URL to the specified directory and filename
* @param {string} url The URL to download the model from

View File

@@ -51,6 +51,7 @@ import {
} from '@/services/dialogService'
import { useSettingStore } from '@/stores/settingStore'
import { useToastStore } from '@/stores/toastStore'
import { ModelStore, useModelStore } from '@/stores/modelStore'
import type { ToastMessageOptions } from 'primevue/toast'
import { useWorkspaceStore } from '@/stores/workspaceStateStore'
import { LGraphGroup } from '@comfyorg/litegraph'
@@ -137,7 +138,6 @@ export class ComfyApp {
bodyBottom: HTMLElement
canvasContainer: HTMLElement
menu: ComfyAppMenu
modelsInFolderCache: Record<string, string[]>
constructor() {
this.vueAppReady = false
@@ -152,7 +152,6 @@ export class ComfyApp {
parent: document.body
})
this.menu = new ComfyAppMenu(this)
this.modelsInFolderCache = {}
/**
* List of extensions that are registered with the app
@@ -2260,12 +2259,14 @@ export class ComfyApp {
useSettingStore().get('Comfy.Workflow.ShowMissingModelsWarning')
) {
for (let m of graphData.models) {
const models_available = await this.getModelsInFolderCached(m.directory)
const models_available = await useModelStore().getModelsInFolderCached(
m.directory
)
if (models_available === null) {
// @ts-expect-error
m.directory_invalid = true
missingModels.push(m)
} else if (!models_available.includes(m.name)) {
} else if (!(m.name in models_available.models)) {
missingModels.push(m)
}
}
@@ -2860,19 +2861,6 @@ export class ComfyApp {
app.graph.arrange()
}
/**
* Gets the list of model names in a folder, using a temporary local cache
*/
async getModelsInFolderCached(folder: string): Promise<string[]> {
if (folder in this.modelsInFolderCache) {
return this.modelsInFolderCache[folder]
}
// TODO: needs a lock to avoid overlapping calls
const models = await api.getModels(folder)
this.modelsInFolderCache[folder] = models
return models
}
/**
* Registers a Comfy web extension with the app
* @param {ComfyExtension} extension
@@ -2901,9 +2889,10 @@ export class ComfyApp {
summary: 'Update',
detail: 'Update requested'
}
if (this.vueAppReady) useToastStore().add(requestToastMessage)
this.modelsInFolderCache = {}
if (this.vueAppReady) {
useToastStore().add(requestToastMessage)
useModelStore().clearCache()
}
const defs = await api.getNodeDefs()