|
8 | 8 | use NeuronAI\Agent\Events\AgentStartEvent; |
9 | 9 | use NeuronAI\Agent\Events\AIInferenceEvent; |
10 | 10 | use NeuronAI\Agent\Events\ToolCallEvent; |
| 11 | +use NeuronAI\Agent\Memory\MemoryAwareChatHistory; |
| 12 | +use NeuronAI\Agent\Memory\MemoryInterface; |
11 | 13 | use NeuronAI\Agent\Nodes\ChatNode; |
12 | 14 | use NeuronAI\Agent\Nodes\ParallelToolNode; |
13 | 15 | use NeuronAI\Agent\Nodes\StartNode; |
@@ -42,6 +44,16 @@ class Agent extends Workflow implements AgentInterface |
42 | 44 |
|
43 | 45 | protected ChatHistoryInterface $chatHistory; |
44 | 46 |
|
| 47 | + /** |
| 48 | + * The history instance shared by the composed nodes. When memory is |
| 49 | + * configured this is a decorator around the developer-supplied history. |
| 50 | + */ |
| 51 | + protected ?ChatHistoryInterface $effectiveChatHistory = null; |
| 52 | + |
| 53 | + protected ?MemoryInterface $memory = null; |
| 54 | + |
| 55 | + protected bool $memoryResolved = false; |
| 56 | + |
45 | 57 | /** |
46 | 58 | * The conversation this run belongs to, and the run's declared workflow |
47 | 59 | * ID. Assigned exactly once through adoptThreadId() and NEVER generated — |
@@ -109,6 +121,42 @@ protected function attachChatHistory(ChatHistoryInterface $chatHistory): void |
109 | 121 | } |
110 | 122 |
|
111 | 123 | $this->chatHistory = $chatHistory; |
| 124 | + $this->effectiveChatHistory = null; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Provide the default long-term memory implementation. Subclasses may |
| 129 | + * override this hook; null keeps the Agent memory-free. |
| 130 | + */ |
| 131 | + protected function memory(): ?MemoryInterface |
| 132 | + { |
| 133 | + return null; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Configure long-term memory before the Agent graph is composed. |
| 138 | + */ |
| 139 | + public function setMemory(MemoryInterface $memory): self |
| 140 | + { |
| 141 | + if ($this->eventNodeMap !== []) { |
| 142 | + throw new AgentException('Memory must be configured before the Agent starts executing.'); |
| 143 | + } |
| 144 | + |
| 145 | + $this->memory = $memory; |
| 146 | + $this->memoryResolved = true; |
| 147 | + $this->effectiveChatHistory = null; |
| 148 | + |
| 149 | + return $this; |
| 150 | + } |
| 151 | + |
| 152 | + public function getMemory(): ?MemoryInterface |
| 153 | + { |
| 154 | + if (!$this->memoryResolved) { |
| 155 | + $this->memory = $this->memory(); |
| 156 | + $this->memoryResolved = true; |
| 157 | + } |
| 158 | + |
| 159 | + return $this->memory; |
112 | 160 | } |
113 | 161 |
|
114 | 162 | /** |
@@ -139,7 +187,14 @@ public function getChatHistory(): ChatHistoryInterface |
139 | 187 | $this->attachChatHistory($this->chatHistory()); |
140 | 188 | } |
141 | 189 |
|
142 | | - return $this->chatHistory; |
| 190 | + if ($this->effectiveChatHistory === null) { |
| 191 | + $memory = $this->getMemory(); |
| 192 | + $this->effectiveChatHistory = $memory instanceof MemoryInterface |
| 193 | + ? new MemoryAwareChatHistory($this->chatHistory, $memory) |
| 194 | + : $this->chatHistory; |
| 195 | + } |
| 196 | + |
| 197 | + return $this->effectiveChatHistory; |
143 | 198 | } |
144 | 199 |
|
145 | 200 | /** |
|
0 commit comments