Skip to content

Commit

Permalink
Chat-SVC Refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartosz committed Dec 4, 2024
1 parent b58ea17 commit cffc87a
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 135 deletions.
16 changes: 8 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.github.onecx</groupId>
<groupId>org.tkit.onecx</groupId>
<artifactId>onecx-quarkus3-parent</artifactId>
<version>0.26.0</version>
<version>0.72.0</version>
</parent>

<artifactId>onecx-chat-svc</artifactId>
<version>999-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>io.github.onecx.quarkus</groupId>
<groupId>org.tkit.onecx.quarkus</groupId>
<artifactId>onecx-core</artifactId>
</dependency>
<dependency>
<groupId>io.github.onecx.quarkus</groupId>
<groupId>org.tkit.onecx.quarkus</groupId>
<artifactId>onecx-tenant</artifactId>
</dependency>

<!-- 1000kit -->
<dependency>
<groupId>org.tkit.quarkus.lib</groupId>
Expand Down Expand Up @@ -83,11 +84,11 @@
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive</artifactId>
<artifactId>quarkus-rest</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive-jackson</artifactId>
<artifactId>quarkus-rest-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
Expand Down Expand Up @@ -119,7 +120,6 @@
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
</dependency>

<dependency>
<groupId>io.quarkiverse.mockserver</groupId>
<artifactId>quarkus-mockserver</artifactId>
Expand Down Expand Up @@ -209,7 +209,7 @@
<artifactId>download-maven-plugin</artifactId>
<executions>
<execution>
<id>user-profile-svc-internal</id>
<id>onecx-ai-svc</id>
<phase>generate-resources</phase>
<goals>
<goal>wget</goal>
Expand Down
10 changes: 2 additions & 8 deletions src/main/docker/Dockerfile.jvm
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
FROM registry.access.redhat.com/ubi9/openjdk-17:1.15

ENV LANGUAGE='en_US:en'
FROM ghcr.io/onecx/docker-quarkus-jvm:0.14.0

COPY --chown=185 target/quarkus-app/lib/ /deployments/lib/
COPY --chown=185 target/quarkus-app/*.jar /deployments/
COPY --chown=185 target/quarkus-app/app/ /deployments/app/
COPY --chown=185 target/quarkus-app/quarkus/ /deployments/quarkus/

EXPOSE 8080
USER 185
ENV JAVA_OPTS="-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager"
ENV JAVA_APP_JAR="/deployments/quarkus-run.jar"
USER 185
12 changes: 2 additions & 10 deletions src/main/docker/Dockerfile.native
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
FROM registry.access.redhat.com/ubi9/ubi-minimal:9.2
WORKDIR /work/
RUN chown 1001 /work \
&& chmod "g+rwX" /work \
&& chown 1001:root /work
COPY --chown=1001:root target/*-runner /work/application
FROM ghcr.io/onecx/docker-quarkus-native:0.10.0

EXPOSE 8080
USER 1001

CMD ["./application", "-Dquarkus.http.host=0.0.0.0"]
COPY --chown=1001:root target/*-runner /work/application
9 changes: 8 additions & 1 deletion src/main/helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,11 @@ app:
repository: "onecx-apps/onecx-chat-svc"
tag: 999-SNAPSHOT
db:
enabled: true
enabled: true
operator:
keycloak:
client:
enabled: true
spec:
kcConfig:
defaultClientScopes: [ ocx-tn:read ]
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public class ChatsRestController implements ChatsInternalApi {
@Override
@Transactional
public Response createChat(CreateChatDTO createChatDTO) {

var chat = mapper.create(createChatDTO);
chat = dao.create(chat);

Expand Down Expand Up @@ -150,14 +149,16 @@ public Response createChatMessage(String chatId, CreateMessageDTO createMessageD
ChatRequest chatRequest = new ChatRequest();
chatRequest.chatMessage(chatMessage);
chatRequest.conversation(conversation);
Response response = aiChatClient.chat(chatRequest);

var chatResponse = response.readEntity(ChatMessage.class);

var responseMessage = mapper.mapAiSvcMessage(chatResponse);
responseMessage.setChat(chat);
msgDao.create(responseMessage);

try (Response response = aiChatClient.chat(chatRequest)) {
var chatResponse = response.readEntity(ChatMessage.class);
var responseMessage = mapper.mapAiSvcMessage(chatResponse);
responseMessage.setChat(chat);
msgDao.create(responseMessage);
} catch (Exception e) {
throw new ConstraintException(e.getMessage(), ChatErrorKeys.ERROR_CALLING_AI_CHAT_SERVICE,
null);
}
}

return Response
Expand Down Expand Up @@ -227,7 +228,8 @@ public RestResponse<ProblemDetailResponseDTO> constraint(ConstraintViolationExce
}

enum ChatErrorKeys {
CHAT_DOES_NOT_EXIST
CHAT_DOES_NOT_EXIST,
ERROR_CALLING_AI_CHAT_SERVICE
}

}
113 changes: 24 additions & 89 deletions src/main/java/io/github/onecx/chat/rs/internal/mappers/ChatMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Stream;

import jakarta.inject.Inject;

import org.mapstruct.IterableMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
Expand All @@ -23,9 +18,6 @@
import org.tkit.quarkus.jpa.daos.PageResult;
import org.tkit.quarkus.rs.mappers.OffsetDateTimeMapper;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import gen.io.github.onecx.ai.clients.model.ChatMessage;
import gen.io.github.onecx.ai.clients.model.ChatMessage.TypeEnum;
import gen.io.github.onecx.ai.clients.model.Conversation;
Expand All @@ -35,18 +27,14 @@
import io.github.onecx.chat.domain.models.Message;
import io.github.onecx.chat.domain.models.Message.MessageType;
import io.github.onecx.chat.domain.models.Participant;
import io.github.onecx.chat.domain.models.Participant.ParticipantType;

@Mapper(uses = { OffsetDateTimeMapper.class })
public abstract class ChatMapper {

@Inject
ObjectMapper mapper;
public interface ChatMapper {

public abstract ChatSearchCriteria map(ChatSearchCriteriaDTO dto);
ChatSearchCriteria map(ChatSearchCriteriaDTO dto);

@Mapping(target = "removeStreamItem", ignore = true)
public abstract ChatPageResultDTO mapPage(PageResult<Chat> page);
ChatPageResultDTO mapPage(PageResult<Chat> page);

@Mapping(target = "id", ignore = true)
@Mapping(target = "controlTraceabilityManual", ignore = true)
Expand All @@ -56,96 +44,57 @@ public abstract class ChatMapper {
@Mapping(target = "messages", ignore = true)
//@Mapping(target = "participants", source = "participants")
@Mapping(target = "participants", ignore = true)
public abstract Chat create(CreateChatDTO object);

public List<Participant> mapParticipantDTOs(List<ParticipantDTO> participantDTOs) {

List<Participant> participants = new ArrayList<>();

if (participantDTOs != null) {
for (ParticipantDTO participantDTO : participantDTOs) {
Participant participant = mapParticipant(participantDTO);
if (participant.getId() == null) {
participant.setId(UUID.randomUUID().toString());
}
participants.add(participant);
}
}

return participants;

}
Chat create(CreateChatDTO object);

public Set<Participant> mapParticipants(List<ParticipantDTO> participantDTOs) {
Set<Participant> participants = new HashSet<>();
List<Participant> mapParticipantDTOs(List<ParticipantDTO> participantDTOs);

if (participantDTOs != null && !participantDTOs.isEmpty()) {
for (ParticipantDTO participantDTO : participantDTOs) {

Participant participant = new Participant();

if (participantDTO.getId() != null) {
participant.setId(participantDTO.getId());
} else {
participant.setId(UUID.randomUUID().toString());
}
participant.setUserName(participantDTO.getUserName());
participant.setUserId(participantDTO.getUserId());
participant.setEmail(participantDTO.getEmail());
participant.setType(ParticipantType.valueOf(participantDTO.getType().name()));

participants.add(participant);
}
}

return participants;
}
Set<Participant> mapParticipants(List<ParticipantDTO> participantDTOs);

@Mapping(target = "id", ignore = true)
@Mapping(target = "controlTraceabilityManual", ignore = true)
@Mapping(target = "modificationCount", ignore = true)
@Mapping(target = "persisted", ignore = true)
@Mapping(target = "tenantId", ignore = true)
@Mapping(target = "chat", ignore = true)
public abstract Message createMessage(CreateMessageDTO dto);
Message createMessage(CreateMessageDTO dto);

@Mapping(target = "id", ignore = true)
@Mapping(target = "controlTraceabilityManual", ignore = true)
@Mapping(target = "modificationCount", ignore = true)
@Mapping(target = "persisted", ignore = true)
@Mapping(target = "tenantId", ignore = true)
@Mapping(target = "chat", ignore = true)
public abstract Participant addParticipant(AddParticipantDTO dto);
Participant addParticipant(AddParticipantDTO dto);

@IterableMapping(qualifiedByName = "mapSingleChat")
public abstract List<ChatDTO> map(Stream<Chat> entity);
List<ChatDTO> map(Stream<Chat> entity);

@Mapping(target = "version", source = "modificationCount")
@Mapping(target = "removeParticipantsItem", ignore = true)
@Mapping(target = "participants", ignore = true)
@Named("mapSingleChat")
public abstract ChatDTO mapSingleChat(Chat chat);
ChatDTO mapSingleChat(Chat chat);

@Mapping(target = "version", source = "modificationCount")
@Mapping(target = "removeParticipantsItem", ignore = true)
public abstract ChatDTO mapChat(Chat chat);
ChatDTO mapChat(Chat chat);

@Mapping(target = "version", source = "modificationCount")
public abstract ParticipantDTO mapParticipant(Participant participant);
ParticipantDTO mapParticipant(Participant participant);

public abstract List<ParticipantDTO> mapParticipantList(List<Participant> items);
List<ParticipantDTO> mapParticipantList(List<Participant> items);

@Mapping(target = "version", source = "modificationCount")
public abstract MessageDTO map(Message message);
MessageDTO map(Message message);

public abstract List<MessageDTO> mapMessageList(List<Message> items);
List<MessageDTO> mapMessageList(List<Message> items);

@Mapping(target = "controlTraceabilityManual", ignore = true)
@Mapping(target = "modificationCount", ignore = true)
@Mapping(target = "persisted", ignore = true)
@Mapping(target = "tenantId", ignore = true)
@Mapping(target = "chat", ignore = true)
public abstract Participant mapParticipant(ParticipantDTO participantDTO);
Participant mapParticipant(ParticipantDTO participantDTO);

@Mapping(target = "id", ignore = true)
@Mapping(target = "creationDate", ignore = true)
Expand All @@ -158,33 +107,33 @@ public Set<Participant> mapParticipants(List<ParticipantDTO> participantDTOs) {
@Mapping(target = "tenantId", ignore = true)
@Mapping(target = "messages", ignore = true)
@Mapping(target = "participants", ignore = true)
public abstract void update(UpdateChatDTO chatDTO, @MappingTarget Chat entity);
void update(UpdateChatDTO chatDTO, @MappingTarget Chat entity);

@Mapping(target = "id", ignore = true)
@Mapping(target = "controlTraceabilityManual", ignore = true)
@Mapping(target = "modificationCount", ignore = true)
@Mapping(target = "persisted", ignore = true)
@Mapping(target = "tenantId", ignore = true)
@Mapping(target = "messages", ignore = true)
public abstract Chat map(UpdateChatDTO object);
Chat map(UpdateChatDTO object);

@Mapping(source = "id", target = "conversationId")
@Mapping(source = "messages", target = "history")
@Mapping(target = "conversationType", ignore = true)
public abstract Conversation mapChat2Conversation(Chat object);
Conversation mapChat2Conversation(Chat object);

@Mapping(source = "id", target = "conversationId")
@Mapping(source = "text", target = "message")
public abstract ChatMessage mapMessage(Message object);
ChatMessage mapMessage(Message object);

@ValueMapping(source = "HUMAN", target = "USER")
public abstract TypeEnum mapMessage(MessageType object);
TypeEnum mapMessage(MessageType object);

public Long mapLocalDateTime(LocalDateTime time) {
static Long mapLocalDateTime(LocalDateTime time) {
return time.toEpochSecond(ZoneOffset.UTC);
}

public LocalDateTime mapLongTime(Long timestamp) {
static LocalDateTime mapLongTime(Long timestamp) {
return Instant.ofEpochSecond(timestamp).atZone(ZoneId.systemDefault()).toLocalDateTime();
}

Expand All @@ -201,20 +150,6 @@ public LocalDateTime mapLongTime(Long timestamp) {
@Mapping(target = "tenantId", ignore = true)
@Mapping(target = "text", source = "message")
@Mapping(target = "userName", ignore = true)
public abstract Message mapAiSvcMessage(ChatMessage chatResponse);

@Named("properties")
public String mapToString(Object properties) {

if (properties == null) {
return null;
}

try {
return mapper.writeValueAsString(properties);
} catch (JsonProcessingException e) {
return null;
}
}
Message mapAiSvcMessage(ChatMessage chatResponse);

}
Loading

0 comments on commit cffc87a

Please sign in to comment.