mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-03 04:27:53 +00:00
# Pull Request Template ## Description This PR will replace the usage of `alertMixin` from the code base with the `useAlert` composable. Fixes https://linear.app/chatwoot/issue/CW-3462/replace-alertmixin-usage-with-usealert ## Type of change - [x] Breaking change (fix or feature that would cause existing functionality not to work as expected) ## How Has This Been Tested? Please refer this issue description https://linear.app/chatwoot/issue/CW-3462/replace-alertmixin-usage-with-usealert ## Checklist: - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my code - [ ] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules --------- Co-authored-by: Sojan Jose <sojan@pepalo.com>
109 lines
3.0 KiB
JavaScript
109 lines
3.0 KiB
JavaScript
import { mapGetters } from 'vuex';
|
|
import { useAlert } from 'dashboard/composables';
|
|
import { OPEN_AI_EVENTS } from '../helper/AnalyticsHelper/events';
|
|
import OpenAPI from '../api/integrations/openapi';
|
|
|
|
export default {
|
|
mounted() {
|
|
this.fetchIntegrationsIfRequired();
|
|
},
|
|
computed: {
|
|
...mapGetters({
|
|
uiFlags: 'integrations/getUIFlags',
|
|
appIntegrations: 'integrations/getAppIntegrations',
|
|
currentChat: 'getSelectedChat',
|
|
replyMode: 'draftMessages/getReplyEditorMode',
|
|
}),
|
|
aiIntegration() {
|
|
return this.appIntegrations.find(
|
|
integration => integration.id === 'openai' && !!integration.hooks.length
|
|
)?.hooks[0];
|
|
},
|
|
isAIIntegrationEnabled() {
|
|
return !!this.aiIntegration;
|
|
},
|
|
isLabelSuggestionFeatureEnabled() {
|
|
if (this.aiIntegration) {
|
|
const { settings = {} } = this.aiIntegration || {};
|
|
return settings.label_suggestion;
|
|
}
|
|
return false;
|
|
},
|
|
isFetchingAppIntegrations() {
|
|
return this.uiFlags.isFetching;
|
|
},
|
|
hookId() {
|
|
return this.aiIntegration.id;
|
|
},
|
|
draftMessage() {
|
|
return this.$store.getters['draftMessages/get'](this.draftKey);
|
|
},
|
|
draftKey() {
|
|
return `draft-${this.conversationId}-${this.replyMode}`;
|
|
},
|
|
conversationId() {
|
|
return this.currentChat?.id;
|
|
},
|
|
},
|
|
methods: {
|
|
async fetchIntegrationsIfRequired() {
|
|
if (!this.appIntegrations.length) {
|
|
await this.$store.dispatch('integrations/get');
|
|
}
|
|
},
|
|
async recordAnalytics(type, payload) {
|
|
const event = OPEN_AI_EVENTS[type.toUpperCase()];
|
|
if (event) {
|
|
this.$track(event, {
|
|
type,
|
|
...payload,
|
|
});
|
|
}
|
|
},
|
|
async fetchLabelSuggestions({ conversationId }) {
|
|
if (!conversationId) return [];
|
|
|
|
try {
|
|
const result = await OpenAPI.processEvent({
|
|
type: 'label_suggestion',
|
|
hookId: this.hookId,
|
|
conversationId: conversationId,
|
|
});
|
|
|
|
const {
|
|
data: { message: labels },
|
|
} = result;
|
|
|
|
return this.cleanLabels(labels);
|
|
} catch (error) {
|
|
return [];
|
|
}
|
|
},
|
|
cleanLabels(labels) {
|
|
return labels
|
|
.toLowerCase() // Set it to lowercase
|
|
.split(',') // split the string into an array
|
|
.filter(label => label.trim()) // remove any empty strings
|
|
.map(label => label.trim()) // trim the words
|
|
.filter((label, index, self) => self.indexOf(label) === index); // remove any duplicates
|
|
},
|
|
async processEvent(type = 'rephrase') {
|
|
try {
|
|
const result = await OpenAPI.processEvent({
|
|
hookId: this.hookId,
|
|
type,
|
|
content: this.draftMessage,
|
|
conversationId: this.conversationId,
|
|
});
|
|
const {
|
|
data: { message: generatedMessage },
|
|
} = result;
|
|
return generatedMessage;
|
|
} catch (error) {
|
|
useAlert(this.$t('INTEGRATION_SETTINGS.OPEN_AI.GENERATE_ERROR'));
|
|
return '';
|
|
}
|
|
},
|
|
},
|
|
};
|