Class: Riffer::Skills::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/riffer/skills/context.rb

Overview

Skills context for an agent generation cycle.

Coordinates skill discovery, activation, and prompt rendering. Tracks activations with caching to avoid redundant backend reads.

Built by the agent at the start of generate/stream and passed to tools via context[:skills].

See Riffer::Skills::Backend, Riffer::Skills::Frontmatter.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backend:, skills:, adapter:) ⇒ Context

Creates a new skills context for a generation cycle.

backend

the skills backend for reading skill bodies.

skills

skill catalog indexed by name.

adapter

the adapter used to render skill content.

– : (backend: Riffer::Skills::Backend, skills: Hash[String, Riffer::Skills::Frontmatter], adapter: Riffer::Skills::Adapter) -> void



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

def initialize(backend:, skills:, adapter:)
  @backend = backend
  @skills = skills
  @adapter = adapter
  @activated = {} #: Hash[String, String]
end

Instance Attribute Details

#adapterObject (readonly)

The skill adapter used for this context.



18
19
20
# File 'lib/riffer/skills/context.rb', line 18

def adapter
  @adapter
end

#on_activate=(value) ⇒ Object (writeonly)

Optional callback invoked when a skill is first activated.



21
22
23
# File 'lib/riffer/skills/context.rb', line 21

def on_activate=(value)
  @on_activate = value
end

#skillsObject (readonly)

Skill catalog indexed by name.



15
16
17
# File 'lib/riffer/skills/context.rb', line 15

def skills
  @skills
end

Instance Method Details

#activate(name) ⇒ Object

Activates a skill by name. Returns the cached body on re-activation.

Raises Riffer::ArgumentError if the skill is not in the catalog.

– : (String) -> String



44
45
46
47
48
49
50
# File 'lib/riffer/skills/context.rb', line 44

def activate(name)
  raise Riffer::ArgumentError, "Unknown skill: '#{name}'" unless skills.key?(name)
  return @activated[name] if @activated.key?(name)
  @activated[name] = @backend.read_skill(name)
  @on_activate&.call(name)
  @activated[name]
end

#activated?(name) ⇒ Boolean

Returns whether a skill has been activated.

– : (String) -> bool

Returns:



56
57
58
# File 'lib/riffer/skills/context.rb', line 56

def activated?(name)
  @activated.key?(name)
end

#system_promptObject

Returns the complete skills section for the system prompt.

Includes the catalog and any pre-activated skill bodies.

– : () -> String



66
67
68
69
70
71
72
# File 'lib/riffer/skills/context.rb', line 66

def system_prompt
  available = available_skills
  parts = []
  parts << @adapter.render_catalog(available) unless available.empty?
  @activated.each_value { |body| parts << body }
  parts.join("\n\n")
end