forked from Alishahryar1/free-claude-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathendpoint.py
More file actions
50 lines (39 loc) · 1.83 KB
/
Copy pathendpoint.py
File metadata and controls
50 lines (39 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""Vertex AI service and OpenAI-compatible endpoint construction."""
import re
from urllib.parse import quote
from free_claude_code.application.errors import ApplicationUnavailableError
from free_claude_code.config.provider_catalog import VERTEX_AI_API_ROOT
_LOCATION_PATTERN = re.compile(r"[a-z0-9]+(?:-[a-z0-9]+)*")
def vertex_service_endpoint(location: str) -> str:
"""Return Google's global or regional Vertex AI service endpoint."""
normalized = _validated_location(location)
if normalized == "global":
return VERTEX_AI_API_ROOT
return f"https://{normalized}-aiplatform.googleapis.com"
def vertex_openai_base_url(project_id: str, location: str) -> str:
"""Return the project-scoped Vertex OpenAI-compatible API base URL."""
project = project_id.strip()
if not project:
raise ApplicationUnavailableError(
"VERTEX_PROJECT_ID is not set. Add it in the Admin UI."
)
normalized_location = _validated_location(location)
service_endpoint = vertex_service_endpoint(normalized_location)
return (
f"{service_endpoint}/v1/projects/{quote(project, safe='')}/locations/"
f"{normalized_location}/endpoints/openapi"
)
def vertex_publisher_models_url(location: str) -> str:
"""Return Google's native publisher-model listing endpoint."""
return f"{vertex_service_endpoint(location)}/v1beta1/publishers/google/models"
def _validated_location(location: str) -> str:
normalized = location.strip().lower()
if not normalized:
raise ApplicationUnavailableError(
"VERTEX_LOCATION is not set. Use global or a Google Cloud region."
)
if _LOCATION_PATTERN.fullmatch(normalized) is None:
raise ApplicationUnavailableError(
"VERTEX_LOCATION must be global or a lowercase Google Cloud region."
)
return normalized