| 1 | package Acme::Claude::Shell::Session; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use 5.020; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use open ':std', ':encoding(UTF-8)'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use Object::Proto (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use Claude::Agent qw(session create_sdk_mcp_server); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use Claude::Agent::Options; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use Claude::Agent::Content; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 6 | ▶ | use Claude::Agent::CLI qw( | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | start_spinner stop_spinner | ||||||||||||||||||||||||||||||||
| 14 | header divider status choose_from | ||||||||||||||||||||||||||||||||
| 15 | 6 | ); | |||||||||||||||||||||||||||||||
| 16 | 6 | ▶ | use Acme::Claude::Shell::Tools qw(shell_tools); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 17 | 6 | ▶ | use Acme::Claude::Shell::Hooks qw(safety_hooks); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 18 | 6 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 19 | 6 | ▶ | use Term::ReadLine; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 20 | 6 | ▶ | use Term::ANSIColor qw(colored); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 21 | 6 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | 6 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 24 | 6 | Object::Proto::define('Acme::Claude::Shell::Session', | |||||||||||||||||||||||||||||||
| 25 | 'loop:Object:required', | ||||||||||||||||||||||||||||||||
| 26 | 'dry_run:Bool:default(0):predicate', | ||||||||||||||||||||||||||||||||
| 27 | 'safe_mode:Bool:default(1):predicate', | ||||||||||||||||||||||||||||||||
| 28 | 'working_dir:Str:default(.):predicate', | ||||||||||||||||||||||||||||||||
| 29 | 'colorful:Bool:default(1):predicate', | ||||||||||||||||||||||||||||||||
| 30 | 'verbose:Bool:default(0):predicate', | ||||||||||||||||||||||||||||||||
| 31 | 'audit_log:Bool:default(0):predicate', | ||||||||||||||||||||||||||||||||
| 32 | 'model:Str:predicate', | ||||||||||||||||||||||||||||||||
| 33 | 'effort:Str:predicate', | ||||||||||||||||||||||||||||||||
| 34 | '_client', | ||||||||||||||||||||||||||||||||
| 35 | '_history:ArrayRef:default([])', | ||||||||||||||||||||||||||||||||
| 36 | '_spinner', | ||||||||||||||||||||||||||||||||
| 37 | '_session_start', | ||||||||||||||||||||||||||||||||
| 38 | '_tool_count:Int:default(0)', | ||||||||||||||||||||||||||||||||
| 39 | '_tool_errors:Int:default(0)', | ||||||||||||||||||||||||||||||||
| 40 | '_audit_log:ArrayRef:default([])', | ||||||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | 6 | ▶ | 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"}
| |||||||||||||||||||||||||||||||||
| 45 | 6 | my $MAX_HISTORY = 1000; | |||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | 6 | my @SPINNERS = ( | |||||||||||||||||||||||||||||||
| 48 | { spinner => 'moon', spinner_color => 'yellow' }, | ||||||||||||||||||||||||||||||||
| 49 | { spinner => 'earth', spinner_color => 'cyan' }, | ||||||||||||||||||||||||||||||||
| 50 | { spinner => 'clock', spinner_color => 'blue' }, | ||||||||||||||||||||||||||||||||
| 51 | { spinner => 'dots', spinner_color => 'magenta' }, | ||||||||||||||||||||||||||||||||
| 52 | { spinner => 'material', spinner_color => 'green' }, | ||||||||||||||||||||||||||||||||
| 53 | { spinner => 'circle_half', spinner_color => 'cyan' }, | ||||||||||||||||||||||||||||||||
| 54 | { spinner => 'color_circles', spinner_color => 'white' }, | ||||||||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | sub _random_spinner { | ||||||||||||||||||||||||||||||
|
Subroutine
_random_spinner: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 58 | 0 | return %{ $SPINNERS[rand @SPINNERS] }; | |||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | sub _get_cursor_row { | ||||||||||||||||||||||||||||||
|
Subroutine
_get_cursor_row: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||
| 62 | 0 | my $row; | |||||||||||||||||||||||||||||||
| 63 | 0 | eval { | |||||||||||||||||||||||||||||||
| 64 | 0 | require Term::ReadKey; | |||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | open(my $tty, '+<', '/dev/tty') or return undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 66 | 0 | Term::ReadKey::ReadMode(4, $tty); | |||||||||||||||||||||||||||||||
| 67 | 0 | print STDOUT "\e[6n"; | |||||||||||||||||||||||||||||||
| 68 | 0 | STDOUT->flush(); | |||||||||||||||||||||||||||||||
| 69 | 0 | my $response = ''; | |||||||||||||||||||||||||||||||
| 70 | 0 | while (1) { | |||||||||||||||||||||||||||||||
| 71 | 0 | my $c = Term::ReadKey::ReadKey(0.1, $tty); | |||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | last unless defined $c; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 73 | 0 | $response .= $c; | |||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | last if $c eq 'R'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||
| 76 | 0 | Term::ReadKey::ReadMode(0, $tty); | |||||||||||||||||||||||||||||||
| 77 | 0 | close($tty); | |||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if ($response =~ /\[(\d+);(\d+)R/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 79 | 0 | $row = $1; | |||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||
| 81 | }; | ||||||||||||||||||||||||||||||||
| 82 | 0 | return $row; | |||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | 6 | my @_session_prompts; | |||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | sub _load_history { | ||||||||||||||||||||||||||||||
|
Subroutine
_load_history: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||
| 88 | 0 | my ($term) = @_; | |||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | return unless -f $HISTORY_FILE; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | open my $fh, '<:encoding(UTF-8)', $HISTORY_FILE or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | while (my $line = <$fh>) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 92 | 0 | chomp $line; | |||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | $term->addhistory($line) if length $line; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||
| 95 | 0 | close $fh; | |||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | sub _append_to_history { | ||||||||||||||||||||||||||||||
|
Subroutine
_append_to_history: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||
| 99 | 0 | my ($input) = @_; | |||||||||||||||||||||||||||||||
| 100 | 0 | push @_session_prompts, $input; | |||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | open my $fh, '>>:encoding(UTF-8)', $HISTORY_FILE or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 103 | 0 | print $fh "$input\n"; | |||||||||||||||||||||||||||||||
| 104 | 0 | close $fh; | |||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | _trim_history_file() if @_session_prompts % 100 == 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | sub _trim_history_file { | ||||||||||||||||||||||||||||||
|
Subroutine
_trim_history_file: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | return unless -f $HISTORY_FILE; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | open my $fh, '<:encoding(UTF-8)', $HISTORY_FILE or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 112 | 0 | my @lines = <$fh>; | |||||||||||||||||||||||||||||||
| 113 | 0 | close $fh; | |||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | return if @lines <= $MAX_HISTORY; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | 0 | @lines = @lines[-$MAX_HISTORY..-1]; | |||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | open $fh, '>:encoding(UTF-8)', $HISTORY_FILE or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 120 | 0 | print $fh @lines; | |||||||||||||||||||||||||||||||
| 121 | 0 | close $fh; | |||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | sub options { | ||||||||||||||||||||||||||||||
|
Subroutine
options: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 125 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | 0 | my $mcp = create_sdk_mcp_server( | |||||||||||||||||||||||||||||||
| 128 | name => 'shell-tools', | ||||||||||||||||||||||||||||||||
| 129 | tools => shell_tools($self), | ||||||||||||||||||||||||||||||||
| 130 | ); | ||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | return Claude::Agent::Options->new( | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 133 | permission_mode => 'bypassPermissions', | ||||||||||||||||||||||||||||||||
| 134 | tools => [], | ||||||||||||||||||||||||||||||||
| 135 | mcp_servers => { 'shell-tools' => $mcp }, | ||||||||||||||||||||||||||||||||
| 136 | hooks => safety_hooks($self), | ||||||||||||||||||||||||||||||||
| 137 | dry_run => $self->dry_run, | ||||||||||||||||||||||||||||||||
| 138 | system_prompt => $self->_system_prompt, | ||||||||||||||||||||||||||||||||
| 139 | ($self->has_model ? (model => $self->model) : ()), | ||||||||||||||||||||||||||||||||
| 140 | ($self->has_effort ? (effort => $self->effort) : ()), | ||||||||||||||||||||||||||||||||
| 141 | ); | ||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | async sub run { | ||||||||||||||||||||||||||||||
|
Subroutine
run: not called CC=13 SCAR=52.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 145 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | 0 | $self->_show_banner; | |||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | 0 | my $term = Term::ReadLine->new('acme_claude_shell'); | |||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||
| 151 | 0 | _load_history($term); | |||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | 0 | $self->_client(session( | |||||||||||||||||||||||||||||||
| 154 | options => $self->options, | ||||||||||||||||||||||||||||||||
| 155 | loop => $self->loop, | ||||||||||||||||||||||||||||||||
| 156 | )); | ||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||
| 158 | 0 | while (1) { | |||||||||||||||||||||||||||||||
| 159 | 0 | ▶ | my $prompt_str = $self->colorful | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 160 | ? colored(['bold', 'green'], 'acme_claude_shell> ') | ||||||||||||||||||||||||||||||||
| 161 | : 'acme_claude_shell> '; | ||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||
| 163 | 0 | my $input = $term->readline($prompt_str); | |||||||||||||||||||||||||||||||
| 164 | 0 | ▶ | last unless defined $input; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||
| 166 | 0 | $input =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||
| 167 | 0 | ▶ | next unless length $input; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||
| 169 | 0 | ▶ | last if $input =~ /^(exit|quit)$/i; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||
| 171 | 0 | ▶ | if ($input =~ /^history$/i) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 172 | 0 | my $selected = $self->_show_history; | |||||||||||||||||||||||||||||||
| 173 | 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
| |||||||||||||||||||||||||||||||||
| 174 | 0 | $term->addhistory($selected); | |||||||||||||||||||||||||||||||
| 175 | 0 | _append_to_history($selected); | |||||||||||||||||||||||||||||||
| 176 | 0 | await $self->_process_input($selected); | |||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||
| 178 | 0 | next; | |||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||
| 180 | 0 | ▶ | if ($input =~ /^clear$/i) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 181 | 0 | system('clear'); | |||||||||||||||||||||||||||||||
| 182 | 0 | next; | |||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||
| 184 | 0 | ▶ | if ($input =~ /^help$/i) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 185 | 0 | $self->_show_help; | |||||||||||||||||||||||||||||||
| 186 | 0 | next; | |||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||
| 189 | 0 | $term->addhistory($input); | |||||||||||||||||||||||||||||||
| 190 | 0 | _append_to_history($input); | |||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||
| 192 | 0 | await $self->_process_input($input); | |||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||
| 195 | 0 | ▶ | $self->_client->disconnect if $self->_client && $self->_client->is_connected; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->_client and $self->_client->is_connected
MC/DC0%0/2
$self->_client and $self->_client->is_connected
$self->_client
$self->_client->is_connected
Truth table0%0/3
A and B
A= $self->_clientB= $self->_client->is_connected
| |||||||||||||||||||||||||||||||||
| 196 | 0 | ▶ | status('info', "Goodbye!") if $self->colorful; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 197 | 0 | return 1; | |||||||||||||||||||||||||||||||
| 198 | 6 | } | |||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||
| 200 | 0 | ▶ | async sub _process_input { | ||||||||||||||||||||||||||||||
|
Subroutine
_process_input: not called CC=15 SCAR=54.8
| |||||||||||||||||||||||||||||||||
| 201 | 0 | my ($self, $input) = @_; | |||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||
| 203 | 0 | ▶ | if ($self->colorful) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 204 | 0 | my $cursor_row = _get_cursor_row(); | |||||||||||||||||||||||||||||||
| 205 | 0 | ▶ | $self->_spinner(start_spinner("Thinking...", $self->loop, | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 206 | _random_spinner(), | ||||||||||||||||||||||||||||||||
| 207 | defined $cursor_row ? (terminal_line => $cursor_row) : ())); | ||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||
| 210 | 0 | ▶ | if ($self->_client->is_connected) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 211 | 0 | $self->_client->send($input); | |||||||||||||||||||||||||||||||
| 212 | } else { | ||||||||||||||||||||||||||||||||
| 213 | 0 | $self->_client->connect($input); | |||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||
| 216 | 0 | my $printed_response = 0; | |||||||||||||||||||||||||||||||
| 217 | 0 | my %tool_names; | |||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||
| 219 | 0 | ▶ | while (my $msg = await $self->_client->receive_async) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 220 | 0 | ▶ | if ($msg->isa('Claude::Agent::Message::Assistant')) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 221 | 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
| |||||||||||||||||||||||||||||||||
| 222 | 0 | ▶ | if (length $text) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 223 | 0 | $self->_stop_spinner; | |||||||||||||||||||||||||||||||
| 224 | 0 | ▶ | print "\n" unless $printed_response; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 225 | 0 | ▶ | print $self->colorful ? colored(['white'], $text) : $text; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 226 | 0 | $printed_response = 1; | |||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||
| 228 | 0 | for my $use (@{ $msg->tool_uses }) { | |||||||||||||||||||||||||||||||
| 229 | 0 | $tool_names{ $use->id } = $use->name; | |||||||||||||||||||||||||||||||
| 230 | 0 | ▶ | print "\n" if $printed_response; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 231 | 0 | $printed_response = 0; | |||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||
| 234 | elsif ($msg->isa('Claude::Agent::Message::User')) { | ||||||||||||||||||||||||||||||||
| 235 | 0 | $self->_show_tool_results($msg, \%tool_names); | |||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||
| 237 | elsif ($msg->isa('Claude::Agent::Message::Result')) { | ||||||||||||||||||||||||||||||||
| 238 | 0 | last; | |||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||
| 242 | 0 | $self->_stop_spinner("Done"); | |||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||
| 244 | 0 | ▶ | print "\n" if $printed_response; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 245 | 6 | } | |||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||
| 247 | 0 | ▶ | sub _stop_spinner { | ||||||||||||||||||||||||||||||
|
Subroutine
_stop_spinner: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||
| 248 | 0 | my ($self, $text) = @_; | |||||||||||||||||||||||||||||||
| 249 | 0 | ▶ | return unless $self->_spinner; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 250 | 0 | ▶ | stop_spinner($self->_spinner, (defined $text ? $text : ())); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 251 | 0 | $self->_spinner(undef); | |||||||||||||||||||||||||||||||
| 252 | 0 | return; | |||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||
| 255 | 0 | ▶ | sub _show_tool_results { | ||||||||||||||||||||||||||||||
|
Subroutine
_show_tool_results: not called CC=16 SCAR=56.1
| |||||||||||||||||||||||||||||||||
| 256 | 0 | my ($self, $msg, $tool_names) = @_; | |||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||
| 258 | 0 | my $content = $msg->message->{content}; | |||||||||||||||||||||||||||||||
| 259 | 0 | ▶ | return unless ref $content eq 'ARRAY'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||
| 261 | 0 | for my $block (@$content) { | |||||||||||||||||||||||||||||||
| 262 | 0 | ▶ | next unless ref $block eq 'HASH' && ($block->{type} // '') eq 'tool_result'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
ref $block eq "HASH" and ($block->{"type"} // "") eq "tool_result"
$block->{"type"} // ""
MC/DC0%0/2
ref $block eq "HASH" and ($block->{"type"} // "") eq "tool_result"
ref $block eq "HASH"
$block->{"type"}
Truth table0%0/3
A and B
A= ref $block eq "HASH"B= $block->{"type"}
| |||||||||||||||||||||||||||||||||
| 263 | 0 | my $result = Claude::Agent::Content->from_json($block); | |||||||||||||||||||||||||||||||
| 264 | 0 | ▶ | next unless ref $result && $result->can('text'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $result and $result->can("text")
MC/DC0%0/2
ref $result and $result->can("text")
ref $result
$result->can("text")
Truth table0%0/3
A and B
A= ref $resultB= $result->can("text")
| |||||||||||||||||||||||||||||||||
| 265 | 0 | ▶ | my $name = $tool_names->{ $result->tool_use_id } // ''; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$$tool_names{$result->tool_use_id} // ""
MC/DC0%0/1
$$tool_names{$result->tool_use_id} // ""
$$tool_names{$result->tool_use_id}
Truth table0%0/2
A
A= $$tool_names{$result->tool_use_id}
| |||||||||||||||||||||||||||||||||
| 266 | 0 | ▶ | next unless $name =~ /execute_command$/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 267 | 0 | ▶ | my $text = $result->text // ''; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$result->text // ""
MC/DC0%0/1
$result->text // ""
$result->text
Truth table0%0/2
A
A= $result->text
| |||||||||||||||||||||||||||||||||
| 268 | 0 | ▶ | if ($result->is_error) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 269 | 0 | ▶ | next if $text =~ /^(Dry-run:|User cancelled)/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 270 | 0 | ▶ | status('error', $text) if $self->colorful; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||
| 272 | elsif (length $text) { | ||||||||||||||||||||||||||||||||
| 273 | 0 | print $text; | |||||||||||||||||||||||||||||||
| 274 | 0 | ▶ | print "\n" unless $text =~ /\n\z/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||
| 277 | 0 | return; | |||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||
| 280 | 0 | ▶ | sub _show_banner { | ||||||||||||||||||||||||||||||
|
Subroutine
_show_banner: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 281 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||
| 283 | 0 | ▶ | if ($self->colorful) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 284 | 0 | header("Acme::Claude::Shell"); | |||||||||||||||||||||||||||||||
| 285 | 0 | status('info', "AI-powered shell - describe what you want in plain English"); | |||||||||||||||||||||||||||||||
| 286 | 0 | status('info', "Type 'help' for commands, 'exit' to quit"); | |||||||||||||||||||||||||||||||
| 287 | 0 | divider(); | |||||||||||||||||||||||||||||||
| 288 | 0 | print "\n"; | |||||||||||||||||||||||||||||||
| 289 | } else { | ||||||||||||||||||||||||||||||||
| 290 | 0 | print "=" x 60, "\n"; | |||||||||||||||||||||||||||||||
| 291 | 0 | print " Acme::Claude::Shell\n"; | |||||||||||||||||||||||||||||||
| 292 | 0 | print "=" x 60, "\n"; | |||||||||||||||||||||||||||||||
| 293 | 0 | print "AI-powered shell - describe what you want in plain English\n"; | |||||||||||||||||||||||||||||||
| 294 | 0 | print "Type 'help' for commands, 'exit' to quit\n"; | |||||||||||||||||||||||||||||||
| 295 | 0 | print "-" x 60, "\n\n"; | |||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||
| 299 | 0 | ▶ | sub _show_help { | ||||||||||||||||||||||||||||||
|
Subroutine
_show_help: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 300 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||
| 302 | 0 | ▶ | if ($self->colorful) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 303 | 0 | header("Help"); | |||||||||||||||||||||||||||||||
| 304 | } else { | ||||||||||||||||||||||||||||||||
| 305 | 0 | print "\n--- Help ---\n"; | |||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||
| 308 | 0 | print <<'HELP'; | |||||||||||||||||||||||||||||||
| 309 | Built-in commands: | ||||||||||||||||||||||||||||||||
| 310 | help - Show this help | ||||||||||||||||||||||||||||||||
| 311 | history - Select and re-run previous commands | ||||||||||||||||||||||||||||||||
| 312 | clear - Clear screen | ||||||||||||||||||||||||||||||||
| 313 | exit - Exit shell (or 'quit') | ||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||
| 315 | Just type what you want in plain English: | ||||||||||||||||||||||||||||||||
| 316 | "find all large log files" | ||||||||||||||||||||||||||||||||
| 317 | "show disk usage by directory" | ||||||||||||||||||||||||||||||||
| 318 | "compress files older than 7 days" | ||||||||||||||||||||||||||||||||
| 319 | "now delete those files" (uses context from previous command) | ||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||
| 321 | Claude will show you the command before running it. | ||||||||||||||||||||||||||||||||
| 322 | You can approve, edit, dry-run, or cancel. | ||||||||||||||||||||||||||||||||
| 323 | HELP | ||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||
| 326 | 0 | ▶ | sub _show_history { | ||||||||||||||||||||||||||||||
|
Subroutine
_show_history: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||
| 327 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||
| 329 | 0 | my @history; | |||||||||||||||||||||||||||||||
| 330 | 0 | ▶ | if (-f $HISTORY_FILE) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 331 | 0 | ▶ | open my $fh, '<:encoding(UTF-8)', $HISTORY_FILE or do { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 332 | 0 | print "Could not read history file.\n\n"; | |||||||||||||||||||||||||||||||
| 333 | 0 | return; | |||||||||||||||||||||||||||||||
| 334 | }; | ||||||||||||||||||||||||||||||||
| 335 | 0 | @history = <$fh>; | |||||||||||||||||||||||||||||||
| 336 | 0 | close $fh; | |||||||||||||||||||||||||||||||
| 337 | 0 | chomp @history; | |||||||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||
| 340 | 0 | ▶ | push @history, @_session_prompts if @_session_prompts; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||
| 342 | 0 | ▶ | unless (@history) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 343 | 0 | ▶ | if ($self->colorful) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 344 | 0 | status('info', "No history yet."); | |||||||||||||||||||||||||||||||
| 345 | } else { | ||||||||||||||||||||||||||||||||
| 346 | 0 | print "No history yet.\n"; | |||||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||||
| 348 | 0 | return; | |||||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||
| 351 | 0 | ▶ | my @recent = @history > 20 ? @history[-20..-1] : @history; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||
| 353 | 0 | ▶ | my $selected = choose_from( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 354 | \@recent, | ||||||||||||||||||||||||||||||||
| 355 | prompt => "Select a command to re-run (or press 'q' to cancel):", | ||||||||||||||||||||||||||||||||
| 356 | inline_prompt => @history > 20 ? "(Last 20 of " . scalar(@history) . ")" : "", | ||||||||||||||||||||||||||||||||
| 357 | layout => 2, | ||||||||||||||||||||||||||||||||
| 358 | ); | ||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||
| 360 | 0 | return $selected; | |||||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||
| 363 | 0 | ▶ | sub _system_prompt { | ||||||||||||||||||||||||||||||
|
Subroutine
_system_prompt: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 364 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 365 | 0 | return <<'PROMPT'; | |||||||||||||||||||||||||||||||
| 366 | You are an AI shell assistant. The user describes tasks in natural language, | ||||||||||||||||||||||||||||||||
| 367 | and you translate them into shell commands. | ||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||
| 369 | When the user asks you to do something: | ||||||||||||||||||||||||||||||||
| 370 | 1. Explain what command(s) you'll run and why | ||||||||||||||||||||||||||||||||
| 371 | 2. Use the execute_command tool to run them | ||||||||||||||||||||||||||||||||
| 372 | 3. Summarize the results | ||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||
| 374 | IMPORTANT: Remember context from previous commands! | ||||||||||||||||||||||||||||||||
| 375 | If the user says "now do X to those files", use the results from the | ||||||||||||||||||||||||||||||||
| 376 | previous command to know which files they mean. | ||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||
| 378 | PERL FALLBACK: When a task cannot be done with standard shell commands, | ||||||||||||||||||||||||||||||||
| 379 | or when a shell command isn't available on the system, use Perl one-liners instead. | ||||||||||||||||||||||||||||||||
| 380 | Perl is always available. Examples: | ||||||||||||||||||||||||||||||||
| 381 | - Instead of: jq '.key' file.json | ||||||||||||||||||||||||||||||||
| 382 | Use: perl -MJSON -0777 -ne 'print decode_json($_)->{key}' file.json | ||||||||||||||||||||||||||||||||
| 383 | - Instead of: sed -i 's/old/new/g' file | ||||||||||||||||||||||||||||||||
| 384 | Use: perl -pi -e 's/old/new/g' file | ||||||||||||||||||||||||||||||||
| 385 | - For complex text processing, JSON/YAML parsing, or when shell tools are missing, | ||||||||||||||||||||||||||||||||
| 386 | prefer Perl one-liners as they are portable and powerful. | ||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||
| 388 | Be helpful but safe: | ||||||||||||||||||||||||||||||||
| 389 | - Warn about destructive operations (rm, dd, etc.) | ||||||||||||||||||||||||||||||||
| 390 | - Prefer safe alternatives when possible | ||||||||||||||||||||||||||||||||
| 391 | - Explain what each command does | ||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||
| 393 | Always explain what you're about to do before using tools. | ||||||||||||||||||||||||||||||||
| 394 | PROMPT | ||||||||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||
| 397 | 6 | 1; | |||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||
| 399 | __END__ | ||||||||||||||||||||||||||||||||