From fe5c5c0d588b1ce810c3089275947ccda07c8d40 Mon Sep 17 00:00:00 2001 From: marcus-daily <111281783+marcus-daily@users.noreply.github.com> Date: Thu, 10 Oct 2024 13:18:02 +0100 Subject: [PATCH] Send version number with requests --- .../main/java/ai/rtvi/client/RTVIClient.kt | 37 ++++++++++++++++--- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/rtvi-client-android/src/main/java/ai/rtvi/client/RTVIClient.kt b/rtvi-client-android/src/main/java/ai/rtvi/client/RTVIClient.kt index a0b87c6..638a829 100644 --- a/rtvi-client-android/src/main/java/ai/rtvi/client/RTVIClient.kt +++ b/rtvi-client-android/src/main/java/ai/rtvi/client/RTVIClient.kt @@ -40,6 +40,8 @@ import kotlinx.serialization.json.jsonObject import okhttp3.MediaType.Companion.toMediaType import okhttp3.RequestBody.Companion.toRequestBody +private const val RTVI_PROTOCOL_VERSION = "0.2.0" + /** * An RTVI client. Connects to an RTVI backend and handles bidirectional audio and video * streaming. @@ -86,6 +88,10 @@ open class RTVIClient( ) { val id = msg.id ?: throw Exception("${msg.type} missing ID") + if (id == "END") { + return + } + val respondTo = awaitingServerResponse.remove(id) ?: throw Exception("${msg.type}: no responder for $id") @@ -259,11 +265,13 @@ open class RTVIClient( // Send POST request to the provided base_url to connect and start the bot + val connectionData = ConnectionData.from(options) + val body = ConnectionBundle( services = options.services?.associate { it.service to it.value }, - config = options.config + options.params.config + config = connectionData.config ) - .serializeWithCustomParams(options.customBodyParams + options.params.requestData) + .serializeWithCustomParams(connectionData.requestData) .toRequestBody("application/json".toMediaType()) val currentConnection = Connection().apply { connection = this } @@ -272,7 +280,7 @@ open class RTVIClient( thread = thread, url = options.params.baseUrl + options.params.endpoints.connect, body = body, - customHeaders = options.customHeaders + options.params.headers + customHeaders = connectionData.headers ) .mapError { RTVIError.HttpError(it) @@ -385,19 +393,22 @@ open class RTVIClient( } else -> if (allowSingleTurn) { + + val connectionData = ConnectionData.from(options) + post( thread = thread, url = options.params.baseUrl + options.params.endpoints.action, body = JSON_INSTANCE.encodeToString( Value.serializer(), Value.Object( - (options.customBodyParams + options.params.requestData + listOf( + (connectionData.requestData + listOf( "actions" to Value.Array( valueFrom(MsgClientToServer.serializer(), msg) ) )).toMap() ) ).toRequestBody("application/json".toMediaType()), - customHeaders = options.customHeaders + options.params.headers, + customHeaders = connectionData.headers, responseHandler = { inputStream -> inputStream.parseServerSentEvents { msg -> transportCtx.onMessage(JSON_INSTANCE.decodeFromString(msg)) @@ -571,4 +582,20 @@ open class RTVIClient( ) } } +} + +private class ConnectionData( + val headers: List>, + val requestData: List>, + val config: List, +) { + companion object { + fun from(value: RTVIClientOptions) = ConnectionData( + headers = value.customHeaders + value.params.headers, + requestData = listOf("rtvi_client_version" to Value.Str(RTVI_PROTOCOL_VERSION)) + + value.customBodyParams + + value.params.requestData, + config = value.config + value.params.config + ) + } } \ No newline at end of file