Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
add billing api logic + improve billing alerts components
Ben Fornefelde644a98a1447
diff --git a/apps/web/src/components/Dashboard/BillingAlerts.tsx b/apps/web/src/components/Dashboard/BillingAlerts.tsxindex 6f69061a5..6ce66c08a 100644--- a/apps/web/src/components/Dashboard/BillingAlerts.tsx+++ b/apps/web/src/components/Dashboard/BillingAlerts.tsx@@ -5,10 +5,11 @@ import { useToast } from '../ui/use-toast'import { Button } from '../Button'import { getBillingUrl } from '@/app/(dashboard)/dashboard/utils'import { Team, useUser } from '@/utils/useUser'+import { Loader2 } from 'lucide-react'-interface BillingThreshold {- amount_gte: number | null- alert_amount_percentage: number | null+interface BillingLimit {+ limit_amount_gte: number | null+ alert_amount_gte: number | null}export const BillingAlerts = ({@@ -21,18 +22,36 @@ export const BillingAlerts = ({email: string}) => {const { toast } = useToast()- const [threshold, setThreshold] = useState<BillingThreshold>({- amount_gte: null,- alert_amount_percentage: null,+ const [originalLimits, setOriginalLimits] = useState<BillingLimit>({+ limit_amount_gte: null,+ alert_amount_gte: null,+ })+ const [limits, setLimits] = useState<BillingLimit>({+ limit_amount_gte: null,+ alert_amount_gte: null,+ })+ const [editMode, setEditMode] = useState({+ limit: false,+ alert: false,})const { user } = useUser()+ const [isLoading, setIsLoading] = useState({+ limit: {+ save: false,+ clear: false,+ },+ alert: {+ save: false,+ clear: false,+ },+ })- const fetchBillingThreshold = useCallback(async () => {+ const fetchBillingLimits = useCallback(async () => {if (!user) returntry {const res = await fetch(- getBillingUrl(domain, `/teams/${team.id}/billing-thresholds`),+ getBillingUrl(domain, `/teams/${team.id}/billing-limits`),{headers: {'X-Team-API-Key': team.apiKeys[0],@@ -49,7 +68,8 @@ export const BillingAlerts = ({}const data = await res.json()- setThreshold(data)+ setOriginalLimits(data)+ setLimits(data)} catch (error) {console.error('Error fetching billing threshold:', error)toast({@@ -59,33 +79,48 @@ export const BillingAlerts = ({}}, [user, domain, team, toast])- const updateBillingThreshold = useCallback(async () => {+ const updateBillingLimit = async (type: 'limit' | 'alert') => {if (!user) return+ setIsLoading((prev) => ({+ ...prev,+ [type]: { ...prev[type], save: true },+ }))++ const value =+ type === 'limit' ? limits.limit_amount_gte : limits.alert_amount_gte+try {const res = await fetch(- getBillingUrl(domain, `/teams/${team.id}/billing-thresholds`),+ getBillingUrl(domain, `/teams/${team.id}/billing-limits`),{method: 'PATCH',headers: {'Content-Type': 'application/json','X-Team-API-Key': team.apiKeys[0],},- body: JSON.stringify(threshold),+ body: JSON.stringify({+ [type === 'limit' ? 'limit_amount_gte' : 'alert_amount_gte']: value,+ }),})if (!res.ok) {toast({- title: 'Failed to update billing alerts',- description: 'Unable to save your billing alert settings',+ title: 'Failed to update billing alert',+ description: 'Unable to save your billing alert setting',})return}+ setOriginalLimits((prev) => ({+ ...prev,+ [type === 'limit' ? 'limit_amount_gte' : 'alert_amount_gte']: value,+ }))+toast({- title: 'Billing alerts updated',- description: 'Your billing alert settings have been saved',+ title: 'Billing alert updated',+ description: 'Your billing alert setting has been saved',})} catch (error) {console.error('Error updating billing threshold:', error)@@ -93,79 +128,213 @@ export const BillingAlerts = ({title: 'Error',description: 'Failed to save billing alert settings',})+ } finally {+ setIsLoading((prev) => ({+ ...prev,+ [type]: { ...prev[type], save: false },+ }))+ }+ }++ const deleteBillingLimit = async (type: 'limit' | 'alert') => {+ if (!user) return++ setIsLoading((prev) => ({+ ...prev,+ [type]: { ...prev[type], clear: true },+ }))++ try {+ const res = await fetch(+ getBillingUrl(+ domain,+ `/teams/${team.id}/billing-limits/${+ type === 'limit' ? 'limit_amount_gte' : 'alert_amount_gte'+ }`+ ),+ {+ method: 'DELETE',+ headers: {+ 'X-Team-API-Key': team.apiKeys[0],+ },+ }+ )++ if (!res.ok) {+ toast({+ title: 'Failed to clear billing alert',+ description: 'Unable to clear your billing alert setting',+ })+ return+ }++ setOriginalLimits((prev) => ({+ ...prev,+ [type === 'limit' ? 'limit_amount_gte' : 'alert_amount_gte']: null,+ }))+ setLimits((prev) => ({+ ...prev,+ [type === 'limit' ? 'limit_amount_gte' : 'alert_amount_gte']: null,+ }))++ toast({+ title: 'Billing alert cleared',+ description: 'Your billing alert setting has been cleared',+ })+ } catch (error) {+ console.error('Error clearing billing threshold:', error)+ toast({+ title: 'Error',+ description: 'Failed to clear billing alert settings',+ })+ } finally {+ setIsLoading((prev) => ({+ ...prev,+ [type]: { ...prev[type], clear: false },+ }))}- }, [user, domain, team, threshold, toast])+ }useEffect(() => {- fetchBillingThreshold()- }, [fetchBillingThreshold])+ fetchBillingLimits()+ }, [fetchBillingLimits])- return (- <>- <div className="flex flex-col lg:flex-row gap-6">- <div className="space-y-2 flex-1">- <h3 className="font-medium">Set a Budget Alert</h3>- <p className="text-sm text-white/70">- When your usage reaches this percentage of your budget, you'll- receive an early warning notification to <b>{email}</b>.- </p>-- <div className="relative !mt-4">+ const handleSubmit = async (+ e: React.FormEvent<HTMLFormElement>,+ type: 'limit' | 'alert'+ ) => {+ e.preventDefault()++ await updateBillingLimit(type)+ setEditMode((prev) => ({ ...prev, [type]: false }))+ }++ const renderAmountInput = (type: 'limit' | 'alert') => {+ const value =+ type === 'limit' ? limits.limit_amount_gte : limits.alert_amount_gte+ const originalValue =+ type === 'limit'+ ? originalLimits.limit_amount_gte+ : originalLimits.alert_amount_gte+ const isEditing = type === 'limit' ? editMode.limit : editMode.alert++ const buttonClasses = 'h-9 items-center'++ if (originalValue === null || isEditing) {+ return (+ <div className="flex items-center gap-2">+ <div className="relative"><inputtype="number"- min="1"- max="100"- className="w-full border border-white/10 text-sm focus:outline-none outline-none rounded-md p-2 pr-12"- value={threshold.alert_amount_percentage || ''}+ min="0"+ className="w-[9rem] rounded-md border border-white/10 text-sm focus:outline-none outline-none p-2 pr-6"+ value={value || ''}onChange={(e) =>- setThreshold({- ...threshold,- alert_amount_percentage: Number(e.target.value) || null,+ setLimits({+ ...limits,+ [type === 'limit' ? 'limit_amount_gte' : 'alert_amount_gte']:+ Number(e.target.value) || null,})}- placeholder="Enter percentage"+ placeholder={`${type === 'limit' ? 'Limit' : 'Alert'} Amount`}/><div className="absolute right-3 top-1/2 -translate-y-1/2 text-sm text-white/50">- %+ $</div></div>+ <Button+ type="submit"+ variant="outline"+ disabled={value === originalValue || isLoading[type].save}+ className={buttonClasses}+ >+ {isLoading[type].save ? (+ <Loader2 className="h-4 w-4 animate-spin" />+ ) : (+ 'Save'+ )}+ </Button>+ {originalValue !== null && (+ <Button+ type="button"+ variant="desctructive"+ onClick={() => deleteBillingLimit(type)}+ disabled={isLoading[type].clear}+ className={buttonClasses}+ >+ {isLoading[type].clear ? (+ <Loader2 className="h-4 w-4 animate-spin" />+ ) : (+ 'Clear'+ )}+ </Button>+ )}</div>+ )+ }- <div className="space-y-2 flex-1">- <h3 className="font-medium">Enable Budget Limit</h3>- <p className="text-sm text-white/70">- If your organization exceeds this threshold in a given billing- period,- </p>- <p className="text-sm text-red-400">- Caution: This helps you monitor spending before reaching your budget- limit.- </p>- <div className="relative !mt-4">- <input- type="number"- className="w-full border border-white/10 text-sm focus:outline-none outline-none rounded-md p-2 pr-12"- value={threshold.amount_gte || ''}- onChange={(e) =>- setThreshold({- ...threshold,- amount_gte: Number(e.target.value) || null,- })- }- placeholder="Enter amount"- />- <div className="absolute right-3 top-1/2 -translate-y-1/2 text-sm text-white/50">- USD- </div>- </div>+ return (+ <div className="flex items-center gap-2">+ <div className="text-xs text-white/50 mx-2">+ $+ <span className="text-lg font-semibold text-white">+ {originalValue}+ </span></div>+ <Button+ type="button"+ variant="outline"+ onClick={(e) => {+ e.preventDefault()+ setEditMode((prev) => ({ ...prev, [type]: true }))+ }}+ className={buttonClasses}+ >+ Edit+ </Button>+ <Button+ type="button"+ variant="desctructive"+ onClick={() => deleteBillingLimit(type)}+ disabled={isLoading[type].clear}+ className={buttonClasses}+ >+ {isLoading[type].clear ? (+ <Loader2 className="h-4 w-4 animate-spin" />+ ) : (+ 'Clear'+ )}+ </Button></div>- <Button- onClick={updateBillingThreshold}- className="mt-6 mb-4 float-right"- >- Save Budget Controls- </Button>+ )+ }++ return (+ <>+ <form onSubmit={(e) => handleSubmit(e, 'limit')} className="space-y-2">+ <h3 className="font-medium">Enable Budget Limit</h3>+ <p className="text-sm text-white/70">+ If your organization exceeds this threshold in a given billing period,+ </p>+ <p className="text-sm text-white/70">+ You will automatically receive email notifications when your usage+ reaches 50% and 80% of this limit.+ </p>+ <p className="text-sm text-red-400">+ Caution: This helps you monitor spending before reaching your budget+ limit.+ </p>+ <div className="!mt-4">{renderAmountInput('limit')}</div>+ </form>++ <form onSubmit={(e) => handleSubmit(e, 'alert')} className="space-y-2">+ <h3 className="font-medium">Set an Early Warning Alert</h3>+ <p className="text-sm text-white/70">+ When your usage reaches this amount, you'll receive an early+ warning notification to <b>{email}</b>.+ </p>+ <div className="!mt-4">{renderAmountInput('alert')}</div>+ </form></>)}diff --git a/apps/web/src/components/Dashboard/Usage.tsx b/apps/web/src/components/Dashboard/Usage.tsxindex c29d0cc7e..4205269cc 100644--- a/apps/web/src/components/Dashboard/Usage.tsx+++ b/apps/web/src/components/Dashboard/Usage.tsx@@ -1,5 +1,4 @@import { useEffect, useState } from 'react'-import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'import LineChart from '@/components/Dashboard/Chart'import { toast } from '@/components/ui/use-toast'import { Team } from '@/utils/useUser'@@ -113,19 +112,24 @@ export const UsageContent = ({return (<div className="flex flex-col w-full gap-3">- {/* First Row - Costs and Budget Controls */}- <div className="grid grid-cols-1 lg:grid-cols-2 xl:grid-cols-3 gap-3">- {/* Cost Card - Full width mobile, half tablet, third desktop */}- <Card className="bg-inherit/10 border border-white/20 col-span-2 xl:col-span-1">- <CardHeader className="pb-2">- <CardTitle>Costs in USD</CardTitle>+ {/* Charts Row */}+ <div className="grid grid-cols-1 xl:grid-cols-3 gap-6 gap-y-12">+ {/* Budget Controls */}+ <BillingAlerts team={team} domain={domain} email={team.email} />++ <div className="col-span-1 max-xl:hidden" />++ {/* Cost Section */}+ <div className="bg-inherit/10 rounded-lg">+ <div className="pb-2">+ <h2 className="text-2xl font-semibold">Costs in USD</h2>{!isLoading && costThisMonth && (<p className="text-sm text-white/50">Total costs this month: <b>${costThisMonth?.toFixed(2)}</b></p>)}- </CardHeader>- <CardContent>+ </div>+ <div>{isLoading ? (<ChartSkeleton className="aspect-[21/9] xs:aspect-[2/1] lg:aspect-[16/9]" />) : (@@ -135,34 +139,21 @@ export const UsageContent = ({className="aspect-[21/9] xs:aspect-[2/1] lg:aspect-[16/9]"/>)}- </CardContent>- </Card>-- {/* Budget Controls */}- <Card className="bg-inherit/10 border border-white/20 col-span-2">- <CardHeader className="pb-2">- <CardTitle>Budget Controls</CardTitle>- </CardHeader>- <CardContent>- <BillingAlerts team={team} domain={domain} email={team.email} />- </CardContent>- </Card>- </div>+ </div>+ </div>- {/* Second Row - Resource Usage */}- <div className="grid grid-cols-1 xl:grid-cols-2 gap-3">- {/* vCPU Card - Always half width except mobile */}- <Card className="bg-inherit/10 border border-white/20">- <CardHeader className="pb-2">- <CardTitle>vCPU hours</CardTitle>+ {/* vCPU Section */}+ <div className="bg-inherit/10 rounded-lg">+ <div className="pb-2">+ <h2 className="text-2xl font-semibold">vCPU hours</h2>{!isLoading && vcpuHoursThisMonth && (<p className="text-sm text-white/50">Total vCPU hours this month:{' '}<b>{vcpuHoursThisMonth?.toFixed(2)}</b></p>)}- </CardHeader>- <CardContent>+ </div>+ <div>{isLoading ? (<ChartSkeleton className="aspect-[21/9] xs:aspect-[2/1] lg:aspect-[16/9]" />) : (@@ -172,21 +163,21 @@ export const UsageContent = ({className="aspect-[21/9] xs:aspect-[2/1] lg:aspect-[16/9]"/>)}- </CardContent>- </Card>+ </div>+ </div>- {/* RAM Card - Always half width except mobile */}- <Card className="bg-inherit/10 border border-white/20">- <CardHeader className="pb-2">- <CardTitle>RAM hours</CardTitle>+ {/* RAM Section */}+ <div className="bg-inherit/10 rounded-lg">+ <div className="pb-2">+ <h2 className="text-2xl font-semibold">RAM hours</h2>{!isLoading && ramHoursThisMonth && (<p className="text-sm text-white/50">Total RAM hours this month:{' '}<b>{ramHoursThisMonth?.toFixed(2)}</b></p>)}- </CardHeader>- <CardContent>+ </div>+ <div>{isLoading ? (<ChartSkeleton className="aspect-[21/9] xs:aspect-[2/1] lg:aspect-[16/9]" />) : (@@ -196,8 +187,8 @@ export const UsageContent = ({className="aspect-[21/9] xs:aspect-[2/1] lg:aspect-[16/9]"/>)}- </CardContent>- </Card>+ </div>+ </div></div></div>)diff --git a/apps/web/src/components/ui/alert-dialog.tsx b/apps/web/src/components/ui/alert-dialog.tsxindex c5b9ebc1c..ea4cb7a9f 100644--- a/apps/web/src/components/ui/alert-dialog.tsx+++ b/apps/web/src/components/ui/alert-dialog.tsx@@ -18,7 +18,7 @@ const AlertDialogOverlay = React.forwardRef<>(({ className, ...props }, ref) => (<AlertDialogPrimitive.OverlayclassName={cn(- 'fixed inset-0 z-50 bg-black/80 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0',+ 'fixed inset-0 z-50 bg-black/80 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0',className)}{...props}diff --git a/package.json b/package.jsonindex 57862d8b4..aadc08f87 100644--- a/package.json+++ b/package.json@@ -6,6 +6,7 @@"publish": "changeset publish && pnpm run -r postPublish","test": "pnpm test --recursive --if-present","dev:web": "pnpm --prefix apps/web run dev",+ "dev:build": "pnpm --prefix apps/web run build","rm-node-modules": "find . -name 'node_modules' -type d -prune -exec rm -rf '{}' +","pnpm-install-hack": "cd packages/js-sdk && sed -i '' 's/\"version\": \".*\"/\"version\": \"9.9.9\"/g' package.json && cd ../.. && pnpm i && git checkout -- packages/js-sdk/package.json","generate-sdk-reference": "pnpm --if-present --recursive run generate-sdk-reference"