Class: Truffle::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/truffle/message.rb

Overview

A single message in an agent conversation.

Truffle works with one flat message type across every provider. The provider layer is responsible for translating these into whatever wire shape a given API expects (see Truffle::Providers::Base#serialize_messages). Keeping a single in-memory representation is what lets the agent loop stay provider-agnostic.

Roles:

:system    - instructions that steer the assistant
:user      - input from the human (or upstream caller)
:assistant - a model turn; may carry tool_calls instead of (or with) text
:tool      - the result of running a tool, linked by tool_call_id

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(role:, content: nil, tool_calls: [], tool_call_id: nil, name: nil) ⇒ Message

Returns a new instance of Message.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/truffle/message.rb', line 21

def initialize(role:, content: nil, tool_calls: [], tool_call_id: nil, name: nil)
  role = role.to_sym
  unless ROLES.include?(role)
    raise ArgumentError, "unknown role #{role.inspect}, expected one of #{ROLES.inspect}"
  end

  @role = role
  @content = content
  @tool_calls = tool_calls || []
  @tool_call_id = tool_call_id
  @name = name
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



19
20
21
# File 'lib/truffle/message.rb', line 19

def content
  @content
end

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'lib/truffle/message.rb', line 19

def name
  @name
end

#roleObject (readonly)

Returns the value of attribute role.



19
20
21
# File 'lib/truffle/message.rb', line 19

def role
  @role
end

#tool_call_idObject (readonly)

Returns the value of attribute tool_call_id.



19
20
21
# File 'lib/truffle/message.rb', line 19

def tool_call_id
  @tool_call_id
end

#tool_callsObject (readonly)

Returns the value of attribute tool_calls.



19
20
21
# File 'lib/truffle/message.rb', line 19

def tool_calls
  @tool_calls
end

Class Method Details

.assistant(content: nil, tool_calls: []) ⇒ Object



42
43
44
# File 'lib/truffle/message.rb', line 42

def self.assistant(content: nil, tool_calls: [])
  new(role: :assistant, content: content, tool_calls: tool_calls)
end

.system(content) ⇒ Object



34
35
36
# File 'lib/truffle/message.rb', line 34

def self.system(content)
  new(role: :system, content: content)
end

.tool(content:, tool_call_id:, name: nil) ⇒ Object

A tool result message, linked back to the assistant tool call by id.



47
48
49
# File 'lib/truffle/message.rb', line 47

def self.tool(content:, tool_call_id:, name: nil)
  new(role: :tool, content: content, tool_call_id: tool_call_id, name: name)
end

.user(content) ⇒ Object



38
39
40
# File 'lib/truffle/message.rb', line 38

def self.user(content)
  new(role: :user, content: content)
end

Instance Method Details

#to_hObject



55
56
57
58
59
60
61
62
63
# File 'lib/truffle/message.rb', line 55

def to_h
  {
    role: role,
    content: content,
    tool_calls: tool_calls.map(&:to_h),
    tool_call_id: tool_call_id,
    name: name
  }.compact
end

#tool_calls?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/truffle/message.rb', line 51

def tool_calls?
  !@tool_calls.empty?
end