diff --git a/ai_automation/README.rst b/ai_automation/README.rst new file mode 100644 index 00000000..951b0118 --- /dev/null +++ b/ai_automation/README.rst @@ -0,0 +1,110 @@ +============= +Ai Automation +============= + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:ac6598665c8047af8125789ebf827e8fd7744751b8aad883a0298b1b3c4be233 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fai-lightgray.png?logo=github + :target: https://github.com/OCA/ai/tree/18.0/ai_automation + :alt: OCA/ai +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/ai-18-0/ai-18-0-ai_automation + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/ai&target_branch=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module integrates AI connections with Odoo server actions, allowing +you to define AI-powered automations. By default it provides Ollama +support, but it can be extended with additional providers by adding new +``kind`` options to ``ai.connection``. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +This module adds a new server action type: **AI OCA Action**. + +To use it: + +1. Go to ``Settings > Technical > Actions > Server Actions`` +2. Create a new action and select **AI OCA Action** as the state +3. Select an AI Connection (Ollama) +4. Define the prompt — supports dynamic placeholders using Qweb syntax + (e.g. ``{{ object.name }}``) +5. Optionally select tools the AI can call during execution +6. Define what to do with the result: + + - **Post Message**: posts the AI response as a chatter message on the + record + - **Update Record**: writes the AI response to a specific field + +To extend with a new AI provider, inherit ``ai.connection`` and add a +new selection value to ``kind``, then implement the corresponding +``_run_{kind}`` method. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* Dixmit + +Contributors +------------ + +- `Dixmit `__ + + - Enric Tobella + +- `Vortex Dimensión Digital `__: + + - Jorge Rosado Julián + - Juan L. Sánchez + - Wang Zhong Jiang + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/ai `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/ai_automation/__init__.py b/ai_automation/__init__.py new file mode 100644 index 00000000..0650744f --- /dev/null +++ b/ai_automation/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/ai_automation/__manifest__.py b/ai_automation/__manifest__.py new file mode 100644 index 00000000..25cbd0a5 --- /dev/null +++ b/ai_automation/__manifest__.py @@ -0,0 +1,23 @@ +# Copyright 2026 Dixmit +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "Ai Automation", + "summary": """ + Integrate `ai_tools` with server actions to automate tasks using AI. + """, + "version": "18.0.1.0.0", + "license": "AGPL-3", + "author": "Dixmit,Odoo Community Association (OCA)", + "website": "https://github.com/OCA/ai", + "depends": ["ai_tool"], + "external_dependencies": { + "python": ["ollama"], + }, + "data": [ + "views/ir_actions_server.xml", + "security/ir.model.access.csv", + "views/ai_connection.xml", + ], + "demo": [], +} diff --git a/ai_automation/i18n/ai_automation.pot b/ai_automation/i18n/ai_automation.pot new file mode 100644 index 00000000..63e9c9ed --- /dev/null +++ b/ai_automation/i18n/ai_automation.pot @@ -0,0 +1,169 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * ai_automation +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 18.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: ai_automation +#: model:ir.actions.act_window,name:ai_automation.ai_connection_act_window +#: model:ir.model,name:ai_automation.model_ai_connection +#: model:ir.model.fields,field_description:ai_automation.field_ir_actions_server__ai_connection_id +#: model:ir.model.fields,field_description:ai_automation.field_ir_cron__ai_connection_id +#: model:ir.ui.menu,name:ai_automation.ai_connection_menu +msgid "AI Connection" +msgstr "" + +#. module: ai_automation +#: model_terms:ir.ui.view,arch_db:ai_automation.ir_actions_server_form_view +msgid "AI OCA" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields.selection,name:ai_automation.selection__ir_actions_server__state__ai_oca +msgid "AI OCA Action" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ir_actions_server__ai_prompt +#: model:ir.model.fields,field_description:ai_automation.field_ir_cron__ai_prompt +msgid "AI Prompt" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ir_actions_server__ai_result_action +#: model:ir.model.fields,field_description:ai_automation.field_ir_cron__ai_result_action +msgid "AI Result Action" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ir_actions_server__ai_tool_ids +#: model:ir.model.fields,field_description:ai_automation.field_ir_cron__ai_tool_ids +msgid "AI Tools" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ir_actions_server__ai_update_record_field_id +#: model:ir.model.fields,field_description:ai_automation.field_ir_cron__ai_update_record_field_id +msgid "AI Update Record Field" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ir_actions_server__state +#: model:ir.model.fields,field_description:ai_automation.field_ir_cron__state +msgid "Action To Do" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ai_connection__active +msgid "Active" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ai_connection__create_uid +msgid "Created by" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ai_connection__create_date +msgid "Created on" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ai_connection__display_name +msgid "Display Name" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ai_connection__id +msgid "ID" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ai_connection__kind +msgid "Kind" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ai_connection____last_update +msgid "Last Modified on" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ai_connection__write_uid +msgid "Last Updated by" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ai_connection__write_date +msgid "Last Updated on" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ir_actions_server__mailing_model_real +#: model:ir.model.fields,field_description:ai_automation.field_ir_cron__mailing_model_real +msgid "Mailing Model Real" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ai_connection__model +msgid "Model" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ai_connection__name +msgid "Name" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields.selection,name:ai_automation.selection__ai_connection__kind__ollama +msgid "Ollama" +msgstr "" + +#. module: ai_automation +#: model_terms:ir.ui.view,arch_db:ai_automation.ai_connection_form_view +msgid "Ollama configuration" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields.selection,name:ai_automation.selection__ir_actions_server__ai_result_action__post_message +msgid "Post Message" +msgstr "" + +#. module: ai_automation +#: model:ir.model,name:ai_automation.model_ir_actions_server +msgid "Server Action" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,help:ai_automation.field_ir_actions_server__state +#: model:ir.model.fields,help:ai_automation.field_ir_cron__state +msgid "" +"Type of server action. The following values are available:\n" +"- 'Execute Python Code': a block of python code that will be executed\n" +"- 'Create a new Record': create a new record with new values\n" +"- 'Update a Record': update the values of a record\n" +"- 'Execute several actions': define an action that triggers several other server actions\n" +"- 'Send Email': post a message, a note or send an email (Discuss)\n" +"- 'Add Followers': add followers to a record (Discuss)\n" +"- 'Create Next Activity': create an activity (Discuss)\n" +"- 'Send SMS Text Message': send SMS, log them on documents (SMS)" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields.selection,name:ai_automation.selection__ir_actions_server__ai_result_action__update_record +msgid "Update Record" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ai_connection__url +msgid "Url" +msgstr "" diff --git a/ai_automation/i18n/it.po b/ai_automation/i18n/it.po new file mode 100644 index 00000000..c5ad95bd --- /dev/null +++ b/ai_automation/i18n/it.po @@ -0,0 +1,170 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * ai_automation +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 18.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: Automatically generated\n" +"Language-Team: none\n" +"Language: it\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" + +#. module: ai_automation +#: model:ir.actions.act_window,name:ai_automation.ai_connection_act_window +#: model:ir.model,name:ai_automation.model_ai_connection +#: model:ir.model.fields,field_description:ai_automation.field_ir_actions_server__ai_connection_id +#: model:ir.model.fields,field_description:ai_automation.field_ir_cron__ai_connection_id +#: model:ir.ui.menu,name:ai_automation.ai_connection_menu +msgid "AI Connection" +msgstr "" + +#. module: ai_automation +#: model_terms:ir.ui.view,arch_db:ai_automation.ir_actions_server_form_view +msgid "AI OCA" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields.selection,name:ai_automation.selection__ir_actions_server__state__ai_oca +msgid "AI OCA Action" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ir_actions_server__ai_prompt +#: model:ir.model.fields,field_description:ai_automation.field_ir_cron__ai_prompt +msgid "AI Prompt" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ir_actions_server__ai_result_action +#: model:ir.model.fields,field_description:ai_automation.field_ir_cron__ai_result_action +msgid "AI Result Action" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ir_actions_server__ai_tool_ids +#: model:ir.model.fields,field_description:ai_automation.field_ir_cron__ai_tool_ids +msgid "AI Tools" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ir_actions_server__ai_update_record_field_id +#: model:ir.model.fields,field_description:ai_automation.field_ir_cron__ai_update_record_field_id +msgid "AI Update Record Field" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ir_actions_server__state +#: model:ir.model.fields,field_description:ai_automation.field_ir_cron__state +msgid "Action To Do" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ai_connection__active +msgid "Active" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ai_connection__create_uid +msgid "Created by" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ai_connection__create_date +msgid "Created on" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ai_connection__display_name +msgid "Display Name" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ai_connection__id +msgid "ID" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ai_connection__kind +msgid "Kind" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ai_connection____last_update +msgid "Last Modified on" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ai_connection__write_uid +msgid "Last Updated by" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ai_connection__write_date +msgid "Last Updated on" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ir_actions_server__mailing_model_real +#: model:ir.model.fields,field_description:ai_automation.field_ir_cron__mailing_model_real +msgid "Mailing Model Real" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ai_connection__model +msgid "Model" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ai_connection__name +msgid "Name" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields.selection,name:ai_automation.selection__ai_connection__kind__ollama +msgid "Ollama" +msgstr "" + +#. module: ai_automation +#: model_terms:ir.ui.view,arch_db:ai_automation.ai_connection_form_view +msgid "Ollama configuration" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields.selection,name:ai_automation.selection__ir_actions_server__ai_result_action__post_message +msgid "Post Message" +msgstr "" + +#. module: ai_automation +#: model:ir.model,name:ai_automation.model_ir_actions_server +msgid "Server Action" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,help:ai_automation.field_ir_actions_server__state +#: model:ir.model.fields,help:ai_automation.field_ir_cron__state +msgid "" +"Type of server action. The following values are available:\n" +"- 'Execute Python Code': a block of python code that will be executed\n" +"- 'Create a new Record': create a new record with new values\n" +"- 'Update a Record': update the values of a record\n" +"- 'Execute several actions': define an action that triggers several other server actions\n" +"- 'Send Email': post a message, a note or send an email (Discuss)\n" +"- 'Add Followers': add followers to a record (Discuss)\n" +"- 'Create Next Activity': create an activity (Discuss)\n" +"- 'Send SMS Text Message': send SMS, log them on documents (SMS)" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields.selection,name:ai_automation.selection__ir_actions_server__ai_result_action__update_record +msgid "Update Record" +msgstr "" + +#. module: ai_automation +#: model:ir.model.fields,field_description:ai_automation.field_ai_connection__url +msgid "Url" +msgstr "" diff --git a/ai_automation/models/__init__.py b/ai_automation/models/__init__.py new file mode 100644 index 00000000..f2ab448f --- /dev/null +++ b/ai_automation/models/__init__.py @@ -0,0 +1,2 @@ +from . import ai_connection +from . import ir_actions_server diff --git a/ai_automation/models/ai_connection.py b/ai_automation/models/ai_connection.py new file mode 100644 index 00000000..a417068d --- /dev/null +++ b/ai_automation/models/ai_connection.py @@ -0,0 +1,74 @@ +# Copyright 2026 Dixmit +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +import json + +import ollama + +from odoo import fields, models + + +class AiConnection(models.Model): + _name = "ai.connection" + _description = "AI Connection" + + name = fields.Char(required=True) + kind = fields.Selection([("ollama", "Ollama")], required=True, default="ollama") + active = fields.Boolean(default=True) + url = fields.Char(groups="base.group_system") + model = fields.Char(groups="base.group_system") + + def _run(self, prompt, tools=None, record=None): + return getattr(self, f"_run_{self.kind}")(prompt, tools=tools, record=record) + + def _run_ollama(self, prompt, tools=None, messages=None, record=None): + tool_definition = [] + for tool in tools or []: + definition = tool._get_tool_definition() + input_schema = definition["inputSchema"] + input_schema["additionalProperties"] = False + tool_definition.append( + { + "type": "function", + "function": { + "name": definition["name"], + "description": definition["description"], + "parameters": input_schema, + }, + } + ) + ollama_client = ollama.Client(**self._get_ollama_client_parameters()) + if messages is None: + messages = [] + messages.append({"role": "user", "content": prompt}) + while True: + response = ollama_client.chat( + model=self.model, + messages=messages, + tools=tool_definition, + ) + if not response.message.tool_calls: + return response.message.content + messages.append(response.message) + for call in response.message.tool_calls: + function_name = call.function.name + tool = tools.filtered(lambda t, name=function_name: t.name == name) + tool_output = tool._execute_tool( + record=record, **call.function.arguments + ) + if isinstance(tool_output, dict): + tool_output = json.dumps(tool_output) + messages.append( + { + "role": "tool", + "tool_name": function_name, + "content": tool_output, + } + ) + + def _get_ollama_client_parameters(self): + """ + We provide this hook so people can modify the client and other configurations + like headers and so on. + """ + return {"host": self.url, "headers": {}} diff --git a/ai_automation/models/ir_actions_server.py b/ai_automation/models/ir_actions_server.py new file mode 100644 index 00000000..ce65099b --- /dev/null +++ b/ai_automation/models/ir_actions_server.py @@ -0,0 +1,71 @@ +# Copyright 2026 Dixmit +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from lxml import etree + +from odoo import api, fields, models + +from odoo.addons.web_editor.models.ir_qweb_fields import html_to_text + + +class IrActionsServer(models.Model): + _inherit = "ir.actions.server" + + state = fields.Selection( + selection_add=[("ai_oca", "AI OCA Action")], ondelete={"ai_oca": "cascade"} + ) + ai_connection_id = fields.Many2one( + "ai.connection", string="AI Connection", groups="base.group_system" + ) + ai_tool_ids = fields.Many2many( + "ai.tool", + string="AI Tools", + groups="base.group_system", + ) + ai_prompt = fields.Html(string="AI Prompt", sanitize=False) + mailing_model_real = fields.Char(compute="_compute_mailing_model_real") + ai_result_action = fields.Selection( + [ + ("post_message", "Post Message"), + ("update_record", "Update Record"), + ], + string="AI Result Action", + ) + ai_update_record_field_id = fields.Many2one( + "ir.model.fields", + string="AI Update Record Field", + domain="[('model_id', '=', model_id), ('ttype', 'in', ['char', 'text'])]", + ) + + @api.depends("model_id") + def _compute_mailing_model_real(self): + for record in self: + record.mailing_model_real = ( + record.model_id.model if record.model_id else False + ) + + def _run_action_ai_oca(self, eval_context=None): + record = eval_context.get("record") + result = self.ai_connection_id._run( + self._get_ai_oca_prompt(record), tools=self.ai_tool_ids, record=record + ) + self._post_run_action_ai_oca(result, record) + + def _post_run_action_ai_oca(self, result, record): + if self.ai_result_action == "post_message": + self.env["ai.tool"]._ai_post_message(result, record=record) + elif ( + self.ai_result_action == "update_record" + and record + and self.ai_update_record_field_id + ): + record.write({self.ai_update_record_field_id.name: result}) + + def _get_ai_oca_prompt(self, record): + ai_prompt = self.ai_prompt + if record: + ai_prompt = str( + self.env["mail.render.mixin"]._render_template_qweb( + self.ai_prompt, record and record._name, record and record.ids + )[record.id] + ) + return html_to_text(etree.fromstring("" + ai_prompt + "")) diff --git a/ai_automation/pyproject.toml b/ai_automation/pyproject.toml new file mode 100644 index 00000000..4231d0cc --- /dev/null +++ b/ai_automation/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/ai_automation/readme/CONTRIBUTORS.md b/ai_automation/readme/CONTRIBUTORS.md new file mode 100644 index 00000000..eaf8ac56 --- /dev/null +++ b/ai_automation/readme/CONTRIBUTORS.md @@ -0,0 +1,6 @@ +- [Dixmit](https://www.dixmit.com) + - Enric Tobella +- [Vortex Dimensión Digital](https://www.dimensionvortex.com/): + - Jorge Rosado Julián \<\> + - Juan L. Sánchez \<\> + - Wang Zhong Jiang \<\> \ No newline at end of file diff --git a/ai_automation/readme/DESCRIPTION.md b/ai_automation/readme/DESCRIPTION.md new file mode 100644 index 00000000..7fa64139 --- /dev/null +++ b/ai_automation/readme/DESCRIPTION.md @@ -0,0 +1,3 @@ +This module integrates AI connections with Odoo server actions, allowing you to +define AI-powered automations. By default it provides Ollama support, but it can +be extended with additional providers by adding new `kind` options to `ai.connection`. diff --git a/ai_automation/readme/USAGE.md b/ai_automation/readme/USAGE.md new file mode 100644 index 00000000..e597ad9a --- /dev/null +++ b/ai_automation/readme/USAGE.md @@ -0,0 +1,14 @@ +This module adds a new server action type: **AI OCA Action**. + +To use it: +1. Go to `Settings > Technical > Actions > Server Actions` +2. Create a new action and select **AI OCA Action** as the state +3. Select an AI Connection (Ollama) +4. Define the prompt — supports dynamic placeholders using Qweb syntax (e.g. `{{ object.name }}`) +5. Optionally select tools the AI can call during execution +6. Define what to do with the result: + - **Post Message**: posts the AI response as a chatter message on the record + - **Update Record**: writes the AI response to a specific field + +To extend with a new AI provider, inherit `ai.connection` and add a new selection +value to `kind`, then implement the corresponding `_run_{kind}` method. \ No newline at end of file diff --git a/ai_automation/security/ir.model.access.csv b/ai_automation/security/ir.model.access.csv new file mode 100644 index 00000000..9d762bd9 --- /dev/null +++ b/ai_automation/security/ir.model.access.csv @@ -0,0 +1,3 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_ai_connection,access_ai_connection,model_ai_connection,base.group_user,1,0,0,0 +manage_ai_connection,manage_ai_connection,model_ai_connection,base.group_system,1,1,1,0 diff --git a/ai_automation/static/description/icon.png b/ai_automation/static/description/icon.png new file mode 100644 index 00000000..3a0328b5 Binary files /dev/null and b/ai_automation/static/description/icon.png differ diff --git a/ai_automation/static/description/index.html b/ai_automation/static/description/index.html new file mode 100644 index 00000000..44f359a0 --- /dev/null +++ b/ai_automation/static/description/index.html @@ -0,0 +1,458 @@ + + + + + +Ai Automation + + + +
+

Ai Automation

+ + +

Beta License: AGPL-3 OCA/ai Translate me on Weblate Try me on Runboat

+

This module integrates AI connections with Odoo server actions, allowing +you to define AI-powered automations. By default it provides Ollama +support, but it can be extended with additional providers by adding new +kind options to ai.connection.

+

Table of contents

+ +
+

Usage

+

This module adds a new server action type: AI OCA Action.

+

To use it:

+
    +
  1. Go to Settings > Technical > Actions > Server Actions
  2. +
  3. Create a new action and select AI OCA Action as the state
  4. +
  5. Select an AI Connection (Ollama)
  6. +
  7. Define the prompt — supports dynamic placeholders using Qweb syntax +(e.g. {{ object.name }})
  8. +
  9. Optionally select tools the AI can call during execution
  10. +
  11. Define what to do with the result:
      +
    • Post Message: posts the AI response as a chatter message on the +record
    • +
    • Update Record: writes the AI response to a specific field
    • +
    +
  12. +
+

To extend with a new AI provider, inherit ai.connection and add a +new selection value to kind, then implement the corresponding +_run_{kind} method.

+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Dixmit
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/ai project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/ai_automation/tests/__init__.py b/ai_automation/tests/__init__.py new file mode 100644 index 00000000..f1aa56d4 --- /dev/null +++ b/ai_automation/tests/__init__.py @@ -0,0 +1 @@ +from . import test_connection_ollama diff --git a/ai_automation/tests/test_connection_ollama.py b/ai_automation/tests/test_connection_ollama.py new file mode 100644 index 00000000..93335c27 --- /dev/null +++ b/ai_automation/tests/test_connection_ollama.py @@ -0,0 +1,134 @@ +# Copyright 2026 Dixmit +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from unittest import mock + +from ollama._types import ChatResponse + +from odoo.tests.common import TransactionCase + + +class OllamaClient: + def __init__(self, chat_messages=None): + self.chat_messages = chat_messages or [] + self.current_message = -1 + self.calls = [] + + def chat(self, *args, **kwargs): + self.current_message += 1 + self.calls.append((args, kwargs)) + return ChatResponse.model_validate( + {"message": self.chat_messages[self.current_message]} + ) + + +class TestConnectionOllama(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.connection = cls.env["ai.connection"].create( + { + "name": "Ollama Connection", + "url": "http://my_ollama_server:11434", + "model": "my_ollama_model", + "kind": "ollama", + } + ) + cls.action = cls.env["ir.actions.server"].create( + { + "name": "Test Action", + "model_id": cls.env.ref("base.model_res_partner").id, + "state": "ai_oca", + "ai_connection_id": cls.connection.id, + "ai_tool_ids": [(4, cls.env.ref("ai_tool.current_date").id)], + "ai_prompt": "What is the current date?", + } + ) + cls.partner = cls.env["res.partner"].create({"name": "Test Partner"}) + + def test_execute_action_with_tools(self): + client = OllamaClient( + [ + { + "role": "assistant", + "content": "", + "tool_calls": [ + { + "function": { + "name": "get_date", + "arguments": {}, + }, + "id": "1", + } + ], + }, + { + "role": "assistant", + "content": "Thanks it is 2024-01-01", + }, + ] + ) + with mock.patch("ollama.Client", return_value=client): + messages = self.partner.message_ids + self.action.with_context( + active_id=self.partner.id, active_model="res.partner" + ).run() + self.assertEqual(client.current_message, 1) + self.assertEqual(messages, self.partner.message_ids) + + def test_execute_action_post_message(self): + client = OllamaClient( + [ + { + "role": "assistant", + "content": "Thanks it is 2024-01-01", + }, + ] + ) + self.action.ai_result_action = "post_message" + with mock.patch("ollama.Client", return_value=client): + messages = self.partner.message_ids + self.action.with_context( + active_id=self.partner.id, active_model="res.partner" + ).run() + self.assertEqual(client.current_message, 0) + self.assertEqual(len(messages) + 1, len(self.partner.message_ids)) + self.assertRegex( + (self.partner.message_ids - messages).body, r".*2024-01-01.*" + ) + + def test_execute_action_field(self): + client = OllamaClient( + [ + { + "role": "assistant", + "content": "", + "tool_calls": [ + { + "function": { + "name": "get_date", + "arguments": {}, + }, + "id": "1", + } + ], + }, + { + "role": "assistant", + "content": "Thanks it is 2024-01-01", + }, + ] + ) + self.action.ai_result_action = "update_record" + self.action.ai_update_record_field_id = self.env.ref( + "base.field_res_partner__comment" + ) + self.partner.comment = "Initial Comment" + with mock.patch("ollama.Client", return_value=client): + messages = self.partner.message_ids + self.action.with_context( + active_id=self.partner.id, active_model="res.partner" + ).run() + self.assertEqual(client.current_message, 1) + self.assertEqual(messages, self.partner.message_ids) + self.assertRegex(self.partner.comment, r".*2024-01-01.*") diff --git a/ai_automation/views/ai_connection.xml b/ai_automation/views/ai_connection.xml new file mode 100644 index 00000000..261ecc95 --- /dev/null +++ b/ai_automation/views/ai_connection.xml @@ -0,0 +1,64 @@ + + + + + ai.connection + +
+
+ + + + + + + + + + + + + + + + + + + + ai.connection + + + + + + + + + ai.connection + + + + + + + + + + AI Connection + ai.connection + list,form + [] + {} + + + + AI Connection + + + + + diff --git a/ai_automation/views/ir_actions_server.xml b/ai_automation/views/ir_actions_server.xml new file mode 100644 index 00000000..bc7b4af5 --- /dev/null +++ b/ai_automation/views/ir_actions_server.xml @@ -0,0 +1,35 @@ + + + + + ir.actions.server + + + + + + + + + + + + + + + + +