diff --git a/app/controllers/discourse_ai/ai_bot/bot_controller.rb b/app/controllers/discourse_ai/ai_bot/bot_controller.rb index 3430e1db..f405dd65 100644 --- a/app/controllers/discourse_ai/ai_bot/bot_controller.rb +++ b/app/controllers/discourse_ai/ai_bot/bot_controller.rb @@ -69,6 +69,40 @@ module DiscourseAi render json: {}, status: 200 end + + def discover_continue_convo + raise Discourse::InvalidParameters.new("user_id") if !params[:user_id] + raise Discourse::InvalidParameters.new("query") if !params[:query] + raise Discourse::InvalidParameters.new("context") if !params[:context] + + user = User.find(params[:user_id]) + + bot_user_id = AiPersona.find_by(id: SiteSetting.ai_bot_discover_persona).user_id + bot_username = User.find_by(id: bot_user_id).username + + query = params[:query] + context = "[quote]\n#{params[:context]}\n[/quote]" + + post = + PostCreator.create!( + user, + title: + I18n.t("discourse_ai.ai_bot.discoveries.continue_conversation.title", query: query), + raw: + I18n.t( + "discourse_ai.ai_bot.discoveries.continue_conversation.raw", + query: query, + context: context, + ), + archetype: Archetype.private_message, + target_usernames: bot_username, + skip_validations: true, + ) + + render json: success_json.merge(topic_id: post.topic_id) + rescue StandardError => e + render json: failed_json.merge(errors: [e.message]), status: 422 + end end end end diff --git a/assets/javascripts/discourse/components/ai-search-discoveries.gjs b/assets/javascripts/discourse/components/ai-search-discoveries.gjs index 4af4249d..93ddb84e 100644 --- a/assets/javascripts/discourse/components/ai-search-discoveries.gjs +++ b/assets/javascripts/discourse/components/ai-search-discoveries.gjs @@ -10,11 +10,15 @@ import CookText from "discourse/components/cook-text"; import DButton from "discourse/components/d-button"; import concatClass from "discourse/helpers/concat-class"; import { ajax } from "discourse/lib/ajax"; +import { popupAjaxError } from "discourse/lib/ajax-error"; import { bind } from "discourse/lib/decorators"; import { withPluginApi } from "discourse/lib/plugin-api"; +import DiscourseURL from "discourse/lib/url"; +import Topic from "discourse/models/topic"; import { i18n } from "discourse-i18n"; import SmoothStreamer from "../lib/smooth-streamer"; import AiBlinkingAnimation from "./ai-blinking-animation"; +import AiIndicatorWave from "./ai-indicator-wave"; const DISCOVERY_TIMEOUT_MS = 10000; @@ -23,7 +27,11 @@ export default class AiSearchDiscoveries extends Component { @service messageBus; @service discobotDiscoveries; @service appEvents; + @service currentUser; + @service siteSettings; + @service composer; + @tracked loadingConversationTopic = false; @tracked hideDiscoveries = false; @tracked fullDiscoveryToggled = false; @tracked discoveryPreviewLength = this.args.discoveryPreviewLength || 150; @@ -145,6 +153,28 @@ export default class AiSearchDiscoveries extends Component { return !this.fullDiscoveryToggled && this.canShowExpandtoggle; } + get canContinueConversation() { + const personas = this.currentUser?.ai_enabled_personas; + const discoverPersona = personas.find( + (persona) => persona.id === this.siteSettings?.ai_bot_discover_persona + ); + const discoverPersonaHasBot = discoverPersona?.username; + + return ( + this.discobotDiscoveries.discovery?.length > 0 && + !this.smoothStreamer.isStreaming && + discoverPersonaHasBot + ); + } + + get continueConvoBtnLabel() { + if (this.loadingConversationTopic) { + return "discourse_ai.discobot_discoveries.loading_convo"; + } + + return "discourse_ai.discobot_discoveries.continue_convo"; + } + @action async triggerDiscovery() { if (this.discobotDiscoveries.lastQuery === this.query) { @@ -180,6 +210,43 @@ export default class AiSearchDiscoveries extends Component { this.fullDiscoveryToggled = !this.fullDiscoveryToggled; } + @action + async continueConversation() { + const data = { + user_id: this.currentUser.id, + query: this.query, + context: this.discobotDiscoveries.discovery, + }; + try { + this.loadingConversationTopic = true; + const continueRequest = await ajax( + `/discourse-ai/ai-bot/discover/continue-convo`, + { + type: "POST", + data, + } + ); + const topicJSON = await Topic.find(continueRequest.topic_id, {}); + const topic = Topic.create(topicJSON); + + DiscourseURL.routeTo(`/t/${continueRequest.topic_id}`, { + afterRouteComplete: () => { + if (this.args.closeSearchMenu) { + this.args.closeSearchMenu(); + } + + this.composer.focusComposer({ + topic, + }); + }, + }); + } catch (e) { + popupAjaxError(e); + } finally { + this.loadingConversationTopic = false; + } + } + timeoutDiscovery() { this.discobotDiscoveries.loadingDiscoveries = false; this.discobotDiscoveries.discovery = ""; @@ -226,6 +293,18 @@ export default class AiSearchDiscoveries extends Component { {{/if}} {{/if}} + + {{#if this.canContinueConversation}} +