feat(addon): show AI bot online in Discuss when agent is healthy
Extend cron_ping_all to upsert a bus.presence row for the activeblue_ai_bot user, mirroring the agent service's reachability: green dot when /health responds 200, offline otherwise. Drop the cron interval from 5m to 1m so the presence stays fresh (Odoo's presence client expects refreshes well under a minute).
This commit is contained in:
@@ -6,7 +6,7 @@
|
|||||||
<field name="model_id" ref="model_ab_ai_bot"/>
|
<field name="model_id" ref="model_ab_ai_bot"/>
|
||||||
<field name="state">code</field>
|
<field name="state">code</field>
|
||||||
<field name="code">model.cron_ping_all()</field>
|
<field name="code">model.cron_ping_all()</field>
|
||||||
<field name="interval_number">5</field>
|
<field name="interval_number">1</field>
|
||||||
<field name="interval_type">minutes</field>
|
<field name="interval_type">minutes</field>
|
||||||
<field name="active">True</field>
|
<field name="active">True</field>
|
||||||
</record>
|
</record>
|
||||||
|
|||||||
@@ -95,8 +95,33 @@ class AbAiBot(models.Model):
|
|||||||
|
|
||||||
@api.model
|
@api.model
|
||||||
def cron_ping_all(self):
|
def cron_ping_all(self):
|
||||||
|
any_online = False
|
||||||
for bot in self.search([('active', '=', True)]):
|
for bot in self.search([('active', '=', True)]):
|
||||||
try:
|
try:
|
||||||
bot.action_ping()
|
bot.action_ping()
|
||||||
|
if bot.status == 'online':
|
||||||
|
any_online = True
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
_logger.warning('Ping failed for bot %s: %s', bot.id, exc)
|
_logger.warning('Ping failed for bot %s: %s', bot.id, exc)
|
||||||
|
# Mirror agent-service health to the bot user's Discuss presence so it
|
||||||
|
# shows a green dot when the agent is reachable.
|
||||||
|
self._sync_bot_user_presence(online=any_online)
|
||||||
|
|
||||||
|
@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
|
||||||
|
Presence = self.env.get('bus.presence')
|
||||||
|
if Presence is None:
|
||||||
|
return
|
||||||
|
status = 'online' if online else 'offline'
|
||||||
|
now = fields.Datetime.now()
|
||||||
|
rec = Presence.sudo().search([('user_id', '=', bot_user.id)], limit=1)
|
||||||
|
vals = {'status': status, 'last_poll': now, 'last_presence': now}
|
||||||
|
if rec:
|
||||||
|
rec.write(vals)
|
||||||
|
else:
|
||||||
|
vals['user_id'] = bot_user.id
|
||||||
|
Presence.sudo().create(vals)
|
||||||
|
|||||||
Reference in New Issue
Block a user