Skip to content

Commit

Permalink
Websocket client tool results (#10)
Browse files Browse the repository at this point in the history
This fixes large tool results sometimes failing to reach our server
  • Loading branch information
mdepinet authored Dec 14, 2024
1 parent 7ad1f5d commit 1264e25
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion demoapp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {

defaultConfig {
applicationId = "ai.ultravox.demoapp"
minSdk = 24
minSdk = 26
targetSdk = 34
versionCode = 1
versionName = "1.0"
Expand Down
21 changes: 21 additions & 0 deletions demoapp/src/main/java/ai/ultravox/demoapp/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ai.ultravox.demoapp

import ai.ultravox.ClientToolResult
import ai.ultravox.Transcript
import ai.ultravox.UltravoxSession
import android.Manifest.permission.RECORD_AUDIO
Expand All @@ -18,6 +19,8 @@ import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import org.json.JSONObject
import java.time.LocalDate

@SuppressLint("SetTextI18n")
class MainActivity : AppCompatActivity() {
Expand Down Expand Up @@ -111,13 +114,31 @@ class MainActivity : AppCompatActivity() {
}
}
}
session.registerToolImplementation("getSecretMenu") { args: JSONObject ->
getSecretMenu(args)
}
session.joinCall(joinText.text.toString())
joinButton.text = "Leave"
joinButton.setOnClickListener { onLeaveClicked() }
muteMicButton.visibility = View.VISIBLE
muteSpeakerButton.visibility = View.VISIBLE
}

@Suppress("UNUSED_PARAMETER")
private fun getSecretMenu(args: JSONObject): ClientToolResult {
val result = JSONObject()
result.put("date", LocalDate.now().toString())
val items = ArrayList<JSONObject>()
items.add(JSONObject())
items[0].put("name", "Banana smoothie")
items[0].put("price", 3.99)
items.add(JSONObject())
items[1].put("name", "Butter pecan ice cream (one scoop)")
items[1].put("price", 1.99)
result.put("specialItems", items)
return ClientToolResult(result.toString(), null)
}

override fun onDestroy() {
session.leaveCall()
super.onDestroy()
Expand Down
2 changes: 1 addition & 1 deletion ultravox_client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ publishing {
register<MavenPublication>("release") {
groupId = "ai.fixie"
artifactId = "ultravox-client-sdk"
version = "0.1.5"
version = "0.1.6"

pom {
name = "Ultravox Client"
Expand Down
16 changes: 11 additions & 5 deletions ultravox_client/src/main/java/ai/ultravox/UltravoxSession.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class UltravoxSession(
private val experimentalMessages: Set<String> = HashSet(),
) {
companion object {
const val ULTRAVOX_SDK_VERSION = "0.1.5"
const val ULTRAVOX_SDK_VERSION = "0.1.6"
}

private var socket: WebSocket? = null
Expand Down Expand Up @@ -140,8 +140,8 @@ class UltravoxSession(
registeredSyncTools[name] = impl
}

/** Override of [registerToolImplementation] for suspendable tool implementations. */
fun registerToolImplementation(name: String, impl: AsyncClientToolImplementation) {
/** Same as [registerToolImplementation], but for suspendable tool implementations. */
fun registerAsyncToolImplementation(name: String, impl: AsyncClientToolImplementation) {
registeredAsyncTools[name] = impl
}

Expand Down Expand Up @@ -263,8 +263,14 @@ class UltravoxSession(
if (!message.has("type")) {
throw RuntimeException("Cannot send a data message without a type.")
}
coroScope.launch {
room.localParticipant.publishData(message.toString().encodeToByteArray())
val messageStr = message.toString()
val messageBytes = messageStr.encodeToByteArray()
if (messageBytes.size > 1024) {
socket?.send(messageStr)
} else {
coroScope.launch {
room.localParticipant.publishData(messageBytes)
}
}
}

Expand Down

0 comments on commit 1264e25

Please sign in to comment.