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

Commit 4146a5d

Browse files
committed
DEV: Validate duplicate tool_name between builin tools and custom tools
1 parent 5f797ed commit 4146a5d

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

app/models/ai_persona.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,40 @@ def tools_can_not_be_duplicated
114114

115115
seen_tools = Set.new
116116

117+
custom_tool_ids = Set.new
118+
builtin_tool_names = Set.new
119+
117120
tools.each do |tool|
118121
inner_name, _, _ = tool.is_a?(Array) ? tool : [tool, nil]
119122

123+
if inner_name.start_with?("custom-")
124+
custom_tool_ids.add(inner_name.split("-", 2).last.to_i)
125+
else
126+
builtin_tool_names.add(inner_name.downcase)
127+
end
128+
120129
if seen_tools.include?(inner_name)
121130
errors.add(:tools, I18n.t("discourse_ai.ai_bot.personas.cannot_have_duplicate_tools"))
131+
break
122132
else
123133
seen_tools.add(inner_name)
124134
end
125135
end
136+
137+
return if errors.any?
138+
139+
# Checking if there are any duplicate tool_names between custom and builtin tools
140+
if builtin_tool_names.present? && custom_tool_ids.present?
141+
AiTool
142+
.where(id: custom_tool_ids)
143+
.pluck(:tool_name)
144+
.each do |tool_name|
145+
if builtin_tool_names.include?(tool_name.downcase)
146+
errors.add(:tools, I18n.t("discourse_ai.ai_bot.personas.cannot_have_duplicate_tools"))
147+
break
148+
end
149+
end
150+
end
126151
end
127152

128153
def class_instance

spec/models/ai_persona_spec.rb

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,41 @@
3434
allowed_group_ids: [],
3535
)
3636

37+
Fabricate(:ai_tool, id: 1)
38+
Fabricate(:ai_tool, id: 2, name: "Archie search", tool_name: "search")
39+
3740
expect(persona.valid?).to eq(true)
3841

39-
persona.tools = [["test-1", { test: "test" }, false]]
42+
persona.tools = %w[search image_generation]
4043
expect(persona.valid?).to eq(true)
4144

42-
persona.tools = [["test-1", { test: "test" }, false], ["test-1", { test: "test" }, false]]
45+
persona.tools = %w[search image_generation search]
4346
expect(persona.valid?).to eq(false)
4447
expect(persona.errors[:tools]).to eq(["Can not have duplicate tools"])
4548

46-
persona.tools = [["test-1", { test: "test" }, false], ["test-2", { test: "test" }, false]]
49+
persona.tools = [["custom-1", { test: "test" }, false], ["custom-2", { test: "test" }, false]]
4750
expect(persona.valid?).to eq(true)
51+
expect(persona.errors[:tools]).to eq([])
52+
53+
persona.tools = [["custom-1", { test: "test" }, false], ["custom-1", { test: "test" }, false]]
54+
expect(persona.valid?).to eq(false)
55+
expect(persona.errors[:tools]).to eq(["Can not have duplicate tools"])
56+
57+
persona.tools = [
58+
["custom-1", { test: "test" }, false],
59+
["custom-2", { test: "test" }, false],
60+
"image_generation",
61+
]
62+
expect(persona.valid?).to eq(true)
63+
expect(persona.errors[:tools]).to eq([])
64+
65+
persona.tools = [
66+
["custom-1", { test: "test" }, false],
67+
["custom-2", { test: "test" }, false],
68+
"Search",
69+
]
70+
expect(persona.valid?).to eq(false)
71+
expect(persona.errors[:tools]).to eq(["Can not have duplicate tools"])
4872
end
4973

5074
it "allows creation of user" do

0 commit comments

Comments
 (0)