MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / overwriteCostInLine

Function overwriteCostInLine

web/src/llm-api/openai.ts:179–194  ·  view source on GitHub ↗

* Overwrite the cost field in an SSE line to reflect actual billed credits.

(line: string, billedCredits: number)

Source from the content-addressed store, hash-verified

177 * Overwrite the cost field in an SSE line to reflect actual billed credits.
178 */
179function 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
196export class OpenAIError extends Error {
197 constructor(

Callers 1

startFunction · 0.85

Calls 2

parseMethod · 0.80
creditsToFakeCostFunction · 0.70

Tested by

no test coverage detected