| 1 | package Acme::Claude::Shell::Session; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use 5.020; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use open ':std', ':encoding(UTF-8)'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Types::Standard qw(InstanceOf Str); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | use Marlin | ||||||||||||||||||||||||||||
| 10 | 5 | ▶ | 'loop!' => InstanceOf['IO::Async::Loop'], | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | 'dry_run?' => sub { 0 }, | ||||||||||||||||||||||||||
|
Subroutine
_build_dry_run: called CC=1 SCAR=1.2
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 12 | 0 | ▶ | 'safe_mode?' => sub { 1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
Subroutine
_build_safe_mode: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 13 | 0 | ▶ | 'working_dir?' => sub { '.' }, | ||||||||||||||||||||||||||
|
Subroutine
_build_working_dir: not called CC=1 SCAR=1.2
Subroutine
__ANON__: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 14 | 0 | ▶ | 'colorful?' => sub { 1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
Subroutine
_build_colorful: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 15 | 'model?' => Str, | ||||||||||||||||||||||||||||
| 16 | '_client==.', | ||||||||||||||||||||||||||||
| 17 | 2 | ▶ | '_history==.' => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 5 | '_spinner==.', | |||||||||||||||||||||||||||
| 19 | 5 | ▶ | '_pending_approval==.'; # Future for tool approval (set by hooks, awaited by tool handler) | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 5 | ▶ | use Claude::Agent qw(session create_sdk_mcp_server); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 5 | ▶ | use Claude::Agent::Options; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 5 | ▶ | use Claude::Agent::CLI qw( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | start_spinner stop_spinner | ||||||||||||||||||||||||||||
| 25 | header divider status choose_from | ||||||||||||||||||||||||||||
| 26 | 5 | ); | |||||||||||||||||||||||||||
| 27 | 5 | ▶ | use Acme::Claude::Shell::Tools qw(shell_tools); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 5 | ▶ | use Acme::Claude::Shell::Hooks qw(safety_hooks); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 5 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 5 | ▶ | use Term::ReadLine; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 5 | ▶ | use Term::ANSIColor qw(colored); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 5 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # History file location | ||||||||||||||||||||||||||||
| 35 | 5 | ▶ | my $HISTORY_FILE = File::Spec->catfile($ENV{HOME} // '.', '.acme_claude_shell_history'); | ||||||||||||||||||||||||||
|
Condition50%1/2
$ENV{"HOME"} // "."
MC/DC0%0/1
$ENV{"HOME"} // "."
$ENV{"HOME"}
Truth table50%1/2
A
A= $ENV{"HOME"}
| |||||||||||||||||||||||||||||
| 36 | 5 | my $MAX_HISTORY = 1000; # Maximum lines to keep | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # Fun spinner styles with matching colors | ||||||||||||||||||||||||||||
| 39 | 5 | my @SPINNERS = ( | |||||||||||||||||||||||||||
| 40 | { spinner => 'moon', spinner_color => 'yellow' }, | ||||||||||||||||||||||||||||
| 41 | { spinner => 'earth', spinner_color => 'cyan' }, | ||||||||||||||||||||||||||||
| 42 | { spinner => 'clock', spinner_color => 'blue' }, | ||||||||||||||||||||||||||||
| 43 | { spinner => 'dots', spinner_color => 'magenta' }, | ||||||||||||||||||||||||||||
| 44 | { spinner => 'material', spinner_color => 'green' }, | ||||||||||||||||||||||||||||
| 45 | { spinner => 'circle_half', spinner_color => 'cyan' }, | ||||||||||||||||||||||||||||
| 46 | { spinner => 'color_circles', spinner_color => 'white' }, | ||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub _random_spinner { | ||||||||||||||||||||||||||
|
Subroutine
_random_spinner: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 50 | 0 | return %{ $SPINNERS[rand @SPINNERS] }; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =head1 NAME | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | Acme::Claude::Shell::Session - Multi-turn session for Acme::Claude::Shell | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | use Acme::Claude::Shell::Session; | ||||||||||||||||||||||||||||
| 60 | use IO::Async::Loop; | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | my $loop = IO::Async::Loop->new; | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | my $session = Acme::Claude::Shell::Session->new( | ||||||||||||||||||||||||||||
| 65 | loop => $loop, | ||||||||||||||||||||||||||||
| 66 | dry_run => 0, | ||||||||||||||||||||||||||||
| 67 | safe_mode => 1, | ||||||||||||||||||||||||||||
| 68 | ); | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | $session->run->get; | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | Runs an interactive REPL using Claude's session() function for multi-turn | ||||||||||||||||||||||||||||
| 75 | conversations. Claude remembers context from previous commands, so you can | ||||||||||||||||||||||||||||
| 76 | say things like "now compress those files" after a find command. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | Uses Claude::Agent SDK features: | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =over 4 | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =item * C<session()> - Multi-turn conversation with context | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =item * SDK MCP tools - execute_command, read_file, list_directory, search_files, get_system_info, get_working_directory | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =item * Hooks - PreToolUse (audit), PostToolUse (stats), PostToolUseFailure (errors), Stop (statistics), Notification (logging) | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =item * CLI utilities - Spinners, menus, colored output | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =back | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =head2 Attributes | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =over 4 | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =item * C<loop> (required) - IO::Async::Loop instance | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =item * C<dry_run> - Preview mode, don't execute commands (default: 0) | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =item * C<safe_mode> - Confirm dangerous commands (default: 1) | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =item * C<working_dir> - Starting directory (default: '.') | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =item * C<colorful> - Use colored output (default: 1) | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =item * C<model> - Claude model to use (optional) | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =back | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head2 Built-in Commands | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =over 4 | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =item * C<help> - Show help message | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =item * C<history> - Select and re-run previous commands | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =item * C<clear> - Clear the screen | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =item * C<exit> / C<quit> - Exit the shell | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =back | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =head2 History | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | Command history is persisted to C<~/.acme_claude_shell_history> and loaded | ||||||||||||||||||||||||||||
| 127 | on startup. Maximum 1000 lines are kept. | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =cut | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # Query cursor row position via /dev/tty (works even after Term::ReadLine) | ||||||||||||||||||||||||||||
| 132 | # This avoids Term::ProgressSpinner's STDIN-based query which fails after readline | ||||||||||||||||||||||||||||
| 133 | 0 | ▶ | sub _get_cursor_row { | ||||||||||||||||||||||||||
|
Subroutine
_get_cursor_row: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 134 | 0 | my $row; | |||||||||||||||||||||||||||
| 135 | 0 | eval { | |||||||||||||||||||||||||||
| 136 | 0 | require Term::ReadKey; | |||||||||||||||||||||||||||
| 137 | 0 | ▶ | open(my $tty, '+<', '/dev/tty') or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | # Set raw mode on the tty | ||||||||||||||||||||||||||||
| 139 | 0 | Term::ReadKey::ReadMode(4, $tty); | |||||||||||||||||||||||||||
| 140 | # Send cursor position query to STDOUT (terminal sees it) | ||||||||||||||||||||||||||||
| 141 | 0 | print STDOUT "\e[6n"; | |||||||||||||||||||||||||||
| 142 | 0 | STDOUT->flush(); | |||||||||||||||||||||||||||
| 143 | # Read response from the tty | ||||||||||||||||||||||||||||
| 144 | 0 | my $response = ''; | |||||||||||||||||||||||||||
| 145 | 0 | while (1) { | |||||||||||||||||||||||||||
| 146 | 0 | my $c = Term::ReadKey::ReadKey(0.1, $tty); | |||||||||||||||||||||||||||
| 147 | 0 | ▶ | last unless defined $c; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 0 | $response .= $c; | |||||||||||||||||||||||||||
| 149 | 0 | ▶ | last if $c eq 'R'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | # Restore normal mode | ||||||||||||||||||||||||||||
| 152 | 0 | Term::ReadKey::ReadMode(0, $tty); | |||||||||||||||||||||||||||
| 153 | 0 | close($tty); | |||||||||||||||||||||||||||
| 154 | 0 | ▶ | if ($response =~ /\[(\d+);(\d+)R/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 0 | $row = $1; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | }; | ||||||||||||||||||||||||||||
| 158 | 0 | return $row; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # Track session prompts for saving | ||||||||||||||||||||||||||||
| 162 | 5 | my @_session_prompts; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 0 | ▶ | sub _load_history { | ||||||||||||||||||||||||||
|
Subroutine
_load_history: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 165 | 0 | my ($term) = @_; | |||||||||||||||||||||||||||
| 166 | 0 | ▶ | return unless -f $HISTORY_FILE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 0 | ▶ | open my $fh, '<:encoding(UTF-8)', $HISTORY_FILE or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 0 | ▶ | while (my $line = <$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 0 | chomp $line; | |||||||||||||||||||||||||||
| 170 | 0 | ▶ | $term->addhistory($line) if length $line; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | 0 | close $fh; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 0 | ▶ | sub _append_to_history { | ||||||||||||||||||||||||||
|
Subroutine
_append_to_history: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 176 | 0 | my ($input) = @_; | |||||||||||||||||||||||||||
| 177 | 0 | push @_session_prompts, $input; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | # Append to file immediately | ||||||||||||||||||||||||||||
| 180 | 0 | ▶ | open my $fh, '>>:encoding(UTF-8)', $HISTORY_FILE or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 0 | print $fh "$input\n"; | |||||||||||||||||||||||||||
| 182 | 0 | close $fh; | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | # Trim file if too large (occasionally) | ||||||||||||||||||||||||||||
| 185 | 0 | ▶ | _trim_history_file() if @_session_prompts % 100 == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 0 | ▶ | sub _trim_history_file { | ||||||||||||||||||||||||||
|
Subroutine
_trim_history_file: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 189 | 0 | ▶ | return unless -f $HISTORY_FILE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | 0 | ▶ | open my $fh, '<:encoding(UTF-8)', $HISTORY_FILE or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | 0 | my @lines = <$fh>; | |||||||||||||||||||||||||||
| 192 | 0 | close $fh; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 0 | ▶ | return if @lines <= $MAX_HISTORY; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | # Keep only last MAX_HISTORY lines | ||||||||||||||||||||||||||||
| 197 | 0 | @lines = @lines[-$MAX_HISTORY..-1]; | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 0 | ▶ | open $fh, '>:encoding(UTF-8)', $HISTORY_FILE or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | 0 | print $fh @lines; | |||||||||||||||||||||||||||
| 201 | 0 | close $fh; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 0 | ▶ | async sub run { | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=12 SCAR=50.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 205 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | # Show colorful header | ||||||||||||||||||||||||||||
| 208 | 0 | $self->_show_banner; | |||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 0 | my $term = Term::ReadLine->new('acme_claude_shell'); | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | # Load persistent history | ||||||||||||||||||||||||||||
| 213 | 0 | _load_history($term); | |||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | # Create SDK MCP server with shell tools | ||||||||||||||||||||||||||||
| 216 | 0 | my $mcp = create_sdk_mcp_server( | |||||||||||||||||||||||||||
| 217 | name => 'shell-tools', | ||||||||||||||||||||||||||||
| 218 | tools => shell_tools($self), | ||||||||||||||||||||||||||||
| 219 | ); | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | # Create options with hooks | ||||||||||||||||||||||||||||
| 222 | 0 | ▶ | my $options = Claude::Agent::Options->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | permission_mode => 'bypassPermissions', | ||||||||||||||||||||||||||||
| 224 | mcp_servers => { 'shell-tools' => $mcp }, | ||||||||||||||||||||||||||||
| 225 | hooks => safety_hooks($self), | ||||||||||||||||||||||||||||
| 226 | dry_run => $self->dry_run, | ||||||||||||||||||||||||||||
| 227 | system_prompt => $self->_system_prompt, | ||||||||||||||||||||||||||||
| 228 | ($self->has_model ? (model => $self->model) : ()), | ||||||||||||||||||||||||||||
| 229 | ); | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | # Create persistent session client | ||||||||||||||||||||||||||||
| 232 | 0 | $self->_client(session( | |||||||||||||||||||||||||||
| 233 | options => $options, | ||||||||||||||||||||||||||||
| 234 | loop => $self->loop, | ||||||||||||||||||||||||||||
| 235 | )); | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | # REPL loop | ||||||||||||||||||||||||||||
| 238 | 0 | while (1) { | |||||||||||||||||||||||||||
| 239 | 0 | ▶ | my $prompt_str = $self->colorful | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | ? colored(['bold', 'green'], 'acme_claude_shell> ') | ||||||||||||||||||||||||||||
| 241 | : 'acme_claude_shell> '; | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 0 | my $input = $term->readline($prompt_str); | |||||||||||||||||||||||||||
| 244 | 0 | ▶ | last unless defined $input; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | 0 | $input =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||
| 247 | 0 | ▶ | next unless length $input; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | # Built-in commands | ||||||||||||||||||||||||||||
| 250 | 0 | ▶ | last if $input =~ /^(exit|quit)$/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | 0 | ▶ | if ($input =~ /^history$/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 253 | 0 | my $selected = $self->_show_history; | |||||||||||||||||||||||||||
| 254 | 0 | ▶ | if (defined $selected && length $selected) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $selected and length $selected
MC/DC0%0/2
defined $selected and length $selected
defined $selected
length $selected
Truth table0%0/3
A and B
A= defined $selectedB= length $selected
| |||||||||||||||||||||||||||||
| 255 | # User selected a command to re-run - process it | ||||||||||||||||||||||||||||
| 256 | 0 | $term->addhistory($selected); | |||||||||||||||||||||||||||
| 257 | 0 | _append_to_history($selected); | |||||||||||||||||||||||||||
| 258 | 0 | await $self->_process_input($selected); | |||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||
| 260 | 0 | next; | |||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||
| 262 | 0 | ▶ | if ($input =~ /^clear$/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | 0 | system('clear'); | |||||||||||||||||||||||||||
| 264 | 0 | next; | |||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | 0 | ▶ | if ($input =~ /^help$/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | 0 | $self->_show_help; | |||||||||||||||||||||||||||
| 268 | 0 | next; | |||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | # Add to readline history and persist to file | ||||||||||||||||||||||||||||
| 272 | 0 | $term->addhistory($input); | |||||||||||||||||||||||||||
| 273 | 0 | _append_to_history($input); | |||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | # Process with Claude | ||||||||||||||||||||||||||||
| 276 | 0 | await $self->_process_input($input); | |||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | 0 | ▶ | status('info', "Goodbye!") if $self->colorful; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 280 | 0 | return 1; | |||||||||||||||||||||||||||
| 281 | 5 | } | |||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | 0 | ▶ | async sub _process_input { | ||||||||||||||||||||||||||
|
Subroutine
_process_input: not called CC=21 SCAR=61.4
| |||||||||||||||||||||||||||||
| 284 | 0 | my ($self, $input) = @_; | |||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | # Query cursor position via /dev/tty before starting spinner | ||||||||||||||||||||||||||||
| 287 | # This avoids Term::ProgressSpinner's STDIN query which fails after Term::ReadLine | ||||||||||||||||||||||||||||
| 288 | 0 | my $cursor_row = _get_cursor_row(); | |||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | # Store spinner in session so hooks can stop it before reading STDIN | ||||||||||||||||||||||||||||
| 291 | # Pick a random fun spinner each time | ||||||||||||||||||||||||||||
| 292 | 0 | ▶ | $self->_spinner(start_spinner("Thinking...", $self->loop, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 293 | _random_spinner(), | ||||||||||||||||||||||||||||
| 294 | defined $cursor_row ? (terminal_line => $cursor_row) : ())); | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | # First turn or follow-up | ||||||||||||||||||||||||||||
| 297 | 0 | ▶ | if ($self->_client->session_id) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 298 | 0 | $self->_client->send($input); | |||||||||||||||||||||||||||
| 299 | } else { | ||||||||||||||||||||||||||||
| 300 | 0 | $self->_client->connect($input); | |||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | 0 | my $printed_response = 0; | |||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | 0 | ▶ | while (my $msg = await $self->_client->receive_async) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 306 | 0 | ▶ | if ($msg->isa('Claude::Agent::Message::Assistant')) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 307 | # Print reasoning immediately so it appears BEFORE tool approval | ||||||||||||||||||||||||||||
| 308 | 0 | ▶ | my $text = $msg->text // ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$msg->text // ""
MC/DC0%0/1
$msg->text // ""
$msg->text
Truth table0%0/2
A
A= $msg->text
| |||||||||||||||||||||||||||||
| 309 | 0 | ▶ | if ($text) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 310 | # Stop spinner before printing text | ||||||||||||||||||||||||||||
| 311 | 0 | ▶ | if ($self->_spinner) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 312 | 0 | stop_spinner($self->_spinner); | |||||||||||||||||||||||||||
| 313 | 0 | $self->_spinner(undef); | |||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||
| 315 | 0 | ▶ | print "\n" unless $printed_response; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 316 | 0 | ▶ | my $output = $self->colorful ? colored(['white'], $text) : $text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 317 | 0 | print $output; | |||||||||||||||||||||||||||
| 318 | 0 | $printed_response = 1; | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||
| 321 | elsif ($msg->isa('Claude::Agent::Message::ToolUse')) { | ||||||||||||||||||||||||||||
| 322 | # Print newline after reasoning text before tool approval menu | ||||||||||||||||||||||||||||
| 323 | 0 | ▶ | print "\n" if $printed_response; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | 0 | $printed_response = 0; # Reset for next assistant message | |||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||
| 326 | elsif ($msg->isa('Claude::Agent::Message::ToolResult')) { | ||||||||||||||||||||||||||||
| 327 | # Show result | ||||||||||||||||||||||||||||
| 328 | 0 | ▶ | my $content = $msg->content // ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$msg->content // ""
MC/DC0%0/1
$msg->content // ""
$msg->content
Truth table0%0/2
A
A= $msg->content
| |||||||||||||||||||||||||||||
| 329 | 0 | ▶ | if ($msg->is_error) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 330 | # Check if this was a user denial (dry-run, cancel, etc.) | ||||||||||||||||||||||||||||
| 331 | # In that case, don't show error and stop processing | ||||||||||||||||||||||||||||
| 332 | 0 | ▶ | if ($content =~ /^(Dry-run:|User cancelled)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 333 | 0 | last; # Stop the conversation here | |||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||
| 335 | 0 | ▶ | status('error', $content) if $self->colorful; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 336 | } else { | ||||||||||||||||||||||||||||
| 337 | 0 | ▶ | print $content, "\n" if $content; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||
| 339 | # Don't restart spinner - avoids conflicts with Term::ProgressSpinner | ||||||||||||||||||||||||||||
| 340 | # when STDIN was used for hook confirmation | ||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||
| 342 | elsif ($msg->isa('Claude::Agent::Message::Result')) { | ||||||||||||||||||||||||||||
| 343 | 0 | last; | |||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | 0 | ▶ | stop_spinner($self->_spinner, "Done") if $self->_spinner; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 348 | 0 | $self->_spinner(undef); | |||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | # Final newline if we printed any response | ||||||||||||||||||||||||||||
| 351 | 0 | ▶ | print "\n" if $printed_response; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 352 | 5 | } | |||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | 0 | ▶ | sub _show_banner { | ||||||||||||||||||||||||||
|
Subroutine
_show_banner: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 355 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | 0 | ▶ | if ($self->colorful) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 358 | 0 | header("Acme::Claude::Shell"); | |||||||||||||||||||||||||||
| 359 | 0 | status('info', "AI-powered shell - describe what you want in plain English"); | |||||||||||||||||||||||||||
| 360 | 0 | status('info', "Type 'help' for commands, 'exit' to quit"); | |||||||||||||||||||||||||||
| 361 | 0 | divider(); | |||||||||||||||||||||||||||
| 362 | 0 | print "\n"; | |||||||||||||||||||||||||||
| 363 | } else { | ||||||||||||||||||||||||||||
| 364 | 0 | print "=" x 60, "\n"; | |||||||||||||||||||||||||||
| 365 | 0 | print " Acme::Claude::Shell\n"; | |||||||||||||||||||||||||||
| 366 | 0 | print "=" x 60, "\n"; | |||||||||||||||||||||||||||
| 367 | 0 | print "AI-powered shell - describe what you want in plain English\n"; | |||||||||||||||||||||||||||
| 368 | 0 | print "Type 'help' for commands, 'exit' to quit\n"; | |||||||||||||||||||||||||||
| 369 | 0 | print "-" x 60, "\n\n"; | |||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | 0 | ▶ | sub _show_help { | ||||||||||||||||||||||||||
|
Subroutine
_show_help: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 374 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | 0 | ▶ | if ($self->colorful) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 377 | 0 | header("Help"); | |||||||||||||||||||||||||||
| 378 | } else { | ||||||||||||||||||||||||||||
| 379 | 0 | print "\n--- Help ---\n"; | |||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | 0 | print <<'HELP'; | |||||||||||||||||||||||||||
| 383 | Built-in commands: | ||||||||||||||||||||||||||||
| 384 | help - Show this help | ||||||||||||||||||||||||||||
| 385 | history - Select and re-run previous commands | ||||||||||||||||||||||||||||
| 386 | clear - Clear screen | ||||||||||||||||||||||||||||
| 387 | exit - Exit shell (or 'quit') | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | Just type what you want in plain English: | ||||||||||||||||||||||||||||
| 390 | "find all large log files" | ||||||||||||||||||||||||||||
| 391 | "show disk usage by directory" | ||||||||||||||||||||||||||||
| 392 | "compress files older than 7 days" | ||||||||||||||||||||||||||||
| 393 | "now delete those files" (uses context from previous command) | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | Claude will show you the command before running it. | ||||||||||||||||||||||||||||
| 396 | You can approve, edit, dry-run, or cancel. | ||||||||||||||||||||||||||||
| 397 | HELP | ||||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | 0 | ▶ | sub _show_history { | ||||||||||||||||||||||||||
|
Subroutine
_show_history: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 401 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | # Load prompt history from file | ||||||||||||||||||||||||||||
| 404 | 0 | my @history; | |||||||||||||||||||||||||||
| 405 | 0 | ▶ | if (-f $HISTORY_FILE) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 406 | 0 | ▶ | open my $fh, '<:encoding(UTF-8)', $HISTORY_FILE or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 407 | 0 | print "Could not read history file.\n\n"; | |||||||||||||||||||||||||||
| 408 | 0 | return; | |||||||||||||||||||||||||||
| 409 | }; | ||||||||||||||||||||||||||||
| 410 | 0 | @history = <$fh>; | |||||||||||||||||||||||||||
| 411 | 0 | close $fh; | |||||||||||||||||||||||||||
| 412 | 0 | chomp @history; | |||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | # Add current session prompts not yet in file | ||||||||||||||||||||||||||||
| 416 | 0 | ▶ | push @history, @_session_prompts if @_session_prompts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | 0 | ▶ | unless (@history) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 419 | 0 | ▶ | if ($self->colorful) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 420 | 0 | status('info', "No history yet."); | |||||||||||||||||||||||||||
| 421 | } else { | ||||||||||||||||||||||||||||
| 422 | 0 | print "No history yet.\n"; | |||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||
| 424 | 0 | return; | |||||||||||||||||||||||||||
| 425 | } | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | # Get last 20 unique entries for selection | ||||||||||||||||||||||||||||
| 428 | 0 | ▶ | my @recent = @history > 20 ? @history[-20..-1] : @history; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | # Use choose_from for interactive selection | ||||||||||||||||||||||||||||
| 431 | 0 | ▶ | my $selected = choose_from( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 432 | \@recent, | ||||||||||||||||||||||||||||
| 433 | prompt => "Select a command to re-run (or press 'q' to cancel):", | ||||||||||||||||||||||||||||
| 434 | inline_prompt => @history > 20 ? "(Last 20 of " . scalar(@history) . ")" : "", | ||||||||||||||||||||||||||||
| 435 | layout => 2, # Single column for readability | ||||||||||||||||||||||||||||
| 436 | ); | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | 0 | return $selected; | |||||||||||||||||||||||||||
| 439 | } | ||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | 0 | ▶ | sub _system_prompt { | ||||||||||||||||||||||||||
|
Subroutine
_system_prompt: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 442 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 443 | 0 | return <<'PROMPT'; | |||||||||||||||||||||||||||
| 444 | You are an AI shell assistant. The user describes tasks in natural language, | ||||||||||||||||||||||||||||
| 445 | and you translate them into shell commands. | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | When the user asks you to do something: | ||||||||||||||||||||||||||||
| 448 | 1. Explain what command(s) you'll run and why | ||||||||||||||||||||||||||||
| 449 | 2. Use the execute_command tool to run them | ||||||||||||||||||||||||||||
| 450 | 3. Summarize the results | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | IMPORTANT: Remember context from previous commands! | ||||||||||||||||||||||||||||
| 453 | If the user says "now do X to those files", use the results from the | ||||||||||||||||||||||||||||
| 454 | previous command to know which files they mean. | ||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | PERL FALLBACK: When a task cannot be done with standard shell commands, | ||||||||||||||||||||||||||||
| 457 | or when a shell command isn't available on the system, use Perl one-liners instead. | ||||||||||||||||||||||||||||
| 458 | Perl is always available. Examples: | ||||||||||||||||||||||||||||
| 459 | - Instead of: jq '.key' file.json | ||||||||||||||||||||||||||||
| 460 | Use: perl -MJSON -0777 -ne 'print decode_json($_)->{key}' file.json | ||||||||||||||||||||||||||||
| 461 | - Instead of: sed -i 's/old/new/g' file | ||||||||||||||||||||||||||||
| 462 | Use: perl -pi -e 's/old/new/g' file | ||||||||||||||||||||||||||||
| 463 | - For complex text processing, JSON/YAML parsing, or when shell tools are missing, | ||||||||||||||||||||||||||||
| 464 | prefer Perl one-liners as they are portable and powerful. | ||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | Be helpful but safe: | ||||||||||||||||||||||||||||
| 467 | - Warn about destructive operations (rm, dd, etc.) | ||||||||||||||||||||||||||||
| 468 | - Prefer safe alternatives when possible | ||||||||||||||||||||||||||||
| 469 | - Explain what each command does | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | Always explain what you're about to do before using tools. | ||||||||||||||||||||||||||||
| 472 | PROMPT | ||||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | LNATION, C<< <email at lnation.org> >> | ||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||
| 479 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | This software is Copyright (c) 2026 by LNATION. | ||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | This is free software, licensed under: | ||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||
| 487 | =cut | ||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | 5 | 1; | |||||||||||||||||||||||||||