blib/lib/Acme/Claude/Shell.pm

filter: Statementstmt 75.0% 24 / 32 Branchbran 16.6% 1 / 6 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 80.0% 8 / 10 Podpod 100% 2 / 2 totaltotal 39.7% 35 / 88 CC 14 SCAR 35.4
CC
14
Cov
40%
shell
56.0
run
56.0
_detect_color
2.1
CRAP 34.6
« blib/lib/Acme/Claude/Shell/Query.pm ↑ Summary blib/lib/Acme/Claude/Shell/Hooks.pm »
1 package Acme::Claude::Shell;
2  
3 4 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 our @EXPORT_OK = qw(shell run);
9  
10 4 our $VERSION = '0.03';
11  
12 =head1 NAME
13  
14 Acme::Claude::Shell - AI-powered interactive shell using Claude Agent SDK
15  
16 =head1 VERSION
17  
18 Version 0.03
19  
20 =head1 SYNOPSIS
21  
22 use Acme::Claude::Shell qw(shell run);
23  
24 # Interactive session mode (uses session() for multi-turn context)
25 shell();
26  
27 # Single-shot query mode (uses query())
28 my $result = run("find all large log files");
29  
30 # With options
31 shell(
32 dry_run => 1, # Preview mode - show commands without executing
33 safe_mode => 0, # Disable dangerous command warnings
34 colorful => 1, # Force colors (default: auto-detect)
35 );
36  
37 =head1 DESCRIPTION
38  
39 Acme::Claude::Shell is an AI-enhanced interactive shell that showcases
40 all Claude::Agent SDK features:
41  
42 =over 4
43  
44 =item * C<query()> for single-shot commands
45  
46 =item * C<session()> for multi-turn conversations with context
47  
48 =item * SDK MCP tools for shell operations
49  
50 =item * Hooks for safety (confirm destructive commands)
51  
52 =item * Dry-run mode to preview
53  
54 =item * Async with IO::Async
55  
56 =item * CLI utilities (spinners, colored output, menus)
57  
58 =back
59  
60 Describe what you want in natural language, and Claude figures out the
61 shell commands, explains them, and executes them (with your approval).
62  
63 =head1 EXPORTS
64  
65 =head2 shell
66  
67 shell(%options);
68  
69 Start an interactive REPL session. Claude remembers context from previous
70 commands, so you can say things like "now compress those files".
71  
72 Options:
73  
74 dry_run - Preview mode, don't execute commands
75 safe_mode - Confirm dangerous commands (default: 1)
76 working_dir - Starting directory (default: current)
77 colorful - Force colors on/off (default: auto-detect)
78  
79 =head2 run
80  
81 my $result = run($prompt, %options);
82  
83 Execute a single command and return the result. Does not maintain
84 session context between calls.
85  
86 =cut
87  
88 4 ▶use Acme::Claude::Shell::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 4 ▶use Acme::Claude::Shell::Query;
Subroutine BEGIN: called CC=1 SCAR=0.0
90 4 ▶use IO::Async::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
91  
92 0 ▶sub shell {
Subroutine shell: not called CC=7 SCAR=40.3
Pod documented
93 0 my (%args) = @_;
94  
95 0 ▶ my $loop = $args{loop} // IO::Async::Loop->new;
Condition0%0/3
$args{"loop"} // "IO::Async::Loop"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"loop"} // "IO::Async::Loop"->new
$args{"loop"} "IO::Async::Loop"->new
Truth table0%0/3
A // B
A= $args{"loop"}B= "IO::Async::Loop"->new
ABresult
1X1
011
000
96  
97 my $session = Acme::Claude::Shell::Session->new(
98 loop => $loop,
99 dry_run => $args{dry_run} // 0,
100 safe_mode => $args{safe_mode} // 1,
101 working_dir => $args{working_dir} // '.',
102 colorful => $args{colorful} // _detect_color(),
103 0 ▶ ($args{model} ? (model => $args{model}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$args{'model'} ? : 0 0 0
Condition0%0/9
$args{"dry_run"} // 0
l!l
00
$args{"safe_mode"} // 1
l!l
00
$args{"working_dir"} // "."
l!l
00
$args{"colorful"} // _detect_color()
l!l&&r!l&&!r
000
MC/DC0%0/1
$args{"dry_run"} // 0
$args{"dry_run"}
MC/DC0%0/1
$args{"safe_mode"} // 1
$args{"safe_mode"}
MC/DC0%0/1
$args{"working_dir"} // "."
$args{"working_dir"}
MC/DC0%0/2
$args{"colorful"} // _detect_color()
$args{"colorful"} _detect_color()
Truth table0%0/2
A
A= $args{"dry_run"}
Aresult
11
00
Truth table0%0/2
A
A= $args{"safe_mode"}
Aresult
11
00
Truth table0%0/2
A
A= $args{"working_dir"}
Aresult
11
00
Truth table0%0/3
A // B
A= $args{"colorful"}B= _detect_color()
ABresult
1X1
011
000
104 );
105  
106 0 return $session->run->get;
107 }
108  
109 0 ▶sub run {
Subroutine run: not called CC=7 SCAR=40.3
Pod documented
110 0 my ($prompt, %args) = @_;
111  
112 0 ▶ my $loop = $args{loop} // IO::Async::Loop->new;
Condition0%0/3
$args{"loop"} // "IO::Async::Loop"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"loop"} // "IO::Async::Loop"->new
$args{"loop"} "IO::Async::Loop"->new
Truth table0%0/3
A // B
A= $args{"loop"}B= "IO::Async::Loop"->new
ABresult
1X1
011
000
113  
114 my $query = Acme::Claude::Shell::Query->new(
115 loop => $loop,
116 dry_run => $args{dry_run} // 0,
117 safe_mode => $args{safe_mode} // 1,
118 working_dir => $args{working_dir} // '.',
119 colorful => $args{colorful} // _detect_color(),
120 0 ▶ ($args{model} ? (model => $args{model}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$args{'model'} ? : 0 0 0
Condition0%0/9
$args{"dry_run"} // 0
l!l
00
$args{"safe_mode"} // 1
l!l
00
$args{"working_dir"} // "."
l!l
00
$args{"colorful"} // _detect_color()
l!l&&r!l&&!r
000
MC/DC0%0/1
$args{"dry_run"} // 0
$args{"dry_run"}
MC/DC0%0/1
$args{"safe_mode"} // 1
$args{"safe_mode"}
MC/DC0%0/1
$args{"working_dir"} // "."
$args{"working_dir"}
MC/DC0%0/2
$args{"colorful"} // _detect_color()
$args{"colorful"} _detect_color()
Truth table0%0/2
A
A= $args{"dry_run"}
Aresult
11
00
Truth table0%0/2
A
A= $args{"safe_mode"}
Aresult
11
00
Truth table0%0/2
A
A= $args{"working_dir"}
Aresult
11
00
Truth table0%0/3
A // B
A= $args{"colorful"}B= _detect_color()
ABresult
1X1
011
000
121 );
122  
123 0 return $query->run($prompt)->get;
124 }
125  
126 1 ▶sub _detect_color {
Subroutine _detect_color: called CC=2 SCAR=7.6
127 1 ▶ return -t STDOUT ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
-t STDOUT ? : 0 1 1
128 }
129  
130 =head1 EXAMPLE SESSION
131  
132 ============================================================
133 Acme::Claude::Shell
134 ============================================================
135  
136 i AI-powered shell - describe what you want in plain English
137 i Type 'exit' or 'quit' to leave, 'history' for command log
138 ------------------------------------------------------------
139  
140 acme_claude_shell> find all perl files larger than 100k
141 Thinking...
142  
143 I'll find all .pl files over 100KB and display their sizes:
144  
145 i Command: find . -name "*.pl" -size +100k -exec ls -lh {} \;
146  
147 Action:
148 [a] Approve and run
149 [d] Dry-run (show only)
150 [e] Edit command
151 [x] Cancel
152 > a
153  
154 -rw-r--r-- 1 user staff 142K Jan 10 14:23 ./big_script.pl
155  
156 Done
157  
158 acme_claude_shell> now compress that file
159 Thinking...
160  
161 Based on our previous results, I'll compress:
162  
163 i Command: gzip ./big_script.pl
164  
165 Action:
166 [a] Approve and run
167 > a
168  
169 Files compressed successfully
170  
171 =head1 SDK FEATURES DEMONSTRATED
172  
173 This module demonstrates every major feature of the Claude::Agent SDK:
174  
175 =over 4
176  
177 =item B<query()> - Single-shot mode via C<run()>
178  
179 =item B<session()> - Multi-turn context via C<shell()>
180  
181 =item B<SDK MCP Tools> - 6 tools: execute_command, read_file, list_directory, search_files, get_system_info, get_working_directory
182  
183 =item B<Hooks (PreToolUse)> - Audit logging of tool calls
184  
185 =item B<Hooks (PostToolUse)> - Stop spinner, track statistics
186  
187 =item B<Hooks (PostToolUseFailure)> - Graceful error handling
188  
189 =item B<Hooks (Stop)> - Session statistics on exit
190  
191 =item B<Hooks (Notification)> - Event logging (verbose mode)
192  
193 =item B<Dry-run mode> - Preview without executing
194  
195 =item B<IO::Async> - Non-blocking command execution and spinners
196  
197 =item B<CLI utilities> - Spinners, menus, colored output
198  
199 =back
200  
201 B<Note:> Command approval is handled directly in the execute_command tool
202 handler to ensure it happens synchronously before execution.
203  
204 =head1 AUTHOR
205  
206 LNATION, C<< <email at lnation.org> >>
207  
208 =head1 SEE ALSO
209  
210 =over 4
211  
212 =item * L<Claude::Agent> - The underlying Claude Agent SDK
213  
214 =item * L<Claude::Agent::CLI> - Terminal UI utilities
215  
216 =item * L<Acme::Claude::Shell::Session> - Multi-turn session manager
217  
218 =item * L<Acme::Claude::Shell::Query> - Single-shot query mode
219  
220 =item * L<Acme::Claude::Shell::Tools> - SDK MCP tool definitions
221  
222 =item * L<Acme::Claude::Shell::Hooks> - Safety hooks
223  
224 =back
225  
226 =head1 LICENSE AND COPYRIGHT
227  
228 This software is Copyright (c) 2026 by LNATION.
229  
230 This is free software, licensed under:
231  
232 The Artistic License 2.0 (GPL Compatible)
233  
234 =cut
235  
236 4 1;
« blib/lib/Acme/Claude/Shell/Query.pm ↑ Summary blib/lib/Acme/Claude/Shell/Hooks.pm »