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,5 @@
declare module "models" {
export interface LivechatChannelRule {
ai_agent_id: AiAgent;
}
}

View File

@@ -0,0 +1,39 @@
import { ChatWindow } from "@mail/core/common/chat_window";
import { rpc } from "@web/core/network/rpc";
import { patch } from "@web/core/utils/patch";
patch(ChatWindow.prototype, {
get showForwardOperatorButton() {
const channel = this.props.chatWindow.channel;
return channel.channel_type === "livechat" && channel.ai_agent_id;
},
async forwardOperator(ev) {
let thread = this.props.chatWindow.channel.thread;
if (this.channel.channel_type !== "livechat") {
return;
}
// The livechat thread is persisted only after the first message is sent.
if (thread.isTransient) {
const channel = await this.store.env.services["im_livechat.livechat"].persist(thread);
thread = channel.thread;
if (!thread) {
return;
}
}
const result = await rpc("/ai_livechat/forward_operator", {
channel_id: thread.id,
});
if (result["store_data"]) {
this.store.insert(result["store_data"]);
}
if (result["notification"]) {
this.store.env.services.notification.add(result["notification"], {
type: result["notification_type"],
});
}
if (result["success"] === true) {
thread.resolveReadyToSwap();
}
},
});

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">
<t t-inherit="mail.ChatWindow" t-inherit-mode="extension">
<xpath expr="//Composer" position="before">
<div t-if="this.showForwardOperatorButton" class="m-3">
<button class="btn btn-primary" t-on-click="this.forwardOperator">Ask a Human</button>
</div>
</xpath>
</t>
</templates>

View File

@@ -0,0 +1,12 @@
import { Composer } from "@mail/core/common/composer";
import { patch } from "@web/core/utils/patch";
patch(Composer.prototype, {
get supportedFileTypes() {
if (this.thread?.channel?.ai_agent_id) {
// indexable files and image types supported by most AI providers
return "text/*,application/pdf,.docx,.pptx,.xlsx,.opendoc,.png,.jpg,.jpeg,.webp,.gif";
}
return super.supportedFileTypes;
},
});

View File

@@ -0,0 +1,11 @@
import { DiscussChannel } from "@mail/discuss/core/common/discuss_channel_model";
import { patch } from "@web/core/utils/patch";
patch(DiscussChannel.prototype, {
get allowCalls() {
if (this.ai_agent_id) {
return false;
}
return super.allowCalls;
},
});

View File

@@ -0,0 +1,11 @@
import { LivechatChannelRule } from "@im_livechat/core/common/livechat_channel_rule_model";
import { fields } from "@mail/model/export";
import { patch } from "@web/core/utils/patch";
patch(LivechatChannelRule.prototype, {
setup() {
super.setup(...arguments);
this.ai_agent_id = fields.One("ai.agent");
},
});

View File

@@ -0,0 +1,9 @@
import { Thread } from "@mail/core/common/thread_model";
import { patch } from "@web/core/utils/patch";
import { session } from "@web/session";
patch(Thread.prototype, {
get aiFromFrontend() {
return session.is_frontend || session.livechatData;
},
});

View File

@@ -0,0 +1,12 @@
import { Thread } from "@mail/core/common/thread";
import { patch } from "@web/core/utils/patch";
// AI chats in frontend do not share the backend's design. Hide steps from frontend
patch(Thread.prototype, {
get orderedMessages() {
if (this.channel?.ai_agent_id && this.props.thread?.aiFromFrontend) {
return super.orderedMessages.filter((msg) => !(msg.aiIsAgentStep || msg.aiIsToolUse));
}
return super.orderedMessages;
},
});

View File

@@ -0,0 +1,20 @@
import { LivechatService } from "@im_livechat/embed/common/livechat_service";
import { patch } from "@web/core/utils/patch";
patch(LivechatService.prototype, {
getSessionExtraParams(thread, options) {
const extraParams = super.getSessionExtraParams(thread, options);
// 1. The agent options.ai_agent_id has higher priority than the agent specified on
// the livechat rule matching the current URL.
extraParams["ai_agent_id"] =
options.ai_agent_id ?? this.store.livechat_rule?.ai_agent_id?.id;
return extraParams;
},
open(options = {}) {
const isAiAgentRule = Boolean(this.store.livechat_rule?.ai_agent_id);
if (isAiAgentRule) {
options.persist = true;
}
return super.open(options);
},
});

View File

@@ -0,0 +1,6 @@
import { livechatRoutingMap } from "@im_livechat/embed/cors/livechat_routing_map";
livechatRoutingMap
.add("/ai/start_session_advance", "/ai/cors/start_session_advance")
.add("/ai/resume_pending_interaction", "/ai/cors/resume_pending_interaction")
.add("/ai_livechat/forward_operator", "/ai_livechat/cors/forward_operator");