Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Let anonymous visitors preview the AI bot conversations page (#43923)

Kris65a9aa298ca5
diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/ai_bot/conversations_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/ai_bot/conversations_controller.rb
index c77a62913e3..99a817a60b3 100644
--- a/plugins/discourse-ai/app/controllers/discourse_ai/ai_bot/conversations_controller.rb
+++ b/plugins/discourse-ai/app/controllers/discourse_ai/ai_bot/conversations_controller.rb
@@ -6,7 +6,11 @@ module DiscourseAi
include AiCreditLimitHandler
requires_plugin PLUGIN_NAME
- requires_login
+ # `index` also serves the page's HTML, which anonymous visitors may load
+ # for a preview or a login prompt. This check runs after `check_xhr` has
+ # already rendered that HTML, so it only guards the conversation list.
+ requires_login except: %i[index]
+ before_action :ensure_logged_in, only: %i[index]
def index
ListConversations.call(service_params) do
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-anonymous-card.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-anonymous-card.gjs
new file mode 100644
index 00000000000..01ac1b97c15
--- /dev/null
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-anonymous-card.gjs
@@ -0,0 +1,22 @@
+import { on } from "@ember/modifier";
+import routeAction from "discourse/helpers/route-action";
+import dIcon from "discourse/ui-kit/helpers/d-icon";
+import { i18n } from "discourse-i18n";
+
+export default <template>
+ <button
+ class="ai-bot-anonymous-card"
+ type="button"
+ {{on "click" (routeAction "showLogin")}}
+ >
+ <span class="ai-bot-anonymous-card__icon">
+ {{dIcon "lock"}}
+ </span>
+ <span class="ai-bot-anonymous-card__title">
+ {{i18n "discourse_ai.ai_bot.conversations.preview.title"}}
+ </span>
+ <span class="ai-bot-anonymous-card__body">
+ {{i18n "discourse_ai.ai_bot.conversations.preview.body"}}
+ </span>
+ </button>
+</template>
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-conversations-layout.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-conversations-layout.gjs
new file mode 100644
index 00000000000..b991fab44b7
--- /dev/null
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-conversations-layout.gjs
@@ -0,0 +1,31 @@
+import PluginOutlet from "discourse/components/plugin-outlet";
+import bodyClass from "discourse/helpers/body-class";
+import lazyHash from "discourse/helpers/lazy-hash";
+import dIcon from "discourse/ui-kit/helpers/d-icon";
+import { i18n } from "discourse-i18n";
+
+export default <template>
+ <div class="ai-bot-conversations" ...attributes>
+ {{bodyClass "ai-bot-conversations-page"}}
+
+ <div class="ai-bot-conversations__content-wrapper">
+ <div class="ai-bot-conversations__title">
+ {{dIcon "far-discobot"}}
+ {{i18n "discourse_ai.ai_bot.conversations.header"}}
+ </div>
+ <PluginOutlet
+ @name="ai-bot-conversations-above-input"
+ @outletArgs={{lazyHash updateInput=@updateInput submit=@submit}}
+ />
+ <div class="ai-bot-conversations__input-container">
+ {{yield}}
+ </div>
+
+ <p class="ai-disclaimer">
+ {{i18n "discourse_ai.ai_bot.conversations.disclaimer"}}
+ </p>
+
+ {{yield to="footer"}}
+ </div>
+ </div>
+</template>
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-conversations-preview.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-conversations-preview.gjs
new file mode 100644
index 00000000000..ace17616801
--- /dev/null
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-conversations-preview.gjs
@@ -0,0 +1,66 @@
+import Component from "@glimmer/component";
+import { tracked } from "@glimmer/tracking";
+import { on } from "@ember/modifier";
+import { action } from "@ember/object";
+import { getOwner } from "@ember/owner";
+import { service } from "@ember/service";
+import DButton from "discourse/ui-kit/d-button";
+import { i18n } from "discourse-i18n";
+import AiBotAnonymousCard from "discourse/plugins/discourse-ai/discourse/components/ai-bot-anonymous-card";
+import AiBotConversationsLayout from "discourse/plugins/discourse-ai/discourse/components/ai-bot-conversations-layout";
+import { storeConversationDraft } from "discourse/plugins/discourse-ai/discourse/lib/ai-bot-conversation-draft";
+
+export default class AiBotConversationsPreview extends Component {
+ @service sessionStore;
+
+ @tracked inputValue = "";
+
+ @action
+ updateInput(value) {
+ this.inputValue = value?.target?.value ?? value;
+ storeConversationDraft(this.sessionStore, this.inputValue);
+ }
+
+ @action
+ handleKeyDown(event) {
+ if (event.key === "Enter" && !event.shiftKey && !event.isComposing) {
+ event.preventDefault();
+ this.showLogin();
+ }
+ }
+
+ @action
+ showLogin() {
+ getOwner(this).lookup("route:application").send("showLogin");
+ }
+
+ <template>
+ <AiBotConversationsLayout
+ class="--preview"
+ @submit={{this.showLogin}}
+ @updateInput={{this.updateInput}}
+ >
+ <:default>
+ <div class="ai-bot-conversations__input-wrapper">
+ <textarea
+ id="ai-bot-conversations-input"
+ placeholder={{i18n "discourse_ai.ai_bot.conversations.placeholder"}}
+ rows="1"
+ value={{this.inputValue}}
+ {{on "input" this.updateInput}}
+ {{on "keydown" this.handleKeyDown}}
+ />
+ <DButton
+ class="ai-bot-button btn-transparent ai-conversation-submit"
+ @action={{this.showLogin}}
+ @icon="paper-plane"
+ @title="discourse_ai.ai_bot.conversations.header"
+ />
+ </div>
+ </:default>
+ <:footer>
+ <AiBotAnonymousCard />
+ </:footer>
+ </AiBotConversationsLayout>
+ </template>
+}
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-conversations.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-conversations.gjs
index 1e5da6748a1..57e5fa09fbc 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-conversations.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-conversations.gjs
@@ -12,10 +12,7 @@ import { service } from "@ember/service";
import { trustHTML } from "@ember/template";
import { tagName } from "@ember-decorators/component";
import { modifier } from "ember-modifier";
-import PluginOutlet from "discourse/components/plugin-outlet";
import UserAutocompleteResults from "discourse/components/user-autocomplete-results";
-import bodyClass from "discourse/helpers/body-class";
-import lazyHash from "discourse/helpers/lazy-hash";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { SEND_SHORTCUT_META_ENTER } from "discourse/lib/constants";
import { hashtagAutocompleteOptions } from "discourse/lib/hashtag-autocomplete";
@@ -31,10 +28,11 @@ import {
import { clipboardHelpers, slugify } from "discourse/lib/utilities";
import DButton from "discourse/ui-kit/d-button";
import dConcatClass from "discourse/ui-kit/helpers/d-concat-class";
-import dIcon from "discourse/ui-kit/helpers/d-icon";
import dAutocomplete from "discourse/ui-kit/modifiers/d-autocomplete";
import { i18n } from "discourse-i18n";
import AiAgentLlmSelector from "discourse/plugins/discourse-ai/discourse/components/ai-agent-llm-selector";
+import AiBotConversationsLayout from "discourse/plugins/discourse-ai/discourse/components/ai-bot-conversations-layout";
+import { takeConversationDraft } from "discourse/plugins/discourse-ai/discourse/lib/ai-bot-conversation-draft";
@tagName("")
export default class AiBotConversations extends Component {
@@ -43,6 +41,7 @@ export default class AiBotConversations extends Component {
@service capabilities;
@service currentUser;
@service mediaOptimizationWorker;
+ @service sessionStore;
@service site;
@service siteSettings;
@service tooltip;
@@ -266,6 +265,7 @@ export default class AiBotConversations extends Component {
setTextArea(element) {
this.textarea = element;
this.setupAutocomplete(element);
+ this.#restoreDraft();
scheduleOnce("afterRender", this, this.focusTextarea);
}
@@ -387,6 +387,14 @@ export default class AiBotConversations extends Component {
}
}
+ #restoreDraft() {
+ const draft = takeConversationDraft(this.sessionStore);
+ if (draft) {
+ this.textarea.value = draft;
+ this.updateInputValue(draft);
+ }
+ }
+
async #checkCreditStatus(id, type) {
// Clear status immediately to prevent stale data
this.creditStatus = null;
@@ -422,85 +430,66 @@ export default class AiBotConversations extends Component {
}
<template>
- <div class="ai-bot-conversations" ...attributes>
- {{bodyClass "ai-bot-conversations-page"}}
-
- <div class="ai-bot-conversations__content-wrapper">
- <div class="ai-bot-conversations__title">
- {{dIcon "far-discobot"}}
- {{i18n "discourse_ai.ai_bot.conversations.header"}}
- </div>
- <PluginOutlet
- @name="ai-bot-conversations-above-input"
- @outletArgs={{lazyHash
- updateInput=this.updateInputValue
- submit=this.prepareAndSubmitToBot
+ <AiBotConversationsLayout
+ ...attributes
+ @submit={{this.prepareAndSubmitToBot}}
+ @updateInput={{this.updateInputValue}}
+ >
+ <:default>
+ <div
+ class={{dConcatClass
+ "ai-bot-conversations__input-wrapper"
+ (if this.isSubmitDisabled "--disabled")
}}
- />
- <div class="ai-bot-conversations__input-container">
- <div
- class={{dConcatClass
- "ai-bot-conversations__input-wrapper"
- (if this.isSubmitDisabled "--disabled")
- }}
- {{this.creditLimitTooltipModifier}}
- >
- <DButton
- class="btn btn-transparent ai-bot-upload-btn"
- @action={{unless this.isSubmitDisabled this.openFileUpload}}
- @disabled={{this.isSubmitDisabled}}
- @icon="upload"
- @title="discourse_ai.ai_bot.conversations.upload_files"
- />
- <textarea
- autofocus={{unless this.isSubmitDisabled "true"}}
- disabled={{if this.isSubmitDisabled true this.loading}}
- id="ai-bot-conversations-input"
- minlength="10"
- placeholder={{i18n
- "discourse_ai.ai_bot.conversations.placeholder"
- }}
- rows="1"
- {{didInsert this.setTextArea}}
- {{on "input" this.updateInputValue}}
- {{on "keydown" this.handleKeyDown}}
- {{on "beforeinput" this.handleBeforeInput}}
- />
- <DButton
- class="ai-bot-button btn-transparent ai-conversation-submit"
- @action={{unless
- this.isSubmitDisabled
- this.prepareAndSubmitToBot
- }}
- @disabled={{this.isSubmitDisabled}}
- @icon="paper-plane"
- @isLoading={{unless this.isSubmitDisabled this.loading}}
- @title="discourse_ai.ai_bot.conversations.header"
- />
- <input
- class="hidden-upload-field"
- id="ai-bot-file-uploader"
- multiple="multiple"
- type="file"
- {{didInsert this.registerFileInput}}
- />
- </div>
-
- <AiAgentLlmSelector
- @agentName={{@controller.agent}}
- @llmName={{@controller.llm}}
- @onSelectionChanged={{this.onSelectionChanged}}
- @setAgentId={{this.setAgentId}}
- @setLlmId={{this.setLlmId}}
- @setTargetRecipient={{this.setTargetRecipient}}
- @showLabels={{true}}
+ {{this.creditLimitTooltipModifier}}
+ >
+ <DButton
+ class="btn btn-transparent ai-bot-upload-btn"
+ @action={{unless this.isSubmitDisabled this.openFileUpload}}
+ @disabled={{this.isSubmitDisabled}}
+ @icon="upload"
+ @title="discourse_ai.ai_bot.conversations.upload_files"
+ />
+ <textarea
+ autofocus={{unless this.isSubmitDisabled "true"}}
+ disabled={{if this.isSubmitDisabled true this.loading}}
+ id="ai-bot-conversations-input"
+ minlength="10"
+ placeholder={{i18n "discourse_ai.ai_bot.conversations.placeholder"}}
+ rows="1"
+ {{didInsert this.setTextArea}}
+ {{on "input" this.updateInputValue}}
+ {{on "keydown" this.handleKeyDown}}
+ {{on "beforeinput" this.handleBeforeInput}}
+ />
+ <DButton
+ class="ai-bot-button btn-transparent ai-conversation-submit"
+ @action={{unless this.isSubmitDisabled this.prepareAndSubmitToBot}}
+ @disabled={{this.isSubmitDisabled}}
+ @icon="paper-plane"
+ @isLoading={{unless this.isSubmitDisabled this.loading}}
+ @title="discourse_ai.ai_bot.conversations.header"
+ />
+ <input
+ class="hidden-upload-field"
+ id="ai-bot-file-uploader"
+ multiple="multiple"
+ type="file"
+ {{didInsert this.registerFileInput}}
/>
</div>
- <p class="ai-disclaimer">
- {{i18n "discourse_ai.ai_bot.conversations.disclaimer"}}
- </p>
-
+ <AiAgentLlmSelector
+ @agentName={{@controller.agent}}
+ @llmName={{@controller.llm}}
+ @onSelectionChanged={{this.onSelectionChanged}}
+ @setAgentId={{this.setAgentId}}
+ @setLlmId={{this.setLlmId}}
+ @setTargetRecipient={{this.setTargetRecipient}}
+ @showLabels={{true}}
+ />
+ </:default>
+ <:footer>
{{#if this.showUploadsContainer}}
<div class="ai-bot-conversations__uploads-container">
{{#each this.uploads as |upload|}}
@@ -531,7 +520,7 @@ export default class AiBotConversations extends Component {
{{/each}}
</div>
{{/if}}
- </div>
- </div>
+ </:footer>
+ </AiBotConversationsLayout>
</template>
}
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-sidebar-new-conversation.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-sidebar-new-conversation.gjs
index b329291a1ed..6a848d03e50 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-sidebar-new-conversation.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-sidebar-new-conversation.gjs
@@ -5,12 +5,14 @@ import BackToForum from "discourse/components/sidebar/back-to-forum";
import getURL from "discourse/lib/get-url";
import DButton from "discourse/ui-kit/d-button";
import { AI_CONVERSATIONS_PANEL } from "../services/ai-conversations-sidebar-manager";
+import AiBotAnonymousCard from "./ai-bot-anonymous-card";
const TEXTAREA_ID = "ai-bot-conversations-input";
export default class AiBotSidebarNewConversation extends Component {
@service aiConversationsSidebarManager;
@service appEvents;
+ @service currentUser;
@service router;
@service sidebarState;
@service siteSettings;
@@ -50,14 +52,18 @@ export default class AiBotSidebarNewConversation extends Component {
{{#if this.shouldShowBackLink}}
<BackToForum @href={{this.backToForumHref}} />
{{/if}}
- <div class="ai-new-question-button__wrapper">
- <DButton
- class="ai-new-question-button btn-default"
- @action={{this.routeTo}}
- @icon="plus"
- @label="discourse_ai.ai_bot.conversations.new"
- />
- </div>
+ {{#if this.currentUser}}
+ <div class="ai-new-question-button__wrapper">
+ <DButton
+ class="ai-new-question-button btn-default"
+ @action={{this.routeTo}}
+ @icon="plus"
+ @label="discourse_ai.ai_bot.conversations.new"
+ />
+ </div>
+ {{else}}
+ <AiBotAnonymousCard />
+ {{/if}}
{{/if}}
</template>
}
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/controllers/discourse-ai-bot-conversations.gjs b/plugins/discourse-ai/assets/javascripts/discourse/controllers/discourse-ai-bot-conversations.gjs
index cab807b7de3..a70d8bf1996 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/controllers/discourse-ai-bot-conversations.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/controllers/discourse-ai-bot-conversations.gjs
@@ -1,8 +1,15 @@
import { tracked } from "@glimmer/tracking";
import Controller from "@ember/controller";
+import { service } from "@ember/service";
export default class DiscourseAiBotConversations extends Controller {
+ @service currentUser;
+
@tracked llm = null;
@tracked agent = null;
queryParams = ["llm", "agent"];
+
+ get showPreview() {
+ return !this.currentUser;
+ }
}
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-conversations-sidebar.js b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-conversations-sidebar.js
index 111272e2051..afaa7184279 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-conversations-sidebar.js
+++ b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-conversations-sidebar.js
@@ -8,7 +8,8 @@ export default {
initialize() {
withPluginApi((api) => {
const currentUser = api.container.lookup("service:current-user");
- if (!currentUser) {
+ const site = api.container.lookup("service:site");
+ if (!currentUser && !site.ai_bot_anonymous_preview) {
return;
}
@@ -41,6 +42,7 @@ export default {
// if the topic is not a private message, not created by the current user,
// or doesn't have a bot response, we don't need to override sidebar
if (
+ currentUser &&
topic?.archetype === "private_message" &&
topic.user_id === currentUser.id &&
topic.is_bot_pm
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-bot-conversation-draft.js b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-bot-conversation-draft.js
new file mode 100644
index 00000000000..913da61a79b
--- /dev/null
+++ b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-bot-conversation-draft.js
@@ -0,0 +1,15 @@
+const DRAFT_KEY = "ai-bot-conversations-draft";
+
+export function storeConversationDraft(sessionStore, text) {
+ if (text?.trim()) {
+ sessionStore.set({ key: DRAFT_KEY, value: text });
+ } else {
+ sessionStore.remove(DRAFT_KEY);
+ }
+}
+
+export function takeConversationDraft(sessionStore) {
+ const draft = sessionStore.get(DRAFT_KEY);
+ sessionStore.remove(DRAFT_KEY);
+ return draft;
+}
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/routes/discourse-ai-bot-conversations.js b/plugins/discourse-ai/assets/javascripts/discourse/routes/discourse-ai-bot-conversations.js
index 1a6019682a7..7104417d1e4 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/routes/discourse-ai-bot-conversations.js
+++ b/plugins/discourse-ai/assets/javascripts/discourse/routes/discourse-ai-bot-conversations.js
@@ -3,6 +3,7 @@ import DiscourseRoute from "discourse/routes/discourse";
export default class DiscourseAiBotConversationsRoute extends DiscourseRoute {
@service currentUser;
+ @service site;
queryParams = {
agent: { replace: true },
@@ -10,7 +11,7 @@ export default class DiscourseAiBotConversationsRoute extends DiscourseRoute {
};
beforeModel(transition) {
- if (!this.currentUser) {
+ if (!this.currentUser && !this.site.ai_bot_anonymous_preview) {
transition.send("showLogin");
}
}
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/services/ai-conversations-sidebar-manager.js b/plugins/discourse-ai/assets/javascripts/discourse/services/ai-conversations-sidebar-manager.js
index 25f72d65c96..9ed628831a2 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/services/ai-conversations-sidebar-manager.js
+++ b/plugins/discourse-ai/assets/javascripts/discourse/services/ai-conversations-sidebar-manager.js
@@ -22,6 +22,7 @@ const TITLE_CHANNEL = `/discourse-ai/ai-bot/topic-titles`;
export default class AiConversationsSidebarManager extends Service {
@service appEvents;
+ @service currentUser;
@service sidebarState;
@service messageBus;
@service routeHistory;
@@ -80,7 +81,9 @@ export default class AiConversationsSidebarManager extends Service {
this._attachScrollListener
);
- this._watchForTitleUpdates();
+ if (this.currentUser) {
+ this._watchForTitleUpdates();
+ }
}
willDestroy() {
@@ -122,6 +125,12 @@ export default class AiConversationsSidebarManager extends Service {
this.sidebarState.setSeparatedMode();
this.sidebarState.hideSwitchPanelButtons();
+ // Anonymous visitors get the panel for its login prompt, but have no
+ // conversations to list.
+ if (!this.currentUser) {
+ return true;
+ }
+
// don't render sidebar multiple times
if (this._didInit) {
this._rebuildSections();
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/templates/discourse-ai-bot-conversations.gjs b/plugins/discourse-ai/assets/javascripts/discourse/templates/discourse-ai-bot-conversations.gjs
index b009b3aa44e..4c40348d0c5 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/templates/discourse-ai-bot-conversations.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/templates/discourse-ai-bot-conversations.gjs
@@ -1,5 +1,10 @@
import AiBotConversations from "discourse/plugins/discourse-ai/discourse/components/ai-bot-conversations";
+import AiBotConversationsPreview from "discourse/plugins/discourse-ai/discourse/components/ai-bot-conversations-preview";
export default <template>
- <AiBotConversations @controller={{@controller}} />
+ {{#if @controller.showPreview}}
+ <AiBotConversationsPreview />
+ {{else}}
+ <AiBotConversations @controller={{@controller}} />
+ {{/if}}
</template>
diff --git a/plugins/discourse-ai/assets/stylesheets/modules/ai-bot-conversations/common.scss b/plugins/discourse-ai/assets/stylesheets/modules/ai-bot-conversations/common.scss
index 6602b6998ef..7dc7d15ce2c 100644
--- a/plugins/discourse-ai/assets/stylesheets/modules/ai-bot-conversations/common.scss
+++ b/plugins/discourse-ai/assets/stylesheets/modules/ai-bot-conversations/common.scss
@@ -142,7 +142,9 @@ body.has-ai-conversations-sidebar {
}
}
}
+}
+body.ai-bot-conversations-page {
.ai-bot-conversations {
--input-max-width: 46em;
display: flex;
@@ -417,8 +419,16 @@ body.has-ai-conversations-sidebar {
}
}
}
+
+ &.--preview #ai-bot-conversations-input {
+ padding-inline-start: var(--space-3);
+ border-radius: var(--d-input-border-radius) 0 0
+ var(--d-input-border-radius);
+ }
}
+}
+body.has-ai-conversations-sidebar {
// custom user card link
.user-card-meta__profile-link {
display: block;
@@ -505,6 +515,85 @@ body.has-ai-conversations-sidebar {
}
}
+.ai-bot-anonymous-card {
+ display: grid;
+ grid-template-columns: auto 1fr;
+ grid-template-areas:
+ "icon title"
+ "body body";
+ align-items: center;
+ gap: 0 var(--space-3);
+ box-sizing: border-box;
+ width: calc(100% - 2 * var(--space-4));
+ margin: var(--space-4) var(--space-4) 0;
+ padding: var(--space-4);
+ border: 1px solid var(--tertiary-medium);
+ background: rgb(var(--tertiary-rgb), 0.075);
+ box-shadow: var(--shadow-card);
+ border-radius: var(--d-border-radius-large);
+ font: inherit;
+ text-align: start;
+ cursor: pointer;
+
+ @include viewport.until(sm) {
+ margin-bottom: var(--space-4);
+ gap: var(--space-3);
+ }
+
+ &:hover,
+ &:focus-visible {
+ border-color: var(--tertiary);
+ background: rgb(var(--tertiary-rgb), 0.15);
+ }
+
+ .sidebar-container & {
+ flex-shrink: 0;
+ width: calc(var(--d-sidebar-width) - 2 * var(--space-4));
+ gap: var(--space-3);
+ }
+
+ .ai-bot-conversations & {
+ grid-template-areas:
+ "icon title"
+ "icon body";
+ width: 100%;
+ max-width: var(--input-max-width);
+ margin: 0;
+ align-items: start;
+
+ .ai-bot-anonymous-card__icon {
+ margin-top: var(--space-1);
+ }
+
+ body.has-sidebar-page & {
+ display: none;
+ }
+ }
+
+ &__icon {
+ grid-area: icon;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ border-radius: 50%;
+ background-color: var(--tertiary);
+ color: var(--secondary);
+ width: 2rem;
+ height: 2rem;
+ }
+
+ &__title {
+ grid-area: title;
+ font-weight: 700;
+ }
+
+ &__body {
+ grid-area: body;
+ color: var(--primary-high);
+ font-size: var(--font-down-1);
+ }
+}
+
.user-navigation .user-nav__messages-ai-bot-chats {
.d-icon.d-icon-discobot {
font-size: var(--font-0);
diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.yml
index 47eab273ca6..a23f621e262 100644
--- a/plugins/discourse-ai/config/locales/client.en.yml
+++ b/plugins/discourse-ai/config/locales/client.en.yml
@@ -1632,6 +1632,9 @@ en:
"1": "Mixtral-8x7B V0.1"
conversations:
homepage_option: "AI bot conversations"
+ preview:
+ title: "Log in to ask a question"
+ body: "Get answers from AI, and keep your conversations here to come back to later."
header: "What can I help with?"
submit: "Submit question"
disclaimer: "Generative AI can make mistakes. Verify important information."
diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.yml
index f4da4ee4cd9..b1ddcdaef9e 100644
--- a/plugins/discourse-ai/config/locales/server.en.yml
+++ b/plugins/discourse-ai/config/locales/server.en.yml
@@ -156,7 +156,7 @@ en:
ai_bot_enabled: "Enable the AI Bot module."
ai_bot_enabled_llms: "LLMs available to select for AI bot conversations"
ai_bot_enable_chat_warning: "Display a warning when PM chat is initiated. Can be overridden by editing the translation string: discourse_ai.ai_bot.pm_warning"
- ai_bot_allowed_groups: "When the GPT Bot has access to the PM, it will reply to members of these groups."
+ ai_bot_allowed_groups: "Members of these groups can have conversations with AI bots. Agents that allow personal messages use their own allowed groups instead. Anonymous users can't use the bot; when added to this setting, they can only preview the AI bot conversations page and are prompted to log in to ask a question."
ai_bot_debugging_allowed_groups: "Allow these groups to see a debug button on posts which displays the raw AI request and response"
ai_bot_public_sharing_allowed_groups: "Allow these groups to share AI personal messages with the public via a unique publicly available link. Note: if your site requires login, shares will also require login."
ai_bot_add_to_header: "Display a button in the header to start a PM with an AI Bot"
diff --git a/plugins/discourse-ai/lib/ai_bot/entry_point.rb b/plugins/discourse-ai/lib/ai_bot/entry_point.rb
index 06d0cbc7c7c..43ba2fc44fe 100644
--- a/plugins/discourse-ai/lib/ai_bot/entry_point.rb
+++ b/plugins/discourse-ai/lib/ai_bot/entry_point.rb
@@ -42,6 +42,13 @@ module DiscourseAi
SQL
end
+ # Anonymous users can't converse with a bot. Adding them to
+ # `ai_bot_allowed_groups` only lets them preview the conversations page.
+ def self.anonymous_preview_allowed?
+ SiteSetting.ai_bot_enabled &&
+ SiteSetting.ai_bot_allowed_groups_map.include?(Group::AUTO_GROUPS[:anonymous_users])
+ end
+
def self.personal_message_bot_user_ids(user)
return [] if user.blank? || !SiteSetting.ai_bot_enabled
@@ -99,12 +106,26 @@ module DiscourseAi
name: "discourse_ai.ai_bot.conversations.homepage_option",
path: "/discourse-ai/ai-bot/conversations",
route: "discourse_ai/ai_bot/conversations#index",
+ anonymous: true,
enabled: -> { SiteSetting.ai_bot_enabled },
available: ->(guardian:, request:) do
- EntryPoint.personal_message_bot_user_ids(guardian.user).present?
+ if guardian.anonymous?
+ DiscourseAi::AiBot::EntryPoint.anonymous_preview_allowed? &&
+ !CrawlerDetection.crawler_layout_request?(request)
+ else
+ DiscourseAi::AiBot::EntryPoint.personal_message_bot_user_ids(guardian.user).present?
+ end
end,
)
+ plugin.add_to_serializer(
+ :site,
+ :ai_bot_anonymous_preview,
+ include_condition: -> do
+ scope.anonymous? && DiscourseAi::AiBot::EntryPoint.anonymous_preview_allowed?
+ end,
+ ) { true }
+
# Hide bot PMs from the personal inbox queries (Latest, New, Unread)
# so human conversations are not buried under bot replies. Sent and
# Archive are intentionally untouched.
diff --git a/plugins/discourse-ai/spec/lib/modules/ai_bot/entry_point_spec.rb b/plugins/discourse-ai/spec/lib/modules/ai_bot/entry_point_spec.rb
index d8e72a32cf6..9ed29e03d20 100644
--- a/plugins/discourse-ai/spec/lib/modules/ai_bot/entry_point_spec.rb
+++ b/plugins/discourse-ai/spec/lib/modules/ai_bot/entry_point_spec.rb
@@ -32,6 +32,15 @@ describe DiscourseAi::AiBot::EntryPoint do
it "falls back to the top menu homepage for anonymous visitors" do
expect(HomepageHelper.resolve).to eq("latest")
+ expect(Site.json_for(Guardian.new)).not_to include("ai_bot_anonymous_preview")
+ end
+
+ it "offers anonymous visitors a preview when they're in the allowed groups" do
+ SiteSetting.ai_bot_allowed_groups =
+ "#{bot_allowed_group.id}|#{Group::AUTO_GROUPS[:anonymous_users]}"
+
+ expect(HomepageHelper.resolve).to eq("ai-conversations")
+ expect(JSON.parse(Site.json_for(Guardian.new))["ai_bot_anonymous_preview"]).to eq(true)
end
end
diff --git a/plugins/discourse-ai/spec/requests/ai_bot/conversations_controller_spec.rb b/plugins/discourse-ai/spec/requests/ai_bot/conversations_controller_spec.rb
index 676d8f7ef01..155e415eac8 100644
--- a/plugins/discourse-ai/spec/requests/ai_bot/conversations_controller_spec.rb
+++ b/plugins/discourse-ai/spec/requests/ai_bot/conversations_controller_spec.rb
@@ -23,7 +23,6 @@ RSpec.describe DiscourseAi::AiBot::ConversationsController do
before do
enable_current_plugin
- sign_in(current_user)
[conversation, starred_conversation, other_conversation].each { |topic| mark_ai_bot_pm(topic) }
end
@@ -42,6 +41,7 @@ RSpec.describe DiscourseAi::AiBot::ConversationsController do
it "renders conversations as the homepage for people who can use the bot" do
bot_allowed_group.add(current_user)
+ sign_in(current_user)
get "/"
@@ -52,15 +52,61 @@ RSpec.describe DiscourseAi::AiBot::ConversationsController do
end
it "renders the top menu homepage for people outside the bot's allowed groups" do
+ sign_in(current_user)
+
+ get "/"
+
+ expect(response.status).to eq(200)
+ expect(response.body).to include('<meta name="discourse_current_homepage" content="latest">')
+ end
+
+ it "renders the conversations preview for anonymous visitors when they're allowed a preview" do
+ SiteSetting.ai_bot_allowed_groups =
+ "#{bot_allowed_group.id}|#{Group::AUTO_GROUPS[:anonymous_users]}"
+
+ get "/"
+
+ expect(response.status).to eq(200)
+ expect(response.body).to include(
+ '<meta name="discourse_current_homepage" content="ai-conversations">',
+ )
+ end
+
+ it "renders the top menu homepage for anonymous visitors without a preview" do
get "/"
expect(response.status).to eq(200)
expect(response.body).to include('<meta name="discourse_current_homepage" content="latest">')
end
+
+ it "renders the topic list for crawlers even when anonymous visitors get a preview" do
+ SiteSetting.ai_bot_allowed_groups =
+ "#{bot_allowed_group.id}|#{Group::AUTO_GROUPS[:anonymous_users]}"
+
+ get "/", headers: { "HTTP_USER_AGENT" => "Googlebot" }
+
+ expect(response.status).to eq(200)
+ expect(response.body).to include(regular_topic.title)
+ end
+ end
+
+ describe "GET /discourse-ai/ai-bot/conversations" do
+ it "renders the app for anonymous visitors, so they can preview it or log in" do
+ get "/discourse-ai/ai-bot/conversations"
+
+ expect(response.status).to eq(200)
+ end
+
+ it "requires login for the conversation list" do
+ get "/discourse-ai/ai-bot/conversations.json"
+
+ expect(response.status).to eq(403)
+ end
end
describe "GET /discourse-ai/ai-bot/conversations.json" do
before do
+ sign_in(current_user)
DiscourseAi::AiBot::ConversationStar.create!(user: current_user, topic: starred_conversation)
DiscourseAi::AiBot::ConversationStar.create!(user: other_user, topic: other_conversation)
end
@@ -111,6 +157,8 @@ RSpec.describe DiscourseAi::AiBot::ConversationsController do
end
describe "PUT /discourse-ai/ai-bot/conversations/:topic_id/starred.json" do
+ before { sign_in(current_user) }
+
it "stars a conversation for the current user" do
put "/discourse-ai/ai-bot/conversations/#{conversation.id}/starred.json",
params: {
diff --git a/plugins/discourse-ai/spec/system/ai_bot/anonymous_preview_spec.rb b/plugins/discourse-ai/spec/system/ai_bot/anonymous_preview_spec.rb
new file mode 100644
index 00000000000..6dfe1e4d826
--- /dev/null
+++ b/plugins/discourse-ai/spec/system/ai_bot/anonymous_preview_spec.rb
@@ -0,0 +1,64 @@
+# frozen_string_literal: true
+
+RSpec.describe "AI Bot - Anonymous conversations preview" do
+ let(:ai_pm_homepage) { PageObjects::Components::AiPmHomepage.new }
+ let(:login_form) { PageObjects::Pages::Login.new }
+
+ fab!(:user) do
+ Fabricate(:user, username: "john", password: "supersecurepassword", refresh_auto_groups: true)
+ end
+ fab!(:group)
+ fab!(:gpt_4) { Fabricate(:llm_model, name: "gpt-4") }
+
+ before do
+ enable_current_plugin
+ SiteSetting.ai_bot_enabled = true
+ toggle_enabled_bots(bots: [gpt_4])
+ SiteSetting.ai_bot_allowed_groups = "#{group.id}|#{Group::AUTO_GROUPS[:anonymous_users]}"
+ SiteSetting.default_homepage = "ai-conversations"
+ SiteSetting.enable_local_logins_via_code = false
+ EmailToken.confirm(Fabricate(:email_token, user: user).token)
+ group.add(user)
+ end
+
+ it "keeps a question asked while logged out and restores it after logging in" do
+ visit "/"
+
+ expect(ai_pm_homepage).to have_homepage
+ expect(ai_pm_homepage).to have_anonymous_card
+ expect(page).to have_current_path("/")
+
+ ai_pm_homepage.input.fill_in(with: "How do I install a theme component?")
+ ai_pm_homepage.submit
+ login_form.fill(username: "john", password: "supersecurepassword").click_login
+
+ expect(ai_pm_homepage).to have_input_value("How do I install a theme component?")
+ end
+
+ it "opens login from the sidebar card" do
+ visit "/"
+ ai_pm_homepage.click_anonymous_card
+
+ expect(login_form).to be_open
+ end
+
+ it "moves the card into the page while the sidebar is closed" do
+ visit "/"
+ expect(page).to have_css(".sidebar-wrapper .ai-bot-anonymous-card")
+ expect(page).to have_no_css(".ai-bot-conversations .ai-bot-anonymous-card")
+
+ find(".header-sidebar-toggle button").click
+
+ expect(page).to have_no_css(".sidebar-wrapper .ai-bot-anonymous-card")
+ expect(page).to have_css(".ai-bot-conversations .ai-bot-anonymous-card")
+ end
+
+ it "prompts anonymous visitors to log in when the preview is off" do
+ SiteSetting.ai_bot_allowed_groups = group.id.to_s
+
+ ai_pm_homepage.visit
+
+ expect(login_form).to be_open
+ expect(ai_pm_homepage).to have_no_homepage
+ end
+end
diff --git a/plugins/discourse-ai/spec/system/page_objects/components/ai_pm_homepage.rb b/plugins/discourse-ai/spec/system/page_objects/components/ai_pm_homepage.rb
index 70b299e2522..0745e86449e 100644
--- a/plugins/discourse-ai/spec/system/page_objects/components/ai_pm_homepage.rb
+++ b/plugins/discourse-ai/spec/system/page_objects/components/ai_pm_homepage.rb
@@ -36,6 +36,18 @@ module PageObjects
page.has_no_css?(HOMEPAGE_WRAPPER_CLASS)
end
+ def has_anonymous_card?
+ page.has_css?(".ai-bot-anonymous-card")
+ end
+
+ def click_anonymous_card
+ page.find(".ai-bot-anonymous-card").click
+ end
+
+ def has_input_value?(value)
+ page.has_field?("ai-bot-conversations-input", with: value)
+ end
+
def has_no_new_question_button?
page.has_no_css?(".ai-new-question-button")
end