openassistant: fix title setting for conversations
- Commit
5e2a8ab4a7173e74a391c25a7d80e0c35f119542- Author
- Vayun Mathur <[email protected]>
- When
- 2026-04-12 00:20 UTC (7 months ago)
- Parents
7fc10f3
Diff
openassistant/src/main/java/com/vayunmathur/openassistant/AssistantToolSet.kt
diff --git a/openassistant/src/main/java/com/vayunmathur/openassistant/AssistantToolSet.kt b/openassistant/src/main/java/com/vayunmathur/openassistant/AssistantToolSet.ktindex 47e286326c..be481ec167 100644--- a/openassistant/src/main/java/com/vayunmathur/openassistant/AssistantToolSet.kt+++ b/openassistant/src/main/java/com/vayunmathur/openassistant/AssistantToolSet.kt@@ -63,6 +63,12 @@ class AssistantToolSet(private val context: Context) : ToolSet { } catch (e: Exception) { "Error: ${e.message}" } } + @Tool("Set title of current conversation. Mandatory for first response")+ fun set_conversation_title(newTitle: String): String {+ InferenceService.newTitle = newTitle+ return "Conversation title set successfully"+ }+ @Tool(description = "Get weather") fun get_weather(latitude: Double, longitude: Double): String = "Weather: 22°C, Sunny." }\ No newline at end of file
openassistant/src/main/java/com/vayunmathur/openassistant/InferenceService.kt
diff --git a/openassistant/src/main/java/com/vayunmathur/openassistant/InferenceService.kt b/openassistant/src/main/java/com/vayunmathur/openassistant/InferenceService.ktindex b09f361dc5..42cda93924 100644--- a/openassistant/src/main/java/com/vayunmathur/openassistant/InferenceService.kt+++ b/openassistant/src/main/java/com/vayunmathur/openassistant/InferenceService.kt@@ -14,6 +14,10 @@ import kotlin.time.Clock class InferenceService : Service() { + companion object {+ var newTitle: String? = null+ }+ private val serviceScope = CoroutineScope(SupervisorJob() + Dispatchers.IO) // Keeping the engine and conversation warm in memory@@ -100,7 +104,10 @@ class InferenceService : Service() { } private fun setupConversation(id: Long, history: List<Message>) {- val systemPrompt = """You are a helpful Android assistant.""".trimIndent()+ val systemPrompt = """+ You are a helpful Android assistant.+ On the first request the user sends to you, you MUST define a title for the conversation. You may optionally change the title if the topic of conversation changes sufficiently+ """.trimIndent() val initialMessages = history.map { msg -> when (msg.role) {@@ -137,10 +144,6 @@ class InferenceService : Service() { var fullResponseText = "" var displayedText = ""- var tagBuffer = ""- var insideTag = false- var insideTitleBlock = false- var titleBuffer = "" // On the first loop iteration, build multimodal content val stream = run {@@ -171,31 +174,11 @@ class InferenceService : Service() { }.collect { chunk -> val chunkText = chunk.contents.contents.filterIsInstance<Content.Text>().joinToString("") { it.text } fullResponseText += chunkText+ displayedText += chunkText - for (char in chunkText) {- if (char == '<') {- insideTag = true- tagBuffer = "<"- } else if (insideTag) {- tagBuffer += char- if (char == '>') {- insideTag = false- when {- tagBuffer.contains("title_start") -> insideTitleBlock = true- tagBuffer.contains("title_end") -> {- insideTitleBlock = false- updateTitleInDb(conversationId, titleBuffer.trim())- }- }- tagBuffer = ""- }- } else {- if (insideTitleBlock) {- titleBuffer += char- } else {- displayedText += char- }- }+ if(newTitle != null) {+ updateTitleInDb(conversationId, newTitle!!)+ newTitle = null } if (displayedText.isNotBlank()) {