1717use Symfony \AI \Platform \Message \MessageBag ;
1818use Symfony \AI \Platform \PlatformInterface ;
1919use Symfony \AI \Platform \Result \ResultInterface ;
20+ use Symfony \AI \Voice \VoiceProviderInterface ;
2021
2122/**
2223 * @author Christopher Hertel <[email protected] > @@ -33,20 +34,28 @@ final class Agent implements AgentInterface
3334 */
3435 private readonly array $ outputProcessors ;
3536
37+ /**
38+ * @var VoiceProviderInterface[]
39+ */
40+ private array $ voiceProviders ;
41+
3642 /**
3743 * @param InputProcessorInterface[] $inputProcessors
3844 * @param OutputProcessorInterface[] $outputProcessors
45+ * @param VoiceProviderInterface[] $voiceProviders
3946 * @param non-empty-string $model
4047 */
4148 public function __construct (
4249 private readonly PlatformInterface $ platform ,
4350 private readonly string $ model ,
4451 iterable $ inputProcessors = [],
4552 iterable $ outputProcessors = [],
53+ iterable $ voiceProviders = [],
4654 private readonly string $ name = 'agent ' ,
4755 ) {
4856 $ this ->inputProcessors = $ this ->initializeProcessors ($ inputProcessors , InputProcessorInterface::class);
4957 $ this ->outputProcessors = $ this ->initializeProcessors ($ outputProcessors , OutputProcessorInterface::class);
58+ $ this ->voiceProviders = $ this ->initializeVoiceProviders ($ voiceProviders );
5059 }
5160
5261 public function getModel (): string
@@ -69,7 +78,7 @@ public function getName(): string
6978 public function call (MessageBag $ messages , array $ options = []): ResultInterface
7079 {
7180 $ input = new Input ($ this ->getModel (), $ messages , $ options );
72- array_map (fn (InputProcessorInterface $ processor ) => $ processor ->processInput ($ input ), $ this ->inputProcessors );
81+ array_map (static fn (InputProcessorInterface $ processor ) => $ processor ->processInput ($ input ), $ this ->inputProcessors );
7382
7483 $ model = $ input ->getModel ();
7584 $ messages = $ input ->getMessageBag ();
@@ -78,7 +87,8 @@ public function call(MessageBag $messages, array $options = []): ResultInterface
7887 $ result = $ this ->platform ->invoke ($ model , $ messages , $ options )->getResult ();
7988
8089 $ output = new Output ($ model , $ result , $ messages , $ options );
81- array_map (fn (OutputProcessorInterface $ processor ) => $ processor ->processOutput ($ output ), $ this ->outputProcessors );
90+ array_map (static fn (OutputProcessorInterface $ processor ) => $ processor ->processOutput ($ output ), $ this ->outputProcessors );
91+ array_map (static fn (VoiceProviderInterface $ provider ) => $ provider ->addVoice ($ output ), $ this ->voiceProviders );
8292
8393 return $ output ->getResult ();
8494 }
@@ -103,4 +113,24 @@ private function initializeProcessors(iterable $processors, string $interface):
103113
104114 return $ processors instanceof \Traversable ? iterator_to_array ($ processors ) : $ processors ;
105115 }
116+
117+ /**
118+ * @param VoiceProviderInterface[] $providers
119+ *
120+ * @return VoiceProviderInterface[]
121+ */
122+ private function initializeVoiceProviders (iterable $ providers ): array
123+ {
124+ foreach ($ providers as $ provider ) {
125+ if (!$ provider instanceof VoiceProviderInterface) {
126+ throw new InvalidArgumentException (\sprintf ('Voice provider "%s" must implement "%s". ' , $ provider ::class, VoiceProviderInterface::class));
127+ }
128+
129+ if ($ provider instanceof AgentAwareInterface) {
130+ $ provider ->setAgent ($ this );
131+ }
132+ }
133+
134+ return $ providers instanceof \Traversable ? iterator_to_array ($ providers ) : $ providers ;
135+ }
106136}
0 commit comments