File Coverage

blib/lib/App/Chit/Util.pm
Criterion Covered Total %
statement 32 73 43.8
branch 0 10 0.0
condition 0 10 0.0
subroutine 11 16 68.7
pod 0 5 0.0
total 43 114 37.7


line stmt bran cond sub pod time code
1 1     1   15 use v5.20;
  1         5  
2 1     1   7 use strict;
  1         2  
  1         32  
3 1     1   5 use warnings;
  1         2  
  1         94  
4 1     1   8 use experimental qw( signatures lexical_subs postderef );
  1         2  
  1         9  
5              
6             package App::Chit::Util;
7              
8 1     1   865 use AI::Chat;
  1         98118  
  1         56  
9 1     1   10 use Carp qw( croak );
  1         2  
  1         74  
10 1     1   8 use Cwd qw( getcwd );
  1         1  
  1         59  
11 1     1   1079 use Path::Tiny qw( path );
  1         19348  
  1         138  
12 1     1   658 use YAML::XS qw( LoadFile DumpFile );
  1         4139  
  1         91  
13              
14 1     1   40 use constant CHIT_FILENAME => '.chit.yml';
  1         2  
  1         78  
15 1     1   7 use constant CHIT_KEY_VAR => 'OPENAI_API_KEY';
  1         2  
  1         1012  
16              
17             our $AUTHORITY = 'cpan:TOBYINK';
18             our $VERSION = '0.001001';
19              
20 0     0 0   sub is_chit_dir ( $dir ) {
  0            
  0            
21 0           my $path = path( $dir );
22 0           my $file = $path->child( CHIT_FILENAME );
23 0 0         croak "Chit config file was unexpectedly a directory: $file" if $file->is_dir;
24 0           return $file->is_file;
25             }
26              
27 0     0 0   sub find_chit_dir ( $starting_from=getcwd() ) {
  0            
  0            
28 0           my $path = path( $starting_from );
29 0           while ( not is_chit_dir( $path ) ) {
30 0 0         return undef if $path->is_rootdir;
31 0           $path = $path->parent;
32             }
33 0 0         is_chit_dir( $path ) ? $path : undef;
34             }
35              
36 0     0 0   sub load_chit ( $dir ) {
  0            
  0            
37 0           my $path = path( $dir );
38 0 0         is_chit_dir( $path ) or croak "Not a chit dir: $dir";
39 0           my ( $chit ) = LoadFile( $path->child( CHIT_FILENAME )->stringify );
40 0   0       $chit->{model} //= 'gpt-4o-mini';
41 0   0       $chit->{temperature} //= 0.99;
42 0   0       $chit->{role} //= "You are a helpful assistant, valued for your precise, accurate, and concise answers.";
43 0   0       $chit->{history} //= 100;
44 0           return $chit;
45             }
46              
47 0     0 0   sub save_chit ( $dir, $chit ) {
  0            
  0            
  0            
48 0           my $path = path( $dir );
49 0 0         if ( $chit->{chat} ) {
50 0   0       $chit->{history} //= 100;
51 0           shift @{ $chit->{chat} } while @{ $chit->{chat} } > $chit->{history};
  0            
  0            
52             }
53 0           return DumpFile( $path->child( CHIT_FILENAME )->stringify, $chit );
54             }
55              
56 0     0 0   sub chatgpt ( $chit ) {
  0            
  0            
57             return AI::Chat->new(
58             key => $ENV{ CHIT_KEY_VAR() },
59             api => 'OpenAI',
60             model => $chit->{model},
61             role => $chit->{role},
62 0           );
63             }
64              
65             1;
66