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

[Backport 2.x] Fix returns 500 error for AI agent APIs when OpenSearch returns 4xx error with json format error message #381

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions server/routes/agent_routes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { Boom } from '@hapi/boom';
import { OpenSearchDashboardsRequest, Router } from '../../../../src/core/server/http/router';
import { enhanceWithContext, triggerHandler } from './router.mock';
import { resetMocks } from '../services/storage/agent_framework_storage_service.mock';
import { httpServerMock } from '../../../../src/core/server/http/http_server.mocks';
import { loggerMock } from '../../../../src/core/server/logging/logger.mock';
import { AGENT_API } from '../../common/constants/llm';
import { AssistantClient } from '../services/assistant_client';
import { RequestHandlerContext } from '../../../../src/core/server';
import { registerAgentRoutes } from './agent_routes';
const mockedLogger = loggerMock.create();

export const createMockedAssistantClient = (
request: OpenSearchDashboardsRequest
): AssistantClient => {
return new AssistantClient(request, {} as RequestHandlerContext);
};

const mockedAssistantClient = createMockedAssistantClient({} as OpenSearchDashboardsRequest);

describe('test execute agent route', () => {
const router = new Router(
'',
mockedLogger,
enhanceWithContext({
assistant_plugin: {
logger: mockedLogger,
},
})
);
registerAgentRoutes(router, {
getScopedClient: jest.fn(
(request: OpenSearchDashboardsRequest, context: RequestHandlerContext) => {
return mockedAssistantClient;
}
),
});
const executeAgentRequest = (payload: {}, query: {}) =>
triggerHandler(router, {
method: 'post',
path: AGENT_API.EXECUTE,
req: httpServerMock.createRawRequest({
payload: JSON.stringify(payload),
query,
}),
});
beforeEach(() => {
loggerMock.clear(mockedLogger);
resetMocks();
});

it('return 4xx when execute agent throws 4xx error', async () => {
mockedAssistantClient.executeAgentByConfigName = jest.fn().mockRejectedValue({
statusCode: 429,
body: {
status: 429,
error: {
type: 'OpenSearchStatusException',
reason: 'System Error',
details: 'Request is throttled at model level.',
},
},
});
const result = (await executeAgentRequest(
{},
{
agentConfigName: 'os_insight',
}
)) as Boom;
expect(result.output).toMatchInlineSnapshot(`
Object {
"headers": Object {},
"payload": Object {
"error": "Too Many Requests",
"message": "{\\"status\\":429,\\"error\\":{\\"type\\":\\"OpenSearchStatusException\\",\\"reason\\":\\"System Error\\",\\"details\\":\\"Request is throttled at model level.\\"}}",
"statusCode": 429,
},
"statusCode": 429,
}
`);
});

it('return 4xx when executeAgent throws 4xx error in string format', async () => {
mockedAssistantClient.executeAgentByConfigName = jest.fn().mockRejectedValue({
statusCode: 429,
body: 'Request is throttled at model level',
});
const result = (await executeAgentRequest(
{},
{
agentConfigName: 'os_insight',
}
)) as Boom;
expect(result.output).toMatchInlineSnapshot(`
Object {
"headers": Object {},
"payload": Object {
"error": "Too Many Requests",
"message": "Request is throttled at model level",
"statusCode": 429,
},
"statusCode": 429,
}
`);
});

it('return 5xx when executeAgent throws 5xx error', async () => {
mockedAssistantClient.executeAgentByConfigName = jest.fn().mockRejectedValue({
statusCode: 500,
body: 'Server error',
});
const result = (await executeAgentRequest(
{},
{
agentConfigName: 'os_insight',
}
)) as Boom;
expect(result.output).toMatchInlineSnapshot(`
Object {
"headers": Object {},
"payload": Object {
"error": "Internal Server Error",
"message": "Execute agent failed!",
"statusCode": 500,
},
"statusCode": 500,
}
`);
});
});
4 changes: 1 addition & 3 deletions server/routes/agent_routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,13 @@ export function registerAgentRoutes(router: IRouter, assistantService: Assistant
context.assistant_plugin.logger.error('Execute agent failed!', e);
if (e.statusCode >= 400 && e.statusCode <= 499) {
return res.customError({
body: e.body,
body: { message: typeof e.body === 'string' ? e.body : JSON.stringify(e.body) },
statusCode: e.statusCode,
headers: e.headers,
});
} else {
return res.customError({
body: 'Execute agent failed!',
statusCode: 500,
headers: e.headers,
});
}
}
Expand Down
Loading
Loading