ai模块测试
This commit is contained in:
@@ -27,6 +27,7 @@
|
||||
'views/res_users_views.xml',
|
||||
'wizard/wecom_api_sync_wizard.xml',
|
||||
'views/wx_work_views.xml',
|
||||
'views/wecom_ai_views.xml',
|
||||
],
|
||||
# "assets": {
|
||||
# "web.assets_frontend": [
|
||||
|
||||
@@ -7,6 +7,7 @@ from . import res_users
|
||||
from . import wecom_apps_config
|
||||
from . import wx_work
|
||||
from . import wecom_ai_chat
|
||||
from . import wecom_ai_message_log
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -21,11 +21,18 @@ class WecomAiChat(models.Model):
|
||||
user_id = fields.Many2one('res.users', string='关联用户', index=True)
|
||||
conversation_id = fields.Many2one('ai.conversation', string='AI对话', index=True)
|
||||
last_message_time = fields.Datetime(string='最后消息时间')
|
||||
log_ids = fields.One2many('wecom.ai.message.log', 'chat_id', string='消息日志')
|
||||
log_count = fields.Integer(string='消息数', compute='_compute_log_count')
|
||||
|
||||
_sql_constraints = [
|
||||
('wecom_userid_unique', 'UNIQUE(wecom_userid)', '每个企业微信用户只能有一条AI对话记录'),
|
||||
]
|
||||
|
||||
@api.depends('log_ids')
|
||||
def _compute_log_count(self):
|
||||
for rec in self:
|
||||
rec.log_count = len(rec.log_ids)
|
||||
|
||||
@api.model
|
||||
def process_ai_message(self, wecom_userid, content):
|
||||
"""
|
||||
@@ -98,6 +105,9 @@ class WecomAiChat(models.Model):
|
||||
|
||||
self._send_wecom_reply(wecom_userid, reply_text)
|
||||
|
||||
# 6. 记录消息日志
|
||||
self._log_message(chat_record, wecom_userid, content, reply_text)
|
||||
|
||||
# 更新最后消息时间
|
||||
chat_record.write({'last_message_time': fields.Datetime.now()})
|
||||
_logger.info("企业微信AI消息处理完成: user=%s", wecom_userid)
|
||||
@@ -180,6 +190,21 @@ class WecomAiChat(models.Model):
|
||||
except Exception as e:
|
||||
_logger.exception("企业微信AI回复推送失败 [user=%s]: %s", wecom_userid, e)
|
||||
|
||||
def _log_message(self, chat_record, wecom_userid, user_content, ai_reply):
|
||||
"""记录企业微信AI消息日志"""
|
||||
try:
|
||||
self.env['wecom.ai.message.log'].sudo().create({
|
||||
'chat_id': chat_record.id if chat_record else False,
|
||||
'wecom_userid': wecom_userid,
|
||||
'user_id': chat_record.user_id.id if chat_record and chat_record.user_id else False,
|
||||
'user_message': user_content,
|
||||
'ai_reply': ai_reply,
|
||||
'message_time': fields.Datetime.now(),
|
||||
'status': 'success',
|
||||
})
|
||||
except Exception as e:
|
||||
_logger.warning("记录消息日志失败: %s", e)
|
||||
|
||||
@api.model
|
||||
def reset_conversation(self, wecom_userid):
|
||||
"""
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
企业微信 AI 消息日志
|
||||
记录通过企业微信与 AI 交互的每条消息
|
||||
"""
|
||||
|
||||
from odoo import models, fields
|
||||
|
||||
|
||||
class WecomAiMessageLog(models.Model):
|
||||
"""企业微信AI消息日志"""
|
||||
_name = 'wecom.ai.message.log'
|
||||
_description = '企业微信AI消息日志'
|
||||
_order = 'message_time desc'
|
||||
|
||||
chat_id = fields.Many2one('wecom.ai.chat', string='对话记录', index=True, ondelete='set null')
|
||||
wecom_userid = fields.Char(string='企业微信UserID', index=True)
|
||||
user_id = fields.Many2one('res.users', string='用户')
|
||||
user_name = fields.Char(string='用户名称', related='user_id.name', store=True)
|
||||
user_message = fields.Text(string='用户消息')
|
||||
ai_reply = fields.Text(string='AI回复')
|
||||
message_time = fields.Datetime(string='消息时间', default=fields.Datetime.now, index=True)
|
||||
status = fields.Selection([
|
||||
('success', '成功'),
|
||||
('error', '失败'),
|
||||
('timeout', '超时'),
|
||||
], string='状态', default='success')
|
||||
error_message = fields.Char(string='错误信息')
|
||||
@@ -3,5 +3,6 @@ access_wecom_api,wecom.api.user,model_wecom_api,,1,1,0,1
|
||||
access_wecom_apps,wecom.apps.user,model_wecom_apps,,1,1,1,1
|
||||
access__wecom_apps_config,wecom.app.config.user,model_wecom_apps_config,,1,1,1,1
|
||||
access_wecom_ai_chat,wecom.ai.chat.user,model_wecom_ai_chat,,1,1,1,1
|
||||
access_wecom_ai_message_log,wecom.ai.message.log.user,model_wecom_ai_message_log,,1,1,1,1
|
||||
|
||||
access_wecom_api_sync_wizard_manager,wecom.api.sync.wizard,model_wecom_api_sync_wizard,,1,1,1,1
|
||||
|
||||
|
@@ -0,0 +1,126 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<odoo>
|
||||
<!-- ============================================ -->
|
||||
<!-- 企业微信AI消息日志 - 列表视图 -->
|
||||
<!-- ============================================ -->
|
||||
<record id="view_wecom_ai_message_log_tree" model="ir.ui.view">
|
||||
<field name="name">wecom.ai.message.log.tree</field>
|
||||
<field name="model">wecom.ai.message.log</field>
|
||||
<field name="arch" type="xml">
|
||||
<tree string="企业微信AI消息日志" default_order="message_time desc">
|
||||
<field name="message_time"/>
|
||||
<field name="wecom_userid"/>
|
||||
<field name="user_name"/>
|
||||
<field name="user_message" widget="text"/>
|
||||
<field name="ai_reply" widget="text"/>
|
||||
<field name="status" decoration-success="status == 'success'" decoration-danger="status == 'error'" widget="badge"/>
|
||||
</tree>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<!-- ============================================ -->
|
||||
<!-- 企业微信AI消息日志 - 表单视图 -->
|
||||
<!-- ============================================ -->
|
||||
<record id="view_wecom_ai_message_log_form" model="ir.ui.view">
|
||||
<field name="name">wecom.ai.message.log.form</field>
|
||||
<field name="model">wecom.ai.message.log</field>
|
||||
<field name="arch" type="xml">
|
||||
<form string="消息详情" create="0" edit="0">
|
||||
<sheet>
|
||||
<group>
|
||||
<group string="基本信息">
|
||||
<field name="message_time"/>
|
||||
<field name="wecom_userid"/>
|
||||
<field name="user_id"/>
|
||||
<field name="status"/>
|
||||
<field name="error_message" invisible="status == 'success'"/>
|
||||
</group>
|
||||
<group string="关联信息">
|
||||
<field name="chat_id"/>
|
||||
</group>
|
||||
</group>
|
||||
<notebook>
|
||||
<page string="用户消息">
|
||||
<field name="user_message" readonly="1"/>
|
||||
</page>
|
||||
<page string="AI回复">
|
||||
<field name="ai_reply" readonly="1"/>
|
||||
</page>
|
||||
</notebook>
|
||||
</sheet>
|
||||
</form>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<!-- ============================================ -->
|
||||
<!-- 企业微信AI消息日志 - 搜索视图 -->
|
||||
<!-- ============================================ -->
|
||||
<record id="view_wecom_ai_message_log_search" model="ir.ui.view">
|
||||
<field name="name">wecom.ai.message.log.search</field>
|
||||
<field name="model">wecom.ai.message.log</field>
|
||||
<field name="arch" type="xml">
|
||||
<search string="搜索消息日志">
|
||||
<field name="wecom_userid"/>
|
||||
<field name="user_name"/>
|
||||
<field name="user_message"/>
|
||||
<filter name="today" string="今天" domain="[('message_time', '>=', (context_today()).strftime('%%Y-%%m-%%d'))]"/>
|
||||
<filter name="this_week" string="本周" domain="[('message_time', '>=', (context_today() - datetime.timedelta(days=7)).strftime('%%Y-%%m-%%d'))]"/>
|
||||
<separator/>
|
||||
<filter name="success" string="成功" domain="[('status', '=', 'success')]"/>
|
||||
<filter name="error" string="失败" domain="[('status', '=', 'error')]"/>
|
||||
<group expand="0" string="分组">
|
||||
<filter name="group_user" string="按用户" context="{'group_by': 'user_id'}"/>
|
||||
<filter name="group_date" string="按日期" context="{'group_by': 'message_time:day'}"/>
|
||||
<filter name="group_status" string="按状态" context="{'group_by': 'status'}"/>
|
||||
</group>
|
||||
</search>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<!-- ============================================ -->
|
||||
<!-- 企业微信AI消息日志 - Action & Menu -->
|
||||
<!-- ============================================ -->
|
||||
<record id="action_wecom_ai_message_log" model="ir.actions.act_window">
|
||||
<field name="name">企业微信消息日志</field>
|
||||
<field name="res_model">wecom.ai.message.log</field>
|
||||
<field name="view_mode">tree,form</field>
|
||||
<field name="context">{'search_default_today': 1}</field>
|
||||
</record>
|
||||
|
||||
<menuitem id="menu_wecom_ai_message_log"
|
||||
name="企业微信消息日志"
|
||||
parent="yuthon_ai_agent.menu_ai_root"
|
||||
action="action_wecom_ai_message_log"
|
||||
sequence="40"
|
||||
groups="yuthon_ai_agent.group_ai_agent_admin"/>
|
||||
|
||||
<!-- ============================================ -->
|
||||
<!-- 企业微信AI对话记录 - 列表视图 -->
|
||||
<!-- ============================================ -->
|
||||
<record id="view_wecom_ai_chat_tree" model="ir.ui.view">
|
||||
<field name="name">wecom.ai.chat.tree</field>
|
||||
<field name="model">wecom.ai.chat</field>
|
||||
<field name="arch" type="xml">
|
||||
<tree string="企业微信AI对话">
|
||||
<field name="wecom_userid"/>
|
||||
<field name="user_id"/>
|
||||
<field name="conversation_id"/>
|
||||
<field name="last_message_time"/>
|
||||
<field name="log_count" string="消息数"/>
|
||||
</tree>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<record id="action_wecom_ai_chat" model="ir.actions.act_window">
|
||||
<field name="name">企业微信AI对话</field>
|
||||
<field name="res_model">wecom.ai.chat</field>
|
||||
<field name="view_mode">tree,form</field>
|
||||
</record>
|
||||
|
||||
<menuitem id="menu_wecom_ai_chat"
|
||||
name="企业微信AI对话"
|
||||
parent="yuthon_ai_agent.menu_ai_root"
|
||||
action="action_wecom_ai_chat"
|
||||
sequence="41"
|
||||
groups="yuthon_ai_agent.group_ai_agent_admin"/>
|
||||
</odoo>
|
||||
Reference in New Issue
Block a user