From 346abc24fefb72fa587c82c14291bb87b3e4cbc9 Mon Sep 17 00:00:00 2001 From: Eugene Date: Sat, 12 Sep 2026 23:53:16 +0300 Subject: [PATCH] add send message --- .../gigawear/backend/BackendModule.kt | 10 ++++++++++ .../ru/shadowsparky/gigawear/backend/Main.kt | 15 +++++++++------ .../backend/presentation/ChatRouting.kt | 17 +++++++++++++++++ .../backend/presentation/routing/ChatRouting.kt | 4 +--- .../backend/domain/ProcessUserMessageUseCase.kt | 4 ++++ .../http/data/HttpClientSettings.kt | 2 +- 6 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 apps/giga-wear/backend/src/main/kotlin/ru/shadowsparky/gigawear/backend/presentation/ChatRouting.kt diff --git a/apps/giga-wear/backend/src/main/kotlin/ru/shadowsparky/gigawear/backend/BackendModule.kt b/apps/giga-wear/backend/src/main/kotlin/ru/shadowsparky/gigawear/backend/BackendModule.kt index a2c75ad..75aa5bb 100644 --- a/apps/giga-wear/backend/src/main/kotlin/ru/shadowsparky/gigawear/backend/BackendModule.kt +++ b/apps/giga-wear/backend/src/main/kotlin/ru/shadowsparky/gigawear/backend/BackendModule.kt @@ -5,6 +5,7 @@ import org.koin.core.annotation.Module import org.koin.core.annotation.Single import ru.shadowsparky.backend.data.EnvFetcher import ru.shadowsparky.backend.domain.JwtInfo +import ru.shadowsparky.domain.AppConfig @Module @ComponentScan @@ -20,4 +21,13 @@ class BackendModule { "wear" ) } + + @Single + fun provideAppConfig(): AppConfig { + return object : AppConfig { + override val debug = true + override val appId = "ru.shadowsparky.gigawear.backend" + override val backend = true + } + } } diff --git a/apps/giga-wear/backend/src/main/kotlin/ru/shadowsparky/gigawear/backend/Main.kt b/apps/giga-wear/backend/src/main/kotlin/ru/shadowsparky/gigawear/backend/Main.kt index 3c7b088..d6fc4a5 100644 --- a/apps/giga-wear/backend/src/main/kotlin/ru/shadowsparky/gigawear/backend/Main.kt +++ b/apps/giga-wear/backend/src/main/kotlin/ru/shadowsparky/gigawear/backend/Main.kt @@ -1,13 +1,17 @@ package ru.shadowsparky.gigawear.backend import io.ktor.http.HttpStatusCode +import io.ktor.serialization.kotlinx.json.json 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.routing.get import io.ktor.server.routing.routing import org.koin.core.annotation.KoinApplication -import org.koin.core.component.KoinComponent import org.koin.plugin.module.dsl.startKoin +import ru.shadowsparky.gigawear.backend.presentation.setupChat +import ru.shadowsparky.koin @KoinApplication object GigaWearApp @@ -18,10 +22,9 @@ fun main(args: Array) { } fun Application.module() { - val koin = object : KoinComponent {} - this.routing { - get("health") { - this.call.respond(HttpStatusCode.OK) - } + install(ContentNegotiation) { json(koin.get()) } + routing { + get("health") { this.call.respond(HttpStatusCode.OK) } + setupChat(koin.get(), koin.get()) } } diff --git a/apps/giga-wear/backend/src/main/kotlin/ru/shadowsparky/gigawear/backend/presentation/ChatRouting.kt b/apps/giga-wear/backend/src/main/kotlin/ru/shadowsparky/gigawear/backend/presentation/ChatRouting.kt new file mode 100644 index 0000000..69b0444 --- /dev/null +++ b/apps/giga-wear/backend/src/main/kotlin/ru/shadowsparky/gigawear/backend/presentation/ChatRouting.kt @@ -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().copy(timestamp = System.currentTimeMillis()) + call.respond(useCase.execute(message, MOCK_USER_ID)) + } +} diff --git a/apps/vbox/backend/src/main/kotlin/ru/shadowsparky/vbox/backend/presentation/routing/ChatRouting.kt b/apps/vbox/backend/src/main/kotlin/ru/shadowsparky/vbox/backend/presentation/routing/ChatRouting.kt index 48d3c29..5b2eea6 100644 --- a/apps/vbox/backend/src/main/kotlin/ru/shadowsparky/vbox/backend/presentation/routing/ChatRouting.kt +++ b/apps/vbox/backend/src/main/kotlin/ru/shadowsparky/vbox/backend/presentation/routing/ChatRouting.kt @@ -32,9 +32,7 @@ fun Route.setupChat( val userId = call.obtainUserId() val info = call.receive() if (info.role != ChatRoles.USER) throw BadRequestException("invalid role") - val repository = chatRepositoryFactory.create(userId) - repository.send(info) - val response = processUserMessageUseCase.execute(userId) + val response = processUserMessageUseCase.execute(info, userId) remoteEventHandler.notify(RemoteEvent.OnChatUpdate(userId)) call.respond(response) } diff --git a/feature/chat/chat-backend/src/main/kotlin/ru/shadowsparky/chat/backend/domain/ProcessUserMessageUseCase.kt b/feature/chat/chat-backend/src/main/kotlin/ru/shadowsparky/chat/backend/domain/ProcessUserMessageUseCase.kt index 7374711..f77db2d 100644 --- a/feature/chat/chat-backend/src/main/kotlin/ru/shadowsparky/chat/backend/domain/ProcessUserMessageUseCase.kt +++ b/feature/chat/chat-backend/src/main/kotlin/ru/shadowsparky/chat/backend/domain/ProcessUserMessageUseCase.kt @@ -4,6 +4,7 @@ import org.koin.core.annotation.Factory import ru.shadowsparky.backend.data.EnvFetcher import ru.shadowsparky.chat.domain.ChatRoles import ru.shadowsparky.chat.domain.Message +import ru.shadowsparky.http.domain.BadRequestException @Factory class ProcessUserMessageUseCase( @@ -12,9 +13,12 @@ class ProcessUserMessageUseCase( private val envFetcher: EnvFetcher ) { suspend fun execute( + userMessage: Message, userId: Long, systemPrompt: String = envFetcher.get("CHAT_SYSTEM_PROMPT").trim('"') ): Message { + if (userMessage.role != ChatRoles.USER) throw BadRequestException("invalid role") + storage.insert(userId, userMessage) val history = storage.query(userId, null, limit = 10).toList() val sortedHistory = history.sortedBy { it.timestamp } val systemMessage = Message( diff --git a/libs/http-client/src/commonMain/kotlin/ru/shadowsparky/http/data/HttpClientSettings.kt b/libs/http-client/src/commonMain/kotlin/ru/shadowsparky/http/data/HttpClientSettings.kt index 70c95ec..b3551df 100644 --- a/libs/http-client/src/commonMain/kotlin/ru/shadowsparky/http/data/HttpClientSettings.kt +++ b/libs/http-client/src/commonMain/kotlin/ru/shadowsparky/http/data/HttpClientSettings.kt @@ -78,7 +78,7 @@ class HttpClientSettings( install(Logging) { val isDebug = koin.getOrNull()?.debug ?: false if (isDebug) { - this.level = LogLevel.HEADERS + this.level = LogLevel.ALL this.logger = object : Logger { override fun log(message: String) { log.d("Http", message)