blib/lib/Langertha/Knarr/Protocol.pm

filter: Statementstmt 44.6% 21 / 47 Branchbran 8.3% 1 / 12 Conditioncond 11.1% 1 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 25.0% 4 / 16 Podpod 90.9% 10 / 11 totaltotal 36.6% 37 / 101 CC 11 SCAR 22.6
CC
11
Cov
34%
request_body_error
30.0
_status_error_code
12.0
_forward_headers
8.1
CRAP 9.6
« blib/lib/Langertha/Knarr/PSGI/FakeReq.pm ↑ Summary blib/lib/Langertha/Knarr/Role/UpstreamHTTP.pm »
1 package Langertha::Knarr::Protocol;
2 # ABSTRACT: Role for Knarr wire protocols (OpenAI, Anthropic, Ollama, A2A, ACP, AG-UI)
3 9 our $VERSION = '1.103';
4 9 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 # Identifier (e.g. 'openai', 'anthropic', 'ollama').
10 9 requires 'protocol_name';
11  
12 # Returns arrayref of route specs:
13 # [ { method => 'POST', path => '/v1/chat/completions', action => 'chat' }, ... ]
14 9 requires 'protocol_routes';
15  
16 # parse_chat_request($http_req, $body_ref) -> Langertha::Knarr::Request
17 9 requires 'parse_chat_request';
18  
19 # format_chat_response($response, $request) -> ($status, \%headers, $body)
20 9 requires 'format_chat_response';
21  
22 # format_stream_chunk($chunk, $request) -> string (raw bytes for the wire)
23 # Default: SSE-style "data: {...}\n\n" — protocols may override (Ollama uses NDJSON).
24 0 ▶sub format_stream_chunk {
Subroutine format_stream_chunk: not called CC=1 SCAR=6.9
Pod documented
25 0 my ($self, $chunk_json) = @_;
26 0 return "data: $chunk_json\n\n";
27 }
28  
29 0 ▶sub format_stream_done {
Subroutine format_stream_done: not called CC=1 SCAR=6.9
Pod documented
30 0 my ($self) = @_;
31 0 return "data: [DONE]\n\n";
32 }
33  
34 # Optional lifecycle hooks for protocols that need to frame the stream
35 # (Anthropic message_start/stop, A2A status events, ACP run.created, AGUI RUN_STARTED).
36 # Default: empty — protocols like OpenAI / Ollama don't need them.
37 0 ▶sub format_stream_open { '' }
Subroutine format_stream_open: not called CC=1 SCAR=6.9
Pod documented
38 0 ▶sub format_stream_close { '' }
Subroutine format_stream_close: not called CC=1 SCAR=6.9
Pod documented
39  
40 # An error answer in the protocol's own shape; default is the OpenAI wire's.
41 0 ▶sub error_uses_body { 0 }
Subroutine error_uses_body: not called CC=1 SCAR=6.9
Pod documented
42  
43 # The reason a request body cannot be a chat request, or nothing: it is not
44 # JSON, or JSON that is no object (array, string, number, null). An empty
45 # body counts as {} like in the parsers. The texts are fixed -- the
46 # decoder's own message never reaches a client (k81). A protocol with its
47 # own error for a broken body (A2A's JSON-RPC parse error) overrides it.
48 0 ▶sub request_body_error {
Subroutine request_body_error: not called CC=5 SCAR=34.0
Pod documented
49 0 my ($self, $body_ref) = @_;
50 0 my $body = $$body_ref;
51 0 ▶ return unless defined $body && length $body;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body and length $body 0 0 0
Condition0%0/3
defined $body and length $body
!ll&&!rl&&r
000
MC/DC0%0/2
defined $body and length $body
defined $body length $body
Truth table0%0/3
A and B
A= defined $bodyB= length $body
ABresult
0X0
100
111
52 0 my $data = eval { JSON::MaybeXS->new( utf8 => 1 )->decode($body) };
53 0 ▶ return 'request body is not valid JSON' if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
54 0 ▶ return 'request body must be a JSON object' unless ref $data eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 0 0 0
55 0 return;
56 }
57  
58 # The code of a status in the error vocabulary ACP and AG-UI share.
59 0 ▶sub _status_error_code {
Subroutine _status_error_code: not called CC=3 SCAR=24.8
60 0 my ($self, $status) = @_;
61 0 ▶ return $status == 404 ? 'not_found' : $status >= 500 ? 'server_error' : 'invalid_input';
Branch: 2 branches
BranchTrueFalseTotal
$status == 404 ? : 0 0 0
$status >= 500 ? : 0 0 0
62 }
63  
64 0 ▶sub format_error_response {
Subroutine format_error_response: not called CC=1 SCAR=6.9
Pod documented
65 0 my ($self, $status, $message, $request) = @_;
66 0 return ( $status, { 'Content-Type' => 'application/json' },
67 JSON::MaybeXS->new( utf8 => 1, canonical => 1 )->encode({ error => { message => "$message" } }) );
68 }
69  
70 # The frame that marks a stream as failed once its headers are out; ''
71 # when the protocol has none.
72 0 ▶sub format_stream_error { '' }
Subroutine format_stream_error: not called CC=1 SCAR=6.9
Pod documented
73  
74 # Content-Type for streaming responses. Default is SSE; Ollama overrides.
75 0 ▶sub stream_content_type { 'text/event-stream' }
Subroutine stream_content_type: not called CC=1 SCAR=6.9
Pod documented
76  
77 # Manifest publication (k14): undef = not in the provider manifest.
78 0 ▶sub manifest_endpoint { undef }
Subroutine manifest_endpoint: not called CC=1 SCAR=6.9
Pod documented
79  
80 # format_models_response(\@models) -> ($status, \%headers, $body)
81 0 ▶sub format_models_response {
Subroutine format_models_response: not called CC=1 SCAR=6.9
Pod undocumented
82 0 my ($self, $models) = @_;
83 0 return ( 200, { 'Content-Type' => 'application/json' }, '{"data":[]}' );
84 }
85  
86 # The client's headers of the given names as [ name, value ] pairs for a
87 # Request's forward_headers: one pair per line the client sent, repeats in
88 # their order, read with a list-context ->header (k60). A request object
89 # that has no ->header gives none.
90 75 ▶sub _forward_headers {
Subroutine _forward_headers: called CC=5 SCAR=20.9
91 75 my ($self, $http_req, @names) = @_;
92 75 ▶ return [] unless Scalar::Util::blessed($http_req) && $http_req->can('header');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($http_req) and $http_req->can("header") 0 75 75
Condition33%1/3
builtin::blessed($http_req) and $http_req->can("header")
!ll&&!rl&&r
0075
MC/DC0%0/2
builtin::blessed($http_req) and $http_req->can("header")
builtin::blessed($http_req) $http_req->can("header")
Truth table33%1/3
A and B
A= builtin::blessed($http_req)B= $http_req->can("header")
ABresult
0X0
100
111
93 75 my @pairs;
94 75 for my $name (@names) {
95 0 ▶ push @pairs, map { [ $name, $_ ] } grep { defined && length } $http_req->header($name);
Condition0%0/3
defined $_ && length $_
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table0%0/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
96 }
97 75 return \@pairs;
98 }
99  
100 9 1;
101  
102 __END__
« blib/lib/Langertha/Knarr/PSGI/FakeReq.pm ↑ Summary blib/lib/Langertha/Knarr/Role/UpstreamHTTP.pm »