Class: SharedTools::Tools::CompositeAnalysisTool

Inherits:
RubyLLM::Tool
  • Object
show all
Defined in:
lib/shared_tools/tools/composite_analysis_tool.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger: nil) ⇒ CompositeAnalysisTool

Returns a new instance of CompositeAnalysisTool.



57
58
59
# File 'lib/shared_tools/tools/composite_analysis_tool.rb', line 57

def initialize(logger: nil)
  @logger = logger || RubyLLM.logger
end

Class Method Details

.nameObject



8
# File 'lib/shared_tools/tools/composite_analysis_tool.rb', line 8

def self.name = "composite_analysis"

Instance Method Details

#execute(data_source: nil, data: nil, analysis_type: "standard", **options) ⇒ Object



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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/shared_tools/tools/composite_analysis_tool.rb', line 61

def execute(data_source: nil, data: nil, analysis_type: "standard", **options)
  if data_source.nil? && data.nil?
    return { success: false, error: "Either data_source or data must be provided." }
  end

  results = {}
  analysis_start = Time.now

  begin
    @logger.info("CompositeAnalysisTool#execute data_source=#{data_source.inspect} analysis_type=#{analysis_type}")

    # Step 1: Fetch data using appropriate method
    @logger.debug("Fetching data from source...")
    if data
      results[:data] = parse_inline_data(data)
      results[:source_type] = 'inline'
    elsif data_source.start_with?('http://', 'https://')
      results[:data] = fetch_web_data(data_source)
      results[:source_type] = 'web'
    else
      results[:data] = read_file_data(data_source)
      results[:source_type] = 'file'
    end

    # Step 2: Analyze data structure
    @logger.debug("Analyzing data structure...")
    results[:structure] = analyze_data_structure(results[:data])

    # Step 3: Generate insights based on analysis type
    if ['standard', 'comprehensive'].include?(analysis_type)
      @logger.debug("Generating insights...")
      results[:insights] = generate_insights(results[:data], results[:structure], options)
    end

    # Step 4: Create visualization suggestions
    if results[:structure][:numeric_columns]&.any?
      @logger.debug("Suggesting visualizations...")
      viz_limit = options[:visualization_limit] || 5
      results[:visualizations] = suggest_visualizations(results[:structure], viz_limit)
    end

    # Step 5: Perform correlation analysis for comprehensive mode
    if analysis_type == 'comprehensive' && results[:structure][:numeric_columns]&.length.to_i > 1
      include_corr = options[:include_correlations].nil? ? true : options[:include_correlations]
      if include_corr
        @logger.debug("Performing correlation analysis...")
        results[:correlations] = perform_correlation_analysis(results[:data], results[:structure])
      end
    end

    analysis_duration = (Time.now - analysis_start).round(3)
    @logger.info("Analysis completed in #{analysis_duration}s")

    {
      success: true,
      analysis: results,
      data_source: data_source,
      analysis_type: analysis_type,
      analyzed_at: Time.now.iso8601,
      duration_seconds: analysis_duration
    }
  rescue => e
    @logger.error("Analysis failed: #{e.message}")
    {
      success: false,
      error: e.message,
      error_type: e.class.name,
      data_source: data_source,
      partial_results: results
    }
  end
end