This commit is contained in:
2026-11-08 20:08:53 +08:00
parent 7526a5fe4d
commit 9b924747c5
2692 changed files with 394867 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
from . import ai_livechat_thread
from . import main
from . import cors

View File

@@ -0,0 +1,12 @@
from odoo.addons.ai.controllers.thread import AIThreadController
class AILivechatThreadController(AIThreadController):
def _get_channel_ai_session(self, channel):
if session := super()._get_channel_ai_session(channel):
return session
if channel.channel_type == 'livechat':
return self.env['ai.session'].sudo().create({
'agent_id': channel.sudo().ai_agent_id.id, 'channel_id': channel.id,
})
return session

View File

@@ -0,0 +1,2 @@
from . import main
from . import ai_controller

View File

@@ -0,0 +1,51 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.http import route
from odoo.addons.ai_livechat.controllers.ai_livechat_thread import AILivechatThreadController
class CorsLivechatController(AILivechatThreadController):
@route(
"/ai/cors/start_session_advance",
type="jsonrpc",
methods=["POST"],
auth="force_guest",
save_session=False,
cors="*",
)
def cors_start_session_advance(
self, guest_token, channel_id, mail_message_id,
current_view_info=None, ai_session_config=None, ai_session_identifier=None, debug=False,
):
return self.start_session_advance(
channel_id,
mail_message_id,
current_view_info=current_view_info,
ai_session_config=ai_session_config,
ai_session_identifier=ai_session_identifier,
debug=debug,
)
@route(
"/ai/cors/resume_pending_interaction",
type="jsonrpc",
methods=["POST"],
auth="force_guest",
save_session=False,
cors="*",
)
def cors_resume_pending_interaction(
self, guest_token, channel_id, session_id, resume_token,
response, current_view_info=None, ai_session_config=None, ai_session_identifier=None, debug=False,
):
return self.resume_pending_interaction(
channel_id,
session_id,
resume_token,
response=response,
current_view_info=current_view_info,
ai_session_config=ai_session_config,
ai_session_identifier=ai_session_identifier,
debug=debug,
)

View File

@@ -0,0 +1,11 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.http import route
from odoo.addons.ai_livechat.controllers.main import AILivechatController
class CorsLivechatController(AILivechatController):
@route("/ai_livechat/cors/forward_operator", type="jsonrpc", auth="force_guest", save_session=False, cors="*")
def cors_forward_operator(self, guest_token, channel_id):
return self.forward_operator(channel_id)

View File

@@ -0,0 +1,44 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from werkzeug.exceptions import BadRequest
from odoo import _
from odoo.http import request
from odoo.addons.mail.tools.discuss import mail_route
from odoo.addons.mail.tools.discuss import Store
from odoo.addons.im_livechat.controllers.main import LivechatController
class AILivechatController(LivechatController):
@mail_route('/ai_livechat/forward_operator', methods=["POST"], type="jsonrpc", auth='public')
def forward_operator(self, channel_id):
channel = request.env['discuss.channel'].search([('id', '=', channel_id)])
if not channel or channel.channel_type != 'livechat' or not channel.is_member:
raise BadRequest()
url = request.httprequest.headers.get("Referer")
# sudo() => visitor can retrieve the rule the matches the current URL
livechat_channel_rule = request.env['im_livechat.channel.rule'].sudo().match_rule(
channel_id=channel.livechat_channel_id.id,
url=url
)
# Forward the chat to a scripted chatbot if defined on the livechat channel rule.
# In such case, the scripted chatbot performs some steps first before forwarding to a human operator.
# sudo() => The visitor should be able to forward the chat to a scripted chatbot or a human operator
if livechat_channel_rule.chatbot_script_id:
channel.sudo()._forward_scripted_chatbot(chatbot_script_id=livechat_channel_rule.chatbot_script_id)
store = Store().add(channel, "_store_channel_fields")
return {"success": True, "store_data": store}
channel.sudo()._forward_human_operator()
# forwarding failed given that the ai agent is still a member of the channel.
if channel.sudo().ai_agent_id:
return {
'success': False,
'notification': _("There is no human agent available at the moment. Please try again later."),
"notification_type": "warning"
}
return {
'success': True,
'notification': _("The conversation has been forwarded successfully."),
"notification_type": "success"
}