Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/agent/src/Toolbox/StreamResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ public function getContent(): \Generator
if ($value instanceof ToolCallResult) {
yield from ($this->handleToolCallsCallback)($value, Message::ofAssistant($streamedResult))->getContent();

break;
continue;
}

if (!\is_string($value)) {
yield $value;

continue;
}

$streamedResult .= $value;
Expand Down
132 changes: 132 additions & 0 deletions src/agent/tests/Toolbox/StreamResultTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\Agent\Tests\Toolbox;

use PHPUnit\Framework\TestCase;
use Symfony\AI\Agent\Toolbox\StreamResult as ToolboxStreamResult;
use Symfony\AI\Platform\Message\AssistantMessage;
use Symfony\AI\Platform\Metadata\TokenUsage;
use Symfony\AI\Platform\Result\BaseResult;
use Symfony\AI\Platform\Result\ToolCall;
use Symfony\AI\Platform\Result\ToolCallResult;

final class StreamResultTest extends TestCase
{
public function testStreamsPlainChunksWithoutToolCall()
{
$chunks = ['He', 'llo'];
$generator = (function () use ($chunks) {
foreach ($chunks as $c) {
yield $c;
}
})();

$callbackCalled = false;
$callback = function () use (&$callbackCalled) {
$callbackCalled = true;

// Return any result, won't be used in this test
return new class extends BaseResult {
public function getContent(): iterable
{
yield 'ignored';
}
};
};

$stream = new ToolboxStreamResult($generator, $callback);
$received = [];
foreach ($stream->getContent() as $value) {
$received[] = $value;
}

$this->assertSame($chunks, $received);
$this->assertFalse($callbackCalled, 'Callback should not be called when no ToolCallResult appears.');
}

public function testInvokesCallbackOnToolCallAndYieldsItsContent()
{
$toolCallResult = new ToolCallResult(new ToolCall('id1', 'tool1', ['arg' => 'value']));

$generator = (function () use ($toolCallResult) {
yield 'He';
yield 'llo';
yield $toolCallResult;
yield 'AFTER';
})();

$receivedAssistantMessage = null;
$receivedToolCallResult = null;

$callback = function (ToolCallResult $result, AssistantMessage $assistantMessage) use (&$receivedAssistantMessage, &$receivedToolCallResult) {
$receivedToolCallResult = $result;
$receivedAssistantMessage = $assistantMessage;

// Return a result that itself yields more chunks
return new class extends BaseResult {
public function getContent(): iterable
{
yield ' world';
yield '!';
}
};
};

$stream = new ToolboxStreamResult($generator, $callback);

$received = [];
foreach ($stream->getContent() as $value) {
$received[] = $value;
}

$this->assertSame(['He', 'llo', ' world', '!', 'AFTER'], $received);
$this->assertInstanceOf(ToolCallResult::class, $receivedToolCallResult);
$this->assertInstanceOf(AssistantMessage::class, $receivedAssistantMessage);
$this->assertSame('Hello', $receivedAssistantMessage->content);
}

public function testStreamsPlainChunksWithTokenUsage()
{
$chunks = [
'He',
'llo',
new TokenUsage(),
];
$generator = (function () use ($chunks) {
foreach ($chunks as $c) {
yield $c;
}
})();

$callbackCalled = false;
$callback = function () use (&$callbackCalled) {
$callbackCalled = true;

// Return any result, won't be used in this test
return new class extends BaseResult {
public function getContent(): iterable
{
yield 'ignored';
}
};
};

$stream = new ToolboxStreamResult($generator, $callback);
$received = [];
foreach ($stream->getContent() as $value) {
$received[] = $value;
}

$this->assertSame($chunks, $received);
$this->assertFalse($callbackCalled, 'Callback should not be called when no ToolCallResult appears.');
}
}
11 changes: 11 additions & 0 deletions src/platform/src/Bridge/OpenAi/Gpt/ResultConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\AI\Platform\Exception\ContentFilterException;
use Symfony\AI\Platform\Exception\RateLimitExceededException;
use Symfony\AI\Platform\Exception\RuntimeException;
use Symfony\AI\Platform\Metadata\TokenUsage;
use Symfony\AI\Platform\Model;
use Symfony\AI\Platform\Result\ChoiceResult;
use Symfony\AI\Platform\Result\RawHttpResult;
Expand Down Expand Up @@ -106,6 +107,16 @@ private function convertStream(HttpResponse $result): \Generator
yield new ToolCallResult(...array_map($this->convertToolCall(...), $toolCalls));
}

if ($usage = $data['usage'] ?? null) {
yield new TokenUsage(
promptTokens: $usage['prompt_tokens'] ?? null,
completionTokens: $usage['completion_tokens'] ?? null,
thinkingTokens: $usage['completion_tokens_details']['reasoning_tokens'] ?? null,
cachedTokens: $usage['prompt_tokens_details']['cached_tokens'] ?? null,
totalTokens: $usage['total_tokens'] ?? null,
);
}

if (!isset($data['choices'][0]['delta']['content'])) {
continue;
}
Expand Down
139 changes: 139 additions & 0 deletions src/platform/tests/Bridge/OpenAi/Gpt/ResultConverterStreamTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\Platform\Tests\Bridge\OpenAi\Gpt;

use PHPUnit\Framework\TestCase;
use Symfony\AI\Platform\Bridge\OpenAi\Gpt\ResultConverter;
use Symfony\AI\Platform\Metadata\TokenUsage;
use Symfony\AI\Platform\Result\RawHttpResult;
use Symfony\AI\Platform\Result\StreamResult;
use Symfony\AI\Platform\Result\ToolCallResult;
use Symfony\Component\HttpClient\EventSourceHttpClient;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;

final class ResultConverterStreamTest extends TestCase
{
public function testStreamTextDeltas()
{
$sseBody = ''
."data: {\"choices\":[{\"delta\":{\"role\":\"assistant\"},\"index\":0}]}\n\n"
."data: {\"choices\":[{\"delta\":{\"content\":\"Hello \"},\"index\":0}]}\n\n"
."data: {\"choices\":[{\"delta\":{\"content\":\"world\"},\"index\":0}]}\n\n"
."data: {\"choices\":[{\"delta\":{},\"index\":0,\"finish_reason\":\"stop\"}]}\n\n"
."data: [DONE]\n\n";

$mockClient = new MockHttpClient([
new MockResponse($sseBody, [
'http_code' => 200,
'response_headers' => [
'content-type' => 'text/event-stream',
],
]),
]);
$esClient = new EventSourceHttpClient($mockClient);
$asyncResponse = $esClient->request('GET', 'http://localhost/stream');

$converter = new ResultConverter();
$result = $converter->convert(new RawHttpResult($asyncResponse), ['stream' => true]);

$this->assertInstanceOf(StreamResult::class, $result);
$chunks = [];
foreach ($result->getContent() as $delta) {
$chunks[] = $delta;
}

// Only text deltas are yielded; role and finish chunks are ignored
$this->assertSame(['Hello ', 'world'], $chunks);
}

public function testStreamToolCallsAreAssembledAndYielded()
{
// Simulate a tool call that is streamed in multiple argument parts
$sseBody = ''
."data: {\"choices\":[{\"delta\":{\"role\":\"assistant\"},\"index\":0}]}\n\n"
."data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"id\":\"call_123\",\"type\":\"function\",\"function\":{\"name\":\"test_function\",\"arguments\":\"{\\\"arg1\\\": \\\"value1\\\"}\"}}]},\"index\":0}]}\n\n"
."data: {\"choices\":[{\"delta\":{},\"index\":0,\"finish_reason\":\"tool_calls\"}]}\n\n"
."data: [DONE]\n\n";

$mockClient = new MockHttpClient([
new MockResponse($sseBody, [
'http_code' => 200,
'response_headers' => [
'content-type' => 'text/event-stream',
],
]),
]);
$esClient = new EventSourceHttpClient($mockClient);
$asyncResponse = $esClient->request('GET', 'http://localhost/stream');

$converter = new ResultConverter();
$result = $converter->convert(new RawHttpResult($asyncResponse), ['stream' => true]);

$this->assertInstanceOf(StreamResult::class, $result);

$yielded = [];
foreach ($result->getContent() as $delta) {
$yielded[] = $delta;
}

// Expect only one yielded item and it should be a ToolCallResult
$this->assertCount(1, $yielded);
$this->assertInstanceOf(ToolCallResult::class, $yielded[0]);
/** @var ToolCallResult $toolCallResult */
$toolCallResult = $yielded[0];
$toolCalls = $toolCallResult->getContent();

$this->assertCount(1, $toolCalls);
$this->assertSame('call_123', $toolCalls[0]->getId());
$this->assertSame('test_function', $toolCalls[0]->getName());
$this->assertSame(['arg1' => 'value1'], $toolCalls[0]->getArguments());
}

public function testStreamTokenUsage()
{
$sseBody = ''
."data: {\"choices\":[{\"delta\":{\"role\":\"assistant\"},\"index\":0}]}\n\n"
."data: {\"choices\":[{\"delta\":{\"content\":\"Hello \"},\"index\":0}]}\n\n"
."data: {\"choices\":[{\"delta\":{\"content\":\"world\"},\"index\":0}]}\n\n"
."data: {\"choices\":[{\"delta\":{},\"index\":0,\"finish_reason\":\"stop\"}]}\n\n"
."data: {\"usage\":{\"prompt_tokens\":1039,\"completion_tokens\":10,\"total_tokens\":1049,\"prompt_tokens_details\":{\"cached_tokens\":0,\"audio_tokens\":0},\"completion_tokens_details\":{\"reasoning_tokens\":0,\"audio_tokens\":0,\"accepted_prediction_tokens\":0,\"rejected_prediction_tokens\":0}}}\n\n"
."data: [DONE]\n\n";

$mockClient = new MockHttpClient([
new MockResponse($sseBody, [
'http_code' => 200,
'response_headers' => [
'content-type' => 'text/event-stream',
],
]),
]);
$esClient = new EventSourceHttpClient($mockClient);
$asyncResponse = $esClient->request('GET', 'http://localhost/stream');

$converter = new ResultConverter();
$result = $converter->convert(new RawHttpResult($asyncResponse), ['stream' => true]);

$this->assertInstanceOf(StreamResult::class, $result);

$yielded = [];
foreach ($result->getContent() as $delta) {
$yielded[] = $delta;
}
$this->assertCount(3, $yielded);
$this->assertInstanceOf(TokenUsage::class, $yielded[2]);
$this->assertSame(1039, $yielded[2]->promptTokens);
$this->assertSame(10, $yielded[2]->completionTokens);
$this->assertSame(1049, $yielded[2]->totalTokens);
$this->assertSame(0, $yielded[2]->cachedTokens);
}
}