Skip to content

Commit

Permalink
Merge branch 'main' into rh-comfy
Browse files Browse the repository at this point in the history
  • Loading branch information
robinjhuang authored Nov 21, 2024
2 parents 0575425 + fedad31 commit be9a90a
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 21 deletions.
5 changes: 4 additions & 1 deletion .env_example
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ USE_EXTERNAL_SERVER=false
DEV_SERVER_URL=http://192.168.2.20:5173

# The level of logging to use.
LOG_LEVEL=debug
LOG_LEVEL=debug

# Send events to Sentry
SENTRY_ENABLED=false
5 changes: 4 additions & 1 deletion .env_test_example
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ COMFY_HOST=localhost
COMFY_PORT=5173

# Whether to use an external server instead of starting one locally.
USE_EXTERNAL_SERVER=true
USE_EXTERNAL_SERVER=true

# Send events to Sentry
SENTRY_ENABLED=false
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ dist
assets
.vite
scripts
.env_*
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"main": ".vite/build/main.js",
"packageManager": "[email protected]",
"config": {
"frontendVersion": "1.4.4",
"frontendVersion": "1.4.6",
"comfyVersion": "0.3.0",
"managerCommit": "e629215c100c89a9a9d33fc03be3248069ff67ef",
"uvVersion": "0.5.2"
Expand Down
38 changes: 22 additions & 16 deletions src/main-process/comfyDesktopApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ import { getAppResourcesPath } from '../install/resourcePaths';

export class ComfyDesktopApp {
public comfyServer: ComfyServer | null = null;
private terminal: Terminal;
private terminal: Terminal | null = null; // Only created after server starts.

constructor(
public basePath: string,
public comfySettings: ComfySettings,
public appWindow: AppWindow
) {
this.terminal = new Terminal(this.appWindow, getAppResourcesPath());
}
) {}

get pythonInstallPath() {
return app.isPackaged ? this.basePath : path.join(app.getAppPath(), 'assets');
Expand All @@ -50,6 +48,22 @@ export class ComfyDesktopApp {
});
}

private initializeTerminal(virtualEnvironment: VirtualEnvironment) {
this.terminal = new Terminal(this.appWindow, this.basePath, virtualEnvironment.uvPath);

ipcMain.handle(IPC_CHANNELS.TERMINAL_WRITE, (_event, command: string) => {
this.terminal?.write(command);
});

ipcMain.handle(IPC_CHANNELS.TERMINAL_RESIZE, (_event, cols: number, rows: number) => {
this.terminal?.resize(cols, rows);
});

ipcMain.handle(IPC_CHANNELS.TERMINAL_RESTORE, (_event) => {
return this.terminal?.restore();
});
}

async setupGPUContext(): Promise<void> {
log.debug('Setting up GPU context');
try {
Expand Down Expand Up @@ -103,24 +117,15 @@ export class ComfyDesktopApp {
return Sentry.captureMessage(error, {
level: 'error',
extra: { ...extras, comfyUIExecutionError: true },
tags: {
comfyorigin: 'core',
},
});
} catch (err) {
log.error('Failed to send error to Sentry:', err);
return null;
}
});

ipcMain.handle(IPC_CHANNELS.TERMINAL_WRITE, (_event, command: string) => {
this.terminal.write(command);
});

ipcMain.handle(IPC_CHANNELS.TERMINAL_RESIZE, (_event, cols: number, rows: number) => {
this.terminal.resize(cols, rows);
});

ipcMain.handle(IPC_CHANNELS.TERMINAL_RESTORE, (_event) => {
return this.terminal.restore();
});
}

/**
Expand Down Expand Up @@ -172,6 +177,7 @@ export class ComfyDesktopApp {
this.appWindow.sendServerStartProgress(ProgressStatus.STARTING_SERVER);
this.comfyServer = new ComfyServer(this.basePath, serverArgs, virtualEnvironment, this.appWindow);
await this.comfyServer.start();
this.initializeTerminal(virtualEnvironment);
}

static async create(appWindow: AppWindow): Promise<ComfyDesktopApp> {
Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ let alwaysSendCrashReports = false;
Sentry.init({
dsn: SENTRY_URL_ENDPOINT,
autoSessionTracking: false,
enabled: process.env.SENTRY_ENABLED === 'true' || app.isPackaged,
beforeSend: async (event, hint) => {
if (event.extra?.comfyUIExecutionError || alwaysSendCrashReports) {
return event;
Expand Down
25 changes: 23 additions & 2 deletions src/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export class Terminal {
#pty: pty.IPty | undefined;
#window: AppWindow | undefined;
#cwd: string | undefined;
#uvPath: string | undefined;

readonly sessionBuffer: string[] = [];
readonly size = { cols: 80, rows: 30 };
Expand All @@ -21,9 +22,10 @@ export class Terminal {
return this.#window;
}

constructor(window: AppWindow, cwd: string) {
constructor(window: AppWindow, cwd: string, uvPath: string) {
this.#window = window;
this.#cwd = cwd;
this.#uvPath = uvPath;
}

write(data: string) {
Expand All @@ -46,7 +48,7 @@ export class Terminal {
#createPty() {
const window = this.window;
// TODO: does this want to be a setting?
const shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash';
const shell = this.#getDefaultShell();
const instance = pty.spawn(shell, [], {
handleFlowControl: false,
conptyInheritCursor: false,
Expand All @@ -56,6 +58,14 @@ export class Terminal {
cwd: this.#cwd,
});

if (process.platform === 'win32') {
// PowerShell function
instance.write(`function pip { & "${this.#uvPath}" pip $args }\r\n`);
} else {
// Bash/Zsh alias
instance.write(`alias pip='"${this.#uvPath}" pip'\r\n`);
}

instance.onData((data) => {
this.sessionBuffer.push(data);
window.send(IPC_CHANNELS.TERMINAL_ON_OUTPUT, data);
Expand All @@ -64,4 +74,15 @@ export class Terminal {

return instance;
}

#getDefaultShell(): string {
switch (os.platform()) {
case 'win32':
return 'powershell.exe';
case 'darwin':
return 'zsh';
default: // Linux and others
return 'bash';
}
}
}

0 comments on commit be9a90a

Please sign in to comment.