Skip to content

Commit

Permalink
Add UV mirror settings (#713)
Browse files Browse the repository at this point in the history
  • Loading branch information
huchenlei authored Jan 24, 2025
1 parent a05aaf0 commit a6a59f6
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/config/comfySettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ export const DEFAULT_SETTINGS: ComfySettingsData = {
'Comfy.Workflow.WorkflowTabsPosition': 'Topbar',
'Comfy.Workflow.ShowMissingModelsWarning': true,
'Comfy.Server.LaunchArgs': {},
'Comfy-Desktop.PythonInstallMirror': '',
'Comfy-Desktop.PypiInstallMirror': '',
} as const;

export interface ComfySettingsData {
'Comfy-Desktop.AutoUpdate': boolean;
'Comfy-Desktop.SendStatistics': boolean;
'Comfy.Server.LaunchArgs': Record<string, string>;
'Comfy-Desktop.PythonInstallMirror': string;
'Comfy-Desktop.PypiInstallMirror': string;
[key: string]: unknown;
}

Expand Down
2 changes: 2 additions & 0 deletions src/install/installWizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export class InstallWizard implements HasTelemetry {
...existingSettings,
'Comfy-Desktop.AutoUpdate': this.installOptions.autoUpdate,
'Comfy-Desktop.SendStatistics': this.installOptions.allowMetrics,
'Comfy-Desktop.PythonInstallMirror': this.installOptions.pythonMirror,
'Comfy-Desktop.PypiInstallMirror': this.installOptions.pypiMirror,
};

if (this.installOptions.device === 'cpu') {
Expand Down
8 changes: 4 additions & 4 deletions src/main-process/comfyDesktopApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import path from 'node:path';
import { graphics } from 'systeminformation';

import { ComfyServerConfig } from '../config/comfyServerConfig';
import { ComfySettings } from '../config/comfySettings';
import { IPC_CHANNELS, ProgressStatus, ServerArgs } from '../constants';
import { InstallationManager } from '../install/installationManager';
import { DownloadManager } from '../models/DownloadManager';
Expand All @@ -23,14 +22,15 @@ import { ComfyServer } from './comfyServer';

export class ComfyDesktopApp implements HasTelemetry {
public comfyServer: ComfyServer | null = null;
public comfySettings: ComfySettings;
private terminal: Terminal | null = null; // Only created after server starts.
constructor(
public installation: ComfyInstallation,
public appWindow: AppWindow,
readonly telemetry: ITelemetry
) {
this.comfySettings = new ComfySettings(installation.basePath);
) {}

get comfySettings() {
return this.installation.comfySettings;
}

get basePath() {
Expand Down
18 changes: 15 additions & 3 deletions src/main-process/comfyInstallation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import log from 'electron-log/main';
import { rm } from 'node:fs/promises';

import { ComfyServerConfig } from '../config/comfyServerConfig';
import { ComfySettings } from '../config/comfySettings';
import type { InstallValidation, TorchDeviceType } from '../preload';
import { type ITelemetry, getTelemetry } from '../services/telemetry';
import { useDesktopConfig } from '../store/desktopConfig';
Expand Down Expand Up @@ -32,6 +33,7 @@ export class ComfyInstallation {
}

virtualEnvironment: VirtualEnvironment;
comfySettings: ComfySettings;

_basePath: string;
/** The base path of the desktop app. Models, nodes, and configuration are saved here by default. */
Expand All @@ -41,7 +43,7 @@ export class ComfyInstallation {
set basePath(value: string) {
// Duplicated in constructor to avoid non-nullable type assertions.
this._basePath = value;
this.virtualEnvironment = new VirtualEnvironment(value, this.telemetry, this.device);
this.virtualEnvironment = this.createVirtualEnvironment(value);
}

/**
Expand All @@ -61,7 +63,17 @@ export class ComfyInstallation {
) {
// TypeScript workaround: duplication of basePath setter
this._basePath = basePath;
this.virtualEnvironment = new VirtualEnvironment(basePath, telemetry, device);
this.comfySettings = new ComfySettings(basePath);
this.virtualEnvironment = this.createVirtualEnvironment(basePath);
}

private createVirtualEnvironment(basePath: string) {
return new VirtualEnvironment(basePath, {
telemetry: this.telemetry,
selectedDevice: this.device,
pythonMirror: this.comfySettings.get('Comfy-Desktop.PythonInstallMirror'),
pypiMirror: this.comfySettings.get('Comfy-Desktop.PypiInstallMirror'),
});
}

/**
Expand Down Expand Up @@ -105,7 +117,7 @@ export class ComfyInstallation {
validation.basePath = 'OK';
this.onUpdate?.(validation);

const venv = new VirtualEnvironment(basePath, this.telemetry, this.device);
const venv = this.createVirtualEnvironment(basePath);
if (await venv.exists()) {
validation.venvDirectory = 'OK';
this.onUpdate?.(validation);
Expand Down
3 changes: 3 additions & 0 deletions src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export interface InstallOptions {
migrationItemIds?: string[];
/** Torch compute device */
device: TorchDeviceType;
/** UV python mirrors */
pythonMirror?: string; // UV_PYTHON_INSTALL_MIRROR
pypiMirror?: string; // UV_PYPI_INSTALL_MIRROR
}

export interface SystemPaths {
Expand Down
26 changes: 22 additions & 4 deletions src/virtualEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export class VirtualEnvironment implements HasTelemetry {
readonly comfyUIRequirementsPath: string;
readonly comfyUIManagerRequirementsPath: string;
readonly selectedDevice?: string;
readonly telemetry: ITelemetry;
readonly pythonMirror?: string;
readonly pypiMirror?: string;
uvPty: pty.IPty | undefined;

/** @todo Refactor to `using` */
Expand All @@ -51,6 +54,8 @@ export class VirtualEnvironment implements HasTelemetry {
UV_TOOL_BIN_DIR: this.cacheDir,
UV_PYTHON_INSTALL_DIR: this.cacheDir,
VIRTUAL_ENV: this.venvPath,
UV_PYTHON_INSTALL_MIRROR: this.pythonMirror,
UV_PYPI_INSTALL_MIRROR: this.pypiMirror,
},
});
}
Expand All @@ -59,13 +64,26 @@ export class VirtualEnvironment implements HasTelemetry {

constructor(
venvPath: string,
readonly telemetry: ITelemetry,
selectedDevice: TorchDeviceType | undefined,
pythonVersion: string = '3.12.8'
{
telemetry,
selectedDevice,
pythonVersion,
pythonMirror,
pypiMirror,
}: {
telemetry: ITelemetry;
selectedDevice?: TorchDeviceType;
pythonVersion?: string;
pythonMirror?: string;
pypiMirror?: string;
}
) {
this.venvRootPath = venvPath;
this.pythonVersion = pythonVersion;
this.telemetry = telemetry;
this.pythonVersion = pythonVersion ?? '3.12';
this.selectedDevice = selectedDevice;
this.pythonMirror = pythonMirror;
this.pypiMirror = pypiMirror;

// uv defaults to .venv
this.venvPath = path.join(venvPath, '.venv');
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/comfySettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ describe('ComfySettings', () => {
'Comfy-Desktop.AutoUpdate': false,
'Comfy-Desktop.SendStatistics': false,
'Comfy.Server.LaunchArgs': { test: 'value' },
'Comfy-Desktop.PythonInstallMirror': '',
'Comfy-Desktop.PypiInstallMirror': '',
};

vi.mocked(fs.access).mockResolvedValue();
Expand Down

0 comments on commit a6a59f6

Please sign in to comment.