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

Commit 68b4a06

Browse files
committed
fix invalid filters should raise, topic filter not working
1 parent 2272bc3 commit 68b4a06

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

lib/personas/tools/researcher.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ def invoke(&blk)
8585
limit: max_results,
8686
guardian: guardian,
8787
)
88+
89+
if filter.invalid_filters.present?
90+
return(
91+
{
92+
error:
93+
"Invalid filter fragment: #{filter.invalid_filters.join(" ")}\n\n#{self.class.filter_description}",
94+
}
95+
)
96+
end
97+
8898
@result_count = filter.search.count
8999

90100
blk.call details

lib/utils/research/filter.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def self.word_to_date(str)
1717
::Search.word_to_date(str)
1818
end
1919

20-
attr_reader :term, :filters, :order, :guardian, :limit, :offset
20+
attr_reader :term, :filters, :order, :guardian, :limit, :offset, :invalid_filters
2121

2222
# Define all filters at class level
2323
register_filter(/\Astatus:open\z/i) do |relation, _, _|
@@ -206,16 +206,17 @@ def self.word_to_date(str)
206206
end
207207

208208
def initialize(term, guardian: nil, limit: nil, offset: nil)
209-
@term = term.to_s
210209
@guardian = guardian || Guardian.new
211210
@limit = limit
212211
@offset = offset
213212
@filters = []
214213
@valid = true
215214
@order = :latest_post
216215
@topic_ids = nil
216+
@invalid_filters = []
217+
@term = term.to_s.strip
217218

218-
@term = process_filters(@term)
219+
process_filters(@term)
219220
end
220221

221222
def set_order!(order)
@@ -250,7 +251,7 @@ def search
250251
if @topic_ids.present?
251252
filtered =
252253
original_filtered.where(
253-
"posts.topic_id IN (?) OR posts.id IN (?)",
254+
"posts.topic_id IN (?) AND posts.id IN (?)",
254255
@topic_ids,
255256
filtered.select("posts.id"),
256257
)
@@ -275,7 +276,7 @@ def search
275276
private
276277

277278
def process_filters(term)
278-
return "" if term.blank?
279+
return if term.blank?
279280

280281
term
281282
.to_s
@@ -293,10 +294,8 @@ def process_filters(term)
293294
end
294295
end
295296

296-
found ? nil : word
297+
invalid_filters << word if !found
297298
end
298-
.compact
299-
.join(" ")
300299
end
301300
end
302301
end

spec/lib/personas/tools/researcher_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,23 @@
1717

1818
fab!(:topic_with_tags) { Fabricate(:topic, category: category, tags: [tag_research, tag_data]) }
1919
fab!(:post) { Fabricate(:post, topic: topic_with_tags) }
20+
fab!(:another_post) { Fabricate(:post) }
2021

2122
before { SiteSetting.ai_bot_enabled = true }
2223

2324
describe "#invoke" do
25+
it "can correctly filter to a topic id" do
26+
researcher =
27+
described_class.new(
28+
{ dry_run: true, filter: "topic:#{topic_with_tags.id}", goals: "analyze topic content" },
29+
bot_user: bot_user,
30+
llm: llm,
31+
context: DiscourseAi::Personas::BotContext.new(user: user, post: post),
32+
)
33+
results = researcher.invoke(&progress_blk)
34+
expect(results[:number_of_posts]).to eq(1)
35+
end
36+
2437
it "returns filter information and result count" do
2538
researcher =
2639
described_class.new(
@@ -63,6 +76,20 @@
6376
expect(researcher.options[:max_results]).to eq(50)
6477
end
6578

79+
it "returns error for invalid filter fragments" do
80+
researcher =
81+
described_class.new(
82+
{ filter: "invalidfilter tag:research", goals: "analyze content" },
83+
bot_user: bot_user,
84+
llm: llm,
85+
context: DiscourseAi::Personas::BotContext.new(user: user, post: post),
86+
)
87+
88+
results = researcher.invoke(&progress_blk)
89+
90+
expect(results[:error]).to include("Invalid filter fragment")
91+
end
92+
6693
it "returns correct results for non-dry-run with filtered posts" do
6794
# Stage 2 topics, each with 2 posts
6895
topics = Array.new(2) { Fabricate(:topic, category: category, tags: [tag_research]) }

0 commit comments

Comments
 (0)