3333 */
3434 private array $ outputProcessors ;
3535
36+ /**
37+ * @var VoiceProviderInterface[]
38+ */
39+ private array $ voiceProviders ;
40+
3641 /**
3742 * @param InputProcessorInterface[] $inputProcessors
3843 * @param OutputProcessorInterface[] $outputProcessors
44+ * @param VoiceProviderInterface[] $voiceProviders
3945 * @param non-empty-string $model
4046 */
4147 public function __construct (
4248 private PlatformInterface $ platform ,
4349 private string $ model ,
4450 iterable $ inputProcessors = [],
4551 iterable $ outputProcessors = [],
52+ iterable $ voiceProviders = [],
4653 private string $ name = 'agent ' ,
4754 ) {
4855 $ this ->inputProcessors = $ this ->initializeProcessors ($ inputProcessors , InputProcessorInterface::class);
4956 $ this ->outputProcessors = $ this ->initializeProcessors ($ outputProcessors , OutputProcessorInterface::class);
57+ $ this ->voiceProviders = $ this ->initializeVoiceProviders ($ voiceProviders );
5058 }
5159
5260 public function getModel (): string
@@ -79,6 +87,7 @@ public function call(MessageBag $messages, array $options = []): ResultInterface
7987
8088 $ output = new Output ($ model , $ result , $ messages , $ options );
8189 array_map (fn (OutputProcessorInterface $ processor ) => $ processor ->processOutput ($ output ), $ this ->outputProcessors );
90+ array_map (static fn (VoiceProviderInterface $ provider ) => $ provider ->addVoice ($ output ), $ this ->voiceProviders );
8291
8392 return $ output ->getResult ();
8493 }
@@ -103,4 +112,20 @@ private function initializeProcessors(iterable $processors, string $interface):
103112
104113 return $ processors instanceof \Traversable ? iterator_to_array ($ processors ) : $ processors ;
105114 }
115+
116+ /**
117+ * @param VoiceProviderInterface[] $providers
118+ *
119+ * @return InputProcessorInterface[]|OutputProcessorInterface[]
120+ */
121+ private function initializeVoiceProviders (iterable $ providers ): array
122+ {
123+ foreach ($ providers as $ provider ) {
124+ if (!$ provider instanceof VoiceProviderInterface) {
125+ throw new InvalidArgumentException (\sprintf ('Voice provider "%s" must implement "%s". ' , $ provider ::class, VoiceProviderInterface::class));
126+ }
127+ }
128+
129+ return $ providers instanceof \Traversable ? iterator_to_array ($ providers ) : $ providers ;
130+ }
106131}
0 commit comments