2024-03-18 09:37:36 +01:00
const { multiUserMode , userFromSession } = require ( "../utils/http" ) ;
const { validatedRequest } = require ( "../utils/middleware/validatedRequest" ) ;
const { Telemetry } = require ( "../models/telemetry" ) ;
const {
flexUserRoleValid ,
ROLES ,
} = require ( "../utils/middleware/multiUserProtected" ) ;
const { EventLogs } = require ( "../models/eventLogs" ) ;
const { WorkspaceMetaResponse } = require ( "../models/workspaceMetaResponse" ) ;
const { validWorkspaceSlug } = require ( "../utils/middleware/validWorkspace" ) ;
function workspaceMetaResponse ( app ) {
if ( ! app ) return ;
app . patch (
"/workspace/:slug/metaResponse/toggle" ,
[ validatedRequest , flexUserRoleValid ( [ ROLES . all ] ) , validWorkspaceSlug ] ,
async ( request , response ) => {
try {
const user = await userFromSession ( request , response ) ;
const workspace = response . locals . workspace ;
const metaResponse = workspace . metaResponse ;
// If metaResponse settings are not found, create them
if ( ! workspace . metaResponse && ! workspace . metaResponseSettings ) {
metaResponseDefaultSettings . inputs . config . systemPrompt . openAiPrompt =
workspace . openAiPrompt || "" ;
await WorkspaceMetaResponse . update (
workspace . id ,
JSON . stringify ( metaResponseDefaultSettings )
) ;
await Telemetry . sendTelemetry (
"workspace_meta_response_enabled" ,
{
multiUserMode : multiUserMode ( response ) ,
LLMSelection : process . env . LLM _PROVIDER || "openai" ,
Embedder : process . env . EMBEDDING _ENGINE || "inherit" ,
VectorDbSelection : process . env . VECTOR _DB || "pinecone" ,
} ,
user ? . id
) ;
await EventLogs . logEvent (
"workspace_meta_response_enabled" ,
{
workspaceName : workspace ? . name || "Unknown Workspace" ,
} ,
user ? . id
) ;
}
await WorkspaceMetaResponse . toggleMetaResponse ( workspace . id , ! metaResponse ) ;
response . sendStatus ( 200 ) . end ( ) ;
} catch ( e ) {
console . log ( e . message , e ) ;
response . sendStatus ( 500 ) . end ( ) ;
}
}
) ;
app . get (
"/workspace/:slug/metaResponse/status" ,
[ validatedRequest , flexUserRoleValid ( [ ROLES . all ] ) , validWorkspaceSlug ] ,
async ( request , response ) => {
try {
const workspace = response . locals . workspace ;
response . status ( 200 ) . json ( { status : workspace . metaResponse } ) ;
} catch ( e ) {
console . log ( e . message , e ) ;
response . sendStatus ( 500 ) . end ( ) ;
}
}
) ;
app . get (
"/workspace/:slug/metaResponse/settings" ,
[ validatedRequest , flexUserRoleValid ( [ ROLES . all ] ) , validWorkspaceSlug ] ,
async ( request , response ) => {
try {
const workspace = response . locals . workspace ;
const settings = JSON . parse ( workspace . metaResponseSettings ) ;
if ( ! settings ) {
response . status ( 200 ) . json ( metaResponseDefaultSettings ) ;
return ;
}
response . status ( 200 ) . json ( settings ) ;
} catch ( e ) {
console . log ( e . message , e ) ;
response . sendStatus ( 500 ) . end ( ) ;
}
}
) ;
app . patch (
"/workspace/:slug/metaResponse/settings" ,
[ validatedRequest , flexUserRoleValid ( [ ROLES . all ] ) , validWorkspaceSlug ] ,
async ( request , response ) => {
try {
const workspace = response . locals . workspace ;
const data = request . body ;
2024-03-20 01:37:38 +01:00
const result = await WorkspaceMetaResponse . update ( workspace . id , data ) ;
2024-03-18 09:37:36 +01:00
response . status ( 200 ) . json ( JSON . parse ( result . metaResponseSettings ) ) ;
} catch ( e ) {
console . log ( e . message , e ) ;
response . sendStatus ( 500 ) . end ( ) ;
}
}
) ;
app . get (
"/workspace/:slug/metaResponse/inputs/settings" ,
[ validatedRequest , flexUserRoleValid ( [ ROLES . all ] ) , validWorkspaceSlug ] ,
async ( request , response ) => {
try {
const workspace = response . locals . workspace ;
const settings = JSON . parse ( workspace . metaResponseSettings ) ;
response . status ( 200 ) . json ( settings . inputs ) ;
} catch ( e ) {
console . log ( e . message , e ) ;
response . sendStatus ( 500 ) . end ( ) ;
}
}
) ;
app . patch (
"/workspace/:slug/metaResponse/inputs/settings" ,
[ validatedRequest , flexUserRoleValid ( [ ROLES . all ] ) , validWorkspaceSlug ] ,
async ( request , response ) => {
try {
const workspace = response . locals . workspace ;
const data = request . body ;
const settings = JSON . parse ( workspace . metaResponseSettings ) ;
settings . inputs = data ;
const result = await WorkspaceMetaResponse . update (
workspace . id ,
JSON . stringify ( settings )
) ;
response . status ( 200 ) . json ( JSON . parse ( result . metaResponseSettings ) . inputs ) ;
} catch ( e ) {
console . log ( e . message , e ) ;
response . sendStatus ( 500 ) . end ( ) ;
}
}
) ;
app . patch (
"/workspace/:slug/metaResponse/inputs/toggle" ,
[ validatedRequest , flexUserRoleValid ( [ ROLES . all ] ) , validWorkspaceSlug ] ,
async ( request , response ) => {
try {
const workspace = response . locals . workspace ;
const settings = JSON . parse ( workspace . metaResponseSettings ) ;
settings . inputs . isEnabled = ! settings . inputs . isEnabled ;
const result = await WorkspaceMetaResponse . update (
workspace . id ,
JSON . stringify ( settings )
) ;
response . sendStatus ( 200 ) . end ( ) ;
} catch ( e ) {
console . log ( e . message , e ) ;
response . sendStatus ( 500 ) . end ( ) ;
}
}
) ;
app . get (
"/workspace/:slug/metaResponse/sentiments/settings" ,
[ validatedRequest , flexUserRoleValid ( [ ROLES . all ] ) , validWorkspaceSlug ] ,
async ( request , response ) => {
try {
const workspace = response . locals . workspace ;
const settings = JSON . parse ( workspace . metaResponseSettings ) ;
response . status ( 200 ) . json ( settings . sentiments ) ;
} catch ( e ) {
console . log ( e . message , e ) ;
response . sendStatus ( 500 ) . end ( ) ;
}
}
) ;
app . patch (
"/workspace/:slug/metaResponse/sentiments/settings" ,
[ validatedRequest , flexUserRoleValid ( [ ROLES . all ] ) , validWorkspaceSlug ] ,
async ( request , response ) => {
try {
const workspace = response . locals . workspace ;
const data = request . body ;
const settings = JSON . parse ( workspace . metaResponseSettings ) ;
settings . sentiments = data ;
const result = await WorkspaceMetaResponse . update (
workspace . id ,
JSON . stringify ( settings )
) ;
response . status ( 200 ) . json ( JSON . parse ( result . metaResponseSettings ) . sentiments ) ;
} catch ( e ) {
console . log ( e . message , e ) ;
response . sendStatus ( 500 ) . end ( ) ;
}
}
) ;
app . patch (
"/workspace/:slug/metaResponse/sentiments/toggle" ,
[ validatedRequest , flexUserRoleValid ( [ ROLES . all ] ) , validWorkspaceSlug ] ,
async ( request , response ) => {
try {
const workspace = response . locals . workspace ;
const settings = JSON . parse ( workspace . metaResponseSettings ) ;
settings . sentiments . isEnabled = ! settings . sentiments . isEnabled ;
const result = await WorkspaceMetaResponse . update (
workspace . id ,
JSON . stringify ( settings )
) ;
response . sendStatus ( 200 ) . end ( ) ;
} catch ( e ) {
console . log ( e . message , e ) ;
response . sendStatus ( 500 ) . end ( ) ;
}
}
) ;
}
const metaResponseDefaultSettings = {
inputs : {
isEnabled : false ,
config : {
systemPrompt : {
isEnabled : false ,
content : "" ,
openAiPrompt : "" ,
overrideSystemPrompt : false ,
suggestionsList : [
{
title : "" ,
content : "" ,
} ,
] ,
canEdit : [ "admin" , "manager" ] ,
} ,
promptSchema : {
content : "" ,
suggestionsList : [
{
title : "" ,
content : "" ,
} ,
] ,
overrideWorkspacePrompt : false ,
canEdit : [ "admin" , "manager" ] ,
} ,
components : {
dropDownMenu : {
isEnabled : false ,
options : [ ] ,
2024-03-21 06:59:22 +01:00
description : "Drop Down menu best to select between functional derisions, ie: continue, Repeat or Move to a new sequence.. etc" ,
2024-03-18 09:37:36 +01:00
} ,
optionsList : {
isEnabled : false ,
options : [ ] ,
2024-03-21 06:59:22 +01:00
description : "Best suited for expansion on a topic" ,
2024-03-18 09:37:36 +01:00
} ,
optionsButtons : {
isEnabled : false ,
options : [ ] ,
2024-03-21 06:59:22 +01:00
description : "Chat will provide answers with the LLM's general knowledge and document context that is found." ,
2024-03-18 09:37:36 +01:00
} ,
multiSelectCheckboxes : {
isEnabled : false ,
options : [ ] ,
2024-03-21 06:59:22 +01:00
description : "Chat will provide answers with the LLM's general knowledge and document context that is found." ,
2024-03-18 09:37:36 +01:00
} ,
} ,
} ,
permissions : [ "user" ] ,
2024-03-20 01:37:38 +01:00
description : "Traditionally, interaction with AnythingLLM occurs through a text area. Meta Inputs enhance this by offering alternative interaction methods, including option buttons, multi-select checkboxes, sliders, drop-down menus, and date/time selectors. To utilize these components, you'll need to guide the LLM on incorporating them into its responses with a specific schema" ,
2024-03-18 09:37:36 +01:00
} ,
sentiments : {
isEnabled : false ,
config : {
sentimentAnalysis : {
isEnabled : false ,
scoreThreshold : 0.5 ,
} ,
} ,
permissions : [ "user" ] ,
2024-03-20 01:37:38 +01:00
description : "Activate to enable the AI to analyze and adapt its responses based on the emotional tone of the conversation, enhancing interaction personalization"
2024-03-18 09:37:36 +01:00
} ,
avatars : {
isEnabled : false ,
config : {
avatarType : "circle" ,
avatarSize : "medium" ,
avatarName : "user" ,
} ,
permissions : [ "user" ] ,
2024-03-20 01:37:38 +01:00
description : "Enable avatars to reflect user sentiments, allowing the AI to visually empathize and convey understanding through changes in its profile image based on the meta object's sentiment data."
2024-03-18 09:37:36 +01:00
} ,
} ;
module . exports = { workspaceMetaResponse } ;