Class: TurnKit::Message
- Inherits:
-
Object
- Object
- TurnKit::Message
- Defined in:
- lib/turnkit/message.rb
Constant Summary collapse
- ROLES =
%w[user assistant tool].freeze
- KINDS =
%w[text tool_call tool_result context_summary].freeze
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#conversation_id ⇒ Object
readonly
Returns the value of attribute conversation_id.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#kind ⇒ Object
readonly
Returns the value of attribute kind.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#provider_message_id ⇒ Object
readonly
Returns the value of attribute provider_message_id.
-
#role ⇒ Object
readonly
Returns the value of attribute role.
-
#sequence ⇒ Object
readonly
Returns the value of attribute sequence.
-
#tool_execution_id ⇒ Object
readonly
Returns the value of attribute tool_execution_id.
-
#turn_id ⇒ Object
readonly
Returns the value of attribute turn_id.
Instance Method Summary collapse
- #compaction_metadata ⇒ Object
- #context_summary? ⇒ Boolean
-
#initialize(attributes = {}) ⇒ Message
constructor
A new instance of Message.
- #text ⇒ Object
- #text? ⇒ Boolean
- #to_h ⇒ Object
- #tool_call? ⇒ Boolean
- #tool_result? ⇒ Boolean
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 |
# 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"].nil? ? attrs["text"] : attrs["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
#content ⇒ Object (readonly)
Returns the value of attribute content.
9 10 11 |
# File 'lib/turnkit/message.rb', line 9 def content @content end |
#conversation_id ⇒ Object (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_at ⇒ Object (readonly)
Returns the value of attribute created_at.
9 10 11 |
# File 'lib/turnkit/message.rb', line 9 def created_at @created_at end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
8 9 10 |
# File 'lib/turnkit/message.rb', line 8 def id @id end |
#kind ⇒ Object (readonly)
Returns the value of attribute kind.
8 9 10 |
# File 'lib/turnkit/message.rb', line 8 def kind @kind end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
9 10 11 |
# File 'lib/turnkit/message.rb', line 9 def @metadata end |
#provider_message_id ⇒ Object (readonly)
Returns the value of attribute provider_message_id.
9 10 11 |
# File 'lib/turnkit/message.rb', line 9 def @provider_message_id end |
#role ⇒ Object (readonly)
Returns the value of attribute role.
8 9 10 |
# File 'lib/turnkit/message.rb', line 8 def role @role end |
#sequence ⇒ Object (readonly)
Returns the value of attribute sequence.
8 9 10 |
# File 'lib/turnkit/message.rb', line 8 def sequence @sequence end |
#tool_execution_id ⇒ Object (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_id ⇒ Object (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
#compaction_metadata ⇒ Object
67 68 69 |
# File 'lib/turnkit/message.rb', line 67 def .fetch("compaction", {}) end |
#context_summary? ⇒ Boolean
56 57 58 |
# File 'lib/turnkit/message.rb', line 56 def context_summary? kind == "context_summary" end |
#text ⇒ Object
60 61 62 63 64 65 |
# File 'lib/turnkit/message.rb', line 60 def text content.filter_map do |part| attrs = stringify(part) attrs["text"] if attrs["type"] == "text" end.join("\n") end |
#text? ⇒ Boolean
44 45 46 |
# File 'lib/turnkit/message.rb', line 44 def text? kind == "text" end |
#to_h ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/turnkit/message.rb', line 28 def to_h { "id" => id, "conversation_id" => conversation_id, "turn_id" => turn_id, "role" => role, "kind" => kind, "sequence" => sequence, "content" => content, "tool_execution_id" => tool_execution_id, "provider_message_id" => , "metadata" => , "created_at" => created_at } end |
#tool_call? ⇒ Boolean
48 49 50 |
# File 'lib/turnkit/message.rb', line 48 def tool_call? kind == "tool_call" end |
#tool_result? ⇒ Boolean
52 53 54 |
# File 'lib/turnkit/message.rb', line 52 def tool_result? kind == "tool_result" end |