66use Utopia \Agents \Conversation ;
77use Utopia \Agents \Message ;
88use Utopia \Agents \Messages \Text ;
9- use Utopia \Agents \Roles \Assistant ;
109use Utopia \Fetch \Chunk ;
1110use Utopia \Fetch \Client ;
1211
@@ -77,12 +76,13 @@ public function __construct(
7776 /**
7877 * Send a message to the Anthropic API
7978 *
80- * @param Conversation $conversation
79+ * @param array<Message> $messages
80+ * @param callable|null $listener
8181 * @return Message
8282 *
8383 * @throws \Exception
8484 */
85- public function send (Conversation $ conversation ): Message
85+ public function send (array $ messages , ? callable $ listener = null ): Message
8686 {
8787 if ($ this ->getAgent () === null ) {
8888 throw new \Exception ('Agent not set ' );
@@ -94,8 +94,7 @@ public function send(Conversation $conversation): Message
9494 ->addHeader ('anthropic-version ' , '2023-06-01 ' )
9595 ->addHeader ('content-type ' , 'application/json ' );
9696
97- $ messages = [];
98- foreach ($ conversation ->getMessages () as $ message ) {
97+ foreach ($ messages as $ message ) {
9998 $ messages [] = [
10099 'role ' => $ message ['role ' ],
101100 'content ' => $ message ['content ' ],
@@ -121,8 +120,8 @@ public function send(Conversation $conversation): Message
121120 'stream ' => true ,
122121 ],
123122 [],
124- function ($ chunk ) use ($ conversation , & $ content ) {
125- $ content .= $ this ->process ($ chunk , $ conversation , $ conversation -> getListener () );
123+ function ($ chunk ) use (& $ content , $ listener ) {
124+ $ content .= $ this ->process ($ chunk , $ listener );
126125 }
127126 );
128127
@@ -145,7 +144,7 @@ function ($chunk) use ($conversation, &$content) {
145144 *
146145 * @throws \Exception
147146 */
148- protected function process (Chunk $ chunk , Conversation $ conversation , ?callable $ listener ): string
147+ protected function process (Chunk $ chunk , ?callable $ listener ): string
149148 {
150149 $ block = '' ;
151150 $ data = $ chunk ->getData ();
@@ -176,10 +175,10 @@ protected function process(Chunk $chunk, Conversation $conversation, ?callable $
176175 if (isset ($ json ['message ' ]['usage ' ])) {
177176 $ usage = $ json ['message ' ]['usage ' ];
178177 if (isset ($ usage ['input_tokens ' ]) && is_int ($ usage ['input_tokens ' ])) {
179- $ conversation ->countInputTokens ($ usage ['input_tokens ' ]);
178+ $ this ->countInputTokens ($ usage ['input_tokens ' ]);
180179 }
181180 if (isset ($ usage ['output_tokens ' ]) && is_int ($ usage ['output_tokens ' ])) {
182- $ conversation ->countOutputTokens ($ usage ['output_tokens ' ]);
181+ $ this ->countOutputTokens ($ usage ['output_tokens ' ]);
183182 }
184183 }
185184 break ;
@@ -211,13 +210,13 @@ protected function process(Chunk $chunk, Conversation $conversation, ?callable $
211210 break ;
212211
213212 case 'message_delta ' :
214- if (isset ($ json ['message ' ][ ' usage ' ])) {
215- $ usage = $ json ['message ' ][ ' usage ' ];
213+ if (isset ($ json ['usage ' ])) {
214+ $ usage = $ json ['usage ' ];
216215 if (isset ($ usage ['input_tokens ' ]) && is_int ($ usage ['input_tokens ' ])) {
217- $ conversation ->countInputTokens ($ usage ['input_tokens ' ]);
216+ $ this ->countInputTokens ($ usage ['input_tokens ' ]);
218217 }
219218 if (isset ($ usage ['output_tokens ' ]) && is_int ($ usage ['output_tokens ' ])) {
220- $ conversation ->countOutputTokens ($ usage ['output_tokens ' ]);
219+ $ this ->countOutputTokens ($ usage ['output_tokens ' ]);
221220 }
222221 }
223222 break ;
0 commit comments