Class: SorbetBaml::DSPyToolConverter
- Inherits:
-
Object
- Object
- SorbetBaml::DSPyToolConverter
- Extended by:
- T::Sig
- Defined in:
- lib/sorbet_baml/dspy_tool_converter.rb
Overview
Converter for DSPy tools to BAML format
Class Method Summary collapse
Instance Method Summary collapse
- #convert_dspy_tool(klass) ⇒ Object
-
#initialize(options = {}) ⇒ DSPyToolConverter
constructor
A new instance of DSPyToolConverter.
Constructor Details
#initialize(options = {}) ⇒ DSPyToolConverter
Returns a new instance of DSPyToolConverter.
17 18 19 20 21 |
# File 'lib/sorbet_baml/dspy_tool_converter.rb', line 17 def initialize( = {}) @options = @indent_size = T.let(.fetch(:indent_size, 2), Integer) @include_descriptions = T.let(.fetch(:include_descriptions, true), T::Boolean) end |
Class Method Details
.from_dspy_tool(klass, options = {}) ⇒ Object
12 13 14 |
# File 'lib/sorbet_baml/dspy_tool_converter.rb', line 12 def self.from_dspy_tool(klass, = {}) new().convert_dspy_tool(klass) end |
Instance Method Details
#convert_dspy_tool(klass) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/sorbet_baml/dspy_tool_converter.rb', line 24 def convert_dspy_tool(klass) # Extract tool metadata from DSPy tool tool_name = klass.tool_name_value || klass.name&.split('::')&.last tool_description = klass.tool_description_value # Get parameters from DSPy's call_schema_object which extracts from Sorbet signatures. call_schema = klass.call_schema_object parameters = call_schema[:properties] || {} required_params = call_schema[:required] || [] # Extract Sorbet types directly from the call method signature for richer type info. # This gives us access to T::Enum classes, T::Struct references, etc. that are lost # when converting to JSON schema. sorbet_types = extract_sorbet_types(klass) # Collect enum classes referenced by parameters for generating enum definitions. enum_classes = collect_enum_classes(sorbet_types) lines = [] # Add tool description as class-level comment if available lines << "// #{tool_description}" if @include_descriptions && tool_description # Generate enum definitions between description and class if enum_classes.any? lines << '' unless lines.empty? enum_converter = Converter.new(@options) enum_classes.each_with_index do |enum_klass, i| lines << '' unless i.zero? lines << enum_converter.convert_enum(enum_klass) end lines << '' end # Generate BAML class definition lines << "class #{tool_name} {" parameters.each do |param_name, param_info| # Use Sorbet type mapping when available (preserves enum/struct type names). # Fall back to JSON schema conversion for tools without Sorbet signatures. baml_type = if sorbet_types.key?(param_name) TypeMapper.map_type(sorbet_types[param_name]) else json_schema_type_to_baml(param_info) end # TypeMapper already encodes T.nilable(...) as a trailing `?`. # Avoid emitting invalid `??` when DSPy also marks the kwarg optional. baml_type += '?' if !required_params.include?(param_name.to_s) && !baml_type.end_with?('?') line = "#{' ' * @indent_size}#{param_name} #{baml_type}" # Add description from schema if available if @include_descriptions && param_info[:description] escaped_description = param_info[:description].gsub('"', '\\"') line += " @description(\"#{escaped_description}\")" end lines << line end lines << '}' lines.join("\n") end |