Fix #19750 : Handle reference images correctly in image edit

- Modified get_last_images() to distinguish between user-uploaded and assistant-generated images
- Fixes bug where uploading a reference image after generation would ignore the previously generated image
- Assistant images are now prioritized for editing with user references appended
This commit is contained in:
eml-henn 2025-12-04 17:18:32 +01:00
parent ce945a9334
commit aa806d12d0

View file

@ -716,20 +716,37 @@ async def chat_web_search_handler(
return form_data
def get_last_images(message_list):
def get_last_user_images(message_list):
"""Get all images from the last USER message only"""
images = []
for message in reversed(message_list):
images_flag = False
if message.get("role") == "user":
for file in message.get("files", []):
if file.get("type") == "image":
images.append(file.get("url"))
images_flag = True
if images_flag:
break
break # Only from most recent user message
return images
def get_last_assistant_image(message_list):
"""Get the most recent ASSISTANT-generated image"""
for message in reversed(message_list):
if message.get("role") == "assistant":
for file in message.get("files", []):
if file.get("type") == "image":
return file.get("url")
return None
def get_last_images(message_list):
assistant_image = get_last_assistant_image(message_list)
user_images = get_last_user_images(message_list)
if assistant_image:
return [assistant_image] + user_images
return user_images
def get_image_urls(delta_images, request, metadata, user) -> list[str]:
if not isinstance(delta_images, list):