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

Commit 1dd42ac

Browse files
committed
Add basic search mode
1 parent 3cc78e5 commit 1dd42ac

File tree

5 files changed

+46
-10
lines changed

5 files changed

+46
-10
lines changed

app/controllers/discourse_ai/discord/bot_controller.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ def interactions
2525
unless SiteSetting.ai_discord_allowed_guilds_map.include?(interaction.guild_id)
2626
return head :forbidden
2727
end
28+
2829
response = { type: 5, data: { content: "Searching..." } }
2930
hijack { render json: response }
3031

31-
Rails.logger.info("Discord interaction received: #{body}")
32-
3332
# Respond to Discord command
3433
Jobs.enqueue(:stream_discord_reply, interaction: body.dup)
3534
end

app/jobs/regular/stream_discord_reply.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ class StreamDiscordReply < ::Jobs::Base
77
def execute(args)
88
interaction = args[:interaction]
99

10-
DiscourseAi::Discord::Bot::PersonaReplier.new(interaction).handle_interaction!
10+
if SiteSetting.ai_discord_search_mode == "persona"
11+
DiscourseAi::Discord::Bot::PersonaReplier.new(interaction).handle_interaction!
12+
else
13+
DiscourseAi::Discord::Bot::Search.new(interaction).handle_interaction!
14+
end
1115
end
1216
end
1317
end

lib/discord/bot/base.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ def create_reply(reply)
2323
{ "Content-Type" => "application/json" },
2424
)
2525
@reply_response = JSON.parse(response.body, symbolize_names: true)
26-
Rails.logger.info("Discord reply created: #{@reply_response}")
27-
@reply_response
2826
end
2927

3028
def update_reply(reply)
@@ -38,8 +36,6 @@ def update_reply(reply)
3836
{ "Content-Type" => "application/json" },
3937
)
4038
@last_update_response = JSON.parse(response.body, symbolize_names: true)
41-
Rails.logger.info("Discord reply updated: #{@last_update_response}")
42-
@last_update_response
4339
end
4440
end
4541
end

lib/discord/bot/persona_replier.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ def handle_interaction!
2929
next if reply.blank?
3030

3131
if @reply_response.nil?
32-
create_reply(reply.dup)
32+
create_reply(wrap_links(reply.dup))
3333
elsif @last_update_response.nil?
34-
update_reply(reply.dup)
34+
update_reply(wrap_links(reply.dup))
3535
elsif Time.now - last_update_sent_at > 1
36-
update_reply(reply.dup)
36+
update_reply(wrap_links(reply.dup))
3737
last_update_sent_at = Time.now
3838
end
3939
end

lib/discord/bot/search.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
module DiscourseAi
4+
module Discord::Bot
5+
class Search < Base
6+
def initialize(body)
7+
@search = DiscourseAi::AiBot::Tools::Search
8+
super(body)
9+
end
10+
11+
def handle_interaction!
12+
results =
13+
@search.new(
14+
{ search_query: @query },
15+
persona_options: {
16+
"max_results" => 10,
17+
},
18+
bot_user: nil,
19+
llm: nil,
20+
).invoke(&Proc.new {})
21+
22+
formatted_results = results[:rows].map.with_index { |result, index| <<~RESULT }.join("\n")
23+
#{index + 1}. [#{result[0]}](<#{Discourse.base_url}#{result[1]}>)
24+
#{result[3].truncate(50)}
25+
RESULT
26+
27+
reply = <<~REPLY
28+
Here are the top search results for your query:
29+
30+
#{formatted_results}
31+
REPLY
32+
33+
create_reply(reply)
34+
end
35+
end
36+
end
37+
end

0 commit comments

Comments
 (0)