| 1 | package Claude::Agent::Client; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use 5.020; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Object::Proto (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Future; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Claude::Agent::Logger '$log'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Claude::Agent::Options; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Claude::Agent::Query; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Claude::Agent::Message; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Claude::Agent::Error; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 3 | Object::Proto::define('Claude::Agent::Client', | |||||||||||||||||||||||||||
| 17 | 'options:lazy:builder(_build_options)', | ||||||||||||||||||||||||||||
| 18 | 'loop:predicate', | ||||||||||||||||||||||||||||
| 19 | 'query_args:HashRef:default({})', | ||||||||||||||||||||||||||||
| 20 | '_query', | ||||||||||||||||||||||||||||
| 21 | '_session_id', | ||||||||||||||||||||||||||||
| 22 | '_connected:Bool:default(0)', | ||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | sub _build_options { Claude::Agent::Options->new } | ||||||||||||||||||||||||||
|
Subroutine
_build_options: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 2 | ▶ | sub connect { | ||||||||||||||||||||||||||
|
Subroutine
connect: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | 2 | my ($self, $prompt) = @_; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 2 | ▶ | Claude::Agent::Error->throw(message => 'Already connected') if $self->_connected; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 2 | $log->debug("Client: connecting"); | |||||||||||||||||||||||||||
| 34 | 2 | $self->_start($self->options, $prompt); | |||||||||||||||||||||||||||
| 35 | 2 | return $self; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 3 | ▶ | sub resume { | ||||||||||||||||||||||||||
|
Subroutine
resume: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | 3 | my ($self, $session_id, $prompt, %args) = @_; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 3 | ▶ | Claude::Agent::Error->throw(message => 'Already connected') if $self->_connected; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 2 | ▶ | Claude::Agent::Error->throw(message => 'resume needs a session id') unless defined $session_id && length $session_id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $session_id and length $session_id
MC/DC0%0/2
defined $session_id and length $session_id
defined $session_id
length $session_id
Truth table33%1/3
A and B
A= defined $session_idB= length $session_id
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 2 | $log->debug(sprintf("Client: resuming session %s", $session_id)); | |||||||||||||||||||||||||||
| 45 | 2 | ▶ | $self->_start($self->options->with(resume => $session_id, ($args{fork} ? (fork_session => 1) : ())), $prompt); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 2 | $self->_session_id($session_id); | |||||||||||||||||||||||||||
| 47 | 2 | return $self; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 1 | ▶ | sub fork { | ||||||||||||||||||||||||||
|
Subroutine
fork: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 1 | my ($self, $session_id, $prompt) = @_; | |||||||||||||||||||||||||||
| 52 | 1 | return $self->resume($session_id, $prompt, fork => 1); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 4 | ▶ | sub _start { | ||||||||||||||||||||||||||
|
Subroutine
_start: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 56 | 4 | my ($self, $options, $prompt) = @_; | |||||||||||||||||||||||||||
| 57 | $self->_query(Claude::Agent::Query->new( | ||||||||||||||||||||||||||||
| 58 | (defined $prompt ? (prompt => $prompt) : ()), | ||||||||||||||||||||||||||||
| 59 | options => $options, | ||||||||||||||||||||||||||||
| 60 | persistent => 1, | ||||||||||||||||||||||||||||
| 61 | ($self->has_loop ? (loop => $self->loop) : ()), | ||||||||||||||||||||||||||||
| 62 | 4 | ▶ | %{$self->query_args}, | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 63 | )); | ||||||||||||||||||||||||||||
| 64 | 4 | $self->_connected(1); | |||||||||||||||||||||||||||
| 65 | 4 | return; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 9 | ▶ | sub is_connected { | ||||||||||||||||||||||||||
|
Subroutine
is_connected: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 9 | my ($self) = @_; | |||||||||||||||||||||||||||
| 70 | 9 | return $self->_connected; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 5 | ▶ | sub session_id { | ||||||||||||||||||||||||||
|
Subroutine
session_id: called CC=3 SCAR=11.7
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 75 | 5 | ▶ | return $self->_session_id // ($self->_query ? $self->_query->session_id : undef); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->_session_id // ($self->_query ? $self->_query->session_id : undef)
MC/DC50%1/2
$self->_session_id // ($self->_query ? $self->_query->session_id : undef)
$self->_session_id
($self->_query ? $self->_query->session_id : undef)
Truth table66%2/3
A // B
A= $self->_session_idB= ($self->_query ? $self->_query->session_id : undef)
| |||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 7 | ▶ | sub query { | ||||||||||||||||||||||||||
|
Subroutine
query: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 7 | my ($self) = @_; | |||||||||||||||||||||||||||
| 80 | 7 | return $self->_query; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 11 | ▶ | sub _live_query { | ||||||||||||||||||||||||||
|
Subroutine
_live_query: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 84 | 11 | my ($self) = @_; | |||||||||||||||||||||||||||
| 85 | 11 | ▶ | Claude::Agent::Error->throw(message => 'Not connected') unless $self->_connected; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 4 | ▶ | Claude::Agent::Error->throw(message => 'Query not initialized') unless $self->_query; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 4 | return $self->_query; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 5 | ▶ | sub send { | ||||||||||||||||||||||||||
|
Subroutine
send: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | 5 | my ($self, $content, %args) = @_; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 5 | my $query = $self->_live_query; | |||||||||||||||||||||||||||
| 94 | 2 | ▶ | my $session_id = $args{session_id} // 'default'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"session_id"} // "default"
MC/DC100%1/1
$args{"session_id"} // "default"
$args{"session_id"}
Truth table100%2/2
A
A= $args{"session_id"}
| |||||||||||||||||||||||||||||
| 95 | 2 | $log->debug(sprintf("Client: sending a message on session %s", $session_id)); | |||||||||||||||||||||||||||
| 96 | 2 | ▶ | Claude::Agent::Error->throw(message => 'Could not send the message: the session has ended') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | unless $query->send_user_message($content, session_id => $session_id); | ||||||||||||||||||||||||||||
| 98 | 2 | return $self; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 5 | ▶ | sub receive { | ||||||||||||||||||||||||||
|
Subroutine
receive: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 102 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 5 | my $msg = $self->_live_query->next; | |||||||||||||||||||||||||||
| 105 | 2 | ▶ | if ($msg && $msg->isa('Claude::Agent::Message::System')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$msg and $msg->isa("Claude::Agent::Message::System")
MC/DC50%1/2
$msg and $msg->isa("Claude::Agent::Message::System")
$msg
$msg->isa("Claude::Agent::Message::System")
Truth table66%2/3
A and B
A= $msgB= $msg->isa("Claude::Agent::Message::System")
| |||||||||||||||||||||||||||||
| 106 | 1 | my $sid = $msg->get_session_id; | |||||||||||||||||||||||||||
| 107 | 1 | ▶ | $self->_session_id($sid) if $sid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | 2 | return $msg; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 1 | ▶ | sub receive_async { | ||||||||||||||||||||||||||
|
Subroutine
receive_async: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 113 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 114 | 1 | return $self->_live_query->next_async; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 1 | ▶ | sub receive_until_result { | ||||||||||||||||||||||||||
|
Subroutine
receive_until_result: called CC=9 SCAR=26.2
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 1 | my $max_iterations = 1000; | |||||||||||||||||||||||||||
| 121 | 1 | my $max_env = $ENV{CLAUDE_AGENT_MAX_MESSAGES}; | |||||||||||||||||||||||||||
| 122 | 1 | ▶ | $max_env =~ s/^\s+|\s+$//g if defined $max_env; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 1 | ▶ | if (defined $max_env && $max_env =~ /^[1-9]\d*$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $max_env and $max_env =~ /^[1-9]\d*$/u
MC/DC0%0/2
defined $max_env and $max_env =~ /^[1-9]\d*$/u
defined $max_env
$max_env =~ /^[1-9]\d*$/u
Truth table33%1/3
A and B
A= defined $max_envB= $max_env =~ /^[1-9]\d*$/u
| |||||||||||||||||||||||||||||
| 124 | 0 | ▶ | $max_iterations = $max_env > 5000 ? 5000 : $max_env; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 1 | my @messages; | |||||||||||||||||||||||||||
| 128 | 1 | ▶ | while (my $msg = $self->receive) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 2 | push @messages, $msg; | |||||||||||||||||||||||||||
| 130 | 2 | ▶ | last if $msg->isa('Claude::Agent::Message::Result'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 1 | ▶ | if (@messages >= $max_iterations) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 0 | $log->warning(sprintf("receive_until_result: stopped after %d messages without a result", $max_iterations)); | |||||||||||||||||||||||||||
| 133 | 0 | last; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | 1 | ▶ | return wantarray ? @messages : \@messages; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 8 | ▶ | sub disconnect { | ||||||||||||||||||||||||||
|
Subroutine
disconnect: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 140 | 8 | my ($self) = @_; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 8 | ▶ | if ($self->_query) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 4 | $log->debug("Client: disconnecting"); | |||||||||||||||||||||||||||
| 144 | 4 | $self->_query->close; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | 8 | $self->_query(undef); | |||||||||||||||||||||||||||
| 147 | 8 | $self->_session_id(undef); | |||||||||||||||||||||||||||
| 148 | 8 | $self->_connected(0); | |||||||||||||||||||||||||||
| 149 | 8 | return $self; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 3 | for my $method (qw( | |||||||||||||||||||||||||||
| 153 | interrupt set_permission_mode set_model set_max_thinking_tokens rewind_files | ||||||||||||||||||||||||||||
| 154 | mcp_status get_context_usage reconnect_mcp_server toggle_mcp_server stop_task | ||||||||||||||||||||||||||||
| 155 | )) { | ||||||||||||||||||||||||||||
| 156 | 3 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 157 | 30 | ▶ | *{$method} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=13.3
| |||||||||||||||||||||||||||||
| 158 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 159 | 2 | ▶ | return unless $self->_connected && $self->_query; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->_connected and $self->_query
MC/DC0%0/2
$self->_connected and $self->_query
$self->_connected
$self->_query
Truth table33%1/3
A and B
A= $self->_connectedB= $self->_query
| |||||||||||||||||||||||||||||
| 160 | 0 | return $self->_query->$method(@_); | |||||||||||||||||||||||||||
| 161 | 30 | }; | |||||||||||||||||||||||||||
| 162 | 30 | my $async = "${method}_async"; | |||||||||||||||||||||||||||
| 163 | 30 | ▶ | *{$async} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 164 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 165 | 2 | ▶ | return Future->fail('Not connected') unless $self->_connected && $self->_query; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->_connected and $self->_query
MC/DC50%1/2
$self->_connected and $self->_query
$self->_connected
$self->_query
Truth table66%2/3
A and B
A= $self->_connectedB= $self->_query
| |||||||||||||||||||||||||||||
| 166 | 1 | return $self->_query->$async(@_); | |||||||||||||||||||||||||||
| 167 | 30 | }; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 1 | ▶ | sub server_info { | ||||||||||||||||||||||||||
|
Subroutine
server_info: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 171 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 172 | 1 | ▶ | return $self->_query ? $self->_query->server_info : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 3 | 1; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | __END__ | ||||||||||||||||||||||||||||