| 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 Types::Standard qw(InstanceOf Str); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | use Marlin | ||||||||||||||||||||||
| 9 | 5 | ▶ | 'loop!' => InstanceOf['IO::Async::Loop'], | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 10 | 1 | ▶ | 'dry_run?' => sub { 0 }, | ||||||||||||||||||||
|
Subroutine
_build_dry_run: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 11 | 0 | ▶ | 'safe_mode?' => sub { 1 }, | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
Subroutine
_build_safe_mode: called CC=1 SCAR=1.2
| |||||||||||||||||||||||
| 12 | 1 | ▶ | 'working_dir?' => sub { '.' }, | ||||||||||||||||||||
|
Subroutine
_build_working_dir: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 13 | 1 | ▶ | 'colorful?' => sub { 1 }, | ||||||||||||||||||||
|
Subroutine
_build_colorful: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 14 | 5 | 'model?' => Str, | |||||||||||||||||||||
| 15 | 5 | ▶ | '_spinner==.'; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 16 | |||||||||||||||||||||||
| 17 | 5 | ▶ | use Claude::Agent qw(query create_sdk_mcp_server); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 18 | 5 | ▶ | use Claude::Agent::Options; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 19 | 5 | ▶ | use Claude::Agent::CLI qw( | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 20 | start_spinner stop_spinner | ||||||||||||||||||||||
| 21 | header divider status | ||||||||||||||||||||||
| 22 | 5 | ); | |||||||||||||||||||||
| 23 | 5 | ▶ | use Acme::Claude::Shell::Tools qw(shell_tools); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 24 | 5 | ▶ | use Acme::Claude::Shell::Hooks qw(safety_hooks); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 25 | 5 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 26 | |||||||||||||||||||||||
| 27 | # Fun spinner styles with matching colors | ||||||||||||||||||||||
| 28 | 5 | my @SPINNERS = ( | |||||||||||||||||||||
| 29 | { spinner => 'moon', spinner_color => 'yellow' }, | ||||||||||||||||||||||
| 30 | { spinner => 'earth', spinner_color => 'cyan' }, | ||||||||||||||||||||||
| 31 | { spinner => 'clock', spinner_color => 'blue' }, | ||||||||||||||||||||||
| 32 | { spinner => 'dots', spinner_color => 'magenta' }, | ||||||||||||||||||||||
| 33 | { spinner => 'material', spinner_color => 'green' }, | ||||||||||||||||||||||
| 34 | { spinner => 'circle_half', spinner_color => 'cyan' }, | ||||||||||||||||||||||
| 35 | { spinner => 'color_circles', spinner_color => 'white' }, | ||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||
| 37 | |||||||||||||||||||||||
| 38 | 0 | ▶ | sub _random_spinner { | ||||||||||||||||||||
|
Subroutine
_random_spinner: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 39 | 0 | return %{ $SPINNERS[rand @SPINNERS] }; | |||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||
| 41 | |||||||||||||||||||||||
| 42 | =head1 NAME | ||||||||||||||||||||||
| 43 | |||||||||||||||||||||||
| 44 | Acme::Claude::Shell::Query - Single-shot query mode for Acme::Claude::Shell | ||||||||||||||||||||||
| 45 | |||||||||||||||||||||||
| 46 | =head1 SYNOPSIS | ||||||||||||||||||||||
| 47 | |||||||||||||||||||||||
| 48 | use Acme::Claude::Shell::Query; | ||||||||||||||||||||||
| 49 | use IO::Async::Loop; | ||||||||||||||||||||||
| 50 | |||||||||||||||||||||||
| 51 | my $loop = IO::Async::Loop->new; | ||||||||||||||||||||||
| 52 | |||||||||||||||||||||||
| 53 | my $query = Acme::Claude::Shell::Query->new( | ||||||||||||||||||||||
| 54 | loop => $loop, | ||||||||||||||||||||||
| 55 | dry_run => 0, | ||||||||||||||||||||||
| 56 | safe_mode => 1, | ||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||
| 58 | |||||||||||||||||||||||
| 59 | my $result = $query->run("find all large log files")->get; | ||||||||||||||||||||||
| 60 | |||||||||||||||||||||||
| 61 | =head1 DESCRIPTION | ||||||||||||||||||||||
| 62 | |||||||||||||||||||||||
| 63 | Executes a single natural language command using Claude's query() function. | ||||||||||||||||||||||
| 64 | Does not maintain session context between calls - each run() is independent. | ||||||||||||||||||||||
| 65 | |||||||||||||||||||||||
| 66 | This is useful for scripting or when you want a one-shot command without | ||||||||||||||||||||||
| 67 | starting an interactive session. | ||||||||||||||||||||||
| 68 | |||||||||||||||||||||||
| 69 | Uses Claude::Agent SDK features: | ||||||||||||||||||||||
| 70 | |||||||||||||||||||||||
| 71 | =over 4 | ||||||||||||||||||||||
| 72 | |||||||||||||||||||||||
| 73 | =item * C<query()> - Single-shot prompt execution | ||||||||||||||||||||||
| 74 | |||||||||||||||||||||||
| 75 | =item * SDK MCP tools - execute_command, read_file, list_directory, search_files, get_system_info, get_working_directory | ||||||||||||||||||||||
| 76 | |||||||||||||||||||||||
| 77 | =item * Hooks - PreToolUse (audit), PostToolUse (stats), PostToolUseFailure (errors), Stop (statistics), Notification (logging) | ||||||||||||||||||||||
| 78 | |||||||||||||||||||||||
| 79 | =item * CLI utilities - Spinners and colored output | ||||||||||||||||||||||
| 80 | |||||||||||||||||||||||
| 81 | =back | ||||||||||||||||||||||
| 82 | |||||||||||||||||||||||
| 83 | =head2 Attributes | ||||||||||||||||||||||
| 84 | |||||||||||||||||||||||
| 85 | =over 4 | ||||||||||||||||||||||
| 86 | |||||||||||||||||||||||
| 87 | =item * C<loop> (required) - IO::Async::Loop instance | ||||||||||||||||||||||
| 88 | |||||||||||||||||||||||
| 89 | =item * C<dry_run> - Preview mode, don't execute commands (default: 0) | ||||||||||||||||||||||
| 90 | |||||||||||||||||||||||
| 91 | =item * C<safe_mode> - Confirm dangerous commands (default: 1) | ||||||||||||||||||||||
| 92 | |||||||||||||||||||||||
| 93 | =item * C<working_dir> - Starting directory (default: '.') | ||||||||||||||||||||||
| 94 | |||||||||||||||||||||||
| 95 | =item * C<colorful> - Use colored output (default: 1) | ||||||||||||||||||||||
| 96 | |||||||||||||||||||||||
| 97 | =item * C<model> - Claude model to use (optional) | ||||||||||||||||||||||
| 98 | |||||||||||||||||||||||
| 99 | =back | ||||||||||||||||||||||
| 100 | |||||||||||||||||||||||
| 101 | =head2 Methods | ||||||||||||||||||||||
| 102 | |||||||||||||||||||||||
| 103 | =over 4 | ||||||||||||||||||||||
| 104 | |||||||||||||||||||||||
| 105 | =item * C<run($prompt)> - Execute a single prompt and return a Future | ||||||||||||||||||||||
| 106 | |||||||||||||||||||||||
| 107 | =back | ||||||||||||||||||||||
| 108 | |||||||||||||||||||||||
| 109 | =cut | ||||||||||||||||||||||
| 110 | |||||||||||||||||||||||
| 111 | 0 | ▶ | async sub run { | ||||||||||||||||||||
|
Subroutine
run: not called CC=17 SCAR=57.2
Pod
documented
| |||||||||||||||||||||||
| 112 | 0 | my ($self, $prompt) = @_; | |||||||||||||||||||||
| 113 | |||||||||||||||||||||||
| 114 | # Show header | ||||||||||||||||||||||
| 115 | 0 | ▶ | if ($self->colorful) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 116 | 0 | header("Acme::Claude::Shell - Query Mode"); | |||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||
| 118 | |||||||||||||||||||||||
| 119 | # Create SDK MCP server with shell tools | ||||||||||||||||||||||
| 120 | 0 | my $mcp = create_sdk_mcp_server( | |||||||||||||||||||||
| 121 | name => 'shell-tools', | ||||||||||||||||||||||
| 122 | tools => shell_tools($self), | ||||||||||||||||||||||
| 123 | ); | ||||||||||||||||||||||
| 124 | |||||||||||||||||||||||
| 125 | # Create options with hooks | ||||||||||||||||||||||
| 126 | 0 | ▶ | my $options = Claude::Agent::Options->new( | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 127 | permission_mode => 'bypassPermissions', | ||||||||||||||||||||||
| 128 | mcp_servers => { 'shell-tools' => $mcp }, | ||||||||||||||||||||||
| 129 | hooks => safety_hooks($self), | ||||||||||||||||||||||
| 130 | dry_run => $self->dry_run, | ||||||||||||||||||||||
| 131 | system_prompt => $self->_system_prompt, | ||||||||||||||||||||||
| 132 | ($self->has_model ? (model => $self->model) : ()), | ||||||||||||||||||||||
| 133 | ); | ||||||||||||||||||||||
| 134 | |||||||||||||||||||||||
| 135 | # Store spinner in self so hooks can stop it before reading STDIN | ||||||||||||||||||||||
| 136 | # Pick a random fun spinner each time | ||||||||||||||||||||||
| 137 | 0 | $self->_spinner(start_spinner("Thinking...", $self->loop, | |||||||||||||||||||||
| 138 | _random_spinner(), | ||||||||||||||||||||||
| 139 | )); | ||||||||||||||||||||||
| 140 | |||||||||||||||||||||||
| 141 | # Execute query | ||||||||||||||||||||||
| 142 | 0 | my $iter = query( | |||||||||||||||||||||
| 143 | prompt => $prompt, | ||||||||||||||||||||||
| 144 | options => $options, | ||||||||||||||||||||||
| 145 | loop => $self->loop, | ||||||||||||||||||||||
| 146 | ); | ||||||||||||||||||||||
| 147 | |||||||||||||||||||||||
| 148 | 0 | my $response_text = ''; | |||||||||||||||||||||
| 149 | 0 | my $result; | |||||||||||||||||||||
| 150 | |||||||||||||||||||||||
| 151 | 0 | ▶ | while (my $msg = await $iter->next_async) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 152 | 0 | ▶ | if ($msg->isa('Claude::Agent::Message::Assistant')) { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 153 | 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
| |||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||
| 155 | elsif ($msg->isa('Claude::Agent::Message::ToolUse')) { | ||||||||||||||||||||||
| 156 | # Spinner already stopped by hook before STDIN read | ||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||
| 158 | elsif ($msg->isa('Claude::Agent::Message::ToolResult')) { | ||||||||||||||||||||||
| 159 | # Show result | ||||||||||||||||||||||
| 160 | 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
| |||||||||||||||||||||||
| 161 | 0 | ▶ | if ($msg->is_error) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 162 | # Check if this was a user denial (dry-run, cancel, etc.) | ||||||||||||||||||||||
| 163 | # In that case, don't show error and stop processing | ||||||||||||||||||||||
| 164 | 0 | ▶ | if ($content =~ /^(Dry-run:|User cancelled)/) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 165 | 0 | last; # Stop the conversation here | |||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||
| 167 | 0 | ▶ | status('error', $content) if $self->colorful; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 168 | } else { | ||||||||||||||||||||||
| 169 | 0 | ▶ | print $content, "\n" if $content; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||
| 171 | # Don't restart spinner - avoids conflicts with Term::ProgressSpinner | ||||||||||||||||||||||
| 172 | # when STDIN was used for hook confirmation | ||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||
| 174 | elsif ($msg->isa('Claude::Agent::Message::Result')) { | ||||||||||||||||||||||
| 175 | 0 | $result = $msg; | |||||||||||||||||||||
| 176 | 0 | last; | |||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||
| 179 | |||||||||||||||||||||||
| 180 | # Stop spinner | ||||||||||||||||||||||
| 181 | 0 | ▶ | stop_spinner($self->_spinner, "Done") if $self->_spinner; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 182 | 0 | $self->_spinner(undef); | |||||||||||||||||||||
| 183 | |||||||||||||||||||||||
| 184 | # Show response | ||||||||||||||||||||||
| 185 | 0 | ▶ | if ($response_text) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 186 | 0 | print "\n", $response_text, "\n"; | |||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||
| 188 | |||||||||||||||||||||||
| 189 | # Cleanup | ||||||||||||||||||||||
| 190 | 0 | ▶ | $iter->cleanup if $iter->can('cleanup'); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 191 | |||||||||||||||||||||||
| 192 | 0 | return $result; | |||||||||||||||||||||
| 193 | 5 | } | |||||||||||||||||||||
| 194 | |||||||||||||||||||||||
| 195 | 0 | ▶ | sub _system_prompt { | ||||||||||||||||||||
|
Subroutine
_system_prompt: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 196 | 0 | my ($self) = @_; | |||||||||||||||||||||
| 197 | 0 | return <<'PROMPT'; | |||||||||||||||||||||
| 198 | You are an AI shell assistant. The user describes a task in natural language, | ||||||||||||||||||||||
| 199 | and you translate it into shell commands. | ||||||||||||||||||||||
| 200 | |||||||||||||||||||||||
| 201 | When the user asks you to do something: | ||||||||||||||||||||||
| 202 | 1. Explain what command(s) you'll run and why | ||||||||||||||||||||||
| 203 | 2. Use the execute_command tool to run them | ||||||||||||||||||||||
| 204 | 3. Summarize the results | ||||||||||||||||||||||
| 205 | |||||||||||||||||||||||
| 206 | PERL FALLBACK: When a task cannot be done with standard shell commands, | ||||||||||||||||||||||
| 207 | or when a shell command isn't available on the system, use Perl one-liners instead. | ||||||||||||||||||||||
| 208 | Perl is always available. Examples: | ||||||||||||||||||||||
| 209 | - Instead of: jq '.key' file.json | ||||||||||||||||||||||
| 210 | Use: perl -MJSON -0777 -ne 'print decode_json($_)->{key}' file.json | ||||||||||||||||||||||
| 211 | - Instead of: sed -i 's/old/new/g' file | ||||||||||||||||||||||
| 212 | Use: perl -pi -e 's/old/new/g' file | ||||||||||||||||||||||
| 213 | - For complex text processing, JSON/YAML parsing, or when shell tools are missing, | ||||||||||||||||||||||
| 214 | prefer Perl one-liners as they are portable and powerful. | ||||||||||||||||||||||
| 215 | |||||||||||||||||||||||
| 216 | Be helpful but safe - warn about destructive operations. | ||||||||||||||||||||||
| 217 | Always explain what you're about to do before using tools. | ||||||||||||||||||||||
| 218 | PROMPT | ||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||
| 220 | |||||||||||||||||||||||
| 221 | =head1 AUTHOR | ||||||||||||||||||||||
| 222 | |||||||||||||||||||||||
| 223 | LNATION, C<< <email at lnation.org> >> | ||||||||||||||||||||||
| 224 | |||||||||||||||||||||||
| 225 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||
| 226 | |||||||||||||||||||||||
| 227 | This software is Copyright (c) 2026 by LNATION. | ||||||||||||||||||||||
| 228 | |||||||||||||||||||||||
| 229 | This is free software, licensed under: | ||||||||||||||||||||||
| 230 | |||||||||||||||||||||||
| 231 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||||||||||
| 232 | |||||||||||||||||||||||
| 233 | =cut | ||||||||||||||||||||||
| 234 | |||||||||||||||||||||||
| 235 | 5 | 1; | |||||||||||||||||||||