Class: Kward::PluginRegistry::Context
- Inherits:
-
Object
- Object
- Kward::PluginRegistry::Context
- Defined in:
- lib/kward/plugins/registry.rb
Overview
Runtime context passed to plugin commands, tools, footers, prompt context renderers, hooks, and transcript event handlers.
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#cancellation ⇒ Object
readonly
Returns the value of attribute cancellation.
-
#requested_turn ⇒ Object
readonly
Returns the value of attribute requested_turn.
-
#ui ⇒ Object
readonly
Returns the value of attribute ui.
-
#workspace_root ⇒ Object
readonly
Returns the value of attribute workspace_root.
Instance Method Summary collapse
-
#allow(message = nil, metadata: nil) ⇒ Hooks::Decision
Allows the current lifecycle event to continue.
-
#ask(message = nil, metadata: nil) ⇒ Hooks::Decision
Requests frontend approval for the current lifecycle event.
-
#cancelled? ⇒ Boolean
Returns whether the active plugin operation has been cancelled.
-
#defer(message = nil, payload: nil, metadata: nil) ⇒ Hooks::Decision
Defers the event when the current workflow supports it.
-
#deny(message = nil, metadata: nil) ⇒ Hooks::Decision
Denies the current lifecycle event.
-
#for_tool(conversation:, cancellation: nil) ⇒ Object
private
Builds a fresh context for one model-callable plugin tool invocation.
-
#initialize(conversation:, args: "", session: nil, workspace_root: Dir.pwd, say_callback: nil, cancellation: nil, ui: nil, tool_ui: nil, turn_command: nil) ⇒ Context
constructor
Creates an object for trusted plugin loading and dispatch.
-
#modify(payload, message: nil, metadata: nil) ⇒ Hooks::Decision
Continues with an event-specific payload replacement.
-
#refresh_system_message! ⇒ nil
Requests that the conversation rebuild its system message after plugin state changes that affect prompt context.
-
#request_turn(system:) ⇒ nil
Stages one turn for dispatch after the command successfully returns.
-
#result(message: nil, data: nil) ⇒ PluginResult
Builds a structured result for a typed command or plugin action.
-
#retry(message = nil, payload: nil, metadata: nil) ⇒ Hooks::Decision
Requests a retry when the current event supports it.
-
#say(message) ⇒ nil
Emits command output to the active frontend when available.
-
#session_id ⇒ String?
Active session identifier.
-
#session_name ⇒ String?
Human-readable active session name.
-
#session_path ⇒ String?
Saved active session path.
-
#transcript ⇒ Transcript
Read-only transcript wrapper.
-
#turn_requests_supported? ⇒ Boolean
Whether this command runs on a host that can execute a session turn.
-
#warn(message = nil, metadata: nil) ⇒ Hooks::Decision
Allows the event while recording a warning.
Constructor Details
#initialize(conversation:, args: "", session: nil, workspace_root: Dir.pwd, say_callback: nil, cancellation: nil, ui: nil, tool_ui: nil, turn_command: nil) ⇒ Context
Creates an object for trusted plugin loading and dispatch.
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/kward/plugins/registry.rb', line 103 def initialize(conversation:, args: "", session: nil, workspace_root: Dir.pwd, say_callback: nil, cancellation: nil, ui: nil, tool_ui: nil, turn_command: nil) @conversation = conversation @args = args.is_a?(Hash) ? DeepCopy.freeze(DeepCopy.dup(args)) : args.to_s @session = session @workspace_root = workspace_root @say_callback = say_callback @cancellation = cancellation @ui = (ui || PluginUI.new(say_callback: say_callback)).with_cancellation(cancellation) @tool_ui = tool_ui @turn_command = turn_command end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
100 101 102 |
# File 'lib/kward/plugins/registry.rb', line 100 def args @args end |
#cancellation ⇒ Object (readonly)
Returns the value of attribute cancellation.
100 101 102 |
# File 'lib/kward/plugins/registry.rb', line 100 def cancellation @cancellation end |
#requested_turn ⇒ Object (readonly)
Returns the value of attribute requested_turn.
100 101 102 |
# File 'lib/kward/plugins/registry.rb', line 100 def requested_turn @requested_turn end |
#ui ⇒ Object (readonly)
Returns the value of attribute ui.
100 101 102 |
# File 'lib/kward/plugins/registry.rb', line 100 def ui @ui end |
#workspace_root ⇒ Object (readonly)
Returns the value of attribute workspace_root.
100 101 102 |
# File 'lib/kward/plugins/registry.rb', line 100 def workspace_root @workspace_root end |
Instance Method Details
#allow(message = nil, metadata: nil) ⇒ Hooks::Decision
Allows the current lifecycle event to continue.
200 201 202 |
# File 'lib/kward/plugins/registry.rb', line 200 def allow( = nil, metadata: nil) Hooks::Decision.allow(, metadata: ) end |
#ask(message = nil, metadata: nil) ⇒ Hooks::Decision
Requests frontend approval for the current lifecycle event.
212 213 214 |
# File 'lib/kward/plugins/registry.rb', line 212 def ask( = nil, metadata: nil) Hooks::Decision.ask(, metadata: ) end |
#cancelled? ⇒ Boolean
Returns whether the active plugin operation has been cancelled. Contexts without a cancellable operation return false.
181 182 183 |
# File 'lib/kward/plugins/registry.rb', line 181 def cancelled? @cancellation&.cancelled? == true end |
#defer(message = nil, payload: nil, metadata: nil) ⇒ Hooks::Decision
Defers the event when the current workflow supports it.
237 238 239 |
# File 'lib/kward/plugins/registry.rb', line 237 def defer( = nil, payload: nil, metadata: nil) Hooks::Decision.defer(, payload: payload, metadata: ) end |
#deny(message = nil, metadata: nil) ⇒ Hooks::Decision
Denies the current lifecycle event.
206 207 208 |
# File 'lib/kward/plugins/registry.rb', line 206 def deny( = nil, metadata: nil) Hooks::Decision.deny(, metadata: ) end |
#for_tool(conversation:, cancellation: nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Builds a fresh context for one model-callable plugin tool invocation.
187 188 189 190 191 192 193 194 195 196 |
# File 'lib/kward/plugins/registry.rb', line 187 def for_tool(conversation:, cancellation: nil) self.class.new( conversation: conversation, session: @session, workspace_root: @workspace_root, say_callback: @say_callback, cancellation: cancellation, ui: @tool_ui || @ui ) end |
#modify(payload, message: nil, metadata: nil) ⇒ Hooks::Decision
Continues with an event-specific payload replacement.
219 220 221 |
# File 'lib/kward/plugins/registry.rb', line 219 def modify(payload, message: nil, metadata: nil) Hooks::Decision.modify(payload, message: , metadata: ) end |
#refresh_system_message! ⇒ nil
Requests that the conversation rebuild its system message after plugin state changes that affect prompt context.
165 166 167 168 |
# File 'lib/kward/plugins/registry.rb', line 165 def @conversation. if @conversation.respond_to?(:refresh_system_message!) nil end |
#request_turn(system:) ⇒ nil
Stages one turn for dispatch after the command successfully returns. Available only to asynchronous, session-backed plugin commands.
123 124 125 126 127 128 129 130 |
# File 'lib/kward/plugins/registry.rb', line 123 def request_turn(system:) raise ArgumentError, "Model turn requests are unavailable here; use a session command through the TUI or RPC turns/start" unless turn_requests_supported? @cancellation&.raise_if_cancelled! raise ArgumentError, "Only one model turn may be requested per command" if @requested_turn @requested_turn = PluginTurnRequest.new(system: system, command: @turn_command.name, plugin_id: @turn_command.plugin_id) nil end |
#result(message: nil, data: nil) ⇒ PluginResult
Builds a structured result for a typed command or plugin action.
175 176 177 |
# File 'lib/kward/plugins/registry.rb', line 175 def result(message: nil, data: nil) PluginResult.new(message: , data: data) end |
#retry(message = nil, payload: nil, metadata: nil) ⇒ Hooks::Decision
Requests a retry when the current event supports it.
231 232 233 |
# File 'lib/kward/plugins/registry.rb', line 231 def retry( = nil, payload: nil, metadata: nil) Hooks::Decision.retry(, payload: payload, metadata: ) end |
#say(message) ⇒ nil
Emits command output to the active frontend when available.
141 142 143 144 |
# File 'lib/kward/plugins/registry.rb', line 141 def say() @say_callback&.call(.to_s) nil end |
#session_id ⇒ String?
Returns active session identifier.
147 148 149 |
# File 'lib/kward/plugins/registry.rb', line 147 def session_id @session&.id end |
#session_name ⇒ String?
Returns human-readable active session name.
152 153 154 |
# File 'lib/kward/plugins/registry.rb', line 152 def session_name @session&.name end |
#session_path ⇒ String?
Returns saved active session path.
157 158 159 |
# File 'lib/kward/plugins/registry.rb', line 157 def session_path @session&.path end |
#transcript ⇒ Transcript
Returns read-only transcript wrapper.
133 134 135 |
# File 'lib/kward/plugins/registry.rb', line 133 def transcript Transcript.new(@conversation) end |
#turn_requests_supported? ⇒ Boolean
Whether this command runs on a host that can execute a session turn.
116 117 118 |
# File 'lib/kward/plugins/registry.rb', line 116 def turn_requests_supported? !@turn_command.nil? end |
#warn(message = nil, metadata: nil) ⇒ Hooks::Decision
Allows the event while recording a warning.
225 226 227 |
# File 'lib/kward/plugins/registry.rb', line 225 def warn( = nil, metadata: nil) Hooks::Decision.warn(, metadata: ) end |