fix opencode pricing

Merged
KA
2mo ago
+10651-32

Greptile Summary

This PR centralizes pricing computation into a shared module and adds health check functionality for OpenCode sessions. The main changes extract hardcoded pricing logic from engineer.py into a new pricing.py module with support for multiple models (Claude Sonnet/Opus/Haiku 4.5 and Google Gemini models).

Key changes:

  • Created pricing.py with calculate_cost() function and MODEL_PRICING dictionary
  • Added health check endpoint call in opencode_engineer.py to verify server status before creating sessions
  • Enhanced UI to display session summaries with token breakdown and costs
  • Added OpenCode API documentation files for reference

Critical issue found:

  • The calculate_cost() function accesses pricing dictionary keys directly using bracket notation (pricing["cache_creation"]), which will throw a KeyError for models like google-gemini-3-flash that don't have all pricing fields defined (missing cache_creation, cache_read, reasoning)

Confidence Score: 2/5

  • This PR contains a critical bug that will cause runtime failures for certain models
  • The pricing calculation has a KeyError bug that will crash when using Google Gemini models or any future models without complete pricing fields. While the overall refactoring approach is good, this bug must be fixed before merging.
  • Pay close attention to src/reverse_api/pricing.py - the calculate_cost() function needs to use .get() with defaults instead of direct key access

Important Files Changed

FilenameOverview
src/reverse_api/pricing.pyNew pricing module with critical KeyError bug for models missing optional pricing fields
src/reverse_api/opencode_engineer.pyAdded health check, pricing calculation fallback, and session summary display
src/reverse_api/opencode_ui.pyEnhanced UI with health check, model info display, and improved session summary formatting
src/reverse_api/engineer.pyRefactored to use shared pricing module for cost calculation