Skip to content

Commit 4daf705

Browse files
committed
new chat history implementation
1 parent ce1d406 commit 4daf705

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

src/Chat/History/AbstractChatHistory.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,12 @@ protected function ensureValidAlternation(): void
174174

175175
// Tool result messages have a special case - they're user messages
176176
// but can only follow tool call messages (assistant)
177-
if ($message instanceof ToolCallResultMessage) {
178-
// This is valid after a ToolCallMessage
179-
if (!empty($result) &&
180-
$result[\count($result) - 1] instanceof ToolCallMessage) {
181-
$result[] = $message;
182-
// After the tool result, we expect assistant again
183-
$expectingRole = MessageRole::ASSISTANT->value;
184-
continue;
185-
}
177+
// This is valid after a ToolCallMessage
178+
if ($message instanceof ToolCallResultMessage && ($result !== [] && $result[\count($result) - 1] instanceof ToolCallMessage)) {
179+
$result[] = $message;
180+
// After the tool result, we expect assistant again
181+
$expectingRole = MessageRole::ASSISTANT->value;
182+
continue;
186183
}
187184

188185
// Check if this message has the expected role

tests/ChatHistory/InMemoryChatHistoryTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,15 @@ public function test_remove_intermediate_invalid_message_types(): void
184184
$this->chatHistory->flushAll();
185185
}
186186

187-
public function test_empty_history_if_no_user_message()
187+
public function test_empty_history_if_no_user_message(): void
188188
{
189189
$this->chatHistory->flushAll();
190190

191191
$this->chatHistory->addMessage(new AssistantMessage('Test message'));
192192
$this->assertEmpty($this->chatHistory->getMessages());
193193
}
194194

195-
public function test_remove_messages_before_the_first_user_message()
195+
public function test_remove_messages_before_the_first_user_message(): void
196196
{
197197
$this->chatHistory->flushAll();
198198

0 commit comments

Comments
 (0)