Skip to content

feat: Refactor Gemini and Vertex handlers to be fully independent #4374

@kiwina

Description

@kiwina

Summary

Refactor the Gemini and Vertex model provider handlers to be fully independent, eliminating shared logic and inheritance. This improves code maintainability, reduces coupling, and ensures each handler operates with provider-specific logic.

Problem

Currently, the VertexHandler inherits from GeminiHandler, which creates several issues:

  • Mixed vertex and gemini logic in both handlers
  • Vertex-specific parameters polluting the Gemini constructor
  • Shared inheritance making debugging and maintenance difficult
  • Model selection confusion between providers

Solution

  1. Complete Independence: Make VertexHandler a standalone class that doesn't inherit from GeminiHandler
  2. Clean Separation: Remove all vertex-related logic from GeminiHandler
  3. Provider-Specific Models: Ensure each handler uses only its own model set and defaults
  4. Enhanced Debugging: Add comprehensive debug output to trace model selection

Changes Made

GeminiHandler (src/api/providers/gemini.ts)

  • Removed all vertex-related constructor parameters (isVertex, vertex credentials)
  • Removed vertex-specific logic and model handling
  • Now uses only geminiApiKey and geminiModels
  • Clean, gemini-only implementation

VertexHandler (src/api/providers/vertex.ts)

  • Complete standalone implementation (no inheritance)
  • Uses only vertex-specific credentials and configuration
  • Implements all required methods independently
  • Enhanced debug output in getModel() method for troubleshooting
  • Uses vertexModels and vertexDefaultModelId exclusively

Tests Updated

  • Updated vertex test expectations to use correct default model (claude-sonnet-4@20250514)
  • Fixed error message expectations after refactoring
  • All tests passing (11 vertex tests, 15 gemini tests)

Benefits

  • Cleaner Architecture: Each handler is responsible only for its provider
  • Easier Debugging: Clear separation makes issues easier to trace
  • Better Maintainability: Changes to one provider don't affect the other
  • Reduced Coupling: No shared state or inheritance dependencies
  • Provider-Specific Optimization: Each handler can be optimized for its specific use case

Testing

  • ✅ All existing gemini tests pass (15/15)
  • ✅ All vertex tests updated and passing (11/11)
  • ✅ No breaking changes to public API
  • ✅ Model selection and fallback logic verified
  • ✅ Debug output added for troubleshooting apiModelId flow

Files Changed

  • src/api/providers/gemini.ts - Refactored to pure Gemini implementation
  • src/api/providers/vertex.ts - Standalone Vertex implementation with enhanced debugging
  • src/api/providers/__tests__/vertex.test.ts - Updated test expectations

Type of Change

  • Refactor (code change that improves structure without changing functionality)
  • Enhancement (improvement to existing feature)
  • Test updates (updating existing tests)

This refactoring improves code quality while maintaining full backward compatibility and makes future maintenance much easier.

Metadata

Metadata

Assignees

Labels

EnhancementNew feature or requestIssue - In ProgressSomeone is actively working on this. Should link to a PR soon.

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions