Class: TurnKit::Conversation
- Inherits:
-
Object
- Object
- TurnKit::Conversation
- Defined in:
- lib/turnkit/conversation.rb
Instance Attribute Summary collapse
-
#agent ⇒ Object
readonly
Returns the value of attribute agent.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#store ⇒ Object
readonly
Returns the value of attribute store.
-
#subject ⇒ Object
readonly
Returns the value of attribute subject.
Instance Method Summary collapse
- #append_message(role:, kind:, text: nil, content: nil, turn_id: nil, tool_execution_id: nil, metadata: {}) ⇒ Object
- #ask(text, async: false, **options) ⇒ Object
- #build_turn(trigger_message_id: nil, model: nil, budget: nil, parent_turn: nil, parent_tool_execution: nil, depth: 0, agent: self.agent) ⇒ Object
-
#initialize(agent:, record:, store:, model:, subject: nil, metadata: {}) ⇒ Conversation
constructor
A new instance of Conversation.
- #latest_message_sequence ⇒ Object
- #messages ⇒ Object
- #messages_for_turn(turn) ⇒ Object
- #run!(trigger_message_id: nil, model: nil, budget: nil, parent_turn: nil, parent_tool_execution: nil, depth: 0, agent: self.agent) ⇒ Object
- #say(text, metadata: {}) ⇒ Object
Constructor Details
#initialize(agent:, record:, store:, model:, subject: nil, metadata: {}) ⇒ Conversation
Returns a new instance of Conversation.
7 8 9 10 11 12 13 14 15 |
# File 'lib/turnkit/conversation.rb', line 7 def initialize(agent:, record:, store:, model:, subject: nil, metadata: {}) @agent = agent @record = record.transform_keys(&:to_s) @id = @record.fetch("id") @store = store @model = model @subject = subject @metadata = || {} end |
Instance Attribute Details
#agent ⇒ Object (readonly)
Returns the value of attribute agent.
5 6 7 |
# File 'lib/turnkit/conversation.rb', line 5 def agent @agent end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
5 6 7 |
# File 'lib/turnkit/conversation.rb', line 5 def id @id end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
5 6 7 |
# File 'lib/turnkit/conversation.rb', line 5 def @metadata end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
5 6 7 |
# File 'lib/turnkit/conversation.rb', line 5 def model @model end |
#store ⇒ Object (readonly)
Returns the value of attribute store.
5 6 7 |
# File 'lib/turnkit/conversation.rb', line 5 def store @store end |
#subject ⇒ Object (readonly)
Returns the value of attribute subject.
5 6 7 |
# File 'lib/turnkit/conversation.rb', line 5 def subject @subject end |
Instance Method Details
#append_message(role:, kind:, text: nil, content: nil, turn_id: nil, tool_execution_id: nil, metadata: {}) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/turnkit/conversation.rb', line 55 def (role:, kind:, text: nil, content: nil, turn_id: nil, tool_execution_id: nil, metadata: {}) attrs = store.( "conversation_id" => id, "turn_id" => turn_id, "role" => role, "kind" => kind, "text" => text, "content" => content, "tool_execution_id" => tool_execution_id, "metadata" => ) Message.new(attrs) end |
#ask(text, async: false, **options) ⇒ Object
21 22 23 24 25 |
# File 'lib/turnkit/conversation.rb', line 21 def ask(text, async: false, **) trigger = say(text) turn = build_turn(trigger_message_id: trigger.id, **) async ? turn : turn.run! end |
#build_turn(trigger_message_id: nil, model: nil, budget: nil, parent_turn: nil, parent_tool_execution: nil, depth: 0, agent: self.agent) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/turnkit/conversation.rb', line 31 def build_turn(trigger_message_id: nil, model: nil, budget: nil, parent_turn: nil, parent_tool_execution: nil, depth: 0, agent: self.agent) snapshot = record = store.create_turn( "conversation_id" => id, "agent_name" => agent.name, "parent_turn_id" => parent_turn&.id, "parent_tool_execution_id" => parent_tool_execution&.id, "root_turn_id" => parent_turn&.root_turn_id, "context_message_sequence" => snapshot, "status" => "pending", "model" => model || self.model || agent.effective_model, "options" => { "trigger_message_id" => }.compact ) Turn.new(agent: agent, conversation: self, record: record, store: store, budget: budget, depth: depth) end |
#latest_message_sequence ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/turnkit/conversation.rb', line 69 def if store.respond_to?(:latest_message_sequence) store.(id) else .map(&:sequence).max.to_i end end |
#messages ⇒ Object
47 48 49 |
# File 'lib/turnkit/conversation.rb', line 47 def store.(id).map { |attrs| Message.new(attrs) } end |
#messages_for_turn(turn) ⇒ Object
51 52 53 |
# File 'lib/turnkit/conversation.rb', line 51 def (turn) store.(id, through_sequence: turn., turn_id: turn.id).map { |attrs| Message.new(attrs) } end |
#run!(trigger_message_id: nil, model: nil, budget: nil, parent_turn: nil, parent_tool_execution: nil, depth: 0, agent: self.agent) ⇒ Object
27 28 29 |
# File 'lib/turnkit/conversation.rb', line 27 def run!(trigger_message_id: nil, model: nil, budget: nil, parent_turn: nil, parent_tool_execution: nil, depth: 0, agent: self.agent) build_turn(trigger_message_id: , model: model, budget: budget, parent_turn: parent_turn, parent_tool_execution: parent_tool_execution, depth: depth, agent: agent).run! end |
#say(text, metadata: {}) ⇒ Object
17 18 19 |
# File 'lib/turnkit/conversation.rb', line 17 def say(text, metadata: {}) (role: "user", kind: "text", text: text, metadata: ) end |