Class: SwarmCLI::Formatters::JsonFormatter
- Inherits:
-
Object
- Object
- SwarmCLI::Formatters::JsonFormatter
- Defined in:
- lib/swarm_cli/formatters/json_formatter.rb
Overview
JsonFormatter streams JSON log entries in real-time for consumption by scripts. Each log entry is output as a single line of JSON (newline-delimited JSON).
No colors, no spinners, no animations - just pure structured data.
Instance Method Summary collapse
-
#initialize(output: $stdout) ⇒ JsonFormatter
constructor
A new instance of JsonFormatter.
-
#on_error(error:, duration: nil) ⇒ Object
Called when swarm execution fails.
-
#on_log(entry) ⇒ Object
Called for each log entry from SwarmSDK.
-
#on_start(config_path:, swarm_name:, lead_agent:, prompt:) ⇒ Object
Called when swarm execution starts.
-
#on_success(result:) ⇒ Object
Called when swarm execution completes successfully.
Constructor Details
#initialize(output: $stdout) ⇒ JsonFormatter
Returns a new instance of JsonFormatter.
10 11 12 |
# File 'lib/swarm_cli/formatters/json_formatter.rb', line 10 def initialize(output: $stdout) @output = output end |
Instance Method Details
#on_error(error:, duration: nil) ⇒ Object
Called when swarm execution fails
Emits error information as JSON. If error occurs during execution, SwarmSDK's swarm_stop event will also contain error details. This handles errors that occur before or outside swarm execution.
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/swarm_cli/formatters/json_formatter.rb', line 41 def on_error(error:, duration: nil) error_data = { type: "error", error_class: error.class.name, error_message: error., timestamp: Time.now.utc.iso8601(6), } error_data[:duration] = duration if duration error_data[:backtrace] = error.backtrace if error.respond_to?(:backtrace) && error.backtrace emit(error_data) end |
#on_log(entry) ⇒ Object
Called for each log entry from SwarmSDK
24 25 26 |
# File 'lib/swarm_cli/formatters/json_formatter.rb', line 24 def on_log(entry) emit(entry) end |
#on_start(config_path:, swarm_name:, lead_agent:, prompt:) ⇒ Object
Called when swarm execution starts
Note: SwarmSDK now automatically emits swarm_start as a log event, so we don't emit it here to avoid duplicates. The swarm_start event will flow through on_log() from the SwarmSDK event stream.
19 20 21 |
# File 'lib/swarm_cli/formatters/json_formatter.rb', line 19 def on_start(config_path:, swarm_name:, lead_agent:, prompt:) # SwarmSDK emits swarm_start automatically - no need to emit here end |
#on_success(result:) ⇒ Object
Called when swarm execution completes successfully
No action needed - SwarmSDK's swarm_stop event already contains all necessary information (content, last_agent, duration, metrics, etc.)
32 33 34 |
# File 'lib/swarm_cli/formatters/json_formatter.rb', line 32 def on_success(result:) # SwarmSDK emits swarm_stop automatically with complete information end |