# -*- coding: utf-8 -*- from odoo import api, fields, models from odoo.exceptions import UserError class AISkill(models.Model): _name = 'ai.skill' _description = "AI Skill" _explanation = ( "A skill bundles instructions and tools so the assistant can handle a " "specific kind of task." ) _order = 'sequence, name' name = fields.Char(string="Title", required=True, translate=True) sequence = fields.Integer(default=10) description = fields.Text( string="Description", translate=True, help="Shown to the model to decide whether this skill is relevant.", ) instructions = fields.Text( string="Instructions", translate=True, help="Injected in full once the skill is loaded.", ) tool_ids = fields.Many2many( 'ir.actions.server', 'ai_skill_tool_rel', 'skill_id', 'tool_id', string="AI Tools", domain="[('use_in_ai', '=', True)]", groups='base.group_system', ) type = fields.Char(string="Type", compute="_compute_type") is_native_skill = fields.Boolean( default=False, readonly=True, copy=False, help="Built-in skill with fixed behavior that cannot be edited.", ) _skill_name_uniq = models.Constraint( 'UNIQUE(name)', "A skill with this title already exists.", ) @api.depends('tool_ids') def _compute_type(self): for skill in self: skill.type = 'executable' if skill.tool_ids else 'guidance' @api.model_create_multi def create(self, vals_list): return super().create(vals_list) def _is_install_mode(self): """Data files are allowed to (re)write built-in skills when installing or upgrading the module; the UI is not.""" return bool(self.env.context.get('install_mode')) def write(self, vals): if (not self._is_install_mode() and set(vals) & {'name', 'instructions', 'tool_ids'} and any(skill.is_native_skill for skill in self)): raise UserError(self.env._( "Built-in skills cannot be modified. Duplicate the skill to customize it." )) return super().write(vals) def unlink(self): if not self._is_install_mode() and any(skill.is_native_skill for skill in self): raise UserError(self.env._("Built-in skills cannot be deleted.")) return super().unlink() def _get_instructions(self): self.ensure_one() return (self.instructions or '').strip()