Skip to content

refactor(usage): extract shared OpenRouter/Vercel cost and stream drain helpers#1463

Merged
chrarnoldus merged 2 commits intomainfrom
refactor/extract-process-usage-shared
Mar 25, 2026
Merged

refactor(usage): extract shared OpenRouter/Vercel cost and stream drain helpers#1463
chrarnoldus merged 2 commits intomainfrom
refactor/extract-process-usage-shared

Conversation

@kilo-code-bot
Copy link
Contributor

@kilo-code-bot kilo-code-bot bot commented Mar 24, 2026

Summary

  • Extracted three duplicated code patterns across processUsage.ts, processUsage.messages.ts, and processUsage.responses.ts into a new processUsage.shared.ts module:
    • computeOpenRouterCostFields — the OpenRouter BYOK cost computation + suspicious-accounting Sentry warning, previously copy-pasted verbatim in all three files
    • computeVercelCostMicrodollars — the Vercel gateway marketCost/cost parsing, duplicated in messages and responses handlers
    • drainSseStream — the ReadableStream drain loop with ResponseAborted handling, duplicated in all three stream parsers
  • No behaviour changes; all three callers pass the same source tag string as before so Sentry tags are preserved.

Verification

  • tsgo --noEmit passes with exit code 0
  • Tests in src/lib/processUsage*.test.ts could not be run (DB connection unavailable in this environment); the failures are pre-existing infrastructure issues unrelated to this change

Visual Changes

N/A

Reviewer Notes

The only subtle change is that computeOpenRouterCostFields accepts a partial OpenRouterCostFields type (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 same cost, is_byok, and cost_details fields.

@kilo-code-bot
Copy link
Contributor Author

kilo-code-bot bot commented Mar 24, 2026

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (1 file)
  • src/lib/processUsage.shared.ts

Reviewed by gpt-5.4-20260305 · 89,781 tokens

Copy link
Contributor

@chrarnoldus-bot chrarnoldus-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean refactor — three duplicated patterns extracted into well-typed shared helpers with no behavior changes. A few observations:

  1. 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.

  2. Null safety looks correct: usage ?? {} in processOpenRouterUsage properly handles the nullable input, and the structural typing for OpenRouterCostFields works since all fields are optional.

  3. drainSseStream is a nice abstraction — the callback-based onTextChunk pattern keeps it generic while preserving the abort-handling semantics.

All CI green. LGTM.

Copy link
Contributor

@chrarnoldus-bot chrarnoldus-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion to re-add the lost inline comments from the original processOpenRouterUsage:

Co-authored-by: Capricorn <chrarnoldus+githubbot@gmail.com>
@chrarnoldus chrarnoldus merged commit bd49223 into main Mar 25, 2026
19 checks passed
@chrarnoldus chrarnoldus deleted the refactor/extract-process-usage-shared branch March 25, 2026 07:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants