1010use Cortex \LLM \Data \Usage ;
1111use Cortex \Prompts \Prompt ;
1212use Cortex \Events \AgentEnd ;
13+ use Illuminate \Support \Str ;
1314use Cortex \Contracts \ToolKit ;
1415use Cortex \Events \AgentStart ;
1516use Cortex \JsonSchema \Schema ;
3233use Illuminate \Support \Collection ;
3334use Cortex \Events \AgentStreamChunk ;
3435use Cortex \LLM \Data \ChatStreamResult ;
35- use Cortex \Agents \Contracts \Middleware ;
3636use Cortex \Events \Contracts \AgentEvent ;
3737use Cortex \Exceptions \GenericException ;
3838use Cortex \Memory \Stores \InMemoryStore ;
4848use Cortex \Support \Traits \DispatchesEvents ;
4949use Illuminate \Contracts \Support \Arrayable ;
5050use Cortex \LLM \Contracts \LLM as LLMContract ;
51+ use Cortex \Agents \Concerns \HandlesMiddleware ;
5152use Cortex \Agents \Stages \TrackAgentStepStart ;
5253use Cortex \Prompts \Builders \ChatPromptBuilder ;
53- use Cortex \Agents \Middleware \AfterModelWrapper ;
5454use Cortex \LLM \Data \Messages \MessageCollection ;
55- use Cortex \Agents \Middleware \BeforeModelWrapper ;
5655use Cortex \LLM \Data \Messages \MessagePlaceholder ;
5756use Cortex \Prompts \Templates \ChatPromptTemplate ;
5857use Cortex \Agents \Contracts \AfterModelMiddleware ;
59- use Cortex \Agents \Middleware \BeforePromptWrapper ;
6058use Cortex \Agents \Contracts \BeforeModelMiddleware ;
6159use Cortex \Agents \Contracts \BeforePromptMiddleware ;
6260use Cortex \Contracts \ChatMemory as ChatMemoryContract ;
@@ -67,6 +65,7 @@ class Agent implements Pipeable
6765{
6866 use CanPipe;
6967 use DispatchesEvents;
68+ use HandlesMiddleware;
7069
7170 protected LLMContract $ llm ;
7271
@@ -383,57 +382,6 @@ protected function executionStages(): array
383382 ];
384383 }
385384
386- /**
387- * Get the middleware of a specific type.
388- * If middleware implements multiple middleware interfaces, wrap it appropriately
389- * to delegate to the correct hook method (beforePrompt, beforeModel, or afterModel).
390- *
391- * @param class-string<\Cortex\Agents\Contracts\Middleware> $type
392- *
393- * @return array<int, \Cortex\Agents\Contracts\Middleware>
394- */
395- protected function getMiddleware (string $ type ): array
396- {
397- return array_map (
398- function (Middleware $ middleware ) use ($ type ): Middleware {
399- // Wrap all hook-based middleware to ensure hook methods are called
400- if (! $ this ->isHookMiddlewareType ($ type )) {
401- return $ middleware ;
402- }
403-
404- // If middleware implements multiple interfaces, wrap to delegate to correct hook
405- // If it only implements one interface, still wrap to ensure hook method is called
406- return $ this ->wrapMiddleware ($ middleware , $ type );
407- },
408- array_filter ($ this ->middleware , fn (Middleware $ middleware ): bool => $ middleware instanceof $ type ),
409- );
410- }
411-
412- /**
413- * Check if the given type is a hook-based middleware interface.
414- */
415- protected function isHookMiddlewareType (string $ type ): bool
416- {
417- return in_array ($ type , [
418- BeforePromptMiddleware::class,
419- BeforeModelMiddleware::class,
420- AfterModelMiddleware::class,
421- ], true );
422- }
423-
424- /**
425- * Wrap middleware to delegate to the appropriate hook method.
426- */
427- protected function wrapMiddleware (Middleware $ middleware , string $ type ): Middleware
428- {
429- return match ($ type ) {
430- BeforePromptMiddleware::class => new BeforePromptWrapper ($ middleware ),
431- BeforeModelMiddleware::class => new BeforeModelWrapper ($ middleware ),
432- AfterModelMiddleware::class => new AfterModelWrapper ($ middleware ),
433- default => $ middleware ,
434- };
435- }
436-
437385 /**
438386 * @param array<int, \Cortex\LLM\Contracts\Message> $messages
439387 * @param array<string, mixed> $input
@@ -546,8 +494,10 @@ protected static function buildPromptTemplate(
546494 */
547495 protected static function buildMemory (ChatPromptTemplate $ prompt , ?Store $ memoryStore = null ): ChatMemoryContract
548496 {
549- $ memoryStore ??= new InMemoryStore ();
550- $ memoryStore ->setMessages ($ prompt ->messages ->withoutPlaceholders ());
497+ $ memoryStore ??= new InMemoryStore (
498+ threadId: Str::uuid7 ()->toString (),
499+ messages: $ prompt ->messages ->withoutPlaceholders (),
500+ );
551501
552502 return new ChatMemory ($ memoryStore );
553503 }
0 commit comments