2023-09-28 23:00:03 +02:00
const prisma = require ( "../utils/prisma" ) ;
2024-07-18 19:35:02 +02:00
const slugifyModule = require ( "slugify" ) ;
2023-06-08 06:31:35 +02:00
const { Document } = require ( "./documents" ) ;
2023-07-25 19:37:04 +02:00
const { WorkspaceUser } = require ( "./workspaceUsers" ) ;
2024-01-22 23:14:01 +01:00
const { ROLES } = require ( "../utils/middleware/multiUserProtected" ) ;
2024-01-31 22:38:21 +01:00
const { v4 : uuidv4 } = require ( "uuid" ) ;
2024-04-02 23:53:35 +02:00
const { User } = require ( "./user" ) ;
2023-06-04 04:28:07 +02:00
const Workspace = {
2024-03-21 23:25:42 +01:00
defaultPrompt :
"Given the following conversation, relevant context, and a follow up question, reply with an answer to the current question the user is asking. Return only your response to the question given the above information following the users instructions as needed." ,
2023-06-15 08:12:59 +02:00
writable : [
// Used for generic updates so we can validate keys in request body
"name" ,
"slug" ,
"vectorTag" ,
"openAiTemp" ,
2023-06-28 21:53:26 +02:00
"openAiHistory" ,
2023-06-15 08:12:59 +02:00
"lastUpdatedAt" ,
2023-07-20 20:14:23 +02:00
"openAiPrompt" ,
2023-11-07 01:49:29 +01:00
"similarityThreshold" ,
2024-04-05 19:58:36 +02:00
"chatProvider" ,
2024-01-17 21:59:25 +01:00
"chatModel" ,
2024-01-18 21:34:20 +01:00
"topN" ,
2024-02-16 23:50:40 +01:00
"chatMode" ,
2024-03-12 19:36:59 +01:00
"pfpFilename" ,
2024-04-16 19:50:10 +02:00
"agentProvider" ,
"agentModel" ,
2024-05-01 01:14:30 +02:00
"queryRefusalResponse" ,
2023-06-15 08:12:59 +02:00
] ,
2024-07-18 19:35:02 +02:00
/ * *
* The default Slugify module requires some additional mapping to prevent downstream issues
* with some vector db providers and instead of building a normalization method for every provider
* we can capture this on the table level to not have to worry about it .
* @ param { ... any } args - slugify args for npm package .
* @ returns { string }
* /
slugify : function ( ... args ) {
slugifyModule . extend ( { "+" : " plus " , "!" : " bang " } ) ;
return slugifyModule ( ... args ) ;
} ,
2023-06-15 08:12:59 +02:00
2023-07-25 19:37:04 +02:00
new : async function ( name = null , creatorId = null ) {
2023-06-08 06:31:35 +02:00
if ( ! name ) return { result : null , message : "name cannot be null" } ;
2024-07-18 19:35:02 +02:00
var slug = this . slugify ( name , { lower : true } ) ;
2024-01-31 22:38:21 +01:00
slug = slug || uuidv4 ( ) ;
2023-06-15 08:12:59 +02:00
2023-09-28 23:00:03 +02:00
const existingBySlug = await this . get ( { slug } ) ;
2023-06-15 08:12:59 +02:00
if ( existingBySlug !== null ) {
const slugSeed = Math . floor ( 10000000 + Math . random ( ) * 90000000 ) ;
2024-07-18 19:35:02 +02:00
slug = this . slugify ( ` ${ name } - ${ slugSeed } ` , { lower : true } ) ;
2023-06-15 08:12:59 +02:00
}
2023-06-04 04:28:07 +02:00
2023-09-28 23:00:03 +02:00
try {
const workspace = await prisma . workspaces . create ( {
data : { name , slug } ,
2023-06-08 06:31:35 +02:00
} ) ;
2023-06-15 08:12:59 +02:00
2023-09-28 23:00:03 +02:00
// If created with a user then we need to create the relationship as well.
// If creating with an admin User it wont change anything because admins can
// view all workspaces anyway.
if ( ! ! creatorId ) await WorkspaceUser . create ( creatorId , workspace . id ) ;
return { workspace , message : null } ;
} catch ( error ) {
console . error ( error . message ) ;
return { workspace : null , message : error . message } ;
2023-06-15 08:12:59 +02:00
}
2023-06-04 04:28:07 +02:00
} ,
2023-09-28 23:00:03 +02:00
2024-04-05 19:58:36 +02:00
update : async function ( id = null , updates = { } ) {
2023-06-15 08:12:59 +02:00
if ( ! id ) throw new Error ( "No workspace id provided for update" ) ;
2024-04-05 19:58:36 +02:00
const validFields = Object . keys ( updates ) . filter ( ( key ) =>
2023-06-15 08:12:59 +02:00
this . writable . includes ( key )
) ;
2024-04-05 19:58:36 +02:00
Object . entries ( updates ) . forEach ( ( [ key ] ) => {
if ( validFields . includes ( key ) ) return ;
delete updates [ key ] ;
} ) ;
if ( Object . keys ( updates ) . length === 0 )
2023-06-15 08:12:59 +02:00
return { workspace : { id } , message : "No valid fields to update!" } ;
2023-06-28 21:54:18 +02:00
2024-04-05 19:58:36 +02:00
// If the user unset the chatProvider we will need
// to then clear the chatModel as well to prevent confusion during
// LLM loading.
if ( updates ? . chatProvider === "default" ) {
updates . chatProvider = null ;
updates . chatModel = null ;
}
return this . _update ( id , updates ) ;
} ,
// Explicit update of settings + key validations.
// Only use this method when directly setting a key value
// that takes no user input for the keys being modified.
_update : async function ( id = null , data = { } ) {
if ( ! id ) throw new Error ( "No workspace id provided for update" ) ;
2023-09-28 23:00:03 +02:00
try {
const workspace = await prisma . workspaces . update ( {
where : { id } ,
2024-04-05 19:58:36 +02:00
data ,
2023-06-15 08:12:59 +02:00
} ) ;
2023-09-28 23:00:03 +02:00
return { workspace , message : null } ;
} catch ( error ) {
console . error ( error . message ) ;
return { workspace : null , message : error . message } ;
2023-06-15 08:12:59 +02:00
}
} ,
2023-09-28 23:00:03 +02:00
getWithUser : async function ( user = null , clause = { } ) {
2024-01-22 23:14:01 +01:00
if ( [ ROLES . admin , ROLES . manager ] . includes ( user . role ) )
return this . get ( clause ) ;
2023-07-25 19:37:04 +02:00
2023-09-28 23:00:03 +02:00
try {
const workspace = await prisma . workspaces . findFirst ( {
where : {
... clause ,
workspace _users : {
some : {
user _id : user ? . id ,
} ,
} ,
} ,
include : {
workspace _users : true ,
documents : true ,
} ,
} ) ;
if ( ! workspace ) return null ;
return {
... workspace ,
documents : await Document . forWorkspace ( workspace . id ) ,
} ;
} catch ( error ) {
console . error ( error . message ) ;
return null ;
}
2023-07-25 19:37:04 +02:00
} ,
2023-09-28 23:00:03 +02:00
get : async function ( clause = { } ) {
try {
const workspace = await prisma . workspaces . findFirst ( {
where : clause ,
include : {
documents : true ,
} ,
} ) ;
return workspace || null ;
} catch ( error ) {
console . error ( error . message ) ;
return null ;
}
2023-06-04 04:28:07 +02:00
} ,
2023-06-15 08:12:59 +02:00
2023-09-28 23:00:03 +02:00
delete : async function ( clause = { } ) {
try {
await prisma . workspaces . delete ( {
where : clause ,
} ) ;
return true ;
} catch ( error ) {
console . error ( error . message ) ;
return false ;
}
2023-06-04 04:28:07 +02:00
} ,
2023-06-15 08:12:59 +02:00
2023-09-28 23:00:03 +02:00
where : async function ( clause = { } , limit = null , orderBy = null ) {
try {
const results = await prisma . workspaces . findMany ( {
where : clause ,
... ( limit !== null ? { take : limit } : { } ) ,
... ( orderBy !== null ? { orderBy } : { } ) ,
} ) ;
return results ;
} catch ( error ) {
console . error ( error . message ) ;
return [ ] ;
}
2023-06-04 04:28:07 +02:00
} ,
2023-09-28 23:00:03 +02:00
2023-07-25 19:37:04 +02:00
whereWithUser : async function (
user ,
2023-09-28 23:00:03 +02:00
clause = { } ,
2023-07-25 19:37:04 +02:00
limit = null ,
orderBy = null
) {
2024-01-22 23:14:01 +01:00
if ( [ ROLES . admin , ROLES . manager ] . includes ( user . role ) )
2023-11-13 23:51:16 +01:00
return await this . where ( clause , limit , orderBy ) ;
2023-09-28 23:00:03 +02:00
try {
const workspaces = await prisma . workspaces . findMany ( {
where : {
... clause ,
workspace _users : {
some : {
user _id : user . id ,
} ,
} ,
} ,
... ( limit !== null ? { take : limit } : { } ) ,
... ( orderBy !== null ? { orderBy } : { } ) ,
} ) ;
return workspaces ;
} catch ( error ) {
console . error ( error . message ) ;
return [ ] ;
}
2023-07-25 19:37:04 +02:00
} ,
2023-09-28 23:00:03 +02:00
whereWithUsers : async function ( clause = { } , limit = null , orderBy = null ) {
try {
const workspaces = await this . where ( clause , limit , orderBy ) ;
for ( const workspace of workspaces ) {
const userIds = (
await WorkspaceUser . where ( { workspace _id : Number ( workspace . id ) } )
) . map ( ( rel ) => rel . user _id ) ;
workspace . userIds = userIds ;
}
return workspaces ;
} catch ( error ) {
console . error ( error . message ) ;
return [ ] ;
2023-07-25 19:37:04 +02:00
}
} ,
2023-09-28 23:00:03 +02:00
2024-04-02 23:53:35 +02:00
workspaceUsers : async function ( workspaceId ) {
try {
const users = (
await WorkspaceUser . where ( { workspace _id : Number ( workspaceId ) } )
) . map ( ( rel ) => rel ) ;
const usersById = await User . where ( {
id : { in : users . map ( ( user ) => user . user _id ) } ,
} ) ;
const userInfo = usersById . map ( ( user ) => {
const workspaceUser = users . find ( ( u ) => u . user _id === user . id ) ;
return {
2024-06-18 18:35:40 +02:00
userId : user . id ,
2024-04-02 23:53:35 +02:00
username : user . username ,
role : user . role ,
lastUpdatedAt : workspaceUser . lastUpdatedAt ,
} ;
} ) ;
return userInfo ;
} catch ( error ) {
console . error ( error . message ) ;
return [ ] ;
}
} ,
2023-07-25 19:37:04 +02:00
updateUsers : async function ( workspaceId , userIds = [ ] ) {
2023-09-28 23:00:03 +02:00
try {
await WorkspaceUser . delete ( { workspace _id : Number ( workspaceId ) } ) ;
await WorkspaceUser . createManyUsers ( userIds , workspaceId ) ;
return { success : true , error : null } ;
} catch ( error ) {
console . error ( error . message ) ;
return { success : false , error : error . message } ;
}
2023-07-25 19:37:04 +02:00
} ,
2024-01-17 21:59:25 +01:00
2024-03-21 23:25:42 +01:00
trackChange : async function ( prevData , newData , user ) {
try {
2024-04-05 19:58:36 +02:00
await this . _trackWorkspacePromptChange ( prevData , newData , user ) ;
2024-03-21 23:25:42 +01:00
return ;
} catch ( error ) {
console . error ( "Error tracking workspace change:" , error . message ) ;
return ;
}
} ,
2024-04-05 19:58:36 +02:00
// We are only tracking this change to determine the need to a prompt library or
// prompt assistant feature. If this is something you would like to see - tell us on GitHub!
_trackWorkspacePromptChange : async function ( prevData , newData , user ) {
const { Telemetry } = require ( "./telemetry" ) ;
const { EventLogs } = require ( "./eventLogs" ) ;
if (
! newData ? . openAiPrompt ||
newData ? . openAiPrompt === this . defaultPrompt ||
newData ? . openAiPrompt === prevData ? . openAiPrompt
)
return ;
await Telemetry . sendTelemetry ( "workspace_prompt_changed" ) ;
await EventLogs . logEvent (
"workspace_prompt_changed" ,
{
workspaceName : prevData ? . name ,
prevSystemPrompt : prevData ? . openAiPrompt || this . defaultPrompt ,
newSystemPrompt : newData ? . openAiPrompt ,
} ,
user ? . id
) ;
return ;
} ,
2023-06-08 06:31:35 +02:00
} ;
2023-06-04 04:28:07 +02:00
2023-06-08 06:31:35 +02:00
module . exports = { Workspace } ;