Class: Lyman::Conversation

Inherits:
Object
  • Object
show all
Defined in:
lib/lyman/conversation.rb

Overview

The item that flows through the spine and the circuit: the whole conversation so far, plus the control data workers consult to decide whether to act (never which of several things to do).

Messages use string keys throughout, for clean round-tripping with OpenAI-compatible wire formats.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(system_prompt: nil, max_rounds: 10) ⇒ Conversation

Returns a new instance of Conversation.



12
13
14
15
16
17
18
# File 'lib/lyman/conversation.rb', line 12

def initialize(system_prompt: nil, max_rounds: 10)
  @messages = []
  @messages << {"role" => "system", "content" => system_prompt} if system_prompt
  @rounds = 0
  @max_rounds = max_rounds
  @finished = false
end

Instance Attribute Details

#max_roundsObject

Returns the value of attribute max_rounds.



10
11
12
# File 'lib/lyman/conversation.rb', line 10

def max_rounds
  @max_rounds
end

#messagesObject (readonly)

Returns the value of attribute messages.



9
10
11
# File 'lib/lyman/conversation.rb', line 9

def messages
  @messages
end

#roundsObject

Returns the value of attribute rounds.



10
11
12
# File 'lib/lyman/conversation.rb', line 10

def rounds
  @rounds
end

Instance Method Details

#add_assistant_message(message) ⇒ Object



27
28
29
30
# File 'lib/lyman/conversation.rb', line 27

def add_assistant_message(message)
  @messages << message
  self
end

#add_tool_result(tool_call_id, content) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/lyman/conversation.rb', line 32

def add_tool_result(tool_call_id, content)
  @messages << {
    "role" => "tool",
    "tool_call_id" => tool_call_id,
    "content" => content
  }
  self
end

#add_user_message(text) ⇒ Object



20
21
22
23
24
25
# File 'lib/lyman/conversation.rb', line 20

def add_user_message(text)
  @messages << {"role" => "user", "content" => text}
  @finished = false
  @rounds = 0
  self
end

#finish!Object



52
53
54
55
# File 'lib/lyman/conversation.rb', line 52

def finish!
  @finished = true
  self
end

#finished?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/lyman/conversation.rb', line 57

def finished?
  @finished
end

#last_assistant_contentObject



47
48
49
50
# File 'lib/lyman/conversation.rb', line 47

def last_assistant_content
  message = @messages.rfind { |m| m["role"] == "assistant" }
  message && message["content"]
end

#pending_tool_callsObject



41
42
43
44
45
# File 'lib/lyman/conversation.rb', line 41

def pending_tool_calls
  last = @messages.last
  return [] unless last && last["role"] == "assistant"
  last["tool_calls"] || []
end

#runaway?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/lyman/conversation.rb', line 61

def runaway?
  @rounds >= @max_rounds
end