ai*
This commit is contained in:
5
ai_livechat/static/src/discuss/core/common/@types/models.d.ts
vendored
Normal file
5
ai_livechat/static/src/discuss/core/common/@types/models.d.ts
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
declare module "models" {
|
||||
export interface LivechatChannelRule {
|
||||
ai_agent_id: AiAgent;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
},
|
||||
});
|
||||
@@ -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>
|
||||
12
ai_livechat/static/src/discuss/core/common/composer_patch.js
Normal file
12
ai_livechat/static/src/discuss/core/common/composer_patch.js
Normal 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;
|
||||
},
|
||||
});
|
||||
@@ -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;
|
||||
},
|
||||
});
|
||||
@@ -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");
|
||||
},
|
||||
});
|
||||
@@ -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;
|
||||
},
|
||||
});
|
||||
12
ai_livechat/static/src/discuss/core/common/thread_patch.js
Normal file
12
ai_livechat/static/src/discuss/core/common/thread_patch.js
Normal 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;
|
||||
},
|
||||
});
|
||||
@@ -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);
|
||||
},
|
||||
});
|
||||
@@ -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");
|
||||
Reference in New Issue
Block a user