fix: chat attachment detection — 3-method fallback + deferred retry
ab_ai_mail.py: when a user sends a file via Odoo 18 Discuss, the zip was going through /dispatch (text-only) instead of /upload, causing the bot to respond "I'm unable to locate the zip file" because attachment_ids was empty in the message_post override. Root cause: Odoo 18 Discuss links file attachments to mail.message records via three different mechanisms depending on the upload path, and we only checked one (the Many2many relation table). Fixes: 1. Three-method attachment detection in message_post: - Method 1: result.attachment_ids (Many2many relation table) - Method 2: ir.attachment with res_model='mail.message' (Odoo 15+ style) - Method 3: attachment IDs parsed from href URLs in the HTML body 2. Deferred retry in _agent_thread: if att_data is still empty but a message_id is known, sleep 1s then re-read via a fresh DB cursor so we see data committed after message_post returned (timing race fix) 3. Skip zero-byte attachments and warn instead of silently using them 4. Pass message_id to the background thread (new kwarg, backward compat) 5. Add debug logging so future issues can be diagnosed from Odoo logs Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,7 @@ import base64
|
|||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
import threading
|
import threading
|
||||||
|
import time
|
||||||
|
|
||||||
import requests as _requests
|
import requests as _requests
|
||||||
from markupsafe import Markup, escape
|
from markupsafe import Markup, escape
|
||||||
@@ -11,6 +12,8 @@ from odoo import SUPERUSER_ID, api, registry as odoo_registry, models
|
|||||||
_logger = logging.getLogger(__name__)
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
_HTML_TAG = re.compile(r'<[^>]+>')
|
_HTML_TAG = re.compile(r'<[^>]+>')
|
||||||
|
# Matches /web/content/ir.attachment/<id>/ or /web/image/ir.attachment/<id>/
|
||||||
|
_ATT_URL_RE = re.compile(r'/(?:web/content|web/image)/ir\.attachment/(\d+)/')
|
||||||
|
|
||||||
|
|
||||||
def _strip_html(html: str) -> str:
|
def _strip_html(html: str) -> str:
|
||||||
@@ -38,14 +41,57 @@ def _post_bot_reply(db: str, channel_id: int, bot_partner_id: int, reply_text: s
|
|||||||
_logger.error('_post_bot_reply failed channel=%s: %s', channel_id, exc)
|
_logger.error('_post_bot_reply failed channel=%s: %s', channel_id, exc)
|
||||||
|
|
||||||
|
|
||||||
|
def _read_message_attachments(db: str, message_id: int) -> list[tuple[str, bytes, str]]:
|
||||||
|
"""Re-read attachment bytes for a message using a fresh DB cursor.
|
||||||
|
|
||||||
|
Called from the background agent thread as a fallback when the
|
||||||
|
message_post override ran before the transaction that linked the
|
||||||
|
attachment to the message had committed (common in Odoo 18 Discuss).
|
||||||
|
Retries up to 3 times with 0.5s delay.
|
||||||
|
"""
|
||||||
|
for attempt in range(3):
|
||||||
|
try:
|
||||||
|
with odoo_registry(db).cursor() as cr:
|
||||||
|
env = api.Environment(cr, SUPERUSER_ID, {})
|
||||||
|
msg = env['mail.message'].browse(message_id)
|
||||||
|
att_data = []
|
||||||
|
for att in msg.attachment_ids:
|
||||||
|
try:
|
||||||
|
data = base64.b64decode(att.datas) if att.datas else b''
|
||||||
|
if data:
|
||||||
|
att_data.append((att.name or 'attachment', data,
|
||||||
|
att.mimetype or 'application/octet-stream'))
|
||||||
|
except Exception as exc:
|
||||||
|
_logger.warning('_read_message_attachments: decode att %s: %s',
|
||||||
|
att.id, exc)
|
||||||
|
if att_data:
|
||||||
|
return att_data
|
||||||
|
except Exception as exc:
|
||||||
|
_logger.warning('_read_message_attachments attempt %d: %s', attempt, exc)
|
||||||
|
if attempt < 2:
|
||||||
|
time.sleep(0.5)
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
def _agent_thread(db: str, uid: int, text: str, att_data: list,
|
def _agent_thread(db: str, uid: int, text: str, att_data: list,
|
||||||
bot_partner_id: int, channel_id: int,
|
bot_partner_id: int, channel_id: int,
|
||||||
bot_url: str, bot_secret: str):
|
bot_url: str, bot_secret: str, message_id: int = 0):
|
||||||
"""
|
"""
|
||||||
Background thread: calls the agent service and posts the reply.
|
Background thread: calls the agent service and posts the reply.
|
||||||
All messages — text, files, or both — are routed here so the LLM
|
All messages — text, files, or both — are routed here so the LLM
|
||||||
handles every response. Nothing is intercepted or templated in Odoo.
|
handles every response. Nothing is intercepted or templated in Odoo.
|
||||||
"""
|
"""
|
||||||
|
# Deferred attachment detection: if no att_data was found in message_post
|
||||||
|
# (happens in Odoo 18 when the transaction linking attachments to the message
|
||||||
|
# commits after our override already ran), wait briefly and retry using a
|
||||||
|
# fresh DB cursor so we see the committed attachment data.
|
||||||
|
if not att_data and message_id:
|
||||||
|
time.sleep(1.0)
|
||||||
|
att_data = _read_message_attachments(db, message_id)
|
||||||
|
if att_data:
|
||||||
|
_logger.info('_agent_thread: deferred read found %d attachment(s) for msg %d',
|
||||||
|
len(att_data), message_id)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
headers = {}
|
headers = {}
|
||||||
if bot_secret:
|
if bot_secret:
|
||||||
@@ -124,22 +170,49 @@ class DiscussChannel(models.Model):
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
text = _strip_html(body)
|
text = _strip_html(body)
|
||||||
|
message_id = result.id
|
||||||
|
|
||||||
|
# ── Attachment detection ─────────────────────────────────────────────
|
||||||
|
# In Odoo 18, file attachments in Discuss can be linked to a message
|
||||||
|
# via three different mechanisms depending on the upload path. Try all
|
||||||
|
# three so we don't miss any files.
|
||||||
|
|
||||||
|
# Method 1: standard Many2many relation table (mail.message → ir.attachment)
|
||||||
attachments = result.attachment_ids
|
attachments = result.attachment_ids
|
||||||
|
|
||||||
# Nothing to process
|
# Method 2: ir.attachment records with res_model='mail.message' (Odoo 15+ style)
|
||||||
if not text and not attachments:
|
if not attachments:
|
||||||
return result
|
attachments = self.env['ir.attachment'].sudo().search([
|
||||||
|
('res_model', '=', 'mail.message'),
|
||||||
|
('res_id', '=', message_id),
|
||||||
|
])
|
||||||
|
|
||||||
# Read attachment bytes NOW, inside the current transaction
|
# Method 3: attachment IDs embedded in HTML body links
|
||||||
|
# e.g. <a href="/web/content/ir.attachment/123/...">file.zip</a>
|
||||||
|
if not attachments:
|
||||||
|
body_att_ids = [int(m) for m in _ATT_URL_RE.findall(body or '')]
|
||||||
|
if body_att_ids:
|
||||||
|
attachments = self.env['ir.attachment'].sudo().browse(body_att_ids).exists()
|
||||||
|
|
||||||
|
# Read the raw bytes for each attachment found
|
||||||
att_data: list[tuple[str, bytes, str]] = []
|
att_data: list[tuple[str, bytes, str]] = []
|
||||||
for att in attachments:
|
for att in attachments:
|
||||||
try:
|
try:
|
||||||
data = base64.b64decode(att.datas) if att.datas else b''
|
data = base64.b64decode(att.datas) if att.datas else b''
|
||||||
att_data.append((att.name or 'attachment', data,
|
if data:
|
||||||
att.mimetype or 'application/octet-stream'))
|
att_data.append((att.name or 'attachment', data,
|
||||||
|
att.mimetype or 'application/octet-stream'))
|
||||||
|
else:
|
||||||
|
_logger.warning('message_post: attachment %s (%s) has no data — '
|
||||||
|
'will retry in background thread', att.id, att.name)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
_logger.warning('Could not read attachment %s: %s', att.id, exc)
|
_logger.warning('Could not read attachment %s: %s', att.id, exc)
|
||||||
|
|
||||||
|
# Nothing to process
|
||||||
|
if not text and not att_data and not attachments:
|
||||||
|
return result
|
||||||
|
|
||||||
|
# ── Fire the agent ───────────────────────────────────────────────────
|
||||||
human_partner = member_partners.filtered(lambda p: p != bot_partner)[:1]
|
human_partner = member_partners.filtered(lambda p: p != bot_partner)[:1]
|
||||||
user = self.env['res.users'].search([('partner_id', '=', human_partner.id)], limit=1)
|
user = self.env['res.users'].search([('partner_id', '=', human_partner.id)], limit=1)
|
||||||
uid = user.id if user else self.env.uid
|
uid = user.id if user else self.env.uid
|
||||||
@@ -154,11 +227,18 @@ class DiscussChannel(models.Model):
|
|||||||
channel_id = self.id
|
channel_id = self.id
|
||||||
bot_partner_id = bot_partner.id
|
bot_partner_id = bot_partner.id
|
||||||
|
|
||||||
# Launch the agent call in a daemon thread — message_post returns immediately
|
_logger.debug('message_post: channel=%s msg=%s text=%r att_count=%d deferred=%s',
|
||||||
|
channel_id, message_id, text[:60] if text else '',
|
||||||
|
len(att_data), bool(attachments and not att_data))
|
||||||
|
|
||||||
|
# Launch the agent call in a daemon thread — message_post returns immediately.
|
||||||
|
# message_id is passed so the thread can re-read attachments if they weren't
|
||||||
|
# yet committed when we read them above (Odoo 18 timing race).
|
||||||
threading.Thread(
|
threading.Thread(
|
||||||
target=_agent_thread,
|
target=_agent_thread,
|
||||||
args=(db, uid, text, att_data, bot_partner_id, channel_id,
|
args=(db, uid, text, att_data, bot_partner_id, channel_id,
|
||||||
bot_url, bot_secret),
|
bot_url, bot_secret),
|
||||||
|
kwargs={'message_id': message_id},
|
||||||
daemon=True,
|
daemon=True,
|
||||||
).start()
|
).start()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user