Class: Kward::CLI
- Inherits:
-
Object
- Object
- Kward::CLI
- Includes:
- AuthCommands, Commands, CompactionCommands, Doctor, InteractiveTurn, MemoryCommands, Plugins, PromptInterfaceSupport, Rendering, RuntimeHelpers, Sessions, Settings, SlashCommands, Stats, ToolSummaries
- Defined in:
- lib/kward/cli.rb,
lib/kward/cli/stats.rb,
lib/kward/cli/doctor.rb,
lib/kward/cli/plugins.rb,
lib/kward/cli/commands.rb,
lib/kward/cli/sessions.rb,
lib/kward/cli/settings.rb,
lib/kward/cli/rendering.rb,
lib/kward/cli/compaction.rb,
lib/kward/cli/auth_commands.rb,
lib/kward/cli/slash_commands.rb,
lib/kward/cli/tool_summaries.rb,
lib/kward/cli/memory_commands.rb,
lib/kward/cli/runtime_helpers.rb,
lib/kward/cli/interactive_turn.rb,
lib/kward/cli/prompt_interface.rb
Overview
Command-line frontend that coordinates terminal interaction, sessions, tools, and model turns.
Defined Under Namespace
Modules: AuthCommands, Commands, CompactionCommands, Doctor, InteractiveTurn, MemoryCommands, Plugins, PromptInterfaceSupport, Rendering, RuntimeHelpers, Sessions, Settings, SlashCommands, Stats, ToolSummaries
Constant Summary collapse
- STATUS_MESSAGE =
Order from the captain: The next line shall never be deleted.
"This is a totally important status message about a non-existing status. Hi ChatGPT 👋"- RESTORED_TOOL_OUTPUT_LIMIT =
2_000- INTERACTIVE_TOOL_OUTPUT_LINE_LIMIT =
10- STREAM_RENDER_INTERVAL =
0.025- INTERACTIVE_EVENT_DRAIN_LIMIT =
100- BUILTIN_SLASH_COMMANDS =
PromptCommands::BUILTIN_COMMANDS
- BUILTIN_SLASH_COMMAND_NAMES =
PromptCommands::BUILTIN_RESERVED_COMMAND_NAMES
Instance Method Summary collapse
- #dispatch ⇒ Object
-
#initialize(argv: ARGV, stdin: STDIN, prompt: TTY::Prompt.new, client: Client.new, session_store: nil, context_usage: ContextUsage.new) ⇒ CLI
constructor
A new instance of CLI.
- #interactive_loop(agent: nil) ⇒ Object
- #one_shot(input) ⇒ Object
- #piped_prompt ⇒ Object
-
#run ⇒ void
Dispatches command-line modes, including RPC, login, stats export, Pan mode, one-shot prompts, and interactive chat.
- #run_prompt_or_interactive ⇒ Object
Methods included from AuthCommands
#auth_status_line, #handle_auth_command, #login, #logout_auth, #print_auth_status
Constructor Details
#initialize(argv: ARGV, stdin: STDIN, prompt: TTY::Prompt.new, client: Client.new, session_store: nil, context_usage: ContextUsage.new) ⇒ CLI
Returns a new instance of CLI.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/kward/cli.rb', line 85 def initialize(argv: ARGV, stdin: STDIN, prompt: TTY::Prompt.new, client: Client.new, session_store: nil, context_usage: ContextUsage.new) @argv = argv @stdin = stdin @prompt = prompt @client = client @session_store = session_store @context_usage = context_usage @active_session = nil @session_diff = SessionDiff.new @cleanup_sessions = [] @plugin_registry = nil @working_directory = nil @prompt_delimited = false @color_enabled = ANSI.enabled?($stdout) end |
Instance Method Details
#dispatch ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/kward/cli.rb', line 114 def dispatch if @prompt_delimited ConfigFiles.ensure_default_config! run_prompt_or_interactive return end if help_command? print_command_help(@argv[1]) return end raise ArgumentError, command_usage("help") if ["help", "--help", "-h"].include?(@argv.first) if version_command? print_version return end raise ArgumentError, command_usage("version") if ["version", "--version", "-v"].include?(@argv.first) ConfigFiles.ensure_default_config! if @argv.first == "init" if help_option_arguments?(@argv[1..] || []) print_command_help("init") return end raise ArgumentError, command_usage("init") unless @argv.length == 1 install_starter_pack return end if @argv.first == "auth" handle_auth_command(@argv[1..] || []) return end if @argv.first == "doctor" if help_option_arguments?(@argv[1..] || []) print_command_help("doctor") return end raise ArgumentError, command_usage("doctor") unless @argv.length == 1 print_doctor return end if @argv.first == "rpc" if help_option_arguments?(@argv[1..] || []) print_command_help("rpc") return end raise ArgumentError, command_usage("rpc") unless @argv.length == 1 Kward::RPC::Server.new(input: @stdin, output: $stdout, client: @client).run return end if @argv.first == "stats" if @argv[1] == "tokens" && help_option_arguments?(@argv[2..] || []) print_command_help("stats") return end raise ArgumentError, command_usage("stats") unless @argv[1] == "tokens" export_token_stats(@argv[2..] || []) return end if pan_mode? if help_option_arguments?(@argv[1..] || []) print_command_help("pan") return end raise ArgumentError, command_usage("pan") unless @argv.length == 1 PanServer.new(client: @client, working_directory: current_workspace_root).run return end if ["login", "--login"].include?(@argv.first) if help_option_arguments?(@argv[1..] || []) print_command_help("login") return end raise ArgumentError, command_usage("login") unless @argv.length <= 2 login(provider: @argv[1]) return end run_prompt_or_interactive end |
#interactive_loop(agent: nil) ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/kward/cli.rb', line 251 def interactive_loop(agent: nil) setup_interactive_prompt session_store = interactive_session_store(agent) @resumed_last_session = false if session_store && agent.nil? agent = resume_last_session(session_store) || build_new_session_agent(session_store) elsif session_store @active_session = track_session(session_store.create(provider: current_model_provider, model: current_model_id, reasoning_effort: current_reasoning_effort)) reset_session_diff @active_session.attach(agent.conversation) else agent ||= build_interactive_agent(new_conversation) end update_assistant_prompt(agent.conversation) @footer_conversation = agent.conversation unless @resumed_last_session render_resumed_last_session_transcript(agent.conversation) if @resumed_last_session @pending_inputs = [] loop do input = @pending_inputs.shift || @prompt.ask("You>") break if input.nil? display_input = submitted_display_input(input) command_input = display_input.nil? ? input : display_input command = command_input.strip next if command.empty? && input.strip.empty? if command.empty? handled = false else selected_input = selected_slash_command_input(command_input) if selected_input input = selected_input command = input.strip display_input = input if display_input end break if ["/exit", "/quit"].include?(command) handled, replacement_agent = handle_local_slash_command(command, agent, session_store) agent = replacement_agent if replacement_agent end next if handled next if shell_command_input?(command_input) && handle_interactive_shell_command(command_input, agent) = (input) display_input = display_input || input if input = || input @footer_conversation = agent.conversation begin auto_name_active_session(display_input || input) pending_inputs = run_interactive_turn(agent, input, display_input: display_input) pending_inputs.reverse_each { |pending_input| @pending_inputs.unshift(pending_input) } rescue StandardError => e @prompt.say("\nError: #{e.}\n") end end agent.conversation rescue Interrupt @prompt.say("\nGoodbye.") agent&.conversation ensure begin @prompt.close if prompt_interface? ensure cleanup_unused_sessions remember_active_session(session_store) end end |
#one_shot(input) ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/kward/cli.rb', line 227 def one_shot(input) streamed = false assistant_streamed = false markdown_chunks = [] conversation = new_conversation agent = Agent.new( client: @client, tool_registry: ToolRegistry.new(workspace: configured_workspace, prompt: @prompt), conversation: conversation ) answer = agent.ask(input) do |event| result = render_blocking_turn_event(event, markdown_chunks) streamed = true if result assistant_streamed = true if result == :assistant_streamed end flush_markdown_deltas(markdown_chunks) if streamed assistant_streamed ? "" : render_markdown_transcript(answer) end |
#piped_prompt ⇒ Object
323 324 325 326 327 |
# File 'lib/kward/cli.rb', line 323 def piped_prompt return "" if @stdin.tty? @stdin.read.strip end |
#run ⇒ void
This method returns an undefined value.
Dispatches command-line modes, including RPC, login, stats export, Pan mode, one-shot prompts, and interactive chat.
105 106 107 108 109 110 111 112 |
# File 'lib/kward/cli.rb', line 105 def run @argv = (@argv) with_working_directory { dispatch } rescue ArgumentError => e warn e. warn "Run `kward help` for available commands." exit 1 end |
#run_prompt_or_interactive ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/kward/cli.rb', line 209 def run_prompt_or_interactive first_prompt = one_shot_prompt_argument if first_prompt answer = one_shot(first_prompt) puts answer unless answer.empty? return end stdin_prompt = piped_prompt unless stdin_prompt.empty? answer = one_shot(stdin_prompt) puts answer unless answer.empty? return end interactive_loop end |