Documentation
¶
Overview ¶
Package gemini implements the model_protocol adapter for the Google Gemini protocol (generateContent / streamGenerateContent).
Index ¶
- func ExtractModelFromPath(path string) string
- type Adapter
- func (a *Adapter) CredentialHeader() string
- func (a *Adapter) ErrorNormalizer() utils.ErrorNormalizer
- func (a *Adapter) ErrorParser() utils.ErrorParser
- func (a *Adapter) ExtraHeaders() map[string]string
- func (a *Adapter) ExtractUsageFields(data []byte) utils.UsageFields
- func (a *Adapter) InjectAuth(outreq *bfe_http.Request, key string) error
- func (a *Adapter) IsFinalUsageEvent(ev utils.StreamEvent) bool
- func (a *Adapter) IsStreamTerminal(ev utils.StreamEvent) bool
- func (a *Adapter) Key() string
- func (a *Adapter) ParseError(statusCode int, body []byte, header http.Header) *utils.ProtocolError
- func (a *Adapter) ParseStreamError(ev utils.StreamEvent) *utils.ProtocolError
- func (a *Adapter) StreamEndsAtEOF() bool
- func (a *Adapter) StreamErrorParser() utils.StreamErrorParser
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExtractModelFromPath ¶ added in v1.8.8
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 (*Adapter) CredentialHeader ¶ added in v1.8.9
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 ¶
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 ¶
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) ParseError ¶ added in v1.8.9
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
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.