<?php
require 'examples/boot.php';
use Cognesy\Agents\AgentLoop;
use Cognesy\Agents\Data\AgentState;
use Cognesy\Agents\Events\Support\AgentEventConsoleObserver;
use Cognesy\Messages\Messages;
// AgentEventConsoleObserver shows execution lifecycle events on the console
$logger = new AgentEventConsoleObserver(
useColors: true,
showTimestamps: true,
showContinuation: true,
);
// Create a default agent loop (no tools, just LLM conversation)
$loop = AgentLoop::default()
->wiretap($logger->wiretap());
// Prepare initial state with a user message
$state = AgentState::empty()->withMessages(
Messages::fromString('What are the three primary colors? Answer in one sentence.')
);
// Execute the loop to completion
echo "=== Agent Execution ===\n\n";
$finalState = $loop->execute($state);
// Read the result
echo "\n=== Result ===\n";
$response = $finalState->finalResponse()->toString() ?: 'No response';
echo "Answer: {$response}\n";
echo "Agent ID: {$finalState->agentId()->toString()}\n";
echo "Execution ID: {$finalState->execution()?->executionId()->toString()}\n";
echo "Last Step ID: {$finalState->lastStep()?->stepId()->toString()}\n";
echo "Steps: {$finalState->stepCount()}\n";
echo "Tokens used: {$finalState->usage()->total()}\n";
echo "Status: {$finalState->status()->value}\n";
if ($finalState->status()->value !== 'completed') {
echo "Skipping assertions because execution status is {$finalState->status()->value}.\n";
return;
}
// Assertions
assert($finalState->status() === \Cognesy\Agents\Enums\ExecutionStatus::Completed);
assert(!empty($finalState->finalResponse()->toString()), 'Expected non-empty response');
assert($finalState->stepCount() >= 1, 'Expected at least 1 step');
assert($finalState->usage()->total() > 0, 'Expected token usage > 0');
?>