Class: SkillBench::Execution::ContextHydrator

Inherits:
Object
  • Object
show all
Defined in:
lib/skill_bench/execution/context_hydrator.rb

Overview

Responsible for loading source context files from a given path and wrapping them in XML tags for injection into the LLM system prompt.

Constant Summary collapse

HYDRATION_FAILED =

Error message returned when context hydration fails.

'Failed to hydrate context from source path'
TEXT_EXTENSIONS =

File extensions considered for context hydration.

%w[.md .rb .json .yml .yaml .txt].freeze
MAX_FILE_SIZE =

Maximum file size (in bytes) for files included in context hydration.

50_000

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_path: nil, skill_path: nil, base_path: nil) ⇒ void

Parameters:

  • source_path (String) (defaults to: nil)

    The path to the source directory containing readable files.

  • skill_path (String) (defaults to: nil)

    Deprecated alias for source_path.

  • base_path (Pathname, String) (defaults to: nil)

    The base path to resolve the source directory against.

Raises:

  • (TypeError)

    when the provided source or base path cannot be converted into a pathname.



35
36
37
38
# File 'lib/skill_bench/execution/context_hydrator.rb', line 35

def initialize(source_path: nil, skill_path: nil, base_path: nil)
  @source_path = source_path || skill_path
  @base_path = base_path || Pathname.new(Dir.pwd)
end

Class Method Details

.call(params) ⇒ Hash

Loads and formats source context files.

Parameters:

  • params (Hash)

    The configuration for context hydration.

Options Hash (params):

  • :source_path (String)

    The path to the source directory containing readable files.

  • :skill_path (String)

    Deprecated alias for ‘:source_path`.

  • :base_path (Pathname, String) — default: optional

    The base path to resolve the source directory against.

Returns:

  • (Hash)

    A result hash with :success, and :response containing the XML formatted context.

Raises:

  • (TypeError)

    when the provided source or base path cannot be converted into a pathname.



26
27
28
# File 'lib/skill_bench/execution/context_hydrator.rb', line 26

def self.call(params)
  new(**params).call
end

Instance Method Details

#callHash

Performs the hydration process.

Returns:

  • (Hash)

    The standardized result hash indicating success or failure.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/skill_bench/execution/context_hydrator.rb', line 43

def call
  return missing_path_result unless @source_path

  full_path = @base_path.join(@source_path).expand_path
  base_expanded = @base_path.expand_path

  return missing_path_result unless full_path.to_path.start_with?(base_expanded.to_path)
  return missing_path_result unless full_path.exist? && full_path.directory?

  context_files = collect_context_files(full_path)
  xml_context = build_xml(context_files)

  { success: true, response: { context: xml_context } }
rescue StandardError => e
  SkillBench::ErrorLogger.log_error(e, 'Hydration Error')
  { success: false, response: { error: { message: e.message } } }
end