googlecommon

package
v0.1.55 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 20, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package googlecommon holds infrastructure shared by GoModel's Google-backed providers (Gemini AI Studio + Vertex AI). It currently covers authentication (ADC / service-account TokenSource resolution + quota project propagation via X-Goog-User-Project) and Vertex URL transformations between the native publisher endpoint and the OpenAI-compatible endpoint.

Index

Constants

View Source
const DefaultScope = "https://www.googleapis.com/auth/cloud-platform"
View Source
const QuotaProjectHeader = "X-Goog-User-Project"

QuotaProjectHeader is the HTTP header Google APIs read to determine which project to bill and count quota against. Required for ADC user credentials against APIs like aiplatform.googleapis.com; harmless (and routinely set) for service-account credentials.

Variables

This section is empty.

Functions

func HTTPClient

func HTTPClient(base *http.Client, source oauth2.TokenSource, quotaProject string) *http.Client

HTTPClient returns an *http.Client that injects bearer tokens from source on every request and, when quotaProject is non-empty, also sets the X-Goog-User-Project header. The latter is required for ADC user-credential flows against APIs like aiplatform.googleapis.com that demand an explicit billing project; pass an empty string to skip.

func HasServiceAccount

func HasServiceAccount(cfg Config) bool

func NormalizeAuthType

func NormalizeAuthType(authType string, hasServiceAccount bool) string

func VertexBaseURLs

func VertexBaseURLs(baseURL, project, location string) (openAICompatibleBaseURL, nativeBaseURL string)

VertexBaseURLs derives Vertex AI's two base URLs (OpenAI-compatible endpoint + native publisher endpoint) from either an operator-supplied baseURL or the project/location pair. When baseURL is empty the canonical aiplatform.googleapis.com host is used. When baseURL is supplied:

  • if it ends in /endpoints/openapi it is treated as the OpenAI-compatible surface and the native URL is derived by replacing the suffix
  • if it ends in /publishers/google it is treated as the native surface and the OpenAI-compatible URL is derived likewise
  • otherwise both bases are set to the verbatim baseURL (caller has wired up a custom proxy and is responsible for routing both shapes through it)

Returning two strings rather than mutating a *Config keeps this pure and trivially testable; callers unpack their own provider config.

func VertexNativeBaseURLFromOpenAICompatibleBaseURL

func VertexNativeBaseURLFromOpenAICompatibleBaseURL(baseURL string) (string, bool)

VertexNativeBaseURLFromOpenAICompatibleBaseURL converts an /endpoints/openapi base URL into the matching /publishers/google native URL. Returns ok=false when the input does not end with /endpoints/openapi.

func VertexOpenAICompatibleBaseURLFromNativeBaseURL

func VertexOpenAICompatibleBaseURLFromNativeBaseURL(baseURL string) (string, bool)

VertexOpenAICompatibleBaseURLFromNativeBaseURL converts a /publishers/google native base URL into the matching /endpoints/openapi OpenAI-compatible URL. Returns ok=false when the input does not end with /publishers/google.

Types

type Config

type Config struct {
	AuthType                 string
	ServiceAccountFile       string
	ServiceAccountJSON       string
	ServiceAccountJSONBase64 string
	Scope                    string
}

type Credentials

type Credentials struct {
	TokenSource    oauth2.TokenSource
	QuotaProjectID string
}

Credentials bundles the OAuth2 token source with the project ID resolved from the credentials material. For ADC user credentials this is the `quota_project_id` written by `gcloud auth application-default set-quota-project`; for service accounts it is the SA JSON `project_id`. QuotaProjectID may be empty when neither source supplies one.

func FindCredentials

func FindCredentials(ctx context.Context, cfg Config) (*Credentials, error)

FindCredentials resolves a TokenSource and QuotaProjectID from either a service-account credential (file, raw JSON, or base64 JSON) or Application Default Credentials. The returned QuotaProjectID flows from ADC `quota_project_id` or SA `project_id` and is intended to be sent as the X-Goog-User-Project header on outbound API calls; callers may override it when their resource-owning project differs from the auth project.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL