log billing usage

This commit is contained in:
Ramnique Singh 2025-08-05 17:17:42 +05:30
parent 7592df8068
commit b3a245484f

View file

@ -1,6 +1,6 @@
import { Turn, TurnEvent } from "@/src/entities/models/turn"; import { Turn, TurnEvent } from "@/src/entities/models/turn";
import { USE_BILLING } from "@/app/lib/feature_flags"; import { USE_BILLING } from "@/app/lib/feature_flags";
import { authorize, getCustomerIdForProject } from "@/app/lib/billing"; import { authorize, getCustomerIdForProject, logUsage } from "@/app/lib/billing";
import { NotFoundError } from '@/src/entities/errors/common'; import { NotFoundError } from '@/src/entities/errors/common';
import { IConversationsRepository } from "@/src/application/repositories/conversations.repository.interface"; import { IConversationsRepository } from "@/src/application/repositories/conversations.repository.interface";
import { streamResponse } from "@/app/lib/agents"; import { streamResponse } from "@/app/lib/agents";
@ -63,14 +63,15 @@ export class RunConversationTurnUseCase implements IRunConversationTurnUseCase {
await this.usageQuotaPolicy.assertAndConsume(projectId); await this.usageQuotaPolicy.assertAndConsume(projectId);
// Check billing auth // Check billing auth
let billingCustomerId: string | null = null;
if (USE_BILLING) { if (USE_BILLING) {
// get billing customer id for project // get billing customer id for project
const customerId = await getCustomerIdForProject(projectId); billingCustomerId = await getCustomerIdForProject(projectId);
const agentModels = conversation.workflow.agents.reduce((acc, agent) => { const agentModels = conversation.workflow.agents.reduce((acc, agent) => {
acc.push(agent.model); acc.push(agent.model);
return acc; return acc;
}, [] as string[]); }, [] as string[]);
const response = await authorize(customerId, { const response = await authorize(billingCustomerId, {
type: 'agent_response', type: 'agent_response',
data: { data: {
agentModels, agentModels,
@ -141,5 +142,13 @@ export class RunConversationTurnUseCase implements IRunConversationTurnUseCase {
} }
} }
} }
// Log billing usage
if (USE_BILLING && billingCustomerId) {
await logUsage(billingCustomerId, {
type: "agent_messages",
amount: outputMessages.length,
});
}
} }
} }