Skip to content

Commit a1b4fb2

Browse files
committed
Fix billing cost attribution and extract hexToRgba utility
- Fix copilot billing counters to only increment for copilot/mcp_copilot sources Previously, workspace-chat and mothership_block sources were incorrectly attributed to copilot-specific counters (totalCopilotCost, etc.) - Extract hexToRgba utility from features.tsx and templates.tsx to shared lib/core/utils/color.ts to eliminate duplication
1 parent b07925f commit a1b4fb2

4 files changed

Lines changed: 22 additions & 18 deletions

File tree

‎apps/sim/app/(home)/components/features/features.tsx‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@
33
import{useState}from'react'
44
importImagefrom'next/image'
55
import{Badge}from'@/components/emcn'
6-
7-
functionhexToRgba(hex: string,alpha: number): string{
8-
constr=Number.parseInt(hex.slice(1,3),16)
9-
constg=Number.parseInt(hex.slice(3,5),16)
10-
constb=Number.parseInt(hex.slice(5,7),16)
11-
return`rgba(${r},${g},${b},${alpha})`
12-
}
6+
import{hexToRgba}from'@/lib/core/utils/color'
137

148
constFEATURE_TABS=[
159
{

‎apps/sim/app/(home)/components/templates/templates.tsx‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import dynamic from 'next/dynamic'
66
importLinkfrom'next/link'
77
import{Badge,ChevronDown}from'@/components/emcn'
88
import{cn}from'@/lib/core/utils/cn'
9+
import{hexToRgba}from'@/lib/core/utils/color'
910
import{TEMPLATE_WORKFLOWS}from'@/app/(home)/components/templates/template-workflows'
1011

1112
constLandingPreviewWorkflow=dynamic(
@@ -19,13 +20,6 @@ const LandingPreviewWorkflow = dynamic(
1920
}
2021
)
2122

22-
functionhexToRgba(hex: string,alpha: number): string{
23-
constr=Number.parseInt(hex.slice(1,3),16)
24-
constg=Number.parseInt(hex.slice(3,5),16)
25-
constb=Number.parseInt(hex.slice(5,7),16)
26-
return`rgba(${r},${g},${b},${alpha})`
27-
}
28-
2923
constLEFT_WALL_CLIP='polygon(0 8px, 100% 0, 100% 100%, 0 100%)'
3024
constBOTTOM_WALL_CLIP='polygon(0 0, 100% 0, calc(100% - 8px) 100%, 0 100%)'
3125

‎apps/sim/app/api/billing/update-cost/route.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export async function POST(req: NextRequest) {
7878
}
7979

8080
const{ userId, cost, model, inputTokens, outputTokens, source }=validation.data
81+
constisCopilot=source==='copilot'||source==='mcp_copilot'
8182
constisMcp=source==='mcp_copilot'
8283

8384
logger.info(`[${requestId}] Processing cost update`,{
@@ -105,13 +106,16 @@ export async function POST(req: NextRequest) {
105106
constupdateFields: Record<string,unknown>={
106107
totalCost: sql`total_cost + ${cost}`,
107108
currentPeriodCost: sql`current_period_cost + ${cost}`,
108-
totalCopilotCost: sql`total_copilot_cost + ${cost}`,
109-
currentPeriodCopilotCost: sql`current_period_copilot_cost + ${cost}`,
110-
totalCopilotCalls: sql`total_copilot_calls + 1`,
111-
totalCopilotTokens: sql`total_copilot_tokens + ${totalTokens}`,
112109
lastActive: newDate(),
113110
}
114111

112+
if(isCopilot){
113+
updateFields.totalCopilotCost=sql`total_copilot_cost + ${cost}`
114+
updateFields.currentPeriodCopilotCost=sql`current_period_copilot_cost + ${cost}`
115+
updateFields.totalCopilotCalls=sql`total_copilot_calls + 1`
116+
updateFields.totalCopilotTokens=sql`total_copilot_tokens + ${totalTokens}`
117+
}
118+
115119
if(isMcp){
116120
updateFields.totalMcpCopilotCost=sql`total_mcp_copilot_cost + ${cost}`
117121
updateFields.currentPeriodMcpCopilotCost=sql`current_period_mcp_copilot_cost + ${cost}`

‎apps/sim/lib/core/utils/color.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Converts a hex color string to an rgba string with the specified alpha.
3+
* @param hex - Hex color string (e.g., '#FF0000')
4+
* @param alpha - Alpha value between 0 and 1
5+
* @returns An rgba color string (e.g., 'rgba(255,0,0,0.5)')
6+
*/
7+
exportfunctionhexToRgba(hex: string,alpha: number): string{
8+
constr=Number.parseInt(hex.slice(1,3),16)
9+
constg=Number.parseInt(hex.slice(3,5),16)
10+
constb=Number.parseInt(hex.slice(5,7),16)
11+
return`rgba(${r},${g},${b},${alpha})`
12+
}

0 commit comments

Comments
 (0)