| 1 | package Acme::Claude::Shell::Query; | ||||||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Object::Proto (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Claude::Agent qw(query create_sdk_mcp_server); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Claude::Agent::Options; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Claude::Agent::Content; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use Claude::Agent::CLI qw( | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | start_spinner stop_spinner | ||||||||||||||||||||||||||||||||
| 13 | header divider status | ||||||||||||||||||||||||||||||||
| 14 | 5 | ); | |||||||||||||||||||||||||||||||
| 15 | 5 | ▶ | use Acme::Claude::Shell::Tools qw(shell_tools); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | 5 | ▶ | use Acme::Claude::Shell::Hooks qw(safety_hooks); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 17 | 5 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | 5 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 20 | 5 | Object::Proto::define('Acme::Claude::Shell::Query', | |||||||||||||||||||||||||||||||
| 21 | 'loop:Object:required', | ||||||||||||||||||||||||||||||||
| 22 | 'dry_run:Bool:default(0):predicate', | ||||||||||||||||||||||||||||||||
| 23 | 'safe_mode:Bool:default(1):predicate', | ||||||||||||||||||||||||||||||||
| 24 | 'working_dir:Str:default(.):predicate', | ||||||||||||||||||||||||||||||||
| 25 | 'colorful:Bool:default(1):predicate', | ||||||||||||||||||||||||||||||||
| 26 | 'verbose:Bool:default(0):predicate', | ||||||||||||||||||||||||||||||||
| 27 | 'audit_log:Bool:default(0):predicate', | ||||||||||||||||||||||||||||||||
| 28 | 'model:Str:predicate', | ||||||||||||||||||||||||||||||||
| 29 | 'effort:Str:predicate', | ||||||||||||||||||||||||||||||||
| 30 | '_history:ArrayRef:default([])', | ||||||||||||||||||||||||||||||||
| 31 | '_spinner', | ||||||||||||||||||||||||||||||||
| 32 | '_session_start', | ||||||||||||||||||||||||||||||||
| 33 | '_tool_count:Int:default(0)', | ||||||||||||||||||||||||||||||||
| 34 | '_tool_errors:Int:default(0)', | ||||||||||||||||||||||||||||||||
| 35 | '_audit_log:ArrayRef:default([])', | ||||||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub options { | ||||||||||||||||||||||||||||||
|
Subroutine
options: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 54 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | 0 | my $mcp = create_sdk_mcp_server( | |||||||||||||||||||||||||||||||
| 57 | name => 'shell-tools', | ||||||||||||||||||||||||||||||||
| 58 | tools => shell_tools($self), | ||||||||||||||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | return Claude::Agent::Options->new( | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 62 | permission_mode => 'bypassPermissions', | ||||||||||||||||||||||||||||||||
| 63 | tools => [], | ||||||||||||||||||||||||||||||||
| 64 | mcp_servers => { 'shell-tools' => $mcp }, | ||||||||||||||||||||||||||||||||
| 65 | hooks => safety_hooks($self), | ||||||||||||||||||||||||||||||||
| 66 | dry_run => $self->dry_run, | ||||||||||||||||||||||||||||||||
| 67 | system_prompt => $self->_system_prompt, | ||||||||||||||||||||||||||||||||
| 68 | ($self->has_model ? (model => $self->model) : ()), | ||||||||||||||||||||||||||||||||
| 69 | ($self->has_effort ? (effort => $self->effort) : ()), | ||||||||||||||||||||||||||||||||
| 70 | ); | ||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | async sub run { | ||||||||||||||||||||||||||||||
|
Subroutine
run: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 74 | 0 | my ($self, $prompt) = @_; | |||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if ($self->colorful) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 77 | 0 | header("Acme::Claude::Shell - Query Mode"); | |||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | $self->_spinner(start_spinner("Thinking...", $self->loop, _random_spinner())) if $self->colorful; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | 0 | my $iter = query( | |||||||||||||||||||||||||||||||
| 83 | prompt => $prompt, | ||||||||||||||||||||||||||||||||
| 84 | options => $self->options, | ||||||||||||||||||||||||||||||||
| 85 | loop => $self->loop, | ||||||||||||||||||||||||||||||||
| 86 | ); | ||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | 0 | my $response_text = ''; | |||||||||||||||||||||||||||||||
| 89 | 0 | my $result; | |||||||||||||||||||||||||||||||
| 90 | my %tool_names; | ||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | while (my $msg = await $iter->next_async) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if ($msg->isa('Claude::Agent::Message::Assistant')) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | $response_text .= $msg->text // ''; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$msg->text // ""
MC/DC0%0/1
$msg->text // ""
$msg->text
Truth table0%0/2
A
A= $msg->text
| |||||||||||||||||||||||||||||||||
| 95 | 0 | $tool_names{ $_->id } = $_->name for @{ $msg->tool_uses }; | |||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||
| 97 | elsif ($msg->isa('Claude::Agent::Message::User')) { | ||||||||||||||||||||||||||||||||
| 98 | 0 | $self->_show_tool_results($msg, \%tool_names); | |||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||
| 100 | elsif ($msg->isa('Claude::Agent::Message::Result')) { | ||||||||||||||||||||||||||||||||
| 101 | 0 | $result = $msg; | |||||||||||||||||||||||||||||||
| 102 | 0 | last; | |||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | stop_spinner($self->_spinner, "Done") if $self->_spinner; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 107 | 0 | $self->_spinner(undef); | |||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | if (length $response_text) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 110 | 0 | print "\n", $response_text, "\n"; | |||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | 0 | $iter->close; | |||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | 0 | return $result; | |||||||||||||||||||||||||||||||
| 116 | 5 | } | |||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | sub _show_tool_results { | ||||||||||||||||||||||||||||||
|
Subroutine
_show_tool_results: not called CC=16 SCAR=56.1
| |||||||||||||||||||||||||||||||||
| 119 | 0 | my ($self, $msg, $tool_names) = @_; | |||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | 0 | my $content = $msg->message->{content}; | |||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | return unless ref $content eq 'ARRAY'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | 0 | for my $block (@$content) { | |||||||||||||||||||||||||||||||
| 125 | 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"}
| |||||||||||||||||||||||||||||||||
| 126 | 0 | my $result = Claude::Agent::Content->from_json($block); | |||||||||||||||||||||||||||||||
| 127 | 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")
| |||||||||||||||||||||||||||||||||
| 128 | 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}
| |||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | next unless $name =~ /execute_command$/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 130 | 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
| |||||||||||||||||||||||||||||||||
| 131 | 0 | ▶ | if ($result->is_error) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | next if $text =~ /^(Dry-run:|User cancelled)/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | status('error', $text) if $self->colorful; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||
| 135 | elsif (length $text) { | ||||||||||||||||||||||||||||||||
| 136 | 0 | print $text; | |||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | print "\n" unless $text =~ /\n\z/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||
| 140 | 0 | return; | |||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | 0 | ▶ | sub _system_prompt { | ||||||||||||||||||||||||||||||
|
Subroutine
_system_prompt: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 144 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 145 | 0 | return <<'PROMPT'; | |||||||||||||||||||||||||||||||
| 146 | You are an AI shell assistant. The user describes a task in natural language, | ||||||||||||||||||||||||||||||||
| 147 | and you translate it into shell commands. | ||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | When the user asks you to do something: | ||||||||||||||||||||||||||||||||
| 150 | 1. Explain what command(s) you'll run and why | ||||||||||||||||||||||||||||||||
| 151 | 2. Use the execute_command tool to run them | ||||||||||||||||||||||||||||||||
| 152 | 3. Summarize the results | ||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||
| 154 | PERL FALLBACK: When a task cannot be done with standard shell commands, | ||||||||||||||||||||||||||||||||
| 155 | or when a shell command isn't available on the system, use Perl one-liners instead. | ||||||||||||||||||||||||||||||||
| 156 | Perl is always available. Examples: | ||||||||||||||||||||||||||||||||
| 157 | - Instead of: jq '.key' file.json | ||||||||||||||||||||||||||||||||
| 158 | Use: perl -MJSON -0777 -ne 'print decode_json($_)->{key}' file.json | ||||||||||||||||||||||||||||||||
| 159 | - Instead of: sed -i 's/old/new/g' file | ||||||||||||||||||||||||||||||||
| 160 | Use: perl -pi -e 's/old/new/g' file | ||||||||||||||||||||||||||||||||
| 161 | - For complex text processing, JSON/YAML parsing, or when shell tools are missing, | ||||||||||||||||||||||||||||||||
| 162 | prefer Perl one-liners as they are portable and powerful. | ||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||
| 164 | Be helpful but safe - warn about destructive operations. | ||||||||||||||||||||||||||||||||
| 165 | Always explain what you're about to do before using tools. | ||||||||||||||||||||||||||||||||
| 166 | PROMPT | ||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||
| 169 | 5 | 1; | |||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||
| 171 | __END__ | ||||||||||||||||||||||||||||||||