<?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, // Show tool input args
showSandbox: true, // Show sandbox setup
);
$toolCalls = [];
echo "=== Agent Execution Log ===\n\n";
$response = AgentCtrl::claudeCode()
->wiretap($logger->wiretap())
->withMaxTurns(10)
->onText(function (string $text) {
echo $text;
})
->onToolUse(function (string $tool, array $input, ?string $output) use (&$toolCalls) {
$target = $input['pattern'] ?? $input['file_path'] ?? $input['command'] ?? '';
if (strlen($target) > 50) {
$target = '...' . substr($target, -47);
}
$toolCalls[] = $tool;
echo "\n >> [{$tool}] {$target}\n";
})
->executeStreaming(<<<'PROMPT'
Complete this task in steps:
1. Use Glob or find command to locate PHP files with "validation" in the filename under ./examples
2. Read the contents of relevant PHP file
3. Analyze the code and provide a concise explanation (under 200 words) covering:
- What validation is being performed
- What validation constraints/attributes are used
- How validation is triggered
- What happens when validation fails
Provide your final explanation as a clear, structured response.
PROMPT);
echo "\n=== Result ===\n";
echo "Tools used: " . implode(' > ', $toolCalls) . "\n";
echo "Total tool calls: " . count($toolCalls) . "\n";
echo "Exit code: {$response->exitCode}\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: Agent search failed with exit code {$response->exitCode}\n";
exit(1);
}
?>