CPAN/Maker/Bootstrapper/Role/LLM/ChangeLog.pm untested

filter: Statementstmt 0.0% 0 / 109 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 194 CC 20 SCAR 55.1
CC
20
Cov
0%
cmd_change_log
420.0
BEGIN
2.0
BEGIN
2.0
CRAP 247.9
1 package CPAN::Maker::Bootstrapper::Role::LLM::ChangeLog;
2  
3 use strict;
4 use warnings;
5  
6 use open ':std', ':encoding(UTF-8)';
7  
8 use Archive::Tar;
9 use CLI::Simple::Constants qw(:booleans);
10 use CLI::Simple::Utils qw(slurp choose);
11 use CPAN::Maker::Bootstrapper::Constants qw(:all);
12 use Data::Dumper;
13 use English qw(-no_match_vars);
14 use Git::ReleaseDiffs;
15 use IO::Scalar;
16 use Pod::Extract qw(extract_pod);
17  
18 use Role::Tiny;
19  
20 ########################################################################
21 sub cmd_change_log {
22 ########################################################################
23 my ($self) = @_;
24  
25 eval { require Git::Raw; 1; } or do {
26 die "ERROR: Git::Raw must be installed to use this command.\n";
27 };
28  
29 my ( $version, $api_key ) = $self->get_args;
30  
31 if ( !$version ) {
32 $version = eval { slurp('VERSION'); };
33 if ($version) {
34 chomp $version;
35 }
36 }
37  
38 die "ERROR: usage cpan-maker-bootstrapper change-log [version] [api-key]\n"
39 if !$version;
40  
41 die "ERROR: invalid version\n"
42 if $version !~ /\A\d+[.]\d+[.]\d+\z/xsm;
43  
44 my $output_file = sprintf 'ChangeLog-%s.tmp', $version;
45  
46 die "ERROR: $output_file already exists\n" if -e $output_file;
47  
48 my $llm = $self->_check_llm($api_key);
49  
50 # produce release artifacts from staged changes
51 my $release = Git::ReleaseDiffs->new( release_version => $version );
52 $release->write_diffs;
53 $release->write_list;
54 $release->write_status;
55 $release->write_tarball;
56  
57 # verify artifacts exist
58 my @file_list = map {"release-$version.$_"} qw(diffs lst status tar.gz);
59  
60 foreach my $file (@file_list) {
61 die "ERROR: $file not found or empty!\n"
62 if !-e $file || !-s $file;
63 }
64  
65 # truncate ChangeLog to current release section only (in memory)
66 my $changelog_excerpt = $self->_extract_changelog_section('ChangeLog');
67  
68 my @prompt;
69  
70 if ( !-e '.prompts/change-log.prompt' ) {
71 $self->_install_prompts('change-log.prompt');
72 }
73  
74 my $prompt_text = choose {
75  
76 return slurp('.prompts/change-log.prompt')
77 if -f '.prompts/change-log.prompt';
78  
79 return 'Produce a file-oriented ChangeLog entry for this Perl CPAN distribution release.';
80 };
81  
82 if ( !$self->get_dryrun ) {
83 push @prompt, $llm->text($prompt_text);
84  
85 push @prompt,
86 $llm->document(
87 data => slurp("release-$version.status"),
88 title => 'Git File Status'
89 );
90  
91 push @prompt,
92 $llm->document(
93 data => slurp("release-$version.diffs"),
94 title => 'Release Diffs'
95 );
96  
97 push @prompt,
98 $llm->document(
99 data => slurp("release-$version.lst"),
100 title => 'Changed Files Listing'
101 );
102  
103 if ($changelog_excerpt) {
104 push @prompt,
105 $llm->document(
106 data => $changelog_excerpt,
107 title => 'ChangeLog'
108 );
109 }
110 }
111  
112 # send updated files from tarball, stripping POD from Perl sources
113 my $iter = Archive::Tar->iter( "release-$version.tar.gz", 1 );
114  
115 my $file_limit = $self->get_max_diff_files;
116 my $file_count = 0;
117  
118 while ( my $file = $iter->() ) {
119  
120 next if $file->is_dir;
121 next if $file->name =~ /[.](png|jpg|gif|gz|zip|tar)\z/xsmi;
122  
123 die sprintf "ERROR: max number of files (%s) in diff listing has been reached. Use --max-diff-files to increase\n",
124 $file_limit
125 if $file_limit && $file_count == $file_limit;
126  
127 my $file_content = $file->get_content;
128  
129 next if !defined $file_content || $file_content eq q{};
130  
131 # strip POD from Perl sources to reduce token cost
132 if ( $file->name =~ /[.]p[ml][.]in\z/xsm ) {
133 $file_content = $self->_strip_pod_from_content($file_content);
134 }
135  
136 push @prompt,
137 $llm->document(
138 data => $file_content,
139 title => $file->name
140 ) if !$self->get_dryrun;
141  
142 $file_count++;
143 }
144  
145 return $SUCCESS
146 if $self->get_dryrun;
147  
148 my $llm_rsp = $self->_submit_prompt( $llm, \@prompt );
149  
150 my $content = $llm_rsp->content;
151  
152 if ( !$content || !$content->text ) {
153 print {*STDERR} $llm_rsp->raw_content;
154 return $FAILURE;
155 }
156  
157 if ( $llm_rsp->was_cutoff ) {
158 warn "WARNING: response was truncated (hit max_tokens). Increase --max-tokens for a complete response.\n";
159 }
160  
161 open my $fh, '>', $output_file
162 or die "ERROR: could not open $output_file for writing\n$OS_ERROR";
163  
164 print {$fh} $content->text;
165  
166 close $fh
167 or die "ERROR: could not close $output_file: $OS_ERROR\n";
168  
169 print "Created $output_file\n";
170  
171 $self->_print_token_usage( $llm_rsp, 'ChangeLog: Token Usage Report' );
172  
173 return $SUCCESS;
174 }
175  
176 1;