| 1 | package Langertha::ToolCall; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Immutable canonical tool invocation emitted by an LLM | ||||||||||||||||||||||||||||
| 3 | 39 | our $VERSION = '0.502'; | |||||||||||||||||||||||||||
| 4 | 39 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 39 | ▶ | use JSON::MaybeXS qw( encode_json decode_json ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 39 | has name => ( | |||||||||||||||||||||||||||
| 8 | is => 'ro', | ||||||||||||||||||||||||||||
| 9 | isa => 'Str', | ||||||||||||||||||||||||||||
| 10 | required => 1, | ||||||||||||||||||||||||||||
| 11 | ); | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | has arguments => ( | ||||||||||||||||||||||||||||
| 14 | is => 'ro', | ||||||||||||||||||||||||||||
| 15 | isa => 'HashRef', | ||||||||||||||||||||||||||||
| 16 | 0 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 17 | 39 | ); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # Provider-specific call id (may be empty if the upstream didn't supply one). | ||||||||||||||||||||||||||||
| 20 | 39 | has id => ( | |||||||||||||||||||||||||||
| 21 | is => 'ro', | ||||||||||||||||||||||||||||
| 22 | isa => 'Str', | ||||||||||||||||||||||||||||
| 23 | default => '', | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # True when this call was synthesized by Langertha (e.g. forced-tool | ||||||||||||||||||||||||||||
| 27 | # rewrite via response_format on engines without native named-tool | ||||||||||||||||||||||||||||
| 28 | # forcing) rather than emitted directly by the model. Useful for | ||||||||||||||||||||||||||||
| 29 | # callers that want to distinguish "the model decided to call this" | ||||||||||||||||||||||||||||
| 30 | # from "we asked it to and parsed the result back into a tool_call". | ||||||||||||||||||||||||||||
| 31 | 39 | has synthetic => ( | |||||||||||||||||||||||||||
| 32 | is => 'ro', | ||||||||||||||||||||||||||||
| 33 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 34 | default => 0, | ||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | sub _decode_args { | ||||||||||||||||||||||||||||
| 39 | 7 | ▶ | my ($args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_decode_args: called CC=5 SCAR=17.0
| |||||||||||||||||||||||||||||
| 40 | 7 | ▶ | return {} unless defined $args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 7 | ▶ | return $args if ref($args) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 7 | ▶ | return {} unless length $args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 7 | my $decoded = eval { decode_json($args) }; | |||||||||||||||||||||||||||
| 44 | 7 | ▶ | return ( ref($decoded) eq 'HASH' ) ? $decoded : {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # --- Constructors from wire-format hashes --- | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | sub from_openai { | ||||||||||||||||||||||||||||
| 50 | 5 | ▶ | my ($class, $hash) = @_; | ||||||||||||||||||||||||||
|
Subroutine
from_openai: called CC=7 SCAR=21.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 51 | 5 | ▶ | return undef unless ref($hash) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 5 | ▶ | my $fn = $hash->{function} || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$hash->{'function'} || {}
MC/DC0%0/1
$hash->{'function'} || {}
$hash->{'function'}
Truth table50%1/2
A
A= $hash->{'function'}
| |||||||||||||||||||||||||||||
| 53 | 5 | ▶ | return undef unless ref($fn) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 5 | ▶ | my $name = $fn->{name} // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$fn->{"name"} // ""
MC/DC0%0/1
$fn->{"name"} // ""
$fn->{"name"}
Truth table50%1/2
A
A= $fn->{"name"}
| |||||||||||||||||||||||||||||
| 55 | 5 | ▶ | return undef unless length $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | return $class->new( | ||||||||||||||||||||||||||||
| 57 | name => $name, | ||||||||||||||||||||||||||||
| 58 | arguments => _decode_args( $fn->{arguments} ), | ||||||||||||||||||||||||||||
| 59 | 5 | ▶ | id => ( $hash->{id} // '' ), | ||||||||||||||||||||||||||
|
Condition50%1/2
$hash->{"id"} // ""
MC/DC0%0/1
$hash->{"id"} // ""
$hash->{"id"}
Truth table50%1/2
A
A= $hash->{"id"}
| |||||||||||||||||||||||||||||
| 60 | ); | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | sub from_anthropic { | ||||||||||||||||||||||||||||
| 64 | 12 | ▶ | my ($class, $block) = @_; | ||||||||||||||||||||||||||
|
Subroutine
from_anthropic: called CC=8 SCAR=22.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 65 | 12 | ▶ | return undef unless ref($block) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 12 | ▶ | return undef unless ( $block->{type} // '' ) eq 'tool_use'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$block->{"type"} // ""
MC/DC0%0/1
$block->{"type"} // ""
$block->{"type"}
Truth table50%1/2
A
A= $block->{"type"}
| |||||||||||||||||||||||||||||
| 67 | 3 | ▶ | my $name = $block->{name} // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$block->{"name"} // ""
MC/DC0%0/1
$block->{"name"} // ""
$block->{"name"}
Truth table50%1/2
A
A= $block->{"name"}
| |||||||||||||||||||||||||||||
| 68 | 3 | ▶ | return undef unless length $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | return $class->new( | ||||||||||||||||||||||||||||
| 70 | name => $name, | ||||||||||||||||||||||||||||
| 71 | arguments => ( ref( $block->{input} ) eq 'HASH' ? $block->{input} : {} ), | ||||||||||||||||||||||||||||
| 72 | 3 | ▶ | id => ( $block->{id} // '' ), | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$block->{"id"} // ""
MC/DC0%0/1
$block->{"id"} // ""
$block->{"id"}
Truth table50%1/2
A
A= $block->{"id"}
| |||||||||||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | sub from_ollama { | ||||||||||||||||||||||||||||
| 77 | 0 | ▶ | my ($class, $hash) = @_; | ||||||||||||||||||||||||||
|
Subroutine
from_ollama: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | return undef unless ref($hash) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | my $fn = $hash->{function} || {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$hash->{'function'} || {}
MC/DC0%0/1
$hash->{'function'} || {}
$hash->{'function'}
Truth table0%0/2
A
A= $hash->{'function'}
| |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | return undef unless ref($fn) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | my $name = $fn->{name} // ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$fn->{"name"} // ""
MC/DC0%0/1
$fn->{"name"} // ""
$fn->{"name"}
Truth table0%0/2
A
A= $fn->{"name"}
| |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | return undef unless length $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | return $class->new( | ||||||||||||||||||||||||||||
| 84 | name => $name, | ||||||||||||||||||||||||||||
| 85 | arguments => _decode_args( $fn->{arguments} ), | ||||||||||||||||||||||||||||
| 86 | 0 | ▶ | id => ( $hash->{id} // '' ), | ||||||||||||||||||||||||||
|
Condition0%0/2
$hash->{"id"} // ""
MC/DC0%0/1
$hash->{"id"} // ""
$hash->{"id"}
Truth table0%0/2
A
A= $hash->{"id"}
| |||||||||||||||||||||||||||||
| 87 | ); | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # Gemini: a single functionCall part inside candidates[0].content.parts[]: | ||||||||||||||||||||||||||||
| 91 | # { "functionCall": { "name": "x", "args": { ... } } } | ||||||||||||||||||||||||||||
| 92 | sub from_gemini { | ||||||||||||||||||||||||||||
| 93 | 4 | ▶ | my ($class, $part) = @_; | ||||||||||||||||||||||||||
|
Subroutine
from_gemini: called CC=7 SCAR=31.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 94 | 4 | ▶ | return undef unless ref($part) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 4 | my $fc = $part->{functionCall}; | |||||||||||||||||||||||||||
| 96 | 4 | ▶ | return undef unless ref($fc) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | my $name = $fc->{name} // ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$fc->{"name"} // ""
MC/DC0%0/1
$fc->{"name"} // ""
$fc->{"name"}
Truth table0%0/2
A
A= $fc->{"name"}
| |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | return undef unless length $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | return $class->new( | ||||||||||||||||||||||||||||
| 100 | name => $name, | ||||||||||||||||||||||||||||
| 101 | arguments => ( ref( $fc->{args} ) eq 'HASH' ? $fc->{args} : {} ), | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | id => ( $fc->{id} // '' ), | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$fc->{"id"} // ""
MC/DC0%0/1
$fc->{"id"} // ""
$fc->{"id"}
Truth table0%0/2
A
A= $fc->{"id"}
| |||||||||||||||||||||||||||||
| 103 | ); | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # OpenAI Responses API: function_call appears either as a top-level output[] | ||||||||||||||||||||||||||||
| 107 | # item or nested inside output[type=message].content[]. Both shapes look like: | ||||||||||||||||||||||||||||
| 108 | # { "type": "function_call", "call_id": "call_abc", "name": "foo", "arguments": "{...}" } | ||||||||||||||||||||||||||||
| 109 | sub from_responses { | ||||||||||||||||||||||||||||
| 110 | 2 | ▶ | my ($class, $block) = @_; | ||||||||||||||||||||||||||
|
Subroutine
from_responses: called CC=8 SCAR=23.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 111 | 2 | ▶ | return undef unless ref($block) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 2 | ▶ | return undef unless ( $block->{type} // '' ) eq 'function_call'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$block->{"type"} // ""
MC/DC0%0/1
$block->{"type"} // ""
$block->{"type"}
Truth table50%1/2
A
A= $block->{"type"}
| |||||||||||||||||||||||||||||
| 113 | 2 | ▶ | my $name = $block->{name} // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$block->{"name"} // ""
MC/DC0%0/1
$block->{"name"} // ""
$block->{"name"}
Truth table50%1/2
A
A= $block->{"name"}
| |||||||||||||||||||||||||||||
| 114 | 2 | ▶ | return undef unless length $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 2 | my $args = $block->{arguments}; | |||||||||||||||||||||||||||
| 116 | 2 | $args = _decode_args($args); | |||||||||||||||||||||||||||
| 117 | return $class->new( | ||||||||||||||||||||||||||||
| 118 | name => $name, | ||||||||||||||||||||||||||||
| 119 | arguments => ( ref($args) eq 'HASH' ? $args : {} ), | ||||||||||||||||||||||||||||
| 120 | 2 | ▶ | id => ( $block->{call_id} // '' ), | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$block->{"call_id"} // ""
MC/DC0%0/1
$block->{"call_id"} // ""
$block->{"call_id"}
Truth table50%1/2
A
A= $block->{"call_id"}
| |||||||||||||||||||||||||||||
| 121 | ); | ||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # Pull every tool call out of an upstream response, in any of the formats | ||||||||||||||||||||||||||||
| 125 | # we know about. Returns a list of ToolCall objects (possibly empty). | ||||||||||||||||||||||||||||
| 126 | sub extract { | ||||||||||||||||||||||||||||
| 127 | 31 | ▶ | my ($class, $raw) = @_; | ||||||||||||||||||||||||||
|
Subroutine
extract: called CC=22 SCAR=32.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 128 | 31 | ▶ | return () unless ref($raw) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # OpenAI shape: choices[0].message.tool_calls | ||||||||||||||||||||||||||||
| 131 | 31 | ▶ | if ( my $oai_msg = $raw->{choices}[0]{message} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 18 | ▶ | if ( ref( $oai_msg->{tool_calls} ) eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 3 | return grep { defined } map { $class->from_openai($_) } @{ $oai_msg->{tool_calls} }; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | # Ollama shape: message.tool_calls | ||||||||||||||||||||||||||||
| 138 | 28 | ▶ | if ( my $msg = $raw->{message} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 2 | ▶ | if ( ref( $msg->{tool_calls} ) eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 0 | return grep { defined } map { $class->from_ollama($_) } @{ $msg->{tool_calls} }; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | # Anthropic shape: content[*] where type=tool_use | ||||||||||||||||||||||||||||
| 145 | 28 | ▶ | if ( ref( $raw->{content} ) eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 6 | return grep { defined } map { $class->from_anthropic($_) } @{ $raw->{content} }; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | # Gemini shape: candidates[0].content.parts[*].functionCall | ||||||||||||||||||||||||||||
| 150 | 22 | ▶ | if ( ref( $raw->{candidates} ) eq 'ARRAY' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $raw->{"candidates"} eq "ARRAY" and ref $raw->{"candidates"}[0]{"content"}{"parts"} eq "ARRAY"
MC/DC50%1/2
ref $raw->{"candidates"} eq "ARRAY" and ref $raw->{"candidates"}[0]{"content"}{"parts"} eq "ARRAY"
ref $raw->{"candidates"} eq "ARRAY"
ref $raw->{"candidates"}[0]{"content"}{"parts"} eq "ARRAY"
Truth table66%2/3
A and B
A= ref $raw->{"candidates"} eq "ARRAY"B= ref $raw->{"candidates"}[0]{"content"}{"parts"} eq "ARRAY"
| |||||||||||||||||||||||||||||
| 151 | && ref( $raw->{candidates}[0]{content}{parts} ) eq 'ARRAY' ) { | ||||||||||||||||||||||||||||
| 152 | 4 | return grep { defined } | |||||||||||||||||||||||||||
| 153 | 4 | map { $class->from_gemini($_) } | |||||||||||||||||||||||||||
| 154 | 3 | @{ $raw->{candidates}[0]{content}{parts} }; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | # OpenAI Responses shape: function_call appears either as a top-level | ||||||||||||||||||||||||||||
| 158 | # output[] item (real API) or nested under output[type=message].content[] | ||||||||||||||||||||||||||||
| 159 | # (older / streaming shape). Walk both. | ||||||||||||||||||||||||||||
| 160 | 19 | ▶ | if ( ref( $raw->{output} ) eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 2 | my @calls; | |||||||||||||||||||||||||||
| 162 | 2 | for my $item (@{$raw->{output}}) { | |||||||||||||||||||||||||||
| 163 | 3 | ▶ | next unless ref($item) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 3 | ▶ | my $type = $item->{type} // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$item->{"type"} // ""
MC/DC0%0/1
$item->{"type"} // ""
$item->{"type"}
Truth table50%1/2
A
A= $item->{"type"}
| |||||||||||||||||||||||||||||
| 165 | 3 | ▶ | if ( $type eq 'function_call' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 166 | 1 | ▶ | if ( my $tc = $class->from_responses($item) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 1 | push @calls, $tc; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | elsif ( $type eq 'message' ) { | ||||||||||||||||||||||||||||
| 171 | 1 | ▶ | for my $block (@{$item->{content} // []}) { | ||||||||||||||||||||||||||
|
Condition50%1/2
$item->{"content"} // []
MC/DC0%0/1
$item->{"content"} // []
$item->{"content"}
Truth table50%1/2
A
A= $item->{"content"}
| |||||||||||||||||||||||||||||
| 172 | 1 | ▶ | if (($block->{type} // '') eq 'function_call') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$block->{"type"} // ""
MC/DC0%0/1
$block->{"type"} // ""
$block->{"type"}
Truth table50%1/2
A
A= $block->{"type"}
| |||||||||||||||||||||||||||||
| 173 | 1 | ▶ | if (my $tc = $class->from_responses($block)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 1 | push @calls, $tc; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | 2 | ▶ | return @calls if @calls; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 17 | return (); | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | # Hermes-style XML embedded in plain text. Returns ($cleaned_text, \@calls). | ||||||||||||||||||||||||||||
| 187 | sub extract_hermes_from_text { | ||||||||||||||||||||||||||||
| 188 | 2 | ▶ | my ($class, $text) = @_; | ||||||||||||||||||||||||||
|
Subroutine
extract_hermes_from_text: called CC=7 SCAR=20.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 189 | 2 | ▶ | my $clean = defined($text) ? $text : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | 2 | my @calls; | |||||||||||||||||||||||||||
| 191 | 2 | ▶ | while ( $clean =~ m{<tool_call>\s*(.*?)\s*</tool_call>}sg ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 2 | my $json = $1; | |||||||||||||||||||||||||||
| 193 | 2 | my $obj = eval { decode_json($json) }; | |||||||||||||||||||||||||||
| 194 | 2 | ▶ | next unless ref($obj) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | 2 | ▶ | next unless defined $obj->{name} && length $obj->{name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $obj->{"name"} and length $obj->{"name"}
MC/DC0%0/2
defined $obj->{"name"} and length $obj->{"name"}
defined $obj->{"name"}
length $obj->{"name"}
Truth table33%1/3
A and B
A= defined $obj->{"name"}B= length $obj->{"name"}
| |||||||||||||||||||||||||||||
| 196 | push @calls, $class->new( | ||||||||||||||||||||||||||||
| 197 | name => $obj->{name}, | ||||||||||||||||||||||||||||
| 198 | 2 | ▶ | arguments => ( ref( $obj->{arguments} ) eq 'HASH' ? $obj->{arguments} : {} ), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | ); | ||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | 2 | $clean =~ s{<tool_call>.*?</tool_call>}{}sg; | |||||||||||||||||||||||||||
| 202 | 2 | $clean =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||
| 203 | 2 | return ( $clean, \@calls ); | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | # --- Serializers to wire-format hashes --- | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | sub to_openai { | ||||||||||||||||||||||||||||
| 209 | 3 | ▶ | my ($self, %opts) = @_; | ||||||||||||||||||||||||||
|
Subroutine
to_openai: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 210 | 3 | ▶ | my $id = length( $self->id ) ? $self->id : ( $opts{fallback_id} // 'call_langertha' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$opts{"fallback_id"} // "call_langertha"
MC/DC0%0/1
$opts{"fallback_id"} // "call_langertha"
$opts{"fallback_id"}
Truth table50%1/2
A
A= $opts{"fallback_id"}
| |||||||||||||||||||||||||||||
| 211 | return { | ||||||||||||||||||||||||||||
| 212 | 3 | id => $id, | |||||||||||||||||||||||||||
| 213 | type => 'function', | ||||||||||||||||||||||||||||
| 214 | function => { | ||||||||||||||||||||||||||||
| 215 | name => $self->name, | ||||||||||||||||||||||||||||
| 216 | arguments => encode_json( $self->arguments ), | ||||||||||||||||||||||||||||
| 217 | }, | ||||||||||||||||||||||||||||
| 218 | }; | ||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | sub to_anthropic_block { | ||||||||||||||||||||||||||||
| 222 | 2 | ▶ | my ($self, %opts) = @_; | ||||||||||||||||||||||||||
|
Subroutine
to_anthropic_block: called CC=3 SCAR=13.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 223 | 2 | ▶ | my $id = length( $self->id ) ? $self->id : ( $opts{fallback_id} // 'toolu_langertha' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$opts{"fallback_id"} // "toolu_langertha"
MC/DC0%0/1
$opts{"fallback_id"} // "toolu_langertha"
$opts{"fallback_id"}
Truth table0%0/2
A
A= $opts{"fallback_id"}
| |||||||||||||||||||||||||||||
| 224 | return { | ||||||||||||||||||||||||||||
| 225 | 2 | type => 'tool_use', | |||||||||||||||||||||||||||
| 226 | id => $id, | ||||||||||||||||||||||||||||
| 227 | name => $self->name, | ||||||||||||||||||||||||||||
| 228 | input => $self->arguments, | ||||||||||||||||||||||||||||
| 229 | }; | ||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | sub to_ollama { | ||||||||||||||||||||||||||||
| 233 | 2 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
to_ollama: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 234 | return { | ||||||||||||||||||||||||||||
| 235 | 2 | ▶ | function => { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | name => $self->name, | ||||||||||||||||||||||||||||
| 237 | arguments => $self->arguments, | ||||||||||||||||||||||||||||
| 238 | }, | ||||||||||||||||||||||||||||
| 239 | ( length( $self->id ) ? ( id => $self->id ) : () ), | ||||||||||||||||||||||||||||
| 240 | }; | ||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | sub to_hash { | ||||||||||||||||||||||||||||
| 244 | 3 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
to_hash: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 245 | return { | ||||||||||||||||||||||||||||
| 246 | 3 | id => $self->id, | |||||||||||||||||||||||||||
| 247 | name => $self->name, | ||||||||||||||||||||||||||||
| 248 | arguments => $self->arguments, | ||||||||||||||||||||||||||||
| 249 | }; | ||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | 39 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 253 | 39 | 1; | |||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | __END__ | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | =pod | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | =head1 NAME | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | Langertha::ToolCall - Immutable canonical tool invocation emitted by an LLM | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | =head1 VERSION | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | version 0.502 | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | =head2 synthetic | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | Boolean. True when the tool call was synthesized by Langertha — for | ||||||||||||||||||||||||||||
| 272 | example when L<Langertha::Role::Chat/chat_f> rewrote a forced named | ||||||||||||||||||||||||||||
| 273 | tool into a C<response_format> JSON Schema request and parsed the | ||||||||||||||||||||||||||||
| 274 | output back into a C<ToolCall>. False (the default) for native model | ||||||||||||||||||||||||||||
| 275 | output. | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | =head2 Issues | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||
| 282 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | =head2 IRC | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 301 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | =cut | ||||||||||||||||||||||||||||