mirror of
https://github.com/open-webui/open-webui.git
synced 2025-12-12 12:25:20 +00:00
refac
This commit is contained in:
parent
31dc97b68b
commit
90f76d24ec
2 changed files with 37 additions and 15 deletions
|
|
@ -297,6 +297,27 @@ class ChatTable:
|
||||||
chat["history"] = history
|
chat["history"] = history
|
||||||
return self.update_chat_by_id(id, chat)
|
return self.update_chat_by_id(id, chat)
|
||||||
|
|
||||||
|
def add_message_files_by_id_and_message_id(
|
||||||
|
self, id: str, message_id: str, files: list[dict]
|
||||||
|
) -> list[dict]:
|
||||||
|
chat = self.get_chat_by_id(id)
|
||||||
|
if chat is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
chat = chat.chat
|
||||||
|
history = chat.get("history", {})
|
||||||
|
|
||||||
|
message_files = []
|
||||||
|
|
||||||
|
if message_id in history.get("messages", {}):
|
||||||
|
message_files = history["messages"][message_id].get("files", [])
|
||||||
|
message_files = message_files + files
|
||||||
|
history["messages"][message_id]["files"] = message_files
|
||||||
|
|
||||||
|
chat["history"] = history
|
||||||
|
self.update_chat_by_id(id, chat)
|
||||||
|
return message_files
|
||||||
|
|
||||||
def insert_shared_chat_by_chat_id(self, chat_id: str) -> Optional[ChatModel]:
|
def insert_shared_chat_by_chat_id(self, chat_id: str) -> Optional[ChatModel]:
|
||||||
with get_db() as db:
|
with get_db() as db:
|
||||||
# Get the existing chat to share
|
# Get the existing chat to share
|
||||||
|
|
|
||||||
|
|
@ -742,11 +742,11 @@ def get_last_images(message_list):
|
||||||
return images
|
return images
|
||||||
|
|
||||||
|
|
||||||
def extract_and_process_urls(delta_images, request, metadata, user) -> list[str]:
|
def get_image_urls(delta_images, request, metadata, user) -> list[str]:
|
||||||
if not isinstance(delta_images, list):
|
if not isinstance(delta_images, list):
|
||||||
return []
|
return []
|
||||||
|
|
||||||
processed_urls = []
|
image_urls = []
|
||||||
for img in delta_images:
|
for img in delta_images:
|
||||||
if not isinstance(img, dict) or img.get("type") != "image_url":
|
if not isinstance(img, dict) or img.get("type") != "image_url":
|
||||||
continue
|
continue
|
||||||
|
|
@ -758,9 +758,9 @@ def extract_and_process_urls(delta_images, request, metadata, user) -> list[str]
|
||||||
if url.startswith("data:image/png;base64"):
|
if url.startswith("data:image/png;base64"):
|
||||||
url = get_image_url_from_base64(request, url, metadata, user)
|
url = get_image_url_from_base64(request, url, metadata, user)
|
||||||
|
|
||||||
processed_urls.append(url)
|
image_urls.append(url)
|
||||||
|
|
||||||
return processed_urls
|
return image_urls
|
||||||
|
|
||||||
|
|
||||||
async def chat_image_generation_handler(
|
async def chat_image_generation_handler(
|
||||||
|
|
@ -2602,22 +2602,23 @@ async def process_chat_response(
|
||||||
"arguments"
|
"arguments"
|
||||||
] += delta_arguments
|
] += delta_arguments
|
||||||
|
|
||||||
processed_image_urls = extract_and_process_urls(
|
image_urls = get_image_urls(
|
||||||
delta.get("images", []), request, metadata, user
|
delta.get("images", []), request, metadata, user
|
||||||
)
|
)
|
||||||
if processed_image_urls:
|
if image_urls:
|
||||||
|
message_files = Chats.add_message_files_by_id_and_message_id(
|
||||||
|
metadata["chat_id"],
|
||||||
|
metadata["message_id"],
|
||||||
|
[
|
||||||
|
{"type": "image", "url": url}
|
||||||
|
for url in image_urls
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
await event_emitter(
|
await event_emitter(
|
||||||
{
|
{
|
||||||
"type": "files",
|
"type": "files",
|
||||||
"data": {
|
"data": {"files": message_files},
|
||||||
"files": [
|
|
||||||
{
|
|
||||||
"type": "image",
|
|
||||||
"url": url,
|
|
||||||
}
|
|
||||||
for url in processed_image_urls
|
|
||||||
]
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue