<?phprequire'examples/boot.php';useCognesy\Addons\StepByStep\Continuation\ContinuationCriteria;useCognesy\Addons\StepByStep\Continuation\Criteria\ExecutionTimeLimit;useCognesy\Addons\StepByStep\Continuation\Criteria\RetryLimit;useCognesy\Addons\StepByStep\Continuation\Criteria\StepsLimit;useCognesy\Addons\StepByStep\Continuation\Criteria\TokenUsageLimit;useCognesy\Addons\ToolUse\Collections\Tools;useCognesy\Addons\ToolUse\Data\ToolUseState;useCognesy\Addons\ToolUse\Data\ToolUseStep;useCognesy\Addons\ToolUse\Drivers\ReAct\ContinuationCriteria\StopOnFinalDecision;useCognesy\Addons\ToolUse\Drivers\ReAct\ReActDriver;useCognesy\Addons\ToolUse\Tools\FunctionTool;useCognesy\Addons\ToolUse\ToolUseFactory;useCognesy\Messages\Messages;useCognesy\Polyglot\Inference\LLMProvider;functionadd_numbers(int$a,int$b):int{return$a+$b;}functionsubtract_numbers(int$a,int$b):int{return$a-$b;}$driver=newReActDriver(llm:LLMProvider::using('openai'),finalViaInference:true,);$toolUse=ToolUseFactory::default(tools:newTools(FunctionTool::fromCallable(add_numbers(...)),FunctionTool::fromCallable(subtract_numbers(...))),continuationCriteria:newContinuationCriteria(newStepsLimit(6,fn(ToolUseState$state)=>$state->stepCount()),newTokenUsageLimit(8192,fn(ToolUseState$state)=>$state->usage()->total()),newExecutionTimeLimit(60,fn(ToolUseState$state)=>$state->startedAt()),newRetryLimit(2,fn(ToolUseState$state)=>$state->steps(),fn(ToolUseStep$step)=>$step->hasErrors()),newStopOnFinalDecision(),),driver:$driver);//// PATTERN #1 - manual control//echo"\nReAct PATTERN #1 - manual control\n";$state=(newToolUseState)->withMessages(Messages::fromString('Add 2455 and 3558 then subtract 4344 from the result.'));while($toolUse->hasNextStep($state)){$state=$toolUse->nextStep($state);$step=$state->currentStep();print("STEP - tokens used: ".($step->usage()?->total()??0).' ['.$step->toString().']'."\n");}$result=$state->currentStep()->outputMessages()->toString();print("RESULT: ".$result."\n");//// PATTERN #2 - using iterator//echo"\nReAct PATTERN #2 - using iterator\n";$state=(newToolUseState)->withMessages(Messages::fromString('Add 2455 and 3558 then subtract 4344 from the result.'));foreach($toolUse->iterator($state)as$currentState){$step=$currentState->currentStep();print("STEP - tokens used: ".($step->usage()?->total()??0).' ['.$step->toString().']'."\n");$state=$currentState;// keep the latest state}$result=$state->currentStep()->outputMessages()->toString();print("RESULT: ".$result."\n");//// PATTERN #3 - just get final step (fast forward to it)//echo"\nReAct PATTERN #3 - final via Inference (optional)\n";$state=(newToolUseState)->withMessages(Messages::fromString('Add 2455 and 3558 then subtract 4344 from the result.'));$finalState=$toolUse->finalStep($state);$result=$finalState->currentStep()->outputMessages()->toString();print("RESULT: ".$result."\n");?>