Skip to content

Commit

Permalink
add Message too long handler for web crawling + limit url reply react…
Browse files Browse the repository at this point in the history
…ion to created collections
  • Loading branch information
fegloff committed Oct 10, 2023
1 parent c97b5cb commit 1949726
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ export default {
process.env.PAYMENT_HOLDER_ADDRESS ??
'0x9EE59D58606997AAFd2F6Ba46EC64402829f9b6C',
whitelist: (process.env.PAYMENT_WHITELIST ?? '')
.split(',')
.map((item) => item.toString().toLowerCase()),
groupWhitelist: (process.env.PAYMENT_GROUP_WHITELIST ?? '')
.split(',')
.map((item) => item.toString().toLowerCase())
},
Expand Down
33 changes: 13 additions & 20 deletions src/modules/llms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,6 @@ export class LlmsBot implements PayableBot {
const collection = ctx.session.collections.activeCollections.find(c => c.fileName === fileName)
if (collection) {
await this.queryUrlCollection(ctx, collection.url, prompt)
} else {
if (!ctx.session.collections.isProcessingQueue) {
ctx.session.collections.isProcessingQueue = true
await this.onCheckCollectionStatus(ctx).then(() => {
ctx.session.collections.isProcessingQueue = false
})
}
}
}
ctx.transient.analytics.actualResponseTime = now()
Expand All @@ -214,17 +207,7 @@ export class LlmsBot implements PayableBot {
const prompt = ctx.message?.text ?? 'summarize'
const collection = ctx.session.collections.activeCollections.find(c => c.url === url)
const newPrompt = `${prompt}` // ${url}
if (!collection) {
if (ctx.chat?.id) {
await addUrlToCollection(ctx, ctx.chat?.id, url, newPrompt)
if (!ctx.session.collections.isProcessingQueue) {
ctx.session.collections.isProcessingQueue = true
await this.onCheckCollectionStatus(ctx).then(() => {
ctx.session.collections.isProcessingQueue = false
})
}
}
} else {
if (collection) {
await this.queryUrlCollection(ctx, url, newPrompt)
}
ctx.transient.analytics.actualResponseTime = now()
Expand Down Expand Up @@ -321,6 +304,9 @@ export class LlmsBot implements PayableBot {
if (
!(await this.payments.pay(ctx as OnMessageContext, response.price))
) {
if (ctx.chat?.id) {
await ctx.api.deleteMessage(ctx.chat?.id, msgId)
}
await this.onNotBalanceMessage(ctx)
} else {
conversation.push({
Expand All @@ -333,15 +319,22 @@ export class LlmsBot implements PayableBot {
await ctx.api.editMessageText(ctx.chat?.id ?? '',
msgId, response.completion,
{ parse_mode: 'Markdown', disable_web_page_preview: true })
.catch(async (e) => { await this.onError(ctx, e) })
ctx.session.collections.collectionConversation = [...conversation]
}
}
ctx.transient.analytics.actualResponseTime = now()
} catch (e: any) {
Sentry.captureException(e)
ctx.transient.analytics.sessionState = RequestState.Error
if (e instanceof AxiosError) {
if (e instanceof GrammyError && e.error_code === 400 &&
(e.description.includes('MESSAGE_TOO_LONG') || e.description.includes('find end of the entity starting at byte offset'))) {
await sendMessage(
ctx,
'Error: Completion message too long. Please try again'
)
ctx.session.collections.collectionConversation = []
ctx.transient.analytics.actualResponseTime = now()
} else if (e instanceof AxiosError) {
if (e.message.includes('404')) {
ctx.session.collections.activeCollections =
[...ctx.session.collections.activeCollections.filter(c => c.url !== url.toLocaleLowerCase())]
Expand Down
7 changes: 7 additions & 0 deletions src/modules/payment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,13 @@ export class BotPayments {
)
}

public isGroupInWhitelist (chatId: number | string): boolean {
const { groupWhitelist } = config.payment
return (
groupWhitelist.includes(chatId.toString())
)
}

public isPaymentsEnabled (): boolean {
return Boolean(
config.payment.isEnabled &&
Expand Down

0 comments on commit 1949726

Please sign in to comment.