blib/lib/Langertha/Role/ThinkTag.pm

filter: Statementstmt 67.0% 61 / 91 Branchbran 55.0% 33 / 60 Conditioncond 51.6% 16 / 31 MC/DCmcdc 47.0% 8 / 17 Subroutinesub 85.7% 6 / 7 Podpod 100% 2 / 2 totaltotal 60.5% 126 / 208 CC 42 SCAR 35.5
CC
42
Cov
60%
filter_think_content
49.5
filter_think_content
49.5
__ANON__
7.4
CRAP 34.7
« blib/lib/Langertha/Role/Chat.pm ↑ Summary blib/lib/Langertha/ToolResult.pm »
1 package Langertha::Role::ThinkTag;
2 # ABSTRACT: Configurable think tag filtering for reasoning models
3 194 our $VERSION = '0.505';
4 194 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6  
7 194 has think_tag => (
8 is => 'ro',
9 isa => 'Str',
10 default => 'think',
11 );
12  
13  
14 194 has think_tag_filter => (
15 is => 'ro',
16 isa => 'Bool',
17 default => 1,
18 );
19  
20  
21 968 ▶sub filter_think_content {
Subroutine filter_think_content: called CC=16 SCAR=39.0
Pod documented
22 0 ▶ my ( $self, $text ) = @_;
Subroutine filter_think_content: not called CC=16 SCAR=39.0
Pod documented
23 0 ▶ return ($text, undef) unless $self->think_tag_filter && defined $text;
Branch: 2 branches
BranchTrueFalseTotal
unless $self->think_tag_filter and defined $text 6 962 968
unless $self->think_tag_filter and defined $text 0 0 0
Condition50%3/6
$self->think_tag_filter and defined $text
!ll&&!rl&&r
15962
$self->think_tag_filter and defined $text
!ll&&!rl&&r
000
MC/DC100%2/2
$self->think_tag_filter and defined $text
$self->think_tag_filter defined $text
MC/DC0%0/2
$self->think_tag_filter and defined $text
$self->think_tag_filter defined $text
Truth table100%3/3
A and B
A= $self->think_tag_filterB= defined $text
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $self->think_tag_filterB= defined $text
ABresult
0X0
100
111
24 0 my ( $open, $close ) = ( '<' . $self->think_tag . '>', '</' . $self->think_tag . '>' );
25 0 my $first_open = index( $text, $open );
26 0 my $first_close = index( $text, $close );
27 # No tag at all: the text is the model's, byte for byte (karr k302).
28 0 ▶ return ($text, undef) if $first_open < 0 && $first_close < 0;
Branch: 2 branches
BranchTrueFalseTotal
if $first_open < 0 and $first_close < 0 915 47 962
if $first_open < 0 and $first_close < 0 0 0 0
Condition50%3/6
$first_open < 0 and $first_close < 0
!ll&&!rl&&r
3215915
$first_open < 0 and $first_close < 0
!ll&&!rl&&r
000
MC/DC100%2/2
$first_open < 0 and $first_close < 0
$first_open < 0 $first_close < 0
MC/DC0%0/2
$first_open < 0 and $first_close < 0
$first_open < 0 $first_close < 0
Truth table100%3/3
A and B
A= $first_open < 0B= $first_close < 0
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $first_open < 0B= $first_close < 0
ABresult
0X0
100
111
29 0 my ( @thinking, @kept );
30 0 my $rest = $text;
31 # Orphan closing tag: the chat template (DeepSeek-R1, Qwen3 thinking) put
32 # the opening tag into the prompt, so the reply starts inside the thought.
33 0 ▶ if ( $first_close >= 0 && ( $first_open < 0 || $first_close < $first_open ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($first_close >= 0 and $first_open < 0 || $first_close < $first_open) 16 31 47
if ($first_close >= 0 and $first_open < 0 || $first_close < $first_open) 0 0 0
Condition50%6/12
$first_close >= 0 and $first_open < 0 || $first_close < $first_open
!ll&&!rl&&r
22916
$first_open < 0 or $first_close < $first_open
l!l&&r!l&&!r
15129
$first_close >= 0 and $first_open < 0 || $first_close < $first_open
!ll&&!rl&&r
000
$first_open < 0 or $first_close < $first_open
l!l&&r!l&&!r
000
MC/DC100%3/3
$first_close >= 0 and $first_open < 0 || $first_close < $first_open
$first_close >= 0 $first_open < 0 $first_close < $first_open
MC/DC0%0/3
$first_close >= 0 and $first_open < 0 || $first_close < $first_open
$first_close >= 0 $first_open < 0 $first_close < $first_open
Truth table100%4/4
A and B or C
A= $first_close >= 0B= $first_open < 0C= $first_close < $first_open
ABCresult
0XX0
1000
11X1
1011
Truth table0%0/4
A and B or C
A= $first_close >= 0B= $first_open < 0C= $first_close < $first_open
ABCresult
0XX0
1000
11X1
1011
34 0 my $thought = substr( $rest, 0, $first_close, '' );
35 0 substr( $rest, 0, length $close, '' );
36 0 ▶ push @thinking, $thought if length $thought;
Branch: 2 branches
BranchTrueFalseTotal
if length $thought 15 1 16
if length $thought 0 0 0
37 0 push @kept, '';
38 }
39 0 ▶ while ( ( my $at = index( $rest, $open ) ) >= 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ((my $at = index($rest, $open)) >= 0) 34 44 78
if ((my $at = index($rest, $open)) >= 0) 0 0 0
40 0 push @kept, substr( $rest, 0, $at, '' );
41 0 substr( $rest, 0, length $open, '' );
42 0 my $end = index( $rest, $close );
43 0 ▶ if ( $end < 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($end < 0) 3 31 34
if ($end < 0) 0 0 0
44 # Unclosed tag: <think>... (rest of text) — model stopped mid-thought
45 0 ▶ push @thinking, $rest if length $rest;
Branch: 2 branches
BranchTrueFalseTotal
if length $rest 3 0 3
if length $rest 0 0 0
46 0 $rest = '';
47 0 last;
48 }
49 # Matched pair: <think>...</think>
50 0 push @thinking, substr( $rest, 0, $end, '' );
51 0 substr( $rest, 0, length $close, '' );
52 }
53 0 push @kept, $rest;
54 # Only the whitespace a removed block leaves at either end goes; the line
55 # after a leading block keeps its indentation.
56 0 my $filtered = join '', @kept;
57 0 ▶ if ( $kept[0] =~ /\A\s*\z/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($kept[0] =~ /\A\s*\z/) 45 2 47
if ($kept[0] =~ /\A\s*\z/) 0 0 0
58 0 ▶ $filtered =~ s/\A\s*\n// or $filtered =~ s/\A\s+//;
Branch: 2 branches
BranchTrueFalseTotal
$filtered =~ s/\A\s*\n// or $filtered =~ s/\A\s+// 3 42 45
$filtered =~ s/\A\s*\n// or $filtered =~ s/\A\s+// 0 0 0
59 }
60 0 ▶ $filtered =~ s/\s+\z// if $kept[-1] =~ /\A\s*\z/;
Branch: 2 branches
BranchTrueFalseTotal
if $kept[-1] =~ /\A\s*\z/ 5 42 47
if $kept[-1] =~ /\A\s*\z/ 0 0 0
61 0 ▶ my $thinking = @thinking ? join("\n", @thinking) : undef;
Branch: 2 branches
BranchTrueFalseTotal
@thinking ? : 46 1 47
@thinking ? : 0 0 0
62 0 return ($filtered, $thinking);
63 }
64  
65  
66 838 ▶around 'chat_response' => sub {
Subroutine __ANON__: called CC=7 SCAR=20.0
67 838 my ( $orig, $self, @args ) = @_;
68 838 my $response = $self->$orig(@args);
69 751 ▶ return $response unless $self->think_tag_filter;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->think_tag_filter 1 750 751
70 750 my $content = $response->content;
71 750 my ($filtered, $thinking) = $self->filter_think_content($content);
72 750 ▶ return $response
Branch: 1 branch
BranchTrueFalseTotal
if not defined $thinking and ($filtered // "") eq ($content // "") 727 23 750
Condition57%4/7
not defined $thinking and ($filtered // "") eq ($content // "")
!ll&&!rl&&r
230727
$filtered // ""
l!l
7270
$content // ""
l!l
7270
MC/DC50%1/2
not defined $thinking and ($filtered // "") eq ($content // "")
not defined $thinking $filtered
MC/DC0%0/1
$content // ""
$content
Truth table66%2/3
not(A) and B
A= not defined $thinkingB= $filtered
ABresult
0X0
100
111
Truth table50%1/2
A
A= $content
Aresult
11
00
73 if !defined $thinking && ( $filtered // '' ) eq ( $content // '' );
74 23 ▶ return $response->clone_with(
Branch: 1 branch
BranchTrueFalseTotal
defined $thinking ? : 23 0 23
75 content => $filtered,
76 defined $thinking ? (thinking => $thinking) : (),
77 );
78 194 };
79  
80 # Both streaming entry points carry an aggregated thinking string alongside
81 # content (Role::Chat::aggregate_thinking, filled from the native reasoning
82 # deltas). Two thinking sources can appear and must not both win: a tag-based
83 # model inlines its chain-of-thought in <think> tags in the content, while a
84 # native-reasoning model surfaces it out-of-band as the aggregated string. The
85 # non-stream `around chat_response` resolves this the same way — tag-extracted
86 # thinking overrides the native one only when tags were actually present.
87 6 ▶around 'simple_chat_stream' => sub {
Subroutine __ANON__: called CC=4 SCAR=14.3
88 6 my ( $orig, $self, @args ) = @_;
89 6 my ( $content, $thinking ) = $self->$orig(@args);
90 3 ▶ if ( $self->think_tag_filter ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->think_tag_filter) 3 0 3
91 3 my ( $filtered, $tag_thinking ) = $self->filter_think_content($content);
92 3 $content = $filtered;
93 3 ▶ $thinking = $tag_thinking if defined $tag_thinking;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tag_thinking 0 3 3
94 }
95 3 ▶ return wantarray ? ( $content, $thinking ) : $content;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 3 3
96 194 };
97  
98 227 ▶around 'chat_stream_realtime_f' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
99 227 my ( $orig, $self, @args ) = @_;
100 181 ▶ return $self->$orig(@args)->then(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
101 181 my ( $content, $chunks, $timing, $thinking ) = @_;
102 181 ▶ return Future->done($content, $chunks, $timing, $thinking)
Branch: 1 branch
BranchTrueFalseTotal
unless $self->think_tag_filter 1 180 181
103 unless $self->think_tag_filter;
104 180 my ($filtered, $tag_thinking) = $self->filter_think_content($content);
105 180 ▶ $thinking = $tag_thinking if defined $tag_thinking;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tag_thinking 10 170 180
106 180 return Future->done($filtered, $chunks, $timing, $thinking);
107 227 });
108 194 };
109  
110  
111 194 1;
112  
113 __END__
« blib/lib/Langertha/Role/Chat.pm ↑ Summary blib/lib/Langertha/ToolResult.pm »