StableCanvas / comfyui-client

all ComfyUI http/ws APIs. support NodeJS / Browser environments. and advanced programmable workflow.
https://stablecanvas.github.io/comfyui-client/
MIT License
60 stars 10 forks source link

Support Non-Image Return Types #10

Closed jelling closed 1 month ago

jelling commented 1 month ago

Currently, ComfyUIApiClient calls getPromptImageResult at the end of the inference. But Comfy can be used for non-image inference as well as image inference.

Feature request: support optional generic type for return type.

My hacky temporary workaround:

  async getPromptImageResult(prompt_id: string): Promise<WorkflowOutput> {
    const outputs = await this.getPromptOutputs(prompt_id);

    // New start
    const outputObjs = Object.values(outputs) as any[];

    if (!outputObjs[0].images) {
      return outputs;
    }
    // New end

    // find image from history
    const images: {
      filename?: string;
      subfolder?: string;
      type: string;
    }[] = Object.values(outputs).flatMap((node_output) => {
      return (node_output as any).images || [];
    });
    const images_url = images
      .map((image) => {
        const { filename, subfolder, type } = image;
        if (isNone(filename) || isNone(subfolder) || type !== 'output') {
          return null;
        }
        return this.viewURL(filename, subfolder, type);
      })
      .filter(Boolean) as string[];
    return {
      images: images_url.map((data) => ({ type: 'url', data })),
      prompt_id,
    };
  }
zhzLuke96 commented 1 month ago

You're right, we didn't consider this scenario. Can you share more about your use case? What kind of ComfyUI custom nodes are you using?

I'm guessing either:

  1. Some outputs are causing errors
  2. You need a custom result-fetcher to get specific values from the outputs

More details would help us figure out the best way to implement this feature. Your workaround looks good for now - we'll think about how to make this more flexible while keeping things backwards compatible.

zhzLuke96 commented 1 month ago

I've added a small update with a custom resolver; hopefully, this will cover your needs. If you encounter any issues, feel free to provide further feedback.

Additionally, I've included a brief explanation in the README: https://github.com/StableCanvas/comfyui-client?tab=readme-ov-file#custom-resolver

jelling commented 1 month ago

Thanks for the quick fix. My use case is pretty simple in that I'm outputting videos. But I'm also starting to see people using Comfy for multi-agent LLM workflows.

jelling commented 1 month ago

I spoke too soon. I see the fix for the enqueue function but I'm using invoke_polling:

  const res = await wf.invoke_polling(comfyClient());
zhzLuke96 commented 1 month ago

okey, thx feedback

added https://github.com/StableCanvas/comfyui-client/commit/a4c559bb28b3cf13f2f77b48205dc4adeb3f25a6

I've added the workflow functionality and updated the README.