@empathyco/x-components
Version:
Empathy X Components
101 lines (98 loc) • 3.9 kB
JavaScript
import { getTaggingInfoFromUrl } from '@empathyco/x-adapter-platform';
import '@vue/devtools-api';
import '../../../../plugins/devtools/timeline.devtools.js';
import '@empathyco/x-utils';
import 'rxjs/operators';
import 'rxjs';
import '../../../../plugins/devtools/colors.utils.js';
import '../../../../plugins/x-bus.js';
import { XPlugin } from '../../../../plugins/x-plugin.js';
/**
* Default implementation for the {@link AiActions.fetchAndSaveAiSuggestions}.
*
* @param _ - The {@link https://vuex.vuejs.org/guide/actions.html | context} of the actions,
* provided by Vuex.
* @param request - The AI request to make.
* @returns The AI response.
* @public
*/
const fetchAndSaveAiSuggestions = async ({ commit }, request) => {
if (!request) {
return;
}
commit('setSuggestionsLoading', true);
const queryWords = request.query.split(/\s+/).filter(Boolean).length;
const endpointAdapterFn = queryWords >= 2 ? XPlugin.adapter.aiSuggestions : XPlugin.adapter.aiSummarize;
return endpointAdapterFn(request).then(({ body, status }) => {
if (status !== 200) {
return;
}
if (body) {
const reader = body.getReader();
readAnswer(reader, commit);
}
});
};
function mapTaggingData(tangingData) {
const { toolingDisplay, toolingDisplayClick, searchQueries } = tangingData.tagging[0];
// TODO: Using the getTaggingInfoFromUrl util here is a temporary solution.
// It creates a dependency with the x-adapter-platform project that should be avoided.
return {
toolingDisplay: getTaggingInfoFromUrl(toolingDisplay),
toolingDisplayClick: getTaggingInfoFromUrl(toolingDisplayClick),
searchQueries: Object.fromEntries(Object.entries(searchQueries).map(([query, { toolingDisplay, toolingDisplayClick, toolingDisplayAdd2Cart }]) => [
query,
{
toolingDisplay: getTaggingInfoFromUrl(toolingDisplay),
toolingDisplayClick: getTaggingInfoFromUrl(toolingDisplayClick),
toolingDisplayAdd2Cart: getTaggingInfoFromUrl(toolingDisplayAdd2Cart),
},
])),
};
}
function readAnswer(reader, commit) {
reader
.read()
.then(({ value, done }) => {
if (done) {
commit('setSuggestionsLoading', false);
return;
}
const result = new TextDecoder().decode(value, { stream: true });
const parts = result.split('\n\n');
for (const part of parts) {
const lines = part.split('\n');
for (const line of lines) {
// line.length check to avoid event lines or empty lines
if (line.length <= 5 || line.startsWith('event:'))
continue;
const raw = line.startsWith('data:') ? line.slice(5).trim() : line.trim();
const data = JSON.parse(raw);
if ('suggestionText' in data) {
commit('setIsNoResults', false);
commit('setSuggestionText', data.suggestionText);
}
if ('responseText' in data) {
commit('setIsNoResults', false);
commit('setResponseText', data.responseText);
}
if ('queries' in data) {
commit('setQueries', data.queries);
}
if ('tagging' in data) {
commit('setTagging', mapTaggingData(data));
}
}
}
readAnswer(reader, commit);
})
.catch((error) => {
commit('setSuggestionsLoading', false);
// AbortError code === 20
if (error.code !== 20) {
console.error(error);
}
});
}
export { fetchAndSaveAiSuggestions };
//# sourceMappingURL=fetch-and-save-ai-suggestions.action.js.map