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)