# Part of Odoo. See LICENSE file for full copyright and licensing details. import logging from collections import defaultdict from textwrap import dedent from typing import Unpack import lxml.html from odoo import Command, _, api, fields, models from odoo.exceptions import MissingError, UserError, ValidationError from odoo.tools import BinaryBytes, SQL, file_open, is_html_empty from odoo.tools.misc import mute_logger from odoo.addons.iap import InsufficientCreditError from odoo.addons.mail.tools.discuss import Store from odoo.addons.ai.models.ai_agent_source import AIAgentSource from odoo.addons.ai.utils.ai_citation import ( apply_numeric_citations, get_sources_ids_from_text, ) from ..utils.ai_utils import ( get_text_from_parts, markdown_format, ) from ..utils.agent_instructions_prompts import ( DEFAULT_SYSTEM_PROMPT, get_global_protocol, RAG_SOURCES_FORMAT, RESTRICT_TO_SOURCES, SKILLS_PROTOCOL, ) from ..utils.types import AIMessageParts, CompletionOptions _logger = logging.getLogger(__name__) DEFAULT_TOOLS = [ "ai.ir_actions_server_load_skills", ] class AIAgent(models.Model): _name = 'ai.agent' _description = "AI Agent" _order = 'sequence, name' def _default_skill_ids(self): # a new agent starts with the built-in (native) skills on return self.env['ai.skill'].search([('is_native_skill', '=', True)]) active = fields.Boolean(default=True) sequence = fields.Integer(default=10, help="Built-in agents ship with a sequence below 10 to be listed first; custom agents default after them.") name = fields.Char(string="Agent Name", related='partner_id.name', required=True, readonly=False) subtitle = fields.Char(string="Description") system_prompt = fields.Text(string="System Prompt", help="Customize to control relevance and formatting.") restrict_to_sources = fields.Boolean( string="Restrict to Sources", help="If checked, the agent will only respond based on the provided sources.") image_128 = fields.Image("Image", related="partner_id.image_1920", max_width=128, max_height=128, readonly=False) avatar_128 = fields.Image("Avatar", related="partner_id.avatar_128") skill_ids = fields.Many2many( 'ai.skill', string="Skills", default=lambda self: self._default_skill_ids(), help="A skill includes instructions and tools that guide Odoo AI in helping the user complete their tasks.", ) partner_id = fields.Many2one('res.partner', required=True, ondelete='cascade', index=True) is_system_agent = fields.Boolean('System Agent', default=False) sources_ids = fields.One2many( 'ai.agent.source', 'agent_id', string="Sources", domain=[('parent_id', '=', False)], ) embedding_model = fields.Char() allowed_agent_ids = fields.Many2many('ai.agent', 'ai_agent_delegation_rel', 'parent_id', 'child_id', string="Allowed Agents") @api.model_create_multi def create(self, vals_list): install_mode = self.env.context.get('install_mode') embedding_model = None if install_mode else self.env["ai.embedding"]._get_default_embedding_model() for vals in vals_list: self._check_values_allowed(vals) partner = self.env['res.partner'].create({ 'name': vals.get('name'), 'active': False, }) vals['partner_id'] = partner.id if not install_mode: vals["embedding_model"] = embedding_model ai_agents = super().create(vals_list) agents_no_image = ai_agents.filtered(lambda agent: not agent.image_128) if agents_no_image: agents_no_image.image_128 = self._get_default_image() return ai_agents def write(self, vals): self._check_values_allowed(vals) if 'image_128' in vals and not vals['image_128']: vals['image_128'] = self._get_default_image() old_skills = {agent.id: agent.skill_ids for agent in self} result = super().write(vals) if agents_with_unlinked_skills := self.filtered(lambda agent: old_skills[agent.id] - agent.skill_ids): sessions = self.env['ai.session'].search([('agent_id', 'in', agents_with_unlinked_skills.ids)]) sessions._clean_session_state() return result @api.model def _get_default_image(self): with file_open('ai/static/description/icon.png', 'rb') as f: return BinaryBytes(f.read()) def _check_values_allowed(self, vals): if 'partner_id' in vals: raise ValidationError(_("The partner linked to an AI agent can't be changed")) @api.ondelete(at_uninstall=False) def _unlink_sources(self): """Delete sources (and their attachments) when an agent is deleted.""" for agent in self: all_source_records = agent._get_all_source_records() if all_source_records: all_source_records.unlink() @api.ondelete(at_uninstall=False) def _unlink_empty_ai_chat_channels(self): """Remove empty AI chats before deleting their agent.""" self.env['discuss.channel'].sudo().search([ ('ai_agent_id', 'in', self.ids), ('channel_type', '=', 'ai_chat'), ('has_message', '=', False), ]).unlink() @api.ondelete(at_uninstall=False) def _unlink_except_system_agent(self): """Prevent deletion of system agents.""" system_agents = self.filtered('is_system_agent') if system_agents: raise UserError(_("System agents cannot be deleted.")) def _get_all_source_records(self): """ Get all source records for the agent. :return: recordset of all source records :rtype: ai.agent.source recordset """ self.ensure_one() return self.sources_ids._get_descendants() | self.sources_ids @api.model def action_launch_ai_chat(self, interface_key, res_model=None, res_id=None, channel_title=None, text_selection=None, text_of_editable=None, reference_image_path=None): if res_model: res_id = int(res_id) # don't allow to create chat linked to a record the user cannot access self.env[res_model].browse(res_id).check_access('read') ai_composer = self.env['ai.composer']._get_composer_from_key_and_model(interface_key, res_model) if not (agent := ai_composer._get_agent(res_model, res_id)).exists(): raise MissingError(self.env._("AI not reachable, AI Agent not found.")) if not channel_title and res_model: channel_title = self.env[res_model].browse(res_id).display_name channel = agent._create_ai_chat_channel(channel_title) create_vals = { 'agent_id': agent.id, 'ai_composer_id': ai_composer.id, 'channel_id': channel.id, 'res_model': res_model, 'res_id': res_id, } if context_message := ai_composer._get_initial_context(res_model, res_id, text_selection, text_of_editable, reference_image_path): create_vals['event_ids'] = [ Command.create({ 'metadata': { 'role': 'user', 'content': context_message, }, }) ] self.env['ai.session'].sudo().create(create_vals) model_has_thread = res_model and isinstance(self.env[res_model], self.pool['mail.thread']) store = Store() store.add( channel, lambda res: ( res.attr('are_prompts_from_local_storage', False), res.from_method('_store_channel_fields'), res.many('ai_prompt_buttons', ['id', 'name', 'prompt', 'sequence'], value=ai_composer._get_random_prompts(3, not model_has_thread)), ) ) return { 'ai_channel_id': channel.id, 'data': store, 'model_has_thread': model_has_thread } def _build_rag_context(self, prompt): self.ensure_one() non_folder_sources = self._get_all_source_records().filtered(lambda s: not s.is_folder and s.is_active) if not non_folder_sources: return None context = "" embedding_model = self.embedding_model try: response = self.env['ai.embedding']._get_embeddings( input=[{'content': prompt}], model=embedding_model, mode='query', ) except InsufficientCreditError: return None embedding = response['embeddings'][0] target_per_model = non_folder_sources._get_target_per_model() similar_embeddings = self.env['ai.embedding']._get_similar_chunks( query_embedding=embedding, target_per_model=target_per_model, embedding_model=embedding_model, top_n=5, ) if not similar_embeddings: return None agent_sources = self.env['ai.agent.source'].with_context( skip_res_field_check=True, ).search([ ('agent_id', '=', self.id), ]) source_map = defaultdict(AIAgentSource) for source in agent_sources: if target := source._get_target_records(): source_map[target._name, target.id] = source for embedding in similar_embeddings: if agent_source := source_map.get((embedding.res_model, embedding.res_id)): context += ( f"(Source Chunk {agent_source.name})\n" f"(source_id: {agent_source.id})\n" f"{embedding.content}\n\n" ) return context def _get_llm_response_with_sources(self, llm_text_response): """ Parses inline citations (e.g., [SOURCE:210]) from each LLM message, replaces them with clickable sequential superscript numbers, and enriches the message content with a numbered list of corresponding source names and links. :param llm_text_response: The text response from the LLM :type llm_text_response: str :return: The text response with the sources added if any :rtype: str """ link_attrs = 'target="_blank" rel="noreferrer noopener"' unique_sources_ids = get_sources_ids_from_text(llm_text_response) source_data = {} accessible_sources = self.env['ai.agent.source'] if unique_sources_ids: sources = self.env['ai.agent.source'].search([ ('id', 'in', unique_sources_ids), ('agent_id', '=', self.id), ]) accessible_sources = sources.filtered(lambda s: s.user_has_access) for source in accessible_sources: source_data[source.id] = source return apply_numeric_citations(llm_text_response, source_data, link_attrs=link_attrs) return llm_text_response def _eval_ai_prompts(self, rendered_html, remove_prompts=False, ai_context=""): """Evaluate AI prompts in the given HTML content""" if is_html_empty(rendered_html): return rendered_html Wrapper = rendered_html.__class__ root = lxml.html.fromstring(rendered_html) prompt_containers = root.xpath("//div[hasclass('o_editor_prompt')]") root_is_prompt = root in prompt_containers if root_is_prompt: root = lxml.html.fromstring(f'