Skip to content

feat: integrate chat credits handling into chat stream processing#363

Open
pradipthaadhi wants to merge 9 commits intorecoupable:testfrom
pradipthaadhi:yuliusupwork/myc-4501-credits-are-not-being-deducted-on-chat
Open

feat: integrate chat credits handling into chat stream processing#363
pradipthaadhi wants to merge 9 commits intorecoupable:testfrom
pradipthaadhi:yuliusupwork/myc-4501-credits-are-not-being-deducted-on-chat

Conversation

@pradipthaadhi
Copy link
Copy Markdown

@pradipthaadhi pradipthaadhi commented Mar 27, 2026

  • Added handleChatCredits function to manage usage tracking for chat sessions.
  • Updated the chat stream execution to include credit handling after processing messages.
  • Ensured compatibility with the existing chat completion handling logic.
  • test(api): mock handleChatCredits and DEFAULT_MODEL in handleChatStream tests

Summary by CodeRabbit

  • Bug Fixes
    • Chat completions now reliably apply credit charges when a conversation finishes.
    • Charges are enforced to deduct at least one credit even if reported usage is zero.
    • Credit calculations now use a sensible model fallback when the model isn't specified.

- Added handleChatCredits function to manage usage tracking for chat sessions.
- Updated the chat stream execution to include credit handling after processing messages.
- Ensured compatibility with the existing chat completion handling logic.
@vercel
Copy link
Copy Markdown
Contributor

vercel bot commented Mar 27, 2026

@pradipthaadhi is attempting to deploy a commit to the Recoupable Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 27, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: c94e6fd3-9ae5-493c-b7be-d3bbe850a867

📥 Commits

Reviewing files that changed from the base of the PR and between 12e6797 and a600ead.

📒 Files selected for processing (1)
  • lib/credits/handleChatCredits.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • lib/credits/handleChatCredits.ts

📝 Walkthrough

Walkthrough

Captured and retained the streaming result in lib/chat/handleChatStream.ts, used it for UI streaming and invoked handleChatCredits on completion; lib/credits/handleChatCredits.ts now always computes and deducts at least 1 credit (removed usageCost > 0 guard).

Changes

Cohort / File(s) Summary
Stream result & credit invocation
lib/chat/handleChatStream.ts
Store agent.stream(chatConfig) as streamResult; use streamResult.toUIMessageStream() for UI merge; on onFinish call handleChatCredits with await streamResult.usage, model: body.model ?? DEFAULT_MODEL, and accountId: body.accountId.
Credits deduction logic
lib/credits/handleChatCredits.ts
Removed if (usageCost > 0) check. Always compute creditsToDeduct = Math.max(1, Math.round(usageCost * 100)) and call deductCredits unconditionally, ensuring a minimum deduction of 1 credit even when usageCost ≤ 0.

Sequence Diagram(s)

sequenceDiagram
  actor Client
  participant Handler as "handleChatStream"
  participant Agent
  participant UI as "UI Stream"
  participant Credits as "handleChatCredits"
  participant Billing as "Billing Service"

  Client->>Handler: start chat request
  Handler->>Agent: agent.stream(chatConfig)
  Agent-->>Handler: streamResult
  Handler->>UI: streamResult.toUIMessageStream() (merge)
  UI-->Client: incremental messages
  UI->>Handler: onFinish event
  Handler->>Handler: handleChatCompletion(...)
  Handler->>Credits: handleChatCredits(usage=await streamResult.usage, model, accountId)
  Credits->>Billing: deductCredits(accountId, creditsToDeduct)
  Billing-->>Credits: deduction result
  Credits-->>Handler: completion
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

A stream retained, the tokens told,
Completion rings where numbers fold,
One credit kept as minimum due,
Ledger whispers, balances true. ✨

🚥 Pre-merge checks | ✅ 1
✅ Passed checks (1 passed)
Check name Status Explanation
Solid & Clean Code ✅ Passed Pull request demonstrates strong adherence to SOLID principles, clean code practices, and maintainable architecture with proper separation of concerns, clear dependency flow, comprehensive error handling, and well-structured code organization.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@vercel
Copy link
Copy Markdown
Contributor

vercel bot commented Mar 27, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
recoup-api Ready Ready Preview Mar 30, 2026 3:00am

Request Review

… cost

- Modified handleChatCredits to always deduct at least 1 credit, even when usage cost is zero.
- Updated related tests to reflect the new behavior, ensuring proper credit deduction is called.
- Enhanced test descriptions for clarity on credit deduction logic.
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@lib/credits/handleChatCredits.ts`:
- Around line 30-35: The current logic in handleChatCredits.ts always converts
zero-cost events into a 1-credit charge (creditsToDeduct) and unconditionally
calls deductCredits, causing non-billable sessions to consume credits; update
handleChatCredits so you only call deductCredits(accountId, creditsToDeduct)
when an explicit "billable usage happened" condition from the chat/session is
true (derive this from the same signal used in lib/chat/handleChatStream.ts for
real completions), and keep the Math.max(1, Math.round(usageCost * 100)) minimum
calculation only for those billable cases; ensure you still compute
creditsToDeduct the same way but gate the call to deductCredits(...) behind that
billable flag so cached/provider-failed/non-aborted-but-non-billable flows do
not mutate balance or throw.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 56727693-526b-4be3-bf48-8df898f150c3

📥 Commits

Reviewing files that changed from the base of the PR and between 1433341 and 12e6797.

⛔ Files ignored due to path filters (3)
  • lib/chat/__tests__/handleChatStream.test.ts is excluded by !**/*.test.*, !**/__tests__/** and included by lib/**
  • lib/chat/__tests__/integration/chatEndToEnd.test.ts is excluded by !**/*.test.*, !**/__tests__/** and included by lib/**
  • lib/credits/__tests__/handleChatCredits.test.ts is excluded by !**/*.test.*, !**/__tests__/** and included by lib/**
📒 Files selected for processing (1)
  • lib/credits/handleChatCredits.ts

- Added handleChatCredits function to manage usage tracking for chat sessions.
- Updated the chat stream execution to include credit handling after processing messages.
- Ensured compatibility with the existing chat completion handling logic.
- test(api): mock handleChatCredits and DEFAULT_MODEL in handleChatStream tests
…-chat' of https://github.com/pradipthaadhi/api into yuliusupwork/myc-4501-credits-are-not-being-deducted-on-chat
…ucted-on-chat' of https://github.com/pradipthaadhi/api into yuliusupwork/myc-4501-credits-are-not-being-deducted-on-chat"

This reverts commit 0589afc, reversing
changes made to 5450660.
- Updated handleChatCredits to always deduct at least 1 credit when usage cost is zero.
- Modified related tests to verify that credit deduction occurs correctly, even with zero usage cost.
- Enhanced test descriptions for clarity on the credit deduction logic.
…-chat' of https://github.com/pradipthaadhi/api into yuliusupwork/myc-4501-credits-are-not-being-deducted-on-chat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant