11'use strict' ;
22
33const Archetype = require ( 'archetype' ) ;
4+ const authorize = require ( '../../authorize' ) ;
45const getModelDescriptions = require ( '../../helpers/getModelDescriptions' ) ;
56const mongoose = require ( 'mongoose' ) ;
67
@@ -17,6 +18,9 @@ const CreateChatMessageParams = new Archetype({
1718 authorization : {
1819 $type : 'string' ,
1920 $required : true
21+ } ,
22+ roles : {
23+ $type : [ 'string' ]
2024 }
2125} ) . compile ( 'CreateChatMessageParams' ) ;
2226
@@ -55,10 +59,12 @@ return { numUsers: users.length };
5559Here is a description of the user's models. Assume these are the only models available in the system unless explicitly instructed otherwise by the user.
5660` . trim ( ) ;
5761
58- module . exports = ( { db } ) => async function createChatMessage ( params ) {
59- const { chatThreadId, userId, content, script, authorization } = new CreateChatMessageParams ( params ) ;
60- const ChatThread = db . model ( '__Studio_ChatThread' ) ;
61- const ChatMessage = db . model ( '__Studio_ChatMessage' ) ;
62+ module . exports = ( { db, studioConnection, options } ) => async function createChatMessage ( params ) {
63+ const { chatThreadId, userId, content, script, authorization, roles } = new CreateChatMessageParams ( params ) ;
64+ const ChatThread = studioConnection . model ( '__Studio_ChatThread' ) ;
65+ const ChatMessage = studioConnection . model ( '__Studio_ChatMessage' ) ;
66+
67+ await authorize ( 'ChatThread.createChatMessage' , roles ) ;
6268
6369 // Check that the user owns the thread
6470 const chatThread = await ChatThread . findOne ( { _id : chatThreadId } ) ;
@@ -91,6 +97,12 @@ module.exports = ({ db }) => async function createChatMessage(params) {
9197 role : 'system' ,
9298 content : systemPrompt + getModelDescriptions ( db )
9399 } ) ;
100+ if ( options . context ) {
101+ llmMessages . unshift ( {
102+ role : 'system' ,
103+ content : options . context
104+ } ) ;
105+ }
94106
95107 // Create the chat message and get OpenAI response in parallel
96108 const chatMessages = await Promise . all ( [
@@ -111,7 +123,7 @@ module.exports = ({ db }) => async function createChatMessage(params) {
111123 } )
112124 ] ) ;
113125
114- return { chatMessages } ;
126+ return { chatMessages, chatThread } ;
115127} ;
116128
117129async function getChatCompletion ( messages , authorization ) {
0 commit comments