forked from RocketChat/Rocket.Chat
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* WIP: Proxy for Smarti stub * WIP: Proxy for Smarti stub added to package ``` curl -X POST \ http://localhost:3000/api/v1/assistify/smarti/conversation/ \ -H 'content-type: application/json' \ -H 'x-auth-token: EKZ2_t7Wfqg5GML8bs44v3uS0d_b7f3rUd9IUYKNpvJ' \ //from login -H 'x-user-id: S4Mew2PDtEPr3Ejne' \ //from login -d '{ "thisIsMy":"JSON-Body" }' ``` * use injected logger for logging in API * Fixes #151 - Misspelled label "jetzt chaten" * Using the configured access token as HTTP header * Using the RC proxy for Smarti * Revert "Feature/#23 title first message to new request (#149)" This reverts commit 484b04c. * Revert "Fixes #151 - Misspelled label "jetzt chaten"" This reverts commit 0c9ac4f. * Fixing lint errors. * Fixing non authorized access. * fixed issue with wrong knowledge provider indexes * improved tailing slashes for URLs * - add authorization checks before routing the API calls - roll back injected logger, since it is not defined * removed unused localization keys * Reducing code for adding a tailing slash to the Smarti URL. * consolidate constants naming * - Use RateLimiter for Smarti requests - Use propagation function for 'onMessage' and 'onClose' - Make Smarti the default knowledge provider - Reordered Smarti backend settings - Added descriptions for Smarti backend settings - Also reload the settings, when reloading the Smarti widget - Refactored file names * Using rate limiter in each proxy method and limit the propagateToSmarti function instead of HTTP.call * - merged proxy and adapter into only one file - only use DDP for Smarti Widget / Rocket.Chat messages (getConversation, getSmartiQueryBuilderResult) - added proxy endpoint for Smarti conversation search - added several comments * Migrate settings * Extract Smarti loader * additional migrations * Do not migrate old settings with other defaults * separated responsibilities * Simplify adapter, proxy, router, widgetBackend * Minor fixes to get it work with Smarti. * Fix Webhook-token not being transmitted * revert unintentional changes to other files after branching off the wron state. Copied all changes from `develop` which were not included in `assistify:ai`-package
- Loading branch information
Showing
16 changed files
with
471 additions
and
417 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* globals RocketChat */ | ||
|
||
/** | ||
* Load Smarti script asynchronously to the window. | ||
* This ensures the invalidation as settings are changed and allow the script to live beyond template lifetime. | ||
*/ | ||
RocketChat.settings.onload('Assistify_AI_Smarti_Base_URL', function() { | ||
Meteor.call('getSmartiUiScript', function(error, script) { | ||
if (error) { | ||
console.error('could not load Smarti:', error.message); | ||
} else { | ||
// generate a script tag for smarti JS | ||
const doc = document; | ||
const smartiScriptTag = doc.createElement('script'); | ||
smartiScriptTag.type = 'text/javascript'; | ||
smartiScriptTag.async = true; | ||
smartiScriptTag.defer = true; | ||
smartiScriptTag.innerHTML = script; | ||
// insert the smarti script tag as first script tag | ||
const firstScriptTag = doc.getElementsByTagName('script')[0]; | ||
firstScriptTag.parentNode.insertBefore(smartiScriptTag, firstScriptTag); | ||
console.debug('loaded Smarti successfully'); | ||
} | ||
}); | ||
}); |
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
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
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
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* globals SystemLogger, RocketChat */ | ||
|
||
/** The HTTP methods. */ | ||
export const verbs = { | ||
get: 'GET', | ||
post: 'POST', | ||
put: 'PUT', | ||
delete: 'DELETE' | ||
}; | ||
|
||
/** | ||
* The proxy propagates the HTTP requests to Smarti. | ||
* All HTTP outbound traffic (from Rocket.Chat to Smarti) should pass the this proxy. | ||
*/ | ||
export class SmartiProxy { | ||
|
||
static get smartiAuthToken() { | ||
return RocketChat.settings.get('Assistify_AI_Smarti_Auth_Token'); | ||
} | ||
|
||
static get smartiUrl() { | ||
return RocketChat.settings.get('Assistify_AI_Smarti_Base_URL'); | ||
} | ||
|
||
/** | ||
* Propagates requests to Smarti. | ||
* Make sure all requests to Smarti are using this function. | ||
* | ||
* @param {String} method - the HTTP method to use | ||
* @param {String} path - the path to call | ||
* @param {Object} [body=null] - the payload to pass (optional) | ||
* | ||
* @returns {Object} | ||
*/ | ||
static propagateToSmarti(method, path, body = null) { | ||
|
||
const url = `${ SmartiProxy.smartiUrl }${ path }`; | ||
const header = { | ||
'X-Auth-Token': SmartiProxy.smartiAuthToken, | ||
'Content-Type': 'application/json; charset=utf-8' | ||
}; | ||
try { | ||
const response = HTTP.call(method, url, {data: body, headers: header}); | ||
if (response.statusCode === 200) { | ||
return response.data || response.content; //.data if it's a json-response | ||
} else { | ||
SystemLogger.debug('Got unexpected result from Smarti', method, 'to', url, 'response', JSON.stringify(response)); | ||
} | ||
} catch (error) { | ||
SystemLogger.error('Could not complete', method, 'to', url); | ||
SystemLogger.debug(error); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* globals SystemLogger, RocketChat */ | ||
|
||
import {SmartiAdapter} from './lib/SmartiAdapter'; | ||
|
||
/** | ||
* The SmartiRouter handles all incoming HTTP requests from Smarti. | ||
* This is the only place, where adding routes to the Rocket.Chat API, related to Smarti | ||
* All HTTP inbound traffic (from Rocket.Chat to Smarti) should pass the this router. | ||
*/ | ||
|
||
/** | ||
* Add an incoming webhook '/newConversationResult' to receive answers from Smarti. | ||
* This allows asynchronous callback from Smarti, when analyzing the conversation has finished. | ||
*/ | ||
RocketChat.API.v1.addRoute('smarti.result/:_token', {authRequired: false}, { | ||
|
||
post() { | ||
|
||
check(this.bodyParams, Match.ObjectIncluding({ | ||
conversationId: String, | ||
channelId: String | ||
})); | ||
|
||
const rcWebhookToken = RocketChat.settings.get('Assistify_AI_RocketChat_Webhook_Token'); | ||
|
||
//verify token | ||
if (this.urlParams._token && this.urlParams._token === rcWebhookToken) { | ||
|
||
SystemLogger.debug('Smarti - got conversation result:', JSON.stringify(this.bodyParams, null, 2)); | ||
SmartiAdapter.analysisCompleted(this.bodyParams.channelId, this.bodyParams.conversationId, this.bodyParams.token); | ||
return RocketChat.API.v1.success(); | ||
} else { | ||
return RocketChat.API.v1.unauthorized({msg: 'token not valid'}); | ||
} | ||
} | ||
}); |
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
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.