diff --git a/yuthon_ai_agent/__manifest__.py b/yuthon_ai_agent/__manifest__.py index 2ef678c76..ef3cd5e23 100644 --- a/yuthon_ai_agent/__manifest__.py +++ b/yuthon_ai_agent/__manifest__.py @@ -37,7 +37,6 @@ '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/js/ai_rule_test.js', 'yuthon_ai_agent/static/src/xml/ai_chat_template.xml', 'yuthon_ai_agent/static/src/css/ai_chat.css', diff --git a/yuthon_ai_agent/models/ai_rule.py b/yuthon_ai_agent/models/ai_rule.py index 2502f370a..f0a4bf7ee 100644 --- a/yuthon_ai_agent/models/ai_rule.py +++ b/yuthon_ai_agent/models/ai_rule.py @@ -282,3 +282,10 @@ class AiRule(models.Model): return conv.id + def action_test_guide(self): + """按钮调用:创建测试消息,返回 conv_id 供 JS 打开聊天框""" + try: + conv_id = self._do_test_guide() + return {'conv_id': conv_id, 'message': '测试消息已创建,请查看聊天框'} + except UserError as e: + return {'error': str(e)} diff --git a/yuthon_ai_agent/static/src/js/ai_rule_test.js b/yuthon_ai_agent/static/src/js/ai_rule_test.js index ef76099fb..8559454da 100644 --- a/yuthon_ai_agent/static/src/js/ai_rule_test.js +++ b/yuthon_ai_agent/static/src/js/ai_rule_test.js @@ -1,37 +1,65 @@ /** @odoo-module **/ -import { jsonrpc } from "@web/core/network/rpc_service"; +import { patch } from "@web/core/utils/patch"; +import { FormController } from "@web/views/form/form_controller"; +import { _t } from "@web/core/l10n/translation"; +import { useService } from "@web/core/utils/hooks"; + +const { onWillStart } = owl; /** - * 主动感知规则表单:测试按钮点击处理 - * 由 XML 里 onclick="window._testActiveGuide(this)" 调用 + * 主动感知测试按钮 patch + * 拦截 action_test_guide 按钮点击,调用 RPC 后打开浮动聊天框 */ -window._testActiveGuide = async function (btnEl) { - const formEl = btnEl.closest(".o_form_view"); - if (!formEl) return; +patch(FormController.prototype, "yuthon_ai_agent/FormController", { + setup() { + this._super(...arguments); + this.notificationService = useService("notification"); + this.orm = useService("orm"); + }, - // 从 URL 获取 record id - const resId = (new URLSearchParams(window.location.search)).get("id"); - if (!resId) { - alert("无法获取规则 ID"); - return; - } + async onButtonAction(clickEvent, record, node) { + const methodName = node.getAttribute("name"); + if (methodName === "action_test_guide") { + clickEvent.preventDefault(); + clickEvent.stopPropagation(); - btnEl.disabled = true; - btnEl.textContent = "⏳ 测试中..."; + const buttonEl = clickEvent.currentTarget; + buttonEl.disabled = true; - try { - const res = await jsonrpc("/ai/rule/test_guide", { rule_id: parseInt(resId) }); - if (res.error) { - alert("错误: " + res.error); - } else { - window.dispatchEvent(new CustomEvent("yuthon:ai_open_chat", { - detail: { forceOpen: true }, - })); + try { + const result = await this.orm.call( + record.resModel, + "action_test_guide", + [record.resId] + ); + + if (result && result.conv_id) { + this.notificationService.add( + result.message || _t("测试消息已创建,请查看聊天框"), + { type: "success" } + ); + // 派发事件,通知浮动聊天框打开 + window.dispatchEvent( + new CustomEvent("yuthon:ai_open_chat", { + detail: { conversation_id: result.conv_id }, + }) + ); + } else if (result && result.error) { + this.notificationService.add(result.error, { + type: "danger", + }); + } + } catch (e) { + this.notificationService.add( + _t("测试失败:") + e.message, + { type: "danger" } + ); + } finally { + buttonEl.disabled = false; + } + return; } - } catch (e) { - alert("测试失败: " + e.message); - } finally { - btnEl.disabled = false; - } -}; + return this._super(...arguments); + }, +}); diff --git a/yuthon_ai_agent/views/ai_rule_views.xml b/yuthon_ai_agent/views/ai_rule_views.xml index 3e681094c..65eaea7cf 100644 --- a/yuthon_ai_agent/views/ai_rule_views.xml +++ b/yuthon_ai_agent/views/ai_rule_views.xml @@ -70,10 +70,10 @@
-