Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit f44e61c

Browse files
committed
bad merge, mark
1 parent bc8d8b0 commit f44e61c

File tree

7 files changed

+148
-61
lines changed

7 files changed

+148
-61
lines changed

assets/javascripts/discourse/controllers/discourse-ai-bot-conversations.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import Controller from "@ember/controller";
22
import { action } from "@ember/object";
33
import { service } from "@ember/service";
4-
import SimpleTextareaInteractor from "../lib/simple-textarea-interactor";
54

65
export default class DiscourseAiBotConversations extends Controller {
76
@service aiBotConversationsHiddenSubmit;
@@ -23,7 +22,12 @@ export default class DiscourseAiBotConversations extends Controller {
2322
}
2423

2524
@action
26-
initializeTextarea(element) {
27-
this.textareaInteractor = new SimpleTextareaInteractor(element);
25+
setTextArea(element) {
26+
this.textarea = element;
27+
}
28+
29+
_autoExpandTextarea() {
30+
this.textarea.style.height = "auto";
31+
this.textarea.style.height = this.textarea.scrollHeight + "px";
2832
}
2933
}

assets/javascripts/discourse/lib/simple-textarea-interactor.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

assets/javascripts/discourse/services/ai-bot-conversations-hidden-submit.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,27 @@ export default class AiBotConversationsHiddenSubmit extends Service {
2525
this.composer.close();
2626

2727
if (this.inputValue.length < 10) {
28-
// TODO: Translate
29-
this.dialog.alert({
30-
message: "Message must be longer than 10 characters",
28+
return this.dialog.alert({
29+
message: i18n(
30+
"discourse_ai.ai_bot.conversations.min_input_length_message"
31+
),
3132
didConfirm: () => this.focusInput(),
3233
didCancel: () => this.focusInput(),
3334
});
3435
}
3536

37+
// borrowed from ai-bot-helper.js
38+
const draftKey = "new_private_message_ai_" + new Date().getTime();
39+
40+
const personaWithUsername = this.currentUser.ai_enabled_personas.find(
41+
(persona) => persona.username
42+
);
43+
3644
// this is a total hack, the composer is hidden on the homepage with CSS
3745
await this.composer.open({
3846
action: Composer.PRIVATE_MESSAGE,
39-
draftKey: "private_message_ai",
40-
recipients: this.currentUser.ai_enabled_personas[0].username,
47+
draftKey,
48+
recipients: personaWithUsername.username,
4149
topicTitle: i18n("discourse_ai.ai_bot.default_pm_prefix"),
4250
topicBody: this.inputValue,
4351
archetypeId: "private_message",

assets/javascripts/discourse/templates/discourse-ai-bot-conversations.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<h1>{{i18n "discourse_ai.ai_bot.conversations.header"}}</h1>
55
<div class="custom-homepage__input-wrapper">
66
<textarea
7-
{{didInsert this.initializeTextarea}}
7+
{{didInsert this.setTextArea}}
88
{{on "input" this.updateInputValue}}
99
{{on "keydown" this.handleKeyDown}}
1010
id="custom-homepage-input"
@@ -16,7 +16,7 @@
1616
@action={{this.aiBotConversationsHiddenSubmit.submitToBot}}
1717
@icon="paper-plane"
1818
@title="discourse_ai.ai_bot.conversations.header"
19-
class="ai-bot-button btn-primary"
19+
class="ai-bot-button btn-primary ai-conversation-submit"
2020
/>
2121
</div>
2222
<p class="ai-disclaimer">

config/locales/client.en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,7 @@ en:
711711
disclaimer: "Generative AI can make mistakes"
712712
placeholder: "Ask a question..."
713713
new: "New Question"
714+
min_input_length_message: "Message must be longer than 10 characters"
714715
sentiments:
715716
dashboard:
716717
title: "Sentiment"

spec/system/ai_bot/personal_message_spec.rb

Lines changed: 113 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,85 @@
66
let(:ai_pm_homepage) { PageObjects::Components::AiPmHomepage.new }
77
let(:sidebar) { PageObjects::Components::NavigationMenu::Sidebar.new }
88
let(:header_dropdown) { PageObjects::Components::NavigationMenu::HeaderDropdown.new }
9+
let(:dialog) { PageObjects::Components::Dialog.new }
910

10-
fab!(:user)
11-
fab!(:group)
11+
fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }
1212

13+
fab!(:claude_2) do
14+
Fabricate(
15+
:llm_model,
16+
provider: "anthropic",
17+
url: "https://api.anthropic.com/v1/messages",
18+
name: "claude-2",
19+
)
20+
end
1321
fab!(:bot_user) do
14-
user = Fabricate(:user)
15-
AiPersona.last.update!(user_id: user.id)
16-
user
22+
toggle_enabled_bots(bots: [claude_2])
23+
SiteSetting.ai_bot_enabled = true
24+
claude_2.reload.user
25+
end
26+
fab!(:bot) do
27+
persona =
28+
AiPersona
29+
.find(DiscourseAi::Personas::Persona.system_personas[DiscourseAi::Personas::General])
30+
.class_instance
31+
.new
32+
DiscourseAi::Personas::Bot.as(bot_user, persona: persona)
1733
end
18-
fab!(:llm_model) { Fabricate(:llm_model, enabled_chat_bot: true) }
1934

20-
fab!(:pm) { Fabricate(:private_message_topic, title: "AI Conversation Test", user: user) }
21-
fab!(:reply) do
22-
Fabricate(:post, topic: pm, user: user, post_number: 1, raw: "test test test user reply")
35+
fab!(:pm) do
36+
Fabricate(
37+
:private_message_topic,
38+
title: "This is my special PM",
39+
user: user,
40+
topic_allowed_users: [
41+
Fabricate.build(:topic_allowed_user, user: user),
42+
Fabricate.build(:topic_allowed_user, user: bot_user),
43+
],
44+
)
45+
end
46+
fab!(:first_post) do
47+
Fabricate(:post, topic: pm, user: user, post_number: 1, raw: "This is a reply by the user")
2348
end
24-
fab!(:bot_reply) do
25-
Fabricate(:post, topic: pm, user: bot_user, post_number: 2, raw: "test test test bot reply")
49+
fab!(:second_post) do
50+
Fabricate(:post, topic: pm, user: bot_user, post_number: 2, raw: "This is a bot reply")
51+
end
52+
fab!(:third_post) do
53+
Fabricate(
54+
:post,
55+
topic: pm,
56+
user: user,
57+
post_number: 3,
58+
raw: "This is a second reply by the user",
59+
)
2660
end
2761
fab!(:topic_user) { Fabricate(:topic_user, topic: pm, user: user) }
2862
fab!(:topic_bot_user) { Fabricate(:topic_user, topic: pm, user: bot_user) }
2963

64+
fab!(:persona) do
65+
persona =
66+
AiPersona.create!(
67+
name: "Test Persona",
68+
description: "A test persona",
69+
allowed_group_ids: [Group::AUTO_GROUPS[:trust_level_0]],
70+
enabled: true,
71+
system_prompt: "You are a helpful bot",
72+
)
73+
74+
persona.create_user!
75+
persona.update!(
76+
default_llm_id: claude_2.id,
77+
allow_chat_channel_mentions: true,
78+
allow_topic_mentions: true,
79+
)
80+
persona
81+
end
82+
3083
before do
3184
SiteSetting.ai_enable_experimental_bot_ux = true
3285
SiteSetting.ai_bot_enabled = true
33-
toggle_enabled_bots(bots: [llm_model])
34-
SiteSetting.ai_bot_allowed_groups = group.id.to_s
35-
sign_in(user)
36-
37-
group.add(user)
38-
group.save
39-
40-
allowed_persona = AiPersona.last
41-
allowed_persona.update!(allowed_group_ids: [group.id], enabled: true)
42-
86+
Jobs.run_immediately!
87+
SiteSetting.ai_bot_allowed_groups = "#{Group::AUTO_GROUPS[:trust_level_0]}"
4388
sign_in(user)
4489
end
4590

@@ -60,6 +105,17 @@
60105
expect(sidebar).to be_visible
61106
end
62107

108+
it "displays error when message is too short" do
109+
visit "/"
110+
find(".ai-bot-button").click
111+
112+
ai_pm_homepage.input.fill_in(with: "a")
113+
ai_pm_homepage.submit
114+
expect(ai_pm_homepage).to have_too_short_dialog
115+
dialog.click_yes
116+
expect(composer).to be_closed
117+
end
118+
63119
it "renders sidebar even when navigation menu is set to header" do
64120
SiteSetting.navigation_menu = "header dropdown"
65121
visit "/"
@@ -88,7 +144,42 @@
88144

89145
expect(ai_pm_homepage).to have_homepage
90146
expect(sidebar).to have_section("custom-messages")
91-
expect(sidebar).to have_section_link(pm.title, href: pm.relative_url)
147+
expect(sidebar).to have_section_link(pm.title)
148+
expect(sidebar).to have_no_css("button.ai-new-question-button")
149+
end
150+
151+
it "navigates to the bot conversation when clicked" do
152+
visit "/"
153+
find(".ai-bot-button").click
154+
155+
expect(ai_pm_homepage).to have_homepage
156+
sidebar.find(
157+
".sidebar-section[data-section-name='custom-messages'] a.sidebar-section-link",
158+
).click
159+
expect(topic_page).to have_topic_title(pm.title)
160+
end
161+
162+
it "displays sidebar and 'new question' on the topic page" do
163+
topic_page.visit_topic(pm)
164+
expect(sidebar).to be_visible
165+
expect(sidebar).to have_css("button.ai-new-question-button")
166+
end
167+
168+
it "redirect to the homepage when 'new question' is clicked" do
169+
topic_page.visit_topic(pm)
170+
expect(sidebar).to be_visible
171+
sidebar.find("button.ai-new-question-button").click
172+
expect(ai_pm_homepage).to have_homepage
173+
end
174+
175+
it "can send a new message to the bot" do
176+
topic_page.visit_topic(pm)
177+
topic_page.click_reply_button
178+
expect(composer).to be_opened
179+
180+
composer.fill_in(with: "Hello bot replying to you")
181+
composer.submit
182+
expect(page).to have_content("Hello bot replying to you")
92183
end
93184
end
94185
end

spec/system/page_objects/components/ai_pm_homepage.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ class AiPmHomepage < PageObjects::Components::Base
66
HOMEPAGE_BODY_CLASS = ".discourse-ai-bot-conversations-page"
77
HOMEPAGE_WRAPPER_CLASS = ".custom-homepage__content-wrapper"
88

9+
def input
10+
page.find("#custom-homepage-input")
11+
end
12+
13+
def submit
14+
page.find(".ai-conversation-submit").click
15+
end
16+
17+
def has_too_short_dialog?
18+
page.find(".dialog-content", text: "Message must be longer than 10 characters")
19+
end
20+
921
def has_homepage?
1022
page.has_css?("#{HOMEPAGE_BODY_CLASS} #{HOMEPAGE_WRAPPER_CLASS}")
1123
end

0 commit comments

Comments
 (0)