2929use Symfony \AI \Platform \Result \ToolCall ;
3030use Symfony \AI \Platform \Tool \ExecutionReference ;
3131use Symfony \AI \Platform \Tool \Tool ;
32+ use Symfony \Contracts \Translation \TranslatorInterface ;
3233
3334#[CoversClass(SystemPromptInputProcessor::class)]
3435#[UsesClass(Gpt::class)]
@@ -89,7 +90,7 @@ public function execute(ToolCall $toolCall): mixed
8990 {
9091 return null ;
9192 }
92- }
93+ },
9394 );
9495
9596 $ input = new Input (new Gpt (), new MessageBag (Message::ofUser ('This is a user message ' )));
@@ -105,7 +106,7 @@ public function execute(ToolCall $toolCall): mixed
105106 public function testIncludeToolDefinitions ()
106107 {
107108 $ processor = new SystemPromptInputProcessor (
108- 'This is a system prompt ' ,
109+ 'This is a ' ,
109110 new class implements ToolboxInterface {
110111 public function getTools (): array
111112 {
@@ -127,7 +128,9 @@ public function execute(ToolCall $toolCall): mixed
127128 {
128129 return null ;
129130 }
130- }
131+ },
132+ $ this ->getTranslator (),
133+ true ,
131134 );
132135
133136 $ input = new Input (new Gpt (), new MessageBag (Message::ofUser ('This is a user message ' )));
@@ -138,7 +141,7 @@ public function execute(ToolCall $toolCall): mixed
138141 $ this ->assertInstanceOf (SystemMessage::class, $ messages [0 ]);
139142 $ this ->assertInstanceOf (UserMessage::class, $ messages [1 ]);
140143 $ this ->assertSame (<<<PROMPT
141- This is a system prompt
144+ This is a cool translated system prompt
142145
143146 # Tools
144147
@@ -169,7 +172,7 @@ public function execute(ToolCall $toolCall): mixed
169172 {
170173 return null ;
171174 }
172- }
175+ },
173176 );
174177
175178 $ input = new Input (new Gpt (), new MessageBag (Message::ofUser ('This is a user message ' )));
@@ -190,4 +193,69 @@ public function execute(ToolCall $toolCall): mixed
190193 A tool without parameters
191194 PROMPT , $ messages [0 ]->content );
192195 }
196+
197+ public function testWithTranslatedSystemPrompt ()
198+ {
199+ $ processor = new SystemPromptInputProcessor ('This is a ' , null , $ this ->getTranslator (), true );
200+
201+ $ input = new Input (new Gpt (), new MessageBag (Message::ofUser ('This is a user message ' )), []);
202+ $ processor ->processInput ($ input );
203+
204+ $ messages = $ input ->messages ->getMessages ();
205+ $ this ->assertCount (2 , $ messages );
206+ $ this ->assertInstanceOf (SystemMessage::class, $ messages [0 ]);
207+ $ this ->assertInstanceOf (UserMessage::class, $ messages [1 ]);
208+ $ this ->assertSame ('This is a cool translated system prompt ' , $ messages [0 ]->content );
209+ }
210+
211+ public function testWithTranslationDomainSystemPrompt ()
212+ {
213+ $ processor = new SystemPromptInputProcessor (
214+ 'This is a ' ,
215+ null ,
216+ $ this ->getTranslator (),
217+ true ,
218+ 'prompts '
219+ );
220+
221+ $ input = new Input (new Gpt (), new MessageBag (), []);
222+ $ processor ->processInput ($ input );
223+
224+ $ messages = $ input ->messages ->getMessages ();
225+ $ this ->assertCount (1 , $ messages );
226+ $ this ->assertInstanceOf (SystemMessage::class, $ messages [0 ]);
227+ $ this ->assertSame ('This is a cool translated system prompt with a translation domain ' , $ messages [0 ]->content );
228+ }
229+
230+ public function testWithMissingTranslator ()
231+ {
232+ $ processor = new SystemPromptInputProcessor (
233+ 'This is a ' ,
234+ null ,
235+ null ,
236+ true ,
237+ );
238+
239+ $ input = new Input (new Gpt (), new MessageBag (), []);
240+
241+ $ this ->expectExceptionMessage ('Prompt translation is enabled but no translator was provided ' );
242+ $ processor ->processInput ($ input );
243+ }
244+
245+ private function getTranslator (): TranslatorInterface
246+ {
247+ return new class implements TranslatorInterface {
248+ public function trans (string $ id , array $ parameters = [], ?string $ domain = null , ?string $ locale = null ): string
249+ {
250+ $ translated = \sprintf ('%s cool translated system prompt ' , $ id );
251+
252+ return $ domain ? $ translated .' with a translation domain ' : $ translated ;
253+ }
254+
255+ public function getLocale (): string
256+ {
257+ return 'en ' ;
258+ }
259+ };
260+ }
193261}
0 commit comments