Class: Agent::Sessions::Message

Inherits:
Data
  • Object
show all
Defined in:
lib/agent/sessions/message.rb

Overview

One turn. role is normalized to :user, :assistant, :system or :tool, with :unknown for a role the adapter did not recognize — the four the spec names were written before any real corpus was read, and Codex promptly said "developer".

raw is never dropped (Layer 3 rule 1): when this normalization is wrong or incomplete, a caller escapes the abstraction instead of forking the gem.

usage and model are nil wherever the format does not put them on the message itself — Codex records tokens in separate event records and the model in its session header, so its messages carry neither; the reader's session-level usage is where those formats answer. A nil here means "not recorded on this message", never "zero tokens".

Constant Summary collapse

ROLES =
%i[user assistant system tool unknown].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(role:, at:, parts:, raw:, usage: nil, model: nil) ⇒ Message

Returns a new instance of Message.

Raises:

  • (ArgumentError)


19
20
21
22
23
24
# File 'lib/agent/sessions/message.rb', line 19

def initialize(role:, at:, parts:, raw:, usage: nil, model: nil)
  roles = self.class::ROLES
  raise ArgumentError, "role #{role.inspect} must be one of #{roles.join(", ")}" unless roles.include?(role)

  super
end

Instance Attribute Details

#atObject (readonly)

Returns the value of attribute at

Returns:

  • (Object)

    the current value of at



18
19
20
# File 'lib/agent/sessions/message.rb', line 18

def at
  @at
end

#modelObject (readonly)

Returns the value of attribute model

Returns:

  • (Object)

    the current value of model



18
19
20
# File 'lib/agent/sessions/message.rb', line 18

def model
  @model
end

#partsObject (readonly)

Returns the value of attribute parts

Returns:

  • (Object)

    the current value of parts



18
19
20
# File 'lib/agent/sessions/message.rb', line 18

def parts
  @parts
end

#rawObject (readonly)

Returns the value of attribute raw

Returns:

  • (Object)

    the current value of raw



18
19
20
# File 'lib/agent/sessions/message.rb', line 18

def raw
  @raw
end

#roleObject (readonly)

Returns the value of attribute role

Returns:

  • (Object)

    the current value of role



18
19
20
# File 'lib/agent/sessions/message.rb', line 18

def role
  @role
end

#usageObject (readonly)

Returns the value of attribute usage

Returns:

  • (Object)

    the current value of usage



18
19
20
# File 'lib/agent/sessions/message.rb', line 18

def usage
  @usage
end

Instance Method Details

#textObject

Concatenated :text parts, as the design doc specifies — no separator inserted, because a separator is a formatting decision this layer has no business making. A caller that needs the boundaries has parts.



29
30
31
# File 'lib/agent/sessions/message.rb', line 29

def text
  parts.select { |part| part.type == :text }.map(&:text).join
end