Commit ·
d12a6b6
1
Parent(s): 9963145
major refactor
Browse filesFiles changed (13)
- src/auth.py+18-72
- src/config.pyAdded+170
- src/gemini.pyDeleted-228
- src/gemini_request_builder.pyDeleted-68
- src/gemini_response_handler.pyDeleted-73
- src/gemini_routes.pyAdded+144
- src/google_api_client.pyAdded+214
- src/main.py+19-14
- src/models.py+9
- src/openai.pyDeleted-184
- src/openai_routes.pyAdded+180
- src/openai_transformers.pyAdded+207
- src/utils.py+1-2
12 unmodified lines131415161718192021222324252627281617181920212212 unmodified lines353637473839404138 unmodified lines8081829283848511 unmodified lines9798991101111121001011026 unmodified lines1091101111251121131141151301161171181191201211371381221401411231241251 unmodified line12712812914915015113013115313213315613415815916013513613713816413914014114214314414517114614714817514915015117915215318218318415415518615615715819015919219319416016116216319816416516616724 unmodified lines19219319422919519619726 unmodified lines22422522626222722822926623026826927027123123223323418 unmodified lines25325425529625625725815 unmodified lines27427527631827727827910 unmodified lines2902912923352932942953392962972983433442993003013023033044 unmodified lines3093103113553123133143153163173623183193204 unmodified lines32532632737332832937633033137837933238133333433538533638738833733833934017 unmodified lines35835936041241336136241536336436541936612 unmodified linesfrom google.auth.transport.requests import Request as GoogleAuthRequest
from .utils import get_user_agent, get_client_metadata
# --- Configuration ---CLIENT_ID = "681255809395-oo8ft2oprdrnp9e3aqf6av3hmdib135j.apps.googleusercontent.com"CLIENT_SECRET = "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"SCOPES = [ "https://www.googleapis.com/auth/cloud-platform", "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile",]SCRIPT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))CREDENTIAL_FILE = os.path.join(SCRIPT_DIR, "oauth_creds.json")CODE_ASSIST_ENDPOINT = "https://cloudcode-pa.googleapis.com"GEMINI_AUTH_PASSWORD = os.getenv("GEMINI_AUTH_PASSWORD", "123456") # Default passwordfrom .config import ( CLIENT_ID, CLIENT_SECRET, SCOPES, CREDENTIAL_FILE, CODE_ASSIST_ENDPOINT, GEMINI_AUTH_PASSWORD)
# --- Global State ---credentials = None12 unmodified lines self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() self.wfile.write(b"<h1>Authentication successful!</h1><p>You can close this window and restart the proxy.</p>") self.wfile.write(b"<h1>OAuth authentication successful!</h1><p>You can close this window. Please check the proxy server logs to verify that onboarding completed successfully. No need to restart the proxy.</p>") else: self.send_response(400) self.send_header("Content-type", "text/html")38 unmodified lines )
def save_credentials(creds, project_id=None): print(f"DEBUG: Saving credentials - Token: {creds.token[:20] if creds.token else 'None'}..., Expired: {creds.expired}, Expiry: {creds.expiry}") creds_data = { "client_id": CLIENT_ID,11 unmodified lines else: expiry_utc = creds.expiry creds_data["expiry"] = expiry_utc.isoformat() print(f"DEBUG: Saving expiry as: {creds_data['expiry']}") else: print("DEBUG: No expiry time available to save") if project_id: creds_data["project_id"] = project_id6 unmodified lines except Exception: pass print(f"DEBUG: Final credential data to save: {json.dumps(creds_data, indent=2)}") with open(CREDENTIAL_FILE, "w") as f: json.dump(creds_data, f, indent=2) print("DEBUG: Credentials saved to file")
def get_credentials(): """Loads credentials matching gemini-cli OAuth2 flow.""" global credentials if credentials and credentials.token: print("Using valid credentials from memory cache.") print(f"DEBUG: Memory credentials - Token: {credentials.token[:20] if credentials.token else 'None'}..., Expired: {credentials.expired}, Expiry: {credentials.expiry}") return credentials else: print("No valid credentials in memory. Loading from disk.") env_creds = os.getenv("GOOGLE_APPLICATION_CREDENTIALS") if env_creds and os.path.exists(env_creds):1 unmodified line with open(env_creds, "r") as f: creds_data = json.load(f) credentials = Credentials.from_authorized_user_info(creds_data, SCOPES) print("Loaded credentials from GOOGLE_APPLICATION_CREDENTIALS.") print(f"DEBUG: Env credentials - Token: {credentials.token[:20] if credentials.token else 'None'}..., Expired: {credentials.expired}, Expiry: {credentials.expiry}")
if credentials.refresh_token: print("Refreshing environment credentials at startup for reliability...") try: credentials.refresh(GoogleAuthRequest()) print("Startup token refresh successful for environment credentials.") except Exception as refresh_error: print(f"Startup token refresh failed for environment credentials: {refresh_error}. Credentials may be stale.") else: print("No refresh token available in environment credentials - using as-is.") pass # Use credentials as-is if refresh fails return credentials except Exception as e: print(f"Could not load credentials from GOOGLE_APPLICATION_CREDENTIALS: {e}") pass # Fall through to file-based credentials
if os.path.exists(CREDENTIAL_FILE): try: with open(CREDENTIAL_FILE, "r") as f: creds_data = json.load(f) print(f"DEBUG: Raw credential data from file: {json.dumps(creds_data, indent=2)}") if "access_token" in creds_data and "token" not in creds_data: creds_data["token"] = creds_data["access_token"] print("DEBUG: Converted access_token to token field") if "scope" in creds_data and "scopes" not in creds_data: creds_data["scopes"] = creds_data["scope"].split() print("DEBUG: Converted scope string to scopes list") credentials = Credentials.from_authorized_user_info(creds_data, SCOPES) print("Loaded credentials from cache.") print(f"DEBUG: Loaded credentials - Token: {credentials.token[:20] if credentials.token else 'None'}..., Expired: {credentials.expired}, Expiry: {credentials.expiry}")
if credentials.refresh_token: print("Refreshing tokens at startup for reliability...") try: credentials.refresh(GoogleAuthRequest()) save_credentials(credentials) print("Startup token refresh successful.") except Exception as refresh_error: print(f"Startup token refresh failed: {refresh_error}. Credentials may be stale.") else: print("No refresh token available - using cached credentials as-is.") pass # Use credentials as-is if refresh fails return credentials except Exception as e: print(f"Could not load cached credentials: {e}. Starting new login.") pass # Fall through to new login
client_config = { "installed": {24 unmodified lines auth_code = _OAuthCallbackHandler.auth_code if not auth_code: print("Failed to retrieve authorization code.") return None
import oauthlib.oauth2.rfc6749.parameters26 unmodified lines return
if creds.expired and creds.refresh_token: print("Credentials expired. Refreshing before onboarding...") try: creds.refresh(GoogleAuthRequest()) save_credentials(creds) print("Credentials refreshed successfully.") except Exception as e: print(f"Could not refresh credentials: {e}") raise
print("Checking user onboarding status...") raise Exception(f"Failed to refresh credentials during onboarding: {str(e)}") headers = { "Authorization": f"Bearer {creds.token}", "Content-Type": "application/json",18 unmodified lines tier = None if load_data.get("currentTier"): tier = load_data["currentTier"] print("User is already onboarded.") else: for allowed_tier in load_data.get("allowedTiers", []): if allowed_tier.get("isDefault"):15 unmodified lines onboarding_complete = True return
print(f"Onboarding user to tier: {tier.get('name', 'legacy-tier')}") onboard_req_payload = { "tierId": tier.get("id"), "cloudaicompanionProject": project_id,10 unmodified lines lro_data = onboard_resp.json()
if lro_data.get("done"): print("Onboarding successful.") onboarding_complete = True break print("Onboarding in progress, waiting 5 seconds...") time.sleep(5)
except requests.exceptions.HTTPError as e: print(f"Error during onboarding: {e.response.text}") raise raise Exception(f"User onboarding failed. Please check your Google Cloud project permissions and try again. Error: {e.response.text if hasattr(e, 'response') else str(e)}") except Exception as e: raise Exception(f"User onboarding failed due to an unexpected error: {str(e)}")
def get_user_project_id(creds): """Gets the user's project ID matching gemini-cli setupUser logic."""4 unmodified lines env_project_id = os.getenv("GOOGLE_CLOUD_PROJECT") if env_project_id: user_project_id = env_project_id print(f"Using project ID from GOOGLE_CLOUD_PROJECT: {user_project_id}") save_credentials(creds, user_project_id) return user_project_id
gemini_env_project_id = os.getenv("GEMINI_PROJECT_ID") if gemini_env_project_id: user_project_id = gemini_env_project_id print(f"Using project ID from GEMINI_PROJECT_ID: {user_project_id}") save_credentials(creds, user_project_id) return user_project_id
4 unmodified lines cached_project_id = creds_data.get("project_id") if cached_project_id: user_project_id = cached_project_id print(f"Loaded project ID from cache: {user_project_id}") return user_project_id except Exception as e: print(f"Could not load project ID from cache: {e}") pass
print("Project ID not found in environment or cache. Probing for user project ID...") if creds.expired and creds.refresh_token: print("Credentials expired. Refreshing before project ID probe...") try: creds.refresh(GoogleAuthRequest()) save_credentials(creds) print("Credentials refreshed successfully.") except Exception as e: print(f"Could not refresh credentials: {e}") raise raise Exception(f"Failed to refresh credentials while getting project ID: {str(e)}") headers = { "Authorization": f"Bearer {creds.token}",17 unmodified lines user_project_id = data.get("cloudaicompanionProject") if not user_project_id: raise ValueError("Could not find 'cloudaicompanionProject' in loadCodeAssist response.") print(f"Successfully fetched user project ID: {user_project_id}")
save_credentials(creds, user_project_id) print("Project ID saved to credential file for future use.") return user_project_id except requests.exceptions.HTTPError as e: print(f"Error fetching project ID: {e.response.text}") raise123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170"""Configuration constants for the Geminicli2api proxy server.Centralizes all configuration to avoid duplication across modules."""import os
# API EndpointsCODE_ASSIST_ENDPOINT = "https://cloudcode-pa.googleapis.com"
# Client ConfigurationCLI_VERSION = "0.1.5" # Match current gemini-cli version
# OAuth ConfigurationCLIENT_ID = "681255809395-oo8ft2oprdrnp9e3aqf6av3hmdib135j.apps.googleusercontent.com"CLIENT_SECRET = "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"SCOPES = [ "https://www.googleapis.com/auth/cloud-platform", "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile",]
# File PathsSCRIPT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))CREDENTIAL_FILE = os.path.join(SCRIPT_DIR, "oauth_creds.json")
# AuthenticationGEMINI_AUTH_PASSWORD = os.getenv("GEMINI_AUTH_PASSWORD", "123456")
# Default Safety Settings for Google APIDEFAULT_SAFETY_SETTINGS = [ {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"}, {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"}, {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"}, {"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"}, {"category": "HARM_CATEGORY_CIVIC_INTEGRITY", "threshold": "BLOCK_NONE"}]
# Supported Models (for /v1beta/models endpoint)SUPPORTED_MODELS = [ { "name": "models/gemini-1.5-pro", "version": "001", "displayName": "Gemini 1.5 Pro", "description": "Mid-size multimodal model that supports up to 2 million tokens", "inputTokenLimit": 2097152, "outputTokenLimit": 8192, "supportedGenerationMethods": ["generateContent", "streamGenerateContent"], "temperature": 1.0, "maxTemperature": 2.0, "topP": 0.95, "topK": 64 }, { "name": "models/gemini-1.5-flash", "version": "001", "displayName": "Gemini 1.5 Flash", "description": "Fast and versatile multimodal model for scaling across diverse tasks", "inputTokenLimit": 1048576, "outputTokenLimit": 8192, "supportedGenerationMethods": ["generateContent", "streamGenerateContent"], "temperature": 1.0, "maxTemperature": 2.0, "topP": 0.95, "topK": 64 }, { "name": "models/gemini-2.5-pro-preview-05-06", "version": "001", "displayName": "Gemini 2.5 Pro Preview 05-06", "description": "Preview version of Gemini 2.5 Pro from May 6th", "inputTokenLimit": 1048576, "outputTokenLimit": 8192, "supportedGenerationMethods": ["generateContent", "streamGenerateContent"], "temperature": 1.0, "maxTemperature": 2.0, "topP": 0.95, "topK": 64 }, { "name": "models/gemini-2.5-pro-preview-06-05", "version": "001", "displayName": "Gemini 2.5 Pro Preview 06-05", "description": "Preview version of Gemini 2.5 Pro from June 5th", "inputTokenLimit": 1048576, "outputTokenLimit": 8192, "supportedGenerationMethods": ["generateContent", "streamGenerateContent"], "temperature": 1.0, "maxTemperature": 2.0, "topP": 0.95, "topK": 64 }, { "name": "models/gemini-2.5-pro", "version": "001", "displayName": "Gemini 2.5 Pro", "description": "Advanced multimodal model with enhanced capabilities", "inputTokenLimit": 1048576, "outputTokenLimit": 8192, "supportedGenerationMethods": ["generateContent", "streamGenerateContent"], "temperature": 1.0, "maxTemperature": 2.0, "topP": 0.95, "topK": 64 }, { "name": "models/gemini-2.5-flash-preview-05-20", "version": "001", "displayName": "Gemini 2.5 Flash Preview 05-20", "description": "Preview version of Gemini 2.5 Flash from May 20th", "inputTokenLimit": 1048576, "outputTokenLimit": 8192, "supportedGenerationMethods": ["generateContent", "streamGenerateContent"], "temperature": 1.0, "maxTemperature": 2.0, "topP": 0.95, "topK": 64 }, { "name": "models/gemini-2.5-flash", "version": "001", "displayName": "Gemini 2.5 Flash", "description": "Fast and efficient multimodal model with latest improvements", "inputTokenLimit": 1048576, "outputTokenLimit": 8192, "supportedGenerationMethods": ["generateContent", "streamGenerateContent"], "temperature": 1.0, "maxTemperature": 2.0, "topP": 0.95, "topK": 64 }, { "name": "models/gemini-2.0-flash", "version": "001", "displayName": "Gemini 2.0 Flash", "description": "Latest generation fast multimodal model", "inputTokenLimit": 1048576, "outputTokenLimit": 8192, "supportedGenerationMethods": ["generateContent", "streamGenerateContent"], "temperature": 1.0, "maxTemperature": 2.0, "topP": 0.95, "topK": 64 }, { "name": "models/gemini-2.0-flash-preview-image-generation", "version": "001", "displayName": "Gemini 2.0 Flash Preview Image Generation", "description": "Preview version with image generation capabilities", "inputTokenLimit": 32000, "outputTokenLimit": 8192, "supportedGenerationMethods": ["generateContent", "streamGenerateContent"], "temperature": 1.0, "maxTemperature": 2.0, "topP": 0.95, "topK": 64 }, { "name": "models/gemini-embedding-001", "version": "001", "displayName": "Gemini Embedding 001", "description": "Text embedding model for semantic similarity and search", "inputTokenLimit": 2048, "outputTokenLimit": 1, "supportedGenerationMethods": ["embedContent"], "temperature": 0.0, "maxTemperature": 0.0, "topP": 1.0, "topK": 1 }]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228import jsonimport requestsfrom fastapi import APIRouter, Request, Response, Depends
from .auth import authenticate_user, get_credentials, get_user_project_id, onboard_user, save_credentialsfrom .utils import get_user_agentfrom .gemini_request_builder import build_gemini_requestfrom .gemini_response_handler import handle_gemini_response
CODE_ASSIST_ENDPOINT = "https://cloudcode-pa.googleapis.com"
router = APIRouter()
@router.get("/v1beta/models")async def list_models(request: Request, username: str = Depends(authenticate_user)): """List available models - matching gemini-cli supported models exactly.""" print(f"[GET] {request.url.path} - User: {username}") print(f"[MODELS] Serving models list (both /v1/models and /v1beta/models return the same data)") models_response = { "models": [ { "name": "models/gemini-1.5-pro", "version": "001", "displayName": "Gemini 1.5 Pro", "description": "Mid-size multimodal model that supports up to 2 million tokens", "inputTokenLimit": 2097152, "outputTokenLimit": 8192, "supportedGenerationMethods": ["generateContent", "streamGenerateContent"], "temperature": 1.0, "maxTemperature": 2.0, "topP": 0.95, "topK": 64 }, { "name": "models/gemini-1.5-flash", "version": "001", "displayName": "Gemini 1.5 Flash", "description": "Fast and versatile multimodal model for scaling across diverse tasks", "inputTokenLimit": 1048576, "outputTokenLimit": 8192, "supportedGenerationMethods": ["generateContent", "streamGenerateContent"], "temperature": 1.0, "maxTemperature": 2.0, "topP": 0.95, "topK": 64 }, { "name": "models/gemini-2.5-pro-preview-05-06", "version": "001", "displayName": "Gemini 2.5 Pro Preview 05-06", "description": "Preview version of Gemini 2.5 Pro from May 6th", "inputTokenLimit": 1048576, "outputTokenLimit": 8192, "supportedGenerationMethods": ["generateContent", "streamGenerateContent"], "temperature": 1.0, "maxTemperature": 2.0, "topP": 0.95, "topK": 64 }, { "name": "models/gemini-2.5-pro-preview-06-05", "version": "001", "displayName": "Gemini 2.5 Pro Preview 06-05", "description": "Preview version of Gemini 2.5 Pro from June 5th", "inputTokenLimit": 1048576, "outputTokenLimit": 8192, "supportedGenerationMethods": ["generateContent", "streamGenerateContent"], "temperature": 1.0, "maxTemperature": 2.0, "topP": 0.95, "topK": 64 }, { "name": "models/gemini-2.5-pro", "version": "001", "displayName": "Gemini 2.5 Pro", "description": "Advanced multimodal model with enhanced capabilities", "inputTokenLimit": 1048576, "outputTokenLimit": 8192, "supportedGenerationMethods": ["generateContent", "streamGenerateContent"], "temperature": 1.0, "maxTemperature": 2.0, "topP": 0.95, "topK": 64 }, { "name": "models/gemini-2.5-flash-preview-05-20", "version": "001", "displayName": "Gemini 2.5 Flash Preview 05-20", "description": "Preview version of Gemini 2.5 Flash from May 20th", "inputTokenLimit": 1048576, "outputTokenLimit": 8192, "supportedGenerationMethods": ["generateContent", "streamGenerateContent"], "temperature": 1.0, "maxTemperature": 2.0, "topP": 0.95, "topK": 64 }, { "name": "models/gemini-2.5-flash", "version": "001", "displayName": "Gemini 2.5 Flash", "description": "Fast and efficient multimodal model with latest improvements", "inputTokenLimit": 1048576, "outputTokenLimit": 8192, "supportedGenerationMethods": ["generateContent", "streamGenerateContent"], "temperature": 1.0, "maxTemperature": 2.0, "topP": 0.95, "topK": 64 }, { "name": "models/gemini-2.0-flash", "version": "001", "displayName": "Gemini 2.0 Flash", "description": "Latest generation fast multimodal model", "inputTokenLimit": 1048576, "outputTokenLimit": 8192, "supportedGenerationMethods": ["generateContent", "streamGenerateContent"], "temperature": 1.0, "maxTemperature": 2.0, "topP": 0.95, "topK": 64 }, { "name": "models/gemini-2.0-flash-preview-image-generation", "version": "001", "displayName": "Gemini 2.0 Flash Preview Image Generation", "description": "Preview version with image generation capabilities", "inputTokenLimit": 32000, "outputTokenLimit": 8192, "supportedGenerationMethods": ["generateContent", "streamGenerateContent"], "temperature": 1.0, "maxTemperature": 2.0, "topP": 0.95, "topK": 64 }, { "name": "models/gemini-embedding-001", "version": "001", "displayName": "Gemini Embedding 001", "description": "Text embedding model for semantic similarity and search", "inputTokenLimit": 2048, "outputTokenLimit": 1, "supportedGenerationMethods": ["embedContent"], "temperature": 0.0, "maxTemperature": 0.0, "topP": 1.0, "topK": 1 } ] } return Response(content=json.dumps(models_response), status_code=200, media_type="application/json; charset=utf-8")
async def proxy_request(post_data: bytes, full_path: str, username: str, method: str, query_params: dict, is_openai: bool = False, is_streaming: bool = False): print(f"[{method}] /{full_path} - User: {username}") creds = get_credentials() if not creds: print("❌ No credentials available") return Response(content="Authentication failed. Please restart the proxy to log in.", status_code=500) print(f"Using credentials - Token: {creds.token[:20] if creds.token else 'None'}..., Expired: {creds.expired}")
if creds.expired and creds.refresh_token: print("Credentials expired. Refreshing...") try: from google.auth.transport.requests import Request as GoogleAuthRequest creds.refresh(GoogleAuthRequest()) save_credentials(creds) print("Credentials refreshed successfully.") except Exception as e: print(f"Could not refresh token during request: {e}") return Response(content="Token refresh failed. Please restart the proxy to re-authenticate.", status_code=500) elif not creds.token: print("No access token available.") return Response(content="No access token. Please restart the proxy to re-authenticate.", status_code=500)
proj_id = get_user_project_id(creds) if not proj_id: return Response(content="Failed to get user project ID.", status_code=500) onboard_user(creds, proj_id)
if is_openai: target_url, final_post_data, request_headers, _ = build_gemini_request(post_data, full_path, creds, is_streaming) else: action = "streamGenerateContent" if is_streaming else "generateContent" target_url = f"{CODE_ASSIST_ENDPOINT}/v1internal:{action}" + "?alt=sse" try: incoming_json = json.loads(post_data) except (json.JSONDecodeError, AttributeError): incoming_json = {} final_post_data = json.dumps({ "model": full_path.split('/')[2].split(':')[0], "project": proj_id, "request": incoming_json, }) request_headers = { "Authorization": f"Bearer {creds.token}", "Content-Type": "application/json", "User-Agent": get_user_agent(), }
if is_streaming: print(f"STREAMING REQUEST to: {target_url}") print(f"STREAMING REQUEST PAYLOAD: {final_post_data}") resp = requests.post(target_url, data=final_post_data, headers=request_headers, stream=True) print(f"STREAMING RESPONSE: {resp.status_code}") return handle_gemini_response(resp, is_streaming=True) else: print(f"REQUEST to: {target_url}") print(f"REQUEST PAYLOAD: {final_post_data}") resp = requests.post(target_url, data=final_post_data, headers=request_headers) print(f"RESPONSE: {resp.status_code}, {resp.text}") return handle_gemini_response(resp, is_streaming=False)
@router.api_route("/{full_path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH"])async def proxy(request: Request, full_path: str, username: str = Depends(authenticate_user)): post_data = await request.body() is_streaming = "stream" in full_path return await proxy_request(post_data, full_path, username, request.method, dict(request.query_params), is_streaming=is_streaming)1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768import jsonimport re
from .auth import get_user_project_idfrom .utils import get_user_agent
CODE_ASSIST_ENDPOINT = "https://cloudcode-pa.googleapis.com"
def build_gemini_request(post_data: bytes, full_path: str, creds, is_streaming: bool = False): try: incoming_json = json.loads(post_data) except (json.JSONDecodeError, AttributeError): incoming_json = {}
# Set the action based on streaming action = "streamGenerateContent" if is_streaming else "generateContent"
# The target URL is always one of two values target_url = f"{CODE_ASSIST_ENDPOINT}/v1internal:{action}" if is_streaming: target_url += "?alt=sse"
# Extract model from the incoming JSON payload final_model = incoming_json.get("model")
# Default safety settings if not provided safety_settings = incoming_json.get("safetySettings") if not safety_settings: safety_settings = [ {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"}, {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"}, {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"}, {"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"}, {"category": "HARM_CATEGORY_CIVIC_INTEGRITY", "threshold": "BLOCK_NONE"} ]
# Build the final payload for the Google API structured_payload = { "model": final_model, "project": get_user_project_id(creds), "request": { "contents": incoming_json.get("contents"), "systemInstruction": incoming_json.get("systemInstruction"), "cachedContent": incoming_json.get("cachedContent"), "tools": incoming_json.get("tools"), "toolConfig": incoming_json.get("toolConfig"), "safetySettings": safety_settings, "generationConfig": incoming_json.get("generationConfig", {}), }, } # Remove any keys with None values from the request structured_payload["request"] = { k: v for k, v in structured_payload["request"].items() if v is not None } final_post_data = json.dumps(structured_payload)
# Build the request headers request_headers = { "Authorization": f"Bearer {creds.token}", "Content-Type": "application/json", "User-Agent": get_user_agent(), }
return target_url, final_post_data, request_headers, is_streaming12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273import jsonimport requestsfrom fastapi import Responsefrom fastapi.responses import StreamingResponseimport asyncio
def handle_gemini_response(resp, is_streaming): if is_streaming: async def stream_generator(): try: with resp: resp.raise_for_status() print("[STREAM] Processing with Gemini SDK-compatible logic") for chunk in resp.iter_lines(): if chunk: if not isinstance(chunk, str): chunk = chunk.decode('utf-8') print(chunk) if chunk.startswith('data: '): chunk = chunk[len('data: '):] try: obj = json.loads(chunk) if "response" in obj: response_chunk = obj["response"] response_json = json.dumps(response_chunk, separators=(',', ':')) response_line = f"data: {response_json}\n\n" yield response_line await asyncio.sleep(0) except json.JSONDecodeError: continue except requests.exceptions.RequestException as e: print(f"Error during streaming request: {e}") yield f'data: {{"error": {{"message": "Upstream request failed: {str(e)}"}}}}\n\n'.encode('utf-8') except Exception as e: print(f"An unexpected error occurred during streaming: {e}") yield f'data: {{"error": {{"message": "An unexpected error occurred: {str(e)}"}}}}\n\n'.encode('utf-8')
response_headers = { "Content-Type": "text/event-stream", "Content-Disposition": "attachment", "Vary": "Origin, X-Origin, Referer", "X-XSS-Protection": "0", "X-Frame-Options": "SAMEORIGIN", "X-Content-Type-Options": "nosniff", "Server": "ESF" } return StreamingResponse( stream_generator(), media_type="text/event-stream", headers=response_headers ) else: if resp.status_code == 200: try: google_api_response = resp.text if google_api_response.startswith('data: '): google_api_response = google_api_response[len('data: '):] google_api_response = json.loads(google_api_response) standard_gemini_response = google_api_response.get("response") return Response(content=json.dumps(standard_gemini_response), status_code=200, media_type="application/json; charset=utf-8") except (json.JSONDecodeError, AttributeError) as e: print(f"Error converting to standard Gemini format: {e}") return Response(content=resp.content, status_code=resp.status_code, media_type=resp.headers.get("Content-Type")) else: return Response(content=resp.content, status_code=resp.status_code, media_type=resp.headers.get("Content-Type"))123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144"""Gemini API Routes - Handles native Gemini API endpoints.This module provides native Gemini API endpoints that proxy directly to Google's APIwithout any format transformations."""import jsonfrom fastapi import APIRouter, Request, Response, Depends
from .auth import authenticate_userfrom .google_api_client import send_gemini_request, build_gemini_payload_from_nativefrom .config import SUPPORTED_MODELS
router = APIRouter()
@router.get("/v1beta/models")async def gemini_list_models(request: Request, username: str = Depends(authenticate_user)): """ Native Gemini models endpoint. Returns available models in Gemini format, matching the official Gemini API. """ models_response = { "models": SUPPORTED_MODELS } return Response( content=json.dumps(models_response), status_code=200, media_type="application/json; charset=utf-8" )
@router.api_route("/{full_path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH"])async def gemini_proxy(request: Request, full_path: str, username: str = Depends(authenticate_user)): """ Native Gemini API proxy endpoint. Handles all native Gemini API calls by proxying them directly to Google's API. This endpoint handles paths like: - /v1beta/models/{model}/generateContent - /v1beta/models/{model}/streamGenerateContent - /v1/models/{model}/generateContent - etc. """ # Get the request body post_data = await request.body() # Determine if this is a streaming request is_streaming = "stream" in full_path.lower() # Extract model name from the path # Paths typically look like: v1beta/models/gemini-1.5-pro/generateContent model_name = _extract_model_from_path(full_path) if not model_name: return Response( content=json.dumps({ "error": { "message": f"Could not extract model name from path: {full_path}", "code": 400 } }), status_code=400, media_type="application/json" ) # Parse the incoming request try: if post_data: incoming_request = json.loads(post_data) else: incoming_request = {} except json.JSONDecodeError: return Response( content=json.dumps({ "error": { "message": "Invalid JSON in request body", "code": 400 } }), status_code=400, media_type="application/json" ) # Build the payload for Google API gemini_payload = build_gemini_payload_from_native(incoming_request, model_name) # Send the request to Google API response = send_gemini_request(gemini_payload, is_streaming=is_streaming) return response
def _extract_model_from_path(path: str) -> str: """ Extract the model name from a Gemini API path. Examples: - "v1beta/models/gemini-1.5-pro/generateContent" -> "gemini-1.5-pro" - "v1/models/gemini-2.0-flash/streamGenerateContent" -> "gemini-2.0-flash" Args: path: The API path Returns: Model name (just the model name, not prefixed with "models/") or None if not found """ parts = path.split('/') # Look for the pattern: .../models/{model_name}/... try: models_index = parts.index('models') if models_index + 1 < len(parts): model_name = parts[models_index + 1] # Remove any action suffix like ":streamGenerateContent" or ":generateContent" if ':' in model_name: model_name = model_name.split(':')[0] # Return just the model name without "models/" prefix return model_name except ValueError: pass # If we can't find the pattern, return None return None
@router.get("/v1/models")async def gemini_list_models_v1(request: Request, username: str = Depends(authenticate_user)): """ Alternative models endpoint for v1 API version. Some clients might use /v1/models instead of /v1beta/models. """ return await gemini_list_models(request, username)
# Health check endpoint@router.get("/health")async def health_check(): """ Simple health check endpoint. """ return {"status": "healthy", "service": "geminicli2api"}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214"""Google API Client - Handles all communication with Google's Gemini API.This module is used by both OpenAI compatibility layer and native Gemini endpoints."""import jsonimport requestsfrom fastapi import Responsefrom fastapi.responses import StreamingResponsefrom google.auth.transport.requests import Request as GoogleAuthRequest
from .auth import get_credentials, save_credentials, get_user_project_id, onboard_userfrom .utils import get_user_agentfrom .config import CODE_ASSIST_ENDPOINT, DEFAULT_SAFETY_SETTINGS
def send_gemini_request(payload: dict, is_streaming: bool = False) -> Response: """ Send a request to Google's Gemini API. Args: payload: The request payload in Gemini format is_streaming: Whether this is a streaming request Returns: FastAPI Response object """ # Get and validate credentials creds = get_credentials() if not creds: return Response( content="Authentication failed. Please restart the proxy to log in.", status_code=500 )
# Refresh credentials if needed if creds.expired and creds.refresh_token: try: creds.refresh(GoogleAuthRequest()) save_credentials(creds) except Exception as e: return Response( content="Token refresh failed. Please restart the proxy to re-authenticate.", status_code=500 ) elif not creds.token: return Response( content="No access token. Please restart the proxy to re-authenticate.", status_code=500 )
# Get project ID and onboard user proj_id = get_user_project_id(creds) if not proj_id: return Response(content="Failed to get user project ID.", status_code=500) onboard_user(creds, proj_id)
# Build the final payload with project info final_payload = { "model": payload.get("model"), "project": proj_id, "request": payload.get("request", {}) }
# Determine the action and URL action = "streamGenerateContent" if is_streaming else "generateContent" target_url = f"{CODE_ASSIST_ENDPOINT}/v1internal:{action}" if is_streaming: target_url += "?alt=sse"
# Build request headers request_headers = { "Authorization": f"Bearer {creds.token}", "Content-Type": "application/json", "User-Agent": get_user_agent(), }
final_post_data = json.dumps(final_payload)
# Send the request if is_streaming: resp = requests.post(target_url, data=final_post_data, headers=request_headers, stream=True) return _handle_streaming_response(resp) else: resp = requests.post(target_url, data=final_post_data, headers=request_headers) return _handle_non_streaming_response(resp)
def _handle_streaming_response(resp) -> StreamingResponse: """Handle streaming response from Google API.""" import asyncio async def stream_generator(): try: with resp: resp.raise_for_status() for chunk in resp.iter_lines(): if chunk: if not isinstance(chunk, str): chunk = chunk.decode('utf-8') if chunk.startswith('data: '): chunk = chunk[len('data: '):] try: obj = json.loads(chunk) if "response" in obj: response_chunk = obj["response"] response_json = json.dumps(response_chunk, separators=(',', ':')) response_line = f"data: {response_json}\n\n" yield response_line await asyncio.sleep(0) except json.JSONDecodeError: continue except requests.exceptions.RequestException as e: yield f'data: {{"error": {{"message": "Upstream request failed: {str(e)}"}}}}\n\n'.encode('utf-8') except Exception as e: yield f'data: {{"error": {{"message": "An unexpected error occurred: {str(e)}"}}}}\n\n'.encode('utf-8')
response_headers = { "Content-Type": "text/event-stream", "Content-Disposition": "attachment", "Vary": "Origin, X-Origin, Referer", "X-XSS-Protection": "0", "X-Frame-Options": "SAMEORIGIN", "X-Content-Type-Options": "nosniff", "Server": "ESF" } return StreamingResponse( stream_generator(), media_type="text/event-stream", headers=response_headers )
def _handle_non_streaming_response(resp) -> Response: """Handle non-streaming response from Google API.""" if resp.status_code == 200: try: google_api_response = resp.text if google_api_response.startswith('data: '): google_api_response = google_api_response[len('data: '):] google_api_response = json.loads(google_api_response) standard_gemini_response = google_api_response.get("response") return Response( content=json.dumps(standard_gemini_response), status_code=200, media_type="application/json; charset=utf-8" ) except (json.JSONDecodeError, AttributeError) as e: return Response( content=resp.content, status_code=resp.status_code, media_type=resp.headers.get("Content-Type") ) else: return Response( content=resp.content, status_code=resp.status_code, media_type=resp.headers.get("Content-Type") )
def build_gemini_payload_from_openai(openai_payload: dict) -> dict: """ Build a Gemini API payload from an OpenAI-transformed request. This is used when OpenAI requests are converted to Gemini format. """ # Extract model from the payload model = openai_payload.get("model") # Get safety settings or use defaults safety_settings = openai_payload.get("safetySettings", DEFAULT_SAFETY_SETTINGS) # Build the request portion request_data = { "contents": openai_payload.get("contents"), "systemInstruction": openai_payload.get("systemInstruction"), "cachedContent": openai_payload.get("cachedContent"), "tools": openai_payload.get("tools"), "toolConfig": openai_payload.get("toolConfig"), "safetySettings": safety_settings, "generationConfig": openai_payload.get("generationConfig", {}), } # Remove any keys with None values request_data = {k: v for k, v in request_data.items() if v is not None} return { "model": model, "request": request_data }
def build_gemini_payload_from_native(native_request: dict, model_from_path: str) -> dict: """ Build a Gemini API payload from a native Gemini request. This is used for direct Gemini API calls. """ # Add default safety settings if not provided if "safetySettings" not in native_request: native_request["safetySettings"] = DEFAULT_SAFETY_SETTINGS return { "model": model_from_path, "request": native_request }1234345679 unmodified lines1718192021222324252627282930312021222324252627282930313233343536373839from fastapi import FastAPI, Request, Responsefrom fastapi.middleware.cors import CORSMiddlewarefrom .gemini import router as gemini_routerfrom .openai import router as openai_routerfrom .gemini_routes import router as gemini_routerfrom .openai_routes import router as openai_routerfrom .auth import get_credentials, get_user_project_id, onboard_user
app = FastAPI()9 unmodified lines
@app.on_event("startup")async def startup_event(): print("Initializing credentials...") creds = get_credentials() if creds: proj_id = get_user_project_id(creds) if proj_id: onboard_user(creds, proj_id) print(f"\nStarting Gemini proxy server") print("Send your Gemini API requests to this address.") print(f"Authentication required - Password: see .env file") print("Use HTTP Basic Authentication with any username and the password above.") else: print("\nCould not obtain credentials. Please authenticate and restart the server.") try: creds = get_credentials() if creds: try: proj_id = get_user_project_id(creds) if proj_id: onboard_user(creds, proj_id) print("Gemini proxy server started") print("Authentication required - Password: see .env file") except Exception as e: print(f"Setup failed: {str(e)}") print("Server started but may not function properly until setup issues are resolved.") else: print("Could not obtain credentials. Please authenticate and restart the server.") except Exception as e: print(f"Startup error: {str(e)}") print("Server may not function properly.")
@app.options("/{full_path:path}")async def handle_preflight(request: Request, full_path: str):12 unmodified lines13141516171819202122232425262712 unmodified lines temperature: Optional[float] = None top_p: Optional[float] = None max_tokens: Optional[int] = None stop: Optional[Union[str, List[str]]] = None frequency_penalty: Optional[float] = None presence_penalty: Optional[float] = None n: Optional[int] = None seed: Optional[int] = None response_format: Optional[Dict[str, Any]] = None class Config: extra = "allow" # Allow additional fields not explicitly defined
class OpenAIChatCompletionChoice(BaseModel): index: int123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184import jsonimport timeimport uuidfrom fastapi import APIRouter, Request, Response, Dependsfrom fastapi.responses import StreamingResponse
from .auth import authenticate_userfrom .models import OpenAIChatCompletionRequest, OpenAIChatCompletionResponse, OpenAIChatCompletionStreamResponse, OpenAIChatMessage, OpenAIChatCompletionChoice, OpenAIChatCompletionStreamChoice, OpenAIDelta, GeminiRequest, GeminiContent, GeminiPart, GeminiResponsefrom .gemini import proxy_request
import asyncio
router = APIRouter()
def openai_to_gemini(openai_request: OpenAIChatCompletionRequest) -> dict: contents = [] for message in openai_request.messages: role = message.role if role == "assistant": role = "model" if role == "system": role = "user" if isinstance(message.content, list): parts = [] for part in message.content: if part.get("type") == "text": parts.append({"text": part.get("text", "")}) elif part.get("type") == "image_url": image_url = part.get("image_url", {}).get("url") if image_url: # Assuming the image_url is a base64 encoded string # "data:image/jpeg;base64,{base64_image}" mime_type, base64_data = image_url.split(";") _, mime_type = mime_type.split(":") _, base64_data = base64_data.split(",") parts.append({ "inlineData": { "mimeType": mime_type, "data": base64_data } }) contents.append({"role": role, "parts": parts}) else: contents.append({"role": role, "parts": [{"text": message.content}]}) generation_config = {} if openai_request.temperature is not None: generation_config["temperature"] = openai_request.temperature if openai_request.top_p is not None: generation_config["topP"] = openai_request.top_p if openai_request.max_tokens is not None: generation_config["maxOutputTokens"] = openai_request.max_tokens
safety_settings = [ {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"}, {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"}, {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"}, {"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"}, {"category": "HARM_CATEGORY_CIVIC_INTEGRITY", "threshold": "BLOCK_NONE"} ]
return { "contents": contents, "generationConfig": generation_config, "safetySettings": safety_settings, "model": openai_request.model }
def gemini_to_openai(gemini_response: dict, model: str) -> OpenAIChatCompletionResponse: choices = [] for candidate in gemini_response.get("candidates", []): role = candidate.get("content", {}).get("role", "assistant") if role == "model": role = "assistant" choices.append( { "index": candidate.get("index"), "message": { "role": role, "content": candidate.get("content", {}).get("parts", [{}])[0].get("text"), }, "finish_reason": map_finish_reason(candidate.get("finishReason")), } ) return { "id": str(uuid.uuid4()), "object": "chat.completion", "created": int(time.time()), "model": model, "choices": choices, }
def gemini_to_openai_stream(gemini_response: dict, model: str, response_id: str) -> dict: choices = [] for candidate in gemini_response.get("candidates", []): role = candidate.get("content", {}).get("role", "assistant") if role == "model": role = "assistant" choices.append( { "index": candidate.get("index"), "delta": { "content": candidate.get("content", {}).get("parts", [{}])[0].get("text"), }, "finish_reason": map_finish_reason(candidate.get("finishReason")), } ) return { "id": response_id, "object": "chat.completion.chunk", "created": int(time.time()), "model": model, "choices": choices, }
def map_finish_reason(reason: str) -> str: if reason == "STOP": return "stop" elif reason == "MAX_TOKENS": return "length" elif reason in ["SAFETY", "RECITATION"]: return "content_filter" else: return None
@router.post("/v1/chat/completions")async def chat_completions(request: OpenAIChatCompletionRequest, http_request: Request, username: str = Depends(authenticate_user)): gemini_request = openai_to_gemini(request) if request.stream: async def stream_generator(): response = await proxy_request(json.dumps(gemini_request).encode('utf-8'), http_request.url.path, username, "POST", dict(http_request.query_params), is_openai=True, is_streaming=True) if isinstance(response, StreamingResponse): response_id = "chatcmpl-realstream-" + str(uuid.uuid4()) async for chunk in response.body_iterator: if chunk.startswith('data: '): try: data = json.loads(chunk[6:]) openai_response = gemini_to_openai_stream(data, request.model, response_id) yield f"data: {json.dumps(openai_response)}\n\n" await asyncio.sleep(0) except (json.JSONDecodeError, KeyError): continue yield "data: [DONE]\n\n" else: yield f"data: {response.body.decode()}\n\n" yield "data: [DONE]\n\n"
return StreamingResponse(stream_generator(), media_type="text/event-stream") else: response = await proxy_request(json.dumps(gemini_request).encode('utf-8'), http_request.url.path, username, "POST", dict(http_request.query_params), is_openai=True, is_streaming=False) if isinstance(response, Response) and response.status_code != 200: return response gemini_response = json.loads(response.body) openai_response = gemini_to_openai(gemini_response, request.model) return openai_response
async def event_generator(): """ A generator function that yields a message in the Server-Sent Event (SSE) format every second, five times. """ count = 0 while count < 5: # SSE format is "data: <content>\n\n" # The two newlines are crucial as they mark the end of an event. yield "data: 1\n\n" # Log to the server console to see it working on the backend count += 1 print(f"Sent chunk {count}/5")
# Wait for 1 second await asyncio.sleep(1)
@router.post("/v1/test")async def stream_data(request: OpenAIChatCompletionRequest, http_request: Request, username: str = Depends(authenticate_user)): """ This endpoint returns a streaming response. It uses the event_generator to send data chunks. The media_type is 'text/event-stream' which is standard for SSE. """ return StreamingResponse(event_generator(), media_type="text/event-stream")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180"""OpenAI API Routes - Handles OpenAI-compatible endpoints.This module provides OpenAI-compatible endpoints that transform requests/responsesand delegate to the Google API client."""import jsonimport uuidimport asynciofrom fastapi import APIRouter, Request, Response, Dependsfrom fastapi.responses import StreamingResponse
from .auth import authenticate_userfrom .models import OpenAIChatCompletionRequestfrom .openai_transformers import ( openai_request_to_gemini, gemini_response_to_openai, gemini_stream_chunk_to_openai)from .google_api_client import send_gemini_request, build_gemini_payload_from_openai
router = APIRouter()
@router.post("/v1/chat/completions")async def openai_chat_completions( request: OpenAIChatCompletionRequest, http_request: Request, username: str = Depends(authenticate_user)): """ OpenAI-compatible chat completions endpoint. Transforms OpenAI requests to Gemini format, sends to Google API, and transforms responses back to OpenAI format. """ # Transform OpenAI request to Gemini format gemini_request_data = openai_request_to_gemini(request) # Build the payload for Google API gemini_payload = build_gemini_payload_from_openai(gemini_request_data) if request.stream: # Handle streaming response async def openai_stream_generator(): response = send_gemini_request(gemini_payload, is_streaming=True) if isinstance(response, StreamingResponse): response_id = "chatcmpl-" + str(uuid.uuid4()) async for chunk in response.body_iterator: if isinstance(chunk, bytes): chunk = chunk.decode('utf-8') if chunk.startswith('data: '): try: # Parse the Gemini streaming chunk chunk_data = chunk[6:] # Remove 'data: ' prefix gemini_chunk = json.loads(chunk_data) # Transform to OpenAI format openai_chunk = gemini_stream_chunk_to_openai( gemini_chunk, request.model, response_id ) # Send as OpenAI streaming format yield f"data: {json.dumps(openai_chunk)}\n\n" await asyncio.sleep(0) except (json.JSONDecodeError, KeyError, UnicodeDecodeError) as e: continue # Send the final [DONE] marker yield "data: [DONE]\n\n" else: # Error case - forward the error response error_data = { "error": { "message": "Streaming request failed", "type": "api_error" } } yield f"data: {json.dumps(error_data)}\n\n" yield "data: [DONE]\n\n"
return StreamingResponse( openai_stream_generator(), media_type="text/event-stream" ) else: # Handle non-streaming response response = send_gemini_request(gemini_payload, is_streaming=False) if isinstance(response, Response) and response.status_code != 200: # Forward error responses as-is return response try: # Parse Gemini response and transform to OpenAI format gemini_response = json.loads(response.body) openai_response = gemini_response_to_openai(gemini_response, request.model) return openai_response except (json.JSONDecodeError, AttributeError) as e: return Response( content=json.dumps({ "error": { "message": "Failed to process response", "type": "api_error" } }), status_code=500, media_type="application/json" )
@router.get("/v1/models")async def openai_list_models(username: str = Depends(authenticate_user)): """ OpenAI-compatible models endpoint. Returns available models in OpenAI format. """ # Convert our Gemini models to OpenAI format from .config import SUPPORTED_MODELS openai_models = [] for model in SUPPORTED_MODELS: openai_models.append({ "id": model["name"], "object": "model", "created": 1677610602, # Static timestamp "owned_by": "google", "permission": [ { "id": "modelperm-" + model["name"].replace("/", "-"), "object": "model_permission", "created": 1677610602, "allow_create_engine": False, "allow_sampling": True, "allow_logprobs": False, "allow_search_indices": False, "allow_view": True, "allow_fine_tuning": False, "organization": "*", "group": None, "is_blocking": False } ], "root": model["name"], "parent": None }) return { "object": "list", "data": openai_models }
# Test endpoint for debugging (can be removed in production)@router.post("/v1/test")async def openai_test_endpoint( request: OpenAIChatCompletionRequest, username: str = Depends(authenticate_user)): """ Test endpoint for debugging OpenAI transformations. """ # Transform the request and return the result for inspection gemini_request_data = openai_request_to_gemini(request) return { "original_openai_request": request.dict(), "transformed_gemini_request": gemini_request_data, "message": "Transformation successful" }123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207"""OpenAI Format Transformers - Handles conversion between OpenAI and Gemini API formats.This module contains all the logic for transforming requests and responses between the two formats."""import jsonimport timeimport uuidfrom typing import Dict, Any
from .models import OpenAIChatCompletionRequest, OpenAIChatCompletionResponsefrom .config import DEFAULT_SAFETY_SETTINGS
def openai_request_to_gemini(openai_request: OpenAIChatCompletionRequest) -> Dict[str, Any]: """ Transform an OpenAI chat completion request to Gemini format. Args: openai_request: OpenAI format request Returns: Dictionary in Gemini API format """ contents = [] # Process each message in the conversation for message in openai_request.messages: role = message.role # Map OpenAI roles to Gemini roles if role == "assistant": role = "model" elif role == "system": role = "user" # Gemini treats system messages as user messages # Handle different content types (string vs list of parts) if isinstance(message.content, list): parts = [] for part in message.content: if part.get("type") == "text": parts.append({"text": part.get("text", "")}) elif part.get("type") == "image_url": image_url = part.get("image_url", {}).get("url") if image_url: # Parse data URI: "data:image/jpeg;base64,{base64_image}" try: mime_type, base64_data = image_url.split(";") _, mime_type = mime_type.split(":") _, base64_data = base64_data.split(",") parts.append({ "inlineData": { "mimeType": mime_type, "data": base64_data } }) except ValueError: continue contents.append({"role": role, "parts": parts}) else: # Simple text content contents.append({"role": role, "parts": [{"text": message.content}]}) # Map OpenAI generation parameters to Gemini format generation_config = {} if openai_request.temperature is not None: generation_config["temperature"] = openai_request.temperature if openai_request.top_p is not None: generation_config["topP"] = openai_request.top_p if openai_request.max_tokens is not None: generation_config["maxOutputTokens"] = openai_request.max_tokens if openai_request.stop is not None: # Gemini supports stop sequences if isinstance(openai_request.stop, str): generation_config["stopSequences"] = [openai_request.stop] elif isinstance(openai_request.stop, list): generation_config["stopSequences"] = openai_request.stop if openai_request.frequency_penalty is not None: # Map frequency_penalty to Gemini's frequencyPenalty generation_config["frequencyPenalty"] = openai_request.frequency_penalty if openai_request.presence_penalty is not None: # Map presence_penalty to Gemini's presencePenalty generation_config["presencePenalty"] = openai_request.presence_penalty if openai_request.n is not None: # Map n (number of completions) to Gemini's candidateCount generation_config["candidateCount"] = openai_request.n if openai_request.seed is not None: # Gemini supports seed for reproducible outputs generation_config["seed"] = openai_request.seed if openai_request.response_format is not None: # Handle JSON mode if specified if openai_request.response_format.get("type") == "json_object": generation_config["responseMimeType"] = "application/json"
return { "contents": contents, "generationConfig": generation_config, "safetySettings": DEFAULT_SAFETY_SETTINGS, "model": openai_request.model }
def gemini_response_to_openai(gemini_response: Dict[str, Any], model: str) -> Dict[str, Any]: """ Transform a Gemini API response to OpenAI chat completion format. Args: gemini_response: Response from Gemini API model: Model name to include in response Returns: Dictionary in OpenAI chat completion format """ choices = [] for candidate in gemini_response.get("candidates", []): role = candidate.get("content", {}).get("role", "assistant") # Map Gemini roles back to OpenAI roles if role == "model": role = "assistant" # Extract text content from parts parts = candidate.get("content", {}).get("parts", []) content = "" if parts and len(parts) > 0: content = parts[0].get("text", "") choices.append({ "index": candidate.get("index", 0), "message": { "role": role, "content": content, }, "finish_reason": _map_finish_reason(candidate.get("finishReason")), }) return { "id": str(uuid.uuid4()), "object": "chat.completion", "created": int(time.time()), "model": model, "choices": choices, }
def gemini_stream_chunk_to_openai(gemini_chunk: Dict[str, Any], model: str, response_id: str) -> Dict[str, Any]: """ Transform a Gemini streaming response chunk to OpenAI streaming format. Args: gemini_chunk: Single chunk from Gemini streaming response model: Model name to include in response response_id: Consistent ID for this streaming response Returns: Dictionary in OpenAI streaming format """ choices = [] for candidate in gemini_chunk.get("candidates", []): role = candidate.get("content", {}).get("role", "assistant") # Map Gemini roles back to OpenAI roles if role == "model": role = "assistant" # Extract text content from parts parts = candidate.get("content", {}).get("parts", []) content = "" if parts and len(parts) > 0: content = parts[0].get("text", "") choices.append({ "index": candidate.get("index", 0), "delta": { "content": content, }, "finish_reason": _map_finish_reason(candidate.get("finishReason")), }) return { "id": response_id, "object": "chat.completion.chunk", "created": int(time.time()), "model": model, "choices": choices, }
def _map_finish_reason(gemini_reason: str) -> str: """ Map Gemini finish reasons to OpenAI finish reasons. Args: gemini_reason: Finish reason from Gemini API Returns: OpenAI-compatible finish reason """ if gemini_reason == "STOP": return "stop" elif gemini_reason == "MAX_TOKENS": return "length" elif gemini_reason in ["SAFETY", "RECITATION"]: return "content_filter" else: return None1232345import platform
CLI_VERSION = "0.1.5" # Match current gemini-cli versionfrom .config import CLI_VERSION
def get_user_agent(): """Generate User-Agent string matching gemini-cli format."""src/auth.py+18-72
src/config.pyAdded+170
src/gemini.pyDeleted-228
src/gemini_request_builder.pyDeleted-68
src/gemini_response_handler.pyDeleted-73
src/gemini_routes.pyAdded+144
src/google_api_client.pyAdded+214
src/main.py+19-14
src/models.py+9
src/openai.pyDeleted-184
src/openai_routes.pyAdded+180
src/openai_transformers.pyAdded+207
src/utils.py+1-2