Skip to content

Commit

Permalink
[query assist] update api handler to accommodate new ml-commons confi…
Browse files Browse the repository at this point in the history
…g response (#2111) (#2121)

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



* update `type` field in ml-commons response



---------


(cherry picked from commit 6d128ea)

Signed-off-by: Joshua Li <[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>
  • Loading branch information
1 parent c34d97e commit 3ed6f74
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
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

0 comments on commit 3ed6f74

Please sign in to comment.