Class: TurnKit::Turn

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

Constant Summary collapse

STATUSES =
Record::TURN_STATUSES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent:, conversation:, record:, store:, budget: nil, depth: 0) ⇒ Turn

Returns a new instance of Turn.



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

def initialize(agent:, conversation:, record:, store:, budget: nil, depth: 0)
  @agent = agent
  @conversation = conversation
  @store = store
  @record = record.transform_keys(&:to_s)
  @id = @record.fetch("id")
  @conversation_id = @record.fetch("conversation_id")
  @agent_name = @record["agent_name"]
  @parent_turn_id = @record["parent_turn_id"]
  @parent_tool_execution_id = @record["parent_tool_execution_id"]
  @root_turn_id = @record["root_turn_id"] || id
  @context_message_sequence = @record["context_message_sequence"].to_i
  @model = @record["model"] || agent.effective_model
  @started_at = @record["started_at"]
  @budget = budget || agent.build_budget
  @depth = depth
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



7
8
9
# File 'lib/turnkit/turn.rb', line 7

def agent
  @agent
end

#agent_nameObject (readonly)

Returns the value of attribute agent_name.



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

def agent_name
  @agent_name
end

#budgetObject (readonly)

Returns the value of attribute budget.



7
8
9
# File 'lib/turnkit/turn.rb', line 7

def budget
  @budget
end

#context_message_sequenceObject (readonly)

Returns the value of attribute context_message_sequence.



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

def context_message_sequence
  @context_message_sequence
end

#conversationObject (readonly)

Returns the value of attribute conversation.



7
8
9
# File 'lib/turnkit/turn.rb', line 7

def conversation
  @conversation
end

#conversation_idObject (readonly)

Returns the value of attribute conversation_id.



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

def conversation_id
  @conversation_id
end

#depthObject (readonly)

Returns the value of attribute depth.



7
8
9
# File 'lib/turnkit/turn.rb', line 7

def depth
  @depth
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

#parent_tool_execution_idObject (readonly)

Returns the value of attribute parent_tool_execution_id.



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

def parent_tool_execution_id
  @parent_tool_execution_id
end

#parent_turn_idObject (readonly)

Returns the value of attribute parent_turn_id.



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

def parent_turn_id
  @parent_turn_id
end

#root_turn_idObject (readonly)

Returns the value of attribute root_turn_id.



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

def root_turn_id
  @root_turn_id
end

#started_atObject (readonly)

Returns the value of attribute started_at.



10
11
12
# File 'lib/turnkit/turn.rb', line 10

def started_at
  @started_at
end

#storeObject (readonly)

Returns the value of attribute store.



7
8
9
# File 'lib/turnkit/turn.rb', line 7

def store
  @store
end

Instance Method Details

#output_textObject



78
79
80
# File 'lib/turnkit/turn.rb', line 78

def output_text
  @record["output_text"].to_s
end

#reloadObject



86
87
88
89
# File 'lib/turnkit/turn.rb', line 86

def reload
  @record = store.load_turn(id)
  self
end

#run!Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/turnkit/turn.rb', line 30

def run!
  return self unless status == "pending"

  update!(status: "running", started_at: Clock.now, heartbeat_at: Clock.now)
  loop do
    budget.check!(depth: depth)
    budget.count_iteration!

    result = agent.effective_client.chat(
      model: model,
      messages: llm_messages,
      tools: agent.effective_tools,
      instructions: agent.system_prompt_for(turn: self, conversation: conversation),
      metadata: { turn_id: id, conversation_id: conversation.id }
    )

    budget.add_usage!(result.usage)
    add_usage!(result.usage)
    persist_assistant_message(result)

    if result.tool_calls?
      runner = ToolRunner.new(self)
      terminal = runner.dispatch(result.tool_calls)
      if terminal
        complete_from_terminal_tool(runner, terminal)
        break
      end
    else
      update!(status: "completed", output_text: result.text, completed_at: Clock.now)
      break
    end
  end
  reload
  self
rescue StandardError => error
  update!(status: "failed", error: { "class" => error.class.name, "message" => error.message }, completed_at: Clock.now)
  reload
  self
end

#stale!Object



91
92
93
# File 'lib/turnkit/turn.rb', line 91

def stale!
  update!(status: "stale", completed_at: Clock.now)
end

#statusObject



70
71
72
# File 'lib/turnkit/turn.rb', line 70

def status
  @record.fetch("status")
end

#tool_executionsObject



82
83
84
# File 'lib/turnkit/turn.rb', line 82

def tool_executions
  store.list_tool_executions(turn_id: id).map { |attrs| ToolExecution.new(attrs) }
end