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

Commit 26b67c4

Browse files
committed
DEV: Add duplicate tools validation for AiPersona
1 parent 4b4e93b commit 26b67c4

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

app/models/ai_persona.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ class AiPersona < ActiveRecord::Base
2121
validates :rag_chunk_overlap_tokens, numericality: { greater_than: -1, maximum: 200 }
2222
validates :rag_conversation_chunks, numericality: { greater_than: 0, maximum: 1000 }
2323
validates :forced_tool_count, numericality: { greater_than: -2, maximum: 100_000 }
24+
25+
validate :tools_can_not_be_duplicated
26+
2427
has_many :rag_document_fragments, dependent: :destroy, as: :target
2528

2629
belongs_to :created_by, class_name: "User"
@@ -106,6 +109,22 @@ def bump_cache
106109
self.class.persona_cache.flush!
107110
end
108111

112+
def tools_can_not_be_duplicated
113+
return unless tools.is_a?(Array)
114+
115+
seen_tools = Set.new
116+
117+
tools.each do |tool|
118+
inner_name, _, _ = tool.is_a?(Array) ? tool : [tool, nil]
119+
120+
if seen_tools.include?(inner_name)
121+
errors.add(:tools, I18n.t("discourse_ai.ai_bot.personas.cannot_have_duplicate_tools"))
122+
else
123+
seen_tools.add(inner_name)
124+
end
125+
end
126+
end
127+
109128
def class_instance
110129
attributes = %i[
111130
id

config/locales/server.en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ en:
174174
default_llm_required: "Default LLM model is required prior to enabling Chat"
175175
cannot_delete_system_persona: "System personas cannot be deleted, please disable it instead"
176176
cannot_edit_system_persona: "System personas can only be renamed, you may not edit tools or system prompt, instead disable and make a copy"
177+
cannot_have_duplicate_tools: "Can not have duplicate tools"
177178
github_helper:
178179
name: "GitHub Helper"
179180
description: "AI Bot specialized in assisting with GitHub-related tasks and questions"

lib/ai_bot/personas/general.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ def tools
1010
Tools::Google,
1111
Tools::Image,
1212
Tools::Read,
13-
Tools::Image,
1413
Tools::ListCategories,
1514
Tools::ListTags,
1615
]

spec/models/ai_persona_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,29 @@
2424
expect(persona.valid?).to eq(true)
2525
end
2626

27+
it "validates tools" do
28+
persona =
29+
AiPersona.new(
30+
name: "test",
31+
description: "test",
32+
system_prompt: "test",
33+
tools: [],
34+
allowed_group_ids: [],
35+
)
36+
37+
expect(persona.valid?).to eq(true)
38+
39+
persona.tools = [["test-1", { test: "test" }, false]]
40+
expect(persona.valid?).to eq(true)
41+
42+
persona.tools = [["test-1", { test: "test" }, false], ["test-1", { test: "test" }, false]]
43+
expect(persona.valid?).to eq(false)
44+
expect(persona.errors[:tools]).to eq(["Can not have duplicate tools"])
45+
46+
persona.tools = [["test-1", { test: "test" }, false], ["test-2", { test: "test" }, false]]
47+
expect(persona.valid?).to eq(true)
48+
end
49+
2750
it "allows creation of user" do
2851
persona =
2952
AiPersona.create!(

0 commit comments

Comments
 (0)