| 1 | package Langertha::Knarr::Response; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Normalized chat response shared across all Knarr handlers and protocol formatters | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 51 | our $VERSION = '1.103'; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 51 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 51 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 51 | ▶ | use Langertha::Usage; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 51 | has content => ( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | default => '', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | has model => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 36 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 51 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | has upstream_model => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 83 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 51 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | has usage => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | isa => 'Maybe[Object]', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 40 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 51 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | has tool_calls => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | isa => 'ArrayRef', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 35 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 51 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | has finish_reason => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 25 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 51 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | has id => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 42 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 51 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | has timing => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | isa => 'Maybe[HashRef]', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 42 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 51 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | has thinking => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 42 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 51 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | has rate_limit => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | isa => 'Maybe[Object]', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 42 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 51 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | has raw => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 42 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 51 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | # Upgrade a raw usage HashRef into a Langertha::Usage. Done here rather | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | # than in from_langertha_response so every door into the object is | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | # covered — the Langertha::Response path, a handler returning a | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | # { content => ..., usage => {...} } hashref, and clone_with alike. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | # Already-blessed usage passes untouched, so this stays correct if a | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | # later Langertha coerces on its own side. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 125 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 125 | my ($orig, $class, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 125 | my $params = $class->$orig(@args); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 125 | ▶ | if ( ref $params->{usage} eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 1 | $params->{usage} = Langertha::Usage->from_hash( $params->{usage} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 125 | return $params; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 51 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 113 | ▶ | sub coerce { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
coerce: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 113 | my ($class, $thing) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 113 | ▶ | return $class->new() unless defined $thing; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 112 | ▶ | if (blessed $thing) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 95 | ▶ | return $thing if $thing->isa($class); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 41 | ▶ | return $class->from_langertha_response($thing) if $thing->isa('Langertha::Response'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | return $class->new( content => "$thing" ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 17 | ▶ | if (ref $thing eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 3 | return $class->new( %$thing ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 14 | return $class->new( content => "$thing" ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 41 | ▶ | sub from_langertha_response { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
from_langertha_response: called CC=11 SCAR=29.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 41 | my ($class, $r) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 41 | ▶ | return $class->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 9 branches
Condition100%2/2
$r->tool_calls // []
MC/DC100%1/1
$r->tool_calls // []
$r->tool_calls
Truth table100%2/2
A
A= $r->tool_calls
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | content => "$r", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | model => ( $r->can('model') ? $r->model : undef ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | usage => ( $r->can('usage') ? $r->usage : undef ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | tool_calls => ( $r->can('tool_calls') ? ( $r->tool_calls // [] ) : [] ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | finish_reason => ( $r->can('finish_reason') ? $r->finish_reason : undef ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | id => ( $r->can('id') ? $r->id : undef ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | timing => ( $r->can('timing') ? $r->timing : undef ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | thinking => ( $r->can('thinking') ? $r->thinking : undef ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | rate_limit => ( $r->can('rate_limit') ? $r->rate_limit : undef ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | raw => ( $r->can('raw') ? $r->raw : undef ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 2 | ▶ | sub ttft_seconds { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ttft_seconds: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 2 | ▶ | my $t = $self->timing or return undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 1 | return $t->{ttft_seconds}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 2 | ▶ | sub total_seconds { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
total_seconds: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 2 | ▶ | my $t = $self->timing or return undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 1 | return $t->{total_seconds}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 70 | ▶ | sub has_tool_calls { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
has_tool_calls: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 70 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 70 | return scalar @{ $self->tool_calls } > 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 42 | ▶ | sub clone_with { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clone_with: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 42 | my ($self, %override) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 42 | return ref($self)->new( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | content => $self->content, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | model => $self->model, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | upstream_model => $self->upstream_model, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | usage => $self->usage, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | tool_calls => $self->tool_calls, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | finish_reason => $self->finish_reason, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | id => $self->id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | timing => $self->timing, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | thinking => $self->thinking, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | rate_limit => $self->rate_limit, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | raw => $self->raw, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | %override, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 51 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 51 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||