Class: RubyClaude::Event
- Inherits:
-
Data
- Object
- Data
- RubyClaude::Event
- Defined in:
- lib/ruby_claude/event.rb
Overview
Immutable streaming event parsed from one line of +--output-format
stream-json+ output. The #type mirrors the CLI's type field as a
Symbol (+:system+, :assistant, :user, :result, ...).
Instance Attribute Summary collapse
-
#cost_usd ⇒ Float?
readonly
Total cost, present on the result event.
-
#duration_ms ⇒ Integer?
readonly
Duration, present on the result event.
-
#raw ⇒ Hash
readonly
The full parsed line.
-
#session_id ⇒ String?
readonly
The session id, when present.
-
#text ⇒ String?
readonly
Text extracted from assistant/user/result payloads.
-
#type ⇒ Symbol
readonly
The event type.
Class Method Summary collapse
-
.extract_text(data) ⇒ String?
Pull human-readable text out of a parsed line, if any.
-
.from_hash(data) ⇒ Event
Build an Event from one parsed NDJSON line.
-
.text_from_content(content) ⇒ String?
Join the text from a content array (or pass a bare string through).
Instance Method Summary collapse
-
#assistant? ⇒ Boolean
Whether this is an assistant message event.
-
#result? ⇒ Boolean
Whether this is the final result event.
-
#system? ⇒ Boolean
Whether this is a system event.
-
#user? ⇒ Boolean
Whether this is a user message event.
Instance Attribute Details
#cost_usd ⇒ Float? (readonly)
Returns total cost, present on the result event.
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 72 73 74 75 76 |
# File 'lib/ruby_claude/event.rb', line 20 Event = Data.define(:type, :text, :session_id, :cost_usd, :duration_ms, :raw) do # Build an Event from one parsed NDJSON line. # # @param data [Hash, nil] the parsed line # @return [Event] def self.from_hash(data) data ||= {} new( type: (data["type"] || "unknown").to_sym, text: extract_text(data), session_id: data["session_id"], cost_usd: data["total_cost_usd"], duration_ms: data["duration_ms"], raw: data ) end # Pull human-readable text out of a parsed line, if any. # # @param data [Hash] # @return [String, nil] def self.extract_text(data) case data["type"] when "assistant", "user" = data["message"] || data text_from_content(["content"]) when "result" data["result"] end end # Join the text from a content array (or pass a bare string through). # # @param content [String, Array, nil] # @return [String, nil] def self.text_from_content(content) return content if content.is_a?(String) return nil unless content.is_a?(Array) texts = content .select { |block| block.is_a?(Hash) && block["type"] == "text" } .filter_map { |block| block["text"] } texts.empty? ? nil : texts.join end # @return [Boolean] whether this is the final result event def result? = type == :result # @return [Boolean] whether this is an assistant message event def assistant? = type == :assistant # @return [Boolean] whether this is a system event def system? = type == :system # @return [Boolean] whether this is a user message event def user? = type == :user end |
#duration_ms ⇒ Integer? (readonly)
Returns duration, present on the result event.
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 72 73 74 75 76 |
# File 'lib/ruby_claude/event.rb', line 20 Event = Data.define(:type, :text, :session_id, :cost_usd, :duration_ms, :raw) do # Build an Event from one parsed NDJSON line. # # @param data [Hash, nil] the parsed line # @return [Event] def self.from_hash(data) data ||= {} new( type: (data["type"] || "unknown").to_sym, text: extract_text(data), session_id: data["session_id"], cost_usd: data["total_cost_usd"], duration_ms: data["duration_ms"], raw: data ) end # Pull human-readable text out of a parsed line, if any. # # @param data [Hash] # @return [String, nil] def self.extract_text(data) case data["type"] when "assistant", "user" = data["message"] || data text_from_content(["content"]) when "result" data["result"] end end # Join the text from a content array (or pass a bare string through). # # @param content [String, Array, nil] # @return [String, nil] def self.text_from_content(content) return content if content.is_a?(String) return nil unless content.is_a?(Array) texts = content .select { |block| block.is_a?(Hash) && block["type"] == "text" } .filter_map { |block| block["text"] } texts.empty? ? nil : texts.join end # @return [Boolean] whether this is the final result event def result? = type == :result # @return [Boolean] whether this is an assistant message event def assistant? = type == :assistant # @return [Boolean] whether this is a system event def system? = type == :system # @return [Boolean] whether this is a user message event def user? = type == :user end |
#raw ⇒ Hash (readonly)
Returns the full parsed line.
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 72 73 74 75 76 |
# File 'lib/ruby_claude/event.rb', line 20 Event = Data.define(:type, :text, :session_id, :cost_usd, :duration_ms, :raw) do # Build an Event from one parsed NDJSON line. # # @param data [Hash, nil] the parsed line # @return [Event] def self.from_hash(data) data ||= {} new( type: (data["type"] || "unknown").to_sym, text: extract_text(data), session_id: data["session_id"], cost_usd: data["total_cost_usd"], duration_ms: data["duration_ms"], raw: data ) end # Pull human-readable text out of a parsed line, if any. # # @param data [Hash] # @return [String, nil] def self.extract_text(data) case data["type"] when "assistant", "user" = data["message"] || data text_from_content(["content"]) when "result" data["result"] end end # Join the text from a content array (or pass a bare string through). # # @param content [String, Array, nil] # @return [String, nil] def self.text_from_content(content) return content if content.is_a?(String) return nil unless content.is_a?(Array) texts = content .select { |block| block.is_a?(Hash) && block["type"] == "text" } .filter_map { |block| block["text"] } texts.empty? ? nil : texts.join end # @return [Boolean] whether this is the final result event def result? = type == :result # @return [Boolean] whether this is an assistant message event def assistant? = type == :assistant # @return [Boolean] whether this is a system event def system? = type == :system # @return [Boolean] whether this is a user message event def user? = type == :user end |
#session_id ⇒ String? (readonly)
Returns the session id, when present.
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 72 73 74 75 76 |
# File 'lib/ruby_claude/event.rb', line 20 Event = Data.define(:type, :text, :session_id, :cost_usd, :duration_ms, :raw) do # Build an Event from one parsed NDJSON line. # # @param data [Hash, nil] the parsed line # @return [Event] def self.from_hash(data) data ||= {} new( type: (data["type"] || "unknown").to_sym, text: extract_text(data), session_id: data["session_id"], cost_usd: data["total_cost_usd"], duration_ms: data["duration_ms"], raw: data ) end # Pull human-readable text out of a parsed line, if any. # # @param data [Hash] # @return [String, nil] def self.extract_text(data) case data["type"] when "assistant", "user" = data["message"] || data text_from_content(["content"]) when "result" data["result"] end end # Join the text from a content array (or pass a bare string through). # # @param content [String, Array, nil] # @return [String, nil] def self.text_from_content(content) return content if content.is_a?(String) return nil unless content.is_a?(Array) texts = content .select { |block| block.is_a?(Hash) && block["type"] == "text" } .filter_map { |block| block["text"] } texts.empty? ? nil : texts.join end # @return [Boolean] whether this is the final result event def result? = type == :result # @return [Boolean] whether this is an assistant message event def assistant? = type == :assistant # @return [Boolean] whether this is a system event def system? = type == :system # @return [Boolean] whether this is a user message event def user? = type == :user end |
#text ⇒ String? (readonly)
Returns text extracted from assistant/user/result payloads.
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 72 73 74 75 76 |
# File 'lib/ruby_claude/event.rb', line 20 Event = Data.define(:type, :text, :session_id, :cost_usd, :duration_ms, :raw) do # Build an Event from one parsed NDJSON line. # # @param data [Hash, nil] the parsed line # @return [Event] def self.from_hash(data) data ||= {} new( type: (data["type"] || "unknown").to_sym, text: extract_text(data), session_id: data["session_id"], cost_usd: data["total_cost_usd"], duration_ms: data["duration_ms"], raw: data ) end # Pull human-readable text out of a parsed line, if any. # # @param data [Hash] # @return [String, nil] def self.extract_text(data) case data["type"] when "assistant", "user" = data["message"] || data text_from_content(["content"]) when "result" data["result"] end end # Join the text from a content array (or pass a bare string through). # # @param content [String, Array, nil] # @return [String, nil] def self.text_from_content(content) return content if content.is_a?(String) return nil unless content.is_a?(Array) texts = content .select { |block| block.is_a?(Hash) && block["type"] == "text" } .filter_map { |block| block["text"] } texts.empty? ? nil : texts.join end # @return [Boolean] whether this is the final result event def result? = type == :result # @return [Boolean] whether this is an assistant message event def assistant? = type == :assistant # @return [Boolean] whether this is a system event def system? = type == :system # @return [Boolean] whether this is a user message event def user? = type == :user end |
#type ⇒ Symbol (readonly)
Returns the event type.
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 72 73 74 75 76 |
# File 'lib/ruby_claude/event.rb', line 20 Event = Data.define(:type, :text, :session_id, :cost_usd, :duration_ms, :raw) do # Build an Event from one parsed NDJSON line. # # @param data [Hash, nil] the parsed line # @return [Event] def self.from_hash(data) data ||= {} new( type: (data["type"] || "unknown").to_sym, text: extract_text(data), session_id: data["session_id"], cost_usd: data["total_cost_usd"], duration_ms: data["duration_ms"], raw: data ) end # Pull human-readable text out of a parsed line, if any. # # @param data [Hash] # @return [String, nil] def self.extract_text(data) case data["type"] when "assistant", "user" = data["message"] || data text_from_content(["content"]) when "result" data["result"] end end # Join the text from a content array (or pass a bare string through). # # @param content [String, Array, nil] # @return [String, nil] def self.text_from_content(content) return content if content.is_a?(String) return nil unless content.is_a?(Array) texts = content .select { |block| block.is_a?(Hash) && block["type"] == "text" } .filter_map { |block| block["text"] } texts.empty? ? nil : texts.join end # @return [Boolean] whether this is the final result event def result? = type == :result # @return [Boolean] whether this is an assistant message event def assistant? = type == :assistant # @return [Boolean] whether this is a system event def system? = type == :system # @return [Boolean] whether this is a user message event def user? = type == :user end |
Class Method Details
.extract_text(data) ⇒ String?
Pull human-readable text out of a parsed line, if any.
41 42 43 44 45 46 47 48 49 |
# File 'lib/ruby_claude/event.rb', line 41 def self.extract_text(data) case data["type"] when "assistant", "user" = data["message"] || data text_from_content(["content"]) when "result" data["result"] end end |
.from_hash(data) ⇒ Event
Build an Event from one parsed NDJSON line.
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/ruby_claude/event.rb', line 25 def self.from_hash(data) data ||= {} new( type: (data["type"] || "unknown").to_sym, text: extract_text(data), session_id: data["session_id"], cost_usd: data["total_cost_usd"], duration_ms: data["duration_ms"], raw: data ) end |
.text_from_content(content) ⇒ String?
Join the text from a content array (or pass a bare string through).
55 56 57 58 59 60 61 62 63 |
# File 'lib/ruby_claude/event.rb', line 55 def self.text_from_content(content) return content if content.is_a?(String) return nil unless content.is_a?(Array) texts = content .select { |block| block.is_a?(Hash) && block["type"] == "text" } .filter_map { |block| block["text"] } texts.empty? ? nil : texts.join end |
Instance Method Details
#assistant? ⇒ Boolean
Returns whether this is an assistant message event.
69 |
# File 'lib/ruby_claude/event.rb', line 69 def assistant? = type == :assistant |
#result? ⇒ Boolean
Returns whether this is the final result event.
66 |
# File 'lib/ruby_claude/event.rb', line 66 def result? = type == :result |
#system? ⇒ Boolean
Returns whether this is a system event.
72 |
# File 'lib/ruby_claude/event.rb', line 72 def system? = type == :system |
#user? ⇒ Boolean
Returns whether this is a user message event.
75 |
# File 'lib/ruby_claude/event.rb', line 75 def user? = type == :user |