gemini

package
v1.8.9 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: Apache-2.0 Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package gemini implements the model_protocol adapter for the Google Gemini protocol (generateContent / streamGenerateContent).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractModelFromPath added in v1.8.8

func ExtractModelFromPath(path string) string

ExtractModelFromPath extracts the model name from a Gemini native request path of the form /v1beta/models/{model}[:action], where action is one of generateContent / streamGenerateContent / countTokens etc. The model name is the segment between the prefix and the first ":" (or the end of the path). It returns "" for paths that do not carry a model (issue #1384).

Types

type Adapter

type Adapter struct{}

Adapter is the Gemini protocol adapter. It is stateless.

func New

func New() *Adapter

New returns the Gemini protocol adapter.

func (*Adapter) CredentialHeader added in v1.8.9

func (a *Adapter) CredentialHeader() string

CredentialHeader returns the header InjectAuth writes, in canonical form.

func (*Adapter) ErrorNormalizer

func (a *Adapter) ErrorNormalizer() utils.ErrorNormalizer

ErrorNormalizer returns the phase-1 default normalizer (never recognizes errors, callers keep their status-code whitelist). The client-facing parser for normalization (AIConf.NormalizeUpstreamError) lives in ErrorParser in errors.go; the two seams stay decoupled on purpose.

func (*Adapter) ErrorParser added in v1.8.9

func (a *Adapter) ErrorParser() utils.ErrorParser

ErrorParser returns the Gemini error parser for client-facing normalization (AIConf.NormalizeUpstreamError).

func (*Adapter) ExtraHeaders

func (a *Adapter) ExtraHeaders() map[string]string

ExtraHeaders returns nil: the Gemini protocol needs no supplementary headers (no version header).

func (*Adapter) ExtractUsageFields

func (a *Adapter) ExtractUsageFields(data []byte) utils.UsageFields

ExtractUsageFields extracts usage fields from the top-level usageMetadata object (promptTokenCount / candidatesTokenCount / cachedContentTokenCount / totalTokenCount), normalizing them into the protocol-neutral UsageFields (see utils.ParseGeminiUsageFields).

func (*Adapter) InjectAuth

func (a *Adapter) InjectAuth(outreq *bfe_http.Request, key string) error

InjectAuth writes the upstream credential as the x-goog-api-key header. An empty key is a no-op.

func (*Adapter) IsFinalUsageEvent

func (a *Adapter) IsFinalUsageEvent(ev utils.StreamEvent) bool

IsFinalUsageEvent reports whether the event data carries a usageMetadata object. Every Gemini streaming chunk carries the accumulated usage, so this is true for (intermediate and) final chunks alike; the caller's "last event wins" accumulation guarantees the final chunk's values are the ones billed, while a chunk without usageMetadata (e.g. a bare content candidate) is never mistaken for a usage event.

func (*Adapter) IsStreamTerminal

func (a *Adapter) IsStreamTerminal(ev utils.StreamEvent) bool

IsStreamTerminal always returns false: Gemini streams (streamGenerateContent) carry no SSE termination event; the response ends at HTTP stream EOF, which the callers already handle as the completion fallback.

func (*Adapter) Key

func (a *Adapter) Key() string

func (*Adapter) ParseError added in v1.8.9

func (a *Adapter) ParseError(statusCode int, body []byte, header http.Header) *utils.ProtocolError

ParseError parses a Gemini error envelope ({"error":{code,message,status}}) into a normalized ProtocolError; the google.rpc.Status "status" enum is the primary mapping key. Returning nil means the body is not a recognizable Gemini error envelope and the caller applies its unrecognized-action.

func (*Adapter) ParseStreamError added in v1.8.9

func (a *Adapter) ParseStreamError(ev utils.StreamEvent) *utils.ProtocolError

ParseStreamError inspects one SSE event. Gemini signals in-stream errors with a chunk whose data carries an "error" object. The HTTP status of a stream is 200; the mapping is driven by the error.status enum.

func (*Adapter) StreamEndsAtEOF added in v1.8.9

func (a *Adapter) StreamEndsAtEOF() bool

StreamEndsAtEOF reports that Gemini streams (streamGenerateContent) carry no SSE termination event: the response ends at HTTP stream EOF, so the absence of a terminal event must not be reported as truncation.

func (*Adapter) StreamErrorParser added in v1.8.9

func (a *Adapter) StreamErrorParser() utils.StreamErrorParser

StreamErrorParser returns the per-event SSE error parser.

Jump to

Keyboard shortcuts

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