Saisons/Adapter/Gemini.pm

filter: Statementstmt 89.2% 91 / 102 Branchbran 60.0% 36 / 60 Conditioncond 36.8% 14 / 38 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 82.3% 14 / 17 Podpod 0.0% 0 / 9 totaltotal 62.7% 155 / 247 CC 50 SCAR 31.6
CC
50
Cov
70%
_parse_session
52.0
load_messages
13.5
_extract_text
10.7
CRAP 23.7
« Saisons/Launcher.pm ↑ Summary Saisons/Adapter/Codex.pm »
1 package App::Saisons::Adapter::Gemini;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Time::Local ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use App::Saisons ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use App::Saisons::Launcher ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶sub new { bless {}, shift }
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
9 1 ▶sub name { 'Gemini' }
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
10 2 ▶sub tag { "\x{2726} Gem" }
Subroutine tag: called CC=1 SCAR=0.0
Pod undocumented
11 1 ▶sub tag_color { 'bold blue' }
Subroutine tag_color: called CC=1 SCAR=0.0
Pod undocumented
12 1 ▶sub find_sessions {
Subroutine find_sessions: called CC=10 SCAR=23.4
Pod undocumented
13 1 my ($self) = @_;
14 1 my $tmp = "$ENV{HOME}/.gemini/tmp";
15 1 ▶ return () unless -d $tmp;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $tmp 0 1 1
16 1 my @sessions;
17 1 ▶ opendir my $dh, $tmp or return ();
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $tmp or return () 1 0 1
18 1 for my $slug (readdir $dh) {
19 3 ▶ next if $slug =~ /^\./;
Branch: 1 branch
BranchTrueFalseTotal
if $slug =~ /^\./ 2 1 3
20 1 my $chats_dir = "$tmp/$slug/chats";
21 1 ▶ next unless -d $chats_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $chats_dir 0 1 1
22 1 ▶ opendir my $cdh, $chats_dir or next;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $cdh, $chats_dir or next 1 0 1
23 1 for my $file (readdir $cdh) {
24 3 ▶ next unless $file =~ /^session-.*\.jsonl$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /^session-.*\.jsonl$/ 2 1 3
25 1 ▶ my $s = _parse_session("$chats_dir/$file", $self) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $s = _parse_session("$chats_dir/$file", $self) or next 1 0 1
26 1 push @sessions, $s;
27 }
28 1 closedir $cdh;
29 }
30 1 closedir $dh;
31 1 return @sessions;
32 }
33 0 ▶sub running {
Subroutine running: not called CC=1 SCAR=6.9
Pod undocumented
34 0 return ();
35 }
36 0 ▶sub launch {
Subroutine launch: not called CC=2 SCAR=17.9
Pod undocumented
37 0 my ($self, $sessions, $launcher) = @_;
38 0 for my $s (@$sessions) {
39 0 my $cmd = "cd \Q$s->{cwd}\E && gemini --resume \Q$s->{id}\E";
40 0 App::Saisons::Launcher::launch_cmd($cmd, $s->{cwd}, $launcher, $s->{title});
41 }
42 }
43 0 ▶sub delete_session {
Subroutine delete_session: not called CC=1 SCAR=6.9
Pod undocumented
44 0 my ($self, $session) = @_;
45 0 return unlink $session->{_file};
46 }
47 1 ▶sub load_messages {
Subroutine load_messages: called CC=12 SCAR=26.0
Pod undocumented
48 1 my ($self, $session) = @_;
49 1 my @messages;
50 1 ▶ open my $fh, '<:utf8', $session->{_file} or return @messages;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:utf8", $session->{"_file"} or return @messages 1 0 1
51 1 my $first = 1;
52 1 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 3 1 4
53 3 ▶ if ($first) { $first = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($first) 1 2 3
54 2 ▶ my $obj = eval { JSON::PP::decode_json($line) } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $obj = eval { do { JSON::PP::decode_json($line) } } or next 2 0 2
55 2 ▶ my $type = $obj->{type} // '';
Condition50%1/2
$obj->{"type"} // ""
l!l
20
MC/DC0%0/1
$obj->{"type"} // ""
$obj->{"type"}
Truth table50%1/2
A
A= $obj->{"type"}
Aresult
11
00
56 2 ▶ next unless $type eq 'user' || $type eq 'gemini';
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "user" or $type eq "gemini" 0 2 2
Condition66%2/3
$type eq "user" or $type eq "gemini"
l!l&&r!l&&!r
110
MC/DC0%0/2
$type eq "user" or $type eq "gemini"
$type eq "user" $type eq "gemini"
Truth table66%2/3
A or B
A= $type eq "user"B= $type eq "gemini"
ABresult
1X1
011
000
57 2 ▶ my $role = $type eq 'user' ? 'user' : 'assistant';
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'user' ? : 1 1 2
58 2 ▶ my $text = $obj->{displayContent} // _extract_text($obj->{content});
Condition33%1/3
$obj->{"displayContent"} // _extract_text($obj->{"content"})
l!l&&r!l&&!r
020
MC/DC0%0/2
$obj->{"displayContent"} // _extract_text($obj->{"content"})
$obj->{"displayContent"} _extract_text($obj->{"content"})
Truth table33%1/3
A // B
A= $obj->{"displayContent"}B= _extract_text($obj->{"content"})
ABresult
1X1
011
000
59 2 ▶ push @messages, { role => $role, text => $text } if length($text // '');
Branch: 1 branch
BranchTrueFalseTotal
if length($text // "") 2 0 2
Condition50%1/2
$text // ""
l!l
20
MC/DC0%0/1
$text // ""
$text
Truth table50%1/2
A
A= $text
Aresult
11
00
60 }
61 1 close $fh;
62 1 return @messages;
63 }
64 1 ▶sub _parse_session {
Subroutine _parse_session: called CC=23 SCAR=39.5
65 1 my ($file, $adapter) = @_;
66 1 ▶ open my $fh, '<:utf8', $file or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:utf8", $file or return undef 1 0 1
67 1 my ($id, $title, $date_str, $epoch, $cwd);
68 1 my $first = 1;
69 1 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 3 1 4
70 3 ▶ my $obj = eval { JSON::PP::decode_json($line) } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $obj = eval { do { JSON::PP::decode_json($line) } } or next 3 0 3
71 3 ▶ if ($first) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first) 1 2 3
72 1 $first = 0;
73 1 ▶ $id = $obj->{sessionId} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$id = $obj->{"sessionId"} or return undef 1 0 1
74 1 ▶ $title = $obj->{summary} if $obj->{summary};
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"summary"} 1 0 1
75 1 ▶ if (my $ts = $obj->{startTime} // $obj->{lastUpdated}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ts = $obj->{"startTime"} // $obj->{"lastUpdated"}) 1 0 1
Condition33%1/3
$obj->{"startTime"} // $obj->{"lastUpdated"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$obj->{"startTime"} // $obj->{"lastUpdated"}
$obj->{"startTime"} $obj->{"lastUpdated"}
Truth table33%1/3
A // B
A= $obj->{"startTime"}B= $obj->{"lastUpdated"}
ABresult
1X1
011
000
76 1 ▶ if ($ts =~ /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ts =~ /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/) 1 0 1
77 1 $date_str = "$1-$2-$3T$4:$5:$6";
78 # not POSIX::tzset: it is unimplemented on Windows
79 1 $epoch = Time::Local::timegm($6, $5, $4, $3, $2 - 1, $1);
80 }
81 }
82 1 $cwd = $ENV{HOME};
83 1 ▶ if (ref $obj->{directories} eq 'ARRAY' && @{ $obj->{directories} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj->{"directories"} eq "ARRAY" and @{$obj->{"directories"};}) 1 0 1
Condition33%1/3
ref $obj->{"directories"} eq "ARRAY" and @{$obj->{"directories"};}
!ll&&!rl&&r
001
MC/DC0%0/2
ref $obj->{"directories"} eq "ARRAY" and @{$obj->{"directories"};}
ref $obj->{"directories"} eq "ARRAY" @{$obj->{"directories"};}
Truth table33%1/3
A and B
A= ref $obj->{"directories"} eq "ARRAY"B= @{$obj->{"directories"};}
ABresult
0X0
100
111
84 1 $cwd = $obj->{directories}[0];
85 }
86 1 next;
87 }
88 2 ▶ if (!$title && ($obj->{type} // '') eq 'user') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $title and ($obj->{"type"} // "") eq "user") 0 2 2
Condition20%1/5
not $title and ($obj->{"type"} // "") eq "user"
!ll&&!rl&&r
200
$obj->{"type"} // ""
l!l
00
MC/DC0%0/2
not $title and ($obj->{"type"} // "") eq "user"
not $title $obj->{"type"}
Truth table33%1/3
not(A) and B
A= not $titleB= $obj->{"type"}
ABresult
0X0
100
111
89 0 my $text = _extract_text($obj->{content});
90 0 ▶ $title = $text if length($text // '');
Branch: 1 branch
BranchTrueFalseTotal
if length($text // "") 0 0 0
Condition0%0/2
$text // ""
l!l
00
MC/DC0%0/1
$text // ""
$text
Truth table0%0/2
A
A= $text
Aresult
11
00
91 0 last;
92 }
93 }
94 1 close $fh;
95 1 ▶ return undef unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 1 1
96 1 ▶ $date_str //= '0000-00-00';
Condition50%1/2
$date_str //= "0000-00-00"
l!l
10
MC/DC0%0/1
$date_str //= "0000-00-00"
$date_str
Truth table50%1/2
A
A= $date_str
Aresult
11
00
97 1 ▶ $epoch //= 0;
Condition50%1/2
$epoch //= 0
l!l
10
MC/DC0%0/1
$epoch //= 0
$epoch
Truth table50%1/2
A
A= $epoch
Aresult
11
00
98 1 ▶ $title //= '(no title)';
Condition50%1/2
$title //= "(no title)"
l!l
10
MC/DC0%0/1
$title //= "(no title)"
$title
Truth table50%1/2
A
A= $title
Aresult
11
00
99 1 ▶ $cwd //= $ENV{HOME};
Condition33%1/3
$cwd //= $ENV{"HOME"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$cwd //= $ENV{"HOME"}
$cwd $ENV{"HOME"}
Truth table33%1/3
A //= B
A= $cwdB= $ENV{"HOME"}
ABresult
1X1
011
000
100 return {
101 1 ▶ id => $id,
Condition50%1/2
-s $file // 0
l!l
10
MC/DC0%0/1
-s $file // 0
-s $file
Truth table50%1/2
A
A= -s $file
Aresult
11
00
102 title => $title,
103 date => $date_str,
104 epoch => $epoch,
105 cwd => $cwd,
106 project => $cwd,
107 _file => $file,
108 _size => (-s $file) // 0,
109 _adapter => $adapter,
110 };
111 }
112 2 ▶sub _extract_text {
Subroutine _extract_text: called CC=7 SCAR=23.7
113 2 my ($content) = @_;
114 2 ▶ return '' unless defined $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 0 2 2
115 2 ▶ return $content unless ref $content;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $content 0 2 2
116 2 ▶ if (ref $content eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content eq "ARRAY") 2 0 2
117 2 ▶ return join '', map { ref $_ eq 'HASH' ? ($_->{text} // '') : '' } @$content;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 2 0 2
Condition50%1/2
$_->{"text"} // ""
l!l
20
MC/DC0%0/1
$_->{"text"} // ""
$_->{"text"}
Truth table50%1/2
A
A= $_->{"text"}
Aresult
11
00
118 }
119 0 ▶ return $content->{text} // '';
Condition0%0/2
$content->{"text"} // ""
l!l
00
MC/DC0%0/1
$content->{"text"} // ""
$content->{"text"}
Truth table0%0/2
A
A= $content->{"text"}
Aresult
11
00
120 }
121 1 1;
« Saisons/Launcher.pm ↑ Summary Saisons/Adapter/Codex.pm »