fix: Update emoji and sticker handling to download data as inline_data instead of FileData

This commit is contained in:
Slipstream 2025-05-28 16:49:01 -06:00
parent 7b4a3e8abc
commit c2b10eae42
Signed by: slipstream
GPG Key ID: 13E498CE010AC6FD

View File

@ -877,11 +877,18 @@ async def get_ai_response(cog: 'GurtCog', message: discord.Message, model_name:
is_animated_emoji = emoji_info.get("animated", False)
emoji_mime_type = "image/gif" if is_animated_emoji else "image/png"
try:
parts.append(types.Part(file_data=types.FileData(mime_type=emoji_mime_type, file_uri=emoji_url)))
print(f"Added FileData part for historical emoji: {emoji_name} (MIME: {emoji_mime_type}, URL: {emoji_url})")
# Download emoji data and send as inline_data
async with cog.session.get(emoji_url, timeout=10) as response:
if response.status == 200:
emoji_bytes = await response.read()
parts.append(types.Part(inline_data=types.Blob(data=emoji_bytes, mime_type=emoji_mime_type)))
print(f"Added inline_data part for historical emoji: {emoji_name} (MIME: {emoji_mime_type}, {len(emoji_bytes)} bytes)")
else:
print(f"Error downloading historical emoji {emoji_name} from {emoji_url}: Status {response.status}")
text_parts.append(f"[System Note: Failed to download emoji '{emoji_name}']")
except Exception as e:
print(f"Error creating FileData for historical emoji {emoji_name} from {emoji_url}: {e}")
text_parts.append(f"[System Note: Failed to attach emoji '{emoji_name}' as FileData]")
print(f"Error downloading/processing historical emoji {emoji_name} from {emoji_url}: {e}")
text_parts.append(f"[System Note: Failed to process emoji '{emoji_name}']")
cached_stickers = msg.get("stickers", [])
for sticker_info in cached_stickers:
@ -893,16 +900,23 @@ async def get_ai_response(cog: 'GurtCog', message: discord.Message, model_name:
text_parts.append(f"[Sticker: {sticker_name}{sticker_format_text}]")
is_image_sticker = sticker_format_str in ["StickerFormatType.png", "StickerFormatType.apng"]
if is_image_sticker and sticker_url and sticker_url.startswith('http'):
# Determine MIME type for sticker URI (PNG for both PNG and APNG)
sticker_mime_type = "image/png"
sticker_mime_type = "image/png" # APNG is also sent as image/png
try:
parts.append(types.Part(file_data=types.FileData(mime_type=sticker_mime_type, file_uri=sticker_url)))
print(f"Added FileData part for historical sticker: {sticker_name} (MIME: {sticker_mime_type}, URL: {sticker_url})")
# Download sticker data and send as inline_data
async with cog.session.get(sticker_url, timeout=10) as response:
if response.status == 200:
sticker_bytes = await response.read()
parts.append(types.Part(inline_data=types.Blob(data=sticker_bytes, mime_type=sticker_mime_type)))
print(f"Added inline_data part for historical sticker: {sticker_name} (MIME: {sticker_mime_type}, {len(sticker_bytes)} bytes)")
else:
print(f"Error downloading historical sticker {sticker_name} from {sticker_url}: Status {response.status}")
text_parts.append(f"[System Note: Failed to download sticker '{sticker_name}']")
except Exception as e:
print(f"Error creating FileData for historical sticker {sticker_name} from {sticker_url}: {e}")
text_parts.append(f"[System Note: Failed to attach sticker '{sticker_name}' as FileData]")
print(f"Error downloading/processing historical sticker {sticker_name} from {sticker_url}: {e}")
text_parts.append(f"[System Note: Failed to process sticker '{sticker_name}']")
elif sticker_format_str == "StickerFormatType.lottie":
parts.append(types.Part(text=f"[Sticker is a Lottie animation: {sticker_name}, URL: {sticker_url}]"))
# Lottie files are JSON, not directly viewable by Gemini as images. Send as text.
text_parts.append(f"[Lottie Sticker: {sticker_name} (JSON animation, not displayed as image)]")
full_text = "\n".join(text_parts).strip()
@ -1041,16 +1055,21 @@ async def get_ai_response(cog: 'GurtCog', message: discord.Message, model_name:
if is_supported and file_url:
try:
# Add the URI part with a cleaned MIME type
clean_mime_type = detected_mime_type.split(';')[0] if detected_mime_type else "application/octet-stream"
file_data_obj = types.FileData(mime_type=clean_mime_type, file_uri=file_url)
attachment_parts_to_add.append(types.Part(file_data=file_data_obj))
print(f"Added FileData part for supported attachment: {filename} ({clean_mime_type}) using URL: {file_url}")
# Download attachment data and send as inline_data
async with cog.session.get(file_url, timeout=15) as response: # Increased timeout for potentially larger files
if response.status == 200:
attachment_bytes = await response.read()
attachment_parts_to_add.append(types.Part(inline_data=types.Blob(data=attachment_bytes, mime_type=clean_mime_type)))
print(f"Added inline_data part for supported attachment: {filename} (MIME: {clean_mime_type}, {len(attachment_bytes)} bytes)")
else:
print(f"Error downloading attachment {filename} from {file_url}: Status {response.status}")
attachment_parts_to_add.append(types.Part(text=f"(System Note: Failed to download attachment '{filename}')"))
except Exception as e:
print(f"Error creating FileData types.Part for attachment {filename} ({detected_mime_type}): {e}")
print(f"Error downloading/processing attachment {filename} from {file_url}: {e}")
attachment_parts_to_add.append(types.Part(text=f"(System Note: Failed to process attachment '{filename}' - {e})"))
else:
print(f"Skipping FileData part for unsupported attachment: {filename} (Type: {detected_mime_type}, URL: {file_url})")
print(f"Skipping inline_data part for unsupported attachment: {filename} (Type: {detected_mime_type}, URL: {file_url})")
# Text description was already added above
# Add the collected attachment parts to the existing user message parts
@ -1078,12 +1097,18 @@ async def get_ai_response(cog: 'GurtCog', message: discord.Message, model_name:
is_animated_emoji = emoji_info.get("animated", False)
emoji_mime_type = "image/gif" if is_animated_emoji else "image/png"
try:
image_part = types.Part(file_data=types.FileData(mime_type=emoji_mime_type, file_uri=emoji_url))
emoji_sticker_parts_to_add.append(image_part)
print(f"Added FileData part for current emoji: {emoji_name} (MIME: {emoji_mime_type}, URL: {emoji_url})")
# Download emoji data and send as inline_data
async with cog.session.get(emoji_url, timeout=10) as response:
if response.status == 200:
emoji_bytes = await response.read()
emoji_sticker_parts_to_add.append(types.Part(inline_data=types.Blob(data=emoji_bytes, mime_type=emoji_mime_type)))
print(f"Added inline_data part for current emoji: {emoji_name} (MIME: {emoji_mime_type}, {len(emoji_bytes)} bytes)")
else:
print(f"Error downloading current emoji {emoji_name} from {emoji_url}: Status {response.status}")
emoji_sticker_parts_to_add.append(types.Part(text=f"[System Note: Failed to download emoji '{emoji_name}']"))
except Exception as e:
print(f"Error creating FileData for current emoji {emoji_name} from {emoji_url}: {e}")
emoji_sticker_parts_to_add.append(types.Part(text=f"[System Note: Failed to attach emoji '{emoji_name}' as FileData]"))
print(f"Error downloading/processing current emoji {emoji_name} from {emoji_url}: {e}")
emoji_sticker_parts_to_add.append(types.Part(text=f"[System Note: Failed to process emoji '{emoji_name}']"))
# Process stickers from formatted_current_message
stickers_current = formatted_current_message.get("stickers", [])
@ -1099,19 +1124,23 @@ async def get_ai_response(cog: 'GurtCog', message: discord.Message, model_name:
is_image_sticker = sticker_format_str in ["StickerFormatType.png", "StickerFormatType.apng"]
if is_image_sticker:
# Determine MIME type for sticker URI (PNG for both PNG and APNG)
sticker_mime_type = "image/png"
sticker_mime_type = "image/png" # APNG is also sent as image/png
try:
image_part = types.Part(file_data=types.FileData(mime_type=sticker_mime_type, file_uri=sticker_url))
emoji_sticker_parts_to_add.append(image_part)
print(f"Added FileData part for current sticker: {sticker_name} (MIME: {sticker_mime_type}, URL: {sticker_url})")
# Download sticker data and send as inline_data
async with cog.session.get(sticker_url, timeout=10) as response:
if response.status == 200:
sticker_bytes = await response.read()
emoji_sticker_parts_to_add.append(types.Part(inline_data=types.Blob(data=sticker_bytes, mime_type=sticker_mime_type)))
print(f"Added inline_data part for current sticker: {sticker_name} (MIME: {sticker_mime_type}, {len(sticker_bytes)} bytes)")
else:
print(f"Error downloading current sticker {sticker_name} from {sticker_url}: Status {response.status}")
emoji_sticker_parts_to_add.append(types.Part(text=f"[System Note: Failed to download sticker '{sticker_name}']"))
except Exception as e:
print(f"Error creating FileData for current sticker {sticker_name} from {sticker_url}: {e}")
emoji_sticker_parts_to_add.append(types.Part(text=f"[System Note: Failed to attach sticker '{sticker_name}' as FileData]"))
print(f"Error downloading/processing current sticker {sticker_name} from {sticker_url}: {e}")
emoji_sticker_parts_to_add.append(types.Part(text=f"[System Note: Failed to process sticker '{sticker_name}']"))
elif sticker_format_str == "StickerFormatType.lottie":
lottie_info_part = types.Part(text=f"[Sticker is a Lottie animation: {sticker_name}, URL: {sticker_url}]")
emoji_sticker_parts_to_add.append(lottie_info_part)
print(f"Sticker {sticker_name} is Lottie (JSON animation), added text info.")
# Lottie files are JSON, not directly viewable by Gemini as images. Send as text.
emoji_sticker_parts_to_add.append(types.Part(text=f"[Lottie Sticker: {sticker_name} (JSON animation, not displayed as image)]"))
else:
print(f"Sticker {sticker_name} has format {sticker_format_str}, not attempting image download. URL: {sticker_url}")