Skip to content

Commit

Permalink
update examples to use new feature
Browse files Browse the repository at this point in the history
  • Loading branch information
jhump committed Feb 6, 2024
1 parent 4cb9d14 commit 7e6334e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ class ElizaChatActivity : AppCompatActivity() {
host = host,
serializationStrategy = GoogleJavaLiteProtobufStrategy(),
networkProtocol = selectedNetworkProtocolOption,
// RPC operations that involve network I/O will
// use this coroutine context.
ioCoroutineContext = Dispatchers.IO,
),
)
// Create the Eliza service client.
Expand Down Expand Up @@ -113,7 +116,7 @@ class ElizaChatActivity : AppCompatActivity() {
adapter.add(MessageData(sentence, false))
editTextView.setText("")
// Ensure IO context for unary requests.
lifecycleScope.launch(Dispatchers.IO) {
lifecycleScope.launch {
// Make a unary request to Eliza.
val response = elizaServiceClient.say(SayRequest.newBuilder().setSentence(sentence).build())
response.success { success ->
Expand All @@ -133,7 +136,7 @@ class ElizaChatActivity : AppCompatActivity() {

private fun setupStreamingChat(elizaServiceClient: ElizaServiceClient) {
// On stream result, this callback can be called multiple times.
lifecycleScope.launch(Dispatchers.IO) {
lifecycleScope.launch {
// Initialize a bidi stream with Eliza.
val stream = elizaServiceClient.converse()
try {
Expand All @@ -156,15 +159,13 @@ class ElizaChatActivity : AppCompatActivity() {
} catch (e: ConnectException) {
adapter.add(MessageData("Session failed with code ${e.code}", true))
}
lifecycleScope.launch(Dispatchers.Main) {
buttonView.setOnClickListener {
val sentence = editTextView.text.toString()
adapter.add(MessageData(sentence, false))
editTextView.setText("")
// Send will be streaming a message to Eliza.
lifecycleScope.launch(Dispatchers.IO) {
stream.send(ConverseRequest.newBuilder().setSentence(sentence).build())
}
buttonView.setOnClickListener {
val sentence = editTextView.text.toString()
adapter.add(MessageData(sentence, false))
editTextView.setText("")
// Send will be streaming a message to Eliza.
lifecycleScope.launch {
stream.send(ConverseRequest.newBuilder().setSentence(sentence).build())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import com.connectrpc.impl.ProtocolClient
import com.connectrpc.okhttp.ConnectOkHttpClient
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import java.time.Duration

Expand All @@ -44,6 +43,9 @@ class Main {
ProtocolClientConfig(
host = host,
serializationStrategy = GoogleJavaProtobufStrategy(),
// RPC operations that involve network I/O will
// use this coroutine context.
ioCoroutineContext = Dispatchers.IO,
),
)
val elizaServiceClient = ElizaServiceClient(client)
Expand All @@ -57,13 +59,11 @@ class Main {

private suspend fun connectStreaming(elizaServiceClient: ElizaServiceClient) {
val stream = elizaServiceClient.converse()
withContext(Dispatchers.IO) {
// Add the message the user is sending to the views.
stream.send(converseRequest { sentence = "hello" })
stream.sendClose()
for (response in stream.responseChannel()) {
println(response.sentence)
}
// Add the message the user is sending to the views.
stream.send(converseRequest { sentence = "hello" })
stream.sendClose()
for (response in stream.responseChannel()) {
println(response.sentence)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import com.connectrpc.impl.ProtocolClient
import com.connectrpc.okhttp.ConnectOkHttpClient
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import java.time.Duration

Expand All @@ -44,6 +43,9 @@ class Main {
ProtocolClientConfig(
host = host,
serializationStrategy = GoogleJavaLiteProtobufStrategy(),
// RPC operations that involve network I/O will
// use this coroutine context.
ioCoroutineContext = Dispatchers.IO,
),
)
val elizaServiceClient = ElizaServiceClient(client)
Expand All @@ -57,13 +59,11 @@ class Main {

private suspend fun connectStreaming(elizaServiceClient: ElizaServiceClient) {
val stream = elizaServiceClient.converse()
withContext(Dispatchers.IO) {
// Add the message the user is sending to the views.
stream.send(converseRequest { sentence = "hello" })
stream.sendClose()
for (response in stream.responseChannel()) {
println(response.sentence)
}
// Add the message the user is sending to the views.
stream.send(converseRequest { sentence = "hello" })
stream.sendClose()
for (response in stream.responseChannel()) {
println(response.sentence)
}
}
}
Expand Down

0 comments on commit 7e6334e

Please sign in to comment.