Skip to content

Commit

Permalink
feat(metadata-sidebar): Accommodate changes to ai/extract_structured …
Browse files Browse the repository at this point in the history
…API (#3849)

* feat(metadata-sidebar): Accommodate changes to ai/extract_structured API

* feat(metadata-sidebar): Accommodate changes to ai/extract_structured API

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
wpiesiak and mergify[bot] authored Jan 17, 2025
1 parent f39a65e commit 2a46442
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
5 changes: 3 additions & 2 deletions src/api/Intelligence.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* @author Box
*/

import getProp from 'lodash/get';
import type { QuestionType } from '@box/box-ai-content-answers';
import Base from './Base';
import { AiExtractResponse } from './schemas/AiExtractResponse';
Expand Down Expand Up @@ -86,7 +85,9 @@ class Intelligence extends Base {
id: `file_${item.id}`,
});

return getProp(suggestionsResponse, 'data');
return !!suggestionsResponse?.data?.answer && typeof suggestionsResponse.data.answer === 'object'
? suggestionsResponse.data.answer
: suggestionsResponse.data;
}
}

Expand Down
37 changes: 19 additions & 18 deletions src/api/__tests__/Intelligence.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,25 +124,26 @@ describe('api/Intelligence', () => {
}
});

test('should return a successful response including the answer from the LLM', async () => {
const suggestionsFromServer = {
stringFieldKey: 'fieldVal1',
floatFieldKey: 124.0,
enumFieldKey: 'EnumOptionKey',
multiSelectFieldKey: ['multiSelectOption1', 'multiSelectOption5'],
};
intelligence.xhr.post = jest.fn().mockReturnValueOnce({
data: suggestionsFromServer,
});
test.each`
suggestionsFromServer | responseData
${{ stringFieldKey: 'fieldVal1', floatFieldKey: 124.0, enumFieldKey: 'EnumOptionKey', multiSelectFieldKey: ['multiSelectOption1', 'multiSelectOption5'] }} | ${{ data: { stringFieldKey: 'fieldVal1', floatFieldKey: 124.0, enumFieldKey: 'EnumOptionKey', multiSelectFieldKey: ['multiSelectOption1', 'multiSelectOption5'] } }}
${{ stringFieldKey: 'fieldVal1', floatFieldKey: 124.0, enumFieldKey: 'EnumOptionKey', multiSelectFieldKey: ['multiSelectOption1', 'multiSelectOption5'] }} | ${{ data: { answer: { stringFieldKey: 'fieldVal1', floatFieldKey: 124.0, enumFieldKey: 'EnumOptionKey', multiSelectFieldKey: ['multiSelectOption1', 'multiSelectOption5'] }, create_at: '2025-01-14T00:00:00-00:00' } }}
${{}} | ${{ data: {} }}
${{}} | ${{ data: { answer: {}, create_at: '2025-01-14T00:00:00-00:00' } }}
`(
'should return a successful response including the answer from the LLM',
async ({ suggestionsFromServer, responseData }) => {
intelligence.xhr.post = jest.fn().mockReturnValueOnce(responseData);

const suggestions = await intelligence.extractStructured(request);
expect(suggestions).toEqual(suggestionsFromServer);
expect(intelligence.xhr.post).toHaveBeenCalledWith({
url: `${intelligence.getBaseApiUrl()}/ai/extract_structured`,
id: 'file_123',
data: request,
});
});
const suggestions = await intelligence.extractStructured(request);
expect(suggestions).toEqual(suggestionsFromServer);
expect(intelligence.xhr.post).toHaveBeenCalledWith({
url: `${intelligence.getBaseApiUrl()}/ai/extract_structured`,
id: 'file_123',
data: request,
});
},
);

test('should not return any suggestions when error is 400', async () => {
const error = new Error();
Expand Down

0 comments on commit 2a46442

Please sign in to comment.