Skip to content

Commit 6ef833d

Browse files
committed
add comments
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
1 parent 3556207 commit 6ef833d

4 files changed

Lines changed: 21 additions & 3 deletions

File tree

‎lib/Service/StreamingService.php‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ public function __construct(
1818
}
1919

2020
/**
21+
* Parse a streamed chat response and yield assistant content fragments as they arrive.
22+
*
23+
* The generator return value contains the reconstructed response payload, including
24+
* usage and rebuilt choices for SSE responses, or the original decoded JSON body
25+
* when the upstream responded with non-streamed JSON.
26+
*
2127
* @param array{body?: mixed, content-type?: mixed} $response
2228
* @return \Generator<string, mixed, mixed, array{usage?: array<string, mixed>, choices?: array<int, array<string, mixed>>}>
2329
* @throws Exception
@@ -85,6 +91,9 @@ public function parseStreamChatResponse(array $response): \Generator {
8591
}
8692

8793
/**
94+
* Parse one SSE event payload, update the accumulated choice state, and return any
95+
* assistant text fragments that should be yielded to the caller.
96+
*
8897
* @param string $event
8998
* @param bool $done
9099
* @param array<string, mixed>|null $usage
@@ -166,6 +175,12 @@ private function parseSseEvent(string $event, bool &$done, ?array &$usage, array
166175
}
167176

168177
/**
178+
* Append a raw chunk to the SSE buffer and parse all complete events currently
179+
* available in that buffer.
180+
*
181+
* When `$flush` is true, the remaining buffer is parsed as a final event even if it
182+
* is not terminated by the usual blank-line SSE delimiter.
183+
*
169184
* @param string $chunk
170185
* @param string $buffer
171186
* @param bool $done
@@ -197,6 +212,9 @@ private function parseSseChunk(string $chunk, string &$buffer, bool &$done, ?arr
197212
}
198213

199214
/**
215+
* Merge fragmented `tool_calls` deltas from multiple SSE events into the accumulated
216+
* message structure for a single choice.
217+
*
200218
* @param array<string, mixed> $message
201219
* @param array<int, array<string, mixed>> $toolCallsDelta
202220
*/

‎tests/stream_i_client_repro.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/**
4-
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
4+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
55
* SPDX-License-Identifier: AGPL-3.0-or-later
66
*/
77

‎tests/stream_i_client_sse_server.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/**
4-
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
4+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
55
* SPDX-License-Identifier: AGPL-3.0-or-later
66
*/
77

‎tests/streaming_service_repro.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/**
4-
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
4+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
55
* SPDX-License-Identifier: AGPL-3.0-or-later
66
*/
77

0 commit comments

Comments
 (0)