| 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
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | $meta{text} = $msg->{content} // ''; | ||||||||||||||||||||||||||||
|
Condition0%0/2
$$msg{"content"} // ""
MC/DC0%0/1
$$msg{"content"} // ""
$$msg{"content"}
Truth table0%0/2
A
A= $$msg{"content"}
| |||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | $meta{finish_reason} = $raw->{done_reason} if defined $raw->{done_reason}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||
| 30 | elsif ( my $oai_msg = $raw->{choices}[0]{message} ) { | ||||||||||||||||||||||||||||||
| 31 | 2 | ▶ | $meta{text} = $oai_msg->{content} // ''; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$$oai_msg{"content"} // ""
MC/DC0%0/1
$$oai_msg{"content"} // ""
$$oai_msg{"content"}
Truth table50%1/2
A
A= $$oai_msg{"content"}
| |||||||||||||||||||||||||||||||
| 32 | $meta{finish_reason} = $raw->{choices}[0]{finish_reason} | ||||||||||||||||||||||||||||||
| 33 | 2 | ▶ | if defined $raw->{choices}[0]{finish_reason}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | push @text, ( $block->{text} // '' ) if ( $block->{type} // '' ) eq 'text'; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/4
$$block{"type"} // ""
$$block{"text"} // ""
MC/DC0%0/1
$$block{"type"} // ""
$$block{"type"}
MC/DC0%0/1
$$block{"text"} // ""
$$block{"text"}
Truth table0%0/2
A
A= $$block{"type"}
Truth table0%0/2
A
A= $$block{"text"}
| |||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||
| 41 | 0 | $meta{text} = join( '', @text ); | |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | $meta{finish_reason} = $raw->{stop_reason} if defined $raw->{stop_reason}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 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 || []
MC/DC0%0/1
$canonical_calls || []
$canonical_calls
Truth table50%1/2
A
A= $canonical_calls
| |||||||||||||||||||||||||||||||
| 62 | 1 | $i++; | |||||||||||||||||||||||||||||
| 63 | 1 | ▶ | next unless ref($hash) eq 'HASH'; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 64 | 1 | ▶ | my $name = $hash->{name} // ''; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$$hash{"name"} // ""
MC/DC0%0/1
$$hash{"name"} // ""
$$hash{"name"}
Truth table50%1/2
A
A= $$hash{"name"}
| |||||||||||||||||||||||||||||||
| 65 | 1 | ▶ | next unless length $name; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 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
Condition50%1/2
$$hash{"id"} // ""
MC/DC0%0/1
$$hash{"id"} // ""
$$hash{"id"}
Truth table50%1/2
A
A= $$hash{"id"}
| |||||||||||||||||||||||||||||||
| 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 || []
MC/DC0%0/1
$canonical_calls || []
$canonical_calls
Truth table0%0/2
A
A= $canonical_calls
| |||||||||||||||||||||||||||||||
| 81 | 0 | $i++; | |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | next unless ref($hash) eq 'HASH'; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | my $name = $hash->{name} // ''; | ||||||||||||||||||||||||||||
|
Condition0%0/2
$$hash{"name"} // ""
MC/DC0%0/1
$$hash{"name"} // ""
$$hash{"name"}
Truth table0%0/2
A
A= $$hash{"name"}
| |||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | next unless length $name; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 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
Condition0%0/2
$$hash{"id"} // ""
MC/DC0%0/1
$$hash{"id"} // ""
$$hash{"id"}
Truth table0%0/2
A
A= $$hash{"id"}
| |||||||||||||||||||||||||||||||
| 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 || []
MC/DC0%0/1
$canonical_calls || []
$canonical_calls
Truth table50%1/2
A
A= $canonical_calls
| |||||||||||||||||||||||||||||||
| 99 | 1 | ▶ | next unless ref($hash) eq 'HASH'; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 100 | 1 | ▶ | my $name = $hash->{name} // ''; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$$hash{"name"} // ""
MC/DC0%0/1
$$hash{"name"} // ""
$$hash{"name"}
Truth table50%1/2
A
A= $$hash{"name"}
| |||||||||||||||||||||||||||||||
| 101 | 1 | ▶ | next unless length $name; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 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
Condition50%1/2
$$hash{"id"} // ""
MC/DC0%0/1
$$hash{"id"} // ""
$$hash{"id"}
Truth table50%1/2
A
A= $$hash{"id"}
| |||||||||||||||||||||||||||||||
| 106 | ); | ||||||||||||||||||||||||||||||
| 107 | 1 | push @out, $tc->to_ollama; | |||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||
| 109 | 1 | return \@out; | |||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||
| 112 | 4 | 1; | |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||
| 114 | __END__ | ||||||||||||||||||||||||||||||