Class: Terret::Tools::Registry

Inherits:
Hames::Service
  • Object
show all
Defined in:
lib/terret/tools.rb

Overview

ctx.tools — scoped registry + guarded execution pipeline. Registration is an effect (unloading a plugin removes its tools). Execution runs the three-waterfall pipeline: pre_execute (validate / veto / rewrite) -> execute (a provider may replace execution wholesale) -> post_execute (truncate / redact).

Instance Method Summary collapse

Instance Method Details

#execute(call) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/terret/tools.rb', line 40

def execute(call)
  admitted = @ctx.waterfall("tools/pre_execute", call)
  return Result.new(id: call.id, content: nil, error: admitted.reason) if admitted.is_a?(Veto)

  result = @ctx.waterfall("tools/execute", admitted) do |c|
    d = fetch(c.name)
    begin
      Result.new(id: c.id, content: d.handler.call(**c.args), error: nil)
    rescue => e
      Result.new(id: c.id, content: nil, error: "#{e.class}: #{e.message}")
    end
  end
  @ctx.waterfall("tools/post_execute", result)
end

#fetch(name) ⇒ Object



38
# File 'lib/terret/tools.rb', line 38

def fetch(name) = @defs.fetch(name.to_s)

#register(name:, description:, params: {}, mutating: false, approval: :never, &handler) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/terret/tools.rb', line 26

def register(name:, description:, params: {}, mutating: false,
             approval: :never, &handler)
  d = Definition.new(name: name.to_s, description:, params:, handler:,
                     mutating:, approval:)
  @ctx.effect do
    @defs[d.name] = d
    -> { @defs.delete(d.name) }
  end
  d
end

#schemasObject



37
# File 'lib/terret/tools.rb', line 37

def schemas = @defs.values.map(&:schema)

#start(ctx) ⇒ Object



21
22
23
24
# File 'lib/terret/tools.rb', line 21

def start(ctx)
  @ctx = ctx
  @defs = {}
end