forked from mrsimpson/Rocket.Chat
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Propagate message lifecycle to Smarti (RocketChat#301)
* Once a conversation is closed, don't submit messages to Smarti anymore * Update Chatpal-Search * Smarti-52: Add support for edited messages * Propagate updates and deletions to Smarti * Add tests for message lifecycle. They are failing due to Smarti-error * fix issue in smarti-integration-tests * Propagate request deletion to Smarti and reorder tests * Fix messages for livechat not sent to Smarti * Merge branch 'develop' into fix/smarti/message-lifecycle
- Loading branch information
Showing
8 changed files
with
383 additions
and
123 deletions.
There are no files selected for viewing
67 changes: 50 additions & 17 deletions
67
packages/assistify-ai/server/hooks/sendMessageToKnowledgeAdapter.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,76 @@ | ||
/* globals RocketChat, SystemLogger */ | ||
|
||
import { getKnowledgeAdapter } from '../lib/KnowledgeAdapterProvider'; | ||
import {getKnowledgeAdapter} from '../lib/KnowledgeAdapterProvider'; | ||
|
||
RocketChat.callbacks.remove('afterSaveMessage', 'externalWebHook'); | ||
|
||
RocketChat.callbacks.add('afterSaveMessage', function(message, room) { | ||
// skips this callback if the message was edited | ||
if (message.editedAt) { | ||
return message; | ||
} | ||
|
||
function isMessageRelevant(message, room) { | ||
let knowledgeEnabled = false; | ||
RocketChat.settings.get('Assistify_AI_Enabled', function(key, value) { | ||
knowledgeEnabled = value; | ||
}); | ||
|
||
if (!knowledgeEnabled) { | ||
return message; | ||
return false; | ||
} | ||
|
||
//we only want to forward messages from livechat-rooms - requests are implemented in the help-request-package | ||
if (!room) { | ||
room = RocketChat.models.Rooms.findOneById(message.rid); | ||
} | ||
|
||
//we only want to forward messages from livechat-rooms | ||
if (!(room && (room.t === 'l'))) { | ||
return message; | ||
return false; | ||
} | ||
|
||
const knowledgeAdapter = getKnowledgeAdapter(); | ||
if (!knowledgeAdapter) { | ||
return; | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
RocketChat.callbacks.add('afterSaveMessage', function(message, room) { | ||
if (isMessageRelevant(message, room)) { | ||
const knowledgeAdapter = getKnowledgeAdapter(); | ||
SystemLogger.debug(`Send message ${ message._id } to knowledgeAdapter (Meteor.defer()`); | ||
Meteor.defer(() => { | ||
try { | ||
SystemLogger.debug(`Calling onMessage(${ message._id });`); | ||
knowledgeAdapter.onMessage(message); | ||
} catch (e) { | ||
SystemLogger.error('Error using knowledge provider ->', e); | ||
} | ||
}); | ||
} | ||
}, RocketChat.callbacks.priority.LOW, 'Assistify_AI_OnMessage'); | ||
|
||
RocketChat.callbacks.add('afterDeleteMessage', function(message) { | ||
if (isMessageRelevant(message)) { | ||
const knowledgeAdapter = getKnowledgeAdapter(); | ||
SystemLogger.debug(`Propagating delete of message${ message._id } to knowledge-adapter`); | ||
Meteor.defer(() => { | ||
try { | ||
SystemLogger.debug(`Calling afterDeleteMessage(${ message._id });`); | ||
knowledgeAdapter.afterMessageDeleted(message); | ||
} catch (e) { | ||
SystemLogger.error('Error using knowledge provider ->', e); | ||
} | ||
}); | ||
} | ||
|
||
SystemLogger.debug(`Send message ${ message._id } to knowledgeAdapter (Meteor.defer()`); | ||
}, RocketChat.callbacks.priority.LOW, 'Assistify_AI_afterDeleteMessage'); | ||
|
||
RocketChat.callbacks.add('afterRoomErased', function(room) { | ||
const knowledgeAdapter = getKnowledgeAdapter(); | ||
SystemLogger.debug(`Propagating delete of room ${ room._id } to knowledge-adapter`); | ||
Meteor.defer(() => { | ||
try { | ||
SystemLogger.debug(`Calling onMessage(${ message._id });`); | ||
knowledgeAdapter.onMessage(message); | ||
SystemLogger.debug(`Calling afterRoomErased(${ room._id });`); | ||
knowledgeAdapter.afterRoomErased(room); | ||
} catch (e) { | ||
SystemLogger.error('Error using knowledge provider ->', e); | ||
} | ||
}); | ||
|
||
return message; | ||
}, RocketChat.callbacks.priority.LOW, 'Assistify_AI_OnMessage'); | ||
}, RocketChat.callbacks.priority.LOW, 'Assistify_AI_afterRoomErased'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.