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 web call support to the Retell webhook. Credits are now charged for web calls by resolving agent_id to the owning user or team when phone data is missing.

  • New Features
    • Accepts web calls: phone fields and direction are optional; detected via call_type=web_call or missing from_number.
    • Resolves agent_id via PrismaAgentRepository to charge the correct user or team.
    • Extracts credit charging into chargeCreditsForCall; uses rate-per-minute env var and rounds up credits.
    • Inbound-call skip now applies only to phone calls; logging improved.
    • Adds unit tests for web call paths (agent found, team agent, missing agent_id, agent not found).

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.

1 issue found across 2 files

Prompt for AI agents (all 1 issues)

Understand the root cause of the following 1 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:141">
This change introduces two different sources of truth for determining who to bill for a call: `from_number` for phone calls and `agent_id` for web calls. This fragments the billing logic across two different data models and repositories. A more robust architecture would unify this, for example by assigning a consistent billing entity when a call is initiated, rather than deriving it from different properties after the call has ended.

        DEV MODE: This violation would have been filtered out by GPT-5.
Reasoning:
• **GPT-5**: Intentional per-call-type lookup (agent_id for web calls, from_number for phone calls) that both resolve to the same billing owner (userId/teamId) and feed a unified chargeCreditsForCall. No conflicting rules introduced; this is an architectural choice without clear negative impact.</violation>
</file>

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

let teamId: number | undefined;

// Handle web calls vs phone calls
if (call_type === "web_call" || !from_number) {
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.

This change introduces two different sources of truth for determining who to bill for a call: from_number for phone calls and agent_id for web calls. This fragments the billing logic across two different data models and repositories. A more robust architecture would unify this, for example by assigning a consistent billing entity when a call is initiated, rather than deriving it from different properties after the call has ended.

    DEV MODE: This violation would have been filtered out by GPT-5.

Reasoning:
GPT-5: Intentional per-call-type lookup (agent_id for web calls, from_number for phone calls) that both resolve to the same billing owner (userId/teamId) and feed a unified chargeCreditsForCall. No conflicting rules introduced; this is an architectural choice without clear negative impact.

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

<comment>This change introduces two different sources of truth for determining who to bill for a call: `from_number` for phone calls and `agent_id` for web calls. This fragments the billing logic across two different data models and repositories. A more robust architecture would unify this, for example by assigning a consistent billing entity when a call is initiated, rather than deriving it from different properties after the call has ended.

        DEV MODE: This violation would have been filtered out by GPT-5.
Reasoning:
• **GPT-5**: Intentional per-call-type lookup (agent_id for web calls, from_number for phone calls) that both resolve to the same billing owner (userId/teamId) and feed a unified chargeCreditsForCall. No conflicting rules introduced; this is an architectural choice without clear negative impact.</comment>

<file context>
@@ -124,15 +117,74 @@ async function handleCallAnalyzed(callData: any) {
+  let teamId: number | undefined;
+
+  // Handle web calls vs phone calls
+  if (call_type === &quot;web_call&quot; || !from_number) {
+    if (!agent_id) {
+      log.error(`Web call ${call_id} missing agent_id, cannot charge credits`);
</file context>

[internal] Confidence score: 8/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