1. OdooClient missing self._timeout — every _xmlrpc_call raised AttributeError, making the odoo health check permanently fail. Fix: set self._timeout = XMLRPC_TIMEOUT in __init__. 2. action_ping only accepted ollama=='ok' but health.py now returns 'warming' when the model is not yet hot in VRAM. Fix: treat warming as passing so the bot goes online and the model loads on the first real request. 3. /ai/approval/pending declared methods=['GET'] on a type='json' route — Odoo JSON-RPC always POSTs, so every browser call got 405 METHOD NOT ALLOWED. Fix: change to methods=['POST']. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
218 lines
9.1 KiB
Python
218 lines
9.1 KiB
Python
from odoo import models, fields, api, _
|
|
from odoo.exceptions import UserError
|
|
from datetime import timedelta
|
|
import requests
|
|
import logging
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
AGENT_SERVICE_URL_PARAM = 'activeblue_ai.agent_service_url'
|
|
WEBHOOK_SECRET_PARAM = 'activeblue_ai.webhook_secret'
|
|
|
|
|
|
class AbAiBot(models.Model):
|
|
_name = 'ab.ai.bot'
|
|
_description = 'ActiveBlue AI Bot'
|
|
_rec_name = 'display_name'
|
|
|
|
display_name = fields.Char(string='Bot Name', default='ActiveBlue AI', required=True)
|
|
active = fields.Boolean(default=True)
|
|
agent_service_url = fields.Char(
|
|
string='Agent Service URL',
|
|
default='http://192.168.2.47:8001',
|
|
required=True,
|
|
)
|
|
webhook_secret = fields.Char(string='Webhook Secret')
|
|
privacy_mode = fields.Selection(
|
|
[('local', 'Local (Ollama only)'), ('hybrid', 'Hybrid'), ('cloud', 'Cloud (Claude)')],
|
|
string='Privacy Mode',
|
|
default='local',
|
|
required=True,
|
|
)
|
|
status = fields.Selection(
|
|
[('online', 'Online'), ('offline', 'Offline'), ('error', 'Error')],
|
|
string='Status',
|
|
default='offline',
|
|
readonly=True,
|
|
)
|
|
last_ping = fields.Datetime(string='Last Ping', readonly=True)
|
|
notes = fields.Text(string='Notes')
|
|
|
|
@api.model
|
|
def get_active_bot(self):
|
|
bot = self.search([('active', '=', True)], limit=1)
|
|
if not bot:
|
|
raise UserError(_('No active AI bot configured. Please configure ActiveBlue AI.'))
|
|
return bot
|
|
|
|
def _get_service_url(self):
|
|
self.ensure_one()
|
|
return (self.agent_service_url or '').rstrip('/')
|
|
|
|
def _build_headers(self):
|
|
self.ensure_one()
|
|
headers = {'Content-Type': 'application/json'}
|
|
if self.webhook_secret:
|
|
headers['X-ActiveBlue-Signature'] = self.webhook_secret
|
|
return headers
|
|
|
|
# Systems that must all report 'ok' for the bot to be considered online.
|
|
REQUIRED_SYSTEMS = ['db', 'odoo', 'ollama', 'master_agent']
|
|
|
|
def action_ping(self):
|
|
self.ensure_one()
|
|
url = self._get_service_url() + '/health/detailed'
|
|
try:
|
|
resp = requests.get(url, timeout=5, headers=self._build_headers())
|
|
if resp.status_code != 200:
|
|
self.write({'status': 'error', 'last_ping': fields.Datetime.now()})
|
|
self._sync_bot_user_presence(online=False)
|
|
return {'type': 'ir.actions.client', 'tag': 'display_notification',
|
|
'params': {'message': _('AI service returned %s') % resp.status_code, 'type': 'warning'}}
|
|
data = resp.json() if resp.content else {}
|
|
|
|
# Check every required system individually.
|
|
# 'warming' for ollama means the model is loading into VRAM but
|
|
# Ollama itself is reachable — treat as passing so the bot goes
|
|
# online and the model loads on the first real request.
|
|
def _passes(system, value):
|
|
if system == 'ollama':
|
|
return value in ('ok', 'warming')
|
|
return value == 'ok'
|
|
checks = {s: _passes(s, data.get(s)) for s in self.REQUIRED_SYSTEMS}
|
|
failing = [s for s, ok in checks.items() if not ok]
|
|
|
|
if not failing:
|
|
self.write({'status': 'online', 'last_ping': fields.Datetime.now()})
|
|
self._sync_bot_user_presence(online=True)
|
|
return {'type': 'ir.actions.client', 'tag': 'display_notification',
|
|
'params': {'message': _('AI service is online — all systems operational'), 'type': 'success'}}
|
|
|
|
self.write({'status': 'error', 'last_ping': fields.Datetime.now()})
|
|
self._sync_bot_user_presence(online=False)
|
|
return {'type': 'ir.actions.client', 'tag': 'display_notification',
|
|
'params': {'message': _('AI service degraded — failing: %s') % ', '.join(failing), 'type': 'warning'}}
|
|
except Exception as exc:
|
|
self.write({'status': 'offline', 'last_ping': fields.Datetime.now()})
|
|
self._sync_bot_user_presence(online=False)
|
|
return {'type': 'ir.actions.client', 'tag': 'display_notification',
|
|
'params': {'message': _('AI service unreachable: %s') % exc, 'type': 'danger'}}
|
|
|
|
def dispatch_message(self, user_id, message, context=None, session_id=None):
|
|
self.ensure_one()
|
|
url = self._get_service_url() + '/dispatch'
|
|
payload = {
|
|
'user_id': str(user_id),
|
|
'message': message,
|
|
'context': context or {},
|
|
}
|
|
if session_id:
|
|
payload['session_id'] = session_id
|
|
try:
|
|
resp = requests.post(url, json=payload, headers=self._build_headers(), timeout=600)
|
|
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 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=600)
|
|
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):
|
|
for bot in self.search([('active', '=', True)]):
|
|
try:
|
|
bot.action_ping()
|
|
except Exception as exc:
|
|
_logger.warning('Ping failed for bot %s: %s', bot.id, exc)
|
|
|
|
@api.model
|
|
def _sync_bot_user_presence(self, online):
|
|
bot_user = self.env['res.users'].sudo().search(
|
|
[('login', 'in', ('activeblue_ai_bot', 'activeblue_ai_bot@local'))], limit=1)
|
|
if not bot_user:
|
|
return
|
|
if 'bus.presence' not in self.env:
|
|
return
|
|
try:
|
|
Presence = self.env['bus.presence']
|
|
now = fields.Datetime.now()
|
|
status = 'online' if online else 'offline'
|
|
if online:
|
|
poll_time = now + timedelta(minutes=10)
|
|
presence_time = now + timedelta(minutes=10)
|
|
else:
|
|
poll_time = now - timedelta(hours=1)
|
|
presence_time = now - timedelta(hours=1)
|
|
rec = Presence.sudo().search([('user_id', '=', bot_user.id)], limit=1)
|
|
if rec:
|
|
rec.write({'last_poll': poll_time, 'last_presence': presence_time})
|
|
# Force-update stored status column directly — Odoo 18 WebSocket-based
|
|
# presence doesn't trigger the stored compute when last_poll is written via ORM.
|
|
self.env.cr.execute(
|
|
"UPDATE bus_presence SET status = %s WHERE user_id = %s",
|
|
(status, bot_user.id),
|
|
)
|
|
rec.invalidate_recordset(['status'])
|
|
else:
|
|
Presence.sudo().create({
|
|
'user_id': bot_user.id,
|
|
'last_poll': poll_time,
|
|
'last_presence': presence_time,
|
|
})
|
|
self.env.cr.execute(
|
|
"UPDATE bus_presence SET status = %s WHERE user_id = %s",
|
|
(status, bot_user.id),
|
|
)
|
|
except Exception as exc:
|
|
_logger.warning('Could not update bot user presence: %s', exc)
|
|
|
|
|
|
class BusPresenceBot(models.Model):
|
|
_inherit = 'bus.presence'
|
|
|
|
def _compute_status(self):
|
|
super()._compute_status()
|
|
now = fields.Datetime.now()
|
|
for record in self:
|
|
if record.last_poll and record.last_poll > now:
|
|
record.status = 'online'
|