Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";

export default class AiCancelStreamingButton extends Component {
// TODO (glimmer-post-menu): Remove this static function and move the code into the button action after the widget code is removed
static async cancelStreaming(post) {
@action
async cancelStreaming() {
const post = this.args.post;

try {
await ajax(`/discourse-ai/ai-bot/post/${post.id}/stop-streaming`, {
type: "POST",
Expand All @@ -20,11 +22,6 @@ export default class AiCancelStreamingButton extends Component {
}
}

@action
cancelStreaming() {
this.constructor.cancelStreaming(this.args.post);
}

<template>
<DButton
class="post-action-menu__ai-cancel-streaming cancel-streaming"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,11 @@ export default class AiDebugButton extends Component {
return isPostFromAiBot(args.post, args.state.currentUser);
}

// TODO (glimmer-post-menu): Remove this static function and move the code into the button action after the widget code is removed
static debugAiResponse(post, modal) {
modal.show(DebugAiModal, { model: post });
}

@service modal;

@action
debugAiResponse() {
this.constructor.debugAiResponse(this.args.post, this.modal);
this.modal.show(DebugAiModal, { model: this.args.post });
}

<template>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,20 @@ export default class AiDebugButton extends Component {
return isPostFromAiBot(args.post, args.state.currentUser);
}

// TODO (glimmer-post-menu): Remove this static function and move the code into the button action after the widget code is removed
static async shareAiResponse(post, modal, showFeedback) {
@service modal;

@action
async shareAiResponse() {
const post = this.args.post;

if (post.post_number <= AUTO_COPY_THRESHOLD) {
await copyConversation(post.topic, 1, post.post_number);
showFeedback("discourse_ai.ai_bot.conversation_shared");
this.args.showFeedback("discourse_ai.ai_bot.conversation_shared");
} else {
modal.show(ShareModal, { model: post });
this.modal.show(ShareModal, { model: post });
}
}

@service modal;

@action
shareAiResponse() {
this.constructor.shareAiResponse(
this.args.post,
this.modal,
this.args.showFeedback
);
}

<template>
<DButton
class="post-action-menu__share-ai"
Expand Down
100 changes: 4 additions & 96 deletions assets/javascripts/initializers/ai-bot-replies.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { hbs } from "ember-cli-htmlbars";
import { withSilencedDeprecations } from "discourse/lib/deprecated";
import { withPluginApi } from "discourse/lib/plugin-api";
import { registerWidgetShim } from "discourse/widgets/render-glimmer";
import AiBotHeaderIcon from "../discourse/components/ai-bot-header-icon";
import AiCancelStreamingButton from "../discourse/components/post-menu/ai-cancel-streaming-button";
import AiDebugButton from "../discourse/components/post-menu/ai-debug-button";
import AiShareButton from "../discourse/components/post-menu/ai-share-button";
import {
isPostFromAiBot,
showShareConversationModal,
} from "../discourse/lib/ai-bot-helper";
import { showShareConversationModal } from "../discourse/lib/ai-bot-helper";
import { streamPostText } from "../discourse/lib/ai-streamer/progress-handlers";

let enabledChatBotIds = [];
Expand Down Expand Up @@ -83,7 +79,7 @@ function initializePersonaDecorator(api) {
}

function initializePauseButton(api) {
const transformerRegistered = api.registerValueTransformer(
api.registerValueTransformer(
"post-menu-buttons",
({ value: dag, context: { post, firstButtonKey } }) => {
if (isGPTBot(post.user)) {
Expand All @@ -94,29 +90,6 @@ function initializePauseButton(api) {
}
}
);

const silencedKey =
transformerRegistered && "discourse.post-menu-widget-overrides";

withSilencedDeprecations(silencedKey, () => initializePauseWidgetButton(api));
}

function initializePauseWidgetButton(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 () {
AiCancelStreamingButton.cancelStreaming(this.model);
});
}

function initializeDebugButton(api) {
Expand All @@ -125,7 +98,7 @@ function initializeDebugButton(api) {
return;
}

const transformerRegistered = api.registerValueTransformer(
api.registerValueTransformer(
"post-menu-buttons",
({ value: dag, context: { post, firstButtonKey } }) => {
if (post.topic?.archetype === "private_message") {
Expand All @@ -136,38 +109,6 @@ function initializeDebugButton(api) {
}
}
);

const silencedKey =
transformerRegistered && "discourse.post-menu-widget-overrides";

withSilencedDeprecations(silencedKey, () => initializeDebugWidgetButton(api));
}

function initializeDebugWidgetButton(api) {
const currentUser = api.getCurrentUser();

let debugAiResponse = async function ({ post }) {
const modal = api.container.lookup("service:modal");
AiDebugButton.debugAiResponse(post, modal);
};

api.addPostMenuButton("debugAi", (post) => {
if (post.topic?.archetype !== "private_message") {
return;
}

if (!isPostFromAiBot(post, currentUser)) {
return;
}

return {
action: debugAiResponse,
icon: "info",
className: "post-action-menu__debug-ai",
title: "discourse_ai.ai_bot.debug_ai",
position: "first",
};
});
}

function initializeShareButton(api) {
Expand All @@ -176,7 +117,7 @@ function initializeShareButton(api) {
return;
}

const transformerRegistered = api.registerValueTransformer(
api.registerValueTransformer(
"post-menu-buttons",
({ value: dag, context: { post, firstButtonKey } }) => {
if (post.topic?.archetype === "private_message") {
Expand All @@ -186,39 +127,6 @@ function initializeShareButton(api) {
}
}
);

const silencedKey =
transformerRegistered && "discourse.post-menu-widget-overrides";

withSilencedDeprecations(silencedKey, () => initializeShareWidgetButton(api));
}

function initializeShareWidgetButton(api) {
const currentUser = api.getCurrentUser();

let shareAiResponse = async function ({ post, showFeedback }) {
const modal = api.container.lookup("service:modal");
AiShareButton.shareAiResponse(post, modal, showFeedback);
};

api.addPostMenuButton("share", (post) => {
// for backwards compat so we don't break if topic is undefined
if (post.topic?.archetype !== "private_message") {
return;
}

if (!isPostFromAiBot(post, currentUser)) {
return;
}

return {
action: shareAiResponse,
icon: "far-copy",
className: "post-action-menu__share-ai",
title: "discourse_ai.ai_bot.share",
position: "first",
};
});
}

function initializeShareTopicButton(api) {
Expand Down
Loading