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, 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
Constructor Details
#initialize(reserved_commands: []) ⇒ PluginRegistry
Creates an object for trusted plugin loading and dispatch.
207
208
209
210
211
212
213
214
215
|
# File 'lib/kward/plugin_registry.rb', line 207
def initialize(reserved_commands: [])
@reserved_commands = reserved_commands.map(&:to_s)
@commands = {}
@footer = nil
@footer_path = nil
@transcript_event_handlers = []
@prompt_context_renderers = []
@paths = []
end
|
Class Attribute Details
.loading_path ⇒ Object
Returns the value of attribute loading_path.
174
175
176
|
# File 'lib/kward/plugin_registry.rb', line 174
def loading_path
@loading_path
end
|
.loading_registry ⇒ Object
Returns the value of attribute loading_registry.
174
175
176
|
# File 'lib/kward/plugin_registry.rb', line 174
def loading_registry
@loading_registry
end
|
Instance Attribute Details
Returns plugin file currently responsible for footer output.
218
219
220
|
# File 'lib/kward/plugin_registry.rb', line 218
def
@footer_path
end
|
#paths ⇒ Array<String>
Returns plugin files successfully loaded by this registry.
221
222
223
|
# File 'lib/kward/plugin_registry.rb', line 221
def paths
@paths
end
|
Class Method Details
.deep_dup(value) ⇒ Object
182
183
184
185
186
187
188
189
190
191
192
193
|
# File 'lib/kward/plugin_registry.rb', line 182
def deep_dup(value)
case value
when Hash
value.each_with_object({}) { |(key, item), result| result[key] = deep_dup(item) }
when Array
value.map { |item| deep_dup(item) }
else
value.dup
end
rescue TypeError
value
end
|
.deep_freeze(value) ⇒ Object
195
196
197
198
199
200
201
202
203
|
# File 'lib/kward/plugin_registry.rb', line 195
def deep_freeze(value)
case value
when Hash
value.each_value { |item| deep_freeze(item) }
when Array
value.each { |item| deep_freeze(item) }
end
value.freeze
end
|
.load(paths: ConfigFiles.plugin_paths, reserved_commands: []) ⇒ Object
176
177
178
179
180
|
# File 'lib/kward/plugin_registry.rb', line 176
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
227
228
229
|
# File 'lib/kward/plugin_registry.rb', line 227
def command_for(name)
@commands[name.to_s]
end
|
#commands ⇒ Object
223
224
225
|
# File 'lib/kward/plugin_registry.rb', line 223
def commands
@commands.values
end
|
#evaluate(path: nil, &block) ⇒ Object
280
281
282
283
284
|
# File 'lib/kward/plugin_registry.rb', line 280
def evaluate(path: nil, &block)
dsl = DSL.new(self, path)
block.arity == 1 ? block.call(dsl) : dsl.instance_eval(&block)
self
end
|
231
232
233
|
# File 'lib/kward/plugin_registry.rb', line 231
def
@footer
end
|
#load_file(path) ⇒ Object
266
267
268
269
270
271
272
273
274
275
276
277
278
|
# File 'lib/kward/plugin_registry.rb', line 266
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
254
255
256
257
258
259
260
261
262
263
264
|
# File 'lib/kward/plugin_registry.rb', line 254
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
243
244
245
246
247
248
249
250
251
252
|
# File 'lib/kward/plugin_registry.rb', line 243
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
239
240
241
|
# File 'lib/kward/plugin_registry.rb', line 239
def prompt_context_renderers
@prompt_context_renderers.map { |entry| entry[:renderer] }
end
|
#register_command(name, description: "", argument_hint: "", path: nil, &handler) ⇒ Object
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
# File 'lib/kward/plugin_registry.rb', line 286
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
|
309
310
311
312
313
314
315
|
# File 'lib/kward/plugin_registry.rb', line 309
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_prompt_context(path: nil, &renderer) ⇒ Object
323
324
325
326
327
|
# File 'lib/kward/plugin_registry.rb', line 323
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
317
318
319
320
321
|
# File 'lib/kward/plugin_registry.rb', line 317
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
235
236
237
|
# File 'lib/kward/plugin_registry.rb', line 235
def transcript_event_handlers
@transcript_event_handlers.map { |entry| entry[:handler] }
end
|