Skip to content

Commit f377c99

Browse files
Refactor message-cost-tracker.ts to pass logger as parameter
🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
1 parent 54b932e commit f377c99

File tree

1 file changed

+52
-36
lines changed

1 file changed

+52
-36
lines changed

backend/src/llm-apis/message-cost-tracker.ts

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { WebSocket } from 'ws'
1313

1414
import { getRequestContext } from '../context/app-context'
1515
import { logger, withLoggerContext } from '../util/logger'
16+
import type { Logger } from '@codebuff/types/logger'
1617
import { stripNullCharsFromObject } from '../util/object'
1718
import { SWITCHBOARD } from '../websockets/server'
1819
import { sendAction } from '../websockets/websocket-action'
@@ -212,13 +213,14 @@ const calcCost = (
212213

213214
const VERBOSE = false
214215

215-
async function syncMessageToStripe(messageData: {
216+
async function syncMessageToStripe(params: {
216217
messageId: string
217218
userId: string
218219
costInCents: number
219220
finishedAt: Date
221+
logger: Logger
220222
}) {
221-
const { messageId, userId, costInCents, finishedAt } = messageData
223+
const { messageId, userId, costInCents, finishedAt, logger } = params
222224

223225
if (!userId || userId === TEST_USER_ID) {
224226
if (VERBOSE) {
@@ -329,25 +331,27 @@ type InsertMessageParams = {
329331
latencyMs: number
330332
}
331333

332-
export async function insertMessageRecordWithRetries(
333-
params: InsertMessageParams,
334-
maxRetries = 3,
335-
): Promise<typeof schema.message.$inferSelect | null> {
334+
export async function insertMessageRecordWithRetries(params: {
335+
messageParams: InsertMessageParams
336+
logger: Logger
337+
maxRetries?: number
338+
}): Promise<typeof schema.message.$inferSelect | null> {
339+
const { messageParams, logger, maxRetries = 3 } = params
336340
for (let attempt = 1; attempt <= maxRetries; attempt++) {
337341
try {
338-
return await insertMessageRecord(params)
342+
return await insertMessageRecord(messageParams)
339343
} catch (error) {
340344
if (attempt === maxRetries) {
341345
logger.error(
342-
{ messageId: params.messageId, error, attempt },
346+
{ messageId: messageParams.messageId, error, attempt },
343347
`Failed to save message after ${maxRetries} attempts`,
344348
)
345349
return null
346350
// TODO: Consider rethrowing the error, if we are losing too much money.
347351
// throw error
348352
} else {
349353
logger.warn(
350-
{ messageId: params.messageId, error: error },
354+
{ messageId: messageParams.messageId, error: error },
351355
`Retrying save message to DB (attempt ${attempt}/${maxRetries})`,
352356
)
353357
await new Promise((resolve) => setTimeout(resolve, 1000 * attempt))
@@ -416,12 +420,14 @@ async function insertMessageRecord(
416420
return insertResult[0]
417421
}
418422

419-
async function sendCostResponseToClient(
420-
clientSessionId: string,
421-
userInputId: string,
422-
creditsUsed: number,
423-
agentId?: string,
424-
): Promise<void> {
423+
async function sendCostResponseToClient(params: {
424+
clientSessionId: string
425+
userInputId: string
426+
creditsUsed: number
427+
agentId?: string
428+
logger: Logger
429+
}): Promise<void> {
430+
const { clientSessionId, userInputId, creditsUsed, agentId, logger } = params
425431
try {
426432
const clientEntry = Array.from(SWITCHBOARD.clients.entries()).find(
427433
([_, state]: [WebSocket, ClientState]) =>
@@ -462,17 +468,19 @@ type CreditConsumptionResult = {
462468
fromPurchased: number
463469
}
464470

465-
async function updateUserCycleUsageWithRetries(
466-
userId: string,
467-
creditsUsed: number,
468-
maxRetries = 3,
469-
): Promise<CreditConsumptionResult> {
471+
async function updateUserCycleUsageWithRetries(params: {
472+
userId: string
473+
creditsUsed: number
474+
logger: Logger
475+
maxRetries?: number
476+
}): Promise<CreditConsumptionResult> {
477+
const { userId, creditsUsed, logger, maxRetries = 3 } = params
470478
const requestContext = getRequestContext()
471479
const orgId = requestContext?.approvedOrgIdForRepo
472480

473481
for (let attempt = 1; attempt <= maxRetries; attempt++) {
474482
try {
475-
return await updateUserCycleUsage(userId, creditsUsed)
483+
return await updateUserCycleUsage({ userId, creditsUsed, logger })
476484
} catch (error) {
477485
if (attempt === maxRetries) {
478486
logger.error(
@@ -496,10 +504,12 @@ async function updateUserCycleUsageWithRetries(
496504
throw new Error('Failed to update user cycle usage after all attempts.')
497505
}
498506

499-
async function updateUserCycleUsage(
500-
userId: string,
501-
creditsUsed: number,
502-
): Promise<CreditConsumptionResult> {
507+
async function updateUserCycleUsage(params: {
508+
userId: string
509+
creditsUsed: number
510+
logger: Logger
511+
}): Promise<CreditConsumptionResult> {
512+
const { userId, creditsUsed, logger } = params
503513
if (creditsUsed <= 0) {
504514
if (VERBOSE) {
505515
logger.debug(
@@ -652,17 +662,21 @@ export const saveMessage = async (value: {
652662
)
653663
}
654664

655-
sendCostResponseToClient(
656-
value.clientSessionId,
657-
value.userInputId,
665+
sendCostResponseToClient({
666+
clientSessionId: value.clientSessionId,
667+
userInputId: value.userInputId,
658668
creditsUsed,
659-
value.agentId,
660-
)
669+
agentId: value.agentId,
670+
logger,
671+
})
661672

662673
await insertMessageRecordWithRetries({
663-
...value,
664-
cost,
665-
creditsUsed,
674+
messageParams: {
675+
...value,
676+
cost,
677+
creditsUsed,
678+
},
679+
logger,
666680
})
667681

668682
if (!value.userId) {
@@ -673,10 +687,11 @@ export const saveMessage = async (value: {
673687
return 0
674688
}
675689

676-
const consumptionResult = await updateUserCycleUsageWithRetries(
677-
value.userId,
690+
const consumptionResult = await updateUserCycleUsageWithRetries({
691+
userId: value.userId,
678692
creditsUsed,
679-
)
693+
logger,
694+
})
680695

681696
// Only sync the portion from purchased credits to Stripe
682697
if (consumptionResult.fromPurchased > 0) {
@@ -688,6 +703,7 @@ export const saveMessage = async (value: {
688703
userId: value.userId,
689704
costInCents: purchasedCostInCents,
690705
finishedAt: value.finishedAt,
706+
logger,
691707
}).catch((syncError) => {
692708
logger.error(
693709
{ messageId: value.messageId, error: syncError },

0 commit comments

Comments
 (0)