feat: captain custom tools v1 (#13890)

# Pull Request Template

## Description

Adds custom tool support to v1

## Type of change
- [x] New feature (non-breaking change which adds functionality)


## How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. Please also list any relevant details
for your test configuration.

<img width="1816" height="958" alt="CleanShot 2026-03-24 at 11 37 33@2x"
src="https://github.com/user-attachments/assets/2777a953-8b65-4a2d-88ec-39f395b3fb47"
/>

<img width="378" height="488" alt="CleanShot 2026-03-24 at 11 38 18@2x"
src="https://github.com/user-attachments/assets/f6973c99-efd0-40e4-90fe-4472a2f63cea"
/>

<img width="1884" height="1452" alt="CleanShot 2026-03-24 at 11 38
32@2x"
src="https://github.com/user-attachments/assets/9fba4fc4-0c33-46da-888a-52ec6bad6130"
/>



## Checklist:

- [x] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my code
- [ ] I have commented on my code, particularly in hard-to-understand
areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged and published in downstream
modules

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: Shivam Mishra <scm.mymail@gmail.com>
This commit is contained in:
Aakash Bakhle
2026-04-02 12:40:11 +05:30
committed by GitHub
parent 211fb1102d
commit 8daf6cf6cb
21 changed files with 307 additions and 57 deletions

View File

@@ -1,15 +1,23 @@
module Concerns::Toolable
extend ActiveSupport::Concern
def tool(assistant)
# Isolated namespace for user-defined custom tool classes.
# Keeps them separate from built-in classes in Captain::Tools (e.g., HttpTool, CustomHttpTool).
module CustomTools; end
def tool(assistant, base_class: Captain::Tools::HttpTool, **)
custom_tool_record = self
# Convert slug to valid Ruby constant name (replace hyphens with underscores, then camelize)
class_name = custom_tool_record.slug.underscore.camelize
# Always create a fresh class to reflect current metadata
tool_class = Class.new(Captain::Tools::HttpTool) do
tool_slug = custom_tool_record.slug
tool_class = Class.new(base_class) do
description custom_tool_record.description
# Override name to use the slug directly, avoiding the namespace prefix
# that RubyLLM's default normalization would produce (e.g., "captain--tools--custom_dog_facts").
define_method(:name) { tool_slug }
custom_tool_record.param_schema.each do |param_def|
param param_def['name'].to_sym,
type: param_def['type'],
@@ -18,17 +26,14 @@ module Concerns::Toolable
end
end
# Register the dynamically created class as a constant in the Captain::Tools namespace.
# This is required because RubyLLM's Tool base class derives the tool name from the class name
# (via Class#name). Anonymous classes created with Class.new have no name and return empty strings,
# which causes "Invalid 'tools[].function.name': empty string" errors from the LLM API.
# By setting it as a constant, the class gets a proper name (e.g., "Captain::Tools::CatFactLookup")
# which RubyLLM extracts and normalizes to "cat-fact-lookup" for the LLM API.
# We refresh the constant on each call to ensure tool metadata changes are reflected.
Captain::Tools.send(:remove_const, class_name) if Captain::Tools.const_defined?(class_name, false)
Captain::Tools.const_set(class_name, tool_class)
# Register as a constant so the class gets a proper name (Class#name).
# Anonymous classes return nil for #name, which causes "Invalid 'tools[].function.name':
# empty string" errors from the LLM API. We use CustomTools as the namespace to avoid
# collisions with real classes in Captain::Tools.
CustomTools.send(:remove_const, class_name) if CustomTools.const_defined?(class_name, false)
CustomTools.const_set(class_name, tool_class)
tool_class.new(assistant, self)
tool_class.new(assistant, self, **)
end
def build_request_url(params)