@@ -2,7 +2,6 @@ import { GrammyError, InlineKeyboard } from 'grammy'
2
2
import OpenAI from 'openai'
3
3
import { type Logger , pino } from 'pino'
4
4
5
- import { getCommandNamePrompt } from '../1country/utils'
6
5
import { type BotPayments } from '../payment'
7
6
import {
8
7
type ChatConversation ,
@@ -76,7 +75,7 @@ export class OpenAIBot implements PayableBot {
76
75
ctx : OnMessageContext | OnCallBackQueryData
77
76
) : boolean {
78
77
const hasCommand = ctx . hasCommand (
79
- Object . values ( SupportedCommands ) . map ( ( command ) => command . name )
78
+ Object . values ( SupportedCommands ) . map ( ( command ) => command )
80
79
)
81
80
if ( isMentioned ( ctx ) ) {
82
81
return true
@@ -104,18 +103,18 @@ export class OpenAIBot implements PayableBot {
104
103
return 0
105
104
}
106
105
if (
107
- ctx . hasCommand ( [ SupportedCommands . dalle . name ,
108
- SupportedCommands . dalleImg . name ,
109
- SupportedCommands . dalleShort . name ,
110
- SupportedCommands . dalleShorter . name ] )
106
+ ctx . hasCommand ( [ SupportedCommands . dalle ,
107
+ SupportedCommands . dalleImg ,
108
+ SupportedCommands . dalleShort ,
109
+ SupportedCommands . dalleShorter ] )
111
110
) {
112
111
const imageNumber = ctx . session . openAi . imageGen . numImages
113
112
const imageSize = ctx . session . openAi . imageGen . imgSize
114
113
const model = getDalleModel ( imageSize )
115
114
const price = getDalleModelPrice ( model , true , imageNumber ) // cents
116
115
return price * priceAdjustment
117
116
}
118
- if ( ctx . hasCommand ( SupportedCommands . genImgEn . name ) ) {
117
+ if ( ctx . hasCommand ( SupportedCommands . genImgEn ) ) {
119
118
const imageNumber = ctx . session . openAi . imageGen . numImages
120
119
const imageSize = ctx . session . openAi . imageGen . imgSize
121
120
const chatModelName = ctx . session . openAi . chatGpt . model
@@ -147,7 +146,7 @@ export class OpenAIBot implements PayableBot {
147
146
const prompt = ctx . message ?. caption ?? ctx . message ?. text
148
147
if ( prompt && ! isNaN ( + prompt ) ) { // && !isNaN(+prompt)
149
148
return true
150
- } else if ( prompt && ( ctx . chat ?. type === 'private' || ctx . hasCommand ( SupportedCommands . vision . name ) ) ) {
149
+ } else if ( prompt && ( ctx . chat ?. type === 'private' || ctx . hasCommand ( SupportedCommands . vision ) ) ) {
151
150
return true
152
151
}
153
152
}
@@ -182,7 +181,7 @@ export class OpenAIBot implements PayableBot {
182
181
}
183
182
184
183
if (
185
- ctx . hasCommand ( SupportedCommands . chat . name ) ||
184
+ ctx . hasCommand ( SupportedCommands . chat ) ||
186
185
( ctx . message ?. text ?. startsWith ( 'chat ' ) && ctx . chat ?. type === 'private' )
187
186
) {
188
187
ctx . session . openAi . chatGpt . model = ChatGPTModelsEnum . GPT_4
@@ -191,49 +190,48 @@ export class OpenAIBot implements PayableBot {
191
190
}
192
191
193
192
if (
194
- ctx . hasCommand ( SupportedCommands . new . name ) ||
193
+ ctx . hasCommand ( SupportedCommands . new ) ||
195
194
( ctx . message ?. text ?. startsWith ( 'new ' ) && ctx . chat ?. type === 'private' )
196
195
) {
197
- ctx . session . openAi . chatGpt . model = ChatGPTModelsEnum . GPT_4
198
196
await this . onEnd ( ctx )
199
197
await this . onChat ( ctx )
200
198
return
201
199
}
202
200
203
201
if (
204
- ctx . hasCommand ( SupportedCommands . ask . name ) ||
202
+ ctx . hasCommand ( SupportedCommands . ask ) ||
205
203
( ctx . message ?. text ?. startsWith ( 'ask ' ) && ctx . chat ?. type === 'private' )
206
204
) {
207
205
ctx . session . openAi . chatGpt . model = ChatGPTModelsEnum . GPT_4
208
206
await this . onChat ( ctx )
209
207
return
210
208
}
211
209
212
- if ( ctx . hasCommand ( SupportedCommands . ask35 . name ) ) {
210
+ if ( ctx . hasCommand ( SupportedCommands . ask35 ) ) {
213
211
ctx . session . openAi . chatGpt . model = ChatGPTModelsEnum . GPT_35_TURBO_16K
214
212
await this . onChat ( ctx )
215
213
return
216
214
}
217
215
218
- if ( ctx . hasCommand ( SupportedCommands . gpt4 . name ) ) {
216
+ if ( ctx . hasCommand ( SupportedCommands . gpt4 ) ) {
219
217
ctx . session . openAi . chatGpt . model = ChatGPTModelsEnum . GPT_4
220
218
await this . onChat ( ctx )
221
219
return
222
220
}
223
221
224
- if ( ctx . hasCommand ( SupportedCommands . gpt . name ) ) {
222
+ if ( ctx . hasCommand ( SupportedCommands . gpt ) ) {
225
223
ctx . session . openAi . chatGpt . model = ChatGPTModelsEnum . GPT_4
226
224
await this . onChat ( ctx )
227
225
return
228
226
}
229
227
230
- if ( ctx . hasCommand ( SupportedCommands . ask32 . name ) ) {
228
+ if ( ctx . hasCommand ( SupportedCommands . ask32 ) ) {
231
229
ctx . session . openAi . chatGpt . model = ChatGPTModelsEnum . GPT_4_32K
232
230
await this . onChat ( ctx )
233
231
return
234
232
}
235
233
236
- if ( ctx . hasCommand ( SupportedCommands . vision . name ) ) {
234
+ if ( ctx . hasCommand ( SupportedCommands . vision ) ) {
237
235
const photoUrl = getUrlFromText ( ctx )
238
236
if ( photoUrl ) {
239
237
const prompt = ctx . match
@@ -252,10 +250,10 @@ export class OpenAIBot implements PayableBot {
252
250
}
253
251
254
252
if (
255
- ctx . hasCommand ( [ SupportedCommands . dalle . name ,
256
- SupportedCommands . dalleImg . name ,
257
- SupportedCommands . dalleShort . name ,
258
- SupportedCommands . dalleShorter . name ] ) ||
253
+ ctx . hasCommand ( [ SupportedCommands . dalle ,
254
+ SupportedCommands . dalleImg ,
255
+ SupportedCommands . dalleShort ,
256
+ SupportedCommands . dalleShorter ] ) ||
259
257
( ctx . message ?. text ?. startsWith ( 'image ' ) && ctx . chat ?. type === 'private' )
260
258
) {
261
259
let prompt = ( ctx . match ? ctx . match : ctx . message ?. text ) as string
@@ -280,16 +278,16 @@ export class OpenAIBot implements PayableBot {
280
278
return
281
279
}
282
280
283
- if ( ctx . hasCommand ( SupportedCommands . last . name ) ) {
281
+ if ( ctx . hasCommand ( SupportedCommands . last ) ) {
284
282
await this . onLast ( ctx )
285
283
return
286
284
}
287
285
288
286
const text = ctx . message ?. text ?? ''
289
-
290
- if ( hasNewPrefix ( text ) !== '' ) {
287
+ const newPrefix = hasNewPrefix ( text )
288
+ if ( newPrefix !== '' ) {
291
289
await this . onEnd ( ctx )
292
- await this . onPrefix ( ctx )
290
+ await this . onPrefix ( ctx , newPrefix )
293
291
return
294
292
}
295
293
@@ -311,9 +309,9 @@ export class OpenAIBot implements PayableBot {
311
309
}
312
310
return
313
311
}
314
-
315
- if ( hasChatPrefix ( text ) !== '' ) {
316
- await this . onPrefix ( ctx )
312
+ const prefix = hasChatPrefix ( text )
313
+ if ( prefix !== '' ) {
314
+ await this . onPrefix ( ctx , prefix )
317
315
return
318
316
}
319
317
@@ -426,7 +424,7 @@ export class OpenAIBot implements PayableBot {
426
424
}
427
425
}
428
426
429
- async onPrefix ( ctx : OnMessageContext | OnCallBackQueryData ) : Promise < void > {
427
+ async onPrefix ( ctx : OnMessageContext | OnCallBackQueryData , prefix : string ) : Promise < void > {
430
428
try {
431
429
if ( this . botSuspended ) {
432
430
ctx . transient . analytics . sessionState = RequestState . Error
@@ -436,13 +434,9 @@ export class OpenAIBot implements PayableBot {
436
434
ctx . transient . analytics . actualResponseTime = now ( )
437
435
return
438
436
}
439
- const { prompt } = getCommandNamePrompt (
440
- ctx ,
441
- SupportedCommands
442
- )
443
- const prefix = hasPrefix ( prompt )
437
+ const prompt = ctx . message ?. text ?. slice ( prefix . length ) ?? ''
444
438
ctx . session . openAi . chatGpt . requestQueue . push (
445
- await preparePrompt ( ctx , prompt . slice ( prefix . length ) )
439
+ await preparePrompt ( ctx , prompt )
446
440
)
447
441
if ( ! ctx . session . openAi . chatGpt . isProcessingQueue ) {
448
442
ctx . session . openAi . chatGpt . isProcessingQueue = true
@@ -750,6 +744,7 @@ export class OpenAIBot implements PayableBot {
750
744
}
751
745
752
746
async onEnd ( ctx : OnMessageContext | OnCallBackQueryData ) : Promise < void > {
747
+ ctx . session . openAi . chatGpt . model = ChatGPTModelsEnum . GPT_4
753
748
ctx . session . openAi . chatGpt . chatConversation = [ ]
754
749
ctx . session . openAi . chatGpt . usage = 0
755
750
ctx . session . openAi . chatGpt . price = 0
0 commit comments