Class: Crimson::SessionEntry
- Inherits:
-
Object
- Object
- Crimson::SessionEntry
- Defined in:
- lib/crimson/session_entry.rb
Overview
Data model for a single entry in a session log. Can represent user messages, assistant responses, and tool results.
Instance Attribute Summary collapse
- #content ⇒ String, ...
- #id ⇒ String, ...
- #modified_files ⇒ String, ...
- #parent_id ⇒ String, ...
- #read_files ⇒ String, ...
- #role ⇒ String, ...
- #timestamp ⇒ String, ...
- #token_usage ⇒ String, ...
- #tool_call_id ⇒ String, ...
- #tool_calls ⇒ String, ...
- #tool_name ⇒ String, ...
Class Method Summary collapse
-
.from_h(hash) ⇒ SessionEntry
Deserialize from a hash (with string or symbol keys).
-
.from_message(message, parent_id:, read_files: [], modified_files: []) ⇒ SessionEntry
Build a session entry from a message object.
Instance Method Summary collapse
-
#initialize(attrs = {}) ⇒ SessionEntry
constructor
A new instance of SessionEntry.
-
#to_h ⇒ Hash
Convert to a hash suitable for JSON serialization.
-
#to_json(*_args) ⇒ String
JSON representation.
-
#to_message ⇒ Message::Base?
Convert back to a Message object.
Constructor Details
#initialize(attrs = {}) ⇒ SessionEntry
Returns a new instance of SessionEntry.
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/crimson/session_entry.rb', line 27 def initialize(attrs = {}) @id = attrs[:id] || SecureRandom.uuid @parent_id = attrs[:parent_id] @role = attrs[:role] @content = attrs[:content] @tool_calls = attrs[:tool_calls] || [] @tool_call_id = attrs[:tool_call_id] @tool_name = attrs[:tool_name] @token_usage = attrs[:token_usage] || {} @timestamp = attrs[:timestamp] || Time.now.utc.iso8601 @read_files = attrs[:read_files] || [] @modified_files = attrs[:modified_files] || [] end |
Instance Attribute Details
#content ⇒ String, ...
21 22 23 |
# File 'lib/crimson/session_entry.rb', line 21 def content @content end |
#id ⇒ String, ...
21 22 23 |
# File 'lib/crimson/session_entry.rb', line 21 def id @id end |
#modified_files ⇒ String, ...
21 22 23 |
# File 'lib/crimson/session_entry.rb', line 21 def modified_files @modified_files end |
#parent_id ⇒ String, ...
21 22 23 |
# File 'lib/crimson/session_entry.rb', line 21 def parent_id @parent_id end |
#read_files ⇒ String, ...
21 22 23 |
# File 'lib/crimson/session_entry.rb', line 21 def read_files @read_files end |
#role ⇒ String, ...
21 22 23 |
# File 'lib/crimson/session_entry.rb', line 21 def role @role end |
#timestamp ⇒ String, ...
21 22 23 |
# File 'lib/crimson/session_entry.rb', line 21 def @timestamp end |
#token_usage ⇒ String, ...
21 22 23 |
# File 'lib/crimson/session_entry.rb', line 21 def token_usage @token_usage end |
#tool_call_id ⇒ String, ...
21 22 23 |
# File 'lib/crimson/session_entry.rb', line 21 def tool_call_id @tool_call_id end |
#tool_calls ⇒ String, ...
21 22 23 |
# File 'lib/crimson/session_entry.rb', line 21 def tool_calls @tool_calls end |
#tool_name ⇒ String, ...
21 22 23 |
# File 'lib/crimson/session_entry.rb', line 21 def tool_name @tool_name end |
Class Method Details
.from_h(hash) ⇒ SessionEntry
Deserialize from a hash (with string or symbol keys).
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/crimson/session_entry.rb', line 68 def self.from_h(hash) new( id: hash[:id] || hash["id"], parent_id: hash[:parentId] || hash["parentId"], role: hash[:role] || hash["role"], content: hash[:content] || hash["content"], tool_calls: hash[:toolCalls] || hash["toolCalls"] || [], tool_call_id: hash[:toolCallId] || hash["toolCallId"], tool_name: hash[:toolName] || hash["toolName"], token_usage: hash[:tokenUsage] || hash["tokenUsage"] || {}, timestamp: hash[:timestamp] || hash["timestamp"], read_files: hash[:readFiles] || hash["readFiles"] || [], modified_files: hash[:modifiedFiles] || hash["modifiedFiles"] || [] ) end |
.from_message(message, parent_id:, read_files: [], modified_files: []) ⇒ SessionEntry
Build a session entry from a message object.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/crimson/session_entry.rb', line 90 def self.(, parent_id:, read_files: [], modified_files: []) case when Message::User new(role: "user", content: .content, parent_id: parent_id) when Message::Assistant tc_data = .tool_calls.map do |tc| { "id" => tc.id, "name" => tc.name, "arguments" => tc.arguments } end new( role: "assistant", content: .content, parent_id: parent_id, tool_calls: tc_data ) when Message::ToolResult new( role: "tool_result", content: .content, parent_id: parent_id, tool_call_id: .tool_call_id, tool_name: .name, read_files: read_files, modified_files: modified_files ) else new(role: "system", content: &.content.to_s, parent_id: parent_id) end end |
Instance Method Details
#to_h ⇒ Hash
Convert to a hash suitable for JSON serialization.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/crimson/session_entry.rb', line 43 def to_h h = { id: @id, parentId: @parent_id, role: @role, content: @content, toolCalls: @tool_calls, timestamp: @timestamp } h[:toolCallId] = @tool_call_id if @tool_call_id h[:toolName] = @tool_name if @tool_name h[:tokenUsage] = @token_usage unless @token_usage.empty? h[:readFiles] = @read_files unless @read_files.empty? h[:modifiedFiles] = @modified_files unless @modified_files.empty? h end |
#to_json(*_args) ⇒ String
Returns JSON representation.
61 62 63 |
# File 'lib/crimson/session_entry.rb', line 61 def to_json(*_args) JSON.generate(to_h) end |
#to_message ⇒ Message::Base?
Convert back to a Message object.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/crimson/session_entry.rb', line 121 def case @role when "user" Message::User.new(@content) when "assistant" tcs = (@tool_calls || []).map do |tc| Message::ToolCall.new( id: tc["id"], name: tc["name"], arguments: tc["arguments"] ) end Message::Assistant.new(content: @content, tool_calls: tcs) when "tool_result" Message::ToolResult.new( tool_call_id: @tool_call_id, name: @tool_name, content: @content ) else nil end end |