Module: Legion::LLM::Tools::Interceptors::PythonVenv

Defined in:
lib/legion/llm/tools/interceptors/python_venv.rb

Constant Summary collapse

TOOL_PATTERN =
/\A(python3?|pip3?)\z/i

Class Method Summary collapse

Class Method Details

.match?(tool_name) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/legion/llm/tools/interceptors/python_venv.rb', line 20

def match?(tool_name)
  TOOL_PATTERN.match?(tool_name.to_s)
end

.pip_pathObject



47
48
49
# File 'lib/legion/llm/tools/interceptors/python_venv.rb', line 47

def pip_path
  Special.pip_path || "#{venv_dir}/bin/pip3"
end

.python_pathObject



43
44
45
# File 'lib/legion/llm/tools/interceptors/python_venv.rb', line 43

def python_path
  Special.python_path || "#{venv_dir}/bin/python3"
end

.register!Object



14
15
16
17
18
# File 'lib/legion/llm/tools/interceptors/python_venv.rb', line 14

def register!
  Interceptor.register(:python_venv, matcher: method(:match?)) do |_tool_name, **args|
    rewrite(**args)
  end
end

.rewrite(**args) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/legion/llm/tools/interceptors/python_venv.rb', line 28

def rewrite(**args)
  return args unless venv_available?

  command = args[:command]
  return args unless command.is_a?(String)

  args.merge(command: rewrite_command(command))
end

.rewrite_command(command) ⇒ Object



37
38
39
40
41
# File 'lib/legion/llm/tools/interceptors/python_venv.rb', line 37

def rewrite_command(command)
  command
    .sub(/\Apython3?(\s|\z)/, "#{python_path}\\1")
    .sub(/\Apip3?(\s|\z)/,    "#{pip_path}\\1")
end

.venv_available?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/legion/llm/tools/interceptors/python_venv.rb', line 24

def venv_available?
  Special.python_available? || File.exist?("#{venv_dir}/pyvenv.cfg")
end

.venv_dirObject



51
52
53
54
# File 'lib/legion/llm/tools/interceptors/python_venv.rb', line 51

def venv_dir
  configured = Legion::Settings[:llm][:tools][:python_venv_dir]
  File.expand_path(ENV['LEGION_PYTHON_VENV'] || configured)
end