Class: RailsAiBridge::Tools::Query::PayloadFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai_bridge/tools/query/payload_formatter.rb

Overview

Builds the compact JSON payload for a query result, honoring the detail level and the hard row cap.

Constant Summary collapse

LIMITS_BY_DETAIL =

Row limits by detail level (summary never returns rows).

{ 'summary' => 0, 'standard' => 20, 'full' => Query::MAX_ROWS }.freeze
DEFAULT_ROWS =

Fallback row limit for an unrecognized detail level.

20

Instance Method Summary collapse

Constructor Details

#initialize(result, row_limit:, detail:) ⇒ PayloadFormatter

Returns a new instance of PayloadFormatter.

Parameters:

  • result (ActiveRecord::Result) —

    executed statement result

  • row_limit (Integer, nil) —

    requested row limit

  • detail (String) —

    summary, standard, or full



18
19
20
21
22
23
# File 'lib/rails_ai_bridge/tools/query/payload_formatter.rb', line 18

def initialize(result, row_limit:, detail:)
  @columns = result.columns
  @rows = Query.redact_columns(@columns, result.to_a)
  @row_limit = row_limit
  @detail = detail
end

Instance Method Details

#format ⇒ Hash

Builds the payload: columns, row count, truncation flag, and rows (rows omitted for summary detail).

Returns:

  • (Hash) —

    compact payload



29
30
31
32
33
34
35
# File 'lib/rails_ai_bridge/tools/query/payload_formatter.rb', line 29

def format
  total = @rows.size
  payload = { columns: @columns, row_count: total }
  return payload if @detail == 'summary'

  payload.merge(truncated: total > limit, rows: @rows.first(limit))
end