2023-11-03 07:30:09 -04:00
|
|
|
import { hbs } from "ember-cli-htmlbars";
|
2023-05-05 14:28:31 -04:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
2023-11-03 07:30:09 -04:00
|
|
|
import { withPluginApi } from "discourse/lib/plugin-api";
|
2023-08-30 02:15:03 -04:00
|
|
|
import { registerWidgetShim } from "discourse/widgets/render-glimmer";
|
2023-11-03 07:30:09 -04:00
|
|
|
import { composeAiBotMessage } from "discourse/plugins/discourse-ai/discourse/lib/ai-bot-helper";
|
2023-12-29 03:47:47 -05:00
|
|
|
import ShareModal from "../discourse/components/modal/share-modal";
|
2024-01-10 23:56:40 -05:00
|
|
|
import streamText from "../discourse/lib/ai-streamer";
|
2023-12-29 03:47:47 -05:00
|
|
|
import copyConversation from "../discourse/lib/copy-conversation";
|
|
|
|
|
|
|
|
const AUTO_COPY_THRESHOLD = 4;
|
2023-05-05 14:28:31 -04:00
|
|
|
|
|
|
|
function isGPTBot(user) {
|
2024-01-10 23:56:40 -05:00
|
|
|
return user && [-110, -111, -112, -113, -114, -115, -116].includes(user.id);
|
2023-05-05 14:28:31 -04:00
|
|
|
}
|
|
|
|
|
2023-05-16 13:38:21 -04:00
|
|
|
function attachHeaderIcon(api) {
|
|
|
|
const settings = api.container.lookup("service:site-settings");
|
|
|
|
|
2023-08-31 00:42:28 -04:00
|
|
|
const enabledBots = settings.ai_bot_add_to_header
|
2023-05-18 02:10:08 -04:00
|
|
|
? settings.ai_bot_enabled_chat_bots.split("|").filter(Boolean)
|
|
|
|
: [];
|
|
|
|
if (enabledBots.length > 0) {
|
|
|
|
api.attachWidgetAction("header", "showAiBotPanel", function () {
|
|
|
|
this.state.botSelectorVisible = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
api.attachWidgetAction("header", "hideAiBotPanel", function () {
|
|
|
|
this.state.botSelectorVisible = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
api.decorateWidget("header-icons:before", (helper) => {
|
|
|
|
return helper.attach("header-dropdown", {
|
2023-05-24 21:05:02 -04:00
|
|
|
title: "discourse_ai.ai_bot.shortcut_title",
|
2023-05-18 02:10:08 -04:00
|
|
|
icon: "robot",
|
|
|
|
action: "clickStartAiBotChat",
|
|
|
|
active: false,
|
|
|
|
classNames: ["ai-bot-button"],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
if (enabledBots.length === 1) {
|
|
|
|
api.attachWidgetAction("header", "clickStartAiBotChat", function () {
|
|
|
|
composeAiBotMessage(
|
|
|
|
enabledBots[0],
|
|
|
|
api.container.lookup("service:composer")
|
|
|
|
);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
api.attachWidgetAction("header", "clickStartAiBotChat", function () {
|
|
|
|
this.sendWidgetAction("showAiBotPanel");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
api.addHeaderPanel(
|
|
|
|
"ai-bot-header-panel-wrapper",
|
|
|
|
"botSelectorVisible",
|
|
|
|
function () {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
);
|
2023-05-16 13:38:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 14:28:31 -04:00
|
|
|
function initializeAIBotReplies(api) {
|
|
|
|
api.addPostMenuButton("cancel-gpt", (post) => {
|
|
|
|
if (isGPTBot(post.user)) {
|
|
|
|
return {
|
|
|
|
icon: "pause",
|
|
|
|
action: "cancelStreaming",
|
|
|
|
title: "discourse_ai.ai_bot.cancel_streaming",
|
|
|
|
className: "btn btn-default cancel-streaming",
|
|
|
|
position: "first",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
api.attachWidgetAction("post", "cancelStreaming", function () {
|
|
|
|
ajax(`/discourse-ai/ai-bot/post/${this.model.id}/stop-streaming`, {
|
|
|
|
type: "POST",
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
document
|
|
|
|
.querySelector(`#post_${this.model.post_number}`)
|
|
|
|
.classList.remove("streaming");
|
|
|
|
})
|
|
|
|
.catch(popupAjaxError);
|
|
|
|
});
|
|
|
|
|
|
|
|
api.modifyClass("controller:topic", {
|
|
|
|
pluginId: "discourse-ai",
|
|
|
|
|
|
|
|
onAIBotStreamedReply: function (data) {
|
2024-01-10 23:56:40 -05:00
|
|
|
streamText(this.model.postStream, data);
|
2023-05-05 14:28:31 -04:00
|
|
|
},
|
|
|
|
subscribe: function () {
|
|
|
|
this._super();
|
|
|
|
|
|
|
|
if (
|
|
|
|
this.model.isPrivateMessage &&
|
|
|
|
this.model.details.allowed_users &&
|
2023-05-11 09:03:03 -04:00
|
|
|
this.model.details.allowed_users.filter(isGPTBot).length >= 1
|
2023-05-05 14:28:31 -04:00
|
|
|
) {
|
2024-01-09 07:20:28 -05:00
|
|
|
// we attempt to recover the last message in the bus
|
|
|
|
// so we subscribe at -2
|
2023-05-05 14:28:31 -04:00
|
|
|
this.messageBus.subscribe(
|
|
|
|
`discourse-ai/ai-bot/topic/${this.model.id}`,
|
2024-01-09 07:20:28 -05:00
|
|
|
this.onAIBotStreamedReply.bind(this),
|
|
|
|
-2
|
2023-05-05 14:28:31 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
unsubscribe: function () {
|
2023-05-11 09:03:03 -04:00
|
|
|
this.messageBus.unsubscribe("discourse-ai/ai-bot/topic/*");
|
2023-05-05 14:28:31 -04:00
|
|
|
this._super();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-30 02:15:03 -04:00
|
|
|
function initializePersonaDecorator(api) {
|
|
|
|
let topicController = null;
|
|
|
|
api.decorateWidget(`poster-name:after`, (dec) => {
|
|
|
|
if (!isGPTBot(dec.attrs.user)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// this is hacky and will need to change
|
|
|
|
// trouble is we need to get the model for the topic
|
|
|
|
// and it is not available in the decorator
|
|
|
|
// long term this will not be a problem once we remove widgets and
|
|
|
|
// have a saner structure for our model
|
|
|
|
topicController =
|
|
|
|
topicController || api.container.lookup("controller:topic");
|
|
|
|
|
|
|
|
return dec.widget.attach("persona-flair", {
|
|
|
|
topicController,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
registerWidgetShim(
|
|
|
|
"persona-flair",
|
|
|
|
"span.persona-flair",
|
|
|
|
hbs`{{@data.topicController.model.ai_persona_name}}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-02-20 00:16:23 -05:00
|
|
|
const MAX_PERSONA_USER_ID = -1200;
|
|
|
|
|
2023-12-29 03:47:47 -05:00
|
|
|
function initializeShareButton(api) {
|
|
|
|
const currentUser = api.getCurrentUser();
|
|
|
|
if (!currentUser || !currentUser.ai_enabled_chat_bots) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let shareAiResponse = async function ({ post, showFeedback }) {
|
|
|
|
if (post.post_number <= AUTO_COPY_THRESHOLD) {
|
|
|
|
await copyConversation(post.topic, 1, post.post_number);
|
|
|
|
showFeedback("discourse_ai.ai_bot.conversation_shared");
|
|
|
|
} else {
|
|
|
|
modal.show(ShareModal, { model: post });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
api.addPostMenuButton("share", (post) => {
|
2024-02-20 00:16:23 -05:00
|
|
|
// for backwards compat so we don't break if topic is undefined
|
|
|
|
if (post.topic?.archetype !== "private_message") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-29 03:47:47 -05:00
|
|
|
if (
|
|
|
|
!currentUser.ai_enabled_chat_bots.any(
|
|
|
|
(bot) => post.username === bot.username
|
|
|
|
)
|
|
|
|
) {
|
2024-02-20 00:16:23 -05:00
|
|
|
// special handling for personas (persona bot users start at ID -1200 and go down)
|
|
|
|
if (post.user_id > MAX_PERSONA_USER_ID) {
|
|
|
|
return;
|
|
|
|
}
|
2023-12-29 03:47:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
action: shareAiResponse,
|
|
|
|
icon: "share",
|
|
|
|
className: "post-action-menu__share",
|
|
|
|
title: "discourse_ai.ai_bot.share",
|
|
|
|
position: "first",
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
const modal = api.container.lookup("service:modal");
|
|
|
|
}
|
|
|
|
|
2023-05-05 14:28:31 -04:00
|
|
|
export default {
|
|
|
|
name: "discourse-ai-bot-replies",
|
|
|
|
|
|
|
|
initialize(container) {
|
|
|
|
const settings = container.lookup("service:site-settings");
|
2023-05-16 13:38:21 -04:00
|
|
|
const user = container.lookup("service:current-user");
|
|
|
|
|
2023-09-03 21:52:44 -04:00
|
|
|
if (user?.ai_enabled_chat_bots) {
|
|
|
|
if (settings.ai_bot_add_to_header) {
|
|
|
|
withPluginApi("1.6.0", attachHeaderIcon);
|
|
|
|
}
|
2023-05-05 14:28:31 -04:00
|
|
|
withPluginApi("1.6.0", initializeAIBotReplies);
|
2023-08-30 02:15:03 -04:00
|
|
|
withPluginApi("1.6.0", initializePersonaDecorator);
|
2023-12-29 03:47:47 -05:00
|
|
|
withPluginApi("1.22.0", (api) => initializeShareButton(api, container));
|
2023-05-05 14:28:31 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|