Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom body params #2

Merged
merged 4 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ packages.) The base `VoiceClient` has no transport included.
Add the following dependency to your `build.gradle` file:

```
implementation "ai.rtvi:client:0.1.1"
implementation "ai.rtvi:client:0.1.2"
```

Then instantiate the `VoiceClient` from your code, specifying the backend `baseUrl` and transport.
Expand Down
8 changes: 5 additions & 3 deletions rtvi-client-android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ publishing {
register<MavenPublication>("release") {
groupId = "ai.rtvi"
artifactId = "client"
version = "0.1.1"
version = "0.1.2"

pom {
name.set("RTVI Client")
Expand Down Expand Up @@ -99,6 +99,8 @@ signing {
val signingKey = System.getenv("RTVI_GPG_SIGNING_KEY")
val signingPassphrase = System.getenv("RTVI_GPG_SIGNING_PASSPHRASE")

useInMemoryPgpKeys(signingKey, signingPassphrase)
sign(publishing.publications)
if (!signingKey.isNullOrEmpty() || !signingPassphrase.isNullOrEmpty()) {
useInMemoryPgpKeys(signingKey, signingPassphrase)
sign(publishing.publications)
}
}
20 changes: 13 additions & 7 deletions rtvi-client-android/src/main/java/ai/rtvi/client/VoiceClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,12 @@ open class VoiceClient(

// Send POST request to the provided base_url to connect and start the bot

val body = JSON_INSTANCE.encodeToString(
ConnectionBundle.serializer(),
ConnectionBundle(
services = options.services.associate { it.service to it.value },
config = options.config
)
).toRequestBody("application/json".toMediaType())
val body = ConnectionBundle(
services = options.services.associate { it.service to it.value },
config = options.config
)
.serializeWithCustomParams(options.customBodyParams)
.toRequestBody("application/json".toMediaType())

val currentConnection = Connection().apply { connection = this }

Expand Down Expand Up @@ -466,6 +465,13 @@ open class VoiceClient(
val isCamEnabled
get() = transport.isCamEnabled()

/**
* The expiry time for the transport session, if applicable. Measured in seconds
* since the UNIX epoch (UTC).
*/
val expiry
get() = transport.expiry()

/**
* Returns a list of participant media tracks.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ai.rtvi.client

import ai.rtvi.client.types.ServiceConfig
import ai.rtvi.client.types.ServiceRegistration
import ai.rtvi.client.types.Value

/**
* Configuration options when instantiating a [VoiceClient].
Expand Down Expand Up @@ -34,5 +35,10 @@ data class VoiceClientOptions(
/**
* Custom HTTP headers to be sent with the POST request to baseUrl.
*/
val customHeaders: List<Pair<String, String>> = emptyList()
val customHeaders: List<Pair<String, String>> = emptyList(),

/**
* Custom parameters to add to the auth request body.
*/
val customBodyParams: List<Pair<String, Value>> = emptyList()
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ai.rtvi.client.types.MediaDeviceInfo
import ai.rtvi.client.types.Participant
import ai.rtvi.client.types.PipecatMetrics
import ai.rtvi.client.types.ServiceConfig
import ai.rtvi.client.types.Tracks
import ai.rtvi.client.types.Transcript
import ai.rtvi.client.types.TransportState

Expand Down Expand Up @@ -127,6 +128,11 @@ abstract class VoiceEventCallbacks {
* Invoked when the state of the input devices changes.
*/
open fun onInputsUpdated(camera: Boolean, mic: Boolean) {}

/**
* Invoked when the set of available cam/mic tracks changes.
*/
open fun onTracksUpdated(tracks: Tracks) {}
}

internal class CallbackInterceptor(vararg listeners: VoiceEventCallbacks): VoiceEventCallbacks() {
Expand Down Expand Up @@ -224,4 +230,8 @@ internal class CallbackInterceptor(vararg listeners: VoiceEventCallbacks): Voice
override fun onInputsUpdated(camera: Boolean, mic: Boolean) {
callbacks.forEach { it.onInputsUpdated(camera = camera, mic = mic) }
}

override fun onTracksUpdated(tracks: Tracks) {
callbacks.forEach { it.onTracksUpdated(tracks) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ abstract class Transport {
abstract fun isCamEnabled(): Boolean
abstract fun isMicEnabled(): Boolean

/**
* The expiry time for the transport session, if applicable. Measured in seconds
* since the UNIX epoch (UTC).
*/
abstract fun expiry(): Long?

abstract fun sendMessage(message: MsgClientToServer): Future<Unit, VoiceError>

abstract fun state(): TransportState
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
package ai.rtvi.client.utils

import ai.rtvi.client.types.ServiceConfig
import ai.rtvi.client.types.Value
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.encodeToJsonElement
import kotlinx.serialization.json.jsonObject

@Serializable
internal data class ConnectionBundle(
val services: Map<String, String>,
val config: List<ServiceConfig>
)
) {

fun serializeWithCustomParams(customBodyParams: List<Pair<String, Value>>): String {

val customMap =
customBodyParams.associate { it.first to JSON_INSTANCE.encodeToJsonElement(it.second) }

val result = JsonObject(JSON_INSTANCE.encodeToJsonElement(this).jsonObject + customMap)

return JSON_INSTANCE.encodeToString(JsonObject.serializer(), result)
}
}