Skip to content

Commit

Permalink
Merge branch 'main' into messageAction
Browse files Browse the repository at this point in the history
  • Loading branch information
000FLMS authored Dec 27, 2024
2 parents d862e46 + ffa509b commit ef58c50
Show file tree
Hide file tree
Showing 7 changed files with 702 additions and 20 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

### Bug Fixes

- Optimize the response of AI agent APIs ([#373](https://github.com/opensearch-project/dashboards-assistant/pull/373))
- Optimize the response of AI agent APIs ([#373](https://github.com/opensearch-project/dashboards-assistant/pull/373), [#380](https://github.com/opensearch-project/dashboards-assistant/pull/380))
- fixed incorrect message id field used ([#378](https://github.com/opensearch-project/dashboards-assistant/pull/378))

### Infrastructure
Expand Down
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

0 comments on commit ef58c50

Please sign in to comment.