Class: Legion::CLI::Chat::Tools::ViewEvents
Constant Summary
collapse
- DEFAULT_PORT =
4567
- DEFAULT_HOST =
'127.0.0.1'
Class Method Summary
collapse
Methods inherited from Tools::Base
deferred, deferred?, description, error_response, extension, handle_exception, input_schema, log, mcp_category, mcp_tier, runner, sticky, tags, text_response, tool_name, trigger_words
Class Method Details
.api_get(path) ⇒ Object
71
72
73
74
75
76
77
78
|
# File 'lib/legion/cli/chat/tools/view_events.rb', line 71
def self.api_get(path)
uri = URI("http://#{DEFAULT_HOST}:#{api_port}#{path}")
http = Net::HTTP.new(uri.host, uri.port)
http.open_timeout = 2
http.read_timeout = 5
response = http.get(uri.request_uri)
::JSON.parse(response.body, symbolize_names: true)
end
|
.api_port ⇒ Object
80
81
82
83
84
85
86
|
# File 'lib/legion/cli/chat/tools/view_events.rb', line 80
def self.api_port
return DEFAULT_PORT unless defined?(Legion::Settings)
Legion::Settings[:api]&.dig(:port) || DEFAULT_PORT
rescue StandardError
DEFAULT_PORT
end
|
.call(count: 15) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/legion/cli/chat/tools/view_events.rb', line 32
def self.call(count: 15)
count = count.to_i.clamp(1, 100)
data = api_get("/api/events/recent?count=#{count}")
return "API error: #{data[:error]}" if data[:error]
events = data[:data] || data
events = [events] if events.is_a?(Hash)
return 'No recent events.' if !events.is_a?(Array) || events.empty?
format_events(events)
rescue Errno::ECONNREFUSED
'Legion daemon not running (cannot reach events API).'
rescue StandardError => e
Legion::Logging.warn("ViewEvents#execute failed: #{e.message}") if defined?(Legion::Logging)
"Error fetching events: #{e.message}"
end
|
62
63
64
65
66
67
68
69
|
# File 'lib/legion/cli/chat/tools/view_events.rb', line 62
def self.(event)
parts = []
%i[extension worker_id status severity message rule].each do |key|
val = event[key] || event[key.to_s]
parts << "#{key}: #{val}" if val
end
parts.empty? ? nil : parts.join(', ')
end
|
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/legion/cli/chat/tools/view_events.rb', line 49
def self.format_events(events)
lines = ["Recent Events (#{events.size}):\n"]
events.each do |ev|
name = ev[:event] || ev['event'] || 'unknown'
ts = ev[:timestamp] || ev['timestamp'] || ev[:at] || ev['at']
detail = (ev)
entry = " [#{ts}] #{name}"
entry += " — #{detail}" if detail
lines << entry
end
lines.join("\n")
end
|