()
| 375 | // capture what we were charged. Without this, a well-timed mid-stream failure |
| 376 | // lets the caller walk away with free completion tokens. |
| 377 | const ensureBilled = async () => { |
| 378 | if (state.billed || !state.generationId) return |
| 379 | await new Promise((resolve) => |
| 380 | setTimeout(resolve, GENERATION_LOOKUP_DELAY_MS), |
| 381 | ) |
| 382 | await fallbackBillFromGeneration({ |
| 383 | generationId: state.generationId, |
| 384 | openrouterApiKey, |
| 385 | userId, |
| 386 | stripeCustomerId, |
| 387 | agentId, |
| 388 | clientId, |
| 389 | clientRequestId, |
| 390 | costMode, |
| 391 | byok, |
| 392 | startTime, |
| 393 | state, |
| 394 | request: auditRequest, |
| 395 | fetch, |
| 396 | logger, |
| 397 | insertMessage: insertMessageBigquery, |
| 398 | }) |
| 399 | } |
| 400 | |
| 401 | // Create a ReadableStream that Next.js can handle |
| 402 | const stream = new ReadableStream({ |
no test coverage detected