Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions ai_oca_bridge_chatter/README.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
.. image:: https://odoo-community.org/readme-banner-image
:target: https://odoo-community.org/get-involved?utm_source=readme
:alt: Odoo Community Association

=====================
Ai Oca Bridge Chatter
=====================
Expand All @@ -17,7 +13,7 @@ Ai Oca Bridge Chatter
.. |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/license-AGPL--3-blue.png
.. |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
Expand All @@ -37,6 +33,10 @@ This module allows usage of LLM chatbots inside Odoo.
The logic of the chatbot should be defined in an external system like
n8n.

When a user replies to a Discuss message and mentions a bot, the chatter
payload includes the quoted parent message so the agent can evaluate it
without receiving the rest of the channel history.

**Table of contents**

.. contents::
Expand Down
16 changes: 15 additions & 1 deletion ai_oca_bridge_chatter/models/ai_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def _prepare_payload_chatter(self, record=None, **kwargs):
record = self.env["mail.message"].search([], limit=1)
if record._name != "mail.message":
raise ValueError(_("The record must be a mail.message instance."))
parent = record.parent_id
return {
"message": {
"res_id": record.res_id,
Expand All @@ -29,6 +30,19 @@ def _prepare_payload_chatter(self, record=None, **kwargs):
"date": record.date.isoformat(),
"author_name": record.author_id.name,
"attachment_ids": record.attachment_ids.ids,
"parent_id": record.parent_id.id if record.parent_id else False,
"parent_id": parent.id if parent else False,
"parent": self._prepare_payload_chatter_parent(parent),
}
}

def _prepare_payload_chatter_parent(self, parent):
"""Serialize the replied-to message, if any, for the external agent."""
if not parent:
return False
return {
"id": parent.id,
"body": parent.body,
"author_id": parent.author_id.id,
"author_name": parent.author_id.name,
"date": parent.date.isoformat() if parent.date else False,
}
4 changes: 4 additions & 0 deletions ai_oca_bridge_chatter/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
This module allows usage of LLM chatbots inside Odoo.

The logic of the chatbot should be defined in an external system like n8n.

When a user replies to a Discuss message and mentions a bot, the chatter
payload includes the quoted parent message so the agent can evaluate it
without receiving the rest of the channel history.
29 changes: 13 additions & 16 deletions ai_oca_bridge_chatter/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils: https://docutils.sourceforge.io/" />
<title>README.rst</title>
<title>Ai Oca Bridge Chatter</title>
<style type="text/css">

/*
Expand Down Expand Up @@ -360,24 +360,22 @@
</style>
</head>
<body>
<div class="document">
<div class="document" id="ai-oca-bridge-chatter">
<h1 class="title">Ai Oca Bridge Chatter</h1>


<a class="reference external image-reference" href="https://odoo-community.org/get-involved?utm_source=readme">
<img alt="Odoo Community Association" src="https://odoo-community.org/readme-banner-image" />
</a>
<div class="section" id="ai-oca-bridge-chatter">
<h1>Ai Oca Bridge Chatter</h1>
<!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:57dc3cf709469bf2be35d088794b713d47b74b9aa9c9beb7f1f9f2cf088ee9d2
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/license-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/ai/tree/18.0/ai_oca_bridge_chatter"><img alt="OCA/ai" src="https://img.shields.io/badge/github-OCA%2Fai-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/ai-18-0/ai-18-0-ai_oca_bridge_chatter"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/ai&amp;target_branch=18.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/ai/tree/18.0/ai_oca_bridge_chatter"><img alt="OCA/ai" src="https://img.shields.io/badge/github-OCA%2Fai-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/ai-18-0/ai-18-0-ai_oca_bridge_chatter"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/ai&amp;target_branch=18.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module allows usage of LLM chatbots inside Odoo.</p>
<p>The logic of the chatbot should be defined in an external system like
n8n.</p>
<p>When a user replies to a Discuss message and mentions a bot, the chatter
payload includes the quoted parent message so the agent can evaluate it
without receiving the rest of the channel history.</p>
<p><strong>Table of contents</strong></p>
<div class="contents local topic" id="contents">
<ul class="simple">
Expand All @@ -392,7 +390,7 @@ <h1>Ai Oca Bridge Chatter</h1>
</ul>
</div>
<div class="section" id="configuration">
<h2><a class="toc-backref" href="#toc-entry-1">Configuration</a></h2>
<h1><a class="toc-backref" href="#toc-entry-1">Configuration</a></h1>
<p>On your external AI system create a workflow that will receive messages
and will return the call directly.</p>
<p>Here you can see an <a class="reference external" href="./static/description/Chat.json">example</a> of
Expand All @@ -404,23 +402,23 @@ <h2><a class="toc-backref" href="#toc-entry-1">Configuration</a></h2>
livechat without any issues.</p>
</div>
<div class="section" id="bug-tracker">
<h2><a class="toc-backref" href="#toc-entry-2">Bug Tracker</a></h2>
<h1><a class="toc-backref" href="#toc-entry-2">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/ai/issues">GitHub Issues</a>.
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
<a class="reference external" href="https://github.com/OCA/ai/issues/new?body=module:%20ai_oca_bridge_chatter%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
<h2><a class="toc-backref" href="#toc-entry-3">Credits</a></h2>
<h1><a class="toc-backref" href="#toc-entry-3">Credits</a></h1>
<div class="section" id="authors">
<h3><a class="toc-backref" href="#toc-entry-4">Authors</a></h3>
<h2><a class="toc-backref" href="#toc-entry-4">Authors</a></h2>
<ul class="simple">
<li>Dixmit</li>
</ul>
</div>
<div class="section" id="contributors">
<h3><a class="toc-backref" href="#toc-entry-5">Contributors</a></h3>
<h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
<ul class="simple">
<li><a class="reference external" href="https://www.dixmit.com">Dixmit</a><ul>
<li>Enric Tobella</li>
Expand All @@ -433,7 +431,7 @@ <h3><a class="toc-backref" href="#toc-entry-5">Contributors</a></h3>
</ul>
</div>
<div class="section" id="maintainers">
<h3><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h3>
<h2><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org">
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
Expand All @@ -446,6 +444,5 @@ <h3><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h3>
</div>
</div>
</div>
</div>
</body>
</html>
55 changes: 55 additions & 0 deletions ai_oca_bridge_chatter/tests/test_chatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,58 @@ def test_chat_ai_no_answer(self):
[("res_id", "=", self.channel.id), ("model", "=", "discuss.channel")]
),
)

def test_prepare_payload_without_parent(self):
"""A standalone message exposes parent as False."""
message = self.channel.with_user(self.user.id).message_post(
body="Standalone message",
)
payload = self.bridge._prepare_payload_chatter(record=message)
self.assertFalse(payload["message"]["parent_id"])
self.assertFalse(payload["message"]["parent"])

def test_prepare_payload_with_parent(self):
"""A reply includes the quoted parent body and author."""
parent = self.channel.message_post(
body="<p>Architect proposal</p>",
author_id=self.ai_user.partner_id.id,
)
reply = self.channel.with_user(self.user.id).message_post(
body="Please evaluate",
parent_id=parent.id,
)
payload = self.bridge._prepare_payload_chatter(record=reply)
self.assertEqual(payload["message"]["parent_id"], parent.id)
self.assertEqual(payload["message"]["parent"]["id"], parent.id)
self.assertIn("Architect proposal", payload["message"]["parent"]["body"])
self.assertEqual(
payload["message"]["parent"]["author_name"],
self.ai_user.partner_id.name,
)
self.assertEqual(
payload["message"]["parent"]["author_id"],
self.ai_user.partner_id.id,
)
self.assertTrue(payload["message"]["parent"]["date"])

def test_channel_reply_sends_parent(self):
"""Mentioning a bot in a reply posts the quoted message to the bridge."""
parent = self.channel.message_post(
body="<p>Architect proposal</p>",
author_id=self.ai_user.partner_id.id,
)
with mock.patch("requests.post") as mock_post:
mock_post.return_value = mock.Mock(
status_code=200, json=lambda: {"body": "My review"}
)
self.channel.with_user(self.user.id).message_post(
body="Please evaluate",
parent_id=parent.id,
partner_ids=[self.ai_user.partner_id.id],
)
mock_post.assert_called_once()
sent = mock_post.call_args.kwargs.get("json") or mock_post.call_args[1].get(
"json"
)
self.assertIn("Architect proposal", sent["message"]["parent"]["body"])
self.assertEqual(sent["message"]["parent"]["id"], parent.id)
Loading