blib/lib/Langertha/Output/Tools.pm

filter: Statementstmt 70.8% 51 / 72 Branchbran 27.7% 10 / 36 Conditioncond 26.9% 7 / 26 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 88.8% 8 / 9 Podpod 0.0% 0 / 5 totaltotal 47.2% 76 / 161 CC 36 SCAR 37.9
CC
36
Cov
51%
extract_from_raw
81.7
to_anthropic_tool_use_blocks
72.0
to_ollama_tool_calls
9.3
CRAP 44.4
« blib/lib/Langertha/Role/Streaming.pm ↑ Summary blib/lib/Langertha/ToolCall.pm »
1 package Langertha::Output::Tools;
2 4 our $VERSION = '0.505';
3 # ABSTRACT: Backwards-compat facade over Langertha::ToolCall
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Langertha::ToolCall;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 Carp::carp(
10 "Langertha::Output::Tools is a backwards-compatibility facade. New code should use "
11 . "Langertha::ToolCall directly."
12 );
13  
14 2 ▶sub extract_from_raw {
Subroutine extract_from_raw: called CC=15 SCAR=44.0
Pod undocumented
15 2 my ($class, $raw) = @_;
16 2 my %meta = (
17 text => '',
18 tool_calls => [],
19 finish_reason => undef,
20 );
21 2 ▶ return \%meta unless ref($raw) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $raw eq "HASH" 0 2 2
22  
23 # Text + finish_reason extraction stays simple/manual: ToolCall is just
24 # for tool invocations, the surrounding response shape is the caller's.
25 2 my $msg = $raw->{message};
26 2 ▶ if ( ref($msg) eq 'HASH' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $msg eq 'HASH') { } 0 2 2
elsif (my $oai_msg = $$raw{'choices'}[0]{'message'}) { } 2 0 2
elsif (ref $$raw{'content'} eq 'ARRAY') { } 0 0 0
27 0 ▶ $meta{text} = $msg->{content} // '';
Condition0%0/2
$$msg{"content"} // ""
l!l
00
MC/DC0%0/1
$$msg{"content"} // ""
$$msg{"content"}
Truth table0%0/2
A
A= $$msg{"content"}
Aresult
11
00
28 0 ▶ $meta{finish_reason} = $raw->{done_reason} if defined $raw->{done_reason};
Branch: 1 branch
BranchTrueFalseTotal
if defined $$raw{"done_reason"} 0 0 0
29 }
30 elsif ( my $oai_msg = $raw->{choices}[0]{message} ) {
31 2 ▶ $meta{text} = $oai_msg->{content} // '';
Condition50%1/2
$$oai_msg{"content"} // ""
l!l
20
MC/DC0%0/1
$$oai_msg{"content"} // ""
$$oai_msg{"content"}
Truth table50%1/2
A
A= $$oai_msg{"content"}
Aresult
11
00
32 $meta{finish_reason} = $raw->{choices}[0]{finish_reason}
33 2 ▶ if defined $raw->{choices}[0]{finish_reason};
Branch: 1 branch
BranchTrueFalseTotal
if defined $$raw{"choices"}[0]{"finish_reason"} 2 0 2
34 }
35 elsif ( ref( $raw->{content} ) eq 'ARRAY' ) {
36 0 my @text;
37 0 for my $block ( @{ $raw->{content} } ) {
38 0 ▶ next unless ref($block) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $block eq "HASH" 0 0 0
39 0 ▶ push @text, ( $block->{text} // '' ) if ( $block->{type} // '' ) eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
if ($$block{"type"} // "") eq "text" 0 0 0
Condition0%0/4
$$block{"type"} // ""
l!l
00
$$block{"text"} // ""
l!l
00
MC/DC0%0/1
$$block{"type"} // ""
$$block{"type"}
MC/DC0%0/1
$$block{"text"} // ""
$$block{"text"}
Truth table0%0/2
A
A= $$block{"type"}
Aresult
11
00
Truth table0%0/2
A
A= $$block{"text"}
Aresult
11
00
40 }
41 0 $meta{text} = join( '', @text );
42 0 ▶ $meta{finish_reason} = $raw->{stop_reason} if defined $raw->{stop_reason};
Branch: 1 branch
BranchTrueFalseTotal
if defined $$raw{"stop_reason"} 0 0 0
43 }
44  
45 # This legacy facade has no wire format in scope, so it uses the sniffing
46 # fallback. New code passes a known format to Langertha::ToolCall->extract.
47 2 $meta{tool_calls} = [ map { $_->to_hash } Langertha::ToolCall->extract_sniff($raw) ];
48 2 return \%meta;
49 }
50  
51 8 ▶sub parse_hermes_calls_from_text {
Subroutine parse_hermes_calls_from_text: called CC=1 SCAR=0.0
Pod undocumented
52 8 my ($class, $text) = @_;
53 8 my ($clean, $calls) = Langertha::ToolCall->extract_hermes_from_text($text);
54 8 return ( $clean, [ map { $_->to_hash } @$calls ] );
55 }
56  
57 1 ▶sub to_openai_tool_calls {
Subroutine to_openai_tool_calls: called CC=8 SCAR=22.0
Pod undocumented
58 1 my ($class, $canonical_calls) = @_;
59 1 my @out;
60 1 my $i = 0;
61 1 ▶ for my $hash ( @{ $canonical_calls || [] } ) {
Condition50%1/2
$canonical_calls || []
l!l
10
MC/DC0%0/1
$canonical_calls || []
$canonical_calls
Truth table50%1/2
A
A= $canonical_calls
Aresult
11
00
62 1 $i++;
63 1 ▶ next unless ref($hash) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 1 1
64 1 ▶ my $name = $hash->{name} // '';
Condition50%1/2
$$hash{"name"} // ""
l!l
10
MC/DC0%0/1
$$hash{"name"} // ""
$$hash{"name"}
Truth table50%1/2
A
A= $$hash{"name"}
Aresult
11
00
65 1 ▶ next unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 1 1
66 my $tc = Langertha::ToolCall->new(
67 name => $name,
68 arguments => ( ref( $hash->{arguments} ) eq 'HASH' ? $hash->{arguments} : {} ),
69 1 ▶ id => ( $hash->{id} // '' ),
Branch: 1 branch
BranchTrueFalseTotal
ref $$hash{'arguments'} eq 'HASH' ? : 1 0 1
Condition50%1/2
$$hash{"id"} // ""
l!l
01
MC/DC0%0/1
$$hash{"id"} // ""
$$hash{"id"}
Truth table50%1/2
A
A= $$hash{"id"}
Aresult
11
00
70 );
71 1 push @out, $tc->to_openai( fallback_id => "call_langertha_$i" );
72 }
73 1 return \@out;
74 }
75  
76 0 ▶sub to_anthropic_tool_use_blocks {
Subroutine to_anthropic_tool_use_blocks: not called CC=8 SCAR=42.8
Pod undocumented
77 0 my ($class, $canonical_calls) = @_;
78 0 my @out;
79 0 my $i = 0;
80 0 ▶ for my $hash ( @{ $canonical_calls || [] } ) {
Condition0%0/2
$canonical_calls || []
l!l
00
MC/DC0%0/1
$canonical_calls || []
$canonical_calls
Truth table0%0/2
A
A= $canonical_calls
Aresult
11
00
81 0 $i++;
82 0 ▶ next unless ref($hash) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 0 0
83 0 ▶ my $name = $hash->{name} // '';
Condition0%0/2
$$hash{"name"} // ""
l!l
00
MC/DC0%0/1
$$hash{"name"} // ""
$$hash{"name"}
Truth table0%0/2
A
A= $$hash{"name"}
Aresult
11
00
84 0 ▶ next unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 0 0
85 my $tc = Langertha::ToolCall->new(
86 name => $name,
87 arguments => ( ref( $hash->{arguments} ) eq 'HASH' ? $hash->{arguments} : {} ),
88 0 ▶ id => ( $hash->{id} // '' ),
Branch: 1 branch
BranchTrueFalseTotal
ref $$hash{'arguments'} eq 'HASH' ? : 0 0 0
Condition0%0/2
$$hash{"id"} // ""
l!l
00
MC/DC0%0/1
$$hash{"id"} // ""
$$hash{"id"}
Truth table0%0/2
A
A= $$hash{"id"}
Aresult
11
00
89 );
90 0 push @out, $tc->to_anthropic_block( fallback_id => "toolu_langertha_$i" );
91 }
92 0 return \@out;
93 }
94  
95 1 ▶sub to_ollama_tool_calls {
Subroutine to_ollama_tool_calls: called CC=8 SCAR=22.3
Pod undocumented
96 1 my ($class, $canonical_calls) = @_;
97 1 my @out;
98 1 ▶ for my $hash ( @{ $canonical_calls || [] } ) {
Condition50%1/2
$canonical_calls || []
l!l
10
MC/DC0%0/1
$canonical_calls || []
$canonical_calls
Truth table50%1/2
A
A= $canonical_calls
Aresult
11
00
99 1 ▶ next unless ref($hash) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 1 1
100 1 ▶ my $name = $hash->{name} // '';
Condition50%1/2
$$hash{"name"} // ""
l!l
10
MC/DC0%0/1
$$hash{"name"} // ""
$$hash{"name"}
Truth table50%1/2
A
A= $$hash{"name"}
Aresult
11
00
101 1 ▶ next unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 1 1
102 my $tc = Langertha::ToolCall->new(
103 name => $name,
104 arguments => ( ref( $hash->{arguments} ) eq 'HASH' ? $hash->{arguments} : {} ),
105 1 ▶ id => ( $hash->{id} // '' ),
Branch: 1 branch
BranchTrueFalseTotal
ref $$hash{'arguments'} eq 'HASH' ? : 1 0 1
Condition50%1/2
$$hash{"id"} // ""
l!l
10
MC/DC0%0/1
$$hash{"id"} // ""
$$hash{"id"}
Truth table50%1/2
A
A= $$hash{"id"}
Aresult
11
00
106 );
107 1 push @out, $tc->to_ollama;
108 }
109 1 return \@out;
110 }
111  
112 4 1;
113  
114 __END__
« blib/lib/Langertha/Role/Streaming.pm ↑ Summary blib/lib/Langertha/ToolCall.pm »