Class: TurnKit::Turn
- Inherits:
-
Object
- Object
- TurnKit::Turn
- Defined in:
- lib/turnkit/turn.rb
Constant Summary collapse
- STATUSES =
Record::TURN_STATUSES
Instance Attribute Summary collapse
-
#agent ⇒ Object
readonly
Returns the value of attribute agent.
-
#agent_name ⇒ Object
readonly
Returns the value of attribute agent_name.
-
#budget ⇒ Object
readonly
Returns the value of attribute budget.
-
#compact ⇒ Object
readonly
Returns the value of attribute compact.
-
#context_message_sequence ⇒ Object
readonly
Returns the value of attribute context_message_sequence.
-
#conversation ⇒ Object
readonly
Returns the value of attribute conversation.
-
#conversation_id ⇒ Object
readonly
Returns the value of attribute conversation_id.
-
#depth ⇒ Object
readonly
Returns the value of attribute depth.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#output_schema ⇒ Object
readonly
Returns the value of attribute output_schema.
-
#parent_tool_execution_id ⇒ Object
readonly
Returns the value of attribute parent_tool_execution_id.
-
#parent_turn_id ⇒ Object
readonly
Returns the value of attribute parent_turn_id.
-
#root_turn_id ⇒ Object
readonly
Returns the value of attribute root_turn_id.
-
#started_at ⇒ Object
readonly
Returns the value of attribute started_at.
-
#store ⇒ Object
readonly
Returns the value of attribute store.
-
#thinking ⇒ Object
readonly
Returns the value of attribute thinking.
Instance Method Summary collapse
- #cost ⇒ Object
- #emit(type, payload = {}) ⇒ Object
-
#initialize(agent:, conversation:, record:, store:, budget: nil, depth: 0, on_event: nil) ⇒ Turn
constructor
A new instance of Turn.
- #output_data ⇒ Object
- #output_text ⇒ Object
- #preview ⇒ Object
- #reload ⇒ Object
- #run!(&block) ⇒ Object
- #stale! ⇒ Object
- #status ⇒ Object
- #tool_executions ⇒ Object
- #usage ⇒ Object
Constructor Details
#initialize(agent:, conversation:, record:, store:, budget: nil, depth: 0, on_event: nil) ⇒ Turn
Returns a new instance of Turn.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/turnkit/turn.rb', line 12 def initialize(agent:, conversation:, record:, store:, budget: nil, depth: 0, on_event: nil) @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 @thinking = @compact = @output_schema = @started_at = @record["started_at"] @budget = budget || agent.build_budget @depth = depth @on_event = on_event end |
Instance Attribute Details
#agent ⇒ Object (readonly)
Returns the value of attribute agent.
7 8 9 |
# File 'lib/turnkit/turn.rb', line 7 def agent @agent end |
#agent_name ⇒ Object (readonly)
Returns the value of attribute agent_name.
8 9 10 |
# File 'lib/turnkit/turn.rb', line 8 def agent_name @agent_name end |
#budget ⇒ Object (readonly)
Returns the value of attribute budget.
7 8 9 |
# File 'lib/turnkit/turn.rb', line 7 def budget @budget end |
#compact ⇒ Object (readonly)
Returns the value of attribute compact.
9 10 11 |
# File 'lib/turnkit/turn.rb', line 9 def compact @compact end |
#context_message_sequence ⇒ Object (readonly)
Returns the value of attribute context_message_sequence.
9 10 11 |
# File 'lib/turnkit/turn.rb', line 9 def @context_message_sequence end |
#conversation ⇒ Object (readonly)
Returns the value of attribute conversation.
7 8 9 |
# File 'lib/turnkit/turn.rb', line 7 def conversation @conversation end |
#conversation_id ⇒ Object (readonly)
Returns the value of attribute conversation_id.
8 9 10 |
# File 'lib/turnkit/turn.rb', line 8 def conversation_id @conversation_id end |
#depth ⇒ Object (readonly)
Returns the value of attribute depth.
7 8 9 |
# File 'lib/turnkit/turn.rb', line 7 def depth @depth end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
8 9 10 |
# File 'lib/turnkit/turn.rb', line 8 def id @id end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
9 10 11 |
# File 'lib/turnkit/turn.rb', line 9 def model @model end |
#output_schema ⇒ Object (readonly)
Returns the value of attribute output_schema.
9 10 11 |
# File 'lib/turnkit/turn.rb', line 9 def output_schema @output_schema end |
#parent_tool_execution_id ⇒ Object (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_id ⇒ Object (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_id ⇒ Object (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_at ⇒ Object (readonly)
Returns the value of attribute started_at.
10 11 12 |
# File 'lib/turnkit/turn.rb', line 10 def started_at @started_at end |
#store ⇒ Object (readonly)
Returns the value of attribute store.
7 8 9 |
# File 'lib/turnkit/turn.rb', line 7 def store @store end |
#thinking ⇒ Object (readonly)
Returns the value of attribute thinking.
9 10 11 |
# File 'lib/turnkit/turn.rb', line 9 def thinking @thinking end |
Instance Method Details
#cost ⇒ Object
102 103 104 |
# File 'lib/turnkit/turn.rb', line 102 def cost Cost.from_record(@record) end |
#emit(type, payload = {}) ⇒ Object
122 123 124 |
# File 'lib/turnkit/turn.rb', line 122 def emit(type, payload = {}) emit_event(Event.new(type: type, turn_id: id, conversation_id: conversation.id, payload: payload)) end |
#output_data ⇒ Object
94 95 96 |
# File 'lib/turnkit/turn.rb', line 94 def output_data @record["output_data"] end |
#output_text ⇒ Object
90 91 92 |
# File 'lib/turnkit/turn.rb', line 90 def output_text @record["output_text"].to_s end |
#preview ⇒ Object
78 79 80 |
# File 'lib/turnkit/turn.rb', line 78 def preview model_request end |
#reload ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/turnkit/turn.rb', line 110 def reload @record = store.load_turn(id) @thinking = @compact = @output_schema = self end |
#run!(&block) ⇒ Object
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 69 70 71 72 73 74 75 76 |
# File 'lib/turnkit/turn.rb', line 34 def run!(&block) @on_event = block if block return self unless status == "pending" update!(status: "running", started_at: Clock.now, heartbeat_at: Clock.now) emit("turn.started", status: status, model: model) agent.effective_client.validate!(model: model) loop do budget.check!(depth: depth) budget.count_iteration! TurnKit::Compaction.maybe_compact!(self) request = model_request emit("model.requested", model: request.model, tool_names: request.tool_names) result = call_client(request) emit("model.completed", model: result.model || model, tool_call_count: result.tool_calls.length) result_cost = Cost.from_usage(result.usage, model: result.model || model) budget.add_cost!(result_cost.total) add_usage!(result.usage, cost: result_cost) (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, output_data: result.output_data, completed_at: Clock.now) emit("turn.completed", status: status, output_text: result.text) break end end reload self rescue StandardError => error update!(status: "failed", error: { "class" => error.class.name, "message" => error. }, completed_at: Clock.now) emit("turn.failed", error: { "class" => error.class.name, "message" => error. }) reload self end |
#stale! ⇒ Object
118 119 120 |
# File 'lib/turnkit/turn.rb', line 118 def stale! update!(status: "stale", completed_at: Clock.now) end |
#status ⇒ Object
82 83 84 |
# File 'lib/turnkit/turn.rb', line 82 def status @record.fetch("status") end |
#tool_executions ⇒ Object
106 107 108 |
# File 'lib/turnkit/turn.rb', line 106 def tool_executions store.list_tool_executions(turn_id: id).map { |attrs| ToolExecution.new(attrs) } end |