| 1 | package Langertha::Output::Tools; | ||||||||||||||||||||||||||||||
| 2 | 3 | our $VERSION = '0.502'; | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Backwards-compat facade over Langertha::ToolCall | ||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Carp (); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Langertha::ToolCall; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||
| 9 | 3 | Carp::carp( | |||||||||||||||||||||||||||||
| 10 | "Langertha::Output::Tools is a backwards-compatibility facade. New code should use " | ||||||||||||||||||||||||||||||
| 11 | . "Langertha::ToolCall directly." | ||||||||||||||||||||||||||||||
| 12 | ); | ||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||
| 14 | sub extract_from_raw { | ||||||||||||||||||||||||||||||
| 15 | 2 | ▶ | my ($class, $raw) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
extract_from_raw: called CC=15 SCAR=44.0
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 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 | 2 | $meta{tool_calls} = [ map { $_->to_hash } Langertha::ToolCall->extract($raw) ]; | |||||||||||||||||||||||||||||
| 46 | 2 | return \%meta; | |||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||
| 49 | sub parse_hermes_calls_from_text { | ||||||||||||||||||||||||||||||
| 50 | 1 | ▶ | my ($class, $text) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
parse_hermes_calls_from_text: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 51 | 1 | my ($clean, $calls) = Langertha::ToolCall->extract_hermes_from_text($text); | |||||||||||||||||||||||||||||
| 52 | 1 | return ( $clean, [ map { $_->to_hash } @$calls ] ); | |||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||
| 55 | sub to_openai_tool_calls { | ||||||||||||||||||||||||||||||
| 56 | 1 | ▶ | my ($class, $canonical_calls) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
to_openai_tool_calls: called CC=8 SCAR=22.0
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 57 | 1 | my @out; | |||||||||||||||||||||||||||||
| 58 | 1 | my $i = 0; | |||||||||||||||||||||||||||||
| 59 | 1 | ▶ | for my $hash ( @{ $canonical_calls || [] } ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 60 | 1 | $i++; | |||||||||||||||||||||||||||||
| 61 | 1 | ▶ | next unless ref($hash) eq 'HASH'; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 62 | 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"}
| |||||||||||||||||||||||||||||||
| 63 | 1 | ▶ | next unless length $name; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 64 | my $tc = Langertha::ToolCall->new( | ||||||||||||||||||||||||||||||
| 65 | name => $name, | ||||||||||||||||||||||||||||||
| 66 | arguments => ( ref( $hash->{arguments} ) eq 'HASH' ? $hash->{arguments} : {} ), | ||||||||||||||||||||||||||||||
| 67 | 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"}
| |||||||||||||||||||||||||||||||
| 68 | ); | ||||||||||||||||||||||||||||||
| 69 | 1 | push @out, $tc->to_openai( fallback_id => "call_langertha_$i" ); | |||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||
| 71 | 1 | return \@out; | |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||
| 74 | sub to_anthropic_tool_use_blocks { | ||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | my ($class, $canonical_calls) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
to_anthropic_tool_use_blocks: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 76 | 0 | my @out; | |||||||||||||||||||||||||||||
| 77 | 0 | my $i = 0; | |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | for my $hash ( @{ $canonical_calls || [] } ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 79 | 0 | $i++; | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | next unless ref($hash) eq 'HASH'; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 81 | 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"}
| |||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | next unless length $name; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 83 | my $tc = Langertha::ToolCall->new( | ||||||||||||||||||||||||||||||
| 84 | name => $name, | ||||||||||||||||||||||||||||||
| 85 | arguments => ( ref( $hash->{arguments} ) eq 'HASH' ? $hash->{arguments} : {} ), | ||||||||||||||||||||||||||||||
| 86 | 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"}
| |||||||||||||||||||||||||||||||
| 87 | ); | ||||||||||||||||||||||||||||||
| 88 | 0 | push @out, $tc->to_anthropic_block( fallback_id => "toolu_langertha_$i" ); | |||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||
| 90 | 0 | return \@out; | |||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||
| 93 | sub to_ollama_tool_calls { | ||||||||||||||||||||||||||||||
| 94 | 1 | ▶ | my ($class, $canonical_calls) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
to_ollama_tool_calls: called CC=8 SCAR=22.3
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 95 | 1 | my @out; | |||||||||||||||||||||||||||||
| 96 | 1 | ▶ | for my $hash ( @{ $canonical_calls || [] } ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 97 | 1 | ▶ | next unless ref($hash) eq 'HASH'; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 98 | 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"}
| |||||||||||||||||||||||||||||||
| 99 | 1 | ▶ | next unless length $name; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 100 | my $tc = Langertha::ToolCall->new( | ||||||||||||||||||||||||||||||
| 101 | name => $name, | ||||||||||||||||||||||||||||||
| 102 | arguments => ( ref( $hash->{arguments} ) eq 'HASH' ? $hash->{arguments} : {} ), | ||||||||||||||||||||||||||||||
| 103 | 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"}
| |||||||||||||||||||||||||||||||
| 104 | ); | ||||||||||||||||||||||||||||||
| 105 | 1 | push @out, $tc->to_ollama; | |||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||
| 107 | 1 | return \@out; | |||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||
| 110 | 3 | 1; | |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||
| 112 | __END__ | ||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||
| 114 | =pod | ||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||
| 116 | =encoding UTF-8 | ||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||
| 118 | =head1 NAME | ||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||
| 120 | Langertha::Output::Tools - Backwards-compat facade over Langertha::ToolCall | ||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||
| 122 | =head1 VERSION | ||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||
| 124 | version 0.502 | ||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||
| 126 | =head1 SUPPORT | ||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||
| 128 | =head2 Issues | ||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||
| 130 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||
| 131 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||
| 133 | =head2 IRC | ||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||
| 135 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||
| 137 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||
| 139 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||
| 141 | =head1 AUTHOR | ||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||
| 143 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||
| 145 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||
| 147 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||
| 149 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||
| 150 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||
| 152 | =cut | ||||||||||||||||||||||||||||||