Class: TurnKit::Workflow

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

Constant Summary collapse

DEFAULT_INSTRUCTIONS =
<<~TEXT.strip
  You are an autonomous task orchestrator. Navigate from the application
  request to a final output without asking the user follow-up questions.

  Use the available tools to gather context, inspect sources, take actions,
  persist outputs, and verify work. Use loaded skills as reusable workflow
  patterns. Iterate when work needs missing context, critique, revision, or
  verification.

  When multiple independent items need the same kind of fetch or read, and
  an available batch tool can handle them in one call, prefer the batch tool
  over repeated one-item tool calls.

  Stop when the task is complete, when the available context and tools are
  sufficient for the best possible answer, or when further iteration would
  not materially improve the result. Respect runtime, cost, and iteration
  limits.
TEXT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: "workflow", description: "", instructions: nil, tools: [], skills: [], available_skills: [], model: nil, client: nil, store: nil, prompt_mode: :task, thinking: nil, compaction: nil, on_event: nil, output_schema: nil, max_iterations: nil, timeout: nil, max_spend: nil, cost_limit: nil, max_depth: nil, max_tool_executions: nil, max_tool_executions_by_name: nil, output_audit: nil, output_audit_mode: nil, output_policy: nil, output_policy_mode: nil, output_policy_model: nil, output_policy_thinking: nil) ⇒ Workflow

Returns a new instance of Workflow.

Raises:

  • (ArgumentError)


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
# File 'lib/turnkit/workflow.rb', line 32

def initialize(name: "workflow", description: "", instructions: nil,
  tools: [], skills: [], available_skills: [], model: nil, client: nil,
  store: nil, prompt_mode: :task, thinking: nil, compaction: nil, on_event: nil,
  output_schema: nil, max_iterations: nil, timeout: nil, max_spend: nil,
  cost_limit: nil, max_depth: nil, max_tool_executions: nil, max_tool_executions_by_name: nil,
  output_audit: nil, output_audit_mode: nil, output_policy: nil, output_policy_mode: nil, output_policy_model: nil, output_policy_thinking: nil)

  @name = name.to_s
  @description = description.to_s
  @instructions = instructions || DEFAULT_INSTRUCTIONS
  @tools = Array(tools)
  @skills = Array(skills)
  @available_skills = Array(available_skills)
  @model = model
  @client = client
  @store = store
  @prompt_mode = prompt_mode
  @thinking = thinking
  @compaction = compaction
  @on_event = on_event
  @output_schema = output_schema
  @max_iterations = max_iterations
  @timeout = timeout
  @cost_limit = cost_limit || max_spend
  @max_depth = max_depth
  @max_tool_executions = max_tool_executions
  @max_tool_executions_by_name = max_tool_executions_by_name
  @output_audit = output_audit
  @output_audit_mode = output_audit_mode
  @output_policy = output_policy
  @output_policy_mode = output_policy_mode
  @output_policy_model = output_policy_model
  @output_policy_thinking = output_policy_thinking
  raise ArgumentError, "name is required" if @name.empty?
  @agent = build_agent
end

Instance Attribute Details

#available_skillsObject (readonly)

Returns the value of attribute available_skills.



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

def available_skills
  @available_skills
end

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#compactionObject (readonly)

Returns the value of attribute compaction.



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

def compaction
  @compaction
end

#cost_limitObject (readonly)

Returns the value of attribute cost_limit.



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

def cost_limit
  @cost_limit
end

#descriptionObject (readonly)

Returns the value of attribute description.



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

def description
  @description
end

#instructionsObject (readonly)

Returns the value of attribute instructions.



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

def instructions
  @instructions
end

#max_depthObject (readonly)

Returns the value of attribute max_depth.



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

def max_depth
  @max_depth
end

#max_iterationsObject (readonly)

Returns the value of attribute max_iterations.



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

def max_iterations
  @max_iterations
end

#max_tool_executionsObject (readonly)

Returns the value of attribute max_tool_executions.



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

def max_tool_executions
  @max_tool_executions
end

#max_tool_executions_by_nameObject (readonly)

Returns the value of attribute max_tool_executions_by_name.



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

def max_tool_executions_by_name
  @max_tool_executions_by_name
end

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#on_eventObject (readonly)

Returns the value of attribute on_event.



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

def on_event
  @on_event
end

#output_auditObject (readonly)

Returns the value of attribute output_audit.



11
12
13
# File 'lib/turnkit/workflow.rb', line 11

def output_audit
  @output_audit
end

#output_audit_modeObject (readonly)

Returns the value of attribute output_audit_mode.



11
12
13
# File 'lib/turnkit/workflow.rb', line 11

def output_audit_mode
  @output_audit_mode
end

#output_policyObject (readonly)

Returns the value of attribute output_policy.



11
12
13
# File 'lib/turnkit/workflow.rb', line 11

def output_policy
  @output_policy
end

#output_policy_modeObject (readonly)

Returns the value of attribute output_policy_mode.



11
12
13
# File 'lib/turnkit/workflow.rb', line 11

def output_policy_mode
  @output_policy_mode
end

#output_policy_modelObject (readonly)

Returns the value of attribute output_policy_model.



11
12
13
# File 'lib/turnkit/workflow.rb', line 11

def output_policy_model
  @output_policy_model
end

#output_policy_thinkingObject (readonly)

Returns the value of attribute output_policy_thinking.



11
12
13
# File 'lib/turnkit/workflow.rb', line 11

def output_policy_thinking
  @output_policy_thinking
end

#output_schemaObject (readonly)

Returns the value of attribute output_schema.



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

def output_schema
  @output_schema
end

#prompt_modeObject (readonly)

Returns the value of attribute prompt_mode.



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

def prompt_mode
  @prompt_mode
end

#skillsObject (readonly)

Returns the value of attribute skills.



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

def skills
  @skills
end

#storeObject (readonly)

Returns the value of attribute store.



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

def store
  @store
end

#thinkingObject (readonly)

Returns the value of attribute thinking.



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

def thinking
  @thinking
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



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

def timeout
  @timeout
end

#toolsObject (readonly)

Returns the value of attribute tools.



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

def tools
  @tools
end

Instance Method Details

#agent(**options) ⇒ Object



90
91
92
# File 'lib/turnkit/workflow.rb', line 90

def agent(**options)
  options.empty? ? @agent : build_agent(**options)
end

#max_spendObject



94
95
96
# File 'lib/turnkit/workflow.rb', line 94

def max_spend
  cost_limit
end

#run(prompt = nil, task: nil, input: nil, async: false, subject: nil, metadata: {}, max_spend: nil, cost_limit: nil, **options) ⇒ Object

Raises:

  • (ArgumentError)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/turnkit/workflow.rb', line 69

def run(prompt = nil, task: nil, input: nil, async: false, subject: nil, metadata: {},
  max_spend: nil, cost_limit: nil, **options)

  task = task || prompt
  raise ArgumentError, "task is required" if task.to_s.empty?

  runtime_agent = if options.empty? && cost_limit.nil? && max_spend.nil?
    @agent
  else
    build_agent(cost_limit: cost_limit || max_spend, **options)
  end

  runtime_agent.run(
    task,
    input: input,
    async: async,
    subject: subject,
    metadata: 
  )
end