| 1 | package Langertha::Knarr::Response; | ||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Normalized chat response shared across all Knarr handlers and protocol formatters | ||||||||||||||||||||||||||||||||||||
| 3 | 19 | our $VERSION = '1.100'; | |||||||||||||||||||||||||||||||||||
| 4 | 19 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 5 | 19 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||
| 8 | 19 | has content => ( | |||||||||||||||||||||||||||||||||||
| 9 | is => 'ro', | ||||||||||||||||||||||||||||||||||||
| 10 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||
| 11 | default => '', | ||||||||||||||||||||||||||||||||||||
| 12 | ); | ||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||
| 14 | has model => ( | ||||||||||||||||||||||||||||||||||||
| 15 | is => 'ro', | ||||||||||||||||||||||||||||||||||||
| 16 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||||||||||
| 17 | 36 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 18 | 19 | ); | |||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||
| 20 | has usage => ( | ||||||||||||||||||||||||||||||||||||
| 21 | is => 'ro', | ||||||||||||||||||||||||||||||||||||
| 22 | isa => 'Maybe[Object]', | ||||||||||||||||||||||||||||||||||||
| 23 | 45 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 24 | 19 | ); | |||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||
| 26 | has tool_calls => ( | ||||||||||||||||||||||||||||||||||||
| 27 | is => 'ro', | ||||||||||||||||||||||||||||||||||||
| 28 | isa => 'ArrayRef', | ||||||||||||||||||||||||||||||||||||
| 29 | 44 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 30 | 19 | ); | |||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||
| 32 | has finish_reason => ( | ||||||||||||||||||||||||||||||||||||
| 33 | is => 'ro', | ||||||||||||||||||||||||||||||||||||
| 34 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||||||||||
| 35 | 45 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 36 | 19 | ); | |||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||
| 38 | has raw => ( | ||||||||||||||||||||||||||||||||||||
| 39 | is => 'ro', | ||||||||||||||||||||||||||||||||||||
| 40 | 44 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 41 | 19 | ); | |||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||
| 44 | sub coerce { | ||||||||||||||||||||||||||||||||||||
| 45 | 76 | ▶ | my ($class, $thing) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
coerce: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 46 | 76 | ▶ | return $class->new() unless defined $thing; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 47 | 75 | ▶ | if (blessed $thing) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 48 | 41 | ▶ | return $thing if $thing->isa($class); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 49 | 4 | ▶ | return $class->from_langertha_response($thing) if $thing->isa('Langertha::Response'); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 50 | 0 | return $class->new( content => "$thing" ); | |||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||
| 52 | 34 | ▶ | if (ref $thing eq 'HASH') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 53 | 2 | return $class->new( %$thing ); | |||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||
| 55 | 32 | return $class->new( content => "$thing" ); | |||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||
| 59 | sub from_langertha_response { | ||||||||||||||||||||||||||||||||||||
| 60 | 4 | ▶ | my ($class, $r) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
from_langertha_response: called CC=7 SCAR=23.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 61 | 4 | ▶ | return $class->new( | ||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
Condition50%1/2
$r->tool_calls // []
MC/DC0%0/1
$r->tool_calls // []
$r->tool_calls
Truth table50%1/2
A
A= $r->tool_calls
| |||||||||||||||||||||||||||||||||||||
| 62 | content => "$r", | ||||||||||||||||||||||||||||||||||||
| 63 | model => ( $r->can('model') ? $r->model : undef ), | ||||||||||||||||||||||||||||||||||||
| 64 | usage => ( $r->can('usage') ? $r->usage : undef ), | ||||||||||||||||||||||||||||||||||||
| 65 | tool_calls => ( $r->can('tool_calls') ? ( $r->tool_calls // [] ) : [] ), | ||||||||||||||||||||||||||||||||||||
| 66 | finish_reason => ( $r->can('finish_reason') ? $r->finish_reason : undef ), | ||||||||||||||||||||||||||||||||||||
| 67 | raw => ( $r->can('raw') ? $r->raw : undef ), | ||||||||||||||||||||||||||||||||||||
| 68 | ); | ||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||
| 72 | sub has_tool_calls { | ||||||||||||||||||||||||||||||||||||
| 73 | 32 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
has_tool_calls: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 74 | 32 | return scalar @{ $self->tool_calls } > 0; | |||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||
| 78 | sub clone_with { | ||||||||||||||||||||||||||||||||||||
| 79 | 33 | ▶ | my ($self, %override) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
clone_with: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 80 | 33 | return ref($self)->new( | |||||||||||||||||||||||||||||||||||
| 81 | content => $self->content, | ||||||||||||||||||||||||||||||||||||
| 82 | model => $self->model, | ||||||||||||||||||||||||||||||||||||
| 83 | usage => $self->usage, | ||||||||||||||||||||||||||||||||||||
| 84 | tool_calls => $self->tool_calls, | ||||||||||||||||||||||||||||||||||||
| 85 | finish_reason => $self->finish_reason, | ||||||||||||||||||||||||||||||||||||
| 86 | raw => $self->raw, | ||||||||||||||||||||||||||||||||||||
| 87 | %override, | ||||||||||||||||||||||||||||||||||||
| 88 | ); | ||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||
| 91 | 19 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||
| 92 | 19 | 1; | |||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||
| 94 | __END__ | ||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||
| 96 | =pod | ||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||
| 98 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||
| 100 | =head1 NAME | ||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||
| 102 | Langertha::Knarr::Response - Normalized chat response shared across all Knarr handlers and protocol formatters | ||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||
| 104 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||
| 106 | version 1.100 | ||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||
| 108 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||
| 110 | The single shape every L<Langertha::Knarr::Handler> returns and every | ||||||||||||||||||||||||||||||||||||
| 111 | L<Langertha::Knarr::Protocol> formatter consumes. Mirrors | ||||||||||||||||||||||||||||||||||||
| 112 | L<Langertha::Response> but is decoupled from it so non-engine handlers | ||||||||||||||||||||||||||||||||||||
| 113 | (L<Langertha::Knarr::Handler::Code>, L<Langertha::Knarr::Handler::A2AClient>, | ||||||||||||||||||||||||||||||||||||
| 114 | L<Langertha::Knarr::Handler::ACPClient>) can produce a Knarr response | ||||||||||||||||||||||||||||||||||||
| 115 | without going through Langertha first. | ||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||
| 117 | C<BUILDARGS> upgrades all the legacy shapes Knarr handlers used to | ||||||||||||||||||||||||||||||||||||
| 118 | return — a bare string, a C<{ content =E<gt> ..., model =E<gt> ... }> | ||||||||||||||||||||||||||||||||||||
| 119 | hashref, or a stringifiable L<Langertha::Response> — into a | ||||||||||||||||||||||||||||||||||||
| 120 | proper value object. So existing call sites can pass anything they | ||||||||||||||||||||||||||||||||||||
| 121 | already had and downstream code can rely on a single API. | ||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||
| 123 | =head2 content | ||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||
| 125 | Plain assistant text. Defaults to empty string. | ||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||
| 127 | =head2 model | ||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||
| 129 | The model id that produced the response, if known. | ||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||
| 131 | =head2 usage | ||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||
| 133 | A L<Langertha::Usage> object with token counts, if the engine reported | ||||||||||||||||||||||||||||||||||||
| 134 | them. C<undef> for handlers that have no usage data (Code, Passthrough). | ||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||
| 136 | =head2 tool_calls | ||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||
| 138 | ArrayRef of L<Langertha::ToolCall> objects produced by the engine. | ||||||||||||||||||||||||||||||||||||
| 139 | Empty arrayref when the response is plain text. | ||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||
| 141 | =head2 finish_reason | ||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||
| 143 | Provider-agnostic stop reason (C<stop>, C<tool_calls>, C<length>, ...). | ||||||||||||||||||||||||||||||||||||
| 144 | Optional; the protocol formatters fall back to C<stop> / C<end_turn> | ||||||||||||||||||||||||||||||||||||
| 145 | when undef. | ||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||
| 147 | =head2 raw | ||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||
| 149 | Optional. The provider-native response body, kept around for handlers | ||||||||||||||||||||||||||||||||||||
| 150 | (passthrough-style) that want to preserve every byte upstream returned. | ||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||
| 152 | =head2 coerce | ||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||
| 154 | my $r = Langertha::Knarr::Response->coerce( $whatever ); | ||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||
| 156 | Class method. Accepts: | ||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||
| 158 | =over | ||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||
| 160 | =item * an existing C<Langertha::Knarr::Response> — returned as-is. | ||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||
| 162 | =item * a L<Langertha::Response> — fields lifted via | ||||||||||||||||||||||||||||||||||||
| 163 | C<from_langertha_response>. | ||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||
| 165 | =item * any other blessed object that stringifies — used as C<content>. | ||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||
| 167 | =item * a HashRef — fed to C<new> after key normalization. | ||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||
| 169 | =item * a plain scalar — used as C<content>. | ||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||
| 171 | =item * C<undef> — produces an empty response. | ||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||
| 173 | =back | ||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||
| 175 | This is the single normalization entry point. Handlers can return | ||||||||||||||||||||||||||||||||||||
| 176 | whatever shape is convenient and the dispatcher coerces once at the | ||||||||||||||||||||||||||||||||||||
| 177 | boundary. | ||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||
| 179 | =head2 from_langertha_response | ||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||
| 181 | my $r = Langertha::Knarr::Response->from_langertha_response($lresp); | ||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||
| 183 | Builds a Knarr response from a L<Langertha::Response>. Carries | ||||||||||||||||||||||||||||||||||||
| 184 | C<content>, C<model>, C<usage>, C<tool_calls>, C<finish_reason>, and | ||||||||||||||||||||||||||||||||||||
| 185 | C<raw> across. | ||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||
| 187 | =head2 has_tool_calls | ||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||
| 189 | True when C<tool_calls> contains at least one entry. | ||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||
| 191 | =head2 clone_with | ||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||
| 193 | my $r2 = $r->clone_with( model => 'override' ); | ||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||
| 195 | Returns a new response with the given fields overridden. All other | ||||||||||||||||||||||||||||||||||||
| 196 | attributes carry through from C<$self>. | ||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||
| 198 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||
| 200 | =head2 Issues | ||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||
| 202 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||||||||
| 203 | L<https://github.com/Getty/langertha-knarr/issues>. | ||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||
| 205 | =head2 IRC | ||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||
| 207 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||
| 209 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||
| 211 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||
| 213 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||
| 215 | Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/> | ||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||
| 217 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||
| 219 | This software is copyright (c) 2026 by Torsten Raudssus. | ||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||
| 221 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||
| 222 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||
| 224 | =cut | ||||||||||||||||||||||||||||||||||||