Skip to content
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

Agent execution error in json format #2858

Merged
merged 1 commit into from
Aug 30, 2024

Conversation

jngz-es
Copy link
Collaborator

@jngz-es jngz-es commented Aug 29, 2024

Description

Return agent execution error in xcontent format. If xcontent building failed, use plain text.

Related Issues

Resolves #[Issue number to be closed when this PR is merged]

Check List

  • New functionality includes testing.
  • New functionality has been documented.
  • API changes companion pull request created.
  • Commits are signed per the DCO using --signoff.
  • Public documentation issue/PR created.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@jngz-es jngz-es had a problem deploying to ml-commons-cicd-env August 29, 2024 19:44 — with GitHub Actions Failure
@jngz-es jngz-es temporarily deployed to ml-commons-cicd-env August 29, 2024 21:12 — with GitHub Actions Inactive
verify(channel, times(1)).sendResponse(restResponseArgumentCaptor.capture());
BytesRestResponse response = (BytesRestResponse) restResponseArgumentCaptor.getValue();
assertEquals(RestStatus.BAD_REQUEST, response.status());
assertEquals("text/plain; charset=UTF-8", response.contentType());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Difference between these two tests are only assertEquals("application/json; charset=UTF-8", response.contentType()); vs assertEquals("text/plain; charset=UTF-8", response.contentType());. Is that expected?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is expected. Actually there is another difference when(channel.newBuilder()).thenReturn(XContentFactory.jsonBuilder());
Without this jsonBuilder, the xcontent building will fail, then downgrade to catch branch to send plain text response.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] I see. In that case, what do you think about something like following:

public void testAgentExecutionResponse() throws Exception {
    // Test JSON response
    runAgentExecutionResponseTest(
        "application/json; charset=UTF-8",
        "{\"status\":400,\"error\":{\"type\":\"IllegalArgumentException\",\"reason\":\"Invalid Request\",\"details\":\"Illegal Argument Exception\"}}",
        true
    );

    // Test plain text response
    runAgentExecutionResponseTest(
        "text/plain; charset=UTF-8",
        "{\"error\":{\"reason\":\"Invalid Request\",\"details\":\"Illegal Argument Exception\",\"type\":\"IllegalArgumentException\"},\"status\":400}",
        false
    );
}

private void runAgentExecutionResponseTest(String expectedContentType, String expectedError, boolean isJsonResponse) throws Exception {
    RestRequest request = getExecuteAgentRestRequest();
    
    // Mock client behavior
    doAnswer(invocation -> {
        ActionListener<MLExecuteTaskResponse> actionListener = invocation.getArgument(2);
        actionListener.onFailure(new RemoteTransportException("Remote Transport Exception", new IllegalArgumentException("Illegal Argument Exception")));
        return null;
    }).when(client).execute(eq(MLExecuteTaskAction.INSTANCE), any(), any());
    
    doNothing().when(channel).sendResponse(any());

    // Conditionally mock JSON response building
    if (isJsonResponse) {
        when(channel.newBuilder()).thenReturn(XContentFactory.jsonBuilder());
    }

    restMLExecuteAction.handleRequest(request, channel, client);

    // Verify request processing
    ArgumentCaptor<MLExecuteTaskRequest> argumentCaptor = ArgumentCaptor.forClass(MLExecuteTaskRequest.class);
    verify(client, times(1)).execute(eq(MLExecuteTaskAction.INSTANCE), argumentCaptor.capture(), any());
    Input input = argumentCaptor.getValue().getInput();
    assertEquals(FunctionName.AGENT, input.getFunctionName());

    // Verify response
    ArgumentCaptor<RestResponse> restResponseArgumentCaptor = ArgumentCaptor.forClass(RestResponse.class);
    verify(channel, times(1)).sendResponse(restResponseArgumentCaptor.capture());
    BytesRestResponse response = (BytesRestResponse) restResponseArgumentCaptor.getValue();
    assertEquals(RestStatus.BAD_REQUEST, response.status());
    assertEquals(expectedContentType, response.contentType());
    assertEquals(expectedError, response.content().utf8ToString());
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this looks good. Actually I would prefer to refactor all tests in RestMLExecuteActionTests in next pr, as some existing test cases have the similar improvement to do.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, approving the PR in expectation of a follow up PR to refactor the tests in RestMLExecuteActionTests

@jngz-es jngz-es temporarily deployed to ml-commons-cicd-env August 29, 2024 22:23 — with GitHub Actions Inactive
@jngz-es jngz-es merged commit 1da79ce into opensearch-project:main Aug 30, 2024
9 checks passed
opensearch-trigger-bot bot pushed a commit that referenced this pull request Aug 30, 2024
jngz-es added a commit that referenced this pull request Aug 30, 2024
…2868)

Signed-off-by: Jing Zhang <[email protected]>
(cherry picked from commit 1da79ce)

Co-authored-by: Jing Zhang <[email protected]>
@jngz-es jngz-es deleted the agent_response branch January 13, 2025 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants