Class: TurnKit::Message

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

Constant Summary collapse

ROLES =
%w[user assistant tool].freeze
KINDS =
%w[text tool_call tool_result].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Message

Returns a new instance of Message.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/turnkit/message.rb', line 11

def initialize(attributes = {})
  attrs = stringify(attributes)
  @id = attrs["id"] || Id.generate(:message)
  @conversation_id = attrs.fetch("conversation_id")
  @turn_id = attrs["turn_id"]
  @role = attrs.fetch("role").to_s
  @kind = attrs.fetch("kind", "text").to_s
  @sequence = attrs.fetch("sequence").to_i
  @content = normalize_content(attrs["content"] || attrs["text"])
  @text = attrs["text"] || extract_text(@content)
  @tool_execution_id = attrs["tool_execution_id"]
  @provider_message_id = attrs["provider_message_id"]
  @metadata = attrs["metadata"] || {}
  @created_at = attrs["created_at"] || Clock.now

  validate!
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



9
10
11
# File 'lib/turnkit/message.rb', line 9

def content
  @content
end

#conversation_idObject (readonly)

Returns the value of attribute conversation_id.



8
9
10
# File 'lib/turnkit/message.rb', line 8

def conversation_id
  @conversation_id
end

#created_atObject (readonly)

Returns the value of attribute created_at.



9
10
11
# File 'lib/turnkit/message.rb', line 9

def created_at
  @created_at
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/turnkit/message.rb', line 8

def id
  @id
end

#kindObject (readonly)

Returns the value of attribute kind.



8
9
10
# File 'lib/turnkit/message.rb', line 8

def kind
  @kind
end

#metadataObject (readonly)

Returns the value of attribute metadata.



9
10
11
# File 'lib/turnkit/message.rb', line 9

def 
  @metadata
end

#provider_message_idObject (readonly)

Returns the value of attribute provider_message_id.



9
10
11
# File 'lib/turnkit/message.rb', line 9

def provider_message_id
  @provider_message_id
end

#roleObject (readonly)

Returns the value of attribute role.



8
9
10
# File 'lib/turnkit/message.rb', line 8

def role
  @role
end

#sequenceObject (readonly)

Returns the value of attribute sequence.



8
9
10
# File 'lib/turnkit/message.rb', line 8

def sequence
  @sequence
end

#textObject (readonly)

Returns the value of attribute text.



9
10
11
# File 'lib/turnkit/message.rb', line 9

def text
  @text
end

#tool_execution_idObject (readonly)

Returns the value of attribute tool_execution_id.



9
10
11
# File 'lib/turnkit/message.rb', line 9

def tool_execution_id
  @tool_execution_id
end

#turn_idObject (readonly)

Returns the value of attribute turn_id.



8
9
10
# File 'lib/turnkit/message.rb', line 8

def turn_id
  @turn_id
end

Instance Method Details

#to_hObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/turnkit/message.rb', line 29

def to_h
  {
    "id" => id,
    "conversation_id" => conversation_id,
    "turn_id" => turn_id,
    "role" => role,
    "kind" => kind,
    "sequence" => sequence,
    "content" => content,
    "text" => text,
    "tool_execution_id" => tool_execution_id,
    "provider_message_id" => provider_message_id,
    "metadata" => ,
    "created_at" => created_at
  }
end