66 lines
2.6 KiB
Python
66 lines
2.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
from odoo import api, fields, models
|
|
|
|
INTERFACE_KEYS = [
|
|
('chatter_ai_button', "Chatter AI button"),
|
|
('systray_ai_button', "Systray AI button"),
|
|
('html_field_ai', "HTML field assistant"),
|
|
('email_composer_ai', "Email composer assistant"),
|
|
('chatter_summarize', "Chatter summarize"),
|
|
]
|
|
|
|
|
|
class AIComposer(models.Model):
|
|
_name = 'ai.composer'
|
|
_description = "AI Composer"
|
|
_explanation = (
|
|
"Maps a UI entry point (a button, a field widget) to an agent plus a "
|
|
"default context prompt."
|
|
)
|
|
|
|
name = fields.Char(required=True, translate=True)
|
|
active = fields.Boolean(default=True)
|
|
interface_key = fields.Selection(selection=INTERFACE_KEYS, string="Action", required=True)
|
|
focused_model_id = fields.Many2one('ir.model', string="Model", ondelete='cascade')
|
|
ai_agent_id = fields.Many2one('ai.agent', string="Agent", required=True)
|
|
default_prompt = fields.Text("Context", translate=True)
|
|
is_system_default = fields.Boolean(
|
|
'System default', default=False, readonly=True, copy=False)
|
|
available_prompt_ids = fields.One2many(
|
|
'ai.prompt.button', 'composer_id', string="Suggested prompts")
|
|
|
|
_composer_uniq = models.Constraint(
|
|
'UNIQUE(interface_key, focused_model_id)',
|
|
"A composer already exists for this action and model.",
|
|
)
|
|
|
|
@api.model
|
|
def _get_composer(self, interface_key, res_model=None):
|
|
"""Find the most specific composer for an entry point."""
|
|
domain = [('interface_key', '=', interface_key), ('active', '=', True)]
|
|
if res_model:
|
|
model = self.env['ir.model']._get(res_model)
|
|
composer = self.search(domain + [('focused_model_id', '=', model.id)], limit=1)
|
|
if composer:
|
|
return composer
|
|
return self.search(domain + [('focused_model_id', '=', False)], limit=1)
|
|
|
|
@api.model
|
|
def _get_agent(self, res_model=None, interface_key=None):
|
|
composer = self._get_composer(interface_key or 'chatter_ai_button', res_model)
|
|
return composer.ai_agent_id or self.env.ref('dn_ai.ai_default_agent', raise_if_not_found=False)
|
|
|
|
def _get_initial_context(self, res_model=None, res_id=None):
|
|
self.ensure_one()
|
|
parts = []
|
|
if self.default_prompt:
|
|
parts.append(self.default_prompt)
|
|
if res_model and res_id:
|
|
record = self.env[res_model].browse(res_id).exists()
|
|
if record:
|
|
parts.append(self.env['ir.qweb']._render(
|
|
'dn_ai.ai_record_context',
|
|
{'record': record},
|
|
))
|
|
return '\n'.join(str(part) for part in parts if part)
|