mirror of
https://github.com/lingble/chatwoot.git
synced 2025-10-29 18:22:53 +00:00
feat: handle custom tools in assistant
This commit is contained in:
@@ -33,7 +33,8 @@ class Api::V1::Accounts::Captain::AssistantsController < Api::V1::Accounts::Base
|
||||
end
|
||||
|
||||
def tools
|
||||
@tools = Captain::Assistant.available_agent_tools
|
||||
assistant = Captain::Assistant.new(account: Current.account)
|
||||
@tools = assistant.available_agent_tools
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
@@ -50,6 +50,21 @@ class Captain::Assistant < ApplicationRecord
|
||||
name
|
||||
end
|
||||
|
||||
def available_agent_tools
|
||||
tools = self.class.built_in_agent_tools.dup
|
||||
|
||||
if account.feature_enabled?(:captain_custom_tools)
|
||||
custom_tools = Captain::CustomTool.where(account_id: account_id, enabled: true).map(&:to_tool_metadata)
|
||||
tools.concat(custom_tools)
|
||||
end
|
||||
|
||||
tools
|
||||
end
|
||||
|
||||
def available_tool_ids
|
||||
available_agent_tools.pluck(:id)
|
||||
end
|
||||
|
||||
def push_event_data
|
||||
{
|
||||
id: id,
|
||||
|
||||
@@ -8,12 +8,12 @@ module Concerns::CaptainToolsHelpers
|
||||
TOOL_REFERENCE_REGEX = %r{\[[^\]]+\]\(tool://([^/)]+)\)}
|
||||
|
||||
class_methods do
|
||||
# Returns all available agent tools with their metadata.
|
||||
# Returns all built-in agent tools with their metadata.
|
||||
# Only includes tools that have corresponding class files and can be resolved.
|
||||
#
|
||||
# @return [Array<Hash>] Array of tool hashes with :id, :title, :description, :icon
|
||||
def available_agent_tools
|
||||
@available_agent_tools ||= load_agent_tools
|
||||
def built_in_agent_tools
|
||||
@built_in_agent_tools ||= load_agent_tools
|
||||
end
|
||||
|
||||
# Resolves a tool class from a tool ID.
|
||||
@@ -26,12 +26,12 @@ module Concerns::CaptainToolsHelpers
|
||||
class_name.safe_constantize
|
||||
end
|
||||
|
||||
# Returns an array of all available tool IDs.
|
||||
# Convenience method that extracts just the IDs from available_agent_tools.
|
||||
# Returns an array of all built-in tool IDs.
|
||||
# Convenience method that extracts just the IDs from built_in_agent_tools.
|
||||
#
|
||||
# @return [Array<String>] Array of available tool IDs
|
||||
def available_tool_ids
|
||||
@available_tool_ids ||= available_agent_tools.map { |tool| tool[:id] }
|
||||
# @return [Array<String>] Array of built-in tool IDs
|
||||
def built_in_tool_ids
|
||||
@built_in_tool_ids ||= built_in_agent_tools.map { |tool| tool[:id] }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
Reference in New Issue
Block a user