Class: Crimson::SessionEntry

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ SessionEntry

Returns a new instance of SessionEntry.

Parameters:

  • attrs (Hash) (defaults to: {})

    entry attributes



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

#contentString, ...

Returns:

  • (String)

    unique entry ID

  • (String, nil)

    parent entry ID for threading

  • (String)

    role (user/assistant/tool_result/system)

  • (String, nil)

    message content

  • (Array<Hash>)

    tool call data

  • (String, nil)

    tool call ID for results

  • (String, nil)

    tool name for results

  • (Hash)

    token usage metadata

  • (String)

    ISO 8601 timestamp

  • (Array<String>)

    files read by this entry

  • (Array<String>)

    files modified by this entry



21
22
23
# File 'lib/crimson/session_entry.rb', line 21

def content
  @content
end

#idString, ...

Returns:

  • (String)

    unique entry ID

  • (String, nil)

    parent entry ID for threading

  • (String)

    role (user/assistant/tool_result/system)

  • (String, nil)

    message content

  • (Array<Hash>)

    tool call data

  • (String, nil)

    tool call ID for results

  • (String, nil)

    tool name for results

  • (Hash)

    token usage metadata

  • (String)

    ISO 8601 timestamp

  • (Array<String>)

    files read by this entry

  • (Array<String>)

    files modified by this entry



21
22
23
# File 'lib/crimson/session_entry.rb', line 21

def id
  @id
end

#modified_filesString, ...

Returns:

  • (String)

    unique entry ID

  • (String, nil)

    parent entry ID for threading

  • (String)

    role (user/assistant/tool_result/system)

  • (String, nil)

    message content

  • (Array<Hash>)

    tool call data

  • (String, nil)

    tool call ID for results

  • (String, nil)

    tool name for results

  • (Hash)

    token usage metadata

  • (String)

    ISO 8601 timestamp

  • (Array<String>)

    files read by this entry

  • (Array<String>)

    files modified by this entry



21
22
23
# File 'lib/crimson/session_entry.rb', line 21

def modified_files
  @modified_files
end

#parent_idString, ...

Returns:

  • (String)

    unique entry ID

  • (String, nil)

    parent entry ID for threading

  • (String)

    role (user/assistant/tool_result/system)

  • (String, nil)

    message content

  • (Array<Hash>)

    tool call data

  • (String, nil)

    tool call ID for results

  • (String, nil)

    tool name for results

  • (Hash)

    token usage metadata

  • (String)

    ISO 8601 timestamp

  • (Array<String>)

    files read by this entry

  • (Array<String>)

    files modified by this entry



21
22
23
# File 'lib/crimson/session_entry.rb', line 21

def parent_id
  @parent_id
end

#read_filesString, ...

Returns:

  • (String)

    unique entry ID

  • (String, nil)

    parent entry ID for threading

  • (String)

    role (user/assistant/tool_result/system)

  • (String, nil)

    message content

  • (Array<Hash>)

    tool call data

  • (String, nil)

    tool call ID for results

  • (String, nil)

    tool name for results

  • (Hash)

    token usage metadata

  • (String)

    ISO 8601 timestamp

  • (Array<String>)

    files read by this entry

  • (Array<String>)

    files modified by this entry



21
22
23
# File 'lib/crimson/session_entry.rb', line 21

def read_files
  @read_files
end

#roleString, ...

Returns:

  • (String)

    unique entry ID

  • (String, nil)

    parent entry ID for threading

  • (String)

    role (user/assistant/tool_result/system)

  • (String, nil)

    message content

  • (Array<Hash>)

    tool call data

  • (String, nil)

    tool call ID for results

  • (String, nil)

    tool name for results

  • (Hash)

    token usage metadata

  • (String)

    ISO 8601 timestamp

  • (Array<String>)

    files read by this entry

  • (Array<String>)

    files modified by this entry



21
22
23
# File 'lib/crimson/session_entry.rb', line 21

def role
  @role
end

#timestampString, ...

Returns:

  • (String)

    unique entry ID

  • (String, nil)

    parent entry ID for threading

  • (String)

    role (user/assistant/tool_result/system)

  • (String, nil)

    message content

  • (Array<Hash>)

    tool call data

  • (String, nil)

    tool call ID for results

  • (String, nil)

    tool name for results

  • (Hash)

    token usage metadata

  • (String)

    ISO 8601 timestamp

  • (Array<String>)

    files read by this entry

  • (Array<String>)

    files modified by this entry



21
22
23
# File 'lib/crimson/session_entry.rb', line 21

def timestamp
  @timestamp
end

#token_usageString, ...

Returns:

  • (String)

    unique entry ID

  • (String, nil)

    parent entry ID for threading

  • (String)

    role (user/assistant/tool_result/system)

  • (String, nil)

    message content

  • (Array<Hash>)

    tool call data

  • (String, nil)

    tool call ID for results

  • (String, nil)

    tool name for results

  • (Hash)

    token usage metadata

  • (String)

    ISO 8601 timestamp

  • (Array<String>)

    files read by this entry

  • (Array<String>)

    files modified by this entry



21
22
23
# File 'lib/crimson/session_entry.rb', line 21

def token_usage
  @token_usage
end

#tool_call_idString, ...

Returns:

  • (String)

    unique entry ID

  • (String, nil)

    parent entry ID for threading

  • (String)

    role (user/assistant/tool_result/system)

  • (String, nil)

    message content

  • (Array<Hash>)

    tool call data

  • (String, nil)

    tool call ID for results

  • (String, nil)

    tool name for results

  • (Hash)

    token usage metadata

  • (String)

    ISO 8601 timestamp

  • (Array<String>)

    files read by this entry

  • (Array<String>)

    files modified by this entry



21
22
23
# File 'lib/crimson/session_entry.rb', line 21

def tool_call_id
  @tool_call_id
end

#tool_callsString, ...

Returns:

  • (String)

    unique entry ID

  • (String, nil)

    parent entry ID for threading

  • (String)

    role (user/assistant/tool_result/system)

  • (String, nil)

    message content

  • (Array<Hash>)

    tool call data

  • (String, nil)

    tool call ID for results

  • (String, nil)

    tool name for results

  • (Hash)

    token usage metadata

  • (String)

    ISO 8601 timestamp

  • (Array<String>)

    files read by this entry

  • (Array<String>)

    files modified by this entry



21
22
23
# File 'lib/crimson/session_entry.rb', line 21

def tool_calls
  @tool_calls
end

#tool_nameString, ...

Returns:

  • (String)

    unique entry ID

  • (String, nil)

    parent entry ID for threading

  • (String)

    role (user/assistant/tool_result/system)

  • (String, nil)

    message content

  • (Array<Hash>)

    tool call data

  • (String, nil)

    tool call ID for results

  • (String, nil)

    tool name for results

  • (Hash)

    token usage metadata

  • (String)

    ISO 8601 timestamp

  • (Array<String>)

    files read by this entry

  • (Array<String>)

    files modified by this entry



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).

Parameters:

  • hash (Hash)

Returns:



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.

Parameters:

  • message (Message::Base)
  • parent_id (String, nil)
  • read_files (Array<String>) (defaults to: [])
  • modified_files (Array<String>) (defaults to: [])

Returns:



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.from_message(message, parent_id:, read_files: [], modified_files: [])
  case message
  when Message::User
    new(role: "user", content: message.content, parent_id: parent_id)
  when Message::Assistant
    tc_data = message.tool_calls.map do |tc|
      { "id" => tc.id, "name" => tc.name, "arguments" => tc.arguments }
    end
    new(
      role: "assistant",
      content: message.content,
      parent_id: parent_id,
      tool_calls: tc_data
    )
  when Message::ToolResult
    new(
      role: "tool_result",
      content: message.content,
      parent_id: parent_id,
      tool_call_id: message.tool_call_id,
      tool_name: message.name,
      read_files: read_files,
      modified_files: modified_files
    )
  else
    new(role: "system", content: message&.content.to_s, parent_id: parent_id)
  end
end

Instance Method Details

#to_hHash

Convert to a hash suitable for JSON serialization.

Returns:

  • (Hash)


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.

Returns:

  • (String)

    JSON representation



61
62
63
# File 'lib/crimson/session_entry.rb', line 61

def to_json(*_args)
  JSON.generate(to_h)
end

#to_messageMessage::Base?

Convert back to a Message object.

Returns:



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 to_message
  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