Repository: AUTOMATIC1111/stable-diffusion-webui

Issue:
[Bug]: Cannot load ControlNet Module with API /sdapi/v1/img2img
### Is there an existing issue for this?

- [X] I have searched the existing issues and checked the recent builds/commits

### What happened?

Cannot load ControlNet Module with API /sdapi/v1/img2img

### Steps to reproduce the problem

1. Call API /sdapi/v1/img2img with "controlnet" in "alwayson_script" as described in https://github.com/Mikubill/sd-webui-controlnet/wiki/API#web-api   
2. Produces following error and continue to do img2img without controlNet module.

Error running process: E:\Workspace\stable-diffusion-webui\extensions\sd-webui-controlnet\scripts\controlnet.py
Traceback (most recent call last):
  File "E:\Workspace\stable-diffusion-webui\modules\scripts.py", line 417, in process
    script.process(p, *script_args)
  File "E:\Workspace\stable-diffusion-webui\extensions\sd-webui-controlnet\scripts\controlnet.py", line 628, in process
    unit = self.parse_remote_call(p, unit, idx)
  File "E:\Workspace\stable-diffusion-webui\extensions\sd-webui-controlnet\scripts\controlnet.py", line 540, in parse_remote_call
    unit.enabled = selector(p, "control_net_enabled", unit.enabled, idx, strict=True)
AttributeError: 'str' object has no attribute 'enabled'

### What should have happened?

Load ControlNet Module in used with img2img

### Commit where the problem happens

c7daba71

### What platforms do you use to access the UI ?

Windows

### What browsers do you use to access the UI ?

Google Chrome

### Command Line Arguments

```Shell
--xformers --api --listen
```


### List of extensions

sd-webui-controlnet

### Console logs

```Shell
Error running process: E:\Workspace\stable-diffusion-webui\extensions\sd-webui-controlnet\scripts\controlnet.py
Traceback (most recent call last):
  File "E:\Workspace\stable-diffusion-webui\modules\scripts.py", line 417, in process
    script.process(p, *script_args)
  File "E:\Workspace\stable-diffusion-webui\extensions\sd-webui-controlnet\scripts\controlnet.py", line 628, in process
    unit = self.parse_remote_call(p, unit, idx)
  File "E:\Workspace\stable-diffusion-webui\extensions\sd-webui-controlnet\scripts\controlnet.py", line 540, in parse_remote_call
    unit.enabled = selector(p, "control_net_enabled", unit.enabled, idx, strict=True)
AttributeError: 'str' object has no attribute 'enabled'
```


### Additional information

After revert commit c7daba71, the problem gone.


Discussion:
To me, while this is linked to the recent PR, this seems to belong in sd-webui-controlnet repo issues unless you can replicate this type of error on other extensions. ~I'd open an issue on their repo and include the commit of the webui and of the commit used for the controlnet extension so they can have a look at how the changes affected them~. https://github.com/Mikubill/sd-webui-controlnet/issues/675

 That said, the API tests that I ran for controlnet passed after the refactor of the PR, but I'll take a look when time permits.
Thanks for the investigation. For your information, this issue is resolved once I revert your commit.  Also, this problem does not exist for txt2img.  So I guess it may be missing to pass the args unit to controlnet in this commit. 

Resolving patch:
diff --git a/modules/api/api.py b/modules/api/api.py
--- a/modules/api/api.py
+++ b/modules/api/api.py
@@ -272,7 +272,9 @@ def init_script_args(self, request, default_script_args, selectable_scripts, sel
                     raise HTTPException(status_code=422, detail=f"Cannot have a selectable script in the always on scripts params")
                 # always on script with no arg should always run so you don't really need to add them to the requests
                 if "args" in request.alwayson_scripts[alwayson_script_name]:
-                    script_args[alwayson_script.args_from:alwayson_script.args_to] = request.alwayson_scripts[alwayson_script_name]["args"]
+                    # min between arg length in scriptrunner and arg length in the request
+                    for idx in range(0, min((alwayson_script.args_to - alwayson_script.args_from), len(request.alwayson_scripts[alwayson_script_name]["args"]))):
+                        script_args[alwayson_script.args_from + idx] = request.alwayson_scripts[alwayson_script_name]["args"][idx]
         return script_args
 
     def text2imgapi(self, txt2imgreq: StableDiffusionTxt2ImgProcessingAPI):


Test patch: