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:
@@ -27,6 +27,7 @@ class DirectiveContext:
|
||||
recent_findings: list = field(default_factory=list)
|
||||
conversation_summary: str = ''
|
||||
peer_data: dict = field(default_factory=dict)
|
||||
receipts: list = field(default_factory=list) # populated by upload flow
|
||||
|
||||
|
||||
@dataclass
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
from __future__ import annotations
|
||||
import json
|
||||
import logging
|
||||
from datetime import date as _date
|
||||
from .base_agent import BaseAgent, AgentReport, AgentDirective, SweepReport
|
||||
from ..tools.expenses_tools import ExpensesTools
|
||||
|
||||
@@ -46,23 +48,27 @@ class ExpensesAgent(BaseAgent):
|
||||
def __init__(self, odoo, llm, peer_bus=None):
|
||||
super().__init__(odoo, llm, peer_bus)
|
||||
self._et = ExpensesTools(odoo)
|
||||
self._gathered_data = {}
|
||||
self._actions_taken = []
|
||||
self._escalations_list = []
|
||||
self._gathered_data: dict = {}
|
||||
self._actions_taken: list = []
|
||||
self._escalations_list: list = []
|
||||
|
||||
async def _plan(self, directive: AgentDirective) -> dict:
|
||||
intent = (directive.intent or '').lower()
|
||||
async def _plan(self) -> dict:
|
||||
task = (self._directive.task if self._directive else '').lower()
|
||||
receipts = getattr(self._directive.context, 'receipts', []) if self._directive else []
|
||||
return {
|
||||
'fetch_summary': any(k in intent for k in ('summary', 'overview', 'report')),
|
||||
'fetch_pending': any(k in intent for k in ('pending', 'approve', 'approval')),
|
||||
'employee_id': directive.context.get('employee_id'),
|
||||
'date_from': directive.context.get('date_from'),
|
||||
'date_to': directive.context.get('date_to'),
|
||||
'mode': 'create_from_receipts' if receipts else 'read',
|
||||
'fetch_summary': any(k in task for k in ('summary', 'overview')) and not receipts,
|
||||
'fetch_pending': any(k in task for k in ('pending', 'approve', 'approval')) and not receipts,
|
||||
'employee_id': self._directive.params.get('employee_id') if self._directive else None,
|
||||
'date_from': self._directive.params.get('date_from') if self._directive else None,
|
||||
'date_to': self._directive.params.get('date_to') if self._directive else None,
|
||||
}
|
||||
|
||||
async def _gather(self, ctx: dict) -> dict:
|
||||
plan = ctx.get('plan', {})
|
||||
data: dict = {}
|
||||
async def _gather(self, plan: dict) -> dict:
|
||||
data: dict = {'mode': plan.get('mode', 'read')}
|
||||
if plan.get('mode') == 'create_from_receipts':
|
||||
self._gathered_data = data
|
||||
return data
|
||||
data['summary'] = await self._et.get_expenses_summary(
|
||||
date_from=plan.get('date_from'), date_to=plan.get('date_to'),
|
||||
)
|
||||
@@ -71,9 +77,12 @@ class ExpensesAgent(BaseAgent):
|
||||
self._gathered_data = data
|
||||
return data
|
||||
|
||||
async def _reason(self, ctx: dict) -> dict:
|
||||
async def _reason(self) -> dict:
|
||||
data = self._gathered_data
|
||||
analysis: dict = {'escalations': [], 'flags': []}
|
||||
if data.get('mode') == 'create_from_receipts':
|
||||
self._escalations_list = []
|
||||
return analysis
|
||||
summary = data.get('summary', {})
|
||||
if summary.get('pending_approval_count', 0) > 10:
|
||||
analysis['escalations'].append(
|
||||
@@ -82,23 +91,135 @@ class ExpensesAgent(BaseAgent):
|
||||
self._escalations_list = analysis['escalations']
|
||||
return analysis
|
||||
|
||||
async def _act(self, ctx: dict) -> list:
|
||||
return []
|
||||
async def _act(self, reasoning: dict) -> list:
|
||||
if self._gathered_data.get('mode') != 'create_from_receipts':
|
||||
return []
|
||||
receipts = getattr(self._directive.context, 'receipts', []) if self._directive else []
|
||||
if not receipts:
|
||||
return []
|
||||
|
||||
async def _report(self, ctx: dict) -> AgentReport:
|
||||
user_id = (self._directive.context.peer_data.get('requesting_user_id')
|
||||
if self._directive else None)
|
||||
employee_id = await self._et.get_employee_id_for_user(user_id)
|
||||
if not employee_id:
|
||||
self._escalations_list.append(
|
||||
'No employee record found for the current user; cannot create expense report.')
|
||||
return []
|
||||
|
||||
sheet_name = f'Expense Report - {_date.today().isoformat()}'
|
||||
sheet_result = await self._et.create_expense_sheet(sheet_name, employee_id)
|
||||
if not sheet_result.success:
|
||||
self._escalations_list.append(f'Failed to create expense sheet: {sheet_result.error}')
|
||||
return []
|
||||
|
||||
sheet_id = sheet_result.record_id
|
||||
actions = [f'Created expense sheet "{sheet_name}" (ID {sheet_id})']
|
||||
|
||||
product_id = await self._et.get_default_expense_product()
|
||||
|
||||
for receipt in receipts:
|
||||
parsed = await self._parse_receipt_text(
|
||||
receipt.get('text', ''), receipt.get('filename', 'receipt'))
|
||||
expense_result = await self._et.create_expense(
|
||||
sheet_id=sheet_id,
|
||||
employee_id=employee_id,
|
||||
name=str(parsed.get('vendor', receipt.get('filename', 'Expense')))[:64],
|
||||
total_amount=float(parsed.get('amount', 0.0)),
|
||||
date=str(parsed.get('date') or _date.today().isoformat()),
|
||||
product_id=product_id,
|
||||
description=str(parsed.get('description', '')),
|
||||
)
|
||||
if expense_result.success:
|
||||
actions.append(
|
||||
f"Added: {parsed.get('vendor', 'Unknown vendor')} "
|
||||
f"${float(parsed.get('amount', 0)):.2f} "
|
||||
f"on {parsed.get('date', 'today')}"
|
||||
)
|
||||
if receipt.get('b64'):
|
||||
await self._et.attach_receipt(
|
||||
'hr.expense', expense_result.record_id,
|
||||
receipt.get('filename', 'receipt'),
|
||||
receipt['b64'],
|
||||
receipt.get('mimetype', 'application/octet-stream'),
|
||||
)
|
||||
else:
|
||||
actions.append(
|
||||
f"Could not create expense for {receipt.get('filename', 'receipt')}: "
|
||||
f"{expense_result.error}"
|
||||
)
|
||||
|
||||
self._actions_taken = actions
|
||||
return actions
|
||||
|
||||
async def _parse_receipt_text(self, text: str, filename: str) -> dict:
|
||||
fallback = {'vendor': filename, 'amount': 0.0,
|
||||
'date': _date.today().isoformat(), 'description': filename}
|
||||
if not text or text.startswith('['):
|
||||
return fallback
|
||||
|
||||
prompt = (
|
||||
'Extract expense details from the following receipt text. '
|
||||
'Return ONLY valid JSON with these keys: '
|
||||
'"vendor" (string), "amount" (number, the total charged), '
|
||||
'"date" (string YYYY-MM-DD, use today if absent), '
|
||||
'"description" (string, brief expense type).\n\n'
|
||||
f'Receipt text (first 2000 chars):\n{text[:2000]}\n\nJSON only:'
|
||||
)
|
||||
try:
|
||||
resp = await self._llm.submit(
|
||||
[{'role': 'user', 'content': prompt}],
|
||||
caller='expenses_agent_receipt_parser',
|
||||
)
|
||||
raw = (resp.content or '').strip()
|
||||
first, last = raw.find('{'), raw.rfind('}')
|
||||
if first != -1 and last > first:
|
||||
data = json.loads(raw[first:last + 1])
|
||||
return {
|
||||
'vendor': str(data.get('vendor', filename)),
|
||||
'amount': float(data.get('amount', 0.0)),
|
||||
'date': str(data.get('date', _date.today().isoformat())),
|
||||
'description': str(data.get('description', '')),
|
||||
}
|
||||
except Exception as exc:
|
||||
logger.warning('Receipt parse failed for %s: %s', filename, exc)
|
||||
return fallback
|
||||
|
||||
async def _report(self) -> AgentReport:
|
||||
data = self._gathered_data
|
||||
summary = data.get('summary', {})
|
||||
directive_id = self._directive.directive_id if self._directive else ''
|
||||
|
||||
if data.get('mode') == 'create_from_receipts':
|
||||
if self._actions_taken:
|
||||
lines = '\n'.join(f' • {a}' for a in self._actions_taken)
|
||||
summary = (
|
||||
f'Expense report created successfully:\n{lines}\n\n'
|
||||
'The report is in draft. Please open Odoo > Expenses, '
|
||||
'review the entries, and click Submit to send for approval.'
|
||||
)
|
||||
status = 'complete'
|
||||
else:
|
||||
summary = ('Could not create expense report. ' +
|
||||
'; '.join(self._escalations_list or ['Unknown error']))
|
||||
status = 'failed'
|
||||
return AgentReport(
|
||||
directive_id=directive_id, agent=self.name, status=status,
|
||||
summary=summary, data=data,
|
||||
escalations=self._escalations_list, actions_taken=self._actions_taken)
|
||||
|
||||
summary_data = data.get('summary', {})
|
||||
parts = []
|
||||
if summary:
|
||||
if summary_data:
|
||||
parts.append(
|
||||
f'Expenses: {summary.get("total_expenses", 0)} records, '
|
||||
f'total {summary.get("total_amount", 0):.2f}. '
|
||||
f'{summary.get("pending_approval_count", 0)} pending approval.'
|
||||
f'Expenses: {summary_data.get("total_expenses", 0)} records, '
|
||||
f'total ${summary_data.get("total_amount", 0):.2f}. '
|
||||
f'{summary_data.get("pending_approval_count", 0)} pending approval.'
|
||||
)
|
||||
if not parts:
|
||||
parts.append('Expenses review complete.')
|
||||
return AgentReport(agent=self.name, summary=chr(10).join(parts),
|
||||
data=data, escalations=self._escalations_list, actions_taken=[])
|
||||
return AgentReport(
|
||||
directive_id=directive_id, agent=self.name, status='complete',
|
||||
summary='\n'.join(parts), data=data,
|
||||
escalations=self._escalations_list, actions_taken=[])
|
||||
|
||||
async def _dispatch_tool(self, name: str, args: dict):
|
||||
dispatch = {
|
||||
@@ -121,7 +242,8 @@ class ExpensesAgent(BaseAgent):
|
||||
if req_type == 'expenses_summary':
|
||||
return await self._et.get_expenses_summary()
|
||||
if req_type == 'employee_expenses':
|
||||
return {'expenses': await self._et.get_expense_by_employee(employee_id=request['employee_id'])}
|
||||
return {'expenses': await self._et.get_expense_by_employee(
|
||||
employee_id=request['employee_id'])}
|
||||
return {'error': f'Unknown type: {req_type}'}
|
||||
except Exception as exc:
|
||||
return {'error': str(exc)}
|
||||
@@ -131,10 +253,15 @@ class ExpensesAgent(BaseAgent):
|
||||
try:
|
||||
pending = await self._et.get_pending_approvals()
|
||||
for sheet in pending:
|
||||
findings.append({'type': 'pending_expense_approval', 'sheet_id': sheet.get('id'),
|
||||
'employee': sheet.get('employee_id', [0, ''])[1] if isinstance(sheet.get('employee_id'), list) else '',
|
||||
'amount': sheet.get('total_amount', 0), 'severity': 'low'})
|
||||
emp = sheet.get('employee_id', [0, ''])
|
||||
findings.append({
|
||||
'type': 'pending_expense_approval',
|
||||
'sheet_id': sheet.get('id'),
|
||||
'employee': emp[1] if isinstance(emp, list) else '',
|
||||
'amount': sheet.get('total_amount', 0),
|
||||
'severity': 'low',
|
||||
})
|
||||
except Exception as exc:
|
||||
return SweepReport(agent=self.name, findings=[], actions=[], error=str(exc))
|
||||
return SweepReport(agent=self.name, findings=findings, actions=[],
|
||||
return SweepReport(agent=self.name, findings=[], error=str(exc))
|
||||
return SweepReport(agent=self.name, findings=findings, actions_taken=[],
|
||||
summary=f'Expenses sweep: {len(findings)} pending approvals.')
|
||||
|
||||
@@ -71,7 +71,8 @@ class MasterAgent:
|
||||
# example block, so str.format would treat them as fields.
|
||||
return template.replace('{agent_list}', agent_list)
|
||||
|
||||
async def handle_message(self, user_id, channel_id, message, directive_id) -> MasterResponse:
|
||||
async def handle_message(self, user_id, channel_id, message, directive_id,
|
||||
extra_context: dict = None) -> MasterResponse:
|
||||
try:
|
||||
user_id = int(user_id)
|
||||
except (TypeError, ValueError):
|
||||
@@ -107,7 +108,8 @@ class MasterAgent:
|
||||
await self._memory.append_message(user_id, 'assistant', msg, directive_id)
|
||||
await self._log_directive_complete(directive_id, 'failed', msg)
|
||||
return MasterResponse(directive_id=directive_id, response=msg, status='failed')
|
||||
directives = await self._build_directives(intent, context, directive_id)
|
||||
directives = await self._build_directives(intent, context, directive_id,
|
||||
user_id=user_id, extra_context=extra_context)
|
||||
reports = await self._dispatch_agents(directives)
|
||||
response_text = await self._synthesize(reports, context)
|
||||
await self._update_memory(user_id, message, response_text, reports, directive_id)
|
||||
@@ -197,20 +199,26 @@ class MasterAgent:
|
||||
return AccessResult(allowed=False, denied_agents=denied)
|
||||
return AccessResult(allowed=True)
|
||||
|
||||
async def _build_directives(self, intent: IntentResult, context: MasterContext, directive_id) -> list:
|
||||
async def _build_directives(self, intent: IntentResult, context: MasterContext, directive_id,
|
||||
user_id=None, extra_context: dict = None) -> list:
|
||||
receipts = (extra_context or {}).get('receipts', [])
|
||||
directives = []
|
||||
for agent_key in intent.agents:
|
||||
authorized = ['read', 'search', 'report', 'post_chatter',
|
||||
'send_email', 'create_non_financial', 'write_non_financial']
|
||||
if receipts:
|
||||
authorized.append('create_expense')
|
||||
ctx = DirectiveContext(
|
||||
client_profile=context.knowledge,
|
||||
recent_findings=context.operational_findings,
|
||||
conversation_summary=chr(10).join(
|
||||
m['content'] for m in context.conversation[-5:] if m['role'] == 'assistant'),
|
||||
peer_data={})
|
||||
peer_data={'requesting_user_id': user_id},
|
||||
receipts=receipts)
|
||||
d = AgentDirective(
|
||||
directive_id=directive_id, agent=agent_key, task=intent.intent_summary,
|
||||
params=intent.params, context=ctx,
|
||||
authorized_actions=['read', 'search', 'report', 'post_chatter',
|
||||
'send_email', 'create_non_financial', 'write_non_financial'],
|
||||
authorized_actions=authorized,
|
||||
constraints={'max_amount': 5000})
|
||||
directives.append(d)
|
||||
return directives
|
||||
|
||||
Reference in New Issue
Block a user