-
Notifications
You must be signed in to change notification settings - Fork 4.5k
chore: refactor the appsmith-ai plugin trigger #38303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...-plugins/appsmithAiPlugin/src/main/java/com/external/plugins/services/TriggerService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.external.plugins.services; | ||
|
|
||
| import com.appsmith.external.helpers.restApiUtils.connections.APIConnection; | ||
| import com.appsmith.external.models.DatasourceConfiguration; | ||
| import com.appsmith.external.models.TriggerRequestDTO; | ||
| import com.appsmith.external.models.TriggerResultDTO; | ||
| import reactor.core.publisher.Mono; | ||
|
|
||
| public interface TriggerService { | ||
| Mono<TriggerResultDTO> executeTrigger( | ||
| APIConnection connection, DatasourceConfiguration datasourceConfiguration, TriggerRequestDTO request); | ||
| } |
100 changes: 100 additions & 0 deletions
100
...ns/appsmithAiPlugin/src/main/java/com/external/plugins/services/TriggerServiceCEImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package com.external.plugins.services; | ||
|
|
||
| import com.appsmith.external.exceptions.pluginExceptions.AppsmithPluginError; | ||
| import com.appsmith.external.exceptions.pluginExceptions.AppsmithPluginException; | ||
| import com.appsmith.external.helpers.restApiUtils.connections.APIConnection; | ||
| import com.appsmith.external.models.DatasourceConfiguration; | ||
| import com.appsmith.external.models.TriggerRequestDTO; | ||
| import com.appsmith.external.models.TriggerResultDTO; | ||
| import com.external.plugins.dtos.SourceDetails; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import reactor.core.publisher.Mono; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import static com.appsmith.external.constants.CommonFieldName.VALUE; | ||
| import static com.external.plugins.constants.AppsmithAiConstants.DISABLED; | ||
| import static com.external.plugins.constants.AppsmithAiConstants.LABEL; | ||
| import static com.external.plugins.constants.AppsmithAiConstants.LIST_FILES; | ||
| import static com.external.plugins.constants.AppsmithAiConstants.UPLOAD_FILES; | ||
| import static com.external.plugins.utils.FileUtils.getFileIds; | ||
|
|
||
| @Slf4j | ||
| public class TriggerServiceCEImpl implements TriggerService { | ||
|
|
||
| private final AiServerService aiServerService; | ||
| private final ObjectMapper objectMapper; | ||
|
|
||
| public TriggerServiceCEImpl(AiServerService aiServerService, ObjectMapper objectMapper) { | ||
| this.aiServerService = aiServerService; | ||
| this.objectMapper = objectMapper; | ||
| } | ||
|
|
||
| @Override | ||
| public Mono<TriggerResultDTO> executeTrigger( | ||
| APIConnection connection, DatasourceConfiguration datasourceConfiguration, TriggerRequestDTO request) { | ||
| String requestType = request.getRequestType(); | ||
| return switch (requestType) { | ||
| case UPLOAD_FILES -> this.uploadFiles(request); | ||
| case LIST_FILES -> this.listFiles(datasourceConfiguration, request); | ||
| default -> Mono.empty(); | ||
| }; | ||
| } | ||
|
|
||
| private Mono<TriggerResultDTO> uploadFiles(TriggerRequestDTO request) { | ||
| SourceDetails sourceDetails = SourceDetails.createSourceDetails(request); | ||
| return aiServerService | ||
| .uploadFiles(request.getFiles(), sourceDetails) | ||
| .flatMap(response -> { | ||
| TriggerResultDTO triggerResultDTO = new TriggerResultDTO(); | ||
| triggerResultDTO.setTrigger(response); | ||
| return Mono.just(triggerResultDTO); | ||
| }) | ||
| .onErrorResume(error -> handleError("An error has occurred while trying to upload files", error)); | ||
| } | ||
|
|
||
| private Mono<TriggerResultDTO> listFiles( | ||
| DatasourceConfiguration datasourceConfiguration, TriggerRequestDTO request) { | ||
| SourceDetails sourceDetails = SourceDetails.createSourceDetails(request); | ||
| List<String> fileIds = getFileIds(datasourceConfiguration); | ||
| if (fileIds.isEmpty()) { | ||
| TriggerResultDTO triggerResultDTO = new TriggerResultDTO(); | ||
| triggerResultDTO.setTrigger(List.of(Map.of( | ||
| DISABLED, true, LABEL, "No files available in the datasource", VALUE, "NO_FILES_AVAILABLE"))); | ||
| return Mono.just(triggerResultDTO); | ||
| } | ||
| return aiServerService | ||
| .getFilesStatus(fileIds, sourceDetails) | ||
| .flatMap(fileStatusDTO -> { | ||
| List<Map<String, Object>> response = new ArrayList<>(); | ||
| fileStatusDTO.getFiles().forEach(file -> { | ||
| Map<String, Object> dropdownOption = new HashMap<>(); | ||
| if (!file.isProcessed()) { | ||
| dropdownOption.put(LABEL, "(Processing...) " + file.getName()); | ||
| } else { | ||
| dropdownOption.put(LABEL, file.getName()); | ||
| } | ||
| dropdownOption.put(VALUE, file.getId()); | ||
| dropdownOption.put(DISABLED, !file.isProcessed()); | ||
| response.add(dropdownOption); | ||
| }); | ||
| TriggerResultDTO triggerResultDTO = new TriggerResultDTO(); | ||
| triggerResultDTO.setTrigger(response); | ||
| return Mono.just(triggerResultDTO); | ||
| }) | ||
| .onErrorResume( | ||
| error -> handleError("An error has occurred while trying to list uploaded files", error)); | ||
| } | ||
|
|
||
| protected Mono<TriggerResultDTO> handleError(String message, Throwable error) { | ||
| log.error("{}. Error: {}", message, error.getMessage()); | ||
| if (!(error instanceof AppsmithPluginException)) { | ||
| error = new AppsmithPluginException(AppsmithPluginError.PLUGIN_ERROR, error.getMessage(), error); | ||
| } | ||
| return Mono.error(error); | ||
| } | ||
| } | ||
21 changes: 21 additions & 0 deletions
21
...gins/appsmithAiPlugin/src/main/java/com/external/plugins/services/TriggerServiceImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package com.external.plugins.services; | ||
|
|
||
| import com.appsmith.external.helpers.restApiUtils.connections.APIConnection; | ||
| import com.appsmith.external.models.DatasourceConfiguration; | ||
| import com.appsmith.external.models.TriggerRequestDTO; | ||
| import com.appsmith.external.models.TriggerResultDTO; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import reactor.core.publisher.Mono; | ||
|
|
||
| public class TriggerServiceImpl extends TriggerServiceCEImpl { | ||
|
|
||
| public TriggerServiceImpl(AiServerService aiServerService, ObjectMapper objectMapper) { | ||
| super(aiServerService, objectMapper); | ||
| } | ||
|
|
||
| @Override | ||
| public Mono<TriggerResultDTO> executeTrigger( | ||
| APIConnection connection, DatasourceConfiguration datasourceConfiguration, TriggerRequestDTO request) { | ||
| return super.executeTrigger(connection, datasourceConfiguration, request); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Handle unknown request types in the switch statement.
Returning Mono.empty() for unrecognized request types might silently mask errors. Consider either returning a descriptive error or logging an alert to help surface unexpected request types.
default -> { - return Mono.empty(); + return Mono.error(new AppsmithPluginException( + AppsmithPluginError.PLUGIN_ERROR, + "Unknown request type: " + requestType + )); }📝 Committable suggestion