2025-07-09 08:23:03 -04:00
# -*- coding: utf-8 -*-
from odoo import models , fields , api
2025-07-28 13:27:29 -04:00
import logging
_logger = logging . getLogger ( __name__ )
2025-07-09 08:23:03 -04:00
class HelpdeskTeam ( models . Model ) :
_inherit = ' helpdesk.team '
use_ai_sale_orders = fields . Boolean (
string = ' Use AI for Sale Orders ' ,
2025-07-15 15:18:01 -04:00
help = ' If checked, the system will use AI to automatically generate sale orders from ticket descriptions for this team. This overrides the global setting. ' ,
2025-07-09 08:23:03 -04:00
default = False ,
)
2025-07-15 15:18:01 -04:00
ai_prompt_template_id = fields . Many2one (
' openwebui.prompt.template ' ,
domain = " [( ' template_type ' , ' = ' , ' helpdesk ' )] " ,
2025-07-09 08:23:03 -04:00
string = ' AI Prompt Template ' ,
2025-07-15 15:18:01 -04:00
help = ' Template for the prompt sent to the AI. If empty, the global template will be used. ' ,
)
def _get_use_ai_sale_orders ( self ) :
""" Get whether to use AI sale orders, considering both team and global settings """
self . ensure_one ( )
# If team has specific setting, use that
if self . use_ai_sale_orders :
return True
2025-07-28 13:27:29 -04:00
# Otherwise, check global setting (default to True)
param_value = self . env [ ' ir.config_parameter ' ] . sudo ( ) . get_param ( ' helpdesk_sale_order_ai.use_ai_sale_orders ' , ' True ' )
2025-07-15 15:18:01 -04:00
return param_value . lower ( ) == ' true ' if isinstance ( param_value , str ) else bool ( param_value )
def _get_ai_prompt_template ( self ) :
""" Get the AI prompt template to use for this team """
self . ensure_one ( )
2025-07-28 13:27:29 -04:00
try :
# Ensure template model is initialized
self . env [ ' openwebui.prompt.template ' ] . _ensure_default_template ( ' helpdesk ' )
2025-07-15 15:18:01 -04:00
2025-07-28 13:27:29 -04:00
# If team has a specific template, use it
if self . ai_prompt_template_id and self . ai_prompt_template_id . exists ( ) :
_logger . info ( f " Using team-specific prompt template: { self . ai_prompt_template_id . name } " )
return self . ai_prompt_template_id . content
2025-07-15 15:18:01 -04:00
2025-07-28 13:27:29 -04:00
# Otherwise, get the global default template
default_template = self . env [ ' openwebui.prompt.template ' ] . get_default_template ( ' helpdesk ' )
if default_template :
_logger . info ( f " Using default helpdesk prompt template: { default_template . name } " )
return default_template . content
except Exception as e :
_logger . error ( f " Error retrieving prompt template: { str ( e ) } " )
# Fallback to hardcoded template if all else fails
_logger . warning ( " Using fallback hardcoded prompt template " )
return """
2025-07-15 15:18:01 -04:00
Based on the following helpdesk ticket description , identify products and services that should be included in a sales order :
2025-07-09 08:23:03 -04:00
2025-07-15 15:18:01 -04:00
Ticket Description : { description }
2025-07-28 13:27:29 -04:00
Chatter Messages : { chatter_messages }
Attachments : { attachments_info }
Attachment Contents : { attachment_contents }
2025-07-09 08:23:03 -04:00
2025-07-15 15:18:01 -04:00
Please provide a list of products / services with quantities and descriptions in the following format :
Product / Service Name | Quantity | Description
"""