| 1 | package Langertha::Knarr::Response; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Normalized chat response shared across all Knarr handlers and protocol formatters | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 97 | our $VERSION = '1.102'; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 97 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 97 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 97 | ▶ | use Langertha::Usage; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 97 | has content => ( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | default => '', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | has model => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 202 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 97 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | has upstream_model => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 385 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 97 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | has usage => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | isa => 'Maybe[Object]', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 264 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 97 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | has tool_calls => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | isa => 'ArrayRef', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 203 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 97 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | has finish_reason => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 176 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 97 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | has id => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 271 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 97 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | has timing => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | isa => 'Maybe[HashRef]', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 271 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 97 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | has thinking => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 271 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 97 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | has rate_limit => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | isa => 'Maybe[Object]', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 271 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 97 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | has raw => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 269 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 97 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | 647 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 647 | my ($orig, $class, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 647 | my $params = $class->$orig(@args); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 647 | ▶ | if ( ref $params->{usage} eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 6 | $params->{usage} = Langertha::Usage->from_hash( $params->{usage} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 647 | return $params; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 97 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 618 | ▶ | sub coerce { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
coerce: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 618 | my ($class, $thing) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 618 | ▶ | return $class->new() unless defined $thing; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 617 | ▶ | if (blessed $thing) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 451 | ▶ | return $thing if $thing->isa($class); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 115 | ▶ | return $class->from_langertha_response($thing) if $thing->isa('Langertha::Response'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | return $class->new( content => "$thing" ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 166 | ▶ | if (ref $thing eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 7 | return $class->new( %$thing ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 159 | return $class->new( content => "$thing" ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 115 | ▶ | sub from_langertha_response { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
from_langertha_response: called CC=11 SCAR=29.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 115 | my ($class, $r) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 115 | ▶ | 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 | 373 | ▶ | sub has_tool_calls { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
has_tool_calls: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 373 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 373 | return scalar @{ $self->tool_calls } > 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 261 | ▶ | sub clone_with { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clone_with: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 261 | my ($self, %override) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 261 | 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 | 97 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 97 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | Langertha::Knarr::Response - Normalized chat response shared across all Knarr handlers and protocol formatters | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | version 1.102 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | The single shape every L<Langertha::Knarr::Handler> returns and every | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | L<Langertha::Knarr::Protocol> formatter consumes. Mirrors | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | L<Langertha::Response> but is decoupled from it so non-engine handlers | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | (L<Langertha::Knarr::Handler::Code>, L<Langertha::Knarr::Handler::A2AClient>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | L<Langertha::Knarr::Handler::ACPClient>) can produce a Knarr response | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | without going through Langertha first. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | L</coerce> upgrades all the legacy shapes Knarr handlers used to | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | return — a bare string, a C<{ content =E<gt> ..., model =E<gt> ... }> | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | hashref, or a stringifiable L<Langertha::Response> — into a | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | proper value object. So existing call sites can pass anything they | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | already had and downstream code can rely on a single API. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | =head2 content | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | Plain assistant text. Defaults to empty string. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | =head2 model | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | The model id the response is labeled with for the client, if known: the | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | model that produced it, or the configured name a handler answers under | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | (L<Langertha::Knarr::Handler::Router> labels a routed answer with the model | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | of its config entry). | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | =head2 upstream_model | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | The model the backend reported answering with, when a handler relabeled | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | L</model> for the client; C<undef> otherwise, L</model> then being the | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | reported one. Providers answer with a concrete name for the one asked | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | (C<gpt-4o> answers as C<gpt-4o-2024-08-06>); the Langfuse generation records | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | this one, see L<Langertha::Knarr::Handler::Tracing>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | =head2 usage | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | A L<Langertha::Usage> object with token counts, if the engine reported | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | them. C<undef> for handlers that have no usage data (Code, Passthrough). | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | A plain HashRef is accepted and upgraded with | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | L<Langertha::Usage>'s C<from_hash> — L<Langertha::Response> declares its own | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | C<usage> as C<Maybe[HashRef]> and the engines pass the provider's raw | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | JSON hash straight through, so that is the shape every real engine | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | response arrives in. The upgrade normalizes the provider spellings | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | (C<prompt_tokens> / C<input_tokens> / C<prompt_eval_count>, ...), which | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | is what lets the protocol formatters call C<to_openai_format> and | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | friends on whatever any handler produced. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | =head2 tool_calls | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | ArrayRef of L<Langertha::ToolCall> objects produced by the engine. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | Empty arrayref when the response is plain text. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | =head2 finish_reason | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | Provider-agnostic stop reason (C<stop>, C<tool_calls>, C<length>, ...). | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | Optional; the protocol formatters fall back to C<stop> / C<end_turn> | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | when undef. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | =head2 id | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | The provider-side response id (OpenAI C<chatcmpl-...>, Anthropic | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | C<msg_...>), when the engine reported one. Carried so a Knarr trace can | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | be correlated with the provider's own logs. C<undef> for handlers that | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | have no upstream id. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | =head2 timing | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | HashRef of engine-measured durations, mirroring | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | L<Langertha::Response/timing>. The two keys every Langertha engine | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | populates are C<ttft_seconds> and C<total_seconds> (Float, seconds); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | provider-native stage durations (Ollama's C<load_seconds>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | C<prompt_eval_seconds>, C<eval_seconds>, ...) may be present too. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | Only the routed path has this — it comes from the engine's own | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | measurement inside L<Langertha>. Raw passthrough never produces a | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | L<Langertha::Response> and therefore never a C<timing>; see | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | L<Langertha::Knarr::Tracing/Timing sources> for which path reports | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | latency from where. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | =head2 thinking | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | Chain-of-thought / reasoning text the engine separated from C<content> | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | (DeepSeek C<reasoning_content>, Anthropic thinking blocks, or | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | L<Langertha::Role::ThinkTag> filtering). Carried because it is model | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | output that C<content> no longer holds — without it the reasoning is | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | lost at the proxy boundary. Recorded into the Langfuse generation | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | metadata; the protocol formatters currently do not emit it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | =head2 rate_limit | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | Optional L<Langertha::RateLimit> object built from the upstream | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | provider's quota headers. Kept as the object; consumers pull the | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | scalar fields they need. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | =head2 raw | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | Optional. The provider-native response body, kept around for handlers | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | (passthrough-style) that want to preserve every byte upstream returned. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | =head2 coerce | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | my $r = Langertha::Knarr::Response->coerce( $whatever ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | Class method. Accepts: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | =over | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | =item * an existing C<Langertha::Knarr::Response> — returned as-is. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | =item * a L<Langertha::Response> — fields lifted via | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | C<from_langertha_response>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | =item * any other blessed object that stringifies — used as C<content>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | =item * a HashRef — fed to C<new> after key normalization. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | =item * a plain scalar — used as C<content>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | =item * C<undef> — produces an empty response. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | This is the single normalization entry point. Handlers can return | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | whatever shape is convenient and the dispatcher coerces once at the | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | boundary. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | =head2 from_langertha_response | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | my $r = Langertha::Knarr::Response->from_langertha_response($lresp); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | Builds a Knarr response from a L<Langertha::Response>. Carries | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | C<content>, C<model>, C<usage>, C<tool_calls>, C<finish_reason>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | C<id>, C<timing>, C<thinking>, C<rate_limit>, and C<raw> across. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | C<usage> arrives as the provider's raw HashRef and is upgraded to a | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | L<Langertha::Usage> on the way in; see L</usage>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | Every field is read behind a C<can()> guard so Knarr keeps working | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | against a L<Langertha> release that predates one of them — the older | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | attributes were added over several Langertha versions and C<timing> / | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | C<rate_limit> / C<thinking> are the most recent. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | =head2 ttft_seconds | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | Time-to-first-token in seconds (Float) out of L</timing>, or C<undef> | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | when the engine did not measure it (non-streaming calls, or any handler | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | that is not engine-backed). | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | =head2 total_seconds | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | Total engine-measured call duration in seconds (Float) out of | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | L</timing>, or C<undef>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | =head2 has_tool_calls | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | True when C<tool_calls> contains at least one entry. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | =head2 clone_with | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | my $r2 = $r->clone_with( model => 'override' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | Returns a new response with the given fields overridden. All other | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | attributes carry through from C<$self>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | =head2 Issues | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | L<https://github.com/Getty/langertha-knarr/issues>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | =head2 IRC | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/> | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | This software is copyright (c) 2026 by Torsten Raudssus. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||