refactor(usage): extract shared OpenRouter/Vercel cost and stream drain helpers#1463
Conversation
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Files Reviewed (1 file)
Reviewed by gpt-5.4-20260305 · 89,781 tokens |
chrarnoldus-bot
left a comment
There was a problem hiding this comment.
Clean refactor — three duplicated patterns extracted into well-typed shared helpers with no behavior changes. A few observations:
-
Lost inline comments: The original processOpenRouterUsage in processUsage.ts had useful inline comments explaining why the suspicious-accounting conditions exist (unknown byok status but known non-zero costs, and the BYOK promo note). These are now gone in computeOpenRouterCostFields. Consider adding a brief JSDoc or inline comment in the shared function so future readers don't have to git-blame to understand the rationale.
-
Null safety looks correct:
usage ?? {}in processOpenRouterUsage properly handles the nullable input, and the structural typing for OpenRouterCostFields works since all fields are optional. -
drainSseStream is a nice abstraction — the callback-based onTextChunk pattern keeps it generic while preserving the abort-handling semantics.
All CI green. LGTM.
chrarnoldus-bot
left a comment
There was a problem hiding this comment.
Suggestion to re-add the lost inline comments from the original processOpenRouterUsage:
Co-authored-by: Capricorn <chrarnoldus+githubbot@gmail.com>
Summary
processUsage.ts,processUsage.messages.ts, andprocessUsage.responses.tsinto a newprocessUsage.shared.tsmodule:computeOpenRouterCostFields— the OpenRouter BYOK cost computation + suspicious-accounting Sentry warning, previously copy-pasted verbatim in all three filescomputeVercelCostMicrodollars— the Vercel gatewaymarketCost/costparsing, duplicated in messages and responses handlersdrainSseStream— theReadableStreamdrain loop withResponseAbortedhandling, duplicated in all three stream parserssourcetag string as before so Sentry tags are preserved.Verification
tsgo --noEmitpasses with exit code 0src/lib/processUsage*.test.tscould not be run (DB connection unavailable in this environment); the failures are pre-existing infrastructure issues unrelated to this changeVisual Changes
N/A
Reviewer Notes
The only subtle change is that
computeOpenRouterCostFieldsaccepts a partialOpenRouterCostFieldstype (the common cost fields) rather than the full usage object, so callers in messages/responses pass their respective usage types directly — both satisfy the structural type since they have the samecost,is_byok, andcost_detailsfields.