菜单跳转

This commit is contained in:
李鹏宇
2026-07-02 10:56:50 +08:00
parent 5657da3f65
commit 810cfd8e03
9 changed files with 127 additions and 34 deletions
+1
View File
@@ -37,6 +37,7 @@
'web.assets_backend': [
'yuthon_ai_agent/static/src/js/ai_chat_action.js',
'yuthon_ai_agent/static/src/js/ai_floating.js',
'yuthon_ai_agent/static/src/js/ai_chat_open.js',
'yuthon_ai_agent/static/src/xml/ai_chat_template.xml',
'yuthon_ai_agent/static/src/css/ai_chat.css',
'yuthon_ai_agent/static/src/css/ai_floating.css',
+12
View File
@@ -526,6 +526,18 @@ class AiConversation(models.Model):
Model.browse(ids).unlink()
return {'success': True, 'deleted_ids': ids}
# 自定义模型方法调用(主动感知按钮触发)
# action_config 格式: {"type": "execute_tool", "tool": "方法名", "params": {"record_id": 123, "model": "yuthon.notice"}}
if model_name and args.get('record_id'):
record = self.env[model_name].browse(args['record_id'])
if not record.exists():
return {'error': f'记录不存在:{model_name}, id={args["record_id"]}'}
method = getattr(record, name, None)
if not method or not callable(method):
return {'error': f'模型 {model_name} 没有可调用的方法:{name}'}
result = method()
return {'success': True, 'result': str(result) if result is not None else '执行成功'}
return {'error': f'未知工具:{name}'}
except Exception as e:
_logger.exception('Tool %s failed: %s', name, e)
+57 -7
View File
@@ -223,16 +223,66 @@ class AiRule(models.Model):
string='按钮文字',
default='确认执行',
)
guide_button_action = fields.Char(
string='按钮方法配置',
help='按钮点击后执行的动作,JSON格式,如 {"type": "execute_tool", "tool": "notice_publish", "params": {"notice_id": "{record_id}"}}',
default='{}',
)
guide_method_name = fields.Char(
string='方法名称',
help='后台处理方法名,用于标识触发哪个逻辑',
string='后台方法名',
help='按钮点击后调用的后台方法名,如 notice_send_to_all。后台自动传入 record_id(当前记录ID)',
)
guide_method_desc = fields.Char(
string='方法说明',
)
def action_test_guide(self):
"""测试主动感知:模拟触发,创建引导消息并打开AI对话"""
self.ensure_one()
if self.rule_type != 'active_guide':
return {'warning': {'title': '提示', 'message': '仅主动感知类型的规则可以测试'}}
# 获取或创建用户对话
conv = self.env['ai.conversation'].search([
('create_uid', '=', self.env.uid),
], limit=1, order='write_date desc')
if not conv:
provider = self.env['ai.provider'].search([('active', '=', True)], limit=1)
if not provider:
return {'warning': {'title': '错误', 'message': '请先配置 AI 服务商'}}
conv = self.env['ai.conversation'].create({
'name': 'AI 对话',
'provider_id': provider.id,
})
# 组装测试消息(替换变量为测试值)
message = self.guide_message or f'【测试】主动感知已触发'
message = message.replace('{record_name}', '[测试记录]')
message = message.replace('{user_name}', self.env.user.name)
message = message.replace('{record_id}', '[测试]')
# 组装 action config(按钮点击后的动作)
action_config = {
'type': 'execute_tool',
'tool': self.guide_method_name or '',
'params': {
'record_id': 0,
'model': self.guide_model or '',
}
}
# 创建 AI 消息(带 action 按钮)
self.env['ai.message'].create({
'conversation_id': conv.id,
'role': 'assistant',
'content': f'🧪 {message}',
'action': json.dumps({
'type': 'guide_button',
'label': self.guide_button_label or '确认执行',
'action': action_config,
}, ensure_ascii=False),
})
# 返回 client action 打开浮动聊天框
return {
'type': 'ir.actions.client',
'tag': 'yuthon_ai_chat_open',
'params': {'conversation_id': conv.id},
}
@@ -637,3 +637,16 @@ patch(NavBar.prototype, {
anchor.after(btn);
},
});
// 监听来自 client action / 其他模块的"打开聊天"事件
window.addEventListener("yuthon:ai_open_chat", async () => {
if (!isVisible) {
toggleChat();
}
// 刷新消息列表以显示新创建的测试消息
const msgs = await loadHistory();
if (chatEl) {
renderMessages(msgs);
scrollToBottom();
}
});
@@ -115,4 +115,9 @@
</div>
</div>
</t>
<!-- Client Action: 打开浮动聊天(空壳,仅用于触发事件) -->
<t t-name="yuthon_ai_agent.AiChatOpenTemplate">
<div class="d-none"/>
</t>
</templates>
+17 -4
View File
@@ -69,14 +69,27 @@
<!-- 主动感知配置 -->
<page string="主动感知配置" name="active_guide"
invisible="rule_type != 'active_guide'">
<div class="oe_button_box" style="margin-bottom: 12px;">
<button name="action_test_guide" type="object"
string="🧪 测试主动感知"
class="btn btn-primary oe_stat_button"
icon="fa-play-circle"
help="模拟触发此规则,查看引导内容和按钮效果"/>
</div>
<group>
<group string="引导内容">
<field name="guide_message"
placeholder="主动弹出时显示的文字,支持 {record_name} / {user_name} / {record_id}" />
<field name="guide_button_action"
placeholder='{"type": "execute_tool", "tool": "notice_publish", "params": {"notice_id": "{record_id}"}}' />
placeholder="主动弹出时显示的文字,支持 {record_name} / {user_name}" />
<field name="guide_button_label" />
</group>
<group string="按钮执行配置">
<field name="guide_method_name"
placeholder="后台方法名,如 notice_send_to_all" />
<field name="guide_method_desc"
placeholder="按钮对应后台方法的功能说明" />
placeholder="方法功能说明,方便维护" />
<p class="text-muted mt-2">
后台自动传入当前记录ID,无需手动配置参数
</p>
</group>
</group>
</page>