gemini

package
v0.1.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Overview

Package gemini implements the Gemini generateContent–compatible HTTP frontend.

v1 subset vs vendor spec (cross-check)

Spec: https://ai.google.dev/gemini-api/docs , https://ai.google.dev/api/all-methods , https://ai.google.dev/gemini-api/docs/text-generation

| Area | v1 status | Tests | |-------------------|------------|-------| | POST …:generateContent / :streamGenerateContent | Supported | path_test.go, integration_test.go | | contents, systemInstruction | Supported | decode_test.go | | generationConfig, tools, toolConfig | Supported (decode) | decode_test.go | | functionCall on response | Supported (encode) | encode_test.go, integration_test.go | | safetySettings, cachedContent, … | Out of scope | — |

Index

Constants

View Source
const HeaderRouteSelector = routeselect.HeaderRouteSelector
View Source
const ID = "gemini"

ID is the reserved plugin identifier for the Gemini frontend.

Variables

This section is empty.

Functions

func ModelFromCall

func ModelFromCall(c *lipapi.Call) string

ModelFromCall returns the wire model string stored during decode.

func Mount

func Mount(mux *http.ServeMux, opts lipsdk.FrontendMountOptions) error

Mount registers the Gemini generateContent-compatible handler on mux.

func ParseGenerateContentPath

func ParseGenerateContentPath(urlPath string) (model string, stream bool, ok bool)

ParseGenerateContentPath extracts model id and streaming mode from a Gemini REST path (e.g. /v1beta/models/gemini-2.0-flash:generateContent or …:streamGenerateContent?alt=sse).

func RouteClaims

func RouteClaims(ownerID string) ([]httpcontract.RouteClaim, error)

RouteClaims returns normalized ownership claims; MountPattern is intentionally not exposed through this compatibility projection.

func WriteErrorJSON

func WriteErrorJSON(w http.ResponseWriter, status int, message string) error

WriteErrorJSON writes a Google-style JSON error for generateContent failures. Status strings are derived from the HTTP status code.

func WriteErrorJSONWithStatus

func WriteErrorJSONWithStatus(w http.ResponseWriter, status int, message, googleStatus string) error

WriteErrorJSONWithStatus writes a Google-style JSON error with an explicit RPC status string.

func WriteNonStreamJSON

func WriteNonStreamJSON(ctx context.Context, w http.ResponseWriter, call *lipapi.Call, es lipapi.EventStream, opts EncodeOptions) error

WriteNonStreamJSON encodes a completed canonical stream as a generateContent JSON body.

func WriteStreamSSE

func WriteStreamSSE(ctx context.Context, w http.ResponseWriter, call *lipapi.Call, es lipapi.EventStream, opts EncodeOptions) error

WriteStreamSSE emits Gemini stream chunks incrementally from the canonical stream.

Types

type Config

type Config = frontendconfig.Config

func DecodeConfig

func DecodeConfig(n yaml.Node) (Config, error)

type DecodeOptions

type DecodeOptions struct {
	// RouteSelector is required (e.g. "stub:gemini-2.0-flash"); usually from X-LIP-Route header.
	RouteSelector string
	// Model is taken from the URL path (…/models/{model}:generateContent); must be non-empty.
	Model string
	// Stream is true when the request targets :streamGenerateContent.
	Stream  bool
	Headers http.Header
}

DecodeOptions configures decoding of a generateContent request body.

type DecodedGenerate

type DecodedGenerate struct {
	Call   *lipapi.Call
	Stream bool
	Model  string
}

DecodedGenerate is the result of decoding a generateContent JSON body.

func DecodeGenerateContentRequest

func DecodeGenerateContentRequest(body []byte, opts DecodeOptions) (*DecodedGenerate, error)

DecodeGenerateContentRequest maps a Gemini generateContent JSON body into a canonical call.

type EncodeOptions

type EncodeOptions struct {
	ExposeLipUsageExtensions bool
}

EncodeOptions controls optional encoding tweaks.

type Handler

type Handler struct {
	Exec lipsdk.ExecutorView
	// DefaultRouteSelector is used when HeaderRouteSelector is absent.
	DefaultRouteSelector string
	// RoutePrefixes are backend route-selector prefixes accepted from URL model.
	RoutePrefixes           routeselect.PrefixSet
	MaxRequestBodyBytes     int64
	Log                     *slog.Logger
	TrafficPorts            traffic.PortBundle
	DecodeAdmission         lipsdk.DecodeAdmission
	PreRequestKeepalive     lipsdk.FrontendKeepaliveConfig
	Config                  Config
	HTTPHeaders             lipsdk.HTTPHeaders
	StreamKeepaliveInterval time.Duration
	// contains filtered or unexported fields
}

Handler wires HTTP POST …/models/{model}:generateContent (and stream variant) to decode → executor → encode.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements generateContent / streamGenerateContent for the Google AI (ML dev) layout.

type RouteDescriptor

type RouteDescriptor struct {
	MountPattern string
	Claim        httpcontract.RouteClaim
}

RouteDescriptor keeps the raw ServeMux mount pattern separate from the normalized route claim used by ownership and diagnostics.

func RouteDescriptors

func RouteDescriptors(ownerID string) ([]RouteDescriptor, error)

RouteDescriptors describes the routes Mount registers for one frontend owner.

type WireErrors

type WireErrors struct{}

WireErrors implements frontendpipe.WireErrors for Gemini generateContent errors.

func (WireErrors) WriteAdmissionReject

func (WireErrors) WriteAdmissionReject(w http.ResponseWriter, d decodeqos.Decision) error

func (WireErrors) WriteBodyTooLarge

func (WireErrors) WriteBodyTooLarge(w http.ResponseWriter) error

func (WireErrors) WriteEncodeFailed

func (WireErrors) WriteEncodeFailed(w http.ResponseWriter) error

func (WireErrors) WriteExecuteError

func (WireErrors) WriteExecuteError(w http.ResponseWriter, out execerr.Outcome) error

func (WireErrors) WriteExecutorNotConfigured

func (WireErrors) WriteExecutorNotConfigured(w http.ResponseWriter) error

func (WireErrors) WriteInvalidJSON

func (WireErrors) WriteInvalidJSON(w http.ResponseWriter) error

func (WireErrors) WriteInvalidRequest

func (WireErrors) WriteInvalidRequest(w http.ResponseWriter) error

func (WireErrors) WritePreflightCanceled

func (WireErrors) WritePreflightCanceled(w http.ResponseWriter) error

func (WireErrors) WriteReadBodyFailed

func (WireErrors) WriteReadBodyFailed(w http.ResponseWriter) error

Jump to

Keyboard shortcuts

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