diff --git a/package-lock.json b/package-lock.json index 83f7e73..c522ead 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "comfyui-wrapper", - "version": "1.3.1", + "version": "1.3.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "comfyui-wrapper", - "version": "1.3.1", + "version": "1.3.2", "license": "MIT", "dependencies": { "@fastify/swagger": "^8.15.0", diff --git a/package.json b/package.json index 4624438..f032a0e 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "build": "tsc", - "build-binary": "tsc && pkg -t node18-linux-x64 --out-path bin . && cp src/*.py bin/" + "build-binary": "tsc && pkg -t node18-linux-x64 --out-path bin ." }, "author": "Shawn Rushefsky", "license": "MIT", diff --git a/src/config.ts b/src/config.ts index a6711d1..7fb0a74 100644 --- a/src/config.ts +++ b/src/config.ts @@ -49,7 +49,32 @@ interface ComfyDescription { schedulers: string[]; } +function getPythonCommand(): string { + let pythonCommand = execSync( + "source /opt/ai-dock/etc/environment.sh && which python3", + { + encoding: "utf-8", + } + ).trim(); + if (!pythonCommand) { + pythonCommand = execSync( + "source /opt/ai-dock/etc/environment.sh && which python", + { + encoding: "utf-8", + } + ).trim(); + } + if (!pythonCommand) { + throw new Error("Python not found"); + } + return pythonCommand; +} + function getComfyUIDescription(): ComfyDescription { + const temptComfyFilePath = path.join( + "/opt/ComfyUI", + "temp_comfy_description.json" + ); const pythonCode = ` import comfy.samplers import json @@ -59,23 +84,32 @@ comfy_description = { "schedulers": comfy.samplers.KSampler.SCHEDULERS, } -print(json.dumps(comfy_description, indent=2)) +with open("${temptComfyFilePath}", "w") as f: + json.dump(comfy_description, f) `; const tempFilePath = path.join("/opt/ComfyUI", "temp_comfy_description.py"); + const command = ` + source /opt/ai-dock/etc/environment.sh \ + && source /opt/ai-dock/bin/venv-set.sh comfyui \ + && source "$COMFYUI_VENV/bin/activate" \ + && python ${tempFilePath}`; try { // Write the Python code to a temporary file fs.writeFileSync(tempFilePath, pythonCode); // Execute the Python script synchronously - const output = execSync(`python ${tempFilePath}`, { + execSync(command, { cwd: "/opt/ComfyUI", encoding: "utf-8", + shell: process.env.SHELL, + env: { + ...process.env, + }, }); - - // Parse and return the JSON output - return JSON.parse(output) as ComfyDescription; + const output = fs.readFileSync(temptComfyFilePath, { encoding: "utf-8" }); + return JSON.parse(output.trim()) as ComfyDescription; } catch (error: any) { throw new Error(`Failed to get ComfyUI description: ${error.message}`); } finally { diff --git a/src/server.ts b/src/server.ts index ee445b8..5378634 100644 --- a/src/server.ts +++ b/src/server.ts @@ -65,6 +65,9 @@ server.register(fastifySwagger, { }); server.register(fastifySwaggerUI, { routePrefix: "/docs", + uiConfig: { + deepLinking: true, + }, }); server.after(() => { @@ -340,6 +343,9 @@ export async function start() { // Start the server await server.listen({ port: config.wrapperPort, host: config.wrapperHost }); + server.log.info( + `ComfyUI API ${version} listening on ${server.server.address}` + ); await warmupComfyUI(); warm = true; const warmupTime = Date.now() - start; diff --git a/src/utils.ts b/src/utils.ts index f68f1ad..2acdbf1 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -3,6 +3,8 @@ import { FastifyBaseLogger } from "fastify"; import { CommandExecutor } from "./commands"; import fs from "fs"; import fsPromises from "fs/promises"; +import { spawn } from "child_process"; +import * as readline from "readline"; import { Readable } from "stream"; import path from "path"; import { randomUUID } from "crypto";