<?php
require 'examples/boot.php';
use Cognesy\AgentCtrl\AgentCtrl;
use Cognesy\AgentCtrl\Broadcasting\AgentCtrlConsoleLogger;
use Cognesy\AgentCtrl\Enum\AgentType;
use Cognesy\AgentCtrl\Event\AgentExecutionCompleted;
use Cognesy\AgentCtrl\Event\AgentToolUsed;
// 1. Built-in console logger: formatted, color-coded output
$logger = new AgentCtrlConsoleLogger(
useColors: true,
showTimestamps: true,
showToolArgs: true, // Show tool input args
showSandbox: false, // Hide sandbox setup events
showPipeline: false, // Hide request/response pipeline
showStreaming: false, // Hide stream events
);
$agent = AgentCtrl::make(AgentType::OpenCode)
->wiretap($logger->wiretap());
// 2. Targeted listeners: subscribe to specific event types
$agent->onEvent(AgentToolUsed::class, function (AgentToolUsed $event) {
echo "\n >>> Tool used: {$event->tool}\n\n";
});
$agent->onEvent(AgentExecutionCompleted::class, function (AgentExecutionCompleted $event) {
echo "\n=== Execution Complete ===\n";
echo " Tools: {$event->toolCallCount}\n";
if ($event->cost !== null) {
echo " Cost: $" . number_format($event->cost, 4) . "\n";
}
$tokens = ($event->inputTokens ?? 0) + ($event->outputTokens ?? 0);
if ($tokens > 0) {
echo " Tokens: {$tokens}\n";
}
});
// Run the agent
echo "=== Agent Execution Log ===\n\n";
$response = $agent->executeStreaming('List files in current directory and explain what you see.');
echo "\n=== Result ===\n";
if ($response->isSuccess()) {
echo "Answer: " . $response->text() . "\n";
} else {
echo "Error: Command failed with exit code {$response->exitCode}\n";
exit(1);
}
?>