Skip to content

Commit 3e17103

Browse files
Fix Gemini Live delete tool calling (#129)
1 parent 88a7c49 commit 3e17103

File tree

3 files changed

+52
-25
lines changed

3 files changed

+52
-25
lines changed

samples/gemini-live-todo/src/main/java/com/android/ai/samples/geminilivetodo/data/Todo.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
*/
1616
package com.android.ai.samples.geminilivetodo.data
1717

18-
import java.util.UUID.randomUUID
18+
import kotlin.random.Random
19+
1920

2021
data class Todo(
21-
val id: Long = randomUUID().mostSignificantBits,
22+
val id: Int = Random.nextInt(),
2223
val task: String,
2324
val isCompleted: Boolean = false,
2425
)

samples/gemini-live-todo/src/main/java/com/android/ai/samples/geminilivetodo/data/TodoRepository.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,24 @@ class TodoRepository @Inject constructor() {
4141

4242
fun getTodoList(): List<Todo> = _todos.value
4343

44-
fun addTodo(taskDescription: String) {
44+
fun addTodo(taskDescription: String) : Int? {
4545
if (taskDescription.isNotBlank()) {
4646
val newTodo = Todo(task = taskDescription)
4747
_todos.update { currentList ->
4848
currentList + newTodo
4949
}
50+
return newTodo.id
5051
}
52+
return null
5153
}
5254

53-
fun removeTodo(todoId: Long) {
55+
fun removeTodo(todoId: Int) {
5456
_todos.update { currentList ->
5557
currentList.filterNot { it.id == todoId }
5658
}
5759
}
5860

59-
fun toggleTodoStatus(todoId: Long) {
61+
fun toggleTodoStatus(todoId: Int) {
6062
_todos.update { currentList ->
6163
currentList.map { todo ->
6264
if (todo.id == todoId) {

samples/gemini-live-todo/src/main/java/com/android/ai/samples/geminilivetodo/ui/TodoScreenViewModel.kt

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import kotlinx.coroutines.flow.stateIn
5050
import kotlinx.coroutines.launch
5151
import kotlinx.serialization.json.JsonObject
5252
import kotlinx.serialization.json.JsonPrimitive
53+
import kotlinx.serialization.json.int
5354
import kotlinx.serialization.json.jsonPrimitive
5455
import kotlinx.serialization.json.long
5556

@@ -74,11 +75,11 @@ class TodoScreenViewModel @Inject constructor(private val todoRepository: TodoRe
7475
todoRepository.addTodo(taskDescription)
7576
}
7677

77-
fun removeTodo(todoId: Long) {
78+
fun removeTodo(todoId: Int) {
7879
todoRepository.removeTodo(todoId)
7980
}
8081

81-
fun toggleTodoStatus(todoId: Long) {
82+
fun toggleTodoStatus(todoId: Int) {
8283
todoRepository.toggleTodoStatus(todoId)
8384
}
8485

@@ -194,28 +195,51 @@ class TodoScreenViewModel @Inject constructor(private val todoRepository: TodoRe
194195
}
195196
"addTodo" -> {
196197
val taskDescription = functionCall.args["taskDescription"]!!.jsonPrimitive.content
197-
todoRepository.addTodo(taskDescription)
198-
val response = JsonObject(
199-
mapOf(
200-
"success" to JsonPrimitive(true),
201-
"message" to JsonPrimitive("Task $taskDescription added to the todo list"),
202-
),
203-
)
204-
FunctionResponsePart(functionCall.name, response)
198+
val id = todoRepository.addTodo(taskDescription)
199+
200+
if (id!=null) {
201+
val response = JsonObject(
202+
mapOf(
203+
"success" to JsonPrimitive(true),
204+
"message" to JsonPrimitive("Task $taskDescription added to the todo list (id: $id)"),
205+
),
206+
)
207+
FunctionResponsePart(functionCall.name, response)
208+
} else {
209+
val response = JsonObject(
210+
mapOf(
211+
"success" to JsonPrimitive(false),
212+
"message" to JsonPrimitive("Task $taskDescription wasn't properly added to the list"),
213+
),
214+
)
215+
FunctionResponsePart(functionCall.name, response)
216+
}
217+
205218
}
206219
"removeTodo" -> {
207-
val taskId = functionCall.args["todoId"]!!.jsonPrimitive.long
208-
todoRepository.removeTodo(taskId)
209-
val response = JsonObject(
210-
mapOf(
211-
"success" to JsonPrimitive(true),
212-
"message" to JsonPrimitive("Task was removed from the todo list"),
213-
),
214-
)
215-
FunctionResponsePart(functionCall.name, response)
220+
try {
221+
val taskId = functionCall.args["todoId"]!!.jsonPrimitive.int
222+
todoRepository.removeTodo(taskId)
223+
val response = JsonObject(
224+
mapOf(
225+
"success" to JsonPrimitive(true),
226+
"message" to JsonPrimitive("Task was removed from the todo list"),
227+
),
228+
)
229+
FunctionResponsePart(functionCall.name, response)
230+
} catch (e: Exception) {
231+
val response = JsonObject(
232+
mapOf(
233+
"success" to JsonPrimitive(false),
234+
"message" to JsonPrimitive("Something went wrong: ${e.message}"),
235+
),
236+
)
237+
FunctionResponsePart(functionCall.name, response)
238+
}
239+
216240
}
217241
"toggleTodoStatus" -> {
218-
val taskId = functionCall.args["todoId"]!!.jsonPrimitive.long
242+
val taskId = functionCall.args["todoId"]!!.jsonPrimitive.int
219243
todoRepository.toggleTodoStatus(taskId)
220244
val response = JsonObject(
221245
mapOf(

0 commit comments

Comments
 (0)