blib/lib/Langertha/Raider/CLI/PromptBuilder.pm

filter: Statementstmt 80.3% 45 / 56 Branchbran 66.6% 8 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.7% 6 / 7 Podpod 100% 1 / 1 totaltotal 78.9% 60 / 76 CC 7 SCAR 14.1
CC
7
Cov
78%
run
7.2
__ANON__
2.0
_mission
2.0
CRAP 4.1
« blib/lib/Langertha/Raider/FileTools.pm ↑ Summary blib/lib/Langertha/Raid/Loop.pm »
1 package Langertha::Raider::CLI::PromptBuilder;
2 # ABSTRACT: Internal prompt-builder sub-agent of the raider CLI (/prompt)
3 59 our $VERSION = '0.503';
4 59 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 59 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 59 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 59 ▶use Langertha::Raider;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 59 has app => (
11 is => 'ro',
12 isa => 'Langertha::Raider::CLI',
13 required => 1,
14 );
15  
16 59 has output => (
17 is => 'ro',
18 isa => 'Langertha::Raider::CLI::Output',
19 required => 1,
20 );
21  
22 has in => (
23 is => 'ro',
24 0 ▶ default => sub { \*STDIN },
Subroutine __ANON__: not called CC=1 SCAR=6.9
25 59 );
26  
27 6 ▶sub _mission {
Subroutine _mission: called CC=2 SCAR=6.9
28 6 my ( $self, $file, $label ) = @_;
29 6 ▶ my $current = -f $file ? $file->slurp_utf8 : '(no '.$label.' yet — Langertha default persona is active)';
Branch: 1 branch
BranchTrueFalseTotal
-f $file ? : 5 1 6
30 6 return <<"EOM";
31 You are the raider prompt-builder. Your only job right now is to help the user
32 craft a $label file that customizes the persona and instructions of
33 "raider" (the CLI agent; the default persona is Langertha, a viking
34 shield-maiden).
35  
36 Current $label content:
37 ---
38 $current
39 ---
40  
41 Rules:
42 - Converse naturally with the user. Ask what persona, tone, rules or
43 constraints they want.
44 - When the user is satisfied, use write_file to save to:
45 6 @{[ $file ]}
46 - After writing, confirm what you saved and tell the user they can type
47 "/done" to return to the main agent (the main agent will auto-reload the
48 new persona).
49 - If the user asks you to cancel, do not write anything; just confirm.
50 - Do NOT call bash or any tool other than read_file / write_file / edit_file
51 during this session.
52 EOM
53 }
54  
55  
56 6 ▶sub run {
Subroutine run: called CC=6 SCAR=19.8
Pod documented
57 6 my ($self) = @_;
58 6 my $app = $self->app;
59 6 my $out = $self->output;
60 6 my $instructions = $app->instructions;
61 6 my $file = $instructions->file;
62  
63 6 my $builder_raider = Langertha::Raider->new(
64 engine => $app->_engine,
65 mission => $self->_mission($file, $instructions->label),
66 max_iterations => 20,
67 );
68  
69 6 $out->say_meta('entering prompt-builder. /done to return, /cancel to discard.');
70 6 my $in = $self->in;
71  
72 6 while (1) {
73 7 $out->emit('raider:prompt> ');
74 7 my $line = <$in>;
75 7 ▶ last unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 7 7
76 7 chomp $line;
77 7 $line =~ s/^\s+|\s+$//g;
78 7 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 1 6 7
79  
80 6 ▶ if ($line =~ m{^/(?:done|back|exit|quit)$}i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[^/(?:done|back|exit|quit)$]i) 1 5 6
81 1 my $new = $app->reload_mission;
82 1 $out->say_meta('prompt-builder finished. mission reloaded ('.length($new).' chars).');
83 1 last;
84 }
85 5 ▶ if ($line =~ m{^/cancel$}i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[^/cancel$]i) 5 0 5
86 5 $out->say_meta('prompt-builder cancelled.');
87 5 last;
88 }
89  
90 0 my $f = $builder_raider->raid_f($line);
91 0 $app->loop->await($f);
92 0 my $r = eval { $f->get };
93 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
94 0 my $err = $@; chomp $err;
95 0 $out->say_error($err);
96 0 next;
97 }
98 0 $out->say_agent("$r");
99 }
100 6 return;
101 }
102  
103 59 __PACKAGE__->meta->make_immutable;
104  
105 59 1;
106  
107 __END__
108  
109 =pod
110  
111 =encoding UTF-8
112  
113 =head1 NAME
114  
115 Langertha::Raider::CLI::PromptBuilder - Internal prompt-builder sub-agent of the raider CLI (/prompt)
116  
117 =head1 VERSION
118  
119 version 0.503
120  
121 =head1 SYNOPSIS
122  
123 # Internal to Langertha-Raider -- no API promise.
124 Langertha::Raider::CLI::PromptBuilder->new(app => $app, output => $out)->run;
125  
126 =head1 DESCRIPTION
127  
128 B<Internal module.> Its interface may change without notice.
129  
130 The C</prompt> REPL command and C<--customize-prompt>: a second raider on the
131 same engine whose only job is to write the project instructions file with
132 the user: the one in use (L<Langertha::Raider::Instructions/file>), so
133 F<.raider/instructions.md> when it exists, else F<.raider.md>; it never
134 creates F<.raider/instructions.md>. It reads lines from L</in> until
135 C</done> (reload the mission and return) or C</cancel>.
136  
137 =head2 app
138  
139 The L<Langertha::Raider::CLI> whose instructions file is edited. Required.
140  
141 =head2 output
142  
143 The L<Langertha::Raider::CLI::Output> to print to. Required.
144  
145 =head2 in
146  
147 Filehandle the conversation is read from. Defaults to C<STDIN>.
148  
149 =head2 run
150  
151 Runs the prompt-builder conversation until C</done>, C</cancel> or the end
152 of L</in>.
153  
154 =head1 SEE ALSO
155  
156 =over
157  
158 =item * L<Langertha::Raider::CLI::Commands>
159  
160 =back
161  
162 =head1 SUPPORT
163  
164 =head2 Issues
165  
166 Please report bugs and feature requests on GitHub at
167 L<https://github.com/Getty/langertha-raider/issues>.
168  
169 =head2 IRC
170  
171 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
172  
173 =head1 CONTRIBUTING
174  
175 Contributions are welcome! Please fork the repository and submit a pull request.
176  
177 =head1 AUTHOR
178  
179 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
180  
181 =head1 COPYRIGHT AND LICENSE
182  
183 This software is copyright (c) 2026 by Torsten Raudssus.
184  
185 This is free software; you can redistribute it and/or modify it under
186 the same terms as the Perl 5 programming language system itself.
187  
188 =cut
« blib/lib/Langertha/Raider/FileTools.pm ↑ Summary blib/lib/Langertha/Raid/Loop.pm »