-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #251 from giselles-ai/implement-agent-time-record-…
…and-reporting-to-playground-v2 Implement agent time recording and reporting to playground v2
- Loading branch information
Showing
8 changed files
with
126 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
services/agents/activities/types.ts → ...s/activities/deprecated-agent-activity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
export { calculateAgentTimeUsageMs } from "./agent-time-usage"; | ||
export { AGENT_TIME_CHARGE_LIMIT_MINUTES } from "./constants"; | ||
export { AgentActivity } from "./deprecated-agent-activity"; | ||
export { hasEnoughAgentTimeCharge } from "./has-enough-agent-time-charge"; | ||
export { AgentActivity } from "./types"; | ||
export { recordAgentUsage } from "./record-agent-usage"; | ||
export { saveAgentActivity } from "./save-agent-activity"; | ||
export { getMonthlyBillingCycle } from "./utils"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { toUTCDate } from "@/lib/date"; | ||
import { reportAgentTimeUsage } from "@/services/usage-based-billing/report-agent-time-usage"; | ||
import type { AgentId } from "../types"; | ||
import { saveAgentActivity } from "./save-agent-activity"; | ||
|
||
export async function recordAgentUsage( | ||
agentId: AgentId, | ||
startedAt: number, | ||
endedAt: number, | ||
totalDurationMs: number, | ||
) { | ||
const startedAtDateUTC = toUTCDate(new Date(startedAt)); | ||
const endedAtDateUTC = toUTCDate(new Date(endedAt)); | ||
await saveAgentActivity( | ||
agentId, | ||
startedAtDateUTC, | ||
endedAtDateUTC, | ||
totalDurationMs, | ||
); | ||
await reportAgentTimeUsage(endedAtDateUTC); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { agentActivities, agents, db } from "@/drizzle"; | ||
import type { AgentId } from "@/services/agents"; | ||
import { eq } from "drizzle-orm"; | ||
|
||
export async function saveAgentActivity( | ||
agentId: AgentId, | ||
startedAt: Date, | ||
endedAt: Date, | ||
totalDurationMs: number, | ||
) { | ||
const records = await db | ||
.select({ agentDbId: agents.dbId }) | ||
.from(agents) | ||
.where(eq(agents.id, agentId)); | ||
if (records.length === 0) { | ||
throw new Error(`Agent with id ${agentId} not found`); | ||
} | ||
const agentDbId = records[0].agentDbId; | ||
|
||
await db.insert(agentActivities).values({ | ||
agentDbId, | ||
startedAt: startedAt, | ||
endedAt: endedAt, | ||
totalDurationMs: totalDurationMs.toString(), | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,21 @@ | ||
import { Stripe } from "stripe"; | ||
|
||
export const stripe = new Stripe(process.env.STRIPE_SECRET_KEY as string, { | ||
// https://github.com/stripe/stripe-node#configuration | ||
apiVersion: "2024-11-20.acacia", | ||
}); | ||
let stripeInstance: Stripe | null = null; | ||
|
||
const handler: ProxyHandler<Stripe> = { | ||
get: (_target, prop: keyof Stripe | symbol) => { | ||
if (!stripeInstance) { | ||
const key = process.env.STRIPE_SECRET_KEY; | ||
if (!key) { | ||
throw new Error("STRIPE_SECRET_KEY is not configured"); | ||
} | ||
stripeInstance = new Stripe(key, { | ||
// https://github.com/stripe/stripe-node#configuration | ||
apiVersion: "2024-11-20.acacia", | ||
}); | ||
} | ||
return stripeInstance[prop as keyof Stripe]; | ||
}, | ||
}; | ||
|
||
export const stripe: Stripe = new Proxy(new Stripe("dummy"), handler); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { db } from "@/drizzle"; | ||
import { stripe } from "@/services/external/stripe"; | ||
import { fetchCurrentTeam } from "@/services/teams"; | ||
import { processUnreportedActivities } from "@/services/usage-based-billing"; | ||
import { AgentTimeUsageDAO } from "@/services/usage-based-billing/agent-time-usage-dao"; | ||
|
||
export async function reportAgentTimeUsage(targetDate: Date) { | ||
const currentTeam = await fetchCurrentTeam(); | ||
if (currentTeam.activeSubscriptionId == null) { | ||
return; | ||
} | ||
return processUnreportedActivities( | ||
{ | ||
teamDbId: currentTeam.dbId, | ||
targetDate: targetDate, | ||
}, | ||
{ | ||
dao: new AgentTimeUsageDAO(db), | ||
stripe: stripe, | ||
}, | ||
); | ||
} |