Hoa Nguyen b60926c6e6
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
2025-02-07 14:34:47 +11:00

78 lines
1.6 KiB
Ruby

# frozen_string_literal: true
module DiscourseAi
module AiBot
module Tools
class Custom < Tool
def self.class_instance(tool_id)
klass = Class.new(self)
klass.tool_id = tool_id
klass
end
def self.custom?
true
end
def self.tool_id
@tool_id
end
def self.tool_id=(tool_id)
@tool_id = tool_id
end
def self.signature
AiTool.find(tool_id).signature
end
# Backwards compatibility: if tool_name is not set (existing custom tools), use name
def self.name
name, tool_name = AiTool.where(id: tool_id).pluck(:name, :tool_name).first
tool_name.presence || name
end
def initialize(*args, **kwargs)
@chain_next_response = true
super(*args, **kwargs)
end
def invoke
result = runner.invoke
if runner.custom_raw
self.custom_raw = runner.custom_raw
@chain_next_response = false
end
result
end
def runner
@runner ||= ai_tool.runner(parameters, llm: llm, bot_user: bot_user, context: context)
end
def ai_tool
@ai_tool ||= AiTool.find(self.class.tool_id)
end
def summary
ai_tool.summary
end
def details
runner.details
end
def chain_next_response?
!!@chain_next_response
end
def help
# I do not think this is called, but lets make sure
raise "Not implemented"
end
end
end
end
end