.
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Copyright 2025 Google. This software is provided as-is, without warranty or representation for any use or purpose.
|
||||
* Your use of it is subject to your agreement with Google.
|
||||
*/
|
||||
|
||||
package com.example.mapper.conversation;
|
||||
|
||||
import com.example.dto.dialogflow.base.DetectIntentRequestDTO;
|
||||
import com.example.dto.dialogflow.conversation.QueryInputDTO;
|
||||
import com.example.util.ProtobufUtil;
|
||||
import com.google.cloud.dialogflow.cx.v3.EventInput;
|
||||
import com.google.cloud.dialogflow.cx.v3.DetectIntentRequest;
|
||||
|
||||
import com.google.cloud.dialogflow.cx.v3.QueryInput;
|
||||
import com.google.cloud.dialogflow.cx.v3.QueryParameters;
|
||||
import com.google.cloud.dialogflow.cx.v3.TextInput;
|
||||
import com.google.protobuf.Struct;
|
||||
import com.google.protobuf.Value;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Spring component responsible for mapping a custom `DetectIntentRequestDTO`
|
||||
* into a Dialogflow CX `DetectIntentRequest.Builder`. It handles the conversion
|
||||
* of user text or event inputs and the serialization of custom session parameters,
|
||||
* ensuring the data is in the correct Protobuf format for API communication.
|
||||
*/
|
||||
@Component
|
||||
public class DialogflowRequestMapper {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(DialogflowRequestMapper.class);
|
||||
|
||||
@org.springframework.beans.factory.annotation.Value("${dialogflow.default-language-code:es}")
|
||||
String defaultLanguageCode;
|
||||
|
||||
public DetectIntentRequest.Builder mapToDetectIntentRequestBuilder(DetectIntentRequestDTO requestDto) {
|
||||
Objects.requireNonNull(requestDto, "DetectIntentRequestDTO cannot be null for mapping.");
|
||||
|
||||
logger.debug(
|
||||
"Building partial Dialogflow CX DetectIntentRequest Protobuf Builder from DTO (only QueryInput and QueryParams).");
|
||||
QueryInput.Builder queryInputBuilder = QueryInput.newBuilder();
|
||||
QueryInputDTO queryInputDTO = requestDto.queryInput();
|
||||
|
||||
String languageCodeToSet = (queryInputDTO.languageCode() != null
|
||||
&& !queryInputDTO.languageCode().trim().isEmpty())
|
||||
? queryInputDTO.languageCode()
|
||||
: defaultLanguageCode;
|
||||
queryInputBuilder.setLanguageCode(languageCodeToSet);
|
||||
logger.debug("Setting languageCode for QueryInput to: {}", languageCodeToSet);
|
||||
|
||||
if (queryInputDTO.text() != null && queryInputDTO.text().text() != null
|
||||
&& !queryInputDTO.text().text().trim().isEmpty()) {
|
||||
queryInputBuilder.setText(TextInput.newBuilder()
|
||||
.setText(queryInputDTO.text().text())
|
||||
.build());
|
||||
logger.debug("Mapped text input for QueryInput: '{}'", queryInputDTO.text().text());
|
||||
|
||||
} else if (queryInputDTO.event() != null && queryInputDTO.event().event() != null
|
||||
&& !queryInputDTO.event().event().trim().isEmpty()) {
|
||||
queryInputBuilder.setEvent(EventInput.newBuilder()
|
||||
.setEvent(queryInputDTO.event().event())
|
||||
.build());
|
||||
logger.debug("Mapped event input for QueryInput: '{}'", queryInputDTO.event().event());
|
||||
|
||||
} else {
|
||||
logger.error("Dialogflow query input (either text or event) is required and must not be empty.");
|
||||
throw new IllegalArgumentException("Dialogflow query input (either text or event) is required.");
|
||||
}
|
||||
|
||||
QueryParameters.Builder queryParametersBuilder = QueryParameters.newBuilder();
|
||||
Struct.Builder paramsStructBuilder = Struct.newBuilder();
|
||||
|
||||
if (requestDto.queryParams() != null && requestDto.queryParams().parameters() != null) {
|
||||
for (Map.Entry<String, Object> entry : requestDto.queryParams().parameters().entrySet()) {
|
||||
Value protobufValue = ProtobufUtil.convertJavaObjectToProtobufValue(entry.getValue());
|
||||
paramsStructBuilder.putFields(entry.getKey(), protobufValue);
|
||||
logger.debug("Added session parameter from DTO queryParams: Key='{}', Value='{}'",
|
||||
entry.getKey(),entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
if (paramsStructBuilder.getFieldsCount() > 0) {
|
||||
queryParametersBuilder.setParameters(paramsStructBuilder.build());
|
||||
logger.debug("All custom session parameters added to Protobuf request builder.");
|
||||
} else {
|
||||
logger.debug("No custom session parameters to add to Protobuf request.");
|
||||
}
|
||||
|
||||
DetectIntentRequest.Builder detectIntentRequestBuilder = DetectIntentRequest.newBuilder()
|
||||
.setQueryInput(queryInputBuilder.build());
|
||||
|
||||
if (queryParametersBuilder.hasParameters()) {
|
||||
detectIntentRequestBuilder.setQueryParams(queryParametersBuilder.build());
|
||||
}
|
||||
|
||||
logger.debug("Finished building partial DetectIntentRequest Protobuf Builder.");
|
||||
return detectIntentRequestBuilder;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user