Module: Legion::TTY::DaemonClient

Defined in:
lib/legion/tty/daemon_client.rb

Constant Summary collapse

SUCCESS_CODES =
[200, 201, 202].freeze

Class Method Summary collapse

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
# File 'lib/legion/tty/daemon_client.rb', line 21

def available?
  uri = URI("#{daemon_url}/api/health")
  response = Net::HTTP.start(uri.hostname, uri.port, open_timeout: @timeout, read_timeout: @timeout) do |http|
    http.get(uri.path)
  end
  response.code.to_i == 200
rescue StandardError => e
  Legion::Logging.debug("daemon available? check failed: #{e.message}") if defined?(Legion::Logging)
  false
end

.cached_manifestObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/legion/tty/daemon_client.rb', line 48

def cached_manifest
  return @manifest if @manifest

  return nil unless @cache_file && File.exist?(@cache_file)

  @manifest = Legion::JSON.load(File.read(@cache_file))
rescue StandardError => e
  Legion::Logging.warn("cached_manifest failed: #{e.message}") if defined?(Legion::Logging)
  nil
end

.chat(message:, model: nil, provider: nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/legion/tty/daemon_client.rb', line 77

def chat(message:, model: nil, provider: nil)
  return nil unless available?

  uri = URI("#{daemon_url}/api/llm/chat")
  payload = Legion::JSON.dump({ message: message, model: model, provider: provider })
  response = post_json(uri, payload)

  return nil unless response && SUCCESS_CODES.include?(response.code.to_i)

  Legion::JSON.load(response.body)
rescue StandardError => e
  Legion::Logging.warn("chat failed: #{e.message}") if defined?(Legion::Logging)
  nil
end

.configure(daemon_url: 'http://127.0.0.1:4567', cache_file: nil, timeout: 5) ⇒ Object



14
15
16
17
18
19
# File 'lib/legion/tty/daemon_client.rb', line 14

def configure(daemon_url: 'http://127.0.0.1:4567', cache_file: nil, timeout: 5)
  @daemon_url = daemon_url
  @cache_file = cache_file || File.expand_path('~/.legionio/catalog.json')
  @timeout = timeout
  @manifest = nil
end

.fetch_manifestObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/legion/tty/daemon_client.rb', line 32

def fetch_manifest
  uri = URI("#{daemon_url}/api/catalog")
  response = Net::HTTP.start(uri.hostname, uri.port, open_timeout: @timeout, read_timeout: @timeout) do |http|
    http.get(uri.path)
  end
  return nil unless response.code.to_i == 200

  body = Legion::JSON.load(response.body)
  @manifest = body[:data]
  write_cache(@manifest)
  @manifest
rescue StandardError => e
  Legion::Logging.warn("fetch_manifest failed: #{e.message}") if defined?(Legion::Logging)
  nil
end

.manifestObject



59
60
61
# File 'lib/legion/tty/daemon_client.rb', line 59

def manifest
  @manifest || cached_manifest
end

.match_intent(intent_text) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/legion/tty/daemon_client.rb', line 63

def match_intent(intent_text)
  return nil unless manifest

  normalized = intent_text.downcase.strip
  manifest.each do |ext|
    next unless ext[:known_intents]

    ext[:known_intents].each do |ki|
      return ki if ki[:intent]&.downcase&.strip == normalized && ki[:confidence] >= 0.8
    end
  end
  nil
end

.reset!Object



92
93
94
95
96
97
# File 'lib/legion/tty/daemon_client.rb', line 92

def reset!
  @daemon_url = nil
  @cache_file = nil
  @timeout = nil
  @manifest = nil
end