Class: Cline::SessionMessage

Inherits:
Schema
  • Object
show all
Defined in:
lib/cline/session_message.rb

Overview

Session's message

Defined Under Namespace

Classes: MessageContent, Metrics, ModelInfo

Internal collapse

Attributes inherited from Schema

#extra_attributes

Public API collapse

Internal collapse

Methods inherited from Schema

#==, as_hash, cast, cline_snake_attributes, #to_cline_json, #to_hash

Instance Attribute Details

#cline_modelsModels

Returns The Clines models used to interpret the message.

Returns:

  • (Models)

    The Clines models used to interpret the message



167
168
169
# File 'lib/cline/session_message.rb', line 167

def cline_models
  @cline_models
end

Class Method Details

.of_hash(hash, *args, cline_models: nil, **kwargs) ⇒ Schema

Parse a Hash object and instantiate the proper instance from it.

Parameters:

  • hash (Hash)

    Data

  • args (Array)

    Remaining arguments to be transferred to Shale

  • cline_models (Models, nil) (defaults to: nil)

    The Clines models used to interpret the message, or nil if none

  • kwargs (Hash)

    Remaining kwargs to be transferred to Shale

Returns:

  • (Schema)

    Corresponding instance



160
161
162
163
164
# File 'lib/cline/session_message.rb', line 160

def self.of_hash(hash, *args, cline_models: nil, **kwargs)
  instance = super(hash, *args, **kwargs)
  instance.cline_models = cline_models if cline_models
  instance
end

Instance Method Details

#contentArray<MessageContent>

Returns Content blocks.

Returns:



90
# File 'lib/cline/session_message.rb', line 90

attribute :content, Utils::Schema.collection(MessageContent)

#idString

Returns Message identifier.

Returns:

  • (String)

    Message identifier



84
# File 'lib/cline/session_message.rb', line 84

attribute :id, :string

#metricsMetrics?

Returns Usage metrics.

Returns:



99
# File 'lib/cline/session_message.rb', line 99

attribute :metrics, Metrics

#model_infoModelInfo?

Returns Model metadata.

Returns:



96
# File 'lib/cline/session_message.rb', line 96

attribute :model_info, ModelInfo

#roleString

Returns Role (user or assistant).

Returns:

  • (String)

    Role (user or assistant)



87
# File 'lib/cline/session_message.rb', line 87

attribute :role, :string

#timestampTime

Get the message timestamp as a Ruby time

Returns:

  • (Time)

    The message timestamp



104
105
106
# File 'lib/cline/session_message.rb', line 104

def timestamp
  @timestamp ||= Time.at(ts / 1000.0)
end

#to_human(limit: 128) ⇒ String

Return a human-friendly version of a message. Useful for stdout or logging.

Parameters:

  • limit (Integer) (defaults to: 128)

    Number of characters the message should be limited to

Returns:

  • (String)

    The human translation



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/cline/session_message.rb', line 131

def to_human(limit: 128)
  (
    case role
    when 'user'
      "User: #{first_text}"
    when 'assistant'
      tool_use = content.find { |c| c.type == 'tool_use' }
      if tool_use
        "Assistant uses #{tool_use.name}"
      else
        "Assistant: #{first_text}"
      end
    when 'tool'
      "Tool result: #{content.find { |c| c.type == 'tool_result' }&.content}"
    else
      to_s
    end
  ).ellipsized(limit)
end

#tsInteger

Returns Message timestamp in milliseconds.

Returns:

  • (Integer)

    Message timestamp in milliseconds



93
# File 'lib/cline/session_message.rb', line 93

attribute :ts, :integer

#usageUsage?

Get the usage statistics of this message, if any

Returns:

  • (Usage, nil)

    The usage statistics, or nil if none



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/cline/session_message.rb', line 111

def usage
  return unless metrics

  @usage ||= Usage.new(
    **{
      cost: metrics.cost,
      input_tokens: metrics.input_tokens,
      output_tokens: metrics.output_tokens,
      cache_read_tokens: metrics.cache_read_tokens,
      cache_write_tokens: metrics.cache_write_tokens,
      cline_model: cline_models && cline_models[model_info.id]
    }.compact
  )
end