2023-08-08 17:19:56 -04:00
|
|
|
#frozen_string_literal: true
|
|
|
|
|
2024-01-04 08:44:07 -05:00
|
|
|
RSpec.describe DiscourseAi::AiBot::Tools::Read do
|
2024-07-30 12:44:57 -04:00
|
|
|
fab!(:llm_model)
|
|
|
|
let(:bot_user) { DiscourseAi::AiBot::EntryPoint.find_user_from_model(llm_model.name) }
|
|
|
|
let(:llm) { DiscourseAi::Completions::Llm.proxy("custom:#{llm_model.id}") }
|
2024-05-07 07:55:46 -04:00
|
|
|
let(:tool) { described_class.new({ topic_id: topic_with_tags.id }, bot_user: bot_user, llm: llm) }
|
2023-08-08 17:19:56 -04:00
|
|
|
|
2023-09-11 21:06:55 -04:00
|
|
|
fab!(:parent_category) { Fabricate(:category, name: "animals") }
|
|
|
|
fab!(:category) { Fabricate(:category, parent_category: parent_category, name: "amazing-cat") }
|
|
|
|
|
|
|
|
fab!(:tag_funny) { Fabricate(:tag, name: "funny") }
|
|
|
|
fab!(:tag_sad) { Fabricate(:tag, name: "sad") }
|
|
|
|
fab!(:tag_hidden) { Fabricate(:tag, name: "hidden") }
|
2023-09-12 02:09:28 -04:00
|
|
|
fab!(:staff_tag_group) do
|
|
|
|
tag_group = Fabricate.build(:tag_group, name: "Staff only", tag_names: ["hidden"])
|
|
|
|
|
|
|
|
tag_group.permissions = [
|
|
|
|
[Group::AUTO_GROUPS[:staff], TagGroupPermission.permission_types[:full]],
|
|
|
|
]
|
|
|
|
tag_group.save!
|
|
|
|
tag_group
|
|
|
|
end
|
2023-09-11 21:06:55 -04:00
|
|
|
fab!(:topic_with_tags) do
|
|
|
|
Fabricate(:topic, category: category, tags: [tag_funny, tag_sad, tag_hidden])
|
|
|
|
end
|
|
|
|
|
2024-05-29 16:33:50 -04:00
|
|
|
fab!(:post1) { Fabricate(:post, topic: topic_with_tags, raw: "hello there") }
|
|
|
|
fab!(:post2) { Fabricate(:post, topic: topic_with_tags, raw: "mister sam") }
|
|
|
|
|
2023-10-23 02:00:58 -04:00
|
|
|
before { SiteSetting.ai_bot_enabled = true }
|
|
|
|
|
2023-08-08 17:19:56 -04:00
|
|
|
describe "#process" do
|
2024-06-19 01:49:36 -04:00
|
|
|
it "can read private topics if allowed to" do
|
|
|
|
category = topic_with_tags.category
|
|
|
|
category.set_permissions(Group::AUTO_GROUPS[:staff] => :full)
|
|
|
|
category.save!
|
|
|
|
|
|
|
|
tool =
|
|
|
|
described_class.new(
|
|
|
|
{ topic_id: topic_with_tags.id, post_numbers: [post1.post_number] },
|
|
|
|
bot_user: bot_user,
|
|
|
|
llm: llm,
|
|
|
|
)
|
|
|
|
results = tool.invoke
|
|
|
|
|
|
|
|
expect(results[:description]).to eq("Topic not found")
|
|
|
|
|
|
|
|
admin = Fabricate(:admin)
|
|
|
|
|
|
|
|
tool =
|
|
|
|
described_class.new(
|
|
|
|
{ topic_id: topic_with_tags.id, post_numbers: [post1.post_number] },
|
|
|
|
bot_user: bot_user,
|
|
|
|
llm: llm,
|
|
|
|
persona_options: {
|
|
|
|
"read_private" => true,
|
|
|
|
},
|
|
|
|
context: {
|
|
|
|
user: admin,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
results = tool.invoke
|
|
|
|
expect(results[:content]).to include("hello there")
|
|
|
|
|
|
|
|
tool =
|
|
|
|
described_class.new(
|
|
|
|
{ topic_id: topic_with_tags.id, post_numbers: [post1.post_number] },
|
|
|
|
bot_user: bot_user,
|
|
|
|
llm: llm,
|
|
|
|
context: {
|
|
|
|
user: admin,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
results = tool.invoke
|
|
|
|
expect(results[:description]).to eq("Topic not found")
|
|
|
|
end
|
|
|
|
|
2024-05-29 16:33:50 -04:00
|
|
|
it "can read specific posts" do
|
|
|
|
tool =
|
|
|
|
described_class.new(
|
|
|
|
{ topic_id: topic_with_tags.id, post_numbers: [post1.post_number] },
|
|
|
|
bot_user: bot_user,
|
|
|
|
llm: llm,
|
|
|
|
)
|
|
|
|
results = tool.invoke
|
|
|
|
|
|
|
|
expect(results[:content]).to include("hello there")
|
|
|
|
expect(results[:content]).not_to include("mister sam")
|
|
|
|
end
|
2023-08-08 17:19:56 -04:00
|
|
|
it "can read a topic" do
|
2023-09-11 21:06:55 -04:00
|
|
|
topic_id = topic_with_tags.id
|
2024-05-07 07:55:46 -04:00
|
|
|
results = tool.invoke
|
2023-08-08 17:19:56 -04:00
|
|
|
|
2023-09-11 21:06:55 -04:00
|
|
|
expect(results[:topic_id]).to eq(topic_id)
|
2023-08-08 17:19:56 -04:00
|
|
|
expect(results[:content]).to include("hello")
|
|
|
|
expect(results[:content]).to include("sam")
|
2023-09-11 21:06:55 -04:00
|
|
|
expect(results[:content]).to include("amazing-cat")
|
|
|
|
expect(results[:content]).to include("funny")
|
|
|
|
expect(results[:content]).to include("sad")
|
|
|
|
expect(results[:content]).to include("animals")
|
|
|
|
expect(results[:content]).not_to include("hidden")
|
2024-01-04 08:44:07 -05:00
|
|
|
expect(tool.title).to eq(topic_with_tags.title)
|
|
|
|
expect(tool.url).to eq(topic_with_tags.relative_url)
|
2023-08-08 17:19:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|