Skip to content

feat: pass sessionId to voice API calls for usage cost attribution - #320

Open
VISHNUDAS-tunerlabs wants to merge 1 commit into
ELEVATE-Project:release-2.1.0from
VISHNUDAS-tunerlabs:elevate-main
Open

feat: pass sessionId to voice API calls for usage cost attribution#320
VISHNUDAS-tunerlabs wants to merge 1 commit into
ELEVATE-Project:release-2.1.0from
VISHNUDAS-tunerlabs:elevate-main

Conversation

@VISHNUDAS-tunerlabs

@VISHNUDAS-tunerlabs VISHNUDAS-tunerlabs commented Jun 29, 2026

Copy link
Copy Markdown

Summary

  • Updates voice-related API call functions to include sessionId (or userId
    as fallback) in the request payload sent to the backend
  • Required for the backend to correctly attribute TTS, STT, Translation, and
    Transliteration costs to the right ChatSession in UsageCostLog
  • Without this, voice cost rows would have no session association and the
    Usage Cost Dashboard would show $0 for all voice call types

Context

Part of the Usage Cost (Phase 2) feature. The backend now records a
UsageCostLog row after every voice provider call, but it needs a session_id
from the client to link the cost to the correct session. This change supplies
that identifier from the frontend.

Summary by CodeRabbit

  • New Features

    • Voice and speech features now carry session/profile context through transcription and audio generation requests.
    • Updated voice chat and text-to-speech flows across multiple areas of the app to support this added context.
  • Bug Fixes

    • Improved consistency of voice input and spoken response handling by passing the same contextual details across related requests.

@coderabbitai

coderabbitai Bot commented Jun 29, 2026

Copy link
Copy Markdown

Review Change Stack

Important

Review skipped

No new commits to review since the last review.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e2cb1587-d742-426e-8603-45d4e73dc88a

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Three AI4Bharat API functions (getAI4BharatAudioApi, ai4BharatASRApi, transliterateApiFunc) and their underlying service implementations are extended with optional sessionId and profileId parameters. These are conditionally included in POST payloads. All call sites across voice chat pages and components are updated to forward the new parameters.

Changes

Session/Profile Context in AI4Bharat APIs

Layer / File(s) Summary
API endpoint and service layer contracts
src/api/endpoints/ai.js, src/services/api.service.js
getAI4BharatAudioApi, ai4BharatASRApi, and transliterateApiFunc accept optional sessionId and profileId; request body refactored to a payload object with conditional field inclusion. Same pattern applied to getAI4BharatAudio and ai4BharatASR in the service layer.
TTS service wrapper updates
src/services/audio_service.js, src/pages/ai-creation/apiServices/ai4bharat_services.js
handleAI4BharatTTSRequest in both service files extended with sessionId and profileId parameters, forwarded to the underlying getAI4BharatAudioApi invocations.
Call site propagation
src/pages/ShikshalokamVoiceChat/dynamic-voice-chat.js, src/pages/ShikshalokamVoiceChat/voice-chat.js, src/pages/UnifiedChat/UnifiedVoiceBasedChat.jsx, src/pages/ai-creation/components/textarea-with-mic/index.jsx, src/pages/ai-creation/pages/shikshalokam-mitra/mitra-pages/components/ChatBox.jsx, src/pages/ai-creation/pages/shikshalokam-mitra/mitra-pages/components/VoiceChat.jsx, src/pages/shikshagraha-repository/listing/Filters.jsx
All TTS and ASR invocations updated to pass sessionId and profileId/profileToUse at each call site.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested reviewers

  • Vinod-V3

Poem

🐇 A session ID here, a profile there,
The rabbit hops context through the air!
TTS and ASR now know who you are,
Each API call tagged like a shooting star.
No more anonymous voices in the queue—
The warren's now personalized, just for you! 🎙️

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: adding sessionId to voice API calls for usage cost attribution.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ 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.

@Vinod-V3

Copy link
Copy Markdown
Collaborator

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 29, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 5

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/pages/ai-creation/apiServices/ai4bharat_services.js`:
- Around line 4-12: The new sessionId parameter in handleAI4BharatTTSRequest is
being populated by an outdated positional caller, causing storedRoute to be
passed as the wrong argument. Update the remaining call site in audio_service.js
to pass sessionId and profileId in the correct positions, or switch that caller
to an object/explicit arguments so the 8th and 9th parameters are not
misaligned. Use handleAI4BharatTTSRequest and the audio service caller as the
symbols to locate and fix the mismatch.

In `@src/pages/ai-creation/components/textarea-with-mic/index.jsx`:
- Around line 164-169: The AI-creation ASR call in textarea-with-mic’s audio
submission flow only passes sessionId, so fallback attribution is missing when
getSession() is unavailable. Update the ai4BharatASRApi invocation in the
textarea-with-mic component to also pass the fallback profile/user identifier
(profileId) alongside sessionRoute and sessionId, using the same identifier
source already added elsewhere in the PR so requests remain attributed when no
session exists.

In
`@src/pages/ai-creation/pages/shikshalokam-mitra/mitra-pages/components/ChatBox.jsx`:
- Around line 177-182: The ASR request in ChatBox still omits the fallback
attribution identifier, so voice usage can be detached when the session store is
unavailable. Update the ai4BharatASRApi call in ChatBox to pass the new
profileId fallback alongside sessionId, using the same identifier selection
logic already used elsewhere so UsageCostLog can always attribute the request.

In
`@src/pages/ai-creation/pages/shikshalokam-mitra/mitra-pages/components/VoiceChat.jsx`:
- Around line 135-139: The ai4BharatASRApi call in VoiceChat currently forwards
only sessionId, which can leave ASR requests unattributed when the session is
not yet set. Update the VoiceChat component to pass the same fallback
profile/user identifier used elsewhere in this flow, alongside sessionId, so the
ASR request always has a valid attribution source even when the session is
unavailable.

In `@src/pages/shikshagraha-repository/listing/Filters.jsx`:
- Line 163: The repository ASR call in Filters.jsx is still passing only
sessionId, so the fallback identifier is missing when sessionId is unavailable.
Update the ai4BharatASRApi invocation in the listing Filters flow to include the
fallback profile/user identifier used elsewhere on this page, preserving
attribution when sessionId is absent. Use the existing variables around
transcriptResult and ai4BharatASRApi to locate the call and make sure the
backend receives a non-null identifier consistently.
🪄 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: defaults

Review profile: CHILL

Plan: Pro

Run ID: dd087b3a-0f4c-4cda-8362-422aea3a55d2

📥 Commits

Reviewing files that changed from the base of the PR and between a7fc402 and a7d3e37.

📒 Files selected for processing (11)
  • src/api/endpoints/ai.js
  • src/pages/ShikshalokamVoiceChat/dynamic-voice-chat.js
  • src/pages/ShikshalokamVoiceChat/voice-chat.js
  • src/pages/UnifiedChat/UnifiedVoiceBasedChat.jsx
  • src/pages/ai-creation/apiServices/ai4bharat_services.js
  • src/pages/ai-creation/components/textarea-with-mic/index.jsx
  • src/pages/ai-creation/pages/shikshalokam-mitra/mitra-pages/components/ChatBox.jsx
  • src/pages/ai-creation/pages/shikshalokam-mitra/mitra-pages/components/VoiceChat.jsx
  • src/pages/shikshagraha-repository/listing/Filters.jsx
  • src/services/api.service.js
  • src/services/audio_service.js

Comment on lines +4 to +12
export const handleAI4BharatTTSRequest = async (text, id, language, audioCache, setAudioCache, audioRef, setIsBotTalking, sessionId = null, profileId = null) => {

try {
let cachedAudioUrl = audioCache[id];
let audio_result = "";
let audio;

if (!cachedAudioUrl) {
audio_result = await getAI4BharatAudioApi(text, language, bot_routes.mitra_create);
audio_result = await getAI4BharatAudioApi(text, language, bot_routes.mitra_create, sessionId, profileId);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Update the remaining positional caller before this ships.

Line 4 adds a real 8th parameter, but src/services/audio_service.js:30 still passes storedRoute in that slot. That value will now be forwarded as sessionId, so TTS usage from that path will be tagged with the route string instead of the chat session.

Also applies to: 31-31

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/pages/ai-creation/apiServices/ai4bharat_services.js` around lines 4 - 12,
The new sessionId parameter in handleAI4BharatTTSRequest is being populated by
an outdated positional caller, causing storedRoute to be passed as the wrong
argument. Update the remaining call site in audio_service.js to pass sessionId
and profileId in the correct positions, or switch that caller to an
object/explicit arguments so the 8th and 9th parameters are not misaligned. Use
handleAI4BharatTTSRequest and the audio service caller as the symbols to locate
and fix the mismatch.

Comment on lines 164 to 169
const transcript = await ai4BharatASRApi(
s3Url,
languageToUse,
sessionRoute
sessionRoute,
sessionId
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Forward the fallback profile/user identifier here too.

Line 164 only adds sessionId. When getSession() is falsy, ai4BharatASRApi will omit sessionid, and this caller still never supplies the new profileId fallback that the endpoint now supports. That leaves AI-creation ASR requests unattributed in exactly the case this PR is trying to fix.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/pages/ai-creation/components/textarea-with-mic/index.jsx` around lines
164 - 169, The AI-creation ASR call in textarea-with-mic’s audio submission flow
only passes sessionId, so fallback attribution is missing when getSession() is
unavailable. Update the ai4BharatASRApi invocation in the textarea-with-mic
component to also pass the fallback profile/user identifier (profileId)
alongside sessionRoute and sessionId, using the same identifier source already
added elsewhere in the PR so requests remain attributed when no session exists.

Comment on lines 177 to 182
transcriptResult = await ai4BharatASRApi(
s3Url,
languageToUse,
sessionRoute
sessionRoute,
sessionId
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

This ASR path still drops the fallback attribution key.

Line 177 forwards sessionId, but not the new profileId fallback supported by ai4BharatASRApi. If the session store is empty or not initialized yet, this request still goes out without an identifier, so voice usage for this flow will remain detached from UsageCostLog.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@src/pages/ai-creation/pages/shikshalokam-mitra/mitra-pages/components/ChatBox.jsx`
around lines 177 - 182, The ASR request in ChatBox still omits the fallback
attribution identifier, so voice usage can be detached when the session store is
unavailable. Update the ai4BharatASRApi call in ChatBox to pass the new
profileId fallback alongside sessionId, using the same identifier selection
logic already used elsewhere so UsageCostLog can always attribute the request.

Comment on lines 135 to +139
transcriptResult = await ai4BharatASRApi(
s3Url,
languageToUse,
storedRoute
storedRoute,
sessionId

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Forward the fallback profile/user identifier here too.

This call only adds sessionId, but the stated contract for this PR is sessionId with a profile/user fallback. If the ai-creation session is still unset, ASR usage from this component will still miss session attribution.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@src/pages/ai-creation/pages/shikshalokam-mitra/mitra-pages/components/VoiceChat.jsx`
around lines 135 - 139, The ai4BharatASRApi call in VoiceChat currently forwards
only sessionId, which can leave ASR requests unattributed when the session is
not yet set. Update the VoiceChat component to pass the same fallback
profile/user identifier used elsewhere in this flow, alongside sessionId, so the
ASR request always has a valid attribution source even when the session is
unavailable.

let storedRoute = bot_routes.search_bot

transcriptResult = await ai4BharatASRApi(s3Url, languageToUse, storedRoute)
transcriptResult = await ai4BharatASRApi(s3Url, languageToUse, storedRoute, sessionId)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

This repository ASR path still misses the fallback identifier.

Line 163 forwards only sessionId. On this page that value can be absent, so voice-search costs will still be logged without the profile/user context the backend needs for attribution.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/pages/shikshagraha-repository/listing/Filters.jsx` at line 163, The
repository ASR call in Filters.jsx is still passing only sessionId, so the
fallback identifier is missing when sessionId is unavailable. Update the
ai4BharatASRApi invocation in the listing Filters flow to include the fallback
profile/user identifier used elsewhere on this page, preserving attribution when
sessionId is absent. Use the existing variables around transcriptResult and
ai4BharatASRApi to locate the call and make sure the backend receives a non-null
identifier consistently.

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.

2 participants