* Overwrite the cost field in an SSE line to reflect actual billed credits.
(line: string, billedCredits: number)
| 177 | * Overwrite the cost field in an SSE line to reflect actual billed credits. |
| 178 | */ |
| 179 | function overwriteCostInLine(line: string, billedCredits: number): string { |
| 180 | if (!line.startsWith('data: ')) return line |
| 181 | const raw = line.slice('data: '.length).trim() |
| 182 | if (raw === '[DONE]') return line |
| 183 | try { |
| 184 | const obj = JSON.parse(raw) |
| 185 | if (obj.usage) { |
| 186 | obj.usage.cost = creditsToFakeCost(billedCredits) |
| 187 | obj.usage.cost_details = { upstream_inference_cost: 0 } |
| 188 | return `data: ${JSON.stringify(obj)}\n` |
| 189 | } |
| 190 | } catch { |
| 191 | // pass through |
| 192 | } |
| 193 | return line |
| 194 | } |
| 195 | |
| 196 | export class OpenAIError extends Error { |
| 197 | constructor( |
no test coverage detected