From df02d2e72f687f0a9e27c256841251e25b0b454f Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Thu, 29 May 2025 14:48:03 +0800 Subject: [PATCH 1/5] Refactor message merging logic Simplified the logic for merging system and regular messages in ToolUseAgent and MemoryManager by using unified methods. This improves code readability and reduces redundancy. --- src/Agent/Tool/ToolUseAgent.php | 7 +------ src/Memory/MemoryManager.php | 7 +------ 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/src/Agent/Tool/ToolUseAgent.php b/src/Agent/Tool/ToolUseAgent.php index 629e585..80226a7 100644 --- a/src/Agent/Tool/ToolUseAgent.php +++ b/src/Agent/Tool/ToolUseAgent.php @@ -249,12 +249,7 @@ protected function call(?UserMessage $input = null, bool $stream = false): Gener while (true) { // 合并系统消息和普通消息 - $systemMessages = $this->memory->getSystemMessages(); - if (! empty($systemMessages)) { - $messages = array_merge([end($systemMessages)], $this->memory->getMessages()); - } else { - $messages = $this->memory->getMessages(); - } + $messages = $this->memory->getProcessedMessages(); if (! $stream) { $response = $this->model->chat( diff --git a/src/Memory/MemoryManager.php b/src/Memory/MemoryManager.php index 9561d45..dac95c7 100644 --- a/src/Memory/MemoryManager.php +++ b/src/Memory/MemoryManager.php @@ -110,12 +110,7 @@ public function getProcessedMessages(): array } // 合并系统消息和普通消息 - $systemMessages = $this->getSystemMessages(); - if (! empty($systemMessages)) { - $allMessages = array_merge([end($systemMessages)], $this->getMessages()); - } else { - $allMessages = $this->getMessages(); - } + $allMessages = array_merge($this->getSystemMessages(), $this->getMessages()); // 如果有策略,应用策略处理 if ($this->policy !== null) { From 2b46ce5de8a73cf97cf49abefbb146adc4792a51 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Thu, 29 May 2025 15:03:35 +0800 Subject: [PATCH 2/5] Revert "Refactor message merging logic" This reverts commit df02d2e72f687f0a9e27c256841251e25b0b454f. --- src/Agent/Tool/ToolUseAgent.php | 7 ++++++- src/Memory/MemoryManager.php | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Agent/Tool/ToolUseAgent.php b/src/Agent/Tool/ToolUseAgent.php index 80226a7..629e585 100644 --- a/src/Agent/Tool/ToolUseAgent.php +++ b/src/Agent/Tool/ToolUseAgent.php @@ -249,7 +249,12 @@ protected function call(?UserMessage $input = null, bool $stream = false): Gener while (true) { // 合并系统消息和普通消息 - $messages = $this->memory->getProcessedMessages(); + $systemMessages = $this->memory->getSystemMessages(); + if (! empty($systemMessages)) { + $messages = array_merge([end($systemMessages)], $this->memory->getMessages()); + } else { + $messages = $this->memory->getMessages(); + } if (! $stream) { $response = $this->model->chat( diff --git a/src/Memory/MemoryManager.php b/src/Memory/MemoryManager.php index dac95c7..9561d45 100644 --- a/src/Memory/MemoryManager.php +++ b/src/Memory/MemoryManager.php @@ -110,7 +110,12 @@ public function getProcessedMessages(): array } // 合并系统消息和普通消息 - $allMessages = array_merge($this->getSystemMessages(), $this->getMessages()); + $systemMessages = $this->getSystemMessages(); + if (! empty($systemMessages)) { + $allMessages = array_merge([end($systemMessages)], $this->getMessages()); + } else { + $allMessages = $this->getMessages(); + } // 如果有策略,应用策略处理 if ($this->policy !== null) { From 9ac9a1d3ee96c332b98a1d75da28150c5322afa1 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Thu, 29 May 2025 15:04:16 +0800 Subject: [PATCH 3/5] Fix system message merging logic Updated the logic for merging system messages and regular messages in ToolUseAgent and MemoryManager to include all system messages instead of only the last one. This ensures proper handling of system messages in both classes. --- src/Agent/Tool/ToolUseAgent.php | 2 +- src/Memory/MemoryManager.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Agent/Tool/ToolUseAgent.php b/src/Agent/Tool/ToolUseAgent.php index 629e585..5e46550 100644 --- a/src/Agent/Tool/ToolUseAgent.php +++ b/src/Agent/Tool/ToolUseAgent.php @@ -251,7 +251,7 @@ protected function call(?UserMessage $input = null, bool $stream = false): Gener // 合并系统消息和普通消息 $systemMessages = $this->memory->getSystemMessages(); if (! empty($systemMessages)) { - $messages = array_merge([end($systemMessages)], $this->memory->getMessages()); + $messages = array_merge($systemMessages, $this->memory->getMessages()); } else { $messages = $this->memory->getMessages(); } diff --git a/src/Memory/MemoryManager.php b/src/Memory/MemoryManager.php index 9561d45..cf1a9fa 100644 --- a/src/Memory/MemoryManager.php +++ b/src/Memory/MemoryManager.php @@ -112,7 +112,7 @@ public function getProcessedMessages(): array // 合并系统消息和普通消息 $systemMessages = $this->getSystemMessages(); if (! empty($systemMessages)) { - $allMessages = array_merge([end($systemMessages)], $this->getMessages()); + $allMessages = array_merge($systemMessages, $this->getMessages()); } else { $allMessages = $this->getMessages(); } From 000c0ceca841b407b1325edd7fc1af480ad18929 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Thu, 29 May 2025 18:10:03 +0800 Subject: [PATCH 4/5] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E5=90=88=E5=B9=B6=E9=80=BB=E8=BE=91=EF=BC=8C=E7=AE=80?= =?UTF-8?q?=E5=8C=96=E7=B3=BB=E7=BB=9F=E6=B6=88=E6=81=AF=E4=B8=8E=E6=99=AE?= =?UTF-8?q?=E9=80=9A=E6=B6=88=E6=81=AF=E7=9A=84=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Memory/MemoryManager.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Memory/MemoryManager.php b/src/Memory/MemoryManager.php index cf1a9fa..dac95c7 100644 --- a/src/Memory/MemoryManager.php +++ b/src/Memory/MemoryManager.php @@ -110,12 +110,7 @@ public function getProcessedMessages(): array } // 合并系统消息和普通消息 - $systemMessages = $this->getSystemMessages(); - if (! empty($systemMessages)) { - $allMessages = array_merge($systemMessages, $this->getMessages()); - } else { - $allMessages = $this->getMessages(); - } + $allMessages = array_merge($this->getSystemMessages(), $this->getMessages()); // 如果有策略,应用策略处理 if ($this->policy !== null) { From 833f417efd6dc4d08701b24bdcb5708e9bf7a19d Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Fri, 30 May 2025 13:52:37 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E4=B8=8E=E6=99=AE=E9=80=9A=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E7=9A=84=E5=90=88=E5=B9=B6=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Agent/Tool/ToolUseAgent.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Agent/Tool/ToolUseAgent.php b/src/Agent/Tool/ToolUseAgent.php index 5e46550..c8c049c 100644 --- a/src/Agent/Tool/ToolUseAgent.php +++ b/src/Agent/Tool/ToolUseAgent.php @@ -249,12 +249,7 @@ protected function call(?UserMessage $input = null, bool $stream = false): Gener while (true) { // 合并系统消息和普通消息 - $systemMessages = $this->memory->getSystemMessages(); - if (! empty($systemMessages)) { - $messages = array_merge($systemMessages, $this->memory->getMessages()); - } else { - $messages = $this->memory->getMessages(); - } + $messages = array_merge($this->memory->getSystemMessages(), $this->memory->getMessages()); if (! $stream) { $response = $this->model->chat(