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

Commit 27f6a41

Browse files
committed
DEV: remove unique index from tool_name column and add migrations to migrate name to tool_name
1 parent 93117b1 commit 27f6a41

File tree

6 files changed

+19
-32
lines changed

6 files changed

+19
-32
lines changed

app/models/ai_tool.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class AiTool < ActiveRecord::Base
44
validates :name, presence: true, length: { maximum: 100 }
5-
validates :tool_name, presence: true, length: { maximum: 100 }, uniqueness: true
5+
validates :tool_name, presence: true, length: { maximum: 100 }
66
validates :description, presence: true, length: { maximum: 1000 }
77
validates :summary, presence: true, length: { maximum: 255 }
88
validates :script, presence: true, length: { maximum: 100_000 }
@@ -222,7 +222,7 @@ def self.presets
222222
},
223223
{
224224
preset_id: "stock_quote",
225-
name: "Stock Quote",
225+
name: "Stock Quote (AlphaVantage)",
226226
tool_name: "stock_quote",
227227
description: "Get real-time stock quote information using AlphaVantage API",
228228
parameters: [

app/services/discourse_ai/tool_name_checker.rb

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,10 @@ def initialize(tool_name)
88

99
def check
1010
if @tool_name.match? AiTool::ALPHANUMERIC_PATTERN
11-
check_name_availability
11+
{ available: true }
1212
else
1313
{ available: false, errors: [I18n.t("discourse_ai.tools.name.characters")] }
1414
end
1515
end
16-
17-
private
18-
19-
def check_name_availability
20-
if AiTool.exists?(tool_name: @tool_name)
21-
{ available: false, errors: [I18n.t("errors.messages.taken")] }
22-
else
23-
{ available: true }
24-
end
25-
end
2616
end
2717
end

config/locales/client.en.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,9 @@ en:
212212
back: "Back"
213213
short_title: "Tools"
214214
name: "Name"
215-
name_help: "The unique name of the tool as used by the language model"
215+
name_help: "Name will show up in the Discourse UI and is the short identifier you will use to find the tool in various settings, it should be distinct (it is required)"
216216
tool_name: "Tool Name"
217-
tool_name_help: "Tool name, only include numbers, letters, and underscores"
217+
tool_name_help: "Tool Name is presented to the large language model. It is not distinct, but it is distinct per persona. (persona validates on save)"
218218
description: "Description"
219219
description_help: "A clear description of the tool's purpose for the language model"
220220
summary: "Summary"
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
# frozen_string_literal: true
22

33
class AddToolNameToAiTools < ActiveRecord::Migration[7.1]
4-
def change
4+
def up
55
add_column :ai_tools, :tool_name, :string, null: false, default: "", if_not_exists: true
6-
add_index :ai_tools, :tool_name, unique: true, if_not_exists: true
6+
7+
# Migrate existing name to tool_name
8+
execute <<~SQL
9+
UPDATE ai_tools
10+
SET tool_name = regexp_replace(LOWER(name),'[^a-z0-9_]','', 'g');
11+
SQL
12+
end
13+
14+
def down
15+
remove_column :ai_tools, :tool_name, if_exists: true
716
end
817
end

spec/requests/admin/ai_tools_controller_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@
9797
put "/admin/plugins/discourse-ai/ai-tools/#{ai_tool.id}.json",
9898
params: {
9999
ai_tool: {
100-
name: "UpdatedTool",
100+
name: "Updated Tool",
101101
},
102102
}
103103

104104
expect(response).to be_successful
105-
expect(ai_tool.reload.name).to eq("UpdatedTool")
105+
expect(ai_tool.reload.name).to eq("Updated Tool")
106106
end
107107

108108
context "when updating an enum parameters" do
@@ -170,7 +170,7 @@
170170
post "/admin/plugins/discourse-ai/ai-tools/test.json",
171171
params: {
172172
ai_tool: {
173-
name: "NewTool",
173+
name: "New Tool",
174174
description: "A new test tool",
175175
script: "function invoke(params) { return 'New test result: ' + params.input; }",
176176
parameters: [

spec/services/discourse_ai/tool_name_checker_spec.rb

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,5 @@
1919
end
2020
end
2121
end
22-
23-
context "when the tool name is already" do
24-
let(:tool_name) { "toolname" }
25-
26-
before { Fabricate(:ai_tool, tool_name: tool_name) }
27-
28-
it "returns an error" do
29-
expect(DiscourseAi::ToolNameChecker.new(tool_name).check).to eq(
30-
{ available: false, errors: [I18n.t("errors.messages.taken")] },
31-
)
32-
end
33-
end
3422
end
3523
end

0 commit comments

Comments
 (0)