Skip to content
Open
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,15 @@ class TodoScreenViewModel @Inject constructor(private val todoRepository: TodoRe
Manifest.permission.RECORD_AUDIO,
) == PackageManager.PERMISSION_GRANTED
) {
Log.d(TAG, "Start audio conversation")

it.startAudioConversation(::handleFunctionCall)
liveSessionState.value = LiveSessionState.Running
}
} else {

Log.d(TAG, "Stop audio conversation")

it.stopAudioConversation()
liveSessionState.value = LiveSessionState.Ready
}
Expand Down Expand Up @@ -158,8 +163,8 @@ class TodoScreenViewModel @Inject constructor(private val todoRepository: TodoRe
emptyMap(),
)

val generativeModel = Firebase.ai(backend = GenerativeBackend.vertexAI()).liveModel(
"gemini-2.0-flash-live-preview-04-09",
val generativeModel = Firebase.ai(backend = GenerativeBackend.googleAI()).liveModel(
"gemini-live-2.5-flash-preview",
generationConfig = liveGenerationConfig,
systemInstruction = systemInstruction,
tools = listOf(
Expand All @@ -169,6 +174,8 @@ class TodoScreenViewModel @Inject constructor(private val todoRepository: TodoRe
),
)



try {
session = generativeModel.connect()
} catch (e: Exception) {
Expand All @@ -181,6 +188,8 @@ class TodoScreenViewModel @Inject constructor(private val todoRepository: TodoRe
}

private fun handleFunctionCall(functionCall: FunctionCallPart): FunctionResponsePart {
Log.d(TAG, "handleFunctionCall ${functionCall.name}")

return when (functionCall.name) {
"getTodoList" -> {
val todoList = todoRepository.getTodoList().reversed()
Expand All @@ -190,7 +199,7 @@ class TodoScreenViewModel @Inject constructor(private val todoRepository: TodoRe
"message" to JsonPrimitive("List of tasks in the todo list: $todoList"),
),
)
FunctionResponsePart(functionCall.name, response)
FunctionResponsePart(functionCall.name, response, functionCall.id)
}
"addTodo" -> {
val taskDescription = functionCall.args["taskDescription"]!!.jsonPrimitive.content
Expand All @@ -201,7 +210,7 @@ class TodoScreenViewModel @Inject constructor(private val todoRepository: TodoRe
"message" to JsonPrimitive("Task $taskDescription added to the todo list"),
),
)
FunctionResponsePart(functionCall.name, response)
FunctionResponsePart(functionCall.name, response, functionCall.id)
}
"removeTodo" -> {
val taskId = functionCall.args["todoId"]!!.jsonPrimitive.long
Expand All @@ -212,7 +221,7 @@ class TodoScreenViewModel @Inject constructor(private val todoRepository: TodoRe
"message" to JsonPrimitive("Task was removed from the todo list"),
),
)
FunctionResponsePart(functionCall.name, response)
FunctionResponsePart(functionCall.name, response, functionCall.id)
}
"toggleTodoStatus" -> {
val taskId = functionCall.args["todoId"]!!.jsonPrimitive.long
Expand All @@ -223,13 +232,13 @@ class TodoScreenViewModel @Inject constructor(private val todoRepository: TodoRe
"message" to JsonPrimitive("Task was toggled in the todo list"),
),
)
FunctionResponsePart(functionCall.name, response)
FunctionResponsePart(functionCall.name, response, functionCall.id)
}
else -> {
val response = JsonObject(
mapOf("error" to JsonPrimitive("Unknown function: ${functionCall.name}")),
)
FunctionResponsePart(functionCall.name, response)
FunctionResponsePart(functionCall.name, response, functionCall.id)
}
}
}
Expand Down
Loading