@@ -4,7 +4,7 @@ import { ChatInstance, Prompt, RagIndex, Responsibility } from '../db/models'
4
4
import type { RequestWithUser } from '../types'
5
5
import type { User } from '../../shared/user'
6
6
import { ApplicationError } from '../util/ApplicationError'
7
- import { InferAttributes } from 'sequelize'
7
+ import type { InferAttributes } from 'sequelize'
8
8
9
9
const promptRouter = express . Router ( )
10
10
@@ -51,6 +51,7 @@ const PromptUpdateableParams = z.object({
51
51
systemMessage : z . string ( ) . max ( 20_000 ) ,
52
52
hidden : z . boolean ( ) . default ( false ) ,
53
53
mandatory : z . boolean ( ) . default ( false ) ,
54
+ ragIndexId : z . number ( ) . min ( 1 ) . optional ( ) ,
54
55
} )
55
56
56
57
const PromptCreationParams = z . intersection (
@@ -73,11 +74,6 @@ const PromptCreationParams = z.intersection(
73
74
z . object ( {
74
75
type : z . literal ( 'PERSONAL' ) ,
75
76
} ) ,
76
- z . object ( {
77
- type : z . literal ( 'RAG_INDEX' ) ,
78
- ragIndexId : z . number ( ) . min ( 1 ) ,
79
- chatInstanceId : z . string ( ) . min ( 1 ) . optional ( ) ,
80
- } ) ,
81
77
] ) ,
82
78
)
83
79
@@ -101,14 +97,6 @@ const getPotentialNameConflicts = async (prompt: InferAttributes<Prompt, { omit:
101
97
} ,
102
98
} )
103
99
}
104
- case 'RAG_INDEX' : {
105
- return Prompt . findAll ( {
106
- attributes : [ 'id' , 'name' ] ,
107
- where : {
108
- ragIndexId : prompt . ragIndexId ,
109
- } ,
110
- } )
111
- }
112
100
}
113
101
}
114
102
@@ -138,33 +126,12 @@ const authorizeChatInstancePromptResponsible = async (user: User, prompt: ChatIn
138
126
}
139
127
}
140
128
141
- interface RagIndexPrompt {
142
- ragIndexId : number
143
- chatInstanceId ?: string
144
- }
145
-
146
- const authorizeRagIndexPromptResponsible = async ( user : User , prompt : RagIndexPrompt ) => {
147
- const ragIndex = await RagIndex . findByPk ( prompt . ragIndexId )
148
- const isAuthor = ragIndex ?. userId === user . id
149
-
150
- if ( ! isAuthor && ! user . isAdmin ) {
151
- if ( ! prompt . chatInstanceId ) {
152
- throw ApplicationError . Forbidden ( 'Not allowed' )
153
- }
154
- await authorizeChatInstancePromptResponsible ( user , prompt as ChatInstancePrompt )
155
- }
156
- }
157
-
158
129
const authorizePromptCreation = async ( user : User , promptParams : PromptCreationParamsType ) => {
159
130
switch ( promptParams . type ) {
160
131
case 'CHAT_INSTANCE' : {
161
132
await authorizeChatInstancePromptResponsible ( user , promptParams )
162
133
break
163
134
}
164
- case 'RAG_INDEX' : {
165
- await authorizeRagIndexPromptResponsible ( user , promptParams )
166
- break
167
- }
168
135
case 'PERSONAL' : {
169
136
// This is fine. Anyone can create a personal prompt. Lets just limit the number of prompts per user to 200
170
137
const count = await Prompt . count ( { where : { userId : user . id } } )
@@ -173,6 +140,9 @@ const authorizePromptCreation = async (user: User, promptParams: PromptCreationP
173
140
}
174
141
break
175
142
}
143
+ default : {
144
+ throw ApplicationError . InternalServerError ( 'Unknown prompt type' )
145
+ }
176
146
}
177
147
}
178
148
@@ -200,16 +170,15 @@ const authorizePromptUpdate = async (user: User, prompt: Prompt) => {
200
170
await authorizeChatInstancePromptResponsible ( user , prompt as ChatInstancePrompt )
201
171
break
202
172
}
203
- case 'RAG_INDEX' : {
204
- await authorizeRagIndexPromptResponsible ( user , prompt as RagIndexPrompt )
205
- break
206
- }
207
173
case 'PERSONAL' : {
208
174
if ( user . id !== prompt . userId ) {
209
175
throw ApplicationError . Forbidden ( 'Not allowed' )
210
176
}
211
177
break
212
178
}
179
+ default : {
180
+ throw ApplicationError . InternalServerError ( 'Unknown prompt type' )
181
+ }
213
182
}
214
183
}
215
184
@@ -234,7 +203,7 @@ promptRouter.put('/:id', async (req, res) => {
234
203
const { id } = req . params
235
204
const { user } = req as unknown as RequestWithUser
236
205
const updates = PromptUpdateableParams . parse ( req . body )
237
- const { systemMessage, name, hidden, mandatory } = updates
206
+ const { systemMessage, name, hidden, mandatory, ragIndexId } = updates
238
207
239
208
const prompt = await Prompt . findByPk ( id )
240
209
@@ -249,6 +218,7 @@ promptRouter.put('/:id', async (req, res) => {
249
218
throw ApplicationError . Conflict ( 'Prompt name already exists' )
250
219
}
251
220
221
+ prompt . ragIndexId = ragIndexId
252
222
prompt . systemMessage = systemMessage
253
223
prompt . name = name
254
224
prompt . hidden = hidden
@@ -263,7 +233,14 @@ promptRouter.get('/:id', async (req, res) => {
263
233
const { id } = req . params
264
234
265
235
// Note: we dont have any authorization checks here. Consider?
266
- const prompt = await Prompt . findByPk ( id )
236
+ const prompt = await Prompt . findByPk ( id , {
237
+ include : [
238
+ {
239
+ model : RagIndex ,
240
+ as : 'ragIndex' ,
241
+ } ,
242
+ ] ,
243
+ } )
267
244
268
245
if ( ! prompt ) {
269
246
// We dont throw error here, since this is expected behaviour when the prompt has been deleted but someone still has it in their local storage.
0 commit comments