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

[query assist] update api handler to accommodate new ml-commons config response #2111

Merged
merged 2 commits into from
Sep 4, 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
19 changes: 19 additions & 0 deletions server/routes/query_assist/utils/__tests__/agents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ describe('Agents helper functions', () => {
`);
});

it('acccepts ml_configuration key in get agent response', async () => {
mockedTransport.mockResolvedValueOnce({
body: {
config_type: 'agent',
ml_configuration: { agent_id: 'agentId' },
},
});
const id = await getAgentIdByConfig(client, 'test_agent');
expect(id).toEqual('agentId');
expect(mockedTransport.mock.calls[0]).toMatchInlineSnapshot(`
Array [
Object {
"method": "GET",
"path": "/_plugins/_ml/config/test_agent",
},
]
`);
});

it('handles not found errors', async () => {
mockedTransport.mockRejectedValueOnce(
new ResponseError(({
Expand Down
14 changes: 11 additions & 3 deletions server/routes/query_assist/utils/agents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,20 @@ export const getAgentIdByConfig = async (
const response = (await opensearchClient.transport.request({
method: 'GET',
path: `${ML_COMMONS_API_PREFIX}/config/${configName}`,
})) as ApiResponse<{ type: string; configuration: { agent_id?: string } }>;
})) as ApiResponse<{
type?: string;
config_type?: string;
ml_configuration?: { agent_id?: string };
configuration?: { agent_id?: string };
}>;

if (!response || response.body.configuration.agent_id === undefined) {
if (
!response ||
!(response.body.ml_configuration?.agent_id || response.body.configuration?.agent_id)
) {
throw new Error('cannot find any agent by configuration: ' + configName);
}
return response.body.configuration.agent_id;
return (response.body.ml_configuration?.agent_id || response.body.configuration?.agent_id)!;
} catch (error) {
const errorMessage = JSON.stringify(error.meta?.body) || error;
throw new Error(`Get agent '${configName}' failed, reason: ` + errorMessage);
Expand Down
Loading