Class: I18nContextGenerator::ContextExtractor

Inherits:
Object
  • Object
show all
Includes:
CacheIdentity, RunLogging, SourceEntries, SourceFilters, TranslationFilters, Workflow, Writers::Helpers
Defined in:
lib/i18n_context_generator/context_extractor.rb,
lib/i18n_context_generator/context_extractor/workflow.rb,
lib/i18n_context_generator/context_extractor/run_logging.rb,
lib/i18n_context_generator/context_extractor/cache_identity.rb,
lib/i18n_context_generator/context_extractor/source_entries.rb,
lib/i18n_context_generator/context_extractor/source_filters.rb,
lib/i18n_context_generator/context_extractor/extraction_result.rb,
lib/i18n_context_generator/context_extractor/translation_filters.rb

Overview

Main orchestrator that parses translation files, searches source code for usages, sends context to the LLM, and writes results via the configured writer.

Defined Under Namespace

Modules: CacheIdentity, RunLogging, SourceEntries, SourceFilters, TranslationFilters, Workflow Classes: ExtractionResult

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Writers::Helpers

#find_swift_files, #result_matches_source_path?, #skip_description?, #writable_result?

Constructor Details

#initialize(config, log_output: nil, structured_output: nil, patch_output: nil, quiet: false, progress: true) ⇒ ContextExtractor

Returns a new instance of ContextExtractor.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/i18n_context_generator/context_extractor.rb', line 25

def initialize(config, log_output: nil, structured_output: nil, patch_output: nil,
               quiet: false, progress: true)
  @config = config
  @configured_log_output = log_output
  @structured_output = structured_output
  @patch_output = patch_output
  @quiet = quiet
  @progress_enabled = progress && !quiet
  @results = Concurrent::Array.new
  @errors = Concurrent::Array.new
  @metrics = RunMetrics.from([], provider: @config.provider, model: resolved_model)

  # Defer initialization of expensive resources
  @searcher = nil
  @llm = nil
  @cache = nil
  @supplemental_context = nil
  @cache_context_sources = nil
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



23
24
25
# File 'lib/i18n_context_generator/context_extractor.rb', line 23

def errors
  @errors
end

#metricsObject (readonly)

Returns the value of attribute metrics.



23
24
25
# File 'lib/i18n_context_generator/context_extractor.rb', line 23

def metrics
  @metrics
end

#resultsObject (readonly)

Returns the value of attribute results.



23
24
25
# File 'lib/i18n_context_generator/context_extractor.rb', line 23

def results
  @results
end

Instance Method Details

#runObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/i18n_context_generator/context_extractor.rb', line 45

def run
  @config.validate!
  @platform = PlatformValidator.new(@config).validate!

  entries = load_entries
  entries = filter_entries(entries) if @config.key_filter
  entries = filter_by_diff(entries) if @config.diff_base && translation_backed_discovery?
  entries = filter_by_range(entries) if @config.start_key || @config.end_key

  if entries.empty?
    log_empty_entries_message
    return
  end

  log_loaded_entries(entries.size)

  return if pre_extraction_stage_handled?(entries)

  # Resolve shared prompt inputs and provider state on the caller thread so
  # file reads, digest work, and configuration errors are not duplicated by workers.
  context_sources = supplemental_context
  cache_context_sources
  llm_client = llm
  unless context_sources.empty?
    llm_client.validate_supplemental_context!(
      supplemental_context: context_sources,
      redact_prompts: @config.redact_prompts,
      max_prompt_chars: @config.max_prompt_chars
    )
  end
  process_entries(entries)
  @metrics = RunMetrics.from(@results, provider: @config.provider, model: resolved_model)

  deliver_results

  log "Errors: #{@errors.size}" if @errors.any?
  log_metrics
end