Skip to content

Commit

Permalink
refactore openai module into a llmsBase dervived subclass + add first…
Browse files Browse the repository at this point in the history
… iteration of url webcraler agent for all ai models
  • Loading branch information
fegloff committed Apr 11, 2024
1 parent f60636b commit 7d431ea
Show file tree
Hide file tree
Showing 30 changed files with 769 additions and 2,305 deletions.
6 changes: 1 addition & 5 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ 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'
import { hasPrefix } from './modules/open-ai/helpers'
import { VoiceToVoiceGPTBot } from './modules/voice-to-voice-gpt'
// import { VoiceCommand } from './modules/voice-command'
import { createInitialSessionData } from './helpers'
Expand Down Expand Up @@ -141,10 +140,7 @@ bot.use(async (ctx: BotContext, next: NextFunction): Promise<void> => {
break
}
}
const prefix = hasPrefix(ctx.message?.text ?? '')
if (!command && prefix) {
command = prefix
}

await next()
transaction.finish()

Expand Down
1 change: 1 addition & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export function createInitialSessionData (): BotSessionData {
currentCollection: '',
collectionConversation: []
},
subagents: { running: [], subagentsRequestQueue: [], isProcessingQueue: false },
llms: {
model: config.llms.model,
isEnabled: config.llms.isEnabled,
Expand Down
4 changes: 2 additions & 2 deletions src/modules/1country/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { appText } from './utils/text'
import { type OnMessageContext, type OnCallBackQueryData, type PayableBot, RequestState } from '../types'
import { type BotPayments } from '../payment'
import { getCommandNamePrompt, getUrl } from './utils/'
import { isAdmin } from '../open-ai/utils/context'
import { MAX_TRIES, isValidUrl, sendMessage } from '../open-ai/helpers'
import { isAdmin } from '../llms/utils/context'
import { sendMessage, isValidUrl, MAX_TRIES } from '../llms/utils/helpers'
import { sleep } from '../sd-images/utils'
import { now } from '../../utils/perf'

Expand Down
120 changes: 120 additions & 0 deletions src/modules/agents/agentBase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import { type Logger, pino } from 'pino'

import { type BotPayments } from '../payment'
import {
type OnMessageContext,
type OnCallBackQueryData,
type PayableBot,
RequestState,
type BotSessionData,
type SubagentSessionData,
type SubagentResult,
SubagentStatus,
type ChatConversation
} from '../types'
import { appText } from '../../utils/text'
import { chatService } from '../../database/services'

import { now } from '../../utils/perf'
import { ErrorHandler } from '../errorhandler'
import { sleep } from '../sd-images/utils'

export abstract class AgentBase implements PayableBot {
private readonly sessionDataKey: string
protected completionContext: string
public agentName: string
protected readonly logger: Logger
protected readonly payments: BotPayments
public module: string
errorHandler: ErrorHandler

constructor (payments: BotPayments,
module: string,
agentName: string,
context: string
) {
this.module = module
this.logger = pino({
name: this.module,
transport: {
target: 'pino-pretty',
options: { colorize: true }
}
})
this.agentName = agentName
this.payments = payments
this.completionContext = context
this.sessionDataKey = 'subagents'
this.errorHandler = new ErrorHandler()
}

public abstract run (ctx: OnMessageContext | OnCallBackQueryData, msg: ChatConversation): Promise<SubagentResult>

protected abstract checkStatus (ctx: OnMessageContext | OnCallBackQueryData, agent: SubagentResult): Promise<SubagentResult>

public abstract onEvent (ctx: OnMessageContext | OnCallBackQueryData, refundCallback: (reason?: string) => void): Promise<void>

public abstract isSupportedEvent (ctx: OnMessageContext | OnCallBackQueryData): boolean

public abstract isSupportedSubagent (ctx: OnMessageContext | OnCallBackQueryData): boolean

public abstract getEstimatedPrice (ctx: any): number

// public abstract getCompletion (ctx: OnMessageContext | OnCallBackQueryData, id: number, completion: string): string
protected getSession (ctx: OnMessageContext | OnCallBackQueryData): SubagentSessionData {
return (ctx.session[this.sessionDataKey as keyof BotSessionData] as SubagentSessionData)
}

public static deleteCompletion (ctx: OnMessageContext | OnCallBackQueryData, id: number): void {
ctx.session.subagents.running = ctx.session.subagents.running.filter(agent => agent.id === id)
}

public static getAgents (ctx: OnMessageContext | OnCallBackQueryData, id: number): SubagentResult[] | undefined {
return ctx.session.subagents.running.filter(agent => agent.id === id)
}

public async onCheckAgentStatus (ctx: OnMessageContext | OnCallBackQueryData): Promise<void> {
const session = this.getSession(ctx)
while (session.subagentsRequestQueue.length > 0) {
try {
const agent = session.subagentsRequestQueue.shift()
if (agent) {
const result = await this.checkStatus(ctx, agent)
if (!result || result.status === SubagentStatus.PROCESSING) {
session.subagentsRequestQueue.push(agent)
await sleep(3000)
} else {
session.running.push(agent)
}
}
ctx.transient.analytics.actualResponseTime = now()
} catch (e: any) {
await this.onError(ctx, e)
}
}
}

async onNotBalanceMessage (ctx: OnMessageContext | OnCallBackQueryData): Promise<string> {
const accountId = this.payments.getAccountId(ctx)
const account = this.payments.getUserAccount(accountId)
const addressBalance = await this.payments.getUserBalance(accountId)
const { totalCreditsAmount } = await chatService.getUserCredits(accountId)
const balance = addressBalance.plus(totalCreditsAmount)
const balanceOne = this.payments.toONE(balance, false).toFixed(2)
const balanceMessage = appText.notEnoughBalance
.replaceAll('$CREDITS', balanceOne)
.replaceAll('$WALLET_ADDRESS', account?.address ?? '')
ctx.transient.analytics.sessionState = RequestState.Success
ctx.transient.analytics.actualResponseTime = now()
return balanceMessage
}

async onError (
ctx: OnMessageContext | OnCallBackQueryData,
e: any,
retryCount: number = this.errorHandler.maxTries,
msg = ''
): Promise<void> {
await this.errorHandler.onError(ctx, e, retryCount, this.logger, msg)
}
}
Loading

0 comments on commit 7d431ea

Please sign in to comment.