|
15 | 15 | use SdAiAgent\Abilities\KnowledgeAbilities; |
16 | 16 | use SdAiAgent\Core\AbilityRegistry; |
17 | 17 | use SdAiAgent\Core\AbilityFunctionResolver; |
| 18 | +use SdAiAgent\Core\ConversationTrimmer; |
18 | 19 | use SdAiAgent\Core\IdenticalFailureTracker; |
| 20 | +use WordPress\AiClient\Messages\DTO\MessagePart; |
| 21 | +use WordPress\AiClient\Messages\DTO\ModelMessage; |
| 22 | +use WordPress\AiClient\Messages\DTO\UserMessage; |
19 | 23 | use WordPress\AiClient\Tools\DTO\FunctionCall; |
| 24 | +use WordPress\AiClient\Tools\DTO\FunctionResponse; |
20 | 25 | use WP_UnitTestCase; |
21 | 26 |
|
22 | 27 | final class ThrowingValidationAbility extends \WP_Ability { |
@@ -128,6 +133,60 @@ public function test_empty_arguments_return_schema_guidance_without_dispatching( |
128 | 133 | $this->assertStringContainsString( 'Do not retry with empty arguments', $payload['hint'] ); |
129 | 134 | } |
130 | 135 |
|
| 136 | + /** |
| 137 | + * Test that an idless Gemini call and response survive history validation. |
| 138 | + */ |
| 139 | + public function test_idless_function_call_response_remains_in_gemini_history(): void { |
| 140 | + $this->skip_if_resolver_unavailable(); |
| 141 | + |
| 142 | + $ability = $this->register_schema_thrower_ability(); |
| 143 | + $this->assertNotNull( $ability ); |
| 144 | + |
| 145 | + $function_name = \WP_AI_Client_Ability_Function_Resolver::ability_name_to_function_name( 'test-plugin/schema-thrower' ); |
| 146 | + $call = new FunctionCall( null, $function_name, array( 'query' => 'trigger callback validation exception' ) ); |
| 147 | + $resolver = new AbilityFunctionResolver( $ability ); |
| 148 | + $response = $resolver->execute_ability( $call ); |
| 149 | + |
| 150 | + $this->assertNull( $call->getId() ); |
| 151 | + $this->assertSame( '', $response->getId() ); |
| 152 | + |
| 153 | + $history = array( |
| 154 | + new ModelMessage( array( new MessagePart( $call ) ) ), |
| 155 | + new UserMessage( array( new MessagePart( $response ) ) ), |
| 156 | + ); |
| 157 | + |
| 158 | + $validated = ConversationTrimmer::validate_tool_pairs( $history ); |
| 159 | + $this->assertCount( 2, $validated ); |
| 160 | + $this->assertSame( $history[0], $validated[0] ); |
| 161 | + $this->assertSame( $history[1], $validated[1] ); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Test parallel idless calls require one response per call. |
| 166 | + */ |
| 167 | + public function test_parallel_idless_calls_require_distinct_responses(): void { |
| 168 | + $first_user_message = new UserMessage( array( new MessagePart( 'Do two things' ) ) ); |
| 169 | + $first_call = new FunctionCall( null, 'tool-a', array() ); |
| 170 | + $second_call = new FunctionCall( null, 'tool-b', array() ); |
| 171 | + $call_message = new ModelMessage( |
| 172 | + array( |
| 173 | + new MessagePart( $first_call ), |
| 174 | + new MessagePart( $second_call ), |
| 175 | + ) |
| 176 | + ); |
| 177 | + $response_message = new UserMessage( |
| 178 | + array( new MessagePart( new FunctionResponse( '', 'tool-a', '{"success":true}' ) ) ) |
| 179 | + ); |
| 180 | + $next_user_message = new UserMessage( array( new MessagePart( 'What happened?' ) ) ); |
| 181 | + $history = array( $first_user_message, $call_message, $response_message, $next_user_message ); |
| 182 | + |
| 183 | + $validated = ConversationTrimmer::validate_tool_pairs( $history ); |
| 184 | + |
| 185 | + $this->assertCount( 2, $validated ); |
| 186 | + $this->assertSame( $first_user_message, $validated[0] ); |
| 187 | + $this->assertSame( $next_user_message, $validated[1] ); |
| 188 | + } |
| 189 | + |
131 | 190 | public function test_public_knowledge_search_hydrates_empty_args_from_customer_query(): void { |
132 | 191 | $this->skip_if_resolver_unavailable(); |
133 | 192 | $this->ensure_knowledge_search_registered(); |
|
0 commit comments