Skip to content

Conversation

@cubic-dev-local
Copy link

@cubic-dev-local cubic-dev-local bot commented Sep 12, 2025

## What does this PR do?

Screenshot 2025-09-12 at 4 20 08 PM

Mandatory Tasks (DO NOT REMOVE)

  • I have self-reviewed the code (A decent size PR without self-review might be rejected).
  • N/A I have updated the developer docs in /docs if this PR makes changes that would require a documentation change. If N/A, write N/A here and check the checkbox.
  • I confirm automated tests are in place that prove my fix is effective or that my feature works.

How should this be tested?

---

Based on: calcom/cal.com#23788

Summary by cubic

Adds support for Retell web calls in the webhook. Web calls are now billed by resolving the agent to a user/team instead of relying on phone numbers.

  • New Features

    • Accepts web calls: phone fields are optional; resolves user/team via agent_id (providerAgentId).
    • Charges credits from call duration at CAL_AI_CALL_RATE_PER_MINUTE (default 0.29), rounded up; uses externalRef retell:<call_id>.
    • Keeps skipping inbound calls for phone flows and adds clearer logs.
  • Refactors

    • Extracted chargeCreditsForCall helper for shared billing logic.
    • Added tests for web call paths (user vs team, missing agent, no phone).

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

2 issues found across 2 files

Prompt for AI agents (all 2 issues)

Understand the root cause of the following 2 issues and fix them.


<file name="apps/web/app/api/webhooks/retell-ai/route.ts">

<violation number="1" location="apps/web/app/api/webhooks/retell-ai/route.ts:65">
The `callCost` parameter, derived from Retell AI&#39;s `combined_cost`, is passed to the `chargeCreditsForCall` function but is never used. The function instead recalculates the cost based on call duration and a local rate, which can lead to billing discrepancies and makes the code&#39;s intent unclear.</violation>

<violation number="2" location="apps/web/app/api/webhooks/retell-ai/route.ts:147">
The handler for web calls correctly finds the agent associated with a call via `agent_id`, but it fails to check if the agent is enabled before charging credits. This creates a risk of billing for calls made through agents that have been disabled in the system.</violation>
</file>

React with 👍 or 👎 to teach cubic. Mention @cubic-dev-ai to give feedback, ask questions, or re-run the review.

return;
}

const agent = await PrismaAgentRepository.findByProviderAgentId({
Copy link

@cubic-dev-ai cubic-dev-ai bot Sep 12, 2025

Choose a reason for hiding this comment

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

The handler for web calls correctly finds the agent associated with a call via agent_id, but it fails to check if the agent is enabled before charging credits. This creates a risk of billing for calls made through agents that have been disabled in the system.

Prompt for AI agents
Address the following comment on apps/web/app/api/webhooks/retell-ai/route.ts at line 147:

<comment>The handler for web calls correctly finds the agent associated with a call via `agent_id`, but it fails to check if the agent is enabled before charging credits. This creates a risk of billing for calls made through agents that have been disabled in the system.</comment>

<file context>
@@ -124,15 +117,74 @@ async function handleCallAnalyzed(callData: any) {
+      return;
+    }
+
+    const agent = await PrismaAgentRepository.findByProviderAgentId({
+      providerAgentId: agent_id,
+    });
</file context>

[internal] Confidence score: 10/10

[internal] Posted by: System Design Agent

Fix with Cubic

return;
}

async function chargeCreditsForCall({
Copy link

@cubic-dev-ai cubic-dev-ai bot Sep 12, 2025

Choose a reason for hiding this comment

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

The callCost parameter, derived from Retell AI's combined_cost, is passed to the chargeCreditsForCall function but is never used. The function instead recalculates the cost based on call duration and a local rate, which can lead to billing discrepancies and makes the code's intent unclear.

Prompt for AI agents
Address the following comment on apps/web/app/api/webhooks/retell-ai/route.ts at line 65:

<comment>The `callCost` parameter, derived from Retell AI&#39;s `combined_cost`, is passed to the `chargeCreditsForCall` function but is never used. The function instead recalculates the cost based on call duration and a local rate, which can lead to billing discrepancies and makes the code&#39;s intent unclear.</comment>

<file context>
@@ -59,44 +62,27 @@ const RetellWebhookSchema = z.object({
-    return;
-  }
-
+async function chargeCreditsForCall({
+  userId,
+  teamId,
</file context>

[internal] Confidence score: 9/10

[internal] Posted by: System Design Agent

Fix with Cubic

@github-actions
Copy link

This PR is being marked as stale due to inactivity.

@github-actions github-actions bot added the Stale label Sep 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants