Class: Kernai::SkillContext

Inherits:
Object
  • Object
show all
Defined in:
lib/kernai/skill_context.rb

Overview

Passed as the second argument to a skill’s execute block. Exposes ‘credential(:key)` and `config(:key)` lookups that go through the resolvers configured on Kernai.config.

Validation of required credentials happens lazily at access time, not at skill registration, so a skill can be defined in a context where its credentials aren’t yet set (tests, partial scenarios). Missing required credentials raise Kernai::CredentialMissingError only when the skill actually tries to read them.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(skill, credential_resolver: nil, config_resolver: nil, run_context: nil) ⇒ SkillContext

Returns a new instance of SkillContext.



21
22
23
24
25
26
27
# File 'lib/kernai/skill_context.rb', line 21

def initialize(skill, credential_resolver: nil, config_resolver: nil, run_context: nil)
  @skill = skill
  @credential_resolver = credential_resolver
  @config_resolver = config_resolver
  @run_context = run_context
  @cache = {}
end

Instance Attribute Details

#run_contextObject (readonly)

‘run_context` is the per-run `Kernai::Context` (or a subclass) that initiated the skill invocation. Host applications subclass Context to carry domain references (current user, ticket being executed, request id, …) so skills can act on the broader state without reaching for thread-locals or globals. `nil` outside a Kernel.run.



19
20
21
# File 'lib/kernai/skill_context.rb', line 19

def run_context
  @run_context
end

Instance Method Details

#config(key) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/kernai/skill_context.rb', line 54

def config(key)
  key = key.to_sym
  spec = @skill.configs[key]
  unless spec
    raise ArgumentError,
          "Skill '#{@skill.name}' did not declare config '#{key}'. " \
          "Declared: #{@skill.configs.keys.inspect}"
  end

  cache_key = [:config, key]
  return @cache[cache_key] if @cache.key?(cache_key)

  resolver = @config_resolver || Kernai.config.config_resolver
  raw = resolver.resolve(@skill.name, key)
  value = raw.nil? ? spec[:default] : coerce(raw, spec[:type])
  @cache[cache_key] = value
end

#credential(key) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/kernai/skill_context.rb', line 29

def credential(key)
  key = key.to_sym
  spec = @skill.credentials[key]
  unless spec
    raise ArgumentError,
          "Skill '#{@skill.name}' did not declare credential '#{key}'. " \
          "Declared: #{@skill.credentials.keys.inspect}"
  end

  cache_key = [:credential, key]
  return @cache[cache_key] if @cache.key?(cache_key)

  resolver = @credential_resolver || Kernai.config.credential_resolver
  value = resolver.resolve(@skill.name, key)

  if value.nil? && spec[:required]
    raise Kernai::CredentialMissingError,
          "Missing required credential '#{key}' for skill '#{@skill.name}'. " \
          'Configure it via the host (e.g. kernai-shell `/skill-config`) or ' \
          "set KERNAI_SKILL_#{@skill.name.to_s.upcase}_#{key.to_s.upcase}."
  end

  @cache[cache_key] = value
end