generated from discourse/discourse-plugin-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEATURE: Tool name validation (#842)
* FEATURE: Tool name validation - Add unique index to the name column of the ai_tools table - correct our tests for AiToolController - tool_name field which will be used to represent to LLM - Add tool_name to Tools's presets - Add duplicate tools validation for AiPersona - Add unique constraint to the name column of the ai_tools table * DEV: Validate duplicate tool_name between builin tools and custom tools * lint * chore: fix linting * fix conlict mistakes * chore: correct icon class * chore: fix failed specs * Add max_length to tool_name * chore: correct the option name * lintings * fix lintings
- Loading branch information
Showing
18 changed files
with
225 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,6 +84,7 @@ def ai_tool_params | |
.require(:ai_tool) | ||
.permit( | ||
:name, | ||
:tool_name, | ||
:description, | ||
:script, | ||
:summary, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddToolNameToAiTools < ActiveRecord::Migration[7.1] | ||
def up | ||
add_column :ai_tools, | ||
:tool_name, | ||
:string, | ||
null: false, | ||
limit: 100, | ||
default: "", | ||
if_not_exists: true | ||
|
||
# Migrate existing name to tool_name | ||
execute <<~SQL | ||
UPDATE ai_tools | ||
SET tool_name = regexp_replace(LOWER(name),'[^a-z0-9_]','', 'g'); | ||
SQL | ||
end | ||
|
||
def down | ||
remove_column :ai_tools, :tool_name, if_exists: true | ||
end | ||
end |
25 changes: 25 additions & 0 deletions
25
db/migrate/20241023041242_add_unique_constraint_to_ai_tools.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
class AddUniqueConstraintToAiTools < ActiveRecord::Migration[7.1] | ||
def up | ||
# We need to remove duplicates before adding the unique constraint | ||
execute <<~SQL | ||
WITH duplicates AS ( | ||
SELECT name, COUNT(*) as count, MIN(id) as keeper_id | ||
FROM ai_tools | ||
GROUP BY name | ||
HAVING COUNT(*) > 1 | ||
) | ||
UPDATE ai_tools AS p | ||
SET name = CONCAT(p.name, p.id) | ||
FROM duplicates d | ||
WHERE p.name = d.name | ||
AND p.id != d.keeper_id; | ||
SQL | ||
|
||
add_index :ai_personas, :name, unique: true, if_not_exists: true | ||
end | ||
|
||
def down | ||
remove_index :ai_personas, :name, if_exists: true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
Fabricator(:ai_tool) do | ||
name "github tool" | ||
tool_name "github_tool" | ||
description "This is a tool for GitHub" | ||
summary "This is a tool for GitHub" | ||
script "puts 'Hello, GitHub!'" | ||
created_by_id 1 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.