-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoneapi.py
More file actions
1341 lines (1143 loc) · 54.1 KB
/
oneapi.py
File metadata and controls
1341 lines (1143 loc) · 54.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import json
import traceback
from typing import Sequence
import uuid
import time
import copy
import asyncio
import aiohttp
import tempfile
import mimetypes
from urllib.parse import urlparse
import base64
import random
from aiohttp import web
from server import PromptServer
import folder_paths
import execution
from workflow_format import adjust_workflow_format
from openapi_spec import OPENAPI_SPEC
from swagger_ui import SWAGGER_UI_HTML
# Constants
API_WORKFLOWS_DIR = 'api_workflows'
def _get_auth_headers(request):
"""Extract auth headers from the original request for internal API calls."""
headers = {}
if request:
auth = request.headers.get("Authorization")
if auth:
headers["Authorization"] = auth
cookie = request.headers.get("Cookie")
if cookie:
headers["Cookie"] = cookie
return headers
# Node types that require special media upload handling
MEDIA_UPLOAD_NODE_TYPES = {
'LoadImage',
'VHS_LoadAudioUpload',
'VHS_LoadVideo',
}
# Get routes
prompt_server = PromptServer.instance
routes = prompt_server.routes
# Get workflow paths
path_custom_nodes = os.path.dirname(os.path.dirname(__file__))
path_comfyui_root = os.path.dirname(path_custom_nodes)
path_workflows = os.path.join(path_comfyui_root, 'user/default/workflows')
@routes.get('/oneapi/docs')
async def swagger_ui(request):
"""
Swagger UI interface for API documentation and testing
"""
return web.Response(text=SWAGGER_UI_HTML, content_type='text/html')
@routes.get('/oneapi/openapi.json')
async def openapi_spec(request):
"""
OpenAPI specification JSON
"""
# 动态获取当前请求的地址,避免硬编码
protocol = request.scheme
host = request.host
base_url = f"{protocol}://{host}"
# 将规范中的 localhost 地址动态替换为当前访问地址
spec_json = json.dumps(OPENAPI_SPEC)
spec_json = spec_json.replace("http://localhost:8118", base_url)
spec_json = spec_json.replace("http://localhost:8188", base_url)
spec = json.loads(spec_json)
# 更新 servers 列表
spec['servers'] = [
{
"url": base_url,
"description": "当前 ComfyUI 服务器"
}
]
return web.json_response(spec)
@routes.post('/oneapi/v1/save-api-workflow')
async def save_api_workflow(request):
"""
Save API workflow to user's workflow directory.
"""
data = await request.json()
name = data.get('name')
workflow = data.get('workflow')
overwrite = data.get('overwrite', False)
if not name:
return web.json_response({"error": "Name is required"}, status=400)
if not workflow:
return web.json_response({"error": "Workflow is required"}, status=400)
fmt = adjust_workflow_format(workflow)
if fmt == 'invalid':
return web.json_response({"error": "Invalid workflow format"}, status=400)
if fmt == 'ui':
return web.json_response({"error": "UI format workflow is not supported. Please convert to API format and try again."}, status=400)
name_with_json = name if name.endswith('.json') else f'{name}.json'
relative_path = f'{API_WORKFLOWS_DIR}/{name_with_json}'
save_path = prompt_server.user_manager.get_request_user_filepath(request, relative_path, create_dir=True)
if not save_path:
return web.json_response({"error": "Failed to get save path"}, status=500)
if os.path.exists(save_path) and not overwrite:
return web.json_response({"error": "File already exists. Use overwrite=true to overwrite."}, status=400)
with open(save_path, 'w', encoding='utf-8') as f:
json.dump(workflow, f, indent=2, ensure_ascii=False)
return web.json_response({"message": "Workflow saved successfully", "filename": name_with_json})
@routes.post('/oneapi/v1/execute')
async def execute_workflow(request):
"""
Execute workflow API
Parameters:
- workflow: Workflow JSON, filename (under user/default/api_workflows/), or URL
- params: Parameter mapping dictionary
- wait_for_result: Whether to wait for results (default True)
- timeout: Timeout in seconds (default None)
- prompt_ext_params: Extra parameters for prompt request (optional)
Returns:
- Return values:
- status: Status (queued, processing, completed, timeout)
- prompt_id: Prompt ID
- images: List of image URLs [string, ...], only present if there are image results
- images_by_var: Mapped image URLs by variable name {var_name: [string, ...], ...}, only present if there are image results
- videos: List of video URLs [string, ...], only present if there are video results
- videos_by_var: Mapped video URLs by variable name {var_name: [string, ...], ...}, only present if there are video results
- audios: List of audio URLs [string, ...], only present if there are audio results
- audios_by_var: Mapped audio URLs by variable name {var_name: [string, ...], ...}, only present if there are audio results
- texts: List of text outputs [string, ...], only present if there are text results
- texts_by_var: Mapped text outputs by variable name {var_name: [string, ...], ...}, only present if there are text results
Node title markers:
- Input: Use "$param.field" in node title to map parameter values
- Output: Use "$output.name" in output node title to specify outputs
- "$output.name" - Marks an output node with a custom output name (added to "images_by_var[name]" or "videos_by_var[name]" or "texts_by_var[name]")
- If no explicit output marker is set, the node_id is used as the variable name
- Any node that produces outputs (images, videos, audios, texts) will be included in results
- images/images_by_var/videos/videos_by_var/audios/audios_by_var/texts/texts_by_var fields are only included if there are corresponding results
"""
try:
# Get request data
data = await request.json()
# Extract parameters
workflow = data.get('workflow')
params = data.get('params', {})
if 'seed' not in params:
params['seed'] = random.randint(1, 1125899906842624)
wait_for_result = data.get('wait_for_result', True)
timeout = data.get('timeout', None)
prompt_ext_params = data.get('prompt_ext_params', {})
# Support workflow as local path or URL
if isinstance(workflow, dict):
pass # Use directly
elif isinstance(workflow, str):
if workflow.startswith('http://') or workflow.startswith('https://'):
workflow = await _load_workflow_from_url(workflow)
else:
workflow = _load_workflow_from_local(workflow, request)
else:
return web.json_response({"error": "Invalid workflow parameter"}, status=400)
if not workflow:
return web.json_response({"error": "Workflow data is missing"}, status=400)
# Convert UI format to API format if needed
fmt = adjust_workflow_format(workflow)
if fmt == 'invalid':
return web.json_response({"error": "Invalid workflow format"}, status=400)
if fmt == 'ui':
return web.json_response({"error": "UI format workflow is not supported. Please convert to API format and try again."}, status=400)
# Process workflow parameters
if params:
workflow = await _apply_params_to_workflow(workflow, params, request)
# Extract and save output node information
output_id_2_var = await _extract_output_nodes(workflow)
# Generate client ID
client_id = str(uuid.uuid4())
# Submit workflow to ComfyUI queue
try:
prompt_id = await _queue_prompt(workflow, client_id, prompt_ext_params, request)
except Exception as e:
error_message = f"Failed to submit workflow: [{type(e)}] {str(e)}"
print(error_message)
return web.json_response({"error": error_message}, status=500)
result = {
"status": "queued",
"prompt_id": prompt_id,
"message": "Workflow submitted"
}
# If not waiting for results, return immediately
if not wait_for_result:
return web.json_response(result)
# Poll for results
result = await _wait_for_results(prompt_id, timeout, request, output_id_2_var)
return web.json_response(result)
except Exception as e:
print(f"Error executing workflow: {str(e)}, {traceback.format_exc()}")
return web.json_response({"error": str(e)}, status=500)
@routes.post('/oneapi/v1/models/{model}:generateContent')
@routes.post('/v1beta/models/{model}:generateContent')
@routes.post('/v1/models/{model}:generateContent')
async def generate_content(request):
"""
Open API compatible with Gemini generateContent format
Supports standard Gemini paths for easier integration
支持文生图和图生图两种模式
"""
try:
model = request.match_info.get('model', 'default')
data = await request.json()
# 1. 从 Gemini 格式提取内容 (支持多模态: 文本 + 图像)
contents = data.get('contents', [])
if not contents or not contents[0].get('parts'):
return web.json_response({"error": "Invalid contents/parts"}, status=400)
parts = contents[0].get('parts', [])
prompt_texts = []
image_data = None
# 检查是否有图像输入
for part in parts:
if 'text' in part:
prompt_texts.append(part['text'])
elif 'inlineData' in part:
# 获取 Base64 图像数据
mime_type = part['inlineData'].get('mimeType', 'image/png')
base64_data = part['inlineData'].get('data', '')
if base64_data:
image_data = f"data:{mime_type};base64,{base64_data}"
prompt_text = "\n".join(prompt_texts).strip()
if not prompt_text:
return web.json_response({"error": "Prompt text is empty"}, status=400)
# 2. 根据是否有图片输入选择工作流
workflow_name = model
if ':' in workflow_name:
workflow_name = workflow_name.split(':')[0]
# 如果有图片输入,使用图生图工作流;否则使用文生图工作流
if image_data:
workflow_name = f"{workflow_name}-TT" # 图生图工作流
print(f"[OneAPI] 检测到图片输入,使用图生图工作流: {workflow_name}")
else:
print(f"[OneAPI] 无图片输入,使用文生图工作流: {workflow_name}")
try:
workflow = _load_workflow_from_local(workflow_name, request)
except Exception as e:
msg = str(e)
# Fallback to try generic name if specific one fails
try:
fallback_name = 'default_txt2img-TT' if image_data else 'default_txt2img'
workflow = _load_workflow_from_local(fallback_name, request)
except:
return web.json_response({"error": f"Workflow '{workflow_name}' not found. {msg}"}, status=404)
# 3. 处理图像上传(如果有)
uploaded_filename = None
if image_data:
try:
# 解析 Base64 图像并上传
if image_data.startswith('data:'):
header, encoded = image_data.split(",", 1)
ext = mimetypes.guess_extension(header.split(';')[0].split(':')[1]) or '.png'
with tempfile.NamedTemporaryFile(delete=False, suffix=ext) as tmp:
tmp.write(base64.b64decode(encoded))
temp_path = tmp.name
try:
uploaded_filename = await _upload_media(temp_path, request)
print(f"[OneAPI] 图片上传成功: {uploaded_filename}")
finally:
os.unlink(temp_path)
except Exception as e:
print(f"[OneAPI] Error processing image: {str(e)}")
return web.json_response({"error": f"Failed to process image: {str(e)}"}, status=500)
# 4. Process with execute_workflow internal logic
# Extract aspectRatio and set width/height
generation_config = data.get('generationConfig', {})
image_config = generation_config.get('imageConfig', {})
aspect_ratio = image_config.get('aspectRatio', '16:9')
width, height = 1280, 720
if aspect_ratio == "9:16":
width, height = 720, 1280
params = {
"prompt": prompt_text,
"width": width,
"height": height,
"seed": random.randint(1, 1125899906842624)
}
# 如果有上传的图片,添加到参数中
if uploaded_filename:
params["image"] = uploaded_filename
workflow = await _apply_params_to_workflow(workflow, params, request)
output_id_2_var = await _extract_output_nodes(workflow)
client_id = str(uuid.uuid4())
prompt_id = await _queue_prompt(workflow, client_id, {}, request)
execution_result = await _wait_for_results(prompt_id, 300, request, output_id_2_var)
if execution_result.get('status') != 'completed':
return web.json_response({"error": f"Execution failed: {execution_result.get('status')}"}, status=500)
# 5. Construct Gemini response
parts = []
parts.append({"text": f"Successfully generated images for: {prompt_text}"})
images = execution_result.get('images', [])
for img_url in images:
img_data_base64 = await _fetch_image_base64(img_url, request)
if img_data_base64:
parts.append({
"inlineData": {
"mimeType": "image/png",
"data": img_data_base64
}
})
response_data = {
"candidates": [
{
"content": {
"role": "model",
"parts": parts
},
"finishReason": "STOP"
}
]
}
return web.json_response(response_data)
except Exception as e:
print(f"Error in generateContent: {str(e)}, {traceback.format_exc()}")
return web.json_response({"error": str(e)}, status=500)
async def _fetch_image_base64(url, request=None):
"""Fetch image and return base64 encoded string"""
try:
auth_headers = _get_auth_headers(request)
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=auth_headers) as response:
if response.status == 200:
data = await response.read()
return base64.b64encode(data).decode('utf-8')
except Exception as e:
print(f"Failed to fetch image for base64: {e}")
return None
@routes.post('/oneapi/v1/chat/completions')
@routes.post('/v1/chat/completions')
async def chat_completions(request):
"""
OpenAI 兼容接口 - 支持图生视频 / 多图工作流
请求格式:
{
"model": "LTX-more-image-3-3",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "描述"},
{"type": "image_url", "image_url": {"url": "data:image/png;base64,..."}},
{"type": "image_url", "image_url": {"url": "..."}}
]
}
]
}
"""
try:
data = await request.json()
model = data.get('model', 'LTX-more-image-2-3')
messages = data.get('messages', [])
if not messages:
return web.json_response({"error": "Messages are required"}, status=400)
# 1. 提取提示词和所有图片 (仅提取最后一条用户消息,防止被历史数据污染)
prompt_texts = []
image_data_list = []
# 反向查找最后一条用户发送的消息
last_user_message = None
for message in reversed(messages):
if message.get('role') == 'user':
last_user_message = message
break
# 如果没有用户消息,就直接取最后一条
if not last_user_message and messages:
last_user_message = messages[-1]
if last_user_message:
content = last_user_message.get('content', '')
if isinstance(content, str):
prompt_texts.append(content)
elif isinstance(content, list):
for part in content:
p_type = part.get('type', '')
if p_type == 'text' or 'text' in part:
txt = part.get('text', '')
if txt: prompt_texts.append(txt)
elif p_type == 'image_url' or 'image_url' in part:
image_info = part.get('image_url', {})
if isinstance(image_info, str):
image_data_list.append(image_info)
else:
url = image_info.get('url', '')
if url: image_data_list.append(url)
prompt_text = "\n".join(prompt_texts).strip()
print(f"[OneAPI] Processing OpenAI format request: model={model}, images={len(image_data_list)}")
if not prompt_text:
return web.json_response({"error": "Prompt text is empty"}, status=400)
# 2. 确定并加载工作流 (统一转为小写以匹配文件名)
workflow_name = (model.split(':')[0] if ':' in model else model).lower()
# 核心逻辑:根据传入图片数量动态映射 LTX 多图工作流 (全小写匹配)
if workflow_name.startswith('ltx-more-image-'):
num_pics = len(image_data_list)
# 仅在图片数量为 2-5 之间时进行智能转换
if 2 <= num_pics <= 5:
# 强制映射到对应数量的工作流文件 (如 ltx-more-image-3-3.json)
new_workflow_name = f"ltx-more-image-{num_pics}-3"
if new_workflow_name != workflow_name:
print(f"[OneAPI] 动态映射工作流: {workflow_name} -> {new_workflow_name} (图片数: {num_pics})")
workflow_name = new_workflow_name
try:
workflow = _load_workflow_from_local(workflow_name, request)
except Exception as e:
# Fallback to ltx2-swz if model name not found
try:
workflow = _load_workflow_from_local('ltx2-swz', request)
except:
return web.json_response({"error": f"Workflow '{workflow_name}' not found: {str(e)}"}, status=404)
# 3. 处理所有上传的图片
uploaded_filenames = []
for image_data in image_data_list:
uploaded_filename = None
if image_data.startswith('data:image'):
try:
header, encoded = image_data.split(',', 1)
image_bytes = base64.b64decode(encoded)
mime_type = header.split(';')[0].split(':')[1]
ext = mimetypes.guess_extension(mime_type) or '.png'
with tempfile.NamedTemporaryFile(delete=False, suffix=ext) as tmp:
tmp.write(image_bytes)
temp_path = tmp.name
try:
uploaded_filename = await _upload_media(temp_path, request)
finally:
if os.path.exists(temp_path):
os.unlink(temp_path)
except Exception as e:
print(f"Error processing base64 image: {e}")
raise Exception(f"Base64 image processing failed: {str(e)}")
elif image_data.startswith('http'):
try:
uploaded_filename = await _upload_media_from_source(image_data, request)
except Exception as e:
print(f"Error downloading image: {e}")
raise Exception(f"External image download failed: {str(e)}")
else:
uploaded_filename = image_data
if uploaded_filename:
uploaded_filenames.append(uploaded_filename)
else:
raise Exception(f"Failed to process image data index {len(uploaded_filenames)}")
# 4. 构建参数映射 ($param.text, $param.image1, $param.image2 ...)
# 增加顶层 seed 键,以支持工作流中常见的 $seed.seed 占位符解析
random_seed = random.randint(1, 1125899906842624)
params = {
"seed": random_seed,
"param": {
"text": prompt_text,
"seed": random_seed
}
}
# 统一处理图片插槽映射
num_uploaded = len(uploaded_filenames)
if num_uploaded > 0:
# 基本映射: $param.image 指向第一张或唯一一张
params["param"]["image"] = uploaded_filenames[0]
# 多图工作流映射: 映射到 image1, image2, image3, image4, image5
# 如果实际上传的图片不足,用最后一张图片进行“延长填充”,确保工作流不报错
for i in range(5):
slot_key = f"image{i+1}"
if i < num_uploaded:
params["param"][slot_key] = uploaded_filenames[i]
else:
# 填充最后一张,或者单图时 1 和 2 相同 (首尾对齐)
params["param"][slot_key] = uploaded_filenames[-1]
# 特殊逻辑:如果是 LTX 2图首尾模式,需要把 image2 映射到最后一张(即使有很多张)
if num_uploaded > 2:
# 这种情况下 image2 实际上可能是中间帧,在某些双图逻辑中建议显式保留尾帧
params["param"]["image_end"] = uploaded_filenames[-1]
print(f"[OneAPI] Params constructed for {num_uploaded} images: {list(params['param'].keys())}")
print(f"[OneAPI] Params applied: {list(params['param'].keys())}")
# 5. 执行工作流
workflow = await _apply_params_to_workflow(workflow, params, request)
# DEBUG: Save applied workflow for inspection
try:
debug_file = os.path.join(os.path.dirname(__file__), "debug_last_workflow.json")
with open(debug_file, "w", encoding="utf-8") as f:
json.dump(workflow, f, indent=2, ensure_ascii=False)
print(f"[DEBUG] Saved applied workflow to {debug_file}")
except: pass
output_id_2_var = await _extract_output_nodes(workflow)
client_id = str(uuid.uuid4())
prompt_id = await _queue_prompt(workflow, client_id, {}, request)
execution_result = await _wait_for_results(prompt_id, 1200, request, output_id_2_var)
if execution_result.get('status') != 'completed':
return web.json_response({"error": f"Execution failed: {execution_result.get('status')}"}, status=500)
# 6. 构建响应
videos = execution_result.get('videos', [])
audios = execution_result.get('audios', [])
images = execution_result.get('images', [])
# 构建文本内容 - 动态获取当前请求的完整地址 (协议+域名+端口)
protocol = request.scheme
host = request.host
base_url = f"{protocol}://{host}"
response_content = f"Generation complete."
if videos:
video_url = videos[0]
response_content += f"\nVideo: {video_url}"
if audios:
audio_url = audios[0]
response_content += f"\nAudio: {audio_url}"
if images and not videos:
img_url = images[0]
response_content += f"\nImage: {img_url}"
response_data = {
"id": f"chatcmpl-{uuid.uuid4()}",
"object": "chat.completion",
"created": int(time.time()),
"model": model,
"choices": [{
"index": 0,
"message": {"role": "assistant", "content": response_content},
"finish_reason": "stop"
}],
"usage": {"prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0}
}
return web.json_response(response_data)
except Exception as e:
error_msg = f"{str(e)}\n{traceback.format_exc()}"
print(f"Error in chatCompletions: {error_msg}")
return web.json_response({"error": error_msg}, status=500)
async def _fetch_image_base64(url, request=None):
"""Fetch image and return base64 encoded string"""
try:
auth_headers = _get_auth_headers(request)
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=auth_headers) as response:
if response.status == 200:
data = await response.read()
return base64.b64encode(data).decode('utf-8')
except Exception as e:
print(f"Failed to fetch image for base64: {e}")
return None
async def _apply_params_to_workflow(workflow, params, request=None):
"""
Apply parameters to the workflow
Handles two types of parameter mappings:
1. LoadImage node: $image_param
2. Regular node: $param.field
"""
workflow = copy.deepcopy(workflow)
print(f"[DEBUG] _apply_params_to_workflow called with {len(workflow)} nodes")
print(f"[DEBUG] params: {params}")
for node_id, node_data in workflow.items():
# Skip nodes that don't meet criteria
if not _is_valid_node(node_data):
print(f"[DEBUG] Skipping invalid node {node_id}")
continue
title = node_data.get("_meta", {}).get("title", "")
print(f"[DEBUG] Processing node {node_id}, title: {title}")
# Process parameter markers in the node
await _process_node_params(node_data, params, request)
print(f"[DEBUG] _apply_params_to_workflow completed")
return workflow
def _is_valid_node(node_data):
"""Check if node is valid and contains a title"""
return (isinstance(node_data, dict) and
"_meta" in node_data and
"title" in node_data["_meta"])
async def _process_node_params(node_data, params, request=None):
"""Process parameter markers in the node's inputs and title"""
if "inputs" not in node_data:
return
inputs = node_data["inputs"]
node_class_type = node_data.get('class_type', '')
# ==========================================
# 方式1:遍历 inputs,查找以 $ 开头的值
# ==========================================
for input_name, input_value in list(inputs.items()):
if not isinstance(input_value, str):
continue
new_value = await _resolve_placeholder(input_value, params, node_class_type, node_data, input_name, request)
if new_value is not None:
inputs[input_name] = new_value
# ==========================================
# 方式2:处理 title 中的占位符(旧格式兼容)
# ==========================================
if "_meta" in node_data and "title" in node_data["_meta"]:
title = node_data["_meta"]["title"]
parts = title.split(',')
for part in parts:
part = part.strip()
if not part.startswith('$'):
continue
# 解析 $xxx.yyy 格式
if '.' not in part:
continue
var_name, input_field = part[1:].split('.', 1)
# 从 params 中获取值
param_value = None
if var_name in params:
param_container = params[var_name]
if isinstance(param_container, dict) and input_field in param_container:
param_value = param_container[input_field]
elif not isinstance(param_container, dict):
param_value = param_container
if param_value is not None:
print(f"[DEBUG] Title marker ${var_name}.{input_field} -> {param_value}")
# 安全保护:如果 input_field 不在结点的原生输入中,且结点是 LoadImage 类型,
# 说明占位符意在指定图片,我们应该寻找 'image' 字段
final_field = input_field
if final_field not in inputs and node_class_type in MEDIA_UPLOAD_NODE_TYPES:
if 'image' in inputs:
final_field = 'image'
print(f"[DEBUG] Fallback {input_field} to 'image' for media node")
else:
# 如果连 image 都没有,且字段名看起来像多图插槽,不要盲目添加新字段
if input_field.startswith('image'):
print(f"[DEBUG] Skip unknown field {input_field}")
# 让 _resolve_placeholder 处理输入
new_value = await _resolve_placeholder(input_value, params, node_class_type, node_data, input_name, request)
if new_value is not None:
param_value = new_value
# 处理参数标记
if not title:
continue
# 特殊逻辑:种子节点的字段往往不叫 seed,尝试常见变体
if var_name == 'seed' or input_field == 'seed':
if final_field not in inputs:
for alt in ['noise_seed', 'seed_value', 'seed']:
if alt in inputs:
final_field = alt
break
if node_class_type in MEDIA_UPLOAD_NODE_TYPES:
await _handle_media_upload(node_data, final_field, param_value, request)
else:
inputs[final_field] = param_value
async def _resolve_placeholder(input_value, params, node_class_type, node_data, input_name, request):
"""Resolve a placeholder value and return the new value, or None if not a placeholder"""
# 必须是字符串才可能是占位符
if not isinstance(input_value, str):
return None
# 处理 $param.xxx 格式
if input_value.startswith('$param.'):
param_key = input_value[7:] # 去掉 '$param.' 前缀
print(f"[DEBUG] Found $param.{param_key} in input '{input_name}'")
if "param" in params and param_key in params["param"]:
new_value = params["param"][param_key]
print(f"[DEBUG] Replacing with: {new_value}")
if node_class_type in MEDIA_UPLOAD_NODE_TYPES:
await _handle_media_upload(node_data, input_name, new_value, request)
return None # _handle_media_upload 已经设置了值
return new_value
else:
print(f"[DEBUG] ❌ param key '{param_key}' not found in params['param']")
# 处理 $seed.xxx 格式
elif input_value.startswith('$seed.'):
seed_key = input_value[6:]
print(f"[DEBUG] Found $seed.{seed_key} in input '{input_name}'")
if "seed" in params:
print(f"[DEBUG] Replacing with seed: {params['seed']}")
return params["seed"]
elif "param" in params and "seed" in params["param"]:
print(f"[DEBUG] Replacing with param.seed: {params['param']['seed']}")
return params["param"]["seed"]
else:
print(f"[DEBUG] ❌ seed not found in params")
# 处理 $image.xxx 格式
elif input_value.startswith('$image.'):
image_key = input_value[7:]
print(f"[DEBUG] Found $image.{image_key} in input '{input_name}'")
if "image" in params:
new_value = params["image"]
print(f"[DEBUG] Replacing with image: {new_value}")
if node_class_type in MEDIA_UPLOAD_NODE_TYPES:
await _handle_media_upload(node_data, input_name, new_value, request)
return None
return new_value
elif "param" in params and "image" in params["param"]:
new_value = params["param"]["image"]
print(f"[DEBUG] Replacing with param.image: {new_value}")
if node_class_type in MEDIA_UPLOAD_NODE_TYPES:
await _handle_media_upload(node_data, input_name, new_value, request)
return None
return new_value
else:
print(f"[DEBUG] ❌ image not found in params")
return None
async def _extract_output_nodes(workflow):
"""
Extract output nodes and their output variable names from workflow
Args:
workflow: Workflow JSON object
Returns:
Dictionary mapping node_id to output variable name (only for nodes with explicit $output.name markers)
Note:
Any node that produces outputs will be included in results.
Output fields (images, videos, texts, *_by_var) are only included in the response if there are corresponding results.
"""
output_id_2_var = {}
for node_id, node_data in workflow.items():
# Skip nodes that don't meet criteria
if not _is_valid_node(node_data):
continue
# Get node title
title = node_data["_meta"]["title"]
# Check for $output marker in the title
output_var = None
parts = title.split(',')
for part in parts:
part = part.strip()
if part.startswith('$output'):
# Parse output marker
if '.' in part:
# Format: $output.name - Specify output name
output_var = part.split('.', 1)[1]
if not output_var:
raise Exception(f"Invalid output marker format (empty name): {part}")
else:
# Simple $output without variable name is not valid
raise Exception(f"Invalid output marker format (missing name): {part}. Use $output.name format.")
# Only register in output_id_2_var if there's an explicit output marker
if output_var:
output_id_2_var[node_id] = output_var
return output_id_2_var
async def _process_param_marker(node_data, var_spec, params, request=None):
"""
Process individual parameter marker
Format: param_name.field_name
- param_name: Parameter name, corresponding to key in params
- field_name: Node input field name
Special handling for media upload node types defined in MEDIA_UPLOAD_NODE_TYPES
"""
# Must have field separator
if '.' not in var_spec:
print(f"Parameter marker format error, should be '$param.field': {var_spec}")
return
# Parse parameter name and field name
var_name, input_field = var_spec.split('.', 1)
print(f"[DEBUG] Processing marker: ${var_spec}")
print(f"[DEBUG] var_name={var_name}, input_field={input_field}")
print(f"[DEBUG] Available params keys: {list(params.keys())}")
# Check if parameter exists
if var_name not in params:
print(f"[DEBUG] ❌ var_name '{var_name}' not found in params")
return
# Get parameter value
# 支持嵌套结构:如果 params[var_name] 是字典,则从中提取 input_field
param_container = params[var_name]
print(f"[DEBUG] param_container type: {type(param_container)}")
if isinstance(param_container, dict):
# 嵌套结构:params["param"]["image"]
print(f"[DEBUG] param_container keys: {list(param_container.keys())}")
if input_field not in param_container:
print(f"[DEBUG] ❌ input_field '{input_field}' not found in param_container")
return
param_value = param_container[input_field]
print(f"[DEBUG] ✅ Found param_value: {param_value}")
else:
# 扁平结构(向后兼容)
param_value = param_container
print(f"[DEBUG] ✅ Using param_container as value: {param_value}")
# Check if this node type requires special media upload handling
node_class_type = node_data.get('class_type')
if node_class_type in MEDIA_UPLOAD_NODE_TYPES:
await _handle_media_upload(node_data, input_field, param_value, request)
else:
# Regular parameter setting
await _set_node_param(node_data, input_field, param_value)
async def _handle_media_upload(node_data, input_field, param_value, request=None):
"""
Handle media upload for nodes in MEDIA_UPLOAD_NODE_TYPES
Args:
node_data: Node data
input_field: Input field name
param_value: Parameter value
request: HTTP request object for getting server URL
"""
# Ensure inputs exists
if "inputs" not in node_data:
node_data["inputs"] = {}
# If parameter value is a URL starting with http, upload the media first
if isinstance(param_value, str) and param_value.startswith(('http://', 'https://')):
try:
# Upload media and get uploaded media name
media_value = await _upload_media_from_source(param_value, request)
# Use uploaded media name as node's input value
await _set_node_param(node_data, input_field, media_value)
print(f"Media uploaded: {media_value}")
except Exception as e:
print(f"Failed to upload media: {str(e)}")
# Throw exception on upload failure
raise Exception(f"Media upload failed: {str(e)}")
else:
# Use parameter value directly as media name
await _set_node_param(node_data, input_field, param_value)
async def _set_node_param(node_data, input_field, param_value):
"""
Set node parameter
Args:
node_data: Node data
input_field: Input field name
param_value: Parameter value
"""
# Ensure inputs exists
if "inputs" not in node_data:
node_data["inputs"] = {}
# Set parameter value
node_data["inputs"][input_field] = param_value
async def _upload_media_from_source(media_url, request=None) -> str:
"""
Upload media from URL
Args:
media_url: Media URL
request: HTTP request object for getting server URL
Returns:
Upload media file name
"""
# Download media from URL
async with aiohttp.ClientSession() as session:
async with session.get(media_url) as response:
if response.status != 200:
raise Exception(f"Failed to download media: HTTP {response.status}")
# Extract filename from URL
parsed_url = urlparse(media_url)
filename = os.path.basename(parsed_url.path)
if not filename:
filename = f"temp_media_{hash(media_url)}.jpg"
# Get media data
media_data = await response.read()
# Save to temporary file
suffix = os.path.splitext(filename)[1] or ".jpg"
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp:
tmp.write(media_data)
temp_path = tmp.name
try:
# Upload temporary file to ComfyUI
return await _upload_media(temp_path, request)
finally:
# Delete temporary file
os.unlink(temp_path)
async def _upload_media(media_path, request=None) -> str:
"""
Upload media to ComfyUI
Args:
media_path: Media path
request: HTTP request object for getting server URL
Returns:
Upload media file name
"""
# Read media data
with open(media_path, 'rb') as f:
media_data = f.read()
# Extract filename
filename = os.path.basename(media_path)