Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 2 additions & 17 deletions apps/sim/app/api/billing/update-cost/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Span } from '@opentelemetry/api'
import { createLogger } from '@sim/logger'
import { toError } from '@sim/utils/errors'
import { sql } from 'drizzle-orm'
import { type NextRequest, NextResponse } from 'next/server'
import { billingUpdateCostContract } from '@/lib/api/contracts/subscription'
import { parseRequest } from '@/lib/api/server'
Expand Down Expand Up @@ -155,21 +154,6 @@ async function updateCostInner(req: NextRequest, span: Span): Promise<NextRespon
source,
})

const totalTokens = inputTokens + outputTokens

const additionalStats: Record<string, ReturnType<typeof sql>> = {
totalCopilotCost: sql`total_copilot_cost + ${cost}`,
currentPeriodCopilotCost: sql`current_period_copilot_cost + ${cost}`,
totalCopilotCalls: sql`total_copilot_calls + 1`,
totalCopilotTokens: sql`total_copilot_tokens + ${totalTokens}`,
}

if (isMcp) {
additionalStats.totalMcpCopilotCost = sql`total_mcp_copilot_cost + ${cost}`
additionalStats.currentPeriodMcpCopilotCost = sql`current_period_mcp_copilot_cost + ${cost}`
additionalStats.totalMcpCopilotCalls = sql`total_mcp_copilot_calls + 1`
}

await recordUsage({
userId,
entries: [
Expand All @@ -178,10 +162,11 @@ async function updateCostInner(req: NextRequest, span: Span): Promise<NextRespon
source,
description: model,
cost,
eventKey: idempotencyKey ? `update-cost:${idempotencyKey}` : undefined,
sourceReference: idempotencyKey ? `update-cost:${idempotencyKey}` : requestId,
metadata: { inputTokens, outputTokens },
},
],
additionalStats,
})
usageCommitted = true

Expand Down
17 changes: 1 addition & 16 deletions apps/sim/app/api/mcp/copilot/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ import {
McpError,
type RequestId,
} from '@modelcontextprotocol/sdk/types.js'
import { db } from '@sim/db'
import { userStats } from '@sim/db/schema'
import { createLogger } from '@sim/logger'
import { toError } from '@sim/utils/errors'
import { generateId } from '@sim/utils/id'
import { authorizeWorkflowByWorkspacePermission } from '@sim/workflow-authz'
import { eq, sql } from 'drizzle-orm'
import { type NextRequest, NextResponse } from 'next/server'
import { mcpRequestBodySchema, mcpToolCallParamsSchema } from '@/lib/api/contracts/mcp'
import { validateOAuthAccessToken } from '@/lib/auth/oauth-token'
Expand Down Expand Up @@ -391,20 +388,8 @@ export const DELETE = withRouteHandler(async (request: NextRequest) => {
return NextResponse.json(createError(0, -32000, 'Method not allowed.'), { status: 405 })
})

/**
* Increment MCP copilot call counter in userStats (fire-and-forget).
*/
function trackMcpCopilotCall(userId: string): void {
db.update(userStats)
.set({
totalMcpCopilotCalls: sql`total_mcp_copilot_calls + 1`,
lastActive: new Date(),
})
.where(eq(userStats.userId, userId))
.then(() => {})
.catch((error) => {
logger.error('Failed to track MCP copilot call', { error, userId })
})
logger.debug('MCP copilot call tracked via request logs', { userId })
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dead no-op function trackMcpCopilotCall still called

Low Severity

trackMcpCopilotCall was gutted to a single debug log but is still invoked at the call site. The function no longer tracks anything meaningful — it's dead code that adds confusion about whether MCP calls are actually being counted anywhere.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 05d3bab. Configure here.

}

async function handleToolsCall(
Expand Down
6 changes: 6 additions & 0 deletions apps/sim/app/api/speech/token/route.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createHash } from 'node:crypto'
import { db } from '@sim/db'
import { chat } from '@sim/db/schema'
import { createLogger } from '@sim/logger'
Expand Down Expand Up @@ -31,6 +32,10 @@ const STT_TOKEN_RATE_LIMIT = {
refillIntervalMs: 72 * 1000,
} as const

function hashVoiceToken(token: string): string {
return createHash('sha256').update(token).digest('hex')
}

const rateLimiter = new RateLimiter()

async function validateChatAuth(
Expand Down Expand Up @@ -163,6 +168,7 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
source: 'voice-input',
description: `Voice input session (${maxMinutes} min)`,
cost: sessionCost * getCostMultiplier(),
sourceReference: `voice-input:${hashVoiceToken(data.token)}`,
},
],
}).catch((err) => {
Expand Down
7 changes: 2 additions & 5 deletions apps/sim/app/api/wand/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { db } from '@sim/db'
import { workflow } from '@sim/db/schema'
import { createLogger } from '@sim/logger'
import { eq, sql } from 'drizzle-orm'
import { eq } from 'drizzle-orm'
import { type NextRequest, NextResponse } from 'next/server'
import { wandGenerateContract } from '@/lib/api/contracts'
import { parseRequest } from '@/lib/api/server'
Expand Down Expand Up @@ -106,7 +106,6 @@ async function updateUserStatsForWand(
}

try {
const totalTokens = usage.total_tokens || 0
const promptTokens = usage.prompt_tokens || 0
const completionTokens = usage.completion_tokens || 0

Expand Down Expand Up @@ -138,12 +137,10 @@ async function updateUserStatsForWand(
source: 'wand',
description: modelName,
cost: costToStore,
sourceReference: `wand:${requestId}`,
metadata: { inputTokens: promptTokens, outputTokens: completionTokens },
},
],
additionalStats: {
totalTokensUsed: sql`total_tokens_used + ${totalTokens}`,
},
})

await checkAndBillOverageThreshold(billingUserId)
Expand Down
1 change: 1 addition & 0 deletions apps/sim/background/workflow-column-execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ async function runWorkflowAndWriteTerminal(
source: 'enrichment',
description: enrichment.name,
cost,
sourceReference: `enrichment:${tableId}:${rowId}:${enrichment.id}`,
metadata: { enrichmentId: enrichment.id, tableId, rowId },
},
],
Expand Down
Loading
Loading