37 lines
1.3 KiB
Python
37 lines
1.3 KiB
Python
|
|
"""Content preparation for LLM input."""
|
||
|
|
|
||
|
|
import logging
|
||
|
|
|
||
|
|
logger = logging.getLogger('Beta')
|
||
|
|
|
||
|
|
|
||
|
|
def prepare_message_content(message) -> str:
|
||
|
|
"""Prepare message content for LLM by normalizing mentions and attachments."""
|
||
|
|
if not message:
|
||
|
|
return ""
|
||
|
|
|
||
|
|
content = message.content
|
||
|
|
|
||
|
|
# Replace channel mentions <#123> -> #general
|
||
|
|
for channel in message.channel_mentions:
|
||
|
|
content = content.replace(f'<#{channel.id}>', f'#{channel.name}')
|
||
|
|
|
||
|
|
# Replace role mentions <@&123> -> @admin
|
||
|
|
for role in message.role_mentions:
|
||
|
|
content = content.replace(f'<@&{role.id}>', f'@{role.name}')
|
||
|
|
|
||
|
|
# Replace user mentions <@123> or <@!123> -> @User
|
||
|
|
for mention in message.mentions:
|
||
|
|
content = content.replace(f'<@{mention.id}>', f'@{mention.display_name}')
|
||
|
|
content = content.replace(f'<@!{mention.id}>', f'@{mention.display_name}')
|
||
|
|
|
||
|
|
# Handle attachments
|
||
|
|
if message.attachments:
|
||
|
|
for attachment in message.attachments:
|
||
|
|
if attachment.content_type and attachment.content_type.startswith('image/'):
|
||
|
|
label = f"[Image: {attachment.filename}]"
|
||
|
|
else:
|
||
|
|
label = f"[Fichier: {attachment.filename}]"
|
||
|
|
content = f"{content} {label}".strip() if content else label
|
||
|
|
|
||
|
|
return content.strip()
|