« blib/lib/Langertha/Knarr/Handler/A2AClient.pm
↑ Summary
blib/lib/Langertha/Knarr/PassthroughUsage.pm »
| 1 | package Langertha::Knarr::Manifest; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Build Knarr's provider manifest (/.well-known/langertha.json) from its exposed model surface | ||||||||||||||||||||||||||||
| 3 | 1 | our $VERSION = '1.103'; | |||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Log::Any qw( $log ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | has knarr => ( | |||||||||||||||||||||||||||
| 10 | is => 'ro', | ||||||||||||||||||||||||||||
| 11 | required => 1, | ||||||||||||||||||||||||||||
| 12 | weak_ref => 1, | ||||||||||||||||||||||||||||
| 13 | ); | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | has _cache => ( | |||||||||||||||||||||||||||
| 16 | is => 'rw', | ||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | my $AVAILABLE; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 0 | ▶ | sub available { | ||||||||||||||||||||||||||
|
Subroutine
available: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | $AVAILABLE //= eval { require Langertha::Manifest::Builder; 1 } ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$AVAILABLE //= eval {
do {
require Langertha::Manifest::Builder;
1
}
} ? 1 : 0
MC/DC0%0/1
$AVAILABLE //= eval {
do {
require Langertha::Manifest::Builder;
1
}
} ? 1 : 0
$AVAILABLE
Truth table0%0/2
A
A= $AVAILABLE
| |||||||||||||||||||||||||||||
| 23 | 0 | return $AVAILABLE; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | sub build { | ||||||||||||||||||||||||||
|
Subroutine
build: not called CC=14 SCAR=53.5
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 0 | my ( $self, $base_url ) = @_; | |||||||||||||||||||||||||||
| 29 | 0 | $base_url =~ s{/+\z}{}; | |||||||||||||||||||||||||||
| 30 | 0 | my $knarr = $self->knarr; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | my $builder = Langertha::Manifest::Builder->new( | |||||||||||||||||||||||||||
| 33 | provider_id => 'knarr', | ||||||||||||||||||||||||||||
| 34 | issuer => _origin_of($base_url), | ||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | my $auth_ref; | |||||||||||||||||||||||||||
| 38 | 0 | ▶ | if ( defined $knarr->auth_token && length $knarr->auth_token ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $knarr->auth_token and length $knarr->auth_token
MC/DC0%0/2
defined $knarr->auth_token and length $knarr->auth_token
defined $knarr->auth_token
length $knarr->auth_token
Truth table0%0/3
A and B
A= defined $knarr->auth_tokenB= length $knarr->auth_token
| |||||||||||||||||||||||||||||
| 39 | 0 | $auth_ref = 'api'; | |||||||||||||||||||||||||||
| 40 | 0 | $builder->add_auth( id => $auth_ref, type => 'api_key' ); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | my %allowed = map { $_ => 1 } Langertha::Manifest::Builder->model_capabilities; | |||||||||||||||||||||||||||
| 44 | 0 | my $entries = $self->_model_entries; | |||||||||||||||||||||||||||
| 45 | 0 | my %seen; | |||||||||||||||||||||||||||
| 46 | 0 | for my $proto ( @{ $knarr->_protocol_objects } ) { | |||||||||||||||||||||||||||
| 47 | 0 | ▶ | my $spec = $proto->manifest_endpoint or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | my $endpoint_id = $proto->protocol_name; | |||||||||||||||||||||||||||
| 49 | 0 | ▶ | next if $seen{$endpoint_id}++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | $builder->add_endpoint( | ||||||||||||||||||||||||||||
| 51 | id => $endpoint_id, | ||||||||||||||||||||||||||||
| 52 | dialect => $spec->{dialect}, | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | base_url => $base_url . ( $spec->{path} // '' ), | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$spec->{"path"} // ""
MC/DC0%0/1
$spec->{"path"} // ""
$spec->{"path"}
Truth table0%0/2
A
A= $spec->{"path"}
| |||||||||||||||||||||||||||||
| 54 | ( defined $auth_ref ? ( auth_ref => $auth_ref ) : () ), | ||||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||
| 56 | 0 | ▶ | my %forwarded = map { $_ => 1 } grep { $allowed{$_} } @{ $spec->{capabilities} || [] }; | ||||||||||||||||||||||||||
|
Condition0%0/2
$spec->{'capabilities'} || []
MC/DC0%0/1
$spec->{'capabilities'} || []
$spec->{'capabilities'}
Truth table0%0/2
A
A= $spec->{'capabilities'}
| |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | my %image_format = map { $_ => 1 } @{ $spec->{image_content_formats} || [] }; | ||||||||||||||||||||||||||
|
Condition0%0/2
$spec->{'image_content_formats'} || []
MC/DC0%0/1
$spec->{'image_content_formats'} || []
$spec->{'image_content_formats'}
Truth table0%0/2
A
A= $spec->{'image_content_formats'}
| |||||||||||||||||||||||||||||
| 58 | 0 | for my $entry (@$entries) { | |||||||||||||||||||||||||||
| 59 | 0 | my %caps = map { $_ => 1 } grep { $forwarded{$_} } keys %{ $entry->{capabilities} }; | |||||||||||||||||||||||||||
| 60 | # Only an engine whose content format reads the protocol's image | ||||||||||||||||||||||||||||
| 61 | # parts gets them (k32); with translation (k33) that is every format. | ||||||||||||||||||||||||||||
| 62 | delete $caps{image_input} | ||||||||||||||||||||||||||||
| 63 | 0 | ▶ | unless defined $entry->{content_format} && $image_format{ $entry->{content_format} }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $entry->{"content_format"} and $image_format{$entry->{"content_format"}}
MC/DC0%0/2
defined $entry->{"content_format"} and $image_format{$entry->{"content_format"}}
defined $entry->{"content_format"}
$image_format{$entry->{"content_format"}}
Truth table0%0/3
A and B
A= defined $entry->{"content_format"}B= $image_format{$entry->{"content_format"}}
| |||||||||||||||||||||||||||||
| 64 | eval { | ||||||||||||||||||||||||||||
| 65 | $builder->add_model( | ||||||||||||||||||||||||||||
| 66 | id => $entry->{id}, | ||||||||||||||||||||||||||||
| 67 | 0 | endpoint_ref => $endpoint_id, | |||||||||||||||||||||||||||
| 68 | capabilities => \%caps, | ||||||||||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||||||||||
| 70 | 0 | 1; | |||||||||||||||||||||||||||
| 71 | 0 | ▶ | } or $log->debugf( "Manifest: model %s not published: %s", $entry->{id}, $@ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | return $builder->manifest; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # The listed surface, one entry per model id with the capabilities of the | ||||||||||||||||||||||||||||
| 80 | # engine that serves it. Cached until the listing changes or a capability | ||||||||||||||||||||||||||||
| 81 | # probe finishes. | ||||||||||||||||||||||||||||
| 82 | 0 | ▶ | sub _model_entries { | ||||||||||||||||||||||||||
|
Subroutine
_model_entries: not called CC=14 SCAR=53.5
| |||||||||||||||||||||||||||||
| 83 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 84 | 0 | my $knarr = $self->knarr; | |||||||||||||||||||||||||||
| 85 | 0 | my $router = $knarr->router; | |||||||||||||||||||||||||||
| 86 | 0 | ▶ | my $listed = $router ? $router->list_models : $knarr->handler->list_models; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | my @rows = map { ref $_ eq 'HASH' ? $_ : { id => "$_" } } @{ $listed || [] }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$listed || []
MC/DC0%0/1
$listed || []
$listed
Truth table0%0/2
A
A= $listed
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | my $key = join "\0", map { join "\1", map { $_ // '' } @{$_}{qw( id engine model )} } @rows; | ||||||||||||||||||||||||||
|
Condition0%0/2
$_ // ""
MC/DC0%0/1
$_ // ""
$_
Truth table0%0/2
A
A= $_
| |||||||||||||||||||||||||||||
| 90 | # A capability probe (k37) can change what an engine supports. | ||||||||||||||||||||||||||||
| 91 | 0 | ▶ | $key .= "\2" . $router->capabilities_generation | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$router and $router->can("capabilities_generation")
MC/DC0%0/2
$router and $router->can("capabilities_generation")
$router
$router->can("capabilities_generation")
Truth table0%0/3
A and B
A= $routerB= $router->can("capabilities_generation")
| |||||||||||||||||||||||||||||
| 92 | if $router && $router->can('capabilities_generation'); | ||||||||||||||||||||||||||||
| 93 | 0 | my $cache = $self->_cache; | |||||||||||||||||||||||||||
| 94 | 0 | ▶ | return $cache->{entries} if $cache && $cache->{key} eq $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$cache and $cache->{"key"} eq $key
MC/DC0%0/2
$cache and $cache->{"key"} eq $key
$cache
$cache->{"key"} eq $key
Truth table0%0/3
A and B
A= $cacheB= $cache->{"key"} eq $key
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | my @entries; | |||||||||||||||||||||||||||
| 97 | 0 | for my $row (@rows) { | |||||||||||||||||||||||||||
| 98 | 0 | my $id = $row->{id}; | |||||||||||||||||||||||||||
| 99 | 0 | ▶ | next unless defined $id && length $id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $id and length $id
MC/DC0%0/2
defined $id and length $id
defined $id
length $id
Truth table0%0/3
A and B
A= defined $idB= length $id
| |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | my ( $caps, $content_format ) = $router | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | ? _router_capabilities( $router, $id ) : ( { chat => 1 } ); | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | next unless $caps; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | push @entries, { id => $id, capabilities => $caps, content_format => $content_format }; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 0 | $self->_cache( { key => $key, entries => \@entries } ); | |||||||||||||||||||||||||||
| 106 | 0 | return \@entries; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # Capabilities of the engine serving $id, for the upstream model it sends, | ||||||||||||||||||||||||||||
| 110 | # through core's Builder: model-scoped (ADR 0019) and filtered to the public | ||||||||||||||||||||||||||||
| 111 | # allowlist. A model the router cannot build an engine for (e.g. its key | ||||||||||||||||||||||||||||
| 112 | # variable is unset) is not servable and not published. | ||||||||||||||||||||||||||||
| 113 | 0 | ▶ | sub _router_capabilities { | ||||||||||||||||||||||||||
|
Subroutine
_router_capabilities: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 114 | 0 | my ( $router, $id ) = @_; | |||||||||||||||||||||||||||
| 115 | 0 | my ( $engine, $model, $alias_only ) = eval { $router->resolve( $id, skip_default => 1 ) }; | |||||||||||||||||||||||||||
| 116 | 0 | ▶ | unless ($engine) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | $log->debugf( "Manifest: model %s not published: %s", $id, $@ || 'unresolved' ); | ||||||||||||||||||||||||||
|
Condition0%0/2
$@ || 'unresolved'
MC/DC0%0/1
$@ || 'unresolved'
$@
Truth table0%0/2
A
A= $@
| |||||||||||||||||||||||||||||
| 118 | 0 | return; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | # An alias without model: key sends the engine's own default (k22). | ||||||||||||||||||||||||||||
| 121 | 0 | ▶ | my $upstream = $alias_only ? eval { $engine->chat_model } : $model; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | $upstream = $id unless defined $upstream && length $upstream; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $upstream and length $upstream
MC/DC0%0/2
defined $upstream and length $upstream
defined $upstream
length $upstream
Truth table0%0/3
A and B
A= defined $upstreamB= length $upstream
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | my $caps = eval { | |||||||||||||||||||||||||||
| 125 | 0 | my $probe = Langertha::Manifest::Builder->new( | |||||||||||||||||||||||||||
| 126 | provider_id => 'probe', | ||||||||||||||||||||||||||||
| 127 | issuer => 'http://probe.invalid', | ||||||||||||||||||||||||||||
| 128 | ); | ||||||||||||||||||||||||||||
| 129 | # base_url, dialect and auth are given so nothing of the engine but its | ||||||||||||||||||||||||||||
| 130 | # capabilities is read; the probe manifest is thrown away. | ||||||||||||||||||||||||||||
| 131 | 0 | $probe->add_engine( $engine, | |||||||||||||||||||||||||||
| 132 | endpoint_id => 'probe', | ||||||||||||||||||||||||||||
| 133 | base_url => 'http://probe.invalid', | ||||||||||||||||||||||||||||
| 134 | dialect => 'probe', | ||||||||||||||||||||||||||||
| 135 | auth => 'none', | ||||||||||||||||||||||||||||
| 136 | models => [$upstream], | ||||||||||||||||||||||||||||
| 137 | ); | ||||||||||||||||||||||||||||
| 138 | 0 | my ($entry) = @{ $probe->manifest->models }; | |||||||||||||||||||||||||||
| 139 | 0 | +{ map { $_ => 1 } grep { $entry->supports($_) } keys %{ $entry->capabilities } }; | |||||||||||||||||||||||||||
| 140 | }; | ||||||||||||||||||||||||||||
| 141 | 0 | ▶ | unless ($caps) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | # Not a Langertha chat engine: it answers chat, nothing more is known. | ||||||||||||||||||||||||||||
| 143 | 0 | $log->debugf( "Manifest: no capabilities for %s: %s", $id, $@ ); | |||||||||||||||||||||||||||
| 144 | 0 | $caps = { chat => 1 }; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | # The message-content shape the engine puts on its wire (openai, | ||||||||||||||||||||||||||||
| 147 | # anthropic, gemini, ollama, ...); decides which protocol's image parts | ||||||||||||||||||||||||||||
| 148 | # reach it intact. | ||||||||||||||||||||||||||||
| 149 | 0 | ▶ | my $content_format = $engine->can('content_format') ? eval { $engine->content_format } : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 0 | return ( $caps, $content_format ); | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | ▶ | sub _origin_of { | ||||||||||||||||||||||||||
|
Subroutine
_origin_of: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 154 | 0 | my ($url) = @_; | |||||||||||||||||||||||||||
| 155 | 0 | my $uri = URI->new($url); | |||||||||||||||||||||||||||
| 156 | 0 | ▶ | return $url unless $uri->can('authority') && defined $uri->authority; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$uri->can("authority") and defined $uri->authority
MC/DC0%0/2
$uri->can("authority") and defined $uri->authority
$uri->can("authority")
defined $uri->authority
Truth table0%0/3
A and B
A= $uri->can("authority")B= defined $uri->authority
| |||||||||||||||||||||||||||||
| 157 | 0 | return $uri->scheme . '://' . $uri->authority; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 162 | 1 | 1; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | __END__ | ||||||||||||||||||||||||||||