backend part
This commit is contained in:
+5
-20
@@ -10,7 +10,6 @@ import io.ktor.client.request.header
|
||||
import io.ktor.client.request.post
|
||||
import io.ktor.client.request.setBody
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.http.contentType
|
||||
import io.ktor.serialization.kotlinx.json.json
|
||||
import kotlinx.serialization.SerialName
|
||||
@@ -19,7 +18,6 @@ import kotlinx.serialization.json.Json
|
||||
import org.koin.core.annotation.Factory
|
||||
import ru.shadowsparky.chat.backend.domain.ChatService
|
||||
import ru.shadowsparky.chat.domain.Message
|
||||
import ru.shadowsparky.http.domain.HttpException
|
||||
import java.util.UUID
|
||||
|
||||
@Factory
|
||||
@@ -35,28 +33,15 @@ class GigaChatService(
|
||||
val requestBody = GigaChatRequest(
|
||||
messages = messages.map { GigaChatMessageDto(role = it.role, content = it.content) }
|
||||
)
|
||||
val response = try {
|
||||
executeRequest(clientId, staticSessionId, requestBody)
|
||||
} catch (e: HttpException) {
|
||||
if (e.httpCode == HttpStatusCode.Unauthorized.value) {
|
||||
tokenManager.forceRefreshToken()
|
||||
executeRequest(clientId, staticSessionId, requestBody)
|
||||
} else {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
return response.choices.firstOrNull()?.message?.content
|
||||
?: error("unexpected response $response")
|
||||
}
|
||||
|
||||
private suspend fun executeRequest(clientId: String, sessionId: String, body: GigaChatRequest): GigaChatResponse {
|
||||
return httpClient.post("https://api.giga.chat/v1/chat/completions") {
|
||||
val response: GigaChatResponse = httpClient.post("https://api.giga.chat/v1/chat/completions") {
|
||||
contentType(ContentType.Application.Json)
|
||||
header("X-Request-ID", UUID.randomUUID().toString())
|
||||
header("X-Session-ID", sessionId)
|
||||
header("X-Session-ID", staticSessionId)
|
||||
header("X-Client-ID", clientId)
|
||||
setBody(body)
|
||||
setBody(requestBody)
|
||||
}.body()
|
||||
return response.choices.firstOrNull()?.message?.content
|
||||
?: error("unexpected response $response")
|
||||
}
|
||||
|
||||
private fun createGigaChatHttpClient(): HttpClient {
|
||||
|
||||
+1
-2
@@ -9,8 +9,7 @@ class ProcessUserMessageUseCase(
|
||||
private val storage: MessageStorage,
|
||||
private val chatService: ChatService
|
||||
) {
|
||||
suspend fun execute(userId: Long, userMessage: Message): Message {
|
||||
storage.insert(userId, userMessage)
|
||||
suspend fun execute(userId: Long): Message {
|
||||
val history = storage.query(userId, limit = 10).toList()
|
||||
val sortedHistory = history.sortedBy { it.timestamp }
|
||||
val systemMessage = Message(
|
||||
|
||||
+2
-5
@@ -1,9 +1,6 @@
|
||||
package ru.shadowsparky.chat.domain
|
||||
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface ChatRepository {
|
||||
val messages: Flow<Message>
|
||||
|
||||
suspend fun sendMessage(message: Message)
|
||||
suspend fun query(): List<Message>
|
||||
suspend fun send(message: Message)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user