Add user profile, cancel button, dark theme and UI improvements
This commit is contained in:
parent
cda6eb7ce0
commit
a4d24df8d8
21 changed files with 635 additions and 207 deletions
|
|
@ -6,13 +6,14 @@ import com.google.gson.JsonArray
|
|||
import com.mistral.chat.data.Message
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import okhttp3.Call
|
||||
import okhttp3.MediaType.Companion.toMediaType
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
class MistralClient(private val apiKey: String) {
|
||||
class MistralClient(private val apiKey: String) {
|
||||
|
||||
private val client = OkHttpClient.Builder()
|
||||
.connectTimeout(60, TimeUnit.SECONDS)
|
||||
|
|
@ -22,6 +23,8 @@ class MistralClient(private val apiKey: String) {
|
|||
|
||||
private val gson = Gson()
|
||||
private val jsonMediaType = "application/json".toMediaType()
|
||||
|
||||
private var currentCall: Call? = null
|
||||
|
||||
companion object {
|
||||
private const val BASE_URL = "https://api.mistral.ai/v1"
|
||||
|
|
@ -30,8 +33,7 @@ class MistralClient(private val apiKey: String) {
|
|||
"mistral-small-latest" to "Mistral Small",
|
||||
"mistral-medium-latest" to "Mistral Medium",
|
||||
"mistral-large-latest" to "Mistral Large",
|
||||
"codestral-latest" to "Codestral",
|
||||
"pixtral-large-latest" to "Pixtral Large"
|
||||
"codestral-latest" to "Codestral"
|
||||
)
|
||||
}
|
||||
|
||||
|
|
@ -73,6 +75,11 @@ class MistralClient(private val apiKey: String) {
|
|||
Result.failure(e)
|
||||
}
|
||||
}
|
||||
|
||||
fun cancelRequest() {
|
||||
currentCall?.cancel()
|
||||
currentCall = null
|
||||
}
|
||||
|
||||
suspend fun chat(
|
||||
model: String,
|
||||
|
|
@ -104,7 +111,12 @@ class MistralClient(private val apiKey: String) {
|
|||
.post(body)
|
||||
.build()
|
||||
|
||||
val response = client.newCall(request).execute()
|
||||
currentCall = client.newCall(request)
|
||||
val response = currentCall!!.execute()
|
||||
|
||||
if (response.code == 0 || response.code == -1) {
|
||||
return@withContext Result.failure(Exception("Request cancelled"))
|
||||
}
|
||||
|
||||
if (!response.isSuccessful) {
|
||||
val errorBody = response.body?.string() ?: "Unknown error"
|
||||
|
|
@ -133,6 +145,7 @@ class MistralClient(private val apiKey: String) {
|
|||
|
||||
val usedModel = responseJson.get("model")?.asString ?: model
|
||||
|
||||
currentCall = null
|
||||
Result.success(content to usedModel)
|
||||
} catch (e: Exception) {
|
||||
Result.failure(e)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue