Class: SignalWire::Skills::SkillManager

Inherits:
Object
  • Object
show all
Defined in:
lib/signalwire/skills/skill_manager.rb

Overview

Thread-safe lifecycle manager for loaded skill instances.

manager = SkillManager.new
manager.load('datetime', DateTimeSkill.new)
manager.get('datetime')  #=> <DateTimeSkill>
manager.unload('datetime')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent = nil) ⇒ SkillManager

Python parity: “SkillManager.__init__(self, agent)“ —SkillManager keeps a back-pointer to its agent so loaded skills can attach prompt sections / SWAIG tools directly. Ruby allows nil for standalone use (tests, registry tools).



31
32
33
34
35
36
# File 'lib/signalwire/skills/skill_manager.rb', line 31

def initialize(agent = nil)
  @agent  = agent
  @skills = {}   # instance_key => SkillBase instance
  @mutex  = Mutex.new
  @logger = ::SignalWire::Logging.logger('signalwire.skill_manager')
end

Instance Attribute Details

#agentObject (readonly)

Python parity:

  • “agent“ — owning AgentBase instance (or nil)

  • “logger“ — namespaced logger



25
26
27
# File 'lib/signalwire/skills/skill_manager.rb', line 25

def agent
  @agent
end

#loggerObject (readonly)

Python parity:

  • “agent“ — owning AgentBase instance (or nil)

  • “logger“ — namespaced logger



25
26
27
# File 'lib/signalwire/skills/skill_manager.rb', line 25

def logger
  @logger
end

Instance Method Details

#clearObject

Unload all skills.



89
90
91
92
93
94
# File 'lib/signalwire/skills/skill_manager.rb', line 89

def clear
  @mutex.synchronize do
    @skills.each_value(&:cleanup)
    @skills.clear
  end
end

#get(key) ⇒ SkillBase?

Retrieve a loaded skill.

Parameters:

  • key (String)

Returns:



69
70
71
# File 'lib/signalwire/skills/skill_manager.rb', line 69

def get(key)
  @mutex.synchronize { @skills[key] }
end

#load(key, skill) ⇒ SkillBase

Load a skill instance. Calls setup on the skill; raises if it fails.

Parameters:

  • key (String)

    the instance key

  • skill (SkillBase)

    the skill instance

Returns:



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/signalwire/skills/skill_manager.rb', line 42

def load(key, skill)
  @mutex.synchronize do
    raise ArgumentError, "Skill already loaded: #{key}" if @skills.key?(key)

    unless skill.setup
      raise "Skill setup failed for '#{key}'"
    end

    @skills[key] = skill
  end
  skill
end

#loaded?(key) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/signalwire/skills/skill_manager.rb', line 74

def loaded?(key)
  @mutex.synchronize { @skills.key?(key) }
end

#loaded_keysArray<String>

Returns:

  • (Array<String>)


79
80
81
# File 'lib/signalwire/skills/skill_manager.rb', line 79

def loaded_keys
  @mutex.synchronize { @skills.keys.dup }
end

#sizeInteger

Returns:

  • (Integer)


84
85
86
# File 'lib/signalwire/skills/skill_manager.rb', line 84

def size
  @mutex.synchronize { @skills.size }
end

#unload(key) ⇒ SkillBase?

Unload a skill by instance key. Calls cleanup on it.

Parameters:

  • key (String)

Returns:

  • (SkillBase, nil)

    the removed skill, or nil



58
59
60
61
62
63
64
# File 'lib/signalwire/skills/skill_manager.rb', line 58

def unload(key)
  @mutex.synchronize do
    skill = @skills.delete(key)
    skill&.cleanup
    skill
  end
end