Class: RailsAiBridge::Services::ContextGenerationService

Inherits:
RailsAiBridge::Service show all
Defined in:
lib/rails_ai_bridge/services/context_generation_service.rb

Overview

Application service for generating context files from introspection data.

Delegates to a serializer (default RailsAiBridge::Serializers::ContextFileSerializer) and normalizes successful results to a stable data shape: { written: Array, skipped: Array }, even when the serializer returns a partial hash, a non-hash, or nil. Failures are returned as a failed RailsAiBridge::Service::Result rather than raised.

Examples:

Generate all context files

result = Services::ContextGenerationService.call(introspection_data)
if result.success?
  puts "Generated #{result.data[:written].count} files"
end

Generate specific format

result = Services::ContextGenerationService.call(introspection_data, format: :claude)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context_data, serializer_class: Serializers::ContextFileSerializer, format: :all, fingerprint: nil) ⇒ ContextGenerationService

Returns a new instance of ContextGenerationService.

Parameters:

  • context_data (Hash) —

    Introspection data to serialize

  • serializer_class (Class) (defaults to: Serializers::ContextFileSerializer) —

    Serializer class (see call)

  • format (Symbol) (defaults to: :all) —

    Output format passed to the serializer

  • fingerprint (String, nil) (defaults to: nil) —

    source fingerprint passed to the serializer; when nil, computed from Fingerprinter.source_fingerprint(AppScope.current_app) at call time



41
42
43
44
45
46
47
# File 'lib/rails_ai_bridge/services/context_generation_service.rb', line 41

def initialize(context_data, serializer_class: Serializers::ContextFileSerializer, format: :all, fingerprint: nil)
  super()
  @context_data = context_data
  @serializer_class = serializer_class
  @format = format
  @fingerprint = fingerprint
end

Class Method Details

.call(context_data, format: :all, serializer_class: Serializers::ContextFileSerializer) ⇒ RailsAiBridge::Service::Result

Class-level entry point with explicit serializer and format. The source fingerprint is always computed at call time via Fingerprinter.source_fingerprint; to inject a fingerprint (or serializer class) directly, use #initialize.

Parameters:

  • context_data (Hash) —

    Introspection data to pass to the serializer

  • format (Symbol) (defaults to: :all) —

    Output format (e.g. :all, :claude, :cursor)

  • serializer_class (Class) (defaults to: Serializers::ContextFileSerializer) —

    Serializer class; must respond to #new(context_data, fingerprint:, **write_options) (+:format+ is passed as a write option) and instances must respond to #call

Returns:

  • (RailsAiBridge::Service::Result) —

    On success, data is { written: Array, skipped: Array }; on StandardError, success? is false and errors contains the message



32
33
34
# File 'lib/rails_ai_bridge/services/context_generation_service.rb', line 32

def self.call(context_data, format: :all, serializer_class: Serializers::ContextFileSerializer)
  new(context_data, serializer_class: serializer_class, format: format).call
end

Instance Method Details

#call ⇒ RailsAiBridge::Service::Result

Runs the serializer and wraps the outcome in a RailsAiBridge::Service::Result.

Successful data always uses symbol keys :written and :skipped with array values, derived only from symbol keys on a Hash return value from the serializer.

Returns:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rails_ai_bridge/services/context_generation_service.rb', line 55

def call
  fingerprint = @fingerprint || Fingerprinter.source_fingerprint(AppScope.current_app)
  serializer = @serializer_class.new(@context_data, format: @format, fingerprint: fingerprint)
  serialization_result = serializer.call

  normalized = {
    written: Array(serialization_result.is_a?(Hash) ? serialization_result[:written] : nil),
    skipped: Array(serialization_result.is_a?(Hash) ? serialization_result[:skipped] : nil)
  }

  Service::Result.new(true, data: normalized)
rescue StandardError => error
  Service::Result.new(false, errors: [error.message])
end