22
33namespace Cognesy \Agents \Agent ;
44
5- use Cognesy \Agents \Agent \StateProcessing \CanApplyProcessors ;
6- use Cognesy \Agents \Agent \StateProcessing \CanProcessAgentState ;
7- use Cognesy \Agents \Agent \StateProcessing \StateProcessors ;
85use Cognesy \Agents \Core \Collections \Tools ;
9- use Cognesy \Agents \Core \Tools \ToolExecutor ;
106use Cognesy \Agents \Core \Continuation \ContinuationCriteria ;
11- use Cognesy \Agents \Core \Continuation \Contracts \CanEvaluateContinuation ;
127use Cognesy \Agents \Core \Continuation \Enums \StopReason ;
138use Cognesy \Agents \Core \Contracts \CanControlAgentLoop ;
9+ use Cognesy \Agents \Core \Contracts \CanEmitAgentEvents ;
1410use Cognesy \Agents \Core \Contracts \CanExecuteToolCalls ;
1511use Cognesy \Agents \Core \Contracts \CanHandleAgentErrors ;
1612use Cognesy \Agents \Core \Contracts \CanUseTools ;
17- use Cognesy \Agents \Core \Contracts \ToolInterface ;
1813use Cognesy \Agents \Core \Data \AgentState ;
1914use Cognesy \Agents \Core \Data \AgentStep ;
2015use Cognesy \Agents \Core \Data \CurrentExecution ;
2116use Cognesy \Agents \Core \Data \StepExecution ;
2217use Cognesy \Agents \Core \Enums \AgentStatus ;
23- use Cognesy \Agents \Core \ErrorHandling \AgentErrorHandler ;
24- use Cognesy \Agents \Core \ErrorHandling \ErrorPolicy ;
25- use Cognesy \Agents \Core \Events \AgentEventEmitter ;
2618use Cognesy \Agents \Core \Exceptions \AgentException ;
2719use Cognesy \Agents \Core \Lifecycle \CanObserveAgentLifecycle ;
28- use Cognesy \Events \ Contracts \ CanHandleEvents ;
20+ use Cognesy \Agents \ Core \ Tools \ ToolExecutor ;
2921use DateTimeImmutable ;
3022use Throwable ;
3123
3729 */
3830class Agent implements CanControlAgentLoop
3931{
40- private readonly AgentEventEmitter $ eventEmitter ;
41-
4232 public function __construct (
4333 private readonly Tools $ tools ,
4434 private readonly CanExecuteToolCalls $ toolExecutor ,
4535 private readonly CanHandleAgentErrors $ errorHandler ,
46- private readonly ?CanApplyProcessors $ processors ,
4736 private readonly ContinuationCriteria $ continuationCriteria ,
4837 private readonly CanUseTools $ driver ,
49- AgentEventEmitter $ eventEmitter ,
50- ?CanHandleEvents $ events = null ,
38+ private readonly CanEmitAgentEvents $ eventEmitter ,
5139 private readonly ?CanObserveAgentLifecycle $ observer = null ,
52- ) {
53- $ this ->eventEmitter = $ events !== null
54- ? $ eventEmitter ->withEventHandler ($ events )
55- : $ eventEmitter ;
56- }
40+ ) {}
5741
5842 // PUBLIC API //////////////////////////////////
5943
@@ -102,7 +86,7 @@ protected function onBeforeExecution(AgentState $state): AgentState {
10286 $ this ->eventEmitter ->executionStarted ($ state , count ($ this ->tools ->names ()));
10387
10488 if ($ this ->observer !== null ) {
105- $ state = $ this ->observer ->executionStarting ($ state );
89+ $ state = $ this ->observer ->beforeExecution ($ state );
10690 }
10791
10892 return $ state ;
@@ -112,7 +96,7 @@ protected function onBeforeStep(AgentState $state): AgentState {
11296 $ this ->eventEmitter ->stepStarted ($ state );
11397
11498 if ($ this ->observer !== null ) {
115- $ state = $ this ->observer ->stepStarting ($ state );
99+ $ state = $ this ->observer ->beforeStep ($ state );
116100 }
117101
118102 return $ state ;
@@ -145,7 +129,7 @@ private function onAfterToolUse(AgentState $state, AgentStep $rawStep) : AgentSt
145129
146130 protected function onAfterStep (AgentState $ state ): AgentState {
147131 if ($ this ->observer !== null ) {
148- $ state = $ this ->observer ->stepEnding ($ state );
132+ $ state = $ this ->observer ->afterStep ($ state );
149133 }
150134
151135 $ this ->eventEmitter ->stepCompleted ($ state );
@@ -161,7 +145,7 @@ protected function onAfterExecution(AgentState $state): AgentState {
161145 $ finalState = $ state ->withStatus ($ status );
162146
163147 if ($ this ->observer !== null ) {
164- $ finalState = $ this ->observer ->executionEnding ($ finalState );
148+ $ finalState = $ this ->observer ->afterExecution ($ finalState );
165149 }
166150
167151 $ this ->eventEmitter ->executionFinished ($ finalState );
@@ -199,7 +183,7 @@ protected function onError(Throwable $error, AgentState $state): AgentState {
199183 $ agentException = $ handling ->exception instanceof AgentException
200184 ? $ handling ->exception
201185 : AgentException::fromThrowable ($ handling ->exception );
202- $ nextState = $ this ->observer ->executionFailed ($ nextState , $ agentException );
186+ $ nextState = $ this ->observer ->onError ($ nextState , $ agentException );
203187 }
204188 }
205189
@@ -225,7 +209,7 @@ protected function shouldContinue(AgentState $state): bool {
225209 // We're about to stop - let the observer intervene if registered
226210 if ($ this ->observer !== null ) {
227211 $ stopReason = $ state ->stopReason () ?? StopReason::Completed;
228- $ decision = $ this ->observer ->stopping ($ state , $ stopReason );
212+ $ decision = $ this ->observer ->beforeStopDecision ($ state , $ stopReason );
229213 if ($ decision ->isPrevented ()) {
230214 return true ; // Observer prevented stopping, continue execution
231215 }
@@ -235,10 +219,7 @@ protected function shouldContinue(AgentState $state): bool {
235219 }
236220
237221 private function performStep (AgentState $ state ): AgentState {
238- return match (true ) {
239- ($ this ->processors === null ) => $ this ->useTools ($ state ),
240- default => $ this ->processors ->apply ($ state , fn ($ s ) => $ this ->useTools ($ s )),
241- };
222+ return $ this ->useTools ($ state );
242223 }
243224
244225 private function useTools (AgentState $ state ): AgentState {
@@ -306,32 +287,27 @@ public function driver(): CanUseTools {
306287 return $ this ->driver ;
307288 }
308289
309- public function eventEmitter (): AgentEventEmitter {
290+ public function eventEmitter (): CanEmitAgentEvents {
310291 return $ this ->eventEmitter ;
311292 }
312293
294+ public function observer (): ?CanObserveAgentLifecycle {
295+ return $ this ->observer ;
296+ }
297+
313298 // MUTATORS /////////////////////////////////////////////
314299
315300 public function with (
316301 ?Tools $ tools = null ,
317302 ?CanExecuteToolCalls $ toolExecutor = null ,
318303 ?CanHandleAgentErrors $ errorHandler = null ,
319- ?CanApplyProcessors $ processors = null ,
320304 ?ContinuationCriteria $ continuationCriteria = null ,
321305 ?CanUseTools $ driver = null ,
322- ?AgentEventEmitter $ eventEmitter = null ,
323- ?CanHandleEvents $ events = null ,
306+ ?CanEmitAgentEvents $ eventEmitter = null ,
324307 ?CanObserveAgentLifecycle $ observer = null ,
325308 ): self {
326309 $ resolvedTools = $ tools ?? $ this ->tools ;
327310
328- // Resolve emitter: prefer explicit, then create new if events changed
329- $ resolvedEmitter = $ eventEmitter ?? (
330- $ events !== null
331- ? $ this ->eventEmitter ->withEventHandler ($ events )
332- : $ this ->eventEmitter
333- );
334-
335311 // If tools changed but no executor provided, create new executor
336312 $ resolvedExecutor = $ toolExecutor ?? (
337313 $ tools !== null
@@ -343,52 +319,10 @@ public function with(
343319 tools: $ resolvedTools ,
344320 toolExecutor: $ resolvedExecutor ,
345321 errorHandler: $ errorHandler ?? $ this ->errorHandler ,
346- processors: $ processors ?? $ this ->processors ,
347322 continuationCriteria: $ continuationCriteria ?? $ this ->continuationCriteria ,
348323 driver: $ driver ?? $ this ->driver ,
349- eventEmitter: $ resolvedEmitter ,
324+ eventEmitter: $ eventEmitter ?? $ this -> eventEmitter ,
350325 observer: $ observer ?? $ this ->observer ,
351326 );
352327 }
353-
354- public function withProcessors (CanProcessAgentState ...$ processors ): self {
355- return $ this ->with (processors: new StateProcessors (...$ processors ));
356- }
357-
358- public function withDriver (CanUseTools $ driver ): self {
359- return $ this ->with (driver: $ driver );
360- }
361-
362- public function withContinuationCriteria (CanEvaluateContinuation ...$ criteria ): self {
363- return $ this ->with (continuationCriteria: new ContinuationCriteria (...$ criteria ));
364- }
365-
366- public function withTools (array |ToolInterface |Tools $ tools ): self {
367- return $ this ->with (tools: match (true ) {
368- is_array ($ tools ) => new Tools (...$ tools ),
369- $ tools instanceof ToolInterface => new Tools ($ tools ),
370- $ tools instanceof Tools => $ tools ,
371- default => new Tools (),
372- });
373- }
374-
375- public function withToolExecutor (CanExecuteToolCalls $ toolExecutor ): self {
376- return $ this ->with (toolExecutor: $ toolExecutor );
377- }
378-
379- public function withErrorHandler (CanHandleAgentErrors $ errorHandler ): self {
380- return $ this ->with (errorHandler: $ errorHandler );
381- }
382-
383- public function withErrorPolicy (ErrorPolicy $ policy ): self {
384- return $ this ->with (errorHandler: AgentErrorHandler::withPolicy ($ policy ));
385- }
386-
387- public function withObserver (?CanObserveAgentLifecycle $ observer ): self {
388- return $ this ->with (observer: $ observer );
389- }
390-
391- public function observer (): ?CanObserveAgentLifecycle {
392- return $ this ->observer ;
393- }
394328}
0 commit comments