feat: gateway upgrades#100
Conversation
|
| Filename | Overview |
|---|---|
| app/services/ai/llm_gateway.py | Major refactor introducing CredentialRoutingContext, per-credential gateway interface selection, custom auth headers/secrets, and extra_headers support. Contains a URL double-normalization bug when gateway_interface changes without updating gateway_base_url. |
| app/api/v1/routes/aiproviders.py | Adds per-credential routing/gateway fields with validation and URL sanitization. gateway_base_url is pre-normalized at storage time, which is the root of the double-normalization issue. |
| app/services/optimization/lm_resolver.py | New resolve_lm_call cleanly propagates CredentialRoutingContext; raises RuntimeError instead of silently returning None when no AI provider is found. |
| app/services/ai/llm_resolver.py | get_llm_provider_and_model expanded to handle credential_id lookups, org-wide default rows, and gateway_model resolution; logic is consistent and correct. |
| app/models/schemas.py | Well-validated new fields for gateway routing on AIProvider and Integration schemas. |
| app/models/database.py | New columns for routing_mode, gateway fields, and gateway_extra_headers (JSON) on AIProvider and Integration; aligns with migrations. |
| app/services/ai/llm_gateway_settings.py | Org-level gateway settings extended with gateway_interface; normalization correctly delegates to normalize_bifrost_native_url or normalize_bifrost_url based on effective interface. |
| app/services/judge_alignment/gepa_bridge.py | _resolve_api_key replaced by _resolve_lm_call which carries credential context through gateway and model resolution steps. |
| app/migrations/053_integration_routing_settings.py | Adds routing_mode and gateway_model columns to aiproviders and routing_mode to integrations; idempotent via column-exists checks. |
| frontend/src/lib/gatewayRouting.ts | New utility module for gateway routing checks; routesViaGateway checks effective_routing==='gateway' which the backend never emits, but the routing_mode fallback provides correct behavior. |
| app/api/v1/routes/integrations.py | Adds routing_mode to Integration CRUD; _integration_response helper computes effective_routing correctly. |
| app/services/ai/llm_service.py | Propagates CredentialRoutingContext into apply_llm_gateway and resolve_litellm_model; credential context properly threaded through the call chain. |
Reviews (5): Last reviewed commit: "fix: some more tests" | Re-trigger Greptile
| instance: AIProvider, | ||
| organization_id: UUID, | ||
| ) -> AIProviderResponse: | ||
| """Detach the row from the session before clearing ``api_key``.""" | ||
| gateway_managed = is_gateway_managed_stored_key(instance.api_key) | ||
| effective_routing = get_credential_effective_routing_label( | ||
| organization_id, | ||
| db, | ||
| instance.routing_mode, | ||
| ) | ||
| db.expunge(instance) | ||
| instance.api_key = None | ||
| response = AIProviderResponse.model_validate(instance) | ||
| return response.model_copy(update={"gateway_managed": gateway_managed}) | ||
| return response.model_copy( | ||
| update={ | ||
| "gateway_managed": gateway_managed, |
There was a problem hiding this comment.
N+1 org-settings queries in list endpoints
_scrub_for_response calls get_credential_effective_routing_label → resolve_effective_routing → _get_org_raw_settings, which issues a DB query for each provider in the list. For an org with many AI providers the list endpoint issues N redundant identical queries for the same org row. The same pattern applies to list_integrations. Fetching org settings once before the loop would eliminate the redundant queries.
What Changed?
Briefly describe what this PR changes.
Why?
Explain the problem this solves and why this approach was chosen.
How to Test?
List clear steps for reviewers to verify the change.
Release Label
Select one semantic version bump intent for this PR:
major- breaking change, next release bumps major versionminor- backward-compatible feature, next release bumps minor versionfix- backward-compatible bug fix, next release bumps patch versionIf you do not have permission to apply labels, mention the intended release label here and a maintainer will set it.
Checklist
CONTRIBUTING.mdguide.