Class: Kward::PluginRegistry
- Inherits:
-
Object
- Object
- Kward::PluginRegistry
show all
- Defined in:
- lib/kward/plugin_registry.rb
Overview
Loads trusted user plugin files and provides the plugin DSL.
Plugins live in the user plugin directory, run as local Ruby code, and can
register slash commands, one footer renderer, prompt context, and live
transcript-event observers for CLI and RPC frontends.
This registry is intentionally trust-based, not a sandbox. Keep plugin loading
restricted to ConfigFiles.plugin_paths, keep workspace-local code out of the
load path, and expose immutable transcript views so plugins can observe state
without corrupting active conversations.
Defined Under Namespace
Classes: Command, Context, DSL, HookHandler, InteractiveCommand, Transcript, TranscriptEvent
Constant Summary
collapse
- COMMAND_NAME_PATTERN =
/\A[A-Za-z0-9][A-Za-z0-9_-]*\z/.freeze
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#command_for(name) ⇒ Object
-
#commands ⇒ Object
-
#evaluate(path: nil, &block) ⇒ Object
-
#footer_renderer ⇒ Object
-
#hook_handlers ⇒ Object
-
#hook_manager ⇒ Object
-
#initialize(reserved_commands: []) ⇒ PluginRegistry
constructor
Creates an object for trusted plugin loading and dispatch.
-
#interactive_command_for(name) ⇒ Object
-
#interactive_commands ⇒ Object
-
#load_file(path) ⇒ Object
-
#notify_transcript_event(event, context) ⇒ Object
-
#prompt_context(context) ⇒ Object
-
#prompt_context_renderers ⇒ Object
-
#register_command(name, description: "", argument_hint: "", path: nil, &handler) ⇒ Object
-
#register_footer(path: nil, &renderer) ⇒ Object
-
#register_hook(event, id: nil, description: "", order: 100, match: nil, failure_policy: nil, path: nil, &handler) ⇒ Object
-
#register_interactive_command(name, rows:, fps: 30, description: "", argument_hint: "", path: nil, &handler) ⇒ Object
-
#register_prompt_context(path: nil, &renderer) ⇒ Object
-
#register_transcript_event(path: nil, &handler) ⇒ Object
-
#transcript_event_handlers ⇒ Object
Constructor Details
#initialize(reserved_commands: []) ⇒ PluginRegistry
Creates an object for trusted plugin loading and dispatch.
263
264
265
266
267
268
269
270
271
272
273
|
# File 'lib/kward/plugin_registry.rb', line 263
def initialize(reserved_commands: [])
@reserved_commands = reserved_commands.map(&:to_s)
@commands = {}
@interactive_commands = {}
@footer = nil
@footer_path = nil
@transcript_event_handlers = []
@prompt_context_renderers = []
@hook_handlers = []
@paths = []
end
|
Class Attribute Details
.loading_path ⇒ Object
Returns the value of attribute loading_path.
253
254
255
|
# File 'lib/kward/plugin_registry.rb', line 253
def loading_path
@loading_path
end
|
.loading_registry ⇒ Object
Returns the value of attribute loading_registry.
253
254
255
|
# File 'lib/kward/plugin_registry.rb', line 253
def loading_registry
@loading_registry
end
|
Instance Attribute Details
Returns plugin file currently responsible for footer output.
276
277
278
|
# File 'lib/kward/plugin_registry.rb', line 276
def
@footer_path
end
|
#paths ⇒ Array<String>
Returns plugin files successfully loaded by this registry.
279
280
281
|
# File 'lib/kward/plugin_registry.rb', line 279
def paths
@paths
end
|
Class Method Details
.load(paths: ConfigFiles.plugin_paths, reserved_commands: []) ⇒ Object
255
256
257
258
259
|
# File 'lib/kward/plugin_registry.rb', line 255
def load(paths: ConfigFiles.plugin_paths, reserved_commands: [])
registry = new(reserved_commands: reserved_commands)
paths.each { |path| registry.load_file(path) }
registry
end
|
Instance Method Details
#command_for(name) ⇒ Object
285
286
287
|
# File 'lib/kward/plugin_registry.rb', line 285
def command_for(name)
@commands[name.to_s]
end
|
#commands ⇒ Object
281
282
283
|
# File 'lib/kward/plugin_registry.rb', line 281
def commands
@commands.values
end
|
#evaluate(path: nil, &block) ⇒ Object
360
361
362
363
364
|
# File 'lib/kward/plugin_registry.rb', line 360
def evaluate(path: nil, &block)
dsl = DSL.new(self, path)
block.arity == 1 ? block.call(dsl) : dsl.instance_eval(&block)
self
end
|
297
298
299
|
# File 'lib/kward/plugin_registry.rb', line 297
def
@footer
end
|
#hook_handlers ⇒ Object
309
310
311
|
# File 'lib/kward/plugin_registry.rb', line 309
def hook_handlers
@hook_handlers.dup
end
|
#hook_manager ⇒ Object
313
314
315
316
317
318
319
320
321
|
# File 'lib/kward/plugin_registry.rb', line 313
def hook_manager
manager = Hooks::Manager.new
@hook_handlers.each do |hook|
manager.register(hook.event, id: hook.id, source: hook.path, order: hook.order, match: hook.match, failure_policy: hook.failure_policy) do |event, context|
hook.handler.call(event, context)
end
end
manager
end
|
#interactive_command_for(name) ⇒ Object
293
294
295
|
# File 'lib/kward/plugin_registry.rb', line 293
def interactive_command_for(name)
@interactive_commands[name.to_s]
end
|
#interactive_commands ⇒ Object
289
290
291
|
# File 'lib/kward/plugin_registry.rb', line 289
def interactive_commands
@interactive_commands.values
end
|
#load_file(path) ⇒ Object
346
347
348
349
350
351
352
353
354
355
356
357
358
|
# File 'lib/kward/plugin_registry.rb', line 346
def load_file(path)
previous_registry = self.class.loading_registry
previous_path = self.class.loading_path
self.class.loading_registry = self
self.class.loading_path = path
Kernel.load(path)
@paths << path
rescue StandardError => e
warn "Warning: skipping Kward plugin #{path}: #{e.message}"
ensure
self.class.loading_registry = previous_registry
self.class.loading_path = previous_path
end
|
#notify_transcript_event(event, context) ⇒ Object
334
335
336
337
338
339
340
341
342
343
344
|
# File 'lib/kward/plugin_registry.rb', line 334
def notify_transcript_event(event, context)
transcript_event = transcript_event_for(event)
return unless transcript_event
@transcript_event_handlers.each do |entry|
entry[:handler].call(transcript_event, context)
rescue StandardError => e
warn "Warning: Kward plugin transcript event error in #{entry[:path]}: #{e.message}"
end
nil
end
|
#prompt_context(context) ⇒ Object
323
324
325
326
327
328
329
330
331
332
|
# File 'lib/kward/plugin_registry.rb', line 323
def prompt_context(context)
parts = []
@prompt_context_renderers.each do |entry|
rendered = entry[:renderer].call(context)
parts << rendered.to_s unless rendered.to_s.empty?
rescue StandardError => e
warn "Warning: Kward plugin prompt context error in #{entry[:path]}: #{e.message}"
end
parts.empty? ? nil : parts.join("\n\n")
end
|
#prompt_context_renderers ⇒ Object
305
306
307
|
# File 'lib/kward/plugin_registry.rb', line 305
def prompt_context_renderers
@prompt_context_renderers.map { |entry| entry[:renderer] }
end
|
#register_command(name, description: "", argument_hint: "", path: nil, &handler) ⇒ Object
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
# File 'lib/kward/plugin_registry.rb', line 366
def register_command(name, description: "", argument_hint: "", path: nil, &handler)
name = name.to_s
raise "Plugin command name is invalid: #{name}" unless name.match?(COMMAND_NAME_PATTERN)
raise "Plugin command /#{name} requires a handler" unless handler
if @reserved_commands.include?(name)
warn "Warning: skipping Kward plugin command /#{name}: reserved command"
return nil
end
if @commands.key?(name)
warn "Warning: skipping duplicate Kward plugin command /#{name}: #{path}"
return nil
end
@commands[name] = Command.new(
name: name,
description: description.to_s,
argument_hint: argument_hint.to_s,
path: path,
handler: handler
)
end
|
414
415
416
417
418
419
420
|
# File 'lib/kward/plugin_registry.rb', line 414
def (path: nil, &renderer)
raise "Plugin footer requires a renderer" unless renderer
warn "Warning: replacing Kward plugin footer from #{@footer_path}: #{path}" if @footer
@footer = renderer
@footer_path = path
end
|
#register_hook(event, id: nil, description: "", order: 100, match: nil, failure_policy: nil, path: nil, &handler) ⇒ Object
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
# File 'lib/kward/plugin_registry.rb', line 434
def register_hook(event, id: nil, description: "", order: 100, match: nil, failure_policy: nil, path: nil, &handler)
event = event.to_s
raise "Plugin hook event is required" if event.empty?
raise "Plugin hook #{event} requires a handler" unless handler
@hook_handlers << HookHandler.new(
event: event,
id: id&.to_s || "#{File.basename(path.to_s.empty? ? "plugin" : path)}:#{event}:#{@hook_handlers.length + 1}",
description: description.to_s,
path: path,
order: order.to_i,
match: match,
failure_policy: failure_policy,
handler: handler
)
end
|
#register_interactive_command(name, rows:, fps: 30, description: "", argument_hint: "", path: nil, &handler) ⇒ Object
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
|
# File 'lib/kward/plugin_registry.rb', line 389
def register_interactive_command(name, rows:, fps: 30, description: "", argument_hint: "", path: nil, &handler)
name = name.to_s
raise "Interactive command name is invalid: #{name}" unless name.match?(COMMAND_NAME_PATTERN)
raise "Interactive command /#{name} requires a handler" unless handler
if @reserved_commands.include?(name) || @commands.key?(name)
warn "Warning: skipping Kward interactive command /#{name}: reserved command"
return nil
end
if @interactive_commands.key?(name)
warn "Warning: skipping duplicate Kward interactive command /#{name}: #{path}"
return nil
end
@interactive_commands[name] = InteractiveCommand.new(
name: name,
description: description.to_s,
argument_hint: argument_hint.to_s,
rows: [[rows.to_i, 1].max, 1].max,
fps: [[fps.to_f, 1].max, 120].min,
path: path,
handler: handler
)
end
|
#register_prompt_context(path: nil, &renderer) ⇒ Object
428
429
430
431
432
|
# File 'lib/kward/plugin_registry.rb', line 428
def register_prompt_context(path: nil, &renderer)
raise "Plugin prompt context requires a renderer" unless renderer
@prompt_context_renderers << { path: path, renderer: renderer }
end
|
#register_transcript_event(path: nil, &handler) ⇒ Object
422
423
424
425
426
|
# File 'lib/kward/plugin_registry.rb', line 422
def register_transcript_event(path: nil, &handler)
raise "Plugin transcript event requires a handler" unless handler
@transcript_event_handlers << { path: path, handler: handler }
end
|
#transcript_event_handlers ⇒ Object
301
302
303
|
# File 'lib/kward/plugin_registry.rb', line 301
def transcript_event_handlers
@transcript_event_handlers.map { |entry| entry[:handler] }
end
|