aboutsummaryrefslogtreecommitdiffstats
path: root/run/tool.rb
blob: 2b47788455fa7d7d4fabac8acf14ab555476c79f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
#!/usr/bin/env ruby

require 'json'
require 'pathname'

def parse_argv
  func_file = __FILE__
    func_data = nil

  if func_file.end_with?("tool.rb")
    func_file = ARGV[0]
    func_data = ARGV[1]
  else
    func_file = File.basename(func_file)
    func_data = ARGV[0]
  end

  func_file += '.rb' unless func_file.end_with?(".rb")

  [func_file, func_data]
end

def load_func(func_file)
  func_path = File.expand_path("../tools/#{func_file}", __dir__)

  begin
    require func_path
  rescue LoadError
    puts "Invalid function: #{func_file}"
    exit 1
  end
end

def load_env(file_path)
  return unless File.exist?(file_path)

  File.readlines(file_path).each do |line|
    line = line.strip
    next if line.empty? || line.start_with?('#')

    key, *value = line.split('=', 2)
    ENV[key.strip] = value.join('=').strip
  end
rescue StandardError
end

ENV['LLM_FUNCTIONS_DIR'] = Pathname.new(__dir__).join('..').expand_path.to_s

load_env(Pathname.new(ENV['LLM_FUNCTIONS_DIR']).join('.env').to_s)

func_file, func_data = parse_argv

if ENV["LLM_FUNCTION_ACTION"] == "declarate"
  load_func(func_file)
  puts JSON.pretty_generate(declarate)
else
  if func_data.nil?
    puts "No json data"
    exit 1
  end

  begin
    args = JSON.parse(func_data)
  rescue JSON::ParserError
    puts "Invalid json data"
    exit 1
  end

  load_func(func_file)
  execute(args)
end