| 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
| |||||||||||||||||||||||||||||||||
| 16 | 1 | my @sessions; | |||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | opendir my $dh, $tmp or return (); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 18 | 1 | for my $slug (readdir $dh) { | |||||||||||||||||||||||||||||||
| 19 | 3 | ▶ | next if $slug =~ /^\./; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 20 | 1 | my $chats_dir = "$tmp/$slug/chats"; | |||||||||||||||||||||||||||||||
| 21 | 1 | ▶ | next unless -d $chats_dir; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 22 | 1 | ▶ | opendir my $cdh, $chats_dir or next; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 23 | 1 | for my $file (readdir $cdh) { | |||||||||||||||||||||||||||||||
| 24 | 3 | ▶ | next unless $file =~ /^session-.*\.jsonl$/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 25 | 1 | ▶ | my $s = _parse_session("$chats_dir/$file", $self) or next; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 51 | 1 | my $first = 1; | |||||||||||||||||||||||||||||||
| 52 | 1 | ▶ | while (my $line = <$fh>) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 53 | 3 | ▶ | if ($first) { $first = 0; next } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 54 | 2 | ▶ | my $obj = eval { JSON::PP::decode_json($line) } or next; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 55 | 2 | ▶ | my $type = $obj->{type} // ''; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$obj->{"type"} // ""
MC/DC0%0/1
$obj->{"type"} // ""
$obj->{"type"}
Truth table50%1/2
A
A= $obj->{"type"}
| |||||||||||||||||||||||||||||||||
| 56 | 2 | ▶ | next unless $type eq 'user' || $type eq 'gemini'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$type eq "user" or $type eq "gemini"
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"
| |||||||||||||||||||||||||||||||||
| 57 | 2 | ▶ | my $role = $type eq 'user' ? 'user' : 'assistant'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 58 | 2 | ▶ | my $text = $obj->{displayContent} // _extract_text($obj->{content}); | ||||||||||||||||||||||||||||||
|
Condition33%1/3
$obj->{"displayContent"} // _extract_text($obj->{"content"})
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"})
| |||||||||||||||||||||||||||||||||
| 59 | 2 | ▶ | push @messages, { role => $role, text => $text } if length($text // ''); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$text // ""
MC/DC0%0/1
$text // ""
$text
Truth table50%1/2
A
A= $text
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 67 | 1 | my ($id, $title, $date_str, $epoch, $cwd); | |||||||||||||||||||||||||||||||
| 68 | 1 | my $first = 1; | |||||||||||||||||||||||||||||||
| 69 | 1 | ▶ | while (my $line = <$fh>) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 70 | 3 | ▶ | my $obj = eval { JSON::PP::decode_json($line) } or next; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 71 | 3 | ▶ | if ($first) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 72 | 1 | $first = 0; | |||||||||||||||||||||||||||||||
| 73 | 1 | ▶ | $id = $obj->{sessionId} or return undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 74 | 1 | ▶ | $title = $obj->{summary} if $obj->{summary}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 75 | 1 | ▶ | if (my $ts = $obj->{startTime} // $obj->{lastUpdated}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$obj->{"startTime"} // $obj->{"lastUpdated"}
MC/DC0%0/2
$obj->{"startTime"} // $obj->{"lastUpdated"}
$obj->{"startTime"}
$obj->{"lastUpdated"}
Truth table33%1/3
A // B
A= $obj->{"startTime"}B= $obj->{"lastUpdated"}
| |||||||||||||||||||||||||||||||||
| 76 | 1 | ▶ | if ($ts =~ /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
ref $obj->{"directories"} eq "ARRAY" and @{$obj->{"directories"};}
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"};}
| |||||||||||||||||||||||||||||||||
| 84 | 1 | $cwd = $obj->{directories}[0]; | |||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||
| 86 | 1 | next; | |||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||
| 88 | 2 | ▶ | if (!$title && ($obj->{type} // '') eq 'user') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition20%1/5
not $title and ($obj->{"type"} // "") eq "user"
$obj->{"type"} // ""
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"}
| |||||||||||||||||||||||||||||||||
| 89 | 0 | my $text = _extract_text($obj->{content}); | |||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | $title = $text if length($text // ''); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$text // ""
MC/DC0%0/1
$text // ""
$text
Truth table0%0/2
A
A= $text
| |||||||||||||||||||||||||||||||||
| 91 | 0 | last; | |||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||
| 94 | 1 | close $fh; | |||||||||||||||||||||||||||||||
| 95 | 1 | ▶ | return undef unless $id; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 96 | 1 | ▶ | $date_str //= '0000-00-00'; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$date_str //= "0000-00-00"
MC/DC0%0/1
$date_str //= "0000-00-00"
$date_str
Truth table50%1/2
A
A= $date_str
| |||||||||||||||||||||||||||||||||
| 97 | 1 | ▶ | $epoch //= 0; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$epoch //= 0
MC/DC0%0/1
$epoch //= 0
$epoch
Truth table50%1/2
A
A= $epoch
| |||||||||||||||||||||||||||||||||
| 98 | 1 | ▶ | $title //= '(no title)'; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$title //= "(no title)"
MC/DC0%0/1
$title //= "(no title)"
$title
Truth table50%1/2
A
A= $title
| |||||||||||||||||||||||||||||||||
| 99 | 1 | ▶ | $cwd //= $ENV{HOME}; | ||||||||||||||||||||||||||||||
|
Condition33%1/3
$cwd //= $ENV{"HOME"}
MC/DC0%0/2
$cwd //= $ENV{"HOME"}
$cwd
$ENV{"HOME"}
Truth table33%1/3
A //= B
A= $cwdB= $ENV{"HOME"}
| |||||||||||||||||||||||||||||||||
| 100 | return { | ||||||||||||||||||||||||||||||||
| 101 | 1 | ▶ | id => $id, | ||||||||||||||||||||||||||||||
|
Condition50%1/2
-s $file // 0
MC/DC0%0/1
-s $file // 0
-s $file
Truth table50%1/2
A
A= -s $file
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 115 | 2 | ▶ | return $content unless ref $content; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 116 | 2 | ▶ | if (ref $content eq 'ARRAY') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 117 | 2 | ▶ | return join '', map { ref $_ eq 'HASH' ? ($_->{text} // '') : '' } @$content; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$_->{"text"} // ""
MC/DC0%0/1
$_->{"text"} // ""
$_->{"text"}
Truth table50%1/2
A
A= $_->{"text"}
| |||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | return $content->{text} // ''; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$content->{"text"} // ""
MC/DC0%0/1
$content->{"text"} // ""
$content->{"text"}
Truth table0%0/2
A
A= $content->{"text"}
| |||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||
| 121 | 1 | 1; | |||||||||||||||||||||||||||||||