Class: TurnKit::Agent

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, description: "", model: nil, instructions: "", tools: [], skills: [], sub_agents: [], client: nil, store: nil, max_iterations: nil, timeout: nil, cost_limit: nil, max_depth: nil, max_tool_executions: nil) ⇒ Agent

Returns a new instance of Agent.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/turnkit/agent.rb', line 8

def initialize(name:, description: "", model: nil, instructions: "", tools: [], skills: [], sub_agents: [], client: nil, store: nil,
  max_iterations: nil, timeout: nil, cost_limit: nil, max_depth: nil, max_tool_executions: nil)
  @name = name.to_s
  @description = description.to_s
  @model = model
  @instructions = instructions.to_s
  @tools = Array(tools)
  @skills = Array(skills)
  @sub_agents = Array(sub_agents)
  @client = client
  @store = store
  @max_iterations = max_iterations
  @timeout = timeout
  @cost_limit = cost_limit
  @max_depth = max_depth
  @max_tool_executions = max_tool_executions
  raise ArgumentError, "name is required" if @name.empty?
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



6
7
8
# File 'lib/turnkit/agent.rb', line 6

def client
  @client
end

#cost_limitObject (readonly)

Returns the value of attribute cost_limit.



6
7
8
# File 'lib/turnkit/agent.rb', line 6

def cost_limit
  @cost_limit
end

#descriptionObject (readonly)

Returns the value of attribute description.



5
6
7
# File 'lib/turnkit/agent.rb', line 5

def description
  @description
end

#instructionsObject (readonly)

Returns the value of attribute instructions.



5
6
7
# File 'lib/turnkit/agent.rb', line 5

def instructions
  @instructions
end

#max_depthObject (readonly)

Returns the value of attribute max_depth.



6
7
8
# File 'lib/turnkit/agent.rb', line 6

def max_depth
  @max_depth
end

#max_iterationsObject (readonly)

Returns the value of attribute max_iterations.



6
7
8
# File 'lib/turnkit/agent.rb', line 6

def max_iterations
  @max_iterations
end

#max_tool_executionsObject (readonly)

Returns the value of attribute max_tool_executions.



6
7
8
# File 'lib/turnkit/agent.rb', line 6

def max_tool_executions
  @max_tool_executions
end

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'lib/turnkit/agent.rb', line 5

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/turnkit/agent.rb', line 5

def name
  @name
end

#skillsObject (readonly)

Returns the value of attribute skills.



5
6
7
# File 'lib/turnkit/agent.rb', line 5

def skills
  @skills
end

#storeObject (readonly)

Returns the value of attribute store.



6
7
8
# File 'lib/turnkit/agent.rb', line 6

def store
  @store
end

#sub_agentsObject (readonly)

Returns the value of attribute sub_agents.



5
6
7
# File 'lib/turnkit/agent.rb', line 5

def sub_agents
  @sub_agents
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



6
7
8
# File 'lib/turnkit/agent.rb', line 6

def timeout
  @timeout
end

#toolsObject (readonly)

Returns the value of attribute tools.



5
6
7
# File 'lib/turnkit/agent.rb', line 5

def tools
  @tools
end

Instance Method Details

#build_budget(root_started_at: Clock.now) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/turnkit/agent.rb', line 54

def build_budget(root_started_at: Clock.now)
  Budget.new(
    max_iterations: max_iterations || TurnKit.max_iterations,
    timeout: timeout || TurnKit.timeout,
    max_depth: max_depth || TurnKit.max_depth,
    max_tool_executions: max_tool_executions || TurnKit.max_tool_executions,
    cost_limit: cost_limit || TurnKit.cost_limit,
    root_started_at: root_started_at
  )
end

#conversation(model: nil, subject: nil, metadata: {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/turnkit/agent.rb', line 27

def conversation(model: nil, subject: nil, metadata: {})
  store = effective_store
  record = store.create_conversation(
    "agent_name" => name,
    "model" => model || effective_model,
    "subject" => subject,
    "metadata" => 
  )
  Conversation.new(agent: self, record: record, store: store, model: model || effective_model, subject: subject, metadata: )
end

#effective_clientObject



42
43
44
# File 'lib/turnkit/agent.rb', line 42

def effective_client
  client || TurnKit.client
end

#effective_modelObject



38
39
40
# File 'lib/turnkit/agent.rb', line 38

def effective_model
  model || TurnKit.default_model
end

#effective_storeObject



46
47
48
# File 'lib/turnkit/agent.rb', line 46

def effective_store
  store || TurnKit.store
end

#effective_toolsObject



50
51
52
# File 'lib/turnkit/agent.rb', line 50

def effective_tools
  tools + sub_agents.map { |agent| SubAgentTool.for(agent) }
end

#instructions_with_skillsObject



65
66
67
68
69
70
71
# File 'lib/turnkit/agent.rb', line 65

def instructions_with_skills
  parts = [ instructions ]
  skills.each do |skill|
    parts << "## Skill: #{skill.name}\n\n#{skill.content}"
  end
  parts.reject(&:empty?).join("\n\n")
end