Class: Ace::LLM::Molecules::FormatHandlers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/llm/molecules/format_handlers.rb

Overview

Base format handler class

Direct Known Subclasses

JSON, Markdown, Text

Instance Method Summary collapse

Instance Method Details

#build_cost_summary(cost_data) ⇒ String?

Build cost summary string from cost metadata

Parameters:

  • cost_data (Hash)

    Cost breakdown data

Returns:

  • (String, nil)

    Formatted cost summary



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ace/llm/molecules/format_handlers.rb', line 61

def build_cost_summary(cost_data)
  return nil unless cost_data && cost_data[:total]

  cost_parts = []
  cost_parts << "Cost: $#{format_cost(cost_data[:total])}"

  if cost_data[:input] || cost_data[:output]
    breakdown = []
    breakdown << "input: $#{format_cost(cost_data[:input])}" if cost_data[:input]
    breakdown << "output: $#{format_cost(cost_data[:output])}" if cost_data[:output]

    if cost_data[:cache_creation] && cost_data[:cache_creation] > 0
      breakdown << "cache creation: $#{format_cost(cost_data[:cache_creation])}"
    end

    if cost_data[:cache_read] && cost_data[:cache_read] > 0
      breakdown << "cache read: $#{format_cost(cost_data[:cache_read])}"
    end

    cost_parts << " (#{breakdown.join(", ")})" unless breakdown.empty?
  end

  cost_parts.join
end

#format(response, **options) ⇒ String

Format response for output

Parameters:

  • response (Hash)

    Response with :text and normalized metadata

  • options (Hash)

    Additional formatting options

Returns:

  • (String)

    Formatted output

Raises:

  • (NotImplementedError)


17
18
19
# File 'lib/ace/llm/molecules/format_handlers.rb', line 17

def format(response, **options)
  raise NotImplementedError, "Subclasses must implement #format"
end

#format_cost(cost) ⇒ String

Format cost value for display

Parameters:

  • cost (Float, Numeric)

    Cost value

Returns:

  • (String)

    Formatted cost string



89
90
91
92
93
# File 'lib/ace/llm/molecules/format_handlers.rb', line 89

def format_cost(cost)
  return "0.000000" if cost.nil? || cost.zero?

  sprintf("%.6f", cost)
end

#generate_summary(response, file_path) ⇒ String

Generate summary for stdout when writing to file

Parameters:

  • response (Hash)

    Response with :text and normalized metadata

  • file_path (String)

    Output file path

Returns:

  • (String)

    Summary text



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
# File 'lib/ace/llm/molecules/format_handlers.rb', line 25

def generate_summary(response, file_path)
   = response[:metadata] || {}

  summary_parts = []
  summary_parts << "Response saved to: #{file_path}"

  if [:provider]
    summary_parts << if [:model]
      "Provider: #{[:provider]} (#{[:model]})"
    else
      "Provider: #{[:provider]}"
    end
  end

  summary_parts << "Execution time: #{[:took]}s" if [:took]

  if [:input_tokens] && [:output_tokens]
    tokens_info = "Tokens: #{[:input_tokens]} input, #{[:output_tokens]} output"
    if [:cached_tokens] && [:cached_tokens] > 0
      tokens_info += ", #{[:cached_tokens]} cached"
    end
    summary_parts << tokens_info
  end

  # Add cost information if available
  if [:cost]
    cost_info = build_cost_summary([:cost])
    summary_parts << cost_info if cost_info
  end

  summary_parts.join("\n")
end