Class: Ask::Agent::Definition

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

Overview

Declarative agent definition backed by a file convention.

Subclass Definition inside an agent.rb file under agents/<name>/ or app/agents/<name>/. The directory name becomes the agent name. Instructions load automatically from a sibling instructions.md.

Examples:

agents/health_check/agent.rb

class HealthCheckAgent < Ask::Agent::Definition
  model "gpt-4o"
  tools :bash, :read, :grep
  schedule "every 5 minutes"
end

# agents/health_check/instructions.md is auto-loaded

Using from Ruby

agent = Ask::Agent.new("health_check")
agent.run("Check server health")

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.subclassesObject (readonly)

All subclasses that have been loaded, in definition order.



28
29
30
# File 'lib/ask/agent/definition.rb', line 28

def subclasses
  @subclasses
end

Class Method Details

._configHash

Returns the raw config hash for this definition.

Returns:

  • (Hash)

    the raw config hash for this definition



45
46
47
# File 'lib/ask/agent/definition.rb', line 45

def _config
  @_config ||= { tools: [] }
end

.inherited(subclass) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ask/agent/definition.rb', line 30

def inherited(subclass)
  @subclasses << subclass
  subclass.instance_variable_set(:@_config, { tools: [] })

  # Auto-detect the directory from the file where this class is defined
  # caller(1) is the file containing the `class ... < Definition` line
  path = caller(1)&.first&.sub(/:.*/, "")  # strip line number
  if path && File.basename(path) == "agent.rb"
    subclass._config[:dir] = File.dirname(path)
  end

  super
end

.instructions_contentObject

Contents of the instructions.md file, or nil.



87
88
89
90
# File 'lib/ask/agent/definition.rb', line 87

def instructions_content
  path = instructions_path
  path ? File.read(path) : nil
end

.instructions_pathObject

Path to instructions.md relative to this definition's directory. Returns nil if no instructions file exists.



78
79
80
81
82
83
84
# File 'lib/ask/agent/definition.rb', line 78

def instructions_path
  dir = _config[:dir]
  return nil unless dir

  path = File.join(dir, "instructions.md")
  File.exist?(path) ? path : nil
end

.model(value = :__no_value__) ⇒ Object

Set or get the model identifier.



50
51
52
53
54
55
56
# File 'lib/ask/agent/definition.rb', line 50

def model(value = :__no_value__)
  if value == :__no_value__
    _config[:model]
  else
    _config[:model] = value
  end
end

.schedule(value = :__no_value__) ⇒ Object

Set a cron or interval schedule for recurring runs.



68
69
70
71
72
73
74
# File 'lib/ask/agent/definition.rb', line 68

def schedule(value = :__no_value__)
  if value == :__no_value__
    _config[:schedule]
  else
    _config[:schedule] = value
  end
end

.tools(*values) ⇒ Object

Set tool symbols or classes.



59
60
61
62
63
64
65
# File 'lib/ask/agent/definition.rb', line 59

def tools(*values)
  if values.any?
    _config[:tools] = values
  else
    _config[:tools]
  end
end