Skip to content

Commit 8e47037

Browse files
authored
Merge pull request #375 from harmony-one/hotfix-eventid-5f6bf822
Hotfix eventid 5f6bf822
2 parents fa0e6c2 + 7908c00 commit 8e47037

File tree

23 files changed

+238
-153
lines changed

23 files changed

+238
-153
lines changed

package-lock.json

Lines changed: 32 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
"description": "Harmony One Telegram Bot",
55
"main": "dist/bot.js",
66
"scripts": {
7-
"build": "tsc",
7+
"build": "tsc && npm run sentry:sourcemaps",
88
"start": "node dist/bot.js",
99
"dev": "env-cmd ts-node-dev src/bot.ts",
1010
"test": "jest",
1111
"typeorm": "typeorm-ts-node-commonjs -d src/database/datasource.ts",
1212
"lint": "eslint . --ext .ts",
13-
"sentry:sourcemaps": "sentry-cli sourcemaps inject --org hiddenstate --project harmony-ai-bot ./dist && sentry-cli sourcemaps upload --org hiddenstate --project harmony-ai-bot ./dist",
13+
"sentry:sourcemaps": "sentry-cli sourcemaps inject --org harmony-23 --project harmony1bot ./dist && sentry-cli sourcemaps upload --org harmony-23 --project harmony1bot ./dist",
1414
"prepare": "husky install"
1515
},
1616
"repository": {
@@ -44,7 +44,7 @@
4444
"@babel/core": "^7.22.15",
4545
"@babel/preset-env": "^7.22.15",
4646
"@babel/preset-typescript": "^7.22.15",
47-
"@sentry/cli": "^2.20.7",
47+
"@sentry/cli": "^2.39.1",
4848
"@types/express": "^4.17.14",
4949
"@types/jest": "^29.5.4",
5050
"@types/node": "^18.15.11",
@@ -125,4 +125,4 @@
125125
"web3-utils": "^4.0.5",
126126
"websocket": "^1.0.34"
127127
}
128-
}
128+
}

src/bot.ts

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Sentry } from './monitoring/instrument'
12
import express from 'express'
23
import asyncHandler from 'express-async-handler'
34
import {
@@ -44,9 +45,7 @@ import { run } from '@grammyjs/runner'
4445
import { runBotHeartBit } from './monitoring/monitoring'
4546
import { type BotPaymentLog } from './database/stats.service'
4647
import { TelegramPayments } from './modules/telegram_payment'
47-
import * as Sentry from '@sentry/node'
4848
import * as Events from 'events'
49-
import { ProfilingIntegration } from '@sentry/profiling-node'
5049
import { ES } from './es'
5150
import { hydrateFiles } from '@grammyjs/files'
5251
import { VoiceTranslateBot } from './modules/voice-translate'
@@ -96,18 +95,6 @@ bot.use(
9695
})
9796
)
9897

99-
Sentry.init({
100-
dsn: config.sentry.dsn,
101-
release: config.commitHash,
102-
integrations: [
103-
new ProfilingIntegration()
104-
],
105-
tracesSampleRate: 1.0, // Performance Monitoring. Should use 0.1 in production
106-
profilesSampleRate: 1.0 // Set sampling rate for profiling - this is relative to tracesSampleRate
107-
})
108-
109-
Sentry.setTags({ botName: config.botName })
110-
11198
ES.init()
11299

113100
bot.use(async (ctx: BotContext, next: NextFunction): Promise<void> => {
@@ -126,28 +113,45 @@ bot.use(async (ctx: BotContext, next: NextFunction): Promise<void> => {
126113
paymentFiatCredits: 0
127114
}
128115
}
129-
const transaction = Sentry.startTransaction({ name: 'bot-command' })
130-
const entities = ctx.entities()
116+
131117
const startTime = now()
118+
const entities = ctx.entities()
132119
let command = ''
133-
for (const ent of entities) {
134-
if (ent.type === 'bot_command') {
135-
command = ent.text.substring(1)
136-
const userId = ctx.message?.from?.id
137-
const username = ctx.message?.from?.username
138-
if (userId) {
139-
Sentry.setUser({ id: userId, username })
120+
121+
await Sentry.startSpan(
122+
{
123+
name: 'Bot Command Processing',
124+
op: 'bot.command'
125+
},
126+
async (span) => {
127+
// Process bot commands and set Sentry context
128+
for (const ent of entities) {
129+
if (ent.type === 'bot_command') {
130+
command = ent.text.substring(1)
131+
const userId = ctx.message?.from?.id
132+
const username = ctx.message?.from?.username
133+
if (userId) {
134+
Sentry.setUser({ id: userId, username })
135+
}
136+
if (command) {
137+
Sentry.setTag('command', command)
138+
span?.setTag('command', command)
139+
}
140+
break
141+
}
140142
}
141-
if (command) {
142-
Sentry.setTag('command', command)
143+
144+
try {
145+
await next()
146+
} catch (error) {
147+
if (span) {
148+
span.setStatus('error')
149+
Sentry.captureException(error)
150+
}
151+
throw error
143152
}
144-
// there should be only one bot command
145-
break
146153
}
147-
}
148-
149-
await next()
150-
transaction.finish()
154+
)
151155

152156
if (ctx.transient.analytics.module) {
153157
const userId = Number(ctx.message?.from?.id ?? '0')

src/config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,10 @@ export default {
163163
},
164164
betteruptime: { botHeartBitId: process.env.BOT_HEARTBIT_ID ?? '' },
165165
telegramPayments: { token: process.env.TELEGRAM_PAYMENTS_TOKEN ?? '' },
166-
sentry: { dsn: process.env.SENTRY_DSN },
166+
sentry: {
167+
dsn: process.env.SENTRY_DSN,
168+
env: process.env.SENTRY_ENVIRONMENT ?? 'local'
169+
},
167170
es: {
168171
url: process.env.ES_URL ?? '',
169172
username: process.env.ES_USERNAME ?? '',

src/modules/1country/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { GrammyError, InlineKeyboard } from 'grammy'
22
import { AxiosError } from 'axios'
3-
import * as Sentry from '@sentry/node'
3+
import { Sentry } from '../../monitoring/instrument'
44
import { type Logger, pino } from 'pino'
55

66
import { chatService } from '../../database/services'

src/modules/errorhandler.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AxiosError } from 'axios'
2-
import * as Sentry from '@sentry/node' // Import Sentry for error capturing
2+
import { Sentry } from '../monitoring/instrument'
33
import { RequestState, type OnCallBackQueryData, type OnMessageContext } from './types'
44
import { sleep } from './sd-images/utils'
55
import { type Logger } from 'pino'
@@ -9,8 +9,6 @@ import { now } from '../utils/perf'
99
import OpenAI from 'openai'
1010
import config from '../config'
1111

12-
// const MAX_TRIES = 3 // Define the maximum number of retries
13-
1412
class ErrorHandler {
1513
public maxTries = 3
1614

src/modules/hmny/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { GrammyError } from 'grammy'
2-
import * as Sentry from '@sentry/node'
2+
import { Sentry } from '../../monitoring/instrument'
33
import { type Logger, pino } from 'pino'
44
import {
55
type OnMessageContext,

src/modules/llms/api/openai.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ export const streamChatCompletion = async (
231231

232232
export const streamChatVisionCompletion = async (
233233
ctx: OnMessageContext | OnCallBackQueryData,
234-
model = LlmModelsEnum.GPT_4_VISION,
234+
model = LlmModelsEnum.GPT_4O, // GPT_4_VISION => Deprecated
235235
prompt: string,
236236
imgUrls: string[],
237237
msgId: number,

src/modules/llms/claudeBot.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import {
44
type OnCallBackQueryData,
55
type ChatConversation
66
} from '../types'
7-
import { SupportedCommands } from './utils/helpers'
87
import { type LlmCompletion } from './api/llmApi'
98
import { anthropicCompletion, anthropicStreamCompletion, toolsChatCompletion } from './api/athropic'
109
import { LlmsBase } from './llmsBase'
1110
import { type ModelVersion } from './utils/llmModelsManager'
1211
import { type ModelParameters } from './utils/types'
12+
import { SupportedCommands } from './utils/helpers'
1313

1414
export class ClaudeBot extends LlmsBase {
1515
constructor (payments: BotPayments) {
@@ -25,9 +25,9 @@ export class ClaudeBot extends LlmsBase {
2525
): boolean {
2626
const hasCommand = ctx.hasCommand(this.supportedCommands)
2727

28-
if (ctx.hasCommand(SupportedCommands.new) && this.checkModel(ctx)) {
29-
return true
30-
}
28+
// if (ctx.hasCommand(SupportedCommands.new) && this.checkModel(ctx)) {
29+
// return true
30+
// }
3131
const chatPrefix = this.hasPrefix(ctx.message?.text ?? '')
3232
if (chatPrefix !== '') {
3333
return true
@@ -79,21 +79,18 @@ export class ClaudeBot extends LlmsBase {
7979
return
8080
}
8181

82-
if (
83-
(ctx.hasCommand(SupportedCommands.new) && this.checkModel(ctx))
84-
) {
85-
await this.onStop(ctx)
86-
await this.onChat(ctx, this.modelsEnum.CLAUDE_3_OPUS, true, false)
87-
return
88-
}
89-
9082
const model = this.getModelFromContext(ctx)
9183
if (!model) {
9284
this.logger.warn(`### unsupported model for command ${ctx.message?.text}`)
9385
return
9486
}
95-
this.updateSessionModel(ctx, model.version)
9687

88+
if ((ctx.message?.text ?? '').startsWith(SupportedCommands.c0) || ctx.hasCommand(SupportedCommands.c0)) {
89+
await this.onStop(ctx)
90+
await this.onStop(ctx, 'chatGpt')
91+
}
92+
93+
this.updateSessionModel(ctx, model.version)
9794
const usesTools = ctx.hasCommand([this.commandsEnum.CTOOL, this.commandsEnum.STOOL])
9895
await this.onChat(ctx, model.version, usesTools ? false : this.getStreamOption(model.version), usesTools)
9996
}

src/modules/llms/dalleBot.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
sendMessage
1818
} from './utils/helpers'
1919
import { type LlmCompletion } from './api/llmApi'
20-
import * as Sentry from '@sentry/node'
20+
import { Sentry } from '../../monitoring/instrument'
2121
import { LlmsBase } from './llmsBase'
2222
import config from '../../config'
2323
import { now } from '../../utils/perf'
@@ -356,7 +356,7 @@ export class DalleBot extends LlmsBase {
356356
ctx.message?.reply_to_message?.message_thread_id
357357
})
358358
).message_id
359-
const model = this.modelsEnum.GPT_4_VISION
359+
const model = this.modelsEnum.GPT_4O
360360
const completion = await streamChatVisionCompletion(ctx, model, prompt ?? '', imgList, msgId, true)
361361
if (completion) {
362362
ctx.transient.analytics.sessionState = RequestState.Success

0 commit comments

Comments
 (0)