ComfyUI: Can set a custom models folder
This commit is contained in:
parent
66eb43a9cf
commit
21312811e5
5 changed files with 196 additions and 33 deletions
38
server.js
38
server.js
|
|
@ -7597,8 +7597,14 @@ async function fileSizeOrNull(targetPath) {
|
|||
}
|
||||
}
|
||||
|
||||
function resolveComfySubPath(comfyPath, relativePath) {
|
||||
function resolveComfySubPath(comfyPath, relativePath, modelsPath) {
|
||||
const normalizedRelative = String(relativePath || '').replace(/^[/\\]+/, '');
|
||||
if (modelsPath) {
|
||||
const afterModels = normalizedRelative.replace(/^models[/\\]?/, '');
|
||||
if (afterModels !== normalizedRelative) {
|
||||
return path.join(modelsPath, afterModels);
|
||||
}
|
||||
}
|
||||
return path.join(comfyPath, normalizedRelative);
|
||||
}
|
||||
|
||||
|
|
@ -7638,7 +7644,7 @@ async function downloadFileWithProgress(url, destinationPath, onChunk) {
|
|||
return { receivedBytes, totalBytes };
|
||||
}
|
||||
|
||||
async function runSetupDownloads(jobId, comfyPath, files) {
|
||||
async function runSetupDownloads(jobId, comfyPath, files, modelsPath) {
|
||||
const totalExpectedBytes = files.reduce((sum, file) => sum + (Number(file.expectedBytes) || 0), 0);
|
||||
let cumulativeCompletedBytes = 0;
|
||||
|
||||
|
|
@ -7655,7 +7661,7 @@ async function runSetupDownloads(jobId, comfyPath, files) {
|
|||
|
||||
for (let index = 0; index < files.length; index += 1) {
|
||||
const file = files[index];
|
||||
const destinationPath = resolveComfySubPath(comfyPath, path.join(file.relativeDir, file.fileName));
|
||||
const destinationPath = resolveComfySubPath(comfyPath, path.join(file.relativeDir, file.fileName), modelsPath);
|
||||
|
||||
try {
|
||||
await fs.mkdir(path.dirname(destinationPath), { recursive: true });
|
||||
|
|
@ -7892,6 +7898,7 @@ app.get('/api/setup/config', async (req, res) => {
|
|||
app.post('/api/setup/check-comfy-path', async (req, res) => {
|
||||
try {
|
||||
const comfyPath = String(req.body?.path || '').trim();
|
||||
const modelsPath = String(req.body?.modelsPath || '').trim();
|
||||
if (!comfyPath) {
|
||||
return res.status(400).json({ error: 'A ComfyUI folder path is required' });
|
||||
}
|
||||
|
|
@ -7901,23 +7908,30 @@ app.post('/api/setup/check-comfy-path', async (req, res) => {
|
|||
return res.status(400).json({ error: `Folder does not exist: ${comfyPath}` });
|
||||
}
|
||||
|
||||
const modelsDir = path.join(comfyPath, 'models');
|
||||
const modelsExist = await pathExists(modelsDir);
|
||||
if (!modelsExist) {
|
||||
return res.status(400).json({ error: `This does not look like a ComfyUI folder (missing "models" subfolder): ${comfyPath}` });
|
||||
if (modelsPath) {
|
||||
const modelsPathExists = await pathExists(modelsPath);
|
||||
if (!modelsPathExists) {
|
||||
return res.status(400).json({ error: `Models folder does not exist: ${modelsPath}` });
|
||||
}
|
||||
} else {
|
||||
const modelsDir = path.join(comfyPath, 'models');
|
||||
const modelsExist = await pathExists(modelsDir);
|
||||
if (!modelsExist) {
|
||||
return res.status(400).json({ error: `This does not look like a ComfyUI folder (missing "models" subfolder): ${comfyPath}` });
|
||||
}
|
||||
}
|
||||
|
||||
const config = await loadSetupConfig();
|
||||
const created = [];
|
||||
for (const relativePath of Object.values(config.ComfyUIPaths || {})) {
|
||||
const target = resolveComfySubPath(comfyPath, relativePath);
|
||||
const target = resolveComfySubPath(comfyPath, relativePath, modelsPath);
|
||||
if (!(await pathExists(target))) {
|
||||
await fs.mkdir(target, { recursive: true });
|
||||
created.push(relativePath);
|
||||
}
|
||||
}
|
||||
|
||||
res.json({ ok: true, comfyPath, createdSubfolders: created });
|
||||
res.json({ ok: true, comfyPath, modelsPath, createdSubfolders: created });
|
||||
} catch (err) {
|
||||
console.error('Failed to validate ComfyUI path:', err);
|
||||
res.status(500).json({ error: err.message || 'Failed to validate ComfyUI path' });
|
||||
|
|
@ -7927,6 +7941,7 @@ app.post('/api/setup/check-comfy-path', async (req, res) => {
|
|||
app.post('/api/setup/check-files', async (req, res) => {
|
||||
try {
|
||||
const comfyPath = String(req.body?.comfyPath || '').trim();
|
||||
const modelsPath = String(req.body?.modelsPath || '').trim();
|
||||
const files = Array.isArray(req.body?.files) ? req.body.files : [];
|
||||
|
||||
if (!comfyPath) {
|
||||
|
|
@ -7935,7 +7950,7 @@ app.post('/api/setup/check-files', async (req, res) => {
|
|||
|
||||
const results = [];
|
||||
for (const file of files) {
|
||||
const absPath = resolveComfySubPath(comfyPath, path.join(file.relativeDir || '', file.fileName || ''));
|
||||
const absPath = resolveComfySubPath(comfyPath, path.join(file.relativeDir || '', file.fileName || ''), modelsPath);
|
||||
const size = await fileSizeOrNull(absPath);
|
||||
results.push({
|
||||
relativeDir: file.relativeDir || '',
|
||||
|
|
@ -7955,6 +7970,7 @@ app.post('/api/setup/check-files', async (req, res) => {
|
|||
app.post('/api/setup/download', async (req, res) => {
|
||||
try {
|
||||
const comfyPath = String(req.body?.comfyPath || '').trim();
|
||||
const modelsPath = String(req.body?.modelsPath || '').trim();
|
||||
const files = Array.isArray(req.body?.files) ? req.body.files : [];
|
||||
|
||||
if (!comfyPath) {
|
||||
|
|
@ -7970,7 +7986,7 @@ app.post('/api/setup/download', async (req, res) => {
|
|||
const jobId = randomUUID();
|
||||
getSetupDownloadJob(jobId);
|
||||
|
||||
runSetupDownloads(jobId, comfyPath, files).catch(err => {
|
||||
runSetupDownloads(jobId, comfyPath, files, modelsPath).catch(err => {
|
||||
console.error('[setup] download job crashed:', err);
|
||||
publishSetupDownloadProgress(jobId, { status: 'error', error: err.message || String(err) });
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { useState, useEffect } from 'react'
|
||||
import { useSettings } from '../context/SettingsContext.shared'
|
||||
import { API_BASE } from '../config'
|
||||
import './SettingsModal.css'
|
||||
|
||||
const CUSTOM_API_TYPE_OPTIONS = [
|
||||
|
|
@ -182,6 +183,48 @@ function RiggingInstaller() {
|
|||
)
|
||||
}
|
||||
|
||||
// Desktop-only-friendly folder browse button: calls the backend's native folder
|
||||
// picker (Windows) and reports the chosen path back to the caller.
|
||||
function BrowseFolderButton({ description, initialPath, onPick }) {
|
||||
const [busy, setBusy] = useState(false)
|
||||
const [error, setError] = useState('')
|
||||
|
||||
const handleClick = async () => {
|
||||
setError(''); setBusy(true)
|
||||
try {
|
||||
const res = await fetch(`${API_BASE}/setup/pick-folder`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ description, initialPath })
|
||||
})
|
||||
const data = await res.json()
|
||||
if (!res.ok) throw new Error(data?.error || 'Folder picker failed')
|
||||
if (data.path) onPick(data.path)
|
||||
} catch (err) {
|
||||
setError(err.message || String(err))
|
||||
} finally {
|
||||
setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<button
|
||||
type="button"
|
||||
className="kanban-sidebar__new-asset"
|
||||
style={{ margin: 0, padding: '0.5rem 0.9rem', whiteSpace: 'nowrap' }}
|
||||
onClick={handleClick}
|
||||
disabled={busy}
|
||||
title={description}
|
||||
>
|
||||
<span className="material-symbols-outlined" style={{ fontSize: '16px', verticalAlign: 'middle' }}>folder_open</span>
|
||||
{busy ? '…' : 'Browse'}
|
||||
</button>
|
||||
{error && <p className="settings-helper-text" style={{ color: '#f87171' }}>{error}</p>}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default function SettingsModal({ onClose }) {
|
||||
const { settings, updateSettings, addCustomApi } = useSettings()
|
||||
const [localSettings, setLocalSettings] = useState(settings)
|
||||
|
|
@ -536,21 +579,65 @@ export default function SettingsModal({ onClose }) {
|
|||
|
||||
<div className="settings-input-group">
|
||||
<label className="settings-label">Path</label>
|
||||
<input
|
||||
className="settings-input"
|
||||
placeholder="C:\\ComfyUI"
|
||||
value={localSettings?.apis?.comfyui?.path || ''}
|
||||
onChange={e => setLocalSettings(prev => ({
|
||||
...prev,
|
||||
apis: {
|
||||
...prev?.apis,
|
||||
comfyui: {
|
||||
...prev?.apis?.comfyui,
|
||||
path: e.target.value
|
||||
<div style={{ display: 'flex', gap: '0.5em', alignItems: 'flex-start' }}>
|
||||
<input
|
||||
className="settings-input"
|
||||
style={{ flex: 1 }}
|
||||
placeholder="C:\\ComfyUI"
|
||||
value={localSettings?.apis?.comfyui?.path || ''}
|
||||
onChange={e => setLocalSettings(prev => ({
|
||||
...prev,
|
||||
apis: {
|
||||
...prev?.apis,
|
||||
comfyui: {
|
||||
...prev?.apis?.comfyui,
|
||||
path: e.target.value
|
||||
}
|
||||
}
|
||||
}
|
||||
}))}
|
||||
/>
|
||||
}))}
|
||||
/>
|
||||
<BrowseFolderButton
|
||||
description="Select your ComfyUI folder"
|
||||
initialPath={localSettings?.apis?.comfyui?.path || ''}
|
||||
onPick={picked => setLocalSettings(prev => ({
|
||||
...prev,
|
||||
apis: { ...prev?.apis, comfyui: { ...prev?.apis?.comfyui, path: picked } }
|
||||
}))}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="settings-input-group">
|
||||
<label className="settings-label">Models Path <span style={{ opacity: 0.6, fontWeight: 400 }}>(optional)</span></label>
|
||||
<div style={{ display: 'flex', gap: '0.5em', alignItems: 'flex-start' }}>
|
||||
<input
|
||||
className="settings-input"
|
||||
style={{ flex: 1 }}
|
||||
placeholder="Defaults to {ComfyUI path}\models"
|
||||
value={localSettings?.apis?.comfyui?.modelsPath || ''}
|
||||
onChange={e => setLocalSettings(prev => ({
|
||||
...prev,
|
||||
apis: {
|
||||
...prev?.apis,
|
||||
comfyui: {
|
||||
...prev?.apis?.comfyui,
|
||||
modelsPath: e.target.value
|
||||
}
|
||||
}
|
||||
}))}
|
||||
/>
|
||||
<BrowseFolderButton
|
||||
description="Select your ComfyUI models folder"
|
||||
initialPath={localSettings?.apis?.comfyui?.modelsPath || localSettings?.apis?.comfyui?.path || ''}
|
||||
onPick={picked => setLocalSettings(prev => ({
|
||||
...prev,
|
||||
apis: { ...prev?.apis, comfyui: { ...prev?.apis?.comfyui, modelsPath: picked } }
|
||||
}))}
|
||||
/>
|
||||
</div>
|
||||
<p className="settings-helper-text">
|
||||
Set this only if your models live somewhere other than <code>{'{ComfyUI path}'}\models</code> (e.g. shared across multiple ComfyUI installs).
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div className="settings-grid settings-grid--triple">
|
||||
|
|
|
|||
|
|
@ -74,9 +74,11 @@ export default function SetupWizardModal({ onComplete, onClose }) {
|
|||
const [loadError, setLoadError] = useState('')
|
||||
|
||||
const [comfyPath, setComfyPath] = useState('')
|
||||
const [modelsPath, setModelsPath] = useState('')
|
||||
const [pathBusy, setPathBusy] = useState(false)
|
||||
const [pathError, setPathError] = useState('')
|
||||
const [browseBusy, setBrowseBusy] = useState(false)
|
||||
const [browseModelsBusy, setBrowseModelsBusy] = useState(false)
|
||||
|
||||
const [selectionByName, setSelectionByName] = useState({})
|
||||
const [existingFileKeys, setExistingFileKeys] = useState(new Set())
|
||||
|
|
@ -117,6 +119,10 @@ export default function SetupWizardModal({ onComplete, onClose }) {
|
|||
setComfyPath(settings?.apis?.comfyui?.path || '')
|
||||
}, [settings?.apis?.comfyui?.path])
|
||||
|
||||
useEffect(() => {
|
||||
setModelsPath(settings?.apis?.comfyui?.modelsPath || '')
|
||||
}, [settings?.apis?.comfyui?.modelsPath])
|
||||
|
||||
useEffect(() => () => {
|
||||
eventSourceRef.current?.close()
|
||||
}, [])
|
||||
|
|
@ -131,9 +137,10 @@ export default function SetupWizardModal({ onComplete, onClose }) {
|
|||
return buildFileList(config, allSelections, paths)
|
||||
}, [config])
|
||||
|
||||
const refreshFileExistence = useCallback(async (pathOverride) => {
|
||||
const refreshFileExistence = useCallback(async (pathOverride, modelsPathOverride) => {
|
||||
if (!config) return
|
||||
const effectivePath = pathOverride ?? comfyPath
|
||||
const effectiveModelsPath = modelsPathOverride ?? modelsPath
|
||||
if (!effectivePath) return
|
||||
|
||||
setCheckingFiles(true)
|
||||
|
|
@ -141,7 +148,7 @@ export default function SetupWizardModal({ onComplete, onClose }) {
|
|||
const res = await fetch(`${API_BASE}/setup/check-files`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ comfyPath: effectivePath, files: allCandidateFiles })
|
||||
body: JSON.stringify({ comfyPath: effectivePath, modelsPath: effectiveModelsPath, files: allCandidateFiles })
|
||||
})
|
||||
const data = await res.json()
|
||||
if (!res.ok) throw new Error(data?.error || 'Failed to check files')
|
||||
|
|
@ -156,7 +163,7 @@ export default function SetupWizardModal({ onComplete, onClose }) {
|
|||
} finally {
|
||||
setCheckingFiles(false)
|
||||
}
|
||||
}, [allCandidateFiles, comfyPath, config])
|
||||
}, [allCandidateFiles, comfyPath, modelsPath, config])
|
||||
|
||||
const selections = useMemo(
|
||||
() => Object.entries(selectionByName).map(([diffusionName, modelQuality]) => ({ diffusionName, modelQuality })),
|
||||
|
|
@ -255,15 +262,35 @@ export default function SetupWizardModal({ onComplete, onClose }) {
|
|||
}
|
||||
}
|
||||
|
||||
const handleBrowseModelsFolder = async () => {
|
||||
setPathError('')
|
||||
setBrowseModelsBusy(true)
|
||||
try {
|
||||
const res = await fetch(`${API_BASE}/setup/pick-folder`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ description: 'Select your ComfyUI models folder', initialPath: modelsPath || comfyPath })
|
||||
})
|
||||
const data = await res.json()
|
||||
if (!res.ok) throw new Error(data?.error || 'Folder picker failed')
|
||||
if (data.path) setModelsPath(data.path)
|
||||
} catch (err) {
|
||||
setPathError(err.message || String(err))
|
||||
} finally {
|
||||
setBrowseModelsBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleValidatePath = async () => {
|
||||
setPathError('')
|
||||
setPathBusy(true)
|
||||
try {
|
||||
const trimmed = comfyPath.trim()
|
||||
const trimmedModelsPath = modelsPath.trim()
|
||||
const res = await fetch(`${API_BASE}/setup/check-comfy-path`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ path: trimmed })
|
||||
body: JSON.stringify({ path: trimmed, modelsPath: trimmedModelsPath })
|
||||
})
|
||||
const data = await res.json()
|
||||
if (!res.ok) throw new Error(data?.error || 'Invalid ComfyUI path')
|
||||
|
|
@ -272,11 +299,11 @@ export default function SetupWizardModal({ onComplete, onClose }) {
|
|||
...settings,
|
||||
apis: {
|
||||
...(settings?.apis || {}),
|
||||
comfyui: { ...(settings?.apis?.comfyui || {}), path: trimmed }
|
||||
comfyui: { ...(settings?.apis?.comfyui || {}), path: trimmed, modelsPath: trimmedModelsPath }
|
||||
}
|
||||
})
|
||||
|
||||
await refreshFileExistence(trimmed)
|
||||
await refreshFileExistence(trimmed, trimmedModelsPath)
|
||||
setStepId('models')
|
||||
} catch (err) {
|
||||
setPathError(err.message || String(err))
|
||||
|
|
@ -298,7 +325,7 @@ export default function SetupWizardModal({ onComplete, onClose }) {
|
|||
const res = await fetch(`${API_BASE}/setup/download`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ comfyPath, files: filesToDownload })
|
||||
body: JSON.stringify({ comfyPath, modelsPath, files: filesToDownload })
|
||||
})
|
||||
const data = await res.json()
|
||||
if (!res.ok) throw new Error(data?.error || 'Failed to start downloads')
|
||||
|
|
@ -440,7 +467,36 @@ export default function SetupWizardModal({ onComplete, onClose }) {
|
|||
</button>
|
||||
</div>
|
||||
<p className="setup-wizard__hint">
|
||||
The folder must contain a <code>models</code> subfolder. Missing model-type subfolders will be created.
|
||||
{modelsPath.trim()
|
||||
? 'Missing model-type subfolders will be created inside your models folder below.'
|
||||
: <>The folder must contain a <code>models</code> subfolder. Missing model-type subfolders will be created.</>}
|
||||
</p>
|
||||
|
||||
<label className="projects-page__label font-label" htmlFor="setup-models-path" style={{ marginTop: '1em' }}>
|
||||
Models Folder <span style={{ opacity: 0.6, fontWeight: 400 }}>(optional)</span>
|
||||
</label>
|
||||
<div className="setup-wizard__path-row">
|
||||
<input
|
||||
id="setup-models-path"
|
||||
type="text"
|
||||
className="projects-page__input setup-wizard__path-input"
|
||||
placeholder="Defaults to {ComfyUI folder}\models"
|
||||
value={modelsPath}
|
||||
onChange={(e) => setModelsPath(e.target.value)}
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
className="setup-wizard__browse-btn"
|
||||
onClick={handleBrowseModelsFolder}
|
||||
disabled={browseModelsBusy}
|
||||
title="Browse for models folder"
|
||||
>
|
||||
<span className="material-symbols-outlined" style={{ fontSize: '18px' }}>folder_open</span>
|
||||
{browseModelsBusy ? '…' : 'Browse'}
|
||||
</button>
|
||||
</div>
|
||||
<p className="setup-wizard__hint">
|
||||
Set this only if your models live somewhere other than <code>{'{ComfyUI folder}'}\models</code> (e.g. shared across multiple ComfyUI installs).
|
||||
</p>
|
||||
{pathError && <div className="setup-wizard__error">{pathError}</div>}
|
||||
|
||||
|
|
|
|||
|
|
@ -151,6 +151,7 @@ export function SettingsProvider({ children }) {
|
|||
},
|
||||
comfyui: {
|
||||
path: '',
|
||||
modelsPath: '',
|
||||
url: 'http://127.0.0.1',
|
||||
port: '8188'
|
||||
},
|
||||
|
|
|
|||
|
|
@ -176,6 +176,9 @@ export const DEFAULT_SETTINGS = {
|
|||
},
|
||||
comfyui: {
|
||||
path: '',
|
||||
// Optional override for where models live, when it's not "{path}/models"
|
||||
// (e.g. models are shared across ComfyUI installs via extra_model_paths.yaml).
|
||||
modelsPath: '',
|
||||
url: 'http://127.0.0.1',
|
||||
port: '8188'
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue