
Félix MalfaitandGitHub
f9c61833ec
Add debug info in AI chat (#15758)
## 🐛 Critical Bug Fix
### Cost Calculation Error (1000x undercharge)
- **Fixed**: Cost conversion utility was calculating credits at 1/1000th
of actual value
- **Before**: `cents * 10` ❌
- **After**: `(cents / 100) * DOLLAR_TO_CREDIT_MULTIPLIER` ✅
- **Impact**: Users were being undercharged by 1000x
- Example: 0.75 cents should = 7,500 credits
- Bug calculated it as 7.5 credits
---
## 🎯 Code Centralization & DRY
### Unified Cost Calculation
- Centralized all cost conversions to use `convertCentsToBillingCredits`
utility
- Refactored 3 different implementations into 1 single source of truth
- Files updated:
- `ai-billing.service.ts`
- `agent-streaming.service.ts` (2 usages)
**Before** (multiple implementations):
```typescript
// Wrong implementation
const credits = cents * 10;
// Verbose implementation
const costInDollars = costInCents / 100;
const creditsUsed = Math.round(costInDollars * DOLLAR_TO_CREDIT_MULTIPLIER);
```
**After** (unified):
```typescript
const creditsUsed = Math.round(convertCentsToBillingCredits(costInCents));
```
---
## ✨ UI Component Refactoring
### RoutingDebugDisplay.tsx
- **Reduced from 118 lines to 34 lines** (71% reduction)
- Extracted `renderTimingRow` helper to eliminate 15 repetitive JSX
blocks
- Added `formatTokenBreakdown` helper for token display logic
- Much easier to add new debug metrics
**Before**: 15 nearly-identical blocks of repetitive JSX
**After**: Clean, DRY implementation with reusable helpers
---
## 🧹 Code Quality Improvements
### Removed Debug Code
- Removed `console.log` accidentally left in `RoutingStatusDisplay.tsx`
### Cleaned Up Comments (18+ removed)
Removed redundant comments that stated the obvious:
- ❌ "Calculate routing cost if we have token usage"
- ❌ "Send the updated routing status with execution metrics to the
client"
- ❌ "Count tool calls in the response"
- ❌ "AI SDK's LanguageModelUsage uses inputTokens/outputTokens"
- And 14+ more...
Kept meaningful comments:
- ✅ "Timing is optional, ignore errors" (explains catch block)
- ✅ Type definition grouping comments
---
## 📊 Statistics
**Files Modified**: 10
- `convert-cents-to-billing-credits.util.ts` (fixed formula)
- `ai-billing.service.ts` (use centralized utility)
- `agent-streaming.service.ts` (use utility, remove comments)
- `agent-execution.service.ts` (remove comments)
- `ai-router.service.ts` (remove comments)
- `RoutingStatusDisplay.tsx` (remove debug code)
- `RoutingDebugDisplay.tsx` (major refactor) ⭐
- `isDebugModeState.ts` (new file)
- `DataMessagePart.ts` (type extensions)
- `useClientConfig.ts` (debug mode support)
**Impact**:
- Lines removed: ~130 (redundant code + comments)
- Lines added: ~45 (helper functions)
- **Net reduction**: ~85 lines
- **Bug fixes**: 1 critical (1000x cost error)
- **Centralizations**: 3 locations now using shared utility
- **Major refactors**: 1 UI component (71% reduction)
---
## ✅ Verification
- ✅ All linter checks pass
- ✅ All tests pass (`ai-billing.service.spec.ts` verified)
- ✅ No `any` types in affected code
- ✅ No TODO/FIXME markers
---
## 🎯 Principles Applied
1. ✅ **Fix Root Causes, Not Symptoms** - Fixed utility function, then
used it everywhere
2. ✅ **DRY (Don't Repeat Yourself)** - Centralized cost calculation and
UI rendering
3. ✅ **Single Source of Truth** - One place for cost conversion formula
4. ✅ **Code as Documentation** - Removed comments that repeated what
code says
5. ✅ **Composability** - Created reusable helper functions
6. ✅ **Type Safety** - Maintained strict typing throughout
2025-11-13 13:34:46 +01:00
..
2025-11-12 11:20:59 +01:00
2025-11-12 11:20:59 +01:00
2025-10-27 13:27:36 +00:00
2025-11-12 11:51:54 +01:00
2025-11-05 11:29:37 +01:00
2025-11-08 07:43:59 +01:00
2025-11-13 13:34:46 +01:00
2025-11-12 11:20:59 +01:00
2025-11-13 13:34:46 +01:00
2025-11-13 13:34:46 +01:00
2025-11-13 10:00:28 +00:00
2025-10-22 11:42:24 +02:00
2025-11-11 16:43:55 +01:00
2025-11-03 11:42:28 +01:00