Skip to content

Commit

Permalink
Adjust so there's a role in the Message data class
Browse files Browse the repository at this point in the history
  • Loading branch information
kateliu20 committed Sep 17, 2024
1 parent 450317e commit 9cbda30
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package slack.tooling.aibot

import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import java.io.BufferedReader
import java.io.File
Expand All @@ -38,11 +39,37 @@ class ChatBotActionService {
@VisibleForTesting
private fun createJsonInput(question: String): String {
val gsonInput = Gson()
val content =
Content(messages = listOf(Message(question, isMe = true)), source = "curl", max_tokens = 2048)
val jsonObjectInput =
Content(
messages = listOf(Message(role = "user", question)),
source = "curl",
max_tokens = 2048,
)

val jsonObjectInput2 =
JsonObject().apply {
add(
"messages",
JsonArray().apply {
add(
JsonObject().apply {
addProperty("role", "user")
addProperty("content", question)
}
)
},
)
addProperty("source", "curl")
addProperty("max_tokens", 2048)
}

val content = gsonInput.toJson(jsonObjectInput)
val content2 = gsonInput.toJson(jsonObjectInput2)

println("jsonContent ${content}")
println("jsonContent2 ${content2}")

val jsonContent = gsonInput.toJson(content)
return jsonContent
return content
}

@VisibleForTesting
Expand Down Expand Up @@ -94,6 +121,7 @@ class ChatBotActionService {

@VisibleForTesting
private fun parseOutput(output: String): String {
println("output: ${output}")
val regex = """\{.*\}""".toRegex(RegexOption.DOT_MATCHES_ALL)
val result = regex.find(output.toString())?.value ?: "{}"
val gson = Gson()
Expand All @@ -102,6 +130,8 @@ class ChatBotActionService {
val contentObject = contentArray.get(0).asJsonObject
val actualContent = contentObject.get("content").asString

println("actual content ${actualContent}")

return actualContent
}

Expand All @@ -110,74 +140,4 @@ class ChatBotActionService {
val source: String = "curl",
val max_tokens: Int = 512,
)

// original suspend function command before splitting up:
// suspend fun executeCommand(question: String): String {
// val gsonInput = Gson()
// val jsonObjectInput =
// JsonObject().apply {
// add(
// "messages",
// JsonArray().apply {
// add(
// JsonObject().apply {
// addProperty("role", "user")
// addProperty("content", question)
// }
// )
// },
// )
// addProperty("source", "curl")
// addProperty("max_tokens", 2048)
// }
//
// val content = gsonInput.toJson(jsonObjectInput)
//
// val scriptContent =
// """
// #!/bin/bash
// export PATH="/usr/local/bin:/usr/bin:${'$'}PATH"
// export SSH_OPTIONS="-T"
//
// /usr/local/bin/slack-uberproxy-curl -X POST https://devxp-ai-api.tinyspeck.com/v1/chat/
// -H "Content-Type: application/json" -d '$content'
// """
// .trimIndent()
//
// val tempScript = withContext(Dispatchers.IO) { File.createTempFile("run_command", ".sh")
// }
// tempScript.writeText(scriptContent)
// tempScript.setExecutable(true)
//
// val processBuilder = ProcessBuilder("/bin/bash", tempScript.absolutePath)
// processBuilder.redirectErrorStream(true)
//
// val process = processBuilder.start()
// val output = StringBuilder()
//
// BufferedReader(InputStreamReader(process.inputStream)).use { reader ->
// var line: String?
// while (reader.readLine().also { line = it } != null) {
// output.append(line).append("\n")
// }
// }
//
// val completed = process.waitFor(600, TimeUnit.SECONDS)
// if (!completed) {
// process.destroyForcibly()
// throw RuntimeException("Process timed out after 600 seconds")
// }
//
// tempScript.delete()
//
// val regex = """\{.*\}""".toRegex(RegexOption.DOT_MATCHES_ALL)
// val result = regex.find(output.toString())?.value ?: "{}"
// val gson = Gson()
// val jsonObject = gson.fromJson(result, JsonObject::class.java)
// val contentArray = jsonObject.getAsJsonArray("content")
// val contentObject = contentArray.get(0).asJsonObject
// val actualContent = contentObject.get("content").asString
//
// return actualContent
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class ChatPresenter : Presenter<ChatScreen.State> {
val user = "user"
val bot = "bot"
private val chatBotActionService = ChatBotActionService()

@Composable
Expand All @@ -35,15 +37,15 @@ class ChatPresenter : Presenter<ChatScreen.State> {
return ChatScreen.State(messages = messages) { event ->
when (event) {
is ChatScreen.Event.SendMessage -> {
val newMessage = Message(event.message, isMe = true)
val newMessage = Message(role = user, event.message)
messages = messages + newMessage

CoroutineScope(Dispatchers.IO).launch {
println("${newMessage}")
println("ChatPresenter: Fetching a quote")
val response = chatBotActionService.executeCommand(event.message)
println("ChatPresenter: Received response: $newMessage")
messages = messages + Message(response, isMe = false)
messages = messages + Message(role = user, response)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ fun ChatWindowUi(state: ChatScreen.State, modifier: Modifier = Modifier) {
Column(modifier = modifier.fillMaxSize().background(JewelTheme.globalColors.paneBackground)) {
LazyColumn(modifier = Modifier.weight(1f), reverseLayout = true) {
items(state.messages.reversed()) { message ->
val isMe = message.role == "user"
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = if (message.isMe) Arrangement.End else Arrangement.Start,
horizontalArrangement = if (isMe) Arrangement.End else Arrangement.Start,
) {
ChatBubble(message)
}
Expand Down Expand Up @@ -148,18 +149,19 @@ private fun ConversationField(modifier: Modifier = Modifier, onSendMessage: (Str

@Composable
private fun ChatBubble(message: Message, modifier: Modifier = Modifier) {
val user = "user"
val isMe = message.role == user

Box(
Modifier.wrapContentWidth()
.padding(8.dp)
.shadow(elevation = 0.5.dp, shape = RoundedCornerShape(25.dp), clip = true)
.background(
color = if (message.isMe) ChatColors.promptBackground else ChatColors.responseBackground
)
.background(color = if (isMe) ChatColors.promptBackground else ChatColors.responseBackground)
.padding(8.dp)
) {
Text(
text = message.text,
color = if (message.isMe) ChatColors.userTextColor else ChatColors.responseTextColor,
text = message.content,
color = if (isMe) ChatColors.userTextColor else ChatColors.responseTextColor,
modifier = modifier.padding(8.dp),
fontFamily = FontFamily.SansSerif,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ package slack.tooling.aibot

import androidx.compose.runtime.Immutable

@Immutable data class Message(val text: String, val isMe: Boolean)
@Immutable data class Message(var role: String, val content: String)
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ class ChatBotActionServiceTest {
}

private fun createJsonInput(question: String): String {
val user = "user"
val gsonInput = Gson()
val content =
Content(messages = listOf(Message(question, isMe = true)), source = "curl", max_tokens = 2048)
Content(messages = listOf(Message(role = user, question)), source = "curl", max_tokens = 2048)

val jsonContent = gsonInput.toJson(content)
return jsonContent
Expand Down

0 comments on commit 9cbda30

Please sign in to comment.