Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix eventid 5f6bf822 #375

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 32 additions & 32 deletions package-lock.json

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

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"description": "Harmony One Telegram Bot",
"main": "dist/bot.js",
"scripts": {
"build": "tsc",
"build": "tsc && npm run sentry:sourcemaps",
"start": "node dist/bot.js",
"dev": "env-cmd ts-node-dev src/bot.ts",
"test": "jest",
"typeorm": "typeorm-ts-node-commonjs -d src/database/datasource.ts",
"lint": "eslint . --ext .ts",
"sentry:sourcemaps": "sentry-cli sourcemaps inject --org hiddenstate --project harmony-ai-bot ./dist && sentry-cli sourcemaps upload --org hiddenstate --project harmony-ai-bot ./dist",
"sentry:sourcemaps": "sentry-cli sourcemaps inject --org harmony-23 --project harmony1bot ./dist && sentry-cli sourcemaps upload --org harmony-23 --project harmony1bot ./dist",
"prepare": "husky install"
},
"repository": {
Expand Down Expand Up @@ -44,7 +44,7 @@
"@babel/core": "^7.22.15",
"@babel/preset-env": "^7.22.15",
"@babel/preset-typescript": "^7.22.15",
"@sentry/cli": "^2.20.7",
"@sentry/cli": "^2.39.1",
"@types/express": "^4.17.14",
"@types/jest": "^29.5.4",
"@types/node": "^18.15.11",
Expand Down Expand Up @@ -125,4 +125,4 @@
"web3-utils": "^4.0.5",
"websocket": "^1.0.34"
}
}
}
66 changes: 35 additions & 31 deletions src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Sentry } from './monitoring/instrument'
import express from 'express'
import asyncHandler from 'express-async-handler'
import {
Expand Down Expand Up @@ -44,9 +45,7 @@ import { run } from '@grammyjs/runner'
import { runBotHeartBit } from './monitoring/monitoring'
import { type BotPaymentLog } from './database/stats.service'
import { TelegramPayments } from './modules/telegram_payment'
import * as Sentry from '@sentry/node'
import * as Events from 'events'
import { ProfilingIntegration } from '@sentry/profiling-node'
import { ES } from './es'
import { hydrateFiles } from '@grammyjs/files'
import { VoiceTranslateBot } from './modules/voice-translate'
Expand Down Expand Up @@ -96,18 +95,6 @@ bot.use(
})
)

Sentry.init({
dsn: config.sentry.dsn,
release: config.commitHash,
integrations: [
new ProfilingIntegration()
],
tracesSampleRate: 1.0, // Performance Monitoring. Should use 0.1 in production
profilesSampleRate: 1.0 // Set sampling rate for profiling - this is relative to tracesSampleRate
})

Sentry.setTags({ botName: config.botName })

ES.init()

bot.use(async (ctx: BotContext, next: NextFunction): Promise<void> => {
Expand All @@ -126,28 +113,45 @@ bot.use(async (ctx: BotContext, next: NextFunction): Promise<void> => {
paymentFiatCredits: 0
}
}
const transaction = Sentry.startTransaction({ name: 'bot-command' })
const entities = ctx.entities()

const startTime = now()
const entities = ctx.entities()
let command = ''
for (const ent of entities) {
if (ent.type === 'bot_command') {
command = ent.text.substring(1)
const userId = ctx.message?.from?.id
const username = ctx.message?.from?.username
if (userId) {
Sentry.setUser({ id: userId, username })

await Sentry.startSpan(
{
name: 'Bot Command Processing',
op: 'bot.command'
},
async (span) => {
// Process bot commands and set Sentry context
for (const ent of entities) {
if (ent.type === 'bot_command') {
command = ent.text.substring(1)
const userId = ctx.message?.from?.id
const username = ctx.message?.from?.username
if (userId) {
Sentry.setUser({ id: userId, username })
}
if (command) {
Sentry.setTag('command', command)
span?.setTag('command', command)
}
break
}
}
if (command) {
Sentry.setTag('command', command)

try {
await next()
} catch (error) {
if (span) {
span.setStatus('error')
Sentry.captureException(error)
}
throw error
}
// there should be only one bot command
break
}
}

await next()
transaction.finish()
)

if (ctx.transient.analytics.module) {
const userId = Number(ctx.message?.from?.id ?? '0')
Expand Down
5 changes: 4 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ export default {
},
betteruptime: { botHeartBitId: process.env.BOT_HEARTBIT_ID ?? '' },
telegramPayments: { token: process.env.TELEGRAM_PAYMENTS_TOKEN ?? '' },
sentry: { dsn: process.env.SENTRY_DSN },
sentry: {
dsn: process.env.SENTRY_DSN,
env: process.env.SENTRY_ENVIRONMENT ?? 'local'
},
es: {
url: process.env.ES_URL ?? '',
username: process.env.ES_USERNAME ?? '',
Expand Down
2 changes: 1 addition & 1 deletion src/modules/1country/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GrammyError, InlineKeyboard } from 'grammy'
import { AxiosError } from 'axios'
import * as Sentry from '@sentry/node'
import { Sentry } from '../../monitoring/instrument'
import { type Logger, pino } from 'pino'

import { chatService } from '../../database/services'
Expand Down
4 changes: 1 addition & 3 deletions src/modules/errorhandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AxiosError } from 'axios'
import * as Sentry from '@sentry/node' // Import Sentry for error capturing
import { Sentry } from '../monitoring/instrument'
import { RequestState, type OnCallBackQueryData, type OnMessageContext } from './types'
import { sleep } from './sd-images/utils'
import { type Logger } from 'pino'
Expand All @@ -9,8 +9,6 @@ import { now } from '../utils/perf'
import OpenAI from 'openai'
import config from '../config'

// const MAX_TRIES = 3 // Define the maximum number of retries

class ErrorHandler {
public maxTries = 3

Expand Down
2 changes: 1 addition & 1 deletion src/modules/hmny/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GrammyError } from 'grammy'
import * as Sentry from '@sentry/node'
import { Sentry } from '../../monitoring/instrument'
import { type Logger, pino } from 'pino'
import {
type OnMessageContext,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/llms/api/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export const streamChatCompletion = async (

export const streamChatVisionCompletion = async (
ctx: OnMessageContext | OnCallBackQueryData,
model = LlmModelsEnum.GPT_4_VISION,
model = LlmModelsEnum.GPT_4O, // GPT_4_VISION => Deprecated
prompt: string,
imgUrls: string[],
msgId: number,
Expand Down
23 changes: 10 additions & 13 deletions src/modules/llms/claudeBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import {
type OnCallBackQueryData,
type ChatConversation
} from '../types'
import { SupportedCommands } from './utils/helpers'
import { type LlmCompletion } from './api/llmApi'
import { anthropicCompletion, anthropicStreamCompletion, toolsChatCompletion } from './api/athropic'
import { LlmsBase } from './llmsBase'
import { type ModelVersion } from './utils/llmModelsManager'
import { type ModelParameters } from './utils/types'
import { SupportedCommands } from './utils/helpers'

export class ClaudeBot extends LlmsBase {
constructor (payments: BotPayments) {
Expand All @@ -25,9 +25,9 @@ export class ClaudeBot extends LlmsBase {
): boolean {
const hasCommand = ctx.hasCommand(this.supportedCommands)

if (ctx.hasCommand(SupportedCommands.new) && this.checkModel(ctx)) {
return true
}
// if (ctx.hasCommand(SupportedCommands.new) && this.checkModel(ctx)) {
// return true
// }
const chatPrefix = this.hasPrefix(ctx.message?.text ?? '')
if (chatPrefix !== '') {
return true
Expand Down Expand Up @@ -79,21 +79,18 @@ export class ClaudeBot extends LlmsBase {
return
}

if (
(ctx.hasCommand(SupportedCommands.new) && this.checkModel(ctx))
) {
await this.onStop(ctx)
await this.onChat(ctx, this.modelsEnum.CLAUDE_3_OPUS, true, false)
return
}

const model = this.getModelFromContext(ctx)
if (!model) {
this.logger.warn(`### unsupported model for command ${ctx.message?.text}`)
return
}
this.updateSessionModel(ctx, model.version)

if ((ctx.message?.text ?? '').startsWith(SupportedCommands.c0) || ctx.hasCommand(SupportedCommands.c0)) {
await this.onStop(ctx)
await this.onStop(ctx, 'chatGpt')
}

this.updateSessionModel(ctx, model.version)
const usesTools = ctx.hasCommand([this.commandsEnum.CTOOL, this.commandsEnum.STOOL])
await this.onChat(ctx, model.version, usesTools ? false : this.getStreamOption(model.version), usesTools)
}
Expand Down
4 changes: 2 additions & 2 deletions src/modules/llms/dalleBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
sendMessage
} from './utils/helpers'
import { type LlmCompletion } from './api/llmApi'
import * as Sentry from '@sentry/node'
import { Sentry } from '../../monitoring/instrument'
import { LlmsBase } from './llmsBase'
import config from '../../config'
import { now } from '../../utils/perf'
Expand Down Expand Up @@ -356,7 +356,7 @@ export class DalleBot extends LlmsBase {
ctx.message?.reply_to_message?.message_thread_id
})
).message_id
const model = this.modelsEnum.GPT_4_VISION
const model = this.modelsEnum.GPT_4O
const completion = await streamChatVisionCompletion(ctx, model, prompt ?? '', imgList, msgId, true)
if (completion) {
ctx.transient.analytics.sessionState = RequestState.Success
Expand Down
Loading
Loading