Skip to content

Commit 0cc1ca3

Browse files
committed
Updated message return format
1 parent 736e23f commit 0cc1ca3

File tree

4 files changed

+25
-31
lines changed

4 files changed

+25
-31
lines changed

src/Agents/Adapter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ abstract public function getName(): string;
2222
* Send a message to the AI model
2323
*
2424
* @param Conversation $conversation The conversation instance containing messages and tracking tokens
25-
* @return array<Message> Response from the AI model
25+
* @return Message Response from the AI model
2626
*
2727
* @throws \Exception
2828
*/
29-
abstract public function send(Conversation $conversation): array;
29+
abstract public function send(Conversation $conversation): Message;
3030

3131
/**
3232
* Get available models for this adapter

src/Agents/Adapters/Anthropic.php

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ public function __construct(
7878
* Send a message to the Anthropic API
7979
*
8080
* @param Conversation $conversation
81-
* @return array<Message>
81+
* @return Message
8282
*
8383
* @throws \Exception
8484
*/
85-
public function send(Conversation $conversation): array
85+
public function send(Conversation $conversation): Message
8686
{
8787
if ($this->getAgent() === null) {
8888
throw new \Exception('Agent not set');
@@ -107,7 +107,7 @@ public function send(Conversation $conversation): array
107107
$instructions[] = "# " . $name . "\n\n" . $content;
108108
}
109109

110-
$collectedMessages = [];
110+
$content = '';
111111
$response = $client->fetch(
112112
'https://api.anthropic.com/v1/messages',
113113
Client::METHOD_POST,
@@ -121,19 +121,18 @@ public function send(Conversation $conversation): array
121121
'stream' => true,
122122
],
123123
[],
124-
function ($chunk) use ($conversation, &$collectedMessages) {
125-
$messages = $this->process($chunk, $conversation, $conversation->getListener());
126-
if ($messages) {
127-
$collectedMessages = array_merge($collectedMessages, $messages);
128-
}
124+
function ($chunk) use ($conversation, &$content) {
125+
$content .= $this->process($chunk, $conversation, $conversation->getListener());
129126
}
130127
);
131128

132129
if ($response->getStatusCode() >= 400) {
133130
throw new \Exception('Anthropic API error ('.$response->getStatusCode().'): '.$response->getBody());
134131
}
135132

136-
return $collectedMessages;
133+
$message = new Text($content);
134+
135+
return $message;
137136
}
138137

139138
/**
@@ -142,17 +141,18 @@ function ($chunk) use ($conversation, &$collectedMessages) {
142141
* @param \Utopia\Fetch\Chunk $chunk
143142
* @param Conversation $conversation
144143
* @param callable|null $listener
145-
* @return array<Message>
144+
* @return string
146145
*
147146
* @throws \Exception
148147
*/
149-
protected function process(Chunk $chunk, Conversation $conversation, ?callable $listener): array
148+
protected function process(Chunk $chunk, Conversation $conversation, ?callable $listener): string
150149
{
151-
$messages = [];
150+
$block = '';
152151
$data = $chunk->getData();
153152
$lines = explode("\n", $data);
154153

155154
foreach ($lines as $line) {
155+
156156
if (empty(trim($line))) {
157157
continue;
158158
}
@@ -194,17 +194,14 @@ protected function process(Chunk $chunk, Conversation $conversation, ?callable $
194194
}
195195

196196
$deltaType = $json['delta']['type'];
197-
$message = null;
198197

199198
if ($deltaType === 'text_delta' && isset($json['delta']['text']) && is_string($json['delta']['text'])) {
200-
$message = new Text($json['delta']['text']);
199+
$block = $json['delta']['text'];
201200
}
202201

203-
if ($message !== null) {
204-
$conversation->message(new Assistant('anthropic'), $message);
205-
$messages[] = $message;
202+
if (!empty($block)) {
206203
if ($listener !== null) {
207-
$listener($message);
204+
$listener($block);
208205
}
209206
}
210207
break;
@@ -226,7 +223,6 @@ protected function process(Chunk $chunk, Conversation $conversation, ?callable $
226223
break;
227224

228225
case 'message_stop':
229-
// End of message
230226
break;
231227

232228
case 'error':
@@ -235,7 +231,7 @@ protected function process(Chunk $chunk, Conversation $conversation, ?callable $
235231
}
236232
}
237233

238-
return $messages;
234+
return $block;
239235
}
240236

241237
/**

src/Agents/Adapters/OpenAI.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ public function __construct(
7171
* Send a message to the OpenAI API
7272
*
7373
* @param Conversation $conversation
74-
* @return array<Message> Response from the AI model
74+
* @return Message Response from the AI model
7575
*
7676
* @throws \Exception
7777
*/
78-
public function send(Conversation $conversation): array
78+
public function send(Conversation $conversation): Message
7979
{
8080
// TODO: Implement OpenAI API call
8181
// Example implementation structure:

src/Agents/Conversation.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,17 @@ public function message(Role $from, Message $message): self
7979
/**
8080
* Send the conversation to the agent and get response
8181
*
82-
* @return array<Message>
82+
* @return Message
8383
*
8484
* @throws \Exception
8585
*/
86-
public function send(): array
86+
public function send(): Message
8787
{
88-
$messages = $this->agent->getAdapter()->send($this);
88+
$message = $this->agent->getAdapter()->send($this);
8989
$from = new Assistant($this->agent->getAdapter()->getModel(), 'Assistant');
90-
foreach ($messages as $message) {
91-
$this->message($from, $message);
92-
}
90+
$this->message($from, $message);
9391

94-
return $messages;
92+
return $message;
9593
}
9694

9795
/**

0 commit comments

Comments
 (0)