@@ -73,61 +73,26 @@ def load_schema_class(context = {})
7373 return nil unless File . exist? ( schema_file )
7474 return nil unless defined? ( RubyLLM ::Schema )
7575
76- # Load the schema file in a clean context
7776 schema_content = File . read ( schema_file )
78-
79- # Create a context for evaluating the schema
8077 schema_context = create_schema_context ( context )
8178
82- # Evaluate the schema file (this might define classes/modules)
79+ # Evaluate the schema file
8380 result = schema_context . instance_eval ( schema_content , schema_file . to_s )
8481
85- # Try different ways to find the schema:
86-
87- # 1. If the result is already a valid schema class or instance
88- if result . is_a? ( Class ) && result < RubyLLM ::Schema
89- return result . new
90- elsif result . is_a? ( RubyLLM ::Schema ) || result . respond_to? ( :to_json_schema )
82+ # Handle different patterns:
83+ # 1. RubyLLM::Schema.create { } pattern - returns instance
84+ if result . is_a? ( RubyLLM ::Schema ) || result . respond_to? ( :to_json_schema )
9185 return result
9286 end
9387
94- # 2. Look for a schema class using naming conventions
95- # Convert template name to expected class name (e.g., "identify_brand_from_transaction" -> "IdentifyBrandFromTransaction::Schema")
88+ # 2. Class definition pattern - look for TemplateClass::Schema
9689 template_class_name = @template_name . to_s . split ( '_' ) . map ( &:capitalize ) . join
97- possible_class_names = [
98- "#{ template_class_name } ::Schema" ,
99- "#{ template_class_name } Schema" ,
100- template_class_name
101- ]
102-
103- schema_class = nil
104- possible_class_names . each do |class_name |
105- begin
106- schema_class = class_name . constantize
107- break if schema_class . is_a? ( Class ) && schema_class < RubyLLM ::Schema
108- schema_class = nil
109- rescue NameError
110- # Class doesn't exist, try next one
111- end
112- end
113-
114- if schema_class
115- return schema_class
116- end
117-
118- # 3. If nothing worked, provide detailed error
119- result_info = if result . nil?
120- "nil"
121- elsif result . is_a? ( Class )
122- "Class: #{ result } (< RubyLLM::Schema: #{ result < RubyLLM ::Schema if result . respond_to? ( :< ) } )"
123- else
124- "Instance: #{ result . class } (is_a?(RubyLLM::Schema): #{ result . is_a? ( RubyLLM ::Schema ) } , responds_to?(:to_json_schema): #{ result . respond_to? ( :to_json_schema ) } )"
125- end
90+ schema_class_name = "#{ template_class_name } ::Schema"
91+
92+ schema_class = constantize_safe ( schema_class_name )
93+ return schema_class if schema_class
12694
127- raise Error , "Schema file must return a RubyLLM::Schema class or instance, or define one of: #{ possible_class_names . join ( ', ' ) } . Got: #{ result_info } "
128- rescue Error => e
129- # Re-raise our own errors as-is to preserve the detailed message
130- raise e
95+ raise Error , "Schema file must return a RubyLLM::Schema instance or define class '#{ schema_class_name } '"
13196 rescue => e
13297 raise Error , "Failed to load schema from '#{ @template_name } /schema.rb': #{ e . message } "
13398 end
@@ -158,17 +123,24 @@ def create_binding_context(context)
158123 end
159124
160125 def create_schema_context ( context )
161- # Create an object that has access to context variables and RubyLLM::Schema methods
162126 schema_context = Object . new
163-
164- # Add context variables as instance variables and methods
165127 context . each do |key , value |
166128 schema_context . instance_variable_set ( "@#{ key } " , value )
167129 schema_context . define_singleton_method ( key ) { value }
168130 end
169-
170131 schema_context
171132 end
133+
134+ def constantize_safe ( class_name )
135+ if defined? ( Rails )
136+ class_name . constantize
137+ else
138+ # Simple constantize for non-Rails environments
139+ Object . const_get ( class_name )
140+ end
141+ rescue NameError
142+ nil
143+ end
172144 end
173145 end
174146end
0 commit comments