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.
-
#prompt_mode ⇒ Object
readonly
Returns the value of attribute prompt_mode.
-
#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.
- #internal_model_call(model:, messages:, instructions:, tools: [], thinking: nil, output_schema: nil, metadata: {}, purpose:, client: nil) ⇒ Object
- #output_data ⇒ Object
- #output_text ⇒ Object
- #paint(prompt, model:, provider: nil, size: nil, assume_model_exists: nil, input_images: nil, mask: nil, params: {}, metadata: {}, client: nil) ⇒ Object
- #policy_audit ⇒ Object
- #preview ⇒ Object
- #reload ⇒ Object
- #run!(&block) ⇒ Object
- #stale! ⇒ Object
- #status ⇒ Object
- #tool_executions ⇒ Object
- #usage ⇒ Object
- #view_media(media, objective:, model:, provider: nil, output_schema: nil, params: {}, metadata: {}, client: nil) ⇒ 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 33 |
# 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 = @prompt_mode = @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 |
#prompt_mode ⇒ Object (readonly)
Returns the value of attribute prompt_mode.
9 10 11 |
# File 'lib/turnkit/turn.rb', line 9 def prompt_mode @prompt_mode 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
123 124 125 |
# File 'lib/turnkit/turn.rb', line 123 def cost Cost.from_record(@record) end |
#emit(type, payload = {}) ⇒ Object
144 145 146 |
# File 'lib/turnkit/turn.rb', line 144 def emit(type, payload = {}) emit_event(Event.new(type: type, turn_id: id, conversation_id: conversation.id, payload: payload)) end |
#internal_model_call(model:, messages:, instructions:, tools: [], thinking: nil, output_schema: nil, metadata: {}, purpose:, client: nil) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/turnkit/turn.rb', line 148 def internal_model_call(model:, messages:, instructions:, tools: [], thinking: nil, output_schema: nil, metadata: {}, purpose:, client: nil) request = ModelRequest.new( model: model, messages: , tools: tools, instructions: instructions, thinking: thinking, output_schema: output_schema, metadata: { purpose: purpose.to_s, turn_id: id, conversation_id: conversation.id }.merge( || {}) ) model_client = client || agent.effective_client model_client.validate!(model: request.model) emit_model_requested("#{purpose}.model.requested", request) result = call_client(request, client: model_client) result_cost = Cost.from_usage(result.usage, model: result.model || request.model) add_usage!(result.usage, cost: result_cost) emit_model_completed("#{purpose}.model.completed", result, result_cost, model: request.model) budget.add_cost!(result_cost.total) result end |
#output_data ⇒ Object
111 112 113 |
# File 'lib/turnkit/turn.rb', line 111 def output_data @record["output_data"] end |
#output_text ⇒ Object
107 108 109 |
# File 'lib/turnkit/turn.rb', line 107 def output_text @record["output_text"].to_s end |
#paint(prompt, model:, provider: nil, size: nil, assume_model_exists: nil, input_images: nil, mask: nil, params: {}, metadata: {}, client: nil) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/turnkit/turn.rb', line 170 def paint(prompt, model:, provider: nil, size: nil, assume_model_exists: nil, input_images: nil, mask: nil, params: {}, metadata: {}, client: nil) claimed_standalone = false case status when "pending" claimed = store.claim_turn(id, from: "pending", to: "running", started_at: Clock.now, heartbeat_at: Clock.now) raise Error, "turn is already running" unless claimed @record = claimed @started_at = @record["started_at"] @budget = Budget.resume(store: store, root_turn_id: root_turn_id, limits: budget_limits) claimed_standalone = true emit("turn.started", status: status, model: model) when "running" # Image tools call this while their parent turn is running. else raise Error, "cannot paint for #{status} turn" end image_client = client || agent.effective_client request = { prompt: prompt, model: model, provider: provider, size: size, assume_model_exists: assume_model_exists, input_images: input_images, mask: mask, params: params || {}, metadata: { turn_id: id, conversation_id: conversation.id }.merge( || {}) } image_client.validate!(model: model) emit("image.requested", request.except(:input_images, :mask)) result = call_image_client(image_client, request) result_cost = Cost.from_usage(result.usage, model: result.model || model) add_usage!(result.usage, cost: result_cost) budget.add_cost!(result_cost.total) image = result.images.first raise Error, "image client returned no image" unless image raise Error, "image client returned image without url or data" if image.url.to_s.empty? && image.data.to_s.empty? (image) emit("image.completed", image: image.to_h, model: image.model || model, provider: image.provider || provider&.to_s, mime_type: image.mime_type, usage: result.usage.to_h, cost: result_cost.to_h, metadata: || {}) complete_with_output(image.url.to_s, output_data: { "type" => "image", "images" => [ image.to_h ] }, audit: check_policy(image.url.to_s, output_data: { "type" => "image", "images" => [ image.to_h ] })) if claimed_standalone image rescue StandardError => error if claimed_standalone update!(status: "failed", error: { "class" => error.class.name, "message" => error. }, completed_at: Clock.now) emit("turn.failed", error: { "class" => error.class.name, "message" => error. }) end raise end |
#policy_audit ⇒ Object
115 116 117 |
# File 'lib/turnkit/turn.rb', line 115 def policy_audit (@record["options"] || {})["policy_audit"] end |
#preview ⇒ Object
95 96 97 |
# File 'lib/turnkit/turn.rb', line 95 def preview model_request end |
#reload ⇒ Object
131 132 133 134 135 136 137 138 |
# File 'lib/turnkit/turn.rb', line 131 def reload @record = store.load_turn(id) @thinking = @compact = @output_schema = @prompt_mode = self end |
#run!(&block) ⇒ Object
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/turnkit/turn.rb', line 35 def run!(&block) @on_event = block if block return self unless status == "pending" claimed = store.claim_turn(id, from: "pending", to: "running", started_at: Clock.now, heartbeat_at: Clock.now) return self unless claimed @record = claimed @started_at = @record["started_at"] emit("turn.started", status: status, model: model) agent.effective_client.validate!(model: model) @budget = Budget.resume(store: store, root_turn_id: root_turn_id, limits: budget_limits) revisions_used = 0 loop do budget.check!(depth: depth) count_iteration! TurnKit::Compaction.maybe_compact!(self) request = model_request emit_model_requested("model.requested", request) result = call_client(request) result_cost = Cost.from_usage(result.usage, model: result.model || model) add_usage!(result.usage, cost: result_cost) emit_model_completed("model.completed", result, result_cost, model: model) budget.add_cost!(result_cost.total) (result) if result.tool_calls? runner = ToolRunner.new(self) terminal = runner.dispatch(result.tool_calls) if terminal candidate = append_terminal_completion(runner, terminal) else next end else candidate = result.text end audit = check_policy(candidate, output_data: result.output_data) if should_revise?(audit, revisions_used) revisions_used += 1 (audit, attempt: revisions_used, terminal_tool_name: terminal&.tool_name) emit("output_policy.revision", violation_count: audit.violations.length, attempt: revisions_used) next end complete_with_output(candidate, output_data: result.output_data, audit: audit) break 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
140 141 142 |
# File 'lib/turnkit/turn.rb', line 140 def stale! update!(status: "stale", completed_at: Clock.now) end |
#status ⇒ Object
99 100 101 |
# File 'lib/turnkit/turn.rb', line 99 def status @record.fetch("status") end |
#tool_executions ⇒ Object
127 128 129 |
# File 'lib/turnkit/turn.rb', line 127 def tool_executions store.list_tool_executions(turn_id: id).map { |attrs| ToolExecution.new(attrs) } end |
#usage ⇒ Object
119 120 121 |
# File 'lib/turnkit/turn.rb', line 119 def usage Usage.from_h(@record["usage"] || {}) end |
#view_media(media, objective:, model:, provider: nil, output_schema: nil, params: {}, metadata: {}, client: nil) ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/turnkit/turn.rb', line 223 def view_media(media, objective:, model:, provider: nil, output_schema: nil, params: {}, metadata: {}, client: nil) claimed_standalone = false case status when "pending" claimed = store.claim_turn(id, from: "pending", to: "running", started_at: Clock.now, heartbeat_at: Clock.now) raise Error, "turn is already running" unless claimed @record = claimed @started_at = @record["started_at"] @budget = Budget.resume(store: store, root_turn_id: root_turn_id, limits: budget_limits) claimed_standalone = true emit("turn.started", status: status, model: model) when "running" # Media tools call this while their parent turn is running. else raise Error, "cannot view media for #{status} turn" end media_input = MediaInput.wrap(media) media_client = client || agent.effective_client request = { media: media_input, objective: objective, model: model, provider: provider, output_schema: output_schema, params: params || {}, metadata: { turn_id: id, conversation_id: conversation.id }.merge( || {}) } media_client.validate!(model: model) emit("media.requested", request.except(:media).merge(media: media_input.to_h)) result = call_media_client(media_client, request) result_cost = Cost.from_usage(result.usage, model: result.model || model) add_usage!(result.usage, cost: result_cost) budget.add_cost!(result_cost.total) analysis = result.media_analyses.first raise Error, "media client returned no media analysis" unless analysis (analysis) output_data = { "type" => "media_analysis", "media_analyses" => [ analysis.to_h ] } emit("media.completed", analysis: analysis.to_h, model: analysis.model || model, provider: analysis.provider || provider&.to_s, media: media_input.to_h, usage: result.usage.to_h, cost: result_cost.to_h, metadata: || {}) complete_with_output(analysis.text, output_data: output_data, audit: check_policy(analysis.text, output_data: output_data)) if claimed_standalone analysis rescue StandardError => error emit("media.failed", error: { "class" => error.class.name, "message" => error. }, metadata: || {}) if status == "running" || claimed_standalone if claimed_standalone update!(status: "failed", error: { "class" => error.class.name, "message" => error. }, completed_at: Clock.now) emit("turn.failed", error: { "class" => error.class.name, "message" => error. }) end raise end |