Skip to content

Commit

Permalink
Merge pull request #346 from apeun-gidaechi/feature/340-apply-ai-mapping
Browse files Browse the repository at this point in the history
Feature/Apply Ai Mapping
  • Loading branch information
wnsgur1 authored Oct 25, 2024
2 parents 4d8ce6b + 5fc251d commit ca7bf9d
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ interface MessageRepository {
suspend fun getMessage(chatRoomId: String, timestamp: LocalDateTime?, userId: Long): Flow<Result<MessageLoadModel>>

suspend fun collectStompLifecycle(): Flow<Result<MessageStompLifecycleModel>>

suspend fun sendText(text: String): Flow<Result<String>>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.seugi.data.message.mapper

import com.seugi.data.core.model.MealType
import com.seugi.data.message.model.CatSeugiResponse

fun CatSeugiResponse.toModel(): String {
return when (this) {
is CatSeugiResponse.ETC -> {
this.data
}
is CatSeugiResponse.Meal -> {
val breakfast = this.data.firstOrNull { it.mealType == MealType.BREAKFAST }
val lunch = this.data.firstOrNull { it.mealType == MealType.LUNCH }
val dinner = this.data.firstOrNull { it.mealType == MealType.DINNER }

val visibleMessage = buildString {
breakfast?.let {
append("- 오늘의 조식\n")
append(it.menu.joinToString(separator = "\n"))
if (lunch != null || dinner != null) {
append("\n\n")
}
}

lunch?.let {
append("- 오늘의 중식\n")
append(it.menu.joinToString(separator = "\n"))
if (dinner != null) {
append("\n\n")
}
}

dinner?.let {
append("- 오늘의 석식\n")
append(it.menu.joinToString(separator = "\n"))
}

if (isEmpty()) {
append("오늘의 급식 없습니다.")
}
}

visibleMessage
}
is CatSeugiResponse.NotSupport -> {
this.data
}
is CatSeugiResponse.Timetable -> {
val result = this.data.map {
"${it.time}교시 ${it.subject}"
}.joinToString(separator = "\n")
result
}
is CatSeugiResponse.Picking -> {
val cleanedData = this.data.replace("::", "")
cleanedData
}

is CatSeugiResponse.Team -> {
val result = this.data.replace("::", "")
result
}
is CatSeugiResponse.Notification -> {
var visibleMessage = ""
this.data.forEach {
visibleMessage += "${it.userName} 선생님이 공지를 작성하셨어요\n" +
"제목: ${it.title}" +
it.content
}

visibleMessage
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.seugi.data.message.model

import com.seugi.data.core.model.MealModel
import com.seugi.data.core.model.NotificationModel
import com.seugi.data.core.model.TimetableModel
import kotlinx.collections.immutable.ImmutableList

sealed interface CatSeugiResponse {
data class Meal(
val data: ImmutableList<MealModel>,
) : CatSeugiResponse

data class Timetable(
val data: ImmutableList<TimetableModel>,
) : CatSeugiResponse

data class ETC(
val data: String,
) : CatSeugiResponse

data class NotSupport(
val data: String,
) : CatSeugiResponse

data class Picking(
val data: String,
) : CatSeugiResponse

data class Team(
val data: String,
) : CatSeugiResponse

data class Notification(
val data: ImmutableList<NotificationModel>,
) : CatSeugiResponse
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
package com.seugi.data.message.repository

import android.util.Log
import com.seugi.common.model.Result
import com.seugi.common.model.asResult
import com.seugi.common.utiles.DispatcherType
import com.seugi.common.utiles.SeugiDispatcher
import com.seugi.data.core.mapper.toModel
import com.seugi.data.core.mapper.toModels
import com.seugi.data.core.model.TimetableModel
import com.seugi.data.message.MessageRepository
import com.seugi.data.message.mapper.toEventModel
import com.seugi.data.message.mapper.toModel
import com.seugi.data.message.model.CatSeugiResponse
import com.seugi.data.message.model.MessageBotRawKeyword
import com.seugi.data.message.model.MessageBotRawKeywordInData
import com.seugi.data.message.model.MessageLoadModel
import com.seugi.data.message.model.MessageRoomEvent
import com.seugi.data.message.model.MessageType
import com.seugi.data.message.model.stomp.MessageStompLifecycleModel
import com.seugi.local.room.dao.TokenDao
import com.seugi.network.core.response.safeResponse
import com.seugi.network.core.utiles.toResponse
import com.seugi.network.meal.response.MealResponse
import com.seugi.network.message.MessageDataSource
import com.seugi.network.notification.response.NotificationResponse
import javax.inject.Inject
import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
Expand Down Expand Up @@ -83,4 +95,58 @@ class MessageRepositoryImpl @Inject constructor(
.map { it.toModel() }
.flowOn(dispatcher)
.asResult()

override suspend fun sendText(text: String): Flow<Result<String>> = flow {
val response = datasource.sendText(text).safeResponse()

val keyword = response.toResponse<MessageBotRawKeyword>().keyword
Log.d("TAG", "sendText: $keyword")
val result: CatSeugiResponse = when (keyword) {
"급식" -> {
val data = response.toResponse<MessageBotRawKeywordInData<List<MealResponse>>>()
CatSeugiResponse.Meal(
data = data.data.toModels().toImmutableList(),
)
}
"시간표" -> {
val data = response.toResponse<MessageBotRawKeywordInData<List<TimetableModel>>>()
CatSeugiResponse.Timetable(
data = data.data.toImmutableList(),
)
}
"기타" -> {
val data = response.toResponse<MessageBotRawKeywordInData<String>>()
CatSeugiResponse.ETC(
data = data.data,
)
}
"공지" -> {
val data = response.toResponse<MessageBotRawKeywordInData<List<NotificationResponse>>>()
CatSeugiResponse.Notification(
data = data.data.toModels().toImmutableList(),
)
}
"사람 뽑기" -> {
val data = response.toResponse<MessageBotRawKeywordInData<String>>()
CatSeugiResponse.Picking(
data = data.data,
)
}
"팀짜기" -> {
val data = response.toResponse<MessageBotRawKeywordInData<String>>()
CatSeugiResponse.Team(
data = data.data,
)
}
else -> {
CatSeugiResponse.NotSupport(
data = "현재 버전에서 지원하지 않는 메세지 유형입니다.",
)
}
}

emit(result.toModel())
}
.flowOn(dispatcher)
.asResult()
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.seugi.chatseugi.model.ChatData
import com.seugi.chatseugi.model.ChatSeugiUiState
import com.seugi.common.model.Result
import com.seugi.data.catseugi.CatSeugiRepository
import com.seugi.data.message.MessageRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import java.time.LocalDateTime
import javax.inject.Inject
Expand All @@ -18,6 +19,7 @@ import kotlinx.coroutines.launch
@HiltViewModel
class ChatSeugiViewModel @Inject constructor(
private val catSeugiRepository: CatSeugiRepository,
private val messageRepository: MessageRepository,
) : ViewModel() {

private val _state = MutableStateFlow(ChatSeugiUiState())
Expand All @@ -33,7 +35,7 @@ class ChatSeugiViewModel @Inject constructor(
)
}

catSeugiRepository.sendText(
messageRepository.sendText(
text = message,
).collect {
when (it) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.seugi.home.card
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
Expand All @@ -15,12 +17,12 @@ import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.seugi.designsystem.R
import com.seugi.designsystem.animation.bounceClick
import com.seugi.designsystem.component.GradientPrimary
import com.seugi.designsystem.component.LoadingDotsIndicator
import com.seugi.designsystem.component.modifier.brushDraw
Expand Down Expand Up @@ -50,8 +52,10 @@ internal fun CatSeugiCard(uiState: CommonUiState<ImmutableList<String>>, navigat
brush = GradientPrimary,
shape = CircleShape,
)
.bounceClick(
.clickable(
onClick = navigateToChatSeugi,
indication = null,
interactionSource = remember { MutableInteractionSource() },
),
verticalAlignment = Alignment.CenterVertically,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ interface MessageDataSource {
suspend fun getMessage(chatRoomId: String, timestamp: LocalDateTime?): BaseResponse<MessageLoadResponse>

suspend fun collectStompLifecycle(): Flow<MessageStompLifecycleResponse>

suspend fun sendText(text: String): BaseResponse<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.seugi.network.core.response.BaseResponse
import com.seugi.network.core.utiles.toJsonString
import com.seugi.network.core.utiles.toResponse
import com.seugi.network.message.MessageDataSource
import com.seugi.network.message.request.CatSeugiRequest
import com.seugi.network.message.request.MessageRequest
import com.seugi.network.message.response.MessageRoomEventResponse
import com.seugi.network.message.response.message.MessageLoadResponse
Expand All @@ -20,6 +21,10 @@ import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.get
import io.ktor.client.request.parameter
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.http.ContentType
import io.ktor.http.contentType
import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.coroutineScope
Expand Down Expand Up @@ -138,4 +143,14 @@ class MessageDataSourceImpl @Inject constructor(
)
return true
}

override suspend fun sendText(text: String): BaseResponse<String> = httpClient.post(SeugiUrl.AI) {
setBody(
CatSeugiRequest(
message = text,
roomId = "67177e4ac6b844040200d65c",
),
)
contentType(ContentType.Application.Json)
}.body()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.seugi.network.message.request

data class CatSeugiRequest(
val message: String,
val roomId: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.seugi.network.message.response

sealed class CatSeugiResponse(
@Transient open val type: String,
) {
data class Meal(
override val type: String,
val id: Long,
val workspaceId: String,
val mealType: String,
val menu: List<String>,
val calorie: String,
val mealInfo: List<String>,
val mealDate: List<String>,
) : CatSeugiResponse(type)

data class Timetable(
override val type: String,
val id: Long,
val workspaceId: String,
val grade: String,
val className: String,
val time: String,
val subject: String,
val date: String,
) : CatSeugiResponse(type)

data class ETC(
override val type: String,
val data: String,
) : CatSeugiResponse(type)
}

0 comments on commit ca7bf9d

Please sign in to comment.