Skip to content

Commit

Permalink
Merge branch 'master' into invoice_amount_varchar
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/modules/payment/index.ts
  • Loading branch information
ArtemKolodko committed Oct 2, 2023
2 parents 79c0e0d + ce9b9cb commit 18a57ae
Show file tree
Hide file tree
Showing 36 changed files with 1,308 additions and 514 deletions.
60 changes: 30 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"prettier": "2.8.7",
"ts-jest": "^29.1.1",
"ts-node-dev": "^2.0.0",
"typescript": "^4.8.4"
"typescript": "^5.2.2"
},
"dependencies": {
"@elastic/elasticsearch": "^8.9.0",
Expand Down
53 changes: 32 additions & 21 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
type BotSessionData,
type OnCallBackQueryData,
type OnMessageContext, type PayableBot, type PayableBotConfig,
SessionState, type UtilityBot
RequestState, type UtilityBot
} from './modules/types'
import { mainMenu } from './pages'
import { TranslateBot } from './modules/translate/TranslateBot'
Expand Down Expand Up @@ -52,6 +52,8 @@ import { ES } from './es'
import { hydrateFiles } from '@grammyjs/files'
import { VoiceTranslateBot } from './modules/voice-translate'
import { TextToSpeechBot } from './modules/text-to-speech'
import { VoiceToTextBot } from './modules/voice-to-text'
import { now } from './utils/perf'

Events.EventEmitter.defaultMaxListeners = 30

Expand Down Expand Up @@ -102,9 +104,18 @@ Sentry.setTags({ botName: config.botName })
ES.init()

bot.use(async (ctx: BotContext, next: NextFunction): Promise<void> => {
ctx.transient = {
refunded: false,
analytics: {
module: '',
firstResponseTime: 0n,
actualResponseTime: 0n,
sessionState: RequestState.Initial
}
}
const transaction = Sentry.startTransaction({ name: 'bot-command' })
const entities = ctx.entities()
const startTime = process.hrtime.bigint()
const startTime = now()
let command = ''
for (const ent of entities) {
if (ent.type === 'bot_command') {
Expand All @@ -123,28 +134,28 @@ bot.use(async (ctx: BotContext, next: NextFunction): Promise<void> => {
}
await next()
transaction.finish()
if (ctx.session.analytics.module) {
if (ctx.transient.analytics.module) {
const userId = Number(ctx.message?.from?.id ?? '0')
const username = ctx.message?.from?.username ?? ''
if (!ctx.session.analytics.actualResponseTime) {
ctx.session.analytics.actualResponseTime = process.hrtime.bigint()
if (!ctx.transient.analytics.actualResponseTime) {
ctx.transient.analytics.actualResponseTime = now()
}
if (!ctx.session.analytics.firstResponseTime) {
ctx.session.analytics.firstResponseTime = ctx.session.analytics.actualResponseTime
if (!ctx.transient.analytics.firstResponseTime) {
ctx.transient.analytics.firstResponseTime = ctx.transient.analytics.actualResponseTime
}
const firstResponseTime = ((ctx.session.analytics.firstResponseTime - startTime) / 1000n).toString()
const actualResponseTime = ((ctx.session.analytics.actualResponseTime - startTime) / 1000n).toString()
const totalProcessingTime = (process.hrtime.bigint() - startTime).toString()
const totalProcessingTime = (now() - startTime).toString()
const firstResponseTime = (ctx.transient.analytics.firstResponseTime - startTime).toString()
const actualResponseTime = (ctx.transient.analytics.actualResponseTime - startTime).toString()
ES.add({
command,
text: ctx.message?.text ?? '',
module: ctx.session.analytics.module,
module: ctx.transient.analytics.module,
userId,
username,
firstResponseTime,
actualResponseTime,
refunded: ctx.session.refunded,
sessionState: ctx.session.analytics.sessionState,
refunded: ctx.transient.refunded,
sessionState: ctx.transient.analytics.sessionState,
totalProcessingTime
}).catch((ex: any) => {
logger.error({ errorMsg: ex.message }, 'Failed to add data to ES')
Expand Down Expand Up @@ -176,6 +187,11 @@ function createInitialSessionData (): BotSessionData {
languages: [],
enable: false
},
collections: {
activeCollections: [],
collectionRequestQueue: [],
isProcessingQueue: false
},
llms: {
model: config.llms.model,
isEnabled: config.llms.isEnabled,
Expand All @@ -184,13 +200,6 @@ function createInitialSessionData (): BotSessionData {
usage: 0,
isProcessingQueue: false,
requestQueue: []
},
refunded: false,
analytics: {
module: '',
firstResponseTime: 0n,
actualResponseTime: 0n,
sessionState: SessionState.Initial
}
}
}
Expand Down Expand Up @@ -221,10 +230,11 @@ const documentBot = new DocumentHandler()
const telegramPayments = new TelegramPayments(payments)
const voiceTranslateBot = new VoiceTranslateBot(payments)
const textToSpeechBot = new TextToSpeechBot(payments)
const voiceToTextBot = new VoiceToTextBot(payments)

bot.on('message:new_chat_members:me', async (ctx) => {
try {
const accountId = payments.getAccountId(ctx as OnMessageContext)
const accountId = payments.getAccountId(ctx)

const chat = await chatService.getAccountById(accountId)

Expand Down Expand Up @@ -335,6 +345,7 @@ const PayableBots: Record<string, PayableBotConfig> = {
documentBot: { bot: documentBot },
translateBot: { bot: translateBot },
textToSpeech: { bot: textToSpeechBot },
voiceToText: { bot: voiceToTextBot },
openAiBot: {
enabled: (ctx: OnMessageContext) => ctx.session.openAi.imageGen.isEnabled,
bot: openAiBot
Expand Down
3 changes: 2 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export default {
model: 'chat-bison',
minimumBalance: 0,
isEnabled: Boolean(parseInt(process.env.LLMS_ENABLED ?? '1')),
prefixes: { bardPrefix: [',', 'b.', 'B.'] }
prefixes: { bardPrefix: [',', 'b.', 'B.'] },
pdfUrl: process.env.PDF_URL ?? ''
},
openAi: {
dalle: {
Expand Down
Loading

0 comments on commit 18a57ae

Please sign in to comment.