|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +require __DIR__ . '/../../vendor/autoload.php'; |
| 6 | + |
| 7 | +use Amp\Cancellation; |
| 8 | +use Arcp\Auth\AuthRouter; |
| 9 | +use Arcp\Auth\NoneAuth; |
| 10 | +use Arcp\Client\ARCPClient; |
| 11 | +use Arcp\Envelope\Envelope; |
| 12 | +use Arcp\Messages\Execution\JobAccepted; |
| 13 | +use Arcp\Messages\Session\Auth; |
| 14 | +use Arcp\Messages\Session\Capabilities; |
| 15 | +use Arcp\Messages\Session\PeerInfo; |
| 16 | +use Arcp\Runtime\ARCPRuntime; |
| 17 | +use Arcp\Runtime\Credentials\InMemoryCredentialProvisioner; |
| 18 | +use Arcp\Runtime\JobContext; |
| 19 | +use Arcp\Runtime\ToolHandler; |
| 20 | +use Arcp\Transport\MemoryTransport; |
| 21 | +use Arcp\Transport\Transport; |
| 22 | + |
| 23 | +$provisioner = new InMemoryCredentialProvisioner(); |
| 24 | +$runtime = new ARCPRuntime( |
| 25 | + authRouter: new AuthRouter([new NoneAuth()]), |
| 26 | + credentialProvisioner: $provisioner, |
| 27 | +); |
| 28 | +$runtime->registerTool('planner', new class () implements ToolHandler { |
| 29 | + #[\Override] |
| 30 | + public function invoke(array $arguments, JobContext $ctx, ?Cancellation $cancellation = null): mixed |
| 31 | + { |
| 32 | + $ctx->assertModelAllowed('anthropic/claude-3-5-sonnet'); |
| 33 | + return ['planned' => true]; |
| 34 | + } |
| 35 | +}); |
| 36 | + |
| 37 | +[$serverT, $clientT] = MemoryTransport::pair(); |
| 38 | +$recording = new class ($clientT) implements Transport { |
| 39 | + /** @var list<Envelope> */ |
| 40 | + public array $received = []; |
| 41 | + |
| 42 | + public function __construct(private readonly Transport $inner) |
| 43 | + { |
| 44 | + } |
| 45 | + |
| 46 | + #[\Override] |
| 47 | + public function send(Envelope $env, ?Cancellation $cancellation = null): void |
| 48 | + { |
| 49 | + $this->inner->send($env, $cancellation); |
| 50 | + } |
| 51 | + |
| 52 | + #[\Override] |
| 53 | + public function receive(?Cancellation $cancellation = null): ?Envelope |
| 54 | + { |
| 55 | + $env = $this->inner->receive($cancellation); |
| 56 | + if ($env instanceof Envelope) { |
| 57 | + $this->received[] = $env; |
| 58 | + } |
| 59 | + return $env; |
| 60 | + } |
| 61 | + |
| 62 | + #[\Override] |
| 63 | + public function close(): void |
| 64 | + { |
| 65 | + $this->inner->close(); |
| 66 | + } |
| 67 | + |
| 68 | + #[\Override] |
| 69 | + public function isClosed(): bool |
| 70 | + { |
| 71 | + return $this->inner->isClosed(); |
| 72 | + } |
| 73 | +}; |
| 74 | +$serverFuture = $runtime->serveAsync($serverT); |
| 75 | +$client = new ARCPClient($recording); |
| 76 | +$client->open(Auth::none(), new PeerInfo('provisioned-demo', '0.1'), new Capabilities( |
| 77 | + anonymous: true, |
| 78 | + features: ['provisioned_credentials', 'model.use'], |
| 79 | +)); |
| 80 | +$result = $client->invokeTool('planner', [ |
| 81 | + 'lease' => [ |
| 82 | + 'model.use' => ['anthropic/*'], |
| 83 | + 'cost.budget' => ['USD:1.00'], |
| 84 | + ], |
| 85 | +]); |
| 86 | + |
| 87 | +foreach ($recording->received as $env) { |
| 88 | + if ($env->payload instanceof JobAccepted) { |
| 89 | + print_r($env->payload->credentials); |
| 90 | + break; |
| 91 | + } |
| 92 | +} |
| 93 | +print_r($result->value); |
| 94 | +print_r(['revoked' => $provisioner->revoked]); |
| 95 | + |
| 96 | +$client->close(); |
| 97 | +$serverFuture->await(); |
0 commit comments