Skip to content

Commit 07c9a40

Browse files
opensearch-trigger-bot[bot]github-actions[bot]paulstn
authored
[Backport 2.x] (query assist) remove caching agent id (#1734)
(cherry picked from commit 01bec05) Signed-off-by: Joshua Li <[email protected]> Signed-off-by: Paul Sebastian <[email protected]> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Paul Sebastian <[email protected]>
1 parent 844ff5c commit 07c9a40

File tree

3 files changed

+19
-82
lines changed

3 files changed

+19
-82
lines changed

server/routes/query_assist/routes.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
import { isResponseError } from '../../../../../src/core/server/opensearch/client/errors';
1313
import { ERROR_DETAILS, QUERY_ASSIST_API } from '../../../common/constants/query_assist';
1414
import { generateFieldContext } from '../../common/helpers/query_assist/generate_field_context';
15-
import { getAgentIdByConfig, requestWithRetryAgentSearch } from './utils/agents';
15+
import { getAgentIdByConfig, getAgentIdAndRequest } from './utils/agents';
1616
import { AGENT_CONFIGS } from './utils/constants';
1717

1818
export function registerQueryAssistRoutes(router: IRouter) {
@@ -57,7 +57,7 @@ export function registerQueryAssistRoutes(router: IRouter) {
5757
): Promise<IOpenSearchDashboardsResponse<any | ResponseError>> => {
5858
const client = context.core.opensearch.client.asCurrentUser;
5959
try {
60-
const pplRequest = await requestWithRetryAgentSearch({
60+
const pplRequest = await getAgentIdAndRequest({
6161
client,
6262
configName: AGENT_CONFIGS.PPL_AGENT,
6363
body: {
@@ -118,7 +118,7 @@ export function registerQueryAssistRoutes(router: IRouter) {
118118

119119
try {
120120
if (!isError) {
121-
summaryRequest = await requestWithRetryAgentSearch({
121+
summaryRequest = await getAgentIdAndRequest({
122122
client,
123123
configName: AGENT_CONFIGS.RESPONSE_SUMMARY_AGENT,
124124
body: {
@@ -131,7 +131,7 @@ export function registerQueryAssistRoutes(router: IRouter) {
131131
client.search({ index, size: 1 }),
132132
]);
133133
const fields = generateFieldContext(mappings, sampleDoc);
134-
summaryRequest = await requestWithRetryAgentSearch({
134+
summaryRequest = await getAgentIdAndRequest({
135135
client,
136136
configName: AGENT_CONFIGS.ERROR_SUMMARY_AGENT,
137137
body: {

server/routes/query_assist/utils/__tests__/agents.test.ts

+3-50
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { ApiResponse } from '@opensearch-project/opensearch';
77
import { ResponseError } from '@opensearch-project/opensearch/lib/errors';
88
import { CoreRouteHandlerContext } from '../../../../../../../src/core/server/core_route_handler_context';
99
import { coreMock, httpServerMock } from '../../../../../../../src/core/server/mocks';
10-
import { agentIdMap, getAgentIdByConfig, requestWithRetryAgentSearch } from '../agents';
10+
import { getAgentIdByConfig, getAgentIdAndRequest } from '../agents';
1111

1212
describe('Agents helper functions', () => {
1313
const coreContext = new CoreRouteHandlerContext(
@@ -75,27 +75,7 @@ describe('Agents helper functions', () => {
7575
);
7676
});
7777

78-
it('requests with valid agent id', async () => {
79-
agentIdMap.test_agent = 'test-id';
80-
mockedTransport.mockResolvedValueOnce({
81-
body: { inference_results: [{ output: [{ result: 'test response' }] }] },
82-
});
83-
const response = await requestWithRetryAgentSearch({
84-
client,
85-
configName: 'test_agent',
86-
shouldRetryAgentSearch: true,
87-
body: { parameters: { param1: 'value1' } },
88-
});
89-
expect(mockedTransport).toBeCalledWith(
90-
expect.objectContaining({
91-
path: '/_plugins/_ml/agents/test-id/_execute',
92-
}),
93-
expect.anything()
94-
);
95-
expect(response.body.inference_results[0].output[0].result).toEqual('test response');
96-
});
97-
98-
it('searches for agent id if id is undefined', async () => {
78+
it('searches for agent id and sends request', async () => {
9979
mockedTransport
10080
.mockResolvedValueOnce({
10181
body: {
@@ -106,36 +86,9 @@ describe('Agents helper functions', () => {
10686
.mockResolvedValueOnce({
10787
body: { inference_results: [{ output: [{ result: 'test response' }] }] },
10888
});
109-
const response = await requestWithRetryAgentSearch({
89+
const response = await getAgentIdAndRequest({
11090
client,
11191
configName: 'new_agent',
112-
shouldRetryAgentSearch: true,
113-
body: { parameters: { param1: 'value1' } },
114-
});
115-
expect(mockedTransport).toBeCalledWith(
116-
expect.objectContaining({ path: '/_plugins/_ml/agents/new-id/_execute' }),
117-
expect.anything()
118-
);
119-
expect(response.body.inference_results[0].output[0].result).toEqual('test response');
120-
});
121-
122-
it('searches for agent id if id is not found', async () => {
123-
agentIdMap.test_agent = 'non-exist-agent';
124-
mockedTransport
125-
.mockRejectedValueOnce({ statusCode: 404, body: {}, headers: {} })
126-
.mockResolvedValueOnce({
127-
body: {
128-
type: 'agent',
129-
configuration: { agent_id: 'new-id' },
130-
},
131-
})
132-
.mockResolvedValueOnce({
133-
body: { inference_results: [{ output: [{ result: 'test response' }] }] },
134-
});
135-
const response = await requestWithRetryAgentSearch({
136-
client,
137-
configName: 'test_agent',
138-
shouldRetryAgentSearch: true,
13992
body: { parameters: { param1: 'value1' } },
14093
});
14194
expect(mockedTransport).toBeCalledWith(

server/routes/query_assist/utils/agents.ts

+12-28
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
*/
55

66
import { ApiResponse } from '@opensearch-project/opensearch/.';
7-
import { RequestBody } from '@opensearch-project/opensearch/lib/Transport';
7+
import { RequestBody, TransportRequestPromise } from '@opensearch-project/opensearch/lib/Transport';
88
import { OpenSearchClient } from '../../../../../../src/core/server';
9-
import { isResponseError } from '../../../../../../src/core/server/opensearch/client/errors';
109
import { ML_COMMONS_API_PREFIX } from '../../../../common/constants/query_assist';
1110

1211
const AGENT_REQUEST_OPTIONS = {
@@ -27,8 +26,6 @@ type AgentResponse = ApiResponse<{
2726
}>;
2827
}>;
2928

30-
export const agentIdMap: Record<string, string> = {};
31-
3229
export const getAgentIdByConfig = async (
3330
opensearchClient: OpenSearchClient,
3431
configName: string
@@ -49,32 +46,19 @@ export const getAgentIdByConfig = async (
4946
}
5047
};
5148

52-
export const requestWithRetryAgentSearch = async (options: {
49+
export const getAgentIdAndRequest = async (options: {
5350
client: OpenSearchClient;
5451
configName: string;
55-
shouldRetryAgentSearch?: boolean;
5652
body: RequestBody;
5753
}): Promise<AgentResponse> => {
58-
const { client, configName, shouldRetryAgentSearch = true, body } = options;
59-
let retry = shouldRetryAgentSearch;
60-
if (!agentIdMap[configName]) {
61-
agentIdMap[configName] = await getAgentIdByConfig(client, configName);
62-
retry = false;
63-
}
64-
return client.transport
65-
.request(
66-
{
67-
method: 'POST',
68-
path: `${ML_COMMONS_API_PREFIX}/agents/${agentIdMap[configName]}/_execute`,
69-
body,
70-
},
71-
AGENT_REQUEST_OPTIONS
72-
)
73-
.catch(async (error) => {
74-
if (retry && isResponseError(error) && error.statusCode === 404) {
75-
agentIdMap[configName] = await getAgentIdByConfig(client, configName);
76-
return requestWithRetryAgentSearch({ ...options, shouldRetryAgentSearch: false });
77-
}
78-
return Promise.reject(error);
79-
}) as Promise<AgentResponse>;
54+
const { client, configName, body } = options;
55+
const agentId = await getAgentIdByConfig(client, configName);
56+
return client.transport.request(
57+
{
58+
method: 'POST',
59+
path: `${ML_COMMONS_API_PREFIX}/agents/${agentId}/_execute`,
60+
body,
61+
},
62+
AGENT_REQUEST_OPTIONS
63+
) as TransportRequestPromise<AgentResponse>;
8064
};

0 commit comments

Comments
 (0)