add send message
This commit is contained in:
+10
@@ -5,6 +5,7 @@ import org.koin.core.annotation.Module
|
|||||||
import org.koin.core.annotation.Single
|
import org.koin.core.annotation.Single
|
||||||
import ru.shadowsparky.backend.data.EnvFetcher
|
import ru.shadowsparky.backend.data.EnvFetcher
|
||||||
import ru.shadowsparky.backend.domain.JwtInfo
|
import ru.shadowsparky.backend.domain.JwtInfo
|
||||||
|
import ru.shadowsparky.domain.AppConfig
|
||||||
|
|
||||||
@Module
|
@Module
|
||||||
@ComponentScan
|
@ComponentScan
|
||||||
@@ -20,4 +21,13 @@ class BackendModule {
|
|||||||
"wear"
|
"wear"
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Single
|
||||||
|
fun provideAppConfig(): AppConfig {
|
||||||
|
return object : AppConfig {
|
||||||
|
override val debug = true
|
||||||
|
override val appId = "ru.shadowsparky.gigawear.backend"
|
||||||
|
override val backend = true
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,17 @@
|
|||||||
package ru.shadowsparky.gigawear.backend
|
package ru.shadowsparky.gigawear.backend
|
||||||
|
|
||||||
import io.ktor.http.HttpStatusCode
|
import io.ktor.http.HttpStatusCode
|
||||||
|
import io.ktor.serialization.kotlinx.json.json
|
||||||
import io.ktor.server.application.Application
|
import io.ktor.server.application.Application
|
||||||
|
import io.ktor.server.application.install
|
||||||
|
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
|
||||||
import io.ktor.server.response.respond
|
import io.ktor.server.response.respond
|
||||||
import io.ktor.server.routing.get
|
import io.ktor.server.routing.get
|
||||||
import io.ktor.server.routing.routing
|
import io.ktor.server.routing.routing
|
||||||
import org.koin.core.annotation.KoinApplication
|
import org.koin.core.annotation.KoinApplication
|
||||||
import org.koin.core.component.KoinComponent
|
|
||||||
import org.koin.plugin.module.dsl.startKoin
|
import org.koin.plugin.module.dsl.startKoin
|
||||||
|
import ru.shadowsparky.gigawear.backend.presentation.setupChat
|
||||||
|
import ru.shadowsparky.koin
|
||||||
|
|
||||||
@KoinApplication
|
@KoinApplication
|
||||||
object GigaWearApp
|
object GigaWearApp
|
||||||
@@ -18,10 +22,9 @@ fun main(args: Array<String>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun Application.module() {
|
fun Application.module() {
|
||||||
val koin = object : KoinComponent {}
|
install(ContentNegotiation) { json(koin.get()) }
|
||||||
this.routing {
|
routing {
|
||||||
get("health") {
|
get("health") { this.call.respond(HttpStatusCode.OK) }
|
||||||
this.call.respond(HttpStatusCode.OK)
|
setupChat(koin.get(), koin.get())
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+17
@@ -0,0 +1,17 @@
|
|||||||
|
package ru.shadowsparky.gigawear.backend.presentation
|
||||||
|
|
||||||
|
import io.ktor.server.request.receive
|
||||||
|
import io.ktor.server.response.respond
|
||||||
|
import io.ktor.server.routing.Routing
|
||||||
|
import io.ktor.server.routing.post
|
||||||
|
import ru.shadowsparky.chat.backend.domain.ProcessUserMessageUseCase
|
||||||
|
import ru.shadowsparky.chat.domain.Message
|
||||||
|
|
||||||
|
private const val MOCK_USER_ID = -1L
|
||||||
|
|
||||||
|
fun Routing.setupChat(useCase: ProcessUserMessageUseCase) {
|
||||||
|
post("chat") {
|
||||||
|
val message = call.receive<Message>().copy(timestamp = System.currentTimeMillis())
|
||||||
|
call.respond(useCase.execute(message, MOCK_USER_ID))
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-3
@@ -32,9 +32,7 @@ fun Route.setupChat(
|
|||||||
val userId = call.obtainUserId()
|
val userId = call.obtainUserId()
|
||||||
val info = call.receive<Message>()
|
val info = call.receive<Message>()
|
||||||
if (info.role != ChatRoles.USER) throw BadRequestException("invalid role")
|
if (info.role != ChatRoles.USER) throw BadRequestException("invalid role")
|
||||||
val repository = chatRepositoryFactory.create(userId)
|
val response = processUserMessageUseCase.execute(info, userId)
|
||||||
repository.send(info)
|
|
||||||
val response = processUserMessageUseCase.execute(userId)
|
|
||||||
remoteEventHandler.notify(RemoteEvent.OnChatUpdate(userId))
|
remoteEventHandler.notify(RemoteEvent.OnChatUpdate(userId))
|
||||||
call.respond(response)
|
call.respond(response)
|
||||||
}
|
}
|
||||||
|
|||||||
+4
@@ -4,6 +4,7 @@ import org.koin.core.annotation.Factory
|
|||||||
import ru.shadowsparky.backend.data.EnvFetcher
|
import ru.shadowsparky.backend.data.EnvFetcher
|
||||||
import ru.shadowsparky.chat.domain.ChatRoles
|
import ru.shadowsparky.chat.domain.ChatRoles
|
||||||
import ru.shadowsparky.chat.domain.Message
|
import ru.shadowsparky.chat.domain.Message
|
||||||
|
import ru.shadowsparky.http.domain.BadRequestException
|
||||||
|
|
||||||
@Factory
|
@Factory
|
||||||
class ProcessUserMessageUseCase(
|
class ProcessUserMessageUseCase(
|
||||||
@@ -12,9 +13,12 @@ class ProcessUserMessageUseCase(
|
|||||||
private val envFetcher: EnvFetcher
|
private val envFetcher: EnvFetcher
|
||||||
) {
|
) {
|
||||||
suspend fun execute(
|
suspend fun execute(
|
||||||
|
userMessage: Message,
|
||||||
userId: Long,
|
userId: Long,
|
||||||
systemPrompt: String = envFetcher.get("CHAT_SYSTEM_PROMPT").trim('"')
|
systemPrompt: String = envFetcher.get("CHAT_SYSTEM_PROMPT").trim('"')
|
||||||
): Message {
|
): Message {
|
||||||
|
if (userMessage.role != ChatRoles.USER) throw BadRequestException("invalid role")
|
||||||
|
storage.insert(userId, userMessage)
|
||||||
val history = storage.query(userId, null, limit = 10).toList()
|
val history = storage.query(userId, null, limit = 10).toList()
|
||||||
val sortedHistory = history.sortedBy { it.timestamp }
|
val sortedHistory = history.sortedBy { it.timestamp }
|
||||||
val systemMessage = Message(
|
val systemMessage = Message(
|
||||||
|
|||||||
+1
-1
@@ -78,7 +78,7 @@ class HttpClientSettings(
|
|||||||
install(Logging) {
|
install(Logging) {
|
||||||
val isDebug = koin.getOrNull<AppConfig>()?.debug ?: false
|
val isDebug = koin.getOrNull<AppConfig>()?.debug ?: false
|
||||||
if (isDebug) {
|
if (isDebug) {
|
||||||
this.level = LogLevel.HEADERS
|
this.level = LogLevel.ALL
|
||||||
this.logger = object : Logger {
|
this.logger = object : Logger {
|
||||||
override fun log(message: String) {
|
override fun log(message: String) {
|
||||||
log.d("Http", message)
|
log.d("Http", message)
|
||||||
|
|||||||
Reference in New Issue
Block a user