@@ -18,7 +18,9 @@ const createAssistant = async (req, res) => {
18
18
try {
19
19
const { openai } = await getOpenAIClient ( { req, res } ) ;
20
20
21
- const { tools = [ ] , endpoint, ...assistantData } = req . body ;
21
+ const { tools = [ ] , endpoint, conversation_starters, ...assistantData } = req . body ;
22
+ delete assistantData . conversation_starters ;
23
+
22
24
assistantData . tools = tools
23
25
. map ( ( tool ) => {
24
26
if ( typeof tool !== 'string' ) {
@@ -41,11 +43,22 @@ const createAssistant = async (req, res) => {
41
43
} ;
42
44
43
45
const assistant = await openai . beta . assistants . create ( assistantData ) ;
44
- const promise = updateAssistantDoc ( { assistant_id : assistant . id } , { user : req . user . id } ) ;
46
+
47
+ const createData = { user : req . user . id } ;
48
+ if ( conversation_starters ) {
49
+ createData . conversation_starters = conversation_starters ;
50
+ }
51
+
52
+ const document = await updateAssistantDoc ( { assistant_id : assistant . id } , createData ) ;
53
+
45
54
if ( azureModelIdentifier ) {
46
55
assistant . model = azureModelIdentifier ;
47
56
}
48
- await promise ;
57
+
58
+ if ( document . conversation_starters ) {
59
+ assistant . conversation_starters = document . conversation_starters ;
60
+ }
61
+
49
62
logger . debug ( '/assistants/' , assistant ) ;
50
63
res . status ( 201 ) . json ( assistant ) ;
51
64
} catch ( error ) {
@@ -88,7 +101,7 @@ const patchAssistant = async (req, res) => {
88
101
await validateAuthor ( { req, openai } ) ;
89
102
90
103
const assistant_id = req . params . id ;
91
- const { endpoint : _e , ...updateData } = req . body ;
104
+ const { endpoint : _e , conversation_starters , ...updateData } = req . body ;
92
105
updateData . tools = ( updateData . tools ?? [ ] )
93
106
. map ( ( tool ) => {
94
107
if ( typeof tool !== 'string' ) {
@@ -104,6 +117,15 @@ const patchAssistant = async (req, res) => {
104
117
}
105
118
106
119
const updatedAssistant = await openai . beta . assistants . update ( assistant_id , updateData ) ;
120
+
121
+ if ( conversation_starters !== undefined ) {
122
+ const conversationStartersUpdate = await updateAssistantDoc (
123
+ { assistant_id } ,
124
+ { conversation_starters } ,
125
+ ) ;
126
+ updatedAssistant . conversation_starters = conversationStartersUpdate . conversation_starters ;
127
+ }
128
+
107
129
res . json ( updatedAssistant ) ;
108
130
} catch ( error ) {
109
131
logger . error ( '[/assistants/:id] Error updating assistant' , error ) ;
@@ -153,14 +175,58 @@ const listAssistants = async (req, res) => {
153
175
}
154
176
} ;
155
177
178
+ /**
179
+ * Filter assistants based on configuration.
180
+ *
181
+ * @param {object } params - The parameters object.
182
+ * @param {string } params.userId - The user ID to filter private assistants.
183
+ * @param {AssistantDocument[] } params.assistants - The list of assistants to filter.
184
+ * @param {Partial<TAssistantEndpoint> } [params.assistantsConfig] - The assistant configuration.
185
+ * @returns {AssistantDocument[] } - The filtered list of assistants.
186
+ */
187
+ function filterAssistantDocs ( { documents, userId, assistantsConfig = { } } ) {
188
+ const { supportedIds, excludedIds, privateAssistants } = assistantsConfig ;
189
+ const removeUserId = ( doc ) => {
190
+ const { user : _u , ...document } = doc ;
191
+ return document ;
192
+ } ;
193
+
194
+ if ( privateAssistants ) {
195
+ return documents . filter ( ( doc ) => userId === doc . user . toString ( ) ) . map ( removeUserId ) ;
196
+ } else if ( supportedIds ?. length ) {
197
+ return documents . filter ( ( doc ) => supportedIds . includes ( doc . assistant_id ) ) . map ( removeUserId ) ;
198
+ } else if ( excludedIds ?. length ) {
199
+ return documents . filter ( ( doc ) => ! excludedIds . includes ( doc . assistant_id ) ) . map ( removeUserId ) ;
200
+ }
201
+ return documents . map ( removeUserId ) ;
202
+ }
203
+
156
204
/**
157
205
* Returns a list of the user's assistant documents (metadata saved to database).
158
206
* @route GET /assistants/documents
159
207
* @returns {AssistantDocument[] } 200 - success response - application/json
160
208
*/
161
209
const getAssistantDocuments = async ( req , res ) => {
162
210
try {
163
- res . json ( await getAssistants ( { user : req . user . id } ) ) ;
211
+ const endpoint = req . query ;
212
+ const assistantsConfig = req . app . locals [ endpoint ] ;
213
+ const documents = await getAssistants (
214
+ { } ,
215
+ {
216
+ user : 1 ,
217
+ assistant_id : 1 ,
218
+ conversation_starters : 1 ,
219
+ createdAt : 1 ,
220
+ updatedAt : 1 ,
221
+ } ,
222
+ ) ;
223
+
224
+ const docs = filterAssistantDocs ( {
225
+ documents,
226
+ userId : req . user . id ,
227
+ assistantsConfig,
228
+ } ) ;
229
+ res . json ( docs ) ;
164
230
} catch ( error ) {
165
231
logger . error ( '[/assistants/documents] Error listing assistant documents' , error ) ;
166
232
res . status ( 500 ) . json ( { error : error . message } ) ;
0 commit comments