A model whose profiles run engine comfyui is placed on a node's card like any other, and makes pictures there. ComfyUI asks for no key, so the agent starts it behind llmgw comfyui-gate: ComfyUI on the loopback, the gate on the replica's port demanding the replica's key and letting through only what the router asks — queueing, waiting, fetching, cancelling. The gate answers the agent's /health and /v1/models, shows ComfyUI the fetched weights by their own names, and gives each replica its own base directory. ComfyUI runs as the gate's child, so its card memory is the replica's. A node offers the engine when agent.yaml names its ComfyUI (engines.comfyui.dir). The model's comfyui: block names the workflow and, for a source that is a set of files, the checkpoint. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
125 lines
3.8 KiB
Go
125 lines
3.8 KiB
Go
package engines
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strconv"
|
|
"sync/atomic"
|
|
)
|
|
|
|
// ComfyUIConfig locates a ComfyUI installation.
|
|
type ComfyUIConfig struct {
|
|
// Dir is ComfyUI's directory, where main.py is. Readable by the user
|
|
// engines run as.
|
|
Dir string `koanf:"dir"`
|
|
// Python is the interpreter of its environment; Dir's .venv when unset.
|
|
Python string `koanf:"python"`
|
|
}
|
|
|
|
// ComfyUI runs ComfyUI as an engine, behind llmgw's gate (llmgw
|
|
// comfyui-gate): ComfyUI asks for no key, so it listens on the loopback and
|
|
// the gate, which does, on the replica's port. Its replicas make pictures
|
|
// for /v1/images/generations.
|
|
type ComfyUI struct {
|
|
cfg ComfyUIConfig
|
|
installed atomic.Pointer[string]
|
|
}
|
|
|
|
// Name identifies the engine in the catalog.
|
|
func (*ComfyUI) Name() string { return "comfyui" }
|
|
|
|
// HealthPath is the gate's, which answers once ComfyUI does.
|
|
func (*ComfyUI) HealthPath() string { return "/health" }
|
|
|
|
// MetricsPath is empty: ComfyUI publishes no metrics, and the router reads
|
|
// none from it.
|
|
func (*ComfyUI) MetricsPath() string { return "" }
|
|
|
|
// ServesHTTP is true: a ComfyUI replica is one process on one node.
|
|
func (*ComfyUI) ServesHTTP(ShardSpec) bool { return true }
|
|
|
|
// UseInstalled points the engine at a ComfyUI directory the agent installed.
|
|
func (c *ComfyUI) UseInstalled(dir string) { c.installed.Store(&dir) }
|
|
|
|
// Configured reports whether the configuration names a ComfyUI.
|
|
func (c *ComfyUI) Configured() bool { return c.cfg.Dir != "" }
|
|
|
|
func (c *ComfyUI) dir() string {
|
|
if p := c.installed.Load(); p != nil {
|
|
return *p
|
|
}
|
|
return c.cfg.Dir
|
|
}
|
|
|
|
func (c *ComfyUI) python() string {
|
|
if c.cfg.Python != "" && c.installed.Load() == nil {
|
|
return c.cfg.Python
|
|
}
|
|
return filepath.Join(c.dir(), ".venv", "bin", "python")
|
|
}
|
|
|
|
var comfyVersion = regexp.MustCompile(`__version__\s*=\s*"([^"]+)"`)
|
|
|
|
// Probe reports ComfyUI's version, from the file its build writes it to:
|
|
// importing ComfyUI to ask would load torch and look for cards.
|
|
func (c *ComfyUI) Probe(context.Context) (string, error) {
|
|
dir := c.dir()
|
|
if dir == "" {
|
|
return "", fmt.Errorf("no ComfyUI directory is configured")
|
|
}
|
|
if _, err := os.Stat(filepath.Join(dir, "main.py")); err != nil {
|
|
return "", fmt.Errorf("%s is not ComfyUI: %w", dir, err)
|
|
}
|
|
b, err := os.ReadFile(filepath.Join(dir, "comfyui_version.py"))
|
|
if err != nil {
|
|
return Unknown, nil //nolint:nilerr // installed, of a version it does not say
|
|
}
|
|
if m := comfyVersion.FindSubmatch(b); m != nil {
|
|
return string(m[1]), nil
|
|
}
|
|
return Unknown, nil
|
|
}
|
|
|
|
// Validate checks the profile: one card, on one node. ComfyUI runs a
|
|
// workflow on one device and has no tensor parallelism to spread it by.
|
|
func (*ComfyUI) Validate(p Profile, gpus []GPUInfo) error {
|
|
if p.Nodes > 1 || p.TP > 1 || p.PP > 1 || p.GPUsPerNode > 1 {
|
|
return fmt.Errorf("profile %q: ComfyUI runs on one GPU of one node", p.Name)
|
|
}
|
|
return validateCommon(p, gpus)
|
|
}
|
|
|
|
// Commands is the gate, which starts ComfyUI itself.
|
|
func (c *ComfyUI) Commands(s ShardSpec) ([]Command, error) {
|
|
dir := c.dir()
|
|
if dir == "" {
|
|
return nil, fmt.Errorf("comfyui: no ComfyUI directory is configured (engines.comfyui.dir)")
|
|
}
|
|
bin, err := os.Executable()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
p := s.Profile
|
|
args := []string{"comfyui-gate",
|
|
"--listen", net.JoinHostPort(s.Host(), strconv.Itoa(s.HTTPPort)),
|
|
"--model", p.ServedModelName,
|
|
"--weights", p.ModelPath,
|
|
// Relative to where engines run — their user's home — so it is
|
|
// theirs to write.
|
|
"--state", filepath.Join("comfyui", s.ReplicaID),
|
|
"--python", c.python(),
|
|
"--comfyui", dir,
|
|
"--",
|
|
}
|
|
args = append(args, p.Args...)
|
|
env := baseEnv(s)
|
|
if s.APIKey != "" {
|
|
env["LLMGW_ENGINE_KEY"] = s.APIKey
|
|
}
|
|
return []Command{{Name: "comfyui", Path: bin, Args: args, Env: materializeEnv(env)}}, nil
|
|
}
|