feat(odoo): add activeblue_ai Odoo 18 module with OWL2 frontend

Models:
- ab.ai.bot: service URL, webhook secret, privacy mode, ping/dispatch
- ab.ai.directive: full directive lifecycle log with status tracking
- ab.ai.log: activity log with level/agent/record linkage
- ab.ai.agent.registry: agent list synced from agent service

Controllers:
- webhook.py: /ai/webhook/callback handles directive_completed, escalation, sweep_findings
- health_proxy.py: /ai/health proxies agent service detailed health
- approval.py: /ai/chat dispatch, /ai/approval/pending, /ai/approval/respond

Security:
- group_ai_user (chat) + group_ai_manager (configure, approve, logs)
- ir.model.access.csv for all 4 models

Views: list/form for bot, directives, logs, registry; main menu with AI brain icon

OWL2 frontend:
- systray_button.js: brain icon in top bar, status dot, pending approval badge
- ai_panel.js: slide-in chat panel, approval workflow, 30s poll for pending items
- CSS: slide-in animation, message bubbles, loading dots, approval section

Data: 4 cron jobs (ping, registry sync, directive/log cleanup)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
ActiveBlue Build
2026-04-12 17:59:02 -04:00
parent 430ab966b2
commit 29409ed71d
24 changed files with 1394 additions and 0 deletions

View File

@@ -0,0 +1 @@
from . import ab_ai_bot, ab_ai_directive, ab_ai_log, ab_ai_registry

View File

@@ -0,0 +1,102 @@
from odoo import models, fields, api, _
from odoo.exceptions import UserError
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
def action_ping(self):
self.ensure_one()
url = self._get_service_url() + '/health'
try:
resp = requests.get(url, timeout=5, headers=self._build_headers())
if resp.status_code == 200:
self.write({'status': 'online', 'last_ping': fields.Datetime.now()})
return {'type': 'ir.actions.client', 'tag': 'display_notification',
'params': {'message': _('AI service is online'), 'type': 'success'}}
else:
self.write({'status': 'error'})
return {'type': 'ir.actions.client', 'tag': 'display_notification',
'params': {'message': _('AI service returned %s') % resp.status_code, 'type': 'warning'}}
except Exception as exc:
self.write({'status': 'offline'})
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=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 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)

View File

@@ -0,0 +1,77 @@
from odoo import models, fields, api, _
import logging
_logger = logging.getLogger(__name__)
class AbAiDirective(models.Model):
_name = 'ab.ai.directive'
_description = 'AI Directive Log'
_order = 'create_date desc'
_rec_name = 'directive_id'
directive_id = fields.Char(string='Directive ID', required=True, index=True, readonly=True)
user_id = fields.Many2one('res.users', string='User', readonly=True, index=True)
message = fields.Text(string='User Message', readonly=True)
reply = fields.Text(string='AI Reply', readonly=True)
status = fields.Selection(
[
('pending', 'Pending'),
('running', 'Running'),
('pending_approval', 'Pending Approval'),
('approved', 'Approved'),
('rejected', 'Rejected'),
('completed', 'Completed'),
('failed', 'Failed'),
('timeout', 'Timeout'),
],
string='Status',
default='pending',
required=True,
index=True,
readonly=True,
)
agents_involved = fields.Char(string='Agents', readonly=True)
escalations = fields.Text(string='Escalations', readonly=True)
actions_taken = fields.Text(string='Actions Taken', readonly=True)
session_id = fields.Char(string='Session ID', readonly=True, index=True)
duration_ms = fields.Integer(string='Duration (ms)', readonly=True)
error_message = fields.Text(string='Error', readonly=True)
def action_view_detail(self):
self.ensure_one()
return {
'type': 'ir.actions.act_window',
'res_model': 'ab.ai.directive',
'res_id': self.id,
'view_mode': 'form',
'target': 'new',
}
@api.model
def record_directive(self, directive_id, user_id, message, reply, status,
agents=None, escalations=None, actions=None,
session_id=None, duration_ms=None, error=None):
import json
vals = {
'directive_id': directive_id,
'user_id': user_id,
'message': message,
'reply': reply,
'status': status,
'agents_involved': ', '.join(agents) if agents else '',
'escalations': json.dumps(escalations) if escalations else '',
'actions_taken': json.dumps(actions) if actions else '',
'session_id': session_id,
'duration_ms': duration_ms or 0,
'error_message': error or '',
}
return self.create(vals)
@api.model
def cron_cleanup_old(self):
cutoff = fields.Datetime.subtract(fields.Datetime.now(), days=90)
old = self.search([('create_date', '<', cutoff), ('status', 'in', ['completed', 'failed', 'timeout'])])
count = len(old)
old.unlink()
_logger.info('Cleaned up %d old directives', count)

View File

@@ -0,0 +1,46 @@
from odoo import models, fields, api
import logging
_logger = logging.getLogger(__name__)
class AbAiLog(models.Model):
_name = 'ab.ai.log'
_description = 'AI Activity Log'
_order = 'create_date desc'
_rec_name = 'summary'
summary = fields.Char(string='Summary', required=True, readonly=True)
level = fields.Selection(
[('info', 'Info'), ('warning', 'Warning'), ('error', 'Error'), ('debug', 'Debug')],
string='Level',
default='info',
required=True,
index=True,
)
agent_name = fields.Char(string='Agent', readonly=True, index=True)
directive_id = fields.Char(string='Directive ID', readonly=True, index=True)
user_id = fields.Many2one('res.users', string='User', readonly=True)
details = fields.Text(string='Details', readonly=True)
model_name = fields.Char(string='Model', readonly=True)
record_id = fields.Integer(string='Record ID', readonly=True)
@api.model
def log(self, summary, level='info', agent=None, directive_id=None,
user_id=None, details=None, model=None, record_id=None):
return self.create({
'summary': summary[:255] if summary else '',
'level': level,
'agent_name': agent or '',
'directive_id': directive_id or '',
'user_id': user_id,
'details': details or '',
'model_name': model or '',
'record_id': record_id or 0,
})
@api.model
def cron_cleanup(self):
cutoff = fields.Datetime.subtract(fields.Datetime.now(), days=30)
old = self.search([('create_date', '<', cutoff), ('level', 'in', ['info', 'debug'])])
old.unlink()

View File

@@ -0,0 +1,87 @@
from odoo import models, fields, api
import requests
import logging
_logger = logging.getLogger(__name__)
class AbAiAgentRegistry(models.Model):
_name = 'ab.ai.agent.registry'
_description = 'AI Agent Registry'
_rec_name = 'agent_name'
agent_name = fields.Char(string='Agent Name', required=True, index=True)
domain = fields.Char(string='Domain')
active = fields.Boolean(default=True, index=True)
backend = fields.Selection(
[('ollama', 'Ollama (Local)'), ('claude', 'Claude (Cloud)')],
string='LLM Backend',
default='ollama',
)
description = fields.Text(string='Description')
last_sweep = fields.Datetime(string='Last Sweep', readonly=True)
sweep_count = fields.Integer(string='Sweep Count', default=0, readonly=True)
error_count = fields.Integer(string='Error Count', default=0, readonly=True)
_sql_constraints = [
('agent_name_uniq', 'unique(agent_name)', 'Agent name must be unique'),
]
@api.model
def sync_from_service(self):
bot = self.env['ab.ai.bot'].search([('active', '=', True)], limit=1)
if not bot:
_logger.warning('No active bot — cannot sync registry')
return 0
url = bot._get_service_url() + '/registry/agents'
try:
resp = requests.get(url, headers=bot._build_headers(), timeout=10)
resp.raise_for_status()
agents = resp.json()
except Exception as exc:
_logger.error('Registry sync failed: %s', exc)
return 0
synced = 0
for agent_data in agents:
name = agent_data.get('name', '')
if not name:
continue
existing = self.search([('agent_name', '=', name)], limit=1)
vals = {
'agent_name': name,
'domain': agent_data.get('domain', ''),
'active': agent_data.get('active', True),
'backend': agent_data.get('backend', 'ollama'),
}
if existing:
existing.write(vals)
else:
self.create(vals)
synced += 1
_logger.info('Synced %d agents from service', synced)
return synced
def action_set_backend_claude(self):
self._set_backend('claude')
def action_set_backend_ollama(self):
self._set_backend('ollama')
def _set_backend(self, backend):
self.ensure_one()
bot = self.env['ab.ai.bot'].search([('active', '=', True)], limit=1)
if not bot:
return
url = bot._get_service_url() + '/registry/backend'
payload = {
'agent_name': self.agent_name,
'backend': backend,
'set_by': str(self.env.user.id),
}
try:
resp = requests.post(url, json=payload, headers=bot._build_headers(), timeout=10)
resp.raise_for_status()
self.write({'backend': backend})
except Exception as exc:
_logger.error('set_backend failed: %s', exc)