Saisons/Adapter/Opencode.pm

filter: Statementstmt 31.8% 36 / 113 Branchbran 6.2% 3 / 48 Conditioncond 4.5% 2 / 44 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 52.0% 13 / 25 Podpod 0.0% 0 / 9 totaltotal 20.5% 54 / 263 CC 49 SCAR 43.6
CC
49
Cov
20%
load_messages
240.0
find_sessions
132.0
_sqlite_out
42.0
CRAP 78.0
« Saisons/UI.pm ↑ Summary Saisons/Adapter/Claude.pm »
1 package App::Saisons::Adapter::Opencode;
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 App::Saisons ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use POSIX qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use App::Saisons::Launcher ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶sub new { bless {}, shift }
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
10 1 ▶sub name { 'opencode' }
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
11 2 ▶sub tag { "\x{2395} Opc" }
Subroutine tag: called CC=1 SCAR=0.0
Pod undocumented
12 1 ▶sub tag_color { 'bold magenta' }
Subroutine tag_color: called CC=1 SCAR=0.0
Pod undocumented
13  
14 1 my $SQLITE3;
15  
16 1 ▶sub _sqlite3 {
Subroutine _sqlite3: called CC=7 SCAR=22.9
17 1 ▶ return $SQLITE3 if defined $SQLITE3;
Branch: 1 branch
BranchTrueFalseTotal
if defined $SQLITE3 0 1 1
18 1 ▶ for my $dir (split /:/, ($ENV{PATH} // '')) {
Condition50%1/2
$ENV{"PATH"} // ""
l!l
10
MC/DC0%0/1
$ENV{"PATH"} // ""
$ENV{"PATH"}
Truth table50%1/2
A
A= $ENV{"PATH"}
Aresult
11
00
19 10 ▶ next unless length $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless length $dir 0 10 10
20 10 my $p = "$dir/sqlite3";
21 10 ▶ if (-x $p && !-d $p) {
Branch: 1 branch
BranchTrueFalseTotal
if (-x $p and not -d $p) 0 10 10
Condition33%1/3
-x $p and not -d $p
!ll&&!rl&&r
1000
MC/DC0%0/2
-x $p and not -d $p
-x $p not -d $p
Truth table33%1/3
A and not(B)
A= -x $pB= not -d $p
ABresult
0X0
100
111
22 0 return $SQLITE3 = $p;
23 }
24 }
25 1 return $SQLITE3 = '';
26 }
27  
28 0 ▶sub _db_path {
Subroutine _db_path: not called CC=2 SCAR=17.9
29 0 ▶ my $data_home = $ENV{XDG_DATA_HOME} // "$ENV{HOME}/.local/share";
Condition0%0/2
$ENV{"XDG_DATA_HOME"} // qq[$ENV{"HOME"}/.local/share]
l!l
00
MC/DC0%0/1
$ENV{"XDG_DATA_HOME"} // qq[$ENV{"HOME"}/.local/share]
$ENV{"XDG_DATA_HOME"}
Truth table0%0/2
A
A= $ENV{"XDG_DATA_HOME"}
Aresult
11
00
30 0 return "$data_home/opencode/opencode.db";
31 }
32  
33 0 ▶sub _shq {
Subroutine _shq: not called CC=1 SCAR=6.9
34 0 my ($s) = @_;
35 0 $s =~ s/'/'\\''/g;
36 0 return "'$s'";
37 }
38  
39 0 ▶sub _q {
Subroutine _q: not called CC=3 SCAR=24.8
40 0 my ($s) = @_;
41 0 ▶ $s =~ s/'/''/g if defined $s;
Branch: 1 branch
BranchTrueFalseTotal
if defined $s 0 0 0
42 0 ▶ return $s // '';
Condition0%0/2
$s // ""
l!l
00
MC/DC0%0/1
$s // ""
$s
Truth table0%0/2
A
A= $s
Aresult
11
00
43 }
44  
45 0 ▶sub _sqlite_out {
Subroutine _sqlite_out: not called CC=6 SCAR=37.4
46 0 my ($self, $sql) = @_;
47 0 my $db = _db_path();
48 0 ▶ my $exe = _sqlite3() or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $exe = _sqlite3() or return undef 0 0 0
49 0 ▶ return undef unless -f $db;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $db 0 0 0
50 0 my $cmd = join(' ', _shq($exe), '-readonly', '-json', _shq($db), _shq($sql)) . ' 2>/dev/null';
51 0 my $out = `$cmd`;
52 0 ▶ return undef unless $? == 0 && defined $out;
Branch: 1 branch
BranchTrueFalseTotal
unless $? == 0 and defined $out 0 0 0
Condition0%0/3
$? == 0 and defined $out
!ll&&!rl&&r
000
MC/DC0%0/2
$? == 0 and defined $out
$? == 0 defined $out
Truth table0%0/3
A and B
A= $? == 0B= defined $out
ABresult
0X0
100
111
53 0 my $rows = eval { JSON::PP::decode_json($out) };
54 0 ▶ return $rows // [];
Condition0%0/2
$rows // []
l!l
00
MC/DC0%0/1
$rows // []
$rows
Truth table0%0/2
A
A= $rows
Aresult
11
00
55 }
56  
57 0 ▶sub _sqlite_exec {
Subroutine _sqlite_exec: not called CC=3 SCAR=24.8
58 0 my ($self, $sql) = @_;
59 0 my $db = _db_path();
60 0 ▶ my $exe = _sqlite3() or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $exe = _sqlite3() or return 0 0 0 0
61 0 ▶ return 0 unless -f $db;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $db 0 0 0
62 0 my $cmd = join(' ', _shq($exe), _shq($db), _shq($sql), '>/dev/null', '2>&1');
63 0 return system($cmd) == 0;
64 }
65  
66 0 ▶sub _session_title {
Subroutine _session_title: not called CC=5 SCAR=34.0
67 0 my ($row) = @_;
68 0 ▶ my $title = $row->{title} // '';
Condition0%0/2
$row->{"title"} // ""
l!l
00
MC/DC0%0/1
$row->{"title"} // ""
$row->{"title"}
Truth table0%0/2
A
A= $row->{"title"}
Aresult
11
00
69 0 ▶ return $title if length($title) && $title !~ /\ANew session - /;
Branch: 1 branch
BranchTrueFalseTotal
if length $title and not $title =~ /\ANew session - / 0 0 0
Condition0%0/3
length $title and not $title =~ /\ANew session - /
!ll&&!rl&&r
000
MC/DC0%0/2
length $title and not $title =~ /\ANew session - /
length $title not $title =~ /\ANew session - /
Truth table0%0/3
A and not(B)
A= length $titleB= not $title =~ /\ANew session - /
ABresult
0X0
100
111
70 0 my $text = _first_text($row->{first_hex});
71 0 ▶ return $text if length $text;
Branch: 1 branch
BranchTrueFalseTotal
if length $text 0 0 0
72 0 return '(no title)';
73 }
74  
75 0 ▶sub _first_text {
Subroutine _first_text: not called CC=4 SCAR=26.3
76 0 my ($hex) = @_;
77 0 ▶ return '' unless defined $hex && length $hex;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hex and length $hex 0 0 0
Condition0%0/3
defined $hex and length $hex
!ll&&!rl&&r
000
MC/DC0%0/2
defined $hex and length $hex
defined $hex length $hex
Truth table0%0/3
A and B
A= defined $hexB= length $hex
ABresult
0X0
100
111
78 0 my $text;
79 {
80 0 ▶ no warnings 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.2
81 0 $text = decode('UTF-8', pack('H*', $hex), Encode::FB_DEFAULT);
82 }
83 0 $text =~ s/\s+/ /g;
84 0 $text =~ s/\A\s+//;
85 0 $text =~ s/\s+\z//;
86 0 ▶ $text = substr($text, 0, 79) . "\x{2026}" if length($text) > 80;
Branch: 1 branch
BranchTrueFalseTotal
if length $text > 80 0 0 0
87 0 return $text;
88 }
89  
90 0 ▶sub find_sessions {
Subroutine find_sessions: not called CC=11 SCAR=48.8
Pod undocumented
91 0 my ($self) = @_;
92 0 ▶ my $rows = $self->_sqlite_out(<<'SQL') or return ();
Branch: 1 branch
BranchTrueFalseTotal
my $rows = $self->_sqlite_out("SELECT s.id AS id, s.directory AS dir, s.title AS title, s.time_created AS created_ms, s.time_updated AS updated_ms, COALESCE((SELECT SUM(LENGTH(d.data)) FROM ( SELECT data FROM message WHERE session_id = s.id UNION ALL SELECT data FROM part WHERE session_id = s.id ) d), 0) AS bytes, hex((SELECT json_extract(p.data, '\$.text') FROM part p JOIN message m ON m.id = p.message_id WHERE p.session_id = s.id AND json_valid(p.data) AND json_extract(p.data, '\$.type') = 'text' AND json_valid(m.data) AND json_extract(m.data, '\$.role') = 'user' ORDER BY p.time_created, p.id LIMIT 1)) AS first_hex FROM session s WHERE s.time_archived IS NULL ORDER BY s.time_updated DESC\n") or return () 0 0 0
93 SELECT s.id AS id, s.directory AS dir, s.title AS title, s.time_created AS created_ms, s.time_updated AS updated_ms, COALESCE((SELECT SUM(LENGTH(d.data)) FROM ( SELECT data FROM message WHERE session_id = s.id UNION ALL SELECT data FROM part WHERE session_id = s.id ) d), 0) AS bytes, hex((SELECT json_extract(p.data, '$.text') FROM part p JOIN message m ON m.id = p.message_id WHERE p.session_id = s.id AND json_valid(p.data) AND json_extract(p.data, '$.type') = 'text' AND json_valid(m.data) AND json_extract(m.data, '$.role') = 'user' ORDER BY p.time_created, p.id LIMIT 1)) AS first_hex FROM session s WHERE s.time_archived IS NULL ORDER BY s.time_updated DESC
94 SQL
95 0 my @sessions;
96 0 for my $row (@$rows) {
97 0 ▶ next unless $row->{id};
Branch: 1 branch
BranchTrueFalseTotal
unless $row->{"id"} 0 0 0
98 0 ▶ my $ms = $row->{updated_ms} || $row->{created_ms} || 0;
Condition0%0/5
$row->{'updated_ms'} || $row->{'created_ms'} || 0
l!l
00
$row->{"updated_ms"} or $row->{"created_ms"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$row->{'updated_ms'} || $row->{'created_ms'} || 0
$row->{"updated_ms"} $row->{"created_ms"}
Truth table0%0/3
A or B
A= $row->{"updated_ms"}B= $row->{"created_ms"}
ABresult
1X1
011
000
99 0 my $epoch = int($ms / 1000);
100 push @sessions, {
101 id => $row->{id},
102 title => _session_title($row),
103 date => POSIX::strftime('%Y-%m-%dT%H:%M:%S', gmtime($epoch)),
104 epoch => $epoch,
105 cwd => length($row->{dir} // '') ? $row->{dir} : $ENV{HOME},
106 project => length($row->{dir} // '') ? $row->{dir} : $ENV{HOME},
107 _file => _db_path(),
108 0 ▶ _size => int($row->{bytes} || 0),
Branch: 2 branches
BranchTrueFalseTotal
length($row->{'dir'} // '') ? : 0 0 0
length($row->{'dir'} // '') ? : 0 0 0
Condition0%0/6
$row->{"dir"} // ""
l!l
00
$row->{"dir"} // ""
l!l
00
$row->{'bytes'} || 0
l!l
00
MC/DC0%0/1
$row->{"dir"} // ""
$row->{"dir"}
MC/DC0%0/1
$row->{"dir"} // ""
$row->{"dir"}
MC/DC0%0/1
$row->{'bytes'} || 0
$row->{'bytes'}
Truth table0%0/2
A
A= $row->{"dir"}
Aresult
11
00
Truth table0%0/2
A
A= $row->{"dir"}
Aresult
11
00
Truth table0%0/2
A
A= $row->{'bytes'}
Aresult
11
00
109 _adapter => $self,
110 };
111 }
112 0 return @sessions;
113 }
114 0 ▶sub running {
Subroutine running: not called CC=1 SCAR=6.9
Pod undocumented
115 0 return ();
116 }
117 0 ▶sub launch {
Subroutine launch: not called CC=2 SCAR=17.9
Pod undocumented
118 0 my ($self, $sessions, $launcher) = @_;
119 0 for my $s (@$sessions) {
120 0 my $cmd = "cd \Q$s->{cwd}\E && opencode --session \Q$s->{id}\E";
121 0 App::Saisons::Launcher::launch_cmd($cmd, $s->{cwd}, $launcher, $s->{title});
122 }
123 }
124 0 ▶sub delete_session {
Subroutine delete_session: not called CC=1 SCAR=6.9
Pod undocumented
125 0 my ($self, $session) = @_;
126 0 my $id = _q($session->{id});
127 0 return $self->_sqlite_exec(
128 "DELETE FROM part WHERE session_id = '$id'; "
129 . "DELETE FROM message WHERE session_id = '$id'; "
130 . "DELETE FROM session WHERE id = '$id';"
131 );
132 }
133 0 ▶sub load_messages {
Subroutine load_messages: not called CC=15 SCAR=54.8
Pod undocumented
134 0 my ($self, $session) = @_;
135 0 my $sid = _q($session->{id});
136 0 ▶ my $msgs = $self->_sqlite_out(
Branch: 1 branch
BranchTrueFalseTotal
my $msgs = $self->_sqlite_out('SELECT id AS id, data AS data FROM message ' . "WHERE session_id = '${sid}' ORDER BY time_created, id") or return () 0 0 0
137 "SELECT id AS id, data AS data FROM message "
138 . "WHERE session_id = '$sid' ORDER BY time_created, id"
139 ) or return ();
140 0 ▶ my $parts = $self->_sqlite_out(
Branch: 1 branch
BranchTrueFalseTotal
my $parts = $self->_sqlite_out('SELECT message_id AS mid, data AS data FROM part ' . "WHERE session_id = '${sid}' ORDER BY time_created, id") or return () 0 0 0
141 "SELECT message_id AS mid, data AS data FROM part "
142 . "WHERE session_id = '$sid' ORDER BY time_created, id"
143 ) or return ();
144 0 my %text_of;
145 0 for my $p (@$parts) {
146 0 ▶ my $obj = eval { JSON::PP::decode_json($p->{data}) } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $obj = eval { do { JSON::PP::decode_json($p->{"data"}) } } or next 0 0 0
147 0 ▶ next unless ($obj->{type} // '') eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->{"type"} // "") eq "text" 0 0 0
Condition0%0/2
$obj->{"type"} // ""
l!l
00
MC/DC0%0/1
$obj->{"type"} // ""
$obj->{"type"}
Truth table0%0/2
A
A= $obj->{"type"}
Aresult
11
00
148 0 ▶ $text_of{ $p->{mid} } .= ($obj->{text} // '');
Condition0%0/2
$obj->{"text"} // ""
l!l
00
MC/DC0%0/1
$obj->{"text"} // ""
$obj->{"text"}
Truth table0%0/2
A
A= $obj->{"text"}
Aresult
11
00
149 }
150 0 my @messages;
151 0 for my $m (@$msgs) {
152 0 ▶ my $obj = eval { JSON::PP::decode_json($m->{data}) } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $obj = eval { do { JSON::PP::decode_json($m->{"data"}) } } or next 0 0 0
153 0 ▶ my $role = $obj->{role} // '';
Condition0%0/2
$obj->{"role"} // ""
l!l
00
MC/DC0%0/1
$obj->{"role"} // ""
$obj->{"role"}
Truth table0%0/2
A
A= $obj->{"role"}
Aresult
11
00
154 0 ▶ next unless $role eq 'user' || $role eq 'assistant';
Branch: 1 branch
BranchTrueFalseTotal
unless $role eq "user" or $role eq "assistant" 0 0 0
Condition0%0/3
$role eq "user" or $role eq "assistant"
l!l&&r!l&&!r
000
MC/DC0%0/2
$role eq "user" or $role eq "assistant"
$role eq "user" $role eq "assistant"
Truth table0%0/3
A or B
A= $role eq "user"B= $role eq "assistant"
ABresult
1X1
011
000
155 0 ▶ my $text = $text_of{ $m->{id} } // '';
Condition0%0/2
$text_of{$m->{"id"}} // ""
l!l
00
MC/DC0%0/1
$text_of{$m->{"id"}} // ""
$text_of{$m->{"id"}}
Truth table0%0/2
A
A= $text_of{$m->{"id"}}
Aresult
11
00
156 0 ▶ push @messages, { role => $role, text => $text } if length $text;
Branch: 1 branch
BranchTrueFalseTotal
if length $text 0 0 0
157 }
158 0 return @messages;
159 }
160 1 1;
« Saisons/UI.pm ↑ Summary Saisons/Adapter/Claude.pm »