feat: file upload + expense report creation from Discuss attachments
- Discuss bot now reads ir.attachment from incoming messages; file-only messages no longer silently dropped - ZIP files are described (contents listed) and bot asks clarifying question before acting; user's follow-up reply looks back for pending attachments so files don't need to be re-uploaded - receipt_parser: extracts text from ZIP (recursive), JPG/PNG/etc (OCR), PDF (pdfplumber), HTML, TXT - expenses_agent: full rewrite fixing broken method signatures; adds create_expense_sheet / create_expense / attach_receipt flow driven by LLM receipt parsing (Ollama, HIPAA-locked) - master_agent: extra_context threads receipts + user_id into directives - FastAPI /upload multipart endpoint; registered in main.py - Odoo /ai/upload controller proxies files to agent service - ab_ai_bot: dispatch_message_with_files() for multipart uploads Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -3,7 +3,7 @@ import logging
|
||||
import requests
|
||||
|
||||
from odoo import http
|
||||
from odoo.http import request
|
||||
from odoo.http import request, Response
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -58,3 +58,41 @@ class AiApprovalController(http.Controller):
|
||||
session_id=session_id,
|
||||
)
|
||||
return result
|
||||
|
||||
@http.route('/ai/upload', type='http', auth='user', methods=['POST'], csrf=False)
|
||||
def upload(self, **kwargs):
|
||||
bot = request.env['ab.ai.bot'].sudo().get_active_bot()
|
||||
if not bot:
|
||||
return Response(
|
||||
json.dumps({'error': 'No bot configured'}),
|
||||
content_type='application/json', status=503)
|
||||
|
||||
url = bot._get_service_url() + '/upload'
|
||||
message = request.httprequest.form.get(
|
||||
'message', 'Create an employee expense report from these receipts.')
|
||||
session_id = request.httprequest.form.get('session_id', '')
|
||||
|
||||
files_data = [
|
||||
('files', (f.filename, f.read(), f.content_type or 'application/octet-stream'))
|
||||
for f in request.httprequest.files.getlist('files')
|
||||
]
|
||||
|
||||
try:
|
||||
resp = requests.post(
|
||||
url,
|
||||
data={
|
||||
'user_id': str(request.env.user.id),
|
||||
'message': message,
|
||||
'session_id': session_id,
|
||||
},
|
||||
files=files_data or [('files', ('empty', b'', 'application/octet-stream'))],
|
||||
headers=bot._build_headers(),
|
||||
timeout=120,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
return Response(resp.text, content_type='application/json')
|
||||
except Exception as exc:
|
||||
_logger.error('upload proxy failed: %s', exc)
|
||||
return Response(
|
||||
json.dumps({'error': str(exc), 'reply': 'Upload failed. Please try again.'}),
|
||||
content_type='application/json', status=500)
|
||||
|
||||
@@ -111,6 +111,44 @@ class AbAiBot(models.Model):
|
||||
_logger.error('dispatch_message failed: %s', exc)
|
||||
raise UserError(_('Could not reach AI service: %s') % exc)
|
||||
|
||||
def dispatch_message_with_files(self, user_id, message, attachments, context=None, session_id=None):
|
||||
"""Send a message with file attachments to the /upload endpoint as multipart."""
|
||||
self.ensure_one()
|
||||
import base64
|
||||
url = self._get_service_url() + '/upload'
|
||||
|
||||
files = []
|
||||
for att in attachments:
|
||||
try:
|
||||
data = base64.b64decode(att.datas) if att.datas else b''
|
||||
files.append(('files', (att.name or 'attachment',
|
||||
data,
|
||||
att.mimetype or 'application/octet-stream')))
|
||||
except Exception as exc:
|
||||
_logger.warning('Could not encode attachment %s: %s', att.id, exc)
|
||||
|
||||
# Omit Content-Type so requests sets the multipart boundary automatically
|
||||
headers = {}
|
||||
if self.webhook_secret:
|
||||
headers['X-ActiveBlue-Signature'] = self.webhook_secret
|
||||
|
||||
form_data = {
|
||||
'user_id': str(user_id),
|
||||
'message': message or 'Create an employee expense report from these receipts.',
|
||||
'session_id': session_id or '',
|
||||
}
|
||||
|
||||
try:
|
||||
resp = requests.post(url, data=form_data, files=files or [('files', ('empty', b'', 'text/plain'))],
|
||||
headers=headers, timeout=120)
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
except requests.exceptions.Timeout:
|
||||
raise UserError(_('AI service timed out. Please try again.'))
|
||||
except requests.exceptions.RequestException as exc:
|
||||
_logger.error('dispatch_message_with_files failed: %s', exc)
|
||||
raise UserError(_('Could not reach AI service: %s') % exc)
|
||||
|
||||
@api.model
|
||||
def cron_ping_all(self):
|
||||
any_online = False
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
from __future__ import annotations
|
||||
import base64
|
||||
import io
|
||||
import logging
|
||||
import re
|
||||
import zipfile
|
||||
|
||||
from odoo import models, api
|
||||
|
||||
@@ -8,11 +11,52 @@ _logger = logging.getLogger(__name__)
|
||||
|
||||
_HTML_TAG = re.compile(r'<[^>]+>')
|
||||
|
||||
# How many recent messages to scan when looking for a pending file upload
|
||||
_LOOKBACK_MESSAGES = 10
|
||||
|
||||
# File type labels shown in the clarification message
|
||||
_EXT_LABELS = {
|
||||
'jpg': 'image', 'jpeg': 'image', 'png': 'image', 'gif': 'image',
|
||||
'bmp': 'image', 'tiff': 'image', 'tif': 'image', 'webp': 'image',
|
||||
'pdf': 'PDF', 'html': 'HTML', 'htm': 'HTML',
|
||||
'txt': 'text', 'csv': 'spreadsheet', 'xlsx': 'spreadsheet',
|
||||
'zip': 'ZIP archive',
|
||||
}
|
||||
|
||||
|
||||
def _strip_html(html: str) -> str:
|
||||
return _HTML_TAG.sub(' ', html or '').strip()
|
||||
|
||||
|
||||
def _ext(filename: str) -> str:
|
||||
return filename.rsplit('.', 1)[-1].lower() if '.' in filename else ''
|
||||
|
||||
|
||||
def _describe_zip(datas_b64: str, zip_name: str) -> str:
|
||||
"""Return a short HTML summary of a ZIP's contents without extracting data."""
|
||||
try:
|
||||
raw = base64.b64decode(datas_b64)
|
||||
with zipfile.ZipFile(io.BytesIO(raw)) as zf:
|
||||
members = [m for m in zf.namelist() if not m.endswith('/')]
|
||||
if not members:
|
||||
return f'<b>{zip_name}</b> (empty archive)'
|
||||
# Count by type
|
||||
counts: dict[str, int] = {}
|
||||
for m in members:
|
||||
label = _EXT_LABELS.get(_ext(m), 'file')
|
||||
counts[label] = counts.get(label, 0) + 1
|
||||
type_summary = ', '.join(f'{n} {t}(s)' for t, n in counts.items())
|
||||
lines = [f'<b>{zip_name}</b> — {len(members)} item(s): {type_summary}']
|
||||
for m in members[:8]:
|
||||
lines.append(f' • {m}')
|
||||
if len(members) > 8:
|
||||
lines.append(f' … and {len(members) - 8} more')
|
||||
return '<br/>'.join(lines)
|
||||
except Exception as exc:
|
||||
_logger.warning('_describe_zip failed for %s: %s', zip_name, exc)
|
||||
return f'<b>{zip_name}</b> (could not inspect contents)'
|
||||
|
||||
|
||||
class DiscussChannel(models.Model):
|
||||
_inherit = 'discuss.channel'
|
||||
|
||||
@@ -40,10 +84,28 @@ class DiscussChannel(models.Model):
|
||||
return result
|
||||
|
||||
text = _strip_html(body)
|
||||
if not text:
|
||||
attachments = result.attachment_ids
|
||||
|
||||
# Nothing to work with
|
||||
if not text and not attachments:
|
||||
return result
|
||||
|
||||
# Identify the human sender
|
||||
# ── Case 1: file(s) with no instruction ──────────────────────────────
|
||||
# Show the user what we received and ask what to do with it.
|
||||
if attachments and not text:
|
||||
self._post_file_clarification(attachments, bot_partner)
|
||||
return result
|
||||
|
||||
# ── Case 2: text only — look back for a pending file upload ──────────
|
||||
# If the user just replied to our clarification question, find the
|
||||
# attachment(s) they uploaded earlier in this conversation.
|
||||
pending = self.env['ir.attachment'].browse()
|
||||
if text and not attachments:
|
||||
pending = self._find_pending_attachments(bot_partner)
|
||||
|
||||
effective_attachments = attachments or pending
|
||||
|
||||
# ── Case 3: text (+ possibly pending files) → dispatch ───────────────
|
||||
human_partner = member_partners.filtered(lambda p: p != bot_partner)[:1]
|
||||
user = self.env['res.users'].search([('partner_id', '=', human_partner.id)], limit=1)
|
||||
uid = user.id if user else self.env.uid
|
||||
@@ -52,11 +114,21 @@ class DiscussChannel(models.Model):
|
||||
bot = self.env['ab.ai.bot'].sudo().search([('active', '=', True)], limit=1)
|
||||
if not bot:
|
||||
return result
|
||||
response = bot.dispatch_message(
|
||||
user_id=uid,
|
||||
message=text,
|
||||
context={'channel_id': self.id, 'source': 'discuss'},
|
||||
)
|
||||
|
||||
if effective_attachments:
|
||||
response = bot.dispatch_message_with_files(
|
||||
user_id=uid,
|
||||
message=text,
|
||||
attachments=effective_attachments,
|
||||
context={'channel_id': self.id, 'source': 'discuss'},
|
||||
)
|
||||
else:
|
||||
response = bot.dispatch_message(
|
||||
user_id=uid,
|
||||
message=text,
|
||||
context={'channel_id': self.id, 'source': 'discuss'},
|
||||
)
|
||||
|
||||
reply = (response or {}).get('reply') or (response or {}).get('message') or \
|
||||
'I could not process your request right now.'
|
||||
self.sudo().message_post(
|
||||
@@ -69,3 +141,52 @@ class DiscussChannel(models.Model):
|
||||
_logger.error('AI bot Discuss reply failed: %s', exc)
|
||||
|
||||
return result
|
||||
|
||||
def _post_file_clarification(self, attachments, bot_partner):
|
||||
"""Describe the uploaded file(s) and ask the user what to do with them."""
|
||||
lines = []
|
||||
for att in attachments:
|
||||
name = att.name or 'file'
|
||||
ext = _ext(name)
|
||||
if ext == 'zip' and att.datas:
|
||||
lines.append(_describe_zip(att.datas, name))
|
||||
else:
|
||||
label = _EXT_LABELS.get(ext, 'file')
|
||||
lines.append(f'<b>{name}</b> ({label})')
|
||||
|
||||
file_summary = '<br/>'.join(lines)
|
||||
question = (
|
||||
f'I received the following file(s):<br/>{file_summary}<br/><br/>'
|
||||
'What would you like me to do with them? Some options:<br/>'
|
||||
'• <b>Create an expense report</b> from these receipts<br/>'
|
||||
'• <b>Import products</b> from this data<br/>'
|
||||
'• <b>Something else</b> — just tell me what you need'
|
||||
)
|
||||
self.sudo().message_post(
|
||||
body=question,
|
||||
author_id=bot_partner.id,
|
||||
message_type='comment',
|
||||
subtype_xmlid='mail.mt_comment',
|
||||
)
|
||||
|
||||
def _find_pending_attachments(self, bot_partner):
|
||||
"""
|
||||
Scan the last _LOOKBACK_MESSAGES messages in this channel for the most
|
||||
recent human-sent message that has attachments. Only returns them if
|
||||
the immediately following bot message looks like a clarification question
|
||||
(i.e. the bot hasn't already acted on those files).
|
||||
"""
|
||||
messages = self.message_ids.sorted('date', reverse=True)[:_LOOKBACK_MESSAGES]
|
||||
prev_was_bot_question = False
|
||||
for msg in messages:
|
||||
is_bot = msg.author_id == bot_partner
|
||||
if is_bot:
|
||||
# Check whether this bot message was a clarification question
|
||||
if 'what would you like me to do' in (msg.body or '').lower():
|
||||
prev_was_bot_question = True
|
||||
continue
|
||||
# Human message
|
||||
if msg.attachment_ids and prev_was_bot_question:
|
||||
return msg.attachment_ids
|
||||
prev_was_bot_question = False
|
||||
return self.env['ir.attachment'].browse()
|
||||
|
||||
Reference in New Issue
Block a user