AIPage.kt 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package eu.mjdev.desktop.components.controlcenter.pages
  2. import androidx.compose.foundation.background
  3. import androidx.compose.foundation.layout.*
  4. import androidx.compose.foundation.lazy.LazyColumn
  5. import androidx.compose.foundation.lazy.itemsIndexed
  6. import androidx.compose.foundation.shape.RoundedCornerShape
  7. import androidx.compose.material.OutlinedTextField
  8. import androidx.compose.material.Text
  9. import androidx.compose.material.TextFieldDefaults
  10. import androidx.compose.material.icons.Icons
  11. import androidx.compose.material.icons.filled.Campaign
  12. import androidx.compose.runtime.mutableStateListOf
  13. import androidx.compose.runtime.mutableStateOf
  14. import androidx.compose.ui.Modifier
  15. import androidx.compose.ui.graphics.Color
  16. import androidx.compose.ui.unit.dp
  17. import eu.mjdev.desktop.components.controlcenter.ControlCenterPage
  18. import kotlinx.coroutines.launch
  19. @Suppress("FunctionName")
  20. fun AIPage() = ControlCenterPage(
  21. icon = Icons.Filled.Campaign,
  22. name = "AI",
  23. condition = { ai.isAvailable() }
  24. ) {
  25. val questionsList = remember { mutableStateListOf<Pair<String, String>>() }
  26. val request = remember { mutableStateOf("") }
  27. val onDone: (String) -> Unit = { what ->
  28. if (what.isNotEmpty()) {
  29. questionsList.add(Pair(what, ""))
  30. api.ai.ask(what) { _, res ->
  31. res.replace("* **", "").replace("**", "").let { resx ->
  32. questionsList.removeIf { it.first == what }
  33. questionsList.add(Pair(what, resx))
  34. talk(resx)
  35. }
  36. }
  37. }
  38. }
  39. Box(
  40. modifier = Modifier.fillMaxSize()
  41. .background(backgroundColor)
  42. ) {
  43. Row {
  44. Column(
  45. modifier = Modifier.fillMaxSize()
  46. .padding(16.dp)
  47. ) {
  48. LazyColumn(
  49. modifier = Modifier.fillMaxWidth()
  50. .weight(1f)
  51. .background(Color.Black.copy(alpha = 0.3f), RoundedCornerShape(8.dp)),
  52. ) {
  53. itemsIndexed(questionsList) { idx, textData ->
  54. Column(
  55. modifier = Modifier.padding(4.dp)
  56. .fillMaxWidth()
  57. .background(Color.White.copy(alpha = 0.1f), RoundedCornerShape(8.dp))
  58. .padding(4.dp),
  59. ) {
  60. Text(
  61. modifier = Modifier.padding(4.dp)
  62. .fillMaxWidth()
  63. .background(Color.White.copy(alpha = 0.1f), RoundedCornerShape(8.dp))
  64. .padding(8.dp),
  65. text = "${idx + 1}. ${textData.first}",
  66. color = Color.White
  67. )
  68. Text(
  69. modifier = Modifier.padding(4.dp)
  70. .fillMaxWidth()
  71. .background(Color.White.copy(alpha = 0.1f), RoundedCornerShape(8.dp))
  72. .padding(8.dp),
  73. text = textData.second,
  74. color = Color.White.copy(alpha = 0.9f)
  75. )
  76. }
  77. }
  78. }
  79. OutlinedTextField(
  80. modifier = Modifier.padding(top = 8.dp).fillMaxWidth(),
  81. value = request.value,
  82. onValueChange = { t: String ->
  83. request.value = t
  84. if (t.contains("\n")) {
  85. scope.launch {
  86. onDone(t.replace("\n", ""))
  87. }
  88. request.value = ""
  89. }
  90. },
  91. maxLines = 4,
  92. minLines = 4,
  93. shape = RoundedCornerShape(8.dp),
  94. colors = TextFieldDefaults.outlinedTextFieldColors(
  95. backgroundColor = Color.White.copy(alpha = 0.1f),
  96. textColor = Color.White.copy(alpha = 0.9f),
  97. cursorColor = Color.White,
  98. focusedBorderColor = Color.White,
  99. unfocusedBorderColor = Color.Black
  100. )
  101. )
  102. }
  103. }
  104. }
  105. }