Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ public class LiveServerToolCall(public val functionCalls: List<FunctionCallPart>
toolCall.functionCalls.map { functionCall ->
FunctionCallPart(
name = functionCall.name,
args = functionCall.args.orEmpty().mapValues { it.value ?: JsonNull }
args = functionCall.args.orEmpty().mapValues { it.value ?: JsonNull },
id = functionCall.id
)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,14 @@ internal constructor(
while (true) {
val response = session.incoming.tryReceive()
if (response.isClosed || !startedReceiving.get()) break

response
.getOrNull()
?.let {
JSON.decodeFromString<InternalLiveServerMessage>(
it.readBytes().toString(Charsets.UTF_8)
)
val output = it.readBytes().toString(Charsets.UTF_8)
println(output)
JSON.decodeFromString<InternalLiveServerMessage>(output)
}
?.let { emit(it.toPublic()) }

yield()
}
}
Expand Down Expand Up @@ -213,6 +211,7 @@ internal constructor(
BidiGenerateContentToolResponseSetup(functionList.map { it.toInternalFunctionCall() })
.toInternal()
)
println(jsonString)
session.send(Frame.Text(jsonString))
}
}
Expand Down
41 changes: 41 additions & 0 deletions firebase-ai/src/main/kotlin/com/google/firebase/ai/type/Part.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,35 @@ public class TextPart(public val text: String) : Part {
@Serializable internal data class Internal(val text: String) : InternalPart
}

public class CodeExecutionResultPart(public val outcome: String, public val output: String) : Part {

@Serializable
internal data class Internal(
@SerialName("codeExecutionResult") val codeExecutionResult: CodeExecutionResult
) : InternalPart {

@Serializable
internal data class CodeExecutionResult(
@SerialName("outcome") val outcome: String,
val output: String
)
}
}

public class ExecutableCodePart(public val language: String, public val code: String) : Part {

@Serializable
internal data class Internal(@SerialName("executableCode") val executableCode: ExecutableCode) :
InternalPart {

@Serializable
internal data class ExecutableCode(
@SerialName("language") val language: String,
val code: String
)
}
}

/**
* Represents image data sent to and received from requests. The image is converted client-side to
* JPEG encoding at 80% quality before being sent to the server.
Expand Down Expand Up @@ -176,6 +205,8 @@ internal object PartSerializer :
val jsonObject = element.jsonObject
return when {
"text" in jsonObject -> TextPart.Internal.serializer()
"executableCode" in jsonObject -> ExecutableCodePart.Internal.serializer()
"codeExecutionResult" in jsonObject -> CodeExecutionResultPart.Internal.serializer()
"functionCall" in jsonObject -> FunctionCallPart.Internal.serializer()
"functionResponse" in jsonObject -> FunctionResponsePart.Internal.serializer()
"inlineData" in jsonObject -> InlineDataPart.Internal.serializer()
Expand Down Expand Up @@ -207,6 +238,12 @@ internal fun Part.toInternal(): InternalPart {
)
is FileDataPart ->
FileDataPart.Internal(FileDataPart.Internal.FileData(mimeType = mimeType, fileUri = uri))
is ExecutableCodePart ->
ExecutableCodePart.Internal(ExecutableCodePart.Internal.ExecutableCode(language, code))
is CodeExecutionResultPart ->
CodeExecutionResultPart.Internal(
CodeExecutionResultPart.Internal.CodeExecutionResult(outcome, output)
)
else ->
throw com.google.firebase.ai.type.SerializationException(
"The given subclass of Part (${javaClass.simpleName}) is not supported in the serialization yet."
Expand Down Expand Up @@ -241,6 +278,10 @@ internal fun InternalPart.toPublic(): Part {
is FunctionResponsePart.Internal ->
FunctionResponsePart(functionResponse.name, functionResponse.response, functionResponse.id)
is FileDataPart.Internal -> FileDataPart(fileData.mimeType, fileData.fileUri)
is ExecutableCodePart.Internal ->
ExecutableCodePart(executableCode.language, executableCode.code)
is CodeExecutionResultPart.Internal ->
CodeExecutionResultPart(codeExecutionResult.outcome, codeExecutionResult.output)
else ->
throw com.google.firebase.ai.type.SerializationException(
"Unsupported part type \"${javaClass.simpleName}\" provided. This model may not be supported by this SDK."
Expand Down
Loading