Skip to content

Commit 0a0b20a

Browse files
committed
Fix GoogleLLMClient (include finish reason and emit end frame correctly)
1 parent 3aa210d commit 0a0b20a

File tree

1 file changed

+13
-17
lines changed
  • prompt/prompt-executor/prompt-executor-clients/prompt-executor-google-client/src/commonMain/kotlin/ai/koog/prompt/executor/clients/google

1 file changed

+13
-17
lines changed

prompt/prompt-executor/prompt-executor-clients/prompt-executor-google-client/src/commonMain/kotlin/ai/koog/prompt/executor/clients/google/GoogleLLMClient.kt

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ import io.ktor.http.isSuccess
5151
import io.ktor.serialization.kotlinx.json.json
5252
import kotlinx.coroutines.Dispatchers
5353
import kotlinx.coroutines.flow.Flow
54-
import kotlinx.coroutines.flow.onCompletion
5554
import kotlinx.coroutines.withContext
5655
import kotlinx.datetime.Clock
5756
import kotlinx.serialization.json.Json
@@ -172,27 +171,24 @@ public open class GoogleLLMClient(
172171
setBody(request)
173172
}
174173
) {
175-
incoming.onCompletion {
176-
if (it == null)
177-
emitEnd() // TODO: finishReason?
178-
else
179-
throw it
180-
}.collect { event ->
174+
incoming.collect { event ->
181175
event
182176
.takeIf { it.data != "[DONE]" }
183177
?.data?.trim()?.let { json.decodeFromString<GoogleResponse>(it) }
184-
?.candidates?.firstOrNull()?.content
185-
?.parts?.forEach { part ->
186-
when (part) {
187-
is GooglePart.FunctionCall -> emitToolCall(
188-
id = part.functionCall.id,
189-
name = part.functionCall.name,
190-
content = part.functionCall.args?.toString() ?: "{}"
191-
)
178+
?.candidates?.firstOrNull()?.let { candidate ->
179+
candidate.content?.parts?.forEach { part ->
180+
when (part) {
181+
is GooglePart.FunctionCall -> emitToolCall(
182+
id = part.functionCall.id,
183+
name = part.functionCall.name,
184+
content = part.functionCall.args?.toString() ?: "{}"
185+
)
192186

193-
is GooglePart.Text -> emitAppend(part.text)
194-
else -> Unit
187+
is GooglePart.Text -> emitAppend(part.text)
188+
else -> Unit
189+
}
195190
}
191+
candidate.finishReason?.let { emitEnd(it) }
196192
}
197193
}
198194
}

0 commit comments

Comments
 (0)