<?php
require 'examples/boot.php';
use Cognesy\AgentCtrl\AgentCtrl;
use Cognesy\AgentCtrl\Broadcasting\AgentCtrlConsoleLogger;
// Console logger for execution lifecycle visibility
$logger = new AgentCtrlConsoleLogger(
useColors: true,
showTimestamps: true,
showToolArgs: true,
);
$toolCalls = [];
echo "=== Agent Execution Log ===\n\n";
$response = AgentCtrl::openCode()
->wiretap($logger->wiretap())
->onText(function (string $text) {
echo $text;
})
->onToolUse(function (string $tool, array $input, ?string $output) use (&$toolCalls) {
$target = $input['command'] ?? $input['file_path'] ?? $input['pattern'] ?? '';
if (strlen($target) > 40) {
$target = '...' . substr($target, -37);
}
$toolCalls[] = $tool;
echo "\n >> [{$tool}] {$target}\n";
})
->executeStreaming('Read the first 5 lines of composer.json in this directory and describe what you see.');
echo "\n=== Result ===\n";
echo "Tools used: " . implode(' > ', $toolCalls) . "\n";
echo "Total tool calls: " . count($toolCalls) . "\n";
if ($response->usage) {
echo "Tokens: {$response->usage->input} in / {$response->usage->output} out\n";
}
if ($response->cost) {
echo "Cost: $" . number_format($response->cost, 4) . "\n";
}
if (!$response->isSuccess()) {
echo "Error: Command failed with exit code {$response->exitCode}\n";
exit(1);
}
?>