Skip to content
Open
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
29 changes: 29 additions & 0 deletions lib/Service/ContentChunkParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\OpenAi\Service;

class ContentChunkParser {
/**
* Extract the text of typed content chunks. Only chunks with type `text` and a
* string `text` value contribute; `thinking`, `reference` and other types are skipped.
*
* @param array<int, mixed> $chunks
* @return string
*/
public static function contentChunksToText(array $chunks): string {
$text = '';
foreach ($chunks as $chunk) {
if (is_array($chunk) && ($chunk['type'] ?? null) === 'text' && isset($chunk['text']) && is_string($chunk['text'])) {
$text .= $chunk['text'];
}
}
return $text;
}
}
8 changes: 1 addition & 7 deletions lib/Service/OpenAiAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -1630,14 +1630,8 @@ private function normalizeChatCompletionResponse(array $response): array {
if (is_string($choice['message']['content'])) {
$completions['messages'][] = $choice['message']['content'];
} elseif (is_array($choice['message']['content'])) {
$messageContent = [];
// Handles more complex mistral message content (TODO: missing reasoning for now look at https://github.com/nextcloud/integration_openai/pull/409#discussion_r3638108824)
foreach ($choice['message']['content'] as $content) {
if ($content['type'] === 'text' && is_string($content['text'])) {
$messageContent[] = $content['text'];
}
}
$completions['messages'][] = implode('', $messageContent);
$completions['messages'][] = ContentChunkParser::contentChunksToText($choice['message']['content']);
}
}
if (isset($choice['message']['reasoning_content']) && is_string($choice['message']['reasoning_content'])) {
Expand Down
8 changes: 7 additions & 1 deletion lib/Service/StreamingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,13 @@ private function parseSseEvent(string $event, bool &$done, ?array &$usage, array
$choices[$index]['finish_reason'] = $choice['finish_reason'];
}

if (isset($choice['delta']['content']) && is_string($choice['delta']['content']) && $choice['delta']['content'] !== '') {
if (isset($choice['delta']['content']) && is_array($choice['delta']['content']) && $choice['delta']['content'] !== []) {
$deltaText = ContentChunkParser::contentChunksToText($choice['delta']['content']);
if ($deltaText !== '') {
$choices[$index]['message']['content'] = ($choices[$index]['message']['content'] ?? '') . $deltaText;
$partials[] = ['kind' => 'content', 'text' => $deltaText, 'index' => $index];
}
} elseif (isset($choice['delta']['content']) && is_string($choice['delta']['content']) && $choice['delta']['content'] !== '') {
$choices[$index]['message']['content'] = ($choices[$index]['message']['content'] ?? '') . $choice['delta']['content'];
$partials[] = ['kind' => 'content', 'text' => $choice['delta']['content'], 'index' => $index];
} elseif (isset($choice['message']['content']) && is_string($choice['message']['content']) && $choice['message']['content'] !== '') {
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/Service/ContentChunkParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

declare(strict_types=1);

namespace OCA\OpenAi\Tests\Unit\Service;

use OCA\OpenAi\Service\ContentChunkParser;
use PHPUnit\Framework\TestCase;

class ContentChunkParserTest extends TestCase {
public function testTextChunksAreConcatenatedAndOtherTypesSkipped(): void {
$this->assertSame('Amsterdam. It is worth visiting.', ContentChunkParser::contentChunksToText([
['type' => 'text', 'text' => 'Amsterdam'],
['type' => 'reference', 'reference_ids' => [1]],
['type' => 'thinking', 'thinking' => [['type' => 'text', 'text' => 'Thinking']]],
['type' => 'text', 'text' => '. It is worth visiting.'],
]));
}

public function testMalformedChunksAreSkipped(): void {
$this->assertSame('Hello', ContentChunkParser::contentChunksToText([
'not a chunk',
['type' => 'text'],
['type' => 'text', 'text' => 1],
['type' => 'text', 'text' => 'Hello'],
]));
}

public function testEmptyChunksReturnAnEmptyString(): void {
$this->assertSame('', ContentChunkParser::contentChunksToText([]));
}
}
87 changes: 87 additions & 0 deletions tests/unit/Service/StreamingServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,93 @@ public function testEofFlushesAnIncompleteFinalEventAndClosesTheStream(): void {
$this->assertFalse(is_resource($body));
}

public function testTypedContentChunksAreAssembledAndStreamed(): void {
$body = fopen('php://temp', 'r+');
$this->assertIsResource($body);
fwrite($body, "data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":[{\"type\":\"thinking\",\"thinking\":[{\"type\":\"text\",\"text\":\"Thinking\"}]}]}}]}\n\n"
. "data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":[{\"type\":\"text\",\"text\":\"Hel\"}]}}]}\n\n"
. "data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":[{\"type\":\"text\",\"text\":\"lo\"},{\"type\":\"reference\",\"reference_ids\":[1]}]},\"finish_reason\":\"stop\"}]}\n\n"
. "data: [DONE]\n\n");
rewind($body);

$l10n = $this->createMock(IL10N::class);
$l10n->method('t')->willReturnArgument(0);
$service = new StreamingService($l10n);
$generator = $service->parseStreamChatResponse([
'body' => $body,
'content-type' => 'text/event-stream',
]);

$this->assertSame([
['kind' => 'content', 'text' => 'Hel', 'index' => 0],
['kind' => 'content', 'text' => 'lo', 'index' => 0],
], iterator_to_array($generator, false));
$this->assertSame([
'choices' => [[
'index' => 0,
'message' => ['content' => 'Hello'],
'finish_reason' => 'stop',
]],
], $generator->getReturn());
}

public function testStringDeltasContinueAfterATypedContentChunk(): void {
$body = fopen('php://temp', 'r+');
$this->assertIsResource($body);
fwrite($body, "data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":[{\"type\":\"thinking\",\"thinking\":[{\"type\":\"text\",\"text\":\"Thinking\"}]},{\"type\":\"text\",\"text\":\"Hel\"}]}}]}\n\n"
. "data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"lo\"}}]}\n\n"
. "data: {\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"stop\"}]}\n\n"
. "data: [DONE]\n\n");
rewind($body);

$l10n = $this->createMock(IL10N::class);
$l10n->method('t')->willReturnArgument(0);
$service = new StreamingService($l10n);
$generator = $service->parseStreamChatResponse([
'body' => $body,
'content-type' => 'text/event-stream',
]);

$this->assertSame([
['kind' => 'content', 'text' => 'Hel', 'index' => 0],
['kind' => 'content', 'text' => 'lo', 'index' => 0],
], iterator_to_array($generator, false));
$this->assertSame([
'choices' => [[
'index' => 0,
'message' => ['content' => 'Hello'],
'finish_reason' => 'stop',
]],
], $generator->getReturn());
}

public function testEmptyContentArrayFallsBackToMessageContent(): void {
$body = fopen('php://temp', 'r+');
$this->assertIsResource($body);
fwrite($body, "data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":[]},\"message\":{\"content\":\"Fallback\"},\"finish_reason\":\"stop\"}]}\n\n"
. "data: [DONE]\n\n");
rewind($body);

$l10n = $this->createMock(IL10N::class);
$l10n->method('t')->willReturnArgument(0);
$service = new StreamingService($l10n);
$generator = $service->parseStreamChatResponse([
'body' => $body,
'content-type' => 'text/event-stream',
]);

$this->assertSame([
['kind' => 'content', 'text' => 'Fallback', 'index' => 0],
], iterator_to_array($generator, false));
$this->assertSame([
'choices' => [[
'index' => 0,
'message' => ['content' => 'Fallback'],
'finish_reason' => 'stop',
]],
], $generator->getReturn());
}

public function testDoneEventStopsReadingAnOpenStreamAndClosesIt(): void {
$events = "data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"Hello\"}}]}\n\n"
. "data: {\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"stop\"}]}\n\n"
Expand Down