@@ -11,26 +11,31 @@ import type { ChatMessage } from './chatgpt'
11
11
import { abortChatProcess , chatConfig , chatReplyProcess , containsSensitiveWords , initAuditService } from './chatgpt'
12
12
import { auth , getUserId } from './middleware/auth'
13
13
import { clearApiKeyCache , clearConfigCache , getApiKeys , getCacheApiKeys , getCacheConfig , getOriginConfig } from './storage/config'
14
- import type { AnnounceConfig , AuditConfig , ChatInfo , ChatOptions , Config , GiftCard , KeyConfig , MailConfig , SiteConfig , UserConfig , UserInfo } from './storage/model'
14
+ import type { AnnounceConfig , AuditConfig , ChatInfo , ChatOptions , Config , GiftCard , KeyConfig , MailConfig , SiteConfig , UserConfig , UserInfo , UserPrompt } from './storage/model'
15
15
import { AdvancedConfig , Status , UsageResponse , UserRole } from './storage/model'
16
16
import {
17
17
clearChat ,
18
+ clearUserPrompt ,
18
19
createChatRoom ,
19
20
createUser ,
20
21
deleteAllChatRooms ,
21
22
deleteChat ,
22
23
deleteChatRoom ,
24
+ deleteUserPrompt ,
23
25
disableUser2FA ,
24
26
existsChatRoom ,
25
27
getAmtByCardNo ,
26
28
getChat ,
27
29
getChatRoom ,
28
30
getChatRooms ,
31
+ getChatRoomsCount ,
29
32
getChats ,
30
33
getUser ,
31
34
getUserById ,
35
+ getUserPromptList ,
32
36
getUserStatisticsByDay ,
33
37
getUsers ,
38
+ importUserPrompt ,
34
39
insertChat ,
35
40
insertChatUsage ,
36
41
renameChatRoom ,
@@ -53,6 +58,7 @@ import {
53
58
updateUserPasswordWithVerifyOld ,
54
59
updateUserStatus ,
55
60
upsertKey ,
61
+ upsertUserPrompt ,
56
62
verifyUser ,
57
63
} from './storage/mongo'
58
64
import { authLimiter , limiter } from './middleware/limiter'
@@ -102,6 +108,43 @@ router.get('/chatrooms', auth, async (req, res) => {
102
108
}
103
109
} )
104
110
111
+ function formatTimestamp ( timestamp : number ) {
112
+ const date = new Date ( timestamp )
113
+ const year = date . getFullYear ( )
114
+ const month = String ( date . getMonth ( ) + 1 ) . padStart ( 2 , '0' )
115
+ const day = String ( date . getDate ( ) ) . padStart ( 2 , '0' )
116
+ const hours = String ( date . getHours ( ) ) . padStart ( 2 , '0' )
117
+ const minutes = String ( date . getMinutes ( ) ) . padStart ( 2 , '0' )
118
+ const seconds = String ( date . getSeconds ( ) ) . padStart ( 2 , '0' )
119
+
120
+ return `${ year } -${ month } -${ day } ${ hours } :${ minutes } :${ seconds } `
121
+ }
122
+
123
+ router . get ( '/chatrooms-count' , auth , async ( req , res ) => {
124
+ try {
125
+ const userId = req . query . userId as string
126
+ const page = + req . query . page
127
+ const size = + req . query . size
128
+ const rooms = await getChatRoomsCount ( userId , page , size )
129
+ const result = [ ]
130
+ rooms . data . forEach ( ( r ) => {
131
+ result . push ( {
132
+ uuid : r . roomId ,
133
+ title : r . title ,
134
+ userId : r . userId ,
135
+ name : r . username ,
136
+ lastTime : formatTimestamp ( r . dateTime ) ,
137
+ chatCount : r . chatCount ,
138
+ } )
139
+ } )
140
+ res . send ( { status : 'Success' , message : null , data : { data : result , total : rooms . total } } )
141
+ }
142
+ catch ( error ) {
143
+ console . error ( error )
144
+ res . send ( { status : 'Fail' , message : 'Load error' , data : [ ] } )
145
+ }
146
+ } )
147
+
105
148
router . post ( '/room-create' , auth , async ( req , res ) => {
106
149
try {
107
150
const userId = req . headers . userId as string
@@ -204,17 +247,38 @@ router.get('/chat-history', auth, async (req, res) => {
204
247
const userId = req . headers . userId as string
205
248
const roomId = + req . query . roomId
206
249
const lastId = req . query . lastId as string
207
- if ( ! roomId || ! await existsChatRoom ( userId , roomId ) ) {
250
+ const all = req . query . all as string
251
+ if ( ( ! roomId || ! await existsChatRoom ( userId , roomId ) ) && ( all === null || all === 'undefined' || all === undefined || all . trim ( ) . length === 0 ) ) {
208
252
res . send ( { status : 'Success' , message : null , data : [ ] } )
209
253
return
210
254
}
211
- const chats = await getChats ( roomId , ! isNotEmptyString ( lastId ) ? null : Number . parseInt ( lastId ) )
212
255
256
+ if ( all !== null && all !== 'undefined' && all !== undefined && all . trim ( ) . length !== 0 ) {
257
+ const config = await getCacheConfig ( )
258
+ if ( config . siteConfig . loginEnabled ) {
259
+ try {
260
+ const user = await getUserById ( userId )
261
+ if ( user == null || user . status !== Status . Normal || ! user . roles . includes ( UserRole . Admin ) ) {
262
+ res . send ( { status : 'Fail' , message : '无权限 | No permission.' , data : null } )
263
+ return
264
+ }
265
+ }
266
+ catch ( error ) {
267
+ res . send ( { status : 'Unauthorized' , message : error . message ?? 'Please authenticate.' , data : null } )
268
+ }
269
+ }
270
+ else {
271
+ res . send ( { status : 'Fail' , message : '无权限 | No permission.' , data : null } )
272
+ }
273
+ }
274
+
275
+ const chats = await getChats ( roomId , ! isNotEmptyString ( lastId ) ? null : Number . parseInt ( lastId ) , all )
213
276
const result = [ ]
214
277
chats . forEach ( ( c ) => {
215
278
if ( c . status !== Status . InversionDeleted ) {
216
279
result . push ( {
217
280
uuid : c . uuid ,
281
+ model : c . model ,
218
282
dateTime : new Date ( c . dateTime ) . toLocaleString ( ) ,
219
283
text : c . prompt ,
220
284
images : c . images ,
@@ -413,10 +477,7 @@ router.post('/chat-process', [auth, limiter], async (req, res) => {
413
477
return
414
478
}
415
479
}
416
-
417
- message = regenerate
418
- ? await getChat ( roomId , uuid )
419
- : await insertChat ( uuid , prompt , uploadFileKeys , roomId , options as ChatOptions )
480
+ message = regenerate ? await getChat ( roomId , uuid ) : await insertChat ( uuid , prompt , uploadFileKeys , roomId , model , options as ChatOptions )
420
481
let firstChunk = true
421
482
result = await chatReplyProcess ( {
422
483
message : prompt ,
@@ -476,7 +537,7 @@ router.post('/chat-process', [auth, limiter], async (req, res) => {
476
537
}
477
538
478
539
if ( result . data === undefined )
479
- // eslint-disable-next-line no-unsafe-finally
540
+ // eslint-disable-next-line no-unsafe-finally
480
541
return
481
542
482
543
if ( regenerate && message . options . messageId ) {
@@ -486,6 +547,7 @@ router.post('/chat-process', [auth, limiter], async (req, res) => {
486
547
result . data . text ,
487
548
result . data . id ,
488
549
result . data . conversationId ,
550
+ model ,
489
551
result . data . detail ?. usage as UsageResponse ,
490
552
previousResponse as [ ] )
491
553
}
@@ -494,6 +556,7 @@ router.post('/chat-process', [auth, limiter], async (req, res) => {
494
556
result . data . text ,
495
557
result . data . id ,
496
558
result . data . conversationId ,
559
+ model ,
497
560
result . data . detail ?. usage as UsageResponse )
498
561
}
499
562
@@ -527,7 +590,7 @@ router.post('/chat-abort', [auth, limiter], async (req, res) => {
527
590
text ,
528
591
messageId ,
529
592
conversationId ,
530
- null )
593
+ null , null )
531
594
res . send ( { status : 'Success' , message : 'OK' , data : null } )
532
595
}
533
596
catch ( error ) {
@@ -1352,6 +1415,80 @@ router.post('/statistics/by-day', auth, async (req, res) => {
1352
1415
app . use ( '' , uploadRouter )
1353
1416
app . use ( '/api' , uploadRouter )
1354
1417
1418
+ router . get ( '/prompt-list' , auth , async ( req , res ) => {
1419
+ try {
1420
+ const userId = req . headers . userId as string
1421
+ const prompts = await getUserPromptList ( userId )
1422
+ const result = [ ]
1423
+ prompts . data . forEach ( ( p ) => {
1424
+ result . push ( {
1425
+ _id : p . _id ,
1426
+ title : p . title ,
1427
+ value : p . value ,
1428
+ } )
1429
+ } )
1430
+ res . send ( { status : 'Success' , message : null , data : { data : result , total : prompts . total } } )
1431
+ }
1432
+ catch ( error ) {
1433
+ res . send ( { status : 'Fail' , message : error . message , data : null } )
1434
+ }
1435
+ } )
1436
+
1437
+ router . post ( '/prompt-upsert' , auth , async ( req , res ) => {
1438
+ try {
1439
+ const userId = req . headers . userId as string
1440
+ const userPrompt = req . body as UserPrompt
1441
+ if ( userPrompt . _id !== undefined )
1442
+ userPrompt . _id = new ObjectId ( userPrompt . _id )
1443
+ userPrompt . userId = userId
1444
+ const newUserPrompt = await upsertUserPrompt ( userPrompt )
1445
+ res . send ( { status : 'Success' , message : '成功 | Successfully' , data : { _id : newUserPrompt . _id . toHexString ( ) } } )
1446
+ }
1447
+ catch ( error ) {
1448
+ res . send ( { status : 'Fail' , message : error . message , data : null } )
1449
+ }
1450
+ } )
1451
+
1452
+ router . post ( '/prompt-delete' , auth , async ( req , res ) => {
1453
+ try {
1454
+ const { id } = req . body as { id : string }
1455
+ await deleteUserPrompt ( id )
1456
+ res . send ( { status : 'Success' , message : '成功 | Successfully' } )
1457
+ }
1458
+ catch ( error ) {
1459
+ res . send ( { status : 'Fail' , message : error . message , data : null } )
1460
+ }
1461
+ } )
1462
+
1463
+ router . post ( '/prompt-clear' , auth , async ( req , res ) => {
1464
+ try {
1465
+ const userId = req . headers . userId as string
1466
+ await clearUserPrompt ( userId )
1467
+ res . send ( { status : 'Success' , message : '成功 | Successfully' } )
1468
+ }
1469
+ catch ( error ) {
1470
+ res . send ( { status : 'Fail' , message : error . message , data : null } )
1471
+ }
1472
+ } )
1473
+
1474
+ router . post ( '/prompt-import' , auth , async ( req , res ) => {
1475
+ try {
1476
+ const userId = req . headers . userId as string
1477
+ const userPrompt = req . body as UserPrompt [ ]
1478
+ const updatedUserPrompt = userPrompt . map ( ( prompt ) => {
1479
+ return {
1480
+ ...prompt ,
1481
+ userId,
1482
+ }
1483
+ } )
1484
+ await importUserPrompt ( updatedUserPrompt )
1485
+ res . send ( { status : 'Success' , message : '成功 | Successfully' } )
1486
+ }
1487
+ catch ( error ) {
1488
+ res . send ( { status : 'Fail' , message : error . message , data : null } )
1489
+ }
1490
+ } )
1491
+
1355
1492
app . use ( '' , router )
1356
1493
app . use ( '/api' , router )
1357
1494
0 commit comments