|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Cortex\Agents\Middleware; |
| 6 | + |
| 7 | +use Closure; |
| 8 | +use Cortex\Pipeline\RuntimeConfig; |
| 9 | +use Cortex\Support\Traits\CanPipe; |
| 10 | +use Cortex\Agents\Contracts\Middleware; |
| 11 | + |
| 12 | +/** |
| 13 | + * Abstract base class that provides default implementations for middleware hook methods. |
| 14 | + * |
| 15 | + * This class does not implement any middleware interfaces - consumers should implement |
| 16 | + * the interfaces they need (BeforePromptMiddleware, BeforeModelMiddleware, AfterModelMiddleware). |
| 17 | + * The hook methods provided here can be used to satisfy those interface requirements. |
| 18 | + * |
| 19 | + * Example - implementing a single interface: |
| 20 | + * ```php |
| 21 | + * class BeforePromptOnly extends AbstractMiddleware implements BeforePromptMiddleware |
| 22 | + * { |
| 23 | + * // beforePrompt() is provided by AbstractMiddleware, delegates to handlePipeable() |
| 24 | + * // Override if you need custom logic |
| 25 | + * } |
| 26 | + * ``` |
| 27 | + * |
| 28 | + * Example - implementing multiple interfaces: |
| 29 | + * ```php |
| 30 | + * class MyMiddleware extends AbstractMiddleware |
| 31 | + * implements BeforePromptMiddleware, BeforeModelMiddleware, AfterModelMiddleware |
| 32 | + * { |
| 33 | + * public function beforePrompt(mixed $payload, RuntimeConfig $config, Closure $next): mixed |
| 34 | + * { |
| 35 | + * // Custom logic before prompt |
| 36 | + * return $next($payload, $config); |
| 37 | + * } |
| 38 | + * |
| 39 | + * public function beforeModel(mixed $payload, RuntimeConfig $config, Closure $next): mixed |
| 40 | + * { |
| 41 | + * // Custom logic before model call |
| 42 | + * return $next($payload, $config); |
| 43 | + * } |
| 44 | + * |
| 45 | + * // afterModel() uses default from AbstractMiddleware (delegates to handlePipeable()) |
| 46 | + * } |
| 47 | + * ``` |
| 48 | + */ |
| 49 | +abstract class AbstractMiddleware implements Middleware |
| 50 | +{ |
| 51 | + use CanPipe; |
| 52 | + |
| 53 | + /** |
| 54 | + * Hook that runs before the prompt is processed. |
| 55 | + * Default implementation delegates to handlePipeable(). |
| 56 | + * Override this method to provide before-prompt logic. |
| 57 | + * |
| 58 | + * @param mixed $payload The input to process |
| 59 | + * @param RuntimeConfig $config The runtime context |
| 60 | + * @param Closure $next The next stage in the pipeline |
| 61 | + * |
| 62 | + * @return mixed The processed result |
| 63 | + */ |
| 64 | + public function beforePrompt(mixed $payload, RuntimeConfig $config, Closure $next): mixed |
| 65 | + { |
| 66 | + return $this->handlePipeable($payload, $config, $next); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Hook that runs before the model call. |
| 71 | + * Default implementation delegates to handlePipeable(). |
| 72 | + * Override this method to provide before-model logic. |
| 73 | + * |
| 74 | + * @param mixed $payload The input to process |
| 75 | + * @param RuntimeConfig $config The runtime context |
| 76 | + * @param Closure $next The next stage in the pipeline |
| 77 | + * |
| 78 | + * @return mixed The processed result |
| 79 | + */ |
| 80 | + public function beforeModel(mixed $payload, RuntimeConfig $config, Closure $next): mixed |
| 81 | + { |
| 82 | + return $this->handlePipeable($payload, $config, $next); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Hook that runs after the model call. |
| 87 | + * Default implementation delegates to handlePipeable(). |
| 88 | + * Override this method to provide after-model logic. |
| 89 | + * |
| 90 | + * @param mixed $payload The input to process |
| 91 | + * @param RuntimeConfig $config The runtime context |
| 92 | + * @param Closure $next The next stage in the pipeline |
| 93 | + * |
| 94 | + * @return mixed The processed result |
| 95 | + */ |
| 96 | + public function afterModel(mixed $payload, RuntimeConfig $config, Closure $next): mixed |
| 97 | + { |
| 98 | + return $this->handlePipeable($payload, $config, $next); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Handle the pipeline processing. |
| 103 | + * Default implementation passes through unchanged. |
| 104 | + * Override this method to provide default logic used by all hooks, |
| 105 | + * or override individual hook methods for hook-specific logic. |
| 106 | + * |
| 107 | + * @param mixed $payload The input to process |
| 108 | + * @param RuntimeConfig $config The runtime context |
| 109 | + * @param Closure $next The next stage in the pipeline |
| 110 | + * |
| 111 | + * @return mixed The processed result |
| 112 | + */ |
| 113 | + public function handlePipeable(mixed $payload, RuntimeConfig $config, Closure $next): mixed |
| 114 | + { |
| 115 | + return $next($payload, $config); |
| 116 | + } |
| 117 | +} |
| 118 | + |
0 commit comments