diff --git a/ee/app/assets/javascripts/ai/tanuki_bot/store/actions.js b/ee/app/assets/javascripts/ai/tanuki_bot/store/actions.js index 6c53f9b4b3269ece7df2867d8ece4a54d193c043..9fc6bbe1f3c1d7b41dcf28e5d12cb3b38c332e86 100644 --- a/ee/app/assets/javascripts/ai/tanuki_bot/store/actions.js +++ b/ee/app/assets/javascripts/ai/tanuki_bot/store/actions.js @@ -20,30 +20,31 @@ export const receiveMutationResponse = ({ commit, dispatch }, { data, message }) export const receiveTanukiBotMessage = async ({ commit, dispatch }, data) => { const { errors = [], responseBody } = data.aiCompletionResponse || {}; + let parsedResponse; + try { + parsedResponse = JSON.parse(responseBody); + } catch { + parsedResponse = { content: responseBody }; + } + if (errors?.length) { - dispatch('tanukiBotMessageError'); + dispatch('tanukiBotMessageError', parsedResponse); } else if (responseBody) { commit(types.SET_LOADING, false); - let parsedResponse; - try { - parsedResponse = JSON.parse(responseBody); - } catch { - parsedResponse = { content: responseBody }; - } commit(types.ADD_TANUKI_MESSAGE, parsedResponse); } }; -export const tanukiBotMessageError = ({ commit }) => { +export const tanukiBotMessageError = ({ commit }, data) => { commit(types.SET_LOADING, false); - commit(types.ADD_ERROR_MESSAGE); + commit(types.ADD_ERROR_MESSAGE, data); }; export const setMessages = ({ commit, dispatch }, messages) => { messages.forEach((msg) => { if (msg.errors?.length) { - dispatch('tanukiBotMessageError'); + dispatch('tanukiBotMessageError', msg); } else { switch (msg.role.toLowerCase()) { case MESSAGE_TYPES.USER: diff --git a/ee/app/assets/javascripts/ai/tanuki_bot/store/mutations.js b/ee/app/assets/javascripts/ai/tanuki_bot/store/mutations.js index 0146370a17385ec6d44c3d3ecdf3550798a638fc..f3f9f1ca4be2f1d3ac5506648667094961c8508c 100644 --- a/ee/app/assets/javascripts/ai/tanuki_bot/store/mutations.js +++ b/ee/app/assets/javascripts/ai/tanuki_bot/store/mutations.js @@ -30,11 +30,11 @@ export default { }); } }, - [types.ADD_ERROR_MESSAGE](state) { + [types.ADD_ERROR_MESSAGE](state, msg) { state.messages.push({ id: state.messages.length, role: MESSAGE_TYPES.TANUKI, - content: '', + content: msg ? msg.content || msg.errors.join(' ') : '', errors: [ERROR_MESSAGE], }); }, diff --git a/ee/spec/frontend/ai/tanuki_bot/mock_data.js b/ee/spec/frontend/ai/tanuki_bot/mock_data.js index fed7ee89cf5122be960819ea5edc1bf9e0028f33..fab2e1cfc19fb9caee083203d7697109e419419e 100644 --- a/ee/spec/frontend/ai/tanuki_bot/mock_data.js +++ b/ee/spec/frontend/ai/tanuki_bot/mock_data.js @@ -46,13 +46,15 @@ export const GENERATE_MOCK_TANUKI_RES = (body = JSON.stringify(MOCK_TANUKI_MESSA export const MOCK_TANUKI_SUCCESS_RES = GENERATE_MOCK_TANUKI_RES(); -export const MOCK_TANUKI_ERROR_RES = { - data: { - aiCompletionResponse: { - responseBody: null, - errors: ['error'], +export const MOCK_TANUKI_ERROR_RES = (body = JSON.stringify(MOCK_TANUKI_MESSAGE)) => { + return { + data: { + aiCompletionResponse: { + responseBody: body, + errors: ['error'], + }, }, - }, + }; }; export const MOCK_CHAT_CACHED_MESSAGES_RES = { diff --git a/ee/spec/frontend/ai/tanuki_bot/store/actions_spec.js b/ee/spec/frontend/ai/tanuki_bot/store/actions_spec.js index 7476492db54996237a830acc3d25969ae0311762..fa9d142d376c6bb52f2d3d21eb73b5ce8fcade6f 100644 --- a/ee/spec/frontend/ai/tanuki_bot/store/actions_spec.js +++ b/ee/spec/frontend/ai/tanuki_bot/store/actions_spec.js @@ -81,11 +81,21 @@ describe('TanukiBot Store Actions', () => { describe('with error', () => { it(`should dispatch the correct actions`, () => { + const contentObject = { + content: '', + source: 'foo-bar', + }; + const stringifiedContent = JSON.stringify(contentObject); return testAction({ action: actions.receiveTanukiBotMessage, - payload: MOCK_TANUKI_ERROR_RES.data, + payload: MOCK_TANUKI_ERROR_RES(stringifiedContent).data, state, - expectedActions: [{ type: 'tanukiBotMessageError' }], + expectedActions: [ + { + payload: contentObject, + type: 'tanukiBotMessageError', + }, + ], }); }); }); @@ -172,7 +182,22 @@ describe('TanukiBot Store Actions', () => { }, ], state, - expectedActions: [{ type: 'tanukiBotMessageError' }, { type: 'tanukiBotMessageError' }], + expectedActions: [ + { + type: 'tanukiBotMessageError', + payload: { + ...MOCK_USER_MESSAGE, + errors: ['foo'], + }, + }, + { + type: 'tanukiBotMessageError', + payload: { + ...MOCK_TANUKI_MESSAGE, + errors: ['foo'], + }, + }, + ], }); }); }); diff --git a/ee/spec/frontend/ai/tanuki_bot/store/mutations_spec.js b/ee/spec/frontend/ai/tanuki_bot/store/mutations_spec.js index 5dd006aadde89d3ee1ac262404cac80d2ecb899d..57ac3061e025714bec5d855063a9ac9d187a1c0c 100644 --- a/ee/spec/frontend/ai/tanuki_bot/store/mutations_spec.js +++ b/ee/spec/frontend/ai/tanuki_bot/store/mutations_spec.js @@ -51,10 +51,15 @@ describe('TanukiBot Store Mutations', () => { describe('ADD_ERROR_MESSAGE', () => { it('pushes an error message to the messages array with type: Tanuki', () => { - mutations[types.ADD_ERROR_MESSAGE](state); + mutations[types.ADD_ERROR_MESSAGE](state, { ...MOCK_TANUKI_MESSAGE, msg: 'test' }); expect(state.messages).toStrictEqual([ - { id: 0, role: MESSAGE_TYPES.TANUKI, errors: [ERROR_MESSAGE], content: '' }, + { + id: 0, + role: MESSAGE_TYPES.TANUKI, + errors: [ERROR_MESSAGE], + content: 'Tanuki Bot message', + }, ]); }); });