Skip to content

Commit

Permalink
Merge pull request #137 from Taewan-P/fix/anthropic-api-update
Browse files Browse the repository at this point in the history
Add new required types for Anthropic API
  • Loading branch information
Taewan-P authored Dec 18, 2024
2 parents 20fb65a + 5592fdd commit 1eb0fc3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ data class Usage(
@SerialName("input_tokens")
val inputTokens: Int,

@SerialName("cache_creation_input_tokens")
val cacheCreationInputTokens: Int? = null,

@SerialName("cache_read_input_tokens")
val cacheReadInputTokens: Int? = null,

@SerialName("output_tokens")
val outputTokens: Int
)
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,14 @@ class AnthropicAPIImpl @Inject constructor(

private suspend inline fun <reified T> FlowCollector<T>.streamEventsFrom(response: HttpResponse) {
val channel: ByteReadChannel = response.body()
val jsonInstance = Json { ignoreUnknownKeys = true }

try {
while (currentCoroutineContext().isActive && !channel.isClosedForRead) {
val line = channel.readUTF8Line() ?: continue
val value: T = when {
line.startsWith(STREAM_END_TOKEN) -> break
line.startsWith(STREAM_PREFIX) -> Json.decodeFromString(line.removePrefix(STREAM_PREFIX))
line.startsWith(STREAM_PREFIX) -> jsonInstance.decodeFromString(line.removePrefix(STREAM_PREFIX))
else -> continue
}
emit(value)
Expand Down

0 comments on commit 1eb0fc3

Please sign in to comment.